sfile.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2003 Gunnar Ritter
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute
  10. * it freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. *
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. *
  20. * 3. This notice may not be removed or altered from any source distribution.
  21. */
  22. /* Sccsid @(#)sfile.c 1.9 (gritter) 6/7/04 */
  23. #ifdef __linux__
  24. #undef _FILE_OFFSET_BITS
  25. #include <sys/types.h>
  26. #include <sys/sendfile.h>
  27. #include <sys/stat.h>
  28. #include <unistd.h>
  29. #include <limits.h>
  30. #include <errno.h>
  31. #include "sfile.h"
  32. long long
  33. sfile(int dfd, int sfd, mode_t mode, long long count)
  34. {
  35. static int enosys, einval, success;
  36. off_t offset;
  37. ssize_t sent, total;
  38. extern void writerr(void *, int, int);
  39. /*
  40. * A process is not interruptible while executing a sendfile()
  41. * system call. So it is not advisable to to send an entire
  42. * file with one call; it is sent in parts so signals can
  43. * be delivered in between.
  44. */
  45. const ssize_t chunk = 196608;
  46. /*
  47. * If a previous call returned ENOSYS, the operating system does
  48. * not support sendfile() at all and it makes no sense to try it
  49. * again.
  50. *
  51. * If a previous call returned EINVAL and there was no successful
  52. * call yet, it is very likely that this is a permanent error
  53. * condition (on Linux 2.6.0-test4, sendfile() may be used for
  54. * socket targets only; older versions don't support tmpfs as
  55. * source file system etc.).
  56. */
  57. if (enosys || !success && einval ||
  58. (mode&S_IFMT) != S_IFREG || count > SSIZE_MAX)
  59. return 0;
  60. offset = lseek(sfd, 0, SEEK_CUR);
  61. sent = 0, total = 0;
  62. while (count > 0 && (sent = sendfile(dfd, sfd, &offset,
  63. count > chunk ? chunk : count)) > 0) {
  64. count -= sent, total += sent;
  65. }
  66. if (total && lseek(sfd, offset, SEEK_SET) == (off_t)-1)
  67. return -1;
  68. if (count == 0 || sent == 0) {
  69. success = 1;
  70. return total;
  71. }
  72. switch (errno) {
  73. case ENOSYS:
  74. enosys = 1;
  75. return 0;
  76. case EINVAL:
  77. einval = 1;
  78. return 0;
  79. case ENOMEM:
  80. return 0;
  81. default:
  82. writerr(NULL, count > chunk ? chunk : count, 0);
  83. return -1;
  84. }
  85. }
  86. #else /* !__linux__ */
  87. #include <sys/types.h>
  88. /*ARGSUSED*/
  89. long long
  90. sfile(int dfd, int sfd, mode_t mode, long long count)
  91. {
  92. return 0;
  93. }
  94. #endif /* __linux__ */