testsuite: check if rootfs dir is dirty before running

Keep around a stamp-rootfs file that is generated together with the
rootfs. testsuite checks each test directory if its mtime is greater
than stamp's mtime, deciding if rootfs should be re-generated.
diff --git a/Makefile.am b/Makefile.am
index 4dba42b..223e319 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -128,9 +128,16 @@
 # ------------------------------------------------------------------------------
 
 ROOTFS = testsuite/rootfs
+ROOTFS_PRISTINE = $(top_srcdir)/testsuite/rootfs-pristine
+CREATE_ROOTFS = $(AM_V_GEN) ( $(RM) -rf $(ROOTFS) && \
+				cp -r $(ROOTFS_PRISTINE) $(ROOTFS) && \
+				touch testsuite/stamp-rootfs )
 
-$(ROOTFS): $(top_srcdir)/testsuite/rootfs-pristine
-	$(AM_V_GEN) cp -r $< $@
+rootfs:
+	$(CREATE_ROOTFS)
+
+$(ROOTFS): $(ROOTFS_PRISTINE)
+	$(CREATE_ROOTFS)
 
 TESTSUITE_OVERRIDE_LIBS = testsuite/uname.la testsuite/path.la \
 			  testsuite/init_module.la \
@@ -152,7 +159,8 @@
 TESTSUITE_CPPFLAGS = $(AM_CPPFLAGS) \
 		     -DTESTSUITE_ROOTFS=\"$(abs_top_builddir)/$(ROOTFS)/\" \
 		     -DABS_TOP_BUILDDIR=\"$(abs_top_builddir)\"
-TESTSUITE_LDADD = testsuite/libtestsuite.la libkmod/libkmod-private.la
+TESTSUITE_LDADD = testsuite/libtestsuite.la libkmod/libkmod-private.la \
+		  libkmod/libkmod-util.la
 
 check_LTLIBRARIES += testsuite/libtestsuite.la
 testsuite_libtestsuite_la_SOURCES = testsuite/testsuite.c \
@@ -167,7 +175,7 @@
 check_PROGRAMS = $(TESTSUITE)
 TESTS = $(TESTSUITE)
 
-testsuite_test_testsuite_LDADD = testsuite/libtestsuite.la
+testsuite_test_testsuite_LDADD = testsuite/libtestsuite.la libkmod/libkmod-util.la
 testsuite_test_testsuite_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
 testsuite_test_init_LDADD = $(TESTSUITE_LDADD)
 testsuite_test_init_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
@@ -189,6 +197,7 @@
 testsuite-distclean:
 	-find $(ROOTFS) -type d -exec chmod +w {} \;
 	-$(RM) -rf $(ROOTFS)
+	-$(RM) testsuite/stamp-rootfs
 
 DISTCLEAN_LOCAL_HOOKS += testsuite-distclean
 EXTRA_DIST += testsuite/rootfs-pristine
diff --git a/testsuite/.gitignore b/testsuite/.gitignore
index 8e6a6ac..61c79e1 100644
--- a/testsuite/.gitignore
+++ b/testsuite/.gitignore
@@ -12,3 +12,4 @@
 /test-testsuite
 /test-modprobe
 /rootfs
+/stamp-rootfs
diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c
index 9efe8de..c00746d 100644
--- a/testsuite/testsuite.c
+++ b/testsuite/testsuite.c
@@ -26,8 +26,10 @@
 #include <unistd.h>
 #include <sys/epoll.h>
 #include <sys/prctl.h>
+#include <sys/stat.h>
 #include <sys/wait.h>
 
+#include "libkmod-util.h"
 #include "testsuite.h"
 
 static const char *ANSI_HIGHLIGHT_GREEN_ON = "\x1B[1;32m";
@@ -218,6 +220,28 @@
 		}
 	}
 
+	if (t->config[TC_ROOTFS] != NULL) {
+		const char *stamp = TESTSUITE_ROOTFS "../stamp-rootfs";
+		const char *rootfs = t->config[TC_ROOTFS];
+		struct stat rootfsst, stampst;
+
+		if (stat(stamp, &stampst) != 0) {
+			ERR("could not stat %s\n - %m", stamp);
+			exit(EXIT_FAILURE);
+		}
+
+		if (stat(rootfs, &rootfsst) != 0) {
+			ERR("could not stat %s\n - %m", rootfs);
+			exit(EXIT_FAILURE);
+		}
+
+		if (stat_mstamp(&rootfsst) > stat_mstamp(&stampst)) {
+			ERR("rootfs %s is dirty, please run 'make rootfs' before runnning this test\n",
+								rootfs);
+			exit(EXIT_FAILURE);
+		}
+	}
+
 	if (t->need_spawn)
 		return test_spawn_test(t);
 	else