tools: Download dependencies in a temporary file

This commit is contained in:
Ilya Matveev
2017-03-29 23:23:46 +07:00
committed by ilmat192
parent fd5f837299
commit 7aec0ae532
@@ -2,6 +2,8 @@ package org.jetbrains.kotlin.konan
import java.io.* import java.io.*
import java.net.URL import java.net.URL
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.util.Properties import java.util.Properties
import kotlin.concurrent.thread import kotlin.concurrent.thread
@@ -44,7 +46,6 @@ class DependencyDownloader(dependenciesRoot: File, val dependenciesUrl: String,
val depDir = File(dependenciesDirectory, dependency) val depDir = File(dependenciesDirectory, dependency)
val depName = depDir.name val depName = depDir.name
val cachedDependencies = DependencyFile(cacheDirectory, ".cached")
val extractedDependencies = DependencyFile(dependenciesDirectory, ".extracted") val extractedDependencies = DependencyFile(dependenciesDirectory, ".extracted")
if (extractedDependencies.contains(depName) && if (extractedDependencies.contains(depName) &&
depDir.exists() && depDir.exists() &&
@@ -59,9 +60,8 @@ class DependencyDownloader(dependenciesRoot: File, val dependenciesUrl: String,
} }
val archive = File(cacheDirectory.canonicalPath, "$depName.tar.gz") val archive = File(cacheDirectory.canonicalPath, "$depName.tar.gz")
if (!cachedDependencies.contains(depName) || !archive.exists()) { if (!archive.exists()) {
download(depName, archive) download(depName, archive)
cachedDependencies.addWithSave(depName)
} }
extract(archive, dependenciesDirectory) extract(archive, dependenciesDirectory)
extractedDependencies.addWithSave(depName) extractedDependencies.addWithSave(depName)
@@ -97,7 +97,7 @@ class DependencyDownloader(dependenciesRoot: File, val dependenciesUrl: String,
} }
private fun download(dependencyName: String, outputFile: File) { private fun download(dependencyName: String, outputFile: File) {
if (!outputFile.exists()) { val tmpFile = File("${outputFile.canonicalPath}.part")
val url = URL("$dependenciesUrl/$dependencyName.tar.gz") val url = URL("$dependenciesUrl/$dependencyName.tar.gz")
val connection = url.openConnection() val connection = url.openConnection()
val totalBytes = connection.contentLengthLong val totalBytes = connection.contentLengthLong
@@ -109,7 +109,7 @@ class DependencyDownloader(dependenciesRoot: File, val dependenciesUrl: String,
thread { thread {
try { try {
url.openStream().use { from -> url.openStream().use { from ->
outputFile.outputStream().use { to -> FileOutputStream(tmpFile, false).use { to ->
val buffer = ByteArray(DEFAULT_BUFFER_SIZE) val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var read = from.read(buffer) var read = from.read(buffer)
while (read != -1) { while (read != -1) {
@@ -132,9 +132,14 @@ class DependencyDownloader(dependenciesRoot: File, val dependenciesUrl: String,
} }
println("Done.") println("Done.")
if (downloadError != null) { if (downloadError != null) {
tmpFile.delete()
throw RuntimeException("Cannot download dependency: $url", downloadError) throw RuntimeException("Cannot download dependency: $url", downloadError)
} }
} Files.move(
tmpFile.toPath(),
outputFile.toPath(),
StandardCopyOption.REPLACE_EXISTING
)
} }
fun run() { fun run() {