Skip to content

Commit

Permalink
Merge branch 'rs/dir-strbuf-read-recursive-fix'
Browse files Browse the repository at this point in the history
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()
  • Loading branch information
gitster committed May 25, 2012
2 parents b19ea23 + 2b18943 commit 3501b89
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,16 @@ char *common_prefix(const char **pathspec)

int fill_directory(struct dir_struct *dir, const char **pathspec)
{
const char *path;
size_t len;

/*
* Calculate common prefix for the pathspec, and
* use that to optimize the directory walk
*/
len = common_prefix_len(pathspec);
path = "";

if (len)
path = xmemdupz(*pathspec, len);

/* Read the directory and prune it */
read_directory(dir, path, len, pathspec);
if (*path)
free((char *)path);
read_directory(dir, pathspec ? *pathspec : "", len, pathspec);
return len;
}

Expand Down Expand Up @@ -960,16 +953,17 @@ static int read_directory_recursive(struct dir_struct *dir,
int check_only,
const struct path_simplify *simplify)
{
DIR *fdir = opendir(*base ? base : ".");
DIR *fdir;
int contents = 0;
struct dirent *de;
struct strbuf path = STRBUF_INIT;

if (!fdir)
return 0;

strbuf_add(&path, base, baselen);

fdir = opendir(path.len ? path.buf : ".");
if (!fdir)
goto out;

while ((de = readdir(fdir)) != NULL) {
switch (treat_path(dir, de, &path, baselen, simplify)) {
case path_recurse:
Expand All @@ -984,12 +978,11 @@ static int read_directory_recursive(struct dir_struct *dir,
}
contents++;
if (check_only)
goto exit_early;
else
dir_add_name(dir, path.buf, path.len);
break;
dir_add_name(dir, path.buf, path.len);
}
exit_early:
closedir(fdir);
out:
strbuf_release(&path);

return contents;
Expand Down

0 comments on commit 3501b89

Please sign in to comment.