tttm

git clone https://orangeshoelaces.net/git/tttm.git

21186a82dd98420edc0c9060639229a06fea48f3

Author: Vasily Kolobkov on 05/30/2016

Committer: Vasily Kolobkov on 05/30/2016

Store mail en route under /tmp

Stats

tttm.c | 19 +++++---
1 file changed, 12 insertions(+), 7 deletions(-)

Patch

diff --git a/tttm.c b/tttm.c
index 1c42bd0..0b37c7f 100644
--- a/tttm.c
+++ b/tttm.c
@@ -35,7 +35,8 @@ static void	twarn(int, const char *);
 
 extern char **environ;
 
-static size_t page;
+static size_t	page;
+static char	stortpl[] = "/tmp/tttm.XXXXXX";
 
 int
 main(int argc, char **argv)
@@ -43,7 +44,7 @@ main(int argc, char **argv)
 	int e;
 	struct imapctx con;
 	char *name, *pass, *sink;
-	int tmp;
+	int stor;
 	int refetchc, batchsz, mpiped;
 	struct msgd bag[BATCHSZ], *m, *bend;
 	long sv;
@@ -52,6 +53,11 @@ main(int argc, char **argv)
 		err(1, "failed to find out page size");
 	page = sv;
 
+	if ((stor = mkstemp(stortpl)) == -1)
+		err(1, "failed to set up temporary storage");
+	if (unlink(stortpl) == -1)
+		warn("failed to unlink temporary storage");
+
 	if ((e = imap_init(&con, STDIN_FILENO, STDOUT_FILENO, page))) {
 		terr(e, "failed to initialize session");
 	}
@@ -62,7 +68,6 @@ main(int argc, char **argv)
 	name = argv[1];
 	pass = argv[2];
 	sink = argv[3];
-	tmp = open("tmp", O_RDWR | O_CREAT, S_IRWXU);
 
 	e = imap_login(&con, name, pass);
 	if (e == TE_NO) {
@@ -81,13 +86,13 @@ main(int argc, char **argv)
 	}
 	refetchc = 0;
 	while (con.mb.exists > 0) {
-		if (lseek(tmp, 0, SEEK_SET) == -1) {
-			warn("failed to rewind temporary file");
+		if (lseek(stor, 0, SEEK_SET) == -1) {
+			warn("failed to rewind temporary storage");
 			goto logout;
 		}
 		batchsz = MIN(BATCHSZ, con.mb.exists);
 		bend = bag + batchsz;
-		e = imap_fetch(&con, 1, batchsz, tmp, bag);
+		e = imap_fetch(&con, 1, batchsz, stor, bag);
 		if (e == TE_NIL && refetchc < MAXREFC) {
 			if ((e = imap_nop(&con))) {
 				fin(&con, e, "failed to receive updates");
@@ -99,7 +104,7 @@ main(int argc, char **argv)
 		}
 		refetchc = 0;
 		for (m = bag; m < bend; m++) {
-			if (pipemsg(m, tmp, sink) == -1)
+			if (pipemsg(m, stor, sink) == -1)
 				break;
 		}
 		mpiped = m - bag;