Index: libapr.dsp =================================================================== --- libapr.dsp (revision 589430) +++ libapr.dsp (working copy) @@ -325,6 +325,10 @@ SOURCE=.\file_io\unix\tempdir.c # End Source File +# Begin Source File + +SOURCE=.\file_io\unix\fileutil.c +# End Source File # End Group # Begin Group "locks" Index: apr.dsp =================================================================== --- apr.dsp (revision 589430) +++ apr.dsp (working copy) @@ -273,6 +273,10 @@ SOURCE=.\file_io\unix\tempdir.c # End Source File +# Begin Source File + +SOURCE=.\file_io\unix\fileutil.c +# End Source File # End Group # Begin Group "locks" Index: test/testdir.c =================================================================== --- test/testdir.c (revision 589430) +++ test/testdir.c (working copy) @@ -251,6 +251,69 @@ } +static apr_status_t create_file(abts_case *tc, const char * fname) +{ + apr_status_t rv; + apr_file_t * file; + + rv = apr_file_open(&file, fname, + APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE, + APR_OS_DEFAULT, p); + + ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); + if (APR_SUCCESS != rv) + { + printf("%s\n", fname); + return rv; + exit(-1); +} + rv = apr_file_close(file); + ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); + return rv; +} + +static void test_rmdir_recurs(abts_case *tc, void *data) +{ + apr_status_t rv; + apr_finfo_t finfo; + + /* create a subtree of the following format: + one/ + |----file1 + |----file2 + +----two/ + |----file1 + |----file2 + +three/ + |----file1 + |----file2 + */ + + rv = apr_dir_make_recursive("data/one/two/three", APR_FPROT_OS_DEFAULT, p); + ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); + + rv = apr_stat(&finfo, "data/one/two/three", APR_FINFO_TYPE, p); + ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); + ABTS_INT_EQUAL(tc, APR_DIR, finfo.filetype); + + ABTS_INT_EQUAL(tc, APR_SUCCESS, create_file(tc,"data/one/two/three/file1")); + ABTS_INT_EQUAL(tc, APR_SUCCESS, create_file(tc,"data/one/two/three/file2")); + ABTS_INT_EQUAL(tc, APR_SUCCESS, create_file(tc,"data/one/two/file1")); + ABTS_INT_EQUAL(tc, APR_SUCCESS, create_file(tc,"data/one/two/file2")); + ABTS_INT_EQUAL(tc, APR_SUCCESS, create_file(tc,"data/one/file1")); + ABTS_INT_EQUAL(tc, APR_SUCCESS, create_file(tc,"data/one/file2")); + + + rv = apr_dir_remove_recursive("data/one", p); + ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); + + + + rv = apr_stat(&finfo, "data/one", APR_FINFO_TYPE, p); + ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv)); + ABTS_INT_EQUAL(tc, APR_DIR, finfo.filetype); +} + abts_suite *testdir(abts_suite *suite) { suite = ADD_SUITE(suite) @@ -271,6 +334,7 @@ abts_run_test(suite, test_closedir, NULL); abts_run_test(suite, test_uncleared_errno, NULL); + abts_run_test(suite, test_rmdir_recurs, NULL); return suite; } Index: include/apr_file_io.h =================================================================== --- include/apr_file_io.h (revision 589430) +++ include/apr_file_io.h (working copy) @@ -838,6 +838,18 @@ APR_DECLARE(apr_status_t) apr_dir_remove(const char *path, apr_pool_t *pool); /** + * Remove all files and subdirectories under the specified path. + * @param path The path to the directory to be removed. + * @param pool The pool to use. + * @return APR_SUCCESS or the first error received. + * @remark Deleting does not stop if some file/subdirectory cannot be removed. + * Only the first error received is logged and returned, all other errors are + * ignored. + */ +APR_DECLARE(apr_status_t) apr_dir_remove_recursive(const char * path, + apr_pool_t * pool); + +/** * get the specified file's stats. * @param finfo Where to store the information about the file. * @param wanted The desired apr_finfo_t fields, as a bit flag of APR_FINFO_ values Index: NWGNUmakefile =================================================================== --- NWGNUmakefile (revision 589430) +++ NWGNUmakefile (working copy) @@ -290,6 +290,7 @@ $(OBJDIR)/filedup.o \ $(OBJDIR)/filepath.o \ $(OBJDIR)/filepath_util.o \ + $(OBJDIR)/fileutil.o \ $(OBJDIR)/filestat.o \ $(OBJDIR)/filesys.o \ $(OBJDIR)/flock.o \ Index: file_io/os2/fileutil.c =================================================================== --- file_io/os2/fileutil.c (revision 0) +++ file_io/os2/fileutil.c (revision 0) @@ -0,0 +1 @@ +#include "../unix/fileutil.c" Index: file_io/unix/fileutil.c =================================================================== --- file_io/unix/fileutil.c (revision 0) +++ file_io/unix/fileutil.c (revision 0) @@ -0,0 +1,115 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "apr_file_io.h" +#include "apr_file_info.h" +#include "apr_errno.h" +#include "apr_general.h" + +#define APR_DIR_REMOVE_RECURSIVE_ARRSIZE (6) /*what would be a propper value?*/ + +apr_status_t +apr_dir_remove_recursive(const char *param_path, apr_pool_t * pool) +{ + apr_status_t status, retcode, dirstatus; + apr_dir_t *this_dir; + apr_finfo_t this_entry; + apr_pool_t *array_pool, *loop_pool; + apr_array_header_t *arr; + apr_int32_t flags = APR_FINFO_TYPE | APR_FINFO_NAME; + char *dir_path, *file_path; + int dir_is_empty; + + retcode = APR_SUCCESS; + status = apr_pool_create(&array_pool, pool); + if (status != APR_SUCCESS) { + return status; + } + status = apr_pool_create(&loop_pool, array_pool); + if (status != APR_SUCCESS) { + apr_pool_destroy(array_pool); + return status; + } + + + arr = apr_array_make(array_pool, APR_DIR_REMOVE_RECURSIVE_ARRSIZE, + sizeof(const char *)); + if (arr == NULL) { + apr_pool_destroy(array_pool); + apr_pool_destroy(loop_pool); + return APR_ENOMEM; + } + *(const char **) apr_array_push(arr) = param_path; + + while (!apr_is_empty_array(arr)) { + apr_pool_clear(loop_pool); + dir_path = apr_array_pstrcat(loop_pool, arr, '/'); + if (dir_path == NULL) { + retcode = APR_ENOMEM; + break; + } + + dir_is_empty = 1; + dirstatus = apr_dir_open(&this_dir, dir_path, loop_pool); + if (dirstatus != APR_SUCCESS) { + retcode = dirstatus; + } + else { + for (dirstatus = apr_dir_read(&this_entry, flags, this_dir); + dirstatus == APR_SUCCESS; + dirstatus = apr_dir_read(&this_entry, flags, this_dir)) { + /* skip . and .. */ + if (this_entry.filetype == APR_DIR) { + if (this_entry.name[0] == '.' + && (this_entry.name[1] == '\0' + || (this_entry.name[1] == '.' + && this_entry.name[2] == '\0'))) + continue; + + *(const char **) apr_array_push(arr) = + apr_pstrdup(array_pool, this_entry.name); + dir_is_empty = 0; + break; + } + else { + file_path = apr_pstrcat(loop_pool, dir_path, "/", + this_entry.name, NULL); + status = apr_file_remove(file_path, loop_pool); + if ((status != APR_SUCCESS) && (retcode != APR_SUCCESS)) { + retcode = status; + } + } + } + if (!APR_STATUS_IS_ENOENT(dirstatus) && (retcode != APR_SUCCESS)) { + retcode = dirstatus; + } + dirstatus = apr_dir_close(this_dir); + if ((dirstatus != APR_SUCCESS) && (retcode != APR_SUCCESS)) { + retcode = dirstatus; + } + } + if (dir_is_empty) { + dirstatus = apr_dir_remove(dir_path, loop_pool); + if ((dirstatus != APR_SUCCESS) && (retcode != APR_SUCCESS)) { + retcode = dirstatus; + } + apr_array_pop(arr); + } + } + apr_pool_destroy(array_pool); + return retcode; +} +