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

« back to all changes in this revision

Viewing changes to srclib/apr/file_io/os2/open.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 "apr_arch_file_io.h"
 
18
#include "apr_file_io.h"
 
19
#include "apr_lib.h"
 
20
#include "apr_portable.h"
 
21
#include "apr_strings.h"
 
22
#include "apr_arch_inherit.h"
 
23
#include <string.h>
 
24
 
 
25
apr_status_t apr_file_cleanup(void *thefile)
 
26
{
 
27
    apr_file_t *file = thefile;
 
28
    return apr_file_close(file);
 
29
}
 
30
 
 
31
 
 
32
 
 
33
APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new, const char *fname, apr_int32_t flag,  apr_fileperms_t perm, apr_pool_t *pool)
 
34
{
 
35
    int oflags = 0;
 
36
    int mflags = OPEN_FLAGS_FAIL_ON_ERROR|OPEN_SHARE_DENYNONE;
 
37
    int rv;
 
38
    ULONG action;
 
39
    apr_file_t *dafile = (apr_file_t *)apr_palloc(pool, sizeof(apr_file_t));
 
40
 
 
41
    dafile->pool = pool;
 
42
    dafile->isopen = FALSE;
 
43
    dafile->eof_hit = FALSE;
 
44
    dafile->buffer = NULL;
 
45
    dafile->flags = flag;
 
46
    dafile->blocking = BLK_ON;
 
47
    
 
48
    if ((flag & APR_READ) && (flag & APR_WRITE)) {
 
49
        mflags |= OPEN_ACCESS_READWRITE;
 
50
    } else if (flag & APR_READ) {
 
51
        mflags |= OPEN_ACCESS_READONLY;
 
52
    } else if (flag & APR_WRITE) {
 
53
        mflags |= OPEN_ACCESS_WRITEONLY;
 
54
    } else {
 
55
        dafile->filedes = -1;
 
56
        return APR_EACCES;
 
57
    }
 
58
 
 
59
    dafile->buffered = (flag & APR_BUFFERED) > 0;
 
60
 
 
61
    if (dafile->buffered) {
 
62
        dafile->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
 
63
        rv = apr_thread_mutex_create(&dafile->mutex, 0, pool);
 
64
 
 
65
        if (rv)
 
66
            return rv;
 
67
    }
 
68
 
 
69
    if (flag & APR_CREATE) {
 
70
        oflags |= OPEN_ACTION_CREATE_IF_NEW;
 
71
 
 
72
        if (!(flag & APR_EXCL) && !(flag & APR_TRUNCATE)) {
 
73
            oflags |= OPEN_ACTION_OPEN_IF_EXISTS;
 
74
        }
 
75
    }
 
76
    
 
77
    if ((flag & APR_EXCL) && !(flag & APR_CREATE))
 
78
        return APR_EACCES;
 
79
 
 
80
    if (flag & APR_TRUNCATE) {
 
81
        oflags |= OPEN_ACTION_REPLACE_IF_EXISTS;
 
82
    } else if ((oflags & 0xFF) == 0) {
 
83
        oflags |= OPEN_ACTION_OPEN_IF_EXISTS;
 
84
    }
 
85
    
 
86
    rv = DosOpen(fname, &(dafile->filedes), &action, 0, 0, oflags, mflags, NULL);
 
87
    
 
88
    if (rv == 0 && (flag & APR_APPEND)) {
 
89
        ULONG newptr;
 
90
        rv = DosSetFilePtr(dafile->filedes, 0, FILE_END, &newptr );
 
91
        
 
92
        if (rv)
 
93
            DosClose(dafile->filedes);
 
94
    }
 
95
    
 
96
    if (rv != 0)
 
97
        return APR_FROM_OS_ERROR(rv);
 
98
    
 
99
    dafile->isopen = TRUE;
 
100
    dafile->fname = apr_pstrdup(pool, fname);
 
101
    dafile->filePtr = 0;
 
102
    dafile->bufpos = 0;
 
103
    dafile->dataRead = 0;
 
104
    dafile->direction = 0;
 
105
    dafile->pipe = FALSE;
 
106
 
 
107
    if (!(flag & APR_FILE_NOCLEANUP)) { 
 
108
        apr_pool_cleanup_register(dafile->pool, dafile, apr_file_cleanup, apr_file_cleanup);
 
109
    }
 
110
 
 
111
    *new = dafile;
 
112
    return APR_SUCCESS;
 
113
}
 
114
 
 
115
 
 
116
 
 
117
APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
 
118
{
 
119
    ULONG rc;
 
120
    apr_status_t status;
 
121
    
 
122
    if (file && file->isopen) {
 
123
        apr_file_flush(file);
 
124
        rc = DosClose(file->filedes);
 
125
    
 
126
        if (rc == 0) {
 
127
            file->isopen = FALSE;
 
128
            status = APR_SUCCESS;
 
129
 
 
130
            if (file->flags & APR_DELONCLOSE) {
 
131
                status = APR_FROM_OS_ERROR(DosDelete(file->fname));
 
132
            }
 
133
        } else {
 
134
            return APR_FROM_OS_ERROR(rc);
 
135
        }
 
136
    }
 
137
 
 
138
    if (file->buffered)
 
139
        apr_thread_mutex_destroy(file->mutex);
 
140
 
 
141
    return APR_SUCCESS;
 
142
}
 
143
 
 
144
 
 
145
 
 
146
APR_DECLARE(apr_status_t) apr_file_remove(const char *path, apr_pool_t *pool)
 
147
{
 
148
    ULONG rc = DosDelete(path);
 
149
    return APR_FROM_OS_ERROR(rc);
 
150
}
 
151
 
 
152
 
 
153
 
 
154
APR_DECLARE(apr_status_t) apr_file_rename(const char *from_path, const char *to_path,
 
155
                                   apr_pool_t *p)
 
156
{
 
157
    ULONG rc = DosMove(from_path, to_path);
 
158
 
 
159
    if (rc == ERROR_ACCESS_DENIED || rc == ERROR_ALREADY_EXISTS) {
 
160
        rc = DosDelete(to_path);
 
161
 
 
162
        if (rc == 0 || rc == ERROR_FILE_NOT_FOUND) {
 
163
            rc = DosMove(from_path, to_path);
 
164
        }
 
165
    }
 
166
 
 
167
    return APR_FROM_OS_ERROR(rc);
 
168
}
 
169
 
 
170
 
 
171
 
 
172
APR_DECLARE(apr_status_t) apr_os_file_get(apr_os_file_t *thefile, apr_file_t *file)
 
173
{
 
174
    *thefile = file->filedes;
 
175
    return APR_SUCCESS;
 
176
}
 
177
 
 
178
 
 
179
 
 
180
APR_DECLARE(apr_status_t) apr_os_file_put(apr_file_t **file, apr_os_file_t *thefile, apr_int32_t flags, apr_pool_t *pool)
 
181
{
 
182
    apr_os_file_t *dafile = thefile;
 
183
 
 
184
    (*file) = apr_palloc(pool, sizeof(apr_file_t));
 
185
    (*file)->pool = pool;
 
186
    (*file)->filedes = *dafile;
 
187
    (*file)->isopen = TRUE;
 
188
    (*file)->eof_hit = FALSE;
 
189
    (*file)->flags = flags;
 
190
    (*file)->pipe = FALSE;
 
191
    (*file)->buffered = (flags & APR_BUFFERED) > 0;
 
192
 
 
193
    if ((*file)->buffered) {
 
194
        apr_status_t rv;
 
195
 
 
196
        (*file)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
 
197
        rv = apr_thread_mutex_create(&(*file)->mutex, 0, pool);
 
198
 
 
199
        if (rv)
 
200
            return rv;
 
201
    }
 
202
 
 
203
    return APR_SUCCESS;
 
204
}    
 
205
 
 
206
 
 
207
APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr)
 
208
{
 
209
    if (!fptr->isopen || fptr->eof_hit == 1) {
 
210
        return APR_EOF;
 
211
    }
 
212
    return APR_SUCCESS;
 
213
}   
 
214
 
 
215
 
 
216
APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile, apr_pool_t *pool)
 
217
{
 
218
    apr_os_file_t fd = 2;
 
219
 
 
220
    return apr_os_file_put(thefile, &fd, 0, pool);
 
221
}
 
222
 
 
223
 
 
224
 
 
225
APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile, apr_pool_t *pool)
 
226
{
 
227
    apr_os_file_t fd = 1;
 
228
 
 
229
    return apr_os_file_put(thefile, &fd, 0, pool);
 
230
}
 
231
 
 
232
 
 
233
APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile, apr_pool_t *pool)
 
234
{
 
235
    apr_os_file_t fd = 0;
 
236
 
 
237
    return apr_os_file_put(thefile, &fd, 0, pool);
 
238
}
 
239
 
 
240
APR_POOL_IMPLEMENT_ACCESSOR(file);
 
241
 
 
242
APR_IMPLEMENT_INHERIT_SET(file, flags, pool, apr_file_cleanup)
 
243
 
 
244
APR_IMPLEMENT_INHERIT_UNSET(file, flags, pool, apr_file_cleanup)
 
245