Skip to content

Commit 49bfc87

Browse files
committed
fs-util: unify code we use to check if dirent's d_name is "." or ".."
We use different idioms at different places. Let's replace this is the one true new idiom, that is even a bit faster...
1 parent 1d01069 commit 49bfc87

File tree

12 files changed

+43
-20
lines changed

12 files changed

+43
-20
lines changed

src/basic/cgroup-util.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ int cg_read_subgroup(DIR *d, char **fn) {
182182
if (de->d_type != DT_DIR)
183183
continue;
184184

185-
if (streq(de->d_name, ".") ||
186-
streq(de->d_name, ".."))
185+
if (dot_or_dot_dot(de->d_name))
187186
continue;
188187

189188
b = strdup(de->d_name);

src/basic/copy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ static int fd_copy_directory(
331331
struct stat buf;
332332
int q;
333333

334-
if (STR_IN_SET(de->d_name, ".", ".."))
334+
if (dot_or_dot_dot(de->d_name))
335335
continue;
336336

337337
if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {

src/basic/path-util.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -699,10 +699,7 @@ bool filename_is_valid(const char *p) {
699699
if (isempty(p))
700700
return false;
701701

702-
if (streq(p, "."))
703-
return false;
704-
705-
if (streq(p, ".."))
702+
if (dot_or_dot_dot(p))
706703
return false;
707704

708705
e = strchrnul(p, '/');
@@ -720,14 +717,17 @@ bool path_is_safe(const char *p) {
720717
if (isempty(p))
721718
return false;
722719

723-
if (streq(p, "..") || startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
720+
if (dot_or_dot_dot(p))
721+
return false;
722+
723+
if (startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
724724
return false;
725725

726726
if (strlen(p)+1 > PATH_MAX)
727727
return false;
728728

729729
/* The following two checks are not really dangerous, but hey, they still are confusing */
730-
if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
730+
if (startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
731731
return false;
732732

733733
if (strstr(p, "//"))
@@ -892,3 +892,16 @@ int systemd_installation_has_version(const char *root, unsigned minimal_version)
892892

893893
return false;
894894
}
895+
896+
bool dot_or_dot_dot(const char *path) {
897+
if (!path)
898+
return false;
899+
if (path[0] != '.')
900+
return false;
901+
if (path[1] == 0)
902+
return true;
903+
if (path[1] != '.')
904+
return false;
905+
906+
return path[2] == 0;
907+
}

src/basic/path-util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,5 @@ bool is_device_path(const char *path);
141141
bool is_deviceallow_pattern(const char *path);
142142

143143
int systemd_installation_has_version(const char *root, unsigned minimal_version);
144+
145+
bool dot_or_dot_dot(const char *path);

src/basic/rm-rf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
8383
bool is_dir;
8484
struct stat st;
8585

86-
if (streq(de->d_name, ".") || streq(de->d_name, ".."))
86+
if (dot_or_dot_dot(de->d_name))
8787
continue;
8888

8989
if (de->d_type == DT_UNKNOWN ||

src/basic/socket-util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ bool ifname_valid(const char *p) {
877877
if (strlen(p) >= IFNAMSIZ)
878878
return false;
879879

880-
if (STR_IN_SET(p, ".", ".."))
880+
if (dot_or_dot_dot(p))
881881
return false;
882882

883883
while (*p) {

src/gpt-auto-generator/gpt-auto-generator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ static int get_block_device_harder(const char *path, dev_t *dev) {
658658

659659
FOREACH_DIRENT_ALL(de, d, return -errno) {
660660

661-
if (STR_IN_SET(de->d_name, ".", ".."))
661+
if (dot_or_dot_dot(de->d_name))
662662
continue;
663663

664664
if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))

src/nspawn/nspawn-patch-uid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ static int recurse_fd(int fd, bool donate_fd, const struct stat *st, uid_t shift
375375
FOREACH_DIRENT_ALL(de, d, r = -errno; goto finish) {
376376
struct stat fst;
377377

378-
if (STR_IN_SET(de->d_name, ".", ".."))
378+
if (dot_or_dot_dot(de->d_name))
379379
continue;
380380

381381
if (fstatat(dirfd(d), de->d_name, &fst, AT_SYMLINK_NOFOLLOW) < 0) {

src/resolve/resolved-manager.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ void manager_cleanup_saved_user(Manager *m) {
13491349
if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG))
13501350
continue;
13511351

1352-
if (STR_IN_SET(de->d_name, ".", ".."))
1352+
if (dot_or_dot_dot(de->d_name))
13531353
continue;
13541354

13551355
r = parse_ifindex(de->d_name, &ifindex);

src/shared/clean-ipc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static int clean_posix_shm_internal(DIR *dir, uid_t uid, gid_t gid) {
225225
FOREACH_DIRENT_ALL(de, dir, goto fail) {
226226
struct stat st;
227227

228-
if (STR_IN_SET(de->d_name, "..", "."))
228+
if (dot_or_dot_dot(de->d_name))
229229
continue;
230230

231231
if (fstatat(dirfd(dir), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
@@ -310,7 +310,7 @@ static int clean_posix_mq(uid_t uid, gid_t gid) {
310310
struct stat st;
311311
char fn[1+strlen(de->d_name)+1];
312312

313-
if (STR_IN_SET(de->d_name, "..", "."))
313+
if (dot_or_dot_dot(de->d_name))
314314
continue;
315315

316316
if (fstatat(dirfd(dir), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {

0 commit comments

Comments
 (0)