1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 #if ENABLE_FEATURE_USE_SENDFILE
11 # include <sys/sendfile.h>
13 # define sendfile(a,b,c,d) (-1)
17 * We were using 0x7fff0000 as sendfile chunk size, but it
18 * was seen to cause largish delays when user tries to ^C a file copy.
19 * Let's use a saner size.
20 * Note: needs to be >= max(CONFIG_FEATURE_COPYBUF_KB),
21 * or else "copy to eof" code will use neddlesly short reads.
23 #define SENDFILE_BIGBUF (16*1024*1024)
25 /* Used by NOFORK applets (e.g. cat) - must not use xmalloc.
26 * size < 0 means "ignore write errors", used by tar --to-command
27 * size = 0 means "copy till EOF"
29 static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
33 bool continue_on_write_error = 0;
35 #if CONFIG_FEATURE_COPYBUF_KB > 4
36 char *buffer = buffer; /* for compiler */
39 char buffer[CONFIG_FEATURE_COPYBUF_KB * 1024];
40 enum { buffer_size = sizeof(buffer) };
45 continue_on_write_error = 1;
51 sendfile_sz = !ENABLE_FEATURE_USE_SENDFILE
55 size = SENDFILE_BIGBUF;
56 status = 1; /* copy until eof */
63 rd = sendfile(dst_fd, src_fd, NULL,
64 size > sendfile_sz ? sendfile_sz : size);
67 sendfile_sz = 0; /* do not try sendfile anymore */
69 #if CONFIG_FEATURE_COPYBUF_KB > 4
70 if (buffer_size == 0) {
71 if (size > 0 && size <= 4 * 1024)
73 /* We want page-aligned buffer, just in case kernel is clever
74 * and can do page-aligned io more efficiently */
75 buffer = mmap(NULL, CONFIG_FEATURE_COPYBUF_KB * 1024,
76 PROT_READ | PROT_WRITE,
77 MAP_PRIVATE | MAP_ANON,
78 /* ignored: */ -1, 0);
79 buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024;
80 if (buffer == MAP_FAILED) {
82 buffer = alloca(4 * 1024);
83 buffer_size = 4 * 1024;
87 rd = safe_read(src_fd, buffer,
88 size > buffer_size ? buffer_size : size);
90 bb_perror_msg(bb_msg_read_error);
94 if (!rd) { /* eof - all done */
98 /* dst_fd == -1 is a fake, else... */
99 if (dst_fd >= 0 && !sendfile_sz) {
100 ssize_t wr = full_write(dst_fd, buffer, rd);
102 if (!continue_on_write_error) {
103 bb_perror_msg(bb_msg_write_error);
110 if (status < 0) { /* if we aren't copying till EOF... */
113 /* 'size' bytes copied - all done */
121 /* some environments don't have munmap(), hide it in #if */
122 #if CONFIG_FEATURE_COPYBUF_KB > 4
123 if (buffer_size > 4 * 1024)
124 munmap(buffer, buffer_size);
126 return status ? -1 : total;
131 void FAST_FUNC complain_copyfd_and_die(off_t sz)
134 bb_error_msg_and_die("short read");
135 /* if sz == -1, bb_copyfd_XX already complained */
140 off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size)
143 return bb_full_fd_action(fd1, fd2, size);
148 void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size)
150 off_t sz = bb_copyfd_size(fd1, fd2, size);
151 if (sz == (size >= 0 ? size : -size))
154 bb_error_msg_and_die("short read");
155 /* if sz == -1, bb_copyfd_XX already complained */
159 off_t FAST_FUNC bb_copyfd_eof(int fd1, int fd2)
161 return bb_full_fd_action(fd1, fd2, 0);