diff options
author | Michihiro NAKAJIMA <ggcueroad@gmail.com> | 2012-10-26 15:02:24 +0900 |
---|---|---|
committer | Michihiro NAKAJIMA <ggcueroad@gmail.com> | 2012-11-07 11:50:45 +0900 |
commit | 6f00b2a18cddba6030583ea1338465f31f83f746 (patch) | |
tree | c551166d277e96f25fd3f0e06f1fba2ea92549db /tar/write.c | |
parent | 7337f20b8c86388d92b40aa5d4dfb2e8a203e1d0 (diff) | |
download | libarchive-6f00b2a18cddba6030583ea1338465f31f83f746.tar.gz |
Add support for TAR_WRITE_OPTIONS and TAR_READ_OPTIONS environmment
variables to set default options to writing or reading archives with
bsdtar.
Diffstat (limited to 'tar/write.c')
-rw-r--r-- | tar/write.c | 74 |
1 files changed, 68 insertions, 6 deletions
diff --git a/tar/write.c b/tar/write.c index a82b7fc0..40d2fb0a 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)); |