2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1999-02-02 14:50:58 adam
8 * Updated WIN32 code specific sections. Changed header.
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.
14 * Revision 1.10 1997/09/29 09:08:36 adam
15 * Revised locking system to be thread safe for the server.
17 * Revision 1.9 1997/09/25 14:54:43 adam
18 * WIN32 files lock support.
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.
24 * Revision 1.7 1997/09/04 13:58:04 adam
25 * Added O_BINARY for open calls.
27 * Revision 1.6 1996/10/29 14:06:52 adam
28 * Include zebrautl.h instead of alexutil.h.
30 * Revision 1.5 1996/05/15 11:58:18 adam
31 * Changed some log messages.
33 * Revision 1.4 1996/04/10 16:01:27 quinn
34 * Fixed order of path/filename.
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.
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.
46 * Revision 1.1 1995/12/07 17:38:47 adam
47 * Work locking mechanisms for concurrent updates/commit.
64 int zebra_server_lock_init (ZebraHandle zi)
66 char path_prefix[1024];
69 zi->server_lock_cmt = NULL;
70 zi->server_lock_org = NULL;
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);
76 logf (LOG_DEBUG, "Locking system initialized");
80 int zebra_server_lock_destroy (ZebraHandle zi)
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");
89 int zebra_server_lock (ZebraHandle zi, int commitPhase)
91 if (!zi->server_lock_cmt)
95 strcpy (path, zi->server_path_prefix);
96 strcat (path, FNAME_COMMIT_LOCK);
97 if (!(zi->server_lock_cmt = zebra_lock_create (path, 0)))
99 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
102 assert (zi->server_lock_org == NULL);
104 strcpy (path, zi->server_path_prefix);
105 strcat (path, FNAME_ORG_LOCK);
106 if (!(zi->server_lock_org = zebra_lock_create (path, 0)))
108 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
114 logf (LOG_DEBUG, "Server locks org");
115 zebra_lock (zi->server_lock_org);
119 logf (LOG_DEBUG, "Server locks cmt");
120 zebra_lock (zi->server_lock_cmt);
125 void zebra_server_unlock (ZebraHandle zi, int commitPhase)
127 if (zi->server_lock_org == NULL)
131 logf (LOG_DEBUG, "Server unlocks org");
132 zebra_unlock (zi->server_lock_org);
136 logf (LOG_DEBUG, "Server unlocks cmt");
137 zebra_unlock (zi->server_lock_cmt);
141 int zebra_server_lock_get_state (ZebraHandle zi, time_t *timep)
148 strcpy (path, zi->server_path_prefix);
149 strcat (path, FNAME_TOUCH_TIME);
150 if (stat (path, &xstat) == -1)
153 *timep = xstat.st_ctime;
155 strcpy (path, zi->server_path_prefix);
156 strcat (path, FNAME_MAIN_LOCK);
157 fd = open (path, O_BINARY|O_RDONLY);
163 if (read (fd, buf, 2) == 0)