]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
utils: Add utility functions that write/read to entire files
authorChristian Seiler <christian@iwakd.de>
Sun, 8 Sep 2013 12:14:48 +0000 (14:14 +0200)
committerStéphane Graber <stgraber@ubuntu.com>
Tue, 10 Sep 2013 22:19:21 +0000 (18:19 -0400)
Signed-off-by: Christian Seiler <christian@iwakd.de>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
src/lxc/utils.c
src/lxc/utils.h

index 80d663085141bd980632ade9aa232f13003df0bf..ddcfd1995158348e957a9465f7daea44ffae3ec3 100644 (file)
 #include <sys/wait.h>
 #include <assert.h>
 
+#ifndef HAVE_GETLINE
+#ifdef HAVE_FGETLN
+#include <../include/getline.h>
+#endif
+#endif
+
 #include "utils.h"
 #include "log.h"
 
@@ -805,3 +811,88 @@ void **lxc_dup_array(void **array, lxc_dup_fn element_dup_fn, lxc_free_fn elemen
 
        return result;
 }
+
+int lxc_write_to_file(const char *filename, const void* buf, size_t count, bool add_newline)
+{
+       int fd, saved_errno;
+       ssize_t ret;
+
+       fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0666);
+       if (fd < 0)
+               return -1;
+       ret = lxc_write_nointr(fd, buf, count);
+       if (ret < 0)
+               goto out_error; 
+       if ((size_t)ret != count)
+               goto out_error;
+       if (add_newline) {
+               ret = lxc_write_nointr(fd, "\n", 1);
+               if (ret != 1)
+                       goto out_error;
+       }
+       close(fd);
+       return 0;
+
+out_error:
+       saved_errno = errno;
+       close(fd);
+       errno = saved_errno;
+       return -1;
+}
+
+int lxc_read_from_file(const char *filename, void* buf, size_t count)
+{
+       int fd = -1, saved_errno;
+       ssize_t ret;
+
+       fd = open(filename, O_RDONLY | O_CLOEXEC);
+       if (fd < 0)
+               return -1;
+
+       if (!buf || !count) {
+               char buf2[100];
+               size_t count2 = 0;
+               while ((ret = read(fd, buf2, 100)) > 0)
+                       count2 += ret;
+               if (ret >= 0)
+                       ret = count2;
+       } else {
+               memset(buf, 0, count);
+               ret = read(fd, buf, count);
+       }
+
+       if (ret < 0)
+               ERROR("read %s: %s", filename, strerror(errno));
+
+       saved_errno = errno;
+       close(fd);
+       errno = saved_errno;
+       return ret;
+}
+
+char *lxc_read_line_from_file(const char *filename)
+{
+       FILE *f;
+       char *line = NULL;
+       int saved_errno;
+       size_t sz = 0;
+
+       f = fopen_cloexec(filename, "r");
+       if (!f)
+               return NULL;
+
+       if (getline(&line, &sz, f) == -1) {
+               saved_errno = errno;
+               fclose(f);
+               errno = saved_errno;
+               return NULL;
+       }
+
+       fclose(f);
+
+       /* trim line, if necessary */
+       if (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
+               line[strlen(line) - 1] = '\0';
+
+       return line;
+}
index 7261846bc5e71dd8f337d4884dd05b2b47b1aa9d..4c2ab293a6e56853530f29e66c21a8cff91e36e3 100644 (file)
@@ -194,6 +194,11 @@ extern ssize_t lxc_read_nointr_expect(int fd, void* buf, size_t count, const voi
 extern int sha1sum_file(char *fnam, unsigned char *md_value);
 #endif
 
+/* read and write whole files */
+extern int lxc_write_to_file(const char *filename, const void* buf, size_t count, bool add_newline);
+extern int lxc_read_from_file(const char *filename, void* buf, size_t count);
+extern char *lxc_read_line_from_file(const char *filename);
+
 /* convert variadic argument lists to arrays (for execl type argument lists) */
 extern char** lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup);
 extern const char** lxc_va_arg_list_to_argv_const(va_list ap, size_t skip);