git » libfilo » commit 4652485

Implement filo_plockf().

author Alberto Bertogli
2005-04-15 22:49:43 UTC
committer Alberto Bertogli
2005-04-15 22:49:43 UTC
parent e51bca2a72b27d66531cdc3423b5fb98b7b7ecf6

Implement filo_plockf().

libfilo.c +30 -0
libfilo.h +13 -0

diff --git a/libfilo.c b/libfilo.c
index 084c47b..ad67b57 100644
--- a/libfilo.c
+++ b/libfilo.c
@@ -9,6 +9,7 @@
 #include <pthread.h>		/* for mutexes */
 #include <semaphore.h>		/* for semaphores (duh!) */
 #include <stdlib.h>		/* for malloc()/free() */
+#include <unistd.h>		/* for lockf() commands */
 #include "libfilo.h"
 
 
@@ -454,3 +455,32 @@ int filo_unlock(filock_t *fl, off_t start, off_t len)
 }
 
 
+int filo_plockf(filock_t *fl, int cmd, off_t start, off_t len)
+{
+	int mode, rv;
+	off_t end = start + len;
+
+	if (cmd & _FL_READ)
+		mode = FL_RD_MODE;
+	else if (cmd & _FL_WRITE)
+		mode = FL_WR_MODE;
+	else
+		return -1;
+
+	if (cmd & _FL_LOCK)
+		rv = filo_lock(fl, start, len, mode);
+	else if (cmd & _FL_TLOCK)
+		rv = filo_trylock(fl, start, len, mode);
+	else if (cmd & _FL_ULOCK)
+		rv = filo_unlock(fl, start, end);
+	else
+		return -1;
+
+	if (rv == 0)
+		return -1;
+	else
+		return 0;
+
+	return -1;
+}
+
diff --git a/libfilo.h b/libfilo.h
index 79d1e08..62a5717 100644
--- a/libfilo.h
+++ b/libfilo.h
@@ -18,6 +18,19 @@
 #define FL_RD_MODE 0
 #define FL_WR_MODE 1
 
+/* locking modes for filo_plockf() */
+#define _FL_READ	0x00001
+#define _FL_WRITE	0x00010
+#define _FL_LOCK	0x00100
+#define _FL_TLOCK	0x01000
+#define _FL_ULOCK	0x10000
+
+#define FL_LOCKR	(_FL_LOCK | _FL_READ)
+#define FL_LOCKW	(_FL_LOCK | _FL_WRITE)
+#define FL_TLOCKR	(_FL_TLOCK | _FL_READ)
+#define FL_TLOCKW	(_FL_TLOCK | _FL_WRITE)
+#define FL_ULOCK	(_FL_ULOCK)
+
 
 /* a single locked range */
 struct locked_range {