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.
18 #include "apr_arch_file_io.h"
19 #include "apr_file_io.h"
20 #include "apr_strings.h"
21 #include "apr_portable.h"
22 #include "apr_arch_atime.h"
33 #ifdef HAVE_SYS_STAT_H
38 static apr_status_t dir_cleanup(void *thedir)
40 apr_dir_t *dir = thedir;
41 if (dir->dirhand != INVALID_HANDLE_VALUE && !FindClose(dir->dirhand)) {
42 return apr_get_os_error();
44 dir->dirhand = INVALID_HANDLE_VALUE;
48 APR_DECLARE(apr_status_t) apr_dir_open(apr_dir_t **new, const char *dirname,
53 int len = strlen(dirname);
54 (*new) = apr_pcalloc(pool, sizeof(apr_dir_t));
55 /* Leave room here to add and pop the '*' wildcard for FindFirstFile
56 * and double-null terminate so we have one character to change.
58 (*new)->dirname = apr_palloc(pool, len + 3);
59 memcpy((*new)->dirname, dirname, len);
60 if (len && (*new)->dirname[len - 1] != '/') {
61 (*new)->dirname[len++] = '/';
63 (*new)->dirname[len++] = '\0';
64 (*new)->dirname[len] = '\0';
66 #if APR_HAS_UNICODE_FS
69 /* Create a buffer for the longest file name we will ever see
71 (*new)->w.entry = apr_pcalloc(pool, sizeof(WIN32_FIND_DATAW));
72 (*new)->name = apr_pcalloc(pool, APR_FILE_MAX * 3 + 1);
78 /* Note that we won't open a directory that is greater than MAX_PATH,
79 * including the trailing /* wildcard suffix. If a * won't fit, then
80 * neither will any other file name within the directory.
81 * The length not including the trailing '*' is stored as rootlen, to
82 * skip over all paths which are too long.
84 if (len >= APR_PATH_MAX) {
86 return APR_ENAMETOOLONG;
88 (*new)->n.entry = apr_pcalloc(pool, sizeof(WIN32_FIND_DATAW));
91 (*new)->rootlen = len - 1;
93 (*new)->dirhand = INVALID_HANDLE_VALUE;
94 apr_pool_cleanup_register((*new)->pool, (void *)(*new), dir_cleanup,
95 apr_pool_cleanup_null);
97 rv = apr_dir_read(NULL, 0, *new);
98 if (rv != APR_SUCCESS) {
106 APR_DECLARE(apr_status_t) apr_dir_close(apr_dir_t *dir)
108 apr_pool_cleanup_kill(dir->pool, dir, dir_cleanup);
109 return dir_cleanup(dir);
112 APR_DECLARE(apr_status_t) apr_dir_read(apr_finfo_t *finfo, apr_int32_t wanted,
117 /* The while loops below allow us to skip all invalid file names, so that
118 * we aren't reporting any files where their absolute paths are too long.
120 #if APR_HAS_UNICODE_FS
121 apr_wchar_t wdirname[APR_PATH_MAX];
122 apr_wchar_t *eos = NULL;
125 /* This code path is always be invoked by apr_dir_open or
126 * apr_dir_rewind, so return without filling out the finfo.
128 if (thedir->dirhand == INVALID_HANDLE_VALUE)
131 if (rv = utf8_to_unicode_path(wdirname, sizeof(wdirname)
132 / sizeof(apr_wchar_t),
136 eos = wcschr(wdirname, '\0');
139 thedir->dirhand = FindFirstFileW(wdirname, thedir->w.entry);
141 if (thedir->dirhand == INVALID_HANDLE_VALUE) {
142 return apr_get_os_error();
147 else if (thedir->bof) {
148 /* Noop - we already called FindFirstFileW from
149 * either apr_dir_open or apr_dir_rewind ... use
154 else if (!FindNextFileW(thedir->dirhand, thedir->w.entry)) {
155 return apr_get_os_error();
158 while (thedir->rootlen &&
159 thedir->rootlen + wcslen(thedir->w.entry->cFileName) >= APR_PATH_MAX)
161 if (!FindNextFileW(thedir->dirhand, thedir->w.entry)) {
162 return apr_get_os_error();
165 if (rv = unicode_to_utf8_path(thedir->name, APR_FILE_MAX * 3 + 1,
166 thedir->w.entry->cFileName))
168 fname = thedir->name;
174 /* This code path is always be invoked by apr_dir_open or
175 * apr_dir_rewind, so return without filling out the finfo.
177 if (thedir->dirhand == INVALID_HANDLE_VALUE) {
178 /* '/' terminated, so add the '*' and pop it when we finish */
179 char *eop = strchr(thedir->dirname, '\0');
182 thedir->dirhand = FindFirstFileA(thedir->dirname,
185 if (thedir->dirhand == INVALID_HANDLE_VALUE) {
186 return apr_get_os_error();
191 else if (thedir->bof) {
192 /* Noop - we already called FindFirstFileW from
193 * either apr_dir_open or apr_dir_rewind ... use
198 else if (!FindNextFileA(thedir->dirhand, thedir->n.entry)) {
199 return apr_get_os_error();
201 while (thedir->rootlen &&
202 thedir->rootlen + strlen(thedir->n.entry->cFileName) >= MAX_PATH)
204 if (!FindNextFileA(thedir->dirhand, thedir->n.entry)) {
205 return apr_get_os_error();
208 fname = thedir->n.entry->cFileName;
212 fillin_fileinfo(finfo, (WIN32_FILE_ATTRIBUTE_DATA *) thedir->w.entry,
214 finfo->pool = thedir->pool;
216 finfo->valid |= APR_FINFO_NAME;
219 if (wanted &= ~finfo->valid) {
220 /* Go back and get more_info if we can't answer the whole inquiry
222 #if APR_HAS_UNICODE_FS
225 /* Almost all our work is done. Tack on the wide file name
226 * to the end of the wdirname (already / delimited)
229 eos = wcschr(wdirname, '\0');
230 wcscpy(eos, thedir->w.entry->cFileName);
231 rv = more_finfo(finfo, wdirname, wanted, MORE_OF_WFSPEC);
239 #if APR_HAS_UNICODE_FS
240 /* Don't waste stack space on a second buffer, the one we set
241 * aside for the wide directory name is twice what we need.
243 char *fspec = (char*)wdirname;
245 char fspec[APR_PATH_MAX];
247 int dirlen = strlen(thedir->dirname);
248 if (dirlen >= sizeof(fspec))
249 dirlen = sizeof(fspec) - 1;
250 apr_cpystrn(fspec, thedir->dirname, sizeof(fspec));
251 apr_cpystrn(fspec + dirlen, fname, sizeof(fspec) - dirlen);
252 return more_finfo(finfo, fspec, wanted, MORE_OF_FSPEC);
260 APR_DECLARE(apr_status_t) apr_dir_rewind(apr_dir_t *dir)
264 /* this will mark the handle as invalid and we'll open it
265 * again if apr_dir_read() is subsequently called
267 rv = dir_cleanup(dir);
269 if (rv == APR_SUCCESS)
270 rv = apr_dir_read(NULL, 0, dir);
275 APR_DECLARE(apr_status_t) apr_dir_make(const char *path, apr_fileperms_t perm,
278 #if APR_HAS_UNICODE_FS
281 apr_wchar_t wpath[APR_PATH_MAX];
283 if (rv = utf8_to_unicode_path(wpath, sizeof(wpath)
284 / sizeof(apr_wchar_t), path)) {
287 if (!CreateDirectoryW(wpath, NULL)) {
288 return apr_get_os_error();
294 if (!CreateDirectory(path, NULL)) {
295 return apr_get_os_error();
302 static apr_status_t dir_make_parent(char *path,
303 apr_fileperms_t perm,
307 char *ch = strrchr(path, '\\');
313 rv = apr_dir_make (path, perm, pool); /* Try to make straight off */
315 if (APR_STATUS_IS_ENOENT(rv)) { /* Missing an intermediate dir */
316 rv = dir_make_parent(path, perm, pool);
318 if (rv == APR_SUCCESS) {
319 rv = apr_dir_make (path, perm, pool); /* And complete the path */
323 *ch = '\\'; /* Always replace the slash before returning */
327 APR_DECLARE(apr_status_t) apr_dir_make_recursive(const char *path,
328 apr_fileperms_t perm,
333 rv = apr_dir_make (path, perm, pool); /* Try to make PATH right out */
335 if (APR_STATUS_IS_EEXIST(rv)) /* It's OK if PATH exists */
338 if (APR_STATUS_IS_ENOENT(rv)) { /* Missing an intermediate dir */
341 rv = apr_filepath_merge(&dir, "", path, APR_FILEPATH_NATIVE, pool);
343 if (rv == APR_SUCCESS)
344 rv = dir_make_parent(dir, perm, pool); /* Make intermediate dirs */
346 if (rv == APR_SUCCESS)
347 rv = apr_dir_make (dir, perm, pool); /* And complete the path */
353 APR_DECLARE(apr_status_t) apr_dir_remove(const char *path, apr_pool_t *pool)
355 #if APR_HAS_UNICODE_FS
358 apr_wchar_t wpath[APR_PATH_MAX];
360 if (rv = utf8_to_unicode_path(wpath, sizeof(wpath)
361 / sizeof(apr_wchar_t), path)) {
364 if (!RemoveDirectoryW(wpath)) {
365 return apr_get_os_error();
371 if (!RemoveDirectory(path)) {
372 return apr_get_os_error();
378 APR_DECLARE(apr_status_t) apr_os_dir_get(apr_os_dir_t **thedir,
384 *thedir = dir->dirhand;
388 APR_DECLARE(apr_status_t) apr_os_dir_put(apr_dir_t **dir,
389 apr_os_dir_t *thedir,