source-date-epoch.patch 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. 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. Upstream-Status: Backport [https://github.com/dosfstools/dosfstools/commit/8da7bc93315cb0c32ad868f17808468b81fa76ec]
  13. Signed-off-by: Bjørn Forsman <bjorn.forsman@gmail.com>
  14. ---
  15. src/boot.c | 23 +++++++++++++++++++++--
  16. src/common.c | 18 ++++++++++++++++--
  17. src/mkfs.fat.c | 19 ++++++++++++++++---
  18. 3 files changed, 53 insertions(+), 7 deletions(-)
  19. diff --git a/src/boot.c b/src/boot.c
  20. index 4de450d..8f78e1c 100644
  21. --- a/src/boot.c
  22. +++ b/src/boot.c
  23. @@ -33,6 +33,8 @@
  24. #include <stdlib.h>
  25. #include <sys/types.h>
  26. #include <time.h>
  27. +#include <errno.h>
  28. +#include <ctype.h>
  29. #include "common.h"
  30. #include "fsck.fat.h"
  31. @@ -672,6 +674,7 @@ void write_volume_label(DOS_FS * fs, char *label)
  32. {
  33. time_t now;
  34. struct tm *mtime;
  35. + char *source_date_epoch = NULL;
  36. off_t offset;
  37. int created;
  38. DIR_ENT de;
  39. @@ -687,8 +690,24 @@ void write_volume_label(DOS_FS * fs, char *label)
  40. if (de.name[0] == 0xe5)
  41. de.name[0] = 0x05;
  42. - now = time(NULL);
  43. - mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
  44. + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  45. + if (source_date_epoch) {
  46. + char *tmp = NULL;
  47. + long long conversion = 0;
  48. + errno = 0;
  49. + conversion = strtoll(source_date_epoch, &tmp, 10);
  50. + now = conversion;
  51. + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
  52. + || errno != 0 || (long long)now != conversion) {
  53. + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
  54. + source_date_epoch);
  55. + }
  56. + mtime = gmtime(&now);
  57. + } else {
  58. + now = time(NULL);
  59. + mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
  60. + }
  61. +
  62. if (mtime && mtime->tm_year >= 80 && mtime->tm_year <= 207) {
  63. de.time = htole16((unsigned short)((mtime->tm_sec >> 1) +
  64. (mtime->tm_min << 5) +
  65. diff --git a/src/common.c b/src/common.c
  66. index 6a2e396..4f1afcb 100644
  67. --- a/src/common.c
  68. +++ b/src/common.c
  69. @@ -30,6 +30,7 @@
  70. #include <string.h>
  71. #include <stdarg.h>
  72. #include <errno.h>
  73. +#include <ctype.h>
  74. #include <wctype.h>
  75. #include <termios.h>
  76. #include <sys/time.h>
  77. @@ -298,8 +299,21 @@ void check_atari(void)
  78. uint32_t generate_volume_id(void)
  79. {
  80. struct timeval now;
  81. -
  82. - if (gettimeofday(&now, NULL) != 0 || now.tv_sec == (time_t)-1 || now.tv_sec < 0) {
  83. + char *source_date_epoch = NULL;
  84. +
  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. diff --git a/src/mkfs.fat.c b/src/mkfs.fat.c
  102. index 37fc8ff..1948635 100644
  103. --- a/src/mkfs.fat.c
  104. +++ b/src/mkfs.fat.c
  105. @@ -1074,7 +1074,7 @@ static void setup_tables(void)
  106. }
  107. /* If is not available then generate random 32 bit disk signature */
  108. - if (invariant)
  109. + if (invariant || getenv("SOURCE_DATE_EPOCH"))
  110. disk_sig = volume_id;
  111. else if (!disk_sig)
  112. disk_sig = generate_volume_id();
  113. @@ -1287,7 +1287,7 @@ static void setup_tables(void)
  114. de->name[0] = 0x05;
  115. de->attr = ATTR_VOLUME;
  116. if (create_time != (time_t)-1) {
  117. - if (!invariant)
  118. + if (!invariant && !getenv("SOURCE_DATE_EPOCH"))
  119. ctime = localtime(&create_time);
  120. else
  121. ctime = gmtime(&create_time);
  122. @@ -1477,6 +1477,7 @@ int main(int argc, char **argv)
  123. int blocks_specified = 0;
  124. struct timeval create_timeval;
  125. long long conversion;
  126. + 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;
  131. }
  132. - if (gettimeofday(&create_timeval, NULL) == 0 && create_timeval.tv_sec != (time_t)-1)
  133. + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  134. + if (source_date_epoch) {
  135. + errno = 0;
  136. + conversion = strtoll(source_date_epoch, &tmp, 10);
  137. + create_time = conversion;
  138. + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
  139. + || errno != 0 || (long long)create_time != conversion) {
  140. + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
  141. + source_date_epoch);
  142. + }
  143. + } else if (gettimeofday(&create_timeval, NULL) == 0 && create_timeval.tv_sec != (time_t)-1) {
  144. create_time = create_timeval.tv_sec;
  145. + }
  146. +
  147. volume_id = generate_volume_id();
  148. check_atari();