diff --git a/imap.c b/imap.c index 00f9159..65510d3 100644 --- a/imap.c +++ b/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 */ diff --git a/sync.c b/sync.c index c447176..3b0c6c7 100644 --- a/sync.c +++ b/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) {