]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Introduce archive_match_exclude_pattern_from_file(_w) and
authorMichihiro NAKAJIMA <ggcueroad@gmail.com>
Sun, 12 Feb 2012 13:41:47 +0000 (22:41 +0900)
committerMichihiro NAKAJIMA <ggcueroad@gmail.com>
Sun, 12 Feb 2012 13:41:47 +0000 (22:41 +0900)
archive_match_include_pattern_from_file(_w) reading exclusion/inclusion
patterns from a file. And so retire lafe_exclude_from_file() and
lafe_include_from_file because we can use new APIs instead.

19 files changed:
Makefile.am
cpio/CMakeLists.txt
cpio/cpio.c
cpio/cpio.h
cpio/test/main.c
cpio/test/test.h
libarchive/archive.h
libarchive/archive_match.c
libarchive/test/main.c
libarchive/test/test.h
libarchive/test/test_archive_match_path.c
libarchive_fe/matching.c [deleted file]
libarchive_fe/matching.h [deleted file]
tar/CMakeLists.txt
tar/bsdtar.c
tar/bsdtar.h
tar/read.c
tar/test/main.c
tar/test/test.h

index aa715bb11f2f090ccd0c1a3424c10d3593ac4b60..69b3928f3337c0cf8d6907409def168e7c7c02a6 100644 (file)
@@ -583,9 +583,7 @@ libarchive_fe_la_SOURCES=                   \
        libarchive_fe/err.h                     \
        libarchive_fe/lafe_platform.h           \
        libarchive_fe/line_reader.c             \
-       libarchive_fe/line_reader.h             \
-       libarchive_fe/matching.c                \
-       libarchive_fe/matching.h
+       libarchive_fe/line_reader.h
 
 libarchive_fe_la_CPPFLAGS= -I$(top_srcdir)/libarchive
 #
index 98e3449676bc297d448af697c7472e3775f7b0cb..cc4aa14cb54bc2143ce0ee88f1b055be7e356f1c 100644 (file)
@@ -15,8 +15,6 @@ IF(ENABLE_CPIO)
     ../libarchive_fe/lafe_platform.h
     ../libarchive_fe/line_reader.c
     ../libarchive_fe/line_reader.h
-    ../libarchive_fe/matching.c
-    ../libarchive_fe/matching.h
   )
   INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../libarchive_fe)
   IF(WIN32 AND NOT CYGWIN)
index 8acfae22f3e2340f2d4abbc203c30b34ff83fba7..717476e2317af3bf7ca6d0afcb4d3108b6f978e2 100644 (file)
@@ -82,7 +82,6 @@ __FBSDID("$FreeBSD: src/usr.bin/cpio/cpio.c,v 1.15 2008/12/06 07:30:40 kientzle
 #include "cpio.h"
 #include "err.h"
 #include "line_reader.h"
-#include "matching.h"
 
 /* Fixed size of uname/gname caches. */
 #define        name_cache_size 101
@@ -219,8 +218,11 @@ main(int argc, char *argv[])
                        cpio->extract_flags &= ~ARCHIVE_EXTRACT_NO_AUTODIR;
                        break;
                case 'E': /* NetBSD/OpenBSD */
-                       lafe_include_from_file(cpio->matching,
-                           cpio->argument, cpio->option_null);
+                       if (archive_match_include_pattern_from_file(
+                           cpio->matching, cpio->argument,
+                           cpio->option_null) != ARCHIVE_OK)
+                               lafe_errc(1, 0, "Error : %s",
+                                   archive_error_string(cpio->matching));
                        break;
                case 'F': /* NetBSD/OpenBSD/GNU cpio */
                        cpio->filename = cpio->argument;
index 9e5af674e7860141af6db0ad1a7ba25a5f12b4f7..7e276bdbaf7921337978e309be4bd3161f7cc44b 100644 (file)
@@ -31,8 +31,6 @@
 #include "cpio_platform.h"
 #include <stdio.h>
 
-#include "matching.h"
-
 /*
  * The internal state for the "cpio" program.
  *
index 915237a753bbf3620d3f93b57be8e6865013cc7f..30d5ea775da16e02155d09c4921749344befc10a 100644 (file)
@@ -1520,7 +1520,7 @@ assertion_make_dir(const char *file, int line, const char *dirname, int mode)
 /* Create a file with the specified contents and report any failures. */
 int
 assertion_make_file(const char *file, int line,
-    const char *path, int mode, const char *contents)
+    const char *path, int mode, int csize, const void *contents)
 {
 #if defined(_WIN32) && !defined(__CYGWIN__)
        /* TODO: Rework this to set file mode as well. */
@@ -1534,8 +1534,13 @@ assertion_make_file(const char *file, int line,
                return (0);
        }
        if (contents != NULL) {
-               if (strlen(contents)
-                   != fwrite(contents, 1, strlen(contents), f)) {
+               size_t wsize;
+
+               if (csize < 0)
+                       wsize = strlen(contents);
+               else
+                       wsize = (size_t)csize;
+               if (wsize != fwrite(contents, 1, wsize, f)) {
                        fclose(f);
                        failure_start(file, line,
                            "Could not write file %s", path);
@@ -1555,10 +1560,16 @@ assertion_make_file(const char *file, int line,
                return (0);
        }
        if (contents != NULL) {
-               if ((ssize_t)strlen(contents)
-                   != write(fd, contents, strlen(contents))) {
+               ssize_t wsize;
+
+               if (csize < 0)
+                       wsize = (ssize_t)strlen(contents);
+               else
+                       wsize = (ssize_t)csize;
+               if (wsize != write(fd, contents, wsize)) {
                        close(fd);
-                       failure_start(file, line, "Could not write to %s", path);
+                       failure_start(file, line,
+                           "Could not write to %s", path);
                        failure_finish(NULL);
                        return (0);
                }
@@ -1803,7 +1814,7 @@ canSymlink(void)
                return (value);
 
        ++tested;
-       assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, "a");
+       assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
        /* Note: Cygwin has its own symlink() emulation that does not
         * use the Win32 CreateSymbolicLink() function. */
 #if defined(_WIN32) && !defined(__CYGWIN__)
@@ -1863,7 +1874,7 @@ canNodump(void)
        const char *path = "cannodumptest";
        struct stat sb;
 
-       assertion_make_file(__FILE__, __LINE__, path, 0644, NULL);
+       assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
        if (chflags(path, UF_NODUMP) < 0)
                return (0);
        if (stat(path, &sb) < 0)
@@ -1882,7 +1893,7 @@ canNodump(void)
        const char *path = "cannodumptest";
        int fd, r, flags;
 
-       assertion_make_file(__FILE__, __LINE__, path, 0644, NULL);
+       assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
        fd = open(path, O_RDONLY | O_NONBLOCK);
        if (fd < 0)
                return (0);
index 601ec938a597b81ffc4d7673e9d5d87fb2b2510c..d57a861dcb0bea196d85ac45f0ddfc1131ff8699 100644 (file)
 #define assertMakeDir(dirname, mode)   \
   assertion_make_dir(__FILE__, __LINE__, dirname, mode)
 #define assertMakeFile(path, mode, contents) \
-  assertion_make_file(__FILE__, __LINE__, path, mode, contents)
+  assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents)
+#define assertMakeBinFile(path, mode, csize, contents) \
+  assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents)
 #define assertMakeHardlink(newfile, oldfile)   \
   assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
 #define assertMakeSymlink(newfile, linkto)     \
@@ -243,7 +245,7 @@ int assertion_is_not_hardlink(const char *, int, const char *, const char *);
 int assertion_is_reg(const char *, int, const char *, int);
 int assertion_is_symlink(const char *, int, const char *, const char *);
 int assertion_make_dir(const char *, int, const char *, int);
-int assertion_make_file(const char *, int, const char *, int, const char *);
+int assertion_make_file(const char *, int, const char *, int, int, const void *);
 int assertion_make_hardlink(const char *, int, const char *newpath, const char *);
 int assertion_make_symlink(const char *, int, const char *newpath, const char *);
 int assertion_nodump(const char *, int, const char *);
index d141f4aedbab82e2f7a2502dd579584702e46529..0d2783f13d8f87dc0f059a41e0e944c0df4f0859 100644 (file)
@@ -848,10 +848,20 @@ __LA_DECL int     archive_match_path_excluded(struct archive *,
 __LA_DECL int  archive_match_exclude_pattern(struct archive *, const char *);
 __LA_DECL int  archive_match_exclude_pattern_w(struct archive *,
                    const wchar_t *);
+/* Add exclusion pathname pattern from file. */
+__LA_DECL int  archive_match_exclude_pattern_from_file(struct archive *,
+                   const char *, int _nullSeparator);
+__LA_DECL int  archive_match_exclude_pattern_from_file_w(struct archive *,
+                   const wchar_t *, int _nullSeparator);
 /* Add inclusion pathname pattern. */
 __LA_DECL int  archive_match_include_pattern(struct archive *, const char *);
 __LA_DECL int  archive_match_include_pattern_w(struct archive *,
                    const wchar_t *);
+/* Add inclusion pathname pattern from file. */
+__LA_DECL int  archive_match_include_pattern_from_file(struct archive *,
+                   const char *, int _nullSeparator);
+__LA_DECL int  archive_match_include_pattern_from_file_w(struct archive *,
+                   const wchar_t *, int _nullSeparator);
 /*
  * How to get statistic information for inclusion patterns.
  */
index c4dfd8b4786cd99ab970a26737df8c404cee05e7..36ee043d90bd2f8d3632e9506205ce1578d5efdb 100644 (file)
@@ -129,6 +129,8 @@ struct archive_match {
        struct match_list        inclusion_gnames;
 };
 
+static int     add_pattern_from_file(struct archive_match *,
+                   struct match_list *, int, const void *, int);
 static int     add_entry(struct archive_match *, int,
                    struct archive_entry *);
 static int     add_owner_id(struct archive_match *, struct id_array *,
@@ -341,6 +343,34 @@ archive_match_exclude_pattern_w(struct archive *_a, const wchar_t *pattern)
        return (ARCHIVE_OK);
 }
 
+int
+archive_match_exclude_pattern_from_file(struct archive *_a,
+    const char *pathname, int nullSeparator)
+{
+       struct archive_match *a;
+
+       archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
+           ARCHIVE_STATE_NEW, "archive_match_exclude_pattern_from_file");
+       a = (struct archive_match *)_a;
+
+       return add_pattern_from_file(a, &(a->exclusions), 1, pathname,
+               nullSeparator);
+}
+
+int
+archive_match_exclude_pattern_from_file_w(struct archive *_a,
+    const wchar_t *pathname, int nullSeparator)
+{
+       struct archive_match *a;
+
+       archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
+           ARCHIVE_STATE_NEW, "archive_match_exclude_pattern_from_file_w");
+       a = (struct archive_match *)_a;
+
+       return add_pattern_from_file(a, &(a->exclusions), 0, pathname,
+               nullSeparator);
+}
+
 int
 archive_match_include_pattern(struct archive *_a, const char *pattern)
 {
@@ -379,6 +409,34 @@ archive_match_include_pattern_w(struct archive *_a, const wchar_t *pattern)
        return (ARCHIVE_OK);
 }
 
+int
+archive_match_include_pattern_from_file(struct archive *_a,
+    const char *pathname, int nullSeparator)
+{
+       struct archive_match *a;
+
+       archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
+           ARCHIVE_STATE_NEW, "archive_match_include_pattern_from_file");
+       a = (struct archive_match *)_a;
+
+       return add_pattern_from_file(a, &(a->inclusions), 1, pathname,
+               nullSeparator);
+}
+
+int
+archive_match_include_pattern_from_file_w(struct archive *_a,
+    const wchar_t *pathname, int nullSeparator)
+{
+       struct archive_match *a;
+
+       archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
+           ARCHIVE_STATE_NEW, "archive_match_include_pattern_from_file_w");
+       a = (struct archive_match *)_a;
+
+       return add_pattern_from_file(a, &(a->inclusions), 0, pathname,
+               nullSeparator);
+}
+
 /*
  * Test functions for pathname patterns.
  *
@@ -504,6 +562,117 @@ add_pattern_wcs(struct archive_match *a, struct match_list *list,
        return (ARCHIVE_OK);
 }
 
+static int
+add_pattern_from_file(struct archive_match *a, struct match_list *mlist,
+    int mbs, const void *pathname, int nullSeparator)
+{
+       struct archive *ar;
+       struct archive_entry *ae;
+       struct archive_string as;
+       const void *buff;
+       size_t size;
+       int64_t offset;
+       int r;
+
+       ar = archive_read_new(); 
+       if (ar == NULL) {
+               archive_set_error(&(a->archive), ENOMEM, "No memory");
+               return (ARCHIVE_FATAL);
+       }
+       r = archive_read_support_format_raw(ar);
+       if (r != ARCHIVE_OK) {
+               archive_copy_error(&(a->archive), ar);
+               archive_read_free(ar);
+               return (r);
+       }
+       if (mbs)
+               r = archive_read_open_filename(ar, pathname, 512*20);
+       else
+               r = archive_read_open_filename_w(ar, pathname, 512*20);
+       if (r != ARCHIVE_OK) {
+               archive_copy_error(&(a->archive), ar);
+               archive_read_free(ar);
+               return (r);
+       }
+       r = archive_read_next_header(ar, &ae);
+       if (r != ARCHIVE_OK) {
+               archive_copy_error(&(a->archive), ar);
+               archive_read_free(ar);
+               return (r);
+       }
+
+       archive_string_init(&as);
+
+       while ((r = archive_read_data_block(ar, &buff, &size, &offset))
+           == ARCHIVE_OK) {
+               const char *b = (const char *)buff;
+
+               while (size) {
+                       const char *s = (const char *)b;
+                       size_t length = 0;
+                       int found_separator = 0;
+
+                       while (length < size) {
+                               if (nullSeparator) {
+                                       if (*b == '\0') {
+                                               found_separator = 1;
+                                               break;
+                                       }
+                               } else {
+                                       if (*b == 0x0d || *b == 0x0a) {
+                                               found_separator = 1;
+                                               break;
+                                       }
+                               }
+                               b++;
+                               length++;
+                       }
+                       if (!found_separator) {
+                               archive_strncat(&as, s, length);
+                               /* Read next data block. */
+                               break;
+                       }
+                       b++;
+                       size -= length + 1;
+                       archive_strncat(&as, s, length);
+
+                       /* If the line is not empty, add the pattern. */
+                       if (archive_strlen(&as) > 0) {
+                               /* Add pattern. */
+                               r = add_pattern_mbs(a, mlist, as.s);
+                               if (r != ARCHIVE_OK) {
+                                       archive_read_free(ar);
+                                       archive_string_free(&as);
+                                       return (r);
+                               }
+                               archive_string_empty(&as);
+                       }
+               }
+       }
+
+       /* If something error happend, report it immediately. */ 
+       if (r < ARCHIVE_OK) {
+               archive_copy_error(&(a->archive), ar);
+               archive_read_free(ar);
+               archive_string_free(&as);
+               return (r);
+       }
+
+       /* If the line is not empty, add the pattern. */
+       if (r == ARCHIVE_EOF && archive_strlen(&as) > 0) {
+               /* Add pattern. */
+               r = add_pattern_mbs(a, mlist, as.s);
+               if (r != ARCHIVE_OK) {
+                       archive_read_free(ar);
+                       archive_string_free(&as);
+                       return (r);
+               }
+       }
+       archive_read_free(ar);
+       archive_string_free(&as);
+       return (ARCHIVE_OK);
+}
+
 /*
  * Test if pathname is excluded by inclusion/exclusion patterns.
  */
index f30018e692d557138ed7d47516d0d574e8c21b16..b50ecc8edcd41b690ea867fa042ceeb5c2022ea4 100644 (file)
@@ -1518,7 +1518,7 @@ assertion_make_dir(const char *file, int line, const char *dirname, int mode)
 /* Create a file with the specified contents and report any failures. */
 int
 assertion_make_file(const char *file, int line,
-    const char *path, int mode, const char *contents)
+    const char *path, int mode, int csize, const void *contents)
 {
 #if defined(_WIN32) && !defined(__CYGWIN__)
        /* TODO: Rework this to set file mode as well. */
@@ -1532,8 +1532,13 @@ assertion_make_file(const char *file, int line,
                return (0);
        }
        if (contents != NULL) {
-               if (strlen(contents)
-                   != fwrite(contents, 1, strlen(contents), f)) {
+               size_t wsize;
+
+               if (csize < 0)
+                       wsize = strlen(contents);
+               else
+                       wsize = (size_t)csize;
+               if (wsize != fwrite(contents, 1, wsize, f)) {
                        fclose(f);
                        failure_start(file, line,
                            "Could not write file %s", path);
@@ -1553,10 +1558,16 @@ assertion_make_file(const char *file, int line,
                return (0);
        }
        if (contents != NULL) {
-               if ((ssize_t)strlen(contents)
-                   != write(fd, contents, strlen(contents))) {
+               ssize_t wsize;
+
+               if (csize < 0)
+                       wsize = (ssize_t)strlen(contents);
+               else
+                       wsize = (ssize_t)csize;
+               if (wsize != write(fd, contents, wsize)) {
                        close(fd);
-                       failure_start(file, line, "Could not write to %s", path);
+                       failure_start(file, line,
+                           "Could not write to %s", path);
                        failure_finish(NULL);
                        return (0);
                }
@@ -1801,7 +1812,7 @@ canSymlink(void)
                return (value);
 
        ++tested;
-       assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, "a");
+       assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
        /* Note: Cygwin has its own symlink() emulation that does not
         * use the Win32 CreateSymbolicLink() function. */
 #if defined(_WIN32) && !defined(__CYGWIN__)
@@ -1861,7 +1872,7 @@ canNodump(void)
        const char *path = "cannodumptest";
        struct stat sb;
 
-       assertion_make_file(__FILE__, __LINE__, path, 0644, NULL);
+       assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
        if (chflags(path, UF_NODUMP) < 0)
                return (0);
        if (stat(path, &sb) < 0)
@@ -1880,7 +1891,7 @@ canNodump(void)
        const char *path = "cannodumptest";
        int fd, r, flags;
 
-       assertion_make_file(__FILE__, __LINE__, path, 0644, NULL);
+       assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
        fd = open(path, O_RDONLY | O_NONBLOCK);
        if (fd < 0)
                return (0);
index e464612d5a9310c1fc0f11df6bd51963a3c211d7..7d2f20826f45f042b1dcfbf970e4ff5763c6aad5 100644 (file)
 #define assertMakeDir(dirname, mode)   \
   assertion_make_dir(__FILE__, __LINE__, dirname, mode)
 #define assertMakeFile(path, mode, contents) \
-  assertion_make_file(__FILE__, __LINE__, path, mode, contents)
+  assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents)
+#define assertMakeBinFile(path, mode, csize, contents) \
+  assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents)
 #define assertMakeHardlink(newfile, oldfile)   \
   assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
 #define assertMakeSymlink(newfile, linkto)     \
@@ -243,7 +245,7 @@ int assertion_is_not_hardlink(const char *, int, const char *, const char *);
 int assertion_is_reg(const char *, int, const char *, int);
 int assertion_is_symlink(const char *, int, const char *, const char *);
 int assertion_make_dir(const char *, int, const char *, int);
-int assertion_make_file(const char *, int, const char *, int, const char *);
+int assertion_make_file(const char *, int, const char *, int, int, const void *);
 int assertion_make_hardlink(const char *, int, const char *newpath, const char *);
 int assertion_make_symlink(const char *, int, const char *newpath, const char *);
 int assertion_nodump(const char *, int, const char *);
index 8fcf512b82659acdc6d668643c35f5ec829f4a49..5e9b9a8cbf638abe52978331e39e24a08f836eca 100644 (file)
@@ -112,6 +112,114 @@ test_exclusion_wcs(void)
        archive_match_free(m);
 }
 
+static void
+exclusion_from_file(struct archive *m)
+{
+       struct archive_entry *ae;
+
+       if (!assert((ae = archive_entry_new()) != NULL)) {
+               archive_match_free(m);
+               return;
+       }
+
+       /* Test with 'first', which should not be excluded. */
+       archive_entry_copy_pathname(ae, "first");
+       failure("'first' should not be excluded");
+       assertEqualInt(0, archive_match_path_excluded(m, ae));
+       assertEqualInt(0, archive_match_excluded(m, ae));
+       archive_entry_clear(ae);
+       archive_entry_copy_pathname_w(ae, L"first");
+       failure("'first' should not be excluded");
+       assertEqualInt(0, archive_match_path_excluded(m, ae));
+       assertEqualInt(0, archive_match_excluded(m, ae));
+
+       /* Test with 'second', which should be excluded. */
+       archive_entry_copy_pathname(ae, "second");
+       failure("'second' should be excluded");
+       assertEqualInt(1, archive_match_path_excluded(m, ae));
+       assertEqualInt(1, archive_match_excluded(m, ae));
+       archive_entry_clear(ae);
+       archive_entry_copy_pathname_w(ae, L"second");
+       failure("'second' should be excluded");
+       assertEqualInt(1, archive_match_path_excluded(m, ae));
+       assertEqualInt(1, archive_match_excluded(m, ae));
+
+       /* Test with 'third', which should not be excluded. */
+       archive_entry_copy_pathname(ae, "third");
+       failure("'third' should not be excluded");
+       assertEqualInt(0, archive_match_path_excluded(m, ae));
+       assertEqualInt(0, archive_match_excluded(m, ae));
+       archive_entry_clear(ae);
+       archive_entry_copy_pathname_w(ae, L"third");
+       failure("'third' should not be excluded");
+       assertEqualInt(0, archive_match_path_excluded(m, ae));
+       assertEqualInt(0, archive_match_excluded(m, ae));
+
+       /* Test with 'four', which should be excluded. */
+       archive_entry_copy_pathname(ae, "four");
+       failure("'four' should be excluded");
+       assertEqualInt(1, archive_match_path_excluded(m, ae));
+       assertEqualInt(1, archive_match_excluded(m, ae));
+       archive_entry_clear(ae);
+       archive_entry_copy_pathname_w(ae, L"four");
+       failure("'four' should be excluded");
+       assertEqualInt(1, archive_match_path_excluded(m, ae));
+       assertEqualInt(1, archive_match_excluded(m, ae));
+
+       /* Clean up. */
+       archive_entry_free(ae);
+}
+
+static void
+test_exclusion_from_file_mbs(void)
+{
+       struct archive *m;
+
+       /* Test1: read exclusion patterns from file */
+       if (!assert((m = archive_match_new()) != NULL))
+               return;
+       assertEqualIntA(m, 0,
+           archive_match_exclude_pattern_from_file(m, "exclusion", 0));
+       exclusion_from_file(m);
+       /* Clean up. */
+       archive_match_free(m);
+
+       /* Test2: read exclusion patterns in a null separator from file */
+       if (!assert((m = archive_match_new()) != NULL))
+               return;
+       /* Test for pattern reading from file */
+       assertEqualIntA(m, 0,
+           archive_match_exclude_pattern_from_file(m, "exclusion_null", 1));
+       exclusion_from_file(m);
+       /* Clean up. */
+       archive_match_free(m);
+}
+
+static void
+test_exclusion_from_file_wcs(void)
+{
+       struct archive *m;
+
+       /* Test1: read exclusion patterns from file */
+       if (!assert((m = archive_match_new()) != NULL))
+               return;
+       assertEqualIntA(m, 0,
+           archive_match_exclude_pattern_from_file_w(m, L"exclusion", 0));
+       exclusion_from_file(m);
+       /* Clean up. */
+       archive_match_free(m);
+
+       /* Test2: read exclusion patterns in a null separator from file */
+       if (!assert((m = archive_match_new()) != NULL))
+               return;
+       /* Test for pattern reading from file */
+       assertEqualIntA(m, 0,
+           archive_match_exclude_pattern_from_file_w(m, L"exclusion_null", 1));
+       exclusion_from_file(m);
+       /* Clean up. */
+       archive_match_free(m);
+}
+
 static void
 test_inclusion_mbs(void)
 {
@@ -210,6 +318,56 @@ test_inclusion_wcs(void)
        archive_match_free(m);
 }
 
+static void
+test_inclusion_from_file_mbs(void)
+{
+       struct archive *m;
+
+       /* Test1: read inclusion patterns from file */
+       if (!assert((m = archive_match_new()) != NULL))
+               return;
+       assertEqualIntA(m, 0,
+           archive_match_include_pattern_from_file(m, "inclusion", 0));
+       exclusion_from_file(m);
+       /* Clean up. */
+       archive_match_free(m);
+
+       /* Test2: read inclusion patterns in a null separator from file */
+       if (!assert((m = archive_match_new()) != NULL))
+               return;
+       assertEqualIntA(m, 0,
+           archive_match_include_pattern_from_file(m, "inclusion_null", 1));
+       exclusion_from_file(m);
+       /* Clean up. */
+       archive_match_free(m);
+}
+
+static void
+test_inclusion_from_file_wcs(void)
+{
+       struct archive *m;
+
+       /* Test1: read inclusion patterns from file */
+       if (!assert((m = archive_match_new()) != NULL))
+               return;
+       /* Test for pattern reading from file */
+       assertEqualIntA(m, 0,
+           archive_match_include_pattern_from_file_w(m, L"inclusion", 0));
+       exclusion_from_file(m);
+       /* Clean up. */
+       archive_match_free(m);
+
+       /* Test2: read inclusion patterns in a null separator from file */
+       if (!assert((m = archive_match_new()) != NULL))
+               return;
+       /* Test for pattern reading from file */
+       assertEqualIntA(m, 0,
+           archive_match_include_pattern_from_file_w(m, L"inclusion_null", 1));
+       exclusion_from_file(m);
+       /* Clean up. */
+       archive_match_free(m);
+}
+
 static void
 test_exclusion_and_inclusion(void)
 {
@@ -273,9 +431,20 @@ test_exclusion_and_inclusion(void)
 
 DEFINE_TEST(test_archive_match_path)
 {
+       /* Make exclusion sample files which contain exclusion patterns. */
+       assertMakeFile("exclusion", 0666, "second\nfour\n");
+       assertMakeBinFile("exclusion_null", 0666, 12, "second\0four\0");
+       /* Make inclusion sample files which contain inclusion patterns. */
+       assertMakeFile("inclusion", 0666, "first\nthird\n");
+       assertMakeBinFile("inclusion_null", 0666, 12, "first\0third\0");
+
        test_exclusion_mbs();
        test_exclusion_wcs();
+       test_exclusion_from_file_mbs();
+       test_exclusion_from_file_wcs();
        test_inclusion_mbs();
        test_inclusion_wcs();
+       test_inclusion_from_file_mbs();
+       test_inclusion_from_file_wcs();
        test_exclusion_and_inclusion();
 }
diff --git a/libarchive_fe/matching.c b/libarchive_fe/matching.c
deleted file mode 100644 (file)
index f416eae..0000000
+++ /dev/null
@@ -1,76 +0,0 @@
-/*-
- * Copyright (c) 2003-2007 Tim Kientzle
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "lafe_platform.h"
-__FBSDID("$FreeBSD: src/usr.bin/cpio/matching.c,v 1.2 2008/06/21 02:20:20 kientzle Exp $");
-
-#ifdef HAVE_ERRNO_H
-#include <errno.h>
-#endif
-
-#include "archive.h"
-#include "err.h"
-#include "line_reader.h"
-#include "matching.h"
-
-/*
- * Utility functions to manage exclusion/inclusion patterns
- */
-
-int
-lafe_exclude_from_file(struct archive *matching, const char *pathname)
-{
-       struct lafe_line_reader *lr;
-       const char *p;
-       int ret = 0;
-
-       lr = lafe_line_reader(pathname, 0);
-       while ((p = lafe_line_reader_next(lr)) != NULL) {
-               ret = archive_match_exclude_pattern(matching, p);
-               if (ret == ARCHIVE_FATAL)
-                       lafe_errc(1, errno, "Out of memory");
-       }
-       lafe_line_reader_free(lr);
-       return (ret);
-}
-
-int
-lafe_include_from_file(struct archive *matching, const char *pathname,
-    int nullSeparator)
-{
-       struct lafe_line_reader *lr;
-       const char *p;
-       int ret = 0;
-
-       lr = lafe_line_reader(pathname, nullSeparator);
-       while ((p = lafe_line_reader_next(lr)) != NULL) {
-               ret = archive_match_include_pattern(matching, p);
-               if (ret == ARCHIVE_FATAL)
-                       lafe_errc(1, errno, "Out of memory");
-       }
-       lafe_line_reader_free(lr);
-       return (ret);
-}
-
diff --git a/libarchive_fe/matching.h b/libarchive_fe/matching.h
deleted file mode 100644 (file)
index 4c174a8..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-/*-
- * Copyright (c) 2003-2007 Tim Kientzle
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer
- *    in this position and unchanged.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * $FreeBSD$
- */
-
-#ifndef MATCHING_H
-#define MATCHING_H
-
-#include "archive.h" 
-
-int    lafe_exclude_from_file(struct archive *, const char *pathname);
-int    lafe_include_from_file(struct archive *,
-                              const char *pathname, int nullSeparator);
-
-#endif
index 983aa342356a255636c3d1bd08b888d332de3151..eddf7ecc1ebb2357fe16017c24c9566700ac923d 100644 (file)
@@ -19,8 +19,6 @@ IF(ENABLE_TAR)
     ../libarchive_fe/lafe_platform.h
     ../libarchive_fe/line_reader.c
     ../libarchive_fe/line_reader.h
-    ../libarchive_fe/matching.c
-    ../libarchive_fe/matching.h
   )
   INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../libarchive_fe)
   IF(WIN32 AND NOT CYGWIN)
index 0d723f3acfbc4422b3519b330c444181fa4e84ed..52b5c91badd16b77fbf009219e66614d9a5faf4e 100644 (file)
@@ -555,11 +555,11 @@ main(int argc, char **argv)
                        bsdtar->option_interactive = 1;
                        break;
                case 'X': /* GNU tar */
-                       if (lafe_exclude_from_file(bsdtar->matching,
-                           bsdtar->argument))
-                               lafe_errc(1, 0,
-                                   "failed to process exclusions from file %s",
-                                   bsdtar->argument);
+                       if (archive_match_exclude_pattern_from_file(
+                           bsdtar->matching, bsdtar->argument, 0)
+                           != ARCHIVE_OK)
+                               lafe_errc(1, 0, "Error : %s",
+                                   archive_error_string(bsdtar->matching));
                        break;
                case 'x': /* SUSv2 */
                        set_mode(bsdtar, opt);
index 1f7dd74accf7d0f62242c6865ee7013d0a91fbcc..2387c44a354626bd63af858a462a990c5e1851ed 100644 (file)
@@ -28,8 +28,6 @@
 #include "bsdtar_platform.h"
 #include <stdio.h>
 
-#include "matching.h"
-
 #define        DEFAULT_BYTES_PER_BLOCK (20*512)
 
 /*
index 4fa6a547cdc3a25c059a000346a475383944c955..676ea0aa381b8765d3e5617a1a826373b5d66379 100644 (file)
@@ -166,8 +166,11 @@ read_archive(struct bsdtar *bsdtar, char mode, struct archive *writer)
        }
 
        if (bsdtar->names_from_file != NULL)
-               lafe_include_from_file(bsdtar->matching,
-                   bsdtar->names_from_file, bsdtar->option_null);
+               if (archive_match_include_pattern_from_file(
+                   bsdtar->matching, bsdtar->names_from_file,
+                   bsdtar->option_null) != ARCHIVE_OK)
+                       lafe_errc(1, 0, "Error inclusion pattern: %s",
+                           archive_error_string(bsdtar->matching));
 
        a = archive_read_new();
        if (bsdtar->compress_program != NULL)
index da49c7252f9ed65f91ddf97f62e7c398fab17092..81087d92369bc4cf1c46fea5a6bc276e9c780717 100644 (file)
@@ -1520,7 +1520,7 @@ assertion_make_dir(const char *file, int line, const char *dirname, int mode)
 /* Create a file with the specified contents and report any failures. */
 int
 assertion_make_file(const char *file, int line,
-    const char *path, int mode, const char *contents)
+    const char *path, int mode, int csize, const void *contents)
 {
 #if defined(_WIN32) && !defined(__CYGWIN__)
        /* TODO: Rework this to set file mode as well. */
@@ -1534,8 +1534,13 @@ assertion_make_file(const char *file, int line,
                return (0);
        }
        if (contents != NULL) {
-               if (strlen(contents)
-                   != fwrite(contents, 1, strlen(contents), f)) {
+               size_t wsize;
+
+               if (csize < 0)
+                       wsize = strlen(contents);
+               else
+                       wsize = (size_t)csize;
+               if (wsize != fwrite(contents, 1, wsize, f)) {
                        fclose(f);
                        failure_start(file, line,
                            "Could not write file %s", path);
@@ -1555,10 +1560,16 @@ assertion_make_file(const char *file, int line,
                return (0);
        }
        if (contents != NULL) {
-               if ((ssize_t)strlen(contents)
-                   != write(fd, contents, strlen(contents))) {
+               ssize_t wsize;
+
+               if (csize < 0)
+                       wsize = (ssize_t)strlen(contents);
+               else
+                       wsize = (ssize_t)csize;
+               if (wsize != write(fd, contents, wsize)) {
                        close(fd);
-                       failure_start(file, line, "Could not write to %s", path);
+                       failure_start(file, line,
+                           "Could not write to %s", path);
                        failure_finish(NULL);
                        return (0);
                }
@@ -1803,7 +1814,7 @@ canSymlink(void)
                return (value);
 
        ++tested;
-       assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, "a");
+       assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
        /* Note: Cygwin has its own symlink() emulation that does not
         * use the Win32 CreateSymbolicLink() function. */
 #if defined(_WIN32) && !defined(__CYGWIN__)
@@ -1863,7 +1874,7 @@ canNodump(void)
        const char *path = "cannodumptest";
        struct stat sb;
 
-       assertion_make_file(__FILE__, __LINE__, path, 0644, NULL);
+       assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
        if (chflags(path, UF_NODUMP) < 0)
                return (0);
        if (stat(path, &sb) < 0)
@@ -1882,7 +1893,7 @@ canNodump(void)
        const char *path = "cannodumptest";
        int fd, r, flags;
 
-       assertion_make_file(__FILE__, __LINE__, path, 0644, NULL);
+       assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
        fd = open(path, O_RDONLY | O_NONBLOCK);
        if (fd < 0)
                return (0);
index ccb14db4a43ea841b14e11aa726107fd87963ef9..46abb59eceee18a07408545406e7a1431bf77e85 100644 (file)
 #define assertMakeDir(dirname, mode)   \
   assertion_make_dir(__FILE__, __LINE__, dirname, mode)
 #define assertMakeFile(path, mode, contents) \
-  assertion_make_file(__FILE__, __LINE__, path, mode, contents)
+  assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents)
+#define assertMakeBinFile(path, mode, csize, contents) \
+  assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents)
 #define assertMakeHardlink(newfile, oldfile)   \
   assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
 #define assertMakeSymlink(newfile, linkto)     \
@@ -245,7 +247,7 @@ int assertion_is_not_hardlink(const char *, int, const char *, const char *);
 int assertion_is_reg(const char *, int, const char *, int);
 int assertion_is_symlink(const char *, int, const char *, const char *);
 int assertion_make_dir(const char *, int, const char *, int);
-int assertion_make_file(const char *, int, const char *, int, const char *);
+int assertion_make_file(const char *, int, const char *, int, int, const void *);
 int assertion_make_hardlink(const char *, int, const char *newpath, const char *);
 int assertion_make_symlink(const char *, int, const char *newpath, const char *);
 int assertion_nodump(const char *, int, const char *);