seek.c 3.1 KB

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