~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/file_io/win32/seek.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
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
 
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_size_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->dataRead = 0;
 
34
        thefile->direction = 0;
 
35
    }
 
36
 
 
37
    /* We may be truncating to size here. 
 
38
     * XXX: testing an 'unsigned' as >= 0 below indicates a bug
 
39
     */
 
40
    newbufpos = (apr_size_t)(pos - (thefile->filePtr 
 
41
                                  - thefile->dataRead));
 
42
 
 
43
    if (newbufpos >= 0 && newbufpos <= thefile->dataRead) {
 
44
        thefile->bufpos = (apr_size_t)newbufpos;
 
45
        rv = APR_SUCCESS;
 
46
    } else {
 
47
        DWORD offlo = (DWORD)pos;
 
48
        DWORD offhi = (DWORD)(pos >> 32);
 
49
        rc = SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);
 
50
 
 
51
        if (rc == (DWORD)-1)
 
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.
 
56
             */
 
57
            rv = apr_get_os_error();
 
58
        else
 
59
            rv = APR_SUCCESS;
 
60
 
 
61
        if (rv == APR_SUCCESS) {
 
62
            rv = APR_SUCCESS;
 
63
            thefile->eof_hit = 0;
 
64
            thefile->bufpos = thefile->dataRead = 0;
 
65
            thefile->filePtr = pos;
 
66
        }
 
67
    }
 
68
 
 
69
    return rv;
 
70
}
 
71
 
 
72
 
 
73
APR_DECLARE(apr_status_t) apr_file_seek(apr_file_t *thefile, apr_seek_where_t where, apr_off_t *offset)
 
74
{
 
75
    apr_finfo_t finfo;
 
76
    apr_status_t rc = APR_SUCCESS;
 
77
 
 
78
    thefile->eof_hit = 0;
 
79
 
 
80
    if (thefile->buffered) {
 
81
        switch (where) {
 
82
            case APR_SET:
 
83
                rc = setptr(thefile, *offset);
 
84
                break;
 
85
 
 
86
            case APR_CUR:
 
87
                rc = setptr(thefile, thefile->filePtr - thefile->dataRead 
 
88
                                      + thefile->bufpos + *offset);
 
89
                break;
 
90
 
 
91
            case APR_END:
 
92
                rc = apr_file_info_get(&finfo, APR_FINFO_SIZE, thefile);
 
93
                if (rc == APR_SUCCESS)
 
94
                    rc = setptr(thefile, finfo.size + *offset);
 
95
                break;
 
96
 
 
97
            default:
 
98
                return APR_EINVAL;
 
99
        }
 
100
 
 
101
        *offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
 
102
        return rc;
 
103
    }
 
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.
 
106
     */
 
107
    else if (thefile->pOverlapped || thefile->flags & APR_XTHREAD) {
 
108
        switch(where) {
 
109
            case APR_SET:
 
110
                thefile->filePtr = *offset;
 
111
                break;
 
112
        
 
113
            case APR_CUR:
 
114
                thefile->filePtr += *offset;
 
115
                break;
 
116
        
 
117
            case APR_END:
 
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;
 
121
                break;
 
122
 
 
123
            default:
 
124
                return APR_EINVAL;
 
125
        }
 
126
        *offset = thefile->filePtr;
 
127
        return rc;
 
128
    }
 
129
    else {
 
130
        DWORD howmove;
 
131
        DWORD offlo = (DWORD)*offset;
 
132
        DWORD offhi = (DWORD)(*offset >> 32);
 
133
 
 
134
        switch(where) {
 
135
            case APR_SET:
 
136
                howmove = FILE_BEGIN;   break;
 
137
            case APR_CUR:
 
138
                howmove = FILE_CURRENT; break;
 
139
            case APR_END:
 
140
                howmove = FILE_END;     break;
 
141
            default:
 
142
                return APR_EINVAL;
 
143
        }
 
144
        offlo = SetFilePointer(thefile->filehand, (LONG)offlo, 
 
145
                               (LONG*)&offhi, howmove);
 
146
        if (offlo == 0xFFFFFFFF)
 
147
            rc = apr_get_os_error();
 
148
        else
 
149
            rc = APR_SUCCESS;
 
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;
 
153
        return rc;
 
154
    }
 
155
}
 
156
 
 
157
 
 
158
APR_DECLARE(apr_status_t) apr_file_trunc(apr_file_t *thefile, apr_off_t offset)
 
159
{
 
160
    apr_status_t rv;
 
161
    DWORD offlo = (DWORD)offset;
 
162
    DWORD offhi = (DWORD)(offset >> 32);
 
163
    DWORD rc;
 
164
 
 
165
    rc = SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);
 
166
    if (rc == 0xFFFFFFFF)
 
167
        if ((rv = apr_get_os_error()) != APR_SUCCESS)
 
168
            return rv;
 
169
 
 
170
    if (!SetEndOfFile(thefile->filehand))
 
171
        return apr_get_os_error();
 
172
 
 
173
    if (thefile->buffered) {
 
174
        return setptr(thefile, offset);
 
175
    }
 
176
 
 
177
    return APR_SUCCESS;
 
178
}