summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/copy.c85
-rw-r--r--src/shared/copy.h1
-rw-r--r--src/shared/fileio.c75
-rw-r--r--src/shared/fileio.h1
4 files changed, 62 insertions, 100 deletions
diff --git a/src/shared/copy.c b/src/shared/copy.c
index 3744797b95..a863246b2b 100644
--- a/src/shared/copy.c
+++ b/src/shared/copy.c
@@ -19,17 +19,20 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include <sys/sendfile.h>
+
#include "util.h"
#include "copy.h"
int copy_bytes(int fdf, int fdt, off_t max_bytes) {
+ bool try_sendfile = true;
+
assert(fdf >= 0);
assert(fdt >= 0);
for (;;) {
- char buf[PIPE_BUF];
- ssize_t n, k;
- size_t m = sizeof(buf);
+ size_t m = PIPE_BUF;
+ ssize_t n;
if (max_bytes != (off_t) -1) {
@@ -40,19 +43,44 @@ int copy_bytes(int fdf, int fdt, off_t max_bytes) {
m = (size_t) max_bytes;
}
- n = read(fdf, buf, m);
- if (n < 0)
- return -errno;
- if (n == 0)
- break;
+ /* First try sendfile(), unless we already tried */
+ if (try_sendfile) {
+
+ n = sendfile(fdt, fdf, NULL, m);
+ if (n < 0) {
+ if (errno != EINVAL && errno != ENOSYS)
+ return -errno;
+
+ try_sendfile = false;
+ /* use fallback below */
+ } else if (n == 0) /* EOF */
+ break;
+ else if (n > 0)
+ /* Succcess! */
+ goto next;
+ }
+
+ /* As a fallback just copy bits by hand */
+ {
+ char buf[m];
+ ssize_t k;
- errno = 0;
- k = loop_write(fdt, buf, n, false);
- if (k < 0)
- return k;
- if (k != n)
- return errno ? -errno : -EIO;
+ n = read(fdf, buf, m);
+ if (n < 0)
+ return -errno;
+ if (n == 0) /* EOF */
+ break;
+
+ errno = 0;
+ k = loop_write(fdt, buf, n, false);
+ if (k < 0)
+ return k;
+ if (k != n)
+ return errno ? -errno : -EIO;
+
+ }
+ next:
if (max_bytes != (off_t) -1) {
assert(max_bytes >= n);
max_bytes -= n;
@@ -262,34 +290,39 @@ int copy_tree(const char *from, const char *to, bool merge) {
return -ENOTSUP;
}
-int copy_file(const char *from, const char *to, int flags, mode_t mode) {
- _cleanup_close_ int fdf = -1, fdt = -1;
- int r;
+int copy_file_fd(const char *from, int fdt) {
+ _cleanup_close_ int fdf = -1;
assert(from);
- assert(to);
+ assert(fdt >= 0);
fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fdf < 0)
return -errno;
+ return copy_bytes(fdf, fdt, (off_t) -1);
+}
+
+int copy_file(const char *from, const char *to, int flags, mode_t mode) {
+ int fdt, r;
+
+ assert(from);
+ assert(to);
+
fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
if (fdt < 0)
return -errno;
- r = copy_bytes(fdf, fdt, (off_t) -1);
+ r = copy_file_fd(from, fdt);
if (r < 0) {
+ close(fdt);
unlink(to);
return r;
}
- r = close(fdt);
- fdt = -1;
-
- if (r < 0) {
- r = -errno;
- unlink(to);
- return r;
+ if (close(fdt) < 0) {
+ unlink_noerrno(to);
+ return -errno;
}
return 0;
diff --git a/src/shared/copy.h b/src/shared/copy.h
index 6b93107fab..62932112af 100644
--- a/src/shared/copy.h
+++ b/src/shared/copy.h
@@ -24,6 +24,7 @@
#include <stdbool.h>
#include <sys/types.h>
+int copy_file_fd(const char *from, int to);
int copy_file(const char *from, const char *to, int flags, mode_t mode);
int copy_tree(const char *from, const char *to, bool merge);
int copy_bytes(int fdf, int fdt, off_t max_bytes);
diff --git a/src/shared/fileio.c b/src/shared/fileio.c
index 38028b972e..f4efc4c9f1 100644
--- a/src/shared/fileio.c
+++ b/src/shared/fileio.c
@@ -20,12 +20,12 @@
***/
#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) {
assert(f);
@@ -144,77 +144,6 @@ 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 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);
- if (r < 0)
- return -errno;
-
- return (ssize_t) l;
-}
-
int read_full_stream(FILE *f, char **contents, size_t *size) {
size_t n, l;
_cleanup_free_ char *buf = NULL;
diff --git a/src/shared/fileio.h b/src/shared/fileio.h
index c256915799..5ae51c1e28 100644
--- a/src/shared/fileio.h
+++ b/src/shared/fileio.h
@@ -33,7 +33,6 @@ int write_string_file_atomic(const char *fn, const char *line);
int read_one_line_file(const char *fn, char **line);
int read_full_file(const char *fn, char **contents, size_t *size);
int read_full_stream(FILE *f, char **contents, size_t *size);
-ssize_t sendfile_full(int out_fd, const char *fn);
int parse_env_file(const char *fname, const char *separator, ...) _sentinel_;
int load_env_file(FILE *f, const char *fname, const char *separator, char ***l);