From 7aec0ae532fc377e500a8b0437a5b337e6b8c146 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Wed, 29 Mar 2017 23:23:46 +0700 Subject: [PATCH] tools: Download dependencies in a temporary file --- .../src/main/kotlin/DependencyDownloader.kt | 73 ++++++++++--------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/tools/helpers/src/main/kotlin/DependencyDownloader.kt b/tools/helpers/src/main/kotlin/DependencyDownloader.kt index 77efd5b15b2..c1f83946563 100644 --- a/tools/helpers/src/main/kotlin/DependencyDownloader.kt +++ b/tools/helpers/src/main/kotlin/DependencyDownloader.kt @@ -2,6 +2,8 @@ package org.jetbrains.kotlin.konan import java.io.* import java.net.URL +import java.nio.file.Files +import java.nio.file.StandardCopyOption import java.util.Properties import kotlin.concurrent.thread @@ -44,7 +46,6 @@ class DependencyDownloader(dependenciesRoot: File, val dependenciesUrl: String, val depDir = File(dependenciesDirectory, dependency) val depName = depDir.name - val cachedDependencies = DependencyFile(cacheDirectory, ".cached") val extractedDependencies = DependencyFile(dependenciesDirectory, ".extracted") if (extractedDependencies.contains(depName) && depDir.exists() && @@ -59,9 +60,8 @@ class DependencyDownloader(dependenciesRoot: File, val dependenciesUrl: String, } val archive = File(cacheDirectory.canonicalPath, "$depName.tar.gz") - if (!cachedDependencies.contains(depName) || !archive.exists()) { + if (!archive.exists()) { download(depName, archive) - cachedDependencies.addWithSave(depName) } extract(archive, dependenciesDirectory) extractedDependencies.addWithSave(depName) @@ -97,44 +97,49 @@ class DependencyDownloader(dependenciesRoot: File, val dependenciesUrl: String, } private fun download(dependencyName: String, outputFile: File) { - if (!outputFile.exists()) { - val url = URL("$dependenciesUrl/$dependencyName.tar.gz") - val connection = url.openConnection() - val totalBytes = connection.contentLengthLong + val tmpFile = File("${outputFile.canonicalPath}.part") + val url = URL("$dependenciesUrl/$dependencyName.tar.gz") + val connection = url.openConnection() + val totalBytes = connection.contentLengthLong - var currentBytes = 0L - var done = false - var downloadError: Throwable? = null + var currentBytes = 0L + var done = false + var downloadError: Throwable? = null - thread { - try { - url.openStream().use { from -> - outputFile.outputStream().use { to -> - val buffer = ByteArray(DEFAULT_BUFFER_SIZE) - var read = from.read(buffer) - while (read != -1) { - to.write(buffer, 0, read) - currentBytes += read - read = from.read(buffer) - } + thread { + try { + url.openStream().use { from -> + FileOutputStream(tmpFile, false).use { to -> + val buffer = ByteArray(DEFAULT_BUFFER_SIZE) + var read = from.read(buffer) + while (read != -1) { + to.write(buffer, 0, read) + currentBytes += read + read = from.read(buffer) } } - } catch (e: Throwable) { - downloadError = e } - done = true - } - - // TODO: Improve console logging - while (!done) { - Thread.sleep(1000) // We can use condition variable here. - updateProgressMsg(url.toString(), currentBytes, totalBytes) - } - println("Done.") - if (downloadError != null) { - throw RuntimeException("Cannot download dependency: $url", downloadError) + } catch (e: Throwable) { + downloadError = e } + done = true } + + // TODO: Improve console logging + while (!done) { + Thread.sleep(1000) // We can use condition variable here. + updateProgressMsg(url.toString(), currentBytes, totalBytes) + } + println("Done.") + if (downloadError != null) { + tmpFile.delete() + throw RuntimeException("Cannot download dependency: $url", downloadError) + } + Files.move( + tmpFile.toPath(), + outputFile.toPath(), + StandardCopyOption.REPLACE_EXISTING + ) } fun run() {