2 * $RCSId: xc/lib/fontconfig/src/fccache.c,v 1.12 2002/08/22 07:36:44 keithp Exp $
4 * Copyright © 2000 Keith Packard
5 * Copyright © 2005 Patrick Lam
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Keith Packard not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission. Keith Packard makes no
14 * representations about the suitability of this software for any purpose. It
15 * is provided "as is" without express or implied warranty.
17 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
30 #include <sys/utsname.h>
31 #include <sys/types.h>
36 #define ENDIAN_TEST 0x12345678
37 #define MACHINE_SIGNATURE_SIZE (9 + 5*20 + 1)
44 FcDirCacheOpen (const FcChar8 * dir);
47 FcDirCacheHashName (char * cache_file, int collisions);
50 FcCacheSkipToArch (int fd, const char * arch);
53 FcCacheCopyOld (int fd, int fd_orig, off_t start);
56 FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
59 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config);
62 FcCacheNextOffset(off_t w);
65 FcCacheMachineSignature (void);
68 FcCacheHaveBank (int bank);
71 FcCacheAddBankDir (int bank, const char * dir);
79 static void MD5Init(struct MD5Context *ctx);
80 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len);
81 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
82 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
84 #define FC_DBG_CACHE_REF 1024
87 FcCacheReadString (int fd, char *dest, int len)
95 size = read (fd, dest, len-1);
100 slen = strlen (dest);
102 lseek (fd, slen - size + 1, SEEK_CUR);
103 return slen < len ? dest : 0;
110 FcCacheSkipString (int fd)
116 while ( (size = read (fd, buf, sizeof (buf)-1)) > 0)
122 lseek (fd, slen - size + 1, SEEK_CUR);
129 FcCacheWriteString (int fd, const char *chars)
131 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
137 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
139 FcStrSetDestroy (d->subdirs);
140 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
142 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
147 FcGlobalCacheCreate (void)
149 FcGlobalCache *cache;
151 cache = malloc (sizeof (FcGlobalCache));
154 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
156 cache->updated = FcFalse;
162 FcGlobalCacheDestroy (FcGlobalCache *cache)
164 FcGlobalCacheDir *d, *next;
166 for (d = cache->dirs; d; d = next)
169 FcGlobalCacheDirDestroy (d);
171 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
176 FcGlobalCacheLoad (FcGlobalCache *cache,
178 const FcChar8 *cache_file,
181 char name_buf[FC_MAX_FILE_LEN];
182 FcGlobalCacheDir *d, *next;
183 FcFileTime config_time = FcConfigModifiedTime (config);
184 char * current_arch_machine_name;
185 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
186 off_t current_arch_start;
188 struct stat cache_stat, dir_stat;
189 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
191 if (stat ((char *) cache_file, &cache_stat) < 0)
194 cache->fd = open ((char *) cache_file, O_RDONLY | O_BINARY);
198 cache->updated = FcFalse;
200 if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)))
201 goto bail_and_destroy;
202 if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0)
203 goto bail_and_destroy;
205 current_arch_machine_name = FcCacheMachineSignature ();
206 current_arch_start = FcCacheSkipToArch(cache->fd,
207 current_arch_machine_name);
208 if (current_arch_start < 0)
211 lseek (cache->fd, current_arch_start, SEEK_SET);
212 if (!FcCacheReadString (cache->fd, candidate_arch_machine_name,
213 sizeof (candidate_arch_machine_name)))
214 goto bail_and_destroy;
215 if (strlen(candidate_arch_machine_name) == 0)
216 goto bail_and_destroy;
222 if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)) ||
226 /* Directory must be older than the global cache file; also
227 cache must be newer than the config file. */
228 if (stat ((char *) name_buf, &dir_stat) < 0 ||
229 dir_stat.st_mtime > cache_stat.st_mtime ||
230 (config_time.set && cache_stat.st_mtime < config_time.time))
235 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
238 while (FcCacheReadString (cache->fd, subdirName,
239 sizeof (subdirName)) &&
243 if (read (cache->fd, &md, sizeof (FcCache)) != sizeof(FcCache))
245 perror ("read metadata");
248 off = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count;
249 if (lseek (cache->fd, off, SEEK_SET) != off)
257 d = malloc (sizeof (FcGlobalCacheDir));
261 d->next = cache->dirs;
264 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
266 d->state = FcGCDirFileRead;
268 d->subdirs = FcStrSetCreate();
271 if (!FcCacheReadString (cache->fd, subdirName,
272 sizeof (subdirName)) ||
273 !strlen (subdirName))
275 FcStrSetAdd (d->subdirs, (FcChar8 *)subdirName);
278 d->offset = lseek (cache->fd, 0, SEEK_CUR);
279 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
281 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
282 if (lseek (cache->fd, targ, SEEK_SET) != targ)
288 for (d = cache->dirs; d; d = next)
303 if (stat ((char *) cache_file, &cache_stat) == 0)
304 unlink ((char *)cache_file);
311 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
319 if (!(dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir)))
320 return FcFalse; /* non-existing directory */
322 for (d = cache->dirs; d; d = d->next)
324 if (strcmp (d->name, dir) == 0)
326 if (d->state == FcGCDirDisabled)
329 if (d->state == FcGCDirFileRead)
331 lseek (cache->fd, d->offset, SEEK_SET);
332 if (!FcDirCacheConsume (cache->fd, d->name, set, config))
335 for (i = 0; i < d->subdirs->num; i++)
336 FcStrSetAdd (dirs, (FcChar8 *)d->subdirs->strs[i]);
338 d->state = FcGCDirConsumed;
347 static FcGlobalCacheDir *
348 FcGlobalCacheDirFind (FcGlobalCache *cache, const char *name)
350 FcGlobalCacheDir * d;
355 for (d = cache->dirs; d; d = d->next)
356 if (strcmp((const char *)d->name, (const char *)name) == 0)
363 FcGlobalCacheUpdate (FcGlobalCache *cache,
365 const char *orig_name,
373 name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)orig_name);
376 fprintf(stderr, "Invalid directory name %s\n", orig_name);
380 d = FcGlobalCacheDirFind (cache, name);
384 d = malloc (sizeof (FcGlobalCacheDir));
387 d->next = cache->dirs;
390 /* free old resources */
391 FcStrFree ((FcChar8 *)d->name);
393 FcStrSetDestroy (d->subdirs);
396 cache->updated = FcTrue;
398 d->name = (char *)FcStrCopy ((FcChar8 *)name);
399 d->ent = FcDirCacheProduce (set, &d->metadata);
401 d->subdirs = FcStrSetCreate();
402 d->state = FcGCDirUpdated;
403 for (i = 0; i < dirs->num; i++)
404 FcStrSetAdd (d->subdirs, dirs->strs[i]);
409 FcGlobalCacheSave (FcGlobalCache *cache,
410 const FcChar8 *cache_file,
414 FcGlobalCacheDir *dir;
416 off_t current_arch_start = 0, truncate_to;
417 char * current_arch_machine_name, * header;
422 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
423 /* Set-UID programs can't safely update the cache */
424 if (getuid () != geteuid ())
428 atomic = FcAtomicCreate (cache_file);
432 if (!FcAtomicLock (atomic))
434 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT | O_BINARY,
438 FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
440 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY | O_BINARY);
442 current_arch_machine_name = FcCacheMachineSignature ();
444 current_arch_start = 0;
446 current_arch_start = FcCacheSkipToArch (fd_orig,
447 current_arch_machine_name);
449 if (current_arch_start < 0)
451 off_t i = lseek(fd_orig, 0, SEEK_END);
452 if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
453 i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
454 current_arch_start = FcCacheNextOffset (i);
457 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
460 current_arch_start = lseek(fd, 0, SEEK_CUR);
461 if (ftruncate (fd, current_arch_start) == -1)
464 header = malloc (10 + strlen (current_arch_machine_name));
468 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
469 for (dir = cache->dirs; dir; dir = dir->next)
473 if (dir->state == FcGCDirDisabled)
475 truncate_to += strlen(dir->name) + 1;
476 truncate_to += sizeof (FcCache);
477 truncate_to = FcCacheNextOffset (truncate_to);
478 truncate_to += dir->metadata.count;
480 for (i = 0; i < dir->subdirs->size; i++)
481 truncate_to += strlen((char *)dir->subdirs->strs[i]) + 1;
484 truncate_to -= current_arch_start;
486 sprintf (header, "%8x ", (int)truncate_to);
487 strcat (header, current_arch_machine_name);
488 if (!FcCacheWriteString (fd, header))
491 for (dir = cache->dirs; dir; dir = dir->next)
497 if (!dir->name || dir->state == FcGCDirDisabled)
499 d = (const char *)FcConfigNormalizeFontDir (config, (const FcChar8 *)dir->name);
503 if (dir->metadata.count && !dir->ent)
505 if (dir->state == FcGCDirUpdated || fd_orig < 0)
507 fprintf(stderr, "Invalid metadata entry for %s, skipping...\n", d);
510 /* copy the old content */
511 dir->ent = malloc (dir->metadata.count);
514 perror("malloc error");
517 off = FcCacheNextOffset (dir->offset + sizeof(FcCache));
518 if (lseek (fd_orig, off, SEEK_SET) != off)
524 if (read (fd_orig, dir->ent, dir->metadata.count)
525 != dir->metadata.count)
533 FcCacheWriteString (fd, d);
535 for (i = 0; i < dir->subdirs->size; i++)
536 FcCacheWriteString (fd, (char *)dir->subdirs->strs[i]);
537 FcCacheWriteString (fd, "");
539 if (write (fd, &dir->metadata, sizeof(FcCache)) != sizeof(FcCache))
541 perror ("write metadata");
545 off = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
546 if (lseek (fd, off, SEEK_SET) != off)
552 if (dir->metadata.count)
554 if (write (fd, dir->ent, dir->metadata.count) != dir->metadata.count)
556 perror ("write dirent");
563 FcCacheWriteString (fd, "");
565 if (close (fd) == -1)
571 if (!FcAtomicReplaceOrig (atomic))
574 FcAtomicUnlock (atomic);
575 FcAtomicDestroy (atomic);
577 cache->updated = FcFalse;
588 FcAtomicDeleteNew (atomic);
590 FcAtomicUnlock (atomic);
592 FcAtomicDestroy (atomic);
597 * Find the next presumably-mmapable offset after the supplied file
601 FcCacheNextOffset(off_t w)
603 static long pagesize = -1;
605 pagesize = sysconf(_SC_PAGESIZE);
606 if (w % pagesize == 0)
609 return ((w / pagesize)+1)*pagesize;
612 /* return the address of the segment for the provided arch,
613 * or -1 if arch not found */
615 FcCacheSkipToArch (int fd, const char * arch)
617 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
618 char * candidate_arch;
619 off_t current_arch_start = 0;
621 lseek (fd, 0, SEEK_SET);
622 FcCacheSkipString (fd);
623 current_arch_start = lseek (fd, 0, SEEK_CUR);
625 /* skip arches that are not the current arch */
630 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
633 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
634 sizeof (candidate_arch_machine_name_count)) == 0)
636 if (!strlen(candidate_arch_machine_name_count))
638 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
640 /* count = 0 should probably be distinguished from the !bs condition */
641 if (!bs || bs < strlen (candidate_arch_machine_name_count))
644 candidate_arch++; /* skip leading space */
646 if (strcmp (candidate_arch, arch)==0)
647 return current_arch_start;
648 current_arch_start += bs;
649 current_arch_start = FcCacheNextOffset (current_arch_start);
653 /* Cuts out the segment at the file pointer (moves everything else
654 * down to cover it), and leaves the file pointer at the end of the
657 FcCacheCopyOld (int fd, int fd_orig, off_t start)
659 char * buf = malloc (8192);
660 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
662 int c, bytes_skipped;
669 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
670 FcCacheSkipString (fd); FcCacheSkipString (fd_orig);
677 if ((c = read (fd_orig, buf, b)) <= 0)
679 if (write (fd, buf, c) < 0)
686 lseek (fd, start, SEEK_SET);
687 if (FcCacheReadString (fd, candidate_arch_machine_name,
688 sizeof (candidate_arch_machine_name)) == 0)
690 if (!strlen(candidate_arch_machine_name))
693 bs = strtol(candidate_arch_machine_name, 0, 16);
700 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
701 if ((c = read (fd, buf, 8192)) <= 0)
703 lseek (fd, start+bytes_skipped, SEEK_SET);
704 if (write (fd, buf, c) < 0)
709 lseek (fd, start+bytes_skipped, SEEK_SET);
720 /* Does not check that the cache has the appropriate arch section. */
721 /* Also, this can be fooled if the original location has a stale
722 * cache, and the hashed location has an up-to-date cache. Oh well,
723 * sucks to be you in that case! */
725 FcDirCacheValid (const FcChar8 *dir)
727 struct stat file_stat, dir_stat;
730 if (stat ((char *) dir, &dir_stat) < 0)
733 fd = FcDirCacheOpen (dir);
737 if (fstat (fd, &file_stat) < 0)
743 * If the directory has been modified more recently than
744 * the cache file, the cache is not valid
746 if (dir_stat.st_mtime > file_stat.st_mtime)
756 /* Assumes that the cache file in 'dir' exists.
757 * Checks that the cache has the appropriate arch section. */
759 FcDirCacheHasCurrentArch (const FcChar8 *dir)
762 off_t current_arch_start;
763 char *current_arch_machine_name;
765 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
767 fd = FcDirCacheOpen (dir);
771 current_arch_machine_name = FcCacheMachineSignature();
772 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
774 if (current_arch_start >= 0)
776 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
779 FcCacheSkipString (fd);
781 while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
784 if (read(fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
787 if (metadata.magic != FC_CACHE_MAGIC)
793 if (current_arch_start < 0)
805 FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
808 char *cache_hashed = 0;
810 struct stat cache_stat;
811 char name_buf[FC_MAX_FILE_LEN];
813 dir = FcConfigNormalizeFontDir (config, dir);
814 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
818 /* First remove normal cache file. */
819 if (stat ((char *) cache_file, &cache_stat) == 0)
820 unlink ((char *)cache_file);
822 /* Next remove any applicable hashed files. */
823 fd = -1; collisions = 0;
827 FcStrFree ((FcChar8 *)cache_hashed);
829 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
835 fd = open(cache_hashed, O_RDONLY | O_BINARY);
838 FcStrFree ((FcChar8 *)cache_file);
842 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
844 FcStrFree ((FcChar8 *)cache_hashed);
847 } while (strcmp (name_buf, cache_file) != 0);
851 if (stat ((char *) cache_hashed, &cache_stat) == 0 &&
852 unlink ((char *)cache_hashed) != 0)
854 FcStrFree ((FcChar8 *)cache_hashed);
858 FcStrFree ((FcChar8 *)cache_file);
859 FcStrFree ((FcChar8 *)cache_hashed);
863 FcStrFree ((FcChar8 *)cache_file);
868 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
869 FcStrList *list, FcFontSet * set, FcStrSet *processed_dirs)
879 * Read in the results from 'list'.
881 while ((dir = FcStrListNext (list)))
883 if (!FcConfigAcceptFilename (config, dir))
886 /* Skip this directory if already updated
887 * to avoid the looped directories via symlinks
888 * Clearly a dir not in fonts.conf shouldn't be globally cached.
890 dir = (FcChar8 *)FcConfigNormalizeFontDir (config, dir);
894 if (FcStrSetMember (processed_dirs, dir))
896 if (!FcStrSetAdd (processed_dirs, dir))
899 subdirs = FcStrSetCreate ();
902 fprintf (stderr, "Can't create directory set\n");
907 if (access ((char *) dir, X_OK) < 0)
915 fprintf (stderr, "\"%s\": ", dir);
919 FcStrSetDestroy (subdirs);
922 if (stat ((char *) dir, &statb) == -1)
924 fprintf (stderr, "\"%s\": ", dir);
926 FcStrSetDestroy (subdirs);
930 if (!S_ISDIR (statb.st_mode))
932 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
933 FcStrSetDestroy (subdirs);
936 if (FcDirCacheValid (dir) &&
937 FcDirCacheHasCurrentArch (dir) &&
938 FcDirCacheRead (set, subdirs, dir, config))
940 /* if an old entry is found in the global cache, disable it */
941 if ((d = FcGlobalCacheDirFind (cache, (const char *)dir)) != NULL)
943 d->state = FcGCDirDisabled;
944 /* save the updated config later without this entry */
945 cache->updated = FcTrue;
950 if (FcDebug () & FC_DBG_FONTSET)
951 printf ("cache scan dir %s\n", dir);
953 FcDirScanConfig (set, subdirs, cache,
954 config->blanks, dir, FcFalse, config);
956 sublist = FcStrListCreate (subdirs);
957 FcStrSetDestroy (subdirs);
960 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
964 ret += FcCacheReadDirs (config, cache, sublist, set, processed_dirs);
966 FcStrListDone (list);
971 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
973 FcFontSet *s = FcFontSetCreate();
974 FcStrSet *processed_dirs;
979 processed_dirs = FcStrSetCreate();
983 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s, processed_dirs))
986 FcStrSetDestroy (processed_dirs);
990 FcStrSetDestroy (processed_dirs);
992 FcFontSetDestroy (s);
996 static const char bin2hex[] = { '0', '1', '2', '3',
999 'c', 'd', 'e', 'f' };
1002 FcDirCacheHashName (char * cache_file, int collisions)
1004 unsigned char hash[16], hex_hash[33];
1006 unsigned char uscore = '_';
1009 struct MD5Context ctx;
1012 MD5Update (&ctx, (unsigned char *)cache_file, strlen (cache_file));
1014 for (i = 0; i < collisions; i++)
1015 MD5Update (&ctx, &uscore, 1);
1017 MD5Final (hash, &ctx);
1019 for (cnt = 0; cnt < 16; ++cnt)
1021 hex_hash[2*cnt] = bin2hex[hash[cnt] >> 4];
1022 hex_hash[2*cnt+1] = bin2hex[hash[cnt] & 0xf];
1026 tmp = FcStrPlus ((FcChar8 *)hex_hash, (FcChar8 *)FC_CACHE_SUFFIX);
1030 cache_hashed = (char *)FcStrPlus ((FcChar8 *)PKGCACHEDIR"/", tmp);
1033 return cache_hashed;
1036 /* Opens the hashed name for cache_file.
1037 * This would fail in the unlikely event of a collision and subsequent
1038 * removal of the file which originally caused the collision. */
1040 FcDirCacheOpen (const FcChar8 *dir)
1043 int fd = -1, collisions = 0;
1044 char *cache_file, *cache_hashed;
1045 char name_buf[FC_MAX_FILE_LEN];
1046 struct stat dir_stat;
1048 if (stat ((char *)dir, &dir_stat) == -1)
1051 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1059 FcChar8 *name_buf_dir;
1064 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1067 FcStrFree ((FcChar8 *)cache_file);
1071 fd = open(cache_hashed, O_RDONLY | O_BINARY);
1072 FcStrFree ((FcChar8 *)cache_hashed);
1076 if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) ||
1080 name_buf_dir = FcStrDirname ((FcChar8 *)name_buf);
1081 if (stat ((char *)name_buf_dir, &c) == -1)
1083 FcStrFree (name_buf_dir);
1086 FcStrFree (name_buf_dir);
1087 found = (c.st_ino == dir_stat.st_ino) && (c.st_dev == dir_stat.st_dev);
1090 if (!found || fd < 0)
1094 fd = open(cache_file, O_RDONLY | O_BINARY);
1097 FcStrFree ((FcChar8 *)cache_file);
1101 /* read serialized state from the cache file */
1103 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
1106 char *current_arch_machine_name;
1107 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
1108 off_t current_arch_start = 0;
1109 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
1111 fd = FcDirCacheOpen (dir);
1115 current_arch_machine_name = FcCacheMachineSignature();
1116 current_arch_start = FcCacheSkipToArch(fd,
1117 current_arch_machine_name);
1118 if (current_arch_start < 0)
1121 lseek (fd, current_arch_start, SEEK_SET);
1122 if (FcCacheReadString (fd, candidate_arch_machine_name,
1123 sizeof (candidate_arch_machine_name)) == 0)
1126 while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
1127 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
1129 if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
1142 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
1145 void * current_dir_block;
1148 if (read(fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
1150 if (metadata.magic != FC_CACHE_MAGIC)
1153 if (!metadata.count)
1155 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1156 lseek (fd, pos, SEEK_SET);
1158 FcConfigAddFontDir (config, (FcChar8 *)dir);
1162 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
1163 current_dir_block = mmap (0, metadata.count,
1164 PROT_READ, MAP_SHARED, fd, pos);
1165 lseek (fd, pos+metadata.count, SEEK_SET);
1166 if (current_dir_block == MAP_FAILED)
1169 FcCacheAddBankDir (metadata.bank, dir);
1171 FcConfigAddFontDir (config, (FcChar8 *)dir);
1173 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
1180 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
1182 void * current_dir_block, * final_dir_block;
1183 static unsigned int rand_state = 0;
1184 int bank, needed_bytes_no_align;
1187 rand_state = time(0L);
1188 bank = rand_r(&rand_state);
1190 while (FcCacheHaveBank(bank))
1191 bank = rand_r(&rand_state);
1193 memset (metadata, 0, sizeof(FcCache));
1195 needed_bytes_no_align = FcFontSetNeededBytes (set);
1196 metadata->count = needed_bytes_no_align +
1197 FcFontSetNeededBytesAlign ();
1198 metadata->magic = FC_CACHE_MAGIC;
1199 metadata->bank = bank;
1201 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
1203 /* no, you don't really need to write any bytes at all. */
1204 metadata->count = 0;
1208 current_dir_block = malloc (metadata->count);
1209 if (!current_dir_block)
1211 /* shut up valgrind */
1212 memset (current_dir_block, 0, metadata->count);
1213 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
1215 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
1218 if (!FcFontSetSerialize (bank, set))
1221 return current_dir_block;
1224 free (current_dir_block);
1228 /* write serialized state to the cache file */
1230 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
1234 int fd, fd_orig, i, dirs_count;
1237 off_t current_arch_start = 0, truncate_to;
1238 char name_buf[FC_MAX_FILE_LEN];
1241 char *current_arch_machine_name, * header;
1242 void *current_dir_block = 0;
1244 dir = FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir);
1248 cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
1252 /* Ensure that we're not trampling a cache for some other dir. */
1253 /* This is slightly different from FcDirCacheOpen, since it
1254 * needs the filename, not the file descriptor. */
1255 fd = -1; collisions = 0;
1258 cache_hashed = FcDirCacheHashName (cache_file, collisions++);
1264 fd = open(cache_hashed, O_RDONLY | O_BINARY);
1267 if(!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
1274 if (strcmp (name_buf, cache_file) != 0)
1280 current_dir_block = FcDirCacheProduce (set, &metadata);
1282 if (metadata.count && !current_dir_block)
1285 if (FcDebug () & FC_DBG_CACHE)
1286 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
1288 atomic = FcAtomicCreate ((FcChar8 *)cache_hashed);
1292 if (!FcAtomicLock (atomic))
1294 /* Now try rewriting the original version of the file. */
1295 FcAtomicDestroy (atomic);
1297 atomic = FcAtomicCreate ((FcChar8 *)cache_file);
1298 fd_orig = open (cache_file, O_RDONLY | O_BINARY);
1300 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY | O_BINARY);
1302 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
1307 /* In all cases, try opening the real location of the cache file first. */
1308 /* (even if that's not atomic.) */
1309 fd_orig = open (cache_file, O_RDONLY | O_BINARY);
1311 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY | O_BINARY);
1313 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT | O_BINARY, 0666);
1317 FcCacheWriteString (fd, cache_file);
1319 current_arch_machine_name = FcCacheMachineSignature ();
1320 current_arch_start = 0;
1323 current_arch_start =
1324 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
1326 if (current_arch_start < 0)
1328 off_t offset = lseek(fd_orig, 0, SEEK_END);
1329 current_arch_start = FcCacheNextOffset (offset);
1332 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
1338 current_arch_start = lseek(fd, 0, SEEK_CUR);
1339 if (ftruncate (fd, current_arch_start) == -1)
1342 /* allocate space for subdir names in this block */
1344 for (i = 0; i < dirs->size; i++)
1345 dirs_count += strlen((char *)dirs->strs[i]) + 1;
1348 /* now write the address of the next offset */
1349 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
1350 header = malloc (10 + strlen (current_arch_machine_name));
1353 sprintf (header, "%8x ", (int)truncate_to);
1354 strcat (header, current_arch_machine_name);
1355 if (!FcCacheWriteString (fd, header))
1358 for (i = 0; i < dirs->size; i++)
1359 FcCacheWriteString (fd, (char *)dirs->strs[i]);
1360 FcCacheWriteString (fd, "");
1362 if (write (fd, &metadata, sizeof(FcCache)) != sizeof(FcCache))
1364 perror("write metadata");
1369 off_t off = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
1370 if (lseek (fd, off, SEEK_SET) != off)
1372 else if (write (fd, current_dir_block, metadata.count) !=
1374 perror("write current_dir_block");
1375 free (current_dir_block);
1376 current_dir_block = 0;
1379 /* this actually serves to pad out the cache file, if needed */
1380 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
1385 if (!FcAtomicReplaceOrig(atomic))
1387 FcStrFree ((FcChar8 *)cache_hashed);
1388 FcStrFree ((FcChar8 *)cache_file);
1389 FcAtomicUnlock (atomic);
1390 FcAtomicDestroy (atomic);
1398 FcAtomicUnlock (atomic);
1400 FcAtomicDestroy (atomic);
1402 FcStrFree ((FcChar8 *)cache_hashed);
1404 unlink ((char *)cache_file);
1405 FcStrFree ((FcChar8 *)cache_file);
1406 if (current_dir_block)
1407 free (current_dir_block);
1413 FcCacheMachineSignature ()
1415 static char buf[MACHINE_SIGNATURE_SIZE];
1416 int32_t magic = ENDIAN_TEST;
1417 char * m = (char *)&magic;
1419 sprintf (buf, "%2x%2x%2x%2x "
1420 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
1421 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
1422 m[0], m[1], m[2], m[3],
1423 (unsigned int)sizeof (char),
1424 (unsigned int)sizeof (char *),
1425 (unsigned int)sizeof (int),
1426 (unsigned int)sizeof (FcPattern),
1427 (unsigned int)sizeof (FcPatternEltPtr),
1428 (unsigned int)sizeof (struct _FcPatternElt *),
1429 (unsigned int)sizeof (FcPatternElt),
1430 (unsigned int)sizeof (FcObjectPtr),
1431 (unsigned int)sizeof (FcValueListPtr),
1432 (unsigned int)sizeof (FcValue),
1433 (unsigned int)sizeof (FcValueBinding),
1434 (unsigned int)sizeof (struct _FcValueList *),
1435 (unsigned int)sizeof (FcCharSet),
1436 (unsigned int)sizeof (FcCharLeaf **),
1437 (unsigned int)sizeof (FcChar16 *),
1438 (unsigned int)sizeof (FcChar16),
1439 (unsigned int)sizeof (FcCharLeaf),
1440 (unsigned int)sizeof (FcChar32),
1441 (unsigned int)sizeof (FcCache),
1442 (unsigned int)sysconf(_SC_PAGESIZE));
1447 static int banks_ptr = 0, banks_alloc = 0;
1448 int * _fcBankId = 0, * _fcBankIdx = 0;
1449 static const char ** bankDirs = 0;
1452 FcCacheHaveBank (int bank)
1456 if (bank < FC_BANK_FIRST)
1459 for (i = 0; i < banks_ptr; i++)
1460 if (_fcBankId[i] == bank)
1467 FcCacheBankToIndexMTF (int bank)
1471 for (i = 0; i < banks_ptr; i++)
1472 if (_fcBankId[_fcBankIdx[i]] == bank)
1474 int t = _fcBankIdx[i];
1476 for (j = i; j > 0; j--)
1477 _fcBankIdx[j] = _fcBankIdx[j-1];
1482 if (banks_ptr >= banks_alloc)
1487 b = realloc (_fcBankId, (banks_alloc + 4) * sizeof(int));
1492 bidx = realloc (_fcBankIdx, (banks_alloc + 4) * sizeof(int));
1497 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1506 _fcBankId[i] = bank;
1512 FcCacheAddBankDir (int bank, const char * dir)
1514 int bi = FcCacheBankToIndexMTF (bank);
1519 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1523 FcCacheFindBankDir (int bank)
1525 int bi = FcCacheBankToIndex (bank);
1526 return bankDirs[bi];
1530 * This code implements the MD5 message-digest algorithm.
1531 * The algorithm is due to Ron Rivest. This code was
1532 * written by Colin Plumb in 1993, no copyright is claimed.
1533 * This code is in the public domain; do with it what you wish.
1535 * Equivalent code is available from RSA Data Security, Inc.
1536 * This code has been tested against that, and is equivalent,
1537 * except that you don't need to include two pages of legalese
1540 * To compute the message digest of a chunk of bytes, declare an
1541 * MD5Context structure, pass it to MD5Init, call MD5Update as
1542 * needed on buffers full of bytes, and then call MD5Final, which
1543 * will fill a supplied 16-byte array with the digest.
1547 #define byteReverse(buf, len) /* Nothing */
1550 * Note: this code is harmless on little-endian machines.
1552 void byteReverse(unsigned char *buf, unsigned longs)
1556 t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1557 ((unsigned) buf[1] << 8 | buf[0]);
1558 *(FcChar32 *) buf = t;
1565 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1566 * initialization constants.
1568 static void MD5Init(struct MD5Context *ctx)
1570 ctx->buf[0] = 0x67452301;
1571 ctx->buf[1] = 0xefcdab89;
1572 ctx->buf[2] = 0x98badcfe;
1573 ctx->buf[3] = 0x10325476;
1580 * Update context to reflect the concatenation of another buffer full
1583 static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len)
1587 /* Update bitcount */
1590 if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
1591 ctx->bits[1]++; /* Carry from low to high */
1592 ctx->bits[1] += len >> 29;
1594 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
1596 /* Handle any leading odd-sized chunks */
1599 unsigned char *p = (unsigned char *) ctx->in + t;
1603 memcpy(p, buf, len);
1607 byteReverse(ctx->in, 16);
1608 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1612 /* Process data in 64-byte chunks */
1615 memcpy(ctx->in, buf, 64);
1616 byteReverse(ctx->in, 16);
1617 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1622 /* Handle any remaining bytes of data. */
1624 memcpy(ctx->in, buf, len);
1628 * Final wrapup - pad to 64-byte boundary with the bit pattern
1629 * 1 0* (64-bit count of bits processed, MSB-first)
1631 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
1636 /* Compute number of bytes mod 64 */
1637 count = (ctx->bits[0] >> 3) & 0x3F;
1639 /* Set the first char of padding to 0x80. This is safe since there is
1640 always at least one byte free */
1641 p = ctx->in + count;
1644 /* Bytes of padding needed to make 64 bytes */
1645 count = 64 - 1 - count;
1647 /* Pad out to 56 mod 64 */
1649 /* Two lots of padding: Pad the first block to 64 bytes */
1650 memset(p, 0, count);
1651 byteReverse(ctx->in, 16);
1652 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1654 /* Now fill the next block with 56 bytes */
1655 memset(ctx->in, 0, 56);
1657 /* Pad block to 56 bytes */
1658 memset(p, 0, count - 8);
1660 byteReverse(ctx->in, 14);
1662 /* Append length in bits and transform */
1663 ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
1664 ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
1666 MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
1667 byteReverse((unsigned char *) ctx->buf, 4);
1668 memcpy(digest, ctx->buf, 16);
1669 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
1673 /* The four core functions - F1 is optimized somewhat */
1675 /* #define F1(x, y, z) (x & y | ~x & z) */
1676 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1677 #define F2(x, y, z) F1(z, x, y)
1678 #define F3(x, y, z) (x ^ y ^ z)
1679 #define F4(x, y, z) (y ^ (x | ~z))
1681 /* This is the central step in the MD5 algorithm. */
1682 #define MD5STEP(f, w, x, y, z, data, s) \
1683 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1686 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1687 * reflect the addition of 16 longwords of new data. MD5Update blocks
1688 * the data and converts bytes into longwords for this routine.
1690 static void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
1692 register FcChar32 a, b, c, d;
1699 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1700 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1701 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1702 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1703 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1704 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1705 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1706 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1707 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1708 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1709 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1710 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1711 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1712 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1713 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1714 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1716 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1717 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1718 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1719 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1720 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1721 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1722 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1723 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1724 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1725 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1726 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1727 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1728 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1729 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1730 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1731 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1733 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1734 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1735 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1736 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1737 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1738 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1739 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1740 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1741 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1742 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1743 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1744 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1745 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1746 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1747 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1748 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1750 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1751 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1752 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1753 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1754 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1755 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1756 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1757 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1758 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1759 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1760 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1761 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1762 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1763 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1764 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1765 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);