Add support for fmkpath(), formatted string mkpath()

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2020-03-11 20:03:03 +01:00
parent 02a42a1cad
commit 7f3f4df9e9
4 changed files with 74 additions and 11 deletions
+6
View File
@@ -232,6 +232,12 @@ Generic Functions
Like `makepath()`, but also takes a `mode_t` permission mode argument.
- `fmkpath(mode, fmt, ...)`
Like `mkpath()`, but takes a formatted string as argument.
> **Notice:** the swapped order of `pathname` and `mode`!
- `movefile(src, dst)`
Like `copyfile()`, but renames `src` to `dst`, or recreates symlink
+1
View File
@@ -81,6 +81,7 @@ char *lfgetkey (lfile_t *lf, const char *key);
int lfgetint (lfile_t *lf, const char *key);
int fgetint (const char *file, const char *sep, const char *key);
int fmkpath (mode_t mode, const char *fmt, ...);
int mkpath (const char *dir, mode_t mode);
int makepath (const char *dir);
+34
View File
@@ -17,6 +17,8 @@
#include <errno.h>
#include <libgen.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "lite.h"
@@ -46,6 +48,38 @@ int mkpath(const char *dir, mode_t mode)
return mkdir(dir, mode);
}
/**
* fmkpath - Formatted version of mkpath()
* @mode: A &mode_t mode to create directories with
* @fmt: Formatted string to be composed into a pathname
*
* Note:
* Notice the swapped arguments, compared to mkpath()!
*
* Returns:
* POSIX OK(0) on success, otherwise -1 with @errno set.
*/
int fmkpath(mode_t mode, const char *fmt, ...)
{
va_list ap;
char *path;
int len;
va_start(ap, fmt);
len = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
path = alloca(len + 1);
if (!path)
return -1;
va_start(ap, fmt);
len = vsnprintf(path, len + 1, fmt, ap);
va_end(ap);
return mkpath(path, mode);
}
/**
* makepath - Create all components of the specified directory.
* @dir: Directory to create.
+33 -11
View File
@@ -4,6 +4,8 @@
#include <stdlib.h>
#include "check.h"
#define BASEDIR "/tmp/libite-test"
int checkpath(const char *dir)
{
char tmp[256];
@@ -32,28 +34,48 @@ int test_makepath(const char *dir)
return ret;
}
int test_fmkpath(const char *subdir)
{
int ret;
ret = fmkpath(0755, "%s/%s", BASEDIR, subdir);
if (!ret) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "%s/%s", BASEDIR, subdir);
ret = checkpath(tmp);
} else
perror("Failed");
return ret;
}
int main(void)
{
int i, ret = 0;
char *list[] = {
"/tmp/libite-test/tok/",
"/tmp/libite-test/tok2",
"/tmp/libite-test/ab",
"/tmp/libite-test/b",
"/tmp/libite-test/a/",
"/tmp/libite-test/a/b",
"/tmp/libite-test/a/c/",
BASEDIR "/tok/",
BASEDIR "/tok2",
BASEDIR "/ab",
BASEDIR "/b",
BASEDIR "/a/",
BASEDIR "/a/b",
BASEDIR "/a/c/",
NULL
};
printf("Testing makepath() ...\n");
mkdir("/tmp/libite-test", 0755);
mkdir(BASEDIR, 0755);
printf("PASS 1/2: makepath() ======================================\n");
for (i = 0; list[i] && !ret; i++)
ret |= test_makepath(list[i]);
printf("\nCleaning up ...\n");
system("rm -rf /tmp/libite-test");
printf("PASS 2/2: fmkpath() =======================================\n");
for (i = 0; list[i] && !ret; i++)
ret |= test_fmkpath(list[i] + strlen(BASEDIR) + 1);
printf("DONE: Cleaning up =========================================\n");
system("rm -rf " BASEDIR);
return ret;
}