]>
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
)
136 * Now scan all of the directories into separate databases
137 * and write out the results
139 while ((dir
= FcStrListNext (list
)))
143 printf ("%s: \"%s\": ", program
, dir
);
150 printf ("skipping, no such directory\n");
154 if (FcStrSetMember (processed_dirs
, dir
))
157 printf ("skipping, looped directory detected\n");
161 set
= FcFontSetCreate ();
164 fprintf (stderr
, "%s: Can't create font set\n", dir
);
168 subdirs
= FcStrSetCreate ();
171 fprintf (stderr
, "%s: Can't create directory set\n", dir
);
173 FcFontSetDestroy (set
);
177 if (access ((char *) dir
, W_OK
) < 0)
183 printf ("skipping, no such directory\n");
184 FcFontSetDestroy (set
);
185 FcStrSetDestroy (subdirs
);
189 /* That's ok, caches go to /var anyway. */
190 /* Ideally we'd do an access on the hashed_name. */
191 /* But we hid that behind an abstraction barrier. */
194 fprintf (stderr
, "\"%s\": ", dir
);
198 FcFontSetDestroy (set
);
199 FcStrSetDestroy (subdirs
);
203 if (stat ((char *) dir
, &statb
) == -1)
205 fprintf (stderr
, "\"%s\": ", dir
);
207 FcFontSetDestroy (set
);
208 FcStrSetDestroy (subdirs
);
212 if (!S_ISDIR (statb
.st_mode
))
214 fprintf (stderr
, "\"%s\": not a directory, skipping\n", dir
);
215 FcFontSetDestroy (set
);
216 FcStrSetDestroy (subdirs
);
221 FcDirCacheUnlink (dir
, config
);
224 was_valid
= FcDirCacheValid (dir
, config
);
226 if (!FcDirScanConfig (set
, subdirs
, FcConfigGetBlanks (config
), dir
, force
, config
))
228 fprintf (stderr
, "%s: error scanning\n", dir
);
229 FcFontSetDestroy (set
);
230 FcStrSetDestroy (subdirs
);
234 if (!force
&& was_valid
)
237 printf ("skipping, %d fonts, %d dirs\n",
238 set
->nfont
, nsubdirs(subdirs
));
243 printf ("caching, %d fonts, %d dirs\n",
244 set
->nfont
, nsubdirs (subdirs
));
246 if (!FcDirCacheValid (dir
, config
))
248 fprintf (stderr
, "%s: failed to write cache\n", dir
);
249 (void) FcDirCacheUnlink (dir
, config
);
253 FcFontSetDestroy (set
);
254 sublist
= FcStrListCreate (subdirs
);
255 FcStrSetDestroy (subdirs
);
258 fprintf (stderr
, "%s: Can't create subdir list\n", dir
);
262 FcStrSetAdd (processed_dirs
, dir
);
263 ret
+= scanDirs (sublist
, config
, program
, force
, really_force
, verbose
);
265 FcStrListDone (list
);
270 main (int argc
, char **argv
)
274 FcBool verbose
= FcFalse
;
275 FcBool force
= FcFalse
;
276 FcBool really_force
= FcFalse
;
277 FcBool systemOnly
= FcFalse
;
281 #if HAVE_GETOPT_LONG || HAVE_GETOPT
285 while ((c
= getopt_long (argc
, argv
, "frsVv?", longopts
, NULL
)) != -1)
287 while ((c
= getopt (argc
, argv
, "frsVv?")) != -1)
292 really_force
= FcTrue
;
301 fprintf (stderr
, "fontconfig version %d.%d.%d\n",
302 FC_MAJOR
, FC_MINOR
, FC_REVISION
);
317 FcConfigEnableHome (FcFalse
);
318 config
= FcInitLoadConfig ();
321 fprintf (stderr
, "%s: Can't init font config library\n", argv
[0]);
324 FcConfigSetCurrent (config
);
328 dirs
= FcStrSetCreate ();
331 fprintf (stderr
, "%s: Can't create list of directories\n",
337 if (!FcStrSetAdd (dirs
, (FcChar8
*) argv
[i
]))
339 fprintf (stderr
, "%s: Can't add directory\n", argv
[0]);
344 list
= FcStrListCreate (dirs
);
345 FcStrSetDestroy (dirs
);
348 list
= FcConfigGetConfigDirs (config
);
350 if ((processed_dirs
= FcStrSetCreate()) == NULL
) {
351 fprintf(stderr
, "Cannot malloc\n");
355 ret
= scanDirs (list
, config
, argv
[0], force
, really_force
, verbose
);
357 FcStrSetDestroy (processed_dirs
);
360 * Now we need to sleep a second (or two, to be extra sure), to make
361 * sure that timestamps for changes after this run of fc-cache are later
362 * then any timestamps we wrote. We don't use gettimeofday() because
363 * sleep(3) can't be interrupted by a signal here -- this isn't in the
364 * library, and there aren't any signals flying around here.
366 FcConfigDestroy (config
);
369 printf ("%s: %s\n", argv
[0], ret
? "failed" : "succeeded");