Workaround the race condition issue on updating cache

This commit is contained in:
Akira TAGOH 2013-10-02 16:34:34 +09:00
parent 9161ed1e4a
commit 0203055520
3 changed files with 45 additions and 26 deletions

View File

@ -118,7 +118,7 @@ usage (char *program, int error)
static FcStrSet *processed_dirs; static FcStrSet *processed_dirs;
static int static int
scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, int *changed) scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force, FcBool verbose, FcBool recursive, int *changed)
{ {
int ret = 0; int ret = 0;
const FcChar8 *dir; const FcChar8 *dir;
@ -141,7 +141,7 @@ scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force,
fflush (stdout); fflush (stdout);
} }
if (FcStrSetMember (processed_dirs, dir)) if (recursive && FcStrSetMember (processed_dirs, dir))
{ {
if (verbose) if (verbose)
printf ("skipping, looped directory detected\n"); printf ("skipping, looped directory detected\n");
@ -213,32 +213,37 @@ scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force,
ret++; ret++;
} }
} }
subdirs = FcStrSetCreate (); if (recursive)
if (!subdirs)
{ {
fprintf (stderr, "%s: Can't create subdir set\n", dir); subdirs = FcStrSetCreate ();
ret++; if (!subdirs)
{
fprintf (stderr, "%s: Can't create subdir set\n", dir);
ret++;
FcDirCacheUnload (cache);
continue;
}
for (i = 0; i < FcCacheNumSubdir (cache); i++)
FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
FcDirCacheUnload (cache); FcDirCacheUnload (cache);
continue;
}
for (i = 0; i < FcCacheNumSubdir (cache); i++)
FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
FcDirCacheUnload (cache); sublist = FcStrListCreate (subdirs);
FcStrSetDestroy (subdirs);
sublist = FcStrListCreate (subdirs); if (!sublist)
FcStrSetDestroy (subdirs); {
if (!sublist) fprintf (stderr, "%s: Can't create subdir list\n", dir);
{ ret++;
fprintf (stderr, "%s: Can't create subdir list\n", dir); continue;
ret++; }
continue; FcStrSetAdd (processed_dirs, dir);
ret += scanDirs (sublist, config, force, really_force, verbose, recursive, changed);
FcStrListDone (sublist);
} }
FcStrSetAdd (processed_dirs, dir); else
ret += scanDirs (sublist, config, force, really_force, verbose, changed); FcDirCacheUnload (cache);
} }
FcStrListDone (list);
return ret; return ret;
} }
@ -366,7 +371,11 @@ main (int argc, char **argv)
} }
changed = 0; changed = 0;
ret = scanDirs (list, config, force, really_force, verbose, &changed); ret = scanDirs (list, config, force, really_force, verbose, FcTrue, &changed);
/* Update the directory cache again to avoid the race condition as much as possible */
FcStrListFirst (list);
ret += scanDirs (list, config, FcTrue, really_force, verbose, FcFalse, &changed);
FcStrListDone (list);
/* /*
* Try to create CACHEDIR.TAG anyway. * Try to create CACHEDIR.TAG anyway.
@ -379,6 +388,8 @@ main (int argc, char **argv)
cleanCacheDirectories (config, verbose); cleanCacheDirectories (config, verbose);
FcConfigDestroy (config);
FcFini ();
/* /*
* Now we need to sleep a second (or two, to be extra sure), to make * Now we need to sleep a second (or two, to be extra sure), to make
* sure that timestamps for changes after this run of fc-cache are later * sure that timestamps for changes after this run of fc-cache are later
@ -386,8 +397,7 @@ main (int argc, char **argv)
* sleep(3) can't be interrupted by a signal here -- this isn't in the * sleep(3) can't be interrupted by a signal here -- this isn't in the
* library, and there aren't any signals flying around here. * library, and there aren't any signals flying around here.
*/ */
FcConfigDestroy (config); /* the resolution of mtime on FAT is 2 seconds */
FcFini ();
if (changed) if (changed)
sleep (2); sleep (2);
if (verbose) if (verbose)

View File

@ -974,6 +974,9 @@ FcStrSetDestroy (FcStrSet *set);
FcPublic FcStrList * FcPublic FcStrList *
FcStrListCreate (FcStrSet *set); FcStrListCreate (FcStrSet *set);
FcPublic void
FcStrListFirst (FcStrList *list);
FcPublic FcChar8 * FcPublic FcChar8 *
FcStrListNext (FcStrList *list); FcStrListNext (FcStrList *list);

View File

@ -1374,6 +1374,12 @@ FcStrListCreate (FcStrSet *set)
return list; return list;
} }
void
FcStrListFirst (FcStrList *list)
{
list->n = 0;
}
FcChar8 * FcChar8 *
FcStrListNext (FcStrList *list) FcStrListNext (FcStrList *list)
{ {