seek.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. static apr_status_t setptr(apr_file_t *thefile, apr_off_t pos )
  18. {
  19. apr_off_t newbufpos;
  20. apr_status_t rv;
  21. if (thefile->direction == 1) {
  22. rv = apr_file_flush_locked(thefile);
  23. if (rv) {
  24. return rv;
  25. }
  26. thefile->bufpos = thefile->direction = thefile->dataRead = 0;
  27. }
  28. newbufpos = pos - (thefile->filePtr - thefile->dataRead);
  29. if (newbufpos >= 0 && newbufpos <= thefile->dataRead) {
  30. thefile->bufpos = newbufpos;
  31. rv = APR_SUCCESS;
  32. }
  33. else {
  34. if (lseek(thefile->filedes, pos, SEEK_SET) != -1) {
  35. thefile->bufpos = thefile->dataRead = 0;
  36. thefile->filePtr = pos;
  37. rv = APR_SUCCESS;
  38. }
  39. else {
  40. rv = errno;
  41. }
  42. }
  43. return rv;
  44. }
  45. APR_DECLARE(apr_status_t) apr_file_seek(apr_file_t *thefile, apr_seek_where_t where, apr_off_t *offset)
  46. {
  47. apr_off_t rv;
  48. thefile->eof_hit = 0;
  49. if (thefile->buffered) {
  50. int rc = EINVAL;
  51. apr_finfo_t finfo;
  52. file_lock(thefile);
  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_locked(&finfo, APR_FINFO_SIZE, 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. file_unlock(thefile);
  68. return rc;
  69. }
  70. else {
  71. rv = lseek(thefile->filedes, *offset, where);
  72. if (rv == -1) {
  73. *offset = -1;
  74. return errno;
  75. }
  76. else {
  77. *offset = rv;
  78. return APR_SUCCESS;
  79. }
  80. }
  81. }
  82. apr_status_t apr_file_trunc(apr_file_t *fp, apr_off_t offset)
  83. {
  84. if (fp->buffered) {
  85. int rc = 0;
  86. file_lock(fp);
  87. if (fp->direction == 1 && fp->bufpos != 0) {
  88. apr_off_t len = fp->filePtr + fp->bufpos;
  89. if (offset < len) {
  90. /* New file end fall below our write buffer limit.
  91. * Figure out if and what needs to be flushed.
  92. */
  93. apr_off_t off = len - offset;
  94. if (off >= 0 && off <= fp->bufpos)
  95. fp->bufpos = fp->bufpos - (size_t)off;
  96. else
  97. fp->bufpos = 0;
  98. }
  99. rc = apr_file_flush_locked(fp);
  100. /* Reset buffer positions for write mode */
  101. fp->bufpos = fp->direction = fp->dataRead = 0;
  102. }
  103. if (rc) {
  104. return rc;
  105. }
  106. file_unlock(fp);
  107. }
  108. if (ftruncate(fp->filedes, offset) == -1) {
  109. return errno;
  110. }
  111. return apr_file_seek(fp, APR_SET, &offset);
  112. }