From: Tim Kientzle Date: Thu, 1 May 2008 22:01:44 +0000 (-0400) Subject: Add a basic test of -T option, add support for -r and -T together. X-Git-Tag: v2.6.0~281 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=88d7f8865d93134def7d15a0ce63f104e42f8565;p=thirdparty%2Flibarchive.git Add a basic test of -T option, add support for -r and -T together. PR: bin/123246 Submitted by: Jaakko Heinonen MFP4 after: 1 day SVN-Revision: 11 --- diff --git a/tar/test/Makefile b/tar/test/Makefile index 25bd4cf3a..e54a6eb3f 100644 --- a/tar/test/Makefile +++ b/tar/test/Makefile @@ -14,6 +14,7 @@ TESTS= \ test_copy.c \ test_getdate.c \ test_help.c \ + test_option_T.c \ test_stdio.c \ test_version.c diff --git a/tar/test/main.c b/tar/test/main.c index e3bb263e8..3856f3a3c 100644 --- a/tar/test/main.c +++ b/tar/test/main.c @@ -541,6 +541,48 @@ test_assert_equal_file(const char *f1, const char *f2pattern, ...) return (0); } +int +test_assert_file_exists(const char *fpattern, ...) +{ + char f[1024]; + va_list ap; + + va_start(ap, fpattern); + vsprintf(f, fpattern, ap); + va_end(ap); + + if (!access(f, F_OK)) + return (1); + if (!previous_failures(test_filename, test_line)) { + fprintf(stderr, "%s:%d: File doesn't exist\n", + test_filename, test_line); + fprintf(stderr, " file=\"%s\"\n", f); + report_failure(test_extra); + } + return (0); +} + +int +test_assert_file_not_exists(const char *fpattern, ...) +{ + char f[1024]; + va_list ap; + + va_start(ap, fpattern); + vsprintf(f, fpattern, ap); + va_end(ap); + + if (access(f, F_OK)) + return (1); + if (!previous_failures(test_filename, test_line)) { + fprintf(stderr, "%s:%d: File exists and shouldn't\n", + test_filename, test_line); + fprintf(stderr, " file=\"%s\"\n", f); + report_failure(test_extra); + } + return (0); +} + /* assertFileContents() asserts the contents of a file. */ int test_assert_file_contents(const void *buff, int s, const char *fpattern, ...) diff --git a/tar/test/test.h b/tar/test/test.h index e2fcd1892..416f0dd62 100644 --- a/tar/test/test.h +++ b/tar/test/test.h @@ -102,6 +102,12 @@ /* Assert that a file is empty; supports printf-style arguments. */ #define assertEmptyFile \ test_setup(__FILE__, __LINE__);test_assert_empty_file +/* Assert that a file exists; supports printf-style arguments. */ +#define assertFileExists \ + test_setup(__FILE__, __LINE__);test_assert_file_exists +/* Assert that a file exists; supports printf-style arguments. */ +#define assertFileNotExists \ + test_setup(__FILE__, __LINE__);test_assert_file_not_exists /* * This would be simple with C99 variadic macros, but I don't want to @@ -124,6 +130,8 @@ int test_assert_equal_string(const char *, int, const char *v1, const char *, co int test_assert_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *); int test_assert_equal_mem(const char *, int, const char *, const char *, const char *, const char *, size_t, const char *, void *); int test_assert_file_contents(const void *, int, const char *, ...); +int test_assert_file_exists(const char *, ...); +int test_assert_file_not_exists(const char *, ...); /* Like sprintf, then system() */ int systemf(const char * fmt, ...); diff --git a/tar/test/test_option_T.c b/tar/test/test_option_T.c new file mode 100644 index 000000000..7b579085d --- /dev/null +++ b/tar/test/test_option_T.c @@ -0,0 +1,123 @@ +/*- + * Copyright (c) 2003-2008 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 "test.h" +__FBSDID("$FreeBSD$"); + +static int +touch(const char *fn) +{ + int fd = open(fn, O_RDWR | O_CREAT); + failure("Couldn't create file '%s', fd=%d, errno=%d (%s)\n", + fn, fd, errno, strerror(errno)); + if (!assert(fd > 0)) + return (0); /* Failure. */ + close(fd); + return (1); /* Success */ +} + +DEFINE_TEST(test_option_T) +{ + FILE *f; + + /* Create a simple dir heirarchy; bail if anything fails. */ + if (!assertEqualInt(0, mkdir("d1", 0755))) return; + if (!assertEqualInt(0, mkdir("d1/d2", 0755))) return; + if (!touch("d1/f1")) return; + if (!touch("d1/f2")) return; + if (!touch("d1/d2/f3")) return; + if (!touch("d1/d2/f4")) return; + if (!touch("d1/d2/f5")) 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"); + 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"); + fclose(f); + + /* Use -c -T to archive up the files. */ + systemf("%s -c -f test1.tar -T filelist > test1.out 2> test1.err", + testprog); + assertEmptyFile("test1.out"); + assertEmptyFile("test1.err"); + + /* Use -x -T to dearchive the files */ + if (!assertEqualInt(0, mkdir("test1", 0755))) return; + systemf("%s -x -f test1.tar -T filelist -C test1" + " > test1b.out 2> test1b.err", testprog); + assertEmptyFile("test1b.out"); + assertEmptyFile("test1b.err"); + + /* Verify the files were extracted. */ + assertFileExists("test1/d1/f1"); + assertFileNotExists("test1/d1/f2"); + assertFileNotExists("test1/d1/d2/f3"); + assertFileExists("test1/d1/d2/f4"); + assertFileNotExists("test1/d1/d2/f5"); + + /* Use -r -T to add more files to the archive. */ + systemf("%s -r -f test1.tar -T filelist2 > test2.out 2> test2.err", + testprog); + assertEmptyFile("test2.out"); + assertEmptyFile("test2.err"); + + /* Use -x without -T to dearchive the files (ensure -r worked) */ + if (!assertEqualInt(0, mkdir("test3", 0755))) return; + systemf("%s -x -f test1.tar -C test3" + " > test3.out 2> test3.err", testprog); + assertEmptyFile("test3.out"); + assertEmptyFile("test3.err"); + /* Verify the files were extracted.*/ + assertFileExists("test3/d1/f1"); + assertFileNotExists("test3/d1/f2"); + assertFileExists("test3/d1/d2/f3"); + assertFileExists("test3/d1/d2/f4"); + assertFileExists("test3/d1/d2/f5"); + + /* Use -x -T to dearchive the files (verify -x -T together) */ + if (!assertEqualInt(0, mkdir("test2", 0755))) return; + systemf("%s -x -f test1.tar -T filelist -C test2" + " > test2b.out 2> test2b.err", testprog); + assertEmptyFile("test2b.out"); + assertEmptyFile("test2b.err"); + /* Verify the files were extracted.*/ + assertFileExists("test2/d1/f1"); + assertFileNotExists("test2/d1/f2"); + assertFileNotExists("test2/d1/d2/f3"); + assertFileExists("test2/d1/d2/f4"); + assertFileNotExists("test2/d1/d2/f5"); + + /* 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/write.c b/tar/write.c index 82736afcd..6efb299e7 100644 --- a/tar/write.c +++ b/tar/write.c @@ -1534,7 +1534,7 @@ test_for_append(struct bsdtar *bsdtar) { struct stat s; - if (*bsdtar->argv == NULL) + if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL) bsdtar_errc(bsdtar, 1, 0, "no files or directories specified"); if (bsdtar->filename == NULL) bsdtar_errc(bsdtar, 1, 0, "Cannot append to stdout.");