From: Tim Kientzle Date: Wed, 29 Jun 2011 04:56:26 +0000 (-0400) Subject: Issue 160: --strip-components now works in -c mode as well as -x and -t. X-Git-Tag: v3.0.0a~253 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d5c70ee05eead5697fc96b29fee8758990612d0;p=thirdparty%2Flibarchive.git Issue 160: --strip-components now works in -c mode as well as -x and -t. SVN-Revision: 3444 --- diff --git a/tar/bsdtar.1 b/tar/bsdtar.1 index 41e35ccdc..c71aad52c 100644 --- a/tar/bsdtar.1 +++ b/tar/bsdtar.1 @@ -505,7 +505,6 @@ and the default behavior if .Nm is run as root. .It Fl Fl strip-components Ar count -(x mode only) Remove the specified number of leading path elements. Pathnames with fewer elements will be silently skipped. Note that the pathname is edited after checking inclusion/exclusion patterns diff --git a/tar/bsdtar.c b/tar/bsdtar.c index 00a13e510..d69ffb345 100644 --- a/tar/bsdtar.c +++ b/tar/bsdtar.c @@ -642,8 +642,6 @@ main(int argc, char **argv) buff[1] = bsdtar->symlink_mode; only_mode(bsdtar, buff, "cru"); } - if (bsdtar->strip_components != 0) - only_mode(bsdtar, "--strip-components", "xt"); /* Filename "-" implies stdio. */ if (strcmp(bsdtar->filename, "-") == 0) diff --git a/tar/test/test_strip_components.c b/tar/test/test_strip_components.c index d4a093065..d195af1b3 100644 --- a/tar/test/test_strip_components.c +++ b/tar/test/test_strip_components.c @@ -41,6 +41,9 @@ DEFINE_TEST(test_strip_components) } assertChdir(".."); + /* + * Test 1: Strip components when extracting archives. + */ if (canSymlink()) assertEqualInt(0, systemf("%s -cf test.tar d0/l1 d0/s1 d0/d1", testprog)); @@ -56,6 +59,8 @@ DEFINE_TEST(test_strip_components) assertFileNotExists("target/d0"); failure("d0/d1/ is too short and should not get restored"); assertFileNotExists("target/d1"); + failure("d0/s1 is too short and should not get restored"); + assertFileNotExists("target/s1"); failure("d0/d1/s2 is a symlink to something that won't be extracted"); /* If platform supports symlinks, target/s2 is a broken symlink. */ /* If platform does not support symlink, target/s2 doesn't exist. */ @@ -66,6 +71,8 @@ DEFINE_TEST(test_strip_components) assertIsDir("target/d2", -1); /* + * Test 1b: Strip components extracting archives involving hardlinks. + * * This next is a complicated case. d0/l1, d0/d1/l2, and * d0/d1/d2/f1 are all hardlinks to the same file; d0/l1 can't * be extracted with --strip-components=2 and the other two @@ -92,4 +99,41 @@ DEFINE_TEST(test_strip_components) assertFileNotExists("target/l2"); failure("d0/d1/d2/f1 is a hardlink to file whose name was too short"); assertFileNotExists("target/d2/f1"); + + /* + * Test 2: Strip components when creating archives. + */ + if (canSymlink()) + assertEqualInt(0, systemf("%s --strip-components 2 -cf test2.tar " + "d0/l1 d0/s1 d0/d1", testprog)); + else + assertEqualInt(0, systemf("%s --strip-components 2 -cf test2.tar " + "d0/l1 d0/d1", testprog)); + + assertMakeDir("target2", 0755); + assertEqualInt(0, systemf("%s -x -C target2 -f test2.tar", testprog)); + + failure("d0/ is too short and should not have been archived"); + assertFileNotExists("target2/d0"); + failure("d0/d1/ is too short and should not have been archived"); + assertFileNotExists("target2/d1"); + failure("d0/s1 is too short and should not get restored"); + assertFileNotExists("target/s1"); + /* If platform supports symlinks, target/s2 is included. */ + if (canSymlink()) { + failure("d0/d1/s2 is a symlink to something included in archive"); + assertIsSymlink("target2/s2", "d2/f1"); + } + failure("d0/d1/d2 should be archived"); + assertIsDir("target2/d2", -1); + + /* + * Test 2b: Strip components creating archives involving hardlinks. + */ + failure("d0/l1 is too short and should not have been archived"); + assertFileNotExists("target/l1"); + failure("d0/d1/l2 is a hardlink to file whose name was too short"); + assertFileNotExists("target/l2"); + failure("d0/d1/d2/f1 is a hardlink to file whose name was too short"); + assertFileNotExists("target/d2/f1"); } diff --git a/tar/util.c b/tar/util.c index 42e10e451..dbdcfd667 100644 --- a/tar/util.c +++ b/tar/util.c @@ -365,9 +365,7 @@ edit_pathname(struct bsdtar *bsdtar, struct archive_entry *entry) #if HAVE_REGEX_H char *subst_name; int r; -#endif -#if HAVE_REGEX_H r = apply_substitution(bsdtar, name, &subst_name, 0); if (r == -1) { lafe_warnc(0, "Invalid substitution, skipping entry");