dir_make_recurse.c [plain text]
#include "apr_file_io.h"
#include "apr_lib.h"
#include "apr_strings.h"
#include <string.h>
#define IS_SEP(c) (c == '/' || c == '\\')
static const char *path_canonicalize(const char *path, apr_pool_t *pool)
{
apr_size_t len = strlen(path);
apr_size_t orig_len = len;
while ((len > 0) && IS_SEP(path[len - 1])) {
len--;
}
if (len != orig_len) {
return apr_pstrndup(pool, path, len);
}
else {
return path;
}
}
static char *path_remove_last_component(const char *path, apr_pool_t *pool)
{
const char *newpath = path_canonicalize(path, pool);
int i;
for (i = strlen(newpath) - 1; i >= 0; i--) {
if (IS_SEP(path[i])) {
break;
}
}
return apr_pstrndup(pool, path, (i < 0) ? 0 : i);
}
apr_status_t apr_dir_make_recursive(const char *path, apr_fileperms_t perm,
apr_pool_t *pool)
{
apr_status_t apr_err = APR_SUCCESS;
apr_err = apr_dir_make(path, perm, pool);
if (APR_STATUS_IS_EEXIST(apr_err)) {
return APR_SUCCESS;
}
if (APR_STATUS_IS_ENOENT(apr_err)) {
char *dir;
dir = path_remove_last_component(path, pool);
apr_err = apr_dir_make_recursive(dir, perm, pool);
if (!apr_err) {
apr_err = apr_dir_make(path, perm, pool);
}
}
return apr_err;
}