Skip to content

Commit

Permalink
Fix new_http_archive's strip_prefix for zips
Browse files Browse the repository at this point in the history
The removal of the prefix was incorrectly applied to the zip entry
instead of the destination.

Fixes #221

Tested:
    - Added a copy of the existing integration test with zip archive
    - bash compile.sh all

--
Change-Id: I5d5f75f66a17eb6f146afafb1f347a781490e616
Reviewed-on: https://bazel-review.googlesource.com/#/c/2084/
MOS_MIGRATED_REVID=104774296
  • Loading branch information
kamalmarhubi authored and lberki committed Oct 7, 2015
1 parent ad1114f commit eab38e8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public SkyValue compute(SkyKey skyKey, Environment env) throws RepositoryFunctio
if (entryPath.skip()) {
continue;
}
entry.setName(entryPath.getPathFragment().getPathString());
extractZipEntry(reader, entry, destinationDirectory);
extractZipEntry(reader, entry, destinationDirectory, entryPath.getPathFragment());
}
} catch (IOException e) {
throw new RepositoryFunctionException(new IOException(
Expand All @@ -92,16 +91,19 @@ public SkyValue compute(SkyKey skyKey, Environment env) throws RepositoryFunctio
return new DecompressorValue(destinationDirectory);
}

private void extractZipEntry(ZipReader reader, ZipFileEntry entry, Path destinationDirectory)
private void extractZipEntry(
ZipReader reader,
ZipFileEntry entry,
Path destinationDirectory,
PathFragment strippedRelativePath)
throws IOException {
String relativeString = entry.getName();
PathFragment relativePath = new PathFragment(relativeString);
if (relativePath.isAbsolute()) {
if (strippedRelativePath.isAbsolute()) {
throw new IOException(
String.format("Failed to extract %s, zipped paths cannot be absolute", relativePath));
String.format(
"Failed to extract %s, zipped paths cannot be absolute", strippedRelativePath));
}
Path outputPath = destinationDirectory.getRelative(relativePath);
int permissions = getPermissions(entry.getExternalAttributes(), relativeString);
Path outputPath = destinationDirectory.getRelative(strippedRelativePath);
int permissions = getPermissions(entry.getExternalAttributes(), entry.getName());
FileSystemUtils.createDirectoryAndParents(outputPath.getParentDirectory());
boolean isDirectory = (permissions & 040000) == 040000;
if (isDirectory) {
Expand Down
31 changes: 30 additions & 1 deletion src/test/shell/bazel/external_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ EOF
expect_log "bazel fetch //..."
}

function test_prefix_stripping() {
function test_prefix_stripping_tar_gz() {
mkdir -p x/y/z
echo "abc" > x/y/z/w
tar czf x.tar.gz x
Expand Down Expand Up @@ -529,4 +529,33 @@ EOF
assert_contains "abc" bazel-genfiles/external/x/catter.out
}

function test_prefix_stripping_zip() {
mkdir -p x/y/z
echo "abc" > x/y/z/w
zip -r x x
local sha256=$(sha256sum x.zip | cut -f 1 -d ' ')
serve_file x.zip

cat > WORKSPACE <<EOF
new_http_archive(
name = "x",
url = "http://localhost:$nc_port/x.zip",
sha256 = "$sha256",
strip_prefix = "x/y/z",
build_file = "x.BUILD",
)
EOF
cat > x.BUILD <<EOF
genrule(
name = "catter",
cmd = "cat \$< > \$@",
outs = ["catter.out"],
srcs = ["w"],
)
EOF

bazel build @x//:catter &> $TEST_log || fail "Build failed"
assert_contains "abc" bazel-genfiles/external/x/catter.out
}

run_suite "external tests"

0 comments on commit eab38e8

Please sign in to comment.