X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=sbr%2Flock_file.c;h=6deddd4a2c3e1ce45c21ac1d9b5bb0204b5fed4c;hp=ec887374e279d5131f582575955d4310ff3d2ba3;hb=4885712264980e6cbc2039f9158027bee9213475;hpb=3a162432aa58eff6e48f99afaf746408905b008c diff --git a/sbr/lock_file.c b/sbr/lock_file.c index ec88737..6deddd4 100644 --- a/sbr/lock_file.c +++ b/sbr/lock_file.c @@ -3,6 +3,10 @@ * lock.c -- routines to lock/unlock files * * $Id$ + * + * This code is Copyright (c) 2002, by the authors of nmh. See the + * COPYRIGHT file in the root directory of the nmh distribution for + * complete copyright information. */ /* Modified by Ruud de Rooij to support Miquel van Smoorenburg's liblockfile @@ -52,8 +56,6 @@ #include #endif -extern int errno; - #ifdef LOCKDIR char *lockdir = LOCKDIR; #endif @@ -197,10 +199,22 @@ lkfopen (char *file, char *mode) if (strcmp (mode, "r") == 0) access = O_RDONLY; - else + else if (strcmp (mode, "r+") == 0) access = O_RDWR; + else if (strcmp (mode, "w") == 0) + access = O_WRONLY | O_CREAT | O_TRUNC; + else if (strcmp (mode, "w+") == 0) + access = O_RDWR | O_CREAT | O_TRUNC; + else if (strcmp (mode, "a") == 0) + access = O_WRONLY | O_CREAT | O_APPEND; + else if (strcmp (mode, "a+") == 0) + access = O_RDWR | O_CREAT | O_APPEND; + else { + errno = EINVAL; + return NULL; + } - if ((fd = lkopen (file, access, 0)) == -1) + if ((fd = lkopen (file, access, 0666)) == -1) return NULL; if ((fp = fdopen (fd, mode)) == NULL) { @@ -310,7 +324,8 @@ lkopen_kernel (char *file, int access, mode_t mode) # endif # ifdef FLOCK_LOCKING - if (flock (fd, LOCK_EX | LOCK_NB) != -1) + if (flock (fd, (((access & 03) == O_RDONLY) ? LOCK_SH : LOCK_EX) + | LOCK_NB) != -1) return fd; # endif @@ -419,9 +434,18 @@ lockit (struct lockinfo *li) curlock = li->curlock; tmplock = li->tmplock; +#ifdef HAVE_MKSTEMP + if ((fd = mkstemp(tmplock)) == -1) + return -1; +#else + if (mktemp(tmplock) == NULL) + return -1; + if (unlink(tmplock) == -1 && errno != ENOENT) + return -1; /* create the temporary lock file */ if ((fd = creat(tmplock, 0600)) == -1) return -1; +#endif #if 0 /* write our process id into lock file */ @@ -501,18 +525,6 @@ lockname (char *file, struct lockinfo *li, int isnewlock) else snprintf (li->tmplock, sizeof(li->tmplock), "%.*s,LCK.XXXXXX", cp - li->curlock, li->curlock); -/* - Mkstemp work postponed until later -Doug -#ifdef HAVE_MKSTEMP - mkstemp (li->tmplock); -#else -*/ - mktemp (li->tmplock); -/* -#endif -*/ - - unlink (li->tmplock); /* remove any stray */ } #endif }