Improve the performance issue on rescanning directories
[platform/upstream/fontconfig.git] / src / fcdir.c
index 2b476e8..3bcd0b8 100644 (file)
@@ -35,6 +35,30 @@ FcFileIsDir (const FcChar8 *file)
     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,
@@ -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 ());
 }
@@ -282,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
  */
@@ -302,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 */
 }