blob: eda2747f991b0253b8fe3596d79035a5c373dff3 [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"
Eric Andersen55f98722001-05-15 17:48:09 +000011
Denys Vlasenkod0a8a0d2010-06-26 18:11:44 +020012/* Used by NOFORK applets (e.g. cat) - must not use xmalloc.
13 * size < 0 means "ignore write errors", used by tar --to-command
14 * size = 0 means "copy till EOF"
15 */
Denis Vlasenko7039a662006-10-08 17:54:47 +000016static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
Eric Andersen55f98722001-05-15 17:48:09 +000017{
Rob Landley21ccbb62005-11-04 01:20:46 +000018 int status = -1;
Denis Vlasenko7039a662006-10-08 17:54:47 +000019 off_t total = 0;
Denys Vlasenko1eaf7e72010-06-26 23:08:59 +020020 bool continue_on_write_error = 0;
Denis Vlasenko4c139222007-12-02 03:27:42 +000021#if CONFIG_FEATURE_COPYBUF_KB <= 4
22 char buffer[CONFIG_FEATURE_COPYBUF_KB * 1024];
23 enum { buffer_size = sizeof(buffer) };
24#else
25 char *buffer;
26 int buffer_size;
Denys Vlasenko1eaf7e72010-06-26 23:08:59 +020027#endif
Denys Vlasenkod0a8a0d2010-06-26 18:11:44 +020028
29 if (size < 0) {
30 size = -size;
31 continue_on_write_error = 1;
32 }
Denis Vlasenko4c139222007-12-02 03:27:42 +000033
Denys Vlasenko1eaf7e72010-06-26 23:08:59 +020034#if CONFIG_FEATURE_COPYBUF_KB > 4
Denys Vlasenkobcda0042010-03-19 14:34:30 +010035 if (size > 0 && size <= 4 * 1024)
36 goto use_small_buf;
Denis Vlasenko8003e262007-12-02 07:18:29 +000037 /* We want page-aligned buffer, just in case kernel is clever
38 * and can do page-aligned io more efficiently */
Denis Vlasenko4c139222007-12-02 03:27:42 +000039 buffer = mmap(NULL, CONFIG_FEATURE_COPYBUF_KB * 1024,
40 PROT_READ | PROT_WRITE,
41 MAP_PRIVATE | MAP_ANON,
42 /* ignored: */ -1, 0);
43 buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024;
44 if (buffer == MAP_FAILED) {
Denys Vlasenkobcda0042010-03-19 14:34:30 +010045 use_small_buf:
Denis Vlasenko4c139222007-12-02 03:27:42 +000046 buffer = alloca(4 * 1024);
47 buffer_size = 4 * 1024;
48 }
49#endif
Eric Andersen55f98722001-05-15 17:48:09 +000050
Denis Vlasenko99912ca2007-04-10 15:43:37 +000051 if (src_fd < 0)
52 goto out;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000053
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000054 if (!size) {
Denis Vlasenko4c139222007-12-02 03:27:42 +000055 size = buffer_size;
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
Denis Vlasenko4c139222007-12-02 03:27:42 +000062 rd = safe_read(src_fd, buffer, size > buffer_size ? buffer_size : size);
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000063
Denis Vlasenko714701c2006-12-22 00:21:07 +000064 if (!rd) { /* eof - all done */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000065 status = 0;
66 break;
67 }
68 if (rd < 0) {
69 bb_perror_msg(bb_msg_read_error);
70 break;
71 }
72 /* dst_fd == -1 is a fake, else... */
73 if (dst_fd >= 0) {
74 ssize_t wr = full_write(dst_fd, buffer, rd);
Rob Landley53437472006-07-16 08:14:35 +000075 if (wr < rd) {
Denys Vlasenkod0a8a0d2010-06-26 18:11:44 +020076 if (!continue_on_write_error) {
77 bb_perror_msg(bb_msg_write_error);
78 break;
79 }
80 dst_fd = -1;
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000081 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000082 }
83 total += rd;
Denis Vlasenko714701c2006-12-22 00:21:07 +000084 if (status < 0) { /* if we aren't copying till EOF... */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000085 size -= rd;
86 if (!size) {
Denis Vlasenko714701c2006-12-22 00:21:07 +000087 /* 'size' bytes copied - all done */
Denis Vlasenkof7996f32007-01-11 17:20:00 +000088 status = 0;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000089 break;
90 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000091 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000092 }
Denis Vlasenko714701c2006-12-22 00:21:07 +000093 out:
Denis Vlasenko4c139222007-12-02 03:27:42 +000094
95#if CONFIG_FEATURE_COPYBUF_KB > 4
96 if (buffer_size != 4 * 1024)
97 munmap(buffer, buffer_size);
98#endif
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000099 return status ? -1 : total;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000100}
101
102
Denis Vlasenko714701c2006-12-22 00:21:07 +0000103#if 0
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000104void FAST_FUNC complain_copyfd_and_die(off_t sz)
Denis Vlasenko714701c2006-12-22 00:21:07 +0000105{
106 if (sz != -1)
107 bb_error_msg_and_die("short read");
108 /* if sz == -1, bb_copyfd_XX already complained */
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000109 xfunc_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +0000110}
111#endif
112
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000113off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000114{
Glenn L McGrathc3498f62003-11-24 23:50:07 +0000115 if (size) {
Denis Vlasenko6231e872006-09-17 15:45:48 +0000116 return bb_full_fd_action(fd1, fd2, size);
Glenn L McGrathc3498f62003-11-24 23:50:07 +0000117 }
Denis Vlasenko6231e872006-09-17 15:45:48 +0000118 return 0;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000119}
120
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000121void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size)
Denis Vlasenko714701c2006-12-22 00:21:07 +0000122{
123 off_t sz = bb_copyfd_size(fd1, fd2, size);
Denys Vlasenkod0a8a0d2010-06-26 18:11:44 +0200124 if (sz == (size >= 0 ? size : -size))
Denis Vlasenko714701c2006-12-22 00:21:07 +0000125 return;
126 if (sz != -1)
127 bb_error_msg_and_die("short read");
128 /* if sz == -1, bb_copyfd_XX already complained */
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000129 xfunc_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +0000130}
131
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000132off_t FAST_FUNC bb_copyfd_eof(int fd1, int fd2)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000133{
Denis Vlasenko6231e872006-09-17 15:45:48 +0000134 return bb_full_fd_action(fd1, fd2, 0);
Eric Andersen55f98722001-05-15 17:48:09 +0000135}