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)
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
);
313 if (!FcAtomicLock (atomic
))
315 fd
= open ((char *) FcAtomicNewFile(atomic
), O_RDWR
| O_CREAT
,
320 fd_orig
= open ((char *) FcAtomicOrigFile(atomic
), O_RDONLY
);
322 current_arch_machine_name
= FcCacheMachineSignature ();
324 current_arch_start
= 0;
326 current_arch_start
= FcCacheSkipToArch (fd_orig
,
327 current_arch_machine_name
);
329 if (current_arch_start
< 0)
330 current_arch_start
= FcCacheNextOffset (lseek(fd
, 0, SEEK_END
));
332 if (!FcCacheCopyOld(fd
, fd_orig
, current_arch_start
))
338 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
339 if (ftruncate (fd
, current_arch_start
) == -1)
342 header
= malloc (10 + strlen (current_arch_machine_name
));
346 truncate_to
= current_arch_start
+ strlen(current_arch_machine_name
) + 11;
347 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
349 truncate_to
+= strlen(dir
->name
) + 1;
350 truncate_to
+= sizeof (FcCache
);
351 truncate_to
= FcCacheNextOffset (current_arch_start
+ truncate_to
);
352 truncate_to
+= dir
->metadata
.count
;
354 truncate_to
-= current_arch_start
;
356 sprintf (header
, "%8x ", (int)truncate_to
);
357 strcat (header
, current_arch_machine_name
);
358 if (!FcCacheWriteString (fd
, header
))
361 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
365 FcCacheWriteString (fd
, dir
->name
);
366 write (fd
, &dir
->metadata
, sizeof(FcCache
));
367 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
)), SEEK_SET
);
368 write (fd
, dir
->ent
, dir
->metadata
.count
);
372 FcCacheWriteString (fd
, "");
374 if (close (fd
) == -1)
377 if (!FcAtomicReplaceOrig (atomic
))
380 FcAtomicUnlock (atomic
);
381 FcAtomicDestroy (atomic
);
383 cache
->updated
= FcFalse
;
394 FcAtomicDeleteNew (atomic
);
396 FcAtomicUnlock (atomic
);
398 FcAtomicDestroy (atomic
);
402 #define PAGESIZE 8192
404 * Find the next presumably-mmapable offset after the supplied file
408 FcCacheNextOffset(off_t w
)
410 if (w
% PAGESIZE
== 0)
413 return ((w
/ PAGESIZE
)+1)*PAGESIZE
;
416 /* return the address of the segment for the provided arch,
417 * or -1 if arch not found */
419 FcCacheSkipToArch (int fd
, const char * arch
)
421 char candidate_arch_machine_name_count
[MACHINE_SIGNATURE_SIZE
+ 9];
422 char * candidate_arch
;
423 off_t current_arch_start
= 0;
425 /* skip arches that are not the current arch */
430 if (lseek (fd
, current_arch_start
, SEEK_SET
) != current_arch_start
)
433 if (FcCacheReadString (fd
, candidate_arch_machine_name_count
,
434 sizeof (candidate_arch_machine_name_count
)) == 0)
436 if (!strlen(candidate_arch_machine_name_count
))
438 bs
= strtol(candidate_arch_machine_name_count
, &candidate_arch
, 16);
440 // count = 0 should probably be distinguished from the !bs condition
441 if (!bs
|| bs
< strlen (candidate_arch_machine_name_count
))
444 candidate_arch
++; /* skip leading space */
446 if (strcmp (candidate_arch
, arch
)==0)
447 return current_arch_start
;
448 current_arch_start
+= bs
;
454 /* Cuts out the segment at the file pointer (moves everything else
455 * down to cover it), and leaves the file pointer at the end of the
458 FcCacheCopyOld (int fd
, int fd_orig
, off_t start
)
460 char * buf
= malloc (8192);
461 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
463 int c
, bytes_skipped
;
470 lseek (fd
, 0, SEEK_SET
); lseek (fd_orig
, 0, SEEK_SET
);
477 if ((c
= read (fd_orig
, buf
, b
)) <= 0)
479 if (write (fd
, buf
, c
) < 0)
486 lseek (fd
, start
, SEEK_SET
);
487 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
488 sizeof (candidate_arch_machine_name
)) == 0)
490 if (!strlen(candidate_arch_machine_name
))
493 bs
= strtol(candidate_arch_machine_name
, 0, 16);
500 lseek (fd
, start
+bs
+bytes_skipped
, SEEK_SET
);
501 if ((c
= read (fd
, buf
, 8192)) <= 0)
503 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
504 if (write (fd
, buf
, c
) < 0)
509 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
521 FcDirCacheValid (const FcChar8
*dir
)
523 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
524 struct stat file_stat
, dir_stat
;
526 if (stat ((char *) dir
, &dir_stat
) < 0)
528 FcStrFree (cache_file
);
531 if (stat ((char *) cache_file
, &file_stat
) < 0)
533 FcStrFree (cache_file
);
536 FcStrFree (cache_file
);
538 * If the directory has been modified more recently than
539 * the cache file, the cache is not valid
541 if (dir_stat
.st_mtime
- file_stat
.st_mtime
> 0)
547 FcCacheReadDirs (FcConfig
* config
, FcGlobalCache
* cache
,
548 FcStrList
*list
, FcFontSet
* set
)
552 FcChar8
*file
, *base
;
558 * Read in the results from 'list'.
560 while ((dir
= FcStrListNext (list
)))
563 file
= (FcChar8
*) malloc (strlen ((char *) dir
) + 1 + FC_MAX_FILE_LEN
+ 1);
567 strcpy ((char *) file
, (char *) dir
);
568 strcat ((char *) file
, "/");
569 base
= file
+ strlen ((char *) file
);
571 subdirs
= FcStrSetCreate ();
574 fprintf (stderr
, "Can't create directory set\n");
580 if (access ((char *) dir
, X_OK
) < 0)
588 fprintf (stderr
, "\"%s\": ", dir
);
592 FcStrSetDestroy (subdirs
);
596 if (stat ((char *) dir
, &statb
) == -1)
598 fprintf (stderr
, "\"%s\": ", dir
);
600 FcStrSetDestroy (subdirs
);
605 if (!S_ISDIR (statb
.st_mode
))
607 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
608 FcStrSetDestroy (subdirs
);
612 if (!FcDirCacheValid (dir
) || !FcDirCacheRead (set
, subdirs
, dir
))
614 if (FcDebug () & FC_DBG_FONTSET
)
615 printf ("cache scan dir %s\n", dir
);
617 FcDirScanConfig (set
, subdirs
, cache
,
618 config
->blanks
, dir
, FcFalse
, config
);
620 sublist
= FcStrListCreate (subdirs
);
621 FcStrSetDestroy (subdirs
);
624 fprintf (stderr
, "Can't create subdir list in \"%s\"\n", dir
);
629 ret
+= FcCacheReadDirs (config
, cache
, sublist
, set
);
632 FcStrListDone (list
);
637 FcCacheRead (FcConfig
*config
, FcGlobalCache
* cache
)
639 FcFontSet
* s
= FcFontSetCreate();
643 if (FcCacheReadDirs (config
, cache
, FcConfigGetConfigDirs (config
), s
))
649 FcFontSetDestroy (s
);
653 /* read serialized state from the cache file */
655 FcDirCacheRead (FcFontSet
* set
, FcStrSet
* dirs
, const FcChar8
*dir
)
657 char *cache_file
= (char *)FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
659 char * current_arch_machine_name
;
660 char candidate_arch_machine_name
[9+MACHINE_SIGNATURE_SIZE
];
661 off_t current_arch_start
= 0;
662 char subdirName
[FC_MAX_FILE_LEN
+ 1 + 12 + 1];
667 current_arch_machine_name
= FcCacheMachineSignature();
668 fd
= open(cache_file
, O_RDONLY
);
672 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
673 if (current_arch_start
< 0)
676 lseek (fd
, current_arch_start
, SEEK_SET
);
677 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
678 sizeof (candidate_arch_machine_name
)) == 0)
681 while (strlen(FcCacheReadString (fd
, subdirName
, sizeof (subdirName
))) > 0)
682 FcStrSetAdd (dirs
, (FcChar8
*)subdirName
);
684 if (!FcDirCacheConsume (fd
, set
))
699 FcDirCacheConsume (int fd
, FcFontSet
*set
)
702 void * current_dir_block
;
705 read(fd
, &metadata
, sizeof(FcCache
));
706 if (metadata
.magic
!= FC_CACHE_MAGIC
)
712 pos
= FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
));
713 current_dir_block
= mmap (0, metadata
.count
,
714 PROT_READ
, MAP_SHARED
, fd
, pos
);
715 if (current_dir_block
== MAP_FAILED
)
718 if (!FcFontSetUnserialize (metadata
, set
, current_dir_block
))
725 FcDirCacheProduce (FcFontSet
*set
, FcCache
*metadata
)
727 void * current_dir_block
, * final_dir_block
;
728 static unsigned int rand_state
= 0;
732 rand_state
= time(0L);
733 bank
= rand_r(&rand_state
);
735 while (FcCacheHaveBank(bank
))
736 bank
= rand_r(&rand_state
);
738 memset (metadata
, 0, sizeof(FcCache
));
740 metadata
->count
= FcFontSetNeededBytes (set
);
741 metadata
->magic
= FC_CACHE_MAGIC
;
742 metadata
->bank
= bank
;
744 if (!metadata
->count
) /* not a failure, no fonts to write */
747 current_dir_block
= malloc (metadata
->count
);
748 if (!current_dir_block
)
750 final_dir_block
= FcFontSetDistributeBytes (metadata
, current_dir_block
);
752 if ((char *)current_dir_block
+ metadata
->count
!= final_dir_block
)
755 if (!FcFontSetSerialize (bank
, set
))
758 return current_dir_block
;
761 free (current_dir_block
);
765 /* write serialized state to the cache file */
767 FcDirCacheWrite (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
)
769 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
770 int fd
, fd_orig
, i
, dirs_count
;
773 off_t current_arch_start
= 0, truncate_to
;
775 char *current_arch_machine_name
, * header
;
776 void *current_dir_block
;
781 current_dir_block
= FcDirCacheProduce (set
, &metadata
);
783 if (metadata
.count
&& !current_dir_block
)
786 if (FcDebug () & FC_DBG_CACHE
)
787 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file
);
789 atomic
= FcAtomicCreate (cache_file
);
793 if (!FcAtomicLock (atomic
))
796 fd_orig
= open((char *)FcAtomicOrigFile (atomic
), O_RDONLY
, 0666);
798 fd
= open((char *)FcAtomicNewFile (atomic
), O_RDWR
| O_CREAT
, 0666);
802 current_arch_machine_name
= FcCacheMachineSignature ();
803 current_arch_start
= 0;
807 FcCacheSkipToArch(fd_orig
, current_arch_machine_name
);
809 if (current_arch_start
< 0)
810 current_arch_start
= FcCacheNextOffset (lseek(fd
, 0, SEEK_END
));
812 if (fd_orig
!= -1 && !FcCacheCopyOld(fd
, fd_orig
, current_arch_start
))
818 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
819 if (ftruncate (fd
, current_arch_start
) == -1)
822 /* allocate space for subdir names in this block */
824 for (i
= 0; i
< dirs
->size
; i
++)
825 dirs_count
+= strlen((char *)dirs
->strs
[i
]) + 1;
828 /* now write the address of the next offset */
829 truncate_to
= FcCacheNextOffset (FcCacheNextOffset (current_arch_start
+ sizeof (FcCache
) + dirs_count
) + metadata
.count
) - current_arch_start
;
830 header
= malloc (10 + strlen (current_arch_machine_name
));
833 sprintf (header
, "%8x ", (int)truncate_to
);
834 strcat (header
, current_arch_machine_name
);
835 if (!FcCacheWriteString (fd
, header
))
838 for (i
= 0; i
< dirs
->size
; i
++)
839 FcCacheWriteString (fd
, (char *)dirs
->strs
[i
]);
840 FcCacheWriteString (fd
, "");
842 write (fd
, &metadata
, sizeof(FcCache
));
845 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_END
)), SEEK_SET
);
846 write (fd
, current_dir_block
, metadata
.count
);
847 free (current_dir_block
);
850 /* this actually serves to pad out the cache file, if needed */
851 if (ftruncate (fd
, current_arch_start
+ truncate_to
) == -1)
855 if (!FcAtomicReplaceOrig(atomic
))
857 FcAtomicUnlock (atomic
);
858 FcAtomicDestroy (atomic
);
866 FcAtomicUnlock (atomic
);
868 FcAtomicDestroy (atomic
);
870 unlink ((char *)cache_file
);
872 if (current_dir_block
)
873 free (current_dir_block
);
879 FcCacheMachineSignature ()
881 static char buf
[MACHINE_SIGNATURE_SIZE
];
882 int magic
= ENDIAN_TEST
;
883 char * m
= (char *)&magic
;
885 sprintf (buf
, "%2x%2x%2x%2x "
886 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
887 "%4x %4x %4x %4x %4x %4x %4x\n",
888 m
[0], m
[1], m
[2], m
[3],
889 (unsigned int)sizeof (char),
890 (unsigned int)sizeof (char *),
891 (unsigned int)sizeof (int),
892 (unsigned int)sizeof (FcPattern
),
893 (unsigned int)sizeof (FcPatternEltPtr
),
894 (unsigned int)sizeof (struct _FcPatternElt
*),
895 (unsigned int)sizeof (FcPatternElt
),
896 (unsigned int)sizeof (FcObjectPtr
),
897 (unsigned int)sizeof (FcValueListPtr
),
898 (unsigned int)sizeof (FcValue
),
899 (unsigned int)sizeof (FcValueBinding
),
900 (unsigned int)sizeof (struct _FcValueList
*),
901 (unsigned int)sizeof (FcCharSet
),
902 (unsigned int)sizeof (FcCharLeaf
**),
903 (unsigned int)sizeof (FcChar16
*),
904 (unsigned int)sizeof (FcChar16
),
905 (unsigned int)sizeof (FcCharLeaf
),
906 (unsigned int)sizeof (FcChar32
),
907 (unsigned int)sizeof (FcCache
));
912 static int banks_ptr
= 0, banks_alloc
= 0;
913 static int * bankId
= 0;
916 FcCacheHaveBank (int bank
)
920 if (bank
< FC_BANK_FIRST
)
923 for (i
= 0; i
< banks_ptr
; i
++)
924 if (bankId
[i
] == bank
)
931 FcCacheBankToIndex (int bank
)
933 static int lastBank
= FC_BANK_DYNAMIC
, lastIndex
= -1;
937 if (bank
== lastBank
)
940 for (i
= 0; i
< banks_ptr
; i
++)
941 if (bankId
[i
] == bank
)
944 if (banks_ptr
>= banks_alloc
)
946 b
= realloc (bankId
, (banks_alloc
+ 4) * sizeof(int));