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 <ovurrsl@users.noreply.github.com>
This commit is contained in:
Resul Övürandovurrsl authored and GitHub committed 2026-10-14 22:01:29 -04:00
1 parent 98be0838c4
commit 03f1a14e81
4 files changed
+185 -2

No files matched your search

@@ -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);
@@ -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();
}
}