chiark / gitweb /
util-lib: introduce new empty_or_root() helper (#8746)
authorLennart Poettering <lennart@poettering.net>
Wed, 18 Apr 2018 12:20:49 +0000 (14:20 +0200)
committerSven Eden <yamakuzure@gmx.net>
Fri, 24 Aug 2018 14:47:08 +0000 (16:47 +0200)
We check the same condition at various places. Let's add a trivial,
common helper for this, and use it everywhere.

It's not going to make things much faster or much shorter, but I think a
lot more readable

src/basic/cgroup-util.c
src/basic/fs-util.c
src/basic/path-util.c
src/basic/path-util.h
src/basic/unit-name.c
src/basic/user-util.c
src/core/cgroup.c
src/test/test-path-util.c

index 443d65ff4002e7f4a9616a522c8228cb92acd3ef..1c5ca9899406860a58469071ad50108c1c550059 100644 (file)
@@ -427,7 +427,7 @@ int cg_migrate(
                          * exist in the root cgroup, we only check for
                          * them there. */
                         if (cfrom &&
-                            (isempty(pfrom) || path_equal(pfrom, "/")) &&
+                            empty_or_root(pfrom) &&
                             is_kernel_thread(pid) > 0)
                                 continue;
 
@@ -1202,7 +1202,7 @@ int cg_is_empty_recursive(const char *controller, const char *path) {
         assert(path);
 
         /* The root cgroup is always populated */
-        if (controller && (isempty(path) || path_equal(path, "/")))
+        if (controller && empty_or_root(path))
                 return false;
 
         r = cg_unified_controller(controller);
index 858441c94d91a525e16b43ada622fff0f81e598e..5bf1413c10f59807410d2d435181f708797b90dc 100644 (file)
@@ -579,10 +579,6 @@ int inotify_add_watch_fd(int fd, int what, uint32_t mask) {
 }
 #endif // 0
 
-static bool noop_root(const char *root) {
-        return isempty(root) || path_equal(root, "/");
-}
-
 static bool safe_transition(const struct stat *a, const struct stat *b) {
         /* Returns true if the transition from a to b is safe, i.e. that we never transition from unprivileged to
          * privileged files or directories. Why bother? So that unprivileged code can't symlink to privileged files
@@ -633,7 +629,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
          * specified path. */
 
         /* A root directory of "/" or "" is identical to none */
-        if (noop_root(original_root))
+        if (empty_or_root(original_root))
                 original_root = NULL;
 
         if (!original_root && !ret && (flags & (CHASE_NONEXISTENT|CHASE_NO_AUTOFS|CHASE_SAFE|CHASE_OPEN)) == CHASE_OPEN) {
@@ -718,7 +714,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
 
                         /* If we already are at the top, then going up will not change anything. This is in-line with
                          * how the kernel handles this. */
-                        if (isempty(done) || path_equal(done, "/"))
+                        if (empty_or_root(done))
                                 continue;
 
                         parent = dirname_malloc(done);
@@ -904,7 +900,7 @@ int chase_symlinks_and_open(
         if (chase_flags & CHASE_NONEXISTENT)
                 return -EINVAL;
 
-        if (noop_root(root) && !ret_path && (chase_flags & (CHASE_NO_AUTOFS|CHASE_SAFE)) == 0) {
+        if (empty_or_root(root) && !ret_path && (chase_flags & (CHASE_NO_AUTOFS|CHASE_SAFE)) == 0) {
                 /* Shortcut this call if none of the special features of this call are requested */
                 r = open(path, open_flags);
                 if (r < 0)
@@ -944,7 +940,7 @@ int chase_symlinks_and_opendir(
         if (chase_flags & CHASE_NONEXISTENT)
                 return -EINVAL;
 
-        if (noop_root(root) && !ret_path && (chase_flags & (CHASE_NO_AUTOFS|CHASE_SAFE)) == 0) {
+        if (empty_or_root(root) && !ret_path && (chase_flags & (CHASE_NO_AUTOFS|CHASE_SAFE)) == 0) {
                 /* Shortcut this call if none of the special features of this call are requested */
                 d = opendir(path);
                 if (!d)
index addd39373c40b2a1144e15c1f3fcab4e8fc6707a..48205dc32ebedb8be99bfbbee479efe04b76bbb6 100644 (file)
@@ -646,7 +646,7 @@ char *prefix_root(const char *root, const char *path) {
         while (path[0] == '/' && path[1] == '/')
                 path++;
 
-        if (isempty(root) || path_equal(root, "/"))
+        if (empty_or_root(root))
                 return strdup(path);
 
         l = strlen(root) + 1 + strlen(path) + 1;
@@ -975,3 +975,14 @@ bool dot_or_dot_dot(const char *path) {
 
         return path[2] == 0;
 }
+
+bool empty_or_root(const char *root) {
+
+        /* For operations relative to some root directory, returns true if the specified root directory is redundant,
+         * i.e. either / or NULL or the empty string or any equivalent. */
+
+        if (!root)
+                return true;
+
+        return root[strspn(root, "/")] == 0;
+}
index 08df54573cd493673686c500c71269204e473cc4..6583c2c9b26ee4342a3df19895763d7d78366f1b 100644 (file)
@@ -121,7 +121,7 @@ char *prefix_root(const char *root, const char *path);
                 size_t _l;                                              \
                 while (_path[0] == '/' && _path[1] == '/')              \
                         _path ++;                                       \
-                if (isempty(_root) || path_equal(_root, "/"))           \
+                if (empty_or_root(_root))                               \
                         _ret = _path;                                   \
                 else {                                                  \
                         _l = strlen(_root) + 1 + strlen(_path) + 1;     \
@@ -169,3 +169,5 @@ static inline const char *skip_dev_prefix(const char *p) {
 
         return e ?: p;
 }
+
+bool empty_or_root(const char *root);
index 02cb86390c988dff584c8c2a77925199abf3fcf1..85f06126c7c2f4e026e50344f0d357d0fd924b5f 100644 (file)
@@ -390,7 +390,7 @@ int unit_name_path_escape(const char *f, char **ret) {
 
         path_kill_slashes(p);
 
-        if (STR_IN_SET(p, "/", ""))
+        if (empty_or_root(p))
                 s = strdup("-");
         else {
                 if (!path_is_normalized(p))
index 55f4908c247e33e49ff4cacbf9e751bd9e1a9853..905d34b7f51171b6e02d91b4e8496c03c66894df 100644 (file)
@@ -224,8 +224,7 @@ int get_user_creds_clean(
             (isempty(*shell) || is_nologin_shell(*shell)))
                 *shell = NULL;
 
-        if (home &&
-            (isempty(*home) || path_equal(*home, "/")))
+        if (home && empty_or_root(*home))
                 *home = NULL;
 
         return 0;
index c52bdbf155f437a829c046f4ad212a0f86bf6fcc..9b69fa82e791f2de08fb13c323340b0980c417b0 100644 (file)
@@ -40,7 +40,7 @@ bool manager_owns_root_cgroup(Manager *m) {
         if (detect_container() > 0)
                 return false;
 
-        return isempty(m->cgroup_root) || path_equal(m->cgroup_root, "/");
+        return empty_or_root(m->cgroup_root);
 }
 
 #if 0 /// UNNEEDED by elogind
index f7e96283fd7975177ed59f2643f0a8abfb0671b4..e7a247027f23db2374417773238dd6cf5ca61a81 100644 (file)
@@ -478,6 +478,19 @@ static void test_skip_dev_prefix(void) {
         assert_se(streq(skip_dev_prefix("foo"), "foo"));
 }
 
+static void test_empty_or_root(void) {
+        assert_se(empty_or_root(NULL));
+        assert_se(empty_or_root(""));
+        assert_se(empty_or_root("/"));
+        assert_se(empty_or_root("//"));
+        assert_se(empty_or_root("///"));
+        assert_se(empty_or_root("/////////////////"));
+        assert_se(!empty_or_root("xxx"));
+        assert_se(!empty_or_root("/xxx"));
+        assert_se(!empty_or_root("/xxx/"));
+        assert_se(!empty_or_root("//yy//"));
+}
+
 int main(int argc, char **argv) {
         log_set_max_level(LOG_DEBUG);
         log_parse_environment();
@@ -500,6 +513,7 @@ int main(int argc, char **argv) {
         test_filename_is_valid();
         test_hidden_or_backup_file();
         test_skip_dev_prefix();
+        test_empty_or_root();
 
 #if 0 /// UNNEEDED by elogind
         test_systemd_installation_has_version(argv[1]); /* NULL is OK */