chiark / gitweb /
fileio: consolidate write_string_file*()
[elogind.git] / src / shared / fileio.c
index 38028b972ed2ed0e34ac10139e2bd0e8125fb599..d592bf5ac9c2f5126fd9e01f5ef878c905abc86a 100644 (file)
 ***/
 
 #include <unistd.h>
-#include <sys/sendfile.h>
-#include "fileio.h"
+
 #include "util.h"
 #include "strv.h"
 #include "utf8.h"
 #include "ctype.h"
+#include "fileio.h"
 
-int write_string_stream(FILE *f, const char *line) {
+int write_string_stream(FILE *f, const char *line, bool enforce_newline) {
         assert(f);
         assert(line);
 
         errno = 0;
 
         fputs(line, f);
-        if (!endswith(line, "\n"))
+        if (enforce_newline && !endswith(line, "\n"))
                 fputc('\n', f);
 
         fflush(f);
@@ -45,75 +45,65 @@ int write_string_stream(FILE *f, const char *line) {
         return 0;
 }
 
-int write_string_file(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;
 
         assert(fn);
         assert(line);
 
-        f = fopen(fn, "we");
-        if (!f)
-                return -errno;
-
-        return write_string_stream(f, line);
-}
-
-int write_string_file_no_create(const char *fn, const char *line) {
-        _cleanup_fclose_ FILE *f = NULL;
-        int fd;
-
-        assert(fn);
-        assert(line);
+        r = fopen_temporary(fn, &f, &p);
+        if (r < 0)
+                return r;
 
-        /* We manually build our own version of fopen(..., "we") that
-         * without O_CREAT */
-        fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY);
-        if (fd < 0)
-                return -errno;
+        fchmod_umask(fileno(f), 0644);
 
-        f = fdopen(fd, "we");
-        if (!f) {
-                safe_close(fd);
-                return -errno;
+        r = write_string_stream(f, line, enforce_newline);
+        if (r >= 0) {
+                if (rename(p, fn) < 0)
+                        r = -errno;
         }
 
-        return write_string_stream(f, line);
+        if (r < 0)
+                unlink(p);
+
+        return r;
 }
 
-int write_string_file_atomic(const char *fn, const char *line) {
+int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
         _cleanup_fclose_ FILE *f = NULL;
-        _cleanup_free_ char *p = NULL;
-        int r;
 
         assert(fn);
         assert(line);
 
-        r = fopen_temporary(fn, &f, &p);
-        if (r < 0)
-                return r;
+        if (flags & WRITE_STRING_FILE_ATOMIC) {
+                assert(flags & WRITE_STRING_FILE_CREATE);
 
-        fchmod_umask(fileno(f), 0644);
+                return write_string_file_atomic(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
+        }
 
-        errno = 0;
-        fputs(line, f);
-        if (!endswith(line, "\n"))
-                fputc('\n', f);
+        if (flags & WRITE_STRING_FILE_CREATE) {
+                f = fopen(fn, "we");
+                if (!f)
+                        return -errno;
+        } else {
+                int fd;
 
-        fflush(f);
+                /* 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;
 
-        if (ferror(f))
-                r = errno ? -errno : -EIO;
-        else {
-                if (rename(p, fn) < 0)
-                        r = -errno;
-                else
-                        r = 0;
+                f = fdopen(fd, "we");
+                if (!f) {
+                        safe_close(fd);
+                        return -errno;
+                }
         }
 
-        if (r < 0)
-                unlink(p);
-
-        return r;
+        return write_string_stream(f, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
 }
 
 int read_one_line_file(const char *fn, char **line) {
@@ -144,75 +134,15 @@ int read_one_line_file(const char *fn, char **line) {
         return 0;
 }
 
-ssize_t sendfile_full(int out_fd, const char *fn) {
-        _cleanup_fclose_ FILE *f;
-        struct stat st;
+int verify_one_line_file(const char *fn, const char *line) {
+        _cleanup_free_ char *value = NULL;
         int r;
-        ssize_t s;
-
-        size_t n, l;
-        _cleanup_free_ char *buf = NULL;
-
-        assert(out_fd > 0);
-        assert(fn);
-
-        f = fopen(fn, "re");
-        if (!f)
-                return -errno;
-
-        r = fstat(fileno(f), &st);
-        if (r < 0)
-                return -errno;
-
-        s = sendfile(out_fd, fileno(f), NULL, st.st_size);
-        if (s < 0)
-                if (errno == EINVAL || errno == ENOSYS) {
-                        /* continue below */
-                } else
-                        return -errno;
-        else
-                return s;
-
-        /* sendfile() failed, fall back to read/write */
-
-        /* Safety check */
-        if (st.st_size > 4*1024*1024)
-                return -E2BIG;
-
-        n = st.st_size > 0 ? st.st_size : LINE_MAX;
-        l = 0;
-
-        while (true) {
-                char *t;
-                size_t k;
-
-                t = realloc(buf, n);
-                if (!t)
-                        return -ENOMEM;
-
-                buf = t;
-                k = fread(buf + l, 1, n - l, f);
-
-                if (k <= 0) {
-                        if (ferror(f))
-                                return -errno;
-
-                        break;
-                }
-
-                l += k;
-                n *= 2;
-
-                /* Safety check */
-                if (n > 4*1024*1024)
-                        return -E2BIG;
-        }
 
-        r = write(out_fd, buf, l);
+        r = read_one_line_file(fn, &value);
         if (r < 0)
-                return -errno;
+                return r;
 
-        return (ssize_t) l;
+        return streq(value, line);
 }
 
 int read_full_stream(FILE *f, char **contents, size_t *size) {