]>
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
34 #include <fontconfig/fontconfig.h>
38 #include <sys/types.h>
45 #define sleep(x) Sleep((x) * 1000)
52 #ifndef HAVE_GETOPT_LONG
53 #define HAVE_GETOPT_LONG 0
60 const struct option longopts
[] = {
62 {"really-force", 0, 0, 'r'},
63 {"system-only", 0, 0, 's'},
64 {"version", 0, 0, 'V'},
65 {"verbose", 0, 0, 'v'},
72 extern int optind
, opterr
, optopt
;
80 fprintf (stderr
, "usage: %s [-frsvV?] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
83 fprintf (stderr
, "usage: %s [-frsvV?] [dirs]\n",
86 fprintf (stderr
, "Build font information caches in [dirs]\n"
87 "(all directories in font configuration by default).\n");
88 fprintf (stderr
, "\n");
90 fprintf (stderr
, " -f, --force scan directories with apparently valid caches\n");
91 fprintf (stderr
, " -r, --really-force erase all existing caches, then rescan\n");
92 fprintf (stderr
, " -s, --system-only scan system-wide directories only\n");
93 fprintf (stderr
, " -v, --verbose display status information while busy\n");
94 fprintf (stderr
, " -V, --version display font config version and exit\n");
95 fprintf (stderr
, " -?, --help display this help and exit\n");
97 fprintf (stderr
, " -f (force) scan directories with apparently valid caches\n");
98 fprintf (stderr
, " -r, (really force) erase all existing caches, then rescan\n");
99 fprintf (stderr
, " -s (system) scan system-wide directories only\n");
100 fprintf (stderr
, " -v (verbose) display status information while busy\n");
101 fprintf (stderr
, " -V (version) display font config version and exit\n");
102 fprintf (stderr
, " -? (help) display this help and exit\n");
107 static FcStrSet
*processed_dirs
;
110 nsubdirs (FcStrSet
*set
)
115 list
= FcStrListCreate (set
);
118 while (FcStrListNext (list
))
120 FcStrListDone (list
);
125 scanDirs (FcStrList
*list
, FcConfig
*config
, char *program
, FcBool force
, FcBool really_force
, FcBool verbose
)
135 * Now scan all of the directories into separate databases
136 * and write out the results
138 while ((dir
= FcStrListNext (list
)))
142 printf ("%s: \"%s\": ", program
, dir
);
149 printf ("skipping, no such directory\n");
153 if (FcStrSetMember (processed_dirs
, dir
))
156 printf ("skipping, looped directory detected\n");
160 set
= FcFontSetCreate ();
163 fprintf (stderr
, "Can't create font set\n");
167 subdirs
= FcStrSetCreate ();
170 fprintf (stderr
, "Can't create directory set\n");
172 FcFontSetDestroy (set
);
176 if (access ((char *) dir
, W_OK
) < 0)
182 printf ("skipping, no such directory\n");
183 FcFontSetDestroy (set
);
184 FcStrSetDestroy (subdirs
);
188 /* That's ok, caches go to /var anyway. */
189 /* Ideally we'd do an access on the hashed_name. */
190 /* But we hid that behind an abstraction barrier. */
193 fprintf (stderr
, "\"%s\": ", dir
);
197 FcFontSetDestroy (set
);
198 FcStrSetDestroy (subdirs
);
202 if (stat ((char *) dir
, &statb
) == -1)
204 fprintf (stderr
, "\"%s\": ", dir
);
206 FcFontSetDestroy (set
);
207 FcStrSetDestroy (subdirs
);
211 if (!S_ISDIR (statb
.st_mode
))
213 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
214 FcFontSetDestroy (set
);
215 FcStrSetDestroy (subdirs
);
220 FcDirCacheUnlink (dir
, config
);
222 if (!FcDirScanConfig (set
, subdirs
, FcConfigGetBlanks (config
), dir
, force
, config
))
224 fprintf (stderr
, "\"%s\": error scanning\n", dir
);
225 FcFontSetDestroy (set
);
226 FcStrSetDestroy (subdirs
);
230 if (!force
&& FcDirCacheValid (dir
, config
))
233 printf ("skipping, %d fonts, %d dirs\n",
234 set
->nfont
, nsubdirs(subdirs
));
239 printf ("caching, %d fonts, %d dirs\n",
240 set
->nfont
, nsubdirs (subdirs
));
242 /* This is the only reason we can't combine
243 * Valid w/HasCurrentArch... */
244 if (!FcDirCacheValid (dir
, config
))
245 if (!FcDirCacheUnlink (dir
, config
))
248 if (!FcDirSave (set
, subdirs
, dir
))
250 fprintf (stderr
, "Can't save cache for \"%s\"\n", dir
);
254 FcFontSetDestroy (set
);
255 sublist
= FcStrListCreate (subdirs
);
256 FcStrSetDestroy (subdirs
);
259 fprintf (stderr
, "Can't create subdir list in \"%s\"\n", dir
);
263 FcStrSetAdd (processed_dirs
, dir
);
264 ret
+= scanDirs (sublist
, config
, program
, force
, really_force
, verbose
);
266 FcStrListDone (list
);
271 main (int argc
, char **argv
)
275 FcBool verbose
= FcFalse
;
276 FcBool force
= FcFalse
;
277 FcBool really_force
= FcFalse
;
278 FcBool systemOnly
= FcFalse
;
282 #if HAVE_GETOPT_LONG || HAVE_GETOPT
286 while ((c
= getopt_long (argc
, argv
, "frsVv?", longopts
, NULL
)) != -1)
288 while ((c
= getopt (argc
, argv
, "frsVv?")) != -1)
293 really_force
= FcTrue
;
302 fprintf (stderr
, "fontconfig version %d.%d.%d\n",
303 FC_MAJOR
, FC_MINOR
, FC_REVISION
);
318 FcConfigEnableHome (FcFalse
);
319 config
= FcInitLoadConfig ();
322 fprintf (stderr
, "%s: Can't init font config library\n", argv
[0]);
325 FcConfigSetCurrent (config
);
329 dirs
= FcStrSetCreate ();
332 fprintf (stderr
, "%s: Can't create list of directories\n",
338 if (!FcStrSetAdd (dirs
, (FcChar8
*) argv
[i
]))
340 fprintf (stderr
, "%s: Can't add directory\n", argv
[0]);
345 list
= FcStrListCreate (dirs
);
346 FcStrSetDestroy (dirs
);
349 list
= FcConfigGetConfigDirs (config
);
351 if ((processed_dirs
= FcStrSetCreate()) == NULL
) {
352 fprintf(stderr
, "Cannot malloc\n");
356 ret
= scanDirs (list
, config
, argv
[0], force
, really_force
, verbose
);
358 FcStrSetDestroy (processed_dirs
);
361 * Now we need to sleep a second (or two, to be extra sure), to make
362 * sure that timestamps for changes after this run of fc-cache are later
363 * then any timestamps we wrote. We don't use gettimeofday() because
364 * sleep(3) can't be interrupted by a signal here -- this isn't in the
365 * library, and there aren't any signals flying around here.
367 FcConfigDestroy (config
);
370 printf ("%s: %s\n", argv
[0], ret
? "failed" : "succeeded");