fb46753f2473e79b0c2e4932a2dc1994bb579949
[openwrt/staging/dedeckeh.git] / tools / dosfstools / patches / source-date-epoch.patch
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
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
11 current time.
12
13 [1] https://reproducible-builds.org/specs/source-date-epoch/
14
15 Signed-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 diff --git a/src/boot.c b/src/boot.c
23 index 4de450d..8f78e1c 100644
24 --- a/src/boot.c
25 +++ b/src/boot.c
26 @@ -33,6 +33,8 @@
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <time.h>
30 +#include <errno.h>
31 +#include <ctype.h>
32
33 #include "common.h"
34 #include "fsck.fat.h"
35 @@ -672,6 +674,7 @@ void write_volume_label(DOS_FS * fs, char *label)
36 {
37 time_t now;
38 struct tm *mtime;
39 + char *source_date_epoch = NULL;
40 off_t offset;
41 int created;
42 DIR_ENT de;
43 @@ -687,8 +690,24 @@ void write_volume_label(DOS_FS * fs, char *label)
44 if (de.name[0] == 0xe5)
45 de.name[0] = 0x05;
46
47 - now = time(NULL);
48 - mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
49 + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
50 + if (source_date_epoch) {
51 + char *tmp = NULL;
52 + long long conversion = 0;
53 + errno = 0;
54 + conversion = strtoll(source_date_epoch, &tmp, 10);
55 + now = conversion;
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\"",
59 + source_date_epoch);
60 + }
61 + mtime = gmtime(&now);
62 + } else {
63 + now = time(NULL);
64 + mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
65 + }
66 +
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
72 --- a/src/common.c
73 +++ b/src/common.c
74 @@ -30,6 +30,7 @@
75 #include <string.h>
76 #include <stdarg.h>
77 #include <errno.h>
78 +#include <ctype.h>
79 #include <wctype.h>
80 #include <termios.h>
81 #include <sys/time.h>
82 @@ -298,8 +299,21 @@ void check_atari(void)
83 uint32_t generate_volume_id(void)
84 {
85 struct timeval now;
86 -
87 - if (gettimeofday(&now, NULL) != 0 || now.tv_sec == (time_t)-1 || now.tv_sec < 0) {
88 + char *source_date_epoch = NULL;
89 +
90 + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
91 + if (source_date_epoch) {
92 + char *tmp = NULL;
93 + long long conversion = 0;
94 + errno = 0;
95 + conversion = strtoll(source_date_epoch, &tmp, 10);
96 + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
97 + || errno != 0) {
98 + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
99 + source_date_epoch);
100 + }
101 + return (uint32_t)conversion;
102 + } else if (gettimeofday(&now, NULL) != 0 || now.tv_sec == (time_t)-1 || now.tv_sec < 0) {
103 srand(getpid());
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
108 --- a/src/mkfs.fat.c
109 +++ b/src/mkfs.fat.c
110 @@ -1074,7 +1074,7 @@ static void setup_tables(void)
111 }
112
113 /* If is not available then generate random 32 bit disk signature */
114 - if (invariant)
115 + if (invariant || getenv("SOURCE_DATE_EPOCH"))
116 disk_sig = volume_id;
117 else if (!disk_sig)
118 disk_sig = generate_volume_id();
119 @@ -1287,7 +1287,7 @@ static void setup_tables(void)
120 de->name[0] = 0x05;
121 de->attr = ATTR_VOLUME;
122 if (create_time != (time_t)-1) {
123 - if (!invariant)
124 + if (!invariant && !getenv("SOURCE_DATE_EPOCH"))
125 ctime = localtime(&create_time);
126 else
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;
133
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;
138 }
139
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) {
143 + errno = 0;
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);
150 + }
151 + } else if (gettimeofday(&create_timeval, NULL) == 0 && create_timeval.tv_sec != (time_t)-1) {
152 create_time = create_timeval.tv_sec;
153 + }
154 +
155 volume_id = generate_volume_id();
156 check_atari();
157