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.
29 #include <sys/utsname.h>
30 #include <sys/types.h>
34 #define ENDIAN_TEST 0x12345678
35 #define MACHINE_SIGNATURE_SIZE 9 + 5*19 + 1
38 FcCacheSkipToArch (int fd, const char * arch);
41 FcCacheCopyOld (int fd, int fd_orig, off_t start);
44 FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
47 FcDirCacheConsume (int fd, FcFontSet *set);
50 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir);
53 FcCacheNextOffset(off_t w);
56 FcCacheMachineSignature (void);
59 FcCacheHaveBank (int bank);
61 #define FC_DBG_CACHE_REF 1024
64 FcCacheReadString (int fd, char *dest, int len)
77 while (read (fd, &c, 1) == 1)
104 FcCacheWriteString (int fd, const char *chars)
106 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
112 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
114 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
116 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
121 FcGlobalCacheCreate (void)
123 FcGlobalCache *cache;
125 cache = malloc (sizeof (FcGlobalCache));
128 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
130 cache->updated = FcFalse;
136 FcGlobalCacheDestroy (FcGlobalCache *cache)
138 FcGlobalCacheDir *d, *next;
140 for (d = cache->dirs; d; d = next)
143 FcGlobalCacheDirDestroy (d);
145 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
150 FcGlobalCacheLoad (FcGlobalCache *cache,
152 const FcChar8 *cache_file)
155 FcGlobalCacheDir *d, *next;
156 char * current_arch_machine_name;
157 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
158 off_t current_arch_start;
160 struct stat cache_stat, dir_stat;
162 if (stat ((char *) cache_file, &cache_stat) < 0)
165 cache->fd = open ((char *) cache_file, O_RDONLY);
169 cache->updated = FcFalse;
171 current_arch_machine_name = FcCacheMachineSignature ();
172 current_arch_start = FcCacheSkipToArch(cache->fd,
173 current_arch_machine_name);
174 if (current_arch_start < 0)
175 goto bail_and_destroy;
177 lseek (cache->fd, current_arch_start, SEEK_SET);
178 FcCacheReadString (cache->fd, candidate_arch_machine_name,
179 sizeof (candidate_arch_machine_name));
180 if (strlen(candidate_arch_machine_name) == 0)
181 goto bail_and_destroy;
187 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
188 if (!strlen(name_buf))
191 if (stat ((char *) name_buf, &dir_stat) < 0 ||
192 dir_stat.st_mtime > cache_stat.st_mtime)
196 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
197 read (cache->fd, &md, sizeof (FcCache));
198 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
202 d = malloc (sizeof (FcGlobalCacheDir));
206 d->next = cache->dirs;
209 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
211 d->offset = lseek (cache->fd, 0, SEEK_CUR);
212 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
214 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
215 if (lseek (cache->fd, targ, SEEK_SET) != targ)
221 for (d = cache->dirs; d; d = next)
236 if (stat ((char *) cache_file, &cache_stat) == 0)
237 unlink ((char *)cache_file);
244 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
247 FcBool ret = FcFalse;
252 for (d = cache->dirs; d; d = d->next)
254 if (strncmp (d->name, dir, strlen(dir)) == 0)
256 lseek (cache->fd, d->offset, SEEK_SET);
257 if (!FcDirCacheConsume (cache->fd, set))
259 if (strcmp (d->name, dir) == 0)
268 FcGlobalCacheUpdate (FcGlobalCache *cache,
272 FcGlobalCacheDir * d;
277 for (d = cache->dirs; d; d = d->next)
279 if (strcmp(d->name, name) == 0)
285 d = malloc (sizeof (FcGlobalCacheDir));
288 d->next = cache->dirs;
292 cache->updated = FcTrue;
294 d->name = (char *)FcStrCopy ((FcChar8 *)name);
295 d->ent = FcDirCacheProduce (set, &d->metadata);
301 FcGlobalCacheSave (FcGlobalCache *cache,
302 const FcChar8 *cache_file)
305 FcGlobalCacheDir *dir;
307 off_t current_arch_start = 0, truncate_to;
308 char * current_arch_machine_name, * header;
313 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
314 /* Set-UID programs can't safely update the cache */
315 if (getuid () != geteuid ())
319 atomic = FcAtomicCreate (cache_file);
323 if (!FcAtomicLock (atomic))
325 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
330 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
332 current_arch_machine_name = FcCacheMachineSignature ();
334 current_arch_start = 0;
336 current_arch_start = FcCacheSkipToArch (fd_orig,
337 current_arch_machine_name);
339 if (current_arch_start < 0)
340 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
342 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
348 current_arch_start = lseek(fd, 0, SEEK_CUR);
349 if (ftruncate (fd, current_arch_start) == -1)
352 header = malloc (10 + strlen (current_arch_machine_name));
356 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
357 for (dir = cache->dirs; dir; dir = dir->next)
359 truncate_to += strlen(dir->name) + 1;
360 truncate_to += sizeof (FcCache);
361 truncate_to = FcCacheNextOffset (current_arch_start + truncate_to);
362 truncate_to += dir->metadata.count;
364 truncate_to -= current_arch_start;
366 sprintf (header, "%8x ", (int)truncate_to);
367 strcat (header, current_arch_machine_name);
368 if (!FcCacheWriteString (fd, header))
371 for (dir = cache->dirs; dir; dir = dir->next)
375 FcCacheWriteString (fd, dir->name);
376 write (fd, &dir->metadata, sizeof(FcCache));
377 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
378 write (fd, dir->ent, dir->metadata.count);
382 FcCacheWriteString (fd, "");
384 if (close (fd) == -1)
387 if (!FcAtomicReplaceOrig (atomic))
390 FcAtomicUnlock (atomic);
391 FcAtomicDestroy (atomic);
393 cache->updated = FcFalse;
404 FcAtomicDeleteNew (atomic);
406 FcAtomicUnlock (atomic);
408 FcAtomicDestroy (atomic);
412 #define PAGESIZE 8192
414 * Find the next presumably-mmapable offset after the supplied file
418 FcCacheNextOffset(off_t w)
420 if (w % PAGESIZE == 0)
423 return ((w / PAGESIZE)+1)*PAGESIZE;
426 /* return the address of the segment for the provided arch,
427 * or -1 if arch not found */
429 FcCacheSkipToArch (int fd, const char * arch)
431 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
432 char * candidate_arch;
433 off_t current_arch_start = 0;
435 /* skip arches that are not the current arch */
440 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
443 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
444 sizeof (candidate_arch_machine_name_count)) == 0)
446 if (!strlen(candidate_arch_machine_name_count))
448 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
450 // count = 0 should probably be distinguished from the !bs condition
451 if (!bs || bs < strlen (candidate_arch_machine_name_count))
454 candidate_arch++; /* skip leading space */
456 if (strcmp (candidate_arch, arch)==0)
457 return current_arch_start;
458 current_arch_start += bs;
464 /* Cuts out the segment at the file pointer (moves everything else
465 * down to cover it), and leaves the file pointer at the end of the
468 FcCacheCopyOld (int fd, int fd_orig, off_t start)
470 char * buf = malloc (8192);
471 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
473 int c, bytes_skipped;
480 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
487 if ((c = read (fd_orig, buf, b)) <= 0)
489 if (write (fd, buf, c) < 0)
496 lseek (fd, start, SEEK_SET);
497 if (FcCacheReadString (fd, candidate_arch_machine_name,
498 sizeof (candidate_arch_machine_name)) == 0)
500 if (!strlen(candidate_arch_machine_name))
503 bs = strtol(candidate_arch_machine_name, 0, 16);
510 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
511 if ((c = read (fd, buf, 8192)) <= 0)
513 lseek (fd, start+bytes_skipped, SEEK_SET);
514 if (write (fd, buf, c) < 0)
519 lseek (fd, start+bytes_skipped, SEEK_SET);
530 /* Does not check that the cache has the appropriate arch section. */
532 FcDirCacheValid (const FcChar8 *dir)
534 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
535 struct stat file_stat, dir_stat;
537 if (stat ((char *) dir, &dir_stat) < 0)
539 FcStrFree (cache_file);
542 if (stat ((char *) cache_file, &file_stat) < 0)
544 FcStrFree (cache_file);
548 FcStrFree (cache_file);
550 * If the directory has been modified more recently than
551 * the cache file, the cache is not valid
553 if (dir_stat.st_mtime - file_stat.st_mtime > 0)
558 /* Assumes that the cache file in 'dir' exists.
559 * Checks that the cache has the appropriate arch section. */
561 FcDirCacheHasCurrentArch (const FcChar8 *dir)
563 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
565 off_t current_arch_start;
566 char *current_arch_machine_name;
568 current_arch_machine_name = FcCacheMachineSignature();
569 fd = open ((char *)cache_file, O_RDONLY);
573 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
576 if (current_arch_start < 0)
583 FcDirCacheUnlink (const FcChar8 *dir)
585 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
586 struct stat cache_stat;
588 if (stat ((char *) cache_file, &cache_stat) == 0 &&
589 unlink ((char *)cache_file) != 0)
591 FcStrFree (cache_file);
595 FcStrFree (cache_file);
600 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
601 FcStrList *list, FcFontSet * set)
605 FcChar8 *file, *base;
611 * Read in the results from 'list'.
613 while ((dir = FcStrListNext (list)))
616 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
620 strcpy ((char *) file, (char *) dir);
621 strcat ((char *) file, "/");
622 base = file + strlen ((char *) file);
624 subdirs = FcStrSetCreate ();
627 fprintf (stderr, "Can't create directory set\n");
633 if (access ((char *) dir, X_OK) < 0)
641 fprintf (stderr, "\"%s\": ", dir);
645 FcStrSetDestroy (subdirs);
649 if (stat ((char *) dir, &statb) == -1)
651 fprintf (stderr, "\"%s\": ", dir);
653 FcStrSetDestroy (subdirs);
658 if (!S_ISDIR (statb.st_mode))
660 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
661 FcStrSetDestroy (subdirs);
665 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
667 if (FcDebug () & FC_DBG_FONTSET)
668 printf ("cache scan dir %s\n", dir);
670 FcDirScanConfig (set, subdirs, cache,
671 config->blanks, dir, FcFalse, config);
673 sublist = FcStrListCreate (subdirs);
674 FcStrSetDestroy (subdirs);
677 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
682 ret += FcCacheReadDirs (config, cache, sublist, set);
685 FcStrListDone (list);
690 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
692 FcFontSet * s = FcFontSetCreate();
696 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
702 FcFontSetDestroy (s);
706 /* read serialized state from the cache file */
708 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
710 char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
712 char * current_arch_machine_name;
713 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
714 off_t current_arch_start = 0;
715 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
720 current_arch_machine_name = FcCacheMachineSignature();
721 fd = open(cache_file, O_RDONLY);
725 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
726 if (current_arch_start < 0)
729 lseek (fd, current_arch_start, SEEK_SET);
730 if (FcCacheReadString (fd, candidate_arch_machine_name,
731 sizeof (candidate_arch_machine_name)) == 0)
734 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
735 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
737 if (!FcDirCacheConsume (fd, set))
752 FcDirCacheConsume (int fd, FcFontSet *set)
755 void * current_dir_block;
758 read(fd, &metadata, sizeof(FcCache));
759 if (metadata.magic != FC_CACHE_MAGIC)
765 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
766 current_dir_block = mmap (0, metadata.count,
767 PROT_READ, MAP_SHARED, fd, pos);
768 if (current_dir_block == MAP_FAILED)
771 if (!FcFontSetUnserialize (metadata, set, current_dir_block))
778 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
780 void * current_dir_block, * final_dir_block;
781 static unsigned int rand_state = 0;
785 rand_state = time(0L);
786 bank = rand_r(&rand_state);
788 while (FcCacheHaveBank(bank))
789 bank = rand_r(&rand_state);
791 memset (metadata, 0, sizeof(FcCache));
793 metadata->count = FcFontSetNeededBytes (set);
794 metadata->magic = FC_CACHE_MAGIC;
795 metadata->bank = bank;
797 if (!metadata->count) /* not a failure, no fonts to write */
800 current_dir_block = malloc (metadata->count);
801 if (!current_dir_block)
803 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
805 if ((char *)current_dir_block + metadata->count != final_dir_block)
808 if (!FcFontSetSerialize (bank, set))
811 return current_dir_block;
814 free (current_dir_block);
818 /* write serialized state to the cache file */
820 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
822 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
823 int fd, fd_orig, i, dirs_count;
826 off_t current_arch_start = 0, truncate_to;
828 char *current_arch_machine_name, * header;
829 void *current_dir_block;
834 current_dir_block = FcDirCacheProduce (set, &metadata);
836 if (metadata.count && !current_dir_block)
839 if (FcDebug () & FC_DBG_CACHE)
840 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
842 atomic = FcAtomicCreate (cache_file);
846 if (!FcAtomicLock (atomic))
849 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY, 0666);
851 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
855 current_arch_machine_name = FcCacheMachineSignature ();
856 current_arch_start = 0;
860 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
862 if (current_arch_start < 0)
863 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
865 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
871 current_arch_start = lseek(fd, 0, SEEK_CUR);
872 if (ftruncate (fd, current_arch_start) == -1)
875 /* allocate space for subdir names in this block */
877 for (i = 0; i < dirs->size; i++)
878 dirs_count += strlen((char *)dirs->strs[i]) + 1;
881 /* now write the address of the next offset */
882 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
883 header = malloc (10 + strlen (current_arch_machine_name));
886 sprintf (header, "%8x ", (int)truncate_to);
887 strcat (header, current_arch_machine_name);
888 if (!FcCacheWriteString (fd, header))
891 for (i = 0; i < dirs->size; i++)
892 FcCacheWriteString (fd, (char *)dirs->strs[i]);
893 FcCacheWriteString (fd, "");
895 write (fd, &metadata, sizeof(FcCache));
898 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
899 write (fd, current_dir_block, metadata.count);
900 free (current_dir_block);
903 /* this actually serves to pad out the cache file, if needed */
904 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
908 if (!FcAtomicReplaceOrig(atomic))
910 FcAtomicUnlock (atomic);
911 FcAtomicDestroy (atomic);
919 FcAtomicUnlock (atomic);
921 FcAtomicDestroy (atomic);
923 unlink ((char *)cache_file);
925 if (current_dir_block)
926 free (current_dir_block);
932 FcCacheMachineSignature ()
934 static char buf[MACHINE_SIGNATURE_SIZE];
935 int magic = ENDIAN_TEST;
936 char * m = (char *)&magic;
938 sprintf (buf, "%2x%2x%2x%2x "
939 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
940 "%4x %4x %4x %4x %4x %4x %4x\n",
941 m[0], m[1], m[2], m[3],
942 (unsigned int)sizeof (char),
943 (unsigned int)sizeof (char *),
944 (unsigned int)sizeof (int),
945 (unsigned int)sizeof (FcPattern),
946 (unsigned int)sizeof (FcPatternEltPtr),
947 (unsigned int)sizeof (struct _FcPatternElt *),
948 (unsigned int)sizeof (FcPatternElt),
949 (unsigned int)sizeof (FcObjectPtr),
950 (unsigned int)sizeof (FcValueListPtr),
951 (unsigned int)sizeof (FcValue),
952 (unsigned int)sizeof (FcValueBinding),
953 (unsigned int)sizeof (struct _FcValueList *),
954 (unsigned int)sizeof (FcCharSet),
955 (unsigned int)sizeof (FcCharLeaf **),
956 (unsigned int)sizeof (FcChar16 *),
957 (unsigned int)sizeof (FcChar16),
958 (unsigned int)sizeof (FcCharLeaf),
959 (unsigned int)sizeof (FcChar32),
960 (unsigned int)sizeof (FcCache));
965 static int banks_ptr = 0, banks_alloc = 0;
966 static int * bankId = 0, * bankIdx = 0;
969 FcCacheHaveBank (int bank)
973 if (bank < FC_BANK_FIRST)
976 for (i = 0; i < banks_ptr; i++)
977 if (bankId[i] == bank)
984 FcCacheBankToIndex (int bank)
988 for (i = 0; i < banks_ptr; i++)
989 if (bankId[bankIdx[i]] == bank)
993 for (j = i; j > 0; j--)
994 bankIdx[j] = bankIdx[j-1];
999 if (banks_ptr >= banks_alloc)
1002 b = realloc (bankId, (banks_alloc + 4) * sizeof(int));
1007 bidx = realloc (bankIdx, (banks_alloc + 4) * sizeof(int));