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,
69 int old_nfont = set->nfont;
70 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
72 if (FcDebug () & FC_DBG_SCAN)
74 printf ("\tScanning file %s...", file);
78 if (!FcFreeTypeQueryAll (file, -1, NULL, NULL, set))
81 if (FcDebug () & FC_DBG_SCAN)
84 for (i = old_nfont; i < set->nfont; i++)
86 FcPattern *font = set->fonts[i];
89 * Get rid of sysroot here so that targeting scan rule may contains FC_FILE pattern
90 * and they should usually expect without sysroot.
94 size_t len = strlen ((const char *)sysroot);
97 if (FcPatternObjectGetString (font, FC_FILE_OBJECT, 0, &f) == FcResultMatch &&
98 strncmp ((const char *)f, (const char *)sysroot, len) == 0)
100 FcChar8 *s = FcStrdup (f);
101 FcPatternObjectDel (font, FC_FILE_OBJECT);
104 else if (s[len+1] == '/')
106 FcPatternObjectAddString (font, FC_FILE_OBJECT, &s[len]);
112 * Edit pattern with user-defined rules
114 if (config && !FcConfigSubstitute (config, font, FcMatchScan))
117 if (FcDebug() & FC_DBG_SCANV)
119 printf ("Final font pattern:\n");
120 FcPatternPrint (font);
128 FcFileScanConfig (FcFontSet *set,
133 if (FcFileIsDir (file))
135 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
136 const FcChar8 *d = file;
141 len = strlen ((const char *)sysroot);
142 if (strncmp ((const char *)file, (const char *)sysroot, len) == 0)
144 if (file[len] != '/')
146 else if (file[len+1] == '/')
151 return FcStrSetAdd (dirs, d);
156 return FcFileScanFontConfig (set, file, config);
163 FcFileScan (FcFontSet *set,
165 FcFileCache *cache FC_UNUSED,
166 FcBlanks *blanks FC_UNUSED,
168 FcBool force FC_UNUSED)
170 return FcFileScanConfig (set, dirs, file, FcConfigGetCurrent ());
174 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
177 cmpstringp(const void *p1, const void *p2)
179 return strcmp(* (char **) p1, * (char **) p2);
183 FcDirScanConfig (FcFontSet *set,
186 FcBool force, /* XXX unused */
204 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
210 strcpy ((char *) file, (char *) dir);
211 strcat ((char *) file, "/");
212 base = file + strlen ((char *) file);
214 if (FcDebug () & FC_DBG_SCAN)
215 printf ("\tScanning dir %s\n", dir);
217 d = opendir ((char *) dir);
220 /* Don't complain about missing directories */
226 files = FcStrSetCreateEx (FCSS_ALLOW_DUPLICATES | FCSS_GROW_BY_64);
232 while ((e = readdir (d)))
234 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
236 strcpy ((char *) base, (char *) e->d_name);
237 if (!FcStrSetAdd (files, file)) {
245 * Sort files to make things prettier
247 qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp);
250 * Scan file files to build font patterns
252 for (i = 0; i < files->num; i++)
253 FcFileScanConfig (set, dirs, files->strs[i], config);
256 FcStrSetDestroy (files);
267 FcDirScan (FcFontSet *set,
269 FcFileCache *cache FC_UNUSED,
270 FcBlanks *blanks FC_UNUSED,
272 FcBool force FC_UNUSED)
277 return FcDirScanConfig (set, dirs, dir, force, FcConfigGetCurrent ());
281 * Scan the specified directory and construct a cache of its contents
284 FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
288 FcCache *cache = NULL;
289 struct stat dir_stat;
290 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
297 d = FcStrBuildFilename (sysroot, dir, NULL);
301 if (FcDebug () & FC_DBG_FONTSET)
302 printf ("cache scan dir %s\n", d);
304 if (FcStatChecksum (d, &dir_stat) < 0)
307 set = FcFontSetCreate();
311 dirs = FcStrSetCreateEx (FCSS_GROW_BY_64);
316 fd = FcDirCacheLock (dir, config);
321 if (!FcDirScanConfig (set, dirs, d, FcTrue, config))
325 * Build the cache object
327 cache = FcDirCacheBuild (set, dir, &dir_stat, dirs);
332 * Write out the cache file, ignoring any troubles
334 FcDirCacheWrite (cache, config);
338 FcDirCacheUnlock (fd);
340 FcStrSetDestroy (dirs);
342 FcFontSetDestroy (set);
350 FcDirCacheRescan (const FcChar8 *dir, FcConfig *config)
354 struct stat dir_stat;
356 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
362 cache = FcDirCacheLoad (dir, config, NULL);
367 d = FcStrBuildFilename (sysroot, dir, NULL);
370 if (FcStatChecksum (d, &dir_stat) < 0)
372 dirs = FcStrSetCreateEx (FCSS_GROW_BY_64);
377 fd = FcDirCacheLock (dir, config);
382 if (!FcDirScanConfig (NULL, dirs, d, FcTrue, config))
385 * Rebuild the cache object
387 new = FcDirCacheRebuild (cache, &dir_stat, dirs);
390 FcDirCacheUnload (cache);
392 * Write out the cache file, ignoring any troubles
394 FcDirCacheWrite (new, config);
398 FcDirCacheUnlock (fd);
400 FcStrSetDestroy (dirs);
409 * Read (or construct) the cache for a directory
412 FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
414 FcCache *cache = NULL;
416 FcDirCacheCreateUUID ((FcChar8 *) dir, FcFalse, config);
417 /* Try to use existing cache file */
419 cache = FcDirCacheLoad (dir, config, NULL);
421 /* Not using existing cache file, construct new cache */
423 cache = FcDirCacheScan (dir, config);
426 FcFontSet *fs = FcCacheSet (cache);
428 if (cache->dirs_count == 0 && (!fs || fs->nfont == 0))
429 FcDirCacheDeleteUUID (dir, config);
436 FcDirSave (FcFontSet *set FC_UNUSED, FcStrSet * dirs FC_UNUSED, const FcChar8 *dir FC_UNUSED)
438 return FcFalse; /* XXX deprecated */
441 #include "fcaliastail.h"