Skip to content

Commit 82b7f3f

Browse files
committed
dir: add generic "walk all files" helper
There is sometimes a need to visit every file within a directory, recursively. The main example is remove_dir_recursively(), though it has some extra flags that make it want to iterate over paths in a custom way. There is also the fill_directory() approach but that involves an index and a pathspec. This change adds a new for_each_file_in_dir() method that will be helpful in the next change. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 99f9681 commit 82b7f3f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

dir.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "read-cache-ll.h"
3131
#include "setup.h"
3232
#include "sparse-index.h"
33+
#include "strbuf.h"
3334
#include "submodule-config.h"
3435
#include "symlinks.h"
3536
#include "trace2.h"
@@ -87,6 +88,33 @@ struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp)
8788
return e;
8889
}
8990

91+
int for_each_file_in_dir(struct strbuf *path, file_iterator fn, const void *data)
92+
{
93+
struct dirent *e;
94+
int res = 0;
95+
size_t baselen = path->len;
96+
DIR *dir = opendir(path->buf);
97+
98+
if (!dir)
99+
return 0;
100+
101+
while (!res && (e = readdir_skip_dot_and_dotdot(dir)) != NULL) {
102+
unsigned char dtype = get_dtype(e, path, 0);
103+
strbuf_setlen(path, baselen);
104+
strbuf_addstr(path, e->d_name);
105+
106+
if (dtype == DT_REG) {
107+
res = fn(path->buf, data);
108+
} else if (dtype == DT_DIR) {
109+
strbuf_addch(path, '/');
110+
res = for_each_file_in_dir(path, fn, data);
111+
}
112+
}
113+
114+
closedir(dir);
115+
return res;
116+
}
117+
90118
int count_slashes(const char *s)
91119
{
92120
int cnt = 0;

dir.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,20 @@ int get_sparse_checkout_patterns(struct pattern_list *pl);
536536
*/
537537
int remove_dir_recursively(struct strbuf *path, int flag);
538538

539+
/*
540+
* This function pointer type is called on each file discovered in
541+
* for_each_file_in_dir. The iteration stops if this method returns
542+
* non-zero.
543+
*/
544+
typedef int (*file_iterator)(const char *path, const void *data);
545+
546+
struct strbuf;
547+
/*
548+
* Given a directory path, recursively visit each file within, including
549+
* within subdirectories.
550+
*/
551+
int for_each_file_in_dir(struct strbuf *path, file_iterator fn, const void *data);
552+
539553
/*
540554
* Tries to remove the path, along with leading empty directories so long as
541555
* those empty directories are not startup_info->original_cwd. Ignores

0 commit comments

Comments
 (0)