--- main.c.orig Sun May 22 19:33:26 2005 +++ main.c Tue Jun 14 00:28:12 2005 @@ -33,6 +33,7 @@ #include #include #include +#include #include "config.h" #include "misc.h" @@ -53,6 +54,8 @@ "Subdirectory of \"base\" for collections (default \"sup\")"); lprintf(-1, USAGE_OPTFMT, "-h host", "Override supfile's \"host\" name"); + lprintf(-1, USAGE_OPTFMT, "-l lockfile", + "Lock file during update; fail if already locked"); lprintf(-1, USAGE_OPTFMT, "-L n", "Verbosity level (0..2, default 1)"); lprintf(-1, USAGE_OPTFMT, "-p port", @@ -68,15 +71,16 @@ main(int argc, char *argv[]) { struct config *config; - char *argv0, *base, *colldir, *host, *file; + char *argv0, *base, *colldir, *host, *file, *lockfile; in_port_t port; - int c, compress, error; + int c, compress, error, lockfd, lflag = 0; port = 0; compress = 0; + lockfd = 0; argv0 = argv[0]; - base = colldir = host = NULL; - while ((c = getopt(argc, argv, "b:c:gh:L:p:P:vzZ")) != -1) { + base = colldir = host = lockfile = NULL; + while ((c = getopt(argc, argv, "b:c:gh:l:L:p:P:vzZ")) != -1) { switch (c) { case 'b': base = optarg; @@ -90,6 +94,20 @@ case 'h': host = optarg; break; + case 'l': + lockfile = optarg; + lflag = 1; + errno = 0; + lockfd = open(lockfile, O_EXLOCK|O_CREAT|O_NONBLOCK, 0700); + if (errno == EAGAIN) { + lprintf(-1, "\"%s\" is already locked by another process\n", lockfile); + return (1); + } + if (lockfd == -1) { + lprintf(-1, "Error opening lockfile %s\n", lockfile); + return (1); + } + break; case 'L': errno = 0; verbose = strtol(optarg, NULL, 0); @@ -153,5 +171,7 @@ return (1); lprintf(1, "Connected to %s\n", config->host); cvsup_init(config); + if (lflag) + close(lockfd); return (0); }