#define MACHINE_SIGNATURE_SIZE 9 + 5*20 + 1
static int
-FcDirCacheOpen (char * cache_file);
+FcDirCacheOpen (const FcChar8 * dir);
static char *
FcDirCacheHashName (char * cache_file, int collisions);
static off_t
-FcCacheSkipToArch (int fd, const char * arch, FcBool global);
+FcCacheSkipToArch (int fd, const char * arch);
static FcBool
FcCacheCopyOld (int fd, int fd_orig, off_t start);
FcDirCacheProduce (FcFontSet *set, FcCache * metadata);
static FcBool
-FcDirCacheConsume (int fd, const char * dir, FcFontSet *set);
-
-FcBool
-FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir);
+FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config);
static int
FcCacheNextOffset(off_t w);
static void
FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
{
+ FcStrSetDestroy (d->subdirs);
FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
free (d->name);
FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
off_t current_arch_start;
struct stat cache_stat, dir_stat;
+ char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
if (stat ((char *) cache_file, &cache_stat) < 0)
return;
cache->updated = FcFalse;
+ if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)))
+ return;
+ if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0)
+ return;
+
current_arch_machine_name = FcCacheMachineSignature ();
current_arch_start = FcCacheSkipToArch(cache->fd,
- current_arch_machine_name, FcTrue);
+ current_arch_machine_name);
if (current_arch_start < 0)
goto bail_and_destroy;
lseek (cache->fd, current_arch_start, SEEK_SET);
- FcCacheReadString (cache->fd, candidate_arch_machine_name,
- sizeof (candidate_arch_machine_name));
+ if (!FcCacheReadString (cache->fd, candidate_arch_machine_name,
+ sizeof (candidate_arch_machine_name)))
+ goto bail_and_destroy;
if (strlen(candidate_arch_machine_name) == 0)
goto bail_and_destroy;
{
off_t targ;
- FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
- if (!strlen(name_buf))
+ if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)) ||
+ !strlen(name_buf))
break;
/* Directory must be older than the global cache file; also
d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
d->ent = 0;
d->offset = lseek (cache->fd, 0, SEEK_CUR);
+
+ d->subdirs = FcStrSetCreate();
+ do
+ {
+ if (!FcCacheReadString (cache->fd, subdirName,
+ sizeof (subdirName)) ||
+ !strlen (subdirName))
+ break;
+ FcStrSetAdd (d->subdirs, (FcChar8 *)subdirName);
+ } while (1);
+
if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
goto bail1;
targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
if (cache->fd == -1)
return FcFalse;
+ if (!(dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir)))
+ return FcFalse; /* non-existing directory */
+
for (d = cache->dirs; d; d = d->next)
{
if (strncmp (d->name, dir, strlen(dir)) == 0)
{
lseek (cache->fd, d->offset, SEEK_SET);
- if (!FcDirCacheConsume (cache->fd, dir, set))
+ if (!FcDirCacheConsume (cache->fd, d->name, set, config))
return FcFalse;
if (strcmp (d->name, dir) == 0)
ret = FcTrue;
FcBool
FcGlobalCacheUpdate (FcGlobalCache *cache,
+ FcStrSet *dirs,
const char *name,
- FcFontSet *set)
+ FcFontSet *set,
+ FcConfig *config)
{
- FcGlobalCacheDir * d;
-
- if (!set->nfont)
- return FcTrue;
+ FcGlobalCacheDir *d;
+ int i;
+ name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)name);
for (d = cache->dirs; d; d = d->next)
{
if (strcmp(d->name, name) == 0)
d->name = (char *)FcStrCopy ((FcChar8 *)name);
d->ent = FcDirCacheProduce (set, &d->metadata);
d->offset = 0;
+ d->subdirs = FcStrSetCreate();
+ for (i = 0; i < dirs->num; i++)
+ FcStrSetAdd (d->subdirs, dirs->strs[i]);
return FcTrue;
}
FcBool
FcGlobalCacheSave (FcGlobalCache *cache,
- const FcChar8 *cache_file)
+ const FcChar8 *cache_file,
+ FcConfig *config)
{
- int fd, fd_orig;
+ int fd, fd_orig, i;
FcGlobalCacheDir *dir;
FcAtomic *atomic;
off_t current_arch_start = 0, truncate_to;
S_IRUSR | S_IWUSR);
if (fd == -1)
goto bail2;
+ FcCacheWriteString (fd, FC_GLOBAL_MAGIC_COOKIE);
fd_orig = open ((char *) FcAtomicOrigFile(atomic), O_RDONLY);
current_arch_start = 0;
else
current_arch_start = FcCacheSkipToArch (fd_orig,
- current_arch_machine_name,
- FcTrue);
+ current_arch_machine_name);
if (current_arch_start < 0)
- current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
+ {
+ off_t i = lseek(fd_orig, 0, SEEK_END);
+ if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
+ i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
+ current_arch_start = FcCacheNextOffset (i);
+ }
if (!FcCacheCopyOld(fd, fd_orig, current_arch_start))
goto bail3;
truncate_to += sizeof (FcCache);
truncate_to = FcCacheNextOffset (truncate_to);
truncate_to += dir->metadata.count;
+
+ for (i = 0; i < dir->subdirs->size; i++)
+ truncate_to += strlen((char *)dir->subdirs->strs[i]) + 1;
+ truncate_to ++;
}
truncate_to -= current_arch_start;
for (dir = cache->dirs; dir; dir = dir->next)
{
- if (dir->ent)
+ if (dir->name)
{
- FcCacheWriteString (fd, dir->name);
+ const char * d = (const char *)FcConfigNormalizeFontDir (config, (const FcChar8 *)dir->name);
+
+ FcCacheWriteString (fd, d);
+
+ for (i = 0; i < dir->subdirs->size; i++)
+ FcCacheWriteString (fd, (char *)dir->subdirs->strs[i]);
+ FcCacheWriteString (fd, "");
+
write (fd, &dir->metadata, sizeof(FcCache));
lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
write (fd, dir->ent, dir->metadata.count);
/* return the address of the segment for the provided arch,
* or -1 if arch not found */
static off_t
-FcCacheSkipToArch (int fd, const char * arch, FcBool global)
+FcCacheSkipToArch (int fd, const char * arch)
{
char candidate_arch_machine_name_count[MACHINE_SIGNATURE_SIZE + 9];
char * candidate_arch;
off_t current_arch_start = 0;
lseek (fd, 0, SEEK_SET);
- if (!global)
- FcCacheSkipString (fd);
+ FcCacheSkipString (fd);
current_arch_start = lseek (fd, 0, SEEK_CUR);
/* skip arches that are not the current arch */
FcBool
FcDirCacheValid (const FcChar8 *dir)
{
- FcChar8 *cache_file;
struct stat file_stat, dir_stat;
int fd;
if (stat ((char *) dir, &dir_stat) < 0)
return FcFalse;
- cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
- if (!cache_file)
- return FcFalse;
-
- fd = FcDirCacheOpen ((char *)cache_file);
+ fd = FcDirCacheOpen (dir);
if (fd < 0)
goto bail;
if (fstat (fd, &file_stat) < 0)
- goto bail1;
+ goto bail;
close (fd);
- FcStrFree (cache_file);
/*
* If the directory has been modified more recently than
return FcTrue;
- bail1:
- close (fd);
bail:
- FcStrFree (cache_file);
+ close (fd);
return FcFalse;
}
FcBool
FcDirCacheHasCurrentArch (const FcChar8 *dir)
{
- char *cache_file;
int fd;
off_t current_arch_start;
char *current_arch_machine_name;
- cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
- if (!cache_file)
- return FcFalse;
-
- fd = FcDirCacheOpen (cache_file);
+ fd = FcDirCacheOpen (dir);
if (fd < 0)
goto bail;
current_arch_machine_name = FcCacheMachineSignature();
- current_arch_start = FcCacheSkipToArch(fd,
- current_arch_machine_name, FcFalse);
+ current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
close (fd);
if (current_arch_start < 0)
return FcTrue;
bail:
- free (cache_file);
return FcFalse;
}
FcBool
-FcDirCacheUnlink (const FcChar8 *dir)
+FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
{
- char *cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
- char *cache_hashed;
+ char *cache_file;
+ char *cache_hashed = 0;
int fd, collisions;
struct stat cache_stat;
char name_buf[FC_MAX_FILE_LEN];
+ dir = FcConfigNormalizeFontDir (config, dir);
+ cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
+ if (!cache_file)
+ return FcFalse;
+
/* First remove normal cache file. */
if (stat ((char *) cache_file, &cache_stat) == 0 &&
unlink ((char *)cache_file) != 0)
fd = -1; collisions = 0;
do
{
+ if (cache_hashed)
+ FcStrFree ((FcChar8 *)cache_hashed);
+
cache_hashed = FcDirCacheHashName (cache_file, collisions++);
if (!cache_hashed)
goto bail;
return FcTrue;
}
- FcCacheReadString (fd, name_buf, sizeof (name_buf));
- if (!strlen(name_buf))
+ if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
+ {
+ FcStrFree ((FcChar8 *)cache_hashed);
goto bail;
+ }
} while (strcmp (name_buf, cache_file) != 0);
FcStrFree ((FcChar8 *)cache_file);
free (file);
continue;
}
- if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir))
+ if (!FcDirCacheValid (dir) || !FcDirCacheRead (set, subdirs, dir, config))
{
if (FcDebug () & FC_DBG_FONTSET)
printf ("cache scan dir %s\n", dir);
* This would fail in the unlikely event of a collision and subsequent
* removal of the file which originally caused the collision. */
static int
-FcDirCacheOpen (char *cache_file)
+FcDirCacheOpen (const FcChar8 *dir)
{
+ FcBool found;
int fd = -1, collisions = 0;
- char *cache_hashed;
+ char *cache_file, *cache_hashed;
char name_buf[FC_MAX_FILE_LEN];
+ struct stat dir_stat;
+
+ cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
+ if (!cache_file)
+ return -1;
fd = open(cache_file, O_RDONLY);
if (fd != -1)
return fd;
+ if (stat ((char *)dir, &dir_stat) == -1)
+ return -1;
+
+ found = FcFalse;
do
{
+ struct stat c;
+ FcChar8 * name_buf_dir;
+
cache_hashed = FcDirCacheHashName (cache_file, collisions++);
if (!cache_hashed)
+ {
+ FcStrFree ((FcChar8 *)cache_file);
return -1;
+ }
if (fd > 0)
close (fd);
FcStrFree ((FcChar8 *)cache_hashed);
if (fd == -1)
+ {
+ FcStrFree ((FcChar8 *)cache_file);
return -1;
- FcCacheReadString (fd, name_buf, sizeof (name_buf));
- if (!strlen(name_buf))
+ }
+ if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
goto bail;
- } while (strcmp (name_buf, cache_file) != 0);
+
+ name_buf_dir = FcStrDirname ((FcChar8 *)name_buf);
+ if (stat ((char *)name_buf_dir, &c) == -1)
+ {
+ FcStrFree (name_buf_dir);
+ continue;
+ }
+ FcStrFree (name_buf_dir);
+ found = (c.st_ino == dir_stat.st_ino) && (c.st_dev == dir_stat.st_dev);
+ } while (!found);
+ FcStrFree ((FcChar8 *)cache_file);
return fd;
bail:
+ FcStrFree ((FcChar8 *)cache_file);
close (fd);
return -1;
}
/* read serialized state from the cache file */
FcBool
-FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir)
+FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config)
{
- char *cache_file;
int fd;
char *current_arch_machine_name;
char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
off_t current_arch_start = 0;
char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
- cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
- if (!cache_file)
- return FcFalse;
-
- fd = FcDirCacheOpen (cache_file);
+ fd = FcDirCacheOpen (dir);
if (fd < 0)
goto bail;
current_arch_machine_name = FcCacheMachineSignature();
current_arch_start = FcCacheSkipToArch(fd,
- current_arch_machine_name,
- FcFalse);
+ current_arch_machine_name);
if (current_arch_start < 0)
goto bail1;
sizeof (candidate_arch_machine_name)) == 0)
goto bail1;
- while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
+ while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
FcStrSetAdd (dirs, (FcChar8 *)subdirName);
- if (!FcDirCacheConsume (fd, (const char *)dir, set))
+ if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
goto bail1;
close(fd);
- free (cache_file);
return FcTrue;
bail1:
close (fd);
bail:
- free (cache_file);
return FcFalse;
}
static FcBool
-FcDirCacheConsume (int fd, const char * dir, FcFontSet *set)
+FcDirCacheConsume (int fd, const char * dir, FcFontSet *set, FcConfig *config)
{
FcCache metadata;
void * current_dir_block;
return FcFalse;
if (!metadata.count)
+ {
+ pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
+ lseek (fd, pos, SEEK_SET);
+ if (config)
+ FcConfigAddFontDir (config, (FcChar8 *)dir);
return FcTrue;
+ }
pos = FcCacheNextOffset (lseek(fd, 0, SEEK_CUR));
current_dir_block = mmap (0, metadata.count,
PROT_READ, MAP_SHARED, fd, pos);
+ lseek (fd, pos+metadata.count, SEEK_SET);
if (current_dir_block == MAP_FAILED)
return FcFalse;
FcCacheAddBankDir (metadata.bank, dir);
+ if (config)
+ FcConfigAddFontDir (config, (FcChar8 *)dir);
if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
return FcFalse;
char *current_arch_machine_name, * header;
void *current_dir_block = 0;
+ dir = FcConfigNormalizeFontDir (FcConfigGetCurrent(), dir);
+ if (!dir)
+ return FcFalse;
+
cache_file = (char *)FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
if (!cache_file)
goto bail;
fd = open(cache_hashed, O_RDONLY);
if (fd == -1)
break;
- FcCacheReadString (fd, name_buf, sizeof (name_buf));
- close (fd);
-
- if (!strlen(name_buf))
+ if(!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
+ {
+ close (fd);
break;
+ }
+ close (fd);
if (strcmp (name_buf, cache_file) != 0)
continue;
if (fd_orig != -1)
current_arch_start =
- FcCacheSkipToArch(fd_orig, current_arch_machine_name, FcFalse);
+ FcCacheSkipToArch(fd_orig, current_arch_machine_name);
if (current_arch_start < 0)
- current_arch_start = FcCacheNextOffset (lseek(fd_orig, 0, SEEK_END));
+ {
+ off_t i = lseek(fd_orig, 0, SEEK_END);
+ if (i < strlen (FC_GLOBAL_MAGIC_COOKIE)+1)
+ i = strlen (FC_GLOBAL_MAGIC_COOKIE)+1;
+ current_arch_start = FcCacheNextOffset (i);
+ }
if (fd_orig != -1 && !FcCacheCopyOld(fd, fd_orig, current_arch_start))
goto bail4;
if (ftruncate (fd, current_arch_start + truncate_to) == -1)
goto bail5;
+ free (header);
close(fd);
if (!FcAtomicReplaceOrig(atomic))
goto bail5;
FcStrFree ((FcChar8 *)cache_hashed);
+ FcStrFree ((FcChar8 *)cache_file);
FcAtomicUnlock (atomic);
FcAtomicDestroy (atomic);
return FcTrue;
FcStrFree ((FcChar8 *)cache_hashed);
bail0:
unlink ((char *)cache_file);
- free (cache_file);
+ FcStrFree ((FcChar8 *)cache_file);
if (current_dir_block)
free (current_dir_block);
bail: