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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_arch_file_io.h"
19 static apr_status_t setptr(apr_file_t *thefile, apr_off_t pos )
24 if (thefile->direction == 1) {
25 rv = apr_file_flush_locked(thefile);
29 thefile->bufpos = thefile->direction = thefile->dataRead = 0;
32 newbufpos = pos - (thefile->filePtr - thefile->dataRead);
33 if (newbufpos >= 0 && newbufpos <= thefile->dataRead) {
34 thefile->bufpos = newbufpos;
38 if (lseek(thefile->filedes, pos, SEEK_SET) != -1) {
39 thefile->bufpos = thefile->dataRead = 0;
40 thefile->filePtr = pos;
52 APR_DECLARE(apr_status_t) apr_file_seek(apr_file_t *thefile, apr_seek_where_t where, apr_off_t *offset)
58 if (thefile->buffered) {
66 rc = setptr(thefile, *offset);
70 rc = setptr(thefile, thefile->filePtr - thefile->dataRead + thefile->bufpos + *offset);
74 rc = apr_file_info_get_locked(&finfo, APR_FINFO_SIZE, thefile);
75 if (rc == APR_SUCCESS)
76 rc = setptr(thefile, finfo.size + *offset);
80 *offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
87 rv = lseek(thefile->filedes, *offset, where);
99 apr_status_t apr_file_trunc(apr_file_t *fp, apr_off_t offset)
104 if (fp->direction == 1 && fp->bufpos != 0) {
105 apr_off_t len = fp->filePtr + fp->bufpos;
107 /* New file end fall below our write buffer limit.
108 * Figure out if and what needs to be flushed.
110 apr_off_t off = len - offset;
111 if (off >= 0 && off <= fp->bufpos)
112 fp->bufpos = fp->bufpos - (size_t)off;
116 rc = apr_file_flush_locked(fp);
117 /* Reset buffer positions for write mode */
118 fp->bufpos = fp->direction = fp->dataRead = 0;
125 if (ftruncate(fp->filedes, offset) == -1) {
128 return apr_file_seek(fp, APR_SET, &offset);