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(-)
26 #include <sys/types.h>
33 @@ -672,6 +674,7 @@ void write_volume_label(DOS_FS * fs, cha
37 + char *source_date_epoch = NULL;
41 @@ -687,8 +690,24 @@ void write_volume_label(DOS_FS * fs, cha
42 if (de.name[0] == 0xe5)
46 - mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
47 + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
48 + if (source_date_epoch) {
50 + long long conversion = 0;
52 + conversion = strtoll(source_date_epoch, &tmp, 10);
54 + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
55 + || errno != 0 || (long long)now != conversion) {
56 + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
59 + mtime = gmtime(&now);
62 + mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
65 if (mtime && mtime->tm_year >= 80 && mtime->tm_year <= 207) {
66 de.time = htole16((unsigned short)((mtime->tm_sec >> 1) +
67 (mtime->tm_min << 5) +
78 @@ -298,8 +299,21 @@ void check_atari(void)
79 uint32_t generate_volume_id(void)
82 + char *source_date_epoch = NULL;
84 - if (gettimeofday(&now, NULL) != 0 || now.tv_sec == (time_t)-1 || now.tv_sec < 0) {
85 + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
86 + if (source_date_epoch) {
88 + long long conversion = 0;
90 + conversion = strtoll(source_date_epoch, &tmp, 10);
91 + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
93 + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
96 + return (uint32_t)conversion;
97 + } else if (gettimeofday(&now, NULL) != 0 || now.tv_sec == (time_t)-1 || now.tv_sec < 0) {
99 /* rand() returns int from [0,RAND_MAX], therefore only 31 bits */
100 return (((uint32_t)(rand() & 0xFFFF)) << 16) | ((uint32_t)(rand() & 0xFFFF));
103 @@ -1074,7 +1074,7 @@ static void setup_tables(void)
106 /* If is not available then generate random 32 bit disk signature */
108 + if (invariant || getenv("SOURCE_DATE_EPOCH"))
109 disk_sig = volume_id;
111 disk_sig = generate_volume_id();
112 @@ -1287,7 +1287,7 @@ static void setup_tables(void)
114 de->attr = ATTR_VOLUME;
115 if (create_time != (time_t)-1) {
117 + if (!invariant && !getenv("SOURCE_DATE_EPOCH"))
118 ctime = localtime(&create_time);
120 ctime = gmtime(&create_time);
121 @@ -1477,6 +1477,7 @@ int main(int argc, char **argv)
122 int blocks_specified = 0;
123 struct timeval create_timeval;
124 long long conversion;
125 + char *source_date_epoch = NULL;
127 enum {OPT_HELP=1000, OPT_INVARIANT, OPT_MBR, OPT_VARIANT, OPT_CODEPAGE, OPT_OFFSET};
128 const struct option long_options[] = {
129 @@ -1497,8 +1498,20 @@ int main(int argc, char **argv)
130 program_name = p + 1;
133 - if (gettimeofday(&create_timeval, NULL) == 0 && create_timeval.tv_sec != (time_t)-1)
134 + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
135 + if (source_date_epoch) {
137 + conversion = strtoll(source_date_epoch, &tmp, 10);
138 + create_time = conversion;
139 + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
140 + || errno != 0 || (long long)create_time != conversion) {
141 + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
142 + source_date_epoch);
144 + } else if (gettimeofday(&create_timeval, NULL) == 0 && create_timeval.tv_sec != (time_t)-1) {
145 create_time = create_timeval.tv_sec;
148 volume_id = generate_volume_id();