Merge branch 'rs/dir-strbuf'

By René Scharfe
* rs/dir-strbuf:
  dir: convert to strbuf
This commit is contained in:
Junio C Hamano 2012-05-10 10:49:55 -07:00
commit 8a3500cd21

76
dir.c
View File

@ -873,14 +873,14 @@ enum path_treatment {
}; };
static enum path_treatment treat_one_path(struct dir_struct *dir, static enum path_treatment treat_one_path(struct dir_struct *dir,
char *path, int *len, struct strbuf *path,
const struct path_simplify *simplify, const struct path_simplify *simplify,
int dtype, struct dirent *de) int dtype, struct dirent *de)
{ {
int exclude = excluded(dir, path, &dtype); int exclude = excluded(dir, path->buf, &dtype);
if (exclude && (dir->flags & DIR_COLLECT_IGNORED) if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
&& exclude_matches_pathspec(path, *len, simplify)) && exclude_matches_pathspec(path->buf, path->len, simplify))
dir_add_ignored(dir, path, *len); dir_add_ignored(dir, path->buf, path->len);
/* /*
* Excluded? If we don't explicitly want to show * Excluded? If we don't explicitly want to show
@ -890,7 +890,7 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,
return path_ignored; return path_ignored;
if (dtype == DT_UNKNOWN) if (dtype == DT_UNKNOWN)
dtype = get_dtype(de, path, *len); dtype = get_dtype(de, path->buf, path->len);
/* /*
* Do we want to see just the ignored files? * Do we want to see just the ignored files?
@ -907,9 +907,8 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,
default: default:
return path_ignored; return path_ignored;
case DT_DIR: case DT_DIR:
memcpy(path + *len, "/", 2); strbuf_addch(path, '/');
(*len)++; switch (treat_directory(dir, path->buf, path->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))
@ -930,26 +929,21 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,
static enum path_treatment treat_path(struct dir_struct *dir, static enum path_treatment treat_path(struct dir_struct *dir,
struct dirent *de, struct dirent *de,
char *path, int path_max, struct strbuf *path,
int baselen, int baselen,
const struct path_simplify *simplify, const struct path_simplify *simplify)
int *len)
{ {
int dtype; int dtype;
if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git")) if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
return path_ignored; return path_ignored;
*len = strlen(de->d_name); strbuf_setlen(path, baselen);
/* Ignore overly long pathnames! */ strbuf_addstr(path, de->d_name);
if (*len + baselen + 8 > path_max) if (simplify_away(path->buf, path->len, simplify))
return path_ignored;
memcpy(path + baselen, de->d_name, *len + 1);
*len += baselen;
if (simplify_away(path, *len, simplify))
return path_ignored; return path_ignored;
dtype = DTYPE(de); dtype = DTYPE(de);
return treat_one_path(dir, path, len, simplify, dtype, de); return treat_one_path(dir, path, simplify, dtype, de);
} }
/* /*
@ -969,19 +963,19 @@ static int read_directory_recursive(struct dir_struct *dir,
DIR *fdir = opendir(*base ? base : "."); DIR *fdir = opendir(*base ? base : ".");
int contents = 0; int contents = 0;
struct dirent *de; struct dirent *de;
char path[PATH_MAX + 1]; struct strbuf path = STRBUF_INIT;
if (!fdir) if (!fdir)
return 0; return 0;
memcpy(path, base, baselen); strbuf_add(&path, base, baselen);
while ((de = readdir(fdir)) != NULL) { while ((de = readdir(fdir)) != NULL) {
int len; switch (treat_path(dir, de, &path, baselen, simplify)) {
switch (treat_path(dir, de, path, sizeof(path),
baselen, simplify, &len)) {
case path_recurse: case path_recurse:
contents += read_directory_recursive(dir, path, len, 0, simplify); contents += read_directory_recursive(dir, path.buf,
path.len, 0,
simplify);
continue; continue;
case path_ignored: case path_ignored:
continue; continue;
@ -992,10 +986,11 @@ static int read_directory_recursive(struct dir_struct *dir,
if (check_only) if (check_only)
goto exit_early; goto exit_early;
else else
dir_add_name(dir, path, len); dir_add_name(dir, path.buf, path.len);
} }
exit_early: exit_early:
closedir(fdir); closedir(fdir);
strbuf_release(&path);
return contents; return contents;
} }
@ -1058,8 +1053,8 @@ static int treat_leading_path(struct dir_struct *dir,
const char *path, int len, const char *path, int len,
const struct path_simplify *simplify) const struct path_simplify *simplify)
{ {
char pathbuf[PATH_MAX]; struct strbuf sb = STRBUF_INIT;
int baselen, blen; int baselen, rc = 0;
const char *cp; const char *cp;
while (len && path[len - 1] == '/') while (len && path[len - 1] == '/')
@ -1074,19 +1069,22 @@ static int treat_leading_path(struct dir_struct *dir,
baselen = len; baselen = len;
else else
baselen = cp - path; baselen = cp - path;
memcpy(pathbuf, path, baselen); strbuf_setlen(&sb, 0);
pathbuf[baselen] = '\0'; strbuf_add(&sb, path, baselen);
if (!is_directory(pathbuf)) if (!is_directory(sb.buf))
return 0; break;
if (simplify_away(pathbuf, baselen, simplify)) if (simplify_away(sb.buf, sb.len, simplify))
return 0; break;
blen = baselen; if (treat_one_path(dir, &sb, simplify,
if (treat_one_path(dir, pathbuf, &blen, simplify,
DT_DIR, NULL) == path_ignored) DT_DIR, NULL) == path_ignored)
return 0; /* do not recurse into it */ break; /* do not recurse into it */
if (len <= baselen) if (len <= baselen) {
return 1; /* finished checking */ rc = 1;
break; /* finished checking */
}
} }
strbuf_release(&sb);
return rc;
} }
int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec) int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)