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_file_io.h"
20 #include "apr_strings.h"
21 #include "apr_portable.h"
24 static apr_status_t dir_cleanup(void *thedir)
26 apr_dir_t *dir = thedir;
27 return apr_dir_close(dir);
32 APR_DECLARE(apr_status_t) apr_dir_open(apr_dir_t **new, const char *dirname, apr_pool_t *pool)
34 apr_dir_t *thedir = (apr_dir_t *)apr_palloc(pool, sizeof(apr_dir_t));
40 thedir->dirname = apr_pstrdup(pool, dirname);
42 if (thedir->dirname == NULL)
46 thedir->validentry = FALSE;
48 apr_pool_cleanup_register(pool, thedir, dir_cleanup, apr_pool_cleanup_null);
54 APR_DECLARE(apr_status_t) apr_dir_close(apr_dir_t *thedir)
59 rv = DosFindClose(thedir->handle);
66 return APR_FROM_OS_ERROR(rv);
71 APR_DECLARE(apr_status_t) apr_dir_read(apr_finfo_t *finfo, apr_int32_t wanted,
77 if (thedir->handle == 0) {
78 thedir->handle = HDIR_CREATE;
79 rv = DosFindFirst(apr_pstrcat(thedir->pool, thedir->dirname, "/*", NULL), &thedir->handle,
80 FILE_ARCHIVED|FILE_DIRECTORY|FILE_SYSTEM|FILE_HIDDEN|FILE_READONLY,
81 &thedir->entry, sizeof(thedir->entry), &entries, FIL_STANDARD);
83 rv = DosFindNext(thedir->handle, &thedir->entry, sizeof(thedir->entry), &entries);
86 finfo->pool = thedir->pool;
90 if (rv == 0 && entries == 1) {
91 thedir->validentry = TRUE;
93 /* We passed a name off the stack that has popped */
95 finfo->size = thedir->entry.cbFile;
96 finfo->csize = thedir->entry.cbFileAlloc;
98 /* Only directories & regular files show up in directory listings */
99 finfo->filetype = (thedir->entry.attrFile & FILE_DIRECTORY) ? APR_DIR : APR_REG;
101 apr_os2_time_to_apr_time(&finfo->mtime, thedir->entry.fdateLastWrite,
102 thedir->entry.ftimeLastWrite);
103 apr_os2_time_to_apr_time(&finfo->atime, thedir->entry.fdateLastAccess,
104 thedir->entry.ftimeLastAccess);
105 apr_os2_time_to_apr_time(&finfo->ctime, thedir->entry.fdateCreation,
106 thedir->entry.ftimeCreation);
108 finfo->name = thedir->entry.achName;
109 finfo->valid = APR_FINFO_NAME | APR_FINFO_MTIME | APR_FINFO_ATIME |
110 APR_FINFO_CTIME | APR_FINFO_TYPE | APR_FINFO_SIZE |
116 thedir->validentry = FALSE;
119 return APR_FROM_OS_ERROR(rv);
126 APR_DECLARE(apr_status_t) apr_dir_rewind(apr_dir_t *thedir)
128 return apr_dir_close(thedir);
133 APR_DECLARE(apr_status_t) apr_dir_make(const char *path, apr_fileperms_t perm, apr_pool_t *pool)
135 return APR_FROM_OS_ERROR(DosCreateDir(path, NULL));
140 apr_status_t apr_dir_make_recursive(const char *path, apr_fileperms_t perm,
148 APR_DECLARE(apr_status_t) apr_dir_remove(const char *path, apr_pool_t *pool)
150 return APR_FROM_OS_ERROR(DosDeleteDir(path));
155 APR_DECLARE(apr_status_t) apr_os_dir_get(apr_os_dir_t **thedir, apr_dir_t *dir)
160 *thedir = &dir->handle;
166 APR_DECLARE(apr_status_t) apr_os_dir_put(apr_dir_t **dir, apr_os_dir_t *thedir,
169 if ((*dir) == NULL) {
170 (*dir) = (apr_dir_t *)apr_pcalloc(pool, sizeof(apr_dir_t));
173 (*dir)->handle = *thedir;