]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dissect: remove one more use of basename()
authorLennart Poettering <lennart@poettering.net>
Wed, 21 Dec 2022 15:46:28 +0000 (16:46 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 23 Dec 2022 14:04:19 +0000 (15:04 +0100)
And while we are at it, make use of path_extract_filename() return value
that indicates whether the path was suffixed with a slash.

src/dissect/dissect.c

index ed923c2c863b7911505c6d1a55c73a548e2427e1..1c62d8eafc9ba895892415a725484521d0ba1e59 100644 (file)
@@ -1110,6 +1110,7 @@ static int action_list_or_mtree_or_copy(DissectedImage *m, LoopDevice *d) {
         } else if (arg_action == ACTION_COPY_TO) {
                 _cleanup_close_ int source_fd = -EBADF, target_fd = -EBADF, dfd = -EBADF;
                 _cleanup_free_ char *dn = NULL, *bn = NULL;
+                bool is_dir;
 
                 r = path_extract_directory(arg_target, &dn);
                 if (r < 0)
@@ -1117,6 +1118,7 @@ static int action_list_or_mtree_or_copy(DissectedImage *m, LoopDevice *d) {
                 r = path_extract_filename(arg_target, &bn);
                 if (r < 0)
                         return log_error_errno(r, "Failed to extract filename from target path '%s': %m", arg_target);
+                is_dir = r == O_DIRECTORY;
 
                 r = chase_symlinks(dn, mounted_dir, CHASE_PREFIX_ROOT|CHASE_WARN, NULL, &dfd);
                 if (r < 0)
@@ -1124,6 +1126,9 @@ static int action_list_or_mtree_or_copy(DissectedImage *m, LoopDevice *d) {
 
                 /* Are we reading from stdin? */
                 if (streq(arg_source, "-")) {
+                        if (is_dir)
+                                return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot copy STDIN to a directory, refusing.");
+
                         target_fd = openat(dfd, bn, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_EXCL, 0644);
                         if (target_fd < 0)
                                 return log_error_errno(errno, "Failed to open target file '%s': %m", arg_target);
@@ -1161,8 +1166,11 @@ static int action_list_or_mtree_or_copy(DissectedImage *m, LoopDevice *d) {
                         return 0;
                 }
 
+                if (is_dir)
+                        return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Source is a regular file, but target is not, refusing.");
+
                 /* We area looking at a regular file */
-                target_fd = openat(dfd, basename(arg_target), O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_EXCL, 0600);
+                target_fd = openat(dfd, bn, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_EXCL, 0600);
                 if (target_fd < 0)
                         return log_error_errno(errno, "Failed to open target file '%s': %m", arg_target);