2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1998-03-05 08:45:12 adam
8 * New result set model and modular ranking system. Moved towards
9 * descent server API. System information stored as "SGML" records.
11 * Revision 1.10 1997/09/29 09:08:36 adam
12 * Revised locking system to be thread safe for the server.
14 * Revision 1.9 1997/09/25 14:54:43 adam
15 * WIN32 files lock support.
17 * Revision 1.8 1997/09/17 12:19:15 adam
18 * Zebra version corresponds to YAZ version 1.4.
19 * Changed Zebra server so that it doesn't depend on global common_resource.
21 * Revision 1.7 1997/09/04 13:58:04 adam
22 * Added O_BINARY for open calls.
24 * Revision 1.6 1996/10/29 14:06:52 adam
25 * Include zebrautl.h instead of alexutil.h.
27 * Revision 1.5 1996/05/15 11:58:18 adam
28 * Changed some log messages.
30 * Revision 1.4 1996/04/10 16:01:27 quinn
31 * Fixed order of path/filename.
33 * Revision 1.3 1995/12/11 11:43:29 adam
34 * Locking based on fcntl instead of flock.
35 * Setting commitEnable removed. Command line option -n can be used to
36 * prevent commit if commit setting is defined in the configuration file.
38 * Revision 1.2 1995/12/08 16:22:55 adam
39 * Work on update while servers are running. Three lock files introduced.
40 * The servers reload their registers when necessary, but they don't
41 * reestablish result sets yet.
43 * Revision 1.1 1995/12/07 17:38:47 adam
44 * Work locking mechanisms for concurrent updates/commit.
61 int zebra_server_lock_init (ZebraHandle zi)
63 char path_prefix[1024];
66 zi->server_lock_cmt = NULL;
67 zi->server_lock_org = NULL;
69 zebra_lock_prefix (zi->res, path_prefix);
70 zi->server_path_prefix = xmalloc (strlen(path_prefix)+1);
71 strcpy (zi->server_path_prefix, path_prefix);
73 logf (LOG_DEBUG, "Locking system initialized");
77 int zebra_server_lock_destroy (ZebraHandle zi)
79 xfree (zi->server_path_prefix);
80 zebra_lock_destroy (zi->server_lock_cmt);
81 zebra_lock_destroy (zi->server_lock_org);
82 logf (LOG_DEBUG, "Locking system destroyed");
86 int zebra_server_lock (ZebraHandle zi, int commitPhase)
88 if (!zi->server_lock_cmt)
92 strcpy (path, zi->server_path_prefix);
93 strcat (path, FNAME_COMMIT_LOCK);
94 if (!(zi->server_lock_cmt = zebra_lock_create (path, 0)))
96 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
99 assert (zi->server_lock_org == NULL);
101 strcpy (path, zi->server_path_prefix);
102 strcat (path, FNAME_ORG_LOCK);
103 if (!(zi->server_lock_org = zebra_lock_create (path, 0)))
105 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
111 logf (LOG_DEBUG, "Server locks org");
112 zebra_lock (zi->server_lock_org);
116 logf (LOG_DEBUG, "Server locks cmt");
117 zebra_lock (zi->server_lock_cmt);
122 void zebra_server_unlock (ZebraHandle zi, int commitPhase)
124 if (zi->server_lock_org == NULL)
128 logf (LOG_DEBUG, "Server unlocks org");
129 zebra_unlock (zi->server_lock_org);
133 logf (LOG_DEBUG, "Server unlocks cmt");
134 zebra_unlock (zi->server_lock_cmt);
138 int zebra_server_lock_get_state (ZebraHandle zi, time_t *timep)
145 strcpy (path, zi->server_path_prefix);
146 strcat (path, FNAME_TOUCH_TIME);
147 if (stat (path, &xstat) == -1)
150 *timep = xstat.st_ctime;
152 strcpy (path, zi->server_path_prefix);
153 strcat (path, FNAME_MAIN_LOCK);
154 fd = open (path, O_BINARY|O_RDONLY);
160 if (read (fd, buf, 2) == 0)