blob: c758caa4c1df173807924bf5a6e5a15fc598e8d9 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001From 8da7bc93315cb0c32ad868f17808468b81fa76ec Mon Sep 17 00:00:00 2001
2From: =?UTF-8?q?Bj=C3=B8rn=20Forsman?= <bjorn.forsman@gmail.com>
3Date: Wed, 5 Dec 2018 19:52:51 +0100
4Subject: [PATCH] Honor the SOURCE_DATE_EPOCH variable
5MIME-Version: 1.0
6Content-Type: text/plain; charset=UTF-8
7Content-Transfer-Encoding: 8bit
8
9Implement the SOURCE_DATE_EPOCH specification[1] for reproducible
10builds. If SOURCE_DATE_EPOCH is set, use it as timestamp instead of the
11current time.
12
13[1] https://reproducible-builds.org/specs/source-date-epoch/
14
15Signed-off-by: BjΓΈrn Forsman <bjorn.forsman@gmail.com>
16---
17 src/boot.c | 23 +++++++++++++++++++++--
18 src/common.c | 18 ++++++++++++++++--
19 src/mkfs.fat.c | 19 ++++++++++++++++---
20 3 files changed, 53 insertions(+), 7 deletions(-)
21
22--- a/src/boot.c
23+++ b/src/boot.c
24@@ -33,6 +33,8 @@
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <time.h>
28+#include <errno.h>
29+#include <ctype.h>
30
31 #include "common.h"
32 #include "fsck.fat.h"
33@@ -672,6 +674,7 @@ void write_volume_label(DOS_FS * fs, cha
34 {
35 time_t now;
36 struct tm *mtime;
37+ char *source_date_epoch = NULL;
38 off_t offset;
39 int created;
40 DIR_ENT de;
41@@ -687,8 +690,24 @@ void write_volume_label(DOS_FS * fs, cha
42 if (de.name[0] == 0xe5)
43 de.name[0] = 0x05;
44
45- now = time(NULL);
46- mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
47+ source_date_epoch = getenv("SOURCE_DATE_EPOCH");
48+ if (source_date_epoch) {
49+ char *tmp = NULL;
50+ long long conversion = 0;
51+ errno = 0;
52+ conversion = strtoll(source_date_epoch, &tmp, 10);
53+ now = conversion;
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\"",
57+ source_date_epoch);
58+ }
59+ mtime = gmtime(&now);
60+ } else {
61+ now = time(NULL);
62+ mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
63+ }
64+
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) +
68--- a/src/common.c
69+++ b/src/common.c
70@@ -30,6 +30,7 @@
71 #include <string.h>
72 #include <stdarg.h>
73 #include <errno.h>
74+#include <ctype.h>
75 #include <wctype.h>
76 #include <termios.h>
77 #include <sys/time.h>
78@@ -298,8 +299,21 @@ void check_atari(void)
79 uint32_t generate_volume_id(void)
80 {
81 struct timeval now;
82+ char *source_date_epoch = NULL;
83
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) {
87+ char *tmp = NULL;
88+ long long conversion = 0;
89+ errno = 0;
90+ conversion = strtoll(source_date_epoch, &tmp, 10);
91+ if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
92+ || errno != 0) {
93+ die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
94+ source_date_epoch);
95+ }
96+ return (uint32_t)conversion;
97+ } else if (gettimeofday(&now, NULL) != 0 || now.tv_sec == (time_t)-1 || now.tv_sec < 0) {
98 srand(getpid());
99 /* rand() returns int from [0,RAND_MAX], therefore only 31 bits */
100 return (((uint32_t)(rand() & 0xFFFF)) << 16) | ((uint32_t)(rand() & 0xFFFF));
101--- a/src/mkfs.fat.c
102+++ b/src/mkfs.fat.c
103@@ -1074,7 +1074,7 @@ static void setup_tables(void)
104 }
105
106 /* If is not available then generate random 32 bit disk signature */
107- if (invariant)
108+ if (invariant || getenv("SOURCE_DATE_EPOCH"))
109 disk_sig = volume_id;
110 else if (!disk_sig)
111 disk_sig = generate_volume_id();
112@@ -1287,7 +1287,7 @@ static void setup_tables(void)
113 de->name[0] = 0x05;
114 de->attr = ATTR_VOLUME;
115 if (create_time != (time_t)-1) {
116- if (!invariant)
117+ if (!invariant && !getenv("SOURCE_DATE_EPOCH"))
118 ctime = localtime(&create_time);
119 else
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;
126
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;
131 }
132
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) {
136+ errno = 0;
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);
143+ }
144+ } else if (gettimeofday(&create_timeval, NULL) == 0 && create_timeval.tv_sec != (time_t)-1) {
145 create_time = create_timeval.tv_sec;
146+ }
147+
148 volume_id = generate_volume_id();
149 check_atari();
150