seek.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "apr_arch_file_io.h"
  17. #include "apr_file_io.h"
  18. #include "apr_lib.h"
  19. #include <string.h>
  20. #include <io.h>
  21. static apr_status_t setptr(apr_file_t *thefile, unsigned long pos )
  22. {
  23. long newbufpos;
  24. ULONG rc;
  25. if (thefile->direction == 1) {
  26. /* XXX: flush here is not mutex protected */
  27. apr_status_t rv = apr_file_flush(thefile);
  28. if (rv != APR_SUCCESS) {
  29. return rv;
  30. }
  31. thefile->bufpos = thefile->direction = thefile->dataRead = 0;
  32. }
  33. newbufpos = pos - (thefile->filePtr - thefile->dataRead);
  34. if (newbufpos >= 0 && newbufpos <= thefile->dataRead) {
  35. thefile->bufpos = newbufpos;
  36. rc = 0;
  37. } else {
  38. rc = DosSetFilePtr(thefile->filedes, pos, FILE_BEGIN, &thefile->filePtr );
  39. if ( !rc )
  40. thefile->bufpos = thefile->dataRead = 0;
  41. }
  42. return APR_FROM_OS_ERROR(rc);
  43. }
  44. APR_DECLARE(apr_status_t) apr_file_seek(apr_file_t *thefile, apr_seek_where_t where, apr_off_t *offset)
  45. {
  46. if (!thefile->isopen) {
  47. return APR_EBADF;
  48. }
  49. thefile->eof_hit = 0;
  50. if (thefile->buffered) {
  51. int rc = EINVAL;
  52. apr_finfo_t finfo;
  53. switch (where) {
  54. case APR_SET:
  55. rc = setptr(thefile, *offset);
  56. break;
  57. case APR_CUR:
  58. rc = setptr(thefile, thefile->filePtr - thefile->dataRead + thefile->bufpos + *offset);
  59. break;
  60. case APR_END:
  61. rc = apr_file_info_get(&finfo, APR_FINFO_NORM, thefile);
  62. if (rc == APR_SUCCESS)
  63. rc = setptr(thefile, finfo.size + *offset);
  64. break;
  65. }
  66. *offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
  67. return rc;
  68. } else {
  69. switch (where) {
  70. case APR_SET:
  71. where = FILE_BEGIN;
  72. break;
  73. case APR_CUR:
  74. where = FILE_CURRENT;
  75. break;
  76. case APR_END:
  77. where = FILE_END;
  78. break;
  79. }
  80. return APR_FROM_OS_ERROR(DosSetFilePtr(thefile->filedes, *offset, where, (ULONG *)offset));
  81. }
  82. }
  83. APR_DECLARE(apr_status_t) apr_file_trunc(apr_file_t *fp, apr_off_t offset)
  84. {
  85. int rc = DosSetFileSize(fp->filedes, offset);
  86. if (rc != 0) {
  87. return APR_FROM_OS_ERROR(rc);
  88. }
  89. if (fp->buffered) {
  90. return setptr(fp, offset);
  91. }
  92. return APR_SUCCESS;
  93. }