From 03f1a14e813da55ae88d8064a9e011d0b37b4285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Resul=20=C3=96v=C3=BCr?= <92828257+ovurrsl@users.noreply.github.com> Date: Thu, 15 Oct 2026 05:01:29 +0300 Subject: [PATCH] fix: an interrupted Linux runtime download resumes instead of starting again (#847) * fix: an interrupted Linux runtime download resumes instead of starting again installOnce() asked the downloader to resume but always deleted the archive in its finally block, so the next attempt began at zero. The archive is now named after the release version and checksum, a failed download keeps what it fetched, and archives of other releases (and the old fixed name) are removed. A checksum or extraction failure still deletes the file. * test: Downloader resumes a cut-off download from the bytes it kept Starts a socket server that promises the whole file, hangs up part way and then answers a Range request, and checks that the second downloadFile call resumes with the right offset and ends with identical bytes. * fix: a resumed download discards a 206 that does not start where the kept bytes end Downloader appended every 206 response to the partial file without looking at Content-Range. A server or proxy that answers a Range request with a different offset would leave a corrupt file. Check that the range starts at the kept length; otherwise delete the partial file and start again without a Range header. Adds a test with a server that answers the Range request from offset 0. --------- Co-authored-by: ovurrsl --- .../droiddeck/launcher/core/Downloader.java | 13 ++ .../runtime/LinuxRuntimeInstaller.java | 27 ++++- .../launcher/core/DownloaderResumeTest.kt | 111 ++++++++++++++++++ .../runtime/LinuxRuntimeArchiveNameTest.kt | 36 ++++++ 4 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/runtime/LinuxRuntimeArchiveNameTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/core/Downloader.java b/app/src/main/java/com/droiddeck/launcher/core/Downloader.java index 1e63ef9820..81ddb9c927 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/Downloader.java +++ b/app/src/main/java/com/droiddeck/launcher/core/Downloader.java @@ -48,6 +48,12 @@ public final class Downloader { if (have > 0) connection.setRequestProperty("Range", "bytes=" + have + "-"); int code = connection.getResponseCode(); boolean appending = code == HttpURLConnection.HTTP_PARTIAL; + if (appending && !rangeStartsAt(connection.getHeaderField("Content-Range"), have)) { + // A 206 that does not begin where the kept bytes end would corrupt the file if appended. + connection.disconnect(); + destination.delete(); + return downloadFile(url, destination, false, progress); + } if (code / 100 != 2) { if (have > 0) { connection.disconnect(); @@ -87,6 +93,13 @@ public final class Downloader { } } + /** True when a `Content-Range: bytes START-END/TOTAL` header starts exactly at {@code start}. */ + static boolean rangeStartsAt(String contentRange, long start) { + if (contentRange == null) return false; + java.util.regex.Matcher m = java.util.regex.Pattern.compile("^\\s*bytes\\s+(\\d+)-\\d+/(?:\\d+|\\*)\\s*$").matcher(contentRange); + return m.matches() && Long.parseLong(m.group(1)) == start; + } + private static HttpURLConnection open(String url) throws java.io.IOException { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(TIMEOUT_MS); diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java index b2d0571cf7..72646ba195 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java +++ b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java @@ -230,9 +230,31 @@ public final class LinuxRuntimeInstaller { } } + /** + * The download is named after the release, so a partial file is only ever resumed for the same + * version and checksum, never for a different runtime that happens to share a fixed name. + */ + static String archiveName(Release release) { + String version = release.version.replaceAll("[^A-Za-z0-9._-]", "_"); + String sum = release.sha256.replaceAll("[^A-Za-z0-9]", ""); + if (sum.length() > 12) sum = sum.substring(0, 12); + return "linuxfs-" + version + "-" + sum + ".tar.zst"; + } + + /** Removes archives left by other releases and by builds that used the fixed name. */ + private static void deleteOtherArchives(File dir, String keep) { + File[] files = dir.listFiles((d, name) -> name.startsWith("linuxfs") && name.endsWith(".tar.zst") && !name.equals(keep)); + if (files == null) return; + for (File file : files) file.delete(); + } + private static boolean installOnce(Context context, Release release, ProgressListener listener) { - File archive = new File(context.getCacheDir(), "linuxfs.tar.zst"); + File archive = new File(context.getCacheDir(), archiveName(release)); + // A failed download keeps what it fetched so the next attempt resumes it; anything that + // reached verification and failed is not trusted and is removed. + boolean keepPartial = false; try { + deleteOtherArchives(context.getCacheDir(), archive.getName()); File pendingRemoval = removalDirectory(LinuxRuntime.rootDir(context)); if (pendingRemoval.exists() && listener != null) listener.onProgress(context.getString(R.string.rtinst_removing_leftovers), -1); if (!discard(pendingRemoval, null)) throw new IOException("Cannot clear the previous runtime: " + pendingRemoval); @@ -247,6 +269,7 @@ public final class LinuxRuntimeInstaller { }); if (!ok) { Log.w(TAG, "download failed"); + keepPartial = true; return false; } @@ -300,7 +323,7 @@ public final class LinuxRuntimeInstaller { Log.e(TAG, "install", e); return false; } finally { - archive.delete(); + if (!keepPartial) archive.delete(); } } diff --git a/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt b/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt new file mode 100644 index 0000000000..4a9b62177e --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/core/DownloaderResumeTest.kt @@ -0,0 +1,111 @@ +package com.droiddeck.launcher.core + +import java.io.File +import java.net.ServerSocket +import java.net.Socket +import java.nio.file.Files +import java.util.concurrent.CopyOnWriteArrayList +import org.junit.After +import org.junit.Assert.assertArrayEquals +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test + +/** + * A download that was cut off keeps its bytes, and a second call with `resume` asks for the rest + * with a Range header instead of starting again. The server is a plain socket so the first answer + * can promise the whole file and then hang up halfway. + */ +class DownloaderResumeTest { + private val data = ByteArray(300_000) { (it % 251).toByte() } + private val cutAt = 100_000 + private val requests = CopyOnWriteArrayList() + private lateinit var server: ServerSocket + private lateinit var dir: File + private lateinit var thread: Thread + @Volatile private var misbehave = false + + @Before fun setUp() { + dir = Files.createTempDirectory("downloader-resume").toFile() + server = ServerSocket(0) + thread = Thread { + try { + while (!server.isClosed) { + val socket = server.accept() + // the client may hang up on a body it refuses; that must not stop the server + runCatching { handle(socket) } + } + } catch (_: Exception) { + // closed by tearDown + } + }.apply { isDaemon = true; start() } + } + + @After fun tearDown() { + server.close() + dir.deleteRecursively() + } + + private fun handle(socket: Socket) = socket.use { + val input = it.getInputStream().bufferedReader(Charsets.ISO_8859_1) + var range: String? = null + while (true) { + val line = input.readLine() ?: break + if (line.isEmpty()) break + if (line.startsWith("Range:", ignoreCase = true)) range = line.substringAfter(':').trim() + } + requests.add(range) + val out = it.getOutputStream() + val from = range?.removePrefix("bytes=")?.substringBefore('-')?.toIntOrNull() + if (misbehave) { + // answers a Range request with a 206 that starts at 0, and serves plain requests whole + val start = if (from == null) "200 OK" else "206 Partial Content" + val extra = if (from == null) "" else "Content-Range: bytes 0-${data.size - 1}/${data.size}\r\n" + out.write("HTTP/1.1 $start\r\n${extra}Content-Length: ${data.size}\r\nConnection: close\r\n\r\n".toByteArray()) + out.write(data) + out.flush() + } else if (from == null) { + out.write("HTTP/1.1 200 OK\r\nContent-Length: ${data.size}\r\nConnection: close\r\n\r\n".toByteArray()) + out.write(data, 0, cutAt) + out.flush() + // hang up with most of the promised body still unsent + } else { + val rest = data.size - from + out.write(("HTTP/1.1 206 Partial Content\r\nContent-Range: bytes $from-${data.size - 1}/${data.size}\r\n" + + "Content-Length: $rest\r\nConnection: close\r\n\r\n").toByteArray()) + out.write(data, from, rest) + out.flush() + } + } + + @Test fun cutOffDownloadResumesFromWhatWasKept() { + val url = "http://127.0.0.1:${server.localPort}/runtime.tar.zst" + val target = File(dir, "runtime.tar.zst") + + assertFalse("the cut-off transfer must not report success", Downloader.downloadFile(url, target, true, null)) + val kept = target.length() + assertTrue("the partial bytes stay on disk (kept=$kept)", kept in 1 until data.size) + + assertTrue(Downloader.downloadFile(url, target, true, null)) + assertEquals(data.size.toLong(), target.length()) + assertArrayEquals(data, target.readBytes()) + assertEquals(listOf(null, "bytes=$kept-"), requests.toList()) + } + + @Test fun answerFromTheWrongOffsetIsNotAppended() { + val url = "http://127.0.0.1:${server.localPort}/runtime.tar.zst" + val target = File(dir, "runtime.tar.zst") + + assertFalse(Downloader.downloadFile(url, target, true, null)) + val kept = target.length() + assertTrue(kept in 1 until data.size) + + misbehave = true + assertTrue(Downloader.downloadFile(url, target, true, null)) + assertEquals(data.size.toLong(), target.length()) + assertArrayEquals("the file must not be the kept bytes plus a misplaced body", data, target.readBytes()) + assertEquals(listOf(null, "bytes=$kept-", null), requests.toList()) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/LinuxRuntimeArchiveNameTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/LinuxRuntimeArchiveNameTest.kt new file mode 100644 index 0000000000..9d6ad49b0a --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/LinuxRuntimeArchiveNameTest.kt @@ -0,0 +1,36 @@ +package com.droiddeck.launcher.runtime + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotEquals +import org.junit.Assert.assertTrue +import org.junit.Test + +class LinuxRuntimeArchiveNameTest { + private fun release(version: String, sha: String) = + LinuxRuntimeInstaller.Release(version, "https://example.invalid/runtime.tar.zst", sha, 1L) + + @Test fun sameReleaseKeepsTheSameName() { + val a = LinuxRuntimeInstaller.archiveName(release("r9", "ab".repeat(32))) + val b = LinuxRuntimeInstaller.archiveName(release("r9", "ab".repeat(32))) + assertEquals(a, b) + } + + @Test fun anotherVersionOrChecksumGetsAnotherName() { + val base = LinuxRuntimeInstaller.archiveName(release("r9", "ab".repeat(32))) + assertNotEquals(base, LinuxRuntimeInstaller.archiveName(release("r10", "ab".repeat(32)))) + assertNotEquals(base, LinuxRuntimeInstaller.archiveName(release("r9", "cd".repeat(32)))) + } + + @Test fun nameIsASafeFileNameThatTheCleanupRecognises() { + val name = LinuxRuntimeInstaller.archiveName(release("r9/../x y", "AB/CD ".repeat(10))) + assertFalse(name.contains('/')) + assertFalse(name.contains(' ')) + assertTrue(name.startsWith("linuxfs")) + assertTrue(name.endsWith(".tar.zst")) + } + + @Test fun shortOrEmptyChecksumStillMakesAName() { + assertEquals("linuxfs-r9-.tar.zst", LinuxRuntimeInstaller.archiveName(release("r9", ""))) + } +}