changeset 476:c519f9f6ae65 HEAD

cleanups, also file_wait_lock() return value wasn't checked correctly
author Timo Sirainen <tss@iki.fi>
date Mon, 21 Oct 2002 18:01:56 +0300
parents 584aae46e2ca
children 6617a4d1c424
files src/lib-storage/subscription-file/subscription-file.c
diffstat 1 files changed, 27 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/subscription-file/subscription-file.c	Mon Oct 21 04:56:20 2002 +0300
+++ b/src/lib-storage/subscription-file/subscription-file.c	Mon Oct 21 18:01:56 2002 +0300
@@ -15,6 +15,17 @@
 
 #define SUBSCRIPTION_FILE_NAME ".subscriptions"
 
+static int subsfile_set_syscall_error(MailStorage *storage, const char *path,
+				      const char *function)
+{
+	i_assert(function != NULL);
+
+	mail_storage_set_critical(storage,
+				  "%s failed with subscription file %s: %m",
+				  function, path);
+	return FALSE;
+}
+
 static int subscription_open(MailStorage *storage, int update,
 			     const char **path, void **mmap_base,
 			     size_t *mmap_length)
@@ -26,18 +37,13 @@
 	fd = update ? open(*path, O_RDWR | O_CREAT, 0660) :
 		open(*path, O_RDONLY);
 	if (fd == -1) {
-		if (update || errno != ENOENT) {
-			mail_storage_set_critical(storage, "Can't open "
-						  "subscription file %s: %m",
-						  *path);
-		}
+		if (update || errno != ENOENT)
+                        subsfile_set_syscall_error(storage, "open()", *path);
 		return -1;
 	}
 
-	if (!file_wait_lock(fd, update ? F_WRLCK : F_RDLCK)) {
-		mail_storage_set_critical(storage, "file_wait_lock() failed "
-					  "for subscription file %s: %m",
-					  *path);
+	if (file_wait_lock(fd, update ? F_WRLCK : F_RDLCK) < 0) {
+		subsfile_set_syscall_error(storage, "file_wait_lock()", *path);
 		(void)close(fd);
 		return -1;
 	}
@@ -46,8 +52,7 @@
 		mmap_ro_file(fd, mmap_length);
 	if (*mmap_base == MAP_FAILED) {
 		*mmap_base = NULL;
-		mail_storage_set_critical(storage, "mmap() failed for "
-					  "subscription file %s: %m", *path);
+		subsfile_set_syscall_error(storage, "mmap()", *path);
 		(void)close(fd);
 		return -1;
 	}
@@ -61,11 +66,8 @@
 {
 	char *buf;
 
-	if (lseek(fd, 0, SEEK_END) == -1) {
-		mail_storage_set_critical(storage, "lseek() failed for "
-					  "subscription file %s: %m", path);
-		return FALSE;
-	}
+	if (lseek(fd, 0, SEEK_END) < 0)
+		return subsfile_set_syscall_error(storage, "lseek()", path);
 
 	buf = t_buffer_get(len+2);
 	buf[0] = '\n';
@@ -80,8 +82,7 @@
 	}
 
 	if (write_full(fd, buf, len) < 0) {
-		mail_storage_set_critical(storage, "write() failed for "
-					  "subscription file %s: %m", path);
+		subsfile_set_syscall_error(storage, "write_full()", path);
 		return FALSE;
 	}
 
@@ -135,9 +136,8 @@
 			memmove(p, p+removelen, afterlen-removelen);
 
 		if (ftruncate(fd, (off_t) (mmap_length - removelen)) == -1) {
-			mail_storage_set_critical(storage, "ftruncate() "
-						  "failed for subscription "
-						  "file %s: %m", path);
+			subsfile_set_syscall_error(storage, "ftruncate()",
+						   path);
 			failed = TRUE;
 		}
 	} else if (p == NULL && set) {
@@ -149,13 +149,15 @@
 			failed = TRUE;
 	}
 
-	if (mmap_base != NULL && munmap(mmap_base, mmap_length) == -1) {
-		mail_storage_set_critical(storage, "munmap() failed for "
-					  "subscription file %s: %m", path);
+	if (mmap_base != NULL && munmap(mmap_base, mmap_length) < 0) {
+		subsfile_set_syscall_error(storage, "munmap()", path);
 		failed = TRUE;
 	}
 
-	(void)close(fd);
+	if (close(fd) < 0) {
+		subsfile_set_syscall_error(storage, "close()", path);
+		failed = TRUE;
+	}
 	return !failed;
 }