]>
git.wh0rd.org - fontconfig.git/blob - src/fccache.c
2ee4b1ae7bdc89f2504370e42476b72be9a287b9
2 * $XFree86: xc/lib/fontconfig/src/fccache.c,v 1.4 2002/03/01 01:00:54 keithp Exp $
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
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 FcFileCacheHash (const FcChar8
*string
)
33 while ((c
= *string
++))
39 FcFileCacheFind (FcFileCache
*cache
,
46 FcFileCacheEnt
*c
, *name
;
52 hash
= FcFileCacheHash (match
);
55 for (c
= cache
->ents
[hash
% FC_FILE_CACHE_HASH_SIZE
]; c
; c
= c
->next
)
57 if (c
->hash
== hash
&& !strcmp ((const char *) match
, (const char *) c
->file
))
63 if (stat ((char *) file
, &statb
) < 0)
65 if (FcDebug () & FC_DBG_CACHE
)
66 printf (" file missing\n");
69 if (statb
.st_mtime
!= c
->time
)
71 if (FcDebug () & FC_DBG_CACHE
)
72 printf (" timestamp mismatch (was %d is %d)\n",
73 (int) c
->time
, (int) statb
.st_mtime
);
79 c
->referenced
= FcTrue
;
92 * Cache file syntax is quite simple:
94 * "file_name" id time "font_name" \n
98 FcFileCacheReadString (FILE *f
, FcChar8
*dest
, int len
)
106 while ((c
= getc (f
)) != EOF
)
118 while ((c
= getc (f
)) != EOF
)
133 FcChar8
*new = malloc (size
* 2);
136 memcpy (new, d
, size
);
153 FcFileCacheReadUlong (FILE *f
, unsigned long *dest
)
158 while ((c
= getc (f
)) != EOF
)
168 if (c
== EOF
|| isspace (c
))
172 t
= t
* 10 + (c
- '0');
180 FcFileCacheReadInt (FILE *f
, int *dest
)
185 ret
= FcFileCacheReadUlong (f
, &t
);
192 FcFileCacheReadTime (FILE *f
, time_t *dest
)
197 ret
= FcFileCacheReadUlong (f
, &t
);
204 FcFileCacheAdd (FcFileCache
*cache
,
212 FcFileCacheEnt
**prev
, *old
;
215 if (FcDebug () & FC_DBG_CACHE
)
217 printf ("%s face %s/%d as %s\n", replace
? "Replace" : "Add",
220 hash
= FcFileCacheHash (file
);
221 for (prev
= &cache
->ents
[hash
% FC_FILE_CACHE_HASH_SIZE
];
223 prev
= &(*prev
)->next
)
225 if (old
->hash
== hash
&& old
->id
== id
&& !strcmp ((const char *) old
->file
,
226 (const char *) file
))
242 c
= malloc (sizeof (FcFileCacheEnt
) +
243 strlen ((char *) file
) + 1 +
244 strlen ((char *) name
) + 1);
250 c
->file
= (FcChar8
*) (c
+ 1);
252 c
->name
= c
->file
+ strlen ((char *) file
) + 1;
253 strcpy ((char *) c
->file
, (const char *) file
);
255 c
->referenced
= replace
;
256 strcpy ((char *) c
->name
, (const char *) name
);
262 FcFileCacheCreate (void)
267 cache
= malloc (sizeof (FcFileCache
));
270 for (h
= 0; h
< FC_FILE_CACHE_HASH_SIZE
; h
++)
273 cache
->referenced
= 0;
274 cache
->updated
= FcFalse
;
279 FcFileCacheDestroy (FcFileCache
*cache
)
281 FcFileCacheEnt
*c
, *next
;
284 for (h
= 0; h
< FC_FILE_CACHE_HASH_SIZE
; h
++)
286 for (c
= cache
->ents
[h
]; c
; c
= next
)
296 FcFileCacheLoad (FcFileCache
*cache
,
297 const FcChar8
*cache_file
)
300 FcChar8 file_buf
[8192], *file
;
303 FcChar8 name_buf
[8192], *name
;
305 f
= fopen ((char *) cache_file
, "r");
309 cache
->updated
= FcFalse
;
312 while ((file
= FcFileCacheReadString (f
, file_buf
, sizeof (file_buf
))) &&
313 FcFileCacheReadInt (f
, &id
) &&
314 FcFileCacheReadTime (f
, &time
) &&
315 (name
= FcFileCacheReadString (f
, name_buf
, sizeof (name_buf
))))
317 (void) FcFileCacheAdd (cache
, file
, id
, time
, name
, FcFalse
);
318 if (file
!= file_buf
)
320 if (name
!= name_buf
)
325 if (file
&& file
!= file_buf
)
327 if (name
&& name
!= name_buf
)
333 FcFileCacheUpdate (FcFileCache
*cache
,
338 const FcChar8
*match
;
344 if (stat ((char *) file
, &statb
) < 0)
346 ret
= FcFileCacheAdd (cache
, match
, id
,
347 statb
.st_mtime
, name
, FcTrue
);
349 cache
->updated
= FcTrue
;
354 FcFileCacheWriteString (FILE *f
, const FcChar8
*string
)
358 if (putc ('"', f
) == EOF
)
360 while ((c
= *string
++))
365 if (putc ('\\', f
) == EOF
)
369 if (putc (c
, f
) == EOF
)
373 if (putc ('"', f
) == EOF
)
379 FcFileCacheWriteUlong (FILE *f
, unsigned long t
)
382 unsigned long temp
, digit
;
395 if (putc ((char) digit
+ '0', f
) == EOF
)
397 temp
= temp
- pow
* digit
;
404 FcFileCacheWriteInt (FILE *f
, int i
)
406 return FcFileCacheWriteUlong (f
, (unsigned long) i
);
410 FcFileCacheWriteTime (FILE *f
, time_t t
)
412 return FcFileCacheWriteUlong (f
, (unsigned long) t
);
416 FcFileCacheSave (FcFileCache
*cache
,
417 const FcChar8
*cache_file
)
424 if (!cache
->updated
&& cache
->referenced
== cache
->entries
)
427 /* Set-UID programs can't safely update the cache */
428 if (getuid () != geteuid ())
431 atomic
= FcAtomicCreate (cache_file
);
434 if (!FcAtomicLock (atomic
))
436 f
= fopen ((char *) FcAtomicNewFile(atomic
), "w");
440 for (h
= 0; h
< FC_FILE_CACHE_HASH_SIZE
; h
++)
442 for (c
= cache
->ents
[h
]; c
; c
= c
->next
)
446 if (!FcFileCacheWriteString (f
, c
->file
))
448 if (putc (' ', f
) == EOF
)
450 if (!FcFileCacheWriteInt (f
, c
->id
))
452 if (putc (' ', f
) == EOF
)
454 if (!FcFileCacheWriteTime (f
, c
->time
))
456 if (putc (' ', f
) == EOF
)
458 if (!FcFileCacheWriteString (f
, c
->name
))
460 if (putc ('\n', f
) == EOF
)
465 if (fclose (f
) == EOF
)
468 if (!FcAtomicReplaceOrig (atomic
))
471 FcAtomicUnlock (atomic
);
472 FcAtomicDestroy (atomic
);
474 cache
->updated
= FcFalse
;
480 FcAtomicDeleteNew (atomic
);
482 FcAtomicUnlock (atomic
);
484 FcAtomicDestroy (atomic
);
490 FcFileCacheReadDir (FcFontSet
*set
, const FcChar8
*cache_file
)
496 FcChar8 file_buf
[8192], *file
;
498 FcChar8 name_buf
[8192], *name
;
499 FcBool ret
= FcFalse
;
501 if (FcDebug () & FC_DBG_CACHE
)
503 printf ("FcFileCacheReadDir cache_file \"%s\"\n", cache_file
);
506 f
= fopen ((char *) cache_file
, "r");
509 if (FcDebug () & FC_DBG_CACHE
)
511 printf (" no cache file\n");
516 base
= (FcChar8
*) strrchr ((char *) cache_file
, '/');
520 path
= malloc (base
- cache_file
+ 8192 + 1);
523 memcpy (path
, cache_file
, base
- cache_file
);
524 base
= path
+ (base
- cache_file
);
528 while ((file
= FcFileCacheReadString (f
, file_buf
, sizeof (file_buf
))) &&
529 FcFileCacheReadInt (f
, &id
) &&
530 (name
= FcFileCacheReadString (f
, name_buf
, sizeof (name_buf
))))
532 font
= FcNameParse (name
);
535 strcpy ((char *) base
, (const char *) file
);
536 if (FcDebug () & FC_DBG_CACHEV
)
538 printf (" dir cache file \"%s\"\n", file
);
540 FcPatternAddString (font
, FC_FILE
, path
);
541 if (!FcFontSetAdd (set
, font
))
544 if (file
!= file_buf
)
546 if (name
!= name_buf
)
550 if (FcDebug () & FC_DBG_CACHE
)
552 printf (" cache loaded\n");
558 if (file
&& file
!= file_buf
)
560 if (name
&& name
!= name_buf
)
569 FcFileCacheWriteDir (FcFontSet
*set
, const FcChar8
*cache_file
)
574 const FcChar8
*file
, *base
;
579 if (FcDebug () & FC_DBG_CACHE
)
580 printf ("FcFileCacheWriteDir cache_file \"%s\"\n", cache_file
);
582 f
= fopen ((char *) cache_file
, "w");
585 if (FcDebug () & FC_DBG_CACHE
)
586 printf (" can't create \"%s\"\n", cache_file
);
589 for (n
= 0; n
< set
->nfont
; n
++)
591 font
= set
->fonts
[n
];
592 if (FcPatternGetString (font
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
594 base
= (FcChar8
*) strrchr ((char *) file
, '/');
599 if (FcPatternGetInteger (font
, FC_INDEX
, 0, &id
) != FcResultMatch
)
601 if (FcDebug () & FC_DBG_CACHEV
)
602 printf (" write file \"%s\"\n", base
);
603 if (!FcFileCacheWriteString (f
, base
))
605 if (putc (' ', f
) == EOF
)
607 if (!FcFileCacheWriteInt (f
, id
))
609 if (putc (' ', f
) == EOF
)
611 name
= FcNameUnparse (font
);
614 ret
= FcFileCacheWriteString (f
, name
);
618 if (putc ('\n', f
) == EOF
)
621 if (fclose (f
) == EOF
)
624 if (FcDebug () & FC_DBG_CACHE
)
625 printf (" cache written\n");
631 unlink ((char *) cache_file
);