diff options
author | Xin Gao <xingao@google.com> | 2016-09-22 22:08:01 +0000 |
---|---|---|
committer | Laszlo Csomor <laszlocsomor@google.com> | 2016-09-23 08:15:57 +0000 |
commit | af042f7a5ff29a8775de7019f429ff917b583c32 (patch) | |
tree | 6d89bc4e04f4de9dad4c599ff5203f134d925891 /src | |
parent | d9f2d8599207bd8d16e3046e5443132b95a2952d (diff) |
Fixed symbolic link and hard link path not stripped when "strip_prefix" is set.
--
MOS_MIGRATED_REVID=134005484
Diffstat (limited to 'src')
4 files changed, 78 insertions, 17 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/CompressedTarFunction.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/CompressedTarFunction.java index daf6e66267..c76bf58b5b 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/CompressedTarFunction.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/CompressedTarFunction.java @@ -59,8 +59,13 @@ public abstract class CompressedTarFunction implements Decompressor { } else { if (entry.isSymbolicLink() || entry.isLink()) { PathFragment linkName = new PathFragment(entry.getLinkName()); - if (linkName.isAbsolute()) { - linkName = linkName.relativeTo(PathFragment.ROOT_DIR); + boolean wasAbsolute = linkName.isAbsolute(); + // Strip the prefix from the link path if set. + linkName = + StripPrefixedPath.maybeDeprefix(linkName.getPathString(), prefix).getPathFragment(); + if (wasAbsolute) { + // Recover the path to an absolute path as maybeDeprefix() relativize the path + // even if the prefix is not set linkName = descriptor.repositoryPath().getRelative(linkName).asFragment(); } if (entry.isSymbolicLink()) { diff --git a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java b/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java index c8d1f3c2c4..73a9a5119f 100644 --- a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java +++ b/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java @@ -1010,19 +1010,19 @@ public class FileSystemUtils { */ public static void createHardLink(Path linkPath, Path originalPath) throws IOException { - // Regular file - if (originalPath.isFile()) { + // Directory + if (originalPath.isDirectory()) { + for (Path originalSubpath : originalPath.getDirectoryEntries()) { + Path linkSubpath = linkPath.getRelative(originalSubpath.relativeTo(originalPath)); + createHardLink(linkSubpath, originalSubpath); + } + // Other types of file + } else { Path parentDir = linkPath.getParentDirectory(); if (!parentDir.exists()) { FileSystemUtils.createDirectoryAndParents(parentDir); } originalPath.createHardLink(linkPath); - // Directory - } else if (originalPath.isDirectory()) { - for (Path originalSubpath : originalPath.getDirectoryEntries()) { - Path linkSubpath = linkPath.getRelative(originalSubpath.relativeTo(originalPath)); - createHardLink(linkSubpath, originalSubpath); - } } } } diff --git a/src/test/java/com/google/devtools/build/lib/rules/repository/CompressedTarFunctionTest.java b/src/test/java/com/google/devtools/build/lib/rules/repository/CompressedTarFunctionTest.java index b638b21052..2002c626a7 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/repository/CompressedTarFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/repository/CompressedTarFunctionTest.java @@ -49,13 +49,27 @@ public class CompressedTarFunctionTest { private static final String HARD_LINK_FILE_NAME = "hardLinkFile"; /* Symbolic(Soft) link file, created by ln -s <REGULAR_FILE_NAME> <SYMBOLIC_LINK_FILE_NAME> */ - private static final String SYMBOLIC_LINK_FILE_NAME = "symbolicLinkFile"; + private static final String RELATIVE_SYMBOLIC_LINK_FILE_NAME = "relativeSymbolicLinkFile"; + private static final String ABSOLUTE_SYMBOLIC_LINK_FILE_NAME = "absoluteSymbolicLinkFile"; private static final String PATH_TO_TEST_ARCHIVE = "/com/google/devtools/build/lib/rules/repository/"; + private static final String ROOT_FOLDER_NAME = "root_folder"; + + private static final String INNER_FOLDER_NAME = "another_folder"; + /* Tarball, created by * tar -czf <ARCHIVE_NAME> <REGULAR_FILE_NAME> <HARD_LINK_FILE_NAME> <SYMBOLIC_LINK_FILE_NAME> + * + * The tarball has the following structure + * + * root_folder/ + * another_folder/ + * regularFile + * hardLinkFile hardlink to root_folder/another_folder/regularFile + * relativeSymbolicLinkFile -> regularFile + * absoluteSymbolicLinkFile -> /root_folder/another_folder/regularFile */ private static final String ARCHIVE_NAME = "test_decompress_archive.tar.gz"; @@ -86,12 +100,13 @@ public class CompressedTarFunctionTest { } /** - * Test decompressing a tar.gz file with hard link file and symbolic link file inside + * Test decompressing a tar.gz file with hard link file and symbolic link file inside without + * stripping a prefix * * @throws Exception */ @Test - public void testDecompress() throws Exception { + public void testDecompressWithoutPrefix() throws Exception { Path outputDir = new CompressedTarFunction() { @@ -102,6 +117,35 @@ public class CompressedTarFunctionTest { } }.decompress(descriptorBuilder.build()); + assertOutputFiles(outputDir.getRelative(ROOT_FOLDER_NAME).getRelative(INNER_FOLDER_NAME)); + } + + /** + * Test decompressing a tar.gz file with hard link file and symbolic link file inside and + * stripping a prefix + * + * @throws Exception + */ + @Test + public void testDecompressWithPrefix() throws Exception { + + descriptorBuilder.setPrefix(ROOT_FOLDER_NAME); + + Path outputDir = + new CompressedTarFunction() { + @Override + protected InputStream getDecompressorStream(DecompressorDescriptor descriptor) + throws IOException { + return new GZIPInputStream(new FileInputStream(descriptor.archivePath().getPathFile())); + } + }.decompress(descriptorBuilder.build()); + + assertOutputFiles(outputDir.getRelative(INNER_FOLDER_NAME)); + } + + /** Validate the content of the output directory */ + private void assertOutputFiles(Path outputDir) throws Exception { + assertThat(outputDir.exists()).isTrue(); assertThat(outputDir.getRelative(REGULAR_FILE_NAME).exists()).isTrue(); assertThat(outputDir.getRelative(REGULAR_FILE_NAME).getFileSize()).isNotEqualTo(0); @@ -109,9 +153,14 @@ public class CompressedTarFunctionTest { assertThat(outputDir.getRelative(HARD_LINK_FILE_NAME).exists()).isTrue(); assertThat(outputDir.getRelative(HARD_LINK_FILE_NAME).getFileSize()).isNotEqualTo(0); assertThat(outputDir.getRelative(HARD_LINK_FILE_NAME).isSymbolicLink()).isFalse(); - assertThat(outputDir.getRelative(SYMBOLIC_LINK_FILE_NAME).exists()).isTrue(); - assertThat(outputDir.getRelative(SYMBOLIC_LINK_FILE_NAME).getFileSize()).isNotEqualTo(0); - assertThat(outputDir.getRelative(SYMBOLIC_LINK_FILE_NAME).isSymbolicLink()).isTrue(); + assertThat(outputDir.getRelative(RELATIVE_SYMBOLIC_LINK_FILE_NAME).exists()).isTrue(); + assertThat(outputDir.getRelative(RELATIVE_SYMBOLIC_LINK_FILE_NAME).getFileSize()) + .isNotEqualTo(0); + assertThat(outputDir.getRelative(RELATIVE_SYMBOLIC_LINK_FILE_NAME).isSymbolicLink()).isTrue(); + assertThat(outputDir.getRelative(ABSOLUTE_SYMBOLIC_LINK_FILE_NAME).exists()).isTrue(); + assertThat(outputDir.getRelative(ABSOLUTE_SYMBOLIC_LINK_FILE_NAME).getFileSize()) + .isNotEqualTo(0); + assertThat(outputDir.getRelative(ABSOLUTE_SYMBOLIC_LINK_FILE_NAME).isSymbolicLink()).isTrue(); assertThat( Files.isSameFile( java.nio.file.Paths.get(outputDir.getRelative(REGULAR_FILE_NAME).toString()), @@ -120,7 +169,14 @@ public class CompressedTarFunctionTest { assertThat( Files.isSameFile( java.nio.file.Paths.get(outputDir.getRelative(REGULAR_FILE_NAME).toString()), - java.nio.file.Paths.get(outputDir.getRelative(SYMBOLIC_LINK_FILE_NAME).toString()))) + java.nio.file.Paths.get( + outputDir.getRelative(RELATIVE_SYMBOLIC_LINK_FILE_NAME).toString()))) + .isTrue(); + assertThat( + Files.isSameFile( + java.nio.file.Paths.get(outputDir.getRelative(REGULAR_FILE_NAME).toString()), + java.nio.file.Paths.get( + outputDir.getRelative(ABSOLUTE_SYMBOLIC_LINK_FILE_NAME).toString()))) .isTrue(); } } diff --git a/src/test/java/com/google/devtools/build/lib/rules/repository/test_decompress_archive.tar.gz b/src/test/java/com/google/devtools/build/lib/rules/repository/test_decompress_archive.tar.gz Binary files differindex f951e97860..4e4910c3f9 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/repository/test_decompress_archive.tar.gz +++ b/src/test/java/com/google/devtools/build/lib/rules/repository/test_decompress_archive.tar.gz |