Fix giomodule.cache being wrongly considered stale

In ostree based systems, such as flatpak and fedora silverblue, the
time of modification of every system file is epoch 0, including
giomodule.cache, which means that every module is loaded and unloaded
every time.

The solution is to use the change time of the file as well. In a typical
system, it is equal to the mtime, and in an ostree based system, since
the directory is mounted as read-only, the user cannot add a module and
we must assume that the cache file corresponds to the modules.
This commit is contained in:
Léo Stefanesco
2020-07-03 15:16:33 +02:00
parent 3bc9f57f26
commit 497c511a98

View File

@@ -462,7 +462,7 @@ g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
GDir *dir; GDir *dir;
GStatBuf statbuf; GStatBuf statbuf;
char *data; char *data;
time_t cache_mtime; time_t cache_time;
GHashTable *cache; GHashTable *cache;
if (!g_module_supported ()) if (!g_module_supported ())
@@ -477,21 +477,24 @@ g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
cache = g_hash_table_new_full (g_str_hash, g_str_equal, cache = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, (GDestroyNotify)g_strfreev); g_free, (GDestroyNotify)g_strfreev);
cache_mtime = 0; cache_time = 0;
if (g_stat (filename, &statbuf) == 0 && if (g_stat (filename, &statbuf) == 0 &&
g_file_get_contents (filename, &data, NULL, NULL)) g_file_get_contents (filename, &data, NULL, NULL))
{ {
char **lines; char **lines;
int i; int i;
/* Cache mtime is the time the cache file was created, any file /* cache_time is the time the cache file was created; we also take
* that has a ctime before this was created then and not modified * into account the change time because in ostree based systems, all
* since then (userspace can't change ctime). Its possible to change * system file have mtime equal to epoch 0.
* the ctime forward without changing the file content, by e.g. *
* chmoding the file, but this is uncommon and will only cause us * Any file that has a ctime before this was created then and not modified
* to not use the cache so will not cause bugs. * since then (userspace can't change ctime). Its possible to change the
* ctime forward without changing the file content, by e.g. chmoding the
* file, but this is uncommon and will only cause us to not use the cache
* so will not cause bugs.
*/ */
cache_mtime = statbuf.st_mtime; cache_time = MAX(statbuf.st_mtime, statbuf.st_ctime);
lines = g_strsplit (data, "\n", -1); lines = g_strsplit (data, "\n", -1);
g_free (data); g_free (data);
@@ -539,7 +542,7 @@ g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
extension_points = g_hash_table_lookup (cache, name); extension_points = g_hash_table_lookup (cache, name);
if (extension_points != NULL && if (extension_points != NULL &&
g_stat (path, &statbuf) == 0 && g_stat (path, &statbuf) == 0 &&
statbuf.st_ctime <= cache_mtime) statbuf.st_ctime <= cache_time)
{ {
/* Lazy load/init the library when first required */ /* Lazy load/init the library when first required */
for (i = 0; extension_points[i] != NULL; i++) for (i = 0; extension_points[i] != NULL; i++)