seek.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. static fspr_status_t setptr(fspr_file_t *thefile, fspr_off_t pos )
  18. {
  19. fspr_off_t newbufpos;
  20. fspr_status_t rv;
  21. if (thefile->direction == 1) {
  22. rv = fspr_file_flush(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(fspr_status_t) fspr_file_seek(fspr_file_t *thefile, fspr_seek_where_t where, fspr_off_t *offset)
  46. {
  47. fspr_off_t rv;
  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_SIZE, 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. }
  68. else {
  69. rv = lseek(thefile->filedes, *offset, where);
  70. if (rv == -1) {
  71. *offset = -1;
  72. return errno;
  73. }
  74. else {
  75. *offset = rv;
  76. return APR_SUCCESS;
  77. }
  78. }
  79. }
  80. fspr_status_t fspr_file_trunc(fspr_file_t *fp, fspr_off_t offset)
  81. {
  82. if (ftruncate(fp->filedes, offset) == -1) {
  83. return errno;
  84. }
  85. return setptr(fp, offset);
  86. }