code: plan9front

Download patch

ref: 245264e3716c46a5278f705c90d9cb24afca0924
parent: dab168e6bd204c3cffae263d232051a47442c5ce
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Feb 20 15:49:03 EST 2021

upas/marshal: handle nonexistent save folder correctly (thanks sirjofri)

When the save folder did not exist, and we could not create
it, we would handle up to one Biobuf worth of message, and
then fail, due to a failed tee. The sequence of events leading
up to this was:

	openfolder() -> error
	tee(0, fd, -1) -> wait for read
	write(0, data) ->
		write(fd, data) -> ok
		write(-1, data) -> error, tee terminates
	write(0, attachment) -> error

This change prevents us from writing to a closed fd, and
therefore from erroring out when sending.

We also warn the user.

--- a/sys/src/cmd/upas/marshal/marshal.c
+++ b/sys/src/cmd/upas/marshal/marshal.c
@@ -985,10 +985,12 @@
 	int n;
 	char buf[8*1024];
 
-	while ((n = read(in, buf, sizeof buf)) > 0)
-		if (write(out1, buf, n) != n ||
-		    write(out2, buf, n) != n)
+	while ((n = read(in, buf, sizeof buf)) > 0){
+		if(out1 != -1 && write(out1, buf, n) != n)
 			break;
+		if(out2 != -1 && write(out2, buf, n) != n)
+			break;
+	}
 }
 
 /* print the unix from line */
@@ -1033,7 +1035,7 @@
 sendmail(Addr *to, Addr *cc, Addr *bcc, int *pid, char *rcvr)
 {
 	int ac, fd, pfd[2];
-	char **v, cmd[Pathlen];
+	char **v, *f, cmd[Pathlen];
 	Addr *a;
 	Biobuf *b;
 
@@ -1086,12 +1088,18 @@
 			case 0:
 				close(pfd[0]);
 				/* BOTCH; "From " time gets changed */
-				b = openfolder(foldername(nil, user, rcvr), time(0));
-				fd = b? Bfildes(b): -1;
-				printunixfrom(fd);
-				tee(0, pfd[1], fd);
-				write(fd, "\n", 1);
-				closefolder(b);
+				f = foldername(nil, user, rcvr);
+				b = openfolder(f, time(0));
+				if(b != nil){
+					fd = Bfildes(b);
+					printunixfrom(fd);
+					tee(0, pfd[1], fd);
+					write(fd, "\n", 1);
+					closefolder(b);
+				}else{
+					fprint(2, "warning: open %s: %r", f);
+					tee(0, pfd[1], -1);
+				}
 				exits(0);
 			default:
 				close(pfd[1]);
@@ -1172,6 +1180,7 @@
 
 	err = nil;
 	while((w = wait()) != nil){
+		fprint(2, "%d: %s\n", w->pid, w->msg);
 		if(w->pid == pid || w->pid == pgppid)
 			if(w->msg[0] != 0)
 				err = estrdup(w->msg);