From 4b3f9f774da8859d4f1f7e991b12832d6c09b63e Mon Sep 17 00:00:00 2001
From: Bastien Nocera <hadess@hadess.net>
Date: Fri, 27 Jun 2014 16:57:08 +0200
Subject: [PATCH 13/14] 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.
---
 update-mime-database.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/update-mime-database.c b/update-mime-database.c
index 894ac97..d1849a3 100644
--- update-mime-database.c
+++ update-mime-database.c
@@ -10,6 +10,7 @@
 #include <stdio.h>
 #include <glib.h>
 #include <glib/gprintf.h>
+#include <glib/gstdio.h>
 #include <errno.h>
 #include <dirent.h>
 #include <libxml/parser.h>
@@ -3538,6 +3539,61 @@ fclose_gerror(FILE *f, GError **error)
 	return TRUE;
 }
 
+static gint64
+newest_mtime(const char *packagedir)
+{
+	GDir *dir;
+	GStatBuf statbuf;
+	gint64 mtime = G_MININT64;
+	const char *name;
+	int retval;
+
+	retval = g_stat(packagedir, &statbuf);
+	if (retval < 0)
+		return mtime;
+	mtime = statbuf.st_mtime;
+
+	dir = g_dir_open(packagedir, 0, NULL);
+	if (!dir)
+		return mtime;
+
+	while ((name = g_dir_read_name(dir))) {
+		char *path;
+
+		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 +3666,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,
-- 
1.9.3

