Initial commit
[edesklets.git] / core / src / flocks.c
blob362767d58fddafd101903158c07008ac147df6d0
1 /*-----------------------------------------------------------------------------
2 Copyright (C) Sylvain Fourmanoit <syfou@users.sourceforge.net>
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to
6 deal in the Software without restriction, including without limitation the
7 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies of the Software and its documentation and acknowledgment shall be
13 given in the documentation and software packages that this Software was
14 used.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 ------------------------------------------------------------------------------*/
25 Abstraction for fcntl / flock file locking
27 #include "edesklets.h"
29 /*---------------------------------------------------------------------------*/
30 #ifdef HAVE_FCNTL
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #ifdef HAVE_FCNTL_H
35 #include <fcntl.h>
36 #endif
37 #else
38 #ifdef HAVE_FLOCK
39 #ifdef HAVE_SYS_FILE_H
40 #include <sys/file.h>
41 #endif
42 #endif
43 #endif
45 /*---------------------------------------------------------------------------*/
46 struct _LFILE {
47 FILE * f;
48 gboolean locked;
51 /*---------------------------------------------------------------------------*/
52 FILE * file(LFILE * lf) {return (lf)?lf->f:NULL;}
54 LFILE *
55 fopen_locked(gchar * filename, gchar * mode,
56 gboolean exclusive, gboolean block,
57 GError ** error) {
58 LFILE * lf;
59 char * errmsg = NULL;
60 EError errnum;
61 gboolean errmsg_free;
62 #ifdef HAVE_FCNTL
63 int flags;
64 struct flock lock;
65 #endif
67 #define fatal(ERRNUM) \
68 do{\
69 errnum = ERRNUM;\
70 errmsg = error_system();\
71 errmsg_free = FALSE;\
72 goto eexit;\
73 }while(0)
75 #define bailout(ERRNUM, ...) \
76 do{\
77 errnum = ERRNUM;\
78 errmsg = g_strdup_printf(__VA_ARGS__);\
79 errmsg_free = TRUE;\
80 goto eexit;\
81 }while(0)
83 if (!(lf = g_malloc0(sizeof(LFILE))))
84 bailout(EERROR_UNDEFINED, _("could not allocate memory"));
86 if (!(lf->f = fopen(filename, mode)))
87 fatal(EERROR_SYSTEM);
89 if (exclusive) {
90 #ifdef HAVE_FCNTL
91 lock.l_type = F_WRLCK;
92 lock.l_whence = SEEK_SET;
93 lock.l_start = 0;
94 lock.l_len = 0;
95 if (fcntl(fileno(lf->f),(block)?F_SETLKW:F_SETLK, &lock)!=0) {
96 if (!block && (errno == EACCES || errno == EAGAIN))
97 fatal(EERROR_ALREADY_LOCKED);
98 else
99 fatal(EERROR_SYSTEM);
101 lf->locked = TRUE;
102 #else
103 #ifdef HAVE_FLOCK
104 if (flock(fileno(lf->f), LOCK_EX | ((block)?0:LOCK_NB)) == -1)
105 if (!block && errno == EWOULDBLOCK)
106 fatal(EERROR_ALREADY_LOCKED);
107 else
108 fatal(EERROR_SYSTEM);
109 lf->locked = TRUE;
110 #else
111 bailout(EERROR_ALREADY_LOCKED, _("No lock mechanism available"));
112 #endif
113 #endif
115 return lf;
117 eexit:
118 if (lf) {
119 if (lf->f) fclose(lf->f);
120 g_free(lf);
122 if (error)
123 *error = g_error_new(*EDOMAIN, errnum, "%s", errmsg);
124 if (errmsg_free)
125 g_free(errmsg);
126 return NULL;
128 #undef bailout
129 #undef fatal
132 void
133 fclose_locked(LFILE ** llf, GError ** error) {
134 #ifdef HAVE_FCNTL
135 struct flock lock;
136 #endif
137 if (!llf || !(*llf)) return;
138 g_assert((*llf)->f);
140 #define fatal() \
141 do{\
142 if (error && *error == NULL)\
143 *error = g_error_new(*EDOMAIN, EERROR_SYSTEM, "%s", error_system());\
144 } while(0)
146 if ((*llf)->locked) {
147 #ifdef HAVE_FCNTL
148 lock.l_type = F_UNLCK;
149 lock.l_whence = SEEK_SET;
150 lock.l_start = 0;
151 lock.l_len = 0;
152 if (fcntl(fileno((*llf)->f),F_SETLK,&lock) == -1)
153 fatal();
154 #else
155 #ifdef HAVE_FLOCK
156 if (flock(fileno((*llf)->f), LOCK_UN) == -1)
157 fatal();
158 #endif
159 #endif
161 if (fclose((*llf)->f) != 0)
162 fatal();
164 g_free(*llf);
165 *llf = NULL;
167 #undef fatal