Blob Blame History Raw
From fc04a1bde1941d2c61a9e33e55c5c492327674ba Mon Sep 17 00:00:00 2001
From: Jan Zerebecki <jan.suse@zerebecki.de>
Date: Thu, 15 Feb 2024 09:57:35 +0100
Subject: [PATCH 1/3] Add option to set mtime of files in rpms

to SOURCE_DATE_EPOCH.

For backwards compatibility the option clamp / limit the maximum mtime
is retained.

Setting it ouright avoids problems with an incorrectly older clock. It
also avoids problems with build scrips that incorrectly change file
mtimes when SOURCE_DATE_EPOCH_MTIME is in use.

mtimes are required to increase with new versions and releases
of an rpm with the same name, as rsync without --checksum and similar
tools would get confused if the content changes without newer mtime.

If SOURCE_DATE_EPOCH_MTIME is set use it instead for file modification time
stamps. It is supposed to be newer. This can be used if we might want to
compare if the file content remains the same when a build dependency
changes while a build script embeds SOURCE_DATE_EPOCH in the file
content.

This can be used to support automatic rebuilds. Normally automatic
rebuilds work, but together with reproducible builds an undesirable
situation may occur. If a build e.g. embeds SOURCE_DATE_EPOCH in the
output, then the output changes every time such a rebuild happens, which
can be very often. This is to be avoided as updating packages without
necessity is too expensive.
---
 build/files.c               | 33 ++++++++++++++++++++++++++++-----
 docs/manual/buildprocess.md |  5 +++--
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/build/files.c b/build/files.c
index c403c806e..cec7999ca 100644
--- a/build/files.c
+++ b/build/files.c
@@ -1033,14 +1033,34 @@ static void genCpioListAndHeader(FileList fl, Package pkg, int isSrc)
     rpm_loff_t totalFileSize = 0;
     Header h = pkg->header; /* just a shortcut */
     int override_date = 0;
+    int set_mtime = 0;
     time_t source_date_epoch = 0;
     char *srcdate = getenv("SOURCE_DATE_EPOCH");
+    char *msrcdate = getenv("SOURCE_DATE_EPOCH_MTIME");
 
-    /* Limit the maximum date to SOURCE_DATE_EPOCH if defined
-     * similar to the tar --clamp-mtime option
+    /* If SOURCE_DATE_EPOCH_MTIME is set use it for file modification time
+     * stamps, it is supposed to be newer. This can be used if we might want to
+     * compare if the file content remains the same when a build dependency
+     * changes while a build script embeds SOURCE_DATE_EPOCH in the file
+     * content. mtimes are required to increase with new versions and releases
+     * of an rpm with the same name, as rsync without --checksum and similar
+     * tools would get confused if the content changes without newer mtime. */
+    if (msrcdate != NULL) {
+	srcdate = msrcdate;
+    }
+
+    /* Set the file mtime to SOURCE_DATE_EPOCH it if requested to make the
+     * resulting rpm reproducible.
      * https://reproducible-builds.org/specs/source-date-epoch/
+     *
+     * For backwards compatibility clamp / limit the maximum mtime if requested
+     * similar the tar --clamp-mtime option. Setting it ouright avoids problems
+     * with an incorrectly older clock. It also avoids problems with build
+     * scrips that incorrectly change file mtimes when SOURCE_DATE_EPOCH_MTIME
+     * is in use.
      */
-    if (srcdate && rpmExpandNumeric("%{?clamp_mtime_to_source_date_epoch}")) {
+    if (srcdate && (rpmExpandNumeric("%{?clamp_mtime_to_source_date_epoch}")
+	|| rpmExpandNumeric("%{?set_mtime_to_source_date_epoch}"))) {
 	char *endptr;
 	errno = 0;
 	source_date_epoch = strtol(srcdate, &endptr, 10);
@@ -1049,6 +1069,9 @@ static void genCpioListAndHeader(FileList fl, Package pkg, int isSrc)
 	    fl->processingFailed = 1;
 	}
 	override_date = 1;
+	if (rpmExpandNumeric("%{?set_mtime_to_source_date_epoch}")) {
+	    set_mtime = 1;
+	}
     }
 
     /*
@@ -1191,8 +1214,8 @@ static void genCpioListAndHeader(FileList fl, Package pkg, int isSrc)
 		totalFileSize += flp->fl_size;
 	    }
 	}
-	
-	if (override_date && flp->fl_mtime > source_date_epoch) {
+
+	if (override_date && (flp->fl_mtime > source_date_epoch || set_mtime)) {
 	    flp->fl_mtime = source_date_epoch;
 	}
 	/*
diff --git a/docs/manual/buildprocess.md b/docs/manual/buildprocess.md
index 1ceb47a7e..64cd35626 100644
--- a/docs/manual/buildprocess.md
+++ b/docs/manual/buildprocess.md
@@ -94,13 +94,14 @@ Macro name                                      | Description
 `%_build_pkgcheck`                              | Progam to run on each generated binary package
 `%_build_pkcheck_set`                           | Program to run on the generated binary package set
 
-### Reproducability
+### Reproducibility
 
 Macro name                            | Description
 --------------------------------------|-----------
 `%source_date_epoch_from_changelog`   | Set `SOURCE_DATE_EPOCH` from latest `%changelog` entry
 `%use_source_date_epoch_as_buildtime` | Set package BuildTime to `SOURCE_DATE_EPOCH`
-`%clamp_mtime_to_source_date_epoch`   | Ensure file timestamps are not newer than `SOURCE_DATE_EPOCH`
+`%set_mtime_to_source_date_epoch`     | Set file modification timestamps to `SOURCE_DATE_EPOCH_MTIME` or as fallback to `SOURCE_DATE_EPOCH`
+`%clamp_mtime_to_source_date_epoch`   | You should use the above instead, it is for backwards compatibility only. Ensure file timestamps are not newer than `SOURCE_DATE_EPOCH`
 
 ### Vendor defaults
 
-- 
2.30.2