]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fileio: consolidate write_string_file*()
authorDaniel Mack <daniel@zonque.org>
Mon, 6 Jul 2015 23:19:25 +0000 (19:19 -0400)
committerDaniel Mack <daniel@zonque.org>
Mon, 6 Jul 2015 23:19:25 +0000 (19:19 -0400)
Merge write_string_file(), write_string_file_no_create() and
write_string_file_atomic() into write_string_file() and provide a flags mask
that allows combinations of atomic writing, newline appending and automatic
file creation. Change all users accordingly.

29 files changed:
src/backlight/backlight.c
src/basic/capability.c
src/basic/cgroup-util.c
src/basic/fileio-label.c
src/basic/fileio.c
src/basic/fileio.h
src/basic/smack-util.c
src/basic/util.c
src/binfmt/binfmt.c
src/core/execute.c
src/core/machine-id-setup.c
src/core/main.c
src/core/smack-setup.c
src/firstboot/firstboot.c
src/gpt-auto-generator/gpt-auto-generator.c
src/hibernate-resume/hibernate-resume.c
src/login/logind-dbus.c
src/network/networkd-link.c
src/nspawn/nspawn.c
src/rfkill/rfkill.c
src/shared/sysctl-util.c
src/sleep/sleep.c
src/test/test-btrfs.c
src/test/test-copy.c
src/test/test-fileio.c
src/test/test-util.c
src/udev/udevd.c
src/user-sessions/user-sessions.c
src/vconsole/vconsole-setup.c

index c79ad6520c9f8668f0992c51d7506790f23e8726..c8961de946857c498eb3d6098bc9bb31e0a2a561 100644 (file)
@@ -415,7 +415,7 @@ int main(int argc, char *argv[]) {
                         return EXIT_FAILURE;
                 }
 
-                r = write_string_file(saved, value);
+                r = write_string_file(saved, value, WRITE_STRING_FILE_CREATE);
                 if (r < 0) {
                         log_error_errno(r, "Failed to write %s: %m", saved);
                         return EXIT_FAILURE;
index 58f00e6daec180c910bb72ff4cf83efd0c7c1ec3..8dbe4da5bbbe1280518e81bca9af166549f06bf1 100644 (file)
@@ -204,7 +204,7 @@ static int drop_from_file(const char *fn, uint64_t drop) {
         if (asprintf(&p, "%u %u", lo, hi) < 0)
                 return -ENOMEM;
 
-        r = write_string_file(fn, p);
+        r = write_string_file(fn, p, WRITE_STRING_FILE_CREATE);
         free(p);
 
         return r;
index 439c5516dc2cbfb2206c488dc123e3af4714a15e..34a30605092abdc980776c2686e675e9f0b80317 100644 (file)
@@ -646,7 +646,7 @@ int cg_attach(const char *controller, const char *path, pid_t pid) {
 
         snprintf(c, sizeof(c), PID_FMT"\n", pid);
 
-        return write_string_file_no_create(fs, c);
+        return write_string_file(fs, c, 0);
 }
 
 int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
@@ -820,7 +820,7 @@ int cg_install_release_agent(const char *controller, const char *agent) {
 
         sc = strstrip(contents);
         if (sc[0] == 0) {
-                r = write_string_file_no_create(fs, agent);
+                r = write_string_file(fs, agent, 0);
                 if (r < 0)
                         return r;
         } else if (!streq(sc, agent))
@@ -840,7 +840,7 @@ int cg_install_release_agent(const char *controller, const char *agent) {
 
         sc = strstrip(contents);
         if (streq(sc, "0")) {
-                r = write_string_file_no_create(fs, "1");
+                r = write_string_file(fs, "1", 0);
                 if (r < 0)
                         return r;
 
@@ -861,7 +861,7 @@ int cg_uninstall_release_agent(const char *controller) {
         if (r < 0)
                 return r;
 
-        r = write_string_file_no_create(fs, "0");
+        r = write_string_file(fs, "0", 0);
         if (r < 0)
                 return r;
 
@@ -872,7 +872,7 @@ int cg_uninstall_release_agent(const char *controller) {
         if (r < 0)
                 return r;
 
-        r = write_string_file_no_create(fs, "");
+        r = write_string_file(fs, "", 0);
         if (r < 0)
                 return r;
 
@@ -1708,7 +1708,7 @@ int cg_set_attribute(const char *controller, const char *path, const char *attri
         if (r < 0)
                 return r;
 
-        return write_string_file_no_create(p, value);
+        return write_string_file(p, value, 0);
 }
 
 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
index bec988ca7872c143bf794872c4612068e6c0f958..f596f1d11fc6d54be479bfd8f13c35079c5cf857 100644 (file)
@@ -31,7 +31,7 @@ int write_string_file_atomic_label(const char *fn, const char *line) {
         if (r < 0)
                 return r;
 
-        r = write_string_file_atomic(fn, line);
+        r = write_string_file(fn, line, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
 
         mac_selinux_create_file_clear();
 
index 82b40aa7daf93532dfb5d5d8f43b087d9ee66218..d592bf5ac9c2f5126fd9e01f5ef878c905abc86a 100644 (file)
@@ -45,42 +45,7 @@ int write_string_stream(FILE *f, const char *line, bool enforce_newline) {
         return 0;
 }
 
-int write_string_file(const char *fn, const char *line) {
-        _cleanup_fclose_ FILE *f = NULL;
-
-        assert(fn);
-        assert(line);
-
-        f = fopen(fn, "we");
-        if (!f)
-                return -errno;
-
-        return write_string_stream(f, line, true);
-}
-
-int write_string_file_no_create(const char *fn, const char *line) {
-        _cleanup_fclose_ FILE *f = NULL;
-        int fd;
-
-        assert(fn);
-        assert(line);
-
-        /* We manually build our own version of fopen(..., "we") that
-         * works without O_CREAT */
-        fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY);
-        if (fd < 0)
-                return -errno;
-
-        f = fdopen(fd, "we");
-        if (!f) {
-                safe_close(fd);
-                return -errno;
-        }
-
-        return write_string_stream(f, line, true);
-}
-
-int write_string_file_atomic(const char *fn, const char *line) {
+static int write_string_file_atomic(const char *fn, const char *line, bool enforce_newline) {
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ char *p = NULL;
         int r;
@@ -94,7 +59,7 @@ int write_string_file_atomic(const char *fn, const char *line) {
 
         fchmod_umask(fileno(f), 0644);
 
-        r = write_string_stream(f, line, true);
+        r = write_string_stream(f, line, enforce_newline);
         if (r >= 0) {
                 if (rename(p, fn) < 0)
                         r = -errno;
@@ -106,6 +71,41 @@ int write_string_file_atomic(const char *fn, const char *line) {
         return r;
 }
 
+int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
+        _cleanup_fclose_ FILE *f = NULL;
+
+        assert(fn);
+        assert(line);
+
+        if (flags & WRITE_STRING_FILE_ATOMIC) {
+                assert(flags & WRITE_STRING_FILE_CREATE);
+
+                return write_string_file_atomic(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
+        }
+
+        if (flags & WRITE_STRING_FILE_CREATE) {
+                f = fopen(fn, "we");
+                if (!f)
+                        return -errno;
+        } else {
+                int fd;
+
+                /* We manually build our own version of fopen(..., "we") that
+                 * works without O_CREAT */
+                fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY);
+                if (fd < 0)
+                        return -errno;
+
+                f = fdopen(fd, "we");
+                if (!f) {
+                        safe_close(fd);
+                        return -errno;
+                }
+        }
+
+        return write_string_stream(f, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
+}
+
 int read_one_line_file(const char *fn, char **line) {
         _cleanup_fclose_ FILE *f = NULL;
         char t[LINE_MAX], *c;
index 989b904c26ee580d52474032b7b3cac5d39e5341..2e8148ff2424563bcfc60856a7591bde502601d8 100644 (file)
 
 #include "macro.h"
 
+typedef enum {
+        WRITE_STRING_FILE_CREATE = 1,
+        WRITE_STRING_FILE_ATOMIC = 2,
+        WRITE_STRING_FILE_AVOID_NEWLINE = 4,
+} WriteStringFileFlags;
+
 int write_string_stream(FILE *f, const char *line, bool enforce_newline);
-int write_string_file(const char *fn, const char *line);
-int write_string_file_no_create(const char *fn, const char *line);
-int write_string_file_atomic(const char *fn, const char *line);
+int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags);
 
 int read_one_line_file(const char *fn, char **line);
 int read_full_file(const char *fn, char **contents, size_t *size);
index 2e24b1ea992e0dea7deb8079ec276d8c8cc54a9a..0c3697b61b841b29b5c9504d336c47034058362f 100644 (file)
@@ -139,7 +139,7 @@ int mac_smack_apply_pid(pid_t pid, const char *label) {
                 return 0;
 
         p = procfs_file_alloca(pid, "attr/current");
-        r = write_string_file(p, label);
+        r = write_string_file(p, label, WRITE_STRING_FILE_CREATE);
         if (r < 0)
                 return r;
 #endif
index aa912bde287766e0d531192fecd3d449e0093658..e4e302ae3d82c91ad314d07509e03d6b3a483862 100644 (file)
@@ -4716,7 +4716,7 @@ int update_reboot_param_file(const char *param) {
 
         if (param) {
 
-                r = write_string_file(REBOOT_PARAM_FILE, param);
+                r = write_string_file(REBOOT_PARAM_FILE, param, WRITE_STRING_FILE_CREATE);
                 if (r < 0)
                         log_error("Failed to write reboot param to "
                                   REBOOT_PARAM_FILE": %s", strerror(-r));
index 6028ed68c04b31e66ed85fcad69648e799682111..24eb41057141297ac830fdd6a4d2e33d5cb3e15b 100644 (file)
@@ -53,7 +53,7 @@ static int delete_rule(const char *rule) {
         if (!fn)
                 return log_oom();
 
-        return write_string_file(fn, "-1");
+        return write_string_file(fn, "-1", 0);
 }
 
 static int apply_rule(const char *rule) {
@@ -61,7 +61,7 @@ static int apply_rule(const char *rule) {
 
         delete_rule(rule);
 
-        r = write_string_file("/proc/sys/fs/binfmt_misc/register", rule);
+        r = write_string_file("/proc/sys/fs/binfmt_misc/register", rule, 0);
         if (r < 0)
                 return log_error_errno(r, "Failed to add binary format: %m");
 
@@ -191,7 +191,7 @@ int main(int argc, char *argv[]) {
                 }
 
                 /* Flush out all rules */
-                write_string_file("/proc/sys/fs/binfmt_misc/status", "-1");
+                write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_CREATE);
 
                 STRV_FOREACH(f, files) {
                         k = apply_file(*f, true);
index c92db51330c5965c10ec111832ef3c63b61584fb..61e7304dc7843094cc4b39af92cc5f7a99907199 100644 (file)
@@ -1446,7 +1446,7 @@ static int exec_child(
                  * shouldn't trip up over that. */
 
                 sprintf(t, "%i", context->oom_score_adjust);
-                r = write_string_file("/proc/self/oom_score_adj", t);
+                r = write_string_file("/proc/self/oom_score_adj", t, WRITE_STRING_FILE_CREATE);
                 if (r == -EPERM || r == -EACCES) {
                         log_open();
                         log_unit_debug_errno(unit, r, "Failed to adjust OOM setting, assuming containerized execution, ignoring: %m");
index b3d22840cf6ffce6832859f5caaa4175fc892d11..8e26362546ba675510336b53743504a2a585c054 100644 (file)
@@ -260,7 +260,7 @@ int machine_id_setup(const char *root) {
          * /run/machine-id as a replacement */
 
         RUN_WITH_UMASK(0022) {
-                r = write_string_file(run_machine_id, id);
+                r = write_string_file(run_machine_id, id, WRITE_STRING_FILE_CREATE);
         }
         if (r < 0) {
                 (void) unlink(run_machine_id);
index 523f0ce0206cefb252bf53a91d92197a0c855acd..c4124576a46c9fd181aaede9f8f9f72cc97f1f0e 100644 (file)
@@ -1203,7 +1203,7 @@ static int write_container_id(void) {
         if (isempty(c))
                 return 0;
 
-        return write_string_file("/run/systemd/container", c);
+        return write_string_file("/run/systemd/container", c, WRITE_STRING_FILE_CREATE);
 }
 
 int main(int argc, char *argv[]) {
index ddb02a1580c1f13f8660d382f9af261f0a386bd7..cbe7d0b4a9c998b53eba4002aa2069c61078f3f4 100644 (file)
@@ -221,7 +221,7 @@ int mac_smack_setup(bool *loaded_policy) {
         }
 
 #ifdef SMACK_RUN_LABEL
-        r = write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL);
+        r = write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL, 0);
         if (r)
                 log_warning("Failed to set SMACK label \"%s\" on self: %s",
                             SMACK_RUN_LABEL, strerror(-r));
index cda96d484ae4311dada2ff161debb232672fc9e8..3805b2943734b6b2ec002575718ab8affce7caea 100644 (file)
@@ -415,7 +415,7 @@ static int process_hostname(void) {
                 return 0;
 
         mkdir_parents(etc_hostname, 0755);
-        r = write_string_file(etc_hostname, arg_hostname);
+        r = write_string_file(etc_hostname, arg_hostname, WRITE_STRING_FILE_CREATE);
         if (r < 0)
                 return log_error_errno(r, "Failed to write %s: %m", etc_hostname);
 
@@ -436,7 +436,7 @@ static int process_machine_id(void) {
                 return 0;
 
         mkdir_parents(etc_machine_id, 0755);
-        r = write_string_file(etc_machine_id, sd_id128_to_string(arg_machine_id, id));
+        r = write_string_file(etc_machine_id, sd_id128_to_string(arg_machine_id, id), WRITE_STRING_FILE_CREATE);
         if (r < 0)
                 return log_error_errno(r, "Failed to write machine id: %m");
 
index b46e160888523c43e113768de355fd9652622691..da5f3b647ae2a8a1375401da425271f433b6dd72 100644 (file)
@@ -183,7 +183,8 @@ static int add_cryptsetup(const char *id, const char *what, bool rw, char **devi
         r = write_string_file(p,
                         "# Automatically generated by systemd-gpt-auto-generator\n\n"
                         "[Unit]\n"
-                        "JobTimeoutSec=0\n"); /* the binary handles timeouts anyway */
+                        "JobTimeoutSec=0\n",
+                        WRITE_STRING_FILE_CREATE); /* the binary handles timeouts anyway */
         if (r < 0)
                 return log_error_errno(r, "Failed to write device drop-in: %m");
 
index 43aac616b6261dc1189d037ec9da6ca84a867ee2..1f3b1699056dcb52014c11ad7fb325ff1cbf1fee 100644 (file)
@@ -65,7 +65,7 @@ int main(int argc, char *argv[]) {
                 return EXIT_FAILURE;
         }
 
-        r = write_string_file("/sys/power/resume", major_minor);
+        r = write_string_file("/sys/power/resume", major_minor, WRITE_STRING_FILE_CREATE);
         if (r < 0) {
                 log_error_errno(r, "Failed to write '%s' to /sys/power/resume: %m", major_minor);
                 return EXIT_FAILURE;
index 659ce18a48869649bf3999f9832562f32b7a36b2..b56a61cda4aa57407dbea7dbae63291e97d06afb 100644 (file)
@@ -1196,7 +1196,7 @@ static int trigger_device(Manager *m, struct udev_device *d) {
                 if (!t)
                         return -ENOMEM;
 
-                write_string_file(t, "change");
+                write_string_file(t, "change", WRITE_STRING_FILE_CREATE);
         }
 
         return 0;
@@ -1795,7 +1795,7 @@ static int nologin_timeout_handler(
 
         log_info("Creating /run/nologin, blocking further logins...");
 
-        r = write_string_file_atomic("/run/nologin", "System is going down.");
+        r = write_string_file("/run/nologin", "System is going down.", WRITE_STRING_FILE_ATOMIC);
         if (r < 0)
                 log_error_errno(r, "Failed to create /run/nologin: %m");
         else
index 5607cf470e8a1a26a4aba8d693a10d7355235fb1..9550e89a15a1066c9fca0d929e4801f82bfc017b 100644 (file)
@@ -1495,7 +1495,7 @@ static int link_set_ipv4_forward(Link *link) {
         p = strjoina("/proc/sys/net/ipv4/conf/", link->ifname, "/forwarding");
         v = one_zero(link_ipv4_forward_enabled(link));
 
-        r = write_string_file_no_create(p, v);
+        r = write_string_file(p, v, 0);
         if (r < 0) {
                 /* If the right value is set anyway, don't complain */
                 if (verify_one_line_file(p, v) > 0)
@@ -1524,7 +1524,7 @@ static int link_set_ipv6_forward(Link *link) {
         p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/forwarding");
         v = one_zero(link_ipv6_forward_enabled(link));
 
-        r = write_string_file_no_create(p, v);
+        r = write_string_file(p, v, 0);
         if (r < 0) {
                 /* If the right value is set anyway, don't complain */
                 if (verify_one_line_file(p, v) > 0)
@@ -1553,7 +1553,7 @@ static int link_set_ipv6_privacy_extensions(Link *link) {
         p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/use_tempaddr");
         xsprintf(buf, "%u", link->network->ipv6_privacy_extensions);
 
-        r = write_string_file_no_create(p, buf);
+        r = write_string_file(p, buf, 0);
         if (r < 0) {
                 /* If the right value is set anyway, don't complain */
                 if (verify_one_line_file(p, buf) > 0)
index ab9fbaf1383282d3f9a0932476af32c624250574..c7f4d02855098e2f0afdff7eecde7bba71f2f6f1 100644 (file)
@@ -1703,7 +1703,7 @@ static int setup_boot_id(const char *dest) {
 
         id128_format_as_uuid(rnd, as_uuid);
 
-        r = write_string_file(from, as_uuid);
+        r = write_string_file(from, as_uuid, WRITE_STRING_FILE_CREATE);
         if (r < 0)
                 return log_error_errno(r, "Failed to write boot id: %m");
 
@@ -2508,7 +2508,7 @@ static int reset_audit_loginuid(void) {
         if (streq(p, "4294967295"))
                 return 0;
 
-        r = write_string_file("/proc/self/loginuid", "4294967295");
+        r = write_string_file("/proc/self/loginuid", "4294967295", WRITE_STRING_FILE_CREATE);
         if (r < 0) {
                 log_error_errno(r,
                                 "Failed to reset audit login UID. This probably means that your kernel is too\n"
@@ -4448,13 +4448,13 @@ static int setup_uid_map(pid_t pid) {
 
         xsprintf(uid_map, "/proc/" PID_FMT "/uid_map", pid);
         xsprintf(line, UID_FMT " " UID_FMT " " UID_FMT "\n", 0, arg_uid_shift, arg_uid_range);
-        r = write_string_file(uid_map, line);
+        r = write_string_file(uid_map, line, WRITE_STRING_FILE_CREATE);
         if (r < 0)
                 return log_error_errno(r, "Failed to write UID map: %m");
 
         /* We always assign the same UID and GID ranges */
         xsprintf(uid_map, "/proc/" PID_FMT "/gid_map", pid);
-        r = write_string_file(uid_map, line);
+        r = write_string_file(uid_map, line, WRITE_STRING_FILE_CREATE);
         if (r < 0)
                 return log_error_errno(r, "Failed to write GID map: %m");
 
index 5a90c778fb4033f2e25c1c24356b46efa1295b98..904dec6bfcc7aeda3991ed8d5644eec1c73747b8 100644 (file)
@@ -127,7 +127,7 @@ int main(int argc, char *argv[]) {
                         return EXIT_SUCCESS;
                 }
 
-                r = write_string_file(saved, value);
+                r = write_string_file(saved, value, WRITE_STRING_FILE_CREATE);
                 if (r < 0) {
                         log_error_errno(r, "Failed to write %s: %m", saved);
                         return EXIT_FAILURE;
index 55f4e48601436757d0bd7ed31ae062bb296bc5f9..232a005054a3584b84e48e447c8a444f64cefa95 100644 (file)
@@ -66,7 +66,7 @@ int sysctl_write(const char *property, const char *value) {
         log_debug("Setting '%s' to '%s'", property, value);
 
         p = strjoina("/proc/sys/", property);
-        return write_string_file(p, value);
+        return write_string_file(p, value, WRITE_STRING_FILE_CREATE);
 }
 
 int sysctl_read(const char *property, char **content) {
index 2be63b40c6d74cee8c62b6b8510a1276547255bb..2b2310152d54ab22d55f08f7e87ccddd2f36ebfb 100644 (file)
@@ -42,7 +42,7 @@ static int write_mode(char **modes) {
         STRV_FOREACH(mode, modes) {
                 int k;
 
-                k = write_string_file("/sys/power/disk", *mode);
+                k = write_string_file("/sys/power/disk", *mode, 0);
                 if (k == 0)
                         return 0;
 
index 838ffcba3dd2a9ce29249723fc581993bad6cd00..e4771c9dd73749dfb4d513d766186ed810fb5b69 100644 (file)
@@ -68,7 +68,7 @@ int main(int argc, char *argv[]) {
         if (r < 0)
                 log_error_errno(r, "Failed to make subvolume: %m");
 
-        r = write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha");
+        r = write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE);
         if (r < 0)
                 log_error_errno(r, "Failed to write file: %m");
 
index b1385b8b8776997cb3fca2bd53fcdaa6b98e4419..b73c958ec5755893e637fc1f1de0b4ec2bcdb2a2 100644 (file)
@@ -43,7 +43,7 @@ static void test_copy_file(void) {
         assert_se(fd >= 0);
         close(fd);
 
-        assert_se(write_string_file(fn, "foo bar bar bar foo") == 0);
+        assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0);
 
         assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0);
 
@@ -67,7 +67,7 @@ static void test_copy_file_fd(void) {
         out_fd = mkostemp_safe(out_fn, O_RDWR);
         assert_se(out_fd >= 0);
 
-        assert_se(write_string_file(in_fn, text) == 0);
+        assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0);
         assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0);
         assert_se(copy_file_fd(in_fn, out_fd, true) >= 0);
         assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
@@ -94,7 +94,7 @@ static void test_copy_tree(void) {
                 char *f = strjoina(original_dir, *p);
 
                 assert_se(mkdir_parents(f, 0755) >= 0);
-                assert_se(write_string_file(f, "file") == 0);
+                assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0);
         }
 
         STRV_FOREACH_PAIR(link, p, links) {
index 725c2fab4ac8ef0e64dd3a6d88c9b003c9e75fea..be3a87958f9f426e5dcabc4d57750af527ee88c4 100644 (file)
@@ -334,7 +334,7 @@ static void test_write_string_file(void) {
         fd = mkostemp_safe(fn, O_RDWR);
         assert_se(fd >= 0);
 
-        assert_se(write_string_file(fn, "boohoo") == 0);
+        assert_se(write_string_file(fn, "boohoo", WRITE_STRING_FILE_CREATE) == 0);
 
         assert_se(read(fd, buf, sizeof(buf)) == 7);
         assert_se(streq(buf, "boohoo\n"));
@@ -350,8 +350,8 @@ static void test_write_string_file_no_create(void) {
         fd = mkostemp_safe(fn, O_RDWR);
         assert_se(fd >= 0);
 
-        assert_se(write_string_file_no_create("/a/file/which/does/not/exists/i/guess", "boohoo") < 0);
-        assert_se(write_string_file_no_create(fn, "boohoo") == 0);
+        assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
+        assert_se(write_string_file(fn, "boohoo", 0) == 0);
 
         assert_se(read(fd, buf, sizeof(buf)) == strlen("boohoo\n"));
         assert_se(streq(buf, "boohoo\n"));
@@ -377,8 +377,8 @@ static void test_load_env_file_pairs(void) {
                         "ANSI_COLOR=\"0;36\"\n"
                         "HOME_URL=\"https://www.archlinux.org/\"\n"
                         "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
-                        "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n"
-                        );
+                        "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
+                        WRITE_STRING_FILE_CREATE);
         assert_se(r == 0);
 
         f = fdopen(fd, "r");
index ad9ea3bcce85792c44b10e7cfebf5207498aa062..9fbfece14f69221a093f851d948636c31ffa168d 100644 (file)
@@ -565,14 +565,14 @@ static void test_read_hostname_config(void) {
         close(fd);
 
         /* simple hostname */
-        write_string_file(path, "foo");
+        write_string_file(path, "foo", WRITE_STRING_FILE_CREATE);
         assert_se(read_hostname_config(path, &hostname) == 0);
         assert_se(streq(hostname, "foo"));
         free(hostname);
         hostname = NULL;
 
         /* with comment */
-        write_string_file(path, "# comment\nfoo");
+        write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE);
         assert_se(read_hostname_config(path, &hostname) == 0);
         assert_se(hostname);
         assert_se(streq(hostname, "foo"));
@@ -580,7 +580,7 @@ static void test_read_hostname_config(void) {
         hostname = NULL;
 
         /* with comment and extra whitespace */
-        write_string_file(path, "# comment\n\n foo ");
+        write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE);
         assert_se(read_hostname_config(path, &hostname) == 0);
         assert_se(hostname);
         assert_se(streq(hostname, "foo"));
@@ -588,7 +588,7 @@ static void test_read_hostname_config(void) {
         hostname = NULL;
 
         /* cleans up name */
-        write_string_file(path, "!foo/bar.com");
+        write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE);
         assert_se(read_hostname_config(path, &hostname) == 0);
         assert_se(hostname);
         assert_se(streq(hostname, "foobar.com"));
@@ -597,7 +597,7 @@ static void test_read_hostname_config(void) {
 
         /* no value set */
         hostname = (char*) 0x1234;
-        write_string_file(path, "# nothing here\n");
+        write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE);
         assert_se(read_hostname_config(path, &hostname) == -ENOENT);
         assert_se(hostname == (char*) 0x1234);  /* does not touch argument on error */
 
@@ -1191,11 +1191,11 @@ static void test_execute_directory(void) {
         masked = strjoina(template_lo, "/masked");
         mask = strjoina(template_hi, "/masked");
 
-        assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works") == 0);
-        assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works2") == 0);
-        assert_se(write_string_file(overridden, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed") == 0);
-        assert_se(write_string_file(override, "#!/bin/sh\necho 'Executing '$0") == 0);
-        assert_se(write_string_file(masked, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed") == 0);
+        assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works", WRITE_STRING_FILE_CREATE) == 0);
+        assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works2", WRITE_STRING_FILE_CREATE) == 0);
+        assert_se(write_string_file(overridden, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed", WRITE_STRING_FILE_CREATE) == 0);
+        assert_se(write_string_file(override, "#!/bin/sh\necho 'Executing '$0", WRITE_STRING_FILE_CREATE) == 0);
+        assert_se(write_string_file(masked, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed", WRITE_STRING_FILE_CREATE) == 0);
         assert_se(symlink("/dev/null", mask) == 0);
         assert_se(chmod(name, 0755) == 0);
         assert_se(chmod(name2, 0755) == 0);
index e27fb1fd9eb9a428bff2e54b887028c4e45e5720..8a31089c0c668137b320f2dc6849e7c069404480 100644 (file)
@@ -398,7 +398,7 @@ static void worker_spawn(Manager *manager, struct event *event) {
                 prctl(PR_SET_PDEATHSIG, SIGTERM);
 
                 /* reset OOM score, we only protect the main daemon */
-                write_string_file("/proc/self/oom_score_adj", "0");
+                write_string_file("/proc/self/oom_score_adj", "0", WRITE_STRING_FILE_CREATE);
 
                 for (;;) {
                         struct udev_event *udev_event;
@@ -1091,7 +1091,7 @@ static int synthesize_change(struct udev_device *dev) {
                  */
                 log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
                 strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
-                write_string_file(filename, "change");
+                write_string_file(filename, "change", WRITE_STRING_FILE_CREATE);
 
                 udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
                         _cleanup_udev_device_unref_ struct udev_device *d = NULL;
@@ -1106,7 +1106,7 @@ static int synthesize_change(struct udev_device *dev) {
                         log_debug("device %s closed, synthesising partition '%s' 'change'",
                                   udev_device_get_devnode(dev), udev_device_get_devnode(d));
                         strscpyl(filename, sizeof(filename), udev_device_get_syspath(d), "/uevent", NULL);
-                        write_string_file(filename, "change");
+                        write_string_file(filename, "change", WRITE_STRING_FILE_CREATE);
                 }
 
                 return 0;
@@ -1114,7 +1114,7 @@ static int synthesize_change(struct udev_device *dev) {
 
         log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
         strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
-        write_string_file(filename, "change");
+        write_string_file(filename, "change", WRITE_STRING_FILE_CREATE);
 
         return 0;
 }
@@ -1747,7 +1747,7 @@ int main(int argc, char *argv[]) {
 
                 setsid();
 
-                write_string_file("/proc/self/oom_score_adj", "-1000");
+                write_string_file("/proc/self/oom_score_adj", "-1000", WRITE_STRING_FILE_CREATE);
         }
 
         r = run(fd_ctrl, fd_uevent, cgroup);
index 1c31769fde9b447904987a7fef476589ff21e113..ddeb310c3c9d2093ff034524d8b1cce06b50a8ef 100644 (file)
@@ -65,7 +65,7 @@ int main(int argc, char*argv[]) {
         } else if (streq(argv[1], "stop")) {
                 int r;
 
-                r = write_string_file_atomic("/run/nologin", "System is going down.");
+                r = write_string_file("/run/nologin", "System is going down.", WRITE_STRING_FILE_ATOMIC);
                 if (r < 0) {
                         log_error_errno(r, "Failed to create /run/nologin: %m");
                         return EXIT_FAILURE;
index f7728dcfff411fc1ec2b33bed22a2a9a0c12238c..4b211756119f308e47d01aa85609ea9deea3b74c 100644 (file)
@@ -56,7 +56,7 @@ static int disable_utf8(int fd) {
         if (k < 0)
                 r = k;
 
-        k = write_string_file("/sys/module/vt/parameters/default_utf8", "0");
+        k = write_string_file("/sys/module/vt/parameters/default_utf8", "0", WRITE_STRING_FILE_CREATE);
         if (k < 0)
                 r = k;
 
@@ -89,7 +89,7 @@ static int enable_utf8(int fd) {
         if (k < 0)
                 r = k;
 
-        k = write_string_file("/sys/module/vt/parameters/default_utf8", "1");
+        k = write_string_file("/sys/module/vt/parameters/default_utf8", "1", WRITE_STRING_FILE_CREATE);
         if (k < 0)
                 r = k;