blob: dd0517cd6fde71d3b67ec42b4172252945457797 [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 */
Eric Andersen451c0d22005-04-16 07:46:53 +00009#include "libbb.h"
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010010#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 neddlesly short reads.
22 */
23#define SENDFILE_BIGBUF (16*1024*1024)
Eric Andersen55f98722001-05-15 17:48:09 +000024
Denys Vlasenkod0a8a0d2010-06-26 18:11:44 +020025/* 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 */
Denis Vlasenko7039a662006-10-08 17:54:47 +000029static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
Eric Andersen55f98722001-05-15 17:48:09 +000030{
Rob Landley21ccbb62005-11-04 01:20:46 +000031 int status = -1;
Denis Vlasenko7039a662006-10-08 17:54:47 +000032 off_t total = 0;
Denys Vlasenko1eaf7e72010-06-26 23:08:59 +020033 bool continue_on_write_error = 0;
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010034 ssize_t sendfile_sz;
35#if CONFIG_FEATURE_COPYBUF_KB > 4
36 char *buffer = buffer; /* for compiler */
37 int buffer_size = 0;
38#else
Denis Vlasenko4c139222007-12-02 03:27:42 +000039 char buffer[CONFIG_FEATURE_COPYBUF_KB * 1024];
40 enum { buffer_size = sizeof(buffer) };
Denys Vlasenko1eaf7e72010-06-26 23:08:59 +020041#endif
Denys Vlasenkod0a8a0d2010-06-26 18:11:44 +020042
43 if (size < 0) {
44 size = -size;
45 continue_on_write_error = 1;
46 }
Denis Vlasenko4c139222007-12-02 03:27:42 +000047
Denis Vlasenko99912ca2007-04-10 15:43:37 +000048 if (src_fd < 0)
49 goto out;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000050
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010051 sendfile_sz = !ENABLE_FEATURE_USE_SENDFILE
52 ? 0
53 : SENDFILE_BIGBUF;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000054 if (!size) {
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010055 size = SENDFILE_BIGBUF;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000056 status = 1; /* copy until eof */
57 }
Rob Landley90334532005-11-12 11:04:11 +000058
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000059 while (1) {
60 ssize_t rd;
61
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010062 if (sendfile_sz) {
63 rd = sendfile(dst_fd, src_fd, NULL,
64 size > sendfile_sz ? sendfile_sz : size);
65 if (rd >= 0)
66 goto read_ok;
67 sendfile_sz = 0; /* do not try sendfile anymore */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000068 }
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010069#if CONFIG_FEATURE_COPYBUF_KB > 4
70 if (buffer_size == 0) {
71 if (size > 0 && size <= 4 * 1024)
72 goto use_small_buf;
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) {
81 use_small_buf:
82 buffer = alloca(4 * 1024);
83 buffer_size = 4 * 1024;
84 }
85 }
86#endif
87 rd = safe_read(src_fd, buffer,
88 size > buffer_size ? buffer_size : size);
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000089 if (rd < 0) {
90 bb_perror_msg(bb_msg_read_error);
91 break;
92 }
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010093 read_ok:
94 if (!rd) { /* eof - all done */
95 status = 0;
96 break;
97 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000098 /* dst_fd == -1 is a fake, else... */
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +010099 if (dst_fd >= 0 && !sendfile_sz) {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000100 ssize_t wr = full_write(dst_fd, buffer, rd);
Rob Landley53437472006-07-16 08:14:35 +0000101 if (wr < rd) {
Denys Vlasenkod0a8a0d2010-06-26 18:11:44 +0200102 if (!continue_on_write_error) {
103 bb_perror_msg(bb_msg_write_error);
104 break;
105 }
106 dst_fd = -1;
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000107 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000108 }
109 total += rd;
Denis Vlasenko714701c2006-12-22 00:21:07 +0000110 if (status < 0) { /* if we aren't copying till EOF... */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000111 size -= rd;
112 if (!size) {
Denis Vlasenko714701c2006-12-22 00:21:07 +0000113 /* 'size' bytes copied - all done */
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000114 status = 0;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000115 break;
116 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000117 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000118 }
Denis Vlasenko714701c2006-12-22 00:21:07 +0000119 out:
Denis Vlasenko4c139222007-12-02 03:27:42 +0000120
Johannes Schindeline5b1f5a2017-07-14 16:11:43 +0200121/* some environments don't have munmap(), hide it in #if */
122#if CONFIG_FEATURE_COPYBUF_KB > 4
Bartosz Golaszewski8d75d792014-11-27 13:20:24 +0100123 if (buffer_size > 4 * 1024)
Denis Vlasenko4c139222007-12-02 03:27:42 +0000124 munmap(buffer, buffer_size);
Johannes Schindeline5b1f5a2017-07-14 16:11:43 +0200125#endif
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000126 return status ? -1 : total;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000127}
128
129
Denis Vlasenko714701c2006-12-22 00:21:07 +0000130#if 0
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000131void FAST_FUNC complain_copyfd_and_die(off_t sz)
Denis Vlasenko714701c2006-12-22 00:21:07 +0000132{
133 if (sz != -1)
134 bb_error_msg_and_die("short read");
135 /* if sz == -1, bb_copyfd_XX already complained */
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000136 xfunc_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +0000137}
138#endif
139
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000140off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000141{
Glenn L McGrathc3498f62003-11-24 23:50:07 +0000142 if (size) {
Denis Vlasenko6231e872006-09-17 15:45:48 +0000143 return bb_full_fd_action(fd1, fd2, size);
Glenn L McGrathc3498f62003-11-24 23:50:07 +0000144 }
Denis Vlasenko6231e872006-09-17 15:45:48 +0000145 return 0;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000146}
147
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000148void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size)
Denis Vlasenko714701c2006-12-22 00:21:07 +0000149{
150 off_t sz = bb_copyfd_size(fd1, fd2, size);
Denys Vlasenkod0a8a0d2010-06-26 18:11:44 +0200151 if (sz == (size >= 0 ? size : -size))
Denis Vlasenko714701c2006-12-22 00:21:07 +0000152 return;
153 if (sz != -1)
154 bb_error_msg_and_die("short read");
155 /* if sz == -1, bb_copyfd_XX already complained */
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000156 xfunc_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +0000157}
158
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000159off_t FAST_FUNC bb_copyfd_eof(int fd1, int fd2)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000160{
Denis Vlasenko6231e872006-09-17 15:45:48 +0000161 return bb_full_fd_action(fd1, fd2, 0);
Eric Andersen55f98722001-05-15 17:48:09 +0000162}