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