Apache HTTPD
seek.c
Go to the documentation of this file.
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
17#include "apr_arch_file_io.h"
18
20{
22 apr_status_t rv;
23
24 if (thefile->direction == 1) {
26 if (rv) {
27 return rv;
28 }
30 }
31
33 if (newbufpos >= 0 && newbufpos <= thefile->dataRead) {
35 rv = APR_SUCCESS;
36 }
37 else {
38 if (lseek(thefile->filedes, pos, SEEK_SET) != -1) {
40 thefile->filePtr = pos;
41 rv = APR_SUCCESS;
42 }
43 else {
44 rv = errno;
45 }
46 }
47
48 return rv;
49}
50
51
53{
54 apr_off_t rv;
55
56 thefile->eof_hit = 0;
57
58 if (thefile->buffered) {
59 int rc = EINVAL;
60 apr_finfo_t finfo;
61
63
64 switch (where) {
65 case APR_SET:
67 break;
68
69 case APR_CUR:
71 break;
72
73 case APR_END:
75 if (rc == APR_SUCCESS)
76 rc = setptr(thefile, finfo.size + *offset);
77 break;
78 }
79
81
83
84 return rc;
85 }
86 else {
88 if (rv == -1) {
89 *offset = -1;
90 return errno;
91 }
92 else {
93 *offset = rv;
94 return APR_SUCCESS;
95 }
96 }
97}
98
100{
101 if (fp->buffered) {
102 int rc = 0;
103 file_lock(fp);
104 if (fp->direction == 1 && fp->bufpos != 0) {
105 apr_off_t len = fp->filePtr + fp->bufpos;
106 if (offset < len) {
107 /* New file end fall below our write buffer limit.
108 * Figure out if and what needs to be flushed.
109 */
111 if (off >= 0 && off <= fp->bufpos)
112 fp->bufpos = fp->bufpos - (size_t)off;
113 else
114 fp->bufpos = 0;
115 }
117 /* Reset buffer positions for write mode */
118 fp->bufpos = fp->direction = fp->dataRead = 0;
119 }
120 else if (fp->direction == 0) {
121 /* Discard the read buffer, as we are about to reposition
122 * ourselves to the end of file.
123 */
124 fp->bufpos = 0;
125 fp->dataRead = 0;
126 }
127 file_unlock(fp);
128 if (rc) {
129 return rc;
130 }
131 }
132 if (ftruncate(fp->filedes, offset) == -1) {
133 return errno;
134 }
135 return apr_file_seek(fp, APR_SET, &offset);
136}
const char apr_size_t len
Definition ap_regex.h:187
apr_redis_t * rc
Definition apr_redis.h:173
const void apr_status_t(*) apr_status_t(* APR_DECLARE)(void) apr_pool_pre_cleanup_register(apr_pool_t *p
Definition apr_pools.h:646
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
apr_file_t * thefile
apr_seek_where_t where
apr_seek_where_t apr_off_t * offset
int apr_seek_where_t
#define APR_SET
#define APR_END
#define APR_CUR
#define APR_FINFO_SIZE
#define file_unlock(f)
#define file_lock(f)
apr_status_t apr_file_info_get_locked(apr_finfo_t *finfo, apr_int32_t wanted, apr_file_t *thefile)
Definition filestat.c:85
static apr_status_t setptr(apr_file_t *thefile, unsigned long pos)
Definition seek.c:24
apr_size_t bufpos
apr_off_t filePtr
apr_off_t dataRead
apr_off_t size
apr_status_t apr_file_flush_locked(apr_file_t *thefile)
Definition readwrite.c:313
apr_status_t apr_file_trunc(apr_file_t *fp, apr_off_t offset)
Definition seek.c:99