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>
31 #define ENDIAN_TEST 0x12345678
32 #define MACHINE_SIGNATURE_SIZE 9 + 5*19 + 1
35 FcCacheSkipToArch (int fd
, const char * arch
);
38 FcCacheMoveDown (int fd
, off_t start
);
41 FcDirCacheProduce (FcFontSet
*set
, FcCache
* metadata
);
44 FcDirCacheConsume (int fd
, FcFontSet
*set
);
47 FcDirCacheRead (FcFontSet
* set
, const FcChar8
*dir
);
50 FcCacheNextOffset(off_t w
);
53 FcCacheMachineSignature (void);
56 FcCacheHaveBank (int bank
);
58 #define FC_DBG_CACHE_REF 1024
61 FcCacheReadString (int fd
, FcChar8
*dest
, int len
)
74 while (read (fd
, &c
, 1) == 1)
101 FcCacheWriteString (int fd
, const FcChar8
*chars
)
103 if (write (fd
, chars
, strlen(chars
)+1) != strlen(chars
)+1)
109 FcGlobalCacheDirDestroy (FcGlobalCacheDir
*d
)
111 FcGlobalCacheDir
*dd
, *next
;
113 for (dd
= d
; dd
; dd
= next
)
116 FcMemFree (FC_MEM_STRING
, strlen (dd
->name
)+1);
118 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCacheDir
));
124 FcGlobalCacheCreate (void)
126 FcGlobalCache
*cache
;
128 cache
= malloc (sizeof (FcGlobalCache
));
131 FcMemAlloc (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
133 cache
->updated
= FcFalse
;
139 FcGlobalCacheDestroy (FcGlobalCache
*cache
)
141 FcGlobalCacheDir
*d
, *next
;
143 for (d
= cache
->dirs
; d
; d
= next
)
146 FcGlobalCacheDirDestroy (d
);
148 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
153 FcGlobalCacheLoad (FcGlobalCache
*cache
,
154 const FcChar8
*cache_file
)
156 FcChar8 name_buf
[8192];
157 FcGlobalCacheDir
*d
, *next
;
158 char * current_arch_machine_name
;
159 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
160 off_t current_arch_start
;
162 cache
->fd
= open ((char *) cache_file
, O_RDONLY
);
166 cache
->updated
= FcFalse
;
168 current_arch_machine_name
= FcCacheMachineSignature ();
169 current_arch_start
= FcCacheSkipToArch(cache
->fd
,
170 current_arch_machine_name
);
171 if (current_arch_start
< 0)
174 lseek (cache
->fd
, current_arch_start
, SEEK_SET
);
175 if (FcCacheReadString (cache
->fd
, candidate_arch_machine_name
,
176 sizeof (candidate_arch_machine_name
)) == 0)
181 FcCacheReadString (cache
->fd
, name_buf
, sizeof (name_buf
));
182 if (!strlen(name_buf
))
185 d
= malloc (sizeof (FcGlobalCacheDir
));
189 d
->next
= cache
->dirs
;
192 d
->name
= FcStrCopy (name_buf
);
194 d
->offset
= lseek (cache
->fd
, 0, SEEK_CUR
);
195 read (cache
->fd
, &d
->metadata
, sizeof (FcCache
));
196 lseek (cache
->fd
, d
->metadata
.count
, SEEK_CUR
);
201 for (d
= cache
->dirs
; d
; d
= next
)
208 free (current_arch_machine_name
);
215 FcGlobalCacheReadDir (FcFontSet
*set
, FcStrSet
*dirs
, FcGlobalCache
* cache
, const FcChar8
*dir
, FcConfig
*config
)
222 for (d
= cache
->dirs
; d
; d
= d
->next
)
224 if (strcmp (d
->name
, dir
) == 0)
226 lseek (cache
->fd
, d
->offset
, SEEK_SET
);
227 if (!FcDirCacheConsume (cache
->fd
, set
))
237 FcGlobalCacheUpdate (FcGlobalCache
*cache
,
241 FcGlobalCacheDir
* d
;
246 for (d
= cache
->dirs
; d
; d
= d
->next
)
248 if (strcmp(d
->name
, name
) == 0)
254 d
= malloc (sizeof (FcGlobalCacheDir
));
257 d
->next
= cache
->dirs
;
261 cache
->updated
= FcTrue
;
263 d
->name
= FcStrCopy (name
);
264 d
->ent
= FcDirCacheProduce (set
, &d
->metadata
);
270 FcGlobalCacheSave (FcGlobalCache
*cache
,
271 const FcChar8
*cache_file
)
274 FcGlobalCacheDir
*dir
;
276 off_t current_arch_start
= 0, truncate_to
;
277 char * current_arch_machine_name
, * header
;
282 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
283 /* Set-UID programs can't safely update the cache */
284 if (getuid () != geteuid ())
288 atomic
= FcAtomicCreate (cache_file
);
291 if (!FcAtomicLock (atomic
))
293 fd
= open ((char *) FcAtomicNewFile(atomic
), O_RDWR
| O_CREAT
,
298 current_arch_machine_name
= FcCacheMachineSignature ();
299 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
300 if (current_arch_start
< 0)
301 current_arch_start
= FcCacheNextOffset (lseek(fd
, 0, SEEK_END
));
303 if (!FcCacheMoveDown(fd
, current_arch_start
))
306 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
307 if (ftruncate (fd
, current_arch_start
) == -1)
310 truncate_to
= current_arch_start
;
311 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
313 truncate_to
+= strlen(dir
->name
) + 1;
314 truncate_to
+= sizeof (FcCache
);
315 truncate_to
= FcCacheNextOffset (current_arch_start
+ truncate_to
);
316 truncate_to
+= dir
->metadata
.count
;
318 truncate_to
-= current_arch_start
;
319 header
= malloc (10 + strlen (current_arch_machine_name
));
322 sprintf (header
, "%8x ", (int)truncate_to
);
323 strcat (header
, current_arch_machine_name
);
324 if (!FcCacheWriteString (fd
, header
))
327 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
329 FcCacheWriteString (fd
, dir
->name
);
330 write (fd
, &dir
->metadata
, sizeof(FcCache
));
331 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_END
)), SEEK_SET
);
332 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)
397 current_arch_start
+= bs
;
400 if (candidate_arch
&& strcmp (candidate_arch
, arch
)!=0)
403 return current_arch_start
;
406 /* Cuts out the segment at the file pointer (moves everything else
407 * down to cover it), and leaves the file pointer at the end of the
410 FcCacheMoveDown (int fd
, off_t start
)
412 char * buf
= malloc (8192);
413 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
415 int c
, bytes_skipped
;
420 lseek (fd
, start
, SEEK_SET
);
421 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
422 sizeof (candidate_arch_machine_name
)) == 0)
424 if (!strlen(candidate_arch_machine_name
))
427 bs
= strtol(candidate_arch_machine_name
, 0, 16);
434 lseek (fd
, start
+bs
+bytes_skipped
, SEEK_SET
);
435 if ((c
= read (fd
, buf
, 8192)) <= 0)
437 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
438 if (write (fd
, buf
, c
) < 0)
443 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
455 FcDirCacheValid (const FcChar8
*dir
)
457 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
458 struct stat file_stat
, dir_stat
;
460 if (stat ((char *) dir
, &dir_stat
) < 0)
462 FcStrFree (cache_file
);
465 if (stat ((char *) cache_file
, &file_stat
) < 0)
467 FcStrFree (cache_file
);
470 FcStrFree (cache_file
);
472 * If the directory has been modified more recently than
473 * the cache file, the cache is not valid
475 if (dir_stat
.st_mtime
- file_stat
.st_mtime
> 0)
481 FcCacheReadDirs (FcConfig
* config
, FcGlobalCache
* cache
,
482 FcStrList
*list
, FcFontSet
* set
)
488 FcChar8
*file
, *base
;
494 * Now scan all of the directories into separate databases
495 * and write out the results
497 while ((dir
= FcStrListNext (list
)))
500 file
= (FcChar8
*) malloc (strlen ((char *) dir
) + 1 + FC_MAX_FILE_LEN
+ 1);
504 strcpy ((char *) file
, (char *) dir
);
505 strcat ((char *) file
, "/");
506 base
= file
+ strlen ((char *) file
);
508 subdirs
= FcStrSetCreate ();
511 fprintf (stderr
, "Can't create directory set\n");
517 if (access ((char *) dir
, X_OK
) < 0)
525 fprintf (stderr
, "\"%s\": ", dir
);
529 FcStrSetDestroy (subdirs
);
533 if (stat ((char *) dir
, &statb
) == -1)
535 fprintf (stderr
, "\"%s\": ", dir
);
537 FcStrSetDestroy (subdirs
);
542 if (!S_ISDIR (statb
.st_mode
))
544 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
545 FcStrSetDestroy (subdirs
);
549 d
= opendir ((char *) dir
);
552 FcStrSetDestroy (subdirs
);
556 while ((e
= readdir (d
)))
558 if (e
->d_name
[0] != '.' && strlen (e
->d_name
) < FC_MAX_FILE_LEN
)
560 strcpy ((char *) base
, (char *) e
->d_name
);
561 if (FcFileIsDir (file
) && !FcStrSetAdd (subdirs
, file
))
566 if (!FcDirCacheValid (dir
) || !FcDirCacheRead (set
, dir
))
568 if (FcDebug () & FC_DBG_FONTSET
)
569 printf ("scan dir %s\n", dir
);
570 FcDirScanConfig (set
, subdirs
, cache
,
571 config
->blanks
, dir
, FcFalse
, config
);
573 sublist
= FcStrListCreate (subdirs
);
574 FcStrSetDestroy (subdirs
);
577 fprintf (stderr
, "Can't create subdir list in \"%s\"\n", dir
);
582 ret
+= FcCacheReadDirs (config
, cache
, sublist
, set
);
585 FcStrListDone (list
);
590 FcCacheRead (FcConfig
*config
, FcGlobalCache
* cache
)
592 FcFontSet
* s
= FcFontSetCreate();
596 if (FcCacheReadDirs (config
, cache
, FcConfigGetConfigDirs (config
), s
))
602 FcFontSetDestroy (s
);
606 /* read serialized state from the cache file */
608 FcDirCacheRead (FcFontSet
* set
, const FcChar8
*dir
)
610 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
612 char * current_arch_machine_name
;
613 char candidate_arch_machine_name
[9+MACHINE_SIGNATURE_SIZE
];
614 off_t current_arch_start
= 0;
619 current_arch_machine_name
= FcCacheMachineSignature();
620 fd
= open(cache_file
, O_RDONLY
);
624 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
625 if (current_arch_start
< 0)
628 lseek (fd
, current_arch_start
, SEEK_SET
);
629 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
630 sizeof (candidate_arch_machine_name
)) == 0)
633 if (!FcDirCacheConsume (fd
, set
))
648 FcDirCacheConsume (int fd
, FcFontSet
*set
)
651 void * current_dir_block
;
653 read(fd
, &metadata
, sizeof(FcCache
));
654 if (metadata
.magic
!= FC_CACHE_MAGIC
)
660 off_t pos
= FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
));
661 current_dir_block
= mmap (0, metadata
.count
,
662 PROT_READ
, MAP_SHARED
, fd
, pos
);
663 if (current_dir_block
== MAP_FAILED
)
666 if (!FcFontSetUnserialize (metadata
, set
, current_dir_block
))
673 FcDirCacheProduce (FcFontSet
*set
, FcCache
*metadata
)
675 void * current_dir_block
, * final_dir_block
;
676 static int rand_state
= 0;
680 rand_state
= time(0L);
681 bank
= rand_r(&rand_state
);
683 while (FcCacheHaveBank(bank
))
684 bank
= rand_r(&rand_state
);
686 memset (metadata
, 0, sizeof(FcCache
));
688 metadata
->count
= FcFontSetNeededBytes (set
);
689 metadata
->magic
= FC_CACHE_MAGIC
;
690 metadata
->bank
= bank
;
692 if (!metadata
->count
) /* not a failure, no fonts to write */
695 current_dir_block
= malloc (metadata
->count
);
696 if (!current_dir_block
)
698 final_dir_block
= FcFontSetDistributeBytes (metadata
, current_dir_block
);
700 if ((char *)current_dir_block
+ metadata
->count
!= final_dir_block
)
703 if (!FcFontSetSerialize (bank
, set
))
706 return current_dir_block
;
709 free (current_dir_block
);
713 /* write serialized state to the cache file */
715 FcDirCacheWrite (FcFontSet
*set
, const FcChar8
*dir
)
717 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
720 off_t current_arch_start
= 0, truncate_to
;
721 char * current_arch_machine_name
, * header
;
722 void * current_dir_block
;
727 current_dir_block
= FcDirCacheProduce (set
, &metadata
);
736 if (!current_dir_block
)
739 if (FcDebug () & FC_DBG_CACHE
)
740 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file
);
742 fd
= open(cache_file
, O_RDWR
| O_CREAT
, 0666);
746 current_arch_machine_name
= FcCacheMachineSignature ();
747 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
748 if (current_arch_start
< 0)
749 current_arch_start
= FcCacheNextOffset (lseek(fd
, 0, SEEK_END
));
751 if (!FcCacheMoveDown(fd
, current_arch_start
))
754 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
755 if (ftruncate (fd
, current_arch_start
) == -1)
758 /* now write the address of the next offset */
759 truncate_to
= FcCacheNextOffset (FcCacheNextOffset (current_arch_start
+ sizeof (FcCache
)) + metadata
.count
) - current_arch_start
;
760 header
= malloc (10 + strlen (current_arch_machine_name
));
763 sprintf (header
, "%8x ", (int)truncate_to
);
764 strcat (header
, current_arch_machine_name
);
765 if (!FcCacheWriteString (fd
, header
))
768 write (fd
, &metadata
, sizeof(FcCache
));
769 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_END
)), SEEK_SET
);
770 write (fd
, current_dir_block
, metadata
.count
);
772 /* this actually serves to pad out the cache file, if needed */
773 if (ftruncate (fd
, current_arch_start
+ truncate_to
) == -1)
782 free (current_arch_machine_name
);
784 free (current_dir_block
);
792 FcCacheMachineSignature ()
794 static char buf
[MACHINE_SIGNATURE_SIZE
];
795 int magic
= ENDIAN_TEST
;
796 char * m
= (char *)&magic
;
798 sprintf (buf
, "%2x%2x%2x%2x "
799 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
800 "%4x %4x %4x %4x %4x %4x %4x\n",
801 m
[0], m
[1], m
[2], m
[3],
806 sizeof (FcPatternEltPtr
),
807 sizeof (struct _FcPatternElt
*),
808 sizeof (FcPatternElt
),
809 sizeof (FcObjectPtr
),
810 sizeof (FcValueListPtr
),
812 sizeof (FcValueBinding
),
813 sizeof (struct _FcValueList
*),
815 sizeof (FcCharLeaf
**),
825 static int banks_ptr
= 0, banks_alloc
= 0;
826 static int * bankId
= 0;
829 FcCacheHaveBank (int bank
)
833 if (bank
< FC_BANK_FIRST
)
836 for (i
= 0; i
< banks_ptr
; i
++)
837 if (bankId
[i
] == bank
)
844 FcCacheBankToIndex (int bank
)
846 static int lastBank
= FC_BANK_DYNAMIC
, lastIndex
= -1;
850 if (bank
== lastBank
)
853 for (i
= 0; i
< banks_ptr
; i
++)
854 if (bankId
[i
] == bank
)
857 if (banks_ptr
>= banks_alloc
)
859 b
= realloc (bankId
, (banks_alloc
+ 4) * sizeof(int));