From: Keith Packard Date: Fri, 1 Mar 2002 22:06:30 +0000 (+0000) Subject: Add new FcAtomic datatype for config file locking X-Git-Tag: xf-4_2_99_1~36 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=134f6011f347d1bc1b80a3cd435bb10b38d2932e;p=fontconfig.git Add new FcAtomic datatype for config file locking --- diff --git a/fontconfig/fontconfig.h b/fontconfig/fontconfig.h index 0f6dc7f..f25f57c 100644 --- a/fontconfig/fontconfig.h +++ b/fontconfig/fontconfig.h @@ -1,5 +1,5 @@ /* - * $XFree86: xc/lib/fontconfig/fontconfig/fontconfig.h,v 1.4 2002/02/28 16:51:48 keithp Exp $ + * $XFree86: xc/lib/fontconfig/fontconfig/fontconfig.h,v 1.5 2002/03/01 01:00:54 keithp Exp $ * * Copyright © 2001 Keith Packard, member of The XFree86 Project, Inc. * @@ -212,6 +212,8 @@ typedef enum _FcSetName { FcSetApplication = 1 } FcSetName; +typedef struct _FcAtomic FcAtomic; + #if defined(__cplusplus) || defined(c_plusplus) /* for C++ V2.0 */ #define _FCFUNCPROTOBEGIN extern "C" { /* do not leave open across includes */ #define _FCFUNCPROTOEND } @@ -417,6 +419,32 @@ FcFontList (FcConfig *config, FcPattern *p, FcObjectSet *os); +/* fcatomic.c */ + +FcAtomic * +FcAtomicCreate (const FcChar8 *file); + +FcBool +FcAtomicLock (FcAtomic *atomic); + +FcChar8 * +FcAtomicNewFile (FcAtomic *atomic); + +FcChar8 * +FcAtomicOrigFile (FcAtomic *atomic); + +FcBool +FcAtomicReplaceOrig (FcAtomic *atomic); + +void +FcAtomicDeleteNew (FcAtomic *atomic); + +void +FcAtomicUnlock (FcAtomic *atomic); + +void +FcAtomicDestroy (FcAtomic *atomic); + /* fcmatch.c */ FcPattern * FcFontSetMatch (FcConfig *config, diff --git a/src/Imakefile b/src/Imakefile index ffda2fa..a5fc33a 100644 --- a/src/Imakefile +++ b/src/Imakefile @@ -28,13 +28,13 @@ DEFINES=-DFC_FALLBACK_FONTS='"$(FALLBACK_FONTS)"' EXPATLIB=-lexpat REQUIREDLIBS=$(LDPRELIBS) $(FREETYPE2LIB) $(EXPATLIB) -SRCS=fcblanks.c fccache.c fccfg.c fccharset.c fcdbg.c fcdefault.c fcdir.c \ - fcfreetype.c fcfs.c fcinit.c fclist.c fcmatch.c fcmatrix.c fcname.c \ - fcpat.c fcstr.c fcxml.c +SRCS=fcatomic.c fcblanks.c fccache.c fccfg.c fccharset.c fcdbg.c \ + fcdefault.c fcdir.c fcfreetype.c fcfs.c fcinit.c fclist.c fcmatch.c \ + fcmatrix.c fcname.c fcpat.c fcstr.c fcxml.c -OBJS=fcblanks.o fccache.o fccfg.o fccharset.o fcdbg.o fcdefault.o fcdir.o \ - fcfreetype.o fcfs.o fcinit.o fclist.o fcmatch.o fcmatrix.o fcname.o \ - fcpat.o fcstr.o fcxml.o +OBJS=fcatomic.o fcblanks.o fccache.o fccfg.o fccharset.o fcdbg.o \ + fcdefault.o fcdir.o fcfreetype.o fcfs.o fcinit.o fclist.o fcmatch.o \ + fcmatrix.o fcname.o fcpat.o fcstr.o fcxml.o #include diff --git a/src/fccache.c b/src/fccache.c index 6395776..4b2fbd6 100644 --- a/src/fccache.c +++ b/src/fccache.c @@ -1,5 +1,5 @@ /* - * $XFree86: xc/lib/fontconfig/src/fccache.c,v 1.3 2002/02/19 07:50:43 keithp Exp $ + * $XFree86: xc/lib/fontconfig/src/fccache.c,v 1.4 2002/03/01 01:00:54 keithp Exp $ * * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc. * @@ -416,28 +416,20 @@ FcBool FcFileCacheSave (FcFileCache *cache, const FcChar8 *cache_file) { - FcChar8 *lck; - FcChar8 *tmp; FILE *f; int h; - FcFileCacheEnt *c; + FcFileCacheEnt *c; + FcAtomic *atomic; if (!cache->updated && cache->referenced == cache->entries) return FcTrue; - lck = malloc (strlen ((char *) cache_file)*2 + 4); - if (!lck) + atomic = FcAtomicCreate (cache_file); + if (!atomic) goto bail0; - tmp = lck + strlen ((char *) cache_file) + 2; - strcpy ((char *) lck, (char *) cache_file); - strcat ((char *) lck, "L"); - strcpy ((char *) tmp, (char *) cache_file); - strcat ((char *) tmp, "T"); - if (link ((char *) lck, (char *) cache_file) < 0 && errno != ENOENT) + if (!FcAtomicLock (atomic)) goto bail1; - if (access ((char *) tmp, F_OK) == 0) - goto bail2; - f = fopen ((char *) tmp, "w"); + f = fopen ((char *) FcAtomicNewFile(atomic), "w"); if (!f) goto bail2; @@ -469,21 +461,23 @@ FcFileCacheSave (FcFileCache *cache, if (fclose (f) == EOF) goto bail3; - if (rename ((char *) tmp, (char *) cache_file) < 0) + if (!FcAtomicReplaceOrig (atomic)) goto bail3; - unlink ((char *) lck); + FcAtomicUnlock (atomic); + FcAtomicDestroy (atomic); + cache->updated = FcFalse; return FcTrue; bail4: fclose (f); bail3: - unlink ((char *) tmp); + FcAtomicDeleteNew (atomic); bail2: - unlink ((char *) lck); + FcAtomicUnlock (atomic); bail1: - free (lck); + FcAtomicDestroy (atomic); bail0: return FcFalse; } diff --git a/src/fcint.h b/src/fcint.h index 0bd06f5..467ec9b 100644 --- a/src/fcint.h +++ b/src/fcint.h @@ -1,5 +1,5 @@ /* - * $XFree86: xc/lib/fontconfig/src/fcint.h,v 1.3 2002/02/18 22:29:28 keithp Exp $ + * $XFree86: xc/lib/fontconfig/src/fcint.h,v 1.4 2002/02/19 08:33:23 keithp Exp $ * * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc. * @@ -200,6 +200,13 @@ struct _FcFileCache { int referenced; }; +struct _FcAtomic { + FcChar8 *file; /* original file name */ + FcChar8 *new; /* temp file name -- write data here */ + FcChar8 *lck; /* lockfile name (used for locking) */ + FcChar8 *tmp; /* tmpfile name (used for locking) */ +}; + struct _FcBlanks { int nblank; int sblank;