]> git.wh0rd.org - fontconfig.git/commitdiff
Add new FcAtomic datatype for config file locking
authorKeith Packard <keithp@keithp.com>
Fri, 1 Mar 2002 22:06:30 +0000 (22:06 +0000)
committerKeith Packard <keithp@keithp.com>
Fri, 1 Mar 2002 22:06:30 +0000 (22:06 +0000)
fontconfig/fontconfig.h
src/Imakefile
src/fccache.c
src/fcint.h

index 0f6dc7f200257600406c3e60d6fbc69ef50b4140..f25f57c6a3b5f593c26f0b7999c0c581ce4e4267 100644 (file)
@@ -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,
index ffda2fa10fd1a0909c52f9cd3ad93fdc782b361a..a5fc33af2cd5d27e7bead2c615a12efb792ef1d5 100644 (file)
@@ -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 <Library.tmpl>
 
index 63957765f4b5193ccd2a80043417b102fc59f4c8..4b2fbd635375b686a9d19089f3d8567c3d65fa59 100644 (file)
@@ -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;
 }
index 0bd06f559c54c4ac6d7d2d18bf804c1508d0715e..467ec9b7b15f2dc91cc27afa9a9ff5c56a1b5e2d 100644 (file)
@@ -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;