-#define FC_DBG_CACHE_REF 1024
-
-static char *
-FcCacheReadString (int fd, char *dest, int len)
-{
- int size;
- int slen;
-
- if (len == 0)
- return 0;
-
- size = read (fd, dest, len-1);
-
- if (size > 0)
- {
- dest[size] = '\0';
- slen = strlen (dest);
-
- lseek (fd, slen - size + 1, SEEK_CUR);
- return slen < len ? dest : 0;
- }
-
- return 0;
-}
-
-static void
-FcCacheSkipString (int fd)
-{
- char buf[256];
- int size;
- int slen;
-
- while ( (size = read (fd, buf, sizeof (buf)-1)) > 0)
- {
- buf [size] = '\0';
- slen = strlen (buf);
- if (slen < size)
- {
- lseek (fd, slen - size + 1, SEEK_CUR);
- return;
- }
- }
-}
-
-static FcBool
-FcCacheWriteString (int fd, const char *chars)
-{
- if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
- return FcFalse;
- return FcTrue;
-}
-
-static void
-FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
-{
- FcStrSetDestroy (d->subdirs);
- FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
- free (d->name);
- FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
- free (d);
-}
-
-FcGlobalCache *
-FcGlobalCacheCreate (void)
-{
- FcGlobalCache *cache;
-
- cache = malloc (sizeof (FcGlobalCache));
- if (!cache)
- return 0;
- FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
- cache->dirs = 0;
- cache->updated = FcFalse;
- cache->fd = -1;
- return cache;
-}
-
-void
-FcGlobalCacheDestroy (FcGlobalCache *cache)
-{
- FcGlobalCacheDir *d, *next;
-
- for (d = cache->dirs; d; d = next)
- {
- next = d->next;
- FcGlobalCacheDirDestroy (d);
- }
- FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
- free (cache);
-}
-
-void
-FcGlobalCacheLoad (FcGlobalCache *cache,
- FcStrSet *staleDirs,
- const FcChar8 *cache_file,
- FcConfig *config)
-{
- char name_buf[FC_MAX_FILE_LEN];
- FcGlobalCacheDir *d, *next;
- FcFileTime config_time = FcConfigModifiedTime (config);
- char * current_arch_machine_name;
- char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
- off_t current_arch_start;
-
- struct stat cache_stat, dir_stat;
- char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
-
- if (stat ((char *) cache_file, &cache_stat) < 0)
- return;
-
- cache->fd = open ((char *) cache_file, O_RDONLY);
- if (cache->fd == -1)
- return;
-
- cache->updated = FcFalse;
-
- if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)))
- goto bail_and_destroy;
- if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0)
- goto bail_and_destroy;
-
- current_arch_machine_name = FcCacheMachineSignature ();
- current_arch_start = FcCacheSkipToArch(cache->fd,
- current_arch_machine_name);
- if (current_arch_start < 0)
- goto bail_and_destroy;
-
- lseek (cache->fd, current_arch_start, SEEK_SET);
- if (!FcCacheReadString (cache->fd, candidate_arch_machine_name,
- sizeof (candidate_arch_machine_name)))
- goto bail_and_destroy;
- if (strlen(candidate_arch_machine_name) == 0)
- goto bail_and_destroy;
-
- while (1)
- {
- off_t targ;
-
- if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)) ||
- !strlen(name_buf))
- break;
-
- /* Directory must be older than the global cache file; also
- cache must be newer than the config file. */
- if (stat ((char *) name_buf, &dir_stat) < 0 ||
- dir_stat.st_mtime > cache_stat.st_mtime ||
- (config_time.set && cache_stat.st_mtime < config_time.time))
- {
- FcCache md;
- off_t off;
-
- FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
-
- /* skip subdirs */
- while (FcCacheReadString (cache->fd, subdirName,
- sizeof (subdirName)) &&
- strlen (subdirName))
- ;
-
- if (read (cache->fd, &md, sizeof (FcCache)) != sizeof(FcCache))
- {
- perror ("read metadata");
- goto bail1;
- }
- off = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count;
- if (lseek (cache->fd, off, SEEK_SET) != off)
- {
- perror ("lseek");
- goto bail1;
- }
- continue;
- }
-
- d = malloc (sizeof (FcGlobalCacheDir));
- if (!d)
- goto bail1;
-
- d->next = cache->dirs;
- cache->dirs = d;