changeset 30:8509281fda72 HEAD

Make sure mbox file ends with \n when appending new mail to it
author Timo Sirainen <tss@iki.fi>
date Mon, 26 Aug 2002 02:54:19 +0300
parents e9375147c0cb
children 33e86ee7d167
files src/lib-storage/index/mbox/mbox-save.c
diffstat 1 files changed, 27 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/mbox/mbox-save.c	Mon Aug 26 02:46:59 2002 +0300
+++ b/src/lib-storage/index/mbox/mbox-save.c	Mon Aug 26 02:54:19 2002 +0300
@@ -81,10 +81,33 @@
 					  ibox->index->mbox_path);
 		failed = TRUE;
 	} else {
-		if (!write_from_line(box->storage, fd, internal_date) ||
-		    !index_storage_save_into_fd(box->storage, fd,
-						ibox->index->mbox_path,
-						data, data_size)) {
+		if (pos > 0) {
+			/* make sure the file ends with \n */
+			if (lseek(fd, 0, pos-1) != pos-1)
+				failed = TRUE;
+			else {
+				char ch;
+
+				if (read(fd, &ch, 1) != 1)
+					failed = TRUE;
+				else if (ch != '\n') {
+					if (write_full(fd, &ch, 1) < 0)
+						failed = TRUE;
+				}
+			}
+		}
+
+		if (failed) {
+			/* don't bother separating the errors, it's very
+			   unlikely that this will happen */
+			mail_storage_set_critical(box->storage,
+						  "Error appending LF to mbox "
+						  "file %s: %m",
+						  ibox->index->mbox_path);
+		} else if (!write_from_line(box->storage, fd, internal_date) ||
+			   !index_storage_save_into_fd(box->storage, fd,
+						       ibox->index->mbox_path,
+						       data, data_size)) {
 			/* failed, truncate file back to original size */
 			(void)ftruncate(fd, pos);
 			failed = TRUE;