]> git.wh0rd.org - fontconfig.git/blob - src/fcdir.c
Global cache time checking was using wrong file name and computing wrong
[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 static 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 FcBool
39 FcFileScan (FcFontSet *set,
40 FcStrSet *dirs,
41 FcGlobalCache *cache,
42 FcBlanks *blanks,
43 const FcChar8 *file,
44 FcBool force)
45 {
46 int id;
47 FcChar8 *name;
48 FcPattern *font;
49 FcBool ret = FcTrue;
50 FcBool isDir;
51 int count = 0;
52 FcGlobalCacheFile *cache_file;
53 FcGlobalCacheDir *cache_dir;
54 FcBool need_scan;
55
56 if (force)
57 cache = 0;
58 id = 0;
59 do
60 {
61 need_scan = FcTrue;
62 font = 0;
63 /*
64 * Check the cache
65 */
66 if (cache)
67 {
68 if ((cache_file = FcGlobalCacheFileGet (cache, file, id, &count)))
69 {
70 /*
71 * Found a cache entry for the file
72 */
73 if (FcGlobalCacheCheckTime (file, &cache_file->info))
74 {
75 name = cache_file->name;
76 need_scan = FcFalse;
77 FcGlobalCacheReferenced (cache, &cache_file->info);
78 /* "." means the file doesn't contain a font */
79 if (FcStrCmp (name, FC_FONT_FILE_INVALID) != 0)
80 {
81 font = FcNameParse (name);
82 if (font)
83 if (!FcPatternAddString (font, FC_FILE, file))
84 ret = FcFalse;
85 }
86 }
87 }
88 else if ((cache_dir = FcGlobalCacheDirGet (cache, file,
89 strlen ((const char *) file),
90 FcFalse)))
91 {
92 if (FcGlobalCacheCheckTime (cache_dir->info.file,
93 &cache_dir->info))
94 {
95 font = 0;
96 need_scan = FcFalse;
97 FcGlobalCacheReferenced (cache, &cache_dir->info);
98 if (!FcStrSetAdd (dirs, file))
99 ret = FcFalse;
100 }
101 }
102 }
103 /*
104 * Nothing in the cache, scan the file
105 */
106 if (need_scan)
107 {
108 if (FcDebug () & FC_DBG_SCAN)
109 {
110 printf ("\tScanning file %s...", file);
111 fflush (stdout);
112 }
113 font = FcFreeTypeQuery (file, id, blanks, &count);
114 if (FcDebug () & FC_DBG_SCAN)
115 printf ("done\n");
116 isDir = FcFalse;
117 if (!font && FcFileIsDir (file))
118 {
119 isDir = FcTrue;
120 ret = FcStrSetAdd (dirs, file);
121 }
122 /*
123 * Update the cache
124 */
125 if (cache && font)
126 {
127 FcChar8 *unparse;
128
129 unparse = FcNameUnparse (font);
130 if (unparse)
131 {
132 (void) FcGlobalCacheUpdate (cache, file, id, unparse);
133 FcStrFree (unparse);
134 }
135 }
136 }
137 /*
138 * Add the font
139 */
140 if (font)
141 {
142 if (!FcFontSetAdd (set, font))
143 {
144 FcPatternDestroy (font);
145 font = 0;
146 ret = FcFalse;
147 }
148 }
149 id++;
150 } while (font && ret && id < count);
151 return ret;
152 }
153
154 #define FC_MAX_FILE_LEN 4096
155
156 /*
157 * Scan 'dir', adding font files to 'set' and
158 * subdirectories to 'dirs'
159 */
160
161 FcBool
162 FcDirScan (FcFontSet *set,
163 FcStrSet *dirs,
164 FcGlobalCache *cache,
165 FcBlanks *blanks,
166 const FcChar8 *dir,
167 FcBool force)
168 {
169 DIR *d;
170 struct dirent *e;
171 FcChar8 *file;
172 FcChar8 *base;
173 FcBool ret = FcTrue;
174
175 if (!force)
176 {
177 /*
178 * Check fonts.cache-<version> file
179 */
180 if (FcDirCacheReadDir (set, dirs, dir))
181 {
182 if (cache)
183 FcGlobalCacheReferenceSubdir (cache, dir);
184 return FcTrue;
185 }
186
187 /*
188 * Check ~/.fonts.cache-<version> file
189 */
190 if (cache && FcGlobalCacheScanDir (set, dirs, cache, dir))
191 return FcTrue;
192 }
193
194 /* freed below */
195 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
196 if (!file)
197 return FcFalse;
198
199 strcpy ((char *) file, (char *) dir);
200 strcat ((char *) file, "/");
201 base = file + strlen ((char *) file);
202
203 if (FcDebug () & FC_DBG_SCAN)
204 printf ("\tScanning dir %s\n", dir);
205
206 d = opendir ((char *) dir);
207
208 if (!d)
209 {
210 free (file);
211 /* Don't complain about missing directories */
212 if (errno == ENOENT)
213 return FcTrue;
214 return FcFalse;
215 }
216 while (ret && (e = readdir (d)))
217 {
218 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
219 {
220 strcpy ((char *) base, (char *) e->d_name);
221 ret = FcFileScan (set, dirs, cache, blanks, file, force);
222 }
223 }
224 free (file);
225 closedir (d);
226 /*
227 * Now that the directory has been scanned,
228 * add the cache entry
229 */
230 if (ret && cache)
231 FcGlobalCacheUpdate (cache, dir, 0, 0);
232
233 return ret;
234 }
235
236 FcBool
237 FcDirSave (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
238 {
239 return FcDirCacheWriteDir (set, dirs, dir);
240 }