From: Michihiro NAKAJIMA Date: Fri, 26 Oct 2012 06:02:24 +0000 (+0900) Subject: Add support for TAR_WRITE_OPTIONS and TAR_READ_OPTIONS environmment X-Git-Tag: v3.1.0~39^2~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6f00b2a18cddba6030583ea1338465f31f83f746;p=thirdparty%2Flibarchive.git Add support for TAR_WRITE_OPTIONS and TAR_READ_OPTIONS environmment variables to set default options to writing or reading archives with bsdtar. --- diff --git a/libarchive/archive_options.c b/libarchive/archive_options.c index 08a348fb3..0da814926 100644 --- a/libarchive/archive_options.c +++ b/libarchive/archive_options.c @@ -94,7 +94,7 @@ int _archive_set_options(struct archive *a, const char *options, int magic, const char *fn, option_handler use_option) { - int allok = 1, anyok = 0, r; + int allok = 1, anyok = 0, ignore_mod_err = 0, r; char *data; const char *s, *mod, *opt, *val; @@ -111,6 +111,15 @@ _archive_set_options(struct archive *a, const char *options, mod = opt = val = NULL; parse_option(&s, &mod, &opt, &val); + if (mod == NULL && opt != NULL && + strcmp("__ignore_wrong_module_name__", opt) == 0) { + /* Ignore module name error */ + if (val != NULL) { + ignore_mod_err = 1; + anyok = 1; + } + continue; + } r = use_option(a, mod, opt, val); if (r == ARCHIVE_FATAL) { @@ -122,6 +131,8 @@ _archive_set_options(struct archive *a, const char *options, return (ARCHIVE_FAILED); } if (r == ARCHIVE_WARN - 1) { + if (ignore_mod_err) + continue; /* The module name is wrong. */ archive_set_error(a, ARCHIVE_ERRNO_MISC, "Unknown module name: `%s'", mod); diff --git a/tar/bsdtar.1 b/tar/bsdtar.1 index 72f269ad7..7bb6a6084 100644 --- a/tar/bsdtar.1 +++ b/tar/bsdtar.1 @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd October 31, 2012 +.Dd November 1, 2012 .Dt TAR 1 .Os .Sh NAME @@ -737,6 +737,16 @@ automatically when reading archives. The following environment variables affect the execution of .Nm : .Bl -tag -width ".Ev BLOCKSIZE" +.It Ev TAR_READER_OPTIONS +The default options for format readers and compression readers. +The +.Fl Fl options +option overrides this. +.It Ev TAR_WRITER_OPTIONS +The default options for format writers and compression writers. +The +.Fl Fl options +option overrides this. .It Ev LANG The locale to use. See diff --git a/tar/bsdtar.h b/tar/bsdtar.h index c0ba8eb58..a03d05ad6 100644 --- a/tar/bsdtar.h +++ b/tar/bsdtar.h @@ -29,6 +29,9 @@ #include #define DEFAULT_BYTES_PER_BLOCK (20*512) +#define ENV_READER_OPTIONS "TAR_READER_OPTIONS" +#define ENV_WRITER_OPTIONS "TAR_WRITER_OPTIONS" +#define IGNORE_WRONG_MODULE_NAME "__ignore_wrong_module_name__," struct creation_set; /* diff --git a/tar/read.c b/tar/read.c index ba5c2e335..e2bacad95 100644 --- a/tar/read.c +++ b/tar/read.c @@ -155,6 +155,7 @@ read_archive(struct bsdtar *bsdtar, char mode, struct archive *writer) FILE *out; struct archive *a; struct archive_entry *entry; + const char *reader_options; int r; while (*bsdtar->argv) { @@ -176,6 +177,28 @@ read_archive(struct bsdtar *bsdtar, char mode, struct archive *writer) if (cset_read_support_filter_program(bsdtar->cset, a) == 0) archive_read_support_filter_all(a); archive_read_support_format_all(a); + + reader_options = getenv(ENV_READER_OPTIONS); + if (reader_options != NULL) { + char *p; + /* Set default read options. */ + p = malloc(sizeof(IGNORE_WRONG_MODULE_NAME) + + strlen(reader_options) + 1); + if (p == NULL) + lafe_errc(1, errno, "Out of memory"); + /* Prepend magic code to ignore options for + * a format or modules which are not added to + * the archive read object. */ + strncpy(p, IGNORE_WRONG_MODULE_NAME, + sizeof(IGNORE_WRONG_MODULE_NAME) -1); + strcpy(p + sizeof(IGNORE_WRONG_MODULE_NAME) -1, reader_options); + r = archive_read_set_options(a, p); + free(p); + if (r == ARCHIVE_FATAL) + lafe_errc(1, 0, "%s", archive_error_string(a)); + else + archive_clear_error(a); + } if (ARCHIVE_OK != archive_read_set_options(a, bsdtar->option_options)) lafe_errc(1, 0, "%s", archive_error_string(a)); if (archive_read_open_filename(a, bsdtar->filename, diff --git a/tar/write.c b/tar/write.c index a82b7fc06..40d2fb0a9 100644 --- a/tar/write.c +++ b/tar/write.c @@ -137,6 +137,68 @@ seek_file(int fd, int64_t offset, int whence) #define lseek seek_file #endif +static void +set_writer_options(struct bsdtar *bsdtar, struct archive *a) +{ + const char *writer_options; + int r; + + writer_options = getenv(ENV_WRITER_OPTIONS); + if (writer_options != NULL) { + char *p; + /* Set default write options. */ + p = malloc(sizeof(IGNORE_WRONG_MODULE_NAME) + + strlen(writer_options) + 1); + if (p == NULL) + lafe_errc(1, errno, "Out of memory"); + /* Prepend magic code to ignore options for + * a format or filters which are not added to + * the archive write object. */ + strncpy(p, IGNORE_WRONG_MODULE_NAME, + sizeof(IGNORE_WRONG_MODULE_NAME) -1); + strcpy(p + sizeof(IGNORE_WRONG_MODULE_NAME) -1, writer_options); + r = archive_write_set_options(a, p); + free(p); + if (r < ARCHIVE_WARN) + lafe_errc(1, 0, "%s", archive_error_string(a)); + else + archive_clear_error(a); + } + if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options)) + lafe_errc(1, 0, "%s", archive_error_string(a)); +} + +static void +set_reader_options(struct bsdtar *bsdtar, struct archive *a) +{ + const char *reader_options; + int r; + + (void)bsdtar; /* UNUSED */ + + reader_options = getenv(ENV_READER_OPTIONS); + if (reader_options != NULL) { + char *p; + /* Set default write options. */ + p = malloc(sizeof(IGNORE_WRONG_MODULE_NAME) + + strlen(reader_options) + 1); + if (p == NULL) + lafe_errc(1, errno, "Out of memory"); + /* Prepend magic code to ignore options for + * a format or filters which are not added to + * the archive write object. */ + strncpy(p, IGNORE_WRONG_MODULE_NAME, + sizeof(IGNORE_WRONG_MODULE_NAME) -1); + strcpy(p + sizeof(IGNORE_WRONG_MODULE_NAME) -1, reader_options); + r = archive_read_set_options(a, p); + free(p); + if (r < ARCHIVE_WARN) + lafe_errc(1, 0, "%s", archive_error_string(a)); + else + archive_clear_error(a); + } +} + void tar_mode_c(struct bsdtar *bsdtar) { @@ -173,8 +235,7 @@ tar_mode_c(struct bsdtar *bsdtar) (const char *)filter_name); } - if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options)) - lafe_errc(1, 0, "%s", archive_error_string(a)); + set_writer_options(bsdtar, a); if (ARCHIVE_OK != archive_write_open_filename(a, bsdtar->filename)) lafe_errc(1, 0, "%s", archive_error_string(a)); write_archive(a, bsdtar); @@ -212,6 +273,7 @@ tar_mode_r(struct bsdtar *bsdtar) archive_read_support_format_empty(a); archive_read_support_format_tar(a); archive_read_support_format_gnutar(a); + set_reader_options(bsdtar, a); r = archive_read_open_fd(a, bsdtar->fd, 10240); if (r != ARCHIVE_OK) lafe_errc(1, archive_errno(a), @@ -264,8 +326,7 @@ tar_mode_r(struct bsdtar *bsdtar) } if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0) lafe_errc(1, errno, "Could not seek to archive end"); - if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options)) - lafe_errc(1, 0, "%s", archive_error_string(a)); + set_writer_options(bsdtar, a); if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd)) lafe_errc(1, 0, "%s", archive_error_string(a)); @@ -302,6 +363,7 @@ tar_mode_u(struct bsdtar *bsdtar) archive_read_support_filter_all(a); archive_read_support_format_tar(a); archive_read_support_format_gnutar(a); + set_reader_options(bsdtar, a); if (archive_read_open_fd(a, bsdtar->fd, bsdtar->bytes_per_block) != ARCHIVE_OK) { lafe_errc(1, 0, @@ -341,8 +403,7 @@ tar_mode_u(struct bsdtar *bsdtar) if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0) lafe_errc(1, errno, "Could not seek to archive end"); - if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options)) - lafe_errc(1, 0, "%s", archive_error_string(a)); + set_writer_options(bsdtar, a); if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd)) lafe_errc(1, 0, "%s", archive_error_string(a)); @@ -586,6 +647,7 @@ append_archive_filename(struct bsdtar *bsdtar, struct archive *a, ina = archive_read_new(); archive_read_support_format_all(ina); archive_read_support_filter_all(ina); + set_reader_options(bsdtar, a); if (archive_read_open_filename(ina, filename, bsdtar->bytes_per_block)) { lafe_warnc(0, "%s", archive_error_string(ina));