[dovecot-cvs] dovecot/src/lib file-lock.c,1.3,1.4 file-lock.h,1.2,1.3

cras at procontrol.fi cras at procontrol.fi
Mon Nov 25 21:02:51 EET 2002


Update of /home/cvs/dovecot/src/lib
In directory danu:/tmp/cvs-serv2991/src/lib

Modified Files:
	file-lock.c file-lock.h 
Log Message:
Locking changes triggered a bit larger cleanup :) If we have to wait for a
lock longer, the client is now notified about it every 30 seconds. Also if
mailbox opening fails because of lock timeout, we won't overwrite the index
anymore. Finally user gets a clear error message about lock timeout instead
of "internal error".



Index: file-lock.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/file-lock.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- file-lock.c	28 Oct 2002 04:18:26 -0000	1.3
+++ file-lock.c	25 Nov 2002 19:02:49 -0000	1.4
@@ -30,11 +30,12 @@
 #include <time.h>
 #include <signal.h>
 
-static int file_lock(int fd, int wait_lock, int lock_type,
-		     unsigned int timeout)
+static int file_lock(int fd, int wait_lock, int lock_type, unsigned int timeout,
+		     void (*func)(unsigned int secs_left, void *context),
+		     void *context)
 {
 	struct flock fl;
-	time_t timeout_time;
+	time_t timeout_time, now;
 
 	if (timeout == 0)
 		timeout_time = 0;
@@ -55,10 +56,14 @@
 		if (errno != EINTR)
 			return -1;
 
-		if (timeout != 0 && time(NULL) >= timeout_time) {
+		now = time(NULL);
+		if (timeout != 0 && now >= timeout_time) {
 			errno = EAGAIN;
 			return 0;
 		}
+
+		if (func != NULL)
+			func(timeout_time - now, context);
 	}
 
 	return 1;
@@ -66,13 +71,18 @@
 
 int file_try_lock(int fd, int lock_type)
 {
-	return file_lock(fd, FALSE, lock_type, 0);
+	return file_lock(fd, FALSE, lock_type, 0, NULL, NULL);
 }
 
-int file_wait_lock(int fd, int lock_type, unsigned int timeout)
+int file_wait_lock(int fd, int lock_type)
 {
-	int ret;
+	return file_lock(fd, FALSE, lock_type, DEFAULT_LOCK_TIMEOUT,
+			 NULL, NULL);
+}
 
-	ret = file_lock(fd, TRUE, lock_type, timeout);
-	return ret;
+int file_wait_lock_full(int fd, int lock_type, unsigned int timeout,
+			void (*func)(unsigned int secs_left, void *context),
+			void *context)
+{
+	return file_lock(fd, TRUE, lock_type, timeout, func, context);
 }

Index: file-lock.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/file-lock.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- file-lock.h	23 Oct 2002 20:41:35 -0000	1.2
+++ file-lock.h	25 Nov 2002 19:02:49 -0000	1.3
@@ -11,8 +11,14 @@
 int file_try_lock(int fd, int lock_type);
 
 /* Lock whole file descriptor. Returns 1 if successful, 0 if timeout or
-   -1 if error. When returning 0, errno is also set to EAGAIN.
-   NOTE: timeout uses SIGALRM and resets it at the end. */
-int file_wait_lock(int fd, int lock_type, unsigned int timeout);
+   -1 if error. When returning 0, errno is also set to EAGAIN. Timeouts after
+   DEFAULT_LOCK_TIMEOUT. */
+int file_wait_lock(int fd, int lock_type);
+
+/* Like file_wait_lock(), but you can specify the timout and a function which
+   is called once in a while if waiting takes longer. */
+int file_wait_lock_full(int fd, int lock_type, unsigned int timeout,
+			void (*func)(unsigned int secs_left, void *context),
+			void *context);
 
 #endif




More information about the dovecot-cvs mailing list