masqmail
view src/dotlock.c @ 369:19d57eb1b6a1
Minor refactoring change (else-if).
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Thu, 20 Oct 2011 10:23:47 +0200 |
parents | 41958685480d |
children |
line source
1 /*
2 ** MasqMail
3 ** Copyright (C) 2001 Oliver Kurth
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
20 #include <glib.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <syslog.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <time.h>
30 #include "masqmail.h"
31 #include "dotlock.h"
33 gboolean
34 dot_lock(gchar *lock_name, gchar *hitch_name)
35 {
36 gboolean ok = FALSE;
37 int fd;
39 fd = open(hitch_name, O_WRONLY | O_CREAT | O_EXCL, 0);
40 if (fd != -1) {
41 struct stat stat_buf;
43 close(fd);
44 link(hitch_name, lock_name);
45 if (stat(hitch_name, &stat_buf) == 0) {
46 if (stat_buf.st_nlink == 2) {
47 unlink(hitch_name);
48 ok = TRUE;
49 } else {
50 if (stat(lock_name, &stat_buf) == 0) {
51 if ((time(NULL) - stat_buf.st_mtime) > MAX_LOCKAGE) {
52 /* remove lock if uncredibly old */
53 unlink(lock_name);
55 link(hitch_name, lock_name);
56 if (stat(hitch_name, &stat_buf) == 0) {
57 if (stat_buf.st_nlink == 2) {
58 unlink(hitch_name);
59 ok = TRUE;
60 }
61 }
62 }
63 }
64 }
65 }
66 if (!ok) {
67 unlink(hitch_name);
68 }
69 } else
70 logwrite(LOG_WARNING, "could not create lock file %s: %s\n", lock_name, strerror(errno));
72 return ok;
73 }
75 gboolean
76 dot_unlock(gchar *lock_name)
77 {
78 unlink(lock_name);
80 return TRUE;
81 }