From: Lennart Poettering Date: Wed, 21 Dec 2022 15:51:05 +0000 (+0100) Subject: shared: port various shared helpers basename() → path_extract_filename() X-Git-Tag: v253-rc1~214^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=03469b770bbe056b8df3bd109926b3941c0da878;p=thirdparty%2Fsystemd.git shared: port various shared helpers basename() → path_extract_filename() --- diff --git a/src/shared/btrfs-util.c b/src/shared/btrfs-util.c index 0a4bd48b880..0ef3c608bbe 100644 --- a/src/shared/btrfs-util.c +++ b/src/shared/btrfs-util.c @@ -51,20 +51,22 @@ static int validate_subvolume_name(const char *name) { return 0; } -static int extract_subvolume_name(const char *path, const char **subvolume) { - const char *fn; +static int extract_subvolume_name(const char *path, char **ret) { + _cleanup_free_ char *fn = NULL; int r; assert(path); - assert(subvolume); + assert(ret); - fn = basename(path); + r = path_extract_filename(path, &fn); + if (r < 0) + return r; r = validate_subvolume_name(fn); if (r < 0) return r; - *subvolume = fn; + *ret = TAKE_PTR(fn); return 0; } @@ -119,8 +121,8 @@ int btrfs_subvol_make_fd(int fd, const char *subvolume) { } int btrfs_subvol_make(const char *path) { + _cleanup_free_ char *subvolume = NULL; _cleanup_close_ int fd = -EBADF; - const char *subvolume; int r; assert(path); @@ -1180,8 +1182,8 @@ static int subvol_remove_children(int fd, const char *subvolume, uint64_t subvol } int btrfs_subvol_remove(const char *path, BtrfsRemoveFlags flags) { + _cleanup_free_ char *subvolume = NULL; _cleanup_close_ int fd = -EBADF; - const char *subvolume; int r; assert(path); @@ -1578,8 +1580,8 @@ int btrfs_subvol_snapshot_fd_full( copy_progress_bytes_t progress_bytes, void *userdata) { + _cleanup_free_ char *subvolume = NULL; _cleanup_close_ int new_fd = -EBADF; - const char *subvolume; int r; assert(old_fd >= 0); diff --git a/src/shared/exec-util.c b/src/shared/exec-util.c index cd922b84d4d..f5283f9df45 100644 --- a/src/shared/exec-util.c +++ b/src/shared/exec-util.c @@ -18,6 +18,7 @@ #include "hashmap.h" #include "macro.h" #include "missing_syscall.h" +#include "path-util.h" #include "process-util.h" #include "rlimit-util.h" #include "serialize.h" @@ -131,7 +132,13 @@ static int do_execute( return log_oom(); if (callbacks) { - fd = open_serialization_fd(basename(*path)); + _cleanup_free_ char *bn = NULL; + + r = path_extract_filename(*path, &bn); + if (r < 0) + return log_error_errno(r, "Failed to extract filename from path '%s': %m", *path); + + fd = open_serialization_fd(bn); if (fd < 0) return log_error_errno(fd, "Failed to open serialization file: %m"); } @@ -199,15 +206,16 @@ int execute_directories( ExecDirFlags flags) { char **dirs = (char**) directories; + _cleanup_free_ char *name = NULL; _cleanup_close_ int fd = -EBADF; - char *name; int r; pid_t executor_pid; assert(!strv_isempty(dirs)); - name = basename(dirs[0]); - assert(!isempty(name)); + r = path_extract_filename(dirs[0], &name); + if (r < 0) + return log_error_errno(r, "Failed to extract file name from '%s': %m", dirs[0]); if (callbacks) { assert(callback_args); diff --git a/src/shared/generator.c b/src/shared/generator.c index 7eed3b76c17..f8719b53236 100644 --- a/src/shared/generator.c +++ b/src/shared/generator.c @@ -60,13 +60,19 @@ int generator_open_unit_file( } int generator_add_symlink(const char *dir, const char *dst, const char *dep_type, const char *src) { + _cleanup_free_ char *bn = NULL; + const char *from, *to; + int r; + /* Adds a symlink from ./ to (if src is absolute) * or ../ (otherwise). */ - const char *from, *to; + r = path_extract_filename(src, &bn); + if (r < 0) + return log_error_errno(r, "Failed to extract filename from '%s': %m", src); from = path_is_absolute(src) ? src : strjoina("../", src); - to = strjoina(dir, "/", dst, ".", dep_type, "/", basename(src)); + to = strjoina(dir, "/", dst, ".", dep_type, "/", bn); (void) mkdir_parents_label(to, 0755); if (symlink(from, to) < 0) diff --git a/src/shared/lockfile-util.c b/src/shared/lockfile-util.c index b920f3fe9be..5bbccead7f6 100644 --- a/src/shared/lockfile-util.c +++ b/src/shared/lockfile-util.c @@ -83,18 +83,23 @@ int make_lock_file(const char *p, int operation, LockFile *ret) { } int make_lock_file_for(const char *p, int operation, LockFile *ret) { - const char *fn; - char *t; + _cleanup_free_ char *fn = NULL, *dn = NULL, *t = NULL; + int r; assert(p); assert(ret); - fn = basename(p); - if (!filename_is_valid(fn)) - return -EINVAL; + r = path_extract_filename(p, &fn); + if (r < 0) + return r; + + r = path_extract_directory(p, &dn); + if (r < 0) + return r; - t = newa(char, strlen(p) + 2 + 4 + 1); - stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn), ".lck"); + t = strjoin(dn, "/.#", fn, ".lck"); + if (!t) + return -ENOMEM; return make_lock_file(t, operation, ret); } diff --git a/src/shared/mount-util.c b/src/shared/mount-util.c index 603eb7b780f..8aad531a4d9 100644 --- a/src/shared/mount-util.c +++ b/src/shared/mount-util.c @@ -952,7 +952,7 @@ static int mount_in_namespace( if (r < 0) goto finish; if (r == 0) { - const char *mount_inside; + _cleanup_free_ char *mount_outside_fn = NULL, *mount_inside = NULL; errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); @@ -965,8 +965,19 @@ static int mount_in_namespace( } /* Fifth, move the mount to the right place inside */ - mount_inside = strjoina(incoming_path, basename(mount_outside)); - r = mount_nofollow_verbose(LOG_ERR, mount_inside, dest, NULL, MS_MOVE, NULL); + r = path_extract_filename(mount_outside, &mount_outside_fn); + if (r < 0) { + log_debug_errno(r, "Failed to extract filename from propagation file or directory '%s': %m", mount_outside); + goto child_fail; + } + + mount_inside = path_join(incoming_path, mount_outside_fn); + if (!mount_inside) { + r = log_oom_debug(); + goto child_fail; + } + + r = mount_nofollow_verbose(LOG_DEBUG, mount_inside, dest, NULL, MS_MOVE, NULL); if (r < 0) goto child_fail;