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