Skip to contentSkip to conversation

gen_init_cpio: allow file mtime to be overridden

PATCH New Baptiste Covolato 1 participant 1 message Oct 25, 2018 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 gen_init_cpio: allow file mtime to be overridden

By default gen_init_cpio uses the current time as mtime for links, special files and directories. We have the possibility to set this timestamp to a fixed value using -t. However, this timestamp doesn't apply to regular files. For those, the mtime returned by stat is used.

This commit introduces a '-a' option, which when used, will modify the mtime of regular files to match the timestamp of special files, directories, ...

v2: Fix typos in commit title/message

---
 usr/gen_init_cpio.c       | 19 +++++++++++++++----
 usr/gen_initramfs_list.sh |  2 +-
 2 files changed, 16 insertions(+), 5 deletions(-)
usr/gen_init_cpio.c +15 -4
index 03b21189d58b..e2432ff31965 100644
@@ -24,6 +24,7 @@
 static unsigned int offset;
 static unsigned int ino = 721;
 static time_t default_mtime;
+static int default_mtime_all;
 
 struct file_handler {
 	const char *type;
@@ -305,6 +306,7 @@ static int cpio_mkfile(const char *name, const char *location,
 	int rc = -1;
 	int namesize;
 	unsigned int i;
+	time_t mtime;
 
 	mode |= S_IFREG;
 
@@ -320,6 +322,10 @@ static int cpio_mkfile(const char *name, const char *location,
 		goto error;
 	}
 
+	mtime = buf.st_mtime;
+	if (default_mtime_all)
+		mtime = default_mtime;
+
 	filebuf = malloc(buf.st_size);
 	if (!filebuf) {
 		fprintf (stderr, "out of memory\n");
@@ -348,7 +354,7 @@ static int cpio_mkfile(const char *name, const char *location,
 			(long) uid,		/* uid */
 			(long) gid,		/* gid */
 			nlinks,			/* nlink */
-			(long) buf.st_mtime,	/* mtime */
+			(long) mtime,		/* mtime */
 			size,			/* filesize */
 			3,			/* major */
 			1,			/* minor */
@@ -452,7 +458,7 @@ static int cpio_mkfile_line(const char *line)
 static void usage(const char *prog)
 {
 	fprintf(stderr, "Usage:\n"
-		"\t%s [-t <timestamp>] <cpio_list>\n"
+		"\t%s [-t <timestamp>] [-a] <cpio_list>\n"
 		"\n"
 		"<cpio_list> is a file containing newline separated entries that\n"
 		"describe the files to be included in the initramfs archive:\n"
@@ -487,7 +493,9 @@ static void usage(const char *prog)
 		"\n"
 		"<timestamp> is time in seconds since Epoch that will be used\n"
 		"as mtime for symlinks, special files and directories. The default\n"
-		"is to use the current time for these entries.\n",
+		"is to use the current time for these entries. If -a is specified,\n"
+		"the same mtime will be used for files, overriding their original\n"
+		"mtime.\n",
 		prog);
 }
 
@@ -529,12 +537,15 @@ int main (int argc, char *argv[])
 
 	default_mtime = time(NULL);
 	while (1) {
-		int opt = getopt(argc, argv, "t:h");
+		int opt = getopt(argc, argv, "t:ha");
 		char *invalid;
 
 		if (opt == -1)
 			break;
 		switch (opt) {
+		case 'a':
+			default_mtime_all = 1;
+			break;
 		case 't':
 			default_mtime = strtol(optarg, &invalid, 10);
 			if (!*optarg || *invalid) {
usr/gen_initramfs_list.sh +1 -1
index 0aad760fcd8c..1704e0638bcc 100755
@@ -308,7 +308,7 @@ if [ ! -z ${output_file} ]; then
 		if test -n "$KBUILD_BUILD_TIMESTAMP"; then
 			timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)"
 			if test -n "$timestamp"; then
-				timestamp="-t $timestamp"
+				timestamp="-a -t $timestamp"
 			fi
 		fi
 		cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)"
Signature · 1 line
2.19.1