+FcGlobalCacheDir *
+FcGlobalCacheDirGet (FcGlobalCache *cache,
+ const FcChar8 *dir,
+ int len,
+ FcBool create_missing)
+{
+ unsigned int hash = FcCacheHash (dir, len);
+ FcGlobalCacheDir *d, **prev;
+
+ for (prev = &cache->ents[hash % FC_GLOBAL_CACHE_DIR_HASH_SIZE];
+ (d = *prev);
+ prev = &(*prev)->next)
+ {
+ if (d->info.hash == hash && d->len == len &&
+ !strncmp ((const char *) d->info.file,
+ (const char *) dir, len))
+ break;
+ }
+ if (!(d = *prev))
+ {
+ int i;
+ if (!create_missing)
+ return 0;
+ d = malloc (sizeof (FcGlobalCacheDir) + len + 1);
+ if (!d)
+ return 0;
+ FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCacheDir) + len + 1);
+ d->next = *prev;
+ *prev = d;
+ d->info.hash = hash;
+ d->info.file = (FcChar8 *) (d + 1);
+ strncpy ((char *) d->info.file, (const char *) dir, len);
+ d->info.file[len] = '\0';
+ d->info.time = 0;
+ d->info.referenced = FcFalse;
+ d->len = len;
+ for (i = 0; i < FC_GLOBAL_CACHE_FILE_HASH_SIZE; i++)
+ d->ents[i] = 0;
+ d->subdirs = 0;
+ }
+ return d;
+}
+
+static FcGlobalCacheInfo *
+FcGlobalCacheDirAdd (FcGlobalCache *cache,
+ const FcChar8 *dir,
+ time_t time,
+ FcBool replace,
+ FcBool create_missing)
+{
+ FcGlobalCacheDir *d;
+ FcFilePathInfo i;
+ FcGlobalCacheSubdir *subdir;
+ FcGlobalCacheDir *parent;
+
+ i = FcFilePathInfoGet (dir);
+ parent = FcGlobalCacheDirGet (cache, i.dir, i.dir_len, create_missing);
+ /*
+ * Tricky here -- directories containing fonts.cache-1 files
+ * need entries only when the parent doesn't have a cache file.
+ * That is, when the parent already exists in the cache, is
+ * referenced and has a "real" timestamp. The time of 0 is
+ * special and marks directories which got stuck in the
+ * global cache for this very reason. Yes, it could
+ * use a separate boolean field, and probably should.
+ */
+ if (!parent || (!create_missing &&
+ (!parent->info.referenced ||
+ (parent->info.time == 0))))
+ return 0;
+ /*
+ * Add this directory to the cache
+ */
+ d = FcGlobalCacheDirGet (cache, dir, strlen ((const char *) dir), FcTrue);
+ if (!d)
+ return 0;
+ d->info.time = time;
+ /*
+ * Add this directory to the subdirectory list of the parent
+ */
+ subdir = malloc (sizeof (FcGlobalCacheSubdir));
+ if (!subdir)
+ return 0;
+ FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCacheSubdir));
+ subdir->ent = d;
+ subdir->next = parent->subdirs;
+ parent->subdirs = subdir;
+ return &d->info;
+}
+
+static void
+FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
+{
+ FcGlobalCacheFile *f, *next;
+ int h;
+ FcGlobalCacheSubdir *s, *nexts;
+
+ for (h = 0; h < FC_GLOBAL_CACHE_FILE_HASH_SIZE; h++)
+ for (f = d->ents[h]; f; f = next)
+ {
+ next = f->next;
+ FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheFile) +
+ strlen ((char *) f->info.file) + 1 +
+ strlen ((char *) f->name) + 1);
+ free (f);
+ }
+ for (s = d->subdirs; s; s = nexts)
+ {
+ nexts = s->next;
+ FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheSubdir));
+ free (s);
+ }
+ FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir) + d->len + 1);
+ free (d);
+}
+
+/*
+ * If the parent is in the global cache and referenced, add
+ * an entry for 'dir' to the global cache. This is used
+ * for directories with fonts.cache files
+ */
+
+void
+FcGlobalCacheReferenceSubdir (FcGlobalCache *cache,
+ const FcChar8 *dir)
+{
+ FcGlobalCacheInfo *info;
+ info = FcGlobalCacheDirAdd (cache, dir, 0, FcFalse, FcFalse);
+ if (info && !info->referenced)
+ {
+ info->referenced = FcTrue;
+ cache->referenced++;
+ }
+}
+
+/*
+ * Check to see if the global cache contains valid data for 'dir'.
+ * If so, scan the global cache for files and directories in 'dir'.
+ * else, return False.
+ */
+FcBool
+FcGlobalCacheScanDir (FcFontSet *set,
+ FcStrSet *dirs,
+ FcGlobalCache *cache,
+ const FcChar8 *dir,
+ FcConfig *config)
+{
+ FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, dir,
+ strlen ((const char *) dir),
+ FcFalse);
+ FcGlobalCacheFile *f;
+ int h;
+ int dir_len;
+ FcGlobalCacheSubdir *subdir;
+ FcBool any_in_cache = FcFalse;
+
+ if (FcDebug() & FC_DBG_CACHE)
+ printf ("FcGlobalCacheScanDir %s\n", dir);
+
+ if (!d)
+ {
+ if (FcDebug () & FC_DBG_CACHE)
+ printf ("\tNo dir cache entry\n");
+ return FcFalse;
+ }
+
+ /*
+ * See if the timestamp recorded in the global cache
+ * matches the directory time, if not, return False
+ */
+ if (!FcGlobalCacheCheckTime (d->info.file, &d->info))
+ {
+ if (FcDebug () & FC_DBG_CACHE)
+ printf ("\tdir cache entry time mismatch\n");
+ return FcFalse;
+ }
+
+ /*
+ * Add files from 'dir' to the fontset
+ */
+ dir_len = strlen ((const char *) dir);
+ for (h = 0; h < FC_GLOBAL_CACHE_FILE_HASH_SIZE; h++)
+ for (f = d->ents[h]; f; f = f->next)
+ {
+ if (FcDebug() & FC_DBG_CACHEV)
+ printf ("FcGlobalCacheScanDir add file %s\n", f->info.file);
+ any_in_cache = FcTrue;
+ if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
+ f->info.file, f->name, config))
+ {
+ cache->broken = FcTrue;
+ return FcFalse;
+ }
+ FcGlobalCacheReferenced (cache, &f->info);
+ }
+ /*
+ * Add directories in 'dir' to 'dirs'
+ */
+ for (subdir = d->subdirs; subdir; subdir = subdir->next)
+ {
+ FcFilePathInfo info = FcFilePathInfoGet (subdir->ent->info.file);
+
+ any_in_cache = FcTrue;
+ if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
+ info.base, FC_FONT_FILE_DIR, config))
+ {
+ cache->broken = FcTrue;
+ return FcFalse;
+ }
+ FcGlobalCacheReferenced (cache, &subdir->ent->info);
+ }
+
+ FcGlobalCacheReferenced (cache, &d->info);
+
+ /*
+ * To recover from a bug in previous versions of fontconfig,
+ * return FcFalse if no entries in the cache were found
+ * for this directory. This will cause any empty directories
+ * to get rescanned every time fontconfig is initialized. This
+ * might get removed at some point when the older cache files are
+ * presumably fixed.
+ */
+ return any_in_cache;
+}
+
+/*
+ * Locate the cache entry for a particular file
+ */
+FcGlobalCacheFile *
+FcGlobalCacheFileGet (FcGlobalCache *cache,
+ const FcChar8 *file,
+ int id,
+ int *count)
+{
+ FcFilePathInfo i = FcFilePathInfoGet (file);
+ FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
+ i.dir_len, FcFalse);
+ FcGlobalCacheFile *f, *match = 0;
+ int max = -1;
+
+ if (!d)
+ return 0;
+ for (f = d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE]; f; f = f->next)
+ {
+ if (f->info.hash == i.base_hash &&
+ !strcmp ((const char *) f->info.file, (const char *) i.base))
+ {
+ if (f->id == id)
+ match = f;
+ if (f->id > max)
+ max = f->id;
+ }
+ }
+ if (count)
+ *count = max + 1;
+ return match;
+}
+
+/*
+ * Add a file entry to the cache
+ */
+static FcGlobalCacheInfo *
+FcGlobalCacheFileAdd (FcGlobalCache *cache,
+ const FcChar8 *path,
+ int id,
+ time_t time,
+ const FcChar8 *name,
+ FcBool replace)
+{
+ FcFilePathInfo i = FcFilePathInfoGet (path);
+ FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
+ i.dir_len, FcTrue);
+ FcGlobalCacheFile *f, **prev;
+ int size;
+
+ if (!d)
+ return 0;
+ for (prev = &d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE];
+ (f = *prev);
+ prev = &(*prev)->next)
+ {
+ if (f->info.hash == i.base_hash &&
+ f->id == id &&
+ !strcmp ((const char *) f->info.file, (const char *) i.base))
+ {
+ break;
+ }
+ }
+ if (*prev)
+ {
+ if (!replace)
+ return 0;
+
+ f = *prev;
+ if (f->info.referenced)
+ cache->referenced--;
+ *prev = f->next;
+ FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheFile) +
+ strlen ((char *) f->info.file) + 1 +
+ strlen ((char *) f->name) + 1);
+ free (f);
+ }
+ size = (sizeof (FcGlobalCacheFile) +
+ strlen ((char *) i.base) + 1 +
+ strlen ((char *) name) + 1);
+ f = malloc (size);
+ if (!f)
+ return 0;
+ FcMemAlloc (FC_MEM_CACHE, size);
+ f->next = *prev;
+ *prev = f;
+ f->info.hash = i.base_hash;
+ f->info.file = (FcChar8 *) (f + 1);
+ f->info.time = time;
+ f->info.referenced = FcFalse;
+ f->id = id;
+ f->name = f->info.file + strlen ((char *) i.base) + 1;
+ strcpy ((char *) f->info.file, (const char *) i.base);
+ strcpy ((char *) f->name, (const char *) name);
+ return &f->info;
+}
+