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>
35 #define ENDIAN_TEST 0x12345678
36 #define MACHINE_SIGNATURE_SIZE 9 + 5*20 + 1
39 FcCacheSkipToArch (int fd, const char * arch);
42 FcCacheCopyOld (int fd, int fd_orig, off_t start);
45 FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
48 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set);
51 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir);
54 FcCacheNextOffset(off_t w);
57 FcCacheMachineSignature (void);
60 FcCacheHaveBank (int bank);
63 FcCacheAddBankDir (int bank, const char * dir);
65 #define FC_DBG_CACHE_REF 1024
68 FcCacheReadString (int fd, char *dest, int len)
81 while (read (fd, &c, 1) == 1)
108 FcCacheWriteString (int fd, const char *chars)
110 if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
116 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
118 FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
120 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
125 FcGlobalCacheCreate (void)
127 FcGlobalCache *cache;
129 cache = malloc (sizeof (FcGlobalCache));
132 FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache));
134 cache->updated = FcFalse;
140 FcGlobalCacheDestroy (FcGlobalCache *cache)
142 FcGlobalCacheDir *d, *next;
144 for (d = cache->dirs; d; d = next)
147 FcGlobalCacheDirDestroy (d);
149 FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache));
154 FcGlobalCacheLoad (FcGlobalCache *cache,
156 const FcChar8 *cache_file,
160 FcGlobalCacheDir *d, *next;
161 FcFileTime config_time = FcConfigModifiedTime (config);
162 char * current_arch_machine_name;
163 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
164 off_t current_arch_start;
166 struct stat cache_stat, dir_stat;
168 if (stat ((char *) cache_file, &cache_stat) < 0)
171 cache->fd = open ((char *) cache_file, O_RDONLY);
175 cache->updated = FcFalse;
177 current_arch_machine_name = FcCacheMachineSignature ();
178 current_arch_start = FcCacheSkipToArch(cache->fd,
179 current_arch_machine_name);
180 if (current_arch_start < 0)
181 goto bail_and_destroy;
183 lseek (cache->fd, current_arch_start, SEEK_SET);
184 FcCacheReadString (cache->fd, candidate_arch_machine_name,
185 sizeof (candidate_arch_machine_name));
186 if (strlen(candidate_arch_machine_name) == 0)
187 goto bail_and_destroy;
193 FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
194 if (!strlen(name_buf))
197 /* Directory must be older than the global cache file; also
198 cache must be newer than the config file. */
199 if (stat ((char *) name_buf, &dir_stat) < 0 ||
200 dir_stat.st_mtime > cache_stat.st_mtime ||
201 (config_time.set && cache_stat.st_mtime < config_time.time))
205 FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf));
206 read (cache->fd, &md, sizeof (FcCache));
207 lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET);
211 d = malloc (sizeof (FcGlobalCacheDir));
215 d->next = cache->dirs;
218 d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
220 d->offset = lseek (cache->fd, 0, SEEK_CUR);
221 if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
223 targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
224 if (lseek (cache->fd, targ, SEEK_SET) != targ)
230 for (d = cache->dirs; d; d = next)
245 if (stat ((char *) cache_file, &cache_stat) == 0)
246 unlink ((char *)cache_file);
253 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
256 FcBool ret = FcFalse;
261 for (d = cache->dirs; d; d = d->next)
263 if (strncmp (d->name, dir, strlen(dir)) == 0)
265 lseek (cache->fd, d->offset, SEEK_SET);
266 if (!FcDirCacheConsume (cache->fd, dir, set))
268 if (strcmp (d->name, dir) == 0)
277 FcGlobalCacheUpdate (FcGlobalCache *cache,
281 FcGlobalCacheDir * d;
286 for (d = cache->dirs; d; d = d->next)
288 if (strcmp(d->name, name) == 0)
294 d = malloc (sizeof (FcGlobalCacheDir));
297 d->next = cache->dirs;
301 cache->updated = FcTrue;
303 d->name = (char *)FcStrCopy ((FcChar8 *)name);
304 d->ent = FcDirCacheProduce (set, &d->metadata);
310 FcGlobalCacheSave (FcGlobalCache *cache,
311 const FcChar8 *cache_file)
314 FcGlobalCacheDir *dir;
316 off_t current_arch_start = 0, truncate_to;
317 char * current_arch_machine_name, * header;
322 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
323 /* Set-UID programs can't safely update the cache */
324 if (getuid () != geteuid ())
328 atomic = FcAtomicCreate (cache_file);
332 if (!FcAtomicLock (atomic))
334 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
339 fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
341 current_arch_machine_name = FcCacheMachineSignature ();
343 current_arch_start = 0;
345 current_arch_start = FcCacheSkipToArch (fd_orig,
346 current_arch_machine_name);
348 if (current_arch_start < 0)
349 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
351 if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
357 current_arch_start = lseek(fd, 0, SEEK_CUR);
358 if (ftruncate (fd, current_arch_start) == -1)
361 header = malloc (10 + strlen (current_arch_machine_name));
365 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
366 for (dir = cache->dirs; dir; dir = dir->next)
368 truncate_to += strlen(dir->name) + 1;
369 truncate_to += sizeof (FcCache);
370 truncate_to = FcCacheNextOffset (truncate_to);
371 truncate_to += dir->metadata.count;
373 truncate_to -= current_arch_start;
375 sprintf (header, "%8x ", (int)truncate_to);
376 strcat (header, current_arch_machine_name);
377 if (!FcCacheWriteString (fd, header))
380 for (dir = cache->dirs; dir; dir = dir->next)
384 FcCacheWriteString (fd, dir->name);
385 write (fd, &dir->metadata, sizeof(FcCache));
386 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
387 write (fd, dir->ent, dir->metadata.count);
391 FcCacheWriteString (fd, "");
393 if (close (fd) == -1)
396 if (!FcAtomicReplaceOrig (atomic))
399 FcAtomicUnlock (atomic);
400 FcAtomicDestroy (atomic);
402 cache->updated = FcFalse;
413 FcAtomicDeleteNew (atomic);
415 FcAtomicUnlock (atomic);
417 FcAtomicDestroy (atomic);
422 * Find the next presumably-mmapable offset after the supplied file
426 FcCacheNextOffset(off_t w)
428 static long pagesize = -1;
430 pagesize = sysconf(_SC_PAGESIZE);
431 if (w % pagesize == 0)
434 return ((w / pagesize)+1)*pagesize;
437 /* return the address of the segment for the provided arch,
438 * or -1 if arch not found */
440 FcCacheSkipToArch (int fd, const char * arch)
442 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
443 char * candidate_arch;
444 off_t current_arch_start = 0;
446 /* skip arches that are not the current arch */
451 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
454 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
455 sizeof (candidate_arch_machine_name_count)) == 0)
457 if (!strlen(candidate_arch_machine_name_count))
459 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
461 // count = 0 should probably be distinguished from the !bs condition
462 if (!bs || bs < strlen (candidate_arch_machine_name_count))
465 candidate_arch++; /* skip leading space */
467 if (strcmp (candidate_arch, arch)==0)
468 return current_arch_start;
469 current_arch_start += bs;
475 /* Cuts out the segment at the file pointer (moves everything else
476 * down to cover it), and leaves the file pointer at the end of the
479 FcCacheCopyOld (int fd, int fd_orig, off_t start)
481 char * buf = malloc (8192);
482 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
484 int c, bytes_skipped;
491 lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET);
498 if ((c = read (fd_orig, buf, b)) <= 0)
500 if (write (fd, buf, c) < 0)
507 lseek (fd, start, SEEK_SET);
508 if (FcCacheReadString (fd, candidate_arch_machine_name,
509 sizeof (candidate_arch_machine_name)) == 0)
511 if (!strlen(candidate_arch_machine_name))
514 bs = strtol(candidate_arch_machine_name, 0, 16);
521 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
522 if ((c = read (fd, buf, 8192)) <= 0)
524 lseek (fd, start+bytes_skipped, SEEK_SET);
525 if (write (fd, buf, c) < 0)
530 lseek (fd, start+bytes_skipped, SEEK_SET);
541 /* Does not check that the cache has the appropriate arch section. */
543 FcDirCacheValid (const FcChar8 *dir)
545 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
546 struct stat file_stat, dir_stat;
548 if (stat ((char *) dir, &dir_stat) < 0)
550 FcStrFree (cache_file);
553 if (stat ((char *) cache_file, &file_stat) < 0)
555 FcStrFree (cache_file);
559 FcStrFree (cache_file);
561 * If the directory has been modified more recently than
562 * the cache file, the cache is not valid
564 if (dir_stat.st_mtime > file_stat.st_mtime)
570 /* Assumes that the cache file in 'dir' exists.
571 * Checks that the cache has the appropriate arch section. */
573 FcDirCacheHasCurrentArch (const FcChar8 *dir)
575 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
577 off_t current_arch_start;
578 char *current_arch_machine_name;
580 current_arch_machine_name = FcCacheMachineSignature();
581 fd = open ((char *)cache_file, O_RDONLY);
585 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
588 if (current_arch_start < 0)
595 FcDirCacheUnlink (const FcChar8 *dir)
597 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
598 struct stat cache_stat;
600 if (stat ((char *) cache_file, &cache_stat) == 0 &&
601 unlink ((char *)cache_file) != 0)
603 FcStrFree (cache_file);
607 FcStrFree (cache_file);
612 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
613 FcStrList *list, FcFontSet * set)
617 FcChar8 *file, *base;
623 * Read in the results from 'list'.
625 while ((dir = FcStrListNext (list)))
628 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
632 strcpy ((char *) file, (char *) dir);
633 strcat ((char *) file, "/");
634 base = file + strlen ((char *) file);
636 subdirs = FcStrSetCreate ();
639 fprintf (stderr, "Can't create directory set\n");
645 if (access ((char *) dir, X_OK) < 0)
653 fprintf (stderr, "\"%s\": ", dir);
657 FcStrSetDestroy (subdirs);
661 if (stat ((char *) dir, &statb) == -1)
663 fprintf (stderr, "\"%s\": ", dir);
665 FcStrSetDestroy (subdirs);
670 if (!S_ISDIR (statb.st_mode))
672 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
673 FcStrSetDestroy (subdirs);
677 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
679 if (FcDebug () & FC_DBG_FONTSET)
680 printf ("cache scan dir %s\n", dir);
682 FcDirScanConfig (set, subdirs, cache,
683 config->blanks, dir, FcFalse, config);
685 sublist = FcStrListCreate (subdirs);
686 FcStrSetDestroy (subdirs);
689 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
694 ret += FcCacheReadDirs (config, cache, sublist, set);
697 FcStrListDone (list);
702 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
704 FcFontSet * s = FcFontSetCreate();
708 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
714 FcFontSetDestroy (s);
718 /* read serialized state from the cache file */
720 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
722 char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
724 char * current_arch_machine_name;
725 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
726 off_t current_arch_start = 0;
727 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
732 current_arch_machine_name = FcCacheMachineSignature();
733 fd = open(cache_file, O_RDONLY);
737 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
738 if (current_arch_start < 0)
741 lseek (fd, current_arch_start, SEEK_SET);
742 if (FcCacheReadString (fd, candidate_arch_machine_name,
743 sizeof (candidate_arch_machine_name)) == 0)
746 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
747 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
749 if (!FcDirCacheConsume (fd, (const char *)dir, set))
764 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set)
767 void * current_dir_block;
770 read(fd, &metadata, sizeof(FcCache));
771 if (metadata.magic != FC_CACHE_MAGIC)
777 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
778 current_dir_block = mmap (0, metadata.count,
779 PROT_READ, MAP_SHARED, fd, pos);
780 if (current_dir_block == MAP_FAILED)
783 if (!FcFontSetUnserialize (metadata, set, current_dir_block))
786 FcCacheAddBankDir (metadata.bank, dir);
792 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
794 void * current_dir_block, * final_dir_block;
795 static unsigned int rand_state = 0;
796 int bank, needed_bytes_no_align;
799 rand_state = time(0L);
800 bank = rand_r(&rand_state);
802 while (FcCacheHaveBank(bank))
803 bank = rand_r(&rand_state);
805 memset (metadata, 0, sizeof(FcCache));
807 needed_bytes_no_align = FcFontSetNeededBytes (set);
808 metadata->count = needed_bytes_no_align +
809 FcFontSetNeededBytesAlign ();
810 metadata->magic = FC_CACHE_MAGIC;
811 metadata->bank = bank;
813 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
815 /* no, you don't really need to write any bytes at all. */
820 current_dir_block = malloc (metadata->count);
821 if (!current_dir_block)
824 memset (current_dir_block, 0, metadata->count);
825 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
827 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
830 if (!FcFontSetSerialize (bank, set))
833 return current_dir_block;
836 free (current_dir_block);
840 /* write serialized state to the cache file */
842 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
844 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
845 int fd, fd_orig, i, dirs_count;
848 off_t current_arch_start = 0, truncate_to;
850 char *current_arch_machine_name, * header;
851 void *current_dir_block;
856 current_dir_block = FcDirCacheProduce (set, &metadata);
858 if (metadata.count && !current_dir_block)
861 if (FcDebug () & FC_DBG_CACHE)
862 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
864 atomic = FcAtomicCreate (cache_file);
868 if (!FcAtomicLock (atomic))
871 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY, 0666);
873 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
877 current_arch_machine_name = FcCacheMachineSignature ();
878 current_arch_start = 0;
882 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
884 if (current_arch_start < 0)
885 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
887 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
893 current_arch_start = lseek(fd, 0, SEEK_CUR);
894 if (ftruncate (fd, current_arch_start) == -1)
897 /* allocate space for subdir names in this block */
899 for (i = 0; i < dirs->size; i++)
900 dirs_count += strlen((char *)dirs->strs[i]) + 1;
903 /* now write the address of the next offset */
904 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
905 header = malloc (10 + strlen (current_arch_machine_name));
908 sprintf (header, "%8x ", (int)truncate_to);
909 strcat (header, current_arch_machine_name);
910 if (!FcCacheWriteString (fd, header))
913 for (i = 0; i < dirs->size; i++)
914 FcCacheWriteString (fd, (char *)dirs->strs[i]);
915 FcCacheWriteString (fd, "");
917 write (fd, &metadata, sizeof(FcCache));
920 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
921 write (fd, current_dir_block, metadata.count);
922 free (current_dir_block);
925 /* this actually serves to pad out the cache file, if needed */
926 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
930 if (!FcAtomicReplaceOrig(atomic))
932 FcAtomicUnlock (atomic);
933 FcAtomicDestroy (atomic);
941 FcAtomicUnlock (atomic);
943 FcAtomicDestroy (atomic);
945 unlink ((char *)cache_file);
947 if (current_dir_block)
948 free (current_dir_block);
954 FcCacheMachineSignature ()
956 static char buf[MACHINE_SIGNATURE_SIZE];
957 int32_t magic = ENDIAN_TEST;
958 char * m = (char *)&magic;
960 sprintf (buf, "%2x%2x%2x%2x "
961 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
962 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
963 m[0], m[1], m[2], m[3],
964 (unsigned int)sizeof (char),
965 (unsigned int)sizeof (char *),
966 (unsigned int)sizeof (int),
967 (unsigned int)sizeof (FcPattern),
968 (unsigned int)sizeof (FcPatternEltPtr),
969 (unsigned int)sizeof (struct _FcPatternElt *),
970 (unsigned int)sizeof (FcPatternElt),
971 (unsigned int)sizeof (FcObjectPtr),
972 (unsigned int)sizeof (FcValueListPtr),
973 (unsigned int)sizeof (FcValue),
974 (unsigned int)sizeof (FcValueBinding),
975 (unsigned int)sizeof (struct _FcValueList *),
976 (unsigned int)sizeof (FcCharSet),
977 (unsigned int)sizeof (FcCharLeaf **),
978 (unsigned int)sizeof (FcChar16 *),
979 (unsigned int)sizeof (FcChar16),
980 (unsigned int)sizeof (FcCharLeaf),
981 (unsigned int)sizeof (FcChar32),
982 (unsigned int)sizeof (FcCache),
983 (unsigned int)sysconf(_SC_PAGESIZE));
988 static int banks_ptr = 0, banks_alloc = 0;
989 static int * bankId = 0, * bankIdx = 0;
990 static const char ** bankDirs = 0;
993 FcCacheHaveBank (int bank)
997 if (bank < FC_BANK_FIRST)
1000 for (i = 0; i < banks_ptr; i++)
1001 if (bankId[i] == bank)
1008 FcCacheBankToIndex (int bank)
1012 for (i = 0; i < banks_ptr; i++)
1013 if (bankId[bankIdx[i]] == bank)
1017 for (j = i; j > 0; j--)
1018 bankIdx[j] = bankIdx[j-1];
1023 if (banks_ptr >= banks_alloc)
1028 b = realloc (bankId, (banks_alloc + 4) * sizeof(int));
1033 bidx = realloc (bankIdx, (banks_alloc + 4) * sizeof(int));
1038 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1053 FcCacheAddBankDir (int bank, const char * dir)
1055 int bi = FcCacheBankToIndex (bank);
1060 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1064 FcCacheFindBankDir (int bank)
1066 int bi = FcCacheBankToIndex (bank);
1067 return bankDirs[bi];