From 8a54cbb4c74391e0e65e9f5e272da81b92d87a4e Mon Sep 17 00:00:00 2001 From: Moe Hamade <69801237+moehamade@users.noreply.github.com> Date: Fri, 24 Jul 2026 20:10:17 +0300 Subject: [PATCH] fix: Base the disk space check on remaining bytes when resuming The check ran before resume state was read and always demanded 1.5x the full APK size. Bytes already sitting in download_temp.apk have already consumed storage, so on a low-storage device an interrupted download could fail every resume with "Insufficient storage" even when only a small tail was left to fetch. Read the resume state first and check space for the remaining bytes only. A fresh download still checks the full size, and a complete temp file needs no extra space since promotion is a rename. Addresses the Codex review finding on UniversalApkManager. Co-Authored-By: Claude Fable 5 --- .../java/com/bitchat/android/util/UniversalApkManager.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/bitchat/android/util/UniversalApkManager.kt b/app/src/main/java/com/bitchat/android/util/UniversalApkManager.kt index 26f9fcce..c4aca052 100644 --- a/app/src/main/java/com/bitchat/android/util/UniversalApkManager.kt +++ b/app/src/main/java/com/bitchat/android/util/UniversalApkManager.kt @@ -219,9 +219,6 @@ class UniversalApkManager(private val context: Context) { Log.d(TAG, "Downloading from: $url") Log.d(TAG, "Expected size: ${expectedSize / 1024 / 1024}MB") - // Check available disk space before downloading - checkDiskSpace(expectedSize) - val tempFile = File(cacheDir, "download_temp.apk") // Check for resumable download @@ -241,6 +238,11 @@ class UniversalApkManager(private val context: Context) { } } + // Bytes already in the temp file have already consumed storage, so + // a resume only needs room for the remaining tail. Promotion is a + // rename and needs no extra space. + checkDiskSpace((expectedSize - existingBytes).coerceAtLeast(0)) + // A temp file that already holds the full asset means the process // died between download and verification. Requesting // "Range: bytes=-" for it would get HTTP 416 forever, so skip