[RFC i-g-t 2/5] lib/igt_dir_explorer: Add function to recursively read all files in a directory

Zbigniew Kempczyński zbigniew.kempczynski at intel.com
Fri May 16 05:46:43 UTC 2025


On Thu, May 15, 2025 at 06:40:09PM +0200, Peter Senna Tschudin wrote:

<cut>

> > Just loose thought - if you're doing recursive directory traversal,
> > wouldn't be better to pass function (kind of callback) which would be
> > called for each file/dir entry? I mean single traversal code, and
> > multiple callbacks for different use cases.
> What about?
> 
> /* Callback function type for processing files. I guess we should make
>  * it blocking. Wait for the function to return before starting the next
>  * file.
>  */
> typedef int (*igt_dir_exp_file_callback)(int filefd);

I thought about:

typedef int (*handler)(const char *filename, int flags);

> 
> /* reading order */
> typedef enum {
>     SCAN_ORDER_SEQUENTIAL,
>     SCAN_ORDER_RANDOM
> } igt_dir_exp_scan_order_t;
> 
> /* Struct to hold the library interface */
> typedef struct {
>     int dirfd;                          /* File descriptor of the directory to scan */
>     igt_dir_exp_file_callback callback; /* Callback function for file operations. If null defaults
>                                          * to read and discard */
>     const char *include_pattern;        /* Pattern to match files to include (glob or regex) */
>     const char *exclude_pattern;        /* Pattern to match files to exclude (glob or regex) */
>     uint32_t concurrency;               /* Number of threads, default is 1 */
>     size_t max_depth;                   /* Maximum directory depth to scan. -1 no limit */
>     size_t max_files;                   /* Maximum number of files to process.  -1 no limit */
>     size_t max_bytes;                   /* Maximum bytes to read on each file. -1 no limit */
>     scan_order_t reading_order;         /* Reading order (sequential or random) */
> } igt_dir_exp_file_scan_config_t;
> 
> /* Function prototype for the library function */
> int igt_dir_exp_scan_directory(const igt_dir_exp_file_scan_config_t *config);
> 

void igt_dir_config_scan(struct igt_dir_scan *scan,
			 const char *include_pattern,
			 const char *exclude_pattern,
			 int max_depth);

void igt_dir_perform_scan(const struct igt_dir_scan *scan,
		          int (*handler)(const char *filename, int flags),
		          int flags);


Flags might be used to pass some additional information to the handler.
It might be void *, whatever. Even you may omit it now (there's only
single code which will use it so maybe isn't worth to extend this
too much on the beginning).

But don't get attached to names I proposed, change them to correspond
the code.

--
Zbigniew



More information about the igt-dev mailing list