Fix up fc-cache and fc-cat for no global cache changes.

fc-cache and fc-cat use internal (fcint.h) APIs that have
changed with the elimination of the global cache.
This commit is contained in:
Keith Packard 2006-08-27 22:24:39 -07:00
parent 00f059e930
commit af180c4037
5 changed files with 77 additions and 140 deletions

View File

@ -219,7 +219,7 @@ scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool
if (really_force)
FcDirCacheUnlink (dir, config);
if (!FcDirScanConfig (set, subdirs, 0, FcConfigGetBlanks (config), dir, force, config))
if (!FcDirScanConfig (set, subdirs, FcConfigGetBlanks (config), dir, force, config))
{
fprintf (stderr, "\"%s\": error scanning\n", dir);
FcFontSetDestroy (set);

View File

@ -167,127 +167,37 @@ usage (char *program)
exit (1);
}
static FcBool
FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char *cache_file)
{
char name_buf[8192];
int fd;
char * current_arch_machine_name;
char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
off_t current_arch_start = 0;
if (!cache_file)
goto bail;
current_arch_machine_name = FcCacheMachineSignature();
fd = open(cache_file, O_RDONLY);
if (fd == -1)
goto bail;
current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
if (current_arch_start < 0)
goto bail1;
lseek (fd, current_arch_start, SEEK_SET);
if (FcCacheReadString (fd, candidate_arch_machine_name,
sizeof (candidate_arch_machine_name)) == 0)
goto bail1;
while (1)
{
char * dir;
FcCacheReadString (fd, name_buf, sizeof (name_buf));
if (!strlen(name_buf))
break;
printf ("fc-cat: printing global cache contents for dir %s\n",
name_buf);
do
{
if (!FcCacheReadString (fd, subdirName,
sizeof (subdirName)) ||
!strlen (subdirName))
break;
/* then don't do anything with subdirName. */
} while (1);
if (!FcDirCacheConsume (fd, name_buf, set, 0))
goto bail1;
dir = malloc (strlen (name_buf) + 2);
if (!dir)
goto bail1;
strcpy (dir, name_buf);
strcat (dir, "/");
FcCachePrintSet (set, dirs, dir);
free (dir);
FcFontSetDestroy (set);
set = FcFontSetCreate();
}
bail1:
close (fd);
bail:
return FcFalse;
}
/* read serialized state from the cache file */
static char *
FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file)
{
FILE *file;
int fd;
char * current_arch_machine_name;
off_t current_arch_start = 0;
char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
static char name_buf[8192], *dir;
static char name_buf[8192];
FcChar8 * ls;
char * buf;
int i;
if (!cache_file)
goto bail;
current_arch_machine_name = FcCacheMachineSignature();
fd = open(cache_file, O_RDONLY);
if (fd == -1)
file = fopen(cache_file, "rb");
if (file == NULL)
goto bail;
FcCacheReadString (fd, name_buf, sizeof (name_buf));
if (!strlen (name_buf))
goto bail;
if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) == 0)
goto bail;
if (!FcDirCacheConsume (file, set, dirs, NULL, name_buf))
goto bail1;
fclose (file);
printf ("fc-cat: printing directory cache for cache which would be named %s\n",
name_buf);
current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
if (current_arch_start < 0)
goto bail1;
while ((buf = FcCacheReadString (fd, subdirName, sizeof (subdirName)))
&& *buf)
FcStrSetAdd (dirs, (FcChar8 *)subdirName);
dir = strdup(name_buf);
ls = FcStrLastSlash ((FcChar8 *)dir);
if (ls)
*ls = 0;
if (!FcDirCacheConsume (fd, dir, set, 0))
goto bail2;
free (dir);
close(fd);
return name_buf;
bail2:
free (dir);
bail1:
close (fd);
fclose (file);
bail:
return 0;
}
@ -425,7 +335,7 @@ main (int argc, char **argv)
{
char * dummy_name = (char *)FcStrPlus ((FcChar8 *)argv[i],
(FcChar8 *)"/dummy");
if (!FcDirScanConfig (fs, dirs, 0, 0,
if (!FcDirScanConfig (fs, dirs, 0,
(const FcChar8 *)argv[i], FcFalse, config))
fprintf (stderr, "couldn't load font dir %s\n", argv[i]);
else
@ -439,10 +349,7 @@ main (int argc, char **argv)
FcCachePrintSet (fs, dirs, name_buf);
else
{
FcStrSetDestroy (dirs);
dirs = FcStrSetCreate ();
if (FcCacheGlobalFileReadAndPrint (fs, dirs, argv[i]))
;
printf ("nothing to do\n");
}
FcStrSetDestroy (dirs);

View File

@ -132,7 +132,7 @@ FcDirCacheValid (const FcChar8 *dir, FcConfig *config)
file = FcDirCacheOpen (config, dir, NULL);
if (file != NULL)
if (file == NULL)
return FcFalse;
fclose (file);
@ -334,26 +334,31 @@ FcDirCacheOpen (FcConfig *config, const FcChar8 *dir, FcChar8 **cache_path)
/* read serialized state from the cache file */
FcBool
FcDirCacheRead (FcFontSet * set, FcStrSet * dirs,
const FcChar8 *dir, FcConfig *config)
FcDirCacheConsume (FILE *file, FcFontSet *set, FcStrSet *dirs,
const FcChar8 *dir, char *dirname)
{
FILE *file;
FcCache metadata;
void *current_dir_block;
char subdir_name[FC_MAX_FILE_LEN + 1 + 12 + 1];
file = FcDirCacheOpen (config, dir, NULL);
if (file == NULL)
goto bail;
int i;
if (fread(&metadata, sizeof(FcCache), 1, file) != 1)
goto bail1;
goto bail;
if (metadata.magic != FC_CACHE_MAGIC)
goto bail1;
goto bail;
while (FcCacheReadString (file, subdir_name, sizeof (subdir_name)) &&
strlen (subdir_name) > 0)
FcStrSetAdd (dirs, (FcChar8 *)subdir_name);
/* skip directory name; it's just for fc-cat */
if (!FcCacheReadString (file, subdir_name, sizeof (subdir_name)))
goto bail;
if (dirname)
strcpy (dirname, subdir_name);
for (i = 0; i < metadata.subdirs; i++) {
if (!FcCacheReadString (file, subdir_name, sizeof (subdir_name)))
goto bail;
FcStrSetAdd (dirs, (FcChar8 *)subdir_name);
}
if (metadata.count)
{
@ -362,53 +367,74 @@ FcDirCacheRead (FcFontSet * set, FcStrSet * dirs,
current_dir_block = mmap (0, metadata.count,
PROT_READ, MAP_SHARED, fd, metadata.pos);
if (current_dir_block == MAP_FAILED)
goto bail1;
goto bail;
#elif defined(_WIN32)
{
HANDLE hFileMap;
hFileMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), NULL, PAGE_READONLY, 0, 0, NULL);
if (hFileMap == NULL)
goto bail1;
goto bail;
current_dir_block = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, metadata.pos, metadata.count);
if (current_dir_block == NULL)
{
CloseHandle (hFileMap);
goto bail1;
goto bail;
}
}
#else
if (lseek (fd, metatdata.pos, SEEK_SET) == -1)
goto bail1;
goto bail;
current_dir_block = malloc (metadata.count);
if (!current_dir_block)
goto bail1;
goto bail;
/* could also use CreateMappedViewOfFile under MinGW... */
if (read (fd, current_dir_block, metadata.count) != metadata.count)
{
free (current_dir_block);
goto bail1;
goto bail;
}
#endif
FcCacheAddBankDir (metadata.bank, (char *) dir);
if (!FcFontSetUnserialize (&metadata, set, current_dir_block))
goto bail1;
goto bail;
}
if (config)
FcConfigAddFontDir (config, (FcChar8 *)dir);
fclose(file);
return FcTrue;
bail1:
fclose (file);
bail:
return FcFalse;
}
FcBool
FcDirCacheRead (FcFontSet * set, FcStrSet * dirs,
const FcChar8 *dir, FcConfig *config)
{
FILE *file;
file = FcDirCacheOpen (config, dir, NULL);
if (file == NULL)
goto bail;
if (!FcDirCacheConsume (file, set, dirs, dir, NULL))
goto bail1;
if (config)
FcConfigAddFontDir (config, (FcChar8 *)dir);
fclose (file);
return FcTrue;
bail1:
fclose (file);
bail:
return FcFalse;
}
static void *
FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
{
@ -566,6 +592,7 @@ FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir, FcConfig *c
* Compute file header length -- the FcCache followed by the subdir names
*/
header_len = sizeof (FcCache);
header_len += strlen ((char *) dir) + 1;
for (i = 0; i < dirs->size; i++)
header_len += strlen ((char *)dirs->strs[i]) + 1;
@ -581,6 +608,8 @@ FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir, FcConfig *c
goto bail5;
}
FcCacheWriteString (fd, (char *) dir);
for (i = 0; i < dirs->size; i++)
FcCacheWriteString (fd, (char *)dirs->strs[i]);

View File

@ -220,14 +220,11 @@ FcDirScanConfig (FcFontSet *set,
for (i = 0; i < filelist->num; i++)
FcFileScanFontConfig (tmpSet, blanks, filelist->strs[i], config);
if (FcShouldWriteFiles ())
{
/*
* Now that the directory has been scanned,
* write out the cache file
*/
FcDirCacheWrite (tmpSet, dirlist, dir, config);
}
/*
* Now that the directory has been scanned,
* write out the cache file
*/
FcDirCacheWrite (tmpSet, dirlist, dir, config);
/*
* Add the discovered fonts to our internal non-cache list

View File

@ -422,6 +422,10 @@ FcCacheRead (FcConfig *config);
FcBool
FcDirCacheWrite (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config);
FcBool
FcDirCacheConsume (FILE *file, FcFontSet *set, FcStrSet *dirs,
const FcChar8 *dir, char *dirname);
FcBool
FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *config);