- 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)
- {
- hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
- hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
- }
- hex_hash[32] = 0;
-
- tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
- if (!tmp)
- return 0;
-
- cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
- free (tmp);
-
- return cache_hashed;
-}
-
-/* Opens the hashed name for cache_file.
- * This would fail in the unlikely event of a collision and subsequent
- * removal of the file which originally caused the collision. */
-static int
-FcDirCacheOpen (const FcChar8 *dir)
-{
- FcBool found;
- int fd = -1, collisions = 0;
- char *cache_hashed;
- char dir_buf[FC_MAX_FILE_LEN];
- struct stat dir_stat;
-
- if (stat ((char *)dir, &dir_stat) == -1)
- return -1;
-
- found = FcFalse;
- do
- {
- struct stat c;
-
- if (fd >= 0)
- close (fd);
-
- cache_hashed = FcDirCacheHashName (dir, collisions++);
- if (!cache_hashed)
- return -1;
-
- fd = open(cache_hashed, O_RDONLY | O_BINARY);
- FcStrFree ((FcChar8 *)cache_hashed);
-
- if (fd == -1)
- break;
- if (!FcCacheReadString (fd, dir_buf, sizeof (dir_buf)) ||
- !strlen(dir_buf))
- break;
-
- if (stat ((char *)dir_buf, &c) == -1)
- continue;
-
- found = (c.st_ino == dir_stat.st_ino) && (c.st_dev == dir_stat.st_dev);
- } while (!found);
-
- return fd;
-}
-
-/* read serialized state from the cache file */
-FcBool
-FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
-{
- int fd;
- char *current_arch_machine_name;
- char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
- off_t current_arch_start = 0;
- 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)
- goto bail1;
-
- lseek (fd, current_arch_start, SEEK_SET);
- if (FcCacheReadString (fd, candidate_arch_machine_name,
- sizeof (candidate_arch_machine_name)) == 0)
- goto bail1;
-
- while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
- FcStrSetAdd (dirs, (FcChar8 *)subdirName);
-
- if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
- goto bail1;
-
- close(fd);
- return FcTrue;
-
- bail1:
- close (fd);
- bail:
- return FcFalse;
-}
-
-static FcBool
-FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
-{
- FcCache metadata;
- void * current_dir_block;
- off_t pos, endpos;
-
- if (read(fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
- return FcFalse;
- if (metadata.magic != FC_CACHE_MAGIC)
- return FcFalse;
-
- if (!metadata.count)
- {
- pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
- lseek (fd, pos, SEEK_SET);
- if (config)
- FcConfigAddFontDir (config, (FcChar8 *)dir);
- return FcTrue;
- }
-
- pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
-
- /* This is not failsafe (multi-arches can break it),
- * but fd has got to have at least as many bytes as
- * metadata.count, or something's going to go horribly wrong. */
- if (pos == (off_t)-1)
- return FcFalse;
-
- endpos = lseek (fd, 0, SEEK_END);
- if (endpos == (off_t)-1 || endpos - pos < metadata.count)
- return FcFalse;
- if (lseek (fd, pos, SEEK_SET) == -1)
- return FcFalse;
-
-#if defined(HAVE_MMAP) || defined(__CYGWIN__)
- current_dir_block = mmap (0, metadata.count,
- PROT_READ, MAP_SHARED, fd, pos);
- if (current_dir_block == MAP_FAILED)
- return FcFalse;
-#elif defined(_WIN32)
- {
- HANDLE hFileMap;
-
- hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL, PAGE_READONLY, 0, 0, NULL);
- if (hFileMap == NULL)
- return FcFalse;
-
- current_dir_block = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0, metadata.count + pos);
- if (current_dir_block == NULL)