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
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 /*---------------------------------------------------------------------------*/
39 #ifdef HAVE_SYS_FILE_H
45 /*---------------------------------------------------------------------------*/
51 /*---------------------------------------------------------------------------*/
52 FILE * file(LFILE
* lf
) {return (lf
)?lf
->f
:NULL
;}
55 fopen_locked(gchar
* filename
, gchar
* mode
,
56 gboolean exclusive
, gboolean block
,
67 #define fatal(ERRNUM) \
70 errmsg = error_system();\
75 #define bailout(ERRNUM, ...) \
78 errmsg = g_strdup_printf(__VA_ARGS__);\
83 if (!(lf
= g_malloc0(sizeof(LFILE
))))
84 bailout(EERROR_UNDEFINED
, _("could not allocate memory"));
86 if (!(lf
->f
= fopen(filename
, mode
)))
91 lock
.l_type
= F_WRLCK
;
92 lock
.l_whence
= SEEK_SET
;
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
);
104 if (flock(fileno(lf
->f
), LOCK_EX
| ((block
)?0:LOCK_NB
)) == -1)
105 if (!block
&& errno
== EWOULDBLOCK
)
106 fatal(EERROR_ALREADY_LOCKED
);
108 fatal(EERROR_SYSTEM
);
111 bailout(EERROR_ALREADY_LOCKED
, _("No lock mechanism available"));
119 if (lf
->f
) fclose(lf
->f
);
123 *error
= g_error_new(*EDOMAIN
, errnum
, "%s", errmsg
);
133 fclose_locked(LFILE
** llf
, GError
** error
) {
137 if (!llf
|| !(*llf
)) return;
142 if (error && *error == NULL)\
143 *error = g_error_new(*EDOMAIN, EERROR_SYSTEM, "%s", error_system());\
146 if ((*llf
)->locked
) {
148 lock
.l_type
= F_UNLCK
;
149 lock
.l_whence
= SEEK_SET
;
152 if (fcntl(fileno((*llf
)->f
),F_SETLK
,&lock
) == -1)
156 if (flock(fileno((*llf
)->f
), LOCK_UN
) == -1)
161 if (fclose((*llf
)->f
) != 0)