WIN32 files lock support.
[idzebra-moved-to-github.git] / index / locksrv.c
1 /*
2  * Copyright (C) 1994-1997, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: locksrv.c,v $
7  * Revision 1.9  1997-09-25 14:54:43  adam
8  * WIN32 files lock support.
9  *
10  * Revision 1.8  1997/09/17 12:19:15  adam
11  * Zebra version corresponds to YAZ version 1.4.
12  * Changed Zebra server so that it doesn't depend on global common_resource.
13  *
14  * Revision 1.7  1997/09/04 13:58:04  adam
15  * Added O_BINARY for open calls.
16  *
17  * Revision 1.6  1996/10/29 14:06:52  adam
18  * Include zebrautl.h instead of alexutil.h.
19  *
20  * Revision 1.5  1996/05/15 11:58:18  adam
21  * Changed some log messages.
22  *
23  * Revision 1.4  1996/04/10  16:01:27  quinn
24  * Fixed order of path/filename.
25  *
26  * Revision 1.3  1995/12/11  11:43:29  adam
27  * Locking based on fcntl instead of flock.
28  * Setting commitEnable removed. Command line option -n can be used to
29  * prevent commit if commit setting is defined in the configuration file.
30  *
31  * Revision 1.2  1995/12/08  16:22:55  adam
32  * Work on update while servers are running. Three lock files introduced.
33  * The servers reload their registers when necessary, but they don't
34  * reestablish result sets yet.
35  *
36  * Revision 1.1  1995/12/07  17:38:47  adam
37  * Work locking mechanisms for concurrent updates/commit.
38  *
39  */
40 #include <stdio.h>
41 #include <assert.h>
42 #ifdef WINDOWS
43 #include <io.h>
44 #else
45 #include <unistd.h>
46 #endif
47 #include <sys/stat.h>
48 #include <fcntl.h>
49 #include <string.h>
50 #include <errno.h>
51
52 #include "zserver.h"
53
54 static ZebraLockHandle server_lock_cmt = NULL;
55 static ZebraLockHandle server_lock_org = NULL;
56
57 int zebraServerLock (Res res, int commitPhase)
58 {
59     char pathPrefix[1024];
60     char path[1024];
61     
62     zebraLockPrefix (res, pathPrefix);
63
64     if (!server_lock_cmt)
65     {
66         sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK);
67         if (!(server_lock_cmt = zebra_lock_create (path, 0)))
68         {
69             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
70             return -1;
71         }
72         assert (server_lock_org == NULL);
73
74         sprintf (path, "%s%s", pathPrefix, FNAME_ORG_LOCK);
75         if (!(server_lock_org = zebra_lock_create (path, 0)))
76         {
77             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
78             return -1;
79         }
80     }
81     if (commitPhase)
82     {
83         logf (LOG_DEBUG, "Server locks org");
84         zebra_lock (server_lock_org);
85     }
86     else
87     {
88         logf (LOG_DEBUG, "Server locks cmt");
89         zebra_lock (server_lock_cmt);
90     }
91     return 0;
92 }
93
94 void zebraServerUnlock (int commitPhase)
95 {
96     if (server_lock_org == NULL)
97         return;
98     if (commitPhase)
99     {
100         logf (LOG_DEBUG, "Server unlocks org");
101         zebra_unlock (server_lock_org);
102     }
103     else
104     {
105         logf (LOG_DEBUG, "Server unlocks cmt");
106         zebra_unlock (server_lock_cmt);
107     }
108 }
109
110 int zebraServerLockGetState (Res res, time_t *timep)
111 {
112     char pathPrefix[1024];
113     char path[1024];
114     char buf[256];
115     int fd;
116     struct stat xstat;
117     
118     zebraLockPrefix (res, pathPrefix);
119
120     sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
121     if (stat (path, &xstat) == -1)
122         *timep = 1;
123     else
124         *timep = xstat.st_ctime;
125     
126     sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
127     fd = open (path, O_BINARY|O_RDONLY);
128     if (fd == -1)
129     {
130         *buf = 0;
131         return 0;
132     }
133     if (read (fd, buf, 2) == 0)
134     {
135         *buf = 0;
136         return 0;
137     }
138     close (fd);
139     return *buf;
140 }