]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fs-util: unify code we use to check if dirent's d_name is "." or ".."
authorLennart Poettering <lennart@poettering.net>
Wed, 1 Feb 2017 23:06:18 +0000 (00:06 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 1 Feb 2017 23:06:18 +0000 (00:06 +0100)
We use different idioms at different places. Let's replace this is the
one true new idiom, that is even a bit faster...

12 files changed:
src/basic/cgroup-util.c
src/basic/copy.c
src/basic/path-util.c
src/basic/path-util.h
src/basic/rm-rf.c
src/basic/socket-util.c
src/gpt-auto-generator/gpt-auto-generator.c
src/nspawn/nspawn-patch-uid.c
src/resolve/resolved-manager.c
src/shared/clean-ipc.c
src/test/test-fs-util.c
src/tmpfiles/tmpfiles.c

index d2d18f13f026f0fc11adc5737dae73501fa79560..6948ed393132cc701a3bb35ce58d08e1fd9a46b6 100644 (file)
@@ -182,8 +182,7 @@ int cg_read_subgroup(DIR *d, char **fn) {
                 if (de->d_type != DT_DIR)
                         continue;
 
-                if (streq(de->d_name, ".") ||
-                    streq(de->d_name, ".."))
+                if (dot_or_dot_dot(de->d_name))
                         continue;
 
                 b = strdup(de->d_name);
index 9883f5fa31097a3b472cdb4cc42850d433cb4fbe..e9a7efd232a8dff9beb3daf3d1ed23ca073032bb 100644 (file)
@@ -331,7 +331,7 @@ static int fd_copy_directory(
                 struct stat buf;
                 int q;
 
-                if (STR_IN_SET(de->d_name, ".", ".."))
+                if (dot_or_dot_dot(de->d_name))
                         continue;
 
                 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
index 9a51e0d8bc783bf4147378b74a83bbede40ed542..1313a52c9cab551b682a3977f566b32405f0d7a4 100644 (file)
@@ -699,10 +699,7 @@ bool filename_is_valid(const char *p) {
         if (isempty(p))
                 return false;
 
-        if (streq(p, "."))
-                return false;
-
-        if (streq(p, ".."))
+        if (dot_or_dot_dot(p))
                 return false;
 
         e = strchrnul(p, '/');
@@ -720,14 +717,17 @@ bool path_is_safe(const char *p) {
         if (isempty(p))
                 return false;
 
-        if (streq(p, "..") || startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
+        if (dot_or_dot_dot(p))
+                return false;
+
+        if (startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
                 return false;
 
         if (strlen(p)+1 > PATH_MAX)
                 return false;
 
         /* The following two checks are not really dangerous, but hey, they still are confusing */
-        if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
+        if (startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
                 return false;
 
         if (strstr(p, "//"))
@@ -892,3 +892,16 @@ int systemd_installation_has_version(const char *root, unsigned minimal_version)
 
         return false;
 }
+
+bool dot_or_dot_dot(const char *path) {
+        if (!path)
+                return false;
+        if (path[0] != '.')
+                return false;
+        if (path[1] == 0)
+                return true;
+        if (path[1] != '.')
+                return false;
+
+        return path[2] == 0;
+}
index 349cdac7d6c223921a0b8d3dfac50a3846feb381..35aef3adc83b5ff13364be969c067cf279ddd169 100644 (file)
@@ -141,3 +141,5 @@ bool is_device_path(const char *path);
 bool is_deviceallow_pattern(const char *path);
 
 int systemd_installation_has_version(const char *root, unsigned minimal_version);
+
+bool dot_or_dot_dot(const char *path);
index 07d42f78dd20926b22123acb08b024f3af3560f1..08497af729c13aa600e04c0f3755a4725e38d7b9 100644 (file)
@@ -83,7 +83,7 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
                 bool is_dir;
                 struct stat st;
 
-                if (streq(de->d_name, ".") || streq(de->d_name, ".."))
+                if (dot_or_dot_dot(de->d_name))
                         continue;
 
                 if (de->d_type == DT_UNKNOWN ||
index 77f81a60ba5b0f8865f10841037a1a4a0fb35f88..17e90a89949ee4008e8b3cb1e5988b8d390b7a37 100644 (file)
@@ -877,7 +877,7 @@ bool ifname_valid(const char *p) {
         if (strlen(p) >= IFNAMSIZ)
                 return false;
 
-        if (STR_IN_SET(p, ".", ".."))
+        if (dot_or_dot_dot(p))
                 return false;
 
         while (*p) {
index e61ef8f249e989812e5c17e01eb89700ef667a8b..b958070c903b926863753671724a849e96c958f8 100644 (file)
@@ -658,7 +658,7 @@ static int get_block_device_harder(const char *path, dev_t *dev) {
 
         FOREACH_DIRENT_ALL(de, d, return -errno) {
 
-                if (STR_IN_SET(de->d_name, ".", ".."))
+                if (dot_or_dot_dot(de->d_name))
                         continue;
 
                 if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
index ded5866d05d3f1cc1fa8a1f12bf4f5994f25814f..1a3f129db0c28bb75cd44afe5a68a71bc83919a7 100644 (file)
@@ -375,7 +375,7 @@ static int recurse_fd(int fd, bool donate_fd, const struct stat *st, uid_t shift
                 FOREACH_DIRENT_ALL(de, d, r = -errno; goto finish) {
                         struct stat fst;
 
-                        if (STR_IN_SET(de->d_name, ".", ".."))
+                        if (dot_or_dot_dot(de->d_name))
                                 continue;
 
                         if (fstatat(dirfd(d), de->d_name, &fst, AT_SYMLINK_NOFOLLOW) < 0) {
index 0954641c20a0f4a6d9901f060e6b7940a68bdff2..667774b9065d35fadfabfd3b6f514d51aaec12d9 100644 (file)
@@ -1349,7 +1349,7 @@ void manager_cleanup_saved_user(Manager *m) {
                 if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG))
                         continue;
 
-                if (STR_IN_SET(de->d_name, ".", ".."))
+                if (dot_or_dot_dot(de->d_name))
                         continue;
 
                 r = parse_ifindex(de->d_name, &ifindex);
index f2d1555c132c6f806b2ee40f32b812272d8401b3..f59f6f23ae15eb7ca5f4d798828baa4768722fbf 100644 (file)
@@ -225,7 +225,7 @@ static int clean_posix_shm_internal(DIR *dir, uid_t uid, gid_t gid) {
         FOREACH_DIRENT_ALL(de, dir, goto fail) {
                 struct stat st;
 
-                if (STR_IN_SET(de->d_name, "..", "."))
+                if (dot_or_dot_dot(de->d_name))
                         continue;
 
                 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) {
                 struct stat st;
                 char fn[1+strlen(de->d_name)+1];
 
-                if (STR_IN_SET(de->d_name, "..", "."))
+                if (dot_or_dot_dot(de->d_name))
                         continue;
 
                 if (fstatat(dirfd(dir), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
index 4cb465d0d240b28ccaa9a7514f3f727b3bb1a8e9..e774f567e0307f8881fcd11bde724242144a9d4c 100644 (file)
@@ -305,12 +305,23 @@ static void test_var_tmp(void) {
         }
 }
 
+static void test_dot_or_dot_dot(void) {
+        assert_se(!dot_or_dot_dot(NULL));
+        assert_se(!dot_or_dot_dot(""));
+        assert_se(!dot_or_dot_dot("xxx"));
+        assert_se(dot_or_dot_dot("."));
+        assert_se(dot_or_dot_dot(".."));
+        assert_se(!dot_or_dot_dot(".foo"));
+        assert_se(!dot_or_dot_dot("..foo"));
+}
+
 int main(int argc, char *argv[]) {
         test_unlink_noerrno();
         test_readlink_and_make_absolute();
         test_get_files_in_directory();
         test_var_tmp();
         test_chase_symlinks();
+        test_dot_or_dot_dot();
 
         return 0;
 }
index f4ce9791fbb7f988d5bb4c3de0cd7328317975b9..c4f4d46ca10497feabf06052945decd474a8ee89 100644 (file)
@@ -385,7 +385,7 @@ static int dir_cleanup(
                 usec_t age;
                 _cleanup_free_ char *sub_path = NULL;
 
-                if (STR_IN_SET(dent->d_name, ".", ".."))
+                if (dot_or_dot_dot(dent->d_name))
                         continue;
 
                 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
@@ -1070,9 +1070,7 @@ static int item_do_children(Item *i, const char *path, action_t action) {
                 _cleanup_free_ char *p = NULL;
                 int q;
 
-                errno = 0;
-
-                if (STR_IN_SET(de->d_name, ".", ".."))
+                if (dot_or_dot_dot(de->d_name))
                         continue;
 
                 p = strjoin(path, "/", de->d_name);