100-source-date-epoch.patch 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. --- a/src/boot.c
  19. +++ b/src/boot.c
  20. @@ -33,6 +33,8 @@
  21. #include <stdlib.h>
  22. #include <sys/types.h>
  23. #include <time.h>
  24. +#include <errno.h>
  25. +#include <ctype.h>
  26. #include "common.h"
  27. #include "fsck.fat.h"
  28. @@ -672,6 +674,7 @@ void write_volume_label(DOS_FS * fs, cha
  29. {
  30. time_t now;
  31. struct tm *mtime;
  32. + char *source_date_epoch = NULL;
  33. off_t offset;
  34. int created;
  35. DIR_ENT de;
  36. @@ -687,8 +690,24 @@ void write_volume_label(DOS_FS * fs, cha
  37. if (de.name[0] == 0xe5)
  38. de.name[0] = 0x05;
  39. - now = time(NULL);
  40. - mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
  41. + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  42. + if (source_date_epoch) {
  43. + char *tmp = NULL;
  44. + long long conversion = 0;
  45. + errno = 0;
  46. + conversion = strtoll(source_date_epoch, &tmp, 10);
  47. + now = conversion;
  48. + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
  49. + || errno != 0 || (long long)now != conversion) {
  50. + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
  51. + source_date_epoch);
  52. + }
  53. + mtime = gmtime(&now);
  54. + } else {
  55. + now = time(NULL);
  56. + mtime = (now != (time_t)-1) ? localtime(&now) : NULL;
  57. + }
  58. +
  59. if (mtime && mtime->tm_year >= 80 && mtime->tm_year <= 207) {
  60. de.time = htole16((unsigned short)((mtime->tm_sec >> 1) +
  61. (mtime->tm_min << 5) +
  62. --- a/src/common.c
  63. +++ b/src/common.c
  64. @@ -30,6 +30,7 @@
  65. #include <string.h>
  66. #include <stdarg.h>
  67. #include <errno.h>
  68. +#include <ctype.h>
  69. #include <wctype.h>
  70. #include <termios.h>
  71. #include <sys/time.h>
  72. @@ -298,8 +299,21 @@ void check_atari(void)
  73. uint32_t generate_volume_id(void)
  74. {
  75. struct timeval now;
  76. + char *source_date_epoch = NULL;
  77. - if (gettimeofday(&now, NULL) != 0 || now.tv_sec == (time_t)-1 || now.tv_sec < 0) {
  78. + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  79. + if (source_date_epoch) {
  80. + char *tmp = NULL;
  81. + long long conversion = 0;
  82. + errno = 0;
  83. + conversion = strtoll(source_date_epoch, &tmp, 10);
  84. + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
  85. + || errno != 0) {
  86. + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
  87. + source_date_epoch);
  88. + }
  89. + return (uint32_t)conversion;
  90. + } else if (gettimeofday(&now, NULL) != 0 || now.tv_sec == (time_t)-1 || now.tv_sec < 0) {
  91. srand(getpid());
  92. /* rand() returns int from [0,RAND_MAX], therefore only 31 bits */
  93. return (((uint32_t)(rand() & 0xFFFF)) << 16) | ((uint32_t)(rand() & 0xFFFF));
  94. --- a/src/mkfs.fat.c
  95. +++ b/src/mkfs.fat.c
  96. @@ -1074,7 +1074,7 @@ static void setup_tables(void)
  97. }
  98. /* If is not available then generate random 32 bit disk signature */
  99. - if (invariant)
  100. + if (invariant || getenv("SOURCE_DATE_EPOCH"))
  101. disk_sig = volume_id;
  102. else if (!disk_sig)
  103. disk_sig = generate_volume_id();
  104. @@ -1287,7 +1287,7 @@ static void setup_tables(void)
  105. de->name[0] = 0x05;
  106. de->attr = ATTR_VOLUME;
  107. if (create_time != (time_t)-1) {
  108. - if (!invariant)
  109. + if (!invariant && !getenv("SOURCE_DATE_EPOCH"))
  110. ctime = localtime(&create_time);
  111. else
  112. ctime = gmtime(&create_time);
  113. @@ -1477,6 +1477,7 @@ int main(int argc, char **argv)
  114. int blocks_specified = 0;
  115. struct timeval create_timeval;
  116. long long conversion;
  117. + char *source_date_epoch = NULL;
  118. enum {OPT_HELP=1000, OPT_INVARIANT, OPT_MBR, OPT_VARIANT, OPT_CODEPAGE, OPT_OFFSET};
  119. const struct option long_options[] = {
  120. @@ -1497,8 +1498,20 @@ int main(int argc, char **argv)
  121. program_name = p + 1;
  122. }
  123. - if (gettimeofday(&create_timeval, NULL) == 0 && create_timeval.tv_sec != (time_t)-1)
  124. + source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  125. + if (source_date_epoch) {
  126. + errno = 0;
  127. + conversion = strtoll(source_date_epoch, &tmp, 10);
  128. + create_time = conversion;
  129. + if (!isdigit((unsigned char)*source_date_epoch) || *tmp != '\0'
  130. + || errno != 0 || (long long)create_time != conversion) {
  131. + die("SOURCE_DATE_EPOCH is too big or contains non-digits: \"%s\"",
  132. + source_date_epoch);
  133. + }
  134. + } else if (gettimeofday(&create_timeval, NULL) == 0 && create_timeval.tv_sec != (time_t)-1) {
  135. create_time = create_timeval.tv_sec;
  136. + }
  137. +
  138. volume_id = generate_volume_id();
  139. check_atari();