Improve the performance issue on rescanning directories
[platform/upstream/fontconfig.git] / src / fcdir.c
index 70fd438..3bcd0b8 100644 (file)
@@ -7,9 +7,9 @@
  * 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.
  *
@@ -30,11 +30,35 @@ FcFileIsDir (const FcChar8 *file)
 {
     struct stat            statb;
 
-    if (FcStat ((const char *) file, &statb) != 0)
+    if (FcStat (file, &statb) != 0)
        return FcFalse;
     return S_ISDIR(statb.st_mode);
 }
 
+FcBool
+FcFileIsLink (const FcChar8 *file)
+{
+#if HAVE_LSTAT
+    struct stat statb;
+
+    if (lstat ((const char *)file, &statb) != 0)
+       return FcFalse;
+    return S_ISLNK (statb.st_mode);
+#else
+    return FcFalse;
+#endif
+}
+
+FcBool
+FcFileIsFile (const FcChar8 *file)
+{
+    struct stat statb;
+
+    if (FcStat (file, &statb) != 0)
+       return FcFalse;
+    return S_ISREG (statb.st_mode);
+}
+
 static FcBool
 FcFileScanFontConfig (FcFontSet                *set,
                      FcBlanks          *blanks,
@@ -65,7 +89,7 @@ FcFileScanFontConfig (FcFontSet               *set,
        /*
         * Edit pattern with user-defined rules
         */
-       if (font && config && !FcConfigSubstituteWithPat (config, font, NULL, FcMatchScan))
+       if (font && config && !FcConfigSubstitute (config, font, FcMatchScan))
        {
            FcPatternDestroy (font);
            font = NULL;
@@ -75,7 +99,7 @@ FcFileScanFontConfig (FcFontSet               *set,
        /*
         * Add the font
         */
-       if (font && (!config || FcConfigAcceptFont (config, font)))
+       if (font)
        {
            if (FcDebug() & FC_DBG_SCANV)
            {
@@ -106,16 +130,21 @@ FcFileScanConfig (FcFontSet       *set,
     if (FcFileIsDir (file))
        return FcStrSetAdd (dirs, file);
     else
-       return FcFileScanFontConfig (set, blanks, file, config);
+    {
+       if (set)
+           return FcFileScanFontConfig (set, blanks, file, config);
+       else
+           return FcTrue;
+    }
 }
 
 FcBool
 FcFileScan (FcFontSet      *set,
            FcStrSet        *dirs,
-           FcFileCache     *cache, /* XXX unused */
+           FcFileCache     *cache FC_UNUSED,
            FcBlanks        *blanks,
            const FcChar8   *file,
-           FcBool          force)
+           FcBool          force FC_UNUSED)
 {
     return FcFileScanConfig (set, dirs, blanks, file, FcConfigGetCurrent ());
 }
@@ -211,6 +240,9 @@ bail2:
 bail1:
     closedir (d);
 bail:
+    if (file)
+       free (file);
+
     return ret;
 }
 
@@ -235,7 +267,6 @@ FcCache *
 FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
 {
     FcStrSet           *dirs;
-    FcBool             ret = FcTrue;
     FcFontSet          *set;
     FcCache            *cache = NULL;
     struct stat                dir_stat;
@@ -243,45 +274,29 @@ FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
     if (FcDebug () & FC_DBG_FONTSET)
        printf ("cache scan dir %s\n", dir);
 
-    if (FcStat ((char *) dir, &dir_stat) < 0)
-    {
-       if (errno != ENOENT)
-           ret = FcFalse;
+    if (FcStatChecksum (dir, &dir_stat) < 0)
        goto bail;
-    }
 
     set = FcFontSetCreate();
     if (!set)
-    {
-       ret = FcFalse;
        goto bail;
-    }
 
     dirs = FcStrSetCreate ();
     if (!dirs)
-    {
-       ret = FcFalse;
        goto bail1;
-    }
 
     /*
      * Scan the dir
      */
     if (!FcDirScanConfig (set, dirs, NULL, dir, FcTrue, config))
-    {
-       ret = FcFalse;
        goto bail2;
-    }
 
     /*
      * Build the cache object
      */
     cache = FcDirCacheBuild (set, dir, &dir_stat, dirs);
     if (!cache)
-    {
-       ret = FcFalse;
        goto bail2;
-    }
 
     /*
      * Write out the cache file, ignoring any troubles
@@ -296,6 +311,45 @@ FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
     return cache;
 }
 
+FcCache *
+FcDirCacheRescan (const FcChar8 *dir, FcConfig *config)
+{
+    FcCache *cache = FcDirCacheLoad (dir, config, NULL);
+    FcCache *new = NULL;
+    struct stat dir_stat;
+    FcStrSet *dirs;
+
+    if (!cache)
+       return NULL;
+    if (FcStatChecksum (dir, &dir_stat) < 0)
+       goto bail;
+    dirs = FcStrSetCreate ();
+    if (!dirs)
+       goto bail;
+
+    /*
+     * Scan the dir
+     */
+    if (!FcDirScanConfig (NULL, dirs, NULL, dir, FcTrue, config))
+       goto bail1;
+    /*
+     * Rebuild the cache object
+     */
+    new = FcDirCacheRebuild (cache, &dir_stat, dirs);
+    if (!new)
+       goto bail1;
+    FcDirCacheUnload (cache);
+    /*
+     * Write out the cache file, ignoring any troubles
+     */
+    FcDirCacheWrite (new, config);
+
+bail1:
+    FcStrSetDestroy (dirs);
+bail:
+    return new;
+}
+
 /*
  * Read (or construct) the cache for a directory
  */
@@ -304,9 +358,6 @@ FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
 {
     FcCache            *cache = NULL;
 
-    if (config && !FcConfigAcceptFilename (config, dir))
-       return NULL;
-
     /* Try to use existing cache file */
     if (!force)
        cache = FcDirCacheLoad (dir, config, NULL);
@@ -319,7 +370,7 @@ FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
 }
 
 FcBool
-FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
+FcDirSave (FcFontSet *set FC_UNUSED, FcStrSet * dirs FC_UNUSED, const FcChar8 *dir FC_UNUSED)
 {
     return FcFalse; /* XXX deprecated */
 }