1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
/*
* $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $
*
* Copyright © 2002 Keith Packard
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Keith Packard not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Keith Packard makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "../fc-arch/fcarch.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#else
#ifdef linux
#define HAVE_GETOPT_LONG 1
#endif
#define HAVE_GETOPT 1
#endif
#include <fontconfig/fontconfig.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#if defined (_WIN32)
#define STRICT
#include <windows.h>
#define sleep(x) Sleep((x) * 1000)
#undef STRICT
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifndef HAVE_GETOPT
#define HAVE_GETOPT 0
#endif
#ifndef HAVE_GETOPT_LONG
#define HAVE_GETOPT_LONG 0
#endif
#if HAVE_GETOPT_LONG
#undef _GNU_SOURCE
#define _GNU_SOURCE
#include <getopt.h>
const struct option longopts[] = {
{"force", 0, 0, 'f'},
{"really-force", 0, 0, 'r'},
{"system-only", 0, 0, 's'},
{"version", 0, 0, 'V'},
{"verbose", 0, 0, 'v'},
{"help", 0, 0, '?'},
{NULL,0,0,0},
};
#else
#if HAVE_GETOPT
extern char *optarg;
extern int optind, opterr, optopt;
#endif
#endif
static void
usage (char *program)
{
#if HAVE_GETOPT_LONG
fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
program);
#else
fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n",
program);
#endif
fprintf (stderr, "Build font information caches in [dirs]\n"
"(all directories in font configuration by default).\n");
fprintf (stderr, "\n");
#if HAVE_GETOPT_LONG
fprintf (stderr, " -f, --force scan directories with apparently valid caches\n");
fprintf (stderr, " -r, --really-force erase all existing caches, then rescan\n");
fprintf (stderr, " -s, --system-only scan system-wide directories only\n");
fprintf (stderr, " -v, --verbose display status information while busy\n");
fprintf (stderr, " -V, --version display font config version and exit\n");
fprintf (stderr, " -?, --help display this help and exit\n");
#else
fprintf (stderr, " -f (force) scan directories with apparently valid caches\n");
fprintf (stderr, " -r, (really force) erase all existing caches, then rescan\n");
fprintf (stderr, " -s (system) scan system-wide directories only\n");
fprintf (stderr, " -v (verbose) display status information while busy\n");
fprintf (stderr, " -V (version) display font config version and exit\n");
fprintf (stderr, " -? (help) display this help and exit\n");
#endif
exit (1);
}
static FcStrSet *processed_dirs;
static int
scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose)
{
int ret = 0;
const FcChar8 *dir;
FcStrSet *subdirs;
FcStrList *sublist;
FcCache *cache;
struct stat statb;
FcBool was_valid;
int i;
/*
* Now scan all of the directories into separate databases
* and write out the results
*/
while ((dir = FcStrListNext (list)))
{
if (verbose)
{
printf ("%s: ", dir);
fflush (stdout);
}
if (!dir)
{
if (verbose)
printf ("skipping, no such directory\n");
continue;
}
if (FcStrSetMember (processed_dirs, dir))
{
if (verbose)
printf ("skipping, looped directory detected\n");
continue;
}
if (stat ((char *) dir, &statb) == -1)
{
switch (errno) {
case ENOENT:
case ENOTDIR:
if (verbose)
printf ("skipping, no such directory\n");
break;
default:
fprintf (stderr, "\"%s\": ", dir);
perror ("");
ret++;
break;
}
continue;
}
if (!S_ISDIR (statb.st_mode))
{
fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
continue;
}
if (really_force)
FcDirCacheUnlink (dir, config);
cache = NULL;
was_valid = FcFalse;
if (!force) {
cache = FcDirCacheLoad (dir, config, NULL);
if (cache)
was_valid = FcTrue;
}
if (!cache)
{
cache = FcDirCacheRead (dir, FcTrue, config);
if (!cache)
{
fprintf (stderr, "%s: error scanning\n", dir);
ret++;
continue;
}
}
if (was_valid)
{
if (verbose)
printf ("skipping, existing cache is valid: %d fonts, %d dirs\n",
FcCacheNumFont (cache), FcCacheNumSubdir (cache));
}
else
{
if (verbose)
printf ("caching, new cache contents: %d fonts, %d dirs\n",
FcCacheNumFont (cache), FcCacheNumSubdir (cache));
if (!FcDirCacheValid (dir))
{
fprintf (stderr, "%s: failed to write cache\n", dir);
(void) FcDirCacheUnlink (dir, config);
ret++;
}
}
subdirs = FcStrSetCreate ();
if (!subdirs)
{
fprintf (stderr, "%s: Can't create subdir set\n", dir);
ret++;
FcDirCacheUnload (cache);
continue;
}
for (i = 0; i < FcCacheNumSubdir (cache); i++)
FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
FcDirCacheUnload (cache);
sublist = FcStrListCreate (subdirs);
FcStrSetDestroy (subdirs);
if (!sublist)
{
fprintf (stderr, "%s: Can't create subdir list\n", dir);
ret++;
continue;
}
FcStrSetAdd (processed_dirs, dir);
ret += scanDirs (sublist, config, force, really_force, verbose);
}
FcStrListDone (list);
return ret;
}
static FcBool
cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose)
{
DIR *d;
struct dirent *ent;
FcChar8 *dir_base;
FcBool ret = FcTrue;
FcBool remove;
FcCache *cache;
struct stat file_stat;
struct stat target_stat;
dir_base = FcStrPlus (dir, (FcChar8 *) "/");
if (!dir_base)
{
fprintf (stderr, "%s: out of memory\n", dir);
return FcFalse;
}
if (access ((char *) dir, W_OK) != 0)
{
if (verbose)
printf ("%s: not cleaning %s cache directory\n", dir,
access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
FcStrFree (dir_base);
return FcTrue;
}
if (verbose)
printf ("%s: cleaning cache directory\n", dir);
d = opendir ((char *) dir);
if (!d)
{
perror ((char *) dir);
FcStrFree (dir_base);
return FcFalse;
}
while ((ent = readdir (d)))
{
FcChar8 *file_name;
const FcChar8 *target_dir;
if (ent->d_name[0] == '.')
continue;
/* skip cache files for different architectures and */
/* files which are not cache files at all */
if (strlen(ent->d_name) != 32 + strlen ("-" FC_ARCHITECTURE FC_CACHE_SUFFIX) ||
strcmp(ent->d_name + 32, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX))
continue;
file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
if (!file_name)
{
fprintf (stderr, "%s: allocation failure\n", dir);
ret = FcFalse;
break;
}
remove = FcFalse;
cache = FcDirCacheLoadFile (file_name, &file_stat);
if (!cache)
{
if (verbose)
printf ("%s: invalid cache file: %s\n", dir, ent->d_name);
remove = FcTrue;
}
else
{
target_dir = FcCacheDir (cache);
if (stat ((char *) target_dir, &target_stat) < 0)
{
if (verbose)
printf ("%s: %s: missing directory: %s \n",
dir, ent->d_name, target_dir);
remove = FcTrue;
}
else if (target_stat.st_mtime > file_stat.st_mtime)
{
if (verbose)
printf ("%s: %s: cache outdated: %s\n",
dir, ent->d_name, target_dir);
remove = FcTrue;
}
}
if (remove)
{
if (unlink ((char *) file_name) < 0)
{
perror ((char *) file_name);
ret = FcFalse;
}
}
FcDirCacheUnload (cache);
FcStrFree (file_name);
}
closedir (d);
FcStrFree (dir_base);
return ret;
}
static FcBool
cleanCacheDirectories (FcConfig *config, FcBool verbose)
{
FcStrList *cache_dirs = FcConfigGetCacheDirs (config);
FcChar8 *cache_dir;
FcBool ret = FcTrue;
if (!cache_dirs)
return FcFalse;
while ((cache_dir = FcStrListNext (cache_dirs)))
{
if (!cleanCacheDirectory (config, cache_dir, verbose))
{
ret = FcFalse;
break;
}
}
FcStrListDone (cache_dirs);
return ret;
}
int
main (int argc, char **argv)
{
FcStrSet *dirs;
FcStrList *list;
FcBool verbose = FcFalse;
FcBool force = FcFalse;
FcBool really_force = FcFalse;
FcBool systemOnly = FcFalse;
FcConfig *config;
int i;
int ret;
#if HAVE_GETOPT_LONG || HAVE_GETOPT
int c;
#if HAVE_GETOPT_LONG
while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1)
#else
while ((c = getopt (argc, argv, "frsVv?")) != -1)
#endif
{
switch (c) {
case 'r':
really_force = FcTrue;
/* fall through */
case 'f':
force = FcTrue;
break;
case 's':
systemOnly = FcTrue;
break;
case 'V':
fprintf (stderr, "fontconfig version %d.%d.%d\n",
FC_MAJOR, FC_MINOR, FC_REVISION);
exit (0);
case 'v':
verbose = FcTrue;
break;
default:
usage (argv[0]);
}
}
i = optind;
#else
i = 1;
#endif
if (systemOnly)
FcConfigEnableHome (FcFalse);
config = FcInitLoadConfig ();
if (!config)
{
fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
return 1;
}
FcConfigSetCurrent (config);
if (argv[i])
{
dirs = FcStrSetCreate ();
if (!dirs)
{
fprintf (stderr, "%s: Can't create list of directories\n",
argv[0]);
return 1;
}
while (argv[i])
{
if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
{
fprintf (stderr, "%s: Can't add directory\n", argv[0]);
return 1;
}
i++;
}
list = FcStrListCreate (dirs);
FcStrSetDestroy (dirs);
}
else
list = FcConfigGetConfigDirs (config);
if ((processed_dirs = FcStrSetCreate()) == NULL) {
fprintf(stderr, "Cannot malloc\n");
return 1;
}
ret = scanDirs (list, config, force, really_force, verbose);
FcStrSetDestroy (processed_dirs);
cleanCacheDirectories (config, verbose);
/*
* Now we need to sleep a second (or two, to be extra sure), to make
* sure that timestamps for changes after this run of fc-cache are later
* then any timestamps we wrote. We don't use gettimeofday() because
* sleep(3) can't be interrupted by a signal here -- this isn't in the
* library, and there aren't any signals flying around here.
*/
FcConfigDestroy (config);
sleep (2);
if (verbose)
printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
return ret;
}
|