remove debian/files

move dotlocking code to dotlock.c.

dotlocking code fixed to ignore whether or not the lockfile exists on
open().  we only care about whether fcntl() was able to lock it.
This commit is contained in:
Michael Elkins 2002-06-20 23:33:13 +00:00
parent dd1b19a99a
commit 85b5c5b8c9
4 changed files with 121 additions and 63 deletions

View File

@ -1,7 +1,7 @@
SUBDIRS=debian SUBDIRS=debian
bin_PROGRAMS=isync bin_PROGRAMS=isync
isync_SOURCES=main.c imap.c sync.c maildir.c isync.h list.c cram.c config.c \ isync_SOURCES=main.c imap.c sync.c maildir.c isync.h list.c cram.c config.c \
debug.h debug.h dotlock.c dotlock.h
isync_LDADD=@DEBUGOBJ@ isync_LDADD=@DEBUGOBJ@
isync_DEPENDENCIES=@DEBUGOBJ@ isync_DEPENDENCIES=@DEBUGOBJ@
EXTRA_isync_SOURCES=debug.c EXTRA_isync_SOURCES=debug.c

89
dotlock.c Normal file
View File

@ -0,0 +1,89 @@
/* $Id$
*
* isync - IMAP4 to maildir mailbox synchronizer
* Copyright (C) 2002 Michael R. Elkins <me@mutt.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* this file contains routines to establish a mutex using a `dotlock' file
*/
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#if TESTING
#include <stdio.h>
#endif
#include "dotlock.h"
static struct flock lck = { 0, SEEK_SET, 0, 0, 0 };
static void make_lock (int type)
{
lck.l_type = type;
lck.l_pid = getpid ();
}
int dotlock_lock (const char *path, int *fd)
{
*fd = open (path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (*fd == -1)
return -1;
make_lock (F_WRLCK);
if (fcntl (*fd, F_SETLKW, &lck))
{
close (*fd);
*fd = -1;
return -1;
}
return 0;
}
int dotlock_unlock (int *fd)
{
int r = 0;
if (*fd != -1)
{
make_lock (F_UNLCK);
if (fcntl (*fd, F_SETLKW, &lck))
r = -1;
close (*fd);
*fd = -1;
}
return r;
}
#if TESTING
int main (void)
{
int fd;
if (dotlock_lock ("./lock", &fd))
{
perror ("dotlock_lock");
goto done;
}
puts("sleeping for 5 seconds");
sleep(5);
if (dotlock_unlock (&fd))
{
perror ("dotlock_lock");
}
done:
exit (0);
}
#endif

22
dotlock.h Normal file
View File

@ -0,0 +1,22 @@
/* $Id$
*
* isync - IMAP4 to maildir mailbox synchronizer
* Copyright (C) 2002 Michael R. Elkins <me@mutt.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
int dotlock_lock (const char *, int *);
int dotlock_unlock (int *);

View File

@ -29,26 +29,7 @@
#include <errno.h> #include <errno.h>
#include <time.h> #include <time.h>
#include "isync.h" #include "isync.h"
#include "dotlock.h"
static int
do_lock (int fd, int flag)
{
struct flock lck;
memset (&lck, 0, sizeof (lck));
lck.l_type = flag;
lck.l_whence = SEEK_SET;
lck.l_start = 0;
lck.l_len = 0;
if (fcntl (fd, F_SETLK, &lck))
{
perror ("fcntl");
return -1;
}
return 0;
}
/* 2,<flags> */ /* 2,<flags> */
static void static void
@ -117,44 +98,8 @@ read_uid (const char *path, const char *file, unsigned int *uid /* out */)
return ret; return ret;
} }
/* NOTE: this is NOT NFS safe */ /*
static int * open a maildir mailbox.
maildir_lock (mailbox_t * m)
{
char path[_POSIX_PATH_MAX];
snprintf (path, sizeof (path), "%s/isynclock", m->path);
m->lockfd = open (path, O_WRONLY | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR);
if (m->lockfd == -1)
{
perror (path);
return -1;
}
if (do_lock (m->lockfd, F_WRLCK))
{
close (m->lockfd);
m->lockfd = -1;
return -1;
}
return 0;
}
static void
maildir_unlock (mailbox_t * m)
{
char path[_POSIX_PATH_MAX];
if (m->lockfd != -1)
{
snprintf (path, sizeof (path), "%s/isynclock", m->path);
unlink (path);
do_lock (m->lockfd, F_UNLCK);
close (m->lockfd);
m->lockfd = -1;
}
}
/* open a maildir mailbox.
* if OPEN_FAST is set, we just check to make * if OPEN_FAST is set, we just check to make
* sure its a valid mailbox and don't actually parse it. any IMAP messages * sure its a valid mailbox and don't actually parse it. any IMAP messages
* with the \Recent flag set are guaranteed not to be in the mailbox yet, * with the \Recent flag set are guaranteed not to be in the mailbox yet,
@ -231,10 +176,12 @@ maildir_open (const char *path, int flags)
} }
} }
/* we need a mutex on the maildir because of the state files that isync /*
* we need a mutex on the maildir because of the state files that isync
* uses. * uses.
*/ */
if (maildir_lock (m)) snprintf (buf, sizeof (buf), "%s/isynclock", m->path);
if (dotlock_lock (buf, &m->lockfd))
goto err; goto err;
/* check for the uidvalidity value */ /* check for the uidvalidity value */
@ -311,7 +258,7 @@ maildir_open (const char *path, int flags)
err: err:
if (m->db) if (m->db)
dbm_close (m->db); dbm_close (m->db);
maildir_unlock (m); dotlock_unlock (&m->lockfd);
free (m->path); free (m->path);
free (m); free (m);
return NULL; return NULL;
@ -436,7 +383,7 @@ maildir_close (mailbox_t * mbox)
dbm_close (mbox->db); dbm_close (mbox->db);
/* release the mutex on the mailbox */ /* release the mutex on the mailbox */
maildir_unlock (mbox); dotlock_unlock (&mbox->lockfd);
/* per the maildir(5) specification, delivery agents are supposed to /* per the maildir(5) specification, delivery agents are supposed to
* set a 24-hour timer on items placed in the `tmp' directory. * set a 24-hour timer on items placed in the `tmp' directory.