瀏覽代碼

fixed maildir message filenames to comply with the maildir(5) specification.

fixed write_strip() and imap_fetch_message() to check the return code of
write() and fsync() to comply with maildir(5) spec.
Michael Elkins 24 年之前
父節點
當前提交
6f647ae37b
共有 2 個文件被更改,包括 26 次插入13 次删除
  1. 17 9
      imap.c
  2. 9 4
      sync.c

+ 17 - 9
imap.c

@@ -820,12 +820,18 @@ write_strip (int fd, char *buf, size_t len)
 {
     size_t start = 0;
     size_t end = 0;
+    int n;
 
     while (start < len)
     {
 	while (end < len && buf[end] != '\r')
 	    end++;
-	write (fd, buf + start, end - start);
+	n = write (fd, buf + start, end - start);
+	if (n == -1)
+	{
+	    perror ("write");
+	    return -1;
+	}
 	end++;
 	start = end;
     }
@@ -910,7 +916,6 @@ imap_fetch_message (imap_t * imap, unsigned int uid, int fd)
 		return -1;
 	    }
 	    bytes = strtol (arg + 1, 0, 10);
-//          printf ("receiving %d byte message\n", bytes);
 
 	    /* dump whats left over in the input buffer */
 	    n = imap->buf->bytes - imap->buf->offset;
@@ -924,12 +929,14 @@ imap_fetch_message (imap_t * imap, unsigned int uid, int fd)
 	    /* ick.  we have to strip out the \r\n line endings, so
 	     * i can't just dump the raw bytes to disk.
 	     */
-	    write_strip (fd, imap->buf->buf + imap->buf->offset, n);
+	    if (write_strip (fd, imap->buf->buf + imap->buf->offset, n))
+	    {
+		/* write failed, message is not delivered */
+		return -1;
+	    }
 
 	    bytes -= n;
 
-//          printf ("wrote %d buffered bytes\n", n);
-
 	    /* mark that we used part of the buffer */
 	    imap->buf->offset += n;
 
@@ -942,8 +949,11 @@ imap_fetch_message (imap_t * imap, unsigned int uid, int fd)
 		n = socket_read (imap->sock, buf, n);
 		if (n > 0)
 		{
-//                  printf("imap_fetch_message:%d:read %d bytes\n", __LINE__, n);
-		    write_strip (fd, buf, n);
+		    if (write_strip (fd, buf, n))
+		    {
+			/* write failed */
+			return -1;
+		    }
 		    bytes -= n;
 		}
 		else
@@ -953,8 +963,6 @@ imap_fetch_message (imap_t * imap, unsigned int uid, int fd)
 		}
 	    }
 
-//          puts ("finished fetching msg");
-
 	    buffer_gets (imap->buf, &cmd);
 	    if (Verbose)
 		puts (cmd);	/* last part of line */

+ 9 - 4
sync.c

@@ -243,9 +243,9 @@ sync_mailbox (mailbox_t * mbox, imap_t * imap, int flags,
 	    for (;;)
 	    {
 		/* create new file */
-		snprintf (path, sizeof (path), "%s/tmp/%s.%ld_%d.%d,U=%d%s",
-			  mbox->path, Hostname, time (0), MaildirCount++,
-			  getpid (), cur->uid, suffix);
+		snprintf (path, sizeof (path), "%s/tmp/%ld_%d.%d.%s,U=%d%s",
+			  mbox->path, time (0), MaildirCount++, getpid (),
+			  Hostname, cur->uid, suffix);
 
 		if ((fd = open (path, O_WRONLY | O_CREAT | O_EXCL, 0600)) > 0)
 		    break;
@@ -268,7 +268,12 @@ sync_mailbox (mailbox_t * mbox, imap_t * imap, int flags,
 
 	    ret = imap_fetch_message (imap, cur->uid, fd);
 
-	    if (close (fd))
+	    if (fsync (fd))
+	    {
+	    	perror ("fsync");
+		close (fd);
+	    }
+	    else if (close (fd))
 		perror ("close");
 	    else if (!ret)
 	    {