Debian Codegraph @

src/fcdir.c

raw
1 /*
2 * fontconfig/src/fcdir.c
3 *
4 * Copyright © 2000 Keith Packard
5 *
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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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.
23 */
24
25 #include "fcint.h"
26
27 #ifdef HAVE_DIRENT_H
28 # include <dirent.h>
29 #endif
30
31 #if ENABLE_FONTATIONS
32 # include "fontconfig/fcfontations.h"
33 #endif
34
35 FcBool
36 FcFileIsDir (const FcChar8 *file)
37 {
38 struct stat statb;
39
40 if (FcStat (file, &statb) != 0)
41 return FcFalse;
42 return S_ISDIR (statb.st_mode);
43 }
44
45 FcBool
46 FcFileIsLink (const FcChar8 *file)
47 {
48 #if HAVE_LSTAT
49 struct stat statb;
50
51 if (lstat ((const char *)file, &statb) != 0)
52 return FcFalse;
53 return S_ISLNK (statb.st_mode);
54 #else
55 return FcFalse;
56 #endif
57 }
58
59 FcBool
60 FcFileIsFile (const FcChar8 *file)
61 {
62 struct stat statb;
63
64 if (FcStat (file, &statb) != 0)
65 return FcFalse;
66 return S_ISREG (statb.st_mode);
67 }
68
69 static FcBool
70 FcFileScanFontConfig (FcFontSet *set,
71 const FcChar8 *file,
72 FcConfig *config)
73 {
74 int i;
75 FcBool ret = FcTrue;
76 int old_nfont = set->nfont;
77 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
78
79 if (FcDebug() & FC_DBG_SCAN) {
80 printf ("\tScanning file %s...", file);
81 fflush (stdout);
82 }
83
84 unsigned int (*query_function) (const FcChar8 *, unsigned int, FcBlanks *, int *, FcFontSet *) = FcFreeTypeQueryAll;
85 #if ENABLE_FONTATIONS
86 if (getenv ("FC_FONTATIONS")) {
87 query_function = FcFontationsQueryAll;
88 }
89 #endif
90 if (!query_function (file, -1, NULL, NULL, set))
91 return FcFalse;
92
93 if (FcDebug() & FC_DBG_SCAN)
94 printf ("done\n");
95
96 for (i = old_nfont; i < set->nfont; i++) {
97 FcPattern *font = set->fonts[i];
98
99 /*
100 * Get rid of sysroot here so that targeting scan rule may contains FC_FILE pattern
101 * and they should usually expect without sysroot.
102 */
103 if (sysroot) {
104 size_t len = strlen ((const char *)sysroot);
105 FcChar8 *f = NULL;
106
107 if (FcPatternObjectGetString (font, FC_FILE_OBJECT, 0, &f) == FcResultMatch &&
108 strncmp ((const char *)f, (const char *)sysroot, len) == 0) {
109 FcChar8 *s = FcStrdup (f);
110 FcPatternObjectDel (font, FC_FILE_OBJECT);
111 if (s[len] != '/')
112 len--;
113 else if (s[len + 1] == '/')
114 len++;
115 FcPatternObjectAddString (font, FC_FILE_OBJECT, &s[len]);
116 FcStrFree (s);
117 }
118 }
119
120 /*
121 * Edit pattern with user-defined rules
122 */
123 if (config && !FcConfigSubstitute (config, font, FcMatchScan))
124 ret = FcFalse;
125
126 if (FcDebug() & FC_DBG_SCANV) {
127 printf ("Final font pattern:\n");
128 FcPatternPrint (font);
129 }
130 }
131
132 return ret;
133 }
134
135 FcBool
136 FcFileScanConfig (FcFontSet *set,
137 FcStrSet *dirs,
138 const FcChar8 *file,
139 FcConfig *config)
140 {
141 if (FcFileIsDir (file)) {
142 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
143 const FcChar8 *d = file;
144 size_t len;
145
146 if (sysroot) {
147 len = strlen ((const char *)sysroot);
148 if (strncmp ((const char *)file, (const char *)sysroot, len) == 0) {
149 if (file[len] != '/')
150 len--;
151 else if (file[len + 1] == '/')
152 len++;
153 d = &file[len];
154 }
155 }
156 return FcStrSetAdd (dirs, d);
157 } else {
158 if (set)
159 return FcFileScanFontConfig (set, file, config);
160 else
161 return FcTrue;
162 }
163 }
164
165 FcBool
166 FcFileScan (FcFontSet *set,
167 FcStrSet *dirs,
168 FcFileCache *cache FC_UNUSED,
169 FcBlanks *blanks FC_UNUSED,
170 const FcChar8 *file,
171 FcBool force FC_UNUSED)
172 {
173 FcConfig *config;
174 FcBool ret;
175
176 config = FcConfigReference (NULL);
177 if (!config)
178 return FcFalse;
179 ret = FcFileScanConfig (set, dirs, file, config);
180 FcConfigDestroy (config);
181
182 return ret;
183 }
184
185 /*
186 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
187 */
188 static int
189 cmpstringp (const void *p1, const void *p2)
190 {
191 return strcmp (*(char **)p1, *(char **)p2);
192 }
193
194 FcBool
195 FcDirScanConfig (FcFontSet *set,
196 FcStrSet *dirs,
197 const FcChar8 *dir,
198 FcBool force, /* XXX unused */
199 FcConfig *config)
200 {
201 DIR *d;
202 struct dirent *e;
203 FcStrSet *files;
204 FcChar8 *file_prefix = NULL, *s_dir = NULL;
205 FcChar8 *base;
206 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
207 FcBool ret = FcTrue;
208 int i;
209
210 if (!force)
211 return FcFalse;
212
213 if (!set && !dirs)
214 return FcTrue;
215
216 if (sysroot)
217 s_dir = FcStrBuildFilename (sysroot, dir, NULL);
218 else
219 s_dir = FcStrdup (dir);
220 if (!s_dir) {
221 ret = FcFalse;
222 goto bail;
223 }
224
225 /* freed below */
226 file_prefix = (FcChar8 *)malloc (strlen ((char *)s_dir) + 1 + FC_MAX_FILE_LEN + 1);
227 if (!file_prefix) {
228 ret = FcFalse;
229 goto bail;
230 }
231 strcpy ((char *)file_prefix, (char *)s_dir);
232 strcat ((char *)file_prefix, FC_DIR_SEPARATOR_S);
233 base = file_prefix + strlen ((char *)file_prefix);
234
235 if (FcDebug() & FC_DBG_SCAN)
236 printf ("\tScanning dir %s\n", s_dir);
237
238 d = opendir ((char *)s_dir);
239 if (!d) {
240 /* Don't complain about missing directories */
241 if (errno != ENOENT)
242 ret = FcFalse;
243 goto bail;
244 }
245
246 files = FcStrSetCreateEx (FCSS_ALLOW_DUPLICATES | FCSS_GROW_BY_64);
247 if (!files) {
248 ret = FcFalse;
249 goto bail1;
250 }
251 while ((e = readdir (d))) {
252 /* Ignore . and .. */
253 if (e->d_name[0] == '.' &&
254 (e->d_name[1] == 0 ||
255 (e->d_name[1] == '.' && e->d_name[2] == 0)))
256 continue;
257 if (strlen (e->d_name) < FC_MAX_FILE_LEN) {
258 strcpy ((char *)base, (char *)e->d_name);
259 if (!FcStrSetAdd (files, file_prefix)) {
260 ret = FcFalse;
261 goto bail2;
262 }
263 }
264 }
265
266 /*
267 * Sort files to make things prettier
268 */
269 if (files->num)
270 qsort (files->strs, files->num, sizeof (FcChar8 *), cmpstringp);
271
272 /*
273 * Scan file files to build font patterns
274 */
275 for (i = 0; i < files->num; i++)
276 FcFileScanConfig (set, dirs, files->strs[i], config);
277
278 bail2:
279 FcStrSetDestroy (files);
280 bail1:
281 closedir (d);
282 bail:
283 if (s_dir)
284 free (s_dir);
285 if (file_prefix)
286 free (file_prefix);
287
288 return ret;
289 }
290
291 FcBool
292 FcDirScan (FcFontSet *set,
293 FcStrSet *dirs,
294 FcFileCache *cache FC_UNUSED,
295 FcBlanks *blanks FC_UNUSED,
296 const FcChar8 *dir,
297 FcBool force FC_UNUSED)
298 {
299 FcConfig *config;
300 FcBool ret;
301
302 if (cache || !force)
303 return FcFalse;
304
305 config = FcConfigReference (NULL);
306 if (!config)
307 return FcFalse;
308 ret = FcDirScanConfig (set, dirs, dir, force, config);
309 FcConfigDestroy (config);
310
311 return ret;
312 }
313
314 /*
315 * Scan the specified directory and construct a cache of its contents
316 */
317 FcCache *
318 FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
319 {
320 FcStrSet *dirs;
321 FcFontSet *set;
322 FcCache *cache = NULL;
323 struct stat dir_stat;
324 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
325 FcChar8 *d;
326 #ifndef _WIN32
327 int fd = -1;
328 #endif
329
330 if (sysroot)
331 d = FcStrBuildFilename (sysroot, dir, NULL);
332 else
333 d = FcStrdup (dir);
334
335 if (FcDebug() & FC_DBG_FONTSET)
336 printf ("cache scan dir %s\n", d);
337
338 if (FcStatChecksum (d, &dir_stat) < 0)
339 goto bail;
340
341 set = FcFontSetCreate();
342 if (!set)
343 goto bail;
344
345 dirs = FcStrSetCreateEx (FCSS_GROW_BY_64);
346 if (!dirs)
347 goto bail1;
348
349 #ifndef _WIN32
350 fd = FcDirCacheLock (dir, config);
351 #endif
352 /*
353 * Scan the dir
354 */
355 /* Do not pass sysroot here. FcDirScanConfig() do take care of it */
356 if (!FcDirScanConfig (set, dirs, dir, FcTrue, config))
357 goto bail2;
358
359 /*
360 * Build the cache object
361 */
362 cache = FcDirCacheBuild (set, dir, &dir_stat, dirs);
363 if (!cache)
364 goto bail2;
365
366 /*
367 * Write out the cache file, ignoring any troubles
368 */
369 FcDirCacheWrite (cache, config);
370
371 bail2:
372 #ifndef _WIN32
373 FcDirCacheUnlock (fd);
374 #endif
375 FcStrSetDestroy (dirs);
376 bail1:
377 FcFontSetDestroy (set);
378 bail:
379 FcStrFree (d);
380
381 return cache;
382 }
383
384 FcCache *
385 FcDirCacheRescan (const FcChar8 *dir, FcConfig *config)
386 {
387 FcCache *cache;
388 FcCache *newp = NULL;
389 struct stat dir_stat;
390 FcStrSet *dirs;
391 const FcChar8 *sysroot;
392 FcChar8 *d = NULL;
393 #ifndef _WIN32
394 int fd = -1;
395 #endif
396
397 config = FcConfigReference (config);
398 if (!config)
399 return NULL;
400 sysroot = FcConfigGetSysRoot (config);
401 cache = FcDirCacheLoad (dir, config, NULL);
402 if (!cache)
403 goto bail;
404
405 if (sysroot)
406 d = FcStrBuildFilename (sysroot, dir, NULL);
407 else
408 d = FcStrdup (dir);
409 if (FcStatChecksum (d, &dir_stat) < 0)
410 goto bail;
411 dirs = FcStrSetCreateEx (FCSS_GROW_BY_64);
412 if (!dirs)
413 goto bail;
414
415 #ifndef _WIN32
416 fd = FcDirCacheLock (dir, config);
417 #endif
418 /*
419 * Scan the dir
420 */
421 /* Do not pass sysroot here. FcDirScanConfig() do take care of it */
422 if (!FcDirScanConfig (NULL, dirs, dir, FcTrue, config))
423 goto bail1;
424 /*
425 * Rebuild the cache object
426 */
427 newp = FcDirCacheRebuild (cache, &dir_stat, dirs);
428 if (!newp)
429 goto bail1;
430 FcDirCacheUnload (cache);
431 /*
432 * Write out the cache file, ignoring any troubles
433 */
434 FcDirCacheWrite (newp, config);
435
436 bail1:
437 #ifndef _WIN32
438 FcDirCacheUnlock (fd);
439 #endif
440 FcStrSetDestroy (dirs);
441 bail:
442 if (d)
443 FcStrFree (d);
444 FcConfigDestroy (config);
445
446 return newp;
447 }
448
449 /*
450 * Read (or construct) the cache for a directory
451 */
452 FcCache *
453 FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
454 {
455 FcCache *cache = NULL;
456
457 config = FcConfigReference (config);
458 /* Try to use existing cache file */
459 if (!force)
460 cache = FcDirCacheLoad (dir, config, NULL);
461
462 /* Not using existing cache file, construct new cache */
463 if (!cache) {
464 FcDirCacheDeleteUUID (dir, config);
465 cache = FcDirCacheScan (dir, config);
466 }
467 FcConfigDestroy (config);
468
469 return cache;
470 }
471
472 FcBool
473 FcDirSave (FcFontSet *set FC_UNUSED, FcStrSet *dirs FC_UNUSED, const FcChar8 *dir FC_UNUSED)
474 {
475 return FcFalse; /* XXX deprecated */
476 }
477 #define __fcdir__
478 #include "fcaliastail.h"
479 #undef __fcdir__