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 "win32/apr_arch_file_io.h"
18 #include "apr_file_io.h"
22 static apr_status_t setptr(apr_file_t *thefile, apr_off_t pos )
28 if (thefile->direction == 1) {
29 /* XXX: flush here is not mutex protected */
30 rv = apr_file_flush(thefile);
31 if (rv != APR_SUCCESS)
33 thefile->bufpos = thefile->direction = thefile->dataRead = 0;
36 newbufpos = pos - (thefile->filePtr - thefile->dataRead);
38 if (newbufpos >= 0 && newbufpos <= thefile->dataRead) {
39 thefile->bufpos = (apr_size_t)newbufpos;
42 DWORD offlo = (DWORD)pos;
43 DWORD offhi = (DWORD)(pos >> 32);
44 rc = SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);
47 /* A legal value, perhaps? MSDN implies prior SetLastError isn't
48 * needed, googling for SetLastError SetFilePointer seems
49 * to confirm this. INVALID_SET_FILE_POINTER is too recently
50 * added for us to rely on it as a constant.
52 rv = apr_get_os_error();
56 if (rv == APR_SUCCESS) {
57 thefile->eof_hit = thefile->bufpos = thefile->dataRead = 0;
58 thefile->filePtr = pos;
66 APR_DECLARE(apr_status_t) apr_file_seek(apr_file_t *thefile, apr_seek_where_t where, apr_off_t *offset)
69 apr_status_t rc = APR_SUCCESS;
73 if (thefile->buffered) {
76 rc = setptr(thefile, *offset);
80 rc = setptr(thefile, thefile->filePtr - thefile->dataRead
81 + thefile->bufpos + *offset);
85 rc = apr_file_info_get(&finfo, APR_FINFO_SIZE, thefile);
86 if (rc == APR_SUCCESS)
87 rc = setptr(thefile, finfo.size + *offset);
94 *offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
97 /* A file opened with APR_XTHREAD has been opened for overlapped i/o.
98 * APR must explicitly track the file pointer in this case.
100 else if (thefile->pOverlapped || thefile->flags & APR_XTHREAD) {
103 thefile->filePtr = *offset;
107 thefile->filePtr += *offset;
111 rc = apr_file_info_get(&finfo, APR_FINFO_SIZE, thefile);
112 if (rc == APR_SUCCESS && finfo.size + *offset >= 0)
113 thefile->filePtr = finfo.size + *offset;
119 *offset = thefile->filePtr;
124 DWORD offlo = (DWORD)*offset;
125 DWORD offhi = (DWORD)(*offset >> 32);
129 howmove = FILE_BEGIN; break;
131 howmove = FILE_CURRENT; break;
133 howmove = FILE_END; break;
137 offlo = SetFilePointer(thefile->filehand, (LONG)offlo,
138 (LONG*)&offhi, howmove);
139 if (offlo == 0xFFFFFFFF)
140 rc = apr_get_os_error();
143 /* Since we can land at 0xffffffff we will measure our APR_SUCCESS */
144 if (rc == APR_SUCCESS)
145 *offset = ((apr_off_t)offhi << 32) | offlo;
151 APR_DECLARE(apr_status_t) apr_file_trunc(apr_file_t *thefile, apr_off_t offset)
154 DWORD offlo = (DWORD)offset;
155 DWORD offhi = (DWORD)(offset >> 32);
158 rc = SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);
159 if (rc == 0xFFFFFFFF)
160 if ((rv = apr_get_os_error()) != APR_SUCCESS)
163 if (!SetEndOfFile(thefile->filehand))
164 return apr_get_os_error();
166 if (thefile->buffered) {
167 return setptr(thefile, offset);