bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / file_io / win32 / seek.c
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 "win32/apr_arch_file_io.h"
18 #include "apr_file_io.h"
19 #include <errno.h>
20 #include <string.h>
21
22 static apr_status_t setptr(apr_file_t *thefile, apr_off_t pos )
23 {
24     apr_off_t newbufpos;
25     apr_status_t rv;
26     DWORD rc;
27
28     if (thefile->direction == 1) {
29         /* XXX: flush here is not mutex protected */
30         rv = apr_file_flush(thefile);
31         if (rv != APR_SUCCESS)
32             return rv;
33         thefile->bufpos = thefile->direction = thefile->dataRead = 0;
34     }
35
36     newbufpos = pos - (thefile->filePtr - thefile->dataRead);
37
38     if (newbufpos >= 0 && newbufpos <= thefile->dataRead) {
39         thefile->bufpos = (apr_size_t)newbufpos;
40         rv = APR_SUCCESS;
41     } else {
42         DWORD offlo = (DWORD)pos;
43         DWORD offhi = (DWORD)(pos >> 32);
44         rc = SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);
45
46         if (rc == (DWORD)-1)
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.
51              */
52             rv = apr_get_os_error();
53         else
54             rv = APR_SUCCESS;
55
56         if (rv == APR_SUCCESS) {
57             thefile->eof_hit = thefile->bufpos = thefile->dataRead = 0;
58             thefile->filePtr = pos;
59         }
60     }
61
62     return rv;
63 }
64
65
66 APR_DECLARE(apr_status_t) apr_file_seek(apr_file_t *thefile, apr_seek_where_t where, apr_off_t *offset)
67 {
68     apr_finfo_t finfo;
69     apr_status_t rc = APR_SUCCESS;
70
71     thefile->eof_hit = 0;
72
73     if (thefile->buffered) {
74         switch (where) {
75             case APR_SET:
76                 rc = setptr(thefile, *offset);
77                 break;
78
79             case APR_CUR:
80                 rc = setptr(thefile, thefile->filePtr - thefile->dataRead 
81                                       + thefile->bufpos + *offset);
82                 break;
83
84             case APR_END:
85                 rc = apr_file_info_get(&finfo, APR_FINFO_SIZE, thefile);
86                 if (rc == APR_SUCCESS)
87                     rc = setptr(thefile, finfo.size + *offset);
88                 break;
89
90             default:
91                 return APR_EINVAL;
92         }
93
94         *offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
95         return rc;
96     } 
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.
99      */
100     else if (thefile->pOverlapped || thefile->flags & APR_XTHREAD) {
101         switch(where) {
102             case APR_SET:
103                 thefile->filePtr = *offset;
104                 break;
105         
106             case APR_CUR:
107                 thefile->filePtr += *offset;
108                 break;
109         
110             case APR_END:
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;
114                 break;
115
116             default:
117                 return APR_EINVAL;
118         }
119         *offset = thefile->filePtr;
120         return rc;
121     }
122     else {
123         DWORD howmove;
124         DWORD offlo = (DWORD)*offset;
125         DWORD offhi = (DWORD)(*offset >> 32);
126
127         switch(where) {
128             case APR_SET:
129                 howmove = FILE_BEGIN;   break;
130             case APR_CUR:
131                 howmove = FILE_CURRENT; break;
132             case APR_END:
133                 howmove = FILE_END;     break;
134             default:
135                 return APR_EINVAL;
136         }
137         offlo = SetFilePointer(thefile->filehand, (LONG)offlo, 
138                                (LONG*)&offhi, howmove);
139         if (offlo == 0xFFFFFFFF)
140             rc = apr_get_os_error();
141         else
142             rc = APR_SUCCESS;
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;
146         return rc;
147     }
148 }
149
150
151 APR_DECLARE(apr_status_t) apr_file_trunc(apr_file_t *thefile, apr_off_t offset)
152 {
153     apr_status_t rv;
154     DWORD offlo = (DWORD)offset;
155     DWORD offhi = (DWORD)(offset >> 32);
156     DWORD rc;
157
158     rc = SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);
159     if (rc == 0xFFFFFFFF)
160         if ((rv = apr_get_os_error()) != APR_SUCCESS)
161             return rv;
162
163     if (!SetEndOfFile(thefile->filehand))
164         return apr_get_os_error();
165
166     if (thefile->buffered) {
167         return setptr(thefile, offset);
168     }
169
170     return APR_SUCCESS;
171 }