- current_arch_start = lseek(fd, 0, SEEK_CUR);
-#if defined (HAVE_FTRUNCATE)
- if (ftruncate (fd, current_arch_start) == -1)
- goto bail3;
-#else
-#if defined (HAVE_CHSIZE)
- if (chsize (fd, current_arch_start) == -1)
- goto bail3;
-#else
- goto bail3;
-#endif
-#endif
-
- 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)
- {
- int i;
-
- 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;
-
- free (header);
-
- for (dir = cache->dirs; dir; dir = dir->next)
- {
- int i;
- 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;
-}
-
-/*
- * Find the next presumably-mmapable offset after the supplied file
- * position.
- */
-static int
-FcCacheNextOffset(off_t w)
-{
- static long pagesize = -1;
-
- if (w == -1)
- return w;
-
- if (pagesize == -1)
-#if defined (HAVE_SYSCONF)
- pagesize = sysconf(_SC_PAGESIZE);
-#else
- pagesize = FC_HARDCODED_PAGESIZE;
-#endif
- if (w % pagesize == 0)
- return w;
- else
- return ((w / pagesize)+1)*pagesize;
-}
-
-/* return the address of the segment for the provided arch,
- * or -1 if arch not found */
-static off_t
-FcCacheSkipToArch (int fd, const char * arch)
-{
- char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
- char * candidate_arch;
- off_t current_arch_start = 0;
-
- lseek (fd, 0, SEEK_SET);
- FcCacheSkipString (fd);
- current_arch_start = lseek (fd, 0, SEEK_CUR);
-
- /* skip arches that are not the current arch */
- while (1)
- {
- long bs;
-
- if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
- return -1;
-
- if (FcCacheReadString (fd, candidate_arch_machine_name_count,
- sizeof (candidate_arch_machine_name_count)) == 0)
- return -1;
- if (!strlen(candidate_arch_machine_name_count))
- return -1;
- bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
-
- /* count = 0 should probably be distinguished from the !bs condition */
- if (!bs || bs < strlen (candidate_arch_machine_name_count))
- return -1;
-
- candidate_arch++; /* skip leading space */
-
- if (strcmp (candidate_arch, arch)==0)
- return current_arch_start;
- current_arch_start += bs;
- current_arch_start = FcCacheNextOffset (current_arch_start);
- }
-}
-
-/* Cuts out the segment at the file pointer (moves everything else
- * down to cover it), and leaves the file pointer at the end of the
- * file. */
-static FcBool
-FcCacheCopyOld (int fd, int fd_orig, off_t start)
-{
- char * buf = malloc (8192);
- char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
- long bs;
- int c, bytes_skipped;
- off_t loc;
-
- if (!buf)
- return FcFalse;
-
- loc = 0;
- lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
- FcCacheSkipString (fd); FcCacheSkipString (fd_orig);
- do
- {
- int b = 8192;
- if (loc + b > start)
- b = start - loc;
-
- if ((c = read (fd_orig, buf, b)) <= 0)
- break;
- if (write (fd, buf, c) < 0)
- goto bail;
-
- loc += c;
- }
- while (c > 0);
-
- lseek (fd, start, SEEK_SET);
- if (FcCacheReadString (fd, candidate_arch_machine_name,
- sizeof (candidate_arch_machine_name)) == 0)
- goto done;
- if (!strlen(candidate_arch_machine_name))
- goto done;
-
- bs = strtol(candidate_arch_machine_name, 0, 16);
- if (bs == 0)
- goto done;
-
- bytes_skipped = 0;
- do
- {
- lseek (fd, start+bs+bytes_skipped, SEEK_SET);
- if ((c = read (fd, buf, 8192)) <= 0)
- break;
- lseek (fd, start+bytes_skipped, SEEK_SET);
- if (write (fd, buf, c) < 0)
- goto bail;
- bytes_skipped += c;
- }
- while (c > 0);
- lseek (fd, start+bytes_skipped, SEEK_SET);
-
- done:
- free (buf);
- return FcTrue;
-
- bail:
- free (buf);
- return FcFalse;
-}
-
-/* Does not check that the cache has the appropriate arch section. */
-/* Also, this can be fooled if the original location has a stale
- * cache, and the hashed location has an up-to-date cache. Oh well,
- * sucks to be you in that case! */
-FcBool
-FcDirCacheValid (const FcChar8 *dir)
-{
- struct stat file_stat, dir_stat;
- int fd;
-
- if (stat ((char *) dir, &dir_stat) < 0)
- return FcFalse;
-
- fd = FcDirCacheOpen (dir);
-
- if (fd < 0)
- return FcFalse;
- if (fstat (fd, &file_stat) < 0)
- goto bail;
-
- close (fd);
-
- /*
- * If the directory has been modified more recently than
- * the cache file, the cache is not valid
- */
- if (dir_stat.st_mtime > file_stat.st_mtime)
- return FcFalse;
-
- return FcTrue;
-
- bail:
- close (fd);
- return FcFalse;
-}
-
-/* Assumes that the cache file in 'dir' exists.
- * Checks that the cache has the appropriate arch section. */
-FcBool
-FcDirCacheHasCurrentArch (const FcChar8 *dir)
-{
- int fd;
- off_t current_arch_start;
- char *current_arch_machine_name;
- FcCache metadata;
- char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
-
- fd = FcDirCacheOpen (dir);
- if (fd < 0)
- goto bail;
-
- current_arch_machine_name = FcCacheMachineSignature();
- current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
-
- if (current_arch_start >= 0)
- {
- if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
- goto bail1;
-
- FcCacheSkipString (fd);
-
- while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
- ;
-
- if (read(fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
- goto bail1;
-
- if (metadata.magic != FC_CACHE_MAGIC)
- goto bail1;
- }
-
- close (fd);
-
- if (current_arch_start < 0)
- return FcFalse;
-
- return FcTrue;
-
- bail1:
- close (fd);
- bail:
- return FcFalse;
-}
-
-FcBool
-FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
-{
- char *cache_hashed = 0;
- int fd, collisions;
- struct stat cache_stat;
- char dir_buf[FC_MAX_FILE_LEN];
-
- dir = FcConfigNormalizeFontDir (config, dir);
-
- /* Remove any applicable hashed files. */
- fd = -1; collisions = 0;
- do
- {
- if (cache_hashed)
- FcStrFree ((FcChar8 *)cache_hashed);
-
- cache_hashed = FcDirCacheHashName (dir, collisions++);
- if (!cache_hashed)
- goto bail;
-
- if (fd > 0)
- close (fd);
- fd = open(cache_hashed, O_RDONLY | O_BINARY);
- if (fd == -1)
- {
- FcStrFree ((FcChar8 *)cache_hashed);
- return FcTrue;
- }
-
- if (!FcCacheReadString (fd, dir_buf, sizeof (dir_buf)) || !strlen(dir_buf))
- {
- FcStrFree ((FcChar8 *)cache_hashed);
- goto bail;
- }
- } while (strcmp ((char *) dir_buf, (char *) dir) != 0);
-
- close (fd);
-
- if (stat ((char *) cache_hashed, &cache_stat) == 0 &&
- unlink ((char *)cache_hashed) != 0)
- {
- FcStrFree ((FcChar8 *)cache_hashed);
- goto bail;
- }
-
- FcStrFree ((FcChar8 *)cache_hashed);
- return FcTrue;
-
- bail:
- return FcFalse;
-}
-
-static int
-FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
- FcStrList *list, FcFontSet * set, FcStrSet *processed_dirs)
-{
- int ret = 0;
- FcChar8 *dir;
- FcStrSet *subdirs;
- FcStrList *sublist;
- struct stat statb;
- FcGlobalCacheDir *d;
-
- /*
- * Read in the results from 'list'.
- */
- while ((dir = FcStrListNext (list)))
- {
- if (!FcConfigAcceptFilename (config, dir))
- continue;
-
- /* Skip this directory if already updated
- * to avoid the looped directories via symlinks
- * Clearly a dir not in fonts.conf shouldn't be globally cached.
- */
- dir = (FcChar8 *)FcConfigNormalizeFontDir (config, dir);
- if (!dir)
- continue;
-
- if (FcStrSetMember (processed_dirs, dir))
- continue;
- if (!FcStrSetAdd (processed_dirs, dir))
- continue;
-
- subdirs = FcStrSetCreate ();
- if (!subdirs)
- {
- fprintf (stderr, "Can't create directory set\n");
- ret++;
- continue;
- }
-
- if (access ((char *) dir, X_OK) < 0)
- {
- switch (errno) {
- case ENOENT:
- case ENOTDIR:
- case EACCES:
- break;
- default:
- fprintf (stderr, "\"%s\": ", dir);
- perror ("");
- ret++;
- }
- FcStrSetDestroy (subdirs);
- continue;
- }
- if (stat ((char *) dir, &statb) == -1)
- {
- fprintf (stderr, "\"%s\": ", dir);
- perror ("");
- FcStrSetDestroy (subdirs);
- ret++;
- continue;
- }
- if (!S_ISDIR (statb.st_mode))
- {
- fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
- FcStrSetDestroy (subdirs);
- continue;
- }
- if (FcDirCacheValid (dir) &&
- FcDirCacheHasCurrentArch (dir) &&
- FcDirCacheRead (set, subdirs, dir, config))
- {
- /* if an old entry is found in the global cache, disable it */
- if ((d = FcGlobalCacheDirFind (cache, (const char *)dir)) != NULL)
- {
- d->state = FcGCDirDisabled;
- /* save the updated config later without this entry */
- cache->updated = FcTrue;
- }
- }
- else
- {
- if (FcDebug () & FC_DBG_FONTSET)
- printf ("cache scan dir %s\n", dir);
-
- FcDirScanConfig (set, subdirs, cache,
- config->blanks, dir, FcFalse, config);
- }
- sublist = FcStrListCreate (subdirs);
- FcStrSetDestroy (subdirs);
- if (!sublist)
- {
- fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
- ret++;
- continue;
- }
- ret += FcCacheReadDirs (config, cache, sublist, set, processed_dirs);
- }
- FcStrListDone (list);
- return ret;
-}
-
-FcFontSet *
-FcCacheRead (FcConfig *config, FcGlobalCache * cache)
-{
- FcFontSet *s = FcFontSetCreate();
- FcStrSet *processed_dirs;
-
- if (!s)
- return 0;
-
- processed_dirs = FcStrSetCreate();
- if (!processed_dirs)
- goto bail;
-
- if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s, processed_dirs))
- goto bail1;
-
- FcStrSetDestroy (processed_dirs);
- return s;
-
- bail1:
- FcStrSetDestroy (processed_dirs);
- bail:
- FcFontSetDestroy (s);
- return 0;
-}
-
-static const char bin2hex[] = { '0', '1', '2', '3',
- '4', '5', '6', '7',
- '8', '9', 'a', 'b',
- 'c', 'd', 'e', 'f' };
-
-static char *
-FcDirCacheHashName (const FcChar8 * dir, int collisions)
-{
- unsigned char hash[16], hex_hash[33];
- char *cache_hashed;
- unsigned char uscore = '_';
- int cnt, i;
- FcChar8 *tmp;
- struct MD5Context ctx;
-
- MD5Init (&ctx);
- MD5Update (&ctx, (unsigned char *)dir, strlen ((char *) dir));
-
- for (i = 0; i < collisions; i++)
- MD5Update (&ctx, &uscore, 1);
-
- MD5Final (hash, &ctx);
-
- for (cnt = 0; cnt < 16; ++cnt)