seek.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "win32/fspr_arch_file_io.h"
  17. #include "fspr_file_io.h"
  18. #include <errno.h>
  19. #include <string.h>
  20. static fspr_status_t setptr(fspr_file_t *thefile, fspr_off_t pos )
  21. {
  22. fspr_size_t newbufpos;
  23. fspr_status_t rv;
  24. DWORD rc;
  25. if (thefile->direction == 1) {
  26. /* XXX: flush here is not mutex protected */
  27. rv = fspr_file_flush(thefile);
  28. if (rv != APR_SUCCESS)
  29. return rv;
  30. thefile->bufpos = thefile->dataRead = 0;
  31. thefile->direction = 0;
  32. }
  33. /* We may be truncating to size here.
  34. * XXX: testing an 'unsigned' as >= 0 below indicates a bug
  35. */
  36. newbufpos = (fspr_size_t)(pos - (thefile->filePtr
  37. - thefile->dataRead));
  38. if (newbufpos >= 0 && newbufpos <= thefile->dataRead) {
  39. thefile->bufpos = (fspr_size_t)newbufpos;
  40. rv = APR_SUCCESS;
  41. } else {
  42. DWORD offlo = (DWORD)pos;
  43. DWORD offhi = (DWORD)(pos >> 32);
  44. rc = SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);
  45. if (rc == (DWORD)-1)
  46. /* A legal value, perhaps? MSDN implies prior SetLastError isn't
  47. * needed, googling for SetLastError SetFilePointer seems
  48. * to confirm this. INVALID_SET_FILE_POINTER is too recently
  49. * added for us to rely on it as a constant.
  50. */
  51. rv = fspr_get_os_error();
  52. else
  53. rv = APR_SUCCESS;
  54. if (rv == APR_SUCCESS) {
  55. rv = APR_SUCCESS;
  56. thefile->eof_hit = 0;
  57. thefile->bufpos = thefile->dataRead = 0;
  58. thefile->filePtr = pos;
  59. }
  60. }
  61. return rv;
  62. }
  63. APR_DECLARE(fspr_status_t) fspr_file_seek(fspr_file_t *thefile, fspr_seek_where_t where, fspr_off_t *offset)
  64. {
  65. fspr_finfo_t finfo;
  66. fspr_status_t rc = APR_SUCCESS;
  67. thefile->eof_hit = 0;
  68. if (thefile->buffered) {
  69. switch (where) {
  70. case APR_SET:
  71. rc = setptr(thefile, *offset);
  72. break;
  73. case APR_CUR:
  74. rc = setptr(thefile, thefile->filePtr - thefile->dataRead
  75. + thefile->bufpos + *offset);
  76. break;
  77. case APR_END:
  78. rc = fspr_file_info_get(&finfo, APR_FINFO_SIZE, thefile);
  79. if (rc == APR_SUCCESS)
  80. rc = setptr(thefile, finfo.size + *offset);
  81. break;
  82. default:
  83. return APR_EINVAL;
  84. }
  85. *offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
  86. return rc;
  87. }
  88. /* A file opened with APR_XTHREAD has been opened for overlapped i/o.
  89. * APR must explicitly track the file pointer in this case.
  90. */
  91. else if (thefile->pOverlapped || thefile->flags & APR_XTHREAD) {
  92. switch(where) {
  93. case APR_SET:
  94. thefile->filePtr = *offset;
  95. break;
  96. case APR_CUR:
  97. thefile->filePtr += *offset;
  98. break;
  99. case APR_END:
  100. rc = fspr_file_info_get(&finfo, APR_FINFO_SIZE, thefile);
  101. if (rc == APR_SUCCESS && finfo.size + *offset >= 0)
  102. thefile->filePtr = finfo.size + *offset;
  103. break;
  104. default:
  105. return APR_EINVAL;
  106. }
  107. *offset = thefile->filePtr;
  108. return rc;
  109. }
  110. else {
  111. DWORD howmove;
  112. DWORD offlo = (DWORD)*offset;
  113. DWORD offhi = (DWORD)(*offset >> 32);
  114. switch(where) {
  115. case APR_SET:
  116. howmove = FILE_BEGIN; break;
  117. case APR_CUR:
  118. howmove = FILE_CURRENT; break;
  119. case APR_END:
  120. howmove = FILE_END; break;
  121. default:
  122. return APR_EINVAL;
  123. }
  124. offlo = SetFilePointer(thefile->filehand, (LONG)offlo,
  125. (LONG*)&offhi, howmove);
  126. if (offlo == 0xFFFFFFFF)
  127. rc = fspr_get_os_error();
  128. else
  129. rc = APR_SUCCESS;
  130. /* Since we can land at 0xffffffff we will measure our APR_SUCCESS */
  131. if (rc == APR_SUCCESS)
  132. *offset = ((fspr_off_t)offhi << 32) | offlo;
  133. return rc;
  134. }
  135. }
  136. APR_DECLARE(fspr_status_t) fspr_file_trunc(fspr_file_t *thefile, fspr_off_t offset)
  137. {
  138. fspr_status_t rv;
  139. DWORD offlo = (DWORD)offset;
  140. DWORD offhi = (DWORD)(offset >> 32);
  141. DWORD rc;
  142. rc = SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);
  143. if (rc == 0xFFFFFFFF)
  144. if ((rv = fspr_get_os_error()) != APR_SUCCESS)
  145. return rv;
  146. if (!SetEndOfFile(thefile->filehand))
  147. return fspr_get_os_error();
  148. if (thefile->buffered) {
  149. return setptr(thefile, offset);
  150. }
  151. return APR_SUCCESS;
  152. }