- 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);
- 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)
- goto bail;
- 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;
-
- fd = FcDirCacheOpen (dir);
- if (fd < 0)
- goto bail;
-
- current_arch_machine_name = FcCacheMachineSignature();
- current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
- close (fd);
-
- if (current_arch_start < 0)
- return FcFalse;
-
- return FcTrue;
-
- bail:
- return FcFalse;
-}
-
-FcBool
-FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
-{
- char *cache_file;
- char *cache_hashed = 0;
- int fd, collisions;
- struct stat cache_stat;
- char name_buf[FC_MAX_FILE_LEN];
-
- dir = FcConfigNormalizeFontDir (config, dir);
- cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
- if (!cache_file)
- return FcFalse;
-
- /* First remove normal cache file. */
- if (stat ((char *) cache_file, &cache_stat) == 0 &&
- unlink ((char *)cache_file) != 0)
- goto bail;
-
- /* Next remove any applicable hashed files. */
- fd = -1; collisions = 0;
- do
- {
- if (cache_hashed)
- FcStrFree ((FcChar8 *)cache_hashed);
-
- cache_hashed = FcDirCacheHashName (cache_file, collisions++);
- if (!cache_hashed)
- goto bail;
-
- if (fd > 0)
- close (fd);
- fd = open(cache_hashed, O_RDONLY);
- if (fd == -1)
- {
- FcStrFree ((FcChar8 *)cache_file);
- return FcTrue;
- }
-
- if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
- {
- FcStrFree ((FcChar8 *)cache_hashed);
- goto bail;
- }
- } while (strcmp (name_buf, cache_file) != 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_file);
- FcStrFree ((FcChar8 *)cache_hashed);
- return FcTrue;
-
- bail:
- FcStrFree ((FcChar8 *)cache_file);
- 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) && 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;