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) {
139 /* Introduced in Linux 2.6.23. Silently ignored on earlier Linux kernels.
141 if (!(flag & APR_FOPEN_NOCLEANUP)) {
146 #if APR_HAS_LARGE_FILES && defined(_LARGEFILE64_SOURCE)
147 oflags |= O_LARGEFILE;
148 #elif defined(O_LARGEFILE)
149 if (flag & APR_FOPEN_LARGEFILE) {
150 oflags |= O_LARGEFILE;
155 if ((flag & APR_FOPEN_BUFFERED) && (flag & APR_FOPEN_XTHREAD)) {
156 rv = apr_thread_mutex_create(&thlock,
157 APR_THREAD_MUTEX_DEFAULT, pool);
164 if (perm == APR_OS_DEFAULT) {
165 fd = open(fname, oflags, 0666);
168 fd = open(fname, oflags, apr_unix_perms2mode(perm));
173 if (!(flag & APR_FOPEN_NOCLEANUP)) {
175 static int has_o_cloexec = 0;
181 if ((flags = fcntl(fd, F_GETFD)) == -1) {
185 if ((flags & FD_CLOEXEC) == 0) {
187 if (fcntl(fd, F_SETFD, flags) == -1) {
200 (*new) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
202 (*new)->flags = flag;
203 (*new)->filedes = fd;
205 (*new)->fname = apr_pstrdup(pool, fname);
207 (*new)->blocking = BLK_ON;
208 (*new)->buffered = (flag & APR_FOPEN_BUFFERED) > 0;
210 if ((*new)->buffered) {
211 (*new)->buffer = apr_palloc(pool, APR_FILE_DEFAULT_BUFSIZE);
212 (*new)->bufsize = APR_FILE_DEFAULT_BUFSIZE;
214 if ((*new)->flags & APR_FOPEN_XTHREAD) {
215 (*new)->thlock = thlock;
220 (*new)->buffer = NULL;
224 (*new)->timeout = -1;
225 (*new)->ungetchar = -1;
229 (*new)->dataRead = 0;
230 (*new)->direction = 0;
231 #ifndef WAITIO_USES_POLL
232 /* Start out with no pollset. apr_wait_for_io_or_timeout() will
233 * initialize the pollset if needed.
235 (*new)->pollset = NULL;
237 if (!(flag & APR_FOPEN_NOCLEANUP)) {
238 apr_pool_cleanup_register((*new)->pool, (void *)(*new),
239 apr_unix_file_cleanup,
240 apr_unix_child_file_cleanup);
245 APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
247 return apr_pool_cleanup_run(file->pool, file, apr_unix_file_cleanup);
250 APR_DECLARE(apr_status_t) apr_file_remove(const char *path, apr_pool_t *pool)
252 if (unlink(path) == 0) {
260 APR_DECLARE(apr_status_t) apr_file_rename(const char *from_path,
264 if (rename(from_path, to_path) != 0) {
270 APR_DECLARE(apr_status_t) apr_os_file_get(apr_os_file_t *thefile,
273 *thefile = file->filedes;
277 APR_DECLARE(apr_status_t) apr_os_file_put(apr_file_t **file,
278 apr_os_file_t *thefile,
279 apr_int32_t flags, apr_pool_t *pool)
281 int *dafile = thefile;
283 (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
284 (*file)->pool = pool;
285 (*file)->eof_hit = 0;
286 (*file)->blocking = BLK_UNKNOWN; /* in case it is a pipe */
287 (*file)->timeout = -1;
288 (*file)->ungetchar = -1; /* no char avail */
289 (*file)->filedes = *dafile;
290 (*file)->flags = flags | APR_FOPEN_NOCLEANUP;
291 (*file)->buffered = (flags & APR_FOPEN_BUFFERED) > 0;
293 #ifndef WAITIO_USES_POLL
294 /* Start out with no pollset. apr_wait_for_io_or_timeout() will
295 * initialize the pollset if needed.
297 (*file)->pollset = NULL;
300 if ((*file)->buffered) {
301 (*file)->buffer = apr_palloc(pool, APR_FILE_DEFAULT_BUFSIZE);
302 (*file)->bufsize = APR_FILE_DEFAULT_BUFSIZE;
304 if ((*file)->flags & APR_FOPEN_XTHREAD) {
306 rv = apr_thread_mutex_create(&((*file)->thlock),
307 APR_THREAD_MUTEX_DEFAULT, pool);
317 APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr)
319 if (fptr->eof_hit == 1) {
325 APR_DECLARE(apr_status_t) apr_file_open_flags_stderr(apr_file_t **thefile,
329 int fd = STDERR_FILENO;
331 return apr_os_file_put(thefile, &fd, flags | APR_FOPEN_WRITE, pool);
334 APR_DECLARE(apr_status_t) apr_file_open_flags_stdout(apr_file_t **thefile,
338 int fd = STDOUT_FILENO;
340 return apr_os_file_put(thefile, &fd, flags | APR_FOPEN_WRITE, pool);
343 APR_DECLARE(apr_status_t) apr_file_open_flags_stdin(apr_file_t **thefile,
347 int fd = STDIN_FILENO;
349 return apr_os_file_put(thefile, &fd, flags | APR_FOPEN_READ, pool);
352 APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile,
355 return apr_file_open_flags_stderr(thefile, 0, pool);
358 APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile,
361 return apr_file_open_flags_stdout(thefile, 0, pool);
364 APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile,
367 return apr_file_open_flags_stdin(thefile, 0, pool);
370 APR_IMPLEMENT_INHERIT_SET(file, flags, pool, apr_unix_file_cleanup)
372 /* We need to do this by hand instead of using APR_IMPLEMENT_INHERIT_UNSET
373 * because the macro sets both cleanups to the same function, which is not
374 * suitable on Unix (see PR 41119). */
375 APR_DECLARE(apr_status_t) apr_file_inherit_unset(apr_file_t *thefile)
377 if (thefile->flags & APR_FOPEN_NOCLEANUP) {
380 if (thefile->flags & APR_INHERIT) {
383 if ((flags = fcntl(thefile->filedes, F_GETFD)) == -1)
387 if (fcntl(thefile->filedes, F_SETFD, flags) == -1)
390 thefile->flags &= ~APR_INHERIT;
391 apr_pool_child_cleanup_set(thefile->pool,
393 apr_unix_file_cleanup,
394 apr_unix_child_file_cleanup);
399 APR_POOL_IMPLEMENT_ACCESSOR(file)
401 APR_DECLARE(apr_status_t) apr_file_link(const char *from_path,
404 if (link(from_path, to_path) == -1) {