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