2 * This file is in the public domain.
3 * Do with it as you will.
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
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.
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
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.
42 #include <sys/types.h>
46 #include <archive_entry.h>
54 * NO_CREATE implies NO_BZIP2_CREATE and NO_GZIP_CREATE and NO_COMPRESS_CREATE.
57 #undef NO_BZIP2_CREATE
58 #define NO_BZIP2_CREATE
59 #undef NO_COMPRESS_CREATE
60 #define NO_COMPRESS_CREATE
62 #define NO_GZIP_CREATE
66 * The combination of NO_BZIP2_CREATE and NO_BZIP2_EXTRACT is
67 * equivalent to NO_BZIP2.
69 #ifdef NO_BZIP2_CREATE
70 #ifdef NO_BZIP2_EXTRACT
77 #undef NO_BZIP2_EXTRACT
78 #define NO_BZIP2_EXTRACT
79 #undef NO_BZIP2_CREATE
80 #define NO_BZIP2_CREATE
84 * The combination of NO_COMPRESS_CREATE and NO_COMPRESS_EXTRACT is
85 * equivalent to NO_COMPRESS.
87 #ifdef NO_COMPRESS_CREATE
88 #ifdef NO_COMPRESS_EXTRACT
95 #undef NO_COMPRESS_EXTRACT
96 #define NO_COMPRESS_EXTRACT
97 #undef NO_COMPRESS_CREATE
98 #define NO_COMPRESS_CREATE
102 * The combination of NO_GZIP_CREATE and NO_GZIP_EXTRACT is
103 * equivalent to NO_GZIP.
105 #ifdef NO_GZIP_CREATE
106 #ifdef NO_GZIP_EXTRACT
113 #undef NO_GZIP_EXTRACT
114 #define NO_GZIP_EXTRACT
115 #undef NO_GZIP_CREATE
116 #define NO_GZIP_CREATE
120 static void create(const char *filename, int compress, const char **argv);
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);
128 static int verbose = 0;
131 main(int argc, const char **argv)
133 const char *filename = NULL;
134 int compress, flags, mode, opt;
140 flags = ARCHIVE_EXTRACT_TIME;
142 /* Among other sins, getopt(3) pulls in printf(3). */
143 while (*++argv != NULL && **argv == '-') {
144 const char *p = *argv + 1;
146 while ((opt = *p++) != '\0') {
160 #ifndef NO_BZIP2_CREATE
166 flags |= ARCHIVE_EXTRACT_PERM;
167 flags |= ARCHIVE_EXTRACT_ACL;
168 flags |= ARCHIVE_EXTRACT_FFLAGS;
179 #ifndef NO_BZIP2_CREATE
184 #ifndef NO_COMPRESS_CREATE
189 #ifndef NO_GZIP_CREATE
203 create(filename, compress, argv);
207 extract(filename, 0, flags);
210 extract(filename, 1, flags);
219 static char buff[16384];
222 create(const char *filename, int compress, const char **argv)
225 struct archive_entry *entry;
229 a = archive_write_new();
231 #ifndef NO_BZIP2_CREATE
233 archive_write_add_filter_bzip2(a);
236 #ifndef NO_COMPRESS_CREATE
238 archive_write_add_filter_compress(a);
241 #ifndef NO_GZIP_CREATE
243 archive_write_add_filter_gzip(a);
247 archive_write_add_filter_none(a);
250 archive_write_set_format_ustar(a);
251 if (filename != NULL && strcmp(filename, "-") == 0)
253 archive_write_open_filename(a, filename);
255 while (*argv != NULL) {
256 struct archive *disk = archive_read_disk_new();
258 archive_read_disk_set_standard_lookup(disk);
262 r = archive_read_disk_open(disk, *argv);
263 if (r != ARCHIVE_OK) {
264 errmsg(archive_error_string(disk));
272 entry = archive_entry_new();
273 r = archive_read_next_header2(disk, entry);
274 if (r == ARCHIVE_EOF)
276 if (r != ARCHIVE_OK) {
277 errmsg(archive_error_string(disk));
281 archive_read_disk_descend(disk);
284 msg(archive_entry_pathname(entry));
287 r = archive_write_header(a, entry);
288 if (r < ARCHIVE_OK) {
290 errmsg(archive_error_string(a));
293 if (r == ARCHIVE_FATAL)
295 if (r > ARCHIVE_FAILED) {
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
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));
312 archive_write_data(a, buff, len);
313 len = read(fd, buff, sizeof(buff));
318 archive_entry_free(entry);
322 archive_read_close(disk);
323 archive_read_free(disk);
326 archive_write_close(a);
327 archive_write_free(a);
332 extract(const char *filename, int do_extract, int flags)
336 struct archive_entry *entry;
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);
345 #ifndef NO_GZIP_EXTRACT
346 archive_read_support_filter_gzip(a);
348 #ifndef NO_COMPRESS_EXTRACT
349 archive_read_support_filter_compress(a);
351 #ifndef NO_TAR_EXTRACT
352 archive_read_support_format_tar(a);
354 #ifndef NO_CPIO_EXTRACT
355 archive_read_support_format_cpio(a);
358 archive_write_disk_set_standard_lookup(ext);
360 if (filename != NULL && strcmp(filename, "-") == 0)
362 if ((r = archive_read_open_filename(a, filename, 10240))) {
363 errmsg(archive_error_string(a));
369 r = archive_read_next_header(a, &entry);
370 if (r == ARCHIVE_EOF)
372 if (r != ARCHIVE_OK) {
373 errmsg(archive_error_string(a));
377 if (verbose && do_extract)
379 if (verbose || !do_extract) {
380 msg(archive_entry_pathname(entry));
385 r = archive_write_header(ext, entry);
386 if (r != ARCHIVE_OK) {
387 errmsg(archive_error_string(a));
391 r = copy_data(a, ext);
399 archive_read_close(a);
400 archive_read_free(a);
405 copy_data(struct archive *ar, struct archive *aw)
413 r = archive_read_data_block(ar, &buff, &size, &offset);
414 if (r == ARCHIVE_EOF)
416 if (r != ARCHIVE_OK) {
417 errmsg(archive_error_string(ar));
420 r = archive_write_data_block(aw, buff, size, offset);
421 if (r != ARCHIVE_OK) {
422 errmsg(archive_error_string(ar));
431 write(1, m, strlen(m));
435 errmsg(const char *m)
438 m = "Error: No error description provided.\n";
440 write(2, m, strlen(m));
446 /* Many program options depend on compile options. */
447 const char *m = "Usage: minitar [-"
464 "] [-f file] [file]\n";