source: MondoRescue/branches/2.2.9/mindi-busybox/libbb/copyfd.c@ 2861

Last change on this file since 2861 was 2725, checked in by Bruno Cornec, 13 years ago
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File size: 2.8 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
6 *
[2725]7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
[821]8 */
9
10#include "libbb.h"
11
[2725]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 */
[1765]16static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
[821]17{
18 int status = -1;
[1765]19 off_t total = 0;
[2725]20 bool continue_on_write_error = 0;
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;
27#endif
[821]28
[2725]29 if (size < 0) {
30 size = -size;
31 continue_on_write_error = 1;
32 }
33
34#if CONFIG_FEATURE_COPYBUF_KB > 4
35 if (size > 0 && size <= 4 * 1024)
36 goto use_small_buf;
37 /* We want page-aligned buffer, just in case kernel is clever
38 * and can do page-aligned io more efficiently */
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) {
45 use_small_buf:
46 buffer = alloca(4 * 1024);
47 buffer_size = 4 * 1024;
48 }
49#endif
50
[1765]51 if (src_fd < 0)
52 goto out;
[821]53
[1765]54 if (!size) {
[2725]55 size = buffer_size;
[1765]56 status = 1; /* copy until eof */
57 }
[821]58
[1765]59 while (1) {
60 ssize_t rd;
61
[2725]62 rd = safe_read(src_fd, buffer, size > buffer_size ? buffer_size : size);
[1765]63
64 if (!rd) { /* eof - all done */
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);
75 if (wr < rd) {
[2725]76 if (!continue_on_write_error) {
77 bb_perror_msg(bb_msg_write_error);
78 break;
79 }
80 dst_fd = -1;
[821]81 }
82 }
[1765]83 total += rd;
84 if (status < 0) { /* if we aren't copying till EOF... */
85 size -= rd;
86 if (!size) {
87 /* 'size' bytes copied - all done */
88 status = 0;
89 break;
90 }
91 }
[821]92 }
[1765]93 out:
[2725]94
95#if CONFIG_FEATURE_COPYBUF_KB > 4
96 if (buffer_size != 4 * 1024)
97 munmap(buffer, buffer_size);
98#endif
[1765]99 return status ? -1 : total;
100}
[821]101
102
[1765]103#if 0
[2725]104void FAST_FUNC complain_copyfd_and_die(off_t sz)
[1765]105{
106 if (sz != -1)
107 bb_error_msg_and_die("short read");
108 /* if sz == -1, bb_copyfd_XX already complained */
109 xfunc_die();
[821]110}
[1765]111#endif
[821]112
[2725]113off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size)
[821]114{
115 if (size) {
[1765]116 return bb_full_fd_action(fd1, fd2, size);
[821]117 }
[1765]118 return 0;
[821]119}
120
[2725]121void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size)
[821]122{
[1765]123 off_t sz = bb_copyfd_size(fd1, fd2, size);
[2725]124 if (sz == (size >= 0 ? size : -size))
[1765]125 return;
126 if (sz != -1)
127 bb_error_msg_and_die("short read");
128 /* if sz == -1, bb_copyfd_XX already complained */
129 xfunc_die();
[821]130}
[1765]131
[2725]132off_t FAST_FUNC bb_copyfd_eof(int fd1, int fd2)
[1765]133{
134 return bb_full_fd_action(fd1, fd2, 0);
135}
Note: See TracBrowser for help on using the repository browser.