Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repository_ctx: Allow renaming archive entries during extraction. #16052

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
review feedback
jmillikin committed Aug 23, 2022
commit 853fe1cd411af59dcafa719f0f264f036871fb83
Original file line number Diff line number Diff line change
@@ -117,18 +117,17 @@ public static WorkspaceRuleEvent newExtractEvent(
String ruleLabel,
Location location) {

WorkspaceLogProtos.ExtractEvent.Builder e =
ExtractEvent e =
WorkspaceLogProtos.ExtractEvent.newBuilder()
.setArchive(archive)
.setOutput(output)
.setStripPrefix(stripPrefix);
if (renameFiles != null) {
e = e.putAllRenameFiles(renameFiles);
}
.setStripPrefix(stripPrefix)
.putAllRenameFiles(renameFiles)
.build();

WorkspaceLogProtos.WorkspaceEvent.Builder result =
WorkspaceLogProtos.WorkspaceEvent.newBuilder();
result = result.setExtractEvent(e.build());
result = result.setExtractEvent(e);
if (location != null) {
result = result.setLocation(location.toString());
}
@@ -155,13 +154,11 @@ public static WorkspaceRuleEvent newDownloadAndExtractEvent(
.setSha256(sha256)
.setIntegrity(integrity)
.setType(type)
.setStripPrefix(stripPrefix);
.setStripPrefix(stripPrefix)
.putAllRenameFiles(renameFiles);
for (URL u : urls) {
e.addUrl(u.toString());
}
if (renameFiles != null) {
e = e.putAllRenameFiles(renameFiles);
}

WorkspaceLogProtos.WorkspaceEvent.Builder result =
WorkspaceLogProtos.WorkspaceEvent.newBuilder();
Original file line number Diff line number Diff line change
@@ -57,9 +57,7 @@ public Path decompress(DecompressorDescriptor descriptor)
ArArchiveEntry entry;
while ((entry = arStream.getNextArEntry()) != null) {
String entryName = entry.getName();
if (renameFiles != null) {
entryName = renameFiles.getOrDefault(entryName, entryName);
}
entryName = renameFiles.getOrDefault(entryName, entryName);
Path filePath = descriptor.repositoryPath().getRelative(entryName);
filePath.getParentDirectory().createDirectoryAndParents();
if (entry.isDirectory()) {
Original file line number Diff line number Diff line change
@@ -58,9 +58,7 @@ public Path decompress(DecompressorDescriptor descriptor)
TarArchiveEntry entry;
while ((entry = tarStream.getNextTarEntry()) != null) {
String entryName = entry.getName();
if (renameFiles != null) {
entryName = renameFiles.getOrDefault(entryName, entryName);
}
entryName = renameFiles.getOrDefault(entryName, entryName);
StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entryName, prefix);
foundPrefix = foundPrefix || entryPath.foundPrefix();

Original file line number Diff line number Diff line change
@@ -15,10 +15,12 @@
package com.google.devtools.build.lib.bazel.repository;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.bazel.repository.DecompressorValue.Decompressor;
import com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException;
import com.google.devtools.build.lib.vfs.Path;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;
@@ -40,7 +42,7 @@ public class DecompressorDescriptor {
private DecompressorDescriptor(
String targetKind, String targetName, Path archivePath, Path repositoryPath,
@Nullable String prefix, boolean executable,
@Nullable Map<String, String> renameFiles,
Map<String, String> renameFiles,
Decompressor decompressor) {
this.targetKind = targetKind;
this.targetName = targetName;
@@ -135,8 +137,12 @@ public DecompressorDescriptor build() throws RepositoryFunctionException {
if (decompressor == null) {
decompressor = DecompressorValue.getDecompressor(archivePath);
}
if (renameFiles == null) {
renameFiles = Collections.emptyMap();
}
return new DecompressorDescriptor(
targetKind, targetName, archivePath, repositoryPath, prefix, executable, renameFiles, decompressor);
targetKind, targetName, archivePath, repositoryPath, prefix, executable,
renameFiles, decompressor);
}

@CanIgnoreReturnValue
@@ -177,7 +183,7 @@ public Builder setExecutable(boolean executable) {

@CanIgnoreReturnValue
public Builder setRenameFiles(Map<String, String> renameFiles) {
this.renameFiles = renameFiles;
this.renameFiles = ImmutableMap.copyOf(renameFiles);
return this;
}

Original file line number Diff line number Diff line change
@@ -88,9 +88,7 @@ public Path decompress(DecompressorDescriptor descriptor)
Collection<ZipFileEntry> entries = reader.entries();
for (ZipFileEntry entry : entries) {
String entryName = entry.getName();
if (renameFiles != null) {
entryName = renameFiles.getOrDefault(entryName, entryName);
}
entryName = renameFiles.getOrDefault(entryName, entryName);
StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entryName, prefix);
foundPrefix = foundPrefix || entryPath.foundPrefix();
if (entryPath.skip()) {
Original file line number Diff line number Diff line change
@@ -757,7 +757,9 @@ public StructImpl download(
doc =
"An optional dict specifying files to rename during the extraction. Archive entries"
+ " with names exactly matching a key will be renamed to the value, prior to"
+ " any directory prefix adjustment."),
+ " any directory prefix adjustment. This can be used to extract archives that"
+ " contain non-Unicode filenames, or which have files that would extract to"
+ " the same path on case-insensitive filesystems."),
})
public void extract(
Object archive,
@@ -908,7 +910,9 @@ public void extract(
doc =
"An optional dict specifying files to rename during the extraction. Archive entries"
+ " with names exactly matching a key will be renamed to the value, prior to"
+ " any directory prefix adjustment."),
+ " any directory prefix adjustment. This can be used to extract archives that"
+ " contain non-Unicode filenames, or which have files that would extract to"
+ " the same path on case-insensitive filesystems."),
})
public StructImpl downloadAndExtract(
Object url,