]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
4bd4418a | 2 | * $RCSId: xc/lib/fontconfig/src/fccache.c,v 1.12 2002/08/22 07:36:44 keithp Exp $ |
24330d27 | 3 | * |
46b51147 | 4 | * Copyright © 2000 Keith Packard |
03a212e5 | 5 | * Copyright © 2005 Patrick Lam |
24330d27 KP |
6 | * |
7 | * Permission to use, copy, modify, distribute, and sell this software and its | |
8 | * documentation for any purpose is hereby granted without fee, provided that | |
9 | * the above copyright notice appear in all copies and that both that | |
10 | * copyright notice and this permission notice appear in supporting | |
11 | * documentation, and that the name of Keith Packard not be used in | |
12 | * advertising or publicity pertaining to distribution of the software without | |
13 | * specific, written prior permission. Keith Packard makes no | |
14 | * representations about the suitability of this software for any purpose. It | |
15 | * is provided "as is" without express or implied warranty. | |
16 | * | |
17 | * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
18 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
19 | * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR | |
20 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | |
21 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
22 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
23 | * PERFORMANCE OF THIS SOFTWARE. | |
24 | */ | |
25 | ||
212c9f43 | 26 | #include <fcntl.h> |
4262e0b3 | 27 | #include <dirent.h> |
212c9f43 PL |
28 | #include <sys/mman.h> |
29 | #include <sys/utsname.h> | |
fd77c154 PL |
30 | #include <sys/types.h> |
31 | #include <unistd.h> | |
24330d27 | 32 | #include "fcint.h" |
7fd7221e | 33 | #include <unistd.h> |
24330d27 | 34 | |
2dbe7597 | 35 | #define ENDIAN_TEST 0x12345678 |
7fd7221e | 36 | #define MACHINE_SIGNATURE_SIZE 9 + 5*20 + 1 |
2dbe7597 | 37 | |
eb0cf671 PL |
38 | static off_t |
39 | FcCacheSkipToArch (int fd, const char * arch); | |
1b7be377 | 40 | |
eb0cf671 | 41 | static FcBool |
1d879de2 | 42 | FcCacheCopyOld (int fd, int fd_orig, off_t start); |
1b7be377 | 43 | |
eb0cf671 PL |
44 | static void * |
45 | FcDirCacheProduce (FcFontSet *set, FcCache * metadata); | |
1b7be377 PL |
46 | |
47 | static FcBool | |
e77c1718 | 48 | FcDirCacheConsume (int fd, const char * dir, FcFontSet *set); |
1b7be377 | 49 | |
2eb84374 | 50 | FcBool |
2304e38f | 51 | FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir); |
1b7be377 | 52 | |
eb0cf671 PL |
53 | static int |
54 | FcCacheNextOffset(off_t w); | |
1b7be377 | 55 | |
eb0cf671 PL |
56 | static char * |
57 | FcCacheMachineSignature (void); | |
1b7be377 PL |
58 | |
59 | static FcBool | |
eb0cf671 | 60 | FcCacheHaveBank (int bank); |
1b7be377 | 61 | |
e77c1718 PL |
62 | static void |
63 | FcCacheAddBankDir (int bank, const char * dir); | |
64 | ||
eb0cf671 | 65 | #define FC_DBG_CACHE_REF 1024 |
1b7be377 | 66 | |
8245771d PL |
67 | static char * |
68 | FcCacheReadString (int fd, char *dest, int len) | |
24330d27 | 69 | { |
212c9f43 | 70 | FcChar8 c; |
ccb3e93b | 71 | FcBool escape; |
ccb3e93b KP |
72 | int size; |
73 | int i; | |
24330d27 | 74 | |
24330d27 | 75 | if (len == 0) |
03a212e5 | 76 | return 0; |
24330d27 | 77 | |
ccb3e93b KP |
78 | size = len; |
79 | i = 0; | |
24330d27 | 80 | escape = FcFalse; |
212c9f43 | 81 | while (read (fd, &c, 1) == 1) |
24330d27 KP |
82 | { |
83 | if (!escape) | |
84 | { | |
85 | switch (c) { | |
86 | case '"': | |
ccb3e93b KP |
87 | c = '\0'; |
88 | break; | |
24330d27 KP |
89 | case '\\': |
90 | escape = FcTrue; | |
91 | continue; | |
92 | } | |
93 | } | |
ccb3e93b KP |
94 | if (i == size) |
95 | { | |
212c9f43 PL |
96 | dest[i++] = 0; |
97 | return dest; | |
ccb3e93b | 98 | } |
212c9f43 | 99 | dest[i++] = c; |
ccb3e93b | 100 | if (c == '\0') |
212c9f43 | 101 | return dest; |
24330d27 KP |
102 | escape = FcFalse; |
103 | } | |
ccb3e93b | 104 | return 0; |
24330d27 KP |
105 | } |
106 | ||
107 | static FcBool | |
8245771d | 108 | FcCacheWriteString (int fd, const char *chars) |
327a7fd4 | 109 | { |
212c9f43 | 110 | if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1) |
327a7fd4 | 111 | return FcFalse; |
327a7fd4 KP |
112 | return FcTrue; |
113 | } | |
114 | ||
eb0cf671 PL |
115 | static void |
116 | FcGlobalCacheDirDestroy (FcGlobalCacheDir *d) | |
1b7be377 | 117 | { |
5e678e94 PL |
118 | FcMemFree (FC_MEM_STRING, strlen (d->name)+1); |
119 | free (d->name); | |
120 | FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir)); | |
121 | free (d); | |
1b7be377 PL |
122 | } |
123 | ||
eb0cf671 PL |
124 | FcGlobalCache * |
125 | FcGlobalCacheCreate (void) | |
1b7be377 | 126 | { |
eb0cf671 | 127 | FcGlobalCache *cache; |
1b7be377 | 128 | |
eb0cf671 PL |
129 | cache = malloc (sizeof (FcGlobalCache)); |
130 | if (!cache) | |
131 | return 0; | |
132 | FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCache)); | |
133 | cache->dirs = 0; | |
134 | cache->updated = FcFalse; | |
135 | cache->fd = -1; | |
136 | return cache; | |
1b7be377 PL |
137 | } |
138 | ||
eb0cf671 PL |
139 | void |
140 | FcGlobalCacheDestroy (FcGlobalCache *cache) | |
327a7fd4 | 141 | { |
eb0cf671 | 142 | FcGlobalCacheDir *d, *next; |
327a7fd4 | 143 | |
eb0cf671 | 144 | for (d = cache->dirs; d; d = next) |
327a7fd4 | 145 | { |
eb0cf671 PL |
146 | next = d->next; |
147 | FcGlobalCacheDirDestroy (d); | |
327a7fd4 | 148 | } |
eb0cf671 PL |
149 | FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCache)); |
150 | free (cache); | |
327a7fd4 KP |
151 | } |
152 | ||
153 | void | |
eb0cf671 | 154 | FcGlobalCacheLoad (FcGlobalCache *cache, |
03a212e5 | 155 | FcStrSet *staleDirs, |
2b25f00c PL |
156 | const FcChar8 *cache_file, |
157 | FcConfig *config) | |
327a7fd4 | 158 | { |
8245771d | 159 | char name_buf[8192]; |
eb0cf671 | 160 | FcGlobalCacheDir *d, *next; |
2b25f00c | 161 | FcFileTime config_time = FcConfigModifiedTime (config); |
eb0cf671 PL |
162 | char * current_arch_machine_name; |
163 | char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9]; | |
164 | off_t current_arch_start; | |
327a7fd4 | 165 | |
03a212e5 PL |
166 | struct stat cache_stat, dir_stat; |
167 | ||
168 | if (stat ((char *) cache_file, &cache_stat) < 0) | |
169 | return; | |
170 | ||
eb0cf671 PL |
171 | cache->fd = open ((char *) cache_file, O_RDONLY); |
172 | if (cache->fd == -1) | |
173 | return; | |
327a7fd4 | 174 | |
eb0cf671 | 175 | cache->updated = FcFalse; |
327a7fd4 | 176 | |
eb0cf671 PL |
177 | current_arch_machine_name = FcCacheMachineSignature (); |
178 | current_arch_start = FcCacheSkipToArch(cache->fd, | |
179 | current_arch_machine_name); | |
180 | if (current_arch_start < 0) | |
e58b50e8 | 181 | goto bail_and_destroy; |
327a7fd4 | 182 | |
eb0cf671 | 183 | lseek (cache->fd, current_arch_start, SEEK_SET); |
03a212e5 PL |
184 | FcCacheReadString (cache->fd, candidate_arch_machine_name, |
185 | sizeof (candidate_arch_machine_name)); | |
186 | if (strlen(candidate_arch_machine_name) == 0) | |
e58b50e8 | 187 | goto bail_and_destroy; |
1b7be377 | 188 | |
eb0cf671 | 189 | while (1) |
1b7be377 | 190 | { |
8cb4c56d PL |
191 | off_t targ; |
192 | ||
eb0cf671 PL |
193 | FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)); |
194 | if (!strlen(name_buf)) | |
1b7be377 | 195 | break; |
1b7be377 | 196 | |
9090cb9e PL |
197 | /* Directory must be older than the global cache file; also |
198 | cache must be newer than the config file. */ | |
03a212e5 | 199 | if (stat ((char *) name_buf, &dir_stat) < 0 || |
2b25f00c | 200 | dir_stat.st_mtime > cache_stat.st_mtime || |
9090cb9e | 201 | (config_time.set && cache_stat.st_mtime < config_time.time)) |
03a212e5 PL |
202 | { |
203 | FcCache md; | |
204 | ||
8245771d | 205 | FcStrSetAdd (staleDirs, FcStrCopy ((FcChar8 *)name_buf)); |
03a212e5 PL |
206 | read (cache->fd, &md, sizeof (FcCache)); |
207 | lseek (cache->fd, FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + md.count, SEEK_SET); | |
208 | continue; | |
209 | } | |
210 | ||
eb0cf671 PL |
211 | d = malloc (sizeof (FcGlobalCacheDir)); |
212 | if (!d) | |
213 | goto bail1; | |
1b7be377 | 214 | |
eb0cf671 PL |
215 | d->next = cache->dirs; |
216 | cache->dirs = d; | |
1b7be377 | 217 | |
8245771d | 218 | d->name = (char *)FcStrCopy ((FcChar8 *)name_buf); |
eb0cf671 PL |
219 | d->ent = 0; |
220 | d->offset = lseek (cache->fd, 0, SEEK_CUR); | |
8cb4c56d PL |
221 | if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache)) |
222 | goto bail1; | |
223 | targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count; | |
224 | if (lseek (cache->fd, targ, SEEK_SET) != targ) | |
225 | goto bail1; | |
1b7be377 | 226 | } |
eb0cf671 | 227 | return; |
1b7be377 | 228 | |
eb0cf671 PL |
229 | bail1: |
230 | for (d = cache->dirs; d; d = next) | |
1b7be377 | 231 | { |
eb0cf671 PL |
232 | next = d->next; |
233 | free (d); | |
1b7be377 | 234 | } |
eb0cf671 | 235 | cache->dirs = 0; |
e58b50e8 PL |
236 | |
237 | close (cache->fd); | |
238 | cache->fd = -1; | |
239 | return; | |
240 | ||
241 | bail_and_destroy: | |
eb0cf671 PL |
242 | close (cache->fd); |
243 | cache->fd = -1; | |
e58b50e8 PL |
244 | |
245 | if (stat ((char *) cache_file, &cache_stat) == 0) | |
246 | unlink ((char *)cache_file); | |
247 | ||
eb0cf671 | 248 | return; |
e58b50e8 | 249 | |
1b7be377 PL |
250 | } |
251 | ||
1b7be377 | 252 | FcBool |
8245771d | 253 | FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, const char *dir, FcConfig *config) |
1b7be377 | 254 | { |
eb0cf671 | 255 | FcGlobalCacheDir *d; |
03a212e5 | 256 | FcBool ret = FcFalse; |
1b7be377 | 257 | |
eb0cf671 | 258 | if (cache->fd == -1) |
1b7be377 | 259 | return FcFalse; |
1b7be377 | 260 | |
eb0cf671 | 261 | for (d = cache->dirs; d; d = d->next) |
1b7be377 | 262 | { |
03a212e5 | 263 | if (strncmp (d->name, dir, strlen(dir)) == 0) |
1b7be377 | 264 | { |
eb0cf671 | 265 | lseek (cache->fd, d->offset, SEEK_SET); |
e77c1718 | 266 | if (!FcDirCacheConsume (cache->fd, dir, set)) |
eb0cf671 | 267 | return FcFalse; |
03a212e5 PL |
268 | if (strcmp (d->name, dir) == 0) |
269 | ret = FcTrue; | |
1b7be377 | 270 | } |
1b7be377 | 271 | } |
1b7be377 | 272 | |
03a212e5 | 273 | return ret; |
1b7be377 PL |
274 | } |
275 | ||
eb0cf671 PL |
276 | FcBool |
277 | FcGlobalCacheUpdate (FcGlobalCache *cache, | |
8245771d | 278 | const char *name, |
eb0cf671 | 279 | FcFontSet *set) |
1b7be377 | 280 | { |
eb0cf671 | 281 | FcGlobalCacheDir * d; |
1b7be377 | 282 | |
eb0cf671 PL |
283 | if (!set->nfont) |
284 | return FcTrue; | |
1b7be377 | 285 | |
eb0cf671 | 286 | for (d = cache->dirs; d; d = d->next) |
1b7be377 | 287 | { |
eb0cf671 | 288 | if (strcmp(d->name, name) == 0) |
1b7be377 | 289 | break; |
24330d27 | 290 | } |
24330d27 | 291 | |
eb0cf671 | 292 | if (!d) |
24330d27 | 293 | { |
eb0cf671 PL |
294 | d = malloc (sizeof (FcGlobalCacheDir)); |
295 | if (!d) | |
296 | return FcFalse; | |
297 | d->next = cache->dirs; | |
298 | cache->dirs = d; | |
24330d27 | 299 | } |
24330d27 | 300 | |
eb0cf671 | 301 | cache->updated = FcTrue; |
24330d27 | 302 | |
8245771d | 303 | d->name = (char *)FcStrCopy ((FcChar8 *)name); |
eb0cf671 PL |
304 | d->ent = FcDirCacheProduce (set, &d->metadata); |
305 | d->offset = 0; | |
306 | return FcTrue; | |
24330d27 KP |
307 | } |
308 | ||
309 | FcBool | |
327a7fd4 KP |
310 | FcGlobalCacheSave (FcGlobalCache *cache, |
311 | const FcChar8 *cache_file) | |
24330d27 | 312 | { |
1d879de2 | 313 | int fd, fd_orig; |
327a7fd4 | 314 | FcGlobalCacheDir *dir; |
327a7fd4 | 315 | FcAtomic *atomic; |
eb0cf671 PL |
316 | off_t current_arch_start = 0, truncate_to; |
317 | char * current_arch_machine_name, * header; | |
24330d27 | 318 | |
eb0cf671 | 319 | if (!cache->updated) |
24330d27 KP |
320 | return FcTrue; |
321 | ||
daeed6e0 | 322 | #if defined (HAVE_GETUID) && defined (HAVE_GETEUID) |
a391da8f KP |
323 | /* Set-UID programs can't safely update the cache */ |
324 | if (getuid () != geteuid ()) | |
325 | return FcFalse; | |
daeed6e0 | 326 | #endif |
a391da8f | 327 | |
134f6011 KP |
328 | atomic = FcAtomicCreate (cache_file); |
329 | if (!atomic) | |
1d879de2 PL |
330 | return FcFalse; |
331 | ||
134f6011 | 332 | if (!FcAtomicLock (atomic)) |
24330d27 | 333 | goto bail1; |
eb0cf671 PL |
334 | fd = open ((char *) FcAtomicNewFile(atomic), O_RDWR | O_CREAT, |
335 | S_IRUSR | S_IWUSR); | |
336 | if (fd == -1) | |
24330d27 KP |
337 | goto bail2; |
338 | ||
1d879de2 PL |
339 | fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY); |
340 | ||
eb0cf671 | 341 | current_arch_machine_name = FcCacheMachineSignature (); |
1d879de2 PL |
342 | if (fd_orig == -1) |
343 | current_arch_start = 0; | |
344 | else | |
345 | current_arch_start = FcCacheSkipToArch (fd_orig, | |
346 | current_arch_machine_name); | |
347 | ||
eb0cf671 | 348 | if (current_arch_start < 0) |
649cc361 | 349 | current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END)); |
eb0cf671 | 350 | |
1d879de2 PL |
351 | if (!FcCacheCopyOld(fd, fd_orig, current_arch_start)) |
352 | goto bail3; | |
353 | ||
354 | close (fd_orig); | |
355 | fd_orig = -1; | |
eb0cf671 PL |
356 | |
357 | current_arch_start = lseek(fd, 0, SEEK_CUR); | |
358 | if (ftruncate (fd, current_arch_start) == -1) | |
1d879de2 | 359 | goto bail3; |
eb0cf671 | 360 | |
03a212e5 PL |
361 | header = malloc (10 + strlen (current_arch_machine_name)); |
362 | if (!header) | |
1d879de2 | 363 | goto bail3; |
03a212e5 | 364 | |
8cb4c56d | 365 | truncate_to = current_arch_start + strlen(current_arch_machine_name) + 11; |
eb0cf671 | 366 | for (dir = cache->dirs; dir; dir = dir->next) |
24330d27 | 367 | { |
eb0cf671 PL |
368 | truncate_to += strlen(dir->name) + 1; |
369 | truncate_to += sizeof (FcCache); | |
370 | truncate_to = FcCacheNextOffset (current_arch_start + truncate_to); | |
371 | truncate_to += dir->metadata.count; | |
372 | } | |
373 | truncate_to -= current_arch_start; | |
eb0cf671 | 374 | |
8cb4c56d PL |
375 | sprintf (header, "%8x ", (int)truncate_to); |
376 | strcat (header, current_arch_machine_name); | |
377 | if (!FcCacheWriteString (fd, header)) | |
1d879de2 | 378 | goto bail4; |
8cb4c56d | 379 | |
eb0cf671 PL |
380 | for (dir = cache->dirs; dir; dir = dir->next) |
381 | { | |
5e678e94 PL |
382 | if (dir->ent) |
383 | { | |
384 | FcCacheWriteString (fd, dir->name); | |
385 | write (fd, &dir->metadata, sizeof(FcCache)); | |
f6ee3db5 | 386 | lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET); |
5e678e94 PL |
387 | write (fd, dir->ent, dir->metadata.count); |
388 | free (dir->ent); | |
389 | } | |
24330d27 | 390 | } |
eb0cf671 | 391 | FcCacheWriteString (fd, ""); |
24330d27 | 392 | |
eb0cf671 | 393 | if (close (fd) == -1) |
1d879de2 | 394 | goto bail25; |
24330d27 | 395 | |
134f6011 | 396 | if (!FcAtomicReplaceOrig (atomic)) |
1d879de2 | 397 | goto bail25; |
24330d27 | 398 | |
134f6011 KP |
399 | FcAtomicUnlock (atomic); |
400 | FcAtomicDestroy (atomic); | |
401 | ||
24330d27 KP |
402 | cache->updated = FcFalse; |
403 | return FcTrue; | |
404 | ||
1d879de2 PL |
405 | bail4: |
406 | free (header); | |
407 | bail3: | |
408 | if (fd_orig != -1) | |
409 | close (fd_orig); | |
410 | ||
411 | close (fd); | |
412 | bail25: | |
134f6011 | 413 | FcAtomicDeleteNew (atomic); |
1d879de2 | 414 | bail2: |
134f6011 | 415 | FcAtomicUnlock (atomic); |
1d879de2 | 416 | bail1: |
134f6011 | 417 | FcAtomicDestroy (atomic); |
24330d27 KP |
418 | return FcFalse; |
419 | } | |
420 | ||
212c9f43 | 421 | /* |
eb0cf671 PL |
422 | * Find the next presumably-mmapable offset after the supplied file |
423 | * position. | |
212c9f43 | 424 | */ |
4262e0b3 PL |
425 | static int |
426 | FcCacheNextOffset(off_t w) | |
179c3995 | 427 | { |
7fd7221e PL |
428 | static long pagesize = -1; |
429 | if (pagesize == -1) | |
430 | pagesize = sysconf(_SC_PAGESIZE); | |
431 | if (w % pagesize == 0) | |
212c9f43 PL |
432 | return w; |
433 | else | |
7fd7221e | 434 | return ((w / pagesize)+1)*pagesize; |
179c3995 KP |
435 | } |
436 | ||
212c9f43 PL |
437 | /* return the address of the segment for the provided arch, |
438 | * or -1 if arch not found */ | |
439 | static off_t | |
440 | FcCacheSkipToArch (int fd, const char * arch) | |
441 | { | |
2dbe7597 PL |
442 | char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9]; |
443 | char * candidate_arch; | |
212c9f43 PL |
444 | off_t current_arch_start = 0; |
445 | ||
446 | /* skip arches that are not the current arch */ | |
447 | while (1) | |
448 | { | |
449 | long bs; | |
450 | ||
8cb4c56d PL |
451 | if (lseek (fd, current_arch_start, SEEK_SET) != current_arch_start) |
452 | return -1; | |
453 | ||
eb0cf671 | 454 | if (FcCacheReadString (fd, candidate_arch_machine_name_count, |
2dbe7597 | 455 | sizeof (candidate_arch_machine_name_count)) == 0) |
5e678e94 | 456 | return -1; |
2dbe7597 PL |
457 | if (!strlen(candidate_arch_machine_name_count)) |
458 | return -1; | |
459 | bs = strtol(candidate_arch_machine_name_count, &candidate_arch, 16); | |
8cb4c56d | 460 | |
13cdf607 | 461 | // count = 0 should probably be distinguished from the !bs condition |
8cb4c56d PL |
462 | if (!bs || bs < strlen (candidate_arch_machine_name_count)) |
463 | return -1; | |
464 | ||
2dbe7597 | 465 | candidate_arch++; /* skip leading space */ |
212c9f43 | 466 | |
2dbe7597 | 467 | if (strcmp (candidate_arch, arch)==0) |
5e678e94 | 468 | return current_arch_start; |
212c9f43 PL |
469 | current_arch_start += bs; |
470 | } | |
471 | ||
5e678e94 | 472 | return -1; |
212c9f43 PL |
473 | } |
474 | ||
475 | /* Cuts out the segment at the file pointer (moves everything else | |
476 | * down to cover it), and leaves the file pointer at the end of the | |
477 | * file. */ | |
212c9f43 | 478 | static FcBool |
1d879de2 | 479 | FcCacheCopyOld (int fd, int fd_orig, off_t start) |
212c9f43 | 480 | { |
eb0cf671 | 481 | char * buf = malloc (8192); |
2dbe7597 | 482 | char candidate_arch_machine_name[MACHINE_SIGNATURE_SIZE + 9]; |
4262e0b3 | 483 | long bs; |
212c9f43 | 484 | int c, bytes_skipped; |
1d879de2 | 485 | off_t loc; |
212c9f43 PL |
486 | |
487 | if (!buf) | |
488 | return FcFalse; | |
489 | ||
1d879de2 PL |
490 | loc = 0; |
491 | lseek (fd, 0, SEEK_SET); lseek (fd_orig, 0, SEEK_SET); | |
492 | do | |
493 | { | |
494 | int b = 8192; | |
495 | if (loc + b > start) | |
496 | b = start - loc; | |
497 | ||
498 | if ((c = read (fd_orig, buf, b)) <= 0) | |
499 | break; | |
500 | if (write (fd, buf, c) < 0) | |
501 | goto bail; | |
502 | ||
503 | loc += c; | |
504 | } | |
505 | while (c > 0); | |
506 | ||
212c9f43 | 507 | lseek (fd, start, SEEK_SET); |
eb0cf671 | 508 | if (FcCacheReadString (fd, candidate_arch_machine_name, |
212c9f43 PL |
509 | sizeof (candidate_arch_machine_name)) == 0) |
510 | goto done; | |
2dbe7597 | 511 | if (!strlen(candidate_arch_machine_name)) |
212c9f43 PL |
512 | goto done; |
513 | ||
2dbe7597 | 514 | bs = strtol(candidate_arch_machine_name, 0, 16); |
212c9f43 PL |
515 | if (bs == 0) |
516 | goto done; | |
517 | ||
518 | bytes_skipped = 0; | |
519 | do | |
520 | { | |
521 | lseek (fd, start+bs+bytes_skipped, SEEK_SET); | |
eb0cf671 | 522 | if ((c = read (fd, buf, 8192)) <= 0) |
212c9f43 PL |
523 | break; |
524 | lseek (fd, start+bytes_skipped, SEEK_SET); | |
525 | if (write (fd, buf, c) < 0) | |
526 | goto bail; | |
527 | bytes_skipped += c; | |
528 | } | |
529 | while (c > 0); | |
530 | lseek (fd, start+bytes_skipped, SEEK_SET); | |
531 | ||
532 | done: | |
533 | free (buf); | |
534 | return FcTrue; | |
535 | ||
536 | bail: | |
537 | free (buf); | |
538 | return FcFalse; | |
539 | } | |
540 | ||
55c8fa4f | 541 | /* Does not check that the cache has the appropriate arch section. */ |
1b7be377 PL |
542 | FcBool |
543 | FcDirCacheValid (const FcChar8 *dir) | |
544 | { | |
545 | FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); | |
546 | struct stat file_stat, dir_stat; | |
547 | ||
548 | if (stat ((char *) dir, &dir_stat) < 0) | |
549 | { | |
550 | FcStrFree (cache_file); | |
551 | return FcFalse; | |
552 | } | |
553 | if (stat ((char *) cache_file, &file_stat) < 0) | |
554 | { | |
555 | FcStrFree (cache_file); | |
556 | return FcFalse; | |
557 | } | |
6bf23804 | 558 | |
55c8fa4f PL |
559 | FcStrFree (cache_file); |
560 | /* | |
561 | * If the directory has been modified more recently than | |
562 | * the cache file, the cache is not valid | |
563 | */ | |
2b25f00c | 564 | if (dir_stat.st_mtime > file_stat.st_mtime) |
55c8fa4f | 565 | return FcFalse; |
2b25f00c | 566 | |
55c8fa4f PL |
567 | return FcTrue; |
568 | } | |
569 | ||
570 | /* Assumes that the cache file in 'dir' exists. | |
571 | * Checks that the cache has the appropriate arch section. */ | |
572 | FcBool | |
573 | FcDirCacheHasCurrentArch (const FcChar8 *dir) | |
574 | { | |
575 | FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); | |
576 | int fd; | |
577 | off_t current_arch_start; | |
578 | char *current_arch_machine_name; | |
579 | ||
6bf23804 | 580 | current_arch_machine_name = FcCacheMachineSignature(); |
55c8fa4f | 581 | fd = open ((char *)cache_file, O_RDONLY); |
6bf23804 PL |
582 | if (fd == -1) |
583 | return FcFalse; | |
584 | ||
585 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); | |
586 | close (fd); | |
587 | ||
588 | if (current_arch_start < 0) | |
589 | return FcFalse; | |
55c8fa4f PL |
590 | |
591 | return FcTrue; | |
592 | } | |
6bf23804 | 593 | |
55c8fa4f PL |
594 | FcBool |
595 | FcDirCacheUnlink (const FcChar8 *dir) | |
596 | { | |
597 | FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); | |
15d7bd0a | 598 | struct stat cache_stat; |
55c8fa4f | 599 | |
15d7bd0a PL |
600 | if (stat ((char *) cache_file, &cache_stat) == 0 && |
601 | unlink ((char *)cache_file) != 0) | |
1178b569 PL |
602 | { |
603 | FcStrFree (cache_file); | |
1b7be377 | 604 | return FcFalse; |
1178b569 | 605 | } |
55c8fa4f PL |
606 | |
607 | FcStrFree (cache_file); | |
1b7be377 PL |
608 | return FcTrue; |
609 | } | |
610 | ||
4262e0b3 | 611 | static int |
1b7be377 PL |
612 | FcCacheReadDirs (FcConfig * config, FcGlobalCache * cache, |
613 | FcStrList *list, FcFontSet * set) | |
4262e0b3 | 614 | { |
4262e0b3 PL |
615 | int ret = 0; |
616 | FcChar8 *dir; | |
617 | FcChar8 *file, *base; | |
618 | FcStrSet *subdirs; | |
619 | FcStrList *sublist; | |
620 | struct stat statb; | |
621 | ||
622 | /* | |
03a212e5 | 623 | * Read in the results from 'list'. |
4262e0b3 PL |
624 | */ |
625 | while ((dir = FcStrListNext (list))) | |
626 | { | |
627 | /* freed below */ | |
628 | file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); | |
629 | if (!file) | |
630 | return FcFalse; | |
631 | ||
632 | strcpy ((char *) file, (char *) dir); | |
633 | strcat ((char *) file, "/"); | |
634 | base = file + strlen ((char *) file); | |
635 | ||
636 | subdirs = FcStrSetCreate (); | |
637 | if (!subdirs) | |
638 | { | |
639 | fprintf (stderr, "Can't create directory set\n"); | |
640 | ret++; | |
641 | free (file); | |
642 | continue; | |
643 | } | |
644 | ||
645 | if (access ((char *) dir, X_OK) < 0) | |
646 | { | |
647 | switch (errno) { | |
648 | case ENOENT: | |
649 | case ENOTDIR: | |
650 | case EACCES: | |
651 | break; | |
652 | default: | |
653 | fprintf (stderr, "\"%s\": ", dir); | |
654 | perror (""); | |
655 | ret++; | |
656 | } | |
657 | FcStrSetDestroy (subdirs); | |
658 | free (file); | |
659 | continue; | |
660 | } | |
661 | if (stat ((char *) dir, &statb) == -1) | |
662 | { | |
663 | fprintf (stderr, "\"%s\": ", dir); | |
664 | perror (""); | |
665 | FcStrSetDestroy (subdirs); | |
666 | ret++; | |
667 | free (file); | |
668 | continue; | |
669 | } | |
670 | if (!S_ISDIR (statb.st_mode)) | |
671 | { | |
672 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
673 | FcStrSetDestroy (subdirs); | |
674 | free (file); | |
675 | continue; | |
676 | } | |
2304e38f | 677 | if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir)) |
4262e0b3 | 678 | { |
1b7be377 | 679 | if (FcDebug () & FC_DBG_FONTSET) |
03a212e5 | 680 | printf ("cache scan dir %s\n", dir); |
2304e38f | 681 | |
1b7be377 PL |
682 | FcDirScanConfig (set, subdirs, cache, |
683 | config->blanks, dir, FcFalse, config); | |
4262e0b3 PL |
684 | } |
685 | sublist = FcStrListCreate (subdirs); | |
686 | FcStrSetDestroy (subdirs); | |
687 | if (!sublist) | |
688 | { | |
689 | fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir); | |
690 | ret++; | |
691 | free (file); | |
692 | continue; | |
693 | } | |
1b7be377 | 694 | ret += FcCacheReadDirs (config, cache, sublist, set); |
4262e0b3 PL |
695 | free (file); |
696 | } | |
697 | FcStrListDone (list); | |
698 | return ret; | |
699 | } | |
700 | ||
701 | FcFontSet * | |
1b7be377 | 702 | FcCacheRead (FcConfig *config, FcGlobalCache * cache) |
4262e0b3 PL |
703 | { |
704 | FcFontSet * s = FcFontSetCreate(); | |
705 | if (!s) | |
706 | return 0; | |
707 | ||
1b7be377 | 708 | if (FcCacheReadDirs (config, cache, FcConfigGetConfigDirs (config), s)) |
4262e0b3 PL |
709 | goto bail; |
710 | ||
711 | return s; | |
712 | ||
713 | bail: | |
714 | FcFontSetDestroy (s); | |
715 | return 0; | |
716 | } | |
717 | ||
212c9f43 | 718 | /* read serialized state from the cache file */ |
2eb84374 | 719 | FcBool |
2304e38f | 720 | FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir) |
212c9f43 | 721 | { |
8245771d | 722 | char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); |
4262e0b3 | 723 | int fd; |
212c9f43 | 724 | char * current_arch_machine_name; |
2dbe7597 | 725 | char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE]; |
212c9f43 | 726 | off_t current_arch_start = 0; |
8245771d | 727 | char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; |
212c9f43 | 728 | |
4262e0b3 PL |
729 | if (!cache_file) |
730 | goto bail; | |
212c9f43 | 731 | |
eb0cf671 | 732 | current_arch_machine_name = FcCacheMachineSignature(); |
4262e0b3 | 733 | fd = open(cache_file, O_RDONLY); |
212c9f43 | 734 | if (fd == -1) |
2dbe7597 | 735 | goto bail; |
212c9f43 | 736 | |
212c9f43 PL |
737 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); |
738 | if (current_arch_start < 0) | |
4262e0b3 | 739 | goto bail1; |
212c9f43 PL |
740 | |
741 | lseek (fd, current_arch_start, SEEK_SET); | |
eb0cf671 | 742 | if (FcCacheReadString (fd, candidate_arch_machine_name, |
212c9f43 | 743 | sizeof (candidate_arch_machine_name)) == 0) |
4262e0b3 | 744 | goto bail1; |
2304e38f PL |
745 | |
746 | while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0) | |
8245771d | 747 | FcStrSetAdd (dirs, (FcChar8 *)subdirName); |
2304e38f | 748 | |
e77c1718 | 749 | if (!FcDirCacheConsume (fd, (const char *)dir, set)) |
eb0cf671 PL |
750 | goto bail1; |
751 | ||
752 | close(fd); | |
753 | free (cache_file); | |
754 | return FcTrue; | |
755 | ||
756 | bail1: | |
2304e38f | 757 | close (fd); |
eb0cf671 PL |
758 | bail: |
759 | free (cache_file); | |
760 | return FcFalse; | |
761 | } | |
762 | ||
763 | static FcBool | |
e77c1718 | 764 | FcDirCacheConsume (int fd, const char * dir, FcFontSet *set) |
eb0cf671 PL |
765 | { |
766 | FcCache metadata; | |
767 | void * current_dir_block; | |
fd77c154 | 768 | off_t pos; |
212c9f43 | 769 | |
212c9f43 PL |
770 | read(fd, &metadata, sizeof(FcCache)); |
771 | if (metadata.magic != FC_CACHE_MAGIC) | |
eb0cf671 | 772 | return FcFalse; |
212c9f43 | 773 | |
2dbe7597 | 774 | if (!metadata.count) |
eb0cf671 | 775 | return FcTrue; |
2dbe7597 | 776 | |
fd77c154 | 777 | pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)); |
2dbe7597 PL |
778 | current_dir_block = mmap (0, metadata.count, |
779 | PROT_READ, MAP_SHARED, fd, pos); | |
780 | if (current_dir_block == MAP_FAILED) | |
eb0cf671 | 781 | return FcFalse; |
2dbe7597 PL |
782 | |
783 | if (!FcFontSetUnserialize (metadata, set, current_dir_block)) | |
eb0cf671 PL |
784 | return FcFalse; |
785 | ||
e77c1718 PL |
786 | FcCacheAddBankDir (metadata.bank, dir); |
787 | ||
212c9f43 | 788 | return FcTrue; |
eb0cf671 PL |
789 | } |
790 | ||
791 | static void * | |
792 | FcDirCacheProduce (FcFontSet *set, FcCache *metadata) | |
793 | { | |
794 | void * current_dir_block, * final_dir_block; | |
8245771d | 795 | static unsigned int rand_state = 0; |
eb0cf671 PL |
796 | int bank; |
797 | ||
798 | if (!rand_state) | |
799 | rand_state = time(0L); | |
800 | bank = rand_r(&rand_state); | |
801 | ||
802 | while (FcCacheHaveBank(bank)) | |
803 | bank = rand_r(&rand_state); | |
804 | ||
805 | memset (metadata, 0, sizeof(FcCache)); | |
806 | FcFontSetNewBank(); | |
7fd7221e PL |
807 | metadata->count = FcFontSetNeededBytes (set) + |
808 | FcFontSetNeededBytesAlign (); | |
eb0cf671 PL |
809 | metadata->magic = FC_CACHE_MAGIC; |
810 | metadata->bank = bank; | |
811 | ||
812 | if (!metadata->count) /* not a failure, no fonts to write */ | |
813 | return 0; | |
814 | ||
815 | current_dir_block = malloc (metadata->count); | |
816 | if (!current_dir_block) | |
817 | goto bail; | |
818 | final_dir_block = FcFontSetDistributeBytes (metadata, current_dir_block); | |
819 | ||
820 | if ((char *)current_dir_block + metadata->count != final_dir_block) | |
821 | goto bail; | |
822 | ||
823 | if (!FcFontSetSerialize (bank, set)) | |
824 | goto bail; | |
825 | ||
826 | return current_dir_block; | |
212c9f43 | 827 | |
4262e0b3 | 828 | bail: |
eb0cf671 PL |
829 | free (current_dir_block); |
830 | return 0; | |
212c9f43 PL |
831 | } |
832 | ||
833 | /* write serialized state to the cache file */ | |
834 | FcBool | |
2304e38f | 835 | FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir) |
212c9f43 | 836 | { |
4262e0b3 | 837 | FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE); |
1d879de2 PL |
838 | int fd, fd_orig, i, dirs_count; |
839 | FcAtomic *atomic; | |
840 | FcCache metadata; | |
841 | off_t current_arch_start = 0, truncate_to; | |
8245771d | 842 | |
1d879de2 PL |
843 | char *current_arch_machine_name, * header; |
844 | void *current_dir_block; | |
212c9f43 | 845 | |
4262e0b3 PL |
846 | if (!cache_file) |
847 | goto bail; | |
848 | ||
eb0cf671 | 849 | current_dir_block = FcDirCacheProduce (set, &metadata); |
212c9f43 | 850 | |
a9698bed | 851 | if (metadata.count && !current_dir_block) |
1d879de2 | 852 | goto bail0; |
212c9f43 | 853 | |
4262e0b3 PL |
854 | if (FcDebug () & FC_DBG_CACHE) |
855 | printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file); | |
856 | ||
1d879de2 PL |
857 | atomic = FcAtomicCreate (cache_file); |
858 | if (!atomic) | |
eb0cf671 | 859 | goto bail0; |
212c9f43 | 860 | |
1d879de2 PL |
861 | if (!FcAtomicLock (atomic)) |
862 | goto bail1; | |
863 | ||
864 | fd_orig = open((char *)FcAtomicOrigFile (atomic), O_RDONLY, 0666); | |
865 | ||
866 | fd = open((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0666); | |
867 | if (fd == -1) | |
868 | goto bail2; | |
869 | ||
eb0cf671 | 870 | current_arch_machine_name = FcCacheMachineSignature (); |
1d879de2 PL |
871 | current_arch_start = 0; |
872 | ||
873 | if (fd_orig != -1) | |
874 | current_arch_start = | |
875 | FcCacheSkipToArch(fd_orig, current_arch_machine_name); | |
876 | ||
212c9f43 | 877 | if (current_arch_start < 0) |
649cc361 | 878 | current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END)); |
212c9f43 | 879 | |
1d879de2 PL |
880 | if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start)) |
881 | goto bail3; | |
882 | ||
883 | if (fd_orig != -1) | |
884 | close (fd_orig); | |
212c9f43 PL |
885 | |
886 | current_arch_start = lseek(fd, 0, SEEK_CUR); | |
887 | if (ftruncate (fd, current_arch_start) == -1) | |
1d879de2 | 888 | goto bail3; |
212c9f43 | 889 | |
6aee8c6f PL |
890 | /* allocate space for subdir names in this block */ |
891 | dirs_count = 0; | |
892 | for (i = 0; i < dirs->size; i++) | |
893 | dirs_count += strlen((char *)dirs->strs[i]) + 1; | |
894 | dirs_count ++; | |
895 | ||
212c9f43 | 896 | /* now write the address of the next offset */ |
6aee8c6f | 897 | truncate_to = FcCacheNextOffset (FcCacheNextOffset (current_arch_start + sizeof (FcCache) + dirs_count) + metadata.count) - current_arch_start; |
2dbe7597 | 898 | header = malloc (10 + strlen (current_arch_machine_name)); |
eb0cf671 | 899 | if (!header) |
1d879de2 | 900 | goto bail3; |
2dbe7597 PL |
901 | sprintf (header, "%8x ", (int)truncate_to); |
902 | strcat (header, current_arch_machine_name); | |
eb0cf671 | 903 | if (!FcCacheWriteString (fd, header)) |
1d879de2 | 904 | goto bail4; |
212c9f43 | 905 | |
2304e38f | 906 | for (i = 0; i < dirs->size; i++) |
8245771d | 907 | FcCacheWriteString (fd, (char *)dirs->strs[i]); |
2304e38f PL |
908 | FcCacheWriteString (fd, ""); |
909 | ||
4262e0b3 | 910 | write (fd, &metadata, sizeof(FcCache)); |
a9698bed PL |
911 | if (metadata.count) |
912 | { | |
913 | lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_END)), SEEK_SET); | |
914 | write (fd, current_dir_block, metadata.count); | |
915 | free (current_dir_block); | |
916 | } | |
4262e0b3 | 917 | |
2dbe7597 | 918 | /* this actually serves to pad out the cache file, if needed */ |
212c9f43 | 919 | if (ftruncate (fd, current_arch_start + truncate_to) == -1) |
1d879de2 | 920 | goto bail4; |
212c9f43 PL |
921 | |
922 | close(fd); | |
1d879de2 PL |
923 | if (!FcAtomicReplaceOrig(atomic)) |
924 | goto bail4; | |
925 | FcAtomicUnlock (atomic); | |
926 | FcAtomicDestroy (atomic); | |
212c9f43 PL |
927 | return FcTrue; |
928 | ||
1d879de2 | 929 | bail4: |
0230c9f8 | 930 | free (header); |
1d879de2 PL |
931 | bail3: |
932 | close (fd); | |
933 | bail2: | |
934 | FcAtomicUnlock (atomic); | |
935 | bail1: | |
936 | FcAtomicDestroy (atomic); | |
eb0cf671 | 937 | bail0: |
8245771d | 938 | unlink ((char *)cache_file); |
4262e0b3 | 939 | free (cache_file); |
1d879de2 PL |
940 | if (current_dir_block) |
941 | free (current_dir_block); | |
942 | bail: | |
212c9f43 PL |
943 | return FcFalse; |
944 | } | |
945 | ||
2dbe7597 | 946 | static char * |
eb0cf671 | 947 | FcCacheMachineSignature () |
2dbe7597 PL |
948 | { |
949 | static char buf[MACHINE_SIGNATURE_SIZE]; | |
7fd7221e | 950 | int32_t magic = ENDIAN_TEST; |
2dbe7597 PL |
951 | char * m = (char *)&magic; |
952 | ||
953 | sprintf (buf, "%2x%2x%2x%2x " | |
954 | "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x " | |
7fd7221e | 955 | "%4x %4x %4x %4x %4x %4x %4x %4x\n", |
2dbe7597 | 956 | m[0], m[1], m[2], m[3], |
cd310911 PL |
957 | (unsigned int)sizeof (char), |
958 | (unsigned int)sizeof (char *), | |
959 | (unsigned int)sizeof (int), | |
960 | (unsigned int)sizeof (FcPattern), | |
961 | (unsigned int)sizeof (FcPatternEltPtr), | |
962 | (unsigned int)sizeof (struct _FcPatternElt *), | |
963 | (unsigned int)sizeof (FcPatternElt), | |
964 | (unsigned int)sizeof (FcObjectPtr), | |
965 | (unsigned int)sizeof (FcValueListPtr), | |
966 | (unsigned int)sizeof (FcValue), | |
967 | (unsigned int)sizeof (FcValueBinding), | |
968 | (unsigned int)sizeof (struct _FcValueList *), | |
969 | (unsigned int)sizeof (FcCharSet), | |
970 | (unsigned int)sizeof (FcCharLeaf **), | |
971 | (unsigned int)sizeof (FcChar16 *), | |
972 | (unsigned int)sizeof (FcChar16), | |
973 | (unsigned int)sizeof (FcCharLeaf), | |
974 | (unsigned int)sizeof (FcChar32), | |
7fd7221e PL |
975 | (unsigned int)sizeof (FcCache), |
976 | (unsigned int)sysconf(_SC_PAGESIZE)); | |
2dbe7597 PL |
977 | |
978 | return buf; | |
979 | } | |
980 | ||
4262e0b3 | 981 | static int banks_ptr = 0, banks_alloc = 0; |
751932dd | 982 | static int * bankId = 0, * bankIdx = 0; |
e77c1718 | 983 | static const char ** bankDirs = 0; |
4262e0b3 | 984 | |
eb0cf671 | 985 | static FcBool |
4262e0b3 PL |
986 | FcCacheHaveBank (int bank) |
987 | { | |
988 | int i; | |
989 | ||
990 | if (bank < FC_BANK_FIRST) | |
991 | return FcTrue; | |
992 | ||
993 | for (i = 0; i < banks_ptr; i++) | |
994 | if (bankId[i] == bank) | |
995 | return FcTrue; | |
996 | ||
997 | return FcFalse; | |
998 | } | |
999 | ||
1000 | int | |
1001 | FcCacheBankToIndex (int bank) | |
1002 | { | |
751932dd | 1003 | int i, j; |
4262e0b3 PL |
1004 | |
1005 | for (i = 0; i < banks_ptr; i++) | |
751932dd PL |
1006 | if (bankId[bankIdx[i]] == bank) |
1007 | { | |
1008 | int t = bankIdx[i]; | |
1009 | ||
1010 | for (j = i; j > 0; j--) | |
1011 | bankIdx[j] = bankIdx[j-1]; | |
1012 | bankIdx[0] = t; | |
1013 | return t; | |
1014 | } | |
4262e0b3 | 1015 | |
2dbe7597 | 1016 | if (banks_ptr >= banks_alloc) |
4262e0b3 | 1017 | { |
751932dd | 1018 | int * b, * bidx; |
e77c1718 PL |
1019 | const char ** bds; |
1020 | ||
2dbe7597 | 1021 | b = realloc (bankId, (banks_alloc + 4) * sizeof(int)); |
4262e0b3 PL |
1022 | if (!b) |
1023 | return -1; | |
4262e0b3 | 1024 | bankId = b; |
751932dd PL |
1025 | |
1026 | bidx = realloc (bankIdx, (banks_alloc + 4) * sizeof(int)); | |
1027 | if (!bidx) | |
1028 | return -1; | |
1029 | bankIdx = bidx; | |
1030 | ||
e77c1718 PL |
1031 | bds = realloc (bankDirs, (banks_alloc + 4) * sizeof (char *)); |
1032 | if (!bds) | |
1033 | return -1; | |
1034 | bankDirs = bds; | |
1035 | ||
4262e0b3 PL |
1036 | banks_alloc += 4; |
1037 | } | |
1038 | ||
1039 | i = banks_ptr++; | |
1040 | bankId[i] = bank; | |
751932dd | 1041 | bankIdx[i] = i; |
4262e0b3 PL |
1042 | return i; |
1043 | } | |
e77c1718 PL |
1044 | |
1045 | static void | |
1046 | FcCacheAddBankDir (int bank, const char * dir) | |
1047 | { | |
1048 | int bi = FcCacheBankToIndex (bank); | |
1049 | ||
1050 | if (bi < 0) | |
1051 | return; | |
1052 | ||
1053 | bankDirs[bi] = (const char *)FcStrCopy ((FcChar8 *)dir); | |
1054 | } | |
1055 | ||
1056 | const char * | |
1057 | FcCacheFindBankDir (int bank) | |
1058 | { | |
1059 | int bi = FcCacheBankToIndex (bank); | |
1060 | return bankDirs[bi]; | |
1061 | } | |
1062 |