dovecot: Code cleanups

dovecot at dovecot.org dovecot at dovecot.org
Wed Dec 5 17:47:53 EET 2007


details:   http://hg.dovecot.org/dovecot/rev/94e94e3b5b30
changeset: 6937:94e94e3b5b30
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Dec 05 17:40:28 2007 +0200
description:
Code cleanups

diffstat:

1 file changed, 49 insertions(+), 50 deletions(-)
src/lib/file-dotlock.c |   99 +++++++++++++++++++++++-------------------------

diffs (242 lines):

diff -r aacc69cc267b -r 94e94e3b5b30 src/lib/file-dotlock.c
--- a/src/lib/file-dotlock.c	Wed Dec 05 17:22:21 2007 +0200
+++ b/src/lib/file-dotlock.c	Wed Dec 05 17:40:28 2007 +0200
@@ -430,7 +430,8 @@ static void dotlock_wait(struct lock_inf
 }
 
 static int dotlock_create(const char *path, struct dotlock *dotlock,
-			  enum dotlock_create_flags flags, bool write_pid)
+			  enum dotlock_create_flags flags, bool write_pid,
+			  const char **lock_path_r)
 {
 	const struct dotlock_settings *set = &dotlock->settings;
 	const char *lock_path;
@@ -445,7 +446,7 @@ static int dotlock_create(const char *pa
 
 	now = time(NULL);
 
-	lock_path = t_strconcat(path, set->lock_suffix, NULL);
+	lock_path = *lock_path_r = t_strconcat(path, set->lock_suffix, NULL);
 	stale_notify_threshold = set->stale_timeout / 2;
 	max_wait_time = (flags & DOTLOCK_CREATE_FLAG_NONBLOCK) != 0 ? 0 :
 		now + set->timeout;
@@ -488,7 +489,6 @@ static int dotlock_create(const char *pa
 			change_secs = now - lock_info.last_change;
 			wait_left = max_wait_time - now;
 
-			t_push();
 			if (change_secs >= stale_notify_threshold &&
 			    change_secs <= wait_left) {
 				unsigned int secs_left =
@@ -503,7 +503,6 @@ static int dotlock_create(const char *pa
 				(void)set->callback(wait_left, FALSE,
 						    set->context);
 			}
-			t_pop();
 		}
 
 		now = time(NULL);
@@ -550,9 +549,12 @@ static int dotlock_create(const char *pa
 	return ret;
 }
 
-static void file_dotlock_free(struct dotlock *dotlock)
-{
+static void file_dotlock_free(struct dotlock **_dotlock)
+{
+	struct dotlock *dotlock = *_dotlock;
 	int old_errno;
+
+	*_dotlock = NULL;
 
 	if (dotlock->fd != -1) {
 		old_errno = errno;
@@ -567,35 +569,22 @@ static void file_dotlock_free(struct dot
 	i_free(dotlock);
 }
 
-int file_dotlock_create(const struct dotlock_settings *set, const char *path,
-			enum dotlock_create_flags flags,
-			struct dotlock **dotlock_r)
-{
-	struct dotlock *dotlock;
+static int file_dotlock_create_real(struct dotlock *dotlock, const char *path,
+				    enum dotlock_create_flags flags)
+{
 	const char *lock_path;
 	struct stat st;
 	int fd, ret;
 
-	*dotlock_r = NULL;
-
-	t_push();
-	dotlock = file_dotlock_alloc(set);
-	lock_path = t_strconcat(path, dotlock->settings.lock_suffix, NULL);
-
-	ret = dotlock_create(path, dotlock, flags, TRUE);
-	if (ret <= 0 || (flags & DOTLOCK_CREATE_FLAG_CHECKONLY) != 0) {
-		file_dotlock_free(dotlock);
-		t_pop();
+	ret = dotlock_create(path, dotlock, flags, TRUE, &lock_path);
+	if (ret <= 0 || (flags & DOTLOCK_CREATE_FLAG_CHECKONLY) != 0)
 		return ret;
-	}
 
 	fd = dotlock->fd;
 	dotlock->fd = -1;
 
 	if (close(fd) < 0) {
 		i_error("close(%s) failed: %m", lock_path);
-		file_dotlock_free(dotlock);
-		t_pop();
 		return -1;
 	}
 
@@ -609,8 +598,6 @@ int file_dotlock_create(const struct dot
 			i_error("dotlock %s was immediately deleted under us",
 				lock_path);
 		}
-                file_dotlock_free(dotlock);
-		t_pop();
 		return -1;
 	}
 	/* extra sanity check won't hurt.. */
@@ -618,15 +605,28 @@ int file_dotlock_create(const struct dot
 		errno = ENOENT;
 		i_error("dotlock %s was immediately recreated under us",
 			lock_path);
-                file_dotlock_free(dotlock);
-		t_pop();
 		return -1;
 	}
 	dotlock->mtime = st.st_mtime;
+	return 1;
+}
+
+int file_dotlock_create(const struct dotlock_settings *set, const char *path,
+			enum dotlock_create_flags flags,
+			struct dotlock **dotlock_r)
+{
+	struct dotlock *dotlock;
+	int ret;
+
+	dotlock = file_dotlock_alloc(set);
+	t_push();
+	ret = file_dotlock_create_real(dotlock, path, flags);
+	t_pop();
+	if (ret <= 0 || (flags & DOTLOCK_CREATE_FLAG_CHECKONLY) != 0)
+		file_dotlock_free(&dotlock);
 
 	*dotlock_r = dotlock;
-	t_pop();
-	return 1;
+	return ret;
 }
 
 int file_dotlock_delete(struct dotlock **dotlock_p)
@@ -644,12 +644,12 @@ int file_dotlock_delete(struct dotlock *
 			i_warning("Our dotlock file %s was deleted "
 				  "(kept it %d secs)", lock_path,
 				  (int)(time(NULL) - dotlock->lock_time));
-			file_dotlock_free(dotlock);
+			file_dotlock_free(&dotlock);
 			return 0;
 		}
 
 		i_error("lstat(%s) failed: %m", lock_path);
-		file_dotlock_free(dotlock);
+		file_dotlock_free(&dotlock);
 		return -1;
 	}
 
@@ -659,7 +659,7 @@ int file_dotlock_delete(struct dotlock *
 			  "(kept it %d secs)", lock_path,
 			  (int)(dotlock->lock_time - time(NULL)));
 		errno = EEXIST;
-		file_dotlock_free(dotlock);
+		file_dotlock_free(&dotlock);
 		return 0;
 	}
 
@@ -676,16 +676,16 @@ int file_dotlock_delete(struct dotlock *
 			i_warning("Our dotlock file %s was deleted "
 				  "(kept it %d secs)", lock_path,
 				  (int)(time(NULL) - dotlock->lock_time));
-			file_dotlock_free(dotlock);
+			file_dotlock_free(&dotlock);
 			return 0;
 		}
 
 		i_error("unlink(%s) failed: %m", lock_path);
-		file_dotlock_free(dotlock);
-		return -1;
-	}
-
-	file_dotlock_free(dotlock);
+		file_dotlock_free(&dotlock);
+		return -1;
+	}
+
+	file_dotlock_free(&dotlock);
 	return 1;
 }
 
@@ -694,16 +694,15 @@ int file_dotlock_open(const struct dotlo
 		      struct dotlock **dotlock_r)
 {
 	struct dotlock *dotlock;
+	const char *lock_path;
 	int ret;
 
 	dotlock = file_dotlock_alloc(set);
-
 	t_push();
-	ret = dotlock_create(path, dotlock, flags, FALSE);
+	ret = dotlock_create(path, dotlock, flags, FALSE, &lock_path);
 	t_pop();
-
 	if (ret <= 0) {
-		file_dotlock_free(dotlock);
+		file_dotlock_free(&dotlock);
 		*dotlock_r = NULL;
 		return -1;
 	}
@@ -731,13 +730,13 @@ int file_dotlock_replace(struct dotlock 
 	if ((flags & DOTLOCK_REPLACE_FLAG_VERIFY_OWNER) != 0) {
 		if (fstat(fd, &st) < 0) {
 			i_error("fstat(%s) failed: %m", lock_path);
-			file_dotlock_free(dotlock);
+			file_dotlock_free(&dotlock);
 			return -1;
 		}
 
 		if (nfs_safe_lstat(lock_path, &st2) < 0) {
 			i_error("lstat(%s) failed: %m", lock_path);
-			file_dotlock_free(dotlock);
+			file_dotlock_free(&dotlock);
 			return -1;
 		}
 
@@ -747,17 +746,17 @@ int file_dotlock_replace(struct dotlock 
 				  "(kept it %d secs)", lock_path,
 				  (int)(time(NULL) - dotlock->lock_time));
 			errno = EEXIST;
-			file_dotlock_free(dotlock);
+			file_dotlock_free(&dotlock);
 			return 0;
 		}
 	}
 
 	if (rename(lock_path, dotlock->path) < 0) {
 		i_error("rename(%s, %s) failed: %m", lock_path, dotlock->path);
-		file_dotlock_free(dotlock);
-		return -1;
-	}
-	file_dotlock_free(dotlock);
+		file_dotlock_free(&dotlock);
+		return -1;
+	}
+	file_dotlock_free(&dotlock);
 	return 1;
 }
 


More information about the dovecot-cvs mailing list