]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared: port various shared helpers basename() → path_extract_filename()
authorLennart Poettering <lennart@poettering.net>
Wed, 21 Dec 2022 15:51:05 +0000 (16:51 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 23 Dec 2022 16:35:21 +0000 (17:35 +0100)
src/shared/btrfs-util.c
src/shared/exec-util.c
src/shared/generator.c
src/shared/lockfile-util.c
src/shared/mount-util.c

index 0a4bd48b880ffc65c2b9e05a213f3701ace14b11..0ef3c608bbeebbac107d521b24a59e23840e3f1b 100644 (file)
@@ -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);
index cd922b84d4d40ca965fff1348c616c8e5fa11bbb..f5283f9df459cbb1aaa34865af270a50dc46dad6 100644 (file)
@@ -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);
index 7eed3b76c17b00d66443aac263e0faaf4dd99d30..f8719b5323648034aba915595da688041767f1a1 100644 (file)
@@ -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 <dst>.<dep_type>/ to <src> (if src is absolute)
          * or ../<src> (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)
index b920f3fe9be309abd6fcf9de25f72948514aa3f9..5bbccead7f656adda3e6d8bb3bae80a5c00acf72 100644 (file)
@@ -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);
 }
index 603eb7b780f778e8cac43d92187d2eb8b1e9c7ed..8aad531a4d9e8d59de4239b3b15f29fb8be10487 100644 (file)
@@ -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;