Zebra version corresponds to YAZ version 1.4.
[idzebra-moved-to-github.git] / index / lockutil.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: lockutil.c,v $
7  * Revision 1.8  1997-09-17 12:19:15  adam
8  * Zebra version corresponds to YAZ version 1.4.
9  * Changed Zebra server so that it doesn't depend on global common_resource.
10  *
11  * Revision 1.7  1997/09/09 13:38:08  adam
12  * Partial port to WIN95/NT.
13  *
14  * Revision 1.6  1996/10/29 14:08:14  adam
15  * Uses resource lockDir instead of lockPath.
16  *
17  * Revision 1.5  1996/03/26 16:01:13  adam
18  * New setting lockPath: directory of various lock files.
19  *
20  * Revision 1.4  1995/12/13  08:46:10  adam
21  * Locking uses F_WRLCK and F_RDLCK again!
22  *
23  * Revision 1.3  1995/12/12  16:00:57  adam
24  * System call sync(2) used after update/commit.
25  * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
26  * and F_RDLCK.
27  *
28  * Revision 1.2  1995/12/11  11:43:29  adam
29  * Locking based on fcntl instead of flock.
30  * Setting commitEnable removed. Command line option -n can be used to
31  * prevent commit if commit setting is defined in the configuration file.
32  *
33  * Revision 1.1  1995/12/07  17:38:47  adam
34  * Work locking mechanisms for concurrent updates/commit.
35  *
36  */
37 #include <stdio.h>
38 #include <assert.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <sys/types.h>
43 #ifdef WINDOWS
44 #include <io.h>
45 #else
46 #include <unistd.h>
47 #endif
48
49 #include "index.h"
50
51 static char *lockDir = NULL;
52
53 void zebraLockPrefix (Res res, char *pathPrefix)
54 {
55     if (!lockDir)
56         lockDir = res_get_def (res, "lockDir", "");
57     assert (lockDir);
58     
59     strcpy (pathPrefix, lockDir);
60     if (*pathPrefix && pathPrefix[strlen(pathPrefix)-1] != '/')
61         strcat (pathPrefix, "/");
62 }
63
64 static int intLock (int fd, int type, int cmd)
65 {
66     struct flock area;
67     area.l_type = type;
68     area.l_whence = SEEK_SET;
69     area.l_len = area.l_start = 0L;
70     return fcntl (fd, cmd, &area);
71 }
72
73 int zebraLock (int fd, int wr)
74 {
75 #if 0
76     return intLock (fd, wr ? F_EXLCK : F_SHLCK, F_SETLKW);
77 #else
78     return intLock (fd, wr ? F_WRLCK : F_RDLCK, F_SETLKW);
79 #endif
80 }
81
82 int zebraLockNB (int fd, int wr)
83 {
84 #if 0
85     return intLock (fd, wr ? F_EXLCK : F_SHLCK, F_SETLK);
86 #else
87     return intLock (fd, wr ? F_WRLCK : F_RDLCK, F_SETLK);
88 #endif
89 }
90
91 int zebraUnlock (int fd)
92 {
93     return intLock (fd, F_UNLCK, F_SETLKW);
94 }