]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: port various bits over to open_parent() 9668/head
authorLennart Poettering <lennart@poettering.net>
Fri, 20 Jul 2018 10:02:14 +0000 (12:02 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 23 Jul 2018 11:38:18 +0000 (13:38 +0200)
src/basic/fileio.c
src/basic/fs-util.c
src/basic/mount-util.c
src/import/export-raw.c
src/machine/machine-dbus.c

index 9ff9118031da85fc81f6b49757afa7c30fc25603..20d3f567c9d3fef8dc8f8f6e5efb57091296c354 100644 (file)
@@ -1449,22 +1449,14 @@ int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
          * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
          * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
 
-        {
-                _cleanup_free_ char *dn = NULL;
-
-                dn = dirname_malloc(target);
-                if (!dn)
-                        return -ENOMEM;
-
-                fd = open(dn, O_TMPFILE|flags, 0640);
-                if (fd >= 0) {
-                        *ret_path = NULL;
-                        return fd;
-                }
-
-                log_debug_errno(errno, "Failed to use O_TMPFILE on %s: %m", dn);
+        fd = open_parent(target, O_TMPFILE|flags, 0640);
+        if (fd >= 0) {
+                *ret_path = NULL;
+                return fd;
         }
 
+        log_debug_errno(fd, "Failed to use O_TMPFILE for %s: %m", target);
+
         r = tempfn_random(target, NULL, &tmp);
         if (r < 0)
                 return r;
index 567f4af1cad04064c01b742b4107d8ccf90bc6ce..aca9921de7efce9cee1287a7c69ac8f3cf9fc2e0 100644 (file)
@@ -1156,7 +1156,7 @@ int unlinkat_deallocate(int fd, const char *name, int flags) {
 }
 
 int fsync_directory_of_file(int fd) {
-        _cleanup_free_ char *path = NULL, *dn = NULL;
+        _cleanup_free_ char *path = NULL;
         _cleanup_close_ int dfd = -1;
         int r;
 
@@ -1182,13 +1182,9 @@ int fsync_directory_of_file(int fd) {
         if (!path_is_absolute(path))
                 return -EINVAL;
 
-        dn = dirname_malloc(path);
-        if (!dn)
-                return -ENOMEM;
-
-        dfd = open(dn, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
+        dfd = open_parent(path, O_CLOEXEC, 0);
         if (dfd < 0)
-                return -errno;
+                return dfd;
 
         if (fsync(dfd) < 0)
                 return -errno;
index ebe41a4c6c706c7e2951ab43a766631bbd8777ce..54d911b095d54ea9850de65b8a85ded817ef8bd2 100644 (file)
@@ -261,7 +261,7 @@ fallback_fstat:
 
 /* flags can be AT_SYMLINK_FOLLOW or 0 */
 int path_is_mount_point(const char *t, const char *root, int flags) {
-        _cleanup_free_ char *canonical = NULL, *parent = NULL;
+        _cleanup_free_ char *canonical = NULL;
         _cleanup_close_ int fd = -1;
         int r;
 
@@ -283,11 +283,7 @@ int path_is_mount_point(const char *t, const char *root, int flags) {
                 t = canonical;
         }
 
-        parent = dirname_malloc(t);
-        if (!parent)
-                return -ENOMEM;
-
-        fd = openat(AT_FDCWD, parent, O_DIRECTORY|O_CLOEXEC|O_PATH);
+        fd = open_parent(t, O_PATH|O_CLOEXEC, 0);
         if (fd < 0)
                 return -errno;
 
index f97ae461c5a1040efb02b1e7e9749388e4064df8..f3a34d40c51b1d718237291c441c166c8165b7e0 100644 (file)
@@ -16,6 +16,7 @@
 #include "export-raw.h"
 #include "fd-util.h"
 #include "fileio.h"
+#include "fs-util.h"
 #include "import-common.h"
 #include "missing.h"
 #include "ratelimit.h"
@@ -244,13 +245,9 @@ static int raw_export_on_defer(sd_event_source *s, void *userdata) {
 }
 
 static int reflink_snapshot(int fd, const char *path) {
-        char *p, *d;
         int new_fd, r;
 
-        p = strdupa(path);
-        d = dirname(p);
-
-        new_fd = open(d, O_TMPFILE|O_CLOEXEC|O_NOCTTY|O_RDWR, 0600);
+        new_fd = open_parent(path, O_TMPFILE|O_CLOEXEC|O_RDWR, 0600);
         if (new_fd < 0) {
                 _cleanup_free_ char *t = NULL;
 
index 7f41465ccde423b001b02bb628f05a0a2fc01df7..77e8b161b027eea1c7e9d6c1d3fd6640d938276e 100644 (file)
@@ -1058,7 +1058,7 @@ finish:
 }
 
 int bus_machine_method_copy(sd_bus_message *message, void *userdata, sd_bus_error *error) {
-        const char *src, *dest, *host_path, *container_path, *host_basename, *host_dirname, *container_basename, *container_dirname;
+        const char *src, *dest, *host_path, *container_path, *host_basename, *container_basename, *container_dirname;
         _cleanup_close_pair_ int errno_pipe_fd[2] = { -1, -1 };
         CopyFlags copy_flags = COPY_REFLINK|COPY_MERGE;
         _cleanup_close_ int hostfd = -1;
@@ -1119,16 +1119,14 @@ int bus_machine_method_copy(sd_bus_message *message, void *userdata, sd_bus_erro
         }
 
         host_basename = basename(host_path);
-        t = strdupa(host_path);
-        host_dirname = dirname(t);
 
         container_basename = basename(container_path);
         t = strdupa(container_path);
         container_dirname = dirname(t);
 
-        hostfd = open(host_dirname, O_CLOEXEC|O_RDONLY|O_NOCTTY|O_DIRECTORY);
+        hostfd = open_parent(host_path, O_CLOEXEC, 0);
         if (hostfd < 0)
-                return sd_bus_error_set_errnof(error, errno, "Failed to open host directory %s: %m", host_dirname);
+                return sd_bus_error_set_errnof(error, hostfd, "Failed to open host directory %s: %m", host_path);
 
         if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
                 return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m");