blob: f42eb76239890085f7f15cb086967872fcf677dc [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 *
Rob Landley21ccbb62005-11-04 01:20:46 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
Denis Vlasenko50f7f442007-04-11 23:20:53 +000012/* Used by NOFORK applets (e.g. cat) - must not use xmalloc */
Glenn L McGrathf0515da2003-11-22 02:13:41 +000013
Denis Vlasenko7039a662006-10-08 17:54:47 +000014static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
Eric Andersen55f98722001-05-15 17:48:09 +000015{
Rob Landley21ccbb62005-11-04 01:20:46 +000016 int status = -1;
Denis Vlasenko7039a662006-10-08 17:54:47 +000017 off_t total = 0;
Denis Vlasenko4c139222007-12-02 03:27:42 +000018#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
Denys Vlasenkobcda0042010-03-19 14:34:30 +010025 if (size > 0 && size <= 4 * 1024)
26 goto use_small_buf;
Denis Vlasenko8003e262007-12-02 07:18:29 +000027 /* We want page-aligned buffer, just in case kernel is clever
28 * and can do page-aligned io more efficiently */
Denis Vlasenko4c139222007-12-02 03:27:42 +000029 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) {
Denys Vlasenkobcda0042010-03-19 14:34:30 +010035 use_small_buf:
Denis Vlasenko4c139222007-12-02 03:27:42 +000036 buffer = alloca(4 * 1024);
37 buffer_size = 4 * 1024;
38 }
39#endif
Eric Andersen55f98722001-05-15 17:48:09 +000040
Denis Vlasenko99912ca2007-04-10 15:43:37 +000041 if (src_fd < 0)
42 goto out;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000043
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000044 if (!size) {
Denis Vlasenko4c139222007-12-02 03:27:42 +000045 size = buffer_size;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000046 status = 1; /* copy until eof */
47 }
Rob Landley90334532005-11-12 11:04:11 +000048
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000049 while (1) {
50 ssize_t rd;
51
Denis Vlasenko4c139222007-12-02 03:27:42 +000052 rd = safe_read(src_fd, buffer, size > buffer_size ? buffer_size : size);
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000053
Denis Vlasenko714701c2006-12-22 00:21:07 +000054 if (!rd) { /* eof - all done */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000055 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);
Rob Landley53437472006-07-16 08:14:35 +000065 if (wr < rd) {
Rob Landley21ccbb62005-11-04 01:20:46 +000066 bb_perror_msg(bb_msg_write_error);
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 break;
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000068 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000069 }
70 total += rd;
Denis Vlasenko714701c2006-12-22 00:21:07 +000071 if (status < 0) { /* if we aren't copying till EOF... */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000072 size -= rd;
73 if (!size) {
Denis Vlasenko714701c2006-12-22 00:21:07 +000074 /* 'size' bytes copied - all done */
Denis Vlasenkof7996f32007-01-11 17:20:00 +000075 status = 0;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000076 break;
77 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000078 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000079 }
Denis Vlasenko714701c2006-12-22 00:21:07 +000080 out:
Denis Vlasenko4c139222007-12-02 03:27:42 +000081
82#if CONFIG_FEATURE_COPYBUF_KB > 4
83 if (buffer_size != 4 * 1024)
84 munmap(buffer, buffer_size);
85#endif
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000086 return status ? -1 : total;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000087}
88
89
Denis Vlasenko714701c2006-12-22 00:21:07 +000090#if 0
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000091void FAST_FUNC complain_copyfd_and_die(off_t sz)
Denis Vlasenko714701c2006-12-22 00:21:07 +000092{
93 if (sz != -1)
94 bb_error_msg_and_die("short read");
95 /* if sz == -1, bb_copyfd_XX already complained */
Denis Vlasenko335b63d2007-04-10 21:38:30 +000096 xfunc_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +000097}
98#endif
99
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000100off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000101{
Glenn L McGrathc3498f62003-11-24 23:50:07 +0000102 if (size) {
Denis Vlasenko6231e872006-09-17 15:45:48 +0000103 return bb_full_fd_action(fd1, fd2, size);
Glenn L McGrathc3498f62003-11-24 23:50:07 +0000104 }
Denis Vlasenko6231e872006-09-17 15:45:48 +0000105 return 0;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000106}
107
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000108void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size)
Denis Vlasenko714701c2006-12-22 00:21:07 +0000109{
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 */
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000116 xfunc_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +0000117}
118
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000119off_t FAST_FUNC bb_copyfd_eof(int fd1, int fd2)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000120{
Denis Vlasenko6231e872006-09-17 15:45:48 +0000121 return bb_full_fd_action(fd1, fd2, 0);
Eric Andersen55f98722001-05-15 17:48:09 +0000122}