1 /*-
2 * This file is in the public domain.
3 * Do with it as you will.
4 */
5
6 /*-
7 * This is a compact "tar" program whose primary goal is small size.
8 * Statically linked, it can be very small indeed. This serves a number
9 * of goals:
10 * o a testbed for libarchive (to check for link pollution),
11 * o a useful tool for space-constrained systems (boot floppies, etc),
12 * o a place to experiment with new implementation ideas for bsdtar,
13 * o a small program to demonstrate libarchive usage.
14 *
15 * Use the following macros to suppress features:
16 * NO_BZIP2 - Implies NO_BZIP2_CREATE and NO_BZIP2_EXTRACT
17 * NO_BZIP2_CREATE - Suppress bzip2 compression support.
18 * NO_BZIP2_EXTRACT - Suppress bzip2 auto-detection and decompression.
19 * NO_COMPRESS - Implies NO_COMPRESS_CREATE and NO_COMPRESS_EXTRACT
20 * NO_COMPRESS_CREATE - Suppress compress(1) compression support
21 * NO_COMPRESS_EXTRACT - Suppress compress(1) auto-detect and decompression.
22 * NO_CREATE - Suppress all archive creation support.
23 * NO_CPIO_EXTRACT - Suppress auto-detect and dearchiving of cpio archives.
24 * NO_GZIP - Implies NO_GZIP_CREATE and NO_GZIP_EXTRACT
25 * NO_GZIP_CREATE - Suppress gzip compression support.
26 * NO_GZIP_EXTRACT - Suppress gzip auto-detection and decompression.
27 * NO_LOOKUP - Try to avoid getpw/getgr routines, which can be very large
28 * NO_TAR_EXTRACT - Suppress tar extraction
29 *
30 * With all of the above macros defined (except NO_TAR_EXTRACT), you
31 * get a very small program that can recognize and extract essentially
32 * any uncompressed tar archive. On FreeBSD 5.1, this minimal program
33 * is under 64k, statically linked, which compares rather favorably to
34 * main(){printf("hello, world");}
35 * which is over 60k statically linked on the same operating system.
36 * Without any of the above macros, you get a static executable of
37 * about 180k with a lot of very sophisticated modern features.
38 * Obviously, it's trivial to add support for ISO, Zip, mtree,
39 * lzma/xz, etc. Just fill in the appropriate setup calls.
40 */
41
42 #include <sys/types.h>
43 #include <sys/stat.h>
44
45 #include <archive.h>
46 #include <archive_entry.h>
47 #include <fcntl.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 /*
54 * NO_CREATE implies NO_BZIP2_CREATE and NO_GZIP_CREATE and NO_COMPRESS_CREATE.
55 */
56 #ifdef NO_CREATE
57 #undef NO_BZIP2_CREATE
58 #define NO_BZIP2_CREATE
59 #undef NO_COMPRESS_CREATE
60 #define NO_COMPRESS_CREATE
61 #undef NO_GZIP_CREATE
62 #define NO_GZIP_CREATE
63 #endif
64
65 /*
66 * The combination of NO_BZIP2_CREATE and NO_BZIP2_EXTRACT is
67 * equivalent to NO_BZIP2.
68 */
69 #ifdef NO_BZIP2_CREATE
70 #ifdef NO_BZIP2_EXTRACT
71 #undef NO_BZIP2
72 #define NO_BZIP2
73 #endif
74 #endif
75
76 #ifdef NO_BZIP2
77 #undef NO_BZIP2_EXTRACT
78 #define NO_BZIP2_EXTRACT
79 #undef NO_BZIP2_CREATE
80 #define NO_BZIP2_CREATE
81 #endif
82
83 /*
84 * The combination of NO_COMPRESS_CREATE and NO_COMPRESS_EXTRACT is
85 * equivalent to NO_COMPRESS.
86 */
87 #ifdef NO_COMPRESS_CREATE
88 #ifdef NO_COMPRESS_EXTRACT
89 #undef NO_COMPRESS
90 #define NO_COMPRESS
91 #endif
92 #endif
93
94 #ifdef NO_COMPRESS
95 #undef NO_COMPRESS_EXTRACT
96 #define NO_COMPRESS_EXTRACT
97 #undef NO_COMPRESS_CREATE
98 #define NO_COMPRESS_CREATE
99 #endif
100
101 /*
102 * The combination of NO_GZIP_CREATE and NO_GZIP_EXTRACT is
103 * equivalent to NO_GZIP.
104 */
105 #ifdef NO_GZIP_CREATE
106 #ifdef NO_GZIP_EXTRACT
107 #undef NO_GZIP
108 #define NO_GZIP
109 #endif
110 #endif
111
112 #ifdef NO_GZIP
113 #undef NO_GZIP_EXTRACT
114 #define NO_GZIP_EXTRACT
115 #undef NO_GZIP_CREATE
116 #define NO_GZIP_CREATE
117 #endif
118
119 #ifndef NO_CREATE
120 static void create(const char *filename, int compress, const char **argv);
121 #endif
122 static void errmsg(const char *);
123 static void extract(const char *filename, int do_extract, int flags);
124 static int copy_data(struct archive *, struct archive *);
125 static void msg(const char *);
126 static void usage(void);
127
128 static int verbose = 0;
129
130 int
main(int argc,const char ** argv)131 main(int argc, const char **argv)
132 {
133 const char *filename = NULL;
134 int compress, flags, mode, opt;
135
136 (void)argc;
137 mode = 'x';
138 verbose = 0;
139 compress = '\0';
140 flags = ARCHIVE_EXTRACT_TIME;
141
142 /* Among other sins, getopt(3) pulls in printf(3). */
143 while (*++argv != NULL && **argv == '-') {
144 const char *p = *argv + 1;
145
146 while ((opt = *p++) != '\0') {
147 switch (opt) {
148 #ifndef NO_CREATE
149 case 'c':
150 mode = opt;
151 break;
152 #endif
153 case 'f':
154 if (*p != '\0')
155 filename = p;
156 else
157 filename = *++argv;
158 p += strlen(p);
159 break;
160 #ifndef NO_BZIP2_CREATE
161 case 'j':
162 compress = opt;
163 break;
164 #endif
165 case 'p':
166 flags |= ARCHIVE_EXTRACT_PERM;
167 flags |= ARCHIVE_EXTRACT_ACL;
168 flags |= ARCHIVE_EXTRACT_FFLAGS;
169 break;
170 case 't':
171 mode = opt;
172 break;
173 case 'v':
174 verbose++;
175 break;
176 case 'x':
177 mode = opt;
178 break;
179 #ifndef NO_BZIP2_CREATE
180 case 'y':
181 compress = opt;
182 break;
183 #endif
184 #ifndef NO_COMPRESS_CREATE
185 case 'Z':
186 compress = opt;
187 break;
188 #endif
189 #ifndef NO_GZIP_CREATE
190 case 'z':
191 compress = opt;
192 break;
193 #endif
194 default:
195 usage();
196 }
197 }
198 }
199
200 switch (mode) {
201 #ifndef NO_CREATE
202 case 'c':
203 create(filename, compress, argv);
204 break;
205 #endif
206 case 't':
207 extract(filename, 0, flags);
208 break;
209 case 'x':
210 extract(filename, 1, flags);
211 break;
212 }
213
214 return (0);
215 }
216
217
218 #ifndef NO_CREATE
219 static char buff[16384];
220
221 static void
create(const char * filename,int compress,const char ** argv)222 create(const char *filename, int compress, const char **argv)
223 {
224 struct archive *a;
225 struct archive_entry *entry;
226 ssize_t len;
227 int fd;
228
229 a = archive_write_new();
230 switch (compress) {
231 #ifndef NO_BZIP2_CREATE
232 case 'j': case 'y':
233 archive_write_add_filter_bzip2(a);
234 break;
235 #endif
236 #ifndef NO_COMPRESS_CREATE
237 case 'Z':
238 archive_write_add_filter_compress(a);
239 break;
240 #endif
241 #ifndef NO_GZIP_CREATE
242 case 'z':
243 archive_write_add_filter_gzip(a);
244 break;
245 #endif
246 default:
247 archive_write_add_filter_none(a);
248 break;
249 }
250 archive_write_set_format_ustar(a);
251 if (filename != NULL && strcmp(filename, "-") == 0)
252 filename = NULL;
253 archive_write_open_filename(a, filename);
254
255 while (*argv != NULL) {
256 struct archive *disk = archive_read_disk_new();
257 #ifndef NO_LOOKUP
258 archive_read_disk_set_standard_lookup(disk);
259 #endif
260 int r;
261
262 r = archive_read_disk_open(disk, *argv);
263 if (r != ARCHIVE_OK) {
264 errmsg(archive_error_string(disk));
265 errmsg("\n");
266 exit(1);
267 }
268
269 for (;;) {
270 int needcr = 0;
271
272 entry = archive_entry_new();
273 r = archive_read_next_header2(disk, entry);
274 if (r == ARCHIVE_EOF)
275 break;
276 if (r != ARCHIVE_OK) {
277 errmsg(archive_error_string(disk));
278 errmsg("\n");
279 exit(1);
280 }
281 archive_read_disk_descend(disk);
282 if (verbose) {
283 msg("a ");
284 msg(archive_entry_pathname(entry));
285 needcr = 1;
286 }
287 r = archive_write_header(a, entry);
288 if (r < ARCHIVE_OK) {
289 errmsg(": ");
290 errmsg(archive_error_string(a));
291 needcr = 1;
292 }
293 if (r == ARCHIVE_FATAL)
294 exit(1);
295 if (r > ARCHIVE_FAILED) {
296 #if 0
297 /* Ideally, we would be able to use
298 * the same code to copy a body from
299 * an archive_read_disk to an
300 * archive_write that we use for
301 * copying data from an archive_read
302 * to an archive_write_disk.
303 * Unfortunately, this doesn't quite
304 * work yet. */
305 copy_data(disk, a);
306 #else
307 /* For now, we use a simpler loop to copy data
308 * into the target archive. */
309 fd = open(archive_entry_sourcepath(entry), O_RDONLY);
310 len = read(fd, buff, sizeof(buff));
311 while (len > 0) {
312 archive_write_data(a, buff, len);
313 len = read(fd, buff, sizeof(buff));
314 }
315 close(fd);
316 #endif
317 }
318 archive_entry_free(entry);
319 if (needcr)
320 msg("\n");
321 }
322 archive_read_close(disk);
323 archive_read_free(disk);
324 argv++;
325 }
326 archive_write_close(a);
327 archive_write_free(a);
328 }
329 #endif
330
331 static void
extract(const char * filename,int do_extract,int flags)332 extract(const char *filename, int do_extract, int flags)
333 {
334 struct archive *a;
335 struct archive *ext;
336 struct archive_entry *entry;
337 int r;
338
339 a = archive_read_new();
340 ext = archive_write_disk_new();
341 archive_write_disk_set_options(ext, flags);
342 #ifndef NO_BZIP2_EXTRACT
343 archive_read_support_filter_bzip2(a);
344 #endif
345 #ifndef NO_GZIP_EXTRACT
346 archive_read_support_filter_gzip(a);
347 #endif
348 #ifndef NO_COMPRESS_EXTRACT
349 archive_read_support_filter_compress(a);
350 #endif
351 #ifndef NO_TAR_EXTRACT
352 archive_read_support_format_tar(a);
353 #endif
354 #ifndef NO_CPIO_EXTRACT
355 archive_read_support_format_cpio(a);
356 #endif
357 #ifndef NO_LOOKUP
358 archive_write_disk_set_standard_lookup(ext);
359 #endif
360 if (filename != NULL && strcmp(filename, "-") == 0)
361 filename = NULL;
362 if ((r = archive_read_open_filename(a, filename, 10240))) {
363 errmsg(archive_error_string(a));
364 errmsg("\n");
365 exit(r);
366 }
367 for (;;) {
368 int needcr = 0;
369 r = archive_read_next_header(a, &entry);
370 if (r == ARCHIVE_EOF)
371 break;
372 if (r != ARCHIVE_OK) {
373 errmsg(archive_error_string(a));
374 errmsg("\n");
375 exit(1);
376 }
377 if (verbose && do_extract)
378 msg("x ");
379 if (verbose || !do_extract) {
380 msg(archive_entry_pathname(entry));
381 msg(" ");
382 needcr = 1;
383 }
384 if (do_extract) {
385 r = archive_write_header(ext, entry);
386 if (r != ARCHIVE_OK) {
387 errmsg(archive_error_string(a));
388 needcr = 1;
389 }
390 else {
391 r = copy_data(a, ext);
392 if (r != ARCHIVE_OK)
393 needcr = 1;
394 }
395 }
396 if (needcr)
397 msg("\n");
398 }
399 archive_read_close(a);
400 archive_read_free(a);
401
402 archive_write_close(ext);
403 archive_write_free(ext);
404 exit(0);
405 }
406
407 static int
copy_data(struct archive * ar,struct archive * aw)408 copy_data(struct archive *ar, struct archive *aw)
409 {
410 int r;
411 const void *buff;
412 size_t size;
413 int64_t offset;
414
415 for (;;) {
416 r = archive_read_data_block(ar, &buff, &size, &offset);
417 if (r == ARCHIVE_EOF)
418 return (ARCHIVE_OK);
419 if (r != ARCHIVE_OK) {
420 errmsg(archive_error_string(ar));
421 return (r);
422 }
423 r = archive_write_data_block(aw, buff, size, offset);
424 if (r != ARCHIVE_OK) {
425 errmsg(archive_error_string(ar));
426 return (r);
427 }
428 }
429 }
430
431 static void
msg(const char * m)432 msg(const char *m)
433 {
434 write(1, m, strlen(m));
435 }
436
437 static void
errmsg(const char * m)438 errmsg(const char *m)
439 {
440 if (m == NULL) {
441 m = "Error: No error description provided.\n";
442 }
443 write(2, m, strlen(m));
444 }
445
446 static void
usage(void)447 usage(void)
448 {
449 /* Many program options depend on compile options. */
450 const char *m = "Usage: minitar [-"
451 #ifndef NO_CREATE
452 "c"
453 #endif
454 #ifndef NO_BZIP2
455 "j"
456 #endif
457 "tvx"
458 #ifndef NO_BZIP2
459 "y"
460 #endif
461 #ifndef NO_COMPRESS
462 "Z"
463 #endif
464 #ifndef NO_GZIP
465 "z"
466 #endif
467 "] [-f file] [file]\n";
468
469 errmsg(m);
470 exit(1);
471 }
472