From 900f21a5e5270770dbea0f1879f37703afff4d2b Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 10 Apr 2020 01:57:58 -0400 Subject: lib/fs.c --- lib/fs.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/fs.c b/lib/fs.c index 890c261..79c04ff 100644 --- a/lib/fs.c +++ b/lib/fs.c @@ -491,10 +491,13 @@ char *human_readable_size(uint64_t n) { char *spm_mkdtemp(const char *name, const char *extended_path) { const char *template_unique = "XXXXXX"; char *tmpdir = NULL; - char *template = calloc(PATH_MAX, sizeof(char)); + char template[PATH_MAX]; sprintf(template, "%s%s%s_%s", TMP_DIR, DIRSEPS, name, template_unique); tmpdir = mkdtemp(template); + if (tmpdir == NULL) { + return NULL; + } if (extended_path != NULL) { char extended[PATH_MAX] = {0,}; strncpy(extended, tmpdir, PATH_MAX - 1); @@ -502,6 +505,6 @@ char *spm_mkdtemp(const char *name, const char *extended_path) { strcat(extended, extended_path); mkdirs(extended, 0755); } - return tmpdir; + return strdup(tmpdir); } -- cgit From e6eb62c343e368b343e115fab829b26ad23f4d89 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 10 Apr 2020 01:58:22 -0400 Subject: Add test_spm_mkdtemp --- tests/test_spm_mkdtemp.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/test_spm_mkdtemp.c diff --git a/tests/test_spm_mkdtemp.c b/tests/test_spm_mkdtemp.c new file mode 100644 index 0000000..619ca42 --- /dev/null +++ b/tests/test_spm_mkdtemp.c @@ -0,0 +1,27 @@ +#include "spm.h" +#include "framework.h" + +struct TestCase testCase[] = { + {.arg[0].sptr = "testing", .arg[1].sptr = NULL}, + {.arg[0].sptr = "testing", .arg[1].sptr = "a/sub_directory/here"}, +}; +size_t numCases = sizeof(testCase) / sizeof(struct TestCase); + +int main(int argc, char *argv[]) { + // SPM initializes a series of global variables when the main program kicks off. + // We are not fully initializing SPM... + + TMP_DIR = calloc(PATH_MAX, sizeof(char)); + TMP_DIR[0] = '.'; + + for (size_t i = 0; i < numCases; i++) { + char *result = spm_mkdtemp(testCase[i].arg[0].sptr, testCase[i].arg[2].sptr); + myassert(result != NULL, "unexpected NULL\n"); + int present = access(result, F_OK); + myassert(present == 0, "%s: %s\n", result, strerror(errno)); + rmdirs(result); + free(result); + } + free(TMP_DIR); + return 0; +} \ No newline at end of file -- cgit