simplify path_mkdir

This commit is contained in:
twinaphex 2019-04-23 22:53:07 +02:00
parent f9f66a14b3
commit cca51030d7
1 changed files with 8 additions and 20 deletions

View File

@ -207,38 +207,32 @@ 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)
{ {
int ret = path_mkdir_norecurse(dir); int ret = path_mkdir_norecurse(dir);
@ -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;
} }