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