blob: 08bc6f8dfd7cea500550685bd548bd4151940f41 [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
Denis Vlasenko8003e262007-12-02 07:18:29 +000025 /* We want page-aligned buffer, just in case kernel is clever
26 * and can do page-aligned io more efficiently */
Denis Vlasenko4c139222007-12-02 03:27:42 +000027 buffer = mmap(NULL, CONFIG_FEATURE_COPYBUF_KB * 1024,
28 PROT_READ | PROT_WRITE,
29 MAP_PRIVATE | MAP_ANON,
30 /* ignored: */ -1, 0);
31 buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024;
32 if (buffer == MAP_FAILED) {
33 buffer = alloca(4 * 1024);
34 buffer_size = 4 * 1024;
35 }
36#endif
Eric Andersen55f98722001-05-15 17:48:09 +000037
Denis Vlasenko99912ca2007-04-10 15:43:37 +000038 if (src_fd < 0)
39 goto out;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000040
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000041 if (!size) {
Denis Vlasenko4c139222007-12-02 03:27:42 +000042 size = buffer_size;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000043 status = 1; /* copy until eof */
44 }
Rob Landley90334532005-11-12 11:04:11 +000045
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000046 while (1) {
47 ssize_t rd;
48
Denis Vlasenko4c139222007-12-02 03:27:42 +000049 rd = safe_read(src_fd, buffer, size > buffer_size ? buffer_size : size);
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000050
Denis Vlasenko714701c2006-12-22 00:21:07 +000051 if (!rd) { /* eof - all done */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000052 status = 0;
53 break;
54 }
55 if (rd < 0) {
56 bb_perror_msg(bb_msg_read_error);
57 break;
58 }
59 /* dst_fd == -1 is a fake, else... */
60 if (dst_fd >= 0) {
61 ssize_t wr = full_write(dst_fd, buffer, rd);
Rob Landley53437472006-07-16 08:14:35 +000062 if (wr < rd) {
Rob Landley21ccbb62005-11-04 01:20:46 +000063 bb_perror_msg(bb_msg_write_error);
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 break;
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000065 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000066 }
67 total += rd;
Denis Vlasenko714701c2006-12-22 00:21:07 +000068 if (status < 0) { /* if we aren't copying till EOF... */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000069 size -= rd;
70 if (!size) {
Denis Vlasenko714701c2006-12-22 00:21:07 +000071 /* 'size' bytes copied - all done */
Denis Vlasenkof7996f32007-01-11 17:20:00 +000072 status = 0;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000073 break;
74 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000075 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000076 }
Denis Vlasenko714701c2006-12-22 00:21:07 +000077 out:
Denis Vlasenko4c139222007-12-02 03:27:42 +000078
79#if CONFIG_FEATURE_COPYBUF_KB > 4
80 if (buffer_size != 4 * 1024)
81 munmap(buffer, buffer_size);
82#endif
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000083 return status ? -1 : total;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000084}
85
86
Denis Vlasenko714701c2006-12-22 00:21:07 +000087#if 0
88void complain_copyfd_and_die(off_t sz)
89{
90 if (sz != -1)
91 bb_error_msg_and_die("short read");
92 /* if sz == -1, bb_copyfd_XX already complained */
Denis Vlasenko335b63d2007-04-10 21:38:30 +000093 xfunc_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +000094}
95#endif
96
Denis Vlasenko7039a662006-10-08 17:54:47 +000097off_t bb_copyfd_size(int fd1, int fd2, off_t size)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000098{
Glenn L McGrathc3498f62003-11-24 23:50:07 +000099 if (size) {
Denis Vlasenko6231e872006-09-17 15:45:48 +0000100 return bb_full_fd_action(fd1, fd2, size);
Glenn L McGrathc3498f62003-11-24 23:50:07 +0000101 }
Denis Vlasenko6231e872006-09-17 15:45:48 +0000102 return 0;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000103}
104
Denis Vlasenko714701c2006-12-22 00:21:07 +0000105void bb_copyfd_exact_size(int fd1, int fd2, off_t size)
106{
107 off_t sz = bb_copyfd_size(fd1, fd2, size);
108 if (sz == size)
109 return;
110 if (sz != -1)
111 bb_error_msg_and_die("short read");
112 /* if sz == -1, bb_copyfd_XX already complained */
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000113 xfunc_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +0000114}
115
Denis Vlasenko7039a662006-10-08 17:54:47 +0000116off_t bb_copyfd_eof(int fd1, int fd2)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000117{
Denis Vlasenko6231e872006-09-17 15:45:48 +0000118 return bb_full_fd_action(fd1, fd2, 0);
Eric Andersen55f98722001-05-15 17:48:09 +0000119}