source-date-epoch.patch 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. From 8da7bc93315cb0c32ad868f17808468b81fa76ec Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Bj=C3=B8rn=20Forsman?= <[email protected]>
  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. Implement the SOURCE_DATE_EPOCH specification[1] for reproducible
  9. builds. If SOURCE_DATE_EPOCH is set, use it as timestamp instead of the
  10. current time.
  11. [1] https://reproducible-builds.org/specs/source-date-epoch/
  12. Signed-off-by: Bjørn Forsman <[email protected]>
  13. ---
  14. src/boot.c | 23 +++++++++++++++++++++--
  15. src/common.c | 18 ++++++++++++++++--
  16. src/mkfs.fat.c | 19 ++++++++++++++++---
  17. 3 files changed, 53 insertions(+), 7 deletions(-)
  18. diff --git a/src/boot.c b/src/boot.c
  19. index 4de450d..8f78e1c 100644
  20. --- a/src/boot.c
  21. +++ b/src/boot.c
  22. @@ -33,6 +33,8 @@
  23. #include <stdlib.h>
  24. #include <sys/types.h>
  25. #include <time.h>
  26. +#include <errno.h>
  27. +#include <ctype.h>
  28. #include "common.h"
  29. #include "fsck.fat.h"
  30. @@ -672,6 +674,7 @@ void write_volume_label(DOS_FS * fs, char *label)
  31. {
  32. time_t now;
  33. struct tm *mtime;
  34. + char *source_date_epoch = NULL;
  35. off_t offset;
  36. int created;
  37. DIR_ENT de;
  38. @@ -687,8 +690,24 @@ void write_volume_label(DOS_FS * fs, char *label)
  39. if (de.name[0] == 0xe5)
  40. de.name[0] = 0x05;
  41. - now = time(NULL);
  42. - mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
  43. + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  44. + if (source_date_epoch) {
  45. + char *tmp = NULL;
  46. + long long conversion = 0;
  47. + errno = 0;
  48. + conversion = strtoll(source_date_epoch, &tmp, 10);
  49. + now = conversion;
  50. + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
  51. + || errno != 0 || (long long)now != conversion) {
  52. + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
  53. + source_date_epoch);
  54. + }
  55. + mtime = gmtime(&now);
  56. + } else {
  57. + now = time(NULL);
  58. + mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
  59. + }
  60. +
  61. if (mtime && mtime->tm_year >= 80 && mtime->tm_year <= 207) {
  62. de.time = htole16((unsigned short)((mtime->tm_sec >> 1) +
  63. (mtime->tm_min << 5) +
  64. diff --git a/src/common.c b/src/common.c
  65. index 6a2e396..4f1afcb 100644
  66. --- a/src/common.c
  67. +++ b/src/common.c
  68. @@ -30,6 +30,7 @@
  69. #include <string.h>
  70. #include <stdarg.h>
  71. #include <errno.h>
  72. +#include <ctype.h>
  73. #include <wctype.h>
  74. #include <termios.h>
  75. #include <sys/time.h>
  76. @@ -298,8 +299,21 @@ void check_atari(void)
  77. uint32_t generate_volume_id(void)
  78. {
  79. struct timeval now;
  80. -
  81. - if (gettimeofday(&now, NULL) != 0 || now.tv_sec == (time_t)-1 || now.tv_sec < 0) {
  82. + char *source_date_epoch = NULL;
  83. +
  84. + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  85. + if (source_date_epoch) {
  86. + char *tmp = NULL;
  87. + long long conversion = 0;
  88. + errno = 0;
  89. + conversion = strtoll(source_date_epoch, &tmp, 10);
  90. + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
  91. + || errno != 0) {
  92. + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
  93. + source_date_epoch);
  94. + }
  95. + return (uint32_t)conversion;
  96. + } else if (gettimeofday(&now, NULL) != 0 || now.tv_sec == (time_t)-1 || now.tv_sec < 0) {
  97. srand(getpid());
  98. /* rand() returns int from [0,RAND_MAX], therefore only 31 bits */
  99. return (((uint32_t)(rand() & 0xFFFF)) << 16) | ((uint32_t)(rand() & 0xFFFF));
  100. diff --git a/src/mkfs.fat.c b/src/mkfs.fat.c
  101. index 37fc8ff..1948635 100644
  102. --- a/src/mkfs.fat.c
  103. +++ b/src/mkfs.fat.c
  104. @@ -1074,7 +1074,7 @@ static void setup_tables(void)
  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. enum {OPT_HELP=1000, OPT_INVARIANT, OPT_MBR, OPT_VARIANT, OPT_CODEPAGE, OPT_OFFSET};
  127. const struct option long_options[] = {
  128. @@ -1497,8 +1498,20 @@ int main(int argc, char **argv)
  129. program_name = p + 1;
  130. }
  131. - if (gettimeofday(&create_timeval, NULL) == 0 && create_timeval.tv_sec != (time_t)-1)
  132. + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  133. + if (source_date_epoch) {
  134. + errno = 0;
  135. + conversion = strtoll(source_date_epoch, &tmp, 10);
  136. + create_time = conversion;
  137. + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
  138. + || errno != 0 || (long long)create_time != conversion) {
  139. + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
  140. + source_date_epoch);
  141. + }
  142. + } else if (gettimeofday(&create_timeval, NULL) == 0 && create_timeval.tv_sec != (time_t)-1) {
  143. create_time = create_timeval.tv_sec;
  144. + }
  145. +
  146. volume_id = generate_volume_id();
  147. check_atari();