-
-/*
- * lock.c -- routines to lock/unlock files
+/* lock_file.c -- routines to lock/unlock files
*
* This code is Copyright (c) 2002, by the authors of nmh. See the
* COPYRIGHT file in the root directory of the nmh distribution for
* Ruud de Rooij <ruud@debian.org> Sun, 28 Mar 1999 15:34:03 +0200
*/
-#include <h/mh.h>
-#include <h/signals.h>
-#include <h/utils.h>
-#include <h/mts.h>
+#include "h/mh.h"
+#include "context_find.h"
+#include "error.h"
+#include "h/signals.h"
+#include "h/utils.h"
+#include "h/mts.h"
+#include "lock_file.h"
+#include "m_mktemp.h"
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
enum locktype { FCNTL_LOCKING, FLOCK_LOCKING, LOCKF_LOCKING, DOT_LOCKING };
-/*
- * Flags to indicate whether we've initialized the lock types, and
- * our saved lock types
- */
-static int datalockinit = 0;
-static int spoollockinit = 0;
+/* Our saved lock types. */
static enum locktype datalocktype, spoollocktype;
-
/* top of list containing all open locks */
static struct lock *l_top = NULL;
static int lkopen_flock (const char *, int, mode_t, int *);
#endif /* HAVE_FLOCK */
-static enum locktype init_locktype(const char *);
+static enum locktype init_locktype(const char *) PURE;
static int lkopen_dot (const char *, int, mode_t, int *);
static void lkclose_dot (int, const char *);
int
lkopendata(const char *file, int access, mode_t mode, int *failed_to_lock)
{
- if (! datalockinit) {
- char *cp = context_find("datalocking");
+ static bool deja_vu;
+
+ if (!deja_vu) {
+ char *dl;
- if (cp) {
- datalocktype = init_locktype(cp);
+ deja_vu = true;
+ if ((dl = context_find("datalocking"))) {
+ datalocktype = init_locktype(dl);
} else {
/* We default to fcntl locking for data files */
datalocktype = FCNTL_LOCKING;
}
-
- datalockinit = 1;
}
return lkopen(file, access, mode, datalocktype, failed_to_lock);
* Locking using the spool locking algorithm
*/
-int lkopenspool(const char *file, int access, mode_t mode, int *failed_to_lock)
+int
+lkopenspool(const char *file, int access, mode_t mode, int *failed_to_lock)
{
- if (! spoollockinit) {
- spoollocktype = init_locktype(spoollocking);
+ static bool deja_vu;
- spoollockinit = 1;
+ if (!deja_vu) {
+ deja_vu = true;
+ spoollocktype = init_locktype(spoollocking);
}
return lkopen(file, access, mode, spoollocktype, failed_to_lock);
#endif /* HAVE_FLOCK */
default:
- adios(NULL, "Internal locking error: unsupported lock type used!");
+ die("Internal locking error: unsupported lock type used!");
}
return -1;
lockname (file, &lkinfo, 1);
}
+ close(fd);
*failed_to_lock = 1;
return -1;
}
fd = link(tmpfile, curlock);
(void) m_unlink(tmpfile);
- return (fd == -1 ? -1 : 0);
+ return fd == -1 ? -1 : 0;
}
#endif /* HAVE_LIBLOCKFILE */
snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
-#if !defined(HAVE_LIBLOCKFILE)
+#if defined(HAVE_LIBLOCKFILE)
+ NMH_UNUSED(isnewlock);
+#else
/*
* If this is for a new lock, create a name for
* the temporary lock file for lockit()
#ifdef HAVE_LOCKF
return LOCKF_LOCKING;
#else /* ! HAVE_LOCKF */
- adios(NULL, "lockf not supported on this system");
+ die("lockf not supported on this system");
#endif /* HAVE_LOCKF */
}
if (strcasecmp(lockname, "flock") == 0) {
#ifdef HAVE_FLOCK
return FLOCK_LOCKING;
#else /* ! HAVE_FLOCK */
- adios(NULL, "flock not supported on this system");
+ die("flock not supported on this system");
#endif /* HAVE_FLOCK */
}
if (strcasecmp(lockname, "dot") == 0) {
return DOT_LOCKING;
}
- adios(NULL, "Unknown lock type: \"%s\"", lockname);
+ die("Unknown lock type: \"%s\"", lockname);
/* NOTREACHED */
return 0;
}