mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-08-05 16:48:48 +00:00
utils: reimplement/fix mkdir_p() (v2)
Reimplement mkdir_p() such that it: ...handles relativ paths correctly. (currently it crashes) ...does not rely on dirname(). ...is not recursive. ...is shorter. ;-) Signed-off-by: Richard Weinberger <richard@nod.at> Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
parent
23154d5764
commit
860fc865b0
@ -95,39 +95,23 @@ extern int get_u16(unsigned short *val, const char *arg, int base)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int is_all_slashes(char *path)
|
||||
{
|
||||
while (*path && *path == '/')
|
||||
path++;
|
||||
if (*path)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
extern int mkdir_p(char *dir, mode_t mode)
|
||||
{
|
||||
int ret;
|
||||
char *d;
|
||||
char *tmp = dir;
|
||||
char *orig = dir;
|
||||
char *makeme;
|
||||
|
||||
if (is_all_slashes(dir))
|
||||
return 0;
|
||||
|
||||
d = strdup(dir);
|
||||
if (!d)
|
||||
return -1;
|
||||
|
||||
ret = mkdir_p(dirname(d), mode);
|
||||
free(d);
|
||||
if (ret)
|
||||
return -1;
|
||||
|
||||
if (!access(dir, F_OK))
|
||||
return 0;
|
||||
|
||||
if (mkdir(dir, mode)) {
|
||||
SYSERROR("failed to create directory '%s'\n", dir);
|
||||
return -1;
|
||||
}
|
||||
do {
|
||||
dir = tmp + strspn(tmp, "/");
|
||||
tmp = dir + strcspn(dir, "/");
|
||||
makeme = strndupa(orig, dir - orig);
|
||||
if (*makeme) {
|
||||
if (mkdir(makeme, mode) && errno != EEXIST) {
|
||||
SYSERROR("failed to create directory '%s'\n", makeme);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
} while(tmp != dir);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user