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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
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 | |
Denys Vlasenko | d0a8a0d | 2010-06-26 18:11:44 +0200 | [diff] [blame] | 12 | /* 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 Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 16 | 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] | 17 | { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 18 | int status = -1; |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 19 | off_t total = 0; |
Denys Vlasenko | 1eaf7e7 | 2010-06-26 23:08:59 +0200 | [diff] [blame] | 20 | bool continue_on_write_error = 0; |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 21 | #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 Vlasenko | 1eaf7e7 | 2010-06-26 23:08:59 +0200 | [diff] [blame] | 27 | #endif |
Denys Vlasenko | d0a8a0d | 2010-06-26 18:11:44 +0200 | [diff] [blame] | 28 | |
| 29 | if (size < 0) { |
| 30 | size = -size; |
| 31 | continue_on_write_error = 1; |
| 32 | } |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 33 | |
Denys Vlasenko | 1eaf7e7 | 2010-06-26 23:08:59 +0200 | [diff] [blame] | 34 | #if CONFIG_FEATURE_COPYBUF_KB > 4 |
Denys Vlasenko | bcda004 | 2010-03-19 14:34:30 +0100 | [diff] [blame] | 35 | if (size > 0 && size <= 4 * 1024) |
| 36 | goto use_small_buf; |
Denis Vlasenko | 8003e26 | 2007-12-02 07:18:29 +0000 | [diff] [blame] | 37 | /* We want page-aligned buffer, just in case kernel is clever |
| 38 | * and can do page-aligned io more efficiently */ |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 39 | 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 Vlasenko | bcda004 | 2010-03-19 14:34:30 +0100 | [diff] [blame] | 45 | use_small_buf: |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 46 | buffer = alloca(4 * 1024); |
| 47 | buffer_size = 4 * 1024; |
| 48 | } |
| 49 | #endif |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 51 | if (src_fd < 0) |
| 52 | goto out; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 53 | |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 54 | if (!size) { |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 55 | size = buffer_size; |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 56 | status = 1; /* copy until eof */ |
| 57 | } |
Rob Landley | 9033453 | 2005-11-12 11:04:11 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 59 | while (1) { |
| 60 | ssize_t rd; |
| 61 | |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 62 | rd = safe_read(src_fd, buffer, size > buffer_size ? buffer_size : size); |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 64 | if (!rd) { /* eof - all done */ |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 65 | 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 Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 75 | if (wr < rd) { |
Denys Vlasenko | d0a8a0d | 2010-06-26 18:11:44 +0200 | [diff] [blame] | 76 | if (!continue_on_write_error) { |
| 77 | bb_perror_msg(bb_msg_write_error); |
| 78 | break; |
| 79 | } |
| 80 | dst_fd = -1; |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 81 | } |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 82 | } |
| 83 | total += rd; |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 84 | if (status < 0) { /* if we aren't copying till EOF... */ |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 85 | size -= rd; |
| 86 | if (!size) { |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 87 | /* 'size' bytes copied - all done */ |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 88 | status = 0; |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 89 | break; |
| 90 | } |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 91 | } |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 92 | } |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 93 | out: |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 94 | |
| 95 | #if CONFIG_FEATURE_COPYBUF_KB > 4 |
| 96 | if (buffer_size != 4 * 1024) |
| 97 | munmap(buffer, buffer_size); |
| 98 | #endif |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 99 | return status ? -1 : total; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 103 | #if 0 |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 104 | void FAST_FUNC complain_copyfd_and_die(off_t sz) |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 105 | { |
| 106 | if (sz != -1) |
| 107 | bb_error_msg_and_die("short read"); |
| 108 | /* if sz == -1, bb_copyfd_XX already complained */ |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 109 | xfunc_die(); |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 110 | } |
| 111 | #endif |
| 112 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 113 | off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size) |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 114 | { |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 115 | if (size) { |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 116 | return bb_full_fd_action(fd1, fd2, size); |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 117 | } |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 118 | return 0; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 121 | void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size) |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 122 | { |
| 123 | off_t sz = bb_copyfd_size(fd1, fd2, size); |
Denys Vlasenko | d0a8a0d | 2010-06-26 18:11:44 +0200 | [diff] [blame] | 124 | if (sz == (size >= 0 ? size : -size)) |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 125 | return; |
| 126 | if (sz != -1) |
| 127 | bb_error_msg_and_die("short read"); |
| 128 | /* if sz == -1, bb_copyfd_XX already complained */ |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 129 | xfunc_die(); |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 132 | off_t FAST_FUNC bb_copyfd_eof(int fd1, int fd2) |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 133 | { |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 134 | return bb_full_fd_action(fd1, fd2, 0); |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 135 | } |