]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
4bd4418a | 2 | * $RCSId: xc/lib/fontconfig/src/fcdir.c,v 1.9 2002/08/31 22:17:32 keithp Exp $ |
24330d27 | 3 | * |
46b51147 | 4 | * Copyright © 2000 Keith Packard |
24330d27 KP |
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 | ||
24330d27 | 25 | #include "fcint.h" |
179c3995 KP |
26 | #include <dirent.h> |
27 | ||
2d9c79c0 | 28 | FcBool |
179c3995 KP |
29 | FcFileIsDir (const FcChar8 *file) |
30 | { | |
31 | struct stat statb; | |
24330d27 | 32 | |
179c3995 KP |
33 | if (stat ((const char *) file, &statb) != 0) |
34 | return FcFalse; | |
35 | return S_ISDIR(statb.st_mode); | |
36 | } | |
24330d27 KP |
37 | |
38 | FcBool | |
d47c9d6e KP |
39 | FcFileScanConfig (FcFontSet *set, |
40 | FcStrSet *dirs, | |
41 | FcGlobalCache *cache, | |
42 | FcBlanks *blanks, | |
43 | const FcChar8 *file, | |
44 | FcBool force, | |
45 | FcConfig *config) | |
24330d27 | 46 | { |
327a7fd4 KP |
47 | int id; |
48 | FcChar8 *name; | |
49 | FcPattern *font; | |
50 | FcBool ret = FcTrue; | |
51 | FcBool isDir; | |
52 | int count = 0; | |
53 | FcGlobalCacheFile *cache_file; | |
54 | FcGlobalCacheDir *cache_dir; | |
55 | FcBool need_scan; | |
24330d27 | 56 | |
d47c9d6e KP |
57 | if (config && !FcConfigAcceptFilename (config, file)) |
58 | return FcTrue; | |
59 | ||
327a7fd4 KP |
60 | if (force) |
61 | cache = 0; | |
24330d27 KP |
62 | id = 0; |
63 | do | |
64 | { | |
327a7fd4 KP |
65 | need_scan = FcTrue; |
66 | font = 0; | |
67 | /* | |
68 | * Check the cache | |
69 | */ | |
70 | if (cache) | |
24330d27 | 71 | { |
327a7fd4 | 72 | if ((cache_file = FcGlobalCacheFileGet (cache, file, id, &count))) |
179c3995 | 73 | { |
327a7fd4 KP |
74 | /* |
75 | * Found a cache entry for the file | |
76 | */ | |
a8386abc | 77 | if (FcGlobalCacheCheckTime (file, &cache_file->info)) |
327a7fd4 KP |
78 | { |
79 | name = cache_file->name; | |
80 | need_scan = FcFalse; | |
81 | FcGlobalCacheReferenced (cache, &cache_file->info); | |
82 | /* "." means the file doesn't contain a font */ | |
83 | if (FcStrCmp (name, FC_FONT_FILE_INVALID) != 0) | |
84 | { | |
85 | font = FcNameParse (name); | |
86 | if (font) | |
87 | if (!FcPatternAddString (font, FC_FILE, file)) | |
88 | ret = FcFalse; | |
89 | } | |
90 | } | |
179c3995 | 91 | } |
327a7fd4 KP |
92 | else if ((cache_dir = FcGlobalCacheDirGet (cache, file, |
93 | strlen ((const char *) file), | |
94 | FcFalse))) | |
24330d27 | 95 | { |
a8386abc KP |
96 | if (FcGlobalCacheCheckTime (cache_dir->info.file, |
97 | &cache_dir->info)) | |
327a7fd4 KP |
98 | { |
99 | font = 0; | |
100 | need_scan = FcFalse; | |
101 | FcGlobalCacheReferenced (cache, &cache_dir->info); | |
102 | if (!FcStrSetAdd (dirs, file)) | |
6f6563ed | 103 | ret = FcFalse; |
327a7fd4 | 104 | } |
24330d27 | 105 | } |
24330d27 | 106 | } |
327a7fd4 KP |
107 | /* |
108 | * Nothing in the cache, scan the file | |
109 | */ | |
110 | if (need_scan) | |
24330d27 KP |
111 | { |
112 | if (FcDebug () & FC_DBG_SCAN) | |
113 | { | |
114 | printf ("\tScanning file %s...", file); | |
115 | fflush (stdout); | |
116 | } | |
117 | font = FcFreeTypeQuery (file, id, blanks, &count); | |
118 | if (FcDebug () & FC_DBG_SCAN) | |
119 | printf ("done\n"); | |
179c3995 KP |
120 | isDir = FcFalse; |
121 | if (!font && FcFileIsDir (file)) | |
122 | { | |
123 | isDir = FcTrue; | |
124 | ret = FcStrSetAdd (dirs, file); | |
125 | } | |
327a7fd4 KP |
126 | /* |
127 | * Update the cache | |
128 | */ | |
129 | if (cache && font) | |
24330d27 | 130 | { |
327a7fd4 | 131 | FcChar8 *unparse; |
24330d27 | 132 | |
327a7fd4 KP |
133 | unparse = FcNameUnparse (font); |
134 | if (unparse) | |
24330d27 | 135 | { |
327a7fd4 | 136 | (void) FcGlobalCacheUpdate (cache, file, id, unparse); |
9dac3c59 | 137 | FcStrFree (unparse); |
24330d27 KP |
138 | } |
139 | } | |
140 | } | |
327a7fd4 KP |
141 | /* |
142 | * Add the font | |
143 | */ | |
4f27c1c0 | 144 | if (font && (!config || FcConfigAcceptFont (config, font))) |
24330d27 KP |
145 | { |
146 | if (!FcFontSetAdd (set, font)) | |
147 | { | |
148 | FcPatternDestroy (font); | |
149 | font = 0; | |
150 | ret = FcFalse; | |
151 | } | |
152 | } | |
47b49bf1 KP |
153 | else if (font) |
154 | FcPatternDestroy (font); | |
24330d27 KP |
155 | id++; |
156 | } while (font && ret && id < count); | |
157 | return ret; | |
158 | } | |
159 | ||
d47c9d6e KP |
160 | FcBool |
161 | FcFileScan (FcFontSet *set, | |
162 | FcStrSet *dirs, | |
163 | FcGlobalCache *cache, | |
164 | FcBlanks *blanks, | |
165 | const FcChar8 *file, | |
166 | FcBool force) | |
167 | { | |
168 | return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0); | |
169 | } | |
170 | ||
37e3f33c PL |
171 | /* |
172 | * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage | |
173 | */ | |
174 | ||
175 | static int | |
176 | cmpstringp(const void *p1, const void *p2) | |
177 | { | |
178 | return strcmp(* (char **) p1, * (char **) p2); | |
179 | } | |
180 | ||
c8d5753c KP |
181 | /* |
182 | * Scan 'dir', adding font files to 'set' and | |
183 | * subdirectories to 'dirs' | |
184 | */ | |
185 | ||
24330d27 | 186 | FcBool |
d47c9d6e KP |
187 | FcDirScanConfig (FcFontSet *set, |
188 | FcStrSet *dirs, | |
189 | FcGlobalCache *cache, | |
190 | FcBlanks *blanks, | |
191 | const FcChar8 *dir, | |
192 | FcBool force, | |
193 | FcConfig *config) | |
24330d27 | 194 | { |
327a7fd4 KP |
195 | DIR *d; |
196 | struct dirent *e; | |
37e3f33c PL |
197 | FcChar8 **dirlist; |
198 | int dirlistlen, dirlistalloc; | |
327a7fd4 KP |
199 | FcChar8 *file; |
200 | FcChar8 *base; | |
201 | FcBool ret = FcTrue; | |
37e3f33c | 202 | int i; |
24330d27 | 203 | |
d47c9d6e KP |
204 | if (config && !FcConfigAcceptFilename (config, dir)) |
205 | return FcTrue; | |
206 | ||
327a7fd4 KP |
207 | if (!force) |
208 | { | |
209 | /* | |
f21f40f3 | 210 | * Check fonts.cache-<version> file |
327a7fd4 | 211 | */ |
d47c9d6e | 212 | if (FcDirCacheReadDir (set, dirs, dir, config)) |
c4ab52dc | 213 | { |
46d003c3 KP |
214 | if (cache) |
215 | FcGlobalCacheReferenceSubdir (cache, dir); | |
327a7fd4 | 216 | return FcTrue; |
c4ab52dc | 217 | } |
327a7fd4 KP |
218 | |
219 | /* | |
f21f40f3 | 220 | * Check ~/.fonts.cache-<version> file |
327a7fd4 | 221 | */ |
d47c9d6e | 222 | if (cache && FcGlobalCacheScanDir (set, dirs, cache, dir, config)) |
327a7fd4 KP |
223 | return FcTrue; |
224 | } | |
225 | ||
9dac3c59 | 226 | /* freed below */ |
179c3995 | 227 | file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); |
24330d27 KP |
228 | if (!file) |
229 | return FcFalse; | |
230 | ||
ccb3e93b KP |
231 | strcpy ((char *) file, (char *) dir); |
232 | strcat ((char *) file, "/"); | |
233 | base = file + strlen ((char *) file); | |
24330d27 | 234 | |
a8386abc KP |
235 | if (FcDebug () & FC_DBG_SCAN) |
236 | printf ("\tScanning dir %s\n", dir); | |
237 | ||
ccb3e93b | 238 | d = opendir ((char *) dir); |
327a7fd4 | 239 | |
24330d27 KP |
240 | if (!d) |
241 | { | |
242 | free (file); | |
179c3995 KP |
243 | /* Don't complain about missing directories */ |
244 | if (errno == ENOENT) | |
245 | return FcTrue; | |
24330d27 KP |
246 | return FcFalse; |
247 | } | |
37e3f33c PL |
248 | dirlistlen = 0; |
249 | dirlistalloc = 8; | |
250 | dirlist = malloc(dirlistalloc * sizeof(FcChar8 *)); | |
251 | if (!dirlist) | |
252 | return FcFalse; | |
253 | while ((e = readdir (d))) | |
24330d27 | 254 | { |
179c3995 | 255 | if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN) |
24330d27 | 256 | { |
37e3f33c PL |
257 | if (dirlistlen == dirlistalloc) |
258 | { | |
259 | dirlistalloc *= 2; | |
260 | dirlist = realloc(dirlist, dirlistalloc * sizeof(FcChar8 *)); | |
261 | if (!dirlist) | |
262 | return FcFalse; | |
263 | } | |
264 | dirlist[dirlistlen] = malloc(strlen (e->d_name) + 1); | |
265 | if (!dirlist[dirlistlen]) | |
266 | return FcFalse; | |
267 | strcpy(dirlist[dirlistlen], e->d_name); | |
268 | dirlistlen++; | |
24330d27 KP |
269 | } |
270 | } | |
37e3f33c PL |
271 | qsort(dirlist, dirlistlen, sizeof(FcChar8 *), cmpstringp); |
272 | i = 0; | |
273 | while (ret && i < dirlistlen) | |
274 | { | |
275 | strcpy ((char *) base, (char *) dirlist[i]); | |
276 | ret = FcFileScanConfig (set, dirs, cache, blanks, file, force, config); | |
277 | i++; | |
278 | } | |
279 | for (i = 0; i < dirlistlen; i++) | |
280 | free(dirlist[i]); | |
281 | free (dirlist); | |
24330d27 KP |
282 | free (file); |
283 | closedir (d); | |
c8d5753c KP |
284 | /* |
285 | * Now that the directory has been scanned, | |
286 | * add the cache entry | |
287 | */ | |
327a7fd4 KP |
288 | if (ret && cache) |
289 | FcGlobalCacheUpdate (cache, dir, 0, 0); | |
290 | ||
24330d27 KP |
291 | return ret; |
292 | } | |
293 | ||
d47c9d6e KP |
294 | FcBool |
295 | FcDirScan (FcFontSet *set, | |
296 | FcStrSet *dirs, | |
297 | FcGlobalCache *cache, | |
298 | FcBlanks *blanks, | |
299 | const FcChar8 *dir, | |
300 | FcBool force) | |
301 | { | |
302 | return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0); | |
303 | } | |
304 | ||
24330d27 | 305 | FcBool |
179c3995 | 306 | FcDirSave (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir) |
24330d27 | 307 | { |
327a7fd4 | 308 | return FcDirCacheWriteDir (set, dirs, dir); |
24330d27 | 309 | } |