Start 1.33.0 development cycle
[oweals/busybox.git] / libbb / copyfd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9 #include "libbb.h"
10 #if ENABLE_FEATURE_USE_SENDFILE
11 # include <sys/sendfile.h>
12 #else
13 # define sendfile(a,b,c,d) (-1)
14 #endif
15
16 /*
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 needlesly short reads.
22  */
23 #define SENDFILE_BIGBUF (16*1024*1024)
24
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"
28  */
29 static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
30 {
31         int status = -1;
32         off_t total = 0;
33         bool continue_on_write_error = 0;
34         ssize_t sendfile_sz;
35 #if CONFIG_FEATURE_COPYBUF_KB > 4
36         char *buffer = buffer; /* for compiler */
37         int buffer_size = 0;
38 #else
39         char buffer[CONFIG_FEATURE_COPYBUF_KB * 1024];
40         enum { buffer_size = sizeof(buffer) };
41 #endif
42
43         if (size < 0) {
44                 size = -size;
45                 continue_on_write_error = 1;
46         }
47
48         if (src_fd < 0)
49                 goto out;
50
51         sendfile_sz = !ENABLE_FEATURE_USE_SENDFILE
52                 ? 0
53                 : SENDFILE_BIGBUF;
54         if (!size) {
55                 size = SENDFILE_BIGBUF;
56                 status = 1; /* copy until eof */
57         }
58
59         while (1) {
60                 ssize_t rd;
61
62                 if (sendfile_sz) {
63                         /* dst_fd == -1 is a fake, else... */
64                         if (dst_fd >= 0) {
65                                 rd = sendfile(dst_fd, src_fd, NULL,
66                                         size > sendfile_sz ? sendfile_sz : size);
67                                 if (rd >= 0)
68                                         goto read_ok;
69                         }
70                         sendfile_sz = 0; /* do not try sendfile anymore */
71                 }
72 #if CONFIG_FEATURE_COPYBUF_KB > 4
73                 if (buffer_size == 0) {
74                         if (size > 0 && size <= 4 * 1024)
75                                 goto use_small_buf;
76                         /* We want page-aligned buffer, just in case kernel is clever
77                          * and can do page-aligned io more efficiently */
78                         buffer = mmap(NULL, CONFIG_FEATURE_COPYBUF_KB * 1024,
79                                         PROT_READ | PROT_WRITE,
80                                         MAP_PRIVATE | MAP_ANON,
81                                         /* ignored: */ -1, 0);
82                         buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024;
83                         if (buffer == MAP_FAILED) {
84  use_small_buf:
85                                 buffer = alloca(4 * 1024);
86                                 buffer_size = 4 * 1024;
87                         }
88                 }
89 #endif
90                 rd = safe_read(src_fd, buffer,
91                         size > buffer_size ? buffer_size : size);
92                 if (rd < 0) {
93                         bb_simple_perror_msg(bb_msg_read_error);
94                         break;
95                 }
96  read_ok:
97                 if (!rd) { /* eof - all done */
98                         status = 0;
99                         break;
100                 }
101                 /* dst_fd == -1 is a fake, else... */
102                 if (dst_fd >= 0 && !sendfile_sz) {
103                         ssize_t wr = full_write(dst_fd, buffer, rd);
104                         if (wr < rd) {
105                                 if (!continue_on_write_error) {
106                                         bb_simple_perror_msg(bb_msg_write_error);
107                                         break;
108                                 }
109                                 dst_fd = -1;
110                         }
111                 }
112                 total += rd;
113                 if (status < 0) { /* if we aren't copying till EOF... */
114                         size -= rd;
115                         if (!size) {
116                                 /* 'size' bytes copied - all done */
117                                 status = 0;
118                                 break;
119                         }
120                 }
121         }
122  out:
123
124 /* some environments don't have munmap(), hide it in #if */
125 #if CONFIG_FEATURE_COPYBUF_KB > 4
126         if (buffer_size > 4 * 1024)
127                 munmap(buffer, buffer_size);
128 #endif
129         return status ? -1 : total;
130 }
131
132
133 #if 0
134 void FAST_FUNC complain_copyfd_and_die(off_t sz)
135 {
136         if (sz != -1)
137                 bb_error_msg_and_die("short read");
138         /* if sz == -1, bb_copyfd_XX already complained */
139         xfunc_die();
140 }
141 #endif
142
143 off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size)
144 {
145         if (size) {
146                 return bb_full_fd_action(fd1, fd2, size);
147         }
148         return 0;
149 }
150
151 void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size)
152 {
153         off_t sz = bb_copyfd_size(fd1, fd2, size);
154         if (sz == (size >= 0 ? size : -size))
155                 return;
156         if (sz != -1)
157                 bb_simple_error_msg_and_die("short read");
158         /* if sz == -1, bb_copyfd_XX already complained */
159         xfunc_die();
160 }
161
162 off_t FAST_FUNC bb_copyfd_eof(int fd1, int fd2)
163 {
164         return bb_full_fd_action(fd1, fd2, 0);
165 }