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
, FcStrSet
* dirs
, 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
)
210 FcGlobalCacheReadDir (FcFontSet
*set
, FcStrSet
*dirs
, FcGlobalCache
* cache
, const FcChar8
*dir
, FcConfig
*config
)
217 for (d
= cache
->dirs
; d
; d
= d
->next
)
219 if (strcmp (d
->name
, dir
) == 0)
221 lseek (cache
->fd
, d
->offset
, SEEK_SET
);
222 if (!FcDirCacheConsume (cache
->fd
, set
))
232 FcGlobalCacheUpdate (FcGlobalCache
*cache
,
236 FcGlobalCacheDir
* d
;
241 for (d
= cache
->dirs
; d
; d
= d
->next
)
243 if (strcmp(d
->name
, name
) == 0)
249 d
= malloc (sizeof (FcGlobalCacheDir
));
252 d
->next
= cache
->dirs
;
256 cache
->updated
= FcTrue
;
258 d
->name
= FcStrCopy (name
);
259 d
->ent
= FcDirCacheProduce (set
, &d
->metadata
);
265 FcGlobalCacheSave (FcGlobalCache
*cache
,
266 const FcChar8
*cache_file
)
269 FcGlobalCacheDir
*dir
;
271 off_t current_arch_start
= 0, truncate_to
;
272 char * current_arch_machine_name
, * header
;
277 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
278 /* Set-UID programs can't safely update the cache */
279 if (getuid () != geteuid ())
283 atomic
= FcAtomicCreate (cache_file
);
286 if (!FcAtomicLock (atomic
))
288 fd
= open ((char *) FcAtomicNewFile(atomic
), O_RDWR
| O_CREAT
,
293 current_arch_machine_name
= FcCacheMachineSignature ();
294 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
295 if (current_arch_start
< 0)
296 current_arch_start
= FcCacheNextOffset (lseek(fd
, 0, SEEK_END
));
298 if (!FcCacheMoveDown(fd
, current_arch_start
))
301 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
302 if (ftruncate (fd
, current_arch_start
) == -1)
305 truncate_to
= current_arch_start
;
306 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
308 truncate_to
+= strlen(dir
->name
) + 1;
309 truncate_to
+= sizeof (FcCache
);
310 truncate_to
= FcCacheNextOffset (current_arch_start
+ truncate_to
);
311 truncate_to
+= dir
->metadata
.count
;
313 truncate_to
-= current_arch_start
;
314 header
= malloc (10 + strlen (current_arch_machine_name
));
317 sprintf (header
, "%8x ", (int)truncate_to
);
318 strcat (header
, current_arch_machine_name
);
319 if (!FcCacheWriteString (fd
, header
))
322 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
326 FcCacheWriteString (fd
, dir
->name
);
327 write (fd
, &dir
->metadata
, sizeof(FcCache
));
328 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_END
)), SEEK_SET
);
329 write (fd
, dir
->ent
, dir
->metadata
.count
);
333 FcCacheWriteString (fd
, "");
335 if (close (fd
) == -1)
338 if (!FcAtomicReplaceOrig (atomic
))
341 FcAtomicUnlock (atomic
);
342 FcAtomicDestroy (atomic
);
344 cache
->updated
= FcFalse
;
348 FcAtomicDeleteNew (atomic
);
350 FcAtomicUnlock (atomic
);
352 FcAtomicDestroy (atomic
);
357 #define PAGESIZE 8192
359 * Find the next presumably-mmapable offset after the supplied file
363 FcCacheNextOffset(off_t w
)
365 if (w
% PAGESIZE
== 0)
368 return ((w
/ PAGESIZE
)+1)*PAGESIZE
;
371 /* return the address of the segment for the provided arch,
372 * or -1 if arch not found */
374 FcCacheSkipToArch (int fd
, const char * arch
)
376 char candidate_arch_machine_name_count
[MACHINE_SIGNATURE_SIZE
+ 9];
377 char * candidate_arch
;
378 off_t current_arch_start
= 0;
380 /* skip arches that are not the current arch */
385 lseek (fd
, current_arch_start
, SEEK_SET
);
386 if (FcCacheReadString (fd
, candidate_arch_machine_name_count
,
387 sizeof (candidate_arch_machine_name_count
)) == 0)
389 if (!strlen(candidate_arch_machine_name_count
))
391 bs
= strtol(candidate_arch_machine_name_count
, &candidate_arch
, 16);
392 candidate_arch
++; /* skip leading space */
394 if (strcmp (candidate_arch
, arch
)==0)
395 return current_arch_start
;
396 current_arch_start
+= bs
;
402 /* Cuts out the segment at the file pointer (moves everything else
403 * down to cover it), and leaves the file pointer at the end of the
406 FcCacheMoveDown (int fd
, off_t start
)
408 char * buf
= malloc (8192);
409 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
411 int c
, bytes_skipped
;
416 lseek (fd
, start
, SEEK_SET
);
417 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
418 sizeof (candidate_arch_machine_name
)) == 0)
420 if (!strlen(candidate_arch_machine_name
))
423 bs
= strtol(candidate_arch_machine_name
, 0, 16);
430 lseek (fd
, start
+bs
+bytes_skipped
, SEEK_SET
);
431 if ((c
= read (fd
, buf
, 8192)) <= 0)
433 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
434 if (write (fd
, buf
, c
) < 0)
439 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
451 FcDirCacheValid (const FcChar8
*dir
)
453 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
454 struct stat file_stat
, dir_stat
;
456 if (stat ((char *) dir
, &dir_stat
) < 0)
458 FcStrFree (cache_file
);
461 if (stat ((char *) cache_file
, &file_stat
) < 0)
463 FcStrFree (cache_file
);
466 FcStrFree (cache_file
);
468 * If the directory has been modified more recently than
469 * the cache file, the cache is not valid
471 if (dir_stat
.st_mtime
- file_stat
.st_mtime
> 0)
477 FcCacheReadDirs (FcConfig
* config
, FcGlobalCache
* cache
,
478 FcStrList
*list
, FcFontSet
* set
)
484 FcChar8
*file
, *base
;
490 * Now scan all of the directories into separate databases
491 * and write out the results
493 while ((dir
= FcStrListNext (list
)))
496 file
= (FcChar8
*) malloc (strlen ((char *) dir
) + 1 + FC_MAX_FILE_LEN
+ 1);
500 strcpy ((char *) file
, (char *) dir
);
501 strcat ((char *) file
, "/");
502 base
= file
+ strlen ((char *) file
);
504 subdirs
= FcStrSetCreate ();
507 fprintf (stderr
, "Can't create directory set\n");
513 if (access ((char *) dir
, X_OK
) < 0)
521 fprintf (stderr
, "\"%s\": ", dir
);
525 FcStrSetDestroy (subdirs
);
529 if (stat ((char *) dir
, &statb
) == -1)
531 fprintf (stderr
, "\"%s\": ", dir
);
533 FcStrSetDestroy (subdirs
);
538 if (!S_ISDIR (statb
.st_mode
))
540 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
541 FcStrSetDestroy (subdirs
);
545 if (!FcDirCacheValid (dir
) || !FcDirCacheRead (set
, subdirs
, dir
))
547 if (FcDebug () & FC_DBG_FONTSET
)
548 printf ("scan dir %s\n", dir
);
550 FcDirScanConfig (set
, subdirs
, cache
,
551 config
->blanks
, dir
, FcFalse
, config
);
553 sublist
= FcStrListCreate (subdirs
);
554 FcStrSetDestroy (subdirs
);
557 fprintf (stderr
, "Can't create subdir list in \"%s\"\n", dir
);
562 ret
+= FcCacheReadDirs (config
, cache
, sublist
, set
);
565 FcStrListDone (list
);
570 FcCacheRead (FcConfig
*config
, FcGlobalCache
* cache
)
572 FcFontSet
* s
= FcFontSetCreate();
576 if (FcCacheReadDirs (config
, cache
, FcConfigGetConfigDirs (config
), s
))
582 FcFontSetDestroy (s
);
586 /* read serialized state from the cache file */
588 FcDirCacheRead (FcFontSet
* set
, FcStrSet
* dirs
, const FcChar8
*dir
)
590 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
592 char * current_arch_machine_name
;
593 char candidate_arch_machine_name
[9+MACHINE_SIGNATURE_SIZE
];
594 off_t current_arch_start
= 0;
595 FcChar8 subdirName
[FC_MAX_FILE_LEN
+ 1 + 12 + 1];
600 current_arch_machine_name
= FcCacheMachineSignature();
601 fd
= open(cache_file
, O_RDONLY
);
605 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
606 if (current_arch_start
< 0)
609 lseek (fd
, current_arch_start
, SEEK_SET
);
610 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
611 sizeof (candidate_arch_machine_name
)) == 0)
614 while (strlen(FcCacheReadString (fd
, subdirName
, sizeof (subdirName
))) > 0)
615 FcStrSetAdd (dirs
, subdirName
);
617 if (!FcDirCacheConsume (fd
, set
))
632 FcDirCacheConsume (int fd
, FcFontSet
*set
)
635 void * current_dir_block
;
638 read(fd
, &metadata
, sizeof(FcCache
));
639 if (metadata
.magic
!= FC_CACHE_MAGIC
)
645 pos
= FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
));
646 current_dir_block
= mmap (0, metadata
.count
,
647 PROT_READ
, MAP_SHARED
, fd
, pos
);
648 if (current_dir_block
== MAP_FAILED
)
651 if (!FcFontSetUnserialize (metadata
, set
, current_dir_block
))
658 FcDirCacheProduce (FcFontSet
*set
, FcCache
*metadata
)
660 void * current_dir_block
, * final_dir_block
;
661 static int rand_state
= 0;
665 rand_state
= time(0L);
666 bank
= rand_r(&rand_state
);
668 while (FcCacheHaveBank(bank
))
669 bank
= rand_r(&rand_state
);
671 memset (metadata
, 0, sizeof(FcCache
));
673 metadata
->count
= FcFontSetNeededBytes (set
);
674 metadata
->magic
= FC_CACHE_MAGIC
;
675 metadata
->bank
= bank
;
677 if (!metadata
->count
) /* not a failure, no fonts to write */
680 current_dir_block
= malloc (metadata
->count
);
681 if (!current_dir_block
)
683 final_dir_block
= FcFontSetDistributeBytes (metadata
, current_dir_block
);
685 if ((char *)current_dir_block
+ metadata
->count
!= final_dir_block
)
688 if (!FcFontSetSerialize (bank
, set
))
691 return current_dir_block
;
694 free (current_dir_block
);
698 /* write serialized state to the cache file */
700 FcDirCacheWrite (FcFontSet
*set
, FcStrSet
*dirs
, const FcChar8
*dir
)
702 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
705 off_t current_arch_start
= 0, truncate_to
;
706 char * current_arch_machine_name
, * header
;
707 void * current_dir_block
;
712 current_dir_block
= FcDirCacheProduce (set
, &metadata
);
721 if (!current_dir_block
)
724 if (FcDebug () & FC_DBG_CACHE
)
725 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file
);
727 fd
= open(cache_file
, O_RDWR
| O_CREAT
, 0666);
731 current_arch_machine_name
= FcCacheMachineSignature ();
732 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
733 if (current_arch_start
< 0)
734 current_arch_start
= FcCacheNextOffset (lseek(fd
, 0, SEEK_END
));
736 if (!FcCacheMoveDown(fd
, current_arch_start
))
739 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
740 if (ftruncate (fd
, current_arch_start
) == -1)
743 /* now write the address of the next offset */
744 truncate_to
= FcCacheNextOffset (FcCacheNextOffset (current_arch_start
+ sizeof (FcCache
)) + metadata
.count
) - current_arch_start
;
745 header
= malloc (10 + strlen (current_arch_machine_name
));
748 sprintf (header
, "%8x ", (int)truncate_to
);
749 strcat (header
, current_arch_machine_name
);
750 if (!FcCacheWriteString (fd
, header
))
753 for (i
= 0; i
< dirs
->size
; i
++)
754 FcCacheWriteString (fd
, dirs
->strs
[i
]);
755 FcCacheWriteString (fd
, "");
757 write (fd
, &metadata
, sizeof(FcCache
));
758 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_END
)), SEEK_SET
);
759 write (fd
, current_dir_block
, metadata
.count
);
760 free (current_dir_block
);
762 /* this actually serves to pad out the cache file, if needed */
763 if (ftruncate (fd
, current_arch_start
+ truncate_to
) == -1)
772 free (current_dir_block
);
780 FcCacheMachineSignature ()
782 static char buf
[MACHINE_SIGNATURE_SIZE
];
783 int magic
= ENDIAN_TEST
;
784 char * m
= (char *)&magic
;
786 sprintf (buf
, "%2x%2x%2x%2x "
787 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
788 "%4x %4x %4x %4x %4x %4x %4x\n",
789 m
[0], m
[1], m
[2], m
[3],
794 sizeof (FcPatternEltPtr
),
795 sizeof (struct _FcPatternElt
*),
796 sizeof (FcPatternElt
),
797 sizeof (FcObjectPtr
),
798 sizeof (FcValueListPtr
),
800 sizeof (FcValueBinding
),
801 sizeof (struct _FcValueList
*),
803 sizeof (FcCharLeaf
**),
813 static int banks_ptr
= 0, banks_alloc
= 0;
814 static int * bankId
= 0;
817 FcCacheHaveBank (int bank
)
821 if (bank
< FC_BANK_FIRST
)
824 for (i
= 0; i
< banks_ptr
; i
++)
825 if (bankId
[i
] == bank
)
832 FcCacheBankToIndex (int bank
)
834 static int lastBank
= FC_BANK_DYNAMIC
, lastIndex
= -1;
838 if (bank
== lastBank
)
841 for (i
= 0; i
< banks_ptr
; i
++)
842 if (bankId
[i
] == bank
)
845 if (banks_ptr
>= banks_alloc
)
847 b
= realloc (bankId
, (banks_alloc
+ 4) * sizeof(int));