Multiple registers (alpha early)
[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.15 2002-04-04 14:14:13 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 char *zebra_mk_fname (const char *dir, const char *name)
29 {
30     int dlen = dir ? strlen(dir) : 0;
31     char *fname = xmalloc (dlen + strlen(name) + 3);
32
33 #ifdef WIN32
34     if (dlen)
35     {
36         int last_one = dir[dlen-1];
37
38         if (!strchr ("/\\:", last_one))
39             sprintf (fname, "%s\\%s", dir, name);
40         else
41             sprintf (fname, "%s%s", dir, name);
42     }
43     else
44         sprintf (fname, "%s", name);
45 #else
46     if (dlen)
47     {
48         int last_one = dir[dlen-1];
49
50         if (!strchr ("/", last_one))
51             sprintf (fname, "%s/%s", dir, name);
52         else
53             sprintf (fname, "%s%s", dir, name);
54     }
55     else
56         sprintf (fname, "%s", name);
57 #endif
58     return fname;
59 }
60
61 ZebraLockHandle zebra_lock_create (const char *dir,
62                                    const char *name, int excl_flag)
63 {
64     char *fname = zebra_mk_fname(dir, name);
65     ZebraLockHandle h = (ZebraLockHandle) xmalloc (sizeof(*h));
66
67     h->excl_flag = excl_flag;
68     h->fd = -1;
69
70     
71 #ifdef WIN32
72     if (!h->excl_flag)
73         h->fd = open (name, O_BINARY|O_RDONLY);
74     if (h->fd == -1)
75         h->fd = open (fname, ((h->excl_flag > 1) ? O_EXCL : 0)|
76             (O_BINARY|O_CREAT|O_RDWR), 0666);
77 #else
78     h->fd= open (fname, ((h->excl_flag > 1) ? O_EXCL : 0)|
79             (O_BINARY|O_CREAT|O_RDWR|O_SYNC), 0666);
80 #endif
81     if (h->fd == -1)
82     {
83         if (h->excl_flag <= 1)
84             logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
85         xfree (h);
86         h = 0;
87     }
88     xfree (fname);
89     return h;
90 }
91
92 void zebra_lock_destroy (ZebraLockHandle h)
93 {
94     if (!h)
95         return;
96     if (h->fd != -1)
97         close (h->fd);
98     xfree (h);
99 }
100
101 void zebra_lock_prefix (Res res, char *path)
102 {
103     char *lock_dir = res_get_def (res, "lockDir", "");
104
105     strcpy (path, lock_dir);
106     if (*path && path[strlen(path)-1] != '/')
107         strcat (path, "/");
108 }
109
110 #ifndef WIN32
111 static int unixLock (int fd, int type, int cmd)
112 {
113     struct flock area;
114     area.l_type = type;
115     area.l_whence = SEEK_SET;
116     area.l_len = area.l_start = 0L;
117     return fcntl (fd, cmd, &area);
118 }
119 #endif
120
121 int zebra_lock_w (ZebraLockHandle h)
122 {
123 #ifdef WIN32
124     return _locking (h->fd, _LK_LOCK, 1);
125 #else
126     return unixLock (h->fd, F_WRLCK, F_SETLKW);
127 #endif
128 }
129
130 int zebra_lock_r (ZebraLockHandle h)
131 {
132 #ifdef WIN32
133     return _locking (h->fd, _LK_LOCK, 1);
134 #else
135     return unixLock (h->fd, F_RDLCK, F_SETLKW);
136 #endif
137 }
138
139 int zebra_lock (ZebraLockHandle h)
140 {
141 #ifdef WIN32
142     return _locking (h->fd, _LK_LOCK, 1);
143 #else
144     return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLKW);
145 #endif
146 }
147
148 int zebra_lock_nb (ZebraLockHandle h)
149 {
150 #ifdef WIN32
151     return _locking (h->fd, _LK_NBLCK, 1);
152 #else
153     return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLK);
154 #endif
155 }
156
157 int zebra_unlock (ZebraLockHandle h)
158 {
159 #ifdef WIN32
160     return _locking (h->fd, _LK_UNLCK, 1);
161 #else
162     return unixLock (h->fd, F_UNLCK, F_SETLKW);
163 #endif
164 }
165
166 int zebra_lock_fd (ZebraLockHandle h)
167 {
168     return h->fd;
169 }