]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
bootctl: generalize open_tmpfile_linkable() use a bit
authorLennart Poettering <lennart@poettering.net>
Thu, 17 Mar 2022 17:18:04 +0000 (18:18 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 21 Mar 2022 23:34:48 +0000 (00:34 +0100)
We want FILE* here, instead of a plain fd. Let's generalize this in
tmpfile-util.c, so we can reuse it later easily.

src/basic/tmpfile-util.c
src/basic/tmpfile-util.h
src/boot/bootctl.c

index cf3bbad1c427d87d857a602c824c418b05c4fdb2..e0a338c163a726b595ecd17c934c81f39aab4be7 100644 (file)
@@ -275,6 +275,28 @@ int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
         return fd;
 }
 
+int fopen_tmpfile_linkable(const char *target, int flags, char **ret_path, FILE **ret_file) {
+        _cleanup_free_ char *path = NULL;
+        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_close_ int fd = -1;
+
+        assert(target);
+        assert(ret_file);
+        assert(ret_path);
+
+        fd = open_tmpfile_linkable(target, flags, &path);
+        if (fd < 0)
+                return fd;
+
+        f = take_fdopen(&fd, "w");
+        if (!f)
+                return -ENOMEM;
+
+        *ret_path = TAKE_PTR(path);
+        *ret_file = TAKE_PTR(f);
+        return 0;
+}
+
 int link_tmpfile(int fd, const char *path, const char *target) {
         assert(fd >= 0);
         assert(target);
@@ -292,6 +314,23 @@ int link_tmpfile(int fd, const char *path, const char *target) {
         return RET_NERRNO(linkat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), AT_FDCWD, target, AT_SYMLINK_FOLLOW));
 }
 
+int flink_tmpfile(FILE *f, const char *path, const char *target) {
+        int fd, r;
+
+        assert(f);
+        assert(target);
+
+        fd = fileno(f);
+        if (fd < 0) /* Not all FILE* objects encapsulate fds */
+                return -EBADF;
+
+        r = fflush_sync_and_check(f);
+        if (r < 0)
+                return r;
+
+        return link_tmpfile(fd, path, target);
+}
+
 int mkdtemp_malloc(const char *template, char **ret) {
         _cleanup_free_ char *p = NULL;
         int r;
index 45255fc062fc954f128c9fd2bb46793ed20a6699..610cbaf87e76eb4bc55c5f113ea8b8f6edcb7431 100644 (file)
@@ -13,7 +13,9 @@ int tempfn_random_child(const char *p, const char *extra, char **ret);
 
 int open_tmpfile_unlinkable(const char *directory, int flags);
 int open_tmpfile_linkable(const char *target, int flags, char **ret_path);
+int fopen_tmpfile_linkable(const char *target, int flags, char **ret_path, FILE **ret_file);
 
 int link_tmpfile(int fd, const char *path, const char *target);
+int flink_tmpfile(FILE *f, const char *path, const char *target);
 
 int mkdtemp_malloc(const char *template, char **ret);
index 54d1630aab75eea027d07d18950517ef02225dd0..10ed5c6f635177e798c635aa4c199a8c4e123f14 100644 (file)
@@ -1286,7 +1286,6 @@ static int remove_loader_variables(void) {
 static int install_loader_config(const char *esp_path) {
         _cleanup_(unlink_and_freep) char *t = NULL;
         _cleanup_fclose_ FILE *f = NULL;
-        _cleanup_close_ int fd = -1;
         const char *p;
         int r;
 
@@ -1296,13 +1295,9 @@ static int install_loader_config(const char *esp_path) {
         if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
                 return 0;
 
-        fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t);
-        if (fd < 0)
-                return log_error_errno(fd, "Failed to open \"%s\" for writing: %m", p);
-
-        f = take_fdopen(&fd, "w");
-        if (!f)
-                return log_oom();
+        r = fopen_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t, &f);
+        if (r < 0)
+                return log_error_errno(r, "Failed to open \"%s\" for writing: %m", p);
 
         fprintf(f, "#timeout 3\n"
                    "#console-mode keep\n");
@@ -1312,11 +1307,7 @@ static int install_loader_config(const char *esp_path) {
                 fprintf(f, "default %s-*\n", arg_entry_token);
         }
 
-        r = fflush_sync_and_check(f);
-        if (r < 0)
-                return log_error_errno(r, "Failed to write \"%s\": %m", p);
-
-        r = link_tmpfile(fileno(f), t, p);
+        r = flink_tmpfile(f, t, p);
         if (r == -EEXIST)
                 return 0; /* Silently skip creation if the file exists now (recheck) */
         if (r < 0)