bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / file_io / unix / 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_strings.h"
19 #include "apr_portable.h"
20 #include "apr_thread_mutex.h"
21 #include "apr_arch_inherit.h"
22
23 #ifdef NETWARE
24 #include "nks/dirio.h"
25 #include "apr_hash.h"
26 #include "fsio.h"
27 #endif
28
29 static apr_status_t file_cleanup(apr_file_t *file)
30 {
31     apr_status_t rv = APR_SUCCESS;
32
33     if (close(file->filedes) == 0) {
34         file->filedes = -1;
35         if (file->flags & APR_DELONCLOSE) {
36             unlink(file->fname);
37         }
38 #if APR_HAS_THREADS
39         if (file->thlock) {
40             rv = apr_thread_mutex_destroy(file->thlock);
41         }
42 #endif
43     }
44     else {
45         /* Are there any error conditions other than EINTR or EBADF? */
46         rv = errno;
47     }
48     return rv;
49 }
50
51 apr_status_t apr_unix_file_cleanup(void *thefile)
52 {
53     apr_file_t *file = thefile;
54     apr_status_t flush_rv = APR_SUCCESS, rv = APR_SUCCESS;
55
56     if (file->buffered) {
57         flush_rv = apr_file_flush(file);
58     }
59
60     rv = file_cleanup(file);
61
62     return rv != APR_SUCCESS ? rv : flush_rv;
63 }
64
65 apr_status_t apr_unix_child_file_cleanup(void *thefile)
66 {
67     return file_cleanup(thefile);
68 }
69
70 APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new, 
71                                         const char *fname, 
72                                         apr_int32_t flag, 
73                                         apr_fileperms_t perm, 
74                                         apr_pool_t *pool)
75 {
76     apr_os_file_t fd;
77     int oflags = 0;
78 #if APR_HAS_THREADS
79     apr_thread_mutex_t *thlock;
80     apr_status_t rv;
81 #endif
82
83     if ((flag & APR_READ) && (flag & APR_WRITE)) {
84         oflags = O_RDWR;
85     }
86     else if (flag & APR_READ) {
87         oflags = O_RDONLY;
88     }
89     else if (flag & APR_WRITE) {
90         oflags = O_WRONLY;
91     }
92     else {
93         return APR_EACCES; 
94     }
95
96     if (flag & APR_CREATE) {
97         oflags |= O_CREAT; 
98         if (flag & APR_EXCL) {
99             oflags |= O_EXCL;
100         }
101     }
102     if ((flag & APR_EXCL) && !(flag & APR_CREATE)) {
103         return APR_EACCES;
104     }   
105
106     if (flag & APR_APPEND) {
107         oflags |= O_APPEND;
108     }
109     if (flag & APR_TRUNCATE) {
110         oflags |= O_TRUNC;
111     }
112 #ifdef O_BINARY
113     if (flag & APR_BINARY) {
114         oflags |= O_BINARY;
115     }
116 #endif
117 #ifdef O_LARGEFILE
118     if (flag & APR_LARGEFILE) {
119         oflags |= O_LARGEFILE;
120     }
121 #endif
122     
123 #if APR_HAS_THREADS
124     if ((flag & APR_BUFFERED) && (flag & APR_XTHREAD)) {
125         rv = apr_thread_mutex_create(&thlock,
126                                      APR_THREAD_MUTEX_DEFAULT, pool);
127         if (rv) {
128             return rv;
129         }
130     }
131 #endif
132
133     if (perm == APR_OS_DEFAULT) {
134         fd = open(fname, oflags, 0666);
135     }
136     else {
137         fd = open(fname, oflags, apr_unix_perms2mode(perm));
138     } 
139     if (fd < 0) {
140        return errno;
141     }
142
143     (*new) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
144     (*new)->pool = pool;
145     (*new)->flags = flag;
146     (*new)->filedes = fd;
147
148     (*new)->fname = apr_pstrdup(pool, fname);
149
150     (*new)->blocking = BLK_ON;
151     (*new)->buffered = (flag & APR_BUFFERED) > 0;
152
153     if ((*new)->buffered) {
154         (*new)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
155 #if APR_HAS_THREADS
156         if ((*new)->flags & APR_XTHREAD) {
157             (*new)->thlock = thlock;
158         }
159 #endif
160     }
161     else {
162         (*new)->buffer = NULL;
163     }
164
165     (*new)->is_pipe = 0;
166     (*new)->timeout = -1;
167     (*new)->ungetchar = -1;
168     (*new)->eof_hit = 0;
169     (*new)->filePtr = 0;
170     (*new)->bufpos = 0;
171     (*new)->dataRead = 0;
172     (*new)->direction = 0;
173
174     if (!(flag & APR_FILE_NOCLEANUP)) {
175         apr_pool_cleanup_register((*new)->pool, (void *)(*new), 
176                                   apr_unix_file_cleanup, 
177                                   apr_unix_child_file_cleanup);
178     }
179     return APR_SUCCESS;
180 }
181
182 APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
183 {
184     return apr_pool_cleanup_run(file->pool, file, apr_unix_file_cleanup);
185 }
186
187 APR_DECLARE(apr_status_t) apr_file_remove(const char *path, apr_pool_t *pool)
188 {
189     if (unlink(path) == 0) {
190         return APR_SUCCESS;
191     }
192     else {
193         return errno;
194     }
195 }
196
197 APR_DECLARE(apr_status_t) apr_file_rename(const char *from_path, 
198                                           const char *to_path,
199                                           apr_pool_t *p)
200 {
201     if (rename(from_path, to_path) != 0) {
202         return errno;
203     }
204     return APR_SUCCESS;
205 }
206
207 APR_DECLARE(apr_status_t) apr_os_file_get(apr_os_file_t *thefile, 
208                                           apr_file_t *file)
209 {
210     *thefile = file->filedes;
211     return APR_SUCCESS;
212 }
213
214 APR_DECLARE(apr_status_t) apr_os_file_put(apr_file_t **file, 
215                                           apr_os_file_t *thefile,
216                                           apr_int32_t flags, apr_pool_t *pool)
217 {
218     int *dafile = thefile;
219     
220     (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
221     (*file)->pool = pool;
222     (*file)->eof_hit = 0;
223     (*file)->blocking = BLK_UNKNOWN; /* in case it is a pipe */
224     (*file)->timeout = -1;
225     (*file)->ungetchar = -1; /* no char avail */
226     (*file)->filedes = *dafile;
227     (*file)->flags = flags | APR_FILE_NOCLEANUP;
228     (*file)->buffered = (flags & APR_BUFFERED) > 0;
229
230     if ((*file)->buffered) {
231         (*file)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
232 #if APR_HAS_THREADS
233         if ((*file)->flags & APR_XTHREAD) {
234             apr_status_t rv;
235             rv = apr_thread_mutex_create(&((*file)->thlock),
236                                          APR_THREAD_MUTEX_DEFAULT, pool);
237             if (rv) {
238                 return rv;
239             }
240         }
241 #endif
242     }
243     return APR_SUCCESS;
244 }    
245
246 APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr)
247 {
248     if (fptr->eof_hit == 1) {
249         return APR_EOF;
250     }
251     return APR_SUCCESS;
252 }   
253
254 APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile, 
255                                                apr_pool_t *pool)
256 {
257     int fd = STDERR_FILENO;
258
259     return apr_os_file_put(thefile, &fd, 0, pool);
260 }
261
262 APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile, 
263                                                apr_pool_t *pool)
264 {
265     int fd = STDOUT_FILENO;
266
267     return apr_os_file_put(thefile, &fd, 0, pool);
268 }
269
270 APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile, 
271                                               apr_pool_t *pool)
272 {
273     int fd = STDIN_FILENO;
274
275     return apr_os_file_put(thefile, &fd, 0, pool);
276 }
277
278 APR_IMPLEMENT_INHERIT_SET(file, flags, pool, apr_unix_file_cleanup)
279
280 /* We need to do this by hand instead of using APR_IMPLEMENT_INHERIT_UNSET
281  * because the macro sets both cleanups to the same function, which is not
282  * suitable on Unix (see PR 41119). */
283 APR_DECLARE(apr_status_t) apr_file_inherit_unset(apr_file_t *thefile)
284 {
285     if (thefile->flags & APR_FILE_NOCLEANUP) {
286         return APR_EINVAL;
287     }
288     if (thefile->flags & APR_INHERIT) {
289         thefile->flags &= ~APR_INHERIT;
290         apr_pool_child_cleanup_set(thefile->pool,
291                                    (void *)thefile,
292                                    apr_unix_file_cleanup,
293                                    apr_unix_child_file_cleanup);
294     }
295     return APR_SUCCESS;
296 }
297 /* Deprecated */
298 APR_DECLARE(void) apr_file_unset_inherit(apr_file_t *thefile)
299 {
300     apr_file_inherit_unset(thefile);
301 }
302
303 APR_POOL_IMPLEMENT_ACCESSOR(file)