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