Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
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.
 
 
 
 

161 lines
3.7 KiB

  1. /* http://delegate.uec.ac.jp:8081/club/mma/~shimiz98/misc/sendfile.html */
  2. #define _FILE_OFFSET_BITS 64
  3. #include "io_internal.h"
  4. #include "havebsdsf.h"
  5. #include "havesendfile.h"
  6. #include <errno.h>
  7. #if defined(HAVE_BSDSENDFILE)
  8. #define SENDFILE 1
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <sys/uio.h>
  12. int64 io_sendfile(int64 s,int64 fd,uint64 off,uint64 n) {
  13. off_t sbytes;
  14. int r=sendfile(fd,s,off,n,0,&sbytes,0);
  15. if (r==-1)
  16. return (errno==EAGAIN?(sbytes?sbytes:-1):-3);
  17. return n;
  18. }
  19. #elif defined(HAVE_SENDFILE)
  20. #ifdef __hpux__
  21. #define _LARGEFILE64_SOURCE
  22. #include <sys/types.h>
  23. #include <sys/uio.h>
  24. #include <sys/socket.h>
  25. int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) {
  26. long long r=sendfile64(out,in,off,bytes,0,0);
  27. if (r==-1 && errno!=EAGAIN) r=-3;
  28. return r;
  29. }
  30. #elif defined (__sun__) && defined(__svr4__)
  31. #define _LARGEFILE64_SOURCE
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include <sys/sendfile.h>
  35. int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) {
  36. off64_t o=off;
  37. long long r=sendfile64(out,in,&o,bytes);
  38. if (r==-1 && errno!=EAGAIN) r=-3;
  39. return r;
  40. }
  41. #elif defined(_AIX)
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <unistd.h>
  45. #include <errno.h>
  46. int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) {
  47. struct sf_parms p;
  48. int destfd=out;
  49. p.header_data=0;
  50. p.header_length=0;
  51. p.file_descriptor=in;
  52. p.file_offset=off;
  53. p.file_bytes=bytes;
  54. p.trailer_data=0;
  55. p.trailer_length=0;
  56. if (send_file(&destfd,&p,0)>=0)
  57. return p.bytes_sent;
  58. if (errno==EAGAIN)
  59. return -1;
  60. else
  61. return -3;
  62. }
  63. #elif defined(__linux__)
  64. #if defined(__GLIBC__)
  65. #include <sys/sendfile.h>
  66. #elif defined(__dietlibc__)
  67. #include <sys/sendfile.h>
  68. #else
  69. #include <linux/unistd.h>
  70. _syscall4(int,sendfile,int,out,int,in,long *,offset,unsigned long,count)
  71. #endif
  72. int64 io_sendfile(int64 s,int64 fd,uint64 off,uint64 n) {
  73. off_t o=off;
  74. io_entry* e=iarray_get(&io_fds,s);
  75. off_t i;
  76. uint64 done=0;
  77. /* What a spectacularly broken design for sendfile64.
  78. * The offset is 64-bit for sendfile64, but the count is not. */
  79. while (n) {
  80. off_t todo=n>0x7fffffff?0x7fffffff:n;
  81. i=sendfile(s,fd,&o,todo);
  82. if (i==todo) {
  83. done+=todo;
  84. n-=todo;
  85. if (n==0) return done;
  86. continue;
  87. } else if (i==-1) {
  88. if (e) {
  89. e->canwrite=0;
  90. e->next_write=-1;
  91. }
  92. return (errno==EAGAIN?-1:-3);
  93. } else
  94. return done+i;
  95. }
  96. return 0;
  97. }
  98. #endif
  99. #elif defined(__MINGW32__)
  100. #include <windows.h>
  101. #include <mswsock.h>
  102. int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) {
  103. io_entry* e=iarray_get(&io_fds,out);
  104. if (!e) { errno=EBADF; return -3; }
  105. if (e->sendfilequeued==1) {
  106. /* we called TransmitFile, and it returned. */
  107. e->sendfilequeued=2;
  108. errno=e->errorcode;
  109. if (e->bytes_written==-1) return -1;
  110. if (e->bytes_written!=bytes) { /* we wrote less than caller wanted to write */
  111. e->sendfilequeued=1; /* so queue next request */
  112. off+=e->bytes_written;
  113. bytes-=e->bytes_written;
  114. e->os.Offset=off;
  115. e->os.OffsetHigh=(off>>32);
  116. TransmitFile(out,(HANDLE)in,bytes>0xffff?0xffff:bytes,0,&e->os,0,TF_USE_KERNEL_APC);
  117. }
  118. return e->bytes_written;
  119. } else {
  120. e->sendfilequeued=1;
  121. e->os.Offset=off;
  122. e->os.OffsetHigh=(off>>32);
  123. /* we always write at most 64k, so timeout handling is possible */
  124. if (!TransmitFile(out,(HANDLE)in,bytes>0xffff?0xffff:bytes,0,&e->os,0,TF_USE_KERNEL_APC))
  125. return -3;
  126. }
  127. }
  128. #else
  129. #include <iob.h>
  130. #include <unistd.h>
  131. static int64 writecb(int64 s,const void* buf,uint64 n) {
  132. return write(s,buf,n);
  133. }
  134. int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) {
  135. return io_mmapwritefile(out,in,off,bytes,writecb);
  136. }
  137. #endif