From 8c8e3f93e6617ff096af97bc1ff02b0abada2aef Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Fri, 27 Jun 2014 16:57:08 +0200 Subject: [PATCH] Skip mime database update if packages are older than cache Check for the mtime of the version file, the last one to be created when updating the database to see against the mtime of the newest packages file. If one of the files inside the packages directory is newer than the version file, really update the database. Note: we'd need to add a "-f" option if we were to actually merge this patch. --- update-mime-database.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/update-mime-database.c b/update-mime-database.c index 894ac97..210b31d 100644 --- a/update-mime-database.c +++ b/update-mime-database.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -3538,6 +3539,56 @@ fclose_gerror(FILE *f, GError **error) return TRUE; } +static gint64 +newest_mtime(const char *packagedir) +{ + GDir *dir; + gint64 mtime = G_MININT64; + const char *name; + + dir = g_dir_open(packagedir, 0, NULL); + if (!dir) + return mtime; + + while ((name = g_dir_read_name(dir))) { + GStatBuf statbuf; + char *path; + int retval; + + path = g_build_filename(packagedir, name, NULL); + retval = g_stat(path, &statbuf); + g_free(path); + if (retval < 0) + continue; + if (statbuf.st_mtime > mtime) + mtime = statbuf.st_mtime; + } + + g_dir_close(dir); + return mtime; +} + +static gboolean +is_cache_up_to_date (const char *mimedir, const char *packagedir) +{ + GStatBuf version_stat; + gint64 package_mtime; + char *mimeversion; + int retval; + + mimeversion = g_build_filename(mimedir, "/version", NULL); + retval = g_stat(mimeversion, &version_stat); + g_free(mimeversion); + if (retval < 0) + return FALSE; + + package_mtime = newest_mtime(packagedir); + if (package_mtime < 0) + return FALSE; + + return version_stat.st_mtime >= package_mtime; +} + int main(int argc, char **argv) { char *mime_dir = NULL; @@ -3610,6 +3661,11 @@ int main(int argc, char **argv) return EXIT_FAILURE; } + if (is_cache_up_to_date(mime_dir, package_dir)) { + g_message ("Skipping mime update as the cache is up-to-date"); + return EXIT_SUCCESS; + } + types = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, free_type); globs_hash = g_hash_table_new_full(g_str_hash, g_str_equal, -- 2.0.0