Updated WIN32 code specific sections. Changed header.
[idzebra-moved-to-github.git] / index / locksrv.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: locksrv.c,v $
7  * Revision 1.12  1999-02-02 14:50:58  adam
8  * Updated WIN32 code specific sections. Changed header.
9  *
10  * Revision 1.11  1998/03/05 08:45:12  adam
11  * New result set model and modular ranking system. Moved towards
12  * descent server API. System information stored as "SGML" records.
13  *
14  * Revision 1.10  1997/09/29 09:08:36  adam
15  * Revised locking system to be thread safe for the server.
16  *
17  * Revision 1.9  1997/09/25 14:54:43  adam
18  * WIN32 files lock support.
19  *
20  * Revision 1.8  1997/09/17 12:19:15  adam
21  * Zebra version corresponds to YAZ version 1.4.
22  * Changed Zebra server so that it doesn't depend on global common_resource.
23  *
24  * Revision 1.7  1997/09/04 13:58:04  adam
25  * Added O_BINARY for open calls.
26  *
27  * Revision 1.6  1996/10/29 14:06:52  adam
28  * Include zebrautl.h instead of alexutil.h.
29  *
30  * Revision 1.5  1996/05/15 11:58:18  adam
31  * Changed some log messages.
32  *
33  * Revision 1.4  1996/04/10  16:01:27  quinn
34  * Fixed order of path/filename.
35  *
36  * Revision 1.3  1995/12/11  11:43:29  adam
37  * Locking based on fcntl instead of flock.
38  * Setting commitEnable removed. Command line option -n can be used to
39  * prevent commit if commit setting is defined in the configuration file.
40  *
41  * Revision 1.2  1995/12/08  16:22:55  adam
42  * Work on update while servers are running. Three lock files introduced.
43  * The servers reload their registers when necessary, but they don't
44  * reestablish result sets yet.
45  *
46  * Revision 1.1  1995/12/07  17:38:47  adam
47  * Work locking mechanisms for concurrent updates/commit.
48  *
49  */
50 #include <stdio.h>
51 #include <assert.h>
52 #ifdef WIN32
53 #include <io.h>
54 #else
55 #include <unistd.h>
56 #endif
57 #include <sys/stat.h>
58 #include <fcntl.h>
59 #include <string.h>
60 #include <errno.h>
61
62 #include "zserver.h"
63
64 int zebra_server_lock_init (ZebraHandle zi)
65 {
66     char path_prefix[1024];
67
68     assert (zi->res);
69     zi->server_lock_cmt = NULL;
70     zi->server_lock_org = NULL;
71
72     zebra_lock_prefix (zi->res, path_prefix);
73     zi->server_path_prefix = xmalloc (strlen(path_prefix)+1);
74     strcpy (zi->server_path_prefix, path_prefix);
75
76     logf (LOG_DEBUG, "Locking system initialized");
77     return 0;
78 }
79
80 int zebra_server_lock_destroy (ZebraHandle zi)
81 {
82     xfree (zi->server_path_prefix);
83     zebra_lock_destroy (zi->server_lock_cmt);
84     zebra_lock_destroy (zi->server_lock_org);
85     logf (LOG_DEBUG, "Locking system destroyed");
86     return 0;
87 }
88
89 int zebra_server_lock (ZebraHandle zi, int commitPhase)
90 {
91     if (!zi->server_lock_cmt)
92     {
93         char path[1024];
94
95         strcpy (path, zi->server_path_prefix);
96         strcat (path, FNAME_COMMIT_LOCK);
97         if (!(zi->server_lock_cmt = zebra_lock_create (path, 0)))
98         {
99             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
100             return -1;
101         }
102         assert (zi->server_lock_org == NULL);
103
104         strcpy (path, zi->server_path_prefix);
105         strcat (path, FNAME_ORG_LOCK);
106         if (!(zi->server_lock_org = zebra_lock_create (path, 0)))
107         {
108             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
109             return -1;
110         }
111     }
112     if (commitPhase)
113     {
114         logf (LOG_DEBUG, "Server locks org");
115         zebra_lock (zi->server_lock_org);
116     }
117     else
118     {
119         logf (LOG_DEBUG, "Server locks cmt");
120         zebra_lock (zi->server_lock_cmt);
121     }
122     return 0;
123 }
124
125 void zebra_server_unlock (ZebraHandle zi, int commitPhase)
126 {
127     if (zi->server_lock_org == NULL)
128         return;
129     if (commitPhase)
130     {
131         logf (LOG_DEBUG, "Server unlocks org");
132         zebra_unlock (zi->server_lock_org);
133     }
134     else
135     {
136         logf (LOG_DEBUG, "Server unlocks cmt");
137         zebra_unlock (zi->server_lock_cmt);
138     }
139 }
140
141 int zebra_server_lock_get_state (ZebraHandle zi, time_t *timep)
142 {
143     char path[1024];
144     char buf[256];
145     int fd;
146     struct stat xstat;
147     
148     strcpy (path, zi->server_path_prefix);
149     strcat (path, FNAME_TOUCH_TIME);
150     if (stat (path, &xstat) == -1)
151         *timep = 1;
152     else
153         *timep = xstat.st_ctime;
154
155     strcpy (path, zi->server_path_prefix);
156     strcat (path, FNAME_MAIN_LOCK);
157     fd = open (path, O_BINARY|O_RDONLY);
158     if (fd == -1)
159     {
160         *buf = 0;
161         return 0;
162     }
163     if (read (fd, buf, 2) == 0)
164     {
165         *buf = 0;
166         return 0;
167     }
168     close (fd);
169     return *buf;
170 }