simplify path_mkdir
This commit is contained in:
parent
f9f66a14b3
commit
cca51030d7
|
@ -207,37 +207,31 @@ int32_t path_get_size(const char *path)
|
||||||
bool path_mkdir(const char *dir)
|
bool path_mkdir(const char *dir)
|
||||||
{
|
{
|
||||||
/* Use heap. Real chance of stack overflow if we recurse too hard. */
|
/* Use heap. Real chance of stack overflow if we recurse too hard. */
|
||||||
const char *target = NULL;
|
|
||||||
bool sret = false;
|
bool sret = false;
|
||||||
bool norecurse = false;
|
bool norecurse = false;
|
||||||
char *basedir = NULL;
|
char *basedir = (dir && *dir) ? strdup(dir) : NULL;
|
||||||
|
|
||||||
if (dir && *dir)
|
|
||||||
basedir = strdup(dir);
|
|
||||||
|
|
||||||
if (!basedir)
|
if (!basedir)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
path_parent_dir(basedir);
|
path_parent_dir(basedir);
|
||||||
if (!*basedir || !strcmp(basedir, dir))
|
if (!*basedir || !strcmp(basedir, dir))
|
||||||
goto end;
|
{
|
||||||
|
free(basedir);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (path_is_directory(basedir))
|
if (path_is_directory(basedir))
|
||||||
{
|
|
||||||
target = dir;
|
|
||||||
norecurse = true;
|
norecurse = true;
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
target = basedir;
|
|
||||||
sret = path_mkdir(basedir);
|
sret = path_mkdir(basedir);
|
||||||
|
|
||||||
if (sret)
|
if (sret)
|
||||||
{
|
|
||||||
target = dir;
|
|
||||||
norecurse = true;
|
norecurse = true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
free(basedir);
|
||||||
|
|
||||||
if (norecurse)
|
if (norecurse)
|
||||||
{
|
{
|
||||||
|
@ -247,15 +241,9 @@ bool path_mkdir(const char *dir)
|
||||||
if (ret == -2 && path_is_directory(dir))
|
if (ret == -2 && path_is_directory(dir))
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
if (ret < 0)
|
return (ret == 0);
|
||||||
printf("mkdir(%s) error: %s.\n", dir, strerror(errno));
|
|
||||||
sret = (ret == 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
|
||||||
if (target && !sret)
|
|
||||||
printf("Failed to create directory: \"%s\".\n", target);
|
|
||||||
free(basedir);
|
|
||||||
return sret;
|
return sret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue