logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git

sendfile_linux.c (748B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _DEFAULT_SOURCE
  5. #include "./fs.h"
  6. // for sendfile
  7. #include <sys/sendfile.h>
  8. // generic
  9. #include <errno.h>
  10. #include <stddef.h> // NULL
  11. #include <sys/types.h> // ssize_t, off_t
  12. ssize_t
  13. auto_fd_copy(int fd_in, int fd_out, size_t len)
  14. {
  15. off_t *off = NULL;
  16. while(1)
  17. {
  18. ssize_t ret = sendfile(fd_out, fd_in, off, len);
  19. if(ret < 0)
  20. {
  21. switch(errno)
  22. {
  23. case EINVAL:
  24. errno = 0;
  25. return manual_copy(fd_in, fd_out, len, 0);
  26. case EAGAIN:
  27. errno = 0;
  28. continue;
  29. default:
  30. return ret;
  31. }
  32. }
  33. return ret;
  34. }
  35. return -1;
  36. }