blob: 7e353190372260f32627816b2efcd9cd4442a6e2 [file] [log] [blame]
Eric Andersen55f98722001-05-15 17:48:09 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersen451c0d22005-04-16 07:46:53 +00005 * Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
Eric Andersen55f98722001-05-15 17:48:09 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen55f98722001-05-15 17:48:09 +00008 */
9
Eric Andersen451c0d22005-04-16 07:46:53 +000010#include "libbb.h"
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010011#if ENABLE_FEATURE_USE_SENDFILE
12# include <sys/sendfile.h>
13#else
14# define sendfile(a,b,c,d) (-1)
15#endif
16
17/*
18 * We were using 0x7fff0000 as sendfile chunk size, but it
19 * was seen to cause largish delays when user tries to ^C a file copy.
20 * Let's use a saner size.
21 * Note: needs to be >= max(CONFIG_FEATURE_COPYBUF_KB),
22 * or else "copy to eof" code will use neddlesly short reads.
23 */
24#define SENDFILE_BIGBUF (16*1024*1024)
Eric Andersen55f98722001-05-15 17:48:09 +000025
Denys Vlasenkod0a8a0d2010-06-26 18:11:44 +020026/* Used by NOFORK applets (e.g. cat) - must not use xmalloc.
27 * size < 0 means "ignore write errors", used by tar --to-command
28 * size = 0 means "copy till EOF"
29 */
Denis Vlasenko7039a662006-10-08 17:54:47 +000030static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
Eric Andersen55f98722001-05-15 17:48:09 +000031{
Rob Landley21ccbb62005-11-04 01:20:46 +000032 int status = -1;
Denis Vlasenko7039a662006-10-08 17:54:47 +000033 off_t total = 0;
Denys Vlasenko1eaf7e72010-06-26 23:08:59 +020034 bool continue_on_write_error = 0;
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010035 ssize_t sendfile_sz;
36#if CONFIG_FEATURE_COPYBUF_KB > 4
37 char *buffer = buffer; /* for compiler */
38 int buffer_size = 0;
39#else
Denis Vlasenko4c139222007-12-02 03:27:42 +000040 char buffer[CONFIG_FEATURE_COPYBUF_KB * 1024];
41 enum { buffer_size = sizeof(buffer) };
Denys Vlasenko1eaf7e72010-06-26 23:08:59 +020042#endif
Denys Vlasenkod0a8a0d2010-06-26 18:11:44 +020043
44 if (size < 0) {
45 size = -size;
46 continue_on_write_error = 1;
47 }
Denis Vlasenko4c139222007-12-02 03:27:42 +000048
Denis Vlasenko99912ca2007-04-10 15:43:37 +000049 if (src_fd < 0)
50 goto out;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000051
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010052 sendfile_sz = !ENABLE_FEATURE_USE_SENDFILE
53 ? 0
54 : SENDFILE_BIGBUF;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000055 if (!size) {
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010056 size = SENDFILE_BIGBUF;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000057 status = 1; /* copy until eof */
58 }
Rob Landley90334532005-11-12 11:04:11 +000059
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000060 while (1) {
61 ssize_t rd;
62
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010063 if (sendfile_sz) {
64 rd = sendfile(dst_fd, src_fd, NULL,
65 size > sendfile_sz ? sendfile_sz : size);
66 if (rd >= 0)
67 goto read_ok;
68 sendfile_sz = 0; /* do not try sendfile anymore */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000069 }
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010070#if CONFIG_FEATURE_COPYBUF_KB > 4
71 if (buffer_size == 0) {
72 if (size > 0 && size <= 4 * 1024)
73 goto use_small_buf;
74 /* We want page-aligned buffer, just in case kernel is clever
75 * and can do page-aligned io more efficiently */
76 buffer = mmap(NULL, CONFIG_FEATURE_COPYBUF_KB * 1024,
77 PROT_READ | PROT_WRITE,
78 MAP_PRIVATE | MAP_ANON,
79 /* ignored: */ -1, 0);
80 buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024;
81 if (buffer == MAP_FAILED) {
82 use_small_buf:
83 buffer = alloca(4 * 1024);
84 buffer_size = 4 * 1024;
85 }
86 }
87#endif
88 rd = safe_read(src_fd, buffer,
89 size > buffer_size ? buffer_size : size);
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000090 if (rd < 0) {
91 bb_perror_msg(bb_msg_read_error);
92 break;
93 }
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010094 read_ok:
95 if (!rd) { /* eof - all done */
96 status = 0;
97 break;
98 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000099 /* dst_fd == -1 is a fake, else... */
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +0100100 if (dst_fd >= 0 && !sendfile_sz) {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000101 ssize_t wr = full_write(dst_fd, buffer, rd);
Rob Landley53437472006-07-16 08:14:35 +0000102 if (wr < rd) {
Denys Vlasenkod0a8a0d2010-06-26 18:11:44 +0200103 if (!continue_on_write_error) {
104 bb_perror_msg(bb_msg_write_error);
105 break;
106 }
107 dst_fd = -1;
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000108 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000109 }
110 total += rd;
Denis Vlasenko714701c2006-12-22 00:21:07 +0000111 if (status < 0) { /* if we aren't copying till EOF... */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000112 size -= rd;
113 if (!size) {
Denis Vlasenko714701c2006-12-22 00:21:07 +0000114 /* 'size' bytes copied - all done */
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000115 status = 0;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000116 break;
117 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000118 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000119 }
Denis Vlasenko714701c2006-12-22 00:21:07 +0000120 out:
Denis Vlasenko4c139222007-12-02 03:27:42 +0000121
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +0100122 if (buffer_size > 4 * 1024)
Denis Vlasenko4c139222007-12-02 03:27:42 +0000123 munmap(buffer, buffer_size);
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000124 return status ? -1 : total;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000125}
126
127
Denis Vlasenko714701c2006-12-22 00:21:07 +0000128#if 0
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000129void FAST_FUNC complain_copyfd_and_die(off_t sz)
Denis Vlasenko714701c2006-12-22 00:21:07 +0000130{
131 if (sz != -1)
132 bb_error_msg_and_die("short read");
133 /* if sz == -1, bb_copyfd_XX already complained */
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000134 xfunc_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +0000135}
136#endif
137
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000138off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000139{
Glenn L McGrathc3498f62003-11-24 23:50:07 +0000140 if (size) {
Denis Vlasenko6231e872006-09-17 15:45:48 +0000141 return bb_full_fd_action(fd1, fd2, size);
Glenn L McGrathc3498f62003-11-24 23:50:07 +0000142 }
Denis Vlasenko6231e872006-09-17 15:45:48 +0000143 return 0;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000144}
145
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000146void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size)
Denis Vlasenko714701c2006-12-22 00:21:07 +0000147{
148 off_t sz = bb_copyfd_size(fd1, fd2, size);
Denys Vlasenkod0a8a0d2010-06-26 18:11:44 +0200149 if (sz == (size >= 0 ? size : -size))
Denis Vlasenko714701c2006-12-22 00:21:07 +0000150 return;
151 if (sz != -1)
152 bb_error_msg_and_die("short read");
153 /* if sz == -1, bb_copyfd_XX already complained */
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000154 xfunc_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +0000155}
156
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000157off_t FAST_FUNC bb_copyfd_eof(int fd1, int fd2)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000158{
Denis Vlasenko6231e872006-09-17 15:45:48 +0000159 return bb_full_fd_action(fd1, fd2, 0);
Eric Andersen55f98722001-05-15 17:48:09 +0000160}