]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/file_utils.c
rexec: use __do_close_prot_errno
[mirror_lxc.git] / src / lxc / file_utils.c
index 9a7bd160e1cd556605f045160cea8098c361a830..930fd738a942464a1c8ec3a50f764c214ae3afab 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include "config.h"
-
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE 1
+#endif
 #include <errno.h>
 #include <fcntl.h>
 #include <linux/magic.h>
 #include <stdlib.h>
 #include <sys/stat.h>
+#include <sys/sendfile.h>
 #include <sys/types.h>
 
+#include "config.h"
 #include "file_utils.h"
-#include "log.h"
 #include "macro.h"
-#include "string.h"
-
-lxc_log_define(file_utils, lxc);
+#include "string_utils.h"
 
 int lxc_write_to_file(const char *filename, const void *buf, size_t count,
                      bool add_newline, mode_t mode)
@@ -300,7 +300,7 @@ FILE *fopen_cloexec(const char *path, const char *mode)
                        open_mode |= O_EXCL;
        open_mode |= O_CLOEXEC;
 
-       fd = open(path, open_mode, 0666);
+       fd = open(path, open_mode, 0660);
        if (fd < 0)
                return NULL;
 
@@ -311,3 +311,58 @@ FILE *fopen_cloexec(const char *path, const char *mode)
        errno = saved_errno;
        return ret;
 }
+
+ssize_t lxc_sendfile_nointr(int out_fd, int in_fd, off_t *offset, size_t count)
+{
+       ssize_t ret;
+
+again:
+       ret = sendfile(out_fd, in_fd, offset, count);
+       if (ret < 0) {
+               if (errno == EINTR)
+                       goto again;
+
+               return -1;
+       }
+
+       return ret;
+}
+
+char *file_to_buf(char *path, size_t *length)
+{
+       int fd;
+       char buf[PATH_MAX];
+       char *copy = NULL;
+
+       if (!length)
+               return NULL;
+
+       fd = open(path, O_RDONLY | O_CLOEXEC);
+       if (fd < 0)
+               return NULL;
+
+       *length = 0;
+       for (;;) {
+               int n;
+               char *old = copy;
+
+               n = lxc_read_nointr(fd, buf, sizeof(buf));
+               if (n < 0)
+                       goto on_error;
+               if (!n)
+                       break;
+
+               copy = must_realloc(old, (*length + n) * sizeof(*old));
+               memcpy(copy + *length, buf, n);
+               *length += n;
+       }
+
+       close(fd);
+       return copy;
+
+on_error:
+       close(fd);
+       free(copy);
+
+       return NULL;
+}