]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Add support for TAR_WRITE_OPTIONS and TAR_READ_OPTIONS environmment
authorMichihiro NAKAJIMA <ggcueroad@gmail.com>
Fri, 26 Oct 2012 06:02:24 +0000 (15:02 +0900)
committerMichihiro NAKAJIMA <ggcueroad@gmail.com>
Wed, 7 Nov 2012 02:50:45 +0000 (11:50 +0900)
variables to set default options to writing or reading archives with
bsdtar.

libarchive/archive_options.c
tar/bsdtar.1
tar/bsdtar.h
tar/read.c
tar/write.c

index 08a348fb30344ea3ee2ed8dd8da39d1fb118d6dd..0da814926b6c6004880940f04a5e442a4ca930fc 100644 (file)
@@ -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);
index 72f269ad7bc344db0bdfa76d92aac855dea103c8..7bb6a6084d54e81fbe8e89dd9b3207737ae85272 100644 (file)
@@ -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
index c0ba8eb58708356769505f743df8962b757355b1..a03d05ad60718e98fbebcb2c8ab1d81da01c1487 100644 (file)
@@ -29,6 +29,9 @@
 #include <stdio.h>
 
 #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;
 /*
index ba5c2e33584d8d012bf751f51970b21ad31b7fed..e2bacad9573c268d09188522ad1c75602b88437f 100644 (file)
@@ -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,
index a82b7fc06fc1a87e84a7777904455a0835a3561a..40d2fb0a9f52cf63d9e0363458799611a7b99a40 100644 (file)
@@ -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));