Move away from recursion-based algorithm

Previous implementation could not be realiable for embedded applications
since it used recursion and allocated a buffer for each folder in the
tree. Now, one single allocation is made, duplicating the original string so
it can be tokenized.

A check which ensures each element from the tree is a directory has also
been added. On the other hand, errno is now set when standard functions don't.
This commit is contained in:
Xavier Del Campo Romero 2020-09-23 12:23:08 +02:00
parent 09282eefd0
commit 3de04d477f
2 changed files with 76 additions and 58 deletions

129
mkdir_r.c
View File

@ -17,70 +17,83 @@
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
static int mkdir_r_lvl(const char *const path, int level)
{
int ret = -1;
char *dir = NULL;
DIR *d = NULL;
if (!strlen(path))
goto exit;
const char *c = path;
int cur_level = 0;
while (cur_level <= level)
{
for (; *c && *c != '\\' && *c != '/'; c++)
;
if (*c)
c++;
cur_level++;
}
const size_t sz = c - path + 1;
dir = malloc(sz);
if (!dir)
goto exit;
memcpy(dir, path, sz - 1);
dir[sz - 1] = '\0';
d = opendir(dir);
if (!d && mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))
{
perror("mkdir");
goto exit;
}
if (*c)
mkdir_r_lvl(path, cur_level);
/* No more levels left. */
ret = 0;
exit:
if (dir)
free(dir);
if (d)
closedir(d);
return ret;
}
#include <errno.h>
int mkdir_r(const char *const path)
{
return mkdir_r_lvl(path, 0);
int ret = -1;
char *dup = NULL;
if (!path || !*path)
{
errno = EINVAL;
goto exit;
}
const size_t sz = strlen(path) + 1;
dup = malloc(sz);
if (!dup)
{
errno = ENOMEM;
goto exit;
}
memcpy(dup, path, sz);
char *c = dup;
const char *dir = dup;
int exit_fl = 0;
for (c = dup, dir = dup; !exit_fl; c++)
{
switch (*c)
{
case '\0':
exit_fl = 1;
/* Fall through. */
case '/':
{
const char cb = *c;
*c = '\0';
if (!*dir)
/* Path starting with delimiter character. */
;
else if (!access(dir, 0))
{
struct stat sb;
if (stat(dir, &sb))
goto exit;
else if (!(sb.st_mode & S_IFDIR))
{
errno = ENOTDIR;
goto exit;
}
}
else if (mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))
goto exit;
dir = dup;
*c = cb;
}
break;
}
}
ret = 0;
exit:
if (dup)
free(dup);
return ret;
}

View File

@ -20,6 +20,11 @@
* Recursive directory creation.
* @param path Directory path
* @return 0 if successful, -1 otherwise.
* @note Sets @c errno to @c ENOMEM if internal dynamic allocation fails.
* @note Sets @c errno to @c EINVAL if an invalid or empty directory path is
* given.
* @note Sets @c errno to @c ENOTDIR if one of the elements from @c path is not
* a directory.
*/
int mkdir_r(const char *const path);