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_file_io.h"
20
#include "apr_portable.h"
21
#include "apr_strings.h"
22
#include "apr_arch_inherit.h"
25
apr_status_t apr_file_cleanup(void *thefile)
27
apr_file_t *file = thefile;
28
return apr_file_close(file);
33
APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new, const char *fname, apr_int32_t flag, apr_fileperms_t perm, apr_pool_t *pool)
36
int mflags = OPEN_FLAGS_FAIL_ON_ERROR|OPEN_SHARE_DENYNONE;
39
apr_file_t *dafile = (apr_file_t *)apr_palloc(pool, sizeof(apr_file_t));
42
dafile->isopen = FALSE;
43
dafile->eof_hit = FALSE;
44
dafile->buffer = NULL;
46
dafile->blocking = BLK_ON;
48
if ((flag & APR_READ) && (flag & APR_WRITE)) {
49
mflags |= OPEN_ACCESS_READWRITE;
50
} else if (flag & APR_READ) {
51
mflags |= OPEN_ACCESS_READONLY;
52
} else if (flag & APR_WRITE) {
53
mflags |= OPEN_ACCESS_WRITEONLY;
59
dafile->buffered = (flag & APR_BUFFERED) > 0;
61
if (dafile->buffered) {
62
dafile->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
63
rv = apr_thread_mutex_create(&dafile->mutex, 0, pool);
69
if (flag & APR_CREATE) {
70
oflags |= OPEN_ACTION_CREATE_IF_NEW;
72
if (!(flag & APR_EXCL) && !(flag & APR_TRUNCATE)) {
73
oflags |= OPEN_ACTION_OPEN_IF_EXISTS;
77
if ((flag & APR_EXCL) && !(flag & APR_CREATE))
80
if (flag & APR_TRUNCATE) {
81
oflags |= OPEN_ACTION_REPLACE_IF_EXISTS;
82
} else if ((oflags & 0xFF) == 0) {
83
oflags |= OPEN_ACTION_OPEN_IF_EXISTS;
86
rv = DosOpen(fname, &(dafile->filedes), &action, 0, 0, oflags, mflags, NULL);
88
if (rv == 0 && (flag & APR_APPEND)) {
90
rv = DosSetFilePtr(dafile->filedes, 0, FILE_END, &newptr );
93
DosClose(dafile->filedes);
97
return APR_FROM_OS_ERROR(rv);
99
dafile->isopen = TRUE;
100
dafile->fname = apr_pstrdup(pool, fname);
103
dafile->dataRead = 0;
104
dafile->direction = 0;
105
dafile->pipe = FALSE;
107
if (!(flag & APR_FILE_NOCLEANUP)) {
108
apr_pool_cleanup_register(dafile->pool, dafile, apr_file_cleanup, apr_file_cleanup);
117
APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
122
if (file && file->isopen) {
123
apr_file_flush(file);
124
rc = DosClose(file->filedes);
127
file->isopen = FALSE;
128
status = APR_SUCCESS;
130
if (file->flags & APR_DELONCLOSE) {
131
status = APR_FROM_OS_ERROR(DosDelete(file->fname));
134
return APR_FROM_OS_ERROR(rc);
139
apr_thread_mutex_destroy(file->mutex);
146
APR_DECLARE(apr_status_t) apr_file_remove(const char *path, apr_pool_t *pool)
148
ULONG rc = DosDelete(path);
149
return APR_FROM_OS_ERROR(rc);
154
APR_DECLARE(apr_status_t) apr_file_rename(const char *from_path, const char *to_path,
157
ULONG rc = DosMove(from_path, to_path);
159
if (rc == ERROR_ACCESS_DENIED || rc == ERROR_ALREADY_EXISTS) {
160
rc = DosDelete(to_path);
162
if (rc == 0 || rc == ERROR_FILE_NOT_FOUND) {
163
rc = DosMove(from_path, to_path);
167
return APR_FROM_OS_ERROR(rc);
172
APR_DECLARE(apr_status_t) apr_os_file_get(apr_os_file_t *thefile, apr_file_t *file)
174
*thefile = file->filedes;
180
APR_DECLARE(apr_status_t) apr_os_file_put(apr_file_t **file, apr_os_file_t *thefile, apr_int32_t flags, apr_pool_t *pool)
182
apr_os_file_t *dafile = thefile;
184
(*file) = apr_palloc(pool, sizeof(apr_file_t));
185
(*file)->pool = pool;
186
(*file)->filedes = *dafile;
187
(*file)->isopen = TRUE;
188
(*file)->eof_hit = FALSE;
189
(*file)->flags = flags;
190
(*file)->pipe = FALSE;
191
(*file)->buffered = (flags & APR_BUFFERED) > 0;
193
if ((*file)->buffered) {
196
(*file)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
197
rv = apr_thread_mutex_create(&(*file)->mutex, 0, pool);
207
APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr)
209
if (!fptr->isopen || fptr->eof_hit == 1) {
216
APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile, apr_pool_t *pool)
218
apr_os_file_t fd = 2;
220
return apr_os_file_put(thefile, &fd, 0, pool);
225
APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile, apr_pool_t *pool)
227
apr_os_file_t fd = 1;
229
return apr_os_file_put(thefile, &fd, 0, pool);
233
APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile, apr_pool_t *pool)
235
apr_os_file_t fd = 0;
237
return apr_os_file_put(thefile, &fd, 0, pool);
240
APR_POOL_IMPLEMENT_ACCESSOR(file);
242
APR_IMPLEMENT_INHERIT_SET(file, flags, pool, apr_file_cleanup)
244
APR_IMPLEMENT_INHERIT_UNSET(file, flags, pool, apr_file_cleanup)