dovecot: Added file_dotlock_is_locked().

dovecot at dovecot.org dovecot at dovecot.org
Mon Jan 7 07:38:22 EET 2008


details:   http://hg.dovecot.org/dovecot/rev/9c2b65bfe604
changeset: 7134:9c2b65bfe604
user:      Timo Sirainen <tss at iki.fi>
date:      Mon Jan 07 07:37:37 2008 +0200
description:
Added file_dotlock_is_locked().

diffstat:

2 files changed, 28 insertions(+), 23 deletions(-)
src/lib/file-dotlock.c |   49 +++++++++++++++++++++++++-----------------------
src/lib/file-dotlock.h |    2 +

diffs (85 lines):

diff -r e0115302e68e -r 9c2b65bfe604 src/lib/file-dotlock.c
--- a/src/lib/file-dotlock.c	Mon Jan 07 07:37:12 2008 +0200
+++ b/src/lib/file-dotlock.c	Mon Jan 07 07:37:37 2008 +0200
@@ -728,7 +728,6 @@ int file_dotlock_replace(struct dotlock 
 			 enum dotlock_replace_flags flags)
 {
 	struct dotlock *dotlock;
-	struct stat st, st2;
 	const char *lock_path;
 	int fd;
 
@@ -740,28 +739,14 @@ int file_dotlock_replace(struct dotlock 
 		dotlock->fd = -1;
 
 	lock_path = file_dotlock_get_lock_path(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);
-			return -1;
-		}
-
-		if (nfs_safe_lstat(lock_path, &st2) < 0) {
-			i_error("lstat(%s) failed: %m", lock_path);
-			file_dotlock_free(&dotlock);
-			return -1;
-		}
-
-		if (st.st_ino != st2.st_ino ||
-		    !CMP_DEV_T(st.st_dev, st2.st_dev)) {
-			i_warning("Our dotlock file %s was overridden "
-				  "(kept it %d secs)", lock_path,
-				  (int)(time(NULL) - dotlock->lock_time));
-			errno = EEXIST;
-			file_dotlock_free(&dotlock);
-			return 0;
-		}
+	if ((flags & DOTLOCK_REPLACE_FLAG_VERIFY_OWNER) != 0 &&
+	    !file_dotlock_is_locked(dotlock)) {
+		i_warning("Our dotlock file %s was overridden "
+			  "(kept it %d secs)", lock_path,
+			  (int)(time(NULL) - dotlock->lock_time));
+		errno = EEXIST;
+		file_dotlock_free(&dotlock);
+		return 0;
 	}
 
 	if (rename(lock_path, dotlock->path) < 0) {
@@ -795,6 +780,24 @@ int file_dotlock_touch(struct dotlock *d
 	return ret;
 }
 
+bool file_dotlock_is_locked(struct dotlock *dotlock)
+{
+	struct stat st, st2;
+	const char *lock_path;
+
+	lock_path = file_dotlock_get_lock_path(dotlock);
+	if (fstat(dotlock->fd, &st) < 0) {
+		i_error("fstat(%s) failed: %m", lock_path);
+		return FALSE;
+	}
+
+	if (nfs_safe_lstat(lock_path, &st2) < 0) {
+		i_error("lstat(%s) failed: %m", lock_path);
+		return FALSE;
+	}
+	return st.st_ino == st2.st_ino && CMP_DEV_T(st.st_dev, st2.st_dev);
+}
+
 const char *file_dotlock_get_lock_path(struct dotlock *dotlock)
 {
 	if (dotlock->lock_path == NULL) {
diff -r e0115302e68e -r 9c2b65bfe604 src/lib/file-dotlock.h
--- a/src/lib/file-dotlock.h	Mon Jan 07 07:37:12 2008 +0200
+++ b/src/lib/file-dotlock.h	Mon Jan 07 07:37:37 2008 +0200
@@ -75,6 +75,8 @@ int file_dotlock_replace(struct dotlock 
    it's a good idea to update it once in a while so others won't override it.
    If the timestamp is less than a second old, it's not updated. */
 int file_dotlock_touch(struct dotlock *dotlock);
+/* Returns TRUE if the lock is still ok, FALSE if it's been overridden. */
+bool file_dotlock_is_locked(struct dotlock *dotlock);
 
 /* Returns the lock file path. */
 const char *file_dotlock_get_lock_path(struct dotlock *dotlock);


More information about the dovecot-cvs mailing list