From 55ac365893f222e285aac3f0166c1539dadf91c9 Mon Sep 17 00:00:00 2001 From: Stephane Bouchet Date: Mon, 1 Jul 2024 17:47:18 +0200 Subject: [PATCH] add system property to override download path Signed-off-by: Stephane Bouchet --- .gitignore | 2 ++ .../intellij/common/CommonConstants.java | 4 +++ .../intellij/common/utils/DownloadHelper.java | 26 ++++++++++++++----- .../common/utils/DownloadHelperTest.java | 18 +++++++++++-- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 930e76d..3c82572 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ target build .gradle out +/cache/ +/.run/ diff --git a/src/main/java/com/redhat/devtools/intellij/common/CommonConstants.java b/src/main/java/com/redhat/devtools/intellij/common/CommonConstants.java index 9c5f009..60faa75 100644 --- a/src/main/java/com/redhat/devtools/intellij/common/CommonConstants.java +++ b/src/main/java/com/redhat/devtools/intellij/common/CommonConstants.java @@ -19,6 +19,7 @@ public class CommonConstants { public static final String HOME_FOLDER = System.getProperty("user.home"); + public static final String TOOLS_DOWNLOAD_PATH = System.getProperty("tools.dl.path"); public static final Key PROJECT = Key.create("com.redhat.devtools.intellij.common.project"); public static final Key LAST_MODIFICATION_STAMP = Key.create("com.redhat.devtools.intellij.common.last.modification.stamp"); public static final Key TARGET_NODE = Key.create("com.redhat.devtools.intellij.common.targetnode"); @@ -44,4 +45,7 @@ public class CommonConstants { "selfLink", "uid" ); + + private CommonConstants() {} + } diff --git a/src/main/java/com/redhat/devtools/intellij/common/utils/DownloadHelper.java b/src/main/java/com/redhat/devtools/intellij/common/utils/DownloadHelper.java index ad06a65..eaad926 100644 --- a/src/main/java/com/redhat/devtools/intellij/common/utils/DownloadHelper.java +++ b/src/main/java/com/redhat/devtools/intellij/common/utils/DownloadHelper.java @@ -16,9 +16,12 @@ import com.intellij.openapi.progress.Task; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.util.text.Strings; import com.intellij.util.io.HttpRequests; import com.redhat.devtools.intellij.common.CommonConstants; import com.twelvemonkeys.lang.Platform; +import org.apache.commons.codec.DecoderException; +import org.apache.commons.codec.binary.Hex; import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; @@ -51,6 +54,9 @@ import java.util.regex.Pattern; public class DownloadHelper { + + private static final int BUFFER_SIZE = 4096; + private static final UnaryOperator UNCOMPRESSOR = (input -> { try { return new CompressorStreamFactory().createCompressorInputStream(input); @@ -141,7 +147,8 @@ private CompletableFuture downloadIfRequiredAsyncInner(String tool String command = platform.getCmdFileName(); String version = getVersionFromPath(tool, platform); if (!areCompatible(version, tool.getVersionMatchRegExpr())) { - Path path = Paths.get(tool.getBaseDir().replace("$HOME", CommonConstants.HOME_FOLDER), "cache", tool.getVersion(), command); + String replacement = Strings.isEmpty(CommonConstants.TOOLS_DOWNLOAD_PATH) ? CommonConstants.HOME_FOLDER : CommonConstants.TOOLS_DOWNLOAD_PATH; + Path path = Paths.get(tool.getBaseDir().replace("$HOME", replacement), "cache", tool.getVersion(), command); final String cmd = path.toString(); if (!Files.exists(path)) { result = downloadInBackground(toolName, platform, path, cmd, tool, version, platform.getSha256()); @@ -264,7 +271,7 @@ private String getVersionFromPath(ToolsConfig.Tool tool, ToolsConfig.Platform pl } private static void downloadFile(InputStream input, Path dlFileName, ProgressIndicator progressIndicator, long size) throws IOException { - byte[] buffer = new byte[4096]; + byte[] buffer = new byte[BUFFER_SIZE]; Files.createDirectories(dlFileName.getParent()); try (OutputStream output = Files.newOutputStream(dlFileName)) { int lg; @@ -320,12 +327,17 @@ private void save(InputStream source, Path destination, long length) throws IOEx } private boolean verify(Path path, String checksum) throws IOException { - try { + try (InputStream stream = Files.newInputStream(path)) { MessageDigest digest = MessageDigest.getInstance("SHA-256"); - byte[] hash = digest.digest(Files.readAllBytes(path)); - return MessageDigest.isEqual(hash, checksum.getBytes(StandardCharsets.UTF_8)); - } catch (NoSuchAlgorithmException e) { - throw new IOException("Could not verify checksum for file " + path.toString(), e); + final byte[] buffer = new byte[BUFFER_SIZE]; + int read = stream.read(buffer, 0, BUFFER_SIZE); + while (read > -1) { + digest.update(buffer, 0, read); + read = stream.read(buffer, 0, BUFFER_SIZE); + } + return MessageDigest.isEqual(digest.digest(), Hex.decodeHex(checksum)); + } catch (NoSuchAlgorithmException | DecoderException e) { + throw new IOException("Could not verify checksum for file " + path, e); } } diff --git a/src/test/java/com/redhat/devtools/intellij/common/utils/DownloadHelperTest.java b/src/test/java/com/redhat/devtools/intellij/common/utils/DownloadHelperTest.java index 85fee6a..b933baa 100644 --- a/src/test/java/com/redhat/devtools/intellij/common/utils/DownloadHelperTest.java +++ b/src/test/java/com/redhat/devtools/intellij/common/utils/DownloadHelperTest.java @@ -13,6 +13,7 @@ import com.intellij.openapi.ui.TestDialog; import com.intellij.testFramework.LightPlatformTestCase; import org.apache.commons.io.FileUtils; +import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; @@ -24,14 +25,13 @@ public class DownloadHelperTest extends LightPlatformTestCase { public void setUp() throws Exception { super.setUp(); previous = MessagesHelper.setTestDialog(TestDialog.OK); - FileUtils.deleteDirectory(new File("cache")); } @Override protected void tearDown() throws Exception { + System.clearProperty("tools.dl.path"); MessagesHelper.setTestDialog(previous); super.tearDown(); - FileUtils.deleteDirectory(new File("cache")); } public void testThatGZIsDownloaded() throws IOException { @@ -40,6 +40,7 @@ public void testThatGZIsDownloaded() throws IOException { assertNotNull(toolInstance.getCommand()); assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", toolInstance.getCommand()); assertEquals(17, new File(toolInstance.getCommand()).length()); + FileUtils.deleteDirectory(Paths.get(toolInstance.getCommand()).toFile().getParentFile()); } public void testThatTarGZIsDownloaded() throws IOException { @@ -48,6 +49,7 @@ public void testThatTarGZIsDownloaded() throws IOException { assertNotNull(toolInstance.getCommand()); assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", toolInstance.getCommand()); assertEquals(17, new File(toolInstance.getCommand()).length()); + FileUtils.deleteDirectory(Paths.get(toolInstance.getCommand()).toFile().getParentFile()); } public void testThatPlainFileDownloaded() throws IOException { @@ -56,6 +58,7 @@ public void testThatPlainFileDownloaded() throws IOException { assertNotNull(toolInstance.getCommand()); assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", toolInstance.getCommand()); assertEquals(17, new File(toolInstance.getCommand()).length()); + FileUtils.deleteDirectory(Paths.get(toolInstance.getCommand()).toFile().getParentFile()); } public void testThatChecksumIsValidForDownloadedTool() throws IOException { @@ -74,4 +77,15 @@ public void testThatChecksumIsInValidForDownloadedTool() { assertTrue(e.getMessage().contains("Error while setting tool")); } } + + public void testThatPlainFileDownloadedInUserSpecificFolder() throws IOException { + TemporaryFolder temp = new TemporaryFolder(); + temp.create(); + System.setProperty("tools.dl.path", temp.getRoot().getAbsolutePath()); + DownloadHelper.ToolInstance toolInstance = DownloadHelper.getInstance().downloadIfRequired("tkn", DownloadHelperTest.class.getResource("/tkn-test.json")); + assertNotNull(toolInstance); + assertNotNull(toolInstance.getCommand()); + assertEquals(System.getProperty("tools.dl.path") + File.separatorChar + ".tekton" + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", toolInstance.getCommand()); + temp.delete(); + } }