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 FcCacheMoveDown (int fd, 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)
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)
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)
234 FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config)
237 FcBool ret = FcFalse;
242 for (d = cache->dirs; d; d = d->next)
244 if (strncmp (d->name, dir, strlen(dir)) == 0)
246 lseek (cache->fd, d->offset, SEEK_SET);
247 if (!FcDirCacheConsume (cache->fd, set))
249 if (strcmp (d->name, dir) == 0)
258 FcGlobalCacheUpdate (FcGlobalCache *cache,
262 FcGlobalCacheDir * d;
267 for (d = cache->dirs; d; d = d->next)
269 if (strcmp(d->name, name) == 0)
275 d = malloc (sizeof (FcGlobalCacheDir));
278 d->next = cache->dirs;
282 cache->updated = FcTrue;
284 d->name = (char *)FcStrCopy ((FcChar8 *)name);
285 d->ent = FcDirCacheProduce (set, &d->metadata);
291 FcGlobalCacheSave (FcGlobalCache *cache,
292 const FcChar8 *cache_file)
295 FcGlobalCacheDir *dir;
297 off_t current_arch_start = 0, truncate_to;
298 char * current_arch_machine_name, * header;
303 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
304 /* Set-UID programs can't safely update the cache */
305 if (getuid () != geteuid ())
309 atomic = FcAtomicCreate (cache_file);
312 if (!FcAtomicLock (atomic))
314 fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT,
319 current_arch_machine_name = FcCacheMachineSignature ();
320 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
321 if (current_arch_start < 0)
322 current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
324 if (!FcCacheMoveDown(fd, current_arch_start))
327 current_arch_start = lseek(fd, 0, SEEK_CUR);
328 if (ftruncate (fd, current_arch_start) == -1)
331 header = malloc (10 + strlen (current_arch_machine_name));
335 truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11;
336 for (dir = cache->dirs; dir; dir = dir->next)
338 truncate_to += strlen(dir->name) + 1;
339 truncate_to += sizeof (FcCache);
340 truncate_to = FcCacheNextOffset (current_arch_start + truncate_to);
341 truncate_to += dir->metadata.count;
343 truncate_to -= current_arch_start;
345 sprintf (header, "%8x ", (int)truncate_to);
346 strcat (header, current_arch_machine_name);
347 if (!FcCacheWriteString (fd, header))
350 for (dir = cache->dirs; dir; dir = dir->next)
354 FcCacheWriteString (fd, dir->name);
355 write (fd, &dir->metadata, sizeof(FcCache));
356 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
357 write (fd, dir->ent, dir->metadata.count);
361 FcCacheWriteString (fd, "");
363 if (close (fd) == -1)
366 if (!FcAtomicReplaceOrig (atomic))
369 FcAtomicUnlock (atomic);
370 FcAtomicDestroy (atomic);
372 cache->updated = FcFalse;
376 FcAtomicDeleteNew (atomic);
378 FcAtomicUnlock (atomic);
380 FcAtomicDestroy (atomic);
385 #define PAGESIZE 8192
387 * Find the next presumably-mmapable offset after the supplied file
391 FcCacheNextOffset(off_t w)
393 if (w % PAGESIZE == 0)
396 return ((w / PAGESIZE)+1)*PAGESIZE;
399 /* return the address of the segment for the provided arch,
400 * or -1 if arch not found */
402 FcCacheSkipToArch (int fd, const char * arch)
404 char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
405 char * candidate_arch;
406 off_t current_arch_start = 0;
408 /* skip arches that are not the current arch */
413 if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start)
416 if (FcCacheReadString (fd, candidate_arch_machine_name_count,
417 sizeof (candidate_arch_machine_name_count)) == 0)
419 if (!strlen(candidate_arch_machine_name_count))
421 bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16);
423 if (!bs || bs < strlen (candidate_arch_machine_name_count))
426 candidate_arch++; /* skip leading space */
428 if (strcmp (candidate_arch, arch)==0)
429 return current_arch_start;
430 current_arch_start += bs;
436 /* Cuts out the segment at the file pointer (moves everything else
437 * down to cover it), and leaves the file pointer at the end of the
440 FcCacheMoveDown (int fd, off_t start)
442 char * buf = malloc (8192);
443 char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9];
445 int c, bytes_skipped;
450 lseek (fd, start, SEEK_SET);
451 if (FcCacheReadString (fd, candidate_arch_machine_name,
452 sizeof (candidate_arch_machine_name)) == 0)
454 if (!strlen(candidate_arch_machine_name))
457 bs = strtol(candidate_arch_machine_name, 0, 16);
464 lseek (fd, start+bs+bytes_skipped, SEEK_SET);
465 if ((c = read (fd, buf, 8192)) <= 0)
467 lseek (fd, start+bytes_skipped, SEEK_SET);
468 if (write (fd, buf, c) < 0)
473 lseek (fd, start+bytes_skipped, SEEK_SET);
485 FcDirCacheValid (const FcChar8 *dir)
487 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
488 struct stat file_stat, dir_stat;
490 if (stat ((char *) dir, &dir_stat) < 0)
492 FcStrFree (cache_file);
495 if (stat ((char *) cache_file, &file_stat) < 0)
497 FcStrFree (cache_file);
500 FcStrFree (cache_file);
502 * If the directory has been modified more recently than
503 * the cache file, the cache is not valid
505 if (dir_stat.st_mtime - file_stat.st_mtime > 0)
511 FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache,
512 FcStrList *list, FcFontSet * set)
516 FcChar8 *file, *base;
522 * Read in the results from 'list'.
524 while ((dir = FcStrListNext (list)))
527 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
531 strcpy ((char *) file, (char *) dir);
532 strcat ((char *) file, "/");
533 base = file + strlen ((char *) file);
535 subdirs = FcStrSetCreate ();
538 fprintf (stderr, "Can't create directory set\n");
544 if (access ((char *) dir, X_OK) < 0)
552 fprintf (stderr, "\"%s\": ", dir);
556 FcStrSetDestroy (subdirs);
560 if (stat ((char *) dir, &statb) == -1)
562 fprintf (stderr, "\"%s\": ", dir);
564 FcStrSetDestroy (subdirs);
569 if (!S_ISDIR (statb.st_mode))
571 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
572 FcStrSetDestroy (subdirs);
576 if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
578 if (FcDebug () & FC_DBG_FONTSET)
579 printf ("cache scan dir %s\n", dir);
581 FcDirScanConfig (set, subdirs, cache,
582 config->blanks, dir, FcFalse, config);
584 sublist = FcStrListCreate (subdirs);
585 FcStrSetDestroy (subdirs);
588 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
593 ret += FcCacheReadDirs (config, cache, sublist, set);
596 FcStrListDone (list);
601 FcCacheRead (FcConfig *config, FcGlobalCache * cache)
603 FcFontSet * s = FcFontSetCreate();
607 if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s))
613 FcFontSetDestroy (s);
617 /* read serialized state from the cache file */
619 FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
621 char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
623 char * current_arch_machine_name;
624 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
625 off_t current_arch_start = 0;
626 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
631 current_arch_machine_name = FcCacheMachineSignature();
632 fd = open(cache_file, O_RDONLY);
636 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
637 if (current_arch_start < 0)
640 lseek (fd, current_arch_start, SEEK_SET);
641 if (FcCacheReadString (fd, candidate_arch_machine_name,
642 sizeof (candidate_arch_machine_name)) == 0)
645 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
646 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
648 if (!FcDirCacheConsume (fd, set))
663 FcDirCacheConsume (int fd, FcFontSet *set)
666 void * current_dir_block;
669 read(fd, &metadata, sizeof(FcCache));
670 if (metadata.magic != FC_CACHE_MAGIC)
676 pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
677 current_dir_block = mmap (0, metadata.count,
678 PROT_READ, MAP_SHARED, fd, pos);
679 if (current_dir_block == MAP_FAILED)
682 if (!FcFontSetUnserialize (metadata, set, current_dir_block))
689 FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
691 void * current_dir_block, * final_dir_block;
692 static unsigned int rand_state = 0;
696 rand_state = time(0L);
697 bank = rand_r(&rand_state);
699 while (FcCacheHaveBank(bank))
700 bank = rand_r(&rand_state);
702 memset (metadata, 0, sizeof(FcCache));
704 metadata->count = FcFontSetNeededBytes (set);
705 metadata->magic = FC_CACHE_MAGIC;
706 metadata->bank = bank;
708 if (!metadata->count) /* not a failure, no fonts to write */
711 current_dir_block = malloc (metadata->count);
712 if (!current_dir_block)
714 final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block);
716 if ((char *)current_dir_block + metadata->count != final_dir_block)
719 if (!FcFontSetSerialize (bank, set))
722 return current_dir_block;
725 free (current_dir_block);
729 /* write serialized state to the cache file */
731 FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
733 FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
736 off_t current_arch_start = 0, truncate_to;
738 char * current_arch_machine_name, * header;
739 void * current_dir_block;
744 current_dir_block = FcDirCacheProduce (set, &metadata);
748 unlink ((char *)cache_file);
753 if (!current_dir_block)
756 if (FcDebug () & FC_DBG_CACHE)
757 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
759 fd = open((char *)cache_file, O_RDWR | O_CREAT, 0666);
763 current_arch_machine_name = FcCacheMachineSignature ();
764 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
765 if (current_arch_start < 0)
766 current_arch_start = FcCacheNextOffset (lseek(fd, 0, SEEK_END));
768 if (!FcCacheMoveDown(fd, current_arch_start))
771 current_arch_start = lseek(fd, 0, SEEK_CUR);
772 if (ftruncate (fd, current_arch_start) == -1)
775 /* now write the address of the next offset */
776 truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache)) + metadata.count) - current_arch_start;
777 header = malloc (10 + strlen (current_arch_machine_name));
780 sprintf (header, "%8x ", (int)truncate_to);
781 strcat (header, current_arch_machine_name);
782 if (!FcCacheWriteString (fd, header))
785 for (i = 0; i < dirs->size; i++)
786 FcCacheWriteString (fd, (char *)dirs->strs[i]);
787 FcCacheWriteString (fd, "");
789 write (fd, &metadata, sizeof(FcCache));
790 lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET);
791 write (fd, current_dir_block, metadata.count);
792 free (current_dir_block);
794 /* this actually serves to pad out the cache file, if needed */
795 if (ftruncate (fd, current_arch_start + truncate_to) == -1)
804 free (current_dir_block);
806 unlink ((char *)cache_file);
812 FcCacheMachineSignature ()
814 static char buf[MACHINE_SIGNATURE_SIZE];
815 int magic = ENDIAN_TEST;
816 char * m = (char *)&magic;
818 sprintf (buf, "%2x%2x%2x%2x "
819 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
820 "%4x %4x %4x %4x %4x %4x %4x\n",
821 m[0], m[1], m[2], m[3],
826 sizeof (FcPatternEltPtr),
827 sizeof (struct _FcPatternElt *),
828 sizeof (FcPatternElt),
829 sizeof (FcObjectPtr),
830 sizeof (FcValueListPtr),
832 sizeof (FcValueBinding),
833 sizeof (struct _FcValueList *),
835 sizeof (FcCharLeaf **),
845 static int banks_ptr = 0, banks_alloc = 0;
846 static int * bankId = 0;
849 FcCacheHaveBank (int bank)
853 if (bank < FC_BANK_FIRST)
856 for (i = 0; i < banks_ptr; i++)
857 if (bankId[i] == bank)
864 FcCacheBankToIndex (int bank)
866 static int lastBank = FC_BANK_DYNAMIC, lastIndex = -1;
870 if (bank == lastBank)
873 for (i = 0; i < banks_ptr; i++)
874 if (bankId[i] == bank)
877 if (banks_ptr >= banks_alloc)
879 b = realloc (bankId, (banks_alloc + 4) * sizeof(int));