]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tmpfile-util: Introduce fopen_temporary_child()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 17 Nov 2022 13:12:48 +0000 (14:12 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 17 Nov 2022 13:14:31 +0000 (14:14 +0100)
Instead of having fopen_temporary() create the file either next
to an existing file or in tmp/, let's split this up clearly into
two different functions, one for creating temporary files next to
existing files, and one for creating a temporary file in a directory.

src/basic/tmpfile-util.c
src/basic/tmpfile-util.h
src/home/homework-cifs.c
src/partition/repart.c
src/shared/mkfs-util.c
src/shared/tmpfile-util-label.c
src/test/test-uid-range.c

index 909057429d4ce23bcf778a46bbc5c82ae155327b..dbbd54027e6ac74295f272e9a9aa7bce0531cdfc 100644 (file)
 #include "tmpfile-util.h"
 #include "umask-util.h"
 
-int fopen_temporary_at(int dir_fd, const char *path, FILE **ret_file, char **ret_temp_path) {
+static int fopen_temporary_internal(int dir_fd, const char *path, FILE **ret_file) {
         _cleanup_fclose_ FILE *f = NULL;
-        _cleanup_free_ char *t = NULL;
         _cleanup_close_ int fd = -1;
         int r;
 
         assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
+        assert(path);
 
-        if (path) {
-                r = tempfn_random(path, NULL, &t);
-                if (r < 0)
-                        return r;
-        } else {
-                const char *d;
-
-                r = tmp_dir(&d);
-                if (r < 0)
-                        return r;
-
-                r = tempfn_random_child(d, NULL, &t);
-                if (r < 0)
-                        return r;
-        }
-
-        fd = openat(dir_fd, t, O_CLOEXEC|O_NOCTTY|O_RDWR|O_CREAT|O_EXCL, 0600);
+        fd = openat(dir_fd, path, O_CLOEXEC|O_NOCTTY|O_RDWR|O_CREAT|O_EXCL, 0600);
         if (fd < 0)
                 return -errno;
 
@@ -52,15 +36,59 @@ int fopen_temporary_at(int dir_fd, const char *path, FILE **ret_file, char **ret
 
         r = take_fdopen_unlocked(&fd, "w", &f);
         if (r < 0) {
-                (void) unlinkat(dir_fd, t, 0);
+                (void) unlinkat(dir_fd, path, 0);
                 return r;
         }
 
         if (ret_file)
                 *ret_file = TAKE_PTR(f);
 
-        if (ret_temp_path)
-                *ret_temp_path = TAKE_PTR(t);
+        return 0;
+}
+
+int fopen_temporary_at(int dir_fd, const char *path, FILE **ret_file, char **ret_path) {
+        _cleanup_free_ char *t = NULL;
+        int r;
+
+        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
+        assert(path);
+
+        r = tempfn_random(path, NULL, &t);
+        if (r < 0)
+                return r;
+
+        r = fopen_temporary_internal(dir_fd, t, ret_file);
+        if (r < 0)
+                return r;
+
+        if (ret_path)
+                *ret_path = TAKE_PTR(t);
+
+        return 0;
+}
+
+int fopen_temporary_child_at(int dir_fd, const char *path, FILE **ret_file, char **ret_path) {
+        _cleanup_free_ char *t = NULL;
+        int r;
+
+        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
+
+        if (!path) {
+                r = tmp_dir(&path);
+                if (r < 0)
+                        return r;
+        }
+
+        r = tempfn_random_child(path, NULL, &t);
+        if (r < 0)
+                return r;
+
+        r = fopen_temporary_internal(dir_fd, t, ret_file);
+        if (r < 0)
+                return r;
+
+        if (ret_path)
+                *ret_path = TAKE_PTR(t);
 
         return 0;
 }
index 4af28b9da34be3dd92690b95d4a925e79dbaf5bd..e5b7709e3f9dee234ef47944a359e19e7f85fe3a 100644 (file)
@@ -8,6 +8,12 @@ int fopen_temporary_at(int dir_fd, const char *path, FILE **ret_file, char **ret
 static inline int fopen_temporary(const char *path, FILE **ret_file, char **ret_path) {
         return fopen_temporary_at(AT_FDCWD, path, ret_file, ret_path);
 }
+
+int fopen_temporary_child_at(int dir_fd, const char *path, FILE **ret_file, char **ret_path);
+static inline int fopen_temporary_child(const char *path, FILE **ret_file, char **ret_path) {
+        return fopen_temporary_child_at(AT_FDCWD, path, ret_file, ret_path);
+}
+
 int mkostemp_safe(char *pattern);
 int fmkostemp_safe(char *pattern, const char *mode, FILE**_f);
 
index e79def3daec75a3c5215eb9c9f45b3b815fa813d..8ad1da6303d55acab38479c390b49223d81cacfa 100644 (file)
@@ -64,7 +64,7 @@ int home_setup_cifs(
                 pid_t mount_pid;
                 int exit_status;
 
-                r = fopen_temporary(NULL, &f, &p);
+                r = fopen_temporary_child(NULL, &f, &p);
                 if (r < 0)
                         return log_error_errno(r, "Failed to create temporary credentials file: %m");
 
index 5d4cdbe6892d113b4df4317e4c5bce360fc38bd1..9ab051190ddc28b841ac3e346dc86c40868b3773 100644 (file)
@@ -3194,7 +3194,7 @@ static int partition_encrypt(Context *context, Partition *p, const char *node) {
 
         log_info("Encrypting future partition %" PRIu64 "...", p->partno);
 
-        r = fopen_temporary(NULL, &h, &hp);
+        r = fopen_temporary_child(NULL, &h, &hp);
         if (r < 0)
                 return log_error_errno(r, "Failed to create temporary LUKS header file: %m");
 
index 19ffd9151ba708e0ed950712c34edad4c6b3454e..2070965387d9f7cca17f580ec011d3e55d98895c 100644 (file)
@@ -254,7 +254,7 @@ static int make_protofile(const char *root, char **ret) {
 
         assert(ret);
 
-        r = fopen_temporary(NULL, &f, &p);
+        r = fopen_temporary_child(NULL, &f, &p);
         if (r < 0)
                 return log_error_errno(r, "Failed to open temporary file: %m");
 
index d37c0b08456972fb097ef6a517f760467ef61e48..17c5038b51b77a4fc9f07b1f2c1bc508f2379801 100644 (file)
@@ -14,6 +14,8 @@ int fopen_temporary_label(
 
         int r;
 
+        assert(path);
+
         r = mac_selinux_create_file_prepare(target, S_IFREG);
         if (r < 0)
                 return r;
index c75957317391195db6391459dfe8a23668c1a700..186f6ee29c8d8e7b78efec123c1b0139754fba2b 100644 (file)
@@ -114,7 +114,7 @@ TEST(load_userns) {
                 assert_se(uid_range_covers(p, 0, UINT32_MAX));
         }
 
-        assert_se(fopen_temporary(NULL, &f, &fn) >= 0);
+        assert_se(fopen_temporary_child(NULL, &f, &fn) >= 0);
         fputs("0 0 20\n"
               "100 0 20\n", f);
         assert_se(fflush_and_check(f) >= 0);