diff options
author | hiro <hiro@ee746299-78ed-0310-b773-934348b2243d> | 2014-06-10 03:55:35 +0000 |
---|---|---|
committer | hiro <hiro@ee746299-78ed-0310-b773-934348b2243d> | 2014-06-10 03:55:35 +0000 |
commit | c78c4cbc71476468ccaaa64ee9d4b280b7bcd612 (patch) | |
tree | c359831fd3d1cdb258e9630a5ca5e73032b2aa65 /libsylph | |
parent | b055e9771b979f98b4b3237ce8d98f75c56b988c (diff) |
made mbox locking NFS-safe (#202).
git-svn-id: svn://sylpheed.sraoss.jp/sylpheed/trunk@3407 ee746299-78ed-0310-b773-934348b2243d
Diffstat (limited to 'libsylph')
-rw-r--r-- | libsylph/mbox.c | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/libsylph/mbox.c b/libsylph/mbox.c index d00b66de..4c0b2da7 100644 --- a/libsylph/mbox.c +++ b/libsylph/mbox.c @@ -33,6 +33,10 @@ #include <ctype.h> #include <time.h> +#ifdef HAVE_LOCKFILE_H +# include <lockfile.h> +#endif + #include "mbox.h" #include "procmsg.h" #include "procheader.h" @@ -332,6 +336,17 @@ gint lock_mbox(const gchar *base, LockType type) gint retval = 0; if (type == LOCK_FILE) { +#if HAVE_LIBLOCKFILE + gchar *lockfile; + + lockfile = g_strconcat(base, ".lock", NULL); + if (lockfile_create(lockfile, 0, L_PID) != L_SUCCESS) { + FILE_OP_ERROR(lockfile, "lockfile_create"); + g_free(lockfile); + return -1; + } + g_free(lockfile); +#else gchar *lockfile, *locklink; gint retry = 0; FILE *lockfp; @@ -365,28 +380,23 @@ gint lock_mbox(const gchar *base, LockType type) } g_unlink(lockfile); g_free(lockfile); +#endif /* HAVE_LIBLOCKFILE */ } else if (type == LOCK_FLOCK) { gint lockfd; -#if HAVE_FLOCK - if ((lockfd = open(base, O_RDONLY)) < 0) { -#else if ((lockfd = open(base, O_RDWR)) < 0) { -#endif FILE_OP_ERROR(base, "open"); return -1; } -#if HAVE_FLOCK - if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) { - perror("flock"); -#else #if HAVE_LOCKF if (lockf(lockfd, F_TLOCK, 0) < 0) { perror("lockf"); +#elif HAVE_FLOCK + if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) { + perror("flock"); #else { -#endif -#endif /* HAVE_FLOCK */ +#endif /* HAVE_LOCKF */ g_warning(_("can't lock %s\n"), base); if (close(lockfd) < 0) perror("close"); @@ -410,8 +420,13 @@ gint unlock_mbox(const gchar *base, gint fd, LockType type) gchar *lockfile; lockfile = g_strconcat(base, ".lock", NULL); +#if HAVE_LIBLOCKFILE + if (lockfile_remove(lockfile) != L_SUCCESS) { + FILE_OP_ERROR(lockfile, "lockfile_remove"); +#else if (g_unlink(lockfile) < 0) { FILE_OP_ERROR(lockfile, "unlink"); +#endif /* HAVE_LIBLOCKFILE */ g_free(lockfile); return -1; } @@ -419,17 +434,15 @@ gint unlock_mbox(const gchar *base, gint fd, LockType type) return 0; } else if (type == LOCK_FLOCK) { -#if HAVE_FLOCK - if (flock(fd, LOCK_UN) < 0) { - perror("flock"); -#else #if HAVE_LOCKF if (lockf(fd, F_ULOCK, 0) < 0) { perror("lockf"); +#elif HAVE_FLOCK + if (flock(fd, LOCK_UN) < 0) { + perror("flock"); #else { -#endif -#endif /* HAVE_FLOCK */ +#endif /* HAVE_LOCKF */ g_warning(_("can't unlock %s\n"), base); if (close(fd) < 0) perror("close"); |