Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Directory Walk

Recursive directory traversal with filters for hidden files, regular files vs. directories, and user-supplied callbacks.

Source code:

get_ordered_filename_array()

Retrieves an ordered list of filenames from a specified directory, optionally filtered by a pattern and search options.

int get_ordered_filename_array(
    hgobj gobj,
    const char *root_dir,
    const char *re,
    wd_option opt,
    dir_array_t *da
);

Parameters

KeyTypeDescription
gobjhgobjA handle to the GObj instance, used for logging and error reporting.
root_dirconst char *The root directory from which to retrieve filenames.
reconst char *A regex pattern to filter filenames. If NULL, all files are included.
optwd_optionOptions for directory traversal, such as recursion and file type filtering.
dadir_array_t *Pointer to a dir_array_t structure that will receive the results.

Returns

Returns 0 on success, or a negative value on error. Results are stored in the da structure. Free with dir_array_free().

Notes

This function uses qsort() to sort the filenames. The returned array must be freed properly to avoid memory leaks.


walk_dir_tree()

The walk_dir_tree() function traverses a directory tree starting from root_dir, applying a user-defined callback function cb to each file or directory that matches the specified pattern and opt options.

int walk_dir_tree(
    hgobj gobj,
    const char *root_dir,
    const char *pattern,
    wd_option opt,
    walkdir_cb cb,
    void *user_data
);

Parameters

KeyTypeDescription
gobjhgobjA handle to the Yuneta framework object, used for logging and error handling.
root_dirconst char *The root directory from which the traversal begins.
patternconst char *A regular expression pattern to match file or directory names.
optwd_optionOptions controlling the traversal behavior, such as recursion, hidden file inclusion, and file type matching.
cbwalkdir_cbA callback function that is invoked for each matching file or directory.
user_datavoid *A user-defined pointer passed to the callback function.

Returns

Returns 0 on success, or -1 if an error occurs (e.g., if root_dir does not exist or pattern is invalid).

Notes

The callback function cb should return TRUE to continue traversal or FALSE to stop. The function uses regcomp() to compile the pattern and regexec() to match file names.


dir_array_free()

Frees all memory associated with a directory array structure.

void dir_array_free(
    dir_array_t *da
);

Parameters

KeyTypeDescription
dadir_array_t *Pointer to the directory array structure to free.

Returns

This function does not return a value.


dir_array_sort()

Sorts the filenames in a directory array in lexicographic order using qsort().

void dir_array_sort(
    dir_array_t *da
);

Parameters

KeyTypeDescription
dadir_array_t *Pointer to the directory array structure to sort.

Returns

This function does not return a value.


find_files_with_suffix_array()

Finds all regular files in a directory with a given suffix.

int find_files_with_suffix_array(
    hgobj gobj,
    const char *directory,
    const char *suffix,
    dir_array_t *da
);

Parameters

KeyTypeDescription
gobjhgobjA handle to the GObj instance, used for logging and error reporting.
directoryconst char *The directory to search in.
suffixconst char *The file suffix to match (e.g., ".json").
dadir_array_t *Pointer to a dir_array_t structure that will receive the results.

Returns

Returns 0 on success, or -1 on error.


walk_dir_array()

Recursively traverses a directory tree and populates an array with paths matching a regex pattern.

int walk_dir_array(
    hgobj gobj,
    const char *root_dir,
    const char *re,
    wd_option opt,
    dir_array_t *da
);

Parameters

KeyTypeDescription
gobjhgobjA handle to the GObj instance, used for logging and error reporting.
root_dirconst char *The root directory from which the traversal begins.
reconst char *A regex pattern to filter filenames. If NULL, all entries are included.
optwd_optionOptions controlling the traversal behavior, such as recursion and file type filtering.
dadir_array_t *Pointer to a dir_array_t structure that will receive the results.

Returns

Returns 0 on success, or -1 on error.