From 61d75021c8a1180d2b591e70c783bd2eb21188bd Mon Sep 17 00:00:00 2001 From: Erik K Date: Thu, 26 May 2022 16:31:27 +0000 Subject: mkdir parent directories when needed, also fixed bug in list(). --- Makefile | 2 +- posts.c | 10 ++++++++-- rss.c | 10 +++++++++- skull.c | 2 ++ sm.c | 2 ++ sm.conf | 10 +++++----- util.c | 24 ++++++++++++++++++++++++ util.h | 1 + 8 files changed, 52 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 2dd9aca..9c221a9 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ sm: ${OBJ} ${OBJ}: config.h plugins.h -plugins.h: ${PLUGINS} +plugins.h: config.mk printf '%s\n' ${PLUGINS:.c=} | awk -f genplugin.awk install: diff --git a/posts.c b/posts.c index 239f118..04535b9 100644 --- a/posts.c +++ b/posts.c @@ -1,4 +1,5 @@ -#include +#include +#include #include #include @@ -71,7 +72,12 @@ href=\"mailto:erikk@previousplan.org\">erikk@previousplan.org \ qsort(doc, ndoc, sizeof(*doc), (int (*)(const void *, const void *))creatcompar); - outf = xfopen(kpostsexport, "w"); + if (!mkparentdirs(kpostsexport)) + return; + if ((outf = fopen(kpostsexport, "w")) == NULL) { + fprintf(stderr, "%s: %s\n", kpostsexport, strerror(errno)); + return; + } xfputs(header, outf); for (i = 0; i < ndoc; i++) { my_strlcpy(title, doc[i]->title, sizeof(title)); diff --git a/rss.c b/rss.c index e0f22fe..b175441 100644 --- a/rss.c +++ b/rss.c @@ -1,4 +1,7 @@ +#include +#include #include +#include #include "config.h" #include "sm.h" @@ -61,7 +64,12 @@ rssexport(Document **doc, int ndoc) qsort(doc, ndoc, sizeof(*doc), (int (*)(const void *, const void *))creatcompar); - outf = xfopen(krssexport, "w"); + if (!mkparentdirs(krssexport)) + return; + if ((outf = fopen(krssexport, "w")) == NULL) { + fprintf(stderr, "%s: %s\n", krssexport, strerror(errno)); + return; + } for (j = 0; j < LEN(header); j++) xfputs(header[j], outf); for (i = 0; i < ndoc; i++) { diff --git a/skull.c b/skull.c index 5c44f20..4c66833 100644 --- a/skull.c +++ b/skull.c @@ -64,6 +64,8 @@ href=\"mailto:erikk@previousplan.org\">erikk@previousplan.org \ snprintf(path, sizeof(path), "%s/%s", kcontentdir, doc[i]->filename); snprintf(outpath, sizeof(outpath), "%s/%s", kskullexport, doc[i]->filename); + if (!mkparentdirs(outpath)) + continue; if ((outf = fopen(outpath, "w")) == NULL) { fprintf(stderr, "%s: %s\n", outpath, strerror(errno)); continue; diff --git a/sm.c b/sm.c index 59e286e..d45ac85 100644 --- a/sm.c +++ b/sm.c @@ -339,6 +339,7 @@ edit(const char *filename) return 0; } snprintf(path, sizeof(path), "%s/%s", kcontentdir, filename); + mkparentdirs(path); switch ((pid = fork())) { case -1: fprintf(stderr, "fork: %s\n", strerror(errno)); @@ -362,6 +363,7 @@ list(const char *filename) plugstats[0] = '['; plugstats[NPLUGINS+1] = ']'; + plugstats[NPLUGINS+2] = '\0'; if (filename) { snprintf(path, sizeof(path), "%s/%s", kcontentdir, filename); cat(path, stdout); diff --git a/sm.conf b/sm.conf index 63a1352..a6c0946 100644 --- a/sm.conf +++ b/sm.conf @@ -1,12 +1,12 @@ -DbLoc ~/sites/sm/previousplan.org.tsv -ContentDir ~/sites/sm/previousplan.org +DbLoc sm.tsv +ContentDir content LinkPreset https://previousplan.org/% Autoedit True Autoexport True -SkullExport ~/sites/previousplan.org +SkullExport export RssTitle Previous Plan! RssDescription Previous Plan! Blog RssLink https://previousplan.org/rss.xml -RssExport ~/sites/previousplan.org/rss.xml -PostsExport ~/sites/previousplan.org/posts.html +RssExport export/rss.xml +PostsExport export/posts.html diff --git a/util.c b/util.c index 3f4f51d..50a7116 100644 --- a/util.c +++ b/util.c @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -65,6 +67,28 @@ xrealloc(void *ptr, size_t size) return n; } +int +mkparentdirs(const char *path) +{ + char part[512]; + int i; + + if (!path[0]) + return 0; + my_strlcpy(part, path, sizeof(part)); + for (i = 1; part[i]; i++) { + if (part[i] == '/' && part[i + 1] != '/' && part[i + 1]) { + part[i] = '\0'; + if (mkdir(part, 0755) < 0 && errno != EEXIST) { + fprintf(stderr, "mkdir %s: %s\n", part, strerror(errno)); + return 0; + } + part[i] = '/'; + } + } + return 1; +} + /* strlcpy is unportable, and strncpy is a mess. So we define our own strlcpy * instead. */ size_t diff --git a/util.h b/util.h index e1e49e7..eb74034 100644 --- a/util.h +++ b/util.h @@ -6,4 +6,5 @@ FILE *xfopen(const char *, const char *); int xfputs(const char *, FILE *); void *xmalloc(size_t); void *xrealloc(void *, size_t); +int mkparentdirs(const char *); size_t my_strlcpy(char *, const char *, size_t); -- cgit v1.2.3