fc-cache.c 11.1 KB
Newer Older
Keith Packard's avatar
Keith Packard committed
1
/*
2
 * fontconfig/fc-cache/fc-cache.c
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
    {"version", 0, 0, 'V'},
    {"verbose", 0, 0, 'v'},
75
    {"help", 0, 0, 'h'},
Keith Packard's avatar
Keith Packard committed
76 77 78 79 80 81 82 83 84
    {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
static void
86
usage (char *program, int error)
Keith Packard's avatar
Keith Packard committed
87
{
88
    FILE *file = error ? stderr : stdout;
89
#if HAVE_GETOPT_LONG
90
    fprintf (file, "usage: %s [-frsvVh] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
91 92
	     program);
#else
93
    fprintf (file, "usage: %s [-frsvVh] [dirs]\n",
Keith Packard's avatar
Keith Packard committed
94
	     program);
95
#endif
96
    fprintf (file, "Build font information caches in [dirs]\n"
Keith Packard's avatar
Keith Packard committed
97
	     "(all directories in font configuration by default).\n");
98
    fprintf (file, "\n");
99
#if HAVE_GETOPT_LONG
100 101 102 103 104 105
    fprintf (file, "  -f, --force          scan directories with apparently valid caches\n");
    fprintf (file, "  -r, --really-force   erase all existing caches, then rescan\n");
    fprintf (file, "  -s, --system-only    scan system-wide directories only\n");
    fprintf (file, "  -v, --verbose        display status information while busy\n");
    fprintf (file, "  -V, --version        display font config version and exit\n");
    fprintf (file, "  -h, --help           display this help and exit\n");
106
#else
107 108 109 110 111 112
    fprintf (file, "  -f         (force)   scan directories with apparently valid caches\n");
    fprintf (file, "  -r,   (really force) erase all existing caches, then rescan\n");
    fprintf (file, "  -s         (system)  scan system-wide directories only\n");
    fprintf (file, "  -v         (verbose) display status information while busy\n");
    fprintf (file, "  -V         (version) display font config version and exit\n");
    fprintf (file, "  -h         (help)    display this help and exit\n");
113
#endif
114
    exit (error);
Keith Packard's avatar
Keith Packard committed
115 116
}

117 118
static FcStrSet *processed_dirs;

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

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

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

	if (really_force)
	    FcDirCacheUnlink (dir, config);

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

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

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

250 251 252 253 254
static FcBool
cleanCacheDirectory (FcConfig *config, FcChar8 *dir, FcBool verbose)
{
    DIR		*d;
    struct dirent *ent;
255
    FcChar8	*dir_base;
256 257 258 259 260
    FcBool	ret = FcTrue;
    FcBool	remove;
    FcCache	*cache;
    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
    if (access ((char *) dir, W_OK) != 0)
268 269
    {
	if (verbose)
270 271
	    printf ("%s: not cleaning %s cache directory\n", dir,
		    access ((char *) dir, F_OK) == 0 ? "unwritable" : "non-existent");
272
	FcStrFree (dir_base);
273 274
	return FcTrue;
    }
275 276
    if (verbose)
	printf ("%s: cleaning cache directory\n", dir);
277
    d = opendir ((char *) dir);
278 279
    if (!d)
    {
280
	perror ((char *) dir);
281
	FcStrFree (dir_base);
282 283 284 285 286
	return FcFalse;
    }
    while ((ent = readdir (d)))
    {
	FcChar8	*file_name;
287
	const FcChar8	*target_dir;
288 289 290

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

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

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

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

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

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

    FcStrSetDestroy (processed_dirs);

453 454
    cleanCacheDirectories (config, verbose);

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