Simplify read_directory[_recursive]() arguments

Stop the insanity with separate 'path' and 'base' arguments that must
match.  We don't need that crazy interface any more, since we cleaned up
handling of 'path' in commit da4b3e8c28.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Linus Torvalds 2009-07-08 19:24:39 -07:00 committed by Junio C Hamano
parent 1d8842d921
commit dba2e2037f
3 changed files with 30 additions and 31 deletions

57
dir.c
View File

@ -14,8 +14,7 @@ struct path_simplify {
const char *path; const char *path;
}; };
static int read_directory_recursive(struct dir_struct *dir, static int read_directory_recursive(struct dir_struct *dir, const char *path, int len,
const char *path, const char *base, int baselen,
int check_only, const struct path_simplify *simplify); int check_only, const struct path_simplify *simplify);
static int get_dtype(struct dirent *de, const char *path); static int get_dtype(struct dirent *de, const char *path);
@ -54,23 +53,22 @@ static int common_prefix(const char **pathspec)
int fill_directory(struct dir_struct *dir, const char **pathspec) int fill_directory(struct dir_struct *dir, const char **pathspec)
{ {
const char *path, *base; const char *path;
int baselen; int len;
/* /*
* Calculate common prefix for the pathspec, and * Calculate common prefix for the pathspec, and
* use that to optimize the directory walk * use that to optimize the directory walk
*/ */
baselen = common_prefix(pathspec); len = common_prefix(pathspec);
path = ""; path = "";
base = "";
if (baselen) if (len)
path = base = xmemdupz(*pathspec, baselen); path = xmemdupz(*pathspec, len);
/* Read the directory and prune it */ /* Read the directory and prune it */
read_directory(dir, path, base, baselen, pathspec); read_directory(dir, path, len, pathspec);
return baselen; return len;
} }
/* /*
@ -526,7 +524,7 @@ static enum directory_treatment treat_directory(struct dir_struct *dir,
/* This is the "show_other_directories" case */ /* This is the "show_other_directories" case */
if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
return show_directory; return show_directory;
if (!read_directory_recursive(dir, dirname, dirname, len, 1, simplify)) if (!read_directory_recursive(dir, dirname, len, 1, simplify))
return ignore_directory; return ignore_directory;
return show_directory; return show_directory;
} }
@ -595,15 +593,15 @@ static int get_dtype(struct dirent *de, const char *path)
* Also, we ignore the name ".git" (even if it is not a directory). * Also, we ignore the name ".git" (even if it is not a directory).
* That likely will not change. * That likely will not change.
*/ */
static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only, const struct path_simplify *simplify) static int read_directory_recursive(struct dir_struct *dir, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
{ {
DIR *fdir = opendir(*path ? path : "."); DIR *fdir = opendir(*base ? base : ".");
int contents = 0; int contents = 0;
if (fdir) { if (fdir) {
struct dirent *de; struct dirent *de;
char fullname[PATH_MAX + 1]; char path[PATH_MAX + 1];
memcpy(fullname, base, baselen); memcpy(path, base, baselen);
while ((de = readdir(fdir)) != NULL) { while ((de = readdir(fdir)) != NULL) {
int len, dtype; int len, dtype;
@ -614,17 +612,18 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
continue; continue;
len = strlen(de->d_name); len = strlen(de->d_name);
/* Ignore overly long pathnames! */ /* Ignore overly long pathnames! */
if (len + baselen + 8 > sizeof(fullname)) if (len + baselen + 8 > sizeof(path))
continue; continue;
memcpy(fullname + baselen, de->d_name, len+1); memcpy(path + baselen, de->d_name, len+1);
if (simplify_away(fullname, baselen + len, simplify)) len = baselen + len;
if (simplify_away(path, len, simplify))
continue; continue;
dtype = DTYPE(de); dtype = DTYPE(de);
exclude = excluded(dir, fullname, &dtype); exclude = excluded(dir, path, &dtype);
if (exclude && (dir->flags & DIR_COLLECT_IGNORED) if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
&& in_pathspec(fullname, baselen + len, simplify)) && in_pathspec(path, len, simplify))
dir_add_ignored(dir, fullname, baselen + len); dir_add_ignored(dir, path,len);
/* /*
* Excluded? If we don't explicitly want to show * Excluded? If we don't explicitly want to show
@ -634,7 +633,7 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
continue; continue;
if (dtype == DT_UNKNOWN) if (dtype == DT_UNKNOWN)
dtype = get_dtype(de, fullname); dtype = get_dtype(de, path);
/* /*
* Do we want to see just the ignored files? * Do we want to see just the ignored files?
@ -651,9 +650,9 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
default: default:
continue; continue;
case DT_DIR: case DT_DIR:
memcpy(fullname + baselen + len, "/", 2); memcpy(path + len, "/", 2);
len++; len++;
switch (treat_directory(dir, fullname, baselen + len, simplify)) { switch (treat_directory(dir, path, len, simplify)) {
case show_directory: case show_directory:
if (exclude != !!(dir->flags if (exclude != !!(dir->flags
& DIR_SHOW_IGNORED)) & DIR_SHOW_IGNORED))
@ -661,7 +660,7 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
break; break;
case recurse_into_directory: case recurse_into_directory:
contents += read_directory_recursive(dir, contents += read_directory_recursive(dir,
fullname, fullname, baselen + len, 0, simplify); path, len, 0, simplify);
continue; continue;
case ignore_directory: case ignore_directory:
continue; continue;
@ -675,7 +674,7 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
if (check_only) if (check_only)
goto exit_early; goto exit_early;
else else
dir_add_name(dir, fullname, baselen + len); dir_add_name(dir, path, len);
} }
exit_early: exit_early:
closedir(fdir); closedir(fdir);
@ -738,15 +737,15 @@ static void free_simplify(struct path_simplify *simplify)
free(simplify); free(simplify);
} }
int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec) int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
{ {
struct path_simplify *simplify; struct path_simplify *simplify;
if (has_symlink_leading_path(path, strlen(path))) if (has_symlink_leading_path(path, len))
return dir->nr; return dir->nr;
simplify = create_simplify(pathspec); simplify = create_simplify(pathspec);
read_directory_recursive(dir, path, base, baselen, 0, simplify); read_directory_recursive(dir, path, len, 0, simplify);
free_simplify(simplify); free_simplify(simplify);
qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name); qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name); qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);

2
dir.h
View File

@ -67,7 +67,7 @@ struct dir_struct {
extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen); extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
extern int fill_directory(struct dir_struct *dir, const char **pathspec); extern int fill_directory(struct dir_struct *dir, const char **pathspec);
extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen, const char **pathspec); extern int read_directory(struct dir_struct *, const char *path, int len, const char **pathspec);
extern int excluded(struct dir_struct *, const char *, int *); extern int excluded(struct dir_struct *, const char *, int *);
extern void add_excludes_from_file(struct dir_struct *, const char *fname); extern void add_excludes_from_file(struct dir_struct *, const char *fname);

View File

@ -551,7 +551,7 @@ static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
memset(&d, 0, sizeof(d)); memset(&d, 0, sizeof(d));
if (o->dir) if (o->dir)
d.exclude_per_dir = o->dir->exclude_per_dir; d.exclude_per_dir = o->dir->exclude_per_dir;
i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL); i = read_directory(&d, pathbuf, namelen+1, NULL);
if (i) if (i)
return o->gently ? -1 : return o->gently ? -1 :
error(ERRORMSG(o, not_uptodate_dir), ce->name); error(ERRORMSG(o, not_uptodate_dir), ce->name);