1 From 8da7bc93315cb0c32ad868f17808468b81fa76ec Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Bj=C3=B8rn=20Forsman?= <bjorn.forsman@gmail.com>
3 Date: Wed, 5 Dec 2018 19:52:51 +0100
4 Subject: [PATCH] Honor the SOURCE_DATE_EPOCH variable
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Implement the SOURCE_DATE_EPOCH specification[1] for reproducible
10 builds. If SOURCE_DATE_EPOCH is set, use it as timestamp instead of the
13 [1] https://reproducible-builds.org/specs/source-date-epoch/
15 Signed-off-by: Bjørn Forsman <bjorn.forsman@gmail.com>
17 src/boot.c | 23 +++++++++++++++++++++--
18 src/common.c | 18 ++++++++++++++++--
19 src/mkfs.fat.c | 19 ++++++++++++++++---
20 3 files changed, 53 insertions(+), 7 deletions(-)
22 diff --git a/src/boot.c b/src/boot.c
23 index 4de450d..8f78e1c 100644
28 #include <sys/types.h>
35 @@ -672,6 +674,7 @@ void write_volume_label(DOS_FS * fs, char *label)
39 + char *source_date_epoch = NULL;
43 @@ -687,8 +690,24 @@ void write_volume_label(DOS_FS * fs, char *label)
44 if (de.name[0] == 0xe5)
48 - mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
49 + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
50 + if (source_date_epoch) {
52 + long long conversion = 0;
54 + conversion = strtoll(source_date_epoch, &tmp, 10);
56 + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
57 + || errno != 0 || (long long)now != conversion) {
58 + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
61 + mtime = gmtime(&now);
64 + mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
67 if (mtime && mtime->tm_year >= 80 && mtime->tm_year <= 207) {
68 de.time = htole16((unsigned short)((mtime->tm_sec >> 1) +
69 (mtime->tm_min << 5) +
70 diff --git a/src/common.c b/src/common.c
71 index 6a2e396..4f1afcb 100644
82 @@ -298,8 +299,21 @@ void check_atari(void)
83 uint32_t generate_volume_id(void)
87 - if (gettimeofday(&now, NULL) != 0 || now.tv_sec == (time_t)-1 || now.tv_sec < 0) {
88 + char *source_date_epoch = NULL;
90 + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
91 + if (source_date_epoch) {
93 + long long conversion = 0;
95 + conversion = strtoll(source_date_epoch, &tmp, 10);
96 + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
98 + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
101 + return (uint32_t)conversion;
102 + } else if (gettimeofday(&now, NULL) != 0 || now.tv_sec == (time_t)-1 || now.tv_sec < 0) {
104 /* rand() returns int from [0,RAND_MAX], therefore only 31 bits */
105 return (((uint32_t)(rand() & 0xFFFF)) << 16) | ((uint32_t)(rand() & 0xFFFF));
106 diff --git a/src/mkfs.fat.c b/src/mkfs.fat.c
107 index 37fc8ff..1948635 100644
110 @@ -1074,7 +1074,7 @@ static void setup_tables(void)
113 /* If is not available then generate random 32 bit disk signature */
115 + if (invariant || getenv("SOURCE_DATE_EPOCH"))
116 disk_sig = volume_id;
118 disk_sig = generate_volume_id();
119 @@ -1287,7 +1287,7 @@ static void setup_tables(void)
121 de->attr = ATTR_VOLUME;
122 if (create_time != (time_t)-1) {
124 + if (!invariant && !getenv("SOURCE_DATE_EPOCH"))
125 ctime = localtime(&create_time);
127 ctime = gmtime(&create_time);
128 @@ -1477,6 +1477,7 @@ int main(int argc, char **argv)
129 int blocks_specified = 0;
130 struct timeval create_timeval;
131 long long conversion;
132 + char *source_date_epoch = NULL;
134 enum {OPT_HELP=1000, OPT_INVARIANT, OPT_MBR, OPT_VARIANT, OPT_CODEPAGE, OPT_OFFSET};
135 const struct option long_options[] = {
136 @@ -1497,8 +1498,20 @@ int main(int argc, char **argv)
137 program_name = p + 1;
140 - if (gettimeofday(&create_timeval, NULL) == 0 && create_timeval.tv_sec != (time_t)-1)
141 + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
142 + if (source_date_epoch) {
144 + conversion = strtoll(source_date_epoch, &tmp, 10);
145 + create_time = conversion;
146 + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
147 + || errno != 0 || (long long)create_time != conversion) {
148 + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
149 + source_date_epoch);
151 + } else if (gettimeofday(&create_timeval, NULL) == 0 && create_timeval.tv_sec != (time_t)-1) {
152 create_time = create_timeval.tv_sec;
155 volume_id = generate_volume_id();