copyfd.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. #if ENABLE_FEATURE_USE_SENDFILE
  11. # include <sys/sendfile.h>
  12. #else
  13. # define sendfile(a,b,c,d) (-1)
  14. #endif
  15. /*
  16. * We were using 0x7fff0000 as sendfile chunk size, but it
  17. * was seen to cause largish delays when user tries to ^C a file copy.
  18. * Let's use a saner size.
  19. * Note: needs to be >= max(CONFIG_FEATURE_COPYBUF_KB),
  20. * or else "copy to eof" code will use needlesly short reads.
  21. */
  22. #define SENDFILE_BIGBUF (16*1024*1024)
  23. /* Used by NOFORK applets (e.g. cat) - must not use xmalloc.
  24. * size < 0 means "ignore write errors", used by tar --to-command
  25. * size = 0 means "copy till EOF"
  26. */
  27. static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
  28. {
  29. int status = -1;
  30. off_t total = 0;
  31. bool continue_on_write_error = 0;
  32. ssize_t sendfile_sz;
  33. #if CONFIG_FEATURE_COPYBUF_KB > 4
  34. char *buffer = buffer; /* for compiler */
  35. int buffer_size = 0;
  36. #else
  37. char buffer[CONFIG_FEATURE_COPYBUF_KB * 1024];
  38. enum { buffer_size = sizeof(buffer) };
  39. #endif
  40. if (size < 0) {
  41. size = -size;
  42. continue_on_write_error = 1;
  43. }
  44. if (src_fd < 0)
  45. goto out;
  46. sendfile_sz = !ENABLE_FEATURE_USE_SENDFILE
  47. ? 0
  48. : SENDFILE_BIGBUF;
  49. if (!size) {
  50. size = SENDFILE_BIGBUF;
  51. status = 1; /* copy until eof */
  52. }
  53. while (1) {
  54. ssize_t rd;
  55. if (sendfile_sz) {
  56. /* dst_fd == -1 is a fake, else... */
  57. if (dst_fd >= 0) {
  58. rd = sendfile(dst_fd, src_fd, NULL,
  59. size > sendfile_sz ? sendfile_sz : size);
  60. if (rd >= 0)
  61. goto read_ok;
  62. }
  63. sendfile_sz = 0; /* do not try sendfile anymore */
  64. }
  65. #if CONFIG_FEATURE_COPYBUF_KB > 4
  66. if (buffer_size == 0) {
  67. if (size > 0 && size <= 4 * 1024)
  68. goto use_small_buf;
  69. /* We want page-aligned buffer, just in case kernel is clever
  70. * and can do page-aligned io more efficiently */
  71. buffer = mmap_anon(CONFIG_FEATURE_COPYBUF_KB * 1024);
  72. buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024;
  73. if (buffer == MAP_FAILED) {
  74. use_small_buf:
  75. buffer = alloca(4 * 1024);
  76. buffer_size = 4 * 1024;
  77. }
  78. }
  79. #endif
  80. rd = safe_read(src_fd, buffer,
  81. size > buffer_size ? buffer_size : size);
  82. if (rd < 0) {
  83. bb_simple_perror_msg(bb_msg_read_error);
  84. break;
  85. }
  86. read_ok:
  87. if (!rd) { /* eof - all done */
  88. status = 0;
  89. break;
  90. }
  91. /* dst_fd == -1 is a fake, else... */
  92. if (dst_fd >= 0 && !sendfile_sz) {
  93. ssize_t wr = full_write(dst_fd, buffer, rd);
  94. if (wr < rd) {
  95. if (!continue_on_write_error) {
  96. bb_simple_perror_msg(bb_msg_write_error);
  97. break;
  98. }
  99. dst_fd = -1;
  100. }
  101. }
  102. total += rd;
  103. if (status < 0) { /* if we aren't copying till EOF... */
  104. size -= rd;
  105. if (!size) {
  106. /* 'size' bytes copied - all done */
  107. status = 0;
  108. break;
  109. }
  110. }
  111. }
  112. out:
  113. /* some environments don't have munmap(), hide it in #if */
  114. #if CONFIG_FEATURE_COPYBUF_KB > 4
  115. if (buffer_size > 4 * 1024)
  116. munmap(buffer, buffer_size);
  117. #endif
  118. return status ? -1 : total;
  119. }
  120. #if 0
  121. void FAST_FUNC complain_copyfd_and_die(off_t sz)
  122. {
  123. if (sz != -1)
  124. bb_error_msg_and_die("short read");
  125. /* if sz == -1, bb_copyfd_XX already complained */
  126. xfunc_die();
  127. }
  128. #endif
  129. off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size)
  130. {
  131. if (size) {
  132. return bb_full_fd_action(fd1, fd2, size);
  133. }
  134. return 0;
  135. }
  136. void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size)
  137. {
  138. off_t sz = bb_copyfd_size(fd1, fd2, size);
  139. if (sz == (size >= 0 ? size : -size))
  140. return;
  141. if (sz != -1)
  142. bb_simple_error_msg_and_die("short read");
  143. /* if sz == -1, bb_copyfd_XX already complained */
  144. xfunc_die();
  145. }
  146. off_t FAST_FUNC bb_copyfd_eof(int fd1, int fd2)
  147. {
  148. return bb_full_fd_action(fd1, fd2, 0);
  149. }