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 FcGlobalCacheDir
*dd
, *next
;
115 for (dd
= d
; dd
; dd
= next
)
118 FcMemFree (FC_MEM_STRING
, strlen (dd
->name
)+1);
120 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCacheDir
));
126 FcGlobalCacheCreate (void)
128 FcGlobalCache
*cache
;
130 cache
= malloc (sizeof (FcGlobalCache
));
133 FcMemAlloc (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
135 cache
->updated
= FcFalse
;
141 FcGlobalCacheDestroy (FcGlobalCache
*cache
)
143 FcGlobalCacheDir
*d
, *next
;
145 for (d
= cache
->dirs
; d
; d
= next
)
148 FcGlobalCacheDirDestroy (d
);
150 FcMemFree (FC_MEM_CACHE
, sizeof (FcGlobalCache
));
155 FcGlobalCacheLoad (FcGlobalCache
*cache
,
156 const FcChar8
*cache_file
)
158 FcChar8 name_buf
[8192];
159 FcGlobalCacheDir
*d
, *next
;
160 char * current_arch_machine_name
;
161 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
162 off_t current_arch_start
;
164 cache
->fd
= open ((char *) cache_file
, O_RDONLY
);
168 cache
->updated
= FcFalse
;
170 current_arch_machine_name
= FcCacheMachineSignature ();
171 current_arch_start
= FcCacheSkipToArch(cache
->fd
,
172 current_arch_machine_name
);
173 if (current_arch_start
< 0)
176 lseek (cache
->fd
, current_arch_start
, SEEK_SET
);
177 if (FcCacheReadString (cache
->fd
, candidate_arch_machine_name
,
178 sizeof (candidate_arch_machine_name
)) == 0)
183 FcCacheReadString (cache
->fd
, name_buf
, sizeof (name_buf
));
184 if (!strlen(name_buf
))
187 d
= malloc (sizeof (FcGlobalCacheDir
));
191 d
->next
= cache
->dirs
;
194 d
->name
= FcStrCopy (name_buf
);
196 d
->offset
= lseek (cache
->fd
, 0, SEEK_CUR
);
197 read (cache
->fd
, &d
->metadata
, sizeof (FcCache
));
198 lseek (cache
->fd
, d
->metadata
.count
, SEEK_CUR
);
203 for (d
= cache
->dirs
; d
; d
= next
)
210 free (current_arch_machine_name
);
217 FcGlobalCacheReadDir (FcFontSet
*set
, FcStrSet
*dirs
, FcGlobalCache
* cache
, const FcChar8
*dir
, FcConfig
*config
)
224 for (d
= cache
->dirs
; d
; d
= d
->next
)
226 if (strcmp (d
->name
, dir
) == 0)
228 lseek (cache
->fd
, d
->offset
, SEEK_SET
);
229 if (!FcDirCacheConsume (cache
->fd
, set
))
239 FcGlobalCacheUpdate (FcGlobalCache
*cache
,
243 FcGlobalCacheDir
* d
;
248 for (d
= cache
->dirs
; d
; d
= d
->next
)
250 if (strcmp(d
->name
, name
) == 0)
256 d
= malloc (sizeof (FcGlobalCacheDir
));
259 d
->next
= cache
->dirs
;
263 cache
->updated
= FcTrue
;
265 d
->name
= FcStrCopy (name
);
266 d
->ent
= FcDirCacheProduce (set
, &d
->metadata
);
272 FcGlobalCacheSave (FcGlobalCache
*cache
,
273 const FcChar8
*cache_file
)
276 FcGlobalCacheDir
*dir
;
278 off_t current_arch_start
= 0, truncate_to
;
279 char * current_arch_machine_name
, * header
;
284 #if defined (HAVE_GETUID) && defined (HAVE_GETEUID)
285 /* Set-UID programs can't safely update the cache */
286 if (getuid () != geteuid ())
290 atomic
= FcAtomicCreate (cache_file
);
293 if (!FcAtomicLock (atomic
))
295 fd
= open ((char *) FcAtomicNewFile(atomic
), O_RDWR
| O_CREAT
,
300 current_arch_machine_name
= FcCacheMachineSignature ();
301 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
302 if (current_arch_start
< 0)
303 current_arch_start
= FcCacheNextOffset (lseek(fd
, 0, SEEK_END
));
305 if (!FcCacheMoveDown(fd
, current_arch_start
))
308 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
309 if (ftruncate (fd
, current_arch_start
) == -1)
312 truncate_to
= current_arch_start
;
313 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
315 truncate_to
+= strlen(dir
->name
) + 1;
316 truncate_to
+= sizeof (FcCache
);
317 truncate_to
= FcCacheNextOffset (current_arch_start
+ truncate_to
);
318 truncate_to
+= dir
->metadata
.count
;
320 truncate_to
-= current_arch_start
;
321 header
= malloc (10 + strlen (current_arch_machine_name
));
324 sprintf (header
, "%8x ", (int)truncate_to
);
325 strcat (header
, current_arch_machine_name
);
326 if (!FcCacheWriteString (fd
, header
))
329 for (dir
= cache
->dirs
; dir
; dir
= dir
->next
)
331 FcCacheWriteString (fd
, dir
->name
);
332 write (fd
, &dir
->metadata
, sizeof(FcCache
));
333 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_END
)), SEEK_SET
);
334 write (fd
, dir
->ent
, dir
->metadata
.count
);
336 FcCacheWriteString (fd
, "");
338 if (close (fd
) == -1)
341 if (!FcAtomicReplaceOrig (atomic
))
344 FcAtomicUnlock (atomic
);
345 FcAtomicDestroy (atomic
);
347 cache
->updated
= FcFalse
;
351 FcAtomicDeleteNew (atomic
);
353 FcAtomicUnlock (atomic
);
355 FcAtomicDestroy (atomic
);
360 #define PAGESIZE 8192
362 * Find the next presumably-mmapable offset after the supplied file
366 FcCacheNextOffset(off_t w
)
368 if (w
% PAGESIZE
== 0)
371 return ((w
/ PAGESIZE
)+1)*PAGESIZE
;
374 /* return the address of the segment for the provided arch,
375 * or -1 if arch not found */
377 FcCacheSkipToArch (int fd
, const char * arch
)
379 char candidate_arch_machine_name_count
[MACHINE_SIGNATURE_SIZE
+ 9];
380 char * candidate_arch
;
381 off_t current_arch_start
= 0;
383 /* skip arches that are not the current arch */
388 lseek (fd
, current_arch_start
, SEEK_SET
);
389 if (FcCacheReadString (fd
, candidate_arch_machine_name_count
,
390 sizeof (candidate_arch_machine_name_count
)) == 0)
392 if (!strlen(candidate_arch_machine_name_count
))
394 bs
= strtol(candidate_arch_machine_name_count
, &candidate_arch
, 16);
395 candidate_arch
++; /* skip leading space */
397 if (strcmp (candidate_arch
, arch
)==0)
399 current_arch_start
+= bs
;
402 if (candidate_arch
&& strcmp (candidate_arch
, arch
)!=0)
405 return current_arch_start
;
408 /* Cuts out the segment at the file pointer (moves everything else
409 * down to cover it), and leaves the file pointer at the end of the
412 FcCacheMoveDown (int fd
, off_t start
)
414 char * buf
= malloc (8192);
415 char candidate_arch_machine_name
[MACHINE_SIGNATURE_SIZE
+ 9];
417 int c
, bytes_skipped
;
422 lseek (fd
, start
, SEEK_SET
);
423 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
424 sizeof (candidate_arch_machine_name
)) == 0)
426 if (!strlen(candidate_arch_machine_name
))
429 bs
= strtol(candidate_arch_machine_name
, 0, 16);
436 lseek (fd
, start
+bs
+bytes_skipped
, SEEK_SET
);
437 if ((c
= read (fd
, buf
, 8192)) <= 0)
439 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
440 if (write (fd
, buf
, c
) < 0)
445 lseek (fd
, start
+bytes_skipped
, SEEK_SET
);
457 FcDirCacheValid (const FcChar8
*dir
)
459 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
460 struct stat file_stat
, dir_stat
;
462 if (stat ((char *) dir
, &dir_stat
) < 0)
464 FcStrFree (cache_file
);
467 if (stat ((char *) cache_file
, &file_stat
) < 0)
469 FcStrFree (cache_file
);
472 FcStrFree (cache_file
);
474 * If the directory has been modified more recently than
475 * the cache file, the cache is not valid
477 if (dir_stat
.st_mtime
- file_stat
.st_mtime
> 0)
483 FcCacheReadDirs (FcConfig
* config
, FcGlobalCache
* cache
,
484 FcStrList
*list
, FcFontSet
* set
)
490 FcChar8
*file
, *base
;
496 * Now scan all of the directories into separate databases
497 * and write out the results
499 while ((dir
= FcStrListNext (list
)))
502 file
= (FcChar8
*) malloc (strlen ((char *) dir
) + 1 + FC_MAX_FILE_LEN
+ 1);
506 strcpy ((char *) file
, (char *) dir
);
507 strcat ((char *) file
, "/");
508 base
= file
+ strlen ((char *) file
);
510 subdirs
= FcStrSetCreate ();
513 fprintf (stderr
, "Can't create directory set\n");
519 if (access ((char *) dir
, X_OK
) < 0)
527 fprintf (stderr
, "\"%s\": ", dir
);
531 FcStrSetDestroy (subdirs
);
535 if (stat ((char *) dir
, &statb
) == -1)
537 fprintf (stderr
, "\"%s\": ", dir
);
539 FcStrSetDestroy (subdirs
);
544 if (!S_ISDIR (statb
.st_mode
))
546 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
547 FcStrSetDestroy (subdirs
);
551 d
= opendir ((char *) dir
);
554 FcStrSetDestroy (subdirs
);
558 while ((e
= readdir (d
)))
560 if (e
->d_name
[0] != '.' && strlen (e
->d_name
) < FC_MAX_FILE_LEN
)
562 strcpy ((char *) base
, (char *) e
->d_name
);
563 if (FcFileIsDir (file
) && !FcStrSetAdd (subdirs
, file
))
568 if (!FcDirCacheValid (dir
) || !FcDirCacheRead (set
, dir
))
570 if (FcDebug () & FC_DBG_FONTSET
)
571 printf ("scan dir %s\n", dir
);
572 FcDirScanConfig (set
, subdirs
, cache
,
573 config
->blanks
, dir
, FcFalse
, config
);
575 sublist
= FcStrListCreate (subdirs
);
576 FcStrSetDestroy (subdirs
);
579 fprintf (stderr
, "Can't create subdir list in \"%s\"\n", dir
);
584 ret
+= FcCacheReadDirs (config
, cache
, sublist
, set
);
587 FcStrListDone (list
);
592 FcCacheRead (FcConfig
*config
, FcGlobalCache
* cache
)
594 FcFontSet
* s
= FcFontSetCreate();
598 if (FcCacheReadDirs (config
, cache
, FcConfigGetConfigDirs (config
), s
))
604 FcFontSetDestroy (s
);
608 /* read serialized state from the cache file */
610 FcDirCacheRead (FcFontSet
* set
, const FcChar8
*dir
)
612 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
614 char * current_arch_machine_name
;
615 char candidate_arch_machine_name
[9+MACHINE_SIGNATURE_SIZE
];
616 off_t current_arch_start
= 0;
621 current_arch_machine_name
= FcCacheMachineSignature();
622 fd
= open(cache_file
, O_RDONLY
);
626 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
627 if (current_arch_start
< 0)
630 lseek (fd
, current_arch_start
, SEEK_SET
);
631 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
632 sizeof (candidate_arch_machine_name
)) == 0)
635 if (!FcDirCacheConsume (fd
, set
))
650 FcDirCacheConsume (int fd
, FcFontSet
*set
)
653 void * current_dir_block
;
656 read(fd
, &metadata
, sizeof(FcCache
));
657 if (metadata
.magic
!= FC_CACHE_MAGIC
)
663 pos
= FcCacheNextOffset (lseek(fd
, 0, SEEK_CUR
));
664 current_dir_block
= mmap (0, metadata
.count
,
665 PROT_READ
, MAP_SHARED
, fd
, pos
);
666 if (current_dir_block
== MAP_FAILED
)
669 if (!FcFontSetUnserialize (metadata
, set
, current_dir_block
))
676 FcDirCacheProduce (FcFontSet
*set
, FcCache
*metadata
)
678 void * current_dir_block
, * final_dir_block
;
679 static int rand_state
= 0;
683 rand_state
= time(0L);
684 bank
= rand_r(&rand_state
);
686 while (FcCacheHaveBank(bank
))
687 bank
= rand_r(&rand_state
);
689 memset (metadata
, 0, sizeof(FcCache
));
691 metadata
->count
= FcFontSetNeededBytes (set
);
692 metadata
->magic
= FC_CACHE_MAGIC
;
693 metadata
->bank
= bank
;
695 if (!metadata
->count
) /* not a failure, no fonts to write */
698 current_dir_block
= malloc (metadata
->count
);
699 if (!current_dir_block
)
701 final_dir_block
= FcFontSetDistributeBytes (metadata
, current_dir_block
);
703 if ((char *)current_dir_block
+ metadata
->count
!= final_dir_block
)
706 if (!FcFontSetSerialize (bank
, set
))
709 return current_dir_block
;
712 free (current_dir_block
);
716 /* write serialized state to the cache file */
718 FcDirCacheWrite (FcFontSet
*set
, const FcChar8
*dir
)
720 FcChar8
*cache_file
= FcStrPlus (dir
, (FcChar8
*) "/" FC_DIR_CACHE_FILE
);
723 off_t current_arch_start
= 0, truncate_to
;
724 char * current_arch_machine_name
, * header
;
725 void * current_dir_block
;
730 current_dir_block
= FcDirCacheProduce (set
, &metadata
);
739 if (!current_dir_block
)
742 if (FcDebug () & FC_DBG_CACHE
)
743 printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file
);
745 fd
= open(cache_file
, O_RDWR
| O_CREAT
, 0666);
749 current_arch_machine_name
= FcCacheMachineSignature ();
750 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
751 if (current_arch_start
< 0)
752 current_arch_start
= FcCacheNextOffset (lseek(fd
, 0, SEEK_END
));
754 if (!FcCacheMoveDown(fd
, current_arch_start
))
757 current_arch_start
= lseek(fd
, 0, SEEK_CUR
);
758 if (ftruncate (fd
, current_arch_start
) == -1)
761 /* now write the address of the next offset */
762 truncate_to
= FcCacheNextOffset (FcCacheNextOffset (current_arch_start
+ sizeof (FcCache
)) + metadata
.count
) - current_arch_start
;
763 header
= malloc (10 + strlen (current_arch_machine_name
));
766 sprintf (header
, "%8x ", (int)truncate_to
);
767 strcat (header
, current_arch_machine_name
);
768 if (!FcCacheWriteString (fd
, header
))
771 write (fd
, &metadata
, sizeof(FcCache
));
772 lseek (fd
, FcCacheNextOffset (lseek(fd
, 0, SEEK_END
)), SEEK_SET
);
773 write (fd
, current_dir_block
, metadata
.count
);
775 /* this actually serves to pad out the cache file, if needed */
776 if (ftruncate (fd
, current_arch_start
+ truncate_to
) == -1)
785 free (current_arch_machine_name
);
787 free (current_dir_block
);
795 FcCacheMachineSignature ()
797 static char buf
[MACHINE_SIGNATURE_SIZE
];
798 int magic
= ENDIAN_TEST
;
799 char * m
= (char *)&magic
;
801 sprintf (buf
, "%2x%2x%2x%2x "
802 "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
803 "%4x %4x %4x %4x %4x %4x %4x\n",
804 m
[0], m
[1], m
[2], m
[3],
809 sizeof (FcPatternEltPtr
),
810 sizeof (struct _FcPatternElt
*),
811 sizeof (FcPatternElt
),
812 sizeof (FcObjectPtr
),
813 sizeof (FcValueListPtr
),
815 sizeof (FcValueBinding
),
816 sizeof (struct _FcValueList
*),
818 sizeof (FcCharLeaf
**),
828 static int banks_ptr
= 0, banks_alloc
= 0;
829 static int * bankId
= 0;
832 FcCacheHaveBank (int bank
)
836 if (bank
< FC_BANK_FIRST
)
839 for (i
= 0; i
< banks_ptr
; i
++)
840 if (bankId
[i
] == bank
)
847 FcCacheBankToIndex (int bank
)
849 static int lastBank
= FC_BANK_DYNAMIC
, lastIndex
= -1;
853 if (bank
== lastBank
)
856 for (i
= 0; i
< banks_ptr
; i
++)
857 if (bankId
[i
] == bank
)
860 if (banks_ptr
>= banks_alloc
)
862 b
= realloc (bankId
, (banks_alloc
+ 4) * sizeof(int));