2 * $RCSId: xc/lib/fontconfig/src/fccache.c,v 1.12 2002/08/22 07:36:44 keithp Exp $
4 * Copyright © 2000 Keith Packard
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
28 #include <sys/utsname.h>
29 #include <sys/types.h>
33 #define ENDIAN_TEST 0x12345678
34 #define MACHINE_SIGNATURE_SIZE 9 + 5*19 + 1
37 FcCacheSkipToArch (int fd
, const char * arch
);
40 FcCacheMoveDown (int fd
, off_t start
);
43 FcDirCacheProduce (FcFontSet
*set
, FcCache
* metadata
);
46 FcDirCacheConsume (int fd
, FcFontSet
*set
);
49 FcDirCacheRead (FcFontSet
* set
, const FcChar8
*dir
);
52 FcCacheNextOffset(off_t w
);
55 FcCacheMachineSignature (void);
58 FcCacheHaveBank (int bank
);
60 #define FC_DBG_CACHE_REF 1024
63 FcCacheReadString (int fd
, FcChar8
*dest
, int len
)
76 while (read (fd
, &c
, 1) == 1)
103 FcCacheWriteString (int fd
, const FcChar8
*chars
)
105 if (write (fd
, chars
, strlen(chars
)+1) != strlen(chars
)+1)
111 FcGlobalCacheDirDestroy (FcGlobalCacheDir
*d
)
113 FcMemFree (FC_MEM_STRING
, strlen (d
->name
)+1);
115 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCacheDir
));
120 FcGlobalCacheCreate (void)
122 FcGlobalCache
*cache
;
124 cache
= malloc (sizeof (FcGlobalCache
));
127 FcMemAlloc (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
129 cache
->updated
= FcFalse
;
135 FcGlobalCacheDestroy (FcGlobalCache
*cache
)
137 FcGlobalCacheDir
*d
, *next
;
139 for (d
= cache
->dirs
; d
; d
= next
)
142 FcGlobalCacheDirDestroy (d
);
144 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
149 FcGlobalCacheLoad (FcGlobalCache
*cache
,
150 const FcChar8
*cache_file
)
152 FcChar8 name_buf
[8192];
153 FcGlobalCacheDir
*d
, *next
;
154 char * current_arch_machine_name
;
155 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
156 off_t current_arch_start
;
158 cache
->fd
= open ((char *) cache_file
, O_RDONLY
);
162 cache
->updated
= FcFalse
;
164 current_arch_machine_name
= FcCacheMachineSignature ();
165 current_arch_start
= FcCacheSkipToArch(cache
->fd
,
166 current_arch_machine_name
);
167 if (current_arch_start
< 0)
170 lseek (cache
->fd
, current_arch_start
, SEEK_SET
);
171 if (FcCacheReadString (cache
->fd
, candidate_arch_machine_name
,
172 sizeof (candidate_arch_machine_name
)) == 0)
177 FcCacheReadString (cache
->fd
, name_buf
, sizeof (name_buf
));
178 if (!strlen(name_buf
))
181 d
= malloc (sizeof (FcGlobalCacheDir
));
185 d
->next
= cache
->dirs
;
188 d
->name
= FcStrCopy (name_buf
);
190 d
->offset
= lseek (cache
->fd
, 0, SEEK_CUR
);
191 read (cache
->fd
, &d
->metadata
, sizeof (FcCache
));
192 lseek (cache
->fd
, d
->metadata
.count
, SEEK_CUR
);
197 for (d
= cache
->dirs
; d
; d
= next
)
204 free (current_arch_machine_name
);
211 FcGlobalCacheReadDir (FcFontSet
*set
, FcStrSet
*dirs
, FcGlobalCache
* cache
, const FcChar8
*dir
, FcConfig
*config
)
218 for (d
= cache
->dirs
; d
; d
= d
->next
)
220 if (strcmp (d
->name
, dir
) == 0)
222 lseek (cache
->fd
, d
->offset
, SEEK_SET
);
223 if (!FcDirCacheConsume (cache
->fd
, set
))
233 FcGlobalCacheUpdate (FcGlobalCache
*cache
,
237 FcGlobalCacheDir
* d
;
242 for (d
= cache
->dirs
; d
; d
= d
->next
)
244 if (strcmp(d
->name
, name
) == 0)
250 d
= malloc (sizeof (FcGlobalCacheDir
));
253 d
->next
= cache
->dirs
;
257 cache
->updated
= FcTrue
;
259 d
->name
= FcStrCopy (name
);
260 d
->ent
= FcDirCacheProduce (set
, &d
->metadata
);
266 FcGlobalCacheSave (FcGlobalCache
*cache
,
267 const FcChar8
*cache_file
)
270 FcGlobalCacheDir
*dir
;
272 off_t current_arch_start
= 0, truncate_to
;
273 char * current_arch_machine_name
, * header
;
278 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
279 /* Set-UID programs can't safely update the cache */
280 if (getuid () != geteuid ())
284 atomic
= FcAtomicCreate (cache_file
);
287 if (!FcAtomicLock (atomic
))
289 fd
= open ((char *) FcAtomicNewFile(atomic
), O_RDWR
| O_CREAT
,
294 current_arch_machine_name
= FcCacheMachineSignature ();
295 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
296 if (current_arch_start
< 0)
297 current_arch_start
= FcCacheNextOffset (lseek(fd
, 0, SEEK_END
));
299 if (!FcCacheMoveDown(fd
, current_arch_start
))
302 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
303 if (ftruncate (fd
, current_arch_start
) == -1)
306 truncate_to
= current_arch_start
;
307 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
309 truncate_to
+= strlen(dir
->name
) + 1;
310 truncate_to
+= sizeof (FcCache
);
311 truncate_to
= FcCacheNextOffset (current_arch_start
+ truncate_to
);
312 truncate_to
+= dir
->metadata
.count
;
314 truncate_to
-= current_arch_start
;
315 header
= malloc (10 + strlen (current_arch_machine_name
));
318 sprintf (header
, "%8x ", (int)truncate_to
);
319 strcat (header
, current_arch_machine_name
);
320 if (!FcCacheWriteString (fd
, header
))
323 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
327 FcCacheWriteString (fd
, dir
->name
);
328 write (fd
, &dir
->metadata
, sizeof(FcCache
));
329 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_END
)), SEEK_SET
);
330 write (fd
, dir
->ent
, dir
->metadata
.count
);
334 FcCacheWriteString (fd
, "");
336 if (close (fd
) == -1)
339 if (!FcAtomicReplaceOrig (atomic
))
342 FcAtomicUnlock (atomic
);
343 FcAtomicDestroy (atomic
);
345 cache
->updated
= FcFalse
;
349 FcAtomicDeleteNew (atomic
);
351 FcAtomicUnlock (atomic
);
353 FcAtomicDestroy (atomic
);
358 #define PAGESIZE 8192
360 * Find the next presumably-mmapable offset after the supplied file
364 FcCacheNextOffset(off_t w
)
366 if (w
% PAGESIZE
== 0)
369 return ((w
/ PAGESIZE
)+1)*PAGESIZE
;
372 /* return the address of the segment for the provided arch,
373 * or -1 if arch not found */
375 FcCacheSkipToArch (int fd
, const char * arch
)
377 char candidate_arch_machine_name_count
[MACHINE_SIGNATURE_SIZE
+ 9];
378 char * candidate_arch
;
379 off_t current_arch_start
= 0;
381 /* skip arches that are not the current arch */
386 lseek (fd
, current_arch_start
, SEEK_SET
);
387 if (FcCacheReadString (fd
, candidate_arch_machine_name_count
,
388 sizeof (candidate_arch_machine_name_count
)) == 0)
390 if (!strlen(candidate_arch_machine_name_count
))
392 bs
= strtol(candidate_arch_machine_name_count
, &candidate_arch
, 16);
393 candidate_arch
++; /* skip leading space */
395 if (strcmp (candidate_arch
, arch
)==0)
396 return current_arch_start
;
397 current_arch_start
+= bs
;
403 /* Cuts out the segment at the file pointer (moves everything else
404 * down to cover it), and leaves the file pointer at the end of the
407 FcCacheMoveDown (int fd
, off_t start
)
409 char * buf
= malloc (8192);
410 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
412 int c
, bytes_skipped
;
417 lseek (fd
, start
, SEEK_SET
);
418 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
419 sizeof (candidate_arch_machine_name
)) == 0)
421 if (!strlen(candidate_arch_machine_name
))
424 bs
= strtol(candidate_arch_machine_name
, 0, 16);
431 lseek (fd
, start
+bs
+bytes_skipped
, SEEK_SET
);
432 if ((c
= read (fd
, buf
, 8192)) <= 0)
434 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
435 if (write (fd
, buf
, c
) < 0)
440 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
452 FcDirCacheValid (const FcChar8
*dir
)
454 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
455 struct stat file_stat
, dir_stat
;
457 if (stat ((char *) dir
, &dir_stat
) < 0)
459 FcStrFree (cache_file
);
462 if (stat ((char *) cache_file
, &file_stat
) < 0)
464 FcStrFree (cache_file
);
467 FcStrFree (cache_file
);
469 * If the directory has been modified more recently than
470 * the cache file, the cache is not valid
472 if (dir_stat
.st_mtime
- file_stat
.st_mtime
> 0)
478 FcCacheReadDirs (FcConfig
* config
, FcGlobalCache
* cache
,
479 FcStrList
*list
, FcFontSet
* set
)
485 FcChar8
*file
, *base
;
491 * Now scan all of the directories into separate databases
492 * and write out the results
494 while ((dir
= FcStrListNext (list
)))
497 file
= (FcChar8
*) malloc (strlen ((char *) dir
) + 1 + FC_MAX_FILE_LEN
+ 1);
501 strcpy ((char *) file
, (char *) dir
);
502 strcat ((char *) file
, "/");
503 base
= file
+ strlen ((char *) file
);
505 subdirs
= FcStrSetCreate ();
508 fprintf (stderr
, "Can't create directory set\n");
514 if (access ((char *) dir
, X_OK
) < 0)
522 fprintf (stderr
, "\"%s\": ", dir
);
526 FcStrSetDestroy (subdirs
);
530 if (stat ((char *) dir
, &statb
) == -1)
532 fprintf (stderr
, "\"%s\": ", dir
);
534 FcStrSetDestroy (subdirs
);
539 if (!S_ISDIR (statb
.st_mode
))
541 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
542 FcStrSetDestroy (subdirs
);
546 d
= opendir ((char *) dir
);
549 FcStrSetDestroy (subdirs
);
553 while ((e
= readdir (d
)))
555 if (e
->d_name
[0] != '.' && strlen (e
->d_name
) < FC_MAX_FILE_LEN
)
557 strcpy ((char *) base
, (char *) e
->d_name
);
558 if (FcFileIsDir (file
) && !FcStrSetAdd (subdirs
, file
))
563 if (!FcDirCacheValid (dir
) || !FcDirCacheRead (set
, dir
))
565 if (FcDebug () & FC_DBG_FONTSET
)
566 printf ("scan dir %s\n", dir
);
567 FcDirScanConfig (set
, subdirs
, cache
,
568 config
->blanks
, dir
, FcFalse
, config
);
570 sublist
= FcStrListCreate (subdirs
);
571 FcStrSetDestroy (subdirs
);
574 fprintf (stderr
, "Can't create subdir list in \"%s\"\n", dir
);
579 ret
+= FcCacheReadDirs (config
, cache
, sublist
, set
);
582 FcStrListDone (list
);
587 FcCacheRead (FcConfig
*config
, FcGlobalCache
* cache
)
589 FcFontSet
* s
= FcFontSetCreate();
593 if (FcCacheReadDirs (config
, cache
, FcConfigGetConfigDirs (config
), s
))
599 FcFontSetDestroy (s
);
603 /* read serialized state from the cache file */
605 FcDirCacheRead (FcFontSet
* set
, const FcChar8
*dir
)
607 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
609 char * current_arch_machine_name
;
610 char candidate_arch_machine_name
[9+MACHINE_SIGNATURE_SIZE
];
611 off_t current_arch_start
= 0;
616 current_arch_machine_name
= FcCacheMachineSignature();
617 fd
= open(cache_file
, O_RDONLY
);
621 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
622 if (current_arch_start
< 0)
625 lseek (fd
, current_arch_start
, SEEK_SET
);
626 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
627 sizeof (candidate_arch_machine_name
)) == 0)
630 if (!FcDirCacheConsume (fd
, set
))
645 FcDirCacheConsume (int fd
, FcFontSet
*set
)
648 void * current_dir_block
;
651 read(fd
, &metadata
, sizeof(FcCache
));
652 if (metadata
.magic
!= FC_CACHE_MAGIC
)
658 pos
= FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
));
659 current_dir_block
= mmap (0, metadata
.count
,
660 PROT_READ
, MAP_SHARED
, fd
, pos
);
661 if (current_dir_block
== MAP_FAILED
)
664 if (!FcFontSetUnserialize (metadata
, set
, current_dir_block
))
671 FcDirCacheProduce (FcFontSet
*set
, FcCache
*metadata
)
673 void * current_dir_block
, * final_dir_block
;
674 static int rand_state
= 0;
678 rand_state
= time(0L);
679 bank
= rand_r(&rand_state
);
681 while (FcCacheHaveBank(bank
))
682 bank
= rand_r(&rand_state
);
684 memset (metadata
, 0, sizeof(FcCache
));
686 metadata
->count
= FcFontSetNeededBytes (set
);
687 metadata
->magic
= FC_CACHE_MAGIC
;
688 metadata
->bank
= bank
;
690 if (!metadata
->count
) /* not a failure, no fonts to write */
693 current_dir_block
= malloc (metadata
->count
);
694 if (!current_dir_block
)
696 final_dir_block
= FcFontSetDistributeBytes (metadata
, current_dir_block
);
698 if ((char *)current_dir_block
+ metadata
->count
!= final_dir_block
)
701 if (!FcFontSetSerialize (bank
, set
))
704 return current_dir_block
;
707 free (current_dir_block
);
711 /* write serialized state to the cache file */
713 FcDirCacheWrite (FcFontSet
*set
, const FcChar8
*dir
)
715 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
718 off_t current_arch_start
= 0, truncate_to
;
719 char * current_arch_machine_name
, * header
;
720 void * current_dir_block
;
725 current_dir_block
= FcDirCacheProduce (set
, &metadata
);
734 if (!current_dir_block
)
737 if (FcDebug () & FC_DBG_CACHE
)
738 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file
);
740 fd
= open(cache_file
, O_RDWR
| O_CREAT
, 0666);
744 current_arch_machine_name
= FcCacheMachineSignature ();
745 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
746 if (current_arch_start
< 0)
747 current_arch_start
= FcCacheNextOffset (lseek(fd
, 0, SEEK_END
));
749 if (!FcCacheMoveDown(fd
, current_arch_start
))
752 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
753 if (ftruncate (fd
, current_arch_start
) == -1)
756 /* now write the address of the next offset */
757 truncate_to
= FcCacheNextOffset (FcCacheNextOffset (current_arch_start
+ sizeof (FcCache
)) + metadata
.count
) - current_arch_start
;
758 header
= malloc (10 + strlen (current_arch_machine_name
));
761 sprintf (header
, "%8x ", (int)truncate_to
);
762 strcat (header
, current_arch_machine_name
);
763 if (!FcCacheWriteString (fd
, header
))
766 write (fd
, &metadata
, sizeof(FcCache
));
767 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_END
)), SEEK_SET
);
768 write (fd
, current_dir_block
, metadata
.count
);
769 free (current_dir_block
);
771 /* this actually serves to pad out the cache file, if needed */
772 if (ftruncate (fd
, current_arch_start
+ truncate_to
) == -1)
781 free (current_arch_machine_name
);
783 free (current_dir_block
);
791 FcCacheMachineSignature ()
793 static char buf
[MACHINE_SIGNATURE_SIZE
];
794 int magic
= ENDIAN_TEST
;
795 char * m
= (char *)&magic
;
797 sprintf (buf
, "%2x%2x%2x%2x "
798 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
799 "%4x %4x %4x %4x %4x %4x %4x\n",
800 m
[0], m
[1], m
[2], m
[3],
805 sizeof (FcPatternEltPtr
),
806 sizeof (struct _FcPatternElt
*),
807 sizeof (FcPatternElt
),
808 sizeof (FcObjectPtr
),
809 sizeof (FcValueListPtr
),
811 sizeof (FcValueBinding
),
812 sizeof (struct _FcValueList
*),
814 sizeof (FcCharLeaf
**),
824 static int banks_ptr
= 0, banks_alloc
= 0;
825 static int * bankId
= 0;
828 FcCacheHaveBank (int bank
)
832 if (bank
< FC_BANK_FIRST
)
835 for (i
= 0; i
< banks_ptr
; i
++)
836 if (bankId
[i
] == bank
)
843 FcCacheBankToIndex (int bank
)
845 static int lastBank
= FC_BANK_DYNAMIC
, lastIndex
= -1;
849 if (bank
== lastBank
)
852 for (i
= 0; i
< banks_ptr
; i
++)
853 if (bankId
[i
] == bank
)
856 if (banks_ptr
>= banks_alloc
)
858 b
= realloc (bankId
, (banks_alloc
+ 4) * sizeof(int));