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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
24 #include "nks/dirio.h"
29 static apr_status_t file_cleanup(apr_file_t *file)
31 apr_status_t rv = APR_SUCCESS;
33 if (close(file->filedes) == 0) {
35 if (file->flags & APR_DELONCLOSE) {
40 rv = apr_thread_mutex_destroy(file->thlock);
45 /* Are there any error conditions other than EINTR or EBADF? */
51 apr_status_t apr_unix_file_cleanup(void *thefile)
53 apr_file_t *file = thefile;
54 apr_status_t flush_rv = APR_SUCCESS, rv = APR_SUCCESS;
57 flush_rv = apr_file_flush(file);
60 rv = file_cleanup(file);
62 return rv != APR_SUCCESS ? rv : flush_rv;
65 apr_status_t apr_unix_child_file_cleanup(void *thefile)
67 return file_cleanup(thefile);
70 APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new,
79 apr_thread_mutex_t *thlock;
83 if ((flag & APR_READ) && (flag & APR_WRITE)) {
86 else if (flag & APR_READ) {
89 else if (flag & APR_WRITE) {
96 if (flag & APR_CREATE) {
98 if (flag & APR_EXCL) {
102 if ((flag & APR_EXCL) && !(flag & APR_CREATE)) {
106 if (flag & APR_APPEND) {
109 if (flag & APR_TRUNCATE) {
113 if (flag & APR_BINARY) {
118 if (flag & APR_LARGEFILE) {
119 oflags |= O_LARGEFILE;
124 if ((flag & APR_BUFFERED) && (flag & APR_XTHREAD)) {
125 rv = apr_thread_mutex_create(&thlock,
126 APR_THREAD_MUTEX_DEFAULT, pool);
133 if (perm == APR_OS_DEFAULT) {
134 fd = open(fname, oflags, 0666);
137 fd = open(fname, oflags, apr_unix_perms2mode(perm));
143 (*new) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
145 (*new)->flags = flag;
146 (*new)->filedes = fd;
148 (*new)->fname = apr_pstrdup(pool, fname);
150 (*new)->blocking = BLK_ON;
151 (*new)->buffered = (flag & APR_BUFFERED) > 0;
153 if ((*new)->buffered) {
154 (*new)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
156 if ((*new)->flags & APR_XTHREAD) {
157 (*new)->thlock = thlock;
162 (*new)->buffer = NULL;
166 (*new)->timeout = -1;
167 (*new)->ungetchar = -1;
171 (*new)->dataRead = 0;
172 (*new)->direction = 0;
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);
182 APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
184 return apr_pool_cleanup_run(file->pool, file, apr_unix_file_cleanup);
187 APR_DECLARE(apr_status_t) apr_file_remove(const char *path, apr_pool_t *pool)
189 if (unlink(path) == 0) {
197 APR_DECLARE(apr_status_t) apr_file_rename(const char *from_path,
201 if (rename(from_path, to_path) != 0) {
207 APR_DECLARE(apr_status_t) apr_os_file_get(apr_os_file_t *thefile,
210 *thefile = file->filedes;
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)
218 int *dafile = thefile;
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;
230 if ((*file)->buffered) {
231 (*file)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
233 if ((*file)->flags & APR_XTHREAD) {
235 rv = apr_thread_mutex_create(&((*file)->thlock),
236 APR_THREAD_MUTEX_DEFAULT, pool);
246 APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr)
248 if (fptr->eof_hit == 1) {
254 APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile,
257 int fd = STDERR_FILENO;
259 return apr_os_file_put(thefile, &fd, 0, pool);
262 APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile,
265 int fd = STDOUT_FILENO;
267 return apr_os_file_put(thefile, &fd, 0, pool);
270 APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile,
273 int fd = STDIN_FILENO;
275 return apr_os_file_put(thefile, &fd, 0, pool);
278 APR_IMPLEMENT_INHERIT_SET(file, flags, pool, apr_unix_file_cleanup)
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)
285 if (thefile->flags & APR_FILE_NOCLEANUP) {
288 if (thefile->flags & APR_INHERIT) {
289 thefile->flags &= ~APR_INHERIT;
290 apr_pool_child_cleanup_set(thefile->pool,
292 apr_unix_file_cleanup,
293 apr_unix_child_file_cleanup);
298 APR_DECLARE(void) apr_file_unset_inherit(apr_file_t *thefile)
300 apr_file_inherit_unset(thefile);
303 APR_POOL_IMPLEMENT_ACCESSOR(file)