tools: zip: add option for reproducible archives
[oweals/openwrt.git] / tools / zip / patches / 011-add-option-for-reproducible-archives.patch
1 From 6d659fc87451c02c8777dc33f750b16834e4c715 Mon Sep 17 00:00:00 2001
2 From: Mathias Kresin <dev@kresin.me>
3 Date: Sat, 12 Jan 2019 19:33:33 +0100
4 Subject: [PATCH] add option for reproducible archives
5
6 Add the option -mt/--mtime to pass a timestamp which is used as filedate
7 for the containing files.
8
9 So far, it isn't used for anything written to the extra fields,
10 therefore requires the -X (eXclude eXtra file attributes) parameter to
11 be effective.
12
13 Signed-off-by: Mathias Kresin <dev@kresin.me>
14 ---
15  globals.c |  1 +
16  util.c    | 22 ++++++++++++++++++++++
17  zip.c     |  6 ++++++
18  zip.h     |  1 +
19  zipup.c   |  4 +++-
20  5 files changed, 33 insertions(+), 1 deletion(-)
21
22 --- a/globals.c
23 +++ b/globals.c
24 @@ -205,6 +205,7 @@ uzoff_t bytes_this_split = 0;     /* byt
25  int read_split_archive = 0;       /* 1=scanzipf_reg detected spanning signature */
26  int split_method = 0;             /* 0=no splits, 1=seekable, 2=data desc, -1=no */
27  uzoff_t split_size = 0;           /* how big each split should be */
28 +time_t timestamp = -1;             /* fixed timestamp for archive content filedate */
29  int split_bell = 0;               /* when pause for next split ring bell */
30  uzoff_t bytes_prev_splits = 0;    /* total bytes written to all splits before this */
31  uzoff_t bytes_this_entry = 0;     /* bytes written for this entry across all splits */
32 --- a/util.c
33 +++ b/util.c
34 @@ -1217,6 +1217,7 @@ int DisplayNumString(file, i)
35    return 0;
36  }
37  
38 +
39  /* Read numbers with trailing size multiplier (like 10M) and return number.
40     10/30/04 EG */
41  
42 @@ -1279,6 +1280,29 @@ uzoff_t ReadNumString( numstring )
43  }
44  
45  
46 +uzoff_t ReadNumStringUL( numstring )
47 +  char *numstring;
48 +{
49 +  zoff_t num = 0;
50 +
51 +  /* check if valid number (currently no negatives) */
52 +  if (numstring == NULL) {
53 +    zipwarn("Unable to read empty number in ReadNumString", "");
54 +    return (uzoff_t)-1;
55 +  }
56 +  if (numstring[0] < '0' || numstring[0] > '9') {
57 +    zipwarn("Unable to read number (must start with digit): ", numstring);
58 +    return (uzoff_t)-1;
59 +  }
60 +  if (strlen(numstring) > 10) {
61 +    zipwarn("Number too long to read (10 characters max): ", numstring);
62 +    return (uzoff_t)-1;
63 +  }
64 +
65 +  return (uzoff_t)atoll(numstring);
66 +}
67 +
68 +
69  /* Write the number as a string with a multiplier (like 10M) to outstring.
70     Always writes no more than 3 digits followed maybe by a multiplier and
71     returns the characters written or -1 if error.
72 --- a/zip.c
73 +++ b/zip.c
74 @@ -1942,6 +1942,7 @@ int set_filetype(out_path)
75  #ifdef UNICODE_TEST
76  #define o_sC            0x146
77  #endif
78 +#define o_mt            0x255
79  
80  
81  /* the below is mainly from the old main command line
82 @@ -2036,6 +2037,7 @@ struct option_struct far options[] = {
83      {"m",  "move",        o_NO_VALUE,       o_NOT_NEGATABLE, 'm',  "add files to archive then delete files"},
84      {"mm", "",            o_NO_VALUE,       o_NOT_NEGATABLE, o_mm, "not used"},
85      {"MM", "must-match",  o_NO_VALUE,       o_NOT_NEGATABLE, o_MM, "error if in file not matched/not readable"},
86 +    {"mt", "mtime",       o_REQUIRED_VALUE, o_NOT_NEGATABLE, o_mt, "use fixed timestamp for archive content filedate"},
87      {"n",  "suffixes",    o_REQUIRED_VALUE, o_NOT_NEGATABLE, 'n',  "suffixes to not compress: .gz:.zip"},
88      {"nw", "no-wild",     o_NO_VALUE,       o_NOT_NEGATABLE, o_nw, "no wildcards during add or update"},
89  #if defined(AMIGA) || defined(MACOS)
90 @@ -2440,6 +2442,7 @@ char **argv;            /* command line
91    split_method = 0;           /* 0=no splits, 1=update LHs, 2=data descriptors */
92    split_size = 0;             /* how big each split should be */
93    split_bell = 0;             /* when pause for next split ring bell */
94 +  timestamp = -1;             /* fixed timestamp for archive content filedate */
95    bytes_prev_splits = 0;      /* total bytes written to all splits before this */
96    bytes_this_entry = 0;       /* bytes written for this entry across all splits */
97    noisy_splits = 0;           /* be verbose about creating splits */
98 @@ -2897,6 +2900,9 @@ char **argv;            /* command line
99            dispose = 1;  break;
100          case o_MM:  /* Exit with error if input file can't be read */
101            bad_open_is_error = 1; break;
102 +        case o_mt:  /* fixed timestamp for archive content filedate */
103 +          timestamp = ReadNumStringUL(value);
104 +          break;
105          case 'n':   /* Don't compress files with a special suffix */
106            special = value;
107            /* special = NULL; */ /* will be set at next argument */
108 --- a/zip.h
109 +++ b/zip.h
110 @@ -502,6 +502,7 @@ extern uzoff_t bytes_this_split; /* byte
111  extern int read_split_archive;   /* 1=scanzipf_reg detected spanning signature */
112  extern int split_method;         /* 0=no splits, 1=seekable, 2=data descs, -1=no */
113  extern uzoff_t split_size;       /* how big each split should be */
114 +extern time_t timestamp;         /* fixed timestamp for archive content filedate */
115  extern int split_bell;           /* when pause for next split ring bell */
116  extern uzoff_t bytes_prev_splits; /* total bytes written to all splits before this */
117  extern uzoff_t bytes_this_entry; /* bytes written for this entry across all splits */
118 @@ -789,6 +790,7 @@ char *zip_fzofft       OF((zoff_t, char
119  int DisplayNumString OF ((FILE *file, uzoff_t i));
120  int WriteNumString OF((uzoff_t num, char *outstring));
121  uzoff_t ReadNumString OF((char *numstring));
122 +uzoff_t ReadNumStringUL OF((char *numstring));
123  
124  /* returns true if abbrev is abbreviation for string */
125  int abbrevmatch OF((char *, char *, int, int));
126 --- a/zipup.c
127 +++ b/zipup.c
128 @@ -415,7 +415,6 @@ struct zlist far *z;    /* zip entry to
129    char *tempextra = NULL;
130    char *tempcextra = NULL;
131  
132 -
133  #ifdef WINDLL
134  # ifdef ZIP64_SUPPORT
135    extern _int64 filesize64;
136 @@ -441,6 +440,9 @@ struct zlist far *z;    /* zip entry to
137    if (tim == 0 || q == (zoff_t) -3)
138      return ZE_OPEN;
139  
140 +  if (timestamp > 0)
141 +    tim = unix2dostime(&timestamp);
142 +
143    /* q is set to -1 if the input file is a device, -2 for a volume label */
144    if (q == (zoff_t) -2) {
145       isdir = 1;