Blame examples/minitar/minitar.c

Packit 08bd4c
/*-
Packit 08bd4c
 * This file is in the public domain.
Packit 08bd4c
 * Do with it as you will.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
/*-
Packit 08bd4c
 * This is a compact "tar" program whose primary goal is small size.
Packit 08bd4c
 * Statically linked, it can be very small indeed.  This serves a number
Packit 08bd4c
 * of goals:
Packit 08bd4c
 *   o a testbed for libarchive (to check for link pollution),
Packit 08bd4c
 *   o a useful tool for space-constrained systems (boot floppies, etc),
Packit 08bd4c
 *   o a place to experiment with new implementation ideas for bsdtar,
Packit 08bd4c
 *   o a small program to demonstrate libarchive usage.
Packit 08bd4c
 *
Packit 08bd4c
 * Use the following macros to suppress features:
Packit 08bd4c
 *   NO_BZIP2 - Implies NO_BZIP2_CREATE and NO_BZIP2_EXTRACT
Packit 08bd4c
 *   NO_BZIP2_CREATE - Suppress bzip2 compression support.
Packit 08bd4c
 *   NO_BZIP2_EXTRACT - Suppress bzip2 auto-detection and decompression.
Packit 08bd4c
 *   NO_COMPRESS - Implies NO_COMPRESS_CREATE and NO_COMPRESS_EXTRACT
Packit 08bd4c
 *   NO_COMPRESS_CREATE - Suppress compress(1) compression support
Packit 08bd4c
 *   NO_COMPRESS_EXTRACT - Suppress compress(1) auto-detect and decompression.
Packit 08bd4c
 *   NO_CREATE - Suppress all archive creation support.
Packit 08bd4c
 *   NO_CPIO_EXTRACT - Suppress auto-detect and dearchiving of cpio archives.
Packit 08bd4c
 *   NO_GZIP - Implies NO_GZIP_CREATE and NO_GZIP_EXTRACT
Packit 08bd4c
 *   NO_GZIP_CREATE - Suppress gzip compression support.
Packit 08bd4c
 *   NO_GZIP_EXTRACT - Suppress gzip auto-detection and decompression.
Packit 08bd4c
 *   NO_LOOKUP - Try to avoid getpw/getgr routines, which can be very large
Packit 08bd4c
 *   NO_TAR_EXTRACT - Suppress tar extraction
Packit 08bd4c
 *
Packit 08bd4c
 * With all of the above macros defined (except NO_TAR_EXTRACT), you
Packit 08bd4c
 * get a very small program that can recognize and extract essentially
Packit 08bd4c
 * any uncompressed tar archive.  On FreeBSD 5.1, this minimal program
Packit 08bd4c
 * is under 64k, statically linked, which compares rather favorably to
Packit 08bd4c
 *         main(){printf("hello, world");}
Packit 08bd4c
 * which is over 60k statically linked on the same operating system.
Packit 08bd4c
 * Without any of the above macros, you get a static executable of
Packit 08bd4c
 * about 180k with a lot of very sophisticated modern features.
Packit 08bd4c
 * Obviously, it's trivial to add support for ISO, Zip, mtree,
Packit 08bd4c
 * lzma/xz, etc.  Just fill in the appropriate setup calls.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
#include <sys/types.h>
Packit 08bd4c
#include <sys/stat.h>
Packit 08bd4c
Packit 08bd4c
#include <archive.h>
Packit 08bd4c
#include <archive_entry.h>
Packit 08bd4c
#include <fcntl.h>
Packit 08bd4c
#include <stdio.h>
Packit 08bd4c
#include <stdlib.h>
Packit 08bd4c
#include <string.h>
Packit 08bd4c
#include <unistd.h>
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * NO_CREATE implies NO_BZIP2_CREATE and NO_GZIP_CREATE and NO_COMPRESS_CREATE.
Packit 08bd4c
 */
Packit 08bd4c
#ifdef NO_CREATE
Packit 08bd4c
#undef NO_BZIP2_CREATE
Packit 08bd4c
#define NO_BZIP2_CREATE
Packit 08bd4c
#undef NO_COMPRESS_CREATE
Packit 08bd4c
#define	NO_COMPRESS_CREATE
Packit 08bd4c
#undef NO_GZIP_CREATE
Packit 08bd4c
#define NO_GZIP_CREATE
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * The combination of NO_BZIP2_CREATE and NO_BZIP2_EXTRACT is
Packit 08bd4c
 * equivalent to NO_BZIP2.
Packit 08bd4c
 */
Packit 08bd4c
#ifdef NO_BZIP2_CREATE
Packit 08bd4c
#ifdef NO_BZIP2_EXTRACT
Packit 08bd4c
#undef NO_BZIP2
Packit 08bd4c
#define NO_BZIP2
Packit 08bd4c
#endif
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
#ifdef NO_BZIP2
Packit 08bd4c
#undef NO_BZIP2_EXTRACT
Packit 08bd4c
#define NO_BZIP2_EXTRACT
Packit 08bd4c
#undef NO_BZIP2_CREATE
Packit 08bd4c
#define NO_BZIP2_CREATE
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * The combination of NO_COMPRESS_CREATE and NO_COMPRESS_EXTRACT is
Packit 08bd4c
 * equivalent to NO_COMPRESS.
Packit 08bd4c
 */
Packit 08bd4c
#ifdef NO_COMPRESS_CREATE
Packit 08bd4c
#ifdef NO_COMPRESS_EXTRACT
Packit 08bd4c
#undef NO_COMPRESS
Packit 08bd4c
#define NO_COMPRESS
Packit 08bd4c
#endif
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
#ifdef NO_COMPRESS
Packit 08bd4c
#undef NO_COMPRESS_EXTRACT
Packit 08bd4c
#define NO_COMPRESS_EXTRACT
Packit 08bd4c
#undef NO_COMPRESS_CREATE
Packit 08bd4c
#define NO_COMPRESS_CREATE
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * The combination of NO_GZIP_CREATE and NO_GZIP_EXTRACT is
Packit 08bd4c
 * equivalent to NO_GZIP.
Packit 08bd4c
 */
Packit 08bd4c
#ifdef NO_GZIP_CREATE
Packit 08bd4c
#ifdef NO_GZIP_EXTRACT
Packit 08bd4c
#undef NO_GZIP
Packit 08bd4c
#define NO_GZIP
Packit 08bd4c
#endif
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
#ifdef NO_GZIP
Packit 08bd4c
#undef NO_GZIP_EXTRACT
Packit 08bd4c
#define NO_GZIP_EXTRACT
Packit 08bd4c
#undef NO_GZIP_CREATE
Packit 08bd4c
#define NO_GZIP_CREATE
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
#ifndef NO_CREATE
Packit 08bd4c
static void	create(const char *filename, int compress, const char **argv);
Packit 08bd4c
#endif
Packit 08bd4c
static void	errmsg(const char *);
Packit 08bd4c
static void	extract(const char *filename, int do_extract, int flags);
Packit 08bd4c
static int	copy_data(struct archive *, struct archive *);
Packit 08bd4c
static void	msg(const char *);
Packit 08bd4c
static void	usage(void);
Packit 08bd4c
Packit 08bd4c
static int verbose = 0;
Packit 08bd4c
Packit 08bd4c
int
Packit 08bd4c
main(int argc, const char **argv)
Packit 08bd4c
{
Packit 08bd4c
	const char *filename = NULL;
Packit 08bd4c
	int compress, flags, mode, opt;
Packit 08bd4c
Packit 08bd4c
	(void)argc;
Packit 08bd4c
	mode = 'x';
Packit 08bd4c
	verbose = 0;
Packit 08bd4c
	compress = '\0';
Packit 08bd4c
	flags = ARCHIVE_EXTRACT_TIME;
Packit 08bd4c
Packit 08bd4c
	/* Among other sins, getopt(3) pulls in printf(3). */
Packit 08bd4c
	while (*++argv != NULL && **argv == '-') {
Packit 08bd4c
		const char *p = *argv + 1;
Packit 08bd4c
Packit 08bd4c
		while ((opt = *p++) != '\0') {
Packit 08bd4c
			switch (opt) {
Packit 08bd4c
#ifndef NO_CREATE
Packit 08bd4c
			case 'c':
Packit 08bd4c
				mode = opt;
Packit 08bd4c
				break;
Packit 08bd4c
#endif
Packit 08bd4c
			case 'f':
Packit 08bd4c
				if (*p != '\0')
Packit 08bd4c
					filename = p;
Packit 08bd4c
				else
Packit 08bd4c
					filename = *++argv;
Packit 08bd4c
				p += strlen(p);
Packit 08bd4c
				break;
Packit 08bd4c
#ifndef NO_BZIP2_CREATE
Packit 08bd4c
			case 'j':
Packit 08bd4c
				compress = opt;
Packit 08bd4c
				break;
Packit 08bd4c
#endif
Packit 08bd4c
			case 'p':
Packit 08bd4c
				flags |= ARCHIVE_EXTRACT_PERM;
Packit 08bd4c
				flags |= ARCHIVE_EXTRACT_ACL;
Packit 08bd4c
				flags |= ARCHIVE_EXTRACT_FFLAGS;
Packit 08bd4c
				break;
Packit 08bd4c
			case 't':
Packit 08bd4c
				mode = opt;
Packit 08bd4c
				break;
Packit 08bd4c
			case 'v':
Packit 08bd4c
				verbose++;
Packit 08bd4c
				break;
Packit 08bd4c
			case 'x':
Packit 08bd4c
				mode = opt;
Packit 08bd4c
				break;
Packit 08bd4c
#ifndef NO_BZIP2_CREATE
Packit 08bd4c
			case 'y':
Packit 08bd4c
				compress = opt;
Packit 08bd4c
				break;
Packit 08bd4c
#endif
Packit 08bd4c
#ifndef NO_COMPRESS_CREATE
Packit 08bd4c
			case 'Z':
Packit 08bd4c
				compress = opt;
Packit 08bd4c
				break;
Packit 08bd4c
#endif
Packit 08bd4c
#ifndef NO_GZIP_CREATE
Packit 08bd4c
			case 'z':
Packit 08bd4c
				compress = opt;
Packit 08bd4c
				break;
Packit 08bd4c
#endif
Packit 08bd4c
			default:
Packit 08bd4c
				usage();
Packit 08bd4c
			}
Packit 08bd4c
		}
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	switch (mode) {
Packit 08bd4c
#ifndef NO_CREATE
Packit 08bd4c
	case 'c':
Packit 08bd4c
		create(filename, compress, argv);
Packit 08bd4c
		break;
Packit 08bd4c
#endif
Packit 08bd4c
	case 't':
Packit 08bd4c
		extract(filename, 0, flags);
Packit 08bd4c
		break;
Packit 08bd4c
	case 'x':
Packit 08bd4c
		extract(filename, 1, flags);
Packit 08bd4c
		break;
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	return (0);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
Packit 08bd4c
#ifndef NO_CREATE
Packit 08bd4c
static char buff[16384];
Packit 08bd4c
Packit 08bd4c
static void
Packit 08bd4c
create(const char *filename, int compress, const char **argv)
Packit 08bd4c
{
Packit 08bd4c
	struct archive *a;
Packit 08bd4c
	struct archive_entry *entry;
Packit 08bd4c
	ssize_t len;
Packit 08bd4c
	int fd;
Packit 08bd4c
Packit 08bd4c
	a = archive_write_new();
Packit 08bd4c
	switch (compress) {
Packit 08bd4c
#ifndef NO_BZIP2_CREATE
Packit 08bd4c
	case 'j': case 'y':
Packit 08bd4c
		archive_write_add_filter_bzip2(a);
Packit 08bd4c
		break;
Packit 08bd4c
#endif
Packit 08bd4c
#ifndef NO_COMPRESS_CREATE
Packit 08bd4c
	case 'Z':
Packit 08bd4c
		archive_write_add_filter_compress(a);
Packit 08bd4c
		break;
Packit 08bd4c
#endif
Packit 08bd4c
#ifndef NO_GZIP_CREATE
Packit 08bd4c
	case 'z':
Packit 08bd4c
		archive_write_add_filter_gzip(a);
Packit 08bd4c
		break;
Packit 08bd4c
#endif
Packit 08bd4c
	default:
Packit 08bd4c
		archive_write_add_filter_none(a);
Packit 08bd4c
		break;
Packit 08bd4c
	}
Packit 08bd4c
	archive_write_set_format_ustar(a);
Packit 08bd4c
	if (filename != NULL && strcmp(filename, "-") == 0)
Packit 08bd4c
		filename = NULL;
Packit 08bd4c
	archive_write_open_filename(a, filename);
Packit 08bd4c
Packit 08bd4c
	while (*argv != NULL) {
Packit 08bd4c
		struct archive *disk = archive_read_disk_new();
Packit 08bd4c
#ifndef NO_LOOKUP
Packit 08bd4c
		archive_read_disk_set_standard_lookup(disk);
Packit 08bd4c
#endif
Packit 08bd4c
		int r;
Packit 08bd4c
Packit 08bd4c
		r = archive_read_disk_open(disk, *argv);
Packit 08bd4c
		if (r != ARCHIVE_OK) {
Packit 08bd4c
			errmsg(archive_error_string(disk));
Packit 08bd4c
			errmsg("\n");
Packit 08bd4c
			exit(1);
Packit 08bd4c
		}
Packit 08bd4c
Packit 08bd4c
		for (;;) {
Packit 08bd4c
			int needcr = 0;
Packit 08bd4c
Packit 08bd4c
			entry = archive_entry_new();
Packit 08bd4c
			r = archive_read_next_header2(disk, entry);
Packit 08bd4c
			if (r == ARCHIVE_EOF)
Packit 08bd4c
				break;
Packit 08bd4c
			if (r != ARCHIVE_OK) {
Packit 08bd4c
				errmsg(archive_error_string(disk));
Packit 08bd4c
				errmsg("\n");
Packit 08bd4c
				exit(1);
Packit 08bd4c
			}
Packit 08bd4c
			archive_read_disk_descend(disk);
Packit 08bd4c
			if (verbose) {
Packit 08bd4c
				msg("a ");
Packit 08bd4c
				msg(archive_entry_pathname(entry));
Packit 08bd4c
				needcr = 1;
Packit 08bd4c
			}
Packit 08bd4c
			r = archive_write_header(a, entry);
Packit 08bd4c
			if (r < ARCHIVE_OK) {
Packit 08bd4c
				errmsg(": ");
Packit 08bd4c
				errmsg(archive_error_string(a));
Packit 08bd4c
				needcr = 1;
Packit 08bd4c
			}
Packit 08bd4c
			if (r == ARCHIVE_FATAL)
Packit 08bd4c
				exit(1);
Packit 08bd4c
			if (r > ARCHIVE_FAILED) {
Packit 08bd4c
#if 0
Packit 08bd4c
				/* Ideally, we would be able to use
Packit 08bd4c
				 * the same code to copy a body from
Packit 08bd4c
				 * an archive_read_disk to an
Packit 08bd4c
				 * archive_write that we use for
Packit 08bd4c
				 * copying data from an archive_read
Packit 08bd4c
				 * to an archive_write_disk.
Packit 08bd4c
				 * Unfortunately, this doesn't quite
Packit 08bd4c
				 * work yet. */
Packit 08bd4c
				copy_data(disk, a);
Packit 08bd4c
#else
Packit 08bd4c
				/* For now, we use a simpler loop to copy data
Packit 08bd4c
				 * into the target archive. */
Packit 08bd4c
				fd = open(archive_entry_sourcepath(entry), O_RDONLY);
Packit 08bd4c
				len = read(fd, buff, sizeof(buff));
Packit 08bd4c
				while (len > 0) {
Packit 08bd4c
					archive_write_data(a, buff, len);
Packit 08bd4c
					len = read(fd, buff, sizeof(buff));
Packit 08bd4c
				}
Packit 08bd4c
				close(fd);
Packit 08bd4c
#endif
Packit 08bd4c
			}
Packit 08bd4c
			archive_entry_free(entry);
Packit 08bd4c
			if (needcr)
Packit 08bd4c
				msg("\n");
Packit 08bd4c
		}
Packit 08bd4c
		archive_read_close(disk);
Packit 08bd4c
		archive_read_free(disk);
Packit 08bd4c
		argv++;
Packit 08bd4c
	}
Packit 08bd4c
	archive_write_close(a);
Packit 08bd4c
	archive_write_free(a);
Packit 08bd4c
}
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
static void
Packit 08bd4c
extract(const char *filename, int do_extract, int flags)
Packit 08bd4c
{
Packit 08bd4c
	struct archive *a;
Packit 08bd4c
	struct archive *ext;
Packit 08bd4c
	struct archive_entry *entry;
Packit 08bd4c
	int r;
Packit 08bd4c
Packit 08bd4c
	a = archive_read_new();
Packit 08bd4c
	ext = archive_write_disk_new();
Packit 08bd4c
	archive_write_disk_set_options(ext, flags);
Packit 08bd4c
#ifndef NO_BZIP2_EXTRACT
Packit 08bd4c
	archive_read_support_filter_bzip2(a);
Packit 08bd4c
#endif
Packit 08bd4c
#ifndef NO_GZIP_EXTRACT
Packit 08bd4c
	archive_read_support_filter_gzip(a);
Packit 08bd4c
#endif
Packit 08bd4c
#ifndef NO_COMPRESS_EXTRACT
Packit 08bd4c
	archive_read_support_filter_compress(a);
Packit 08bd4c
#endif
Packit 08bd4c
#ifndef NO_TAR_EXTRACT
Packit 08bd4c
	archive_read_support_format_tar(a);
Packit 08bd4c
#endif
Packit 08bd4c
#ifndef NO_CPIO_EXTRACT
Packit 08bd4c
	archive_read_support_format_cpio(a);
Packit 08bd4c
#endif
Packit 08bd4c
#ifndef NO_LOOKUP
Packit 08bd4c
	archive_write_disk_set_standard_lookup(ext);
Packit 08bd4c
#endif
Packit 08bd4c
	if (filename != NULL && strcmp(filename, "-") == 0)
Packit 08bd4c
		filename = NULL;
Packit 08bd4c
	if ((r = archive_read_open_filename(a, filename, 10240))) {
Packit 08bd4c
		errmsg(archive_error_string(a));
Packit 08bd4c
		errmsg("\n");
Packit 08bd4c
		exit(r);
Packit 08bd4c
	}
Packit 08bd4c
	for (;;) {
Packit 08bd4c
		int needcr = 0;
Packit 08bd4c
		r = archive_read_next_header(a, &entry);
Packit 08bd4c
		if (r == ARCHIVE_EOF)
Packit 08bd4c
			break;
Packit 08bd4c
		if (r != ARCHIVE_OK) {
Packit 08bd4c
			errmsg(archive_error_string(a));
Packit 08bd4c
			errmsg("\n");
Packit 08bd4c
			exit(1);
Packit 08bd4c
		}
Packit 08bd4c
		if (verbose && do_extract)
Packit 08bd4c
			msg("x ");
Packit 08bd4c
		if (verbose || !do_extract) {
Packit 08bd4c
			msg(archive_entry_pathname(entry));
Packit 08bd4c
			msg(" ");
Packit 08bd4c
			needcr = 1;
Packit 08bd4c
		}
Packit 08bd4c
		if (do_extract) {
Packit 08bd4c
			r = archive_write_header(ext, entry);
Packit 08bd4c
			if (r != ARCHIVE_OK) {
Packit 08bd4c
				errmsg(archive_error_string(a));
Packit 08bd4c
				needcr = 1;
Packit 08bd4c
			}
Packit 08bd4c
			else {
Packit 08bd4c
				r = copy_data(a, ext);
Packit 08bd4c
				if (r != ARCHIVE_OK)
Packit 08bd4c
					needcr = 1;
Packit 08bd4c
			}
Packit 08bd4c
		}
Packit 08bd4c
		if (needcr)
Packit 08bd4c
			msg("\n");
Packit 08bd4c
	}
Packit 08bd4c
	archive_read_close(a);
Packit 08bd4c
	archive_read_free(a);
Packit 08bd4c
	exit(0);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
static int
Packit 08bd4c
copy_data(struct archive *ar, struct archive *aw)
Packit 08bd4c
{
Packit 08bd4c
	int r;
Packit 08bd4c
	const void *buff;
Packit 08bd4c
	size_t size;
Packit 08bd4c
	int64_t offset;
Packit 08bd4c
Packit 08bd4c
	for (;;) {
Packit 08bd4c
		r = archive_read_data_block(ar, &buff, &size, &offset);
Packit 08bd4c
		if (r == ARCHIVE_EOF)
Packit 08bd4c
			return (ARCHIVE_OK);
Packit 08bd4c
		if (r != ARCHIVE_OK) {
Packit 08bd4c
			errmsg(archive_error_string(ar));
Packit 08bd4c
			return (r);
Packit 08bd4c
		}
Packit 08bd4c
		r = archive_write_data_block(aw, buff, size, offset);
Packit 08bd4c
		if (r != ARCHIVE_OK) {
Packit 08bd4c
			errmsg(archive_error_string(ar));
Packit 08bd4c
			return (r);
Packit 08bd4c
		}
Packit 08bd4c
	}
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
static void
Packit 08bd4c
msg(const char *m)
Packit 08bd4c
{
Packit 08bd4c
	write(1, m, strlen(m));
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
static void
Packit 08bd4c
errmsg(const char *m)
Packit 08bd4c
{
Packit 08bd4c
	if (m == NULL) {
Packit 08bd4c
		m = "Error: No error description provided.\n";
Packit 08bd4c
	}
Packit 08bd4c
	write(2, m, strlen(m));
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
static void
Packit 08bd4c
usage(void)
Packit 08bd4c
{
Packit 08bd4c
/* Many program options depend on compile options. */
Packit 08bd4c
	const char *m = "Usage: minitar [-"
Packit 08bd4c
#ifndef NO_CREATE
Packit 08bd4c
	    "c"
Packit 08bd4c
#endif
Packit 08bd4c
#ifndef	NO_BZIP2
Packit 08bd4c
	    "j"
Packit 08bd4c
#endif
Packit 08bd4c
	    "tvx"
Packit 08bd4c
#ifndef NO_BZIP2
Packit 08bd4c
	    "y"
Packit 08bd4c
#endif
Packit 08bd4c
#ifndef NO_COMPRESS
Packit 08bd4c
	    "Z"
Packit 08bd4c
#endif
Packit 08bd4c
#ifndef NO_GZIP
Packit 08bd4c
	    "z"
Packit 08bd4c
#endif
Packit 08bd4c
	    "] [-f file] [file]\n";
Packit 08bd4c
Packit 08bd4c
	errmsg(m);
Packit 08bd4c
	exit(1);
Packit 08bd4c
}