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>
43
__FBSDID("$FreeBSD$");
48
#include <archive_entry.h>
60
* NO_CREATE implies NO_BZIP2_CREATE and NO_GZIP_CREATE and NO_COMPRESS_CREATE.
63
#undef NO_BZIP2_CREATE
64
#define NO_BZIP2_CREATE
65
#undef NO_COMPRESS_CREATE
66
#define NO_COMPRESS_CREATE
68
#define NO_GZIP_CREATE
72
* The combination of NO_BZIP2_CREATE and NO_BZIP2_EXTRACT is
73
* equivalent to NO_BZIP2.
75
#ifdef NO_BZIP2_CREATE
76
#ifdef NO_BZIP2_EXTRACT
83
#undef NO_BZIP2_EXTRACT
84
#define NO_BZIP2_EXTRACT
85
#undef NO_BZIP2_CREATE
86
#define NO_BZIP2_CREATE
90
* The combination of NO_COMPRESS_CREATE and NO_COMPRESS_EXTRACT is
91
* equivalent to NO_COMPRESS.
93
#ifdef NO_COMPRESS_CREATE
94
#ifdef NO_COMPRESS_EXTRACT
101
#undef NO_COMPRESS_EXTRACT
102
#define NO_COMPRESS_EXTRACT
103
#undef NO_COMPRESS_CREATE
104
#define NO_COMPRESS_CREATE
108
* The combination of NO_GZIP_CREATE and NO_GZIP_EXTRACT is
109
* equivalent to NO_GZIP.
111
#ifdef NO_GZIP_CREATE
112
#ifdef NO_GZIP_EXTRACT
119
#undef NO_GZIP_EXTRACT
120
#define NO_GZIP_EXTRACT
121
#undef NO_GZIP_CREATE
122
#define NO_GZIP_CREATE
126
static void create(const char *filename, int compress, const char **argv);
128
static void errmsg(const char *);
129
static void extract(const char *filename, int do_extract, int flags);
130
static int copy_data(struct archive *, struct archive *);
131
static void msg(const char *);
132
static void usage(void);
134
static int verbose = 0;
137
main(int argc, const char **argv)
139
const char *filename = NULL;
140
int compress, flags, mode, opt;
146
flags = ARCHIVE_EXTRACT_TIME;
148
/* Among other sins, getopt(3) pulls in printf(3). */
149
while (*++argv != NULL && **argv == '-') {
150
const char *p = *argv + 1;
152
while ((opt = *p++) != '\0') {
166
#ifndef NO_BZIP2_CREATE
172
flags |= ARCHIVE_EXTRACT_PERM;
173
flags |= ARCHIVE_EXTRACT_ACL;
174
flags |= ARCHIVE_EXTRACT_FFLAGS;
185
#ifndef NO_BZIP2_CREATE
190
#ifndef NO_COMPRESS_CREATE
195
#ifndef NO_GZIP_CREATE
209
create(filename, compress, argv);
213
extract(filename, 0, flags);
216
extract(filename, 1, flags);
225
static char buff[16384];
228
create(const char *filename, int compress, const char **argv)
231
struct archive *disk;
232
struct archive_entry *entry;
236
a = archive_write_new();
238
#ifndef NO_BZIP2_CREATE
240
archive_write_set_compression_bzip2(a);
243
#ifndef NO_COMPRESS_CREATE
245
archive_write_set_compression_compress(a);
248
#ifndef NO_GZIP_CREATE
250
archive_write_set_compression_gzip(a);
254
archive_write_set_compression_none(a);
257
archive_write_set_format_ustar(a);
258
if (strcmp(filename, "-") == 0)
260
archive_write_open_file(a, filename);
262
disk = archive_read_disk_new();
264
archive_read_disk_set_standard_lookup(disk);
266
while (*argv != NULL) {
267
struct tree *t = tree_open(*argv);
268
while (tree_next(t)) {
269
entry = archive_entry_new();
270
archive_entry_set_pathname(entry, tree_current_path(t));
271
archive_read_disk_entry_from_file(disk, entry, -1,
272
tree_current_stat(t));
275
msg(tree_current_path(t));
277
archive_write_header(a, entry);
278
fd = open(tree_current_access_path(t), O_RDONLY);
279
len = read(fd, buff, sizeof(buff));
281
archive_write_data(a, buff, len);
282
len = read(fd, buff, sizeof(buff));
285
archive_entry_free(entry);
291
archive_write_close(a);
292
archive_write_finish(a);
297
extract(const char *filename, int do_extract, int flags)
301
struct archive_entry *entry;
304
a = archive_read_new();
305
ext = archive_write_disk_new();
306
archive_write_disk_set_options(ext, flags);
307
#ifndef NO_BZIP2_EXTRACT
308
archive_read_support_compression_bzip2(a);
310
#ifndef NO_GZIP_EXTRACT
311
archive_read_support_compression_gzip(a);
313
#ifndef NO_COMPRESS_EXTRACT
314
archive_read_support_compression_compress(a);
316
#ifndef NO_TAR_EXTRACT
317
archive_read_support_format_tar(a);
319
#ifndef NO_CPIO_EXTRACT
320
archive_read_support_format_cpio(a);
323
archive_write_disk_set_standard_lookup(ext);
325
if (filename != NULL && strcmp(filename, "-") == 0)
327
if ((r = archive_read_open_file(a, filename, 10240))) {
328
errmsg(archive_error_string(a));
333
r = archive_read_next_header(a, &entry);
334
if (r == ARCHIVE_EOF)
336
if (r != ARCHIVE_OK) {
337
errmsg(archive_error_string(a));
341
if (verbose && do_extract)
343
if (verbose || !do_extract)
344
msg(archive_entry_pathname(entry));
346
r = archive_write_header(ext, entry);
348
errmsg(archive_error_string(a));
352
if (verbose || !do_extract)
355
archive_read_close(a);
356
archive_read_finish(a);
361
copy_data(struct archive *ar, struct archive *aw)
369
r = archive_read_data_block(ar, &buff, &size, &offset);
370
if (r == ARCHIVE_EOF) {
371
errmsg(archive_error_string(ar));
376
r = archive_write_data_block(aw, buff, size, offset);
377
if (r != ARCHIVE_OK) {
378
errmsg(archive_error_string(ar));
387
write(1, m, strlen(m));
391
errmsg(const char *m)
393
write(2, m, strlen(m));
399
/* Many program options depend on compile options. */
400
const char *m = "Usage: minitar [-"
417
"] [-f file] [file]\n";