about summary refs log tree commit diff
path: root/export.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--export.c42
1 files changed, 24 insertions, 18 deletions
diff --git a/export.c b/export.c
index f9f533e..2b579c3 100644
--- a/export.c
+++ b/export.c
@@ -28,7 +28,6 @@
 #include <err.h>
 #include <errno.h>
 #include <inttypes.h>
-#include <limits.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -40,8 +39,7 @@
 #include "archive.h"
 #include "imap.h"
 
-static const char *exportPath(uint32_t uid, const char *type) {
-	static char buf[PATH_MAX];
+static char *exportPath(uint32_t uid, const char *type) {
 	char str[32];
 	snprintf(str, sizeof(str), "%" PRIu32, uid);
 	struct Variable vars[] = {
@@ -49,8 +47,7 @@ static const char *exportPath(uint32_t uid, const char *type) {
 		{ "type", type },
 		{0},
 	};
-	templateBuffer(buf, sizeof(buf), PATH_UID, vars, escapePath);
-	return buf;
+	return templateString(PATH_UID, vars, escapePath);
 }
 
 bool exportFetch(FILE *imap, enum Atom tag, struct List threads) {
@@ -58,11 +55,17 @@ bool exportFetch(FILE *imap, enum Atom tag, struct List threads) {
 	listFlatten(&uids, threads);
 	for (size_t i = uids.len - 1; i < uids.len; --i) {
 		uint32_t uid = dataCheck(uids.ptr[i], Number).number;
+		char *atom = exportPath(uid, "atom");
+		char *html = exportPath(uid, "html");
+		char *mbox = exportPath(uid, "mbox");
 		int error = 0
-			|| access(exportPath(uid, "atom"), F_OK)
-			|| access(exportPath(uid, "html"), F_OK)
-			|| access(exportPath(uid, "mbox"), F_OK);
+			|| access(atom, F_OK)
+			|| access(html, F_OK)
+			|| access(mbox, F_OK);
 		if (!error) uids.ptr[i] = uids.ptr[--uids.len];
+		free(atom);
+		free(html);
+		free(mbox);
 	}
 	if (!uids.len) {
 		listFree(uids);
@@ -86,7 +89,7 @@ static void exportMbox(
 	uint32_t uid, const struct Envelope *envelope,
 	const char *header, const char *body
 ) {
-	const char *path = exportPath(uid, "mbox");
+	char *path = exportPath(uid, "mbox");
 	FILE *file = fopen(path, "w");
 	if (!file) err(EX_CANTCREAT, "%s", path);
 	int error = 0
@@ -96,17 +99,18 @@ static void exportMbox(
 		|| fclose(file);
 	if (error) err(EX_IOERR, "%s", path);
 
-	char buf[PATH_MAX];
 	struct Variable vars[] = {
 		{ "messageID", envelope->messageID },
 		{ "type", "mbox" },
 		{0},
 	};
-	templateBuffer(buf, sizeof(buf), PATH_MESSAGE, vars, escapePath);
+	char *dest = templateString(PATH_MESSAGE, vars, escapePath);
+	unlink(dest);
+	error = link(path, dest);
+	if (error) err(EX_CANTCREAT, "%s", dest);
 
-	unlink(buf);
-	error = link(path, buf);
-	if (error) err(EX_CANTCREAT, "%s", buf);
+	free(dest);
+	free(path);
 }
 
 static bool isInline(const struct BodyPart *part) {
@@ -124,7 +128,7 @@ static void exportAtom(
 	uint32_t uid, const struct Envelope *envelope,
 	const struct BodyPart *structure, struct Data body
 ) {
-	const char *path = exportPath(uid, "atom");
+	char *path = exportPath(uid, "atom");
 	FILE *file = fopen(path, "w");
 	if (!file) err(EX_CANTCREAT, "%s", path);
 
@@ -155,6 +159,7 @@ static void exportAtom(
 
 	error = atomEntryClose(file) || fclose(file);
 	if (error) err(EX_IOERR, "%s", path);
+	free(path);
 }
 
 static const char *sectionName(struct List section) {
@@ -181,7 +186,6 @@ static int exportHTMLAttachment(
 	const char *disposition = part->disposition.type;
 	if (!disposition) disposition = "INLINE";
 
-	char path[PATH_MAX];
 	struct Variable vars[] = {
 		{ "messageID", envelope->messageID },
 		{ "section", sectionName(section) },
@@ -191,7 +195,7 @@ static int exportHTMLAttachment(
 		{ "subtype", (name ? "" : part->subtype) },
 		{0},
 	};
-	templateBuffer(path, sizeof(path), PATH_ATTACHMENT, vars, escapePath);
+	char *path = templateString(PATH_ATTACHMENT, vars, escapePath);
 
 	for (char *ch = path; (ch = strchr(ch, '/')); ++ch) {
 		*ch = '\0';
@@ -207,6 +211,7 @@ static int exportHTMLAttachment(
 		|| decodeToFile(attachment, part, dataCheck(body, String).string)
 		|| fclose(attachment);
 	if (error) err(EX_IOERR, "%s", path);
+	free(path);
 
 	return htmlAttachment(file, part, vars);
 }
@@ -274,7 +279,7 @@ static void exportHTML(
 	uint32_t uid, const struct Envelope *envelope,
 	const struct BodyPart *structure, struct Data body
 ) {
-	const char *path = exportPath(uid, "html");
+	char *path = exportPath(uid, "html");
 	FILE *file = fopen(path, "w");
 	if (!file) err(EX_CANTCREAT, "%s", path);
 
@@ -288,6 +293,7 @@ static void exportHTML(
 
 	error = htmlMessageClose(file) || fclose(file);
 	if (error) err(EX_IOERR, "%s", path);
+	free(path);
 }
 
 static void fetchParts(