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)))
627 if (!FcConfigAcceptFilename (config, dir))
631 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
635 strcpy ((char *) file, (char *) dir);
636 strcat ((char *) file, "/");
637 base = file + strlen ((char *) file);
639 subdirs = FcStrSetCreate ();
642 fprintf (stderr, "Can't create directory set\n");
648 if (access ((char *) dir, X_OK) < 0)
656 fprintf (stderr, "\"%s\": ", dir);
660 FcStrSetDestroy (subdirs);
664 if (stat ((char *) dir, &statb) == -1)
666 fprintf (stderr, "\"%s\": ", dir);
668 FcStrSetDestroy (subdirs);
673 if (!S_ISDIR (statb.st_mode))
675 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
676 FcStrSetDestroy (subdirs);
680 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
682 if (FcDebug () & FC_DBG_FONTSET)
683 printf ("cache scan dir %s\n", dir);
685 FcDirScanConfig (set, subdirs, cache,
686 config->blanks, dir, FcFalse, config);
688 sublist = FcStrListCreate (subdirs);
689 FcStrSetDestroy (subdirs);
692 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
697 ret += FcCacheReadDirs (config, cache, sublist, set);
700 FcStrListDone (list);
705 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
707 FcFontSet * s = FcFontSetCreate();
711 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
717 FcFontSetDestroy (s);
721 /* read serialized state from the cache file */
723 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
725 char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
727 char * current_arch_machine_name;
728 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
729 off_t current_arch_start = 0;
730 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
735 current_arch_machine_name = FcCacheMachineSignature();
736 fd = open(cache_file, O_RDONLY);
740 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
741 if (current_arch_start < 0)
744 lseek (fd, current_arch_start, SEEK_SET);
745 if (FcCacheReadString (fd, candidate_arch_machine_name,
746 sizeof (candidate_arch_machine_name)) == 0)
749 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
750 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
752 if (!FcDirCacheConsume (fd, (const char *)dir, set))
767 FcDirCacheConsume (int fd, const char * dir, FcFontSet *set)
770 void * current_dir_block;
773 read(fd, &metadata, sizeof(FcCache));
774 if (metadata.magic != FC_CACHE_MAGIC)
780 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
781 current_dir_block = mmap (0, metadata.count,
782 PROT_READ, MAP_SHARED, fd, pos);
783 if (current_dir_block == MAP_FAILED)
786 if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
789 FcCacheAddBankDir (metadata.bank, dir);
795 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
797 void * current_dir_block, * final_dir_block;
798 static unsigned int rand_state = 0;
799 int bank, needed_bytes_no_align;
802 rand_state = time(0L);
803 bank = rand_r(&rand_state);
805 while (FcCacheHaveBank(bank))
806 bank = rand_r(&rand_state);
808 memset (metadata, 0, sizeof(FcCache));
810 needed_bytes_no_align = FcFontSetNeededBytes (set);
811 metadata->count = needed_bytes_no_align +
812 FcFontSetNeededBytesAlign ();
813 metadata->magic = FC_CACHE_MAGIC;
814 metadata->bank = bank;
816 if (!needed_bytes_no_align) /* not a failure, no fonts to write */
818 /* no, you don't really need to write any bytes at all. */
823 current_dir_block = malloc (metadata->count);
824 if (!current_dir_block)
827 memset (current_dir_block, 0, metadata->count);
828 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
830 if ((void *)((char *)current_dir_block+metadata->count) < final_dir_block)
833 if (!FcFontSetSerialize (bank, set))
836 return current_dir_block;
839 free (current_dir_block);
843 /* write serialized state to the cache file */
845 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
847 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
848 int fd, fd_orig, i, dirs_count;
851 off_t current_arch_start = 0, truncate_to;
853 char *current_arch_machine_name, * header;
854 void *current_dir_block;
859 current_dir_block = FcDirCacheProduce (set, &metadata);
861 if (metadata.count && !current_dir_block)
864 if (FcDebug () & FC_DBG_CACHE)
865 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
867 atomic = FcAtomicCreate (cache_file);
871 if (!FcAtomicLock (atomic))
874 fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY, 0666);
876 fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666);
880 current_arch_machine_name = FcCacheMachineSignature ();
881 current_arch_start = 0;
885 FcCacheSkipToArch(fd_orig, current_arch_machine_name);
887 if (current_arch_start < 0)
888 current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
890 if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
896 current_arch_start = lseek(fd, 0, SEEK_CUR);
897 if (ftruncate (fd, current_arch_start) == -1)
900 /* allocate space for subdir names in this block */
902 for (i = 0; i < dirs->size; i++)
903 dirs_count += strlen((char *)dirs->strs[i]) + 1;
906 /* now write the address of the next offset */
907 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start;
908 header = malloc (10 + strlen (current_arch_machine_name));
911 sprintf (header, "%8x ", (int)truncate_to);
912 strcat (header, current_arch_machine_name);
913 if (!FcCacheWriteString (fd, header))
916 for (i = 0; i < dirs->size; i++)
917 FcCacheWriteString (fd, (char *)dirs->strs[i]);
918 FcCacheWriteString (fd, "");
920 write (fd, &metadata, sizeof(FcCache));
923 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
924 write (fd, current_dir_block, metadata.count);
925 free (current_dir_block);
928 /* this actually serves to pad out the cache file, if needed */
929 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
933 if (!FcAtomicReplaceOrig(atomic))
935 FcAtomicUnlock (atomic);
936 FcAtomicDestroy (atomic);
944 FcAtomicUnlock (atomic);
946 FcAtomicDestroy (atomic);
948 unlink ((char *)cache_file);
950 if (current_dir_block)
951 free (current_dir_block);
957 FcCacheMachineSignature ()
959 static char buf[MACHINE_SIGNATURE_SIZE];
960 int32_t magic = ENDIAN_TEST;
961 char * m = (char *)&magic;
963 sprintf (buf, "%2x%2x%2x%2x "
964 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
965 "%4x %4x %4x %4x %4x %4x %4x %4x\n",
966 m[0], m[1], m[2], m[3],
967 (unsigned int)sizeof (char),
968 (unsigned int)sizeof (char *),
969 (unsigned int)sizeof (int),
970 (unsigned int)sizeof (FcPattern),
971 (unsigned int)sizeof (FcPatternEltPtr),
972 (unsigned int)sizeof (struct _FcPatternElt *),
973 (unsigned int)sizeof (FcPatternElt),
974 (unsigned int)sizeof (FcObjectPtr),
975 (unsigned int)sizeof (FcValueListPtr),
976 (unsigned int)sizeof (FcValue),
977 (unsigned int)sizeof (FcValueBinding),
978 (unsigned int)sizeof (struct _FcValueList *),
979 (unsigned int)sizeof (FcCharSet),
980 (unsigned int)sizeof (FcCharLeaf **),
981 (unsigned int)sizeof (FcChar16 *),
982 (unsigned int)sizeof (FcChar16),
983 (unsigned int)sizeof (FcCharLeaf),
984 (unsigned int)sizeof (FcChar32),
985 (unsigned int)sizeof (FcCache),
986 (unsigned int)sysconf(_SC_PAGESIZE));
991 static int banks_ptr = 0, banks_alloc = 0;
992 static int * bankId = 0, * bankIdx = 0;
993 static const char ** bankDirs = 0;
996 FcCacheHaveBank (int bank)
1000 if (bank < FC_BANK_FIRST)
1003 for (i = 0; i < banks_ptr; i++)
1004 if (bankId[i] == bank)
1011 FcCacheBankToIndex (int bank)
1015 for (i = 0; i < banks_ptr; i++)
1016 if (bankId[bankIdx[i]] == bank)
1020 for (j = i; j > 0; j--)
1021 bankIdx[j] = bankIdx[j-1];
1026 if (banks_ptr >= banks_alloc)
1031 b = realloc (bankId, (banks_alloc + 4) * sizeof(int));
1036 bidx = realloc (bankIdx, (banks_alloc + 4) * sizeof(int));
1041 bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *));
1056 FcCacheAddBankDir (int bank, const char * dir)
1058 int bi = FcCacheBankToIndex (bank);
1063 bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir);
1067 FcCacheFindBankDir (int bank)
1069 int bi = FcCacheBankToIndex (bank);
1070 return bankDirs[bi];