]>
git.wh0rd.org - fontconfig.git/blob - fc-cache/fc-cache.c
2 * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $
4 * Copyright © 2002 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.
29 #define HAVE_GETOPT_LONG 1
38 #include <sys/types.h>
47 #define sleep(x) Sleep((x) * 1000)
58 #ifndef HAVE_GETOPT_LONG
59 #define HAVE_GETOPT_LONG 0
66 const struct option longopts
[] = {
68 {"really-force", 0, 0, 'r'},
69 {"system-only", 0, 0, 's'},
70 {"version", 0, 0, 'V'},
71 {"verbose", 0, 0, 'v'},
78 extern int optind
, opterr
, optopt
;
86 fprintf (stderr
, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
89 fprintf (stderr
, "usage: %s [-frsvV?] [dirs]\n",
92 fprintf (stderr
, "Build font information caches in [dirs]\n"
93 "(all directories in font configuration by default).\n");
94 fprintf (stderr
, "\n");
96 fprintf (stderr
, " -f, --force scan directories with apparently valid caches\n");
97 fprintf (stderr
, " -r, --really-force erase all existing caches, then rescan\n");
98 fprintf (stderr
, " -s, --system-only scan system-wide directories only\n");
99 fprintf (stderr
, " -v, --verbose display status information while busy\n");
100 fprintf (stderr
, " -V, --version display font config version and exit\n");
101 fprintf (stderr
, " -?, --help display this help and exit\n");
103 fprintf (stderr
, " -f (force) scan directories with apparently valid caches\n");
104 fprintf (stderr
, " -r, (really force) erase all existing caches, then rescan\n");
105 fprintf (stderr
, " -s (system) scan system-wide directories only\n");
106 fprintf (stderr
, " -v (verbose) display status information while busy\n");
107 fprintf (stderr
, " -V (version) display font config version and exit\n");
108 fprintf (stderr
, " -? (help) display this help and exit\n");
113 static FcStrSet
*processed_dirs
;
116 nsubdirs (FcStrSet
*set
)
121 list
= FcStrListCreate (set
);
124 while (FcStrListNext (list
))
126 FcStrListDone (list
);
131 scanDirs (FcStrList
*list
, FcConfig
*config
, FcBool force
, FcBool really_force
, FcBool verbose
)
144 * Now scan all of the directories into separate databases
145 * and write out the results
147 while ((dir
= FcStrListNext (list
)))
151 printf ("%s: ", dir
);
158 printf ("skipping, no such directory\n");
162 if (FcStrSetMember (processed_dirs
, dir
))
165 printf ("skipping, looped directory detected\n");
169 if (access ((char *) dir
, W_OK
) < 0)
175 printf ("skipping, no such directory\n");
179 /* That's ok, caches go to /var anyway. */
180 /* Ideally we'd do an access on the hashed_name. */
181 /* But we hid that behind an abstraction barrier. */
184 fprintf (stderr
, "\"%s\": ", dir
);
191 if (stat ((char *) dir
, &statb
) == -1)
193 fprintf (stderr
, "\"%s\": ", dir
);
198 if (!S_ISDIR (statb
.st_mode
))
200 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
205 FcDirCacheUnlink (dir
, config
);
210 cache
= FcDirCacheLoad (dir
, config
, NULL
);
217 cache
= FcDirCacheRead (dir
, FcTrue
, config
);
220 fprintf (stderr
, "%s: error scanning\n", dir
);
226 set
= FcCacheSet (cache
);
231 printf ("skipping, %d fonts, %d dirs\n",
232 set
->nfont
, cache
->dirs_count
);
237 printf ("caching, %d fonts, %d dirs\n",
238 set
->nfont
, cache
->dirs_count
);
240 if (!FcDirCacheValid (dir
))
242 fprintf (stderr
, "%s: failed to write cache\n", dir
);
243 (void) FcDirCacheUnlink (dir
, config
);
248 subdirs
= FcStrSetCreate ();
251 fprintf (stderr
, "%s: Can't create subdir set\n", dir
);
253 FcDirCacheUnload (cache
);
256 for (i
= 0; i
< cache
->dirs_count
; i
++)
257 FcStrSetAdd (subdirs
, FcCacheSubdir (cache
, i
));
259 FcDirCacheUnload (cache
);
261 sublist
= FcStrListCreate (subdirs
);
262 FcStrSetDestroy (subdirs
);
265 fprintf (stderr
, "%s: Can't create subdir list\n", dir
);
267 FcDirCacheUnload (cache
);
270 FcStrSetAdd (processed_dirs
, dir
);
271 ret
+= scanDirs (sublist
, config
, force
, really_force
, verbose
);
273 FcStrListDone (list
);
278 cleanCacheDirectory (FcConfig
*config
, FcChar8
*dir
, FcBool verbose
)
286 struct stat file_stat
;
287 struct stat target_stat
;
289 dir_base
= FcStrPlus (dir
, "/");
290 if (access ((char *) dir
, W_OK
|X_OK
) != 0)
293 printf ("%s: not cleaning unwritable cache directory\n", dir
);
297 printf ("%s: cleaning cache directory\n", dir
);
304 while ((ent
= readdir (d
)))
309 if (ent
->d_name
[0] == '.')
311 file_name
= FcStrPlus (dir_base
, ent
->d_name
);
314 fprintf (stderr
, "%s: allocation failure\n", dir
);
318 cache
= FcDirCacheLoadFile (file_name
, &file_stat
);
321 fprintf (stderr
, "%s: invalid cache file: %s\n", dir
, ent
->d_name
);
322 FcStrFree (file_name
);
326 target_dir
= FcCacheDir (cache
);
328 if (stat (target_dir
, &target_stat
) < 0)
331 printf ("%s: %s: missing directory: %s \n",
332 dir
, ent
->d_name
, target_dir
);
335 else if (target_stat
.st_mtime
> file_stat
.st_mtime
)
338 printf ("%s: %s: cache outdated: %s\n",
339 dir
, ent
->d_name
, target_dir
);
344 if (unlink (file_name
) < 0)
350 FcStrFree (file_name
);
358 cleanCacheDirectories (FcConfig
*config
, FcBool verbose
)
360 FcStrList
*cache_dirs
= FcConfigGetCacheDirs (config
);
366 while ((cache_dir
= FcStrListNext (cache_dirs
)))
368 if (!cleanCacheDirectory (config
, cache_dir
, verbose
))
374 FcStrListDone (cache_dirs
);
379 main (int argc
, char **argv
)
383 FcBool verbose
= FcFalse
;
384 FcBool force
= FcFalse
;
385 FcBool really_force
= FcFalse
;
386 FcBool systemOnly
= FcFalse
;
390 #if HAVE_GETOPT_LONG || HAVE_GETOPT
394 while ((c
= getopt_long (argc
, argv
, "frsVv?", longopts
, NULL
)) != -1)
396 while ((c
= getopt (argc
, argv
, "frsVv?")) != -1)
401 really_force
= FcTrue
;
410 fprintf (stderr
, "fontconfig version %d.%d.%d\n",
411 FC_MAJOR
, FC_MINOR
, FC_REVISION
);
426 FcConfigEnableHome (FcFalse
);
427 config
= FcInitLoadConfig ();
430 fprintf (stderr
, "%s: Can't init font config library\n", argv
[0]);
433 FcConfigSetCurrent (config
);
437 dirs
= FcStrSetCreate ();
440 fprintf (stderr
, "%s: Can't create list of directories\n",
446 if (!FcStrSetAdd (dirs
, (FcChar8
*) argv
[i
]))
448 fprintf (stderr
, "%s: Can't add directory\n", argv
[0]);
453 list
= FcStrListCreate (dirs
);
454 FcStrSetDestroy (dirs
);
457 list
= FcConfigGetConfigDirs (config
);
459 if ((processed_dirs
= FcStrSetCreate()) == NULL
) {
460 fprintf(stderr
, "Cannot malloc\n");
464 ret
= scanDirs (list
, config
, force
, really_force
, verbose
);
466 FcStrSetDestroy (processed_dirs
);
468 cleanCacheDirectories (config
, verbose
);
471 * Now we need to sleep a second (or two, to be extra sure), to make
472 * sure that timestamps for changes after this run of fc-cache are later
473 * then any timestamps we wrote. We don't use gettimeofday() because
474 * sleep(3) can't be interrupted by a signal here -- this isn't in the
475 * library, and there aren't any signals flying around here.
477 FcConfigDestroy (config
);
480 printf ("%s: %s\n", argv
[0], ret
? "failed" : "succeeded");