tree-wide: introduce PATH_IN_SET macro
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 17 Apr 2016 02:52:06 +0000 (22:52 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 17 Apr 2016 02:57:05 +0000 (22:57 -0400)
src/basic/path-util.c
src/basic/path-util.h
src/core/mount.c
src/test/test-path-util.c

index 822c09b..044a128 100644 (file)
@@ -569,10 +569,10 @@ static int binary_is_good(const char *binary) {
         if (r < 0)
                 return r;
 
-        return !path_equal(d, "true") &&
-               !path_equal(d, "/bin/true") &&
-               !path_equal(d, "/usr/bin/true") &&
-               !path_equal(d, "/dev/null");
+        return !PATH_IN_SET(d, "true"
+                               "/bin/true",
+                               "/usr/bin/true",
+                               "/dev/null");
 }
 
 int fsck_exists(const char *fstype) {
index 2c2f87a..f43d477 100644 (file)
@@ -48,6 +48,19 @@ bool path_equal(const char *a, const char *b) _pure_;
 bool path_equal_or_files_same(const char *a, const char *b);
 char* path_join(const char *root, const char *path, const char *rest);
 
+/* Note: the search terminates on the first NULL item. */
+#define PATH_IN_SET(p, ...)                                     \
+        ({                                                      \
+                char **s;                                       \
+                bool _found = false;                            \
+                STRV_FOREACH(s, STRV_MAKE(__VA_ARGS__))         \
+                        if (path_equal(p, *s)) {                \
+                               _found = true;                   \
+                               break;                           \
+                        }                                       \
+                _found;                                         \
+        })
+
 int path_strv_make_absolute_cwd(char **l);
 char** path_strv_resolve(char **l, const char *prefix);
 char** path_strv_resolve_uniq(char **l, const char *prefix);
index 74ab54b..632c5c8 100644 (file)
@@ -376,8 +376,7 @@ static int mount_add_quota_links(Mount *m) {
 static bool should_umount(Mount *m) {
         MountParameters *p;
 
-        if (path_equal(m->where, "/") ||
-            path_equal(m->where, "/usr") ||
+        if (PATH_IN_SET(m->where, "/", "/usr") ||
             path_startswith(m->where, "/run/initramfs"))
                 return false;
 
@@ -408,8 +407,7 @@ static int mount_add_default_dependencies(Mount *m) {
          * Also, don't bother with anything mounted below virtual
          * file systems, it's also going to be virtual, and hence
          * not worth the effort. */
-        if (path_equal(m->where, "/") ||
-            path_equal(m->where, "/usr") ||
+        if (PATH_IN_SET(m->where, "/", "/usr") ||
             path_startswith(m->where, "/run/initramfs") ||
             path_startswith(m->where, "/proc") ||
             path_startswith(m->where, "/sys") ||
index d376dd5..7ce2695 100644 (file)
@@ -90,6 +90,12 @@ static void test_path(void) {
                 assert_se(path_equal(path_kill_slashes(p2), "/aaa/./ccc"));
                 assert_se(path_equal(path_kill_slashes(p3), "/./"));
         }
+
+        assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo"));
+        assert_se(PATH_IN_SET("/bin", "/bin"));
+        assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin"));
+        assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar"));
+        assert_se(!PATH_IN_SET("/", "/abc", "/def"));
 }
 
 static void test_find_binary(const char *self) {