1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* 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->dataRead = 0;
34
thefile->direction = 0;
37
/* We may be truncating to size here.
38
* XXX: testing an 'unsigned' as >= 0 below indicates a bug
40
newbufpos = (apr_size_t)(pos - (thefile->filePtr
41
- thefile->dataRead));
43
if (newbufpos >= 0 && newbufpos <= thefile->dataRead) {
44
thefile->bufpos = (apr_size_t)newbufpos;
47
DWORD offlo = (DWORD)pos;
48
DWORD offhi = (DWORD)(pos >> 32);
49
rc = SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);
52
/* A legal value, perhaps? MSDN implies prior SetLastError isn't
53
* needed, googling for SetLastError SetFilePointer seems
54
* to confirm this. INVALID_SET_FILE_POINTER is too recently
55
* added for us to rely on it as a constant.
57
rv = apr_get_os_error();
61
if (rv == APR_SUCCESS) {
64
thefile->bufpos = thefile->dataRead = 0;
65
thefile->filePtr = pos;
73
APR_DECLARE(apr_status_t) apr_file_seek(apr_file_t *thefile, apr_seek_where_t where, apr_off_t *offset)
76
apr_status_t rc = APR_SUCCESS;
80
if (thefile->buffered) {
83
rc = setptr(thefile, *offset);
87
rc = setptr(thefile, thefile->filePtr - thefile->dataRead
88
+ thefile->bufpos + *offset);
92
rc = apr_file_info_get(&finfo, APR_FINFO_SIZE, thefile);
93
if (rc == APR_SUCCESS)
94
rc = setptr(thefile, finfo.size + *offset);
101
*offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
104
/* A file opened with APR_XTHREAD has been opened for overlapped i/o.
105
* APR must explicitly track the file pointer in this case.
107
else if (thefile->pOverlapped || thefile->flags & APR_XTHREAD) {
110
thefile->filePtr = *offset;
114
thefile->filePtr += *offset;
118
rc = apr_file_info_get(&finfo, APR_FINFO_SIZE, thefile);
119
if (rc == APR_SUCCESS && finfo.size + *offset >= 0)
120
thefile->filePtr = finfo.size + *offset;
126
*offset = thefile->filePtr;
131
DWORD offlo = (DWORD)*offset;
132
DWORD offhi = (DWORD)(*offset >> 32);
136
howmove = FILE_BEGIN; break;
138
howmove = FILE_CURRENT; break;
140
howmove = FILE_END; break;
144
offlo = SetFilePointer(thefile->filehand, (LONG)offlo,
145
(LONG*)&offhi, howmove);
146
if (offlo == 0xFFFFFFFF)
147
rc = apr_get_os_error();
150
/* Since we can land at 0xffffffff we will measure our APR_SUCCESS */
151
if (rc == APR_SUCCESS)
152
*offset = ((apr_off_t)offhi << 32) | offlo;
158
APR_DECLARE(apr_status_t) apr_file_trunc(apr_file_t *thefile, apr_off_t offset)
161
DWORD offlo = (DWORD)offset;
162
DWORD offhi = (DWORD)(offset >> 32);
165
rc = SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);
166
if (rc == 0xFFFFFFFF)
167
if ((rv = apr_get_os_error()) != APR_SUCCESS)
170
if (!SetEndOfFile(thefile->filehand))
171
return apr_get_os_error();
173
if (thefile->buffered) {
174
return setptr(thefile, offset);