From b36e39d2eb3605378f67b937b212e7bf0d5a30b3 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 21 Dec 2022 16:49:03 +0100 Subject: [PATCH] =?UTF-8?q?nspawn:=20port=20over=20basename()=20=E2=86=92?= =?UTF-8?q?=20path=5Fextract=5Ffilename()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/nspawn/nspawn-util.c | 6 +++++- src/nspawn/nspawn.c | 13 ++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/nspawn/nspawn-util.c b/src/nspawn/nspawn-util.c index 830ac39e73..6c3848916d 100644 --- a/src/nspawn/nspawn-util.c +++ b/src/nspawn/nspawn-util.c @@ -47,10 +47,14 @@ int systemd_installation_has_version(const char *root, const char *minimal_versi *c = '\0'; /* truncate the glob part */ STRV_FOREACH(name, names) { + _cleanup_free_ char *bn = NULL; /* This is most likely to run only once, hence let's not optimize anything. */ char *t, *t2; - t = startswith(basename(*name), "libsystemd-shared-"); + if (path_extract_filename(*name, &bn) < 0) + continue; + + t = startswith(bn, "libsystemd-shared-"); if (!t) continue; diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 067efbe3f0..551f8f975c 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -3045,16 +3045,19 @@ static int determine_names(void) { else if (arg_image) { char *e; - arg_machine = strdup(basename(arg_image)); + r = path_extract_filename(arg_image, &arg_machine); + if (r < 0) + return log_error_errno(r, "Failed to extract file name from '%s': %m", arg_image); /* Truncate suffix if there is one */ e = endswith(arg_machine, ".raw"); if (e) *e = 0; - } else - arg_machine = strdup(basename(arg_directory)); - if (!arg_machine) - return log_oom(); + } else { + r = path_extract_filename(arg_directory, &arg_machine); + if (r < 0) + return log_error_errno(r, "Failed to extract file name from '%s': %m", arg_directory); + } hostname_cleanup(arg_machine); if (!hostname_is_valid(arg_machine, 0))