]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
generator: add generator_open_unit_file_full to allow creating temporary units
authorAntonio Alvarez Feijoo <antonio.feijoo@suse.com>
Mon, 10 Apr 2023 13:18:33 +0000 (15:18 +0200)
committerAntonio Alvarez Feijoo <antonio.feijoo@suse.com>
Mon, 10 Apr 2023 13:18:33 +0000 (15:18 +0200)
This function is like `generator_open_unit_file`, but if `ret_temp_path` is
passed, a temporary unit is created instead.

src/shared/generator.c
src/shared/generator.h

index 2ed4be2bf3a4efa29a2c574bc4fffa816bfa6a23..b16d0a0ef23e84d4606d4d8daa7218b3ede43e40 100644 (file)
 #include "specifier.h"
 #include "string-util.h"
 #include "time-util.h"
+#include "tmpfile-util.h"
 #include "unit-name.h"
 
-int generator_open_unit_file(
+int generator_open_unit_file_full(
                 const char *dir,
                 const char *source,
                 const char *fn,
-                FILE **ret) {
+                FILE **ret_file,
+                char **ret_temp_path) {
 
         _cleanup_free_ char *p = NULL;
         FILE *f;
         int r;
 
         assert(dir);
-        assert(fn);
-        assert(ret);
+        assert(ret_file);
 
-        p = path_join(dir, fn);
-        if (!p)
-                return log_oom();
+        /* If <ret_temp_path> is specified, it creates a temporary unit file and also returns its
+         * temporary path. */
 
-        r = fopen_unlocked(p, "wxe", &f);
-        if (r < 0) {
-                if (source && r == -EEXIST)
-                        return log_error_errno(r,
-                                               "Failed to create unit file '%s', as it already exists. Duplicate entry in '%s'?",
-                                               p, source);
+        if (ret_temp_path) {
+                r = fopen_temporary(dir, &f, &p);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to create temporary unit file in '%s': %m", dir);
+
+                (void) fchmod(fileno(f), 0644);
 
-                return log_error_errno(r, "Failed to create unit file '%s': %m", p);
+                *ret_temp_path = TAKE_PTR(p);
+        } else {
+                assert(fn);
+
+                p = path_join(dir, fn);
+                if (!p)
+                        return log_oom();
+
+                r = fopen_unlocked(p, "wxe", &f);
+                if (r < 0) {
+                        if (source && r == -EEXIST)
+                                return log_error_errno(r,
+                                                       "Failed to create unit file '%s', as it already exists. Duplicate entry in '%s'?",
+                                                       p, source);
+
+                        return log_error_errno(r, "Failed to create unit file '%s': %m", p);
+                }
         }
 
         fprintf(f,
                 "# Automatically generated by %s\n\n",
                 program_invocation_short_name);
 
-        *ret = f;
+        *ret_file = f;
         return 0;
 }
 
index 111900fd45143d6c24694fd2bccbb32633974b09..d97d6edc676cf6ef26aa23eb068d42d7ae56c564 100644 (file)
@@ -6,11 +6,11 @@
 #include "macro.h"
 #include "main-func.h"
 
-int generator_open_unit_file(
-        const char *dest,
-        const char *source,
-        const char *name,
-        FILE **file);
+int generator_open_unit_file_full(const char *dest, const char *source, const char *name, FILE **ret_file, char **ret_temp_path);
+
+static inline int generator_open_unit_file(const char *dest, const char *source, const char *name, FILE **ret_file) {
+        return generator_open_unit_file_full(dest, source, name, ret_file, NULL);
+}
 
 int generator_add_symlink_full(const char *dir, const char *dst, const char *dep_type, const char *src, const char *instance);