X Tutup
Skip to content

Commit 945403e

Browse files
committed
path-util: introduce empty_to_root() and use it many places
1 parent 54138a8 commit 945403e

File tree

7 files changed

+12
-13
lines changed

7 files changed

+12
-13
lines changed

src/basic/path-util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,6 @@ static inline const char *skip_dev_prefix(const char *p) {
164164
}
165165

166166
bool empty_or_root(const char *root);
167+
static inline const char *empty_to_root(const char *path) {
168+
return isempty(path) ? "/" : path;
169+
}

src/cgls/cgls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static void show_cg_info(const char *controller, const char *path) {
149149
if (cg_all_unified() == 0 && controller && !streq(controller, SYSTEMD_CGROUP_CONTROLLER))
150150
printf("Controller %s; ", controller);
151151

152-
printf("Control group %s:\n", isempty(path) ? "/" : path);
152+
printf("Control group %s:\n", empty_to_root(path));
153153
fflush(stdout);
154154
}
155155

src/cgtop/cgtop.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -529,10 +529,6 @@ static int refresh(const char *root, Hashmap *a, Hashmap *b, unsigned iteration)
529529
return 0;
530530
}
531531

532-
static const char *empty_to_slash(const char *p) {
533-
return isempty(p) ? "/" : p;
534-
}
535-
536532
static int group_compare(const void*a, const void *b) {
537533
const Group *x = *(Group**)a, *y = *(Group**)b;
538534

@@ -542,9 +538,9 @@ static int group_compare(const void*a, const void *b) {
542538
* recursive summing is off, since that is actually
543539
* not accumulative for all children. */
544540

545-
if (path_startswith(empty_to_slash(y->path), empty_to_slash(x->path)))
541+
if (path_startswith(empty_to_root(y->path), empty_to_root(x->path)))
546542
return -1;
547-
if (path_startswith(empty_to_slash(x->path), empty_to_slash(y->path)))
543+
if (path_startswith(empty_to_root(x->path), empty_to_root(y->path)))
548544
return 1;
549545
}
550546

@@ -693,7 +689,7 @@ static void display(Hashmap *a) {
693689

694690
g = array[j];
695691

696-
path = empty_to_slash(g->path);
692+
path = empty_to_root(g->path);
697693
ellipsized = ellipsize(path, path_columns, 33);
698694
printf("%-*s", path_columns, ellipsized ?: path);
699695

src/core/dbus-unit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ static int property_get_cgroup(
931931
* other cases we report as-is. */
932932

933933
if (u->cgroup_path)
934-
t = isempty(u->cgroup_path) ? "/" : u->cgroup_path;
934+
t = empty_to_root(u->cgroup_path);
935935

936936
return sd_bus_message_append(reply, "s", t);
937937
}

src/libsystemd/sd-bus/bus-socket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ static int bus_socket_inotify_setup(sd_bus *b) {
777777
if (IN_SET(errno, ENOENT, ELOOP))
778778
break; /* This component doesn't exist yet, or the path contains a cyclic symlink right now */
779779

780-
r = log_debug_errno(errno, "Failed to add inotify watch on %s: %m", isempty(prefix) ? "/" : prefix);
780+
r = log_debug_errno(errno, "Failed to add inotify watch on %s: %m", empty_to_root(prefix));
781781
goto fail;
782782
} else
783783
new_watches[n++] = wd;

src/test/test-unit-name.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static void test_unit_name_from_path_one(const char *path, const char *suffix, c
9696
_cleanup_free_ char *k = NULL;
9797
assert_se(unit_name_to_path(t, &k) == 0);
9898
puts(strna(k));
99-
assert_se(path_equal(k, isempty(path) ? "/" : path));
99+
assert_se(path_equal(k, empty_to_root(path)));
100100
}
101101
}
102102

@@ -124,7 +124,7 @@ static void test_unit_name_from_path_instance_one(const char *pattern, const cha
124124

125125
assert_se(unit_name_to_instance(t, &k) > 0);
126126
assert_se(unit_name_path_unescape(k, &v) == 0);
127-
assert_se(path_equal(v, isempty(path) ? "/" : path));
127+
assert_se(path_equal(v, empty_to_root(path)));
128128
}
129129
}
130130

src/tmpfiles/tmpfiles.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,7 @@ static int create_item(Item *i) {
15021502

15031503
if (IN_SET(i->type, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA)) {
15041504

1505-
if (btrfs_is_subvol(isempty(arg_root) ? "/" : arg_root) <= 0)
1505+
if (btrfs_is_subvol(empty_to_root(arg_root)) <= 0)
15061506

15071507
/* Don't create a subvolume unless the
15081508
* root directory is one, too. We do

0 commit comments

Comments
 (0)
X Tutup