Skip to content

Commit 906fc55

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent eef8148 commit 906fc55

File tree

11 files changed

+30
-45
lines changed

11 files changed

+30
-45
lines changed

builtin/clean.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,8 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
189189
strbuf_complete(path, '/');
190190

191191
len = path->len;
192-
while ((e = readdir(dir)) != NULL) {
192+
while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) {
193193
struct stat st;
194-
if (is_dot_or_dotdot(e->d_name))
195-
continue;
196194

197195
strbuf_setlen(path, len);
198196
strbuf_addstr(path, e->d_name);

builtin/worktree.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,8 @@ static void prune_worktrees(void)
118118
struct dirent *d;
119119
if (!dir)
120120
return;
121-
while ((d = readdir(dir)) != NULL) {
121+
while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {
122122
char *path;
123-
if (is_dot_or_dotdot(d->d_name))
124-
continue;
125123
strbuf_reset(&reason);
126124
if (should_prune_worktree(d->d_name, &reason, &path, expire))
127125
prune_worktree(d->d_name, reason.buf);

diff-no-index.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ static int read_directory_contents(const char *path, struct string_list *list)
2626
if (!(dir = opendir(path)))
2727
return error("Could not open directory %s", path);
2828

29-
while ((e = readdir(dir)))
30-
if (!is_dot_or_dotdot(e->d_name))
31-
string_list_insert(list, e->d_name);
29+
while ((e = readdir_skip_dot_and_dotdot(dir)))
30+
string_list_insert(list, e->d_name);
3231

3332
closedir(dir);
3433
return 0;

dir.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ void dir_init(struct dir_struct *dir)
5959
memset(dir, 0, sizeof(*dir));
6060
}
6161

62+
struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp)
63+
{
64+
struct dirent *e;
65+
66+
while ((e = readdir(dirp)) != NULL) {
67+
if (!is_dot_or_dotdot(e->d_name))
68+
break;
69+
}
70+
return e;
71+
}
72+
6273
int count_slashes(const char *s)
6374
{
6475
int cnt = 0;
@@ -2332,7 +2343,7 @@ static int read_cached_dir(struct cached_dir *cdir)
23322343
struct dirent *de;
23332344

23342345
if (cdir->fdir) {
2335-
de = readdir(cdir->fdir);
2346+
de = readdir_skip_dot_and_dotdot(cdir->fdir);
23362347
if (!de) {
23372348
cdir->d_name = NULL;
23382349
cdir->d_type = DT_UNKNOWN;
@@ -2931,11 +2942,9 @@ int is_empty_dir(const char *path)
29312942
if (!dir)
29322943
return 0;
29332944

2934-
while ((e = readdir(dir)) != NULL)
2935-
if (!is_dot_or_dotdot(e->d_name)) {
2936-
ret = 0;
2937-
break;
2938-
}
2945+
e = readdir_skip_dot_and_dotdot(dir);
2946+
if (e)
2947+
ret = 0;
29392948

29402949
closedir(dir);
29412950
return ret;
@@ -2975,10 +2984,8 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
29752984
strbuf_complete(path, '/');
29762985

29772986
len = path->len;
2978-
while ((e = readdir(dir)) != NULL) {
2987+
while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) {
29792988
struct stat st;
2980-
if (is_dot_or_dotdot(e->d_name))
2981-
continue;
29822989

29832990
strbuf_setlen(path, len);
29842991
strbuf_addstr(path, e->d_name);

dir.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ struct dir_struct {
342342
unsigned visited_directories;
343343
};
344344

345+
struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);
346+
345347
/*Count the number of slashes for string s*/
346348
int count_slashes(const char *s);
347349

entry.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,9 @@ static void remove_subtree(struct strbuf *path)
5656

5757
if (!dir)
5858
die_errno("cannot opendir '%s'", path->buf);
59-
while ((de = readdir(dir)) != NULL) {
59+
while ((de = readdir_skip_dot_and_dotdot(dir)) != NULL) {
6060
struct stat st;
6161

62-
if (is_dot_or_dotdot(de->d_name))
63-
continue;
64-
6562
strbuf_addch(path, '/');
6663
strbuf_addstr(path, de->d_name);
6764
if (lstat(path->buf, &st))

notes-merge.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,13 +695,10 @@ int notes_merge_commit(struct notes_merge_options *o,
695695

696696
strbuf_addch(&path, '/');
697697
baselen = path.len;
698-
while ((e = readdir(dir)) != NULL) {
698+
while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) {
699699
struct stat st;
700700
struct object_id obj_oid, blob_oid;
701701

702-
if (is_dot_or_dotdot(e->d_name))
703-
continue;
704-
705702
if (get_oid_hex(e->d_name, &obj_oid)) {
706703
if (o->verbosity >= 3)
707704
printf("Skipping non-SHA1 entry '%s%s'\n",

object-file.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,10 +2304,8 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
23042304
strbuf_addch(path, '/');
23052305
baselen = path->len;
23062306

2307-
while ((de = readdir(dir))) {
2307+
while ((de = readdir_skip_dot_and_dotdot(dir))) {
23082308
size_t namelen;
2309-
if (is_dot_or_dotdot(de->d_name))
2310-
continue;
23112309

23122310
namelen = strlen(de->d_name);
23132311
strbuf_setlen(path, baselen);

packfile.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -813,10 +813,7 @@ void for_each_file_in_pack_dir(const char *objdir,
813813
}
814814
strbuf_addch(&path, '/');
815815
dirnamelen = path.len;
816-
while ((de = readdir(dir)) != NULL) {
817-
if (is_dot_or_dotdot(de->d_name))
818-
continue;
819-
816+
while ((de = readdir_skip_dot_and_dotdot(dir)) != NULL) {
820817
strbuf_setlen(&path, dirnamelen);
821818
strbuf_addstr(&path, de->d_name);
822819

rerere.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,13 +1190,11 @@ void rerere_gc(struct repository *r, struct string_list *rr)
11901190
if (!dir)
11911191
die_errno(_("unable to open rr-cache directory"));
11921192
/* Collect stale conflict IDs ... */
1193-
while ((e = readdir(dir))) {
1193+
while ((e = readdir_skip_dot_and_dotdot(dir))) {
11941194
struct rerere_dir *rr_dir;
11951195
struct rerere_id id;
11961196
int now_empty;
11971197

1198-
if (is_dot_or_dotdot(e->d_name))
1199-
continue;
12001198
if (!is_rr_cache_dirname(e->d_name))
12011199
continue; /* or should we remove e->d_name? */
12021200

worktree.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,8 @@ struct worktree **get_worktrees(void)
128128
dir = opendir(path.buf);
129129
strbuf_release(&path);
130130
if (dir) {
131-
while ((d = readdir(dir)) != NULL) {
131+
while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {
132132
struct worktree *linked = NULL;
133-
if (is_dot_or_dotdot(d->d_name))
134-
continue;
135133

136134
if ((linked = get_linked_worktree(d->d_name))) {
137135
ALLOC_GROW(list, counter + 1, alloc);
@@ -486,13 +484,9 @@ int submodule_uses_worktrees(const char *path)
486484
if (!dir)
487485
return 0;
488486

489-
while ((d = readdir(dir)) != NULL) {
490-
if (is_dot_or_dotdot(d->d_name))
491-
continue;
492-
487+
d = readdir_skip_dot_and_dotdot(dir);
488+
if (d != NULL)
493489
ret = 1;
494-
break;
495-
}
496490
closedir(dir);
497491
return ret;
498492
}

0 commit comments

Comments
 (0)