libbb/copyfd.c: don't mmap a largish buffer if we only want to copy a few kb
[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 tarball for details.
8  */
9
10 #include "libbb.h"
11
12 /* Used by NOFORK applets (e.g. cat) - must not use xmalloc */
13
14 static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
15 {
16         int status = -1;
17         off_t total = 0;
18 #if CONFIG_FEATURE_COPYBUF_KB <= 4
19         char buffer[CONFIG_FEATURE_COPYBUF_KB * 1024];
20         enum { buffer_size = sizeof(buffer) };
21 #else
22         char *buffer;
23         int buffer_size;
24
25         if (size > 0 && size <= 4 * 1024)
26                 goto use_small_buf;
27         /* We want page-aligned buffer, just in case kernel is clever
28          * and can do page-aligned io more efficiently */
29         buffer = mmap(NULL, CONFIG_FEATURE_COPYBUF_KB * 1024,
30                         PROT_READ | PROT_WRITE,
31                         MAP_PRIVATE | MAP_ANON,
32                         /* ignored: */ -1, 0);
33         buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024;
34         if (buffer == MAP_FAILED) {
35  use_small_buf:
36                 buffer = alloca(4 * 1024);
37                 buffer_size = 4 * 1024;
38         }
39 #endif
40
41         if (src_fd < 0)
42                 goto out;
43
44         if (!size) {
45                 size = buffer_size;
46                 status = 1; /* copy until eof */
47         }
48
49         while (1) {
50                 ssize_t rd;
51
52                 rd = safe_read(src_fd, buffer, size > buffer_size ? buffer_size : size);
53
54                 if (!rd) { /* eof - all done */
55                         status = 0;
56                         break;
57                 }
58                 if (rd < 0) {
59                         bb_perror_msg(bb_msg_read_error);
60                         break;
61                 }
62                 /* dst_fd == -1 is a fake, else... */
63                 if (dst_fd >= 0) {
64                         ssize_t wr = full_write(dst_fd, buffer, rd);
65                         if (wr < rd) {
66                                 bb_perror_msg(bb_msg_write_error);
67                                 break;
68                         }
69                 }
70                 total += rd;
71                 if (status < 0) { /* if we aren't copying till EOF... */
72                         size -= rd;
73                         if (!size) {
74                                 /* 'size' bytes copied - all done */
75                                 status = 0;
76                                 break;
77                         }
78                 }
79         }
80  out:
81
82 #if CONFIG_FEATURE_COPYBUF_KB > 4
83         if (buffer_size != 4 * 1024)
84                 munmap(buffer, buffer_size);
85 #endif
86         return status ? -1 : total;
87 }
88
89
90 #if 0
91 void FAST_FUNC complain_copyfd_and_die(off_t sz)
92 {
93         if (sz != -1)
94                 bb_error_msg_and_die("short read");
95         /* if sz == -1, bb_copyfd_XX already complained */
96         xfunc_die();
97 }
98 #endif
99
100 off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size)
101 {
102         if (size) {
103                 return bb_full_fd_action(fd1, fd2, size);
104         }
105         return 0;
106 }
107
108 void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size)
109 {
110         off_t sz = bb_copyfd_size(fd1, fd2, size);
111         if (sz == size)
112                 return;
113         if (sz != -1)
114                 bb_error_msg_and_die("short read");
115         /* if sz == -1, bb_copyfd_XX already complained */
116         xfunc_die();
117 }
118
119 off_t FAST_FUNC bb_copyfd_eof(int fd1, int fd2)
120 {
121         return bb_full_fd_action(fd1, fd2, 0);
122 }