Skip to content

Commit 9c98120

Browse files
kjlubickcopybara-github
authored andcommitted
Add support for .ar archives (and .deb files)
This implements #[15130](bazelbuild#15130). As I was updating the docs for .ar and .deb formats, I also addressed some previous formats that had been added but not propagated through to all the documentation places. Closes bazelbuild#15132. PiperOrigin-RevId: 439569440
1 parent 4a1e5e4 commit 9c98120

File tree

8 files changed

+192
-5
lines changed

8 files changed

+192
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2022 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.devtools.build.lib.bazel.repository;
16+
17+
import com.google.common.io.ByteStreams;
18+
import com.google.devtools.build.lib.bazel.repository.DecompressorValue.Decompressor;
19+
import com.google.devtools.build.lib.vfs.Path;
20+
import java.io.BufferedInputStream;
21+
import java.io.FileInputStream;
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.OutputStream;
25+
import org.apache.commons.compress.archivers.ar.ArArchiveEntry;
26+
import org.apache.commons.compress.archivers.ar.ArArchiveInputStream;
27+
28+
/**
29+
* Opens a .ar archive file. It ignores the prefix setting because these archives cannot contain
30+
* directories.
31+
*/
32+
public class ArFunction implements Decompressor {
33+
34+
public static final Decompressor INSTANCE = new ArFunction();
35+
36+
// This is the same value as picked for .tar files, which appears to have worked well.
37+
private static final int BUFFER_SIZE = 32 * 1024;
38+
39+
private InputStream getDecompressorStream(DecompressorDescriptor descriptor) throws IOException {
40+
return new BufferedInputStream(
41+
new FileInputStream(descriptor.archivePath().getPathFile()), BUFFER_SIZE);
42+
}
43+
;
44+
45+
@Override
46+
public Path decompress(DecompressorDescriptor descriptor)
47+
throws InterruptedException, IOException {
48+
if (Thread.interrupted()) {
49+
throw new InterruptedException();
50+
}
51+
52+
try (InputStream decompressorStream = getDecompressorStream(descriptor)) {
53+
ArArchiveInputStream arStream = new ArArchiveInputStream(decompressorStream);
54+
ArArchiveEntry entry;
55+
while ((entry = arStream.getNextArEntry()) != null) {
56+
Path filePath = descriptor.repositoryPath().getRelative(entry.getName());
57+
filePath.getParentDirectory().createDirectoryAndParents();
58+
if (entry.isDirectory()) {
59+
// ar archives don't contain any directory information, so this should never
60+
// happen
61+
continue;
62+
} else {
63+
// We do not have to worry about symlinks in .ar files - it's not supported
64+
// by the .ar file format.
65+
try (OutputStream out = filePath.getOutputStream()) {
66+
ByteStreams.copy(arStream, out);
67+
}
68+
filePath.chmod(entry.getMode());
69+
// entry.getLastModified() appears to be in seconds, so we need to convert
70+
// it into milliseconds for setLastModifiedTime
71+
filePath.setLastModifiedTime(entry.getLastModified() * 1000L);
72+
}
73+
if (Thread.interrupted()) {
74+
throw new InterruptedException();
75+
}
76+
}
77+
}
78+
79+
return descriptor.repositoryPath();
80+
}
81+
}

src/main/java/com/google/devtools/build/lib/bazel/repository/DecompressorValue.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ static Decompressor getDecompressor(Path archivePath)
107107
return TarZstFunction.INSTANCE;
108108
} else if (baseName.endsWith(".tar.bz2")) {
109109
return TarBz2Function.INSTANCE;
110+
} else if (baseName.endsWith(".ar") || baseName.endsWith(".deb")) {
111+
return ArFunction.INSTANCE;
110112
} else {
111113
throw new RepositoryFunctionException(
112114
Starlark.errorf(
113115
"Expected a file with a .zip, .jar, .war, .aar, .tar, .tar.gz, .tgz, .tar.xz, .txz,"
114-
+ " .tar.zst, .tzst, or .tar.bz2 suffix (got %s)",
116+
+ " .tar.zst, .tzst, .tar.bz2, .ar or .deb suffix (got %s)",
115117
archivePath),
116118
Transience.PERSISTENT);
117119
}

src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryContext.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,8 @@ public void extract(Object archive, Object output, String stripPrefix, StarlarkT
825825
+ " By default, the archive type is determined from the file extension of"
826826
+ " the URL."
827827
+ " If the file has no extension, you can explicitly specify either \"zip\","
828-
+ " \"jar\", \"war\", \"aar\", \"tar.gz\", \"tgz\", \"tar.bz2\", or \"tar.xz\""
828+
+ " \"jar\", \"war\", \"aar\", \"tar\", \"tar.gz\", \"tgz\", \"tar.xz\","
829+
+ " \"txz\", \".tar.zst\", \".tzst\", \"tar.bz2\", \".ar\", or \".deb\""
829830
+ " here."),
830831
@Param(
831832
name = "stripPrefix",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2022 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.devtools.build.lib.bazel.repository;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
19+
import com.google.devtools.build.lib.testutil.TestConstants;
20+
import com.google.devtools.build.lib.testutil.TestUtils;
21+
import com.google.devtools.build.lib.unix.UnixFileSystem;
22+
import com.google.devtools.build.lib.util.OS;
23+
import com.google.devtools.build.lib.vfs.DigestHashFunction;
24+
import com.google.devtools.build.lib.vfs.FileSystem;
25+
import com.google.devtools.build.lib.vfs.JavaIoFileSystem;
26+
import com.google.devtools.build.lib.vfs.Path;
27+
import com.google.devtools.build.runfiles.Runfiles;
28+
import java.io.File;
29+
import java.io.IOException;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.junit.runners.JUnit4;
33+
34+
/** Tests decompressing archives. */
35+
@RunWith(JUnit4.class)
36+
public class ArFunctionTest {
37+
/*
38+
* .ar archive created with ar cr test_files.ar archived_first.txt archived_second.md
39+
* The files contain short UTF-8 encoded strings.
40+
*/
41+
private static final String ARCHIVE_NAME = "test_files.ar";
42+
private static final String PATH_TO_TEST_ARCHIVE =
43+
"/com/google/devtools/build/lib/bazel/repository/";
44+
private static final String FIRST_FILE_NAME = "archived_first.txt";
45+
private static final String SECOND_FILE_NAME = "archived_second.md";
46+
47+
@Test
48+
public void testDecompress() throws Exception {
49+
Path outputDir = decompress(createDescriptorBuilder());
50+
51+
assertThat(outputDir.exists()).isTrue();
52+
Path firstFile = outputDir.getRelative(FIRST_FILE_NAME);
53+
assertThat(firstFile.exists()).isTrue();
54+
// There are 20 bytes in the content "this is test file 1"
55+
assertThat(firstFile.getFileSize()).isEqualTo(20);
56+
assertThat(firstFile.isSymbolicLink()).isFalse();
57+
58+
Path secondFile = outputDir.getRelative(SECOND_FILE_NAME);
59+
assertThat(secondFile.exists()).isTrue();
60+
// There are 20 bytes in the content "this is the second test file"
61+
assertThat(secondFile.getFileSize()).isEqualTo(29);
62+
assertThat(secondFile.isSymbolicLink()).isFalse();
63+
}
64+
65+
private Path decompress(DecompressorDescriptor.Builder descriptorBuilder) throws Exception {
66+
descriptorBuilder.setDecompressor(ArFunction.INSTANCE);
67+
return new ArFunction().decompress(descriptorBuilder.build());
68+
}
69+
70+
private DecompressorDescriptor.Builder createDescriptorBuilder() throws IOException {
71+
// This was cribbed from TestArchiveDescriptor
72+
FileSystem testFS =
73+
OS.getCurrent() == OS.WINDOWS
74+
? new JavaIoFileSystem(DigestHashFunction.SHA256)
75+
: new UnixFileSystem(DigestHashFunction.SHA256, /*hashAttributeName=*/ "");
76+
77+
// do not rely on TestConstants.JAVATESTS_ROOT end with slash, but ensure separators
78+
// are not duplicated
79+
String path =
80+
(TestConstants.JAVATESTS_ROOT + PATH_TO_TEST_ARCHIVE + ARCHIVE_NAME).replace("//", "/");
81+
Path tarballPath = testFS.getPath(Runfiles.create().rlocation(path));
82+
83+
Path workingDir = testFS.getPath(new File(TestUtils.tmpDir()).getCanonicalPath());
84+
Path outDir = workingDir.getRelative("out");
85+
86+
return DecompressorDescriptor.builder().setRepositoryPath(outDir).setArchivePath(tarballPath);
87+
}
88+
}

src/test/java/com/google/devtools/build/lib/bazel/repository/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ java_library(
2727
data = [
2828
"test_decompress_archive.tar.gz",
2929
"test_decompress_archive.zip",
30+
"test_files.ar",
3031
],
3132
deps = [
3233
"//src/main/java/com/google/devtools/build/lib/bazel/repository",

src/test/java/com/google/devtools/build/lib/bazel/repository/DecompressorValueTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public void testKnownFileExtensionsDoNotThrow() throws Exception {
5656
DecompressorDescriptor.builder().setArchivePath(path).build();
5757
path = fs.getPath("/foo/.external-repositories/some-repo/bar.baz.tar.bz2");
5858
DecompressorDescriptor.builder().setArchivePath(path).build();
59+
path = fs.getPath("/foo/.external-repositories/some-repo/bar.baz.ar");
60+
DecompressorDescriptor.builder().setArchivePath(path).build();
61+
path = fs.getPath("/foo/.external-repositories/some-repo/bar.baz.deb");
62+
DecompressorDescriptor.builder().setArchivePath(path).build();
5963
}
6064

6165
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
!<arch>
2+
// 40 `
3+
archived_first.txt/
4+
archived_second.md/
5+
/0 0 0 0 644 20 `
6+
this is test file 1
7+
/20 0 0 0 644 29 `
8+
this is the second test file
9+

tools/build_defs/repo/http.bzl

+4-3
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ match a directory in the archive, Bazel will return an error.""",
269269
By default, the archive type is determined from the file extension of the
270270
URL. If the file has no extension, you can explicitly specify one of the
271271
following: `"zip"`, `"jar"`, `"war"`, `"aar"`, `"tar"`, `"tar.gz"`, `"tgz"`,
272-
`"tar.xz"`, or `tar.bz2`.""",
272+
`"tar.xz"`, `"txz"`, `"tar.zst"`, `"tzst"`, `tar.bz2`, `"ar"`, or `"deb"`.""",
273273
),
274274
"patches": attr.label_list(
275275
default = [],
@@ -357,8 +357,9 @@ http_archive = repository_rule(
357357
"""Downloads a Bazel repository as a compressed archive file, decompresses it,
358358
and makes its targets available for binding.
359359
360-
It supports the following file extensions: `"zip"`, `"jar"`, `"war"`, `"aar"`,
361-
`"tar"`, `"tar.gz"`, `"tgz"`, `"tar.xz"`, and `tar.bz2`.
360+
It supports the following file extensions: `"zip"`, `"jar"`, `"war"`, `"aar"`, `"tar"`,
361+
`"tar.gz"`, `"tgz"`, `"tar.xz"`, `"txz"`, `"tar.zst"`, `"tzst"`, `tar.bz2`, `"ar"`,
362+
or `"deb"`.
362363
363364
Examples:
364365
Suppose the current repository contains the source code for a chat program,

0 commit comments

Comments
 (0)