]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tmpfile-util: turn last parameter of link_tmpfile() into a proper flags
authorLennart Poettering <lennart@poettering.net>
Tue, 13 Jun 2023 07:40:53 +0000 (09:40 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 13 Jun 2023 07:40:53 +0000 (09:40 +0200)
This changes a boolean param into a proper bitflag field.

Given this only defines a single flag for now this doesn't look like
much of an improvement. But we'll add another flag shortly, where it
starts to make more sense.

src/basic/tmpfile-util.c
src/basic/tmpfile-util.h
src/boot/bootctl-install.c
src/coredump/coredump.c
src/portable/portable.c
src/shared/copy.c
src/shared/hwdb-util.c
src/test/test-tmpfile-util.c

index d44464dd7bd16fce94235551a4987a625fed72bd..fdc66c5b5e2468c2adf8365f97455b24555585a2 100644 (file)
@@ -349,7 +349,7 @@ static int link_fd(int fd, int newdirfd, const char *newpath) {
         return RET_NERRNO(linkat(fd, "", newdirfd, newpath, AT_EMPTY_PATH));
 }
 
-int link_tmpfile_at(int fd, int dir_fd, const char *path, const char *target, bool replace) {
+int link_tmpfile_at(int fd, int dir_fd, const char *path, const char *target, LinkTmpfileFlags flags) {
         _cleanup_free_ char *tmp = NULL;
         int r;
 
@@ -362,14 +362,14 @@ int link_tmpfile_at(int fd, int dir_fd, const char *path, const char *target, bo
          * is not supported on the directory, and renameat2() is used instead. */
 
         if (path) {
-                if (replace)
+                if (FLAGS_SET(flags, LINK_TMPFILE_REPLACE))
                         return RET_NERRNO(renameat(dir_fd, path, dir_fd, target));
 
                 return rename_noreplace(dir_fd, path, dir_fd, target);
         }
 
         r = link_fd(fd, dir_fd, target);
-        if (r != -EEXIST || !replace)
+        if (r != -EEXIST || !FLAGS_SET(flags, LINK_TMPFILE_REPLACE))
                 return r;
 
         /* So the target already exists and we were asked to replace it. That sucks a bit, since the kernel's
@@ -394,7 +394,7 @@ int link_tmpfile_at(int fd, int dir_fd, const char *path, const char *target, bo
         return 0;
 }
 
-int flink_tmpfile(FILE *f, const char *path, const char *target, bool replace) {
+int flink_tmpfile(FILE *f, const char *path, const char *target, LinkTmpfileFlags flags) {
         int fd, r;
 
         assert(f);
@@ -408,7 +408,7 @@ int flink_tmpfile(FILE *f, const char *path, const char *target, bool replace) {
         if (r < 0)
                 return r;
 
-        return link_tmpfile(fd, path, target, replace);
+        return link_tmpfile(fd, path, target, flags);
 }
 
 int mkdtemp_malloc(const char *template, char **ret) {
index f48ce10e688207fc6b71fe0749c069d3a01ae517..44e9b75f22119c9d4015f919463e2c89f9a0473f 100644 (file)
@@ -29,11 +29,16 @@ static inline int open_tmpfile_linkable(const char *target, int flags, char **re
 }
 int fopen_tmpfile_linkable(const char *target, int flags, char **ret_path, FILE **ret_file);
 
-int link_tmpfile_at(int fd, int dir_fd, const char *path, const char *target, bool replace);
-static inline int link_tmpfile(int fd, const char *path, const char *target, bool replace) {
-        return link_tmpfile_at(fd, AT_FDCWD, path, target, replace);
+
+typedef enum LinkTmpfileFlags {
+        LINK_TMPFILE_REPLACE = 1 << 0,
+} LinkTmpfileFlags;
+
+int link_tmpfile_at(int fd, int dir_fd, const char *path, const char *target, LinkTmpfileFlags flags);
+static inline int link_tmpfile(int fd, const char *path, const char *target, LinkTmpfileFlags flags) {
+        return link_tmpfile_at(fd, AT_FDCWD, path, target, flags);
 }
-int flink_tmpfile(FILE *f, const char *path, const char *target, bool replace);
+int flink_tmpfile(FILE *f, const char *path, const char *target, LinkTmpfileFlags flags);
 
 int mkdtemp_malloc(const char *template, char **ret);
 int mkdtemp_open(const char *template, int flags, char **ret);
index 2dd50360ecd471a21a1600fcf566a6a39e252f7e..26e18e3707ac28fa9da0082b72732208d6af32df 100644 (file)
@@ -454,7 +454,7 @@ static int install_loader_config(const char *esp_path) {
                 fprintf(f, "default %s-*\n", arg_entry_token);
         }
 
-        r = flink_tmpfile(f, t, p, /* replace= */ false);
+        r = flink_tmpfile(f, t, p, /* flags= */ 0);
         if (r == -EEXIST)
                 return 0; /* Silently skip creation if the file exists now (recheck) */
         if (r < 0)
@@ -483,7 +483,7 @@ static int install_loader_specification(const char *root) {
 
         fprintf(f, "type1\n");
 
-        r = flink_tmpfile(f, t, p, /* replace= */ false);
+        r = flink_tmpfile(f, t, p, /* flags= */ 0);
         if (r == -EEXIST)
                 return 0; /* Silently skip creation if the file exists now (recheck) */
         if (r < 0)
index b355970f0ffa61b27eed8f12dd23280b2d990f27..ac1c26e82856ff702e3081d411a3f136768a2457 100644 (file)
@@ -273,7 +273,7 @@ static int fix_permissions(
         if (r < 0)
                 return log_error_errno(r, "Failed to sync coredump %s: %m", coredump_tmpfile_name(filename));
 
-        r = link_tmpfile(fd, filename, target, /* replace= */ false);
+        r = link_tmpfile(fd, filename, target, /* flags= */ 0);
         if (r < 0)
                 return log_error_errno(r, "Failed to move coredump %s into place: %m", target);
 
index 1c31377567812812fb1026938fe306653ad64b2b..44c69c0bebbc431e857372e5744a0398b94472f5 100644 (file)
@@ -1295,7 +1295,7 @@ static int attach_unit_file(
                 if (fchmod(fd, 0644) < 0)
                         return log_debug_errno(errno, "Failed to change unit file access mode for '%s': %m", path);
 
-                r = link_tmpfile(fd, tmp, path, /* replace= */ false);
+                r = link_tmpfile(fd, tmp, path, /* flags= */ 0);
                 if (r < 0)
                         return log_debug_errno(r, "Failed to install unit file '%s': %m", path);
 
index 6b9d4b99423056f98f17c67f70eb05cb077fd347..9c2217856182f47d937799a4edc5f900a396ee85 100644 (file)
@@ -1465,7 +1465,7 @@ int copy_file_atomic_at_full(
                         return -errno;
         }
 
-        r = link_tmpfile_at(fdt, dir_fdt, t, to, copy_flags & COPY_REPLACE);
+        r = link_tmpfile_at(fdt, dir_fdt, t, to, (copy_flags & COPY_REPLACE) ? LINK_TMPFILE_REPLACE : 0);
         if (r < 0)
                 return r;
 
index a2fbcd7078d3a9ad75a26c4d2ad1583cc4750c44..cf41d6a474aac47a8388aa08e3703dead9c25f19 100644 (file)
@@ -408,7 +408,7 @@ static int trie_store(struct trie *trie, const char *filename, bool compat) {
                 return -errno;
         fwrite(&h, sizeof(struct trie_header_f), 1, f);
 
-        r = flink_tmpfile(f, filename_tmp, filename, /* replace= */ true);
+        r = flink_tmpfile(f, filename_tmp, filename, LINK_TMPFILE_REPLACE);
         if (r < 0)
                 return r;
 
index 455b733422af8e5a20bdecdeebb6181f095be6d0..4859f62da8431cde9aba86a93e6d7a1ec16b7f24 100644 (file)
@@ -278,9 +278,9 @@ TEST(link_tmpfile) {
         assert_se(write(fd, "foobar\n", 7) == 7);
 
         assert_se(touch(d) >= 0);
-        assert_se(link_tmpfile(fd, tmp, d, /* replace= */ false) == -EEXIST);
+        assert_se(link_tmpfile(fd, tmp, d, /* flags= */ 0) == -EEXIST);
         assert_se(unlink(d) >= 0);
-        assert_se(link_tmpfile(fd, tmp, d, /* replace= */ false) >= 0);
+        assert_se(link_tmpfile(fd, tmp, d, /* flags= */ 0) >= 0);
 
         assert_se(read_one_line_file(d, &line) >= 0);
         assert_se(streq(line, "foobar"));
@@ -293,8 +293,8 @@ TEST(link_tmpfile) {
 
         assert_se(write(fd, "waumiau\n", 8) == 8);
 
-        assert_se(link_tmpfile(fd, tmp, d, /* replace= */ false) == -EEXIST);
-        assert_se(link_tmpfile(fd, tmp, d, /* replace= */ true) >= 0);
+        assert_se(link_tmpfile(fd, tmp, d, /* flags= */ 0) == -EEXIST);
+        assert_se(link_tmpfile(fd, tmp, d, LINK_TMPFILE_REPLACE) >= 0);
 
         line = mfree(line);
         assert_se(read_one_line_file(d, &line) >= 0);