dir: introduce readdir_skip_dot_and_dotdot() helper
Many places in the code were doing while ((d = readdir(dir)) != NULL) { if (is_dot_or_dotdot(d->d_name)) continue; ...process d... } Introduce a readdir_skip_dot_and_dotdot() helper to make that a one-liner: while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) { ...process d... } This helper particularly simplifies checks for empty directories. Also use this helper in read_cached_dir() so that our statistics are consistent across platforms. (In other words, read_cached_dir() should have been using is_dot_or_dotdot() and skipping such entries, but did not and left it to treat_path() to detect and mark such entries as path_none.) Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
eef814828f
commit
906fc557b7
@ -189,10 +189,8 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
|
|||||||
strbuf_complete(path, '/');
|
strbuf_complete(path, '/');
|
||||||
|
|
||||||
len = path->len;
|
len = path->len;
|
||||||
while ((e = readdir(dir)) != NULL) {
|
while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) {
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (is_dot_or_dotdot(e->d_name))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
strbuf_setlen(path, len);
|
strbuf_setlen(path, len);
|
||||||
strbuf_addstr(path, e->d_name);
|
strbuf_addstr(path, e->d_name);
|
||||||
|
@ -118,10 +118,8 @@ static void prune_worktrees(void)
|
|||||||
struct dirent *d;
|
struct dirent *d;
|
||||||
if (!dir)
|
if (!dir)
|
||||||
return;
|
return;
|
||||||
while ((d = readdir(dir)) != NULL) {
|
while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {
|
||||||
char *path;
|
char *path;
|
||||||
if (is_dot_or_dotdot(d->d_name))
|
|
||||||
continue;
|
|
||||||
strbuf_reset(&reason);
|
strbuf_reset(&reason);
|
||||||
if (should_prune_worktree(d->d_name, &reason, &path, expire))
|
if (should_prune_worktree(d->d_name, &reason, &path, expire))
|
||||||
prune_worktree(d->d_name, reason.buf);
|
prune_worktree(d->d_name, reason.buf);
|
||||||
|
@ -26,9 +26,8 @@ static int read_directory_contents(const char *path, struct string_list *list)
|
|||||||
if (!(dir = opendir(path)))
|
if (!(dir = opendir(path)))
|
||||||
return error("Could not open directory %s", path);
|
return error("Could not open directory %s", path);
|
||||||
|
|
||||||
while ((e = readdir(dir)))
|
while ((e = readdir_skip_dot_and_dotdot(dir)))
|
||||||
if (!is_dot_or_dotdot(e->d_name))
|
string_list_insert(list, e->d_name);
|
||||||
string_list_insert(list, e->d_name);
|
|
||||||
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
return 0;
|
return 0;
|
||||||
|
25
dir.c
25
dir.c
@ -59,6 +59,17 @@ void dir_init(struct dir_struct *dir)
|
|||||||
memset(dir, 0, sizeof(*dir));
|
memset(dir, 0, sizeof(*dir));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp)
|
||||||
|
{
|
||||||
|
struct dirent *e;
|
||||||
|
|
||||||
|
while ((e = readdir(dirp)) != NULL) {
|
||||||
|
if (!is_dot_or_dotdot(e->d_name))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
int count_slashes(const char *s)
|
int count_slashes(const char *s)
|
||||||
{
|
{
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
@ -2332,7 +2343,7 @@ static int read_cached_dir(struct cached_dir *cdir)
|
|||||||
struct dirent *de;
|
struct dirent *de;
|
||||||
|
|
||||||
if (cdir->fdir) {
|
if (cdir->fdir) {
|
||||||
de = readdir(cdir->fdir);
|
de = readdir_skip_dot_and_dotdot(cdir->fdir);
|
||||||
if (!de) {
|
if (!de) {
|
||||||
cdir->d_name = NULL;
|
cdir->d_name = NULL;
|
||||||
cdir->d_type = DT_UNKNOWN;
|
cdir->d_type = DT_UNKNOWN;
|
||||||
@ -2931,11 +2942,9 @@ int is_empty_dir(const char *path)
|
|||||||
if (!dir)
|
if (!dir)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
while ((e = readdir(dir)) != NULL)
|
e = readdir_skip_dot_and_dotdot(dir);
|
||||||
if (!is_dot_or_dotdot(e->d_name)) {
|
if (e)
|
||||||
ret = 0;
|
ret = 0;
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
return ret;
|
return ret;
|
||||||
@ -2975,10 +2984,8 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
|
|||||||
strbuf_complete(path, '/');
|
strbuf_complete(path, '/');
|
||||||
|
|
||||||
len = path->len;
|
len = path->len;
|
||||||
while ((e = readdir(dir)) != NULL) {
|
while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) {
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (is_dot_or_dotdot(e->d_name))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
strbuf_setlen(path, len);
|
strbuf_setlen(path, len);
|
||||||
strbuf_addstr(path, e->d_name);
|
strbuf_addstr(path, e->d_name);
|
||||||
|
2
dir.h
2
dir.h
@ -342,6 +342,8 @@ struct dir_struct {
|
|||||||
unsigned visited_directories;
|
unsigned visited_directories;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);
|
||||||
|
|
||||||
/*Count the number of slashes for string s*/
|
/*Count the number of slashes for string s*/
|
||||||
int count_slashes(const char *s);
|
int count_slashes(const char *s);
|
||||||
|
|
||||||
|
5
entry.c
5
entry.c
@ -56,12 +56,9 @@ static void remove_subtree(struct strbuf *path)
|
|||||||
|
|
||||||
if (!dir)
|
if (!dir)
|
||||||
die_errno("cannot opendir '%s'", path->buf);
|
die_errno("cannot opendir '%s'", path->buf);
|
||||||
while ((de = readdir(dir)) != NULL) {
|
while ((de = readdir_skip_dot_and_dotdot(dir)) != NULL) {
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
if (is_dot_or_dotdot(de->d_name))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
strbuf_addch(path, '/');
|
strbuf_addch(path, '/');
|
||||||
strbuf_addstr(path, de->d_name);
|
strbuf_addstr(path, de->d_name);
|
||||||
if (lstat(path->buf, &st))
|
if (lstat(path->buf, &st))
|
||||||
|
@ -695,13 +695,10 @@ int notes_merge_commit(struct notes_merge_options *o,
|
|||||||
|
|
||||||
strbuf_addch(&path, '/');
|
strbuf_addch(&path, '/');
|
||||||
baselen = path.len;
|
baselen = path.len;
|
||||||
while ((e = readdir(dir)) != NULL) {
|
while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) {
|
||||||
struct stat st;
|
struct stat st;
|
||||||
struct object_id obj_oid, blob_oid;
|
struct object_id obj_oid, blob_oid;
|
||||||
|
|
||||||
if (is_dot_or_dotdot(e->d_name))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (get_oid_hex(e->d_name, &obj_oid)) {
|
if (get_oid_hex(e->d_name, &obj_oid)) {
|
||||||
if (o->verbosity >= 3)
|
if (o->verbosity >= 3)
|
||||||
printf("Skipping non-SHA1 entry '%s%s'\n",
|
printf("Skipping non-SHA1 entry '%s%s'\n",
|
||||||
|
@ -2304,10 +2304,8 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
|
|||||||
strbuf_addch(path, '/');
|
strbuf_addch(path, '/');
|
||||||
baselen = path->len;
|
baselen = path->len;
|
||||||
|
|
||||||
while ((de = readdir(dir))) {
|
while ((de = readdir_skip_dot_and_dotdot(dir))) {
|
||||||
size_t namelen;
|
size_t namelen;
|
||||||
if (is_dot_or_dotdot(de->d_name))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
namelen = strlen(de->d_name);
|
namelen = strlen(de->d_name);
|
||||||
strbuf_setlen(path, baselen);
|
strbuf_setlen(path, baselen);
|
||||||
|
@ -813,10 +813,7 @@ void for_each_file_in_pack_dir(const char *objdir,
|
|||||||
}
|
}
|
||||||
strbuf_addch(&path, '/');
|
strbuf_addch(&path, '/');
|
||||||
dirnamelen = path.len;
|
dirnamelen = path.len;
|
||||||
while ((de = readdir(dir)) != NULL) {
|
while ((de = readdir_skip_dot_and_dotdot(dir)) != NULL) {
|
||||||
if (is_dot_or_dotdot(de->d_name))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
strbuf_setlen(&path, dirnamelen);
|
strbuf_setlen(&path, dirnamelen);
|
||||||
strbuf_addstr(&path, de->d_name);
|
strbuf_addstr(&path, de->d_name);
|
||||||
|
|
||||||
|
4
rerere.c
4
rerere.c
@ -1190,13 +1190,11 @@ void rerere_gc(struct repository *r, struct string_list *rr)
|
|||||||
if (!dir)
|
if (!dir)
|
||||||
die_errno(_("unable to open rr-cache directory"));
|
die_errno(_("unable to open rr-cache directory"));
|
||||||
/* Collect stale conflict IDs ... */
|
/* Collect stale conflict IDs ... */
|
||||||
while ((e = readdir(dir))) {
|
while ((e = readdir_skip_dot_and_dotdot(dir))) {
|
||||||
struct rerere_dir *rr_dir;
|
struct rerere_dir *rr_dir;
|
||||||
struct rerere_id id;
|
struct rerere_id id;
|
||||||
int now_empty;
|
int now_empty;
|
||||||
|
|
||||||
if (is_dot_or_dotdot(e->d_name))
|
|
||||||
continue;
|
|
||||||
if (!is_rr_cache_dirname(e->d_name))
|
if (!is_rr_cache_dirname(e->d_name))
|
||||||
continue; /* or should we remove e->d_name? */
|
continue; /* or should we remove e->d_name? */
|
||||||
|
|
||||||
|
12
worktree.c
12
worktree.c
@ -128,10 +128,8 @@ struct worktree **get_worktrees(void)
|
|||||||
dir = opendir(path.buf);
|
dir = opendir(path.buf);
|
||||||
strbuf_release(&path);
|
strbuf_release(&path);
|
||||||
if (dir) {
|
if (dir) {
|
||||||
while ((d = readdir(dir)) != NULL) {
|
while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {
|
||||||
struct worktree *linked = NULL;
|
struct worktree *linked = NULL;
|
||||||
if (is_dot_or_dotdot(d->d_name))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if ((linked = get_linked_worktree(d->d_name))) {
|
if ((linked = get_linked_worktree(d->d_name))) {
|
||||||
ALLOC_GROW(list, counter + 1, alloc);
|
ALLOC_GROW(list, counter + 1, alloc);
|
||||||
@ -486,13 +484,9 @@ int submodule_uses_worktrees(const char *path)
|
|||||||
if (!dir)
|
if (!dir)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
while ((d = readdir(dir)) != NULL) {
|
d = readdir_skip_dot_and_dotdot(dir);
|
||||||
if (is_dot_or_dotdot(d->d_name))
|
if (d != NULL)
|
||||||
continue;
|
|
||||||
|
|
||||||
ret = 1;
|
ret = 1;
|
||||||
break;
|
|
||||||
}
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user