From 2060d70f0574019381cbf6f2c78db941f391224c Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Mon, 13 Apr 2020 12:00:17 -0400 Subject: Rework path functions again --- archive.h | 26 ++++++++++++-------------- concat.c | 52 ++++++++++++++++++++++++++-------------------------- export.c | 33 ++++++++++++++++----------------- 3 files changed, 54 insertions(+), 57 deletions(-) diff --git a/archive.h b/archive.h index ffe1561..6342236 100644 --- a/archive.h +++ b/archive.h @@ -143,10 +143,10 @@ int templateRender( ); char *templateURL(const char *template, const struct Variable vars[]); -static inline char * -pathUID(char path[static PATH_MAX], uint32_t uid, const char *type) { - snprintf(path, PATH_MAX, "UID/%" PRIu32 ".%s", uid, type); - return path; +static inline const char *pathUID(uint32_t uid, const char *type) { + static char buf[PATH_MAX]; + snprintf(buf, sizeof(buf), "UID/%" PRIu32 ".%s", uid, type); + return buf; } static inline const char *pathMangle(const char *messageID) { @@ -159,18 +159,16 @@ static inline const char *pathMangle(const char *messageID) { return buf; } -static inline char *pathMessage( - char path[static PATH_MAX], const char *messageID, const char *type -) { - snprintf(path, PATH_MAX, "message/%s.%s", pathMangle(messageID), type); - return path; +static inline const char *pathMessage(const char *messageID, const char *type) { + static char buf[PATH_MAX]; + snprintf(buf, sizeof(buf), "message/%s.%s", pathMangle(messageID), type); + return buf; } -static inline char *pathThread( - char path[static PATH_MAX], const char *messageID, const char *type -) { - snprintf(path, PATH_MAX, "thread/%s.%s", pathMangle(messageID), type); - return path; +static inline const char *pathThread(const char *messageID, const char *type) { + static char buf[PATH_MAX]; + snprintf(buf, sizeof(buf), "thread/%s.%s", pathMangle(messageID), type); + return buf; } #define MBOX_HEADERS \ diff --git a/concat.c b/concat.c index 5e8be3d..abf648c 100644 --- a/concat.c +++ b/concat.c @@ -53,14 +53,13 @@ void concatFetch(FILE *imap, enum Atom tag, struct List threads) { } static time_t uidNewest(struct List uids, const char *type) { - char path[PATH_MAX]; time_t newest = 0; for (size_t i = 0; i < uids.len; ++i) { - pathUID(path, dataCheck(uids.ptr[i], Number).number, type); - struct stat file; - int error = stat(path, &file); + const char *path = pathUID(dataCheck(uids.ptr[i], Number).number, type); + struct stat status; + int error = stat(path, &status); if (error) err(EX_DATAERR, "%s", path); - if (file.st_mtime > newest) newest = file.st_mtime; + if (status.st_mtime > newest) newest = status.st_mtime; } return newest; } @@ -78,7 +77,6 @@ static int concatFile(FILE *dst, const char *path) { } static int concatHTML(FILE *file, struct List thread) { - static char path[PATH_MAX]; int error; for (size_t i = 0; i < thread.len; ++i) { if (thread.ptr[i].type == List) { @@ -86,7 +84,7 @@ static int concatHTML(FILE *file, struct List thread) { } else { uint32_t uid = dataCheck(thread.ptr[i], Number).number; error = htmlThreadOpen(file) - || concatFile(file, pathUID(path, uid, "html")); + || concatFile(file, pathUID(uid, "html")); } if (error) return error; } @@ -121,47 +119,49 @@ void concatData(struct List threads, struct List items) { int error; FILE *file; + const char *path; struct stat status; - char dst[PATH_MAX]; - char src[PATH_MAX]; - error = stat(pathThread(dst, envelope.messageID, "mbox"), &status); + path = pathThread(envelope.messageID, "mbox"); + error = stat(path, &status); if (error || status.st_mtime < uidNewest(flat, "mbox")) { - file = fopen(dst, "w"); - if (!file) err(EX_CANTCREAT, "%s", dst); + file = fopen(path, "w"); + if (!file) err(EX_CANTCREAT, "%s", path); for (size_t i = 0; i < flat.len; ++i) { uint32_t uid = dataCheck(flat.ptr[i], Number).number; - error = concatFile(file, pathUID(src, uid, "mbox")); - if (error) err(EX_IOERR, "%s", dst); + error = concatFile(file, pathUID(uid, "mbox")); + if (error) err(EX_IOERR, "%s", path); } error = fclose(file); - if (error) err(EX_IOERR, "%s", dst); + if (error) err(EX_IOERR, "%s", path); } - error = stat(pathThread(dst, envelope.messageID, "atom"), &status); + path = pathThread(envelope.messageID, "atom"); + error = stat(path, &status); if (error || status.st_mtime < uidNewest(flat, "atom")) { - FILE *file = fopen(dst, "w"); - if (!file) err(EX_CANTCREAT, "%s", dst); + FILE *file = fopen(path, "w"); + if (!file) err(EX_CANTCREAT, "%s", path); error = atomFeedOpen(file, &envelope); - if (error) err(EX_IOERR, "%s", dst); + if (error) err(EX_IOERR, "%s", path); for (size_t i = 0; i < flat.len; ++i) { uint32_t uid = dataCheck(flat.ptr[i], Number).number; - error = concatFile(file, pathUID(src, uid, "atom")); - if (error) err(EX_IOERR, "%s", dst); + error = concatFile(file, pathUID(uid, "atom")); + if (error) err(EX_IOERR, "%s", path); } error = atomFeedClose(file) || fclose(file); - if (error) err(EX_IOERR, "%s", dst); + if (error) err(EX_IOERR, "%s", path); } - error = stat(pathThread(dst, envelope.messageID, "html"), &status); + path = pathThread(envelope.messageID, "html"); + error = stat(path, &status); if (error || status.st_mtime < uidNewest(flat, "html")) { - FILE *file = fopen(dst, "w"); - if (!file) err(EX_CANTCREAT, "%s", dst); + FILE *file = fopen(path, "w"); + if (!file) err(EX_CANTCREAT, "%s", path); error = 0 || htmlThreadHead(file, &envelope) // TODO: Include -h file. @@ -169,7 +169,7 @@ void concatData(struct List threads, struct List items) { || concatHTML(file, thread) || htmlThreadTail(file) || fclose(file); - if (error) err(EX_IOERR, "%s", dst); + if (error) err(EX_IOERR, "%s", path); } listFree(flat); diff --git a/export.c b/export.c index 8e97370..6327d22 100644 --- a/export.c +++ b/export.c @@ -16,7 +16,6 @@ #include #include -#include #include #include #include @@ -29,13 +28,12 @@ bool exportFetch(FILE *imap, enum Atom tag, struct List threads) { struct List uids = {0}; listFlatten(&uids, threads); - char path[PATH_MAX]; for (size_t i = uids.len - 1; i < uids.len; --i) { uint32_t uid = dataCheck(uids.ptr[i], Number).number; int error = 0 - || access(pathUID(path, uid, "atom"), F_OK) - || access(pathUID(path, uid, "html"), F_OK) - || access(pathUID(path, uid, "mbox"), F_OK); + || access(pathUID(uid, "atom"), F_OK) + || access(pathUID(uid, "html"), F_OK) + || access(pathUID(uid, "mbox"), F_OK); if (!error) uids.ptr[i] = uids.ptr[--uids.len]; } if (!uids.len) { @@ -58,9 +56,10 @@ bool exportFetch(FILE *imap, enum Atom tag, struct List threads) { static void exportEnvelope(uint32_t uid, const struct Envelope *envelope) { int error; FILE *file; - char path[PATH_MAX]; + const char *path; - file = fopen(pathUID(path, uid, "html"), "w"); + path = pathUID(uid, "html"); + file = fopen(path, "w"); if (!file) err(EX_CANTCREAT, "%s", path); error = 0 || htmlMessageHead(file, envelope) @@ -68,7 +67,8 @@ static void exportEnvelope(uint32_t uid, const struct Envelope *envelope) { || fclose(file); if (error) err(EX_IOERR, "%s", path); - file = fopen(pathUID(path, uid, "atom"), "w"); + path = pathUID(uid, "atom"); + file = fopen(path, "w"); if (!file) err(EX_CANTCREAT, "%s", path); error = 0 || atomEntryOpen(file, envelope) @@ -81,21 +81,20 @@ static void exportRaw( uint32_t uid, const struct Envelope *envelope, const char *header, const char *body ) { - char src[PATH_MAX]; - FILE *file = fopen(pathUID(src, uid, "mbox"), "w"); - if (!file) err(EX_CANTCREAT, "%s", src); + const char *path = pathUID(uid, "mbox"); + FILE *file = fopen(path, "w"); + if (!file) err(EX_CANTCREAT, "%s", path); int error = 0 || mboxFrom(file) || mboxHeader(file, header) || mboxBody(file, body) || fclose(file); - if (error) err(EX_IOERR, "%s", src); + if (error) err(EX_IOERR, "%s", path); - char dst[PATH_MAX]; - pathMessage(dst, envelope->messageID, "mbox"); - unlink(dst); - error = link(src, dst); - if (error) err(EX_CANTCREAT, "%s", dst); + const char *msg = pathMessage(envelope->messageID, "mbox"); + unlink(msg); + error = link(path, msg); + if (error) err(EX_CANTCREAT, "%s", msg); } static void exportBodyPart( -- cgit 1.4.1