]> git.wh0rd.org - fontconfig.git/blame - src/fcdir.c
Deal correctly with changing FC_CACHE_MAGIC.
[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;
327a7fd4
KP
48 FcPattern *font;
49 FcBool ret = FcTrue;
327a7fd4 50 int count = 0;
24330d27 51
d47c9d6e
KP
52 if (config && !FcConfigAcceptFilename (config, file))
53 return FcTrue;
54
98592bbb
PL
55 if (FcFileIsDir (file))
56 return FcStrSetAdd (dirs, file);
57
327a7fd4
KP
58 if (force)
59 cache = 0;
98592bbb 60
24330d27
KP
61 id = 0;
62 do
63 {
327a7fd4
KP
64 font = 0;
65 /*
eb0cf671 66 * Nothing in the cache, scan the file
327a7fd4 67 */
eb0cf671 68 if (FcDebug () & FC_DBG_SCAN)
24330d27 69 {
eb0cf671
PL
70 printf ("\tScanning file %s...", file);
71 fflush (stdout);
24330d27 72 }
eb0cf671
PL
73 font = FcFreeTypeQuery (file, id, blanks, &count);
74 if (FcDebug () & FC_DBG_SCAN)
75 printf ("done\n");
327a7fd4
KP
76 /*
77 * Add the font
78 */
4f27c1c0 79 if (font && (!config || FcConfigAcceptFont (config, font)))
24330d27
KP
80 {
81 if (!FcFontSetAdd (set, font))
82 {
83 FcPatternDestroy (font);
84 font = 0;
85 ret = FcFalse;
86 }
87 }
47b49bf1
KP
88 else if (font)
89 FcPatternDestroy (font);
24330d27
KP
90 id++;
91 } while (font && ret && id < count);
92 return ret;
93}
94
d47c9d6e
KP
95FcBool
96FcFileScan (FcFontSet *set,
97 FcStrSet *dirs,
98 FcGlobalCache *cache,
99 FcBlanks *blanks,
100 const FcChar8 *file,
101 FcBool force)
102{
103 return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0);
104}
105
c8d5753c
KP
106/*
107 * Scan 'dir', adding font files to 'set' and
108 * subdirectories to 'dirs'
109 */
110
24330d27 111FcBool
d47c9d6e
KP
112FcDirScanConfig (FcFontSet *set,
113 FcStrSet *dirs,
114 FcGlobalCache *cache,
115 FcBlanks *blanks,
116 const FcChar8 *dir,
117 FcBool force,
118 FcConfig *config)
24330d27 119{
327a7fd4
KP
120 DIR *d;
121 struct dirent *e;
122 FcChar8 *file;
5c3deb29 123 const FcChar8 *d_can = 0;
327a7fd4
KP
124 FcChar8 *base;
125 FcBool ret = FcTrue;
eb0cf671
PL
126 FcFontSet *tmpSet;
127 int i;
24330d27 128
d47c9d6e
KP
129 if (config && !FcConfigAcceptFilename (config, dir))
130 return FcTrue;
131
58bdd296 132 if (config)
5c3deb29
PL
133 d_can = FcConfigNormalizeFontDir (config, dir);
134 if (d_can)
135 dir = d_can;
58bdd296 136
327a7fd4
KP
137 if (!force)
138 {
327a7fd4 139 /*
f21f40f3 140 * Check ~/.fonts.cache-<version> file
327a7fd4 141 */
8245771d 142 if (cache && FcGlobalCacheReadDir (set, dirs, cache, (char *)dir, config))
327a7fd4 143 return FcTrue;
2eb84374 144
cd9bca69 145 if (FcDirCacheValid (dir) && FcDirCacheRead (set, dirs, dir, config))
2eb84374 146 return FcTrue;
327a7fd4
KP
147 }
148
9dac3c59 149 /* freed below */
179c3995 150 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
24330d27
KP
151 if (!file)
152 return FcFalse;
153
ccb3e93b
KP
154 strcpy ((char *) file, (char *) dir);
155 strcat ((char *) file, "/");
156 base = file + strlen ((char *) file);
24330d27 157
a8386abc
KP
158 if (FcDebug () & FC_DBG_SCAN)
159 printf ("\tScanning dir %s\n", dir);
160
ccb3e93b 161 d = opendir ((char *) dir);
24330d27
KP
162 if (!d)
163 {
164 free (file);
179c3995
KP
165 /* Don't complain about missing directories */
166 if (errno == ENOENT)
167 return FcTrue;
24330d27
KP
168 return FcFalse;
169 }
eb0cf671
PL
170
171 tmpSet = FcFontSetCreate();
172 if (!tmpSet)
173 {
174 free (file);
175 return FcFalse;
176 }
177
24330d27
KP
178 while (ret && (e = readdir (d)))
179 {
179c3995 180 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
24330d27 181 {
ccb3e93b 182 strcpy ((char *) base, (char *) e->d_name);
eb0cf671 183 ret = FcFileScanConfig (tmpSet, dirs, cache, blanks, file, force, config);
24330d27
KP
184 }
185 }
186 free (file);
187 closedir (d);
c8d5753c
KP
188 /*
189 * Now that the directory has been scanned,
190 * add the cache entry
191 */
327a7fd4 192 if (ret && cache)
971cf180 193 FcGlobalCacheUpdate (cache, dirs, (char *)dir, tmpSet, config);
eb0cf671
PL
194
195 for (i = 0; i < tmpSet->nfont; i++)
196 FcFontSetAdd (set, tmpSet->fonts[i]);
197
198 if (tmpSet->fonts)
199 {
200 FcMemFree (FC_MEM_FONTPTR, tmpSet->sfont * sizeof (FcPattern *));
201 free (tmpSet->fonts);
202 }
203 FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet));
204 free (tmpSet);
327a7fd4 205
24330d27
KP
206 return ret;
207}
208
d47c9d6e
KP
209FcBool
210FcDirScan (FcFontSet *set,
211 FcStrSet *dirs,
212 FcGlobalCache *cache,
213 FcBlanks *blanks,
214 const FcChar8 *dir,
215 FcBool force)
216{
217 return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0);
218}
219
24330d27 220FcBool
2304e38f 221FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
24330d27 222{
2304e38f 223 return FcDirCacheWrite (set, dirs, dir);
24330d27 224}