copyfd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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(NULL, CONFIG_FEATURE_COPYBUF_KB * 1024,
  72. PROT_READ | PROT_WRITE,
  73. MAP_PRIVATE | MAP_ANON,
  74. /* ignored: */ -1, 0);
  75. buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024;
  76. if (buffer == MAP_FAILED) {
  77. use_small_buf:
  78. buffer = alloca(4 * 1024);
  79. buffer_size = 4 * 1024;
  80. }
  81. }
  82. #endif
  83. rd = safe_read(src_fd, buffer,
  84. size > buffer_size ? buffer_size : size);
  85. if (rd < 0) {
  86. bb_simple_perror_msg(bb_msg_read_error);
  87. break;
  88. }
  89. read_ok:
  90. if (!rd) { /* eof - all done */
  91. status = 0;
  92. break;
  93. }
  94. /* dst_fd == -1 is a fake, else... */
  95. if (dst_fd >= 0 && !sendfile_sz) {
  96. ssize_t wr = full_write(dst_fd, buffer, rd);
  97. if (wr < rd) {
  98. if (!continue_on_write_error) {
  99. bb_simple_perror_msg(bb_msg_write_error);
  100. break;
  101. }
  102. dst_fd = -1;
  103. }
  104. }
  105. total += rd;
  106. if (status < 0) { /* if we aren't copying till EOF... */
  107. size -= rd;
  108. if (!size) {
  109. /* 'size' bytes copied - all done */
  110. status = 0;
  111. break;
  112. }
  113. }
  114. }
  115. out:
  116. /* some environments don't have munmap(), hide it in #if */
  117. #if CONFIG_FEATURE_COPYBUF_KB > 4
  118. if (buffer_size > 4 * 1024)
  119. munmap(buffer, buffer_size);
  120. #endif
  121. return status ? -1 : total;
  122. }
  123. #if 0
  124. void FAST_FUNC complain_copyfd_and_die(off_t sz)
  125. {
  126. if (sz != -1)
  127. bb_error_msg_and_die("short read");
  128. /* if sz == -1, bb_copyfd_XX already complained */
  129. xfunc_die();
  130. }
  131. #endif
  132. off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size)
  133. {
  134. if (size) {
  135. return bb_full_fd_action(fd1, fd2, size);
  136. }
  137. return 0;
  138. }
  139. void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size)
  140. {
  141. off_t sz = bb_copyfd_size(fd1, fd2, size);
  142. if (sz == (size >= 0 ? size : -size))
  143. return;
  144. if (sz != -1)
  145. bb_simple_error_msg_and_die("short read");
  146. /* if sz == -1, bb_copyfd_XX already complained */
  147. xfunc_die();
  148. }
  149. off_t FAST_FUNC bb_copyfd_eof(int fd1, int fd2)
  150. {
  151. return bb_full_fd_action(fd1, fd2, 0);
  152. }