X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=src%2Ffcatomic.c;h=33c1cc6287df4282feccde3f2a71dc6f9d9c1568;hb=HEAD;hp=bfae53c315f5729285718c662f63913c8f0d12ac;hpb=a391da8f0f867b8f87b1912a91882b108d163e03;p=fontconfig.git diff --git a/src/fcatomic.c b/src/fcatomic.c index bfae53c..33c1cc6 100644 --- a/src/fcatomic.c +++ b/src/fcatomic.c @@ -1,21 +1,21 @@ /* - * $XFree86: $ + * fontconfig/src/fcatomic.c * - * Copyright © 2002 Keith Packard, member of The XFree86 Project, Inc. + * Copyright © 2002 Keith Packard * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting - * documentation, and that the name of Keith Packard not be used in + * documentation, and that the name of the author(s) not be used in * advertising or publicity pertaining to distribution of the software without - * specific, written prior permission. Keith Packard makes no + * specific, written prior permission. The authors make no * representations about the suitability of this software for any purpose. It * is provided "as is" without express or implied warranty. * - * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO - * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR @@ -30,7 +30,7 @@ * Uses only regular filesystem calls so it should * work even in the absense of functioning file locking * - * Four files: + * On Unix, four files are used: * file - the data file accessed by other apps. * new - a new version of the data file while it's being written * lck - the lock file @@ -41,6 +41,10 @@ * Attempt to link it to 'lck' * Unlink 'tmp' * If the link succeeded, the lock is held + * + * On Windows, where there are no links, no tmp file is used, and lck + * is a directory that's mkdir'ed. If the mkdir succeeds, the lock is + * held. */ #include "fcint.h" @@ -51,6 +55,10 @@ #include #include +#ifdef _WIN32 +#define mkdir(path,mode) _mkdir(path) +#endif + #define NEW_NAME ".NEW" #define LCK_NAME ".LCK" #define TMP_NAME ".TMP-XXXXXX" @@ -70,7 +78,8 @@ FcAtomicCreate (const FcChar8 *file) FcAtomic *atomic = malloc (total_len); if (!atomic) return 0; - + FcMemAlloc (FC_MEM_ATOMIC, total_len); + atomic->file = (FcChar8 *) (atomic + 1); strcpy ((char *) atomic->file, (char *) file); @@ -95,6 +104,7 @@ FcAtomicLock (FcAtomic *atomic) int ret; struct stat lck_stat; +#ifdef HAVE_LINK strcpy ((char *) atomic->tmp, (char *) atomic->file); strcat ((char *) atomic->tmp, TMP_NAME); fd = mkstemp ((char *) atomic->tmp); @@ -107,7 +117,7 @@ FcAtomicLock (FcAtomic *atomic) unlink ((char *) atomic->tmp); return FcFalse; } - ret = fprintf (f, "%d\n", getpid()); + ret = fprintf (f, "%ld\n", (long)getpid()); if (ret <= 0) { fclose (f); @@ -121,6 +131,9 @@ FcAtomicLock (FcAtomic *atomic) } ret = link ((char *) atomic->tmp, (char *) atomic->lck); (void) unlink ((char *) atomic->tmp); +#else + ret = mkdir ((char *) atomic->lck, 0600); +#endif if (ret < 0) { /* @@ -129,13 +142,18 @@ FcAtomicLock (FcAtomic *atomic) * machines sharing the same filesystem will have clocks * reasonably close to each other. */ - if (stat ((char *) atomic->lck, &lck_stat) >= 0) + if (FcStat (atomic->lck, &lck_stat) >= 0) { time_t now = time (0); if ((long int) (now - lck_stat.st_mtime) > 10 * 60) { +#ifdef HAVE_LINK if (unlink ((char *) atomic->lck) == 0) return FcAtomicLock (atomic); +#else + if (rmdir ((char *) atomic->lck) == 0) + return FcAtomicLock (atomic); +#endif } } return FcFalse; @@ -159,6 +177,9 @@ FcAtomicOrigFile (FcAtomic *atomic) FcBool FcAtomicReplaceOrig (FcAtomic *atomic) { +#ifdef _WIN32 + unlink (atomic->file); +#endif if (rename ((char *) atomic->new, (char *) atomic->file) < 0) return FcFalse; return FcTrue; @@ -173,11 +194,23 @@ FcAtomicDeleteNew (FcAtomic *atomic) void FcAtomicUnlock (FcAtomic *atomic) { +#ifdef HAVE_LINK unlink ((char *) atomic->lck); +#else + rmdir ((char *) atomic->lck); +#endif } void FcAtomicDestroy (FcAtomic *atomic) { + FcMemFree (FC_MEM_ATOMIC, sizeof (FcAtomic) + + strlen ((char *) atomic->file) * 4 + 4 + + sizeof (NEW_NAME) + sizeof (LCK_NAME) + + sizeof (TMP_NAME)); + free (atomic); } +#define __fcatomic__ +#include "fcaliastail.h" +#undef __fcatomic__