Add verified truncate fallback for SD allocation

This commit is contained in:
xXJsonDeruloXx committed 2026-10-04 15:33:41 -04:00
1 parent 26cb9e2f03
commit df9734f0ca
3 files changed
+128 -33

No files matched your search

@@ -20,6 +20,7 @@ import android.os.IBinder
import android.os.Handler
import android.os.Looper
import android.os.PowerManager
import android.system.Os
import android.util.Log
import com.droiddeck.launcher.R
import com.droiddeck.launcher.SessionActivity
@@ -811,25 +812,37 @@ class SessionService : Service() {
} else {
Log.i(TAG, "game storage: internal only")
}
if (SessionState.mode == MODE_STEAM && SessionPrefs.storageDiagnosticsEnabled(this)) {
val removableLibrary = storageDiagnosticLibrary?.let {
runCatching { Environment.isExternalStorageRemovable(it) }.getOrDefault(false)
} ?: false
val truncateAllocationSupported = storageDiagnosticLibrary?.takeIf { removableLibrary }?.let {
StorageDiagnostics.truncateReservesSpace(it)
}
val preallocationDevice = storageDiagnosticLibrary?.takeIf { truncateAllocationSupported == true }?.let {
runCatching { Os.stat(it.absolutePath).st_dev }.getOrNull()
}
val diagnosticDevice = if (SessionState.mode == MODE_STEAM && SessionPrefs.storageDiagnosticsEnabled(this)) {
val target = storageDiagnosticLibrary ?: File(LinuxRuntime.rootDir(this), "root/.local/share/Steam")
val device = StorageDiagnostics.writeSnapshot(
sessionDir, target,
selectedLibrary = if (storageDiagnosticLibrary != null) "secondary" else "internal",
removable = storageDiagnosticLibrary?.let {
runCatching { Environment.isExternalStorageRemovable(it) }.getOrDefault(false)
} ?: false,
removable = removableLibrary,
truncateAllocates = truncateAllocationSupported,
)
if (device != null) {
// guest already has the session command at this point. Put these with the `env -i`
// assignments so they reach the Steam process instead of becoming script args.
val envAt = guest.indexOf(LinuxRuntime.SESSION_SCRIPT).takeIf { it >= 0 } ?: guest.size
guest.add(envAt, "BL_STORAGE_DIAGNOSTICS=1")
guest.add(envAt + 1, "BL_STORAGE_DEVICE=$device")
guest.add(envAt + 2, "BL_STORAGE_LOG=${File(sessionDir, "storage.log").absolutePath}")
} else {
Log.w(TAG, "storage diagnostics: selected library could not be identified; metadata snapshot only")
if (device == null) Log.w(TAG, "storage diagnostics: selected library could not be identified; metadata snapshot only")
device
} else null
if (SessionState.mode == MODE_STEAM && (preallocationDevice != null || diagnosticDevice != null)) {
// These go with the script's `env -i` assignments, before the session command.
val envAt = guest.indexOf(LinuxRuntime.SESSION_SCRIPT).takeIf { it >= 0 } ?: guest.size
val env = ArrayList<String>()
env.add("BL_STORAGE_DEVICE=${preallocationDevice ?: diagnosticDevice}")
if (preallocationDevice != null) env.add("BL_STORAGE_PREALLOCATE_FALLBACK=1")
if (diagnosticDevice != null) {
env.add("BL_STORAGE_DIAGNOSTICS=1")
env.add("BL_STORAGE_LOG=${File(sessionDir, "storage.log").absolutePath}")
}
guest.addAll(envAt, env)
}
// The user's own games folder (the Steam cog's "Added games"), bound at a fixed place so
// the shortcuts the app writes point somewhere whatever storage the folder is on.
@@ -4,6 +4,7 @@ import android.os.StatFs
import android.system.Os
import android.util.Log
import java.io.File
import java.io.RandomAccessFile
/** The opt-in snapshot written beside a session's other logs. */
object StorageDiagnostics {
@@ -18,6 +19,7 @@ object StorageDiagnostics {
library: File,
selectedLibrary: String,
removable: Boolean,
truncateAllocates: Boolean? = null,
): Long? {
val device = runCatching { Os.stat(library.absolutePath).st_dev }.getOrNull()
val text = buildString {
@@ -25,6 +27,7 @@ object StorageDiagnostics {
appendLine("selected_library=$selectedLibrary")
appendLine("volume_removable=$removable")
appendLine("filesystem=${filesystemType(library) ?: "unknown"}")
truncateAllocates?.let { appendLine("truncate_allocation_probe=${if (it) "passed" else "failed"}") }
runCatching { StatFs(library.path) }.onSuccess { fs ->
appendLine("block_size_bytes=${fs.blockSizeLong}")
appendLine("total_bytes=${fs.totalBytes}")
@@ -46,6 +49,24 @@ object StorageDiagnostics {
}
}
/** Only enable the removable-volume fallback when extending a real file reserves its blocks. */
fun truncateReservesSpace(library: File): Boolean {
val probe = runCatching { File.createTempFile(".droiddeck-alloc-", ".probe", library) }.getOrNull()
?: return false
var supported = false
try {
val size = 1024L * 1024L
RandomAccessFile(probe, "rw").use { it.setLength(size) }
val stat = Os.stat(probe.absolutePath)
supported = stat.st_blocks >= (size + 511L) / 512L
} catch (_: Exception) {
supported = false
} finally {
if (probe.exists() && !probe.delete()) supported = false
}
return supported
}
/** The longest matching mount entry determines the filesystem for nested bindable folders. */
private fun filesystemType(path: File): String? = runCatching {
val canonical = path.canonicalPath