systemctl: replace basename() with path_extract_filename()

This commit is contained in:
Yu Watanabe
2025-06-06 19:00:15 +09:00
parent 59829bb37a
commit 44875628ff

View File

@@ -12,6 +12,7 @@
#include "format-table.h"
#include "hashmap.h"
#include "install.h"
#include "path-util.h"
#include "sort-util.h"
#include "string-util.h"
#include "strv.h"
@@ -21,25 +22,43 @@
static int compare_unit_file_list(const UnitFileList *a, const UnitFileList *b) {
const char *d1, *d2;
int r, s;
assert(a);
assert(a->path);
assert(b);
assert(b->path);
d1 = strrchr(a->path, '.');
d2 = strrchr(b->path, '.');
if (d1 && d2) {
int r;
r = strcasecmp(d1, d2);
if (r != 0)
return r;
}
return strcasecmp(basename(a->path), basename(b->path));
_cleanup_free_ char *f1 = NULL, *f2 = NULL;
r = path_extract_filename(a->path, &f1);
s = path_extract_filename(b->path, &f2);
if (r < 0 || s < 0)
return CMP(r, s);
return strcasecmp(f1, f2);
}
static bool output_show_unit_file(const UnitFileList *u, char **states, char **patterns) {
assert(u);
static int output_show_unit_file(const UnitFileList *u, char **states, char **patterns) {
int r;
if (!strv_fnmatch_or_empty(patterns, basename(u->path), FNM_NOESCAPE))
assert(u);
assert(u->path);
_cleanup_free_ char *id = NULL;
r = path_extract_filename(u->path, &id);
if (r < 0)
return log_debug_errno(r, "Failed to extract unit name from path %s: %m", u->path);
if (!strv_fnmatch_or_empty(patterns, id, FNM_NOESCAPE))
return false;
if (!strv_isempty(arg_types)) {
@@ -91,7 +110,7 @@ static int output_unit_file_list(const UnitFileList *units, unsigned c) {
table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
FOREACH_ARRAY(u, units, c) {
const char *on_underline = NULL, *on_unit_color = NULL, *id;
const char *on_underline = NULL, *on_unit_color = NULL;
bool underline;
underline = u + 1 < units + c &&
@@ -113,7 +132,10 @@ static int output_unit_file_list(const UnitFileList *units, unsigned c) {
else
on_unit_color = on_underline;
id = basename(u->path);
_cleanup_free_ char *id = NULL;
r = path_extract_filename(u->path, &id);
if (r < 0)
return log_error_errno(r, "Failed to extract unit name from path %s: %m", u->path);
r = table_add_many(table,
TABLE_STRING, id,
@@ -173,7 +195,7 @@ int verb_list_unit_files(int argc, char *argv[], void *userdata) {
UnitFileList *u;
HASHMAP_FOREACH(u, h) {
if (!output_show_unit_file(u, NULL, NULL))
if (output_show_unit_file(u, NULL, NULL) <= 0)
continue;
units[c++] = *u;
@@ -246,7 +268,7 @@ int verb_list_unit_files(int argc, char *argv[], void *userdata) {
if (output_show_unit_file(&units[c],
fallback ? arg_states : NULL,
fallback ? strv_skip(argv, 1) : NULL))
fallback ? strv_skip(argv, 1) : NULL) > 0)
c++;
}
if (r < 0)