-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 | O_BINARY);
- 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 bail1;
-
- 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;
-
- d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
- d->ent = 0;
- d->state = FcGCDirFileRead;
-
- d->subdirs = FcStrSetCreate();
- do
- {
- if (!FcCacheReadString (cache->fd, subdirName,
- sizeof (subdirName)) ||
- !strlen (subdirName))
- break;
- FcStrSetAdd (d->subdirs, (FcChar8 *)subdirName);
- } while (1);
-
- d->offset = lseek (cache->fd, 0, SEEK_CUR);
- if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
- goto bail1;
- targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
- if (lseek (cache->fd, targ, SEEK_SET) != targ)
- goto bail1;
- }
- return;
-
- bail1:
- for (d = cache->dirs; d; d = next)
- {
- next = d->next;
- free (d);
- }
- cache->dirs = 0;
-
- close (cache->fd);
- cache->fd = -1;
- return;
-
- bail_and_destroy:
- close (cache->fd);
- cache->fd = -1;
-
- if (stat ((char *) cache_file, &cache_stat) == 0)
- unlink ((char *)cache_file);
-
- return;
-
-}
-
-FcBool
-FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
-{
- FcGlobalCacheDir *d;
- int i;
-
- if (cache->fd == -1)
- return FcFalse;
-
- if (!(dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir)))
- return FcFalse; /* non-existing directory */
-
- for (d = cache->dirs; d; d = d->next)
- {
- if (strcmp (d->name, dir) == 0)
- {
- if (d->state == FcGCDirDisabled)
- return FcFalse;
-
- if (d->state == FcGCDirFileRead)
- {
- lseek (cache->fd, d->offset, SEEK_SET);
- if (!FcDirCacheConsume (cache->fd, d->name, set, config))
- return FcFalse;
-
- for (i = 0; i < d->subdirs->num; i++)
- FcStrSetAdd (dirs, (FcChar8 *)d->subdirs->strs[i]);
-
- d->state = FcGCDirConsumed;
- }
- return FcTrue;
- }
- }
-
- return FcFalse;
-}
-
-static FcGlobalCacheDir *
-FcGlobalCacheDirFind (FcGlobalCache *cache, const char *name)
-{
- FcGlobalCacheDir * d;
-
- if (!cache || !name)
- return NULL;
-
- for (d = cache->dirs; d; d = d->next)
- if (strcmp((const char *)d->name, (const char *)name) == 0)
- return d;
-
- return NULL;
- }
-
-FcBool
-FcGlobalCacheUpdate (FcGlobalCache *cache,
- FcStrSet *dirs,
- const char *orig_name,
- FcFontSet *set,
- FcConfig *config)
-{
- FcGlobalCacheDir *d;
- int i;
- const char *name;
-
- name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)orig_name);
- if (!name)
- {
- fprintf(stderr, "Invalid directory name %s\n", orig_name);
- return FcFalse;
- }
-
- d = FcGlobalCacheDirFind (cache, name);
-
- if (!d)
- {
- d = malloc (sizeof (FcGlobalCacheDir));
- if (!d)
- return FcFalse;
- d->next = cache->dirs;
- cache->dirs = d;
- } else {
- /* free old resources */
- FcStrFree ((FcChar8 *)d->name);
- free (d->ent);
- FcStrSetDestroy (d->subdirs);
- }
-
- cache->updated = FcTrue;
-
- d->name = (char *)FcStrCopy ((FcChar8 *)name);
- d->ent = FcDirCacheProduce (set, &d->metadata);
- d->offset = 0;
- d->subdirs = FcStrSetCreate();
- d->state = FcGCDirUpdated;
- for (i = 0; i < dirs->num; i++)
- FcStrSetAdd (d->subdirs, dirs->strs[i]);
- return FcTrue;
-}
-
-FcBool
-FcGlobalCacheSave (FcGlobalCache *cache,
- const FcChar8 *cache_file,
- FcConfig *config)
-{
- int fd, fd_orig, i;
- FcGlobalCacheDir *dir;
- FcAtomic *atomic;
- off_t current_arch_start = 0, truncate_to;
- char * current_arch_machine_name, * header;
-
- if (!cache->updated)
- return FcTrue;
-
-#if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
- /* Set-UID programs can't safely update the cache */
- if (getuid () != geteuid ())
- return FcFalse;
-#endif
-
- atomic = FcAtomicCreate (cache_file);
- if (!atomic)
- return FcFalse;
-
- if (!FcAtomicLock (atomic))
- goto bail1;
- fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT | O_BINARY,
- S_IRUSR | S_IWUSR);
- if (fd == -1)
- goto bail2;
- FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
-
- fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY | O_BINARY);
-
- current_arch_machine_name = FcCacheMachineSignature ();
- if (fd_orig == -1)
- current_arch_start = 0;
- else
- current_arch_start = FcCacheSkipToArch (fd_orig,
- current_arch_machine_name);
-
- if (current_arch_start < 0)
- {
- off_t i = lseek(fd_orig, 0, SEEK_END);
- if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
- i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
- current_arch_start = FcCacheNextOffset (i);
- }
-
- if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
- goto bail3;
-
- current_arch_start = lseek(fd, 0, SEEK_CUR);
- if (ftruncate (fd, current_arch_start) == -1)
- goto bail3;
-
- header = malloc (10 + strlen (current_arch_machine_name));
- if (!header)
- goto bail3;
-
- truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
- for (dir = cache->dirs; dir; dir = dir->next)
- {
- if (dir->state == FcGCDirDisabled)
- continue;
- truncate_to += strlen(dir->name) + 1;
- truncate_to += sizeof (FcCache);
- truncate_to = FcCacheNextOffset (truncate_to);
- truncate_to += dir->metadata.count;
-
- for (i = 0; i < dir->subdirs->size; i++)
- truncate_to += strlen((char *)dir->subdirs->strs[i]) + 1;
- truncate_to ++;
- }
- truncate_to -= current_arch_start;
-
- sprintf (header, "%8x ", (int)truncate_to);
- strcat (header, current_arch_machine_name);
- if (!FcCacheWriteString (fd, header))
- goto bail4;
-
- for (dir = cache->dirs; dir; dir = dir->next)
- {
- const char * d;
- off_t off;
-
- if (!dir->name || dir->state == FcGCDirDisabled)
- continue;
- d = (const char *)FcConfigNormalizeFontDir (config, (const FcChar8 *)dir->name);
- if (!d)
- continue;
-
- if (dir->metadata.count && !dir->ent)
- {
- if (dir->state == FcGCDirUpdated || fd_orig < 0)
- {
- fprintf(stderr, "Invalid metadata entry for %s, skipping...\n", d);
- continue;
- }
- /* copy the old content */
- dir->ent = malloc (dir->metadata.count);
- if (!dir->ent)
- {
- perror("malloc error");
- continue;
- }
- off = FcCacheNextOffset (dir->offset + sizeof(FcCache));
- if (lseek (fd_orig, off, SEEK_SET) != off)
- {
- perror("lseek");
- free(dir->ent);
- continue;
- }
- if (read (fd_orig, dir->ent, dir->metadata.count)
- != dir->metadata.count)
- {
- perror("read");
- free(dir->ent);
- continue;
- }
- }
-
- FcCacheWriteString (fd, d);
-
- for (i = 0; i < dir->subdirs->size; i++)
- FcCacheWriteString (fd, (char *)dir->subdirs->strs[i]);
- FcCacheWriteString (fd, "");
-
- if (write (fd, &dir->metadata, sizeof(FcCache)) != sizeof(FcCache))
- {
- perror ("write metadata");
- free (dir->ent);
- continue;
- }
- off = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
- if (lseek (fd, off, SEEK_SET) != off)
- {
- perror ("lseek");
- free (dir->ent);
- continue;
- }
- if (dir->metadata.count)
- {
- if (write (fd, dir->ent, dir->metadata.count) != dir->metadata.count)
- {
- perror ("write dirent");
- free (dir->ent);
- continue;
- }
- }
- free (dir->ent);
- }
- FcCacheWriteString (fd, "");
-
- if (close (fd) == -1)
- goto bail25;
-
- close (fd_orig);
- fd_orig = -1;
-
- if (!FcAtomicReplaceOrig (atomic))
- goto bail25;
-
- FcAtomicUnlock (atomic);
- FcAtomicDestroy (atomic);
-
- cache->updated = FcFalse;
- return FcTrue;
-
- bail4:
- free (header);
- bail3:
- if (fd_orig != -1)
- close (fd_orig);
-
- close (fd);
- bail25:
- FcAtomicDeleteNew (atomic);
- bail2:
- FcAtomicUnlock (atomic);
- bail1:
- FcAtomicDestroy (atomic);
- return FcFalse;
-}
-