Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 6 | * |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 11 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 12 | /* Used by NOFORK applets (e.g. cat) - must not use xmalloc */ |
Glenn L McGrath | f0515da | 2003-11-22 02:13:41 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 14 | static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size) |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 15 | { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 16 | int status = -1; |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 17 | off_t total = 0; |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 18 | #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 Vlasenko | 8003e26 | 2007-12-02 07:18:29 +0000 | [diff] [blame] | 25 | /* We want page-aligned buffer, just in case kernel is clever |
| 26 | * and can do page-aligned io more efficiently */ |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 27 | 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 Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 38 | if (src_fd < 0) |
| 39 | goto out; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 41 | if (!size) { |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 42 | size = buffer_size; |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 43 | status = 1; /* copy until eof */ |
| 44 | } |
Rob Landley | 9033453 | 2005-11-12 11:04:11 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 46 | while (1) { |
| 47 | ssize_t rd; |
| 48 | |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 49 | rd = safe_read(src_fd, buffer, size > buffer_size ? buffer_size : size); |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 51 | if (!rd) { /* eof - all done */ |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 52 | 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 Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 62 | if (wr < rd) { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 63 | bb_perror_msg(bb_msg_write_error); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 64 | break; |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 65 | } |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 66 | } |
| 67 | total += rd; |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 68 | if (status < 0) { /* if we aren't copying till EOF... */ |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 69 | size -= rd; |
| 70 | if (!size) { |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 71 | /* 'size' bytes copied - all done */ |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 72 | status = 0; |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 73 | break; |
| 74 | } |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 75 | } |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 76 | } |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 77 | out: |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 78 | |
| 79 | #if CONFIG_FEATURE_COPYBUF_KB > 4 |
| 80 | if (buffer_size != 4 * 1024) |
| 81 | munmap(buffer, buffer_size); |
| 82 | #endif |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 83 | return status ? -1 : total; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 87 | #if 0 |
| 88 | void 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 Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 93 | xfunc_die(); |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 94 | } |
| 95 | #endif |
| 96 | |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 97 | off_t bb_copyfd_size(int fd1, int fd2, off_t size) |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 98 | { |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 99 | if (size) { |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 100 | return bb_full_fd_action(fd1, fd2, size); |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 101 | } |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 102 | return 0; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 105 | void 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 Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 113 | xfunc_die(); |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 116 | off_t bb_copyfd_eof(int fd1, int fd2) |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 117 | { |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 118 | return bb_full_fd_action(fd1, fd2, 0); |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 119 | } |