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 // count = 0 should probably be distinguished from the !bs condition
424 if (!bs
|| bs
< strlen (candidate_arch_machine_name_count
))
427 candidate_arch
++; /* skip leading space */
429 if (strcmp (candidate_arch
, arch
)==0)
430 return current_arch_start
;
431 current_arch_start
+= bs
;
437 /* Cuts out the segment at the file pointer (moves everything else
438 * down to cover it), and leaves the file pointer at the end of the
441 FcCacheMoveDown (int fd
, off_t start
)
443 char * buf
= malloc (8192);
444 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
446 int c
, bytes_skipped
;
451 lseek (fd
, start
, SEEK_SET
);
452 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
453 sizeof (candidate_arch_machine_name
)) == 0)
455 if (!strlen(candidate_arch_machine_name
))
458 bs
= strtol(candidate_arch_machine_name
, 0, 16);
465 lseek (fd
, start
+bs
+bytes_skipped
, SEEK_SET
);
466 if ((c
= read (fd
, buf
, 8192)) <= 0)
468 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
469 if (write (fd
, buf
, c
) < 0)
474 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
486 FcDirCacheValid (const FcChar8
*dir
)
488 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
489 struct stat file_stat
, dir_stat
;
491 if (stat ((char *) dir
, &dir_stat
) < 0)
493 FcStrFree (cache_file
);
496 if (stat ((char *) cache_file
, &file_stat
) < 0)
498 FcStrFree (cache_file
);
501 FcStrFree (cache_file
);
503 * If the directory has been modified more recently than
504 * the cache file, the cache is not valid
506 if (dir_stat
.st_mtime
- file_stat
.st_mtime
> 0)
512 FcCacheReadDirs (FcConfig
* config
, FcGlobalCache
* cache
,
513 FcStrList
*list
, FcFontSet
* set
)
517 FcChar8
*file
, *base
;
523 * Read in the results from 'list'.
525 while ((dir
= FcStrListNext (list
)))
528 file
= (FcChar8
*) malloc (strlen ((char *) dir
) + 1 + FC_MAX_FILE_LEN
+ 1);
532 strcpy ((char *) file
, (char *) dir
);
533 strcat ((char *) file
, "/");
534 base
= file
+ strlen ((char *) file
);
536 subdirs
= FcStrSetCreate ();
539 fprintf (stderr
, "Can't create directory set\n");
545 if (access ((char *) dir
, X_OK
) < 0)
553 fprintf (stderr
, "\"%s\": ", dir
);
557 FcStrSetDestroy (subdirs
);
561 if (stat ((char *) dir
, &statb
) == -1)
563 fprintf (stderr
, "\"%s\": ", dir
);
565 FcStrSetDestroy (subdirs
);
570 if (!S_ISDIR (statb
.st_mode
))
572 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
573 FcStrSetDestroy (subdirs
);
577 if (!FcDirCacheValid (dir
) || !FcDirCacheRead (set
, subdirs
, dir
))
579 if (FcDebug () & FC_DBG_FONTSET
)
580 printf ("cache scan dir %s\n", dir
);
582 FcDirScanConfig (set
, subdirs
, cache
,
583 config
->blanks
, dir
, FcFalse
, config
);
585 sublist
= FcStrListCreate (subdirs
);
586 FcStrSetDestroy (subdirs
);
589 fprintf (stderr
, "Can't create subdir list in \"%s\"\n", dir
);
594 ret
+= FcCacheReadDirs (config
, cache
, sublist
, set
);
597 FcStrListDone (list
);
602 FcCacheRead (FcConfig
*config
, FcGlobalCache
* cache
)
604 FcFontSet
* s
= FcFontSetCreate();
608 if (FcCacheReadDirs (config
, cache
, FcConfigGetConfigDirs (config
), s
))
614 FcFontSetDestroy (s
);
618 /* read serialized state from the cache file */
620 FcDirCacheRead (FcFontSet
* set
, FcStrSet
* dirs
, const FcChar8
*dir
)
622 char *cache_file
= (char *)FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
624 char * current_arch_machine_name
;
625 char candidate_arch_machine_name
[9+MACHINE_SIGNATURE_SIZE
];
626 off_t current_arch_start
= 0;
627 char subdirName
[FC_MAX_FILE_LEN
+ 1 + 12 + 1];
632 current_arch_machine_name
= FcCacheMachineSignature();
633 fd
= open(cache_file
, O_RDONLY
);
637 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
638 if (current_arch_start
< 0)
641 lseek (fd
, current_arch_start
, SEEK_SET
);
642 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
643 sizeof (candidate_arch_machine_name
)) == 0)
646 while (strlen(FcCacheReadString (fd
, subdirName
, sizeof (subdirName
))) > 0)
647 FcStrSetAdd (dirs
, (FcChar8
*)subdirName
);
649 if (!FcDirCacheConsume (fd
, set
))
664 FcDirCacheConsume (int fd
, FcFontSet
*set
)
667 void * current_dir_block
;
670 read(fd
, &metadata
, sizeof(FcCache
));
671 if (metadata
.magic
!= FC_CACHE_MAGIC
)
677 pos
= FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
));
678 current_dir_block
= mmap (0, metadata
.count
,
679 PROT_READ
, MAP_SHARED
, fd
, pos
);
680 if (current_dir_block
== MAP_FAILED
)
683 if (!FcFontSetUnserialize (metadata
, set
, current_dir_block
))
690 FcDirCacheProduce (FcFontSet
*set
, FcCache
*metadata
)
692 void * current_dir_block
, * final_dir_block
;
693 static unsigned int rand_state
= 0;
697 rand_state
= time(0L);
698 bank
= rand_r(&rand_state
);
700 while (FcCacheHaveBank(bank
))
701 bank
= rand_r(&rand_state
);
703 memset (metadata
, 0, sizeof(FcCache
));
705 metadata
->count
= FcFontSetNeededBytes (set
);
706 metadata
->magic
= FC_CACHE_MAGIC
;
707 metadata
->bank
= bank
;
709 if (!metadata
->count
) /* not a failure, no fonts to write */
712 current_dir_block
= malloc (metadata
->count
);
713 if (!current_dir_block
)
715 final_dir_block
= FcFontSetDistributeBytes (metadata
, current_dir_block
);
717 if ((char *)current_dir_block
+ metadata
->count
!= final_dir_block
)
720 if (!FcFontSetSerialize (bank
, set
))
723 return current_dir_block
;
726 free (current_dir_block
);
730 /* write serialized state to the cache file */
732 FcDirCacheWrite (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
)
734 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
735 int fd
, i
, dirs_count
;
737 off_t current_arch_start
= 0, truncate_to
;
739 char * current_arch_machine_name
, * header
;
740 void * current_dir_block
;
745 current_dir_block
= FcDirCacheProduce (set
, &metadata
);
747 if (!metadata
.count
&& !dirs
->size
)
749 unlink ((char *)cache_file
);
754 if (metadata
.count
&& !current_dir_block
)
757 if (FcDebug () & FC_DBG_CACHE
)
758 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file
);
760 fd
= open((char *)cache_file
, O_RDWR
| O_CREAT
, 0666);
764 current_arch_machine_name
= FcCacheMachineSignature ();
765 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
766 if (current_arch_start
< 0)
767 current_arch_start
= FcCacheNextOffset (lseek(fd
, 0, SEEK_END
));
769 if (!FcCacheMoveDown(fd
, current_arch_start
))
772 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
773 if (ftruncate (fd
, current_arch_start
) == -1)
776 /* allocate space for subdir names in this block */
778 for (i
= 0; i
< dirs
->size
; i
++)
779 dirs_count
+= strlen((char *)dirs
->strs
[i
]) + 1;
782 /* now write the address of the next offset */
783 truncate_to
= FcCacheNextOffset (FcCacheNextOffset (current_arch_start
+ sizeof (FcCache
) + dirs_count
) + metadata
.count
) - current_arch_start
;
784 header
= malloc (10 + strlen (current_arch_machine_name
));
787 sprintf (header
, "%8x ", (int)truncate_to
);
788 strcat (header
, current_arch_machine_name
);
789 if (!FcCacheWriteString (fd
, header
))
792 for (i
= 0; i
< dirs
->size
; i
++)
793 FcCacheWriteString (fd
, (char *)dirs
->strs
[i
]);
794 FcCacheWriteString (fd
, "");
796 write (fd
, &metadata
, sizeof(FcCache
));
799 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_END
)), SEEK_SET
);
800 write (fd
, current_dir_block
, metadata
.count
);
801 free (current_dir_block
);
804 /* this actually serves to pad out the cache file, if needed */
805 if (ftruncate (fd
, current_arch_start
+ truncate_to
) == -1)
814 free (current_dir_block
);
816 unlink ((char *)cache_file
);
822 FcCacheMachineSignature ()
824 static char buf
[MACHINE_SIGNATURE_SIZE
];
825 int magic
= ENDIAN_TEST
;
826 char * m
= (char *)&magic
;
828 sprintf (buf
, "%2x%2x%2x%2x "
829 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
830 "%4x %4x %4x %4x %4x %4x %4x\n",
831 m
[0], m
[1], m
[2], m
[3],
836 sizeof (FcPatternEltPtr
),
837 sizeof (struct _FcPatternElt
*),
838 sizeof (FcPatternElt
),
839 sizeof (FcObjectPtr
),
840 sizeof (FcValueListPtr
),
842 sizeof (FcValueBinding
),
843 sizeof (struct _FcValueList
*),
845 sizeof (FcCharLeaf
**),
855 static int banks_ptr
= 0, banks_alloc
= 0;
856 static int * bankId
= 0;
859 FcCacheHaveBank (int bank
)
863 if (bank
< FC_BANK_FIRST
)
866 for (i
= 0; i
< banks_ptr
; i
++)
867 if (bankId
[i
] == bank
)
874 FcCacheBankToIndex (int bank
)
876 static int lastBank
= FC_BANK_DYNAMIC
, lastIndex
= -1;
880 if (bank
== lastBank
)
883 for (i
= 0; i
< banks_ptr
; i
++)
884 if (bankId
[i
] == bank
)
887 if (banks_ptr
>= banks_alloc
)
889 b
= realloc (bankId
, (banks_alloc
+ 4) * sizeof(int));