2 * fontconfig/src/fcdir.c
4 * Copyright © 2000 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 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.
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.
29 FcFileIsDir (const FcChar8 *file)
33 if (FcStat (file, &statb) != 0)
35 return S_ISDIR(statb.st_mode);
39 FcFileIsLink (const FcChar8 *file)
44 if (lstat ((const char *)file, &statb) != 0)
46 return S_ISLNK (statb.st_mode);
53 FcFileIsFile (const FcChar8 *file)
57 if (FcStat (file, &statb) != 0)
59 return S_ISREG (statb.st_mode);
63 FcFileScanFontConfig (FcFontSet *set,
72 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
79 * Nothing in the cache, scan the file
81 if (FcDebug () & FC_DBG_SCAN)
83 printf ("\tScanning file %s...", file);
86 font = FcFreeTypeQuery (file, id, blanks, &count);
87 if (FcDebug () & FC_DBG_SCAN)
90 * Get rid of sysroot here so that targeting scan rule may contains FC_FILE pattern
91 * and they should usually expect without sysroot.
95 size_t len = strlen ((const char *)sysroot);
98 if (FcPatternObjectGetString (font, FC_FILE_OBJECT, 0, &f) == FcResultMatch &&
99 strncmp ((const char *)f, (const char *)sysroot, len) == 0)
101 FcChar8 *s = FcStrdup (f);
102 FcPatternObjectDel (font, FC_FILE_OBJECT);
105 else if (s[len+1] == '/')
107 FcPatternObjectAddString (font, FC_FILE_OBJECT, &s[len]);
113 * Edit pattern with user-defined rules
115 if (font && config && !FcConfigSubstitute (config, font, FcMatchScan))
117 FcPatternDestroy (font);
127 if (FcDebug() & FC_DBG_SCANV)
129 printf ("Final font pattern:\n");
130 FcPatternPrint (font);
132 if (!FcFontSetAdd (set, font))
134 FcPatternDestroy (font);
140 FcPatternDestroy (font);
142 } while (font && ret && id < count);
147 FcFileScanConfig (FcFontSet *set,
153 if (FcFileIsDir (file))
155 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
156 const FcChar8 *d = file;
161 len = strlen ((const char *)sysroot);
162 if (strncmp ((const char *)file, (const char *)sysroot, len) == 0)
164 if (file[len] != '/')
166 else if (file[len+1] == '/')
171 return FcStrSetAdd (dirs, d);
176 return FcFileScanFontConfig (set, blanks, file, config);
183 FcFileScan (FcFontSet *set,
185 FcFileCache *cache FC_UNUSED,
188 FcBool force FC_UNUSED)
190 return FcFileScanConfig (set, dirs, blanks, file, FcConfigGetCurrent ());
194 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
197 cmpstringp(const void *p1, const void *p2)
199 return strcmp(* (char **) p1, * (char **) p2);
203 FcDirScanConfig (FcFontSet *set,
207 FcBool force, /* XXX unused */
225 if (!blanks && !scanOnly)
226 blanks = FcConfigGetBlanks (config);
229 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
235 strcpy ((char *) file, (char *) dir);
236 strcat ((char *) file, "/");
237 base = file + strlen ((char *) file);
239 if (FcDebug () & FC_DBG_SCAN)
240 printf ("\tScanning dir %s\n", dir);
242 d = opendir ((char *) dir);
245 /* Don't complain about missing directories */
251 files = FcStrSetCreate ();
257 while ((e = readdir (d)))
259 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
261 strcpy ((char *) base, (char *) e->d_name);
262 if (!FcStrSetAdd (files, file)) {
270 * Sort files to make things prettier
272 qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp);
275 * Scan file files to build font patterns
277 for (i = 0; i < files->num; i++)
281 if (FcFileIsDir (files->strs[i]))
282 FcFileScanConfig (NULL, dirs, NULL, files->strs[i], config);
286 FcFileScanConfig (set, dirs, blanks, files->strs[i], config);
291 FcStrSetDestroy (files);
302 FcDirScan (FcFontSet *set,
304 FcFileCache *cache, /* XXX unused */
307 FcBool force /* XXX unused */)
312 return FcDirScanConfig (set, dirs, blanks, dir, force, FcConfigGetCurrent (), FcFalse);
316 FcDirScanOnly (FcStrSet *dirs,
320 return FcDirScanConfig (NULL, dirs, NULL, dir, FcTrue, config, FcTrue);
324 * Scan the specified directory and construct a cache of its contents
327 FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
331 FcCache *cache = NULL;
332 struct stat dir_stat;
333 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
337 d = FcStrBuildFilename (sysroot, dir, NULL);
341 if (FcDebug () & FC_DBG_FONTSET)
342 printf ("cache scan dir %s\n", d);
344 if (FcStatChecksum (d, &dir_stat) < 0)
347 set = FcFontSetCreate();
351 dirs = FcStrSetCreate ();
358 if (!FcDirScanConfig (set, dirs, NULL, d, FcTrue, config, FcFalse))
362 * Build the cache object
364 cache = FcDirCacheBuild (set, dir, &dir_stat, dirs);
369 * Write out the cache file, ignoring any troubles
371 FcDirCacheWrite (cache, config);
374 FcStrSetDestroy (dirs);
376 FcFontSetDestroy (set);
384 FcDirCacheRescan (const FcChar8 *dir, FcConfig *config)
388 struct stat dir_stat;
390 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
393 cache = FcDirCacheLoad (dir, config, NULL);
398 d = FcStrBuildFilename (sysroot, dir, NULL);
401 if (FcStatChecksum (d, &dir_stat) < 0)
403 dirs = FcStrSetCreate ();
410 if (!FcDirScanConfig (NULL, dirs, NULL, d, FcTrue, config, FcFalse))
413 * Rebuild the cache object
415 new = FcDirCacheRebuild (cache, &dir_stat, dirs);
418 FcDirCacheUnload (cache);
420 * Write out the cache file, ignoring any troubles
422 FcDirCacheWrite (new, config);
425 FcStrSetDestroy (dirs);
434 * Read (or construct) the cache for a directory
437 FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
439 FcCache *cache = NULL;
441 /* Try to use existing cache file */
443 cache = FcDirCacheLoad (dir, config, NULL);
445 /* Not using existing cache file, construct new cache */
447 cache = FcDirCacheScan (dir, config);
453 FcDirSave (FcFontSet *set FC_UNUSED, FcStrSet * dirs FC_UNUSED, const FcChar8 *dir FC_UNUSED)
455 return FcFalse; /* XXX deprecated */
458 #include "fcaliastail.h"