]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Test various line separators in -T input, including CRLF, NL, and null.
authorTim Kientzle <kientzle@gmail.com>
Sun, 23 Aug 2009 04:54:32 +0000 (00:54 -0400)
committerTim Kientzle <kientzle@gmail.com>
Sun, 23 Aug 2009 04:54:32 +0000 (00:54 -0400)
Fix tar to pass the test.

SVN-Revision: 1385

libarchive_fe/line_reader.c
libarchive_fe/line_reader.h
libarchive_fe/matching.c
libarchive_fe/matching.h
tar/read.c
tar/test/test_option_T_upper.c
tar/write.c

index 527576388837b94b977a8134dd18c1b41b8f34c9..21e1c705682f158be6400ea0a83331c1aff6cdff 100644 (file)
@@ -51,12 +51,12 @@ struct lafe_line_reader {
        char *buff, *buff_end, *line_start, *line_end, *p;
        char *pathname;
        size_t buff_length;
-       int separator;
+       int nullSeparator; /* Lines separated by null, not CR/CRLF/etc. */
        int ret;
 };
 
 struct lafe_line_reader *
-lafe_line_reader(const char *pathname, char separator)
+lafe_line_reader(const char *pathname, int nullSeparator)
 {
        struct lafe_line_reader *lr;
 
@@ -64,7 +64,7 @@ lafe_line_reader(const char *pathname, char separator)
        if (lr == NULL)
                lafe_errc(1, ENOMEM, "Can't open %s", pathname);
 
-       lr->separator = separator;
+       lr->nullSeparator = nullSeparator;
        lr->pathname = strdup(pathname);
 
        if (strcmp(pathname, "-") == 0)
@@ -91,14 +91,22 @@ lafe_line_reader_next(struct lafe_line_reader *lr)
        for (;;) {
                /* If there's a line in the buffer, return it immediately. */
                while (lr->line_end < lr->buff_end) {
-                       if (*lr->line_end == lr->separator) {
+                       if (lr->nullSeparator) {
+                               if (*lr->line_end == '\0') {
+                                       line_start = lr->line_start;
+                                       lr->line_start = lr->line_end + 1;
+                                       lr->line_end = lr->line_start;
+                                       return (line_start);
+                               }
+                       } else if (*lr->line_end == '\x0a' || *lr->line_end == '\x0d') {
                                *lr->line_end = '\0';
                                line_start = lr->line_start;
                                lr->line_start = lr->line_end + 1;
                                lr->line_end = lr->line_start;
-                               return (line_start);
-                       } else
-                               lr->line_end++;
+                               if (line_start[0] != '\0')
+                                       return (line_start);
+                       }
+                       lr->line_end++;
                }
 
                /* If we're at end-of-file, process the final data. */
index 7bb1742cb94f8e8174016457a3538e1d0a1cf7e2..d092c051f606895d14daac4bd07e94b72606c6d6 100644 (file)
@@ -28,7 +28,7 @@
 
 struct lafe_line_reader;
 
-struct lafe_line_reader *lafe_line_reader(const char *, char separator);
+struct lafe_line_reader *lafe_line_reader(const char *, int nullSeparator);
 const char *lafe_line_reader_next(struct lafe_line_reader *);
 void   lafe_line_reader_free(struct lafe_line_reader *);
 
index af5bde4e05d17ebe6f4dc467c6603a2214fce423..bdfd07b7df438a8031102888a174ab8021263e29 100644 (file)
@@ -111,13 +111,14 @@ lafe_include(struct lafe_matching **matching, const char *pattern)
 }
 
 int
-lafe_include_from_file(struct lafe_matching **matching, const char *pathname)
+lafe_include_from_file(struct lafe_matching **matching, const char *pathname,
+    int nullSeparator)
 {
        struct lafe_line_reader *lr;
        const char *p;
        int ret = 0;
 
-       lr = lafe_line_reader(pathname, '\n');
+       lr = lafe_line_reader(pathname, nullSeparator);
        while ((p = lafe_line_reader_next(lr)) != NULL) {
                if (lafe_include(matching, p) != 0)
                        ret = -1;
index 5cdc01667160724c5183a44b359c500c7404c70d..f4edebd4399702985be000f34beeb363fedf1907 100644 (file)
@@ -36,7 +36,7 @@ int   lafe_exclude_from_file(struct lafe_matching **matching,
                               const char *pathname);
 int    lafe_include(struct lafe_matching **matching, const char *pattern);
 int    lafe_include_from_file(struct lafe_matching **matching,
-                              const char *pathname);
+                              const char *pathname, int nullSeparator);
 
 int    lafe_excluded(struct lafe_matching *, const char *pathname);
 void   lafe_cleanup_exclusions(struct lafe_matching **);
index b95b0217ca81f83cae9737b0412fe3856a3e53f8..c5a8718c276c32f82cd6e2dd67eaa6e60357512a 100644 (file)
@@ -142,7 +142,8 @@ read_archive(struct bsdtar *bsdtar, char mode)
        }
 
        if (bsdtar->names_from_file != NULL)
-               lafe_include_from_file(&bsdtar->matching, bsdtar->names_from_file);
+               lafe_include_from_file(&bsdtar->matching,
+                   bsdtar->names_from_file, bsdtar->option_null);
 
        a = archive_read_new();
        if (bsdtar->compress_program != NULL)
index 4e656afc8825a17038276578066953bfd216f106..fbb83d721bc72c3ec10d4cf979ba23430b59eb81 100644 (file)
@@ -51,27 +51,35 @@ DEFINE_TEST(test_option_T_upper)
        if (!touch("d1/d2/f3")) return;
        if (!touch("d1/d2/f4")) return;
        if (!touch("d1/d2/f5")) return;
+       if (!touch("d1/d2/f6")) return;
+       if (!touch("d1/d2/f\x0a")) return;
 
        /* Populate a file list */
        f = fopen("filelist", "w+");
        if (!assert(f != NULL))
                return;
-       fprintf(f, "d1/f1\n");
-       fprintf(f, "d1/d2/f4\n");
+       /* Use a variety of text line endings. */
+       fprintf(f, "d1/f1\x0d\x0a"); /* CRLF */
+       fprintf(f, "d1/d2/f4\x0a"); /* NL */
+       fprintf(f, "d1/d2/f6"); /* EOF */
        fclose(f);
 
        /* Populate a second file list */
        f = fopen("filelist2", "w+");
        if (!assert(f != NULL))
                return;
-       fprintf(f, "d1/d2/f3\n");
-       fprintf(f, "d1/d2/f5\n");
+       /* Use null-terminated names. */
+       fprintf(f, "d1/d2/f3");
+       fwrite("\0", 1, 1, f);
+       fprintf(f, "d1/d2/f5");
+       fwrite("\0", 1, 1, f);
+       fprintf(f, "d1/d2/f\x0a");
+       fwrite("\0", 1, 1, f);
        fclose(f);
 
        /* Use -c -T to archive up the files. */
        r = systemf("%s -c -f test1.tar -T filelist > test1.out 2> test1.err",
            testprog);
-       failure("Failure here probably means that tar can't archive zero-length files without reading them");
        assert(r == 0);
        assertEmptyFile("test1.out");
        assertEmptyFile("test1.err");
@@ -89,9 +97,11 @@ DEFINE_TEST(test_option_T_upper)
        assertFileNotExists("test1/d1/d2/f3");
        assertFileExists("test1/d1/d2/f4");
        assertFileNotExists("test1/d1/d2/f5");
+       assertFileExists("test1/d1/d2/f6");
+       assertFileNotExists("test1/d1/d2/f\x0a");
 
        /* Use -r -T to add more files to the archive. */
-       systemf("%s -r -f test1.tar -T filelist2 > test2.out 2> test2.err",
+       systemf("%s -r -f test1.tar --null -T filelist2 > test2.out 2> test2.err",
            testprog);
        assertEmptyFile("test2.out");
        assertEmptyFile("test2.err");
@@ -108,6 +118,8 @@ DEFINE_TEST(test_option_T_upper)
        assertFileExists("test3/d1/d2/f3");
        assertFileExists("test3/d1/d2/f4");
        assertFileExists("test3/d1/d2/f5");
+       assertFileExists("test3/d1/d2/f6");
+       assertFileExists("test3/d1/d2/f\x0a");
 
        /* Use -x -T to dearchive the files (verify -x -T together) */
        if (!assertMakeDir("test2", 0755)) return;
@@ -121,6 +133,8 @@ DEFINE_TEST(test_option_T_upper)
        assertFileNotExists("test2/d1/d2/f3");
        assertFileExists("test2/d1/d2/f4");
        assertFileNotExists("test2/d1/d2/f5");
+       assertFileExists("test2/d1/d2/f6");
+       assertFileNotExists("test2/d1/d2/f\x0a");
 
        assertMakeDir("test4", 0755);
        assertMakeDir("test4_out", 0755);
@@ -148,6 +162,7 @@ DEFINE_TEST(test_option_T_upper)
        } else {
                skipping("bsdtar does not support -s option on this platform");
        }
+
        /* TODO: Include some use of -C directory-changing within the filelist. */
        /* I'm pretty sure -C within the filelist is broken on extract. */
 }
index 47ded8f245cec523063513c3bcd5233f10ff8c5b..c25a9af2ee026b0c1bf62b8fdd8cd54b59e7a47d 100644 (file)
@@ -520,7 +520,7 @@ archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
 
        bsdtar->next_line_is_dir = 0;
 
-       lr = lafe_line_reader(bsdtar->names_from_file, '\n');
+       lr = lafe_line_reader(bsdtar->names_from_file, bsdtar->option_null);
        while ((line = lafe_line_reader_next(lr)) != NULL) {
                if (bsdtar->next_line_is_dir) {
                        set_chdir(bsdtar, line);