1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

support recursive mkdir in create_dir()

This commit is contained in:
Andres Salomon 2002-05-19 03:46:34 +00:00
parent da676a58ca
commit 310a81c90c

View File

@ -128,7 +128,37 @@ int dir_exists(const char *path)
return 1; return 1;
} }
/* FIXME: Make this create directories recursively */ static int _create_dir_recursive(const char *dir)
{
char *orig, *s;
int rc;
/* create parent directories */
orig = s = dbg_strdup(dir);
while ((s = strchr(s, '/')) != NULL) {
*s = '\0';
if (*orig) {
rc = mkdir(orig, 0777);
if (rc < 0 && errno != EEXIST) {
log_sys_error("mkdir", orig);
dbg_free(orig);
return 0;
}
}
*s++ = '/';
}
dbg_free(orig);
/* done w/ parents, create final directory */
log_verbose("Creating directory \"%s\"", dir);
rc = mkdir(dir, 0777);
if (rc < 0 && errno != EEXIST) {
log_sys_error("mkdir", dir);
return 0;
}
return 1;
}
int create_dir(const char *dir) int create_dir(const char *dir)
{ {
struct stat info; struct stat info;
@ -136,13 +166,8 @@ int create_dir(const char *dir)
if (!*dir) if (!*dir)
return 1; return 1;
if (stat(dir, &info) < 0) { if (stat(dir, &info) < 0)
log_verbose("Creating directory \"%s\"", dir); return _create_dir_recursive(dir);
if (!mkdir(dir, 0777))
return 1;
log_sys_error("mkdir", dir);
return 0;
}
if (S_ISDIR(info.st_mode)) if (S_ISDIR(info.st_mode))
return 1; return 1;