changeset 22274:59406ffe10b1

lib-storage: Use file_lock_set_*_on_free() for .vsize.lock This avoids storing the lock_path and lock_fd separately.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 22 Jun 2017 02:05:16 +0300
parents a8b639453bee
children c4061e9cc721
files src/lib-storage/index/index-mailbox-size.c
diffstat 1 files changed, 13 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/index-mailbox-size.c	Thu Jun 22 01:56:18 2017 +0300
+++ b/src/lib-storage/index/index-mailbox-size.c	Thu Jun 22 02:05:16 2017 +0300
@@ -45,9 +45,8 @@
 	struct mail_index_view *view;
 	struct mailbox_index_vsize vsize_hdr, orig_vsize_hdr;
 
-	char *lock_path;
-	int lock_fd;
 	struct file_lock *lock;
+	bool lock_failed;
 	bool rebuild;
 	bool written;
 	bool finish_in_background;
@@ -116,7 +115,6 @@
 
 	update = i_new(struct mailbox_vsize_update, 1);
 	update->box = box;
-	update->lock_fd = -1;
 
 	vsize_header_refresh(update);
 	return update;
@@ -128,11 +126,13 @@
 	struct mailbox *box = update->box;
 	const struct mailbox_permissions *perm;
 	struct file_create_settings set;
-	const char *error;
+	const char *lock_path, *error;
 	bool created;
 
-	if (update->lock_path != NULL)
-		return update->lock != NULL;
+	if (update->lock != NULL)
+		return TRUE;
+	if (update->lock_failed)
+		return FALSE;
 	if (MAIL_INDEX_IS_IN_MEMORY(box->index))
 		return FALSE;
 
@@ -145,11 +145,9 @@
 	set.gid = perm->file_create_gid;
 	set.gid_origin = perm->file_create_gid_origin;
 
-	update->lock_path = i_strdup_printf("%s/"VSIZE_LOCK_SUFFIX,
-					    box->index->dir);
-	update->lock_fd = file_create_locked(update->lock_path, &set,
-					     &update->lock, &created, &error);
-	if (update->lock_fd == -1) {
+	lock_path = t_strdup_printf("%s/"VSIZE_LOCK_SUFFIX, box->index->dir);
+	if (file_create_locked(lock_path, &set, &update->lock,
+			       &created, &error) == -1) {
 		/* don't log lock timeouts, because we're somewhat expecting
 		   them. Especially when lock_secs is 0. */
 		if (errno != EAGAIN) {
@@ -157,8 +155,11 @@
 				"file_create_locked(%s) failed: %s",
 				update->lock_path, error);
 		}
+		update->lock_failed = TRUE;
 		return FALSE;
 	}
+	file_lock_set_close_on_free(update->lock, TRUE);
+	file_lock_set_unlink_on_free(update->lock, TRUE);
 	update->rebuild = FALSE;
 	vsize_header_refresh(update);
 	index_mailbox_vsize_check_rebuild(update);
@@ -241,17 +242,12 @@
 
 	if (update->lock != NULL || update->rebuild)
 		index_mailbox_vsize_update_write(update);
-	if (update->lock != NULL) {
-		if (unlink(update->lock_path) < 0)
-			i_error("unlink(%s) failed: %m", update->lock_path);
+	if (update->lock != NULL)
 		file_lock_free(&update->lock);
-		i_close_fd(&update->lock_fd);
-	}
 	if (update->finish_in_background)
 		index_mailbox_vsize_notify_indexer(update->box);
 
 	mail_index_view_close(&update->view);
-	i_free(update->lock_path);
 	i_free(update);
 }