fc-cache.c 11.2 KB
Newer Older
Keith Packard's avatar
Keith Packard committed
1
/*
Keith Packard's avatar
Keith Packard committed
2
 * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $
Keith Packard's avatar
Keith Packard committed
3
 *
4
 * Copyright © 2002 Keith Packard
Keith Packard's avatar
Keith Packard committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * 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.
 */

25
26
#include "../fc-arch/fcarch.h"

Keith Packard's avatar
Keith Packard committed
27
28
29
#ifdef HAVE_CONFIG_H
#include <config.h>
#else
Marc Aurele La France's avatar
Marc Aurele La France committed
30
31
32
#ifdef linux
#define HAVE_GETOPT_LONG 1
#endif
Keith Packard's avatar
Keith Packard committed
33
34
35
#define HAVE_GETOPT 1
#endif

36
#include <fontconfig/fontconfig.h>
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
43
44
#include <fcntl.h>
#include <dirent.h>
45
#include <string.h>
46

Patrick Lam's avatar
Patrick Lam committed
47
48
49
50
51
52
53
#if defined (_WIN32)
#define STRICT
#include <windows.h>
#define sleep(x) Sleep((x) * 1000)
#undef STRICT
#endif

54
55
56
57
#ifndef O_BINARY
#define O_BINARY 0
#endif

Marc Aurele La France's avatar
Marc Aurele La France committed
58
59
60
61
62
63
64
#ifndef HAVE_GETOPT
#define HAVE_GETOPT 0
#endif
#ifndef HAVE_GETOPT_LONG
#define HAVE_GETOPT_LONG 0
#endif

Keith Packard's avatar
Keith Packard committed
65
#if HAVE_GETOPT_LONG
Marc Aurele La France's avatar
Marc Aurele La France committed
66
#undef  _GNU_SOURCE
Keith Packard's avatar
Keith Packard committed
67
68
69
#define _GNU_SOURCE
#include <getopt.h>
const struct option longopts[] = {
70
    {"force", 0, 0, 'f'},
71
    {"really-force", 0, 0, 'r'},
72
    {"system-only", 0, 0, 's'},
Keith Packard's avatar
Keith Packard committed
73
74
75
76
77
78
79
80
81
82
83
84
    {"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

Keith Packard's avatar
Keith Packard committed
85
86
static void
usage (char *program)
Keith Packard's avatar
Keith Packard committed
87
{
88
#if HAVE_GETOPT_LONG
89
    fprintf (stderr, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
90
91
	     program);
#else
92
    fprintf (stderr, "usage: %s [-frsvV?] [dirs]\n",
Keith Packard's avatar
Keith Packard committed
93
	     program);
94
#endif
Keith Packard's avatar
Keith Packard committed
95
96
97
    fprintf (stderr, "Build font information caches in [dirs]\n"
	     "(all directories in font configuration by default).\n");
    fprintf (stderr, "\n");
98
#if HAVE_GETOPT_LONG
99
    fprintf (stderr, "  -f, --force          scan directories with apparently valid caches\n");
100
    fprintf (stderr, "  -r, --really-force   erase all existing caches, then rescan\n");
101
    fprintf (stderr, "  -s, --system-only    scan system-wide directories only\n");
Keith Packard's avatar
Keith Packard committed
102
103
104
    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");
105
106
#else
    fprintf (stderr, "  -f         (force)   scan directories with apparently valid caches\n");
107
    fprintf (stderr, "  -r,   (really force) erase all existing caches, then rescan\n");
108
109
110
111
112
    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
Keith Packard's avatar
Keith Packard committed
113
114
115
    exit (1);
}

116
117
static FcStrSet *processed_dirs;

118
static int
119
scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose)
Keith Packard's avatar
Keith Packard committed
120
{
121
122
123
124
125
126
127
128
    int		    ret = 0;
    const FcChar8   *dir;
    FcStrSet	    *subdirs;
    FcStrList	    *sublist;
    FcCache	    *cache;
    struct stat	    statb;
    FcBool	    was_valid;
    int		    i;
129
130
131
132
133
    
    /*
     * Now scan all of the directories into separate databases
     * and write out the results
     */
134
    while ((dir = FcStrListNext (list)))
135
136
137
    {
	if (verbose)
	{
138
	    printf ("%s: ", dir);
139
140
	    fflush (stdout);
	}
141
142
143
144
145
146
147
148
	
	if (!dir)
	{
	    if (verbose)
		printf ("skipping, no such directory\n");
	    continue;
	}
	
149
150
151
152
153
154
	if (FcStrSetMember (processed_dirs, dir))
	{
	    if (verbose)
		printf ("skipping, looped directory detected\n");
	    continue;
	}
155

156
	if (stat ((char *) dir, &statb) == -1)
157
	{
158
159
160
	    switch (errno) {
	    case ENOENT:
	    case ENOTDIR:
161
		if (verbose)
162
163
164
		    printf ("skipping, no such directory\n");
		break;
	    default:
165
166
167
		fprintf (stderr, "\"%s\": ", dir);
		perror ("");
		ret++;
168
		break;
169
	    }
170
171
	    continue;
	}
172

173
174
175
176
177
	if (!S_ISDIR (statb.st_mode))
	{
	    fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
	    continue;
	}
178
179
180
181

	if (really_force)
	    FcDirCacheUnlink (dir, config);

182
183
184
185
186
187
188
	cache = NULL;
	was_valid = FcFalse;
	if (!force) {
	    cache = FcDirCacheLoad (dir, config, NULL);
	    if (cache)
		was_valid = FcTrue;
	}
189
	
190
	if (!cache)
191
	{
192
193
194
195
196
197
198
	    cache = FcDirCacheRead (dir, FcTrue, config);
	    if (!cache)
	    {
		fprintf (stderr, "%s: error scanning\n", dir);
		ret++;
		continue;
	    }
199
	}
200
201

	if (was_valid)
202
203
204
	{
	    if (verbose)
		printf ("skipping, %d fonts, %d dirs\n",
205
			FcCacheNumFont (cache), FcCacheNumSubdir (cache));
206
207
208
209
210
	}
	else
	{
	    if (verbose)
		printf ("caching, %d fonts, %d dirs\n", 
211
			FcCacheNumFont (cache), FcCacheNumSubdir (cache));
212

213
	    if (!FcDirCacheValid (dir))
214
	    {
215
216
		fprintf (stderr, "%s: failed to write cache\n", dir);
		(void) FcDirCacheUnlink (dir, config);
217
218
219
		ret++;
	    }
	}
220
221
222
223
224
225
226
227
228
	
	subdirs = FcStrSetCreate ();
	if (!subdirs)
	{
	    fprintf (stderr, "%s: Can't create subdir set\n", dir);
	    ret++;
	    FcDirCacheUnload (cache);
	    continue;
	}
229
	for (i = 0; i < FcCacheNumSubdir (cache); i++)
230
231
232
233
	    FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
	
	FcDirCacheUnload (cache);
	
234
	sublist = FcStrListCreate (subdirs);
Keith Packard's avatar
Keith Packard committed
235
	FcStrSetDestroy (subdirs);
236
237
	if (!sublist)
	{
238
	    fprintf (stderr, "%s: Can't create subdir list\n", dir);
239
240
241
	    ret++;
	    continue;
	}
242
	FcStrSetAdd (processed_dirs, dir);
243
	ret += scanDirs (sublist, config, force, really_force, verbose);
244
245
246
247
248
    }
    FcStrListDone (list);
    return ret;
}

249
250
251
252
253
static FcBool
cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose)
{
    DIR		*d;
    struct dirent *ent;
254
    FcChar8	*dir_base;
255
256
257
258
259
260
    FcBool	ret = FcTrue;
    FcBool	remove;
    FcCache	*cache;
    struct stat	file_stat;
    struct stat	target_stat;

261
    dir_base = FcStrPlus (dir, (FcChar8 *) "/");
262
263
264
265
266
    if (!dir_base)
    {
	fprintf (stderr, "%s: out of memory\n", dir);
	return FcFalse;
    }
267
268
269
    if (access ((char *) dir, W_OK|X_OK) != 0)
    {
	if (verbose)
270
	    printf ("%s: not cleaning unwritable cache directory\n", dir);
271
	FcStrFree (dir_base);
272
273
	return FcTrue;
    }
274
275
    if (verbose)
	printf ("%s: cleaning cache directory\n", dir);
276
    d = opendir ((char *) dir);
277
278
    if (!d)
    {
279
	perror ((char *) dir);
280
	FcStrFree (dir_base);
281
282
283
284
285
	return FcFalse;
    }
    while ((ent = readdir (d)))
    {
	FcChar8	*file_name;
286
	const FcChar8	*target_dir;
287
288
289

	if (ent->d_name[0] == '.')
	    continue;
290
291
292
293
294
295
	/* 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;
	
296
	file_name = FcStrPlus (dir_base, (FcChar8 *) ent->d_name);
297
298
299
300
301
302
	if (!file_name)
	{
	    fprintf (stderr, "%s: allocation failure\n", dir);
	    ret = FcFalse;
	    break;
	}
303
	cache = FcDirCacheLoadFile (file_name, &file_stat);
304
305
306
307
308
309
310
311
312
	if (!cache)
	{
	    fprintf (stderr, "%s: invalid cache file: %s\n", dir, ent->d_name);
	    FcStrFree (file_name);
	    ret = FcFalse;
	    continue;
	}
	target_dir = FcCacheDir (cache);
	remove = FcFalse;
313
	if (stat ((char *) target_dir, &target_stat) < 0)
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
	{
	    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)
	{
329
	    if (unlink ((char *) file_name) < 0)
330
	    {
331
		perror ((char *) file_name);
332
333
334
		ret = FcFalse;
	    }
	}
335
	FcDirCacheUnload (cache);
336
337
338
339
        FcStrFree (file_name);
    }
    
    closedir (d);
340
    FcStrFree (dir_base);
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
    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;
}

365
366
367
368
369
370
371
int
main (int argc, char **argv)
{
    FcStrSet	*dirs;
    FcStrList	*list;
    FcBool    	verbose = FcFalse;
    FcBool	force = FcFalse;
372
    FcBool	really_force = FcFalse;
373
    FcBool	systemOnly = FcFalse;
374
    FcConfig	*config;
Keith Packard's avatar
Keith Packard committed
375
    int		i;
376
    int		ret;
Keith Packard's avatar
Keith Packard committed
377
378
379
380
#if HAVE_GETOPT_LONG || HAVE_GETOPT
    int		c;

#if HAVE_GETOPT_LONG
381
    while ((c = getopt_long (argc, argv, "frsVv?", longopts, NULL)) != -1)
Keith Packard's avatar
Keith Packard committed
382
#else
383
    while ((c = getopt (argc, argv, "frsVv?")) != -1)
Keith Packard's avatar
Keith Packard committed
384
385
386
#endif
    {
	switch (c) {
387
388
389
	case 'r':
	    really_force = FcTrue;
	    /* fall through */
390
391
392
	case 'f':
	    force = FcTrue;
	    break;
393
394
395
	case 's':
	    systemOnly = FcTrue;
	    break;
Keith Packard's avatar
Keith Packard committed
396
397
398
399
400
	case 'V':
	    fprintf (stderr, "fontconfig version %d.%d.%d\n", 
		     FC_MAJOR, FC_MINOR, FC_REVISION);
	    exit (0);
	case 'v':
401
	    verbose = FcTrue;
Keith Packard's avatar
Keith Packard committed
402
403
404
405
406
407
408
409
410
411
	    break;
	default:
	    usage (argv[0]);
	}
    }
    i = optind;
#else
    i = 1;
#endif

412
413
    if (systemOnly)
	FcConfigEnableHome (FcFalse);
414
    config = FcInitLoadConfig ();
415
    if (!config)
Keith Packard's avatar
Keith Packard committed
416
    {
417
	fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
Keith Packard's avatar
Keith Packard committed
418
419
	return 1;
    }
420
    FcConfigSetCurrent (config);
421

Keith Packard's avatar
Keith Packard committed
422
423
    if (argv[i])
    {
424
425
	dirs = FcStrSetCreate ();
	if (!dirs)
Keith Packard's avatar
Keith Packard committed
426
	{
427
428
429
	    fprintf (stderr, "%s: Can't create list of directories\n",
		     argv[0]);
	    return 1;
Keith Packard's avatar
Keith Packard committed
430
	}
431
	while (argv[i])
Keith Packard's avatar
Keith Packard committed
432
	{
433
	    if (!FcStrSetAddFilename (dirs, (FcChar8 *) argv[i]))
Keith Packard's avatar
Keith Packard committed
434
	    {
435
436
		fprintf (stderr, "%s: Can't add directory\n", argv[0]);
		return 1;
Keith Packard's avatar
Keith Packard committed
437
	    }
438
	    i++;
Keith Packard's avatar
Keith Packard committed
439
	}
440
441
	list = FcStrListCreate (dirs);
	FcStrSetDestroy (dirs);
Keith Packard's avatar
Keith Packard committed
442
    }
443
444
    else
	list = FcConfigGetConfigDirs (config);
445
446
447
448
449
450

    if ((processed_dirs = FcStrSetCreate()) == NULL) {
	fprintf(stderr, "Cannot malloc\n");
	return 1;
    }
	
451
    ret = scanDirs (list, config, force, really_force, verbose);
452
453
454

    FcStrSetDestroy (processed_dirs);

455
456
    cleanCacheDirectories (config, verbose);

457
458
459
460
461
462
463
    /* 
     * 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.
     */
464
    FcConfigDestroy (config);
465
    sleep (2);
Keith Packard's avatar
Keith Packard committed
466
467
468
469
    if (verbose)
	printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
    return ret;
}