From 5a1659d91e72340290d133e0f73c73b3fa15bc3c Mon Sep 17 00:00:00 2001 From: Tim Kientzle Date: Sun, 23 Aug 2009 00:16:58 -0400 Subject: [PATCH] Test -T with varying end-of-line conventions, including --null -T with a filename that includes a NL character. Fix the line parser in util.c to correctly handle these cases. SVN-Revision: 1384 --- tar/test/test_option_T.c | 26 +++++++++++++++++++++----- tar/util.c | 18 +++++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/tar/test/test_option_T.c b/tar/test/test_option_T.c index 36fe1ef4d..c2551e6ee 100644 --- a/tar/test/test_option_T.c +++ b/tar/test/test_option_T.c @@ -51,21 +51,30 @@ DEFINE_TEST(test_option_T) 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. */ @@ -89,9 +98,11 @@ DEFINE_TEST(test_option_T) 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 +119,8 @@ DEFINE_TEST(test_option_T) 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 (!assertEqualInt(0, mkdir("test2", 0755))) return; @@ -121,6 +134,8 @@ DEFINE_TEST(test_option_T) 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"); assertEqualInt(0, mkdir("test4", 0755)); assertEqualInt(0, mkdir("test4_out", 0755)); @@ -148,6 +163,7 @@ DEFINE_TEST(test_option_T) } 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. */ } diff --git a/tar/util.c b/tar/util.c index 274ff03cc..fb60f222f 100644 --- a/tar/util.c +++ b/tar/util.c @@ -286,10 +286,8 @@ process_lines(struct bsdtar *bsdtar, const char *pathname, FILE *f; char *buff, *buff_end, *line_start, *line_end, *p; size_t buff_length, new_buff_length, bytes_read, bytes_wanted; - int separator; int ret; - separator = bsdtar->option_null ? '\0' : '\n'; ret = 0; if (strcmp(pathname, "-") == 0) @@ -310,10 +308,20 @@ process_lines(struct bsdtar *bsdtar, const char *pathname, buff_end += bytes_read; /* Process all complete lines in the buffer. */ while (line_end < buff_end) { - if (*line_end == separator) { + if (bsdtar->option_null) { + if (*line_end == '\0') { + if ((*process)(bsdtar, line_start) != 0) + ret = -1; + line_start = line_end + 1; + line_end = line_start; + } else + line_end++; + } else if (*line_end == '\r' || *line_end == '\n') { *line_end = '\0'; - if ((*process)(bsdtar, line_start) != 0) - ret = -1; + if (line_end - line_start > 1) { + if ((*process)(bsdtar, line_start) != 0) + ret = -1; + } line_start = line_end + 1; line_end = line_start; } else -- 2.47.3