]> git.wh0rd.org - fontconfig.git/blob - src/fcdir.c
Store font directory mtime in cache file.
[fontconfig.git] / src / fcdir.c
1 /*
2 * $RCSId: xc/lib/fontconfig/src/fcdir.c,v 1.9 2002/08/31 22:17:32 keithp Exp $
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 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.
15 *
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.
23 */
24
25 #include "fcint.h"
26 #include <dirent.h>
27
28 FcBool
29 FcFileIsDir (const FcChar8 *file)
30 {
31 struct stat statb;
32
33 if (stat ((const char *) file, &statb) != 0)
34 return FcFalse;
35 return S_ISDIR(statb.st_mode);
36 }
37
38 static FcBool
39 FcFileScanFontConfig (FcFontSet *set,
40 FcBlanks *blanks,
41 const FcChar8 *file,
42 FcConfig *config)
43 {
44 FcPattern *font;
45 FcBool ret = FcTrue;
46 int id;
47 int count = 0;
48
49 id = 0;
50 do
51 {
52 font = 0;
53 /*
54 * Nothing in the cache, scan the file
55 */
56 if (FcDebug () & FC_DBG_SCAN)
57 {
58 printf ("\tScanning file %s...", file);
59 fflush (stdout);
60 }
61 font = FcFreeTypeQuery (file, id, blanks, &count);
62 if (FcDebug () & FC_DBG_SCAN)
63 printf ("done\n");
64
65 /*
66 * Edit pattern with user-defined rules
67 */
68 if (font && config && !FcConfigSubstituteWithPat (config, font, NULL, FcMatchScan))
69 {
70 FcPatternDestroy (font);
71 font = NULL;
72 ret = FcFalse;
73 }
74
75 /*
76 * Add the font
77 */
78 if (font && (!config || FcConfigAcceptFont (config, font)))
79 {
80 if (FcDebug() & FC_DBG_SCANV)
81 {
82 printf ("Final font pattern:\n");
83 FcPatternPrint (font);
84 }
85 if (!FcFontSetAdd (set, font))
86 {
87 FcPatternDestroy (font);
88 font = NULL;
89 ret = FcFalse;
90 }
91 }
92 else if (font)
93 FcPatternDestroy (font);
94 id++;
95 } while (font && ret && id < count);
96 return ret;
97 }
98
99 FcBool
100 FcFileScanConfig (FcFontSet *set,
101 FcStrSet *dirs,
102 FcBlanks *blanks,
103 const FcChar8 *file,
104 FcConfig *config)
105 {
106 if (FcFileIsDir (file))
107 return FcStrSetAdd (dirs, file);
108 else
109 return FcFileScanFontConfig (set, blanks, file, config);
110 }
111
112 FcBool
113 FcFileScan (FcFontSet *set,
114 FcStrSet *dirs,
115 FcFileCache *cache, /* XXX unused */
116 FcBlanks *blanks,
117 const FcChar8 *file,
118 FcBool force)
119 {
120 return FcFileScanConfig (set, dirs, blanks, file, NULL);
121 }
122
123 /*
124 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
125 */
126 static int
127 cmpstringp(const void *p1, const void *p2)
128 {
129 return strcmp(* (char **) p1, * (char **) p2);
130 }
131
132 /*
133 * Scan the specified directory and construct a cache of its contents
134 */
135 FcCache *
136 FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
137 {
138 DIR *d;
139 struct dirent *e;
140 FcStrSet *files;
141 FcStrSet *dirs;
142 FcChar8 *file;
143 FcChar8 *base;
144 FcBool ret = FcTrue;
145 FcFontSet *set;
146 int i;
147 FcBlanks *blanks = FcConfigGetBlanks (config);
148 FcCache *cache = NULL;
149 struct stat dir_stat;
150
151 if (FcDebug () & FC_DBG_FONTSET)
152 printf ("cache scan dir %s\n", dir);
153
154 /* freed below */
155 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
156 if (!file) {
157 ret = FcFalse;
158 goto bail;
159 }
160
161 strcpy ((char *) file, (char *) dir);
162 strcat ((char *) file, "/");
163 base = file + strlen ((char *) file);
164
165 if (FcDebug () & FC_DBG_SCAN)
166 printf ("\tScanning dir %s\n", dir);
167
168 d = opendir ((char *) dir);
169 if (!d)
170 {
171 /* Don't complain about missing directories */
172 if (errno == ENOENT)
173 ret = FcTrue;
174 else
175 ret = FcFalse;
176 goto bail_1;
177 }
178 if (stat ((char *) dir, &dir_stat) < 0)
179 {
180 ret = FcFalse;
181 goto bail_1;
182 }
183
184 set = FcFontSetCreate();
185 if (!set)
186 {
187 ret = FcFalse;
188 goto bail0;
189 }
190
191 files = FcStrSetCreate ();
192 if (!files)
193 {
194 ret = FcFalse;
195 goto bail1;
196 }
197 while ((e = readdir (d)))
198 {
199 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
200 {
201 strcpy ((char *) base, (char *) e->d_name);
202 if (!FcStrSetAdd (files, file)) {
203 ret = FcFalse;
204 goto bail2;
205 }
206 }
207 }
208
209 /*
210 * Sort files to make things prettier
211 */
212 qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp);
213
214 dirs = FcStrSetCreate ();
215 if (!dirs)
216 goto bail2;
217
218 /*
219 * Scan file files to build font patterns
220 */
221 for (i = 0; i < files->num; i++)
222 FcFileScanConfig (set, dirs, blanks, files->strs[i], config);
223
224 /*
225 * Build the cache object
226 */
227 cache = FcDirCacheBuild (set, dir, &dir_stat, dirs);
228 if (!cache)
229 goto bail3;
230
231 /*
232 * Write out the cache file, ignoring any troubles
233 */
234 FcDirCacheWrite (cache, config);
235
236 bail3:
237 FcStrSetDestroy (dirs);
238 bail2:
239 FcStrSetDestroy (files);
240 bail1:
241 FcFontSetDestroy (set);
242
243 bail0:
244 closedir (d);
245
246 bail_1:
247 free (file);
248 bail:
249 return cache;
250 }
251
252 /*
253 * Read (or construct) the cache for a directory
254 */
255 FcCache *
256 FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
257 {
258 FcCache *cache = NULL;
259
260 if (config && !FcConfigAcceptFilename (config, dir))
261 return NULL;
262
263 /* Try to use existing cache file */
264 if (!force)
265 cache = FcDirCacheLoad (dir, config, NULL);
266
267 /* Not using existing cache file, construct new cache */
268 if (!cache)
269 cache = FcDirCacheScan (dir, config);
270
271 return cache;
272 }
273
274 FcBool
275 FcDirScanConfig (FcFontSet *set,
276 FcStrSet *dirs,
277 FcBlanks *blanks,
278 const FcChar8 *dir,
279 FcBool force,
280 FcConfig *config)
281 {
282 return FcFalse; /* XXX deprecated */
283 }
284
285 FcBool
286 FcDirScan (FcFontSet *set,
287 FcStrSet *dirs,
288 FcFileCache *cache, /* XXX unused */
289 FcBlanks *blanks,
290 const FcChar8 *dir,
291 FcBool force)
292 {
293 return FcFalse; /* XXX deprecated */
294 }
295
296 FcBool
297 FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
298 {
299 return FcFalse; /* XXX deprecated */
300 }
301 #define __fcdir__
302 #include "fcaliastail.h"
303 #undef __fcdir__