Locking uses F_WRLCK and F_RDLCK again!
[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.4  1995-12-13 08:46:10  adam
8  * Locking uses F_WRLCK and F_RDLCK again!
9  *
10  * Revision 1.3  1995/12/12  16:00:57  adam
11  * System call sync(2) used after update/commit.
12  * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
13  * and F_RDLCK.
14  *
15  * Revision 1.2  1995/12/11  11:43:29  adam
16  * Locking based on fcntl instead of flock.
17  * Setting commitEnable removed. Command line option -n can be used to
18  * prevent commit if commit setting is defined in the configuration file.
19  *
20  * Revision 1.1  1995/12/07  17:38:47  adam
21  * Work locking mechanisms for concurrent updates/commit.
22  *
23  */
24 #include <stdio.h>
25 #include <assert.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #include <alexutil.h>
33 #include "index.h"
34
35 static char *lockDir = NULL;
36
37 void zebraLockPrefix (char *pathPrefix)
38 {
39     if (!lockDir)
40         lockDir = res_get_def (common_resource, "lockDir", "");
41     assert (lockDir);
42     
43     strcpy (pathPrefix, lockDir);
44     if (*pathPrefix && pathPrefix[strlen(pathPrefix)-1] != '/')
45         strcat (pathPrefix, "/");
46 }
47
48 static int intLock (int fd, int type, int cmd)
49 {
50     struct flock area;
51     area.l_type = type;
52     area.l_whence = SEEK_SET;
53     area.l_len = area.l_start = 0L;
54     return fcntl (fd, cmd, &area);
55 }
56
57 int zebraLock (int fd, int wr)
58 {
59 #if 0
60     return intLock (fd, wr ? F_EXLCK : F_SHLCK, F_SETLKW);
61 #else
62     return intLock (fd, wr ? F_WRLCK : F_RDLCK, F_SETLKW);
63 #endif
64 }
65
66 int zebraLockNB (int fd, int wr)
67 {
68 #if 0
69     return intLock (fd, wr ? F_EXLCK : F_SHLCK, F_SETLK);
70 #else
71     return intLock (fd, wr ? F_WRLCK : F_RDLCK, F_SETLK);
72 #endif
73 }
74
75 int zebraUnlock (int fd)
76 {
77     return intLock (fd, F_UNLCK, F_SETLKW);
78 }