Fix windows logging .. Call _locking repeatedly .. Becuase Windows gives up
[idzebra-moved-to-github.git] / util / flock.c
1 /* $Id: flock.c,v 1.4 2006-03-24 13:33:57 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <assert.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <sys/types.h>
30 #ifdef WIN32
31 #include <io.h>
32 #include <sys/locking.h>
33 #endif
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include <idzebra/flock.h>
39 #include <yaz/xmalloc.h>
40 #include <yaz/log.h>
41
42 struct zebra_lock_info {
43     int fd;
44     char *fname;
45 };
46
47 int log_level = 0 /* YLOG_LOG|YLOG_FLUSH */;
48
49 char *zebra_mk_fname (const char *dir, const char *name)
50 {
51     int dlen = dir ? strlen(dir) : 0;
52     char *fname = xmalloc (dlen + strlen(name) + 3);
53
54 #ifdef WIN32
55     if (dlen)
56     {
57         int last_one = dir[dlen-1];
58
59         if (!strchr ("/\\:", last_one))
60             sprintf (fname, "%s\\%s", dir, name);
61         else
62             sprintf (fname, "%s%s", dir, name);
63     }
64     else
65         sprintf (fname, "%s", name);
66 #else
67     if (dlen)
68     {
69         int last_one = dir[dlen-1];
70
71         if (!strchr ("/", last_one))
72             sprintf (fname, "%s/%s", dir, name);
73         else
74             sprintf (fname, "%s%s", dir, name);
75     }
76     else
77         sprintf (fname, "%s", name);
78 #endif
79     return fname;
80 }
81
82 ZebraLockHandle zebra_lock_create (const char *dir, const char *name)
83 {
84     char *fname = zebra_mk_fname(dir, name);
85     ZebraLockHandle h = (ZebraLockHandle) xmalloc (sizeof(*h));
86
87     h->fd = -1;
88 #ifdef WIN32
89     h->fd = open (name, O_BINARY|O_RDONLY);
90     if (h->fd == -1)
91         h->fd = open (fname, (O_BINARY|O_CREAT|O_RDWR), 0666);
92 #else
93     h->fd= open (fname, (O_BINARY|O_CREAT|O_RDWR), 0666);
94 #endif
95     if (h->fd == -1)
96     {
97         xfree (h);
98         h = 0;
99     }
100     h->fname = fname;
101     yaz_log(log_level, "zebra_lock_create fd=%d p=%p fname=%s", h->fd, h, h->fname);
102     return h;
103 }
104
105 void zebra_lock_destroy (ZebraLockHandle h)
106 {
107     if (!h)
108         return;
109     yaz_log(log_level, "zebra_lock_destroy fd=%d p=%p fname=%s", h->fd, h, h->fname);
110     if (h->fd != -1)
111         close (h->fd);
112     xfree (h->fname);
113     xfree (h);
114 }
115
116 #ifndef WIN32
117 static int unixLock (int fd, int type, int cmd)
118 {
119     struct flock area;
120     area.l_type = type;
121     area.l_whence = SEEK_SET;
122     area.l_len = area.l_start = 0L;
123     return fcntl (fd, cmd, &area);
124 }
125 #endif
126
127 int zebra_lock_w (ZebraLockHandle h)
128 {
129     int r;
130     yaz_log(log_level, "zebra_lock_w fd=%d p=%p fname=%s", h->fd, h, h->fname);
131 #ifdef WIN32
132     while ((r = _locking (h->fd, _LK_LOCK, 1)))
133         ;
134 #else
135     r = unixLock (h->fd, F_WRLCK, F_SETLKW);
136 #endif
137     yaz_log(log_level, "zebra_lock_w fd=%d p=%p fname=%s OK", h->fd, h, h->fname);
138     return r;
139 }
140
141 int zebra_lock_r (ZebraLockHandle h)
142 {
143     int r;
144     yaz_log(log_level, "zebra_lock_r fd=%d p=%p fname=%s", h->fd, h, h->fname);
145 #ifdef WIN32
146     while ((r = _locking (h->fd, _LK_LOCK, 1)))
147         ;
148 #else
149     r = unixLock (h->fd, F_RDLCK, F_SETLKW);
150 #endif
151     yaz_log(log_level, "zebra_lock_r fd=%d p=%p fname=%s OK", h->fd, h, h->fname);
152     return r;
153 }
154
155 int zebra_unlock (ZebraLockHandle h)
156 {
157     yaz_log(log_level, "zebra_unlock fd=%d p=%p fname=%s", h->fd, h, h->fname);
158 #ifdef WIN32
159     return _locking (h->fd, _LK_UNLCK, 1);
160 #else
161     return unixLock (h->fd, F_UNLCK, F_SETLKW);
162 #endif
163 }
164