From: Yu Watanabe Date: Thu, 10 May 2018 16:47:33 +0000 (+0900) Subject: path-util: introduce empty_to_root() and use it many places X-Git-Tag: v239~276^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=945403e6edbe6bf98015aabb38b281aa89334d0a;p=thirdparty%2Fsystemd.git path-util: introduce empty_to_root() and use it many places --- diff --git a/src/basic/path-util.h b/src/basic/path-util.h index fd3143d9acc..134e1ebc98f 100644 --- a/src/basic/path-util.h +++ b/src/basic/path-util.h @@ -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; +} diff --git a/src/cgls/cgls.c b/src/cgls/cgls.c index 61e0a67b83c..7c7ea18b615 100644 --- a/src/cgls/cgls.c +++ b/src/cgls/cgls.c @@ -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); } diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index de7ecf54cda..401c08600ae 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -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); diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c index f93893c62f2..9a61f6ce11f 100644 --- a/src/core/dbus-unit.c +++ b/src/core/dbus-unit.c @@ -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); } diff --git a/src/libsystemd/sd-bus/bus-socket.c b/src/libsystemd/sd-bus/bus-socket.c index 54feddf9e54..04839bada41 100644 --- a/src/libsystemd/sd-bus/bus-socket.c +++ b/src/libsystemd/sd-bus/bus-socket.c @@ -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; diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c index 300a1c5a82e..b46925be806 100644 --- a/src/test/test-unit-name.c +++ b/src/test/test-unit-name.c @@ -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))); } } diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index bc662991425..5190d7eb76d 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -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