copyfd.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 tarball for details.
  8. */
  9. #include "libbb.h"
  10. /* Used by NOFORK applets (e.g. cat) - must not use xmalloc */
  11. static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
  12. {
  13. int status = -1;
  14. off_t total = 0;
  15. #if CONFIG_FEATURE_COPYBUF_KB <= 4
  16. char buffer[CONFIG_FEATURE_COPYBUF_KB * 1024];
  17. enum { buffer_size = sizeof(buffer) };
  18. #else
  19. char *buffer;
  20. int buffer_size;
  21. if (size > 0 && size <= 4 * 1024)
  22. goto use_small_buf;
  23. /* We want page-aligned buffer, just in case kernel is clever
  24. * and can do page-aligned io more efficiently */
  25. buffer = mmap(NULL, CONFIG_FEATURE_COPYBUF_KB * 1024,
  26. PROT_READ | PROT_WRITE,
  27. MAP_PRIVATE | MAP_ANON,
  28. /* ignored: */ -1, 0);
  29. buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024;
  30. if (buffer == MAP_FAILED) {
  31. use_small_buf:
  32. buffer = alloca(4 * 1024);
  33. buffer_size = 4 * 1024;
  34. }
  35. #endif
  36. if (src_fd < 0)
  37. goto out;
  38. if (!size) {
  39. size = buffer_size;
  40. status = 1; /* copy until eof */
  41. }
  42. while (1) {
  43. ssize_t rd;
  44. rd = safe_read(src_fd, buffer, size > buffer_size ? buffer_size : size);
  45. if (!rd) { /* eof - all done */
  46. status = 0;
  47. break;
  48. }
  49. if (rd < 0) {
  50. bb_perror_msg(bb_msg_read_error);
  51. break;
  52. }
  53. /* dst_fd == -1 is a fake, else... */
  54. if (dst_fd >= 0) {
  55. ssize_t wr = full_write(dst_fd, buffer, rd);
  56. if (wr < rd) {
  57. bb_perror_msg(bb_msg_write_error);
  58. break;
  59. }
  60. }
  61. total += rd;
  62. if (status < 0) { /* if we aren't copying till EOF... */
  63. size -= rd;
  64. if (!size) {
  65. /* 'size' bytes copied - all done */
  66. status = 0;
  67. break;
  68. }
  69. }
  70. }
  71. out:
  72. #if CONFIG_FEATURE_COPYBUF_KB > 4
  73. if (buffer_size != 4 * 1024)
  74. munmap(buffer, buffer_size);
  75. #endif
  76. return status ? -1 : total;
  77. }
  78. #if 0
  79. void FAST_FUNC complain_copyfd_and_die(off_t sz)
  80. {
  81. if (sz != -1)
  82. bb_error_msg_and_die("short read");
  83. /* if sz == -1, bb_copyfd_XX already complained */
  84. xfunc_die();
  85. }
  86. #endif
  87. off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size)
  88. {
  89. if (size) {
  90. return bb_full_fd_action(fd1, fd2, size);
  91. }
  92. return 0;
  93. }
  94. void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size)
  95. {
  96. off_t sz = bb_copyfd_size(fd1, fd2, size);
  97. if (sz == size)
  98. return;
  99. if (sz != -1)
  100. bb_error_msg_and_die("short read");
  101. /* if sz == -1, bb_copyfd_XX already complained */
  102. xfunc_die();
  103. }
  104. off_t FAST_FUNC bb_copyfd_eof(int fd1, int fd2)
  105. {
  106. return bb_full_fd_action(fd1, fd2, 0);
  107. }