Skip to content

Commit 3501b89

Browse files
committed
Merge branch 'rs/dir-strbuf-read-recursive-fix'
Simplification for the codepath to read directories recursively. By René Scharfe * rs/dir-strbuf-read-recursive-fix: dir: simplify fill_directory() dir: respect string length argument of read_directory_recursive()
2 parents b19ea23 + 2b18943 commit 3501b89

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

dir.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,16 @@ char *common_prefix(const char **pathspec)
7474

7575
int fill_directory(struct dir_struct *dir, const char **pathspec)
7676
{
77-
const char *path;
7877
size_t len;
7978

8079
/*
8180
* Calculate common prefix for the pathspec, and
8281
* use that to optimize the directory walk
8382
*/
8483
len = common_prefix_len(pathspec);
85-
path = "";
86-
87-
if (len)
88-
path = xmemdupz(*pathspec, len);
8984

9085
/* Read the directory and prune it */
91-
read_directory(dir, path, len, pathspec);
92-
if (*path)
93-
free((char *)path);
86+
read_directory(dir, pathspec ? *pathspec : "", len, pathspec);
9487
return len;
9588
}
9689

@@ -960,16 +953,17 @@ static int read_directory_recursive(struct dir_struct *dir,
960953
int check_only,
961954
const struct path_simplify *simplify)
962955
{
963-
DIR *fdir = opendir(*base ? base : ".");
956+
DIR *fdir;
964957
int contents = 0;
965958
struct dirent *de;
966959
struct strbuf path = STRBUF_INIT;
967960

968-
if (!fdir)
969-
return 0;
970-
971961
strbuf_add(&path, base, baselen);
972962

963+
fdir = opendir(path.len ? path.buf : ".");
964+
if (!fdir)
965+
goto out;
966+
973967
while ((de = readdir(fdir)) != NULL) {
974968
switch (treat_path(dir, de, &path, baselen, simplify)) {
975969
case path_recurse:
@@ -984,12 +978,11 @@ static int read_directory_recursive(struct dir_struct *dir,
984978
}
985979
contents++;
986980
if (check_only)
987-
goto exit_early;
988-
else
989-
dir_add_name(dir, path.buf, path.len);
981+
break;
982+
dir_add_name(dir, path.buf, path.len);
990983
}
991-
exit_early:
992984
closedir(fdir);
985+
out:
993986
strbuf_release(&path);
994987

995988
return contents;

0 commit comments

Comments
 (0)