Skip to content

Commit 153776a

Browse files
committed
FIO: Add support to expand filename/path to a list of filenames
1 parent a444923 commit 153776a

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

sim_fio.c

+60-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@
5454
sim_byte_swap_data - swap data elements inplace in buffer
5555
sim_shmem_open create or attach to a shared memory region
5656
sim_shmem_close close a shared memory region
57-
57+
sim_chdir change working directory
58+
sim_mkdir create a directory
59+
sim_rmdir remove a directory
60+
sim_getcwd get the current working directory
61+
sim_copyfile copy a file
62+
sim_filepath_parts expand and extract filename/path parts
63+
sim_dirscan scan for a filename pattern
64+
sim_get_filelist get a list of files matching a pattern
65+
sim_free_filelist free a filelist
66+
sim_print_filelist print the elements of a filelist
5867
5968
sim_fopen and sim_fseek are OS-dependent. The other routines are not.
6069
sim_fsize is always a 32b routine (it is used only with small capacity random
@@ -347,6 +356,55 @@ if (NULL == _sim_expand_homedir (path, pathbuf, sizeof (pathbuf)))
347356
return rmdir (pathbuf);
348357
}
349358

359+
static void _sim_filelist_entry (const char *directory,
360+
const char *filename,
361+
t_offset FileSize,
362+
const struct stat *filestat,
363+
void *context)
364+
{
365+
char **filelist = *(char ***)context;
366+
char FullPath[PATH_MAX + 1];
367+
int listcount = 0;
368+
369+
snprintf (FullPath, sizeof (FullPath), "%s%s", directory, filename);
370+
if (filelist != NULL) {
371+
while (filelist[listcount++] != NULL);
372+
--listcount;
373+
}
374+
filelist = (char **)realloc (filelist, (listcount + 2) * sizeof (*filelist));
375+
filelist[listcount] = strdup (FullPath);
376+
filelist[listcount + 1] = NULL;
377+
*(char ***)context = filelist;
378+
}
379+
380+
char **sim_get_filelist (const char *filename)
381+
{
382+
char **filelist = NULL;
383+
384+
sim_dir_scan (filename, _sim_filelist_entry, &filelist);
385+
return filelist;
386+
}
387+
388+
void sim_free_filelist (char ***pfilelist)
389+
{
390+
char **listp = *pfilelist;
391+
392+
if (listp == NULL)
393+
return;
394+
while (*listp != NULL)
395+
free (*listp++);
396+
free (*pfilelist);
397+
*pfilelist = NULL;
398+
}
399+
400+
void sim_print_filelist (char **filelist)
401+
{
402+
if (filelist == NULL)
403+
return;
404+
while (*filelist != NULL)
405+
sim_printf ("%s\n", *filelist++);
406+
}
407+
350408

351409
/* OS-dependent routines */
352410

@@ -913,7 +971,7 @@ return getcwd (buf, buf_size);
913971
*
914972
* In the above example above %I% can be replaced by other
915973
* environment variables or numeric parameters to a DO command
916-
* invokation.
974+
* invocation.
917975
*/
918976

919977
char *sim_filepath_parts (const char *filepath, const char *parts)

sim_fio.h

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ typedef void (*DIR_ENTRY_CALLBACK)(const char *directory,
8787
const struct stat *filestat,
8888
void *context);
8989
t_stat sim_dir_scan (const char *cptr, DIR_ENTRY_CALLBACK entry, void *context);
90+
char **sim_get_filelist (const char *filename);
91+
void sim_free_filelist (char ***pfilelist);
92+
void sim_print_filelist (char **filelist);
9093

9194
void sim_buf_swap_data (void *bptr, size_t size, size_t count);
9295
void sim_byte_swap_data (void *bptr, size_t size, size_t count);

0 commit comments

Comments
 (0)