WIN32 files lock support.
authorAdam Dickmeiss <adam@indexdata.dk>
Thu, 25 Sep 1997 14:54:43 +0000 (14:54 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Thu, 25 Sep 1997 14:54:43 +0000 (14:54 +0000)
index/index.h
index/lockidx.c
index/locksrv.c
index/lockutil.c

index ebbbeb2..31fe3b3 100644 (file)
@@ -4,7 +4,10 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: index.h,v $
- * Revision 1.52  1997-09-22 12:39:06  adam
+ * Revision 1.53  1997-09-25 14:54:43  adam
+ * WIN32 files lock support.
+ *
+ * Revision 1.52  1997/09/22 12:39:06  adam
  * Added get_pos method for the ranked result sets.
  *
  * Revision 1.51  1997/09/18 08:59:19  adam
@@ -302,9 +305,13 @@ int zebraIndexWait (int commitPhase);
 #define FNAME_ORG_LOCK    "zebraorg.LCK"
 #define FNAME_TOUCH_TIME  "zebraidx.time"
 
-int zebraLock (int fd, int wr);
-int zebraLockNB (int fd, int wr);
-int zebraUnlock (int fd);
+typedef struct zebra_lock_info *ZebraLockHandle;
+ZebraLockHandle zebra_lock_create(const char *file, int excl_flag);
+void zebra_lock_destroy (ZebraLockHandle h);
+int zebra_lock (ZebraLockHandle h);
+int zebra_lock_nb (ZebraLockHandle h);
+int zebra_unlock (ZebraLockHandle h);
+int zebra_lock_fd (ZebraLockHandle h);
 
 void init_charmap(Res res);
 const char **map_chrs_input(void *vp, const char **from, int len);
index 10be062..9cc80c2 100644 (file)
@@ -4,7 +4,10 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: lockidx.c,v $
- * Revision 1.11  1997-09-17 12:19:15  adam
+ * Revision 1.12  1997-09-25 14:54:43  adam
+ * WIN32 files lock support.
+ *
+ * Revision 1.11  1997/09/17 12:19:15  adam
  * Zebra version corresponds to YAZ version 1.4.
  * Changed Zebra server so that it doesn't depend on global common_resource.
  *
 
 #include "index.h"
 
-static int lock_fd = -1;
-static int server_lock_cmt = -1;
-static int server_lock_org = -1;
+static ZebraLockHandle server_lock_main = NULL;
+static ZebraLockHandle server_lock_cmt = NULL;
+static ZebraLockHandle server_lock_org = NULL;
 
 int zebraIndexWait (int commitPhase)
 {
     char pathPrefix[1024];
     char path[1024];
-    int fd;
+    ZebraLockHandle h;
     
     zebraLockPrefix (common_resource, pathPrefix);
 
-    if (server_lock_cmt == -1)
+    if (server_lock_cmt)
+       zebra_unlock (server_lock_cmt);
+    else
     {
-        sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK);
-        if ((server_lock_cmt = open (path, O_BINARY|O_CREAT|O_RDWR|O_SYNC,
-                                     0666))
-            == -1)
-        {
-            logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
-            return -1;
-        }
+       sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK);
+       server_lock_cmt = zebra_lock_create (path, 1);
+       if (!server_lock_cmt)
+       {
+           logf (LOG_WARN|LOG_ERRNO, "cannot create lock %s", path);
+           return -1;
+       }
     }
+    if (server_lock_org)
+       zebra_unlock (server_lock_org);
     else
-        zebraUnlock (server_lock_cmt);
-    if (server_lock_org == -1)
     {
         sprintf (path, "%s%s", pathPrefix, FNAME_ORG_LOCK);
-        if ((server_lock_org = open (path, O_BINARY|O_CREAT|O_RDWR|O_SYNC,
-                                     0666))
-            == -1)
-        {
-            logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
-            return -1;
-        }
+       server_lock_org = zebra_lock_create (path, 1);
+       if (!server_lock_org)
+       {
+           logf (LOG_WARN|LOG_ERRNO, "cannot create lock %s", path);
+           return -1;
+       }
     }
-    else
-        zebraUnlock (server_lock_org);
     if (commitPhase)
-        fd = server_lock_cmt;
+        h = server_lock_cmt;
     else
-        fd = server_lock_org;
-    if (zebraLockNB (fd, 1) == -1)
+        h = server_lock_org;
+    if (zebra_lock_nb (h))
     {
+#ifndef WINDOWS
         if (errno != EWOULDBLOCK)
         {
             logf (LOG_FATAL|LOG_ERRNO, "flock");
             exit (1);
         }
+#endif
         if (commitPhase)
             logf (LOG_LOG, "Waiting for lock cmt");
         else
             logf (LOG_LOG, "Waiting for lock org");
-        if (zebraLock (fd, 1) == -1)
+        if (zebra_lock (h) == -1)
         {
-            logf (LOG_FATAL|LOG_ERRNO, "flock");
+            logf (LOG_FATAL, "flock");
             exit (1);
         }
     }
-    zebraUnlock (fd);
+    zebra_unlock (h);
     return 0;
 }
 
@@ -130,10 +133,11 @@ void zebraIndexLockMsg (const char *str)
     char pathPrefix[1024];
     int l, r, fd;
 
-    assert (lock_fd != -1);
-    lseek (lock_fd, 0L, SEEK_SET);
+    assert (server_lock_main);
+    fd = zebra_lock_fd (server_lock_main);
+    lseek (fd, 0L, SEEK_SET);
     l = strlen(str);
-    r = write (lock_fd, str, l);
+    r = write (fd, str, l);
     if (r != l)
     {
         logf (LOG_FATAL|LOG_ERRNO, "write lock file");
@@ -162,31 +166,38 @@ void zebraIndexLock (BFiles bfs, int commitNow, const char *rval)
     char buf[256];
     int r;
 
-    if (lock_fd != -1)
+    if (server_lock_main)
         return ;
     zebraLockPrefix (common_resource, pathPrefix);
     sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
     while (1)
     {
-        lock_fd = open (path, O_BINARY|O_CREAT|O_RDWR|O_EXCL, 0666);
-        if (lock_fd == -1)
+        server_lock_main = zebra_lock_create (path, 2);
+        if (!server_lock_main)
         {
-            lock_fd = open (path, O_BINARY|O_RDWR);
-            if (lock_fd == -1) 
+            server_lock_main = zebra_lock_create (path, 1);
+           if (!server_lock_main)
             {
                 if (errno == ENOENT)
                     continue;
                 logf (LOG_FATAL|LOG_ERRNO, "open %s", path);
                 exit (1);
             }
-            if (zebraLockNB (lock_fd, 1) == -1)
+            if (zebra_lock_nb (server_lock_main) == -1)
             {
+#ifdef WINDOWS
+                logf (LOG_LOG, "waiting for other index process");
+                zebra_lock (server_lock_main);
+                zebra_unlock (server_lock_main);
+                zebra_lock_destroy (server_lock_main);
+                continue;
+#else
                 if (errno == EWOULDBLOCK)
                 {
                     logf (LOG_LOG, "waiting for other index process");
-                    zebraLock (lock_fd, 1);
-                    zebraUnlock (lock_fd);
-                    close (lock_fd);
+                    zebra_lock (server_lock_main);
+                    zebra_unlock (server_lock_main);
+                   zebra_lock_destroy (server_lock_main);
                     continue;
                 }
                 else
@@ -194,16 +205,19 @@ void zebraIndexLock (BFiles bfs, int commitNow, const char *rval)
                     logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
                     exit (1);
                 }
+#endif
             }
             else
             {
+               int fd = zebra_lock_fd (server_lock_main);
+
                 logf (LOG_WARN, "unlocked %s", path);
-                r = read (lock_fd, buf, 256);
+                r = read (fd, buf, 256);
                 if (r == 0)
                 {
                     logf (LOG_WARN, "zero length %s", path);
-                    close (lock_fd);
-                    unlink (path);
+                   zebra_lock_destroy (server_lock_main);
+                   unlink (path);
                     continue;
                 }
                 else if (r == -1)
@@ -215,7 +229,7 @@ void zebraIndexLock (BFiles bfs, int commitNow, const char *rval)
                 {
                     logf (LOG_WARN, "previous transaction didn't"
                           " reach commit");
-                    close (lock_fd);
+                   zebra_lock_destroy (server_lock_main);
                     bf_commitClean (bfs, rval);
                     unlink (path);
                     continue;
@@ -223,7 +237,7 @@ void zebraIndexLock (BFiles bfs, int commitNow, const char *rval)
                 else if (*buf == 'd')
                 {
                     logf (LOG_WARN, "commit file wan't deleted after commit");
-                    close (lock_fd);
+                    zebra_lock_destroy (server_lock_main);
                     bf_commitClean (bfs, rval);
                     unlink (path);
                     continue;
@@ -238,7 +252,7 @@ void zebraIndexLock (BFiles bfs, int commitNow, const char *rval)
                     if (commitNow)
                     {
                         unlink (path);
-                        close (lock_fd);
+                       zebra_lock_destroy (server_lock_main);
                         continue;
                     }
                     logf (LOG_FATAL, "previous transaction didn't"
@@ -256,6 +270,6 @@ void zebraIndexLock (BFiles bfs, int commitNow, const char *rval)
         else
             break;
     }
-    zebraLock (lock_fd, 1);
+    zebra_lock (server_lock_main);
 }
 
index abb0349..304dc88 100644 (file)
@@ -1,10 +1,13 @@
 /*
- * Copyright (C) 1994-1996, Index Data I/S 
+ * Copyright (C) 1994-1997, Index Data I/S 
  * All rights reserved.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: locksrv.c,v $
- * Revision 1.8  1997-09-17 12:19:15  adam
+ * Revision 1.9  1997-09-25 14:54:43  adam
+ * WIN32 files lock support.
+ *
+ * Revision 1.8  1997/09/17 12:19:15  adam
  * Zebra version corresponds to YAZ version 1.4.
  * Changed Zebra server so that it doesn't depend on global common_resource.
  *
  */
 #include <stdio.h>
 #include <assert.h>
+#ifdef WINDOWS
+#include <io.h>
+#else
 #include <unistd.h>
+#endif
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <string.h>
@@ -44,8 +51,8 @@
 
 #include "zserver.h"
 
-static int server_lock_cmt = -1;
-static int server_lock_org = -1;
+static ZebraLockHandle server_lock_cmt = NULL;
+static ZebraLockHandle server_lock_org = NULL;
 
 int zebraServerLock (Res res, int commitPhase)
 {
@@ -54,20 +61,18 @@ int zebraServerLock (Res res, int commitPhase)
     
     zebraLockPrefix (res, pathPrefix);
 
-    if (server_lock_cmt == -1)
+    if (!server_lock_cmt)
     {
         sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK);
-        if ((server_lock_cmt = open (path, O_BINARY|O_CREAT|O_RDWR, 0666))
-            == -1)
+        if (!(server_lock_cmt = zebra_lock_create (path, 0)))
         {
             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
             return -1;
         }
-        assert (server_lock_org == -1);
+        assert (server_lock_org == NULL);
 
         sprintf (path, "%s%s", pathPrefix, FNAME_ORG_LOCK);
-        if ((server_lock_org = open (path, O_BINARY|O_CREAT|O_RDWR, 0666))
-            == -1)
+        if (!(server_lock_org = zebra_lock_create (path, 0)))
         {
             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
             return -1;
@@ -76,29 +81,29 @@ int zebraServerLock (Res res, int commitPhase)
     if (commitPhase)
     {
         logf (LOG_DEBUG, "Server locks org");
-        zebraLock (server_lock_org, 0);
+        zebra_lock (server_lock_org);
     }
     else
     {
         logf (LOG_DEBUG, "Server locks cmt");
-        zebraLock (server_lock_cmt, 0);
+        zebra_lock (server_lock_cmt);
     }
     return 0;
 }
 
 void zebraServerUnlock (int commitPhase)
 {
-    if (server_lock_org == -1)
+    if (server_lock_org == NULL)
         return;
     if (commitPhase)
     {
         logf (LOG_DEBUG, "Server unlocks org");
-        zebraUnlock (server_lock_org);
+        zebra_unlock (server_lock_org);
     }
     else
     {
         logf (LOG_DEBUG, "Server unlocks cmt");
-        zebraUnlock (server_lock_cmt);
+        zebra_unlock (server_lock_cmt);
     }
 }
 
index 1e0eb92..366f936 100644 (file)
@@ -1,10 +1,13 @@
 /*
- * Copyright (C) 1994-1995, Index Data I/S 
+ * Copyright (C) 1994-1997, Index Data I/S 
  * All rights reserved.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: lockutil.c,v $
- * Revision 1.8  1997-09-17 12:19:15  adam
+ * Revision 1.9  1997-09-25 14:54:43  adam
+ * WIN32 files lock support.
+ *
+ * Revision 1.8  1997/09/17 12:19:15  adam
  * Zebra version corresponds to YAZ version 1.4.
  * Changed Zebra server so that it doesn't depend on global common_resource.
  *
@@ -42,6 +45,7 @@
 #include <sys/types.h>
 #ifdef WINDOWS
 #include <io.h>
+#include <sys/locking.h>
 #else
 #include <unistd.h>
 #endif
 
 static char *lockDir = NULL;
 
+struct zebra_lock_info {
+    int fd;
+    int excl_flag;
+};
+
+ZebraLockHandle zebra_lock_create (const char *name, int excl_flag)
+{
+    ZebraLockHandle h = xmalloc (sizeof(*h));
+    h->excl_flag = excl_flag;
+    h->fd = -1;
+#ifdef WINDOWS
+    if (!h->excl_flag)
+        h->fd = open (name, O_BINARY|O_RDONLY);
+    if (h->fd == -1)
+        h->fd = open (name, ((h->excl_flag > 1) ? O_EXCL : 0)|
+           (O_BINARY|O_CREAT|O_RDWR), 0666);
+#else
+    h->fd= open (name, ((h->excl_flag > 1) ? O_EXCL : 0)|
+           (O_BINARY|O_CREAT|O_RDWR|O_SYNC), 0666);
+#endif
+    if (h->fd == -1)
+    {
+       if (h->excl_flag <= 1)
+            logf (LOG_WARN|LOG_ERRNO, "open %s", name);
+       xfree (h);
+       return NULL;
+    }
+    return h;
+}
+
+void zebra_lock_destroy (ZebraLockHandle h)
+{
+    if (!h)
+       return;
+    if (h->fd != -1)
+       close (h->fd);
+    xfree (h);
+}
+
 void zebraLockPrefix (Res res, char *pathPrefix)
 {
     if (!lockDir)
@@ -61,7 +104,10 @@ void zebraLockPrefix (Res res, char *pathPrefix)
         strcat (pathPrefix, "/");
 }
 
-static int intLock (int fd, int type, int cmd)
+#ifdef WINDOWS
+
+#else
+static int unixLock (int fd, int type, int cmd)
 {
     struct flock area;
     area.l_type = type;
@@ -69,26 +115,36 @@ static int intLock (int fd, int type, int cmd)
     area.l_len = area.l_start = 0L;
     return fcntl (fd, cmd, &area);
 }
+#endif
 
-int zebraLock (int fd, int wr)
+int zebra_lock (ZebraLockHandle h)
 {
-#if 0
-    return intLock (fd, wr ? F_EXLCK : F_SHLCK, F_SETLKW);
+#ifdef WINDOWS
+    return _locking (h->fd, _LK_LOCK, 1);
 #else
-    return intLock (fd, wr ? F_WRLCK : F_RDLCK, F_SETLKW);
+    return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLKW);
 #endif
 }
 
-int zebraLockNB (int fd, int wr)
+int zebra_lock_nb (ZebraLockHandle h)
 {
-#if 0
-    return intLock (fd, wr ? F_EXLCK : F_SHLCK, F_SETLK);
+#ifdef WINDOWS
+    return _locking (h->fd, _LK_NBLCK, 1);
+#else
+    return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLK);
+#endif
+}
+
+int zebra_unlock (ZebraLockHandle h)
+{
+#ifdef WINDOWS
+    return _locking (h->fd, _LK_UNLCK, 1);
 #else
-    return intLock (fd, wr ? F_WRLCK : F_RDLCK, F_SETLK);
+    return unixLock (h->fd, F_UNLCK, F_SETLKW);
 #endif
 }
 
-int zebraUnlock (int fd)
+int zebra_lock_fd (ZebraLockHandle h)
 {
-    return intLock (fd, F_UNLCK, F_SETLKW);
+    return h->fd;
 }