Work on new API. Locking system re-implemented
[idzebra-moved-to-github.git] / index / lockutil.c
1 /*
2  * Copyright (C) 1994-2002, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: lockutil.c,v 1.13 2002-02-20 17:30:01 adam Exp $
7  */
8 #include <stdio.h>
9 #include <assert.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <sys/types.h>
14 #ifdef WIN32
15 #include <io.h>
16 #include <sys/locking.h>
17 #else
18 #include <unistd.h>
19 #endif
20
21 #include "index.h"
22
23 struct zebra_lock_info {
24     int fd;
25     int excl_flag;
26 };
27
28 ZebraLockHandle zebra_lock_create (const char *name, int excl_flag)
29 {
30     ZebraLockHandle h = (ZebraLockHandle) xmalloc (sizeof(*h));
31     h->excl_flag = excl_flag;
32     h->fd = -1;
33 #ifdef WIN32
34     if (!h->excl_flag)
35         h->fd = open (name, O_BINARY|O_RDONLY);
36     if (h->fd == -1)
37         h->fd = open (name, ((h->excl_flag > 1) ? O_EXCL : 0)|
38             (O_BINARY|O_CREAT|O_RDWR), 0666);
39 #else
40     h->fd= open (name, ((h->excl_flag > 1) ? O_EXCL : 0)|
41             (O_BINARY|O_CREAT|O_RDWR|O_SYNC), 0666);
42 #endif
43     if (h->fd == -1)
44     {
45         if (h->excl_flag <= 1)
46             logf (LOG_WARN|LOG_ERRNO, "open %s", name);
47         xfree (h);
48         return NULL;
49     }
50     return h;
51 }
52
53 void zebra_lock_destroy (ZebraLockHandle h)
54 {
55     if (!h)
56         return;
57     if (h->fd != -1)
58         close (h->fd);
59     xfree (h);
60 }
61
62 void zebra_lock_prefix (Res res, char *path)
63 {
64     char *lock_dir = res_get_def (res, "lockDir", "");
65
66     strcpy (path, lock_dir);
67     if (*path && path[strlen(path)-1] != '/')
68         strcat (path, "/");
69 }
70
71 #ifndef WIN32
72 static int unixLock (int fd, int type, int cmd)
73 {
74     struct flock area;
75     area.l_type = type;
76     area.l_whence = SEEK_SET;
77     area.l_len = area.l_start = 0L;
78     return fcntl (fd, cmd, &area);
79 }
80 #endif
81
82 int zebra_lock_w (ZebraLockHandle h)
83 {
84 #ifdef WIN32
85     return _locking (h->fd, _LK_LOCK, 1);
86 #else
87     return unixLock (h->fd, F_WRLCK, F_SETLKW);
88 #endif
89 }
90
91 int zebra_lock_r (ZebraLockHandle h)
92 {
93 #ifdef WIN32
94     return _locking (h->fd, _LK_LOCK, 1);
95 #else
96     return unixLock (h->fd, F_RDLCK, F_SETLKW);
97 #endif
98 }
99
100 int zebra_lock (ZebraLockHandle h)
101 {
102 #ifdef WIN32
103     return _locking (h->fd, _LK_LOCK, 1);
104 #else
105     return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLKW);
106 #endif
107 }
108
109 int zebra_lock_nb (ZebraLockHandle h)
110 {
111 #ifdef WIN32
112     return _locking (h->fd, _LK_NBLCK, 1);
113 #else
114     return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLK);
115 #endif
116 }
117
118 int zebra_unlock (ZebraLockHandle h)
119 {
120 #ifdef WIN32
121     return _locking (h->fd, _LK_UNLCK, 1);
122 #else
123     return unixLock (h->fd, F_UNLCK, F_SETLKW);
124 #endif
125 }
126
127 int zebra_lock_fd (ZebraLockHandle h)
128 {
129     return h->fd;
130 }