chiark / gitweb /
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)
committerSven Eden <yamakuzure@gmx.net>
Mon, 17 Jul 2017 15:58:36 +0000 (17:58 +0200)
We use different idioms at different places. Let's replace this is the
one true new idiom, that is even a bit faster...

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/shared/clean-ipc.c

index 0e2f2cbc08c987e1f87a45cc3e38ffdf73d28489..32cf4298f2628438fc6f843d2300e98fbc3b0c95 100644 (file)
@@ -184,8 +184,7 @@ int cg_read_subgroup(DIR *d, char **fn) {
                 if (de->d_type != DT_DIR)
                         continue;
 
                 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);
                         continue;
 
                 b = strdup(de->d_name);
index 4e5f392fe59fdc8b7a74bbdb84ea482a117f916d..20078a81e23e417c18d73a95b7d0d17c360a456f 100644 (file)
@@ -333,7 +333,7 @@ static int fd_copy_directory(
                 struct stat buf;
                 int q;
 
                 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) {
                         continue;
 
                 if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
index 84f327d7d050fbd4a6c1e98d21a6fb38316533a8..05384c26b619b8a86beb6207ce1eb55ee5e17a06 100644 (file)
@@ -703,10 +703,7 @@ bool filename_is_valid(const char *p) {
         if (isempty(p))
                 return false;
 
         if (isempty(p))
                 return false;
 
-        if (streq(p, "."))
-                return false;
-
-        if (streq(p, ".."))
+        if (dot_or_dot_dot(p))
                 return false;
 
         e = strchrnul(p, '/');
                 return false;
 
         e = strchrnul(p, '/');
@@ -724,14 +721,17 @@ bool path_is_safe(const char *p) {
         if (isempty(p))
                 return false;
 
         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 */
                 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, "//"))
                 return false;
 
         if (strstr(p, "//"))
@@ -898,3 +898,16 @@ int systemd_installation_has_version(const char *root, unsigned minimal_version)
         return false;
 }
 #endif // 0
         return false;
 }
 #endif // 0
+
+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 6c906e54a13000e62f3c1a24809dba498ad9c157..3e1519efe4f5629d37e2e2fc9d109f5af3d14352 100644 (file)
@@ -157,3 +157,5 @@ bool is_deviceallow_pattern(const char *path);
 
 int systemd_installation_has_version(const char *root, unsigned minimal_version);
 #endif // 0
 
 int systemd_installation_has_version(const char *root, unsigned minimal_version);
 #endif // 0
+
+bool dot_or_dot_dot(const char *path);
index a9035919471e1d241b6b61c07288486c448c1792..85854acf9f0c05f535a05e7acb9807b2a3f5c0ca 100644 (file)
@@ -82,7 +82,7 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
                 bool is_dir;
                 struct stat st;
 
                 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 ||
                         continue;
 
                 if (de->d_type == DT_UNKNOWN ||
index 6a8ca1d7e375168fce35c6eb1d9a29e28c84907f..9ec9012611c6346141bc276599548702042b6e0c 100644 (file)
@@ -881,7 +881,7 @@ bool ifname_valid(const char *p) {
         if (strlen(p) >= IFNAMSIZ)
                 return false;
 
         if (strlen(p) >= IFNAMSIZ)
                 return false;
 
-        if (STR_IN_SET(p, ".", ".."))
+        if (dot_or_dot_dot(p))
                 return false;
 
         while (*p) {
                 return false;
 
         while (*p) {
index ba48fb0ec5871cd1607fbb1282667e01765c741d..3b4231827725cda1c912e59dcc20a314d17b333f 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;
 
         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) {
                         continue;
 
                 if (fstatat(dirfd(dir), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
@@ -311,7 +311,7 @@ static int clean_posix_mq(uid_t uid, gid_t gid) {
                 struct stat st;
                 char fn[1+strlen(de->d_name)+1];
 
                 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) {
                         continue;
 
                 if (fstatat(dirfd(dir), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {