bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / file_io / os2 / open.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 "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     return APR_SUCCESS;
203 }    
204
205
206 APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr)
207 {
208     if (!fptr->isopen || fptr->eof_hit == 1) {
209         return APR_EOF;
210     }
211     return APR_SUCCESS;
212 }   
213
214
215 APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile, apr_pool_t *pool)
216 {
217     apr_os_file_t fd = 2;
218
219     return apr_os_file_put(thefile, &fd, 0, pool);
220 }
221
222
223
224 APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile, apr_pool_t *pool)
225 {
226     apr_os_file_t fd = 1;
227
228     return apr_os_file_put(thefile, &fd, 0, pool);
229 }
230
231
232 APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile, apr_pool_t *pool)
233 {
234     apr_os_file_t fd = 0;
235
236     return apr_os_file_put(thefile, &fd, 0, pool);
237 }
238
239 APR_POOL_IMPLEMENT_ACCESSOR(file);
240
241 APR_IMPLEMENT_INHERIT_SET(file, flags, pool, apr_file_cleanup)
242
243 APR_IMPLEMENT_INHERIT_UNSET(file, flags, pool, apr_file_cleanup)
244