Download to tmp directory in ctx.download_and_extract
Modify `ctx.download_and_extract` to download the archive file to a tmp directory inside the given output path then delete the directory after extraction. This solves the problem that happens when the archive contains an archive with the same name.
Fixes:
Closes
@@ -74,7 +74,9 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
import java.nio.file.InvalidPathException;
+import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
@@ -860,14 +862,21 @@ public class StarlarkRepositoryContext
rule.getLabel().toString(),
thread.getCallerLocation());
- // Download to outputDirectory and delete it after extraction
StarlarkPath outputPath = getPath("download_and_extract()", output);
checkInOutputDirectory("write", outputPath);
createDirectory(outputPath.getPath());
Path downloadedPath;
+ Path downloadDirectory;
try (SilentCloseable c =
Profiler.instance().profile("fetching: " + rule.getLabel().toString())) {
+
+ // Download to temp directory inside the outputDirectory and delete it after extraction
+ java.nio.file.Path tempDirectory =
+ Files.createTempDirectory(Paths.get(outputPath.toString()), "temp");
+ downloadDirectory =
+ outputDirectory.getFileSystem().getPath(tempDirectory.toFile().getAbsolutePath());
+
downloadedPath =
downloadManager.download(
urls,
@@ -875,7 +884,7 @@ public class StarlarkRepositoryContext
checksum,
canonicalId,
Optional.of(type),
- outputPath.getPath(),
+ downloadDirectory,
env.getListener(),
osObject.getEnvironmentVariables(),
getName());
@@ -914,13 +923,13 @@ public class StarlarkRepositoryContext
StructImpl downloadResult = calculateDownloadResult(checksum, downloadedPath);
try {
- if (downloadedPath.exists()) {
- downloadedPath.delete();
+ if (downloadDirectory.exists()) {
+ downloadDirectory.deleteTree();
}
} catch (IOException e) {
throw new RepositoryFunctionException(
new IOException(
- "Couldn't delete temporary file (" + downloadedPath.getPathString() + ")", e),
+ "Couldn't delete temporary directory (" + downloadDirectory.getPathString() + ")", e),
Transience.TRANSIENT);
}
return downloadResult;
