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