* 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)
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;
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;
+}