path-util: introduce empty_to_root() and use it many places
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 10 May 2018 16:47:33 +0000 (01:47 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 10 May 2018 16:47:33 +0000 (01:47 +0900)
src/basic/path-util.h
src/cgls/cgls.c
src/cgtop/cgtop.c
src/core/dbus-unit.c
src/libsystemd/sd-bus/bus-socket.c
src/test/test-unit-name.c
src/tmpfiles/tmpfiles.c

index fd3143d..134e1eb 100644 (file)
@@ -164,3 +164,6 @@ static inline const char *skip_dev_prefix(const char *p) {
 }
 
 bool empty_or_root(const char *root);
+static inline const char *empty_to_root(const char *path) {
+        return isempty(path) ? "/" : path;
+}
index 61e0a67..7c7ea18 100644 (file)
@@ -149,7 +149,7 @@ static void show_cg_info(const char *controller, const char *path) {
         if (cg_all_unified() == 0 && controller && !streq(controller, SYSTEMD_CGROUP_CONTROLLER))
                 printf("Controller %s; ", controller);
 
-        printf("Control group %s:\n", isempty(path) ? "/" : path);
+        printf("Control group %s:\n", empty_to_root(path));
         fflush(stdout);
 }
 
index de7ecf5..401c086 100644 (file)
@@ -529,10 +529,6 @@ static int refresh(const char *root, Hashmap *a, Hashmap *b, unsigned iteration)
         return 0;
 }
 
-static const char *empty_to_slash(const char *p) {
-        return isempty(p) ? "/" : p;
-}
-
 static int group_compare(const void*a, const void *b) {
         const Group *x = *(Group**)a, *y = *(Group**)b;
 
@@ -542,9 +538,9 @@ static int group_compare(const void*a, const void *b) {
                  * recursive summing is off, since that is actually
                  * not accumulative for all children. */
 
-                if (path_startswith(empty_to_slash(y->path), empty_to_slash(x->path)))
+                if (path_startswith(empty_to_root(y->path), empty_to_root(x->path)))
                         return -1;
-                if (path_startswith(empty_to_slash(x->path), empty_to_slash(y->path)))
+                if (path_startswith(empty_to_root(x->path), empty_to_root(y->path)))
                         return 1;
         }
 
@@ -693,7 +689,7 @@ static void display(Hashmap *a) {
 
                 g = array[j];
 
-                path = empty_to_slash(g->path);
+                path = empty_to_root(g->path);
                 ellipsized = ellipsize(path, path_columns, 33);
                 printf("%-*s", path_columns, ellipsized ?: path);
 
index f93893c..9a61f6c 100644 (file)
@@ -931,7 +931,7 @@ static int property_get_cgroup(
          * other cases we report as-is. */
 
         if (u->cgroup_path)
-                t = isempty(u->cgroup_path) ? "/" : u->cgroup_path;
+                t = empty_to_root(u->cgroup_path);
 
         return sd_bus_message_append(reply, "s", t);
 }
index 54feddf..04839ba 100644 (file)
@@ -777,7 +777,7 @@ static int bus_socket_inotify_setup(sd_bus *b) {
                         if (IN_SET(errno, ENOENT, ELOOP))
                                 break; /* This component doesn't exist yet, or the path contains a cyclic symlink right now */
 
-                        r = log_debug_errno(errno, "Failed to add inotify watch on %s: %m", isempty(prefix) ? "/" : prefix);
+                        r = log_debug_errno(errno, "Failed to add inotify watch on %s: %m", empty_to_root(prefix));
                         goto fail;
                 } else
                         new_watches[n++] = wd;
index 300a1c5..b46925b 100644 (file)
@@ -96,7 +96,7 @@ static void test_unit_name_from_path_one(const char *path, const char *suffix, c
                 _cleanup_free_ char *k = NULL;
                 assert_se(unit_name_to_path(t, &k) == 0);
                 puts(strna(k));
-                assert_se(path_equal(k, isempty(path) ? "/" : path));
+                assert_se(path_equal(k, empty_to_root(path)));
         }
 }
 
@@ -124,7 +124,7 @@ static void test_unit_name_from_path_instance_one(const char *pattern, const cha
 
                 assert_se(unit_name_to_instance(t, &k) > 0);
                 assert_se(unit_name_path_unescape(k, &v) == 0);
-                assert_se(path_equal(v, isempty(path) ? "/" : path));
+                assert_se(path_equal(v, empty_to_root(path)));
         }
 }
 
index bc66299..5190d7e 100644 (file)
@@ -1502,7 +1502,7 @@ static int create_item(Item *i) {
 
                 if (IN_SET(i->type, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA)) {
 
-                        if (btrfs_is_subvol(isempty(arg_root) ? "/" : arg_root) <= 0)
+                        if (btrfs_is_subvol(empty_to_root(arg_root)) <= 0)
 
                                 /* Don't create a subvolume unless the
                                  * root directory is one, too. We do