This is a maintenance fork
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
3.4 KiB

1 year ago
1 year ago
  1. /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
  2. vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
  3. Copyright 2020 Justine Alexandra Roberts Tunney
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  14. 02110-1301 USA
  15. */
  16. #include "libc/assert.h"
  17. #include "libc/bits/bits.h"
  18. #include "libc/bits/safemacros.h"
  19. #include "libc/calls/calls.h"
  20. #include "libc/calls/internal.h"
  21. #include "libc/errno.h"
  22. #include "libc/sysv/errfuns.h"
  23. /**
  24. * Copies data between file descriptors the slow way.
  25. *
  26. * @return -1 on error/interrupt, 0 on eof, or [1..uptobytes] on success
  27. * @see copy_file_range() for file file
  28. * @see sendfile() for seekable socket
  29. * @see splice() for fd pipe
  30. */
  31. ssize_t copyfd(int infd, int64_t *inopt_out_inoffset, int to_fd,
  32. int64_t *inopt_out_outoffset, size_t uptobytes, uint32_t flags) {
  33. size_t i;
  34. int64_t offset;
  35. ssize_t got, wrote;
  36. static unsigned char buf[1024 * 64];
  37. /* unsigned char buf[1024 * 3]; */
  38. uptobytes = min(sizeof(buf), uptobytes);
  39. if (inopt_out_inoffset) {
  40. got = pread(infd, buf, uptobytes, *inopt_out_inoffset);
  41. } else {
  42. got = read(infd, buf, uptobytes);
  43. }
  44. if (got == -1) return -1;
  45. offset = inopt_out_outoffset ? *inopt_out_outoffset : -1;
  46. for (i = 0; i < got; i += wrote) {
  47. tryagain:
  48. if (inopt_out_outoffset) {
  49. wrote = pwrite(to_fd, buf, got - i, offset + i);
  50. } else {
  51. wrote = write(to_fd, buf, got - i);
  52. }
  53. if (wrote != -1) continue;
  54. if (errno == EINTR) {
  55. if (inopt_out_inoffset != NULL) {
  56. return -1;
  57. }
  58. goto tryagain;
  59. }
  60. if (errno == EWOULDBLOCK) {
  61. assert(inopt_out_inoffset != NULL); /* or caller is nuts */
  62. }
  63. return -1;
  64. }
  65. if (inopt_out_inoffset) *inopt_out_inoffset += got;
  66. if (inopt_out_outoffset) *inopt_out_outoffset += got;
  67. return got;
  68. }