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.
 
 
 
 

117 lines
2.4 KiB

  1. #include <io_internal.h>
  2. #include <iob.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #ifdef __MINGW32__
  6. #include <windows.h>
  7. #else
  8. #include <sys/mman.h>
  9. #endif
  10. #include <errno.h>
  11. #define BUFSIZE 16384
  12. int64 io_mmapwritefile(int64 out,int64 in,uint64 off,uint64 bytes,io_write_callback writecb) {
  13. char buf[BUFSIZE];
  14. int n,m;
  15. uint64 sent=0;
  16. io_entry* e=iarray_get(&io_fds,out);
  17. if (e) {
  18. const char* c;
  19. unsigned long left;
  20. #ifdef __MINGW32__
  21. if (!e->mh) e->mh=CreateFileMapping(out,0,PAGE_READONLY,0,0,NULL);
  22. if (!e->mh) goto readwrite;
  23. #endif
  24. do {
  25. if (e->mmapped) {
  26. /* did we already map the right chunk? */
  27. if (off>=e->mapofs && off<e->mapofs+e->maplen)
  28. goto mapok; /* ok; mmapped the right chunk*/
  29. #ifdef __MINGW32__
  30. UnmapViewOfFile(e->mmapped);
  31. #else
  32. munmap(e->mmapped,e->maplen);
  33. #endif
  34. }
  35. e->mapofs=off&0xffffffffffff0000ull;
  36. if (e->mapofs+0x10000>off+bytes)
  37. e->maplen=off+bytes-e->mapofs;
  38. else
  39. e->maplen=0x10000;
  40. #ifdef __MINGW32__
  41. if ((e->mmapped=MapViewOfFile(e->mh,FILE_MAP_READ,(DWORD)(e->mapofs>>32),
  42. (DWORD)e->mapofs,e->maplen))==0)
  43. #else
  44. if ((e->mmapped=mmap(0,e->maplen,PROT_READ,MAP_SHARED,in,e->mapofs))==MAP_FAILED)
  45. #endif
  46. {
  47. e->mmapped=0;
  48. goto readwrite;
  49. }
  50. mapok:
  51. c=(const char*)(e->mmapped)+(off&0xffff);
  52. left=e->maplen-(off&0xffff);
  53. if (left>bytes) left=bytes;
  54. while (left>0) {
  55. m=writecb(out,c,left);
  56. if (m<0) {
  57. e->canwrite=0;
  58. e->next_write=-1;
  59. if (errno!=EAGAIN) {
  60. #ifdef __MINGW32__
  61. UnmapViewOfFile(e->mmapped);
  62. #else
  63. munmap(e->mmapped,e->maplen);
  64. #endif
  65. e->mmapped=0;
  66. return -3;
  67. }
  68. return sent?(int64)sent:-1;
  69. }
  70. if (m==0) return sent;
  71. sent+=m;
  72. left-=m;
  73. bytes-=m;
  74. off+=m;
  75. c+=m;
  76. }
  77. } while (bytes);
  78. if (e->mmapped) {
  79. #ifdef __MINGW32__
  80. UnmapViewOfFile(e->mmapped);
  81. #else
  82. munmap(e->mmapped,e->maplen);
  83. #endif
  84. e->mmapped=0;
  85. }
  86. return sent;
  87. }
  88. readwrite:
  89. if (lseek(in,off,SEEK_SET) != (off_t)off)
  90. return -1;
  91. while (bytes>0) {
  92. char* tmp=buf;
  93. if ((n=read(in,tmp,(bytes<BUFSIZE)?bytes:BUFSIZE))<=0)
  94. return (sent?(int64)sent:-1);
  95. while (n>0) {
  96. if ((m=writecb(out,tmp,n))<0) {
  97. if (m==-1) {
  98. if (e) {
  99. e->canwrite=0;
  100. e->next_write=-1;
  101. }
  102. return errno==EAGAIN?(sent?(int64)sent:-1):-3;
  103. }
  104. goto abort;
  105. }
  106. sent+=m;
  107. n-=m;
  108. bytes-=m;
  109. tmp+=m;
  110. }
  111. }
  112. abort:
  113. return sent;
  114. }