chiark / gitweb /
fileio: consolidate write_string_file*()
authorDaniel Mack <daniel@zonque.org>
Mon, 6 Jul 2015 23:19:25 +0000 (19:19 -0400)
committerSven Eden <yamakuzure@gmx.net>
Tue, 14 Mar 2017 09:05:56 +0000 (10:05 +0100)
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.

src/login/logind-dbus.c
src/shared/capability.c
src/shared/cgroup-util.c
src/shared/fileio-label.c
src/shared/fileio.c
src/shared/fileio.h
src/shared/smack-util.c
src/shared/sysctl-util.c
src/shared/util.c

index 0cc2cdf997d3d45b9c3ea467e21ba69486368225..dbe8cae6061088f3d6d060f24968c40d387b8b98 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 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 b286243c3629cb165ed3c8c353f50b5387e80f8e..cfee41ca382b457b4cc44ea54f4c78523b8c01f5 100644 (file)
@@ -645,7 +645,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) {
@@ -819,7 +819,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))
@@ -839,7 +839,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;
 
@@ -860,7 +860,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;
 
@@ -871,7 +871,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;
 
@@ -1701,7 +1701,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 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 727be56f58302a90d1d3395262ea754efa4bb256..950519ed06ddfe96578ff98454c52580c24cbe73 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));