1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // clang-format off
16 #include "minitar.h" // NOLINT(build/include)
17
18 /*
19 * NO_CREATE implies NO_BZIP2_CREATE and NO_GZIP_CREATE and NO_COMPRESS_CREATE.
20 */
21 #ifdef NO_CREATE
22 #undef NO_BZIP2_CREATE
23 #define NO_BZIP2_CREATE
24 #undef NO_COMPRESS_CREATE
25 #define NO_COMPRESS_CREATE
26 #undef NO_GZIP_CREATE
27 #define NO_GZIP_CREATE
28 #endif
29
30 /*
31 * The combination of NO_BZIP2_CREATE and NO_BZIP2_EXTRACT is
32 * equivalent to NO_BZIP2.
33 */
34 #ifdef NO_BZIP2_CREATE
35 #ifdef NO_BZIP2_EXTRACT
36 #undef NO_BZIP2
37 #define NO_BZIP2
38 #endif
39 #endif
40
41 #ifdef NO_BZIP2
42 #undef NO_BZIP2_EXTRACT
43 #define NO_BZIP2_EXTRACT
44 #undef NO_BZIP2_CREATE
45 #define NO_BZIP2_CREATE
46 #endif
47
48 /*
49 * The combination of NO_COMPRESS_CREATE and NO_COMPRESS_EXTRACT is
50 * equivalent to NO_COMPRESS.
51 */
52 #ifdef NO_COMPRESS_CREATE
53 #ifdef NO_COMPRESS_EXTRACT
54 #undef NO_COMPRESS
55 #define NO_COMPRESS
56 #endif
57 #endif
58
59 #ifdef NO_COMPRESS
60 #undef NO_COMPRESS_EXTRACT
61 #define NO_COMPRESS_EXTRACT
62 #undef NO_COMPRESS_CREATE
63 #define NO_COMPRESS_CREATE
64 #endif
65
66 /*
67 * The combination of NO_GZIP_CREATE and NO_GZIP_EXTRACT is
68 * equivalent to NO_GZIP.
69 */
70 #ifdef NO_GZIP_CREATE
71 #ifdef NO_GZIP_EXTRACT
72 #undef NO_GZIP
73 #define NO_GZIP
74 #endif
75 #endif
76
77 #ifdef NO_GZIP
78 #undef NO_GZIP_EXTRACT
79 #define NO_GZIP_EXTRACT
80 #undef NO_GZIP_CREATE
81 #define NO_GZIP_CREATE
82 #endif
83
84 #ifndef NO_CREATE
85 void create(const char* filename, int compress, const char** argv);
86 #endif
87 void errmsg(const char*);
88 void extract(const char* filename, int do_extract, int flags);
89 int copy_data(struct archive*, struct archive*);
90 void msg(const char*);
91 void usage(void);
92
93 #ifndef NO_CREATE
94
create(const char * filename,int compress,const char ** argv,int verbose)95 void create(const char* filename, int compress, const char** argv,
96 int verbose) {
97 struct archive* a;
98 struct archive_entry* entry;
99 ssize_t len;
100 int fd;
101
102 a = archive_write_new();
103 switch (compress) {
104 #ifndef NO_BZIP2_CREATE
105 case 'j':
106 case 'y':
107 archive_write_add_filter_bzip2(a);
108 break;
109 #endif
110 #ifndef NO_COMPRESS_CREATE
111 case 'Z':
112 archive_write_add_filter_compress(a);
113 break;
114 #endif
115 #ifndef NO_GZIP_CREATE
116 case 'z':
117 archive_write_add_filter_gzip(a);
118 break;
119 #endif
120 default:
121 archive_write_add_filter_none(a);
122 break;
123 }
124 archive_write_set_format_ustar(a);
125 if (filename != NULL && strcmp(filename, "-") == 0) filename = NULL;
126 archive_write_open_filename(a, filename);
127
128 while (*argv != NULL) {
129 struct archive* disk = archive_read_disk_new();
130 #ifndef NO_LOOKUP
131 archive_read_disk_set_standard_lookup(disk);
132 #endif
133 int r;
134
135 r = archive_read_disk_open(disk, *argv);
136 if (r != ARCHIVE_OK) {
137 errmsg(archive_error_string(disk));
138 errmsg("\n");
139 exit(1);
140 }
141
142 for (;;) {
143 int needcr = 0;
144
145 entry = archive_entry_new();
146 r = archive_read_next_header2(disk, entry);
147 if (r == ARCHIVE_EOF) break;
148 if (r != ARCHIVE_OK) {
149 errmsg(archive_error_string(disk));
150 errmsg("\n");
151 exit(1);
152 }
153 archive_read_disk_descend(disk);
154 if (verbose) {
155 msg("a ");
156 msg(archive_entry_pathname(entry));
157 needcr = 1;
158 }
159 r = archive_write_header(a, entry);
160 if (r < ARCHIVE_OK) {
161 errmsg(": ");
162 errmsg(archive_error_string(a));
163 needcr = 1;
164 }
165 if (r == ARCHIVE_FATAL) exit(1);
166 if (r > ARCHIVE_FAILED) {
167 static char buff[16384];
168 fd = open(archive_entry_sourcepath(entry), O_RDONLY);
169 len = read(fd, buff, sizeof(buff));
170 while (len > 0) {
171 archive_write_data(a, buff, len);
172 len = read(fd, buff, sizeof(buff));
173 }
174 close(fd);
175 }
176 archive_entry_free(entry);
177 if (needcr) msg("\n");
178 }
179 archive_read_close(disk);
180 archive_read_free(disk);
181 argv++;
182 }
183 archive_write_close(a);
184 archive_write_free(a);
185 }
186 #endif
187
extract(const char * filename,int do_extract,int flags,int verbose)188 void extract(const char* filename, int do_extract, int flags, int verbose) {
189 struct archive* a;
190 struct archive* ext;
191 struct archive_entry* entry;
192 int r;
193
194 a = archive_read_new();
195 ext = archive_write_disk_new();
196 archive_write_disk_set_options(ext, flags);
197 #ifndef NO_BZIP2_EXTRACT
198 archive_read_support_filter_bzip2(a);
199 #endif
200 #ifndef NO_GZIP_EXTRACT
201 archive_read_support_filter_gzip(a);
202 #endif
203 #ifndef NO_COMPRESS_EXTRACT
204 archive_read_support_filter_compress(a);
205 #endif
206 #ifndef NO_TAR_EXTRACT
207 archive_read_support_format_tar(a);
208 #endif
209 #ifndef NO_CPIO_EXTRACT
210 archive_read_support_format_cpio(a);
211 #endif
212 #ifndef NO_LOOKUP
213 archive_write_disk_set_standard_lookup(ext);
214 #endif
215 if (filename != NULL && strcmp(filename, "-") == 0) filename = NULL;
216 if ((r = archive_read_open_filename(a, filename, 10240))) {
217 errmsg(archive_error_string(a));
218 errmsg("\n");
219 exit(r);
220 }
221 for (;;) {
222 int needcr = 0;
223 r = archive_read_next_header(a, &entry);
224 if (r == ARCHIVE_EOF) break;
225 if (r != ARCHIVE_OK) {
226 errmsg(archive_error_string(a));
227 errmsg("\n");
228 exit(1);
229 }
230 if (verbose && do_extract) msg("x ");
231
232 if (verbose || !do_extract) {
233 std::cout << archive_entry_pathname(entry) << std::endl;
234 msg(" ");
235 needcr = 1;
236 }
237 if (do_extract) {
238 r = archive_write_header(ext, entry);
239 if (r != ARCHIVE_OK) {
240 errmsg(archive_error_string(a));
241 needcr = 1;
242 } else {
243 r = copy_data(a, ext);
244 if (r != ARCHIVE_OK) needcr = 1;
245 }
246 }
247 if (needcr) msg("\n");
248 }
249 archive_read_close(a);
250 archive_read_free(a);
251
252 archive_write_close(ext);
253 archive_write_free(ext);
254 }
255
copy_data(struct archive * ar,struct archive * aw)256 int copy_data(struct archive* ar, struct archive* aw) {
257 int r;
258 const void* buff;
259 size_t size;
260 int64_t offset;
261
262 for (;;) {
263 r = archive_read_data_block(ar, &buff, &size, &offset);
264 if (r == ARCHIVE_EOF) return (ARCHIVE_OK);
265 if (r != ARCHIVE_OK) {
266 errmsg(archive_error_string(ar));
267 return (r);
268 }
269 r = archive_write_data_block(aw, buff, size, offset);
270 if (r != ARCHIVE_OK) {
271 errmsg(archive_error_string(ar));
272 return (r);
273 }
274 }
275 }
276
msg(const char * m)277 void msg(const char* m) { write(1, m, strlen(m)); }
278
errmsg(const char * m)279 void errmsg(const char* m) {
280 if (m == NULL) {
281 m = "Error: No error description provided.\n";
282 }
283 write(2, m, strlen(m));
284 }
285
usage(void)286 void usage(void) {
287 /* Many program options depend on compile options. */
288 const char* m =
289 "Usage: minitar [-"
290 #ifndef NO_CREATE
291 "c"
292 #endif
293 #ifndef NO_BZIP2
294 "j"
295 #endif
296 "tvx"
297 #ifndef NO_BZIP2
298 "y"
299 #endif
300 #ifndef NO_COMPRESS
301 "Z"
302 #endif
303 #ifndef NO_GZIP
304 "z"
305 #endif
306 "] [-f file] [file]\n";
307
308 errmsg(m);
309 exit(1);
310 }
311
312 // clang-format on
313