1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* 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
apr_status_t apr_unix_file_cleanup(void *thefile)
31
apr_file_t *file = thefile;
32
apr_status_t flush_rv = APR_SUCCESS, rv = APR_SUCCESS;
35
flush_rv = apr_file_flush(file);
37
if (close(file->filedes) == 0) {
39
if (file->flags & APR_DELONCLOSE) {
44
rv = apr_thread_mutex_destroy(file->thlock);
49
/* Are there any error conditions other than EINTR or EBADF? */
52
#ifndef WAITIO_USES_POLL
53
if (file->pollset != NULL) {
54
int pollset_rv = apr_pollset_destroy(file->pollset);
55
/* If the file close failed, return its error value,
56
* not apr_pollset_destroy()'s.
58
if (rv == APR_SUCCESS) {
62
#endif /* !WAITIO_USES_POLL */
63
return rv != APR_SUCCESS ? rv : flush_rv;
66
APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new,
75
apr_thread_mutex_t *thlock;
79
if ((flag & APR_READ) && (flag & APR_WRITE)) {
82
else if (flag & APR_READ) {
85
else if (flag & APR_WRITE) {
92
if (flag & APR_CREATE) {
94
if (flag & APR_EXCL) {
98
if ((flag & APR_EXCL) && !(flag & APR_CREATE)) {
102
if (flag & APR_APPEND) {
105
if (flag & APR_TRUNCATE) {
109
if (flag & APR_BINARY) {
114
#if APR_HAS_LARGE_FILES && defined(_LARGEFILE64_SOURCE)
115
oflags |= O_LARGEFILE;
116
#elif defined(O_LARGEFILE)
117
if (flag & APR_LARGEFILE) {
118
oflags |= O_LARGEFILE;
123
if ((flag & APR_BUFFERED) && (flag & APR_XTHREAD)) {
124
rv = apr_thread_mutex_create(&thlock,
125
APR_THREAD_MUTEX_DEFAULT, pool);
132
if (perm == APR_OS_DEFAULT) {
133
fd = open(fname, oflags, 0666);
136
fd = open(fname, oflags, apr_unix_perms2mode(perm));
142
(*new) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
144
(*new)->flags = flag;
145
(*new)->filedes = fd;
147
(*new)->fname = apr_pstrdup(pool, fname);
149
(*new)->blocking = BLK_ON;
150
(*new)->buffered = (flag & APR_BUFFERED) > 0;
152
if ((*new)->buffered) {
153
(*new)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
155
if ((*new)->flags & APR_XTHREAD) {
156
(*new)->thlock = thlock;
161
(*new)->buffer = NULL;
165
(*new)->timeout = -1;
166
(*new)->ungetchar = -1;
170
(*new)->dataRead = 0;
171
(*new)->direction = 0;
172
#ifndef WAITIO_USES_POLL
173
/* Start out with no pollset. apr_wait_for_io_or_timeout() will
174
* initialize the pollset if needed.
176
(*new)->pollset = NULL;
178
if (!(flag & APR_FILE_NOCLEANUP)) {
179
apr_pool_cleanup_register((*new)->pool, (void *)(*new),
180
apr_unix_file_cleanup,
181
apr_unix_file_cleanup);
186
APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
188
return apr_pool_cleanup_run(file->pool, file, apr_unix_file_cleanup);
191
APR_DECLARE(apr_status_t) apr_file_remove(const char *path, apr_pool_t *pool)
193
if (unlink(path) == 0) {
201
APR_DECLARE(apr_status_t) apr_file_rename(const char *from_path,
205
if (rename(from_path, to_path) != 0) {
211
APR_DECLARE(apr_status_t) apr_os_file_get(apr_os_file_t *thefile,
214
*thefile = file->filedes;
218
APR_DECLARE(apr_status_t) apr_os_file_put(apr_file_t **file,
219
apr_os_file_t *thefile,
220
apr_int32_t flags, apr_pool_t *pool)
222
int *dafile = thefile;
224
(*file) = apr_pcalloc(pool, sizeof(apr_file_t));
225
(*file)->pool = pool;
226
(*file)->eof_hit = 0;
227
(*file)->blocking = BLK_UNKNOWN; /* in case it is a pipe */
228
(*file)->timeout = -1;
229
(*file)->ungetchar = -1; /* no char avail */
230
(*file)->filedes = *dafile;
231
(*file)->flags = flags | APR_FILE_NOCLEANUP;
232
(*file)->buffered = (flags & APR_BUFFERED) > 0;
234
#ifndef WAITIO_USES_POLL
235
/* Start out with no pollset. apr_wait_for_io_or_timeout() will
236
* initialize the pollset if needed.
238
(*file)->pollset = NULL;
241
if ((*file)->buffered) {
242
(*file)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
244
if ((*file)->flags & APR_XTHREAD) {
246
rv = apr_thread_mutex_create(&((*file)->thlock),
247
APR_THREAD_MUTEX_DEFAULT, pool);
257
APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr)
259
if (fptr->eof_hit == 1) {
265
APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile,
268
int fd = STDERR_FILENO;
270
return apr_os_file_put(thefile, &fd, 0, pool);
273
APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile,
276
int fd = STDOUT_FILENO;
278
return apr_os_file_put(thefile, &fd, 0, pool);
281
APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile,
284
int fd = STDIN_FILENO;
286
return apr_os_file_put(thefile, &fd, 0, pool);
289
APR_IMPLEMENT_INHERIT_SET(file, flags, pool, apr_unix_file_cleanup)
291
APR_IMPLEMENT_INHERIT_UNSET(file, flags, pool, apr_unix_file_cleanup)
293
APR_POOL_IMPLEMENT_ACCESSOR(file)