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, int is_child)
31 apr_status_t rv = APR_SUCCESS;
32 int fd = file->filedes;
34 /* Set file descriptor to -1 before close(), so that there is no
35 * chance of returning an already closed FD from apr_os_file_get().
40 /* Only the parent process should delete the file! */
41 if (!is_child && (file->flags & APR_FOPEN_DELONCLOSE)) {
46 rv = apr_thread_mutex_destroy(file->thlock);
51 /* Restore, close() was not successful. */
54 /* Are there any error conditions other than EINTR or EBADF? */
57 #ifndef WAITIO_USES_POLL
58 if (file->pollset != NULL) {
59 apr_status_t pollset_rv = apr_pollset_destroy(file->pollset);
60 /* If the file close failed, return its error value,
61 * not apr_pollset_destroy()'s.
63 if (rv == APR_SUCCESS) {
67 #endif /* !WAITIO_USES_POLL */
71 apr_status_t apr_unix_file_cleanup(void *thefile)
73 apr_file_t *file = thefile;
74 apr_status_t flush_rv = APR_SUCCESS, rv = APR_SUCCESS;
77 flush_rv = apr_file_flush(file);
80 rv = file_cleanup(file, 0);
82 return rv != APR_SUCCESS ? rv : flush_rv;
85 apr_status_t apr_unix_child_file_cleanup(void *thefile)
87 return file_cleanup(thefile, 1);
90 APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new,
99 apr_thread_mutex_t *thlock;
103 if ((flag & APR_FOPEN_READ) && (flag & APR_FOPEN_WRITE)) {
106 else if (flag & APR_FOPEN_READ) {
109 else if (flag & APR_FOPEN_WRITE) {
116 if (flag & APR_FOPEN_CREATE) {
118 if (flag & APR_FOPEN_EXCL) {
122 if ((flag & APR_FOPEN_EXCL) && !(flag & APR_FOPEN_CREATE)) {
126 if (flag & APR_FOPEN_APPEND) {
129 if (flag & APR_FOPEN_TRUNCATE) {
133 if (flag & APR_FOPEN_BINARY) {
138 if (flag & APR_FOPEN_NONBLOCK) {
140 oflags |= O_NONBLOCK;
147 /* Introduced in Linux 2.6.23. Silently ignored on earlier Linux kernels.
149 if (!(flag & APR_FOPEN_NOCLEANUP)) {
154 #if APR_HAS_LARGE_FILES && defined(_LARGEFILE64_SOURCE)
155 oflags |= O_LARGEFILE;
156 #elif defined(O_LARGEFILE)
157 if (flag & APR_FOPEN_LARGEFILE) {
158 oflags |= O_LARGEFILE;
163 if ((flag & APR_FOPEN_BUFFERED) && (flag & APR_FOPEN_XTHREAD)) {
164 rv = apr_thread_mutex_create(&thlock,
165 APR_THREAD_MUTEX_DEFAULT, pool);
172 if (perm == APR_OS_DEFAULT) {
173 fd = open(fname, oflags, 0666);
176 fd = open(fname, oflags, apr_unix_perms2mode(perm));
181 if (!(flag & APR_FOPEN_NOCLEANUP)) {
183 static int has_o_cloexec = 0;
189 if ((flags = fcntl(fd, F_GETFD)) == -1) {
193 if ((flags & FD_CLOEXEC) == 0) {
195 if (fcntl(fd, F_SETFD, flags) == -1) {
208 (*new) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
210 (*new)->flags = flag;
211 (*new)->filedes = fd;
213 (*new)->fname = apr_pstrdup(pool, fname);
215 (*new)->blocking = BLK_ON;
216 (*new)->buffered = (flag & APR_FOPEN_BUFFERED) > 0;
218 if ((*new)->buffered) {
219 (*new)->buffer = apr_palloc(pool, APR_FILE_DEFAULT_BUFSIZE);
220 (*new)->bufsize = APR_FILE_DEFAULT_BUFSIZE;
222 if ((*new)->flags & APR_FOPEN_XTHREAD) {
223 (*new)->thlock = thlock;
228 (*new)->buffer = NULL;
232 (*new)->timeout = -1;
233 (*new)->ungetchar = -1;
237 (*new)->dataRead = 0;
238 (*new)->direction = 0;
239 #ifndef WAITIO_USES_POLL
240 /* Start out with no pollset. apr_wait_for_io_or_timeout() will
241 * initialize the pollset if needed.
243 (*new)->pollset = NULL;
245 if (!(flag & APR_FOPEN_NOCLEANUP)) {
246 apr_pool_cleanup_register((*new)->pool, (void *)(*new),
247 apr_unix_file_cleanup,
248 apr_unix_child_file_cleanup);
253 APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
255 return apr_pool_cleanup_run(file->pool, file, apr_unix_file_cleanup);
258 APR_DECLARE(apr_status_t) apr_file_remove(const char *path, apr_pool_t *pool)
260 if (unlink(path) == 0) {
268 APR_DECLARE(apr_status_t) apr_file_rename(const char *from_path,
272 if (rename(from_path, to_path) != 0) {
278 APR_DECLARE(apr_status_t) apr_os_file_get(apr_os_file_t *thefile,
281 *thefile = file->filedes;
285 APR_DECLARE(apr_status_t) apr_os_file_put(apr_file_t **file,
286 apr_os_file_t *thefile,
287 apr_int32_t flags, apr_pool_t *pool)
289 int *dafile = thefile;
291 (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
292 (*file)->pool = pool;
293 (*file)->eof_hit = 0;
294 (*file)->blocking = BLK_UNKNOWN; /* in case it is a pipe */
295 (*file)->timeout = -1;
296 (*file)->ungetchar = -1; /* no char avail */
297 (*file)->filedes = *dafile;
298 (*file)->flags = flags | APR_FOPEN_NOCLEANUP;
299 (*file)->buffered = (flags & APR_FOPEN_BUFFERED) > 0;
301 #ifndef WAITIO_USES_POLL
302 /* Start out with no pollset. apr_wait_for_io_or_timeout() will
303 * initialize the pollset if needed.
305 (*file)->pollset = NULL;
308 if ((*file)->buffered) {
309 (*file)->buffer = apr_palloc(pool, APR_FILE_DEFAULT_BUFSIZE);
310 (*file)->bufsize = APR_FILE_DEFAULT_BUFSIZE;
312 if ((*file)->flags & APR_FOPEN_XTHREAD) {
314 rv = apr_thread_mutex_create(&((*file)->thlock),
315 APR_THREAD_MUTEX_DEFAULT, pool);
325 APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr)
327 if (fptr->eof_hit == 1) {
333 APR_DECLARE(apr_status_t) apr_file_open_flags_stderr(apr_file_t **thefile,
337 int fd = STDERR_FILENO;
339 return apr_os_file_put(thefile, &fd, flags | APR_FOPEN_WRITE, pool);
342 APR_DECLARE(apr_status_t) apr_file_open_flags_stdout(apr_file_t **thefile,
346 int fd = STDOUT_FILENO;
348 return apr_os_file_put(thefile, &fd, flags | APR_FOPEN_WRITE, pool);
351 APR_DECLARE(apr_status_t) apr_file_open_flags_stdin(apr_file_t **thefile,
355 int fd = STDIN_FILENO;
357 return apr_os_file_put(thefile, &fd, flags | APR_FOPEN_READ, pool);
360 APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile,
363 return apr_file_open_flags_stderr(thefile, 0, pool);
366 APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile,
369 return apr_file_open_flags_stdout(thefile, 0, pool);
372 APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile,
375 return apr_file_open_flags_stdin(thefile, 0, pool);
378 APR_IMPLEMENT_INHERIT_SET(file, flags, pool, apr_unix_file_cleanup)
380 /* We need to do this by hand instead of using APR_IMPLEMENT_INHERIT_UNSET
381 * because the macro sets both cleanups to the same function, which is not
382 * suitable on Unix (see PR 41119). */
383 APR_DECLARE(apr_status_t) apr_file_inherit_unset(apr_file_t *thefile)
385 if (thefile->flags & APR_FOPEN_NOCLEANUP) {
388 if (thefile->flags & APR_INHERIT) {
391 if ((flags = fcntl(thefile->filedes, F_GETFD)) == -1)
395 if (fcntl(thefile->filedes, F_SETFD, flags) == -1)
398 thefile->flags &= ~APR_INHERIT;
399 apr_pool_child_cleanup_set(thefile->pool,
401 apr_unix_file_cleanup,
402 apr_unix_child_file_cleanup);
407 APR_POOL_IMPLEMENT_ACCESSOR(file)
409 APR_DECLARE(apr_status_t) apr_file_link(const char *from_path,
412 if (link(from_path, to_path) == -1) {