files: reject oversized archives and enforce the budget while extracting
- Commit
5aa8215d16ef2ecc77b73a7ec396b5a02cdb48ac- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-19 03:22 UTC (3 months ago)
- Parents
47a6fe2
Diff
files/src/main/java/com/vayunmathur/files/platform/ArchiveLimits.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/ArchiveLimits.kt b/files/src/main/java/com/vayunmathur/files/platform/ArchiveLimits.ktnew file mode 100644index 0000000000..bc73a0080d--- /dev/null+++ b/files/src/main/java/com/vayunmathur/files/platform/ArchiveLimits.kt@@ -0,0 +1,60 @@+package com.vayunmathur.files.platform++import java.io.File++/**+ * The limits [UnzipWorker] extracts within, kept separate from the worker so they can be tested+ * without WorkManager or a notification channel.+ *+ * Extraction is bounded by how much room the device actually has rather than by a fixed number of+ * megabytes: an archive that fits is always allowed, and one that would fill the disk never is. That+ * matters because the app holds MANAGE_EXTERNAL_STORAGE, so running out of space hurts the whole+ * device rather than just this app.+ */+object ArchiveLimits {++ /**+ * Space deliberately left unused. Android misbehaves badly on a full volume, so extraction stops+ * well before the last byte.+ */+ private const val FREE_SPACE_HEADROOM_BYTES = 512L * 1024 * 1024++ /** Enough for any real archive, low enough that a million-entry archive cannot stall the app. */+ const val MAX_ENTRIES = 100_000++ /** How many bytes an extraction into a volume with [freeBytes] available may write. */+ fun extractionBudget(freeBytes: Long): Long =+ (freeBytes - FREE_SPACE_HEADROOM_BYTES).coerceAtLeast(0)++ /**+ * Total size the archive says it will expand to, or null if any entry declines to say.+ *+ * Only good enough to turn away honestly oversized archives: the value comes from the archive+ * itself, so a crafted one can under-report and has to be caught while writing instead.+ */+ fun declaredUncompressedSize(sizes: Sequence<Long>): Long? {+ var total = 0L+ for (size in sizes) {+ if (size < 0) return null+ total += size+ }+ return total+ }++ /**+ * Where an entry named [entryName] belongs under [destDirCanonical], or null if it would land+ * outside it — a Zip Slip attempt.+ *+ * The separator matters: comparing paths by raw prefix lets an entry escape into a sibling whose+ * name merely starts the same way, so `../out-evil/x` would pass for a destination of `out`.+ */+ fun resolveEntry(destDirCanonical: File, entryName: String): File? {+ val resolved = File(destDirCanonical, entryName).canonicalFile+ val root = destDirCanonical.path+ val inside = resolved.path == root || resolved.path.startsWith(root + File.separator)+ return resolved.takeIf { inside }+ }+}++/** Thrown when an archive tries to write past its budget or entry ceiling. */+class ArchiveTooLargeException : Exception("Archive exceeds its extraction budget")
files/src/main/java/com/vayunmathur/files/platform/FilesViewModel.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/FilesViewModel.kt b/files/src/main/java/com/vayunmathur/files/platform/FilesViewModel.ktindex 98ab7030a7..75b09a5b97 100644--- a/files/src/main/java/com/vayunmathur/files/platform/FilesViewModel.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/FilesViewModel.kt@@ -1,970 +1,1002 @@-package com.vayunmathur.files.platform--import android.app.Application-import android.content.Context-import android.content.Intent-import android.net.Uri-import android.os.Environment-import android.os.FileObserver-import android.os.StatFs-import android.provider.MediaStore-import android.provider.OpenableColumns-import android.webkit.MimeTypeMap-import androidx.core.content.FileProvider-import androidx.core.content.edit-import androidx.lifecycle.AndroidViewModel-import androidx.lifecycle.viewModelScope-import androidx.work.OneTimeWorkRequestBuilder-import androidx.work.WorkManager-import androidx.work.workDataOf-import com.vayunmathur.files.R-import kotlinx.coroutines.Dispatchers-import kotlinx.coroutines.Job-import kotlinx.coroutines.awaitCancellation-import kotlinx.coroutines.flow.MutableSharedFlow-import kotlinx.coroutines.flow.MutableStateFlow-import kotlinx.coroutines.flow.SharedFlow-import kotlinx.coroutines.flow.StateFlow-import kotlinx.coroutines.flow.asSharedFlow-import kotlinx.coroutines.flow.asStateFlow-import kotlinx.coroutines.launch-import java.io.File-import java.util.concurrent.atomic.AtomicInteger-import java.util.zip.ZipFile--/**- * Migrated from okio FileSystem/Path/openZip to java.io.File + java.util.zip.ZipFile.- * Real FS uses File; zip browsing is virtual backed by ZipFile entries.- */-data class FileBrowserItem(- val name: String,- val isDirectory: Boolean,- val size: Long?,- val realFile: File?,- val zipInnerPath: String?,- val key: String,- val lastModified: Long = 0L,-)--class FilesViewModel(application: Application) : AndroidViewModel(application), FilesActions {-- private val prefs =- application.getSharedPreferences("files_prefs", Context.MODE_PRIVATE)-- private val _isFilesGranted = MutableStateFlow(Environment.isExternalStorageManager())- val isFilesGranted: StateFlow<Boolean> = _isFilesGranted.asStateFlow()-- fun refreshPermissions() {- val granted = Environment.isExternalStorageManager()- if (_isFilesGranted.value != granted) {- _isFilesGranted.value = granted- if (granted) {- loadDirectory()- loadHome()- }- }- }-- private val _hasPromptedNotifications =- MutableStateFlow(prefs.getBoolean("has_prompted_notifications", false))- val hasPromptedNotifications: StateFlow<Boolean> = _hasPromptedNotifications.asStateFlow()-- fun setNotificationsPrompted() {- prefs.edit { putBoolean("has_prompted_notifications", true) }- _hasPromptedNotifications.value = true- }-- // ---- Navigation ----- val rootDirectory: File = Environment.getExternalStorageDirectory()-- private val _currentDirectory = MutableStateFlow(rootDirectory)- val currentDirectory: StateFlow<File> = _currentDirectory.asStateFlow()-- private val _zipPath = MutableStateFlow<File?>(null)- val zipPath: StateFlow<File?> = _zipPath.asStateFlow()-- private val _zipInternalPath = MutableStateFlow("")- val zipInternalPath: StateFlow<String> = _zipInternalPath.asStateFlow()-- fun isZipMode(): Boolean = _zipPath.value != null-- // ---- Listing ----- private val _entries =- MutableStateFlow<Pair<List<FileBrowserItem>, List<FileBrowserItem>>>(emptyList<FileBrowserItem>() to emptyList())- val entries: StateFlow<Pair<List<FileBrowserItem>, List<FileBrowserItem>>> = _entries.asStateFlow()-- // ---- Selection (only valid in real FS mode) ----- private val _selectedPaths = MutableStateFlow<Set<FileBrowserItem>>(emptySet())- val selectedPaths: StateFlow<Set<FileBrowserItem>> = _selectedPaths.asStateFlow()-- override fun clearSelection() {- if (_selectedPaths.value.isNotEmpty()) _selectedPaths.value = emptySet()- }-- override fun addToSelection(item: FileBrowserItem) {- if (isZipMode()) return- _selectedPaths.value = _selectedPaths.value + item- }-- override fun toggleSelection(item: FileBrowserItem) {- if (isZipMode()) return- val current = _selectedPaths.value- _selectedPaths.value = if (current.any { it.key == item.key }) {- current.filterNot { it.key == item.key }.toSet()- } else {- current + item- }- }-- override fun selectAll() {- if (isZipMode()) return- val (dirs, files) = _entries.value- _selectedPaths.value = (dirs + files).toSet()- }-- // ---- Sort, view mode, search ----- private val _sortBy = MutableStateFlow(- runCatching { SortBy.valueOf(prefs.getString("sort_by", null) ?: "NAME") }- .getOrDefault(SortBy.NAME)- )- val sortBy: StateFlow<SortBy> = _sortBy.asStateFlow()-- private val _sortAscending = MutableStateFlow(prefs.getBoolean("sort_ascending", true))- val sortAscending: StateFlow<Boolean> = _sortAscending.asStateFlow()-- private val _viewMode = MutableStateFlow(- runCatching { ViewMode.valueOf(prefs.getString("view_mode", null) ?: "LIST") }- .getOrDefault(ViewMode.LIST)- )- val viewMode: StateFlow<ViewMode> = _viewMode.asStateFlow()-- private val _searchQuery = MutableStateFlow("")- val searchQuery: StateFlow<String> = _searchQuery.asStateFlow()-- private val _isSearchActive = MutableStateFlow(false)- val isSearchActive: StateFlow<Boolean> = _isSearchActive.asStateFlow()-- override fun setSortBy(sortBy: SortBy) {- if (_sortBy.value == sortBy) return- _sortBy.value = sortBy- prefs.edit { putString("sort_by", sortBy.name) }- loadDirectory()- }-- override fun toggleSortDirection() {- setSortAscending(!_sortAscending.value)- }-- override fun setSortAscending(ascending: Boolean) {- if (_sortAscending.value == ascending) return- _sortAscending.value = ascending- prefs.edit { putBoolean("sort_ascending", ascending) }- loadDirectory()- }-- private val downloadsDir: File get() = File(rootDirectory, "Download")-- /**- * Apply the sensible default sort when *entering* a folder: the Downloads folder shows- * newest-first (by date), which is what people usually want there; every other folder- * uses the persisted global sort. This only sets the in-memory sort, so an explicit- * choice via the menu (which persists) still wins while you stay in the folder.- */- private fun applyDirDefaults(path: File) {- if (path.absolutePath == downloadsDir.absolutePath) {- _sortBy.value = SortBy.DATE- _sortAscending.value = false- } else {- _sortBy.value = runCatching {- SortBy.valueOf(prefs.getString("sort_by", null) ?: "NAME")- }.getOrDefault(SortBy.NAME)- _sortAscending.value = prefs.getBoolean("sort_ascending", true)- }- }-- override fun toggleViewMode() {- val next = if (_viewMode.value == ViewMode.LIST) ViewMode.GRID else ViewMode.LIST- _viewMode.value = next- prefs.edit { putString("view_mode", next.name) }- }-- override fun setSearchActive(active: Boolean) {- _isSearchActive.value = active- if (!active) _searchQuery.value = ""- }-- override fun setSearchQuery(query: String) {- _searchQuery.value = query- }-- // ---- Hidden files ----- private val _showHidden = MutableStateFlow(prefs.getBoolean("show_hidden", false))- val showHidden: StateFlow<Boolean> = _showHidden.asStateFlow()-- override fun toggleHidden() {- val next = !_showHidden.value- _showHidden.value = next- prefs.edit { putBoolean("show_hidden", next) }- loadDirectory()- }-- // ---- Create ----- override fun createFolder(name: String) {- if (isZipMode() || name.isBlank()) return- viewModelScope.launch(Dispatchers.IO) {- try {- val dir = uniqueDestination(_currentDirectory.value, name.trim())- if (dir.mkdirs()) loadDirectory()- else emit(getApplication<Application>().getString(R.string.create_failed))- } catch (e: Exception) {- emitMoveFailed(e)- }- }- }-- override fun createFile(name: String) {- if (isZipMode() || name.isBlank()) return- viewModelScope.launch(Dispatchers.IO) {- try {- val file = uniqueDestination(_currentDirectory.value, name.trim())- if (file.createNewFile()) loadDirectory()- else emit(getApplication<Application>().getString(R.string.create_failed))- } catch (e: Exception) {- emitMoveFailed(e)- }- }- }-- override fun openWith(item: FileBrowserItem) {- val ctx = getApplication<Application>()- val file = item.realFile ?: return- val uri = try {- FileProvider.getUriForFile(ctx, "${ctx.packageName}.fileprovider", file)- } catch (e: Exception) {- emitMoveFailed(e)- return- }- val view = Intent(Intent.ACTION_VIEW).apply {- setDataAndType(uri, mimeFor(file))- addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK)- }- val chooser = Intent.createChooser(view, null).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)- viewModelScope.launch { _intents.emit(chooser) }- }-- // ---- Home screen: storage, recents, bookmarks, categories ----- private val _atHome = MutableStateFlow(true)- val atHome: StateFlow<Boolean> = _atHome.asStateFlow()-- private val _categoryTitle = MutableStateFlow<String?>(null)- val categoryTitle: StateFlow<String?> = _categoryTitle.asStateFlow()-- private val _storage = MutableStateFlow<StorageInfo?>(null)- val storage: StateFlow<StorageInfo?> = _storage.asStateFlow()-- private val _recents = MutableStateFlow<List<FileBrowserItem>>(emptyList())- val recents: StateFlow<List<FileBrowserItem>> = _recents.asStateFlow()-- private val _bookmarks = MutableStateFlow(loadBookmarks())- val bookmarks: StateFlow<List<FileBrowserItem>> = _bookmarks.asStateFlow()-- fun loadHome() {- viewModelScope.launch(Dispatchers.IO) {- _storage.value = readStorage()- _bookmarks.value = loadBookmarks()- _recents.value = queryRecents()- }- }-- override fun goHome() {- _atHome.value = true- _categoryTitle.value = null- _zipPath.value = null- _zipInternalPath.value = ""- clearSelection()- observerJob?.cancel()- loadHome()- }-- override fun openInternalStorage() {- _categoryTitle.value = null- navigateTo(rootDirectory)- }-- override fun openBookmark(path: File) {- _categoryTitle.value = null- navigateTo(path)- }-- override fun openCategory(category: FileCategory) {- if (category == FileCategory.DOWNLOADS) {- val dl = File(rootDirectory, "Download")- openBookmark(if (dl.isDirectory) dl else rootDirectory)- return- }- _atHome.value = false- _zipPath.value = null- _categoryTitle.value = getApplication<Application>().getString(categoryLabel(category))- clearSelection()- observerJob?.cancel()- viewModelScope.launch(Dispatchers.IO) {- _entries.value = emptyList<FileBrowserItem>() to sortItems(queryCategory(category))- }- }-- override fun addBookmark(item: FileBrowserItem) {- val path = item.realFile ?: return- val set = (prefs.getStringSet("bookmarks", emptySet()) ?: emptySet()).toMutableSet()- set.add(path.absolutePath)- prefs.edit { putStringSet("bookmarks", set) }- _bookmarks.value = loadBookmarks()- emit(getApplication<Application>().getString(R.string.bookmark_added))- }-- override fun removeBookmark(path: File) {- val set = (prefs.getStringSet("bookmarks", emptySet()) ?: emptySet()).toMutableSet()- set.remove(path.absolutePath)- prefs.edit { putStringSet("bookmarks", set) }- _bookmarks.value = loadBookmarks()- }-- private fun loadBookmarks(): List<FileBrowserItem> {- val saved = prefs.getStringSet("bookmarks", emptySet()) ?: emptySet()- return saved.map { File(it) }- .filter { it.exists() }- .sortedBy { it.name.lowercase() }- .map { it.toItem() }- }-- private fun readStorage(): StorageInfo = try {- val stat = StatFs(rootDirectory.absolutePath)- StorageInfo(totalBytes = stat.totalBytes, freeBytes = stat.availableBytes)- } catch (_: Exception) {- StorageInfo(0, 0)- }-- private fun categoryLabel(c: FileCategory): Int = when (c) {- FileCategory.IMAGES -> R.string.cat_images- FileCategory.VIDEOS -> R.string.cat_videos- FileCategory.AUDIO -> R.string.cat_audio- FileCategory.DOCUMENTS -> R.string.cat_documents- FileCategory.DOWNLOADS -> R.string.cat_downloads- }-- private fun queryCategory(category: FileCategory): List<FileBrowserItem> = when (category) {- FileCategory.IMAGES -> queryMedia(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, 2000)- FileCategory.VIDEOS -> queryMedia(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, 2000)- FileCategory.AUDIO -> queryMedia(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, 2000)- FileCategory.DOCUMENTS -> {- val mimes = arrayOf(- "application/pdf", "application/msword",- "application/vnd.openxmlformats-officedocument.wordprocessingml.document",- "application/vnd.ms-excel",- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",- "application/vnd.ms-powerpoint",- "application/vnd.openxmlformats-officedocument.presentationml.presentation",- "text/plain", "text/markdown", "text/csv", "application/rtf",- )- val selection = mimes.joinToString(" OR ") { "${MediaStore.Files.FileColumns.MIME_TYPE}=?" }- queryMedia(MediaStore.Files.getContentUri("external"), selection, mimes, 2000)- }- FileCategory.DOWNLOADS -> emptyList()- }-- private fun queryRecents(): List<FileBrowserItem> =- queryMedia(MediaStore.Files.getContentUri("external"), null, null, 40)-- private fun queryMedia(- uri: Uri,- selection: String?,- args: Array<String>?,- limit: Int,- ): List<FileBrowserItem> {- val ctx = getApplication<Application>()- val projection = arrayOf(MediaStore.MediaColumns.DATA)- val sort = "${MediaStore.MediaColumns.DATE_MODIFIED} DESC"- val out = mutableListOf<FileBrowserItem>()- try {- ctx.contentResolver.query(uri, projection, selection, args, sort)?.use { c ->- val idx = c.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)- while (c.moveToNext() && out.size < limit) {- val path = c.getString(idx) ?: continue- val f = File(path)- if (f.isFile) out.add(f.toItem())- }- }- } catch (_: Exception) {- }- return out- }-- private fun File.toItem() = FileBrowserItem(- name = name,- isDirectory = isDirectory,- size = if (isFile) length() else null,- realFile = this,- zipInnerPath = null,- key = absolutePath,- lastModified = lastModified(),- )-- // ---- Clipboard (copy/cut/paste) ----- private val _clipboard = MutableStateFlow<List<File>>(emptyList())- val clipboard: StateFlow<List<File>> = _clipboard.asStateFlow()-- private val _clipboardIsCut = MutableStateFlow(false)- val clipboardIsCut: StateFlow<Boolean> = _clipboardIsCut.asStateFlow()-- override fun copySelection() {- if (isZipMode()) return- val files = _selectedPaths.value.mapNotNull { it.realFile }- if (files.isEmpty()) return- _clipboard.value = files- _clipboardIsCut.value = false- clearSelection()- emit(getApplication<Application>().getString(R.string.copied_n, files.size))- }-- override fun cutSelection() {- if (isZipMode()) return- val files = _selectedPaths.value.mapNotNull { it.realFile }- if (files.isEmpty()) return- _clipboard.value = files- _clipboardIsCut.value = true- clearSelection()- emit(getApplication<Application>().getString(R.string.cut_n, files.size))- }-- override fun clearClipboard() {- _clipboard.value = emptyList()- _clipboardIsCut.value = false- }-- override fun pasteHere() {- if (isZipMode()) return- val sources = _clipboard.value- if (sources.isEmpty()) return- val target = _currentDirectory.value- val isCut = _clipboardIsCut.value- viewModelScope.launch(Dispatchers.IO) {- var lastError: Exception? = null- sources.forEach { source ->- try {- if (!source.exists()) return@forEach- // Don't paste a folder into itself or a descendant.- if (target.absolutePath == source.absolutePath ||- target.absolutePath.startsWith(source.absolutePath + "/")- ) return@forEach- val dest = uniqueDestination(target, source.name)- if (isCut) {- source.atomicMoveTo(dest)- } else if (source.isDirectory) {- source.copyRecursively(dest, overwrite = false)- } else {- source.copyTo(dest, overwrite = false)- }- } catch (e: Exception) {- lastError = e- }- }- if (isCut) clearClipboard()- loadDirectory()- lastError?.let { emitMoveFailed(it) }- }- }-- override fun shareSelection() {- val ctx = getApplication<Application>()- val files = _selectedPaths.value.mapNotNull { it.realFile }.filter { it.isFile }- if (files.isEmpty()) return- val uris = try {- ArrayList(files.map {- FileProvider.getUriForFile(ctx, "${ctx.packageName}.fileprovider", it)- })- } catch (e: Exception) {- emitMoveFailed(e)- return- }- val intent = if (uris.size == 1) {- Intent(Intent.ACTION_SEND).apply {- type = mimeFor(files.first())- putExtra(Intent.EXTRA_STREAM, uris.first())- }- } else {- Intent(Intent.ACTION_SEND_MULTIPLE).apply {- type = "*/*"- putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)- }- }.apply { addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK) }- clearSelection()- viewModelScope.launch {- _intents.emit(Intent.createChooser(intent, null).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))- }- }-- private fun mimeFor(file: File): String =- MimeTypeMap.getSingleton().getMimeTypeFromExtension(file.extension) ?: "*/*"-- /** A destination in [dir] named [name], suffixed with " (n)" if that already exists. */- private fun uniqueDestination(dir: File, name: String): File {- var candidate = File(dir, name)- if (!candidate.exists()) return candidate- val dot = name.lastIndexOf('.')- val base = if (dot > 0) name.substring(0, dot) else name- val ext = if (dot > 0) name.substring(dot) else ""- var n = 1- while (candidate.exists()) {- candidate = File(dir, "$base ($n)$ext")- n++- }- return candidate- }-- // ---- Share URIs ----- private val _incomingUris = MutableStateFlow<List<Uri>?>(null)- val incomingUris: StateFlow<List<Uri>?> = _incomingUris.asStateFlow()-- fun setIncomingUris(uris: List<Uri>) { _incomingUris.value = uris }- fun clearIncomingUris() { _incomingUris.value = null }-- // ---- Events ----- private val _snackbarMessages = MutableSharedFlow<String>(extraBufferCapacity = 8)- val snackbarMessages: SharedFlow<String> = _snackbarMessages.asSharedFlow()-- private val _intents = MutableSharedFlow<Intent>(extraBufferCapacity = 4)- val intents: SharedFlow<Intent> = _intents.asSharedFlow()-- private var observerJob: Job? = null- private var loadJob: Job? = null- // Guards against a stale reload (e.g. a FileObserver event that snapshots the- // directory mid-deletion) overwriting a newer one. Only the latest load wins.- private val loadGeneration = AtomicInteger(0)-- init {- loadDirectory()- restartObserver()- loadHome()- }-- fun loadDirectory() {- val gen = loadGeneration.incrementAndGet()- loadJob?.cancel()- val zipFile = _zipPath.value- val dir = _currentDirectory.value- val inner = _zipInternalPath.value- loadJob = viewModelScope.launch(Dispatchers.IO) {- val listed = if (zipFile == null) listRealDir(dir) else listZipDir(zipFile, inner)- val sorted = sortEntries(listed)- if (gen == loadGeneration.get()) {- _entries.value = sorted- }- }- }-- private fun sortEntries(- entries: Pair<List<FileBrowserItem>, List<FileBrowserItem>>- ): Pair<List<FileBrowserItem>, List<FileBrowserItem>> =- sortItems(entries.first) to sortItems(entries.second)-- private fun sortItems(items: List<FileBrowserItem>): List<FileBrowserItem> {- val cmp: Comparator<FileBrowserItem> = when (_sortBy.value) {- SortBy.NAME -> compareBy { it.name.lowercase() }- SortBy.DATE -> compareBy { it.lastModified }- SortBy.SIZE -> compareBy { it.size ?: -1L }- SortBy.TYPE -> compareBy<FileBrowserItem> {- it.name.substringAfterLast('.', "").lowercase()- }.thenBy { it.name.lowercase() }- }- val sorted = items.sortedWith(cmp)- return if (_sortAscending.value) sorted else sorted.reversed()- }-- private fun restartObserver() {- observerJob?.cancel()- if (isZipMode()) {- observerJob = null- return- }- val dir = _currentDirectory.value- observerJob = viewModelScope.launch {- val observer = object : FileObserver(dir, CREATE or DELETE or MOVED_FROM or MOVED_TO) {- override fun onEvent(event: Int, path: String?) { loadDirectory() }- }- observer.startWatching()- try { awaitCancellation() } finally { observer.stopWatching() }- }- }-- override fun navigateTo(path: File) {- if (isZipMode()) {- _zipPath.value = null- _zipInternalPath.value = ""- }- _atHome.value = false- _categoryTitle.value = null- _currentDirectory.value = path- applyDirDefaults(path)- clearSelection()- loadDirectory()- restartObserver()- }-- override fun navigateIntoZipDir(dirName: String) {- val current = _zipInternalPath.value- val newPath = if (current.isEmpty()) dirName else "$current/$dirName"- _zipInternalPath.value = newPath- clearSelection()- loadDirectory()- }-- override fun navigateToZipInternalPath(fullInternalPath: String) {- _zipInternalPath.value = fullInternalPath- clearSelection()- loadDirectory()- }-- override fun navigateToZipParentRealFolder(target: File) {- // breadcrumb click on real-FS part while in zip mode → exit zip- _zipPath.value = null- _zipInternalPath.value = ""- _currentDirectory.value = target- clearSelection()- loadDirectory()- restartObserver()- }-- override fun handleBack(): Boolean {- if (_selectedPaths.value.isNotEmpty()) { clearSelection(); return true }- if (_categoryTitle.value != null) { goHome(); return true }- val z = _zipPath.value- when {- z != null -> {- val internal = _zipInternalPath.value- if (internal.isEmpty()) {- val parent = z.parentFile ?: rootDirectory- _zipPath.value = null- _currentDirectory.value = parent- restartObserver()- } else {- val parentInternal = if (internal.contains("/")) internal.substringBeforeLast("/") else ""- _zipInternalPath.value = parentInternal- }- loadDirectory()- }- _currentDirectory.value.absolutePath != rootDirectory.absolutePath -> {- _currentDirectory.value = _currentDirectory.value.parentFile ?: _currentDirectory.value- applyDirDefaults(_currentDirectory.value)- loadDirectory()- restartObserver()- }- // At the storage root, Back returns to the home screen.- else -> goHome()- }- return true- }-- override fun rename(item: FileBrowserItem, newName: String) {- if (isZipMode()) return- val path = item.realFile ?: return- viewModelScope.launch(Dispatchers.IO) {- try {- val target = File(path.parentFile, newName)- path.atomicMoveTo(target)- clearSelection()- loadDirectory()- } catch (e: Exception) { emitMoveFailed(e) }- }- }-- override fun deleteSelection() {- if (isZipMode()) return- val selection = _selectedPaths.value.mapNotNull { it.realFile }- viewModelScope.launch(Dispatchers.IO) {- selection.forEach { it.deleteRecursively() }- clearSelection()- loadDirectory()- }- }-- override fun moveInto(sources: List<File>, target: File) {- if (isZipMode()) return- if (!target.isDirectory) return- moveFiles(sources, target) { source -> source != target && !target.absolutePath.startsWith(source.absolutePath) }- }-- override fun moveToBreadcrumb(sources: List<File>, target: File) {- if (isZipMode()) return- moveFiles(sources, target) { source -> source.parentFile != target && source != target }- }-- private fun moveFiles(sources: List<File>, target: File, canMove: (File) -> Boolean) {- viewModelScope.launch(Dispatchers.IO) {- var movedAny = false- var lastError: Exception? = null- sources.forEach { source ->- if (canMove(source)) {- try {- val dest = File(target, source.name)- source.atomicMoveTo(dest)- movedAny = true- } catch (e: Exception) { lastError = e }- }- }- if (movedAny) { clearSelection(); loadDirectory() }- lastError?.let { emitMoveFailed(it) }- }- }-- override fun openZipFile(item: FileBrowserItem) {- val file = item.realFile ?: return- viewModelScope.launch(Dispatchers.IO) {- try {- ZipFile(file).use { _ -> }- _zipPath.value = file- _zipInternalPath.value = ""- _currentDirectory.value = File("/") // placeholder not used in zip mode- clearSelection()- loadDirectory()- restartObserver()- } catch (e: Exception) {- emit(getApplication<Application>().getString(R.string.could_not_open_zip, e.localizedMessage))- }- }- }-- override fun archive(archiveName: String) {- if (isZipMode()) return- val ctx = getApplication<Application>()- val sources = _selectedPaths.value.mapNotNull { it.realFile?.absolutePath }.toTypedArray()- val destFileName = if (archiveName.endsWith(".zip")) archiveName else "$archiveName.zip"- val destFile = File(_currentDirectory.value, destFileName)- if (sources.isEmpty()) return- val zipWork = OneTimeWorkRequestBuilder<ZipWorker>().setInputData(- workDataOf("source_paths" to sources, "dest_path" to destFile.absolutePath)- ).build()- WorkManager.getInstance(ctx).enqueue(zipWork)- clearSelection()- emit(ctx.getString(R.string.archiving_started))- }-- fun unzip(zipItem: FileBrowserItem, destPath: File) {- val zipFile = zipItem.realFile ?: return- val ctx = getApplication<Application>()- val unzipWork = OneTimeWorkRequestBuilder<UnzipWorker>().setInputData(- workDataOf("zip_path" to zipFile.absolutePath, "dest_path" to destPath.absolutePath)- ).build()- WorkManager.getInstance(ctx).enqueue(unzipWork)- clearSelection()- emit(ctx.getString(R.string.unzipping_started_to, destPath.name))- }-- override fun saveIncomingUris() {- if (isZipMode()) return- val ctx = getApplication<Application>()- val uris = _incomingUris.value ?: return- val target = _currentDirectory.value- viewModelScope.launch(Dispatchers.IO) {- var lastError: Exception? = null- uris.forEach { uri ->- try {- saveUriToPath(ctx, uri, target)- } catch (e: Exception) {- lastError = e- }- }- clearIncomingUris()- loadDirectory()- lastError?.let { emitMoveFailed(it) }- ?: viewModelScope.launch { _snackbarMessages.emit(ctx.getString(R.string.files_saved)) }- }- }-- override fun openFile(item: FileBrowserItem) {- val ctx = getApplication<Application>()- if (isZipMode()) {- emit(ctx.getString(R.string.zip_browse_only))- return- }- val file = item.realFile ?: return- val extension = file.extension- val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) ?: "*/*"- val uri = try {- FileProvider.getUriForFile(ctx, "${ctx.packageName}.fileprovider", file)- } catch (e: Exception) {- emitMoveFailed(e)- return- }- val intent = Intent(Intent.ACTION_VIEW).apply {- setDataAndType(uri, mimeType)- flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION- }- viewModelScope.launch { _intents.emit(intent) }- }-- // ---- APK install ----- /** The APK waiting to be installed once the user grants the "install unknown apps" permission. */- private var pendingApkInstall: File? = null-- private val _installPermissionRequests = MutableSharedFlow<Unit>(extraBufferCapacity = 1)- /** Emitted when an APK was tapped but Files lacks permission to install; UI opens settings. */- val installPermissionRequests: SharedFlow<Unit> = _installPermissionRequests.asSharedFlow()-- override fun installApk(item: FileBrowserItem) {- val ctx = getApplication<Application>()- if (isZipMode()) {- emit(ctx.getString(R.string.zip_browse_only))- return- }- val file = item.realFile ?: return- if (ctx.packageManager.canRequestPackageInstalls()) {- launchApkInstall(file)- } else {- pendingApkInstall = file- emit(ctx.getString(R.string.install_permission_needed))- _installPermissionRequests.tryEmit(Unit)- }- }-- /** Called by the UI after returning from the "install unknown apps" settings screen. */- fun onInstallPermissionResult() {- val ctx = getApplication<Application>()- val file = pendingApkInstall ?: return- pendingApkInstall = null- if (ctx.packageManager.canRequestPackageInstalls()) {- launchApkInstall(file)- } else {- emit(ctx.getString(R.string.install_permission_denied))- }- }-- private fun launchApkInstall(file: File) {- val ctx = getApplication<Application>()- val uri = try {- FileProvider.getUriForFile(ctx, "${ctx.packageName}.fileprovider", file)- } catch (e: Exception) {- emitMoveFailed(e)- return- }- val intent = Intent(Intent.ACTION_VIEW).apply {- setDataAndType(uri, "application/vnd.android.package-archive")- flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION- }- viewModelScope.launch { _intents.emit(intent) }- }-- fun showMessage(message: String) { emit(message) }- private fun emit(message: String) { viewModelScope.launch { _snackbarMessages.emit(message) } }- private fun emitMoveFailed(e: Exception) {- emit(getApplication<Application>().getString(R.string.move_failed, e.localizedMessage))- }-- private fun saveUriToPath(context: Context, uri: Uri, targetDir: File) {- val name = getFileName(context, uri) ?: "shared_file_${System.currentTimeMillis()}"- val targetFile = File(targetDir, name)- context.contentResolver.openInputStream(uri)?.use { input ->- targetFile.outputStream().use { out -> input.copyTo(out) }- }- }-- private fun getFileName(context: Context, uri: Uri): String? {- if (uri.scheme == "content") {- context.contentResolver.query(uri, null, null, null, null)?.use { cursor ->- if (cursor.moveToFirst()) {- val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)- if (nameIndex != -1) return cursor.getString(nameIndex)- }- }- }- return uri.path?.substringAfterLast('/')- }-- private fun listRealDir(dir: File): Pair<List<FileBrowserItem>, List<FileBrowserItem>> {- val all = dir.listFiles()?.toList() ?: emptyList()- val visible = if (_showHidden.value) all else all.filterNot { it.name.startsWith(".") }- val items = visible.map { it.toItem() }- return items.partition { it.isDirectory }- }-- private fun listZipDir(zipFile: File, internalDir: String): Pair<List<FileBrowserItem>, List<FileBrowserItem>> {- return try {- ZipFile(zipFile).use { zf ->- val prefix = if (internalDir.isEmpty()) "" else "$internalDir/"- val dirMap = mutableMapOf<String, FileBrowserItem>()- val fileList = mutableListOf<FileBrowserItem>()- for (entry in zf.entries()) {- val rawName = entry.name- val normalized = rawName.trimEnd('/')- if (normalized.isEmpty()) continue- if (normalized == internalDir) continue- if (internalDir.isNotEmpty() && !rawName.startsWith(prefix) && !normalized.startsWith(prefix)) continue- val remainder = if (prefix.isEmpty()) normalized else {- if (normalized.length <= prefix.length) continue- normalized.substring(prefix.length)- }- if (remainder.isEmpty()) continue- val slashIdx = remainder.indexOf('/')- if (slashIdx != -1) {- val first = remainder.substring(0, slashIdx)- if (first.isEmpty()) continue- if (!dirMap.containsKey(first)) {- val fullInner = if (internalDir.isEmpty()) first else "$internalDir/$first"- dirMap[first] = FileBrowserItem(- name = first,- isDirectory = true,- size = null,- realFile = null,- zipInnerPath = fullInner,- key = "zip:$fullInner"- )- }- } else {- if (entry.isDirectory) {- if (!dirMap.containsKey(remainder)) {- val fullInner = if (internalDir.isEmpty()) remainder else "$internalDir/$remainder"- dirMap[remainder] = FileBrowserItem(- name = remainder,- isDirectory = true,- size = null,- realFile = null,- zipInnerPath = fullInner,- key = "zip:$fullInner"- )- }- } else {- val fullInner = if (internalDir.isEmpty()) remainder else "$internalDir/$remainder"- fileList.add(- FileBrowserItem(- name = remainder,- isDirectory = false,- size = entry.size.takeIf { it >= 0 },- realFile = null,- zipInnerPath = fullInner,- key = "zip:$fullInner"- )- )- }- }- }- dirMap.values.toList() to fileList- }- } catch (_: Exception) {- emptyList<FileBrowserItem>() to emptyList()- }- }-- private fun File.atomicMoveTo(target: File) {- try {- java.nio.file.Files.move(this.toPath(), target.toPath(), java.nio.file.StandardCopyOption.ATOMIC_MOVE)- } catch (_: Exception) {- if (!this.renameTo(target)) {- if (this.isDirectory) {- this.copyRecursively(target, overwrite = true)- this.deleteRecursively()- } else {- this.copyTo(target, overwrite = true)- this.delete()- }- }- }- }-}+package com.vayunmathur.files.platform++import android.app.Application+import android.content.Context+import android.content.Intent+import android.net.Uri+import android.os.Environment+import android.os.FileObserver+import android.os.StatFs+import android.provider.MediaStore+import android.provider.OpenableColumns+import android.text.format.Formatter+import android.webkit.MimeTypeMap+import androidx.core.content.FileProvider+import androidx.core.content.edit+import androidx.lifecycle.AndroidViewModel+import androidx.lifecycle.viewModelScope+import androidx.work.OneTimeWorkRequestBuilder+import androidx.work.WorkManager+import androidx.work.workDataOf+import com.vayunmathur.files.R+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.Job+import kotlinx.coroutines.awaitCancellation+import kotlinx.coroutines.flow.MutableSharedFlow+import kotlinx.coroutines.flow.MutableStateFlow+import kotlinx.coroutines.flow.SharedFlow+import kotlinx.coroutines.flow.StateFlow+import kotlinx.coroutines.flow.asSharedFlow+import kotlinx.coroutines.flow.asStateFlow+import kotlinx.coroutines.launch+import java.io.File+import java.util.concurrent.atomic.AtomicInteger+import java.util.zip.ZipFile++/**+ * Migrated from okio FileSystem/Path/openZip to java.io.File + java.util.zip.ZipFile.+ * Real FS uses File; zip browsing is virtual backed by ZipFile entries.+ */+data class FileBrowserItem(+ val name: String,+ val isDirectory: Boolean,+ val size: Long?,+ val realFile: File?,+ val zipInnerPath: String?,+ val key: String,+ val lastModified: Long = 0L,+)++class FilesViewModel(application: Application) : AndroidViewModel(application), FilesActions {++ private val prefs =+ application.getSharedPreferences("files_prefs", Context.MODE_PRIVATE)++ private val _isFilesGranted = MutableStateFlow(Environment.isExternalStorageManager())+ val isFilesGranted: StateFlow<Boolean> = _isFilesGranted.asStateFlow()++ fun refreshPermissions() {+ val granted = Environment.isExternalStorageManager()+ if (_isFilesGranted.value != granted) {+ _isFilesGranted.value = granted+ if (granted) {+ loadDirectory()+ loadHome()+ }+ }+ }++ private val _hasPromptedNotifications =+ MutableStateFlow(prefs.getBoolean("has_prompted_notifications", false))+ val hasPromptedNotifications: StateFlow<Boolean> = _hasPromptedNotifications.asStateFlow()++ fun setNotificationsPrompted() {+ prefs.edit { putBoolean("has_prompted_notifications", true) }+ _hasPromptedNotifications.value = true+ }++ // ---- Navigation ----+ val rootDirectory: File = Environment.getExternalStorageDirectory()++ private val _currentDirectory = MutableStateFlow(rootDirectory)+ val currentDirectory: StateFlow<File> = _currentDirectory.asStateFlow()++ private val _zipPath = MutableStateFlow<File?>(null)+ val zipPath: StateFlow<File?> = _zipPath.asStateFlow()++ private val _zipInternalPath = MutableStateFlow("")+ val zipInternalPath: StateFlow<String> = _zipInternalPath.asStateFlow()++ fun isZipMode(): Boolean = _zipPath.value != null++ // ---- Listing ----+ private val _entries =+ MutableStateFlow<Pair<List<FileBrowserItem>, List<FileBrowserItem>>>(emptyList<FileBrowserItem>() to emptyList())+ val entries: StateFlow<Pair<List<FileBrowserItem>, List<FileBrowserItem>>> = _entries.asStateFlow()++ // ---- Selection (only valid in real FS mode) ----+ private val _selectedPaths = MutableStateFlow<Set<FileBrowserItem>>(emptySet())+ val selectedPaths: StateFlow<Set<FileBrowserItem>> = _selectedPaths.asStateFlow()++ override fun clearSelection() {+ if (_selectedPaths.value.isNotEmpty()) _selectedPaths.value = emptySet()+ }++ override fun addToSelection(item: FileBrowserItem) {+ if (isZipMode()) return+ _selectedPaths.value = _selectedPaths.value + item+ }++ override fun toggleSelection(item: FileBrowserItem) {+ if (isZipMode()) return+ val current = _selectedPaths.value+ _selectedPaths.value = if (current.any { it.key == item.key }) {+ current.filterNot { it.key == item.key }.toSet()+ } else {+ current + item+ }+ }++ override fun selectAll() {+ if (isZipMode()) return+ val (dirs, files) = _entries.value+ _selectedPaths.value = (dirs + files).toSet()+ }++ // ---- Sort, view mode, search ----+ private val _sortBy = MutableStateFlow(+ runCatching { SortBy.valueOf(prefs.getString("sort_by", null) ?: "NAME") }+ .getOrDefault(SortBy.NAME)+ )+ val sortBy: StateFlow<SortBy> = _sortBy.asStateFlow()++ private val _sortAscending = MutableStateFlow(prefs.getBoolean("sort_ascending", true))+ val sortAscending: StateFlow<Boolean> = _sortAscending.asStateFlow()++ private val _viewMode = MutableStateFlow(+ runCatching { ViewMode.valueOf(prefs.getString("view_mode", null) ?: "LIST") }+ .getOrDefault(ViewMode.LIST)+ )+ val viewMode: StateFlow<ViewMode> = _viewMode.asStateFlow()++ private val _searchQuery = MutableStateFlow("")+ val searchQuery: StateFlow<String> = _searchQuery.asStateFlow()++ private val _isSearchActive = MutableStateFlow(false)+ val isSearchActive: StateFlow<Boolean> = _isSearchActive.asStateFlow()++ override fun setSortBy(sortBy: SortBy) {+ if (_sortBy.value == sortBy) return+ _sortBy.value = sortBy+ prefs.edit { putString("sort_by", sortBy.name) }+ loadDirectory()+ }++ override fun toggleSortDirection() {+ setSortAscending(!_sortAscending.value)+ }++ override fun setSortAscending(ascending: Boolean) {+ if (_sortAscending.value == ascending) return+ _sortAscending.value = ascending+ prefs.edit { putBoolean("sort_ascending", ascending) }+ loadDirectory()+ }++ private val downloadsDir: File get() = File(rootDirectory, "Download")++ /**+ * Apply the sensible default sort when *entering* a folder: the Downloads folder shows+ * newest-first (by date), which is what people usually want there; every other folder+ * uses the persisted global sort. This only sets the in-memory sort, so an explicit+ * choice via the menu (which persists) still wins while you stay in the folder.+ */+ private fun applyDirDefaults(path: File) {+ if (path.absolutePath == downloadsDir.absolutePath) {+ _sortBy.value = SortBy.DATE+ _sortAscending.value = false+ } else {+ _sortBy.value = runCatching {+ SortBy.valueOf(prefs.getString("sort_by", null) ?: "NAME")+ }.getOrDefault(SortBy.NAME)+ _sortAscending.value = prefs.getBoolean("sort_ascending", true)+ }+ }++ override fun toggleViewMode() {+ val next = if (_viewMode.value == ViewMode.LIST) ViewMode.GRID else ViewMode.LIST+ _viewMode.value = next+ prefs.edit { putString("view_mode", next.name) }+ }++ override fun setSearchActive(active: Boolean) {+ _isSearchActive.value = active+ if (!active) _searchQuery.value = ""+ }++ override fun setSearchQuery(query: String) {+ _searchQuery.value = query+ }++ // ---- Hidden files ----+ private val _showHidden = MutableStateFlow(prefs.getBoolean("show_hidden", false))+ val showHidden: StateFlow<Boolean> = _showHidden.asStateFlow()++ override fun toggleHidden() {+ val next = !_showHidden.value+ _showHidden.value = next+ prefs.edit { putBoolean("show_hidden", next) }+ loadDirectory()+ }++ // ---- Create ----+ override fun createFolder(name: String) {+ if (isZipMode() || name.isBlank()) return+ viewModelScope.launch(Dispatchers.IO) {+ try {+ val dir = uniqueDestination(_currentDirectory.value, name.trim())+ if (dir.mkdirs()) loadDirectory()+ else emit(getApplication<Application>().getString(R.string.create_failed))+ } catch (e: Exception) {+ emitMoveFailed(e)+ }+ }+ }++ override fun createFile(name: String) {+ if (isZipMode() || name.isBlank()) return+ viewModelScope.launch(Dispatchers.IO) {+ try {+ val file = uniqueDestination(_currentDirectory.value, name.trim())+ if (file.createNewFile()) loadDirectory()+ else emit(getApplication<Application>().getString(R.string.create_failed))+ } catch (e: Exception) {+ emitMoveFailed(e)+ }+ }+ }++ override fun openWith(item: FileBrowserItem) {+ val ctx = getApplication<Application>()+ val file = item.realFile ?: return+ val uri = try {+ FileProvider.getUriForFile(ctx, "${ctx.packageName}.fileprovider", file)+ } catch (e: Exception) {+ emitMoveFailed(e)+ return+ }+ val view = Intent(Intent.ACTION_VIEW).apply {+ setDataAndType(uri, mimeFor(file))+ addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK)+ }+ val chooser = Intent.createChooser(view, null).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)+ viewModelScope.launch { _intents.emit(chooser) }+ }++ // ---- Home screen: storage, recents, bookmarks, categories ----+ private val _atHome = MutableStateFlow(true)+ val atHome: StateFlow<Boolean> = _atHome.asStateFlow()++ private val _categoryTitle = MutableStateFlow<String?>(null)+ val categoryTitle: StateFlow<String?> = _categoryTitle.asStateFlow()++ private val _storage = MutableStateFlow<StorageInfo?>(null)+ val storage: StateFlow<StorageInfo?> = _storage.asStateFlow()++ private val _recents = MutableStateFlow<List<FileBrowserItem>>(emptyList())+ val recents: StateFlow<List<FileBrowserItem>> = _recents.asStateFlow()++ private val _bookmarks = MutableStateFlow(loadBookmarks())+ val bookmarks: StateFlow<List<FileBrowserItem>> = _bookmarks.asStateFlow()++ fun loadHome() {+ viewModelScope.launch(Dispatchers.IO) {+ _storage.value = readStorage()+ _bookmarks.value = loadBookmarks()+ _recents.value = queryRecents()+ }+ }++ override fun goHome() {+ _atHome.value = true+ _categoryTitle.value = null+ _zipPath.value = null+ _zipInternalPath.value = ""+ clearSelection()+ observerJob?.cancel()+ loadHome()+ }++ override fun openInternalStorage() {+ _categoryTitle.value = null+ navigateTo(rootDirectory)+ }++ override fun openBookmark(path: File) {+ _categoryTitle.value = null+ navigateTo(path)+ }++ override fun openCategory(category: FileCategory) {+ if (category == FileCategory.DOWNLOADS) {+ val dl = File(rootDirectory, "Download")+ openBookmark(if (dl.isDirectory) dl else rootDirectory)+ return+ }+ _atHome.value = false+ _zipPath.value = null+ _categoryTitle.value = getApplication<Application>().getString(categoryLabel(category))+ clearSelection()+ observerJob?.cancel()+ viewModelScope.launch(Dispatchers.IO) {+ _entries.value = emptyList<FileBrowserItem>() to sortItems(queryCategory(category))+ }+ }++ override fun addBookmark(item: FileBrowserItem) {+ val path = item.realFile ?: return+ val set = (prefs.getStringSet("bookmarks", emptySet()) ?: emptySet()).toMutableSet()+ set.add(path.absolutePath)+ prefs.edit { putStringSet("bookmarks", set) }+ _bookmarks.value = loadBookmarks()+ emit(getApplication<Application>().getString(R.string.bookmark_added))+ }++ override fun removeBookmark(path: File) {+ val set = (prefs.getStringSet("bookmarks", emptySet()) ?: emptySet()).toMutableSet()+ set.remove(path.absolutePath)+ prefs.edit { putStringSet("bookmarks", set) }+ _bookmarks.value = loadBookmarks()+ }++ private fun loadBookmarks(): List<FileBrowserItem> {+ val saved = prefs.getStringSet("bookmarks", emptySet()) ?: emptySet()+ return saved.map { File(it) }+ .filter { it.exists() }+ .sortedBy { it.name.lowercase() }+ .map { it.toItem() }+ }++ private fun readStorage(): StorageInfo = try {+ val stat = StatFs(rootDirectory.absolutePath)+ StorageInfo(totalBytes = stat.totalBytes, freeBytes = stat.availableBytes)+ } catch (_: Exception) {+ StorageInfo(0, 0)+ }++ private fun categoryLabel(c: FileCategory): Int = when (c) {+ FileCategory.IMAGES -> R.string.cat_images+ FileCategory.VIDEOS -> R.string.cat_videos+ FileCategory.AUDIO -> R.string.cat_audio+ FileCategory.DOCUMENTS -> R.string.cat_documents+ FileCategory.DOWNLOADS -> R.string.cat_downloads+ }++ private fun queryCategory(category: FileCategory): List<FileBrowserItem> = when (category) {+ FileCategory.IMAGES -> queryMedia(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, 2000)+ FileCategory.VIDEOS -> queryMedia(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, 2000)+ FileCategory.AUDIO -> queryMedia(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, 2000)+ FileCategory.DOCUMENTS -> {+ val mimes = arrayOf(+ "application/pdf", "application/msword",+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",+ "application/vnd.ms-excel",+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",+ "application/vnd.ms-powerpoint",+ "application/vnd.openxmlformats-officedocument.presentationml.presentation",+ "text/plain", "text/markdown", "text/csv", "application/rtf",+ )+ val selection = mimes.joinToString(" OR ") { "${MediaStore.Files.FileColumns.MIME_TYPE}=?" }+ queryMedia(MediaStore.Files.getContentUri("external"), selection, mimes, 2000)+ }+ FileCategory.DOWNLOADS -> emptyList()+ }++ private fun queryRecents(): List<FileBrowserItem> =+ queryMedia(MediaStore.Files.getContentUri("external"), null, null, 40)++ private fun queryMedia(+ uri: Uri,+ selection: String?,+ args: Array<String>?,+ limit: Int,+ ): List<FileBrowserItem> {+ val ctx = getApplication<Application>()+ val projection = arrayOf(MediaStore.MediaColumns.DATA)+ val sort = "${MediaStore.MediaColumns.DATE_MODIFIED} DESC"+ val out = mutableListOf<FileBrowserItem>()+ try {+ ctx.contentResolver.query(uri, projection, selection, args, sort)?.use { c ->+ val idx = c.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)+ while (c.moveToNext() && out.size < limit) {+ val path = c.getString(idx) ?: continue+ val f = File(path)+ if (f.isFile) out.add(f.toItem())+ }+ }+ } catch (_: Exception) {+ }+ return out+ }++ private fun File.toItem() = FileBrowserItem(+ name = name,+ isDirectory = isDirectory,+ size = if (isFile) length() else null,+ realFile = this,+ zipInnerPath = null,+ key = absolutePath,+ lastModified = lastModified(),+ )++ // ---- Clipboard (copy/cut/paste) ----+ private val _clipboard = MutableStateFlow<List<File>>(emptyList())+ val clipboard: StateFlow<List<File>> = _clipboard.asStateFlow()++ private val _clipboardIsCut = MutableStateFlow(false)+ val clipboardIsCut: StateFlow<Boolean> = _clipboardIsCut.asStateFlow()++ override fun copySelection() {+ if (isZipMode()) return+ val files = _selectedPaths.value.mapNotNull { it.realFile }+ if (files.isEmpty()) return+ _clipboard.value = files+ _clipboardIsCut.value = false+ clearSelection()+ emit(getApplication<Application>().getString(R.string.copied_n, files.size))+ }++ override fun cutSelection() {+ if (isZipMode()) return+ val files = _selectedPaths.value.mapNotNull { it.realFile }+ if (files.isEmpty()) return+ _clipboard.value = files+ _clipboardIsCut.value = true+ clearSelection()+ emit(getApplication<Application>().getString(R.string.cut_n, files.size))+ }++ override fun clearClipboard() {+ _clipboard.value = emptyList()+ _clipboardIsCut.value = false+ }++ override fun pasteHere() {+ if (isZipMode()) return+ val sources = _clipboard.value+ if (sources.isEmpty()) return+ val target = _currentDirectory.value+ val isCut = _clipboardIsCut.value+ viewModelScope.launch(Dispatchers.IO) {+ var lastError: Exception? = null+ sources.forEach { source ->+ try {+ if (!source.exists()) return@forEach+ // Don't paste a folder into itself or a descendant.+ if (target.absolutePath == source.absolutePath ||+ target.absolutePath.startsWith(source.absolutePath + "/")+ ) return@forEach+ val dest = uniqueDestination(target, source.name)+ if (isCut) {+ source.atomicMoveTo(dest)+ } else if (source.isDirectory) {+ source.copyRecursively(dest, overwrite = false)+ } else {+ source.copyTo(dest, overwrite = false)+ }+ } catch (e: Exception) {+ lastError = e+ }+ }+ if (isCut) clearClipboard()+ loadDirectory()+ lastError?.let { emitMoveFailed(it) }+ }+ }++ override fun shareSelection() {+ val ctx = getApplication<Application>()+ val files = _selectedPaths.value.mapNotNull { it.realFile }.filter { it.isFile }+ if (files.isEmpty()) return+ val uris = try {+ ArrayList(files.map {+ FileProvider.getUriForFile(ctx, "${ctx.packageName}.fileprovider", it)+ })+ } catch (e: Exception) {+ emitMoveFailed(e)+ return+ }+ val intent = if (uris.size == 1) {+ Intent(Intent.ACTION_SEND).apply {+ type = mimeFor(files.first())+ putExtra(Intent.EXTRA_STREAM, uris.first())+ }+ } else {+ Intent(Intent.ACTION_SEND_MULTIPLE).apply {+ type = "*/*"+ putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)+ }+ }.apply { addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK) }+ clearSelection()+ viewModelScope.launch {+ _intents.emit(Intent.createChooser(intent, null).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))+ }+ }++ private fun mimeFor(file: File): String =+ MimeTypeMap.getSingleton().getMimeTypeFromExtension(file.extension) ?: "*/*"++ /** A destination in [dir] named [name], suffixed with " (n)" if that already exists. */+ private fun uniqueDestination(dir: File, name: String): File {+ var candidate = File(dir, name)+ if (!candidate.exists()) return candidate+ val dot = name.lastIndexOf('.')+ val base = if (dot > 0) name.substring(0, dot) else name+ val ext = if (dot > 0) name.substring(dot) else ""+ var n = 1+ while (candidate.exists()) {+ candidate = File(dir, "$base ($n)$ext")+ n+++ }+ return candidate+ }++ // ---- Share URIs ----+ private val _incomingUris = MutableStateFlow<List<Uri>?>(null)+ val incomingUris: StateFlow<List<Uri>?> = _incomingUris.asStateFlow()++ fun setIncomingUris(uris: List<Uri>) { _incomingUris.value = uris }+ fun clearIncomingUris() { _incomingUris.value = null }++ // ---- Events ----+ private val _snackbarMessages = MutableSharedFlow<String>(extraBufferCapacity = 8)+ val snackbarMessages: SharedFlow<String> = _snackbarMessages.asSharedFlow()++ private val _intents = MutableSharedFlow<Intent>(extraBufferCapacity = 4)+ val intents: SharedFlow<Intent> = _intents.asSharedFlow()++ private var observerJob: Job? = null+ private var loadJob: Job? = null+ // Guards against a stale reload (e.g. a FileObserver event that snapshots the+ // directory mid-deletion) overwriting a newer one. Only the latest load wins.+ private val loadGeneration = AtomicInteger(0)++ init {+ loadDirectory()+ restartObserver()+ loadHome()+ }++ fun loadDirectory() {+ val gen = loadGeneration.incrementAndGet()+ loadJob?.cancel()+ val zipFile = _zipPath.value+ val dir = _currentDirectory.value+ val inner = _zipInternalPath.value+ loadJob = viewModelScope.launch(Dispatchers.IO) {+ val listed = if (zipFile == null) listRealDir(dir) else listZipDir(zipFile, inner)+ val sorted = sortEntries(listed)+ if (gen == loadGeneration.get()) {+ _entries.value = sorted+ }+ }+ }++ private fun sortEntries(+ entries: Pair<List<FileBrowserItem>, List<FileBrowserItem>>+ ): Pair<List<FileBrowserItem>, List<FileBrowserItem>> =+ sortItems(entries.first) to sortItems(entries.second)++ private fun sortItems(items: List<FileBrowserItem>): List<FileBrowserItem> {+ val cmp: Comparator<FileBrowserItem> = when (_sortBy.value) {+ SortBy.NAME -> compareBy { it.name.lowercase() }+ SortBy.DATE -> compareBy { it.lastModified }+ SortBy.SIZE -> compareBy { it.size ?: -1L }+ SortBy.TYPE -> compareBy<FileBrowserItem> {+ it.name.substringAfterLast('.', "").lowercase()+ }.thenBy { it.name.lowercase() }+ }+ val sorted = items.sortedWith(cmp)+ return if (_sortAscending.value) sorted else sorted.reversed()+ }++ private fun restartObserver() {+ observerJob?.cancel()+ if (isZipMode()) {+ observerJob = null+ return+ }+ val dir = _currentDirectory.value+ observerJob = viewModelScope.launch {+ val observer = object : FileObserver(dir, CREATE or DELETE or MOVED_FROM or MOVED_TO) {+ override fun onEvent(event: Int, path: String?) { loadDirectory() }+ }+ observer.startWatching()+ try { awaitCancellation() } finally { observer.stopWatching() }+ }+ }++ override fun navigateTo(path: File) {+ if (isZipMode()) {+ _zipPath.value = null+ _zipInternalPath.value = ""+ }+ _atHome.value = false+ _categoryTitle.value = null+ _currentDirectory.value = path+ applyDirDefaults(path)+ clearSelection()+ loadDirectory()+ restartObserver()+ }++ override fun navigateIntoZipDir(dirName: String) {+ val current = _zipInternalPath.value+ val newPath = if (current.isEmpty()) dirName else "$current/$dirName"+ _zipInternalPath.value = newPath+ clearSelection()+ loadDirectory()+ }++ override fun navigateToZipInternalPath(fullInternalPath: String) {+ _zipInternalPath.value = fullInternalPath+ clearSelection()+ loadDirectory()+ }++ override fun navigateToZipParentRealFolder(target: File) {+ // breadcrumb click on real-FS part while in zip mode → exit zip+ _zipPath.value = null+ _zipInternalPath.value = ""+ _currentDirectory.value = target+ clearSelection()+ loadDirectory()+ restartObserver()+ }++ override fun handleBack(): Boolean {+ if (_selectedPaths.value.isNotEmpty()) { clearSelection(); return true }+ if (_categoryTitle.value != null) { goHome(); return true }+ val z = _zipPath.value+ when {+ z != null -> {+ val internal = _zipInternalPath.value+ if (internal.isEmpty()) {+ val parent = z.parentFile ?: rootDirectory+ _zipPath.value = null+ _currentDirectory.value = parent+ restartObserver()+ } else {+ val parentInternal = if (internal.contains("/")) internal.substringBeforeLast("/") else ""+ _zipInternalPath.value = parentInternal+ }+ loadDirectory()+ }+ _currentDirectory.value.absolutePath != rootDirectory.absolutePath -> {+ _currentDirectory.value = _currentDirectory.value.parentFile ?: _currentDirectory.value+ applyDirDefaults(_currentDirectory.value)+ loadDirectory()+ restartObserver()+ }+ // At the storage root, Back returns to the home screen.+ else -> goHome()+ }+ return true+ }++ override fun rename(item: FileBrowserItem, newName: String) {+ if (isZipMode()) return+ val path = item.realFile ?: return+ viewModelScope.launch(Dispatchers.IO) {+ try {+ val target = File(path.parentFile, newName)+ path.atomicMoveTo(target)+ clearSelection()+ loadDirectory()+ } catch (e: Exception) { emitMoveFailed(e) }+ }+ }++ override fun deleteSelection() {+ if (isZipMode()) return+ val selection = _selectedPaths.value.mapNotNull { it.realFile }+ viewModelScope.launch(Dispatchers.IO) {+ selection.forEach { it.deleteRecursively() }+ clearSelection()+ loadDirectory()+ }+ }++ override fun moveInto(sources: List<File>, target: File) {+ if (isZipMode()) return+ if (!target.isDirectory) return+ moveFiles(sources, target) { source -> source != target && !target.absolutePath.startsWith(source.absolutePath) }+ }++ override fun moveToBreadcrumb(sources: List<File>, target: File) {+ if (isZipMode()) return+ moveFiles(sources, target) { source -> source.parentFile != target && source != target }+ }++ private fun moveFiles(sources: List<File>, target: File, canMove: (File) -> Boolean) {+ viewModelScope.launch(Dispatchers.IO) {+ var movedAny = false+ var lastError: Exception? = null+ sources.forEach { source ->+ if (canMove(source)) {+ try {+ val dest = File(target, source.name)+ source.atomicMoveTo(dest)+ movedAny = true+ } catch (e: Exception) { lastError = e }+ }+ }+ if (movedAny) { clearSelection(); loadDirectory() }+ lastError?.let { emitMoveFailed(it) }+ }+ }++ override fun openZipFile(item: FileBrowserItem) {+ val file = item.realFile ?: return+ viewModelScope.launch(Dispatchers.IO) {+ try {+ ZipFile(file).use { _ -> }+ _zipPath.value = file+ _zipInternalPath.value = ""+ _currentDirectory.value = File("/") // placeholder not used in zip mode+ clearSelection()+ loadDirectory()+ restartObserver()+ } catch (e: Exception) {+ emit(getApplication<Application>().getString(R.string.could_not_open_zip, e.localizedMessage))+ }+ }+ }++ override fun archive(archiveName: String) {+ if (isZipMode()) return+ val ctx = getApplication<Application>()+ val sources = _selectedPaths.value.mapNotNull { it.realFile?.absolutePath }.toTypedArray()+ val destFileName = if (archiveName.endsWith(".zip")) archiveName else "$archiveName.zip"+ val destFile = File(_currentDirectory.value, destFileName)+ if (sources.isEmpty()) return+ val zipWork = OneTimeWorkRequestBuilder<ZipWorker>().setInputData(+ workDataOf("source_paths" to sources, "dest_path" to destFile.absolutePath)+ ).build()+ WorkManager.getInstance(ctx).enqueue(zipWork)+ clearSelection()+ emit(ctx.getString(R.string.archiving_started))+ }++ fun unzip(zipItem: FileBrowserItem, destPath: File) {+ val zipFile = zipItem.realFile ?: return+ val ctx = getApplication<Application>()+ viewModelScope.launch(Dispatchers.IO) {+ val freeBytes = runCatching { StatFs(destPath.absolutePath).availableBytes }.getOrDefault(0L)+ val budget = ArchiveLimits.extractionBudget(freeBytes)++ // What the archive says it expands to. Turning an oversized one away here means the user+ // finds out before anything is written; UnzipWorker still enforces the budget itself,+ // because these figures come from the archive and a crafted one can under-report.+ val declared = runCatching {+ ZipFile(zipFile).use { zf ->+ val entries = zf.entries().asSequence().toList()+ if (entries.size > ArchiveLimits.MAX_ENTRIES) return@runCatching Long.MAX_VALUE+ ArchiveLimits.declaredUncompressedSize(entries.asSequence().map { it.size })+ }+ }.getOrNull()++ if (declared != null && declared > budget) {+ emit(+ ctx.getString(+ R.string.zip_too_large_to_extract,+ Formatter.formatShortFileSize(ctx, declared),+ Formatter.formatShortFileSize(ctx, budget),+ )+ )+ return@launch+ }++ val unzipWork = OneTimeWorkRequestBuilder<UnzipWorker>().setInputData(+ workDataOf(+ "zip_path" to zipFile.absolutePath,+ "dest_path" to destPath.absolutePath,+ "size_budget" to budget,+ )+ ).build()+ WorkManager.getInstance(ctx).enqueue(unzipWork)+ clearSelection()+ emit(ctx.getString(R.string.unzipping_started_to, destPath.name))+ }+ }++ override fun saveIncomingUris() {+ if (isZipMode()) return+ val ctx = getApplication<Application>()+ val uris = _incomingUris.value ?: return+ val target = _currentDirectory.value+ viewModelScope.launch(Dispatchers.IO) {+ var lastError: Exception? = null+ uris.forEach { uri ->+ try {+ saveUriToPath(ctx, uri, target)+ } catch (e: Exception) {+ lastError = e+ }+ }+ clearIncomingUris()+ loadDirectory()+ lastError?.let { emitMoveFailed(it) }+ ?: viewModelScope.launch { _snackbarMessages.emit(ctx.getString(R.string.files_saved)) }+ }+ }++ override fun openFile(item: FileBrowserItem) {+ val ctx = getApplication<Application>()+ if (isZipMode()) {+ emit(ctx.getString(R.string.zip_browse_only))+ return+ }+ val file = item.realFile ?: return+ val extension = file.extension+ val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) ?: "*/*"+ val uri = try {+ FileProvider.getUriForFile(ctx, "${ctx.packageName}.fileprovider", file)+ } catch (e: Exception) {+ emitMoveFailed(e)+ return+ }+ val intent = Intent(Intent.ACTION_VIEW).apply {+ setDataAndType(uri, mimeType)+ flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION+ }+ viewModelScope.launch { _intents.emit(intent) }+ }++ // ---- APK install ----+ /** The APK waiting to be installed once the user grants the "install unknown apps" permission. */+ private var pendingApkInstall: File? = null++ private val _installPermissionRequests = MutableSharedFlow<Unit>(extraBufferCapacity = 1)+ /** Emitted when an APK was tapped but Files lacks permission to install; UI opens settings. */+ val installPermissionRequests: SharedFlow<Unit> = _installPermissionRequests.asSharedFlow()++ override fun installApk(item: FileBrowserItem) {+ val ctx = getApplication<Application>()+ if (isZipMode()) {+ emit(ctx.getString(R.string.zip_browse_only))+ return+ }+ val file = item.realFile ?: return+ if (ctx.packageManager.canRequestPackageInstalls()) {+ launchApkInstall(file)+ } else {+ pendingApkInstall = file+ emit(ctx.getString(R.string.install_permission_needed))+ _installPermissionRequests.tryEmit(Unit)+ }+ }++ /** Called by the UI after returning from the "install unknown apps" settings screen. */+ fun onInstallPermissionResult() {+ val ctx = getApplication<Application>()+ val file = pendingApkInstall ?: return+ pendingApkInstall = null+ if (ctx.packageManager.canRequestPackageInstalls()) {+ launchApkInstall(file)+ } else {+ emit(ctx.getString(R.string.install_permission_denied))+ }+ }++ private fun launchApkInstall(file: File) {+ val ctx = getApplication<Application>()+ val uri = try {+ FileProvider.getUriForFile(ctx, "${ctx.packageName}.fileprovider", file)+ } catch (e: Exception) {+ emitMoveFailed(e)+ return+ }+ val intent = Intent(Intent.ACTION_VIEW).apply {+ setDataAndType(uri, "application/vnd.android.package-archive")+ flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION+ }+ viewModelScope.launch { _intents.emit(intent) }+ }++ fun showMessage(message: String) { emit(message) }+ private fun emit(message: String) { viewModelScope.launch { _snackbarMessages.emit(message) } }+ private fun emitMoveFailed(e: Exception) {+ emit(getApplication<Application>().getString(R.string.move_failed, e.localizedMessage))+ }++ private fun saveUriToPath(context: Context, uri: Uri, targetDir: File) {+ val name = getFileName(context, uri) ?: "shared_file_${System.currentTimeMillis()}"+ val targetFile = File(targetDir, name)+ context.contentResolver.openInputStream(uri)?.use { input ->+ targetFile.outputStream().use { out -> input.copyTo(out) }+ }+ }++ private fun getFileName(context: Context, uri: Uri): String? {+ if (uri.scheme == "content") {+ context.contentResolver.query(uri, null, null, null, null)?.use { cursor ->+ if (cursor.moveToFirst()) {+ val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)+ if (nameIndex != -1) return cursor.getString(nameIndex)+ }+ }+ }+ return uri.path?.substringAfterLast('/')+ }++ private fun listRealDir(dir: File): Pair<List<FileBrowserItem>, List<FileBrowserItem>> {+ val all = dir.listFiles()?.toList() ?: emptyList()+ val visible = if (_showHidden.value) all else all.filterNot { it.name.startsWith(".") }+ val items = visible.map { it.toItem() }+ return items.partition { it.isDirectory }+ }++ private fun listZipDir(zipFile: File, internalDir: String): Pair<List<FileBrowserItem>, List<FileBrowserItem>> {+ return try {+ ZipFile(zipFile).use { zf ->+ val prefix = if (internalDir.isEmpty()) "" else "$internalDir/"+ val dirMap = mutableMapOf<String, FileBrowserItem>()+ // Keyed, not a list: ZIP allows two entries with the same name, and the key ends up as+ // a Compose item key, where a duplicate crashes the browser.+ val fileMap = mutableMapOf<String, FileBrowserItem>()+ for (entry in zf.entries()) {+ val rawName = entry.name+ val normalized = rawName.trimEnd('/')+ if (normalized.isEmpty()) continue+ if (normalized == internalDir) continue+ if (internalDir.isNotEmpty() && !rawName.startsWith(prefix) && !normalized.startsWith(prefix)) continue+ val remainder = if (prefix.isEmpty()) normalized else {+ if (normalized.length <= prefix.length) continue+ normalized.substring(prefix.length)+ }+ if (remainder.isEmpty()) continue+ val slashIdx = remainder.indexOf('/')+ if (slashIdx != -1) {+ val first = remainder.substring(0, slashIdx)+ if (first.isEmpty()) continue+ if (!dirMap.containsKey(first)) {+ val fullInner = if (internalDir.isEmpty()) first else "$internalDir/$first"+ dirMap[first] = FileBrowserItem(+ name = first,+ isDirectory = true,+ size = null,+ realFile = null,+ zipInnerPath = fullInner,+ key = "zip:$fullInner"+ )+ }+ } else {+ if (entry.isDirectory) {+ if (!dirMap.containsKey(remainder)) {+ val fullInner = if (internalDir.isEmpty()) remainder else "$internalDir/$remainder"+ dirMap[remainder] = FileBrowserItem(+ name = remainder,+ isDirectory = true,+ size = null,+ realFile = null,+ zipInnerPath = fullInner,+ key = "zip:$fullInner"+ )+ }+ } else {+ val fullInner = if (internalDir.isEmpty()) remainder else "$internalDir/$remainder"+ fileMap[remainder] = FileBrowserItem(+ name = remainder,+ isDirectory = false,+ size = entry.size.takeIf { it >= 0 },+ realFile = null,+ zipInnerPath = fullInner,+ key = "zip:$fullInner"+ )+ }+ }+ }+ dirMap.values.toList() to fileMap.values.toList()+ }+ } catch (_: Exception) {+ emptyList<FileBrowserItem>() to emptyList()+ }+ }++ private fun File.atomicMoveTo(target: File) {+ try {+ java.nio.file.Files.move(this.toPath(), target.toPath(), java.nio.file.StandardCopyOption.ATOMIC_MOVE)+ } catch (_: Exception) {+ if (!this.renameTo(target)) {+ if (this.isDirectory) {+ this.copyRecursively(target, overwrite = true)+ this.deleteRecursively()+ } else {+ this.copyTo(target, overwrite = true)+ this.delete()+ }+ }+ }+ }+}
files/src/main/java/com/vayunmathur/files/platform/UnzipWorker.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/UnzipWorker.kt b/files/src/main/java/com/vayunmathur/files/platform/UnzipWorker.ktindex 2f71e8c73c..417659bf80 100644--- a/files/src/main/java/com/vayunmathur/files/platform/UnzipWorker.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/UnzipWorker.kt@@ -1,85 +1,118 @@-package com.vayunmathur.files.platform-import android.content.Context-import androidx.work.WorkerParameters-import com.vayunmathur.files.R-import java.io.File-import java.io.FileInputStream-import java.io.FileOutputStream-import java.io.FilterInputStream-import java.util.zip.ZipInputStream--class UnzipWorker(context: Context, params: WorkerParameters) : ProgressNotificationWorker(- context,- params,- channelId = "unzip_progress_channel",- notificationId = 2,- channelNameRes = R.string.unzip_channel_name,- contentTitleRes = R.string.unzipping,-) {-- override suspend fun doWork(): Result {- val zipPathString = inputData.getString("zip_path") ?: return Result.failure()- val destPathString = inputData.getString("dest_path") ?: return Result.failure()-- val zipFile = File(zipPathString)- val destDir = File(destPathString)- val destDirCanonical = destDir.canonicalFile-- createNotificationChannel()- setForeground(createForegroundInfo(0))-- return try {- val zipFileSize = zipFile.length()- var totalBytesRead = 0L-- FileInputStream(zipFile).use { fis ->- val countingInputStream = object : FilterInputStream(fis) {- override fun read(): Int {- val b = super.read()- if (b != -1) {- totalBytesRead++- updateProgress(totalBytesRead, zipFileSize)- }- return b- }-- override fun read(b: ByteArray, off: Int, len: Int): Int {- val count = super.read(b, off, len)- if (count != -1) {- totalBytesRead += count- updateProgress(totalBytesRead, zipFileSize)- }- return count- }- }-- ZipInputStream(countingInputStream).use { zipInputStream ->- var entry = zipInputStream.nextEntry- while (entry != null) {- val entryFile = File(destDir, entry.name).canonicalFile- if (!entryFile.path.startsWith(destDirCanonical.path)) {- entry = zipInputStream.nextEntry- continue- }- if (entry.isDirectory) {- entryFile.mkdirs()- } else {- entryFile.parentFile?.mkdirs()- FileOutputStream(entryFile).use { out ->- zipInputStream.copyTo(out)- }- }- zipInputStream.closeEntry()- entry = zipInputStream.nextEntry- }- }- }- Result.success()- } catch (e: Exception) {- e.printStackTrace()- Result.failure()- } finally {- cancelNotification()- }- }-}+package com.vayunmathur.files.platform+import android.content.Context+import androidx.work.WorkerParameters+import com.vayunmathur.files.R+import java.io.File+import java.io.FileInputStream+import java.io.FileOutputStream+import java.io.FilterInputStream+import java.io.InputStream+import java.io.OutputStream+import java.util.zip.ZipInputStream++class UnzipWorker(context: Context, params: WorkerParameters) : ProgressNotificationWorker(+ context,+ params,+ channelId = "unzip_progress_channel",+ notificationId = 2,+ channelNameRes = R.string.unzip_channel_name,+ contentTitleRes = R.string.unzipping,+) {++ override suspend fun doWork(): Result {+ val zipPathString = inputData.getString("zip_path") ?: return Result.failure()+ val destPathString = inputData.getString("dest_path") ?: return Result.failure()+ // The caller checks the archive's declared size against this too, but the declaration comes+ // from the archive and can lie, so the real ceiling is enforced here as bytes are written.+ val budget = inputData.getLong("size_budget", Long.MAX_VALUE)++ val zipFile = File(zipPathString)+ val destDir = File(destPathString)+ val destDirCanonical = destDir.canonicalFile++ createNotificationChannel()+ setForeground(createForegroundInfo(0))++ // Everything this run created, so a rejected archive does not leave its partial output behind.+ val extracted = mutableListOf<File>()++ return try {+ val zipFileSize = zipFile.length()+ var totalBytesRead = 0L++ FileInputStream(zipFile).use { fis ->+ val countingInputStream = object : FilterInputStream(fis) {+ override fun read(): Int {+ val b = super.read()+ if (b != -1) {+ totalBytesRead+++ updateProgress(totalBytesRead, zipFileSize)+ }+ return b+ }++ override fun read(b: ByteArray, off: Int, len: Int): Int {+ val count = super.read(b, off, len)+ if (count != -1) {+ totalBytesRead += count+ updateProgress(totalBytesRead, zipFileSize)+ }+ return count+ }+ }++ ZipInputStream(countingInputStream).use { zipInputStream ->+ var totalWritten = 0L+ var entryCount = 0+ var entry = zipInputStream.nextEntry+ while (entry != null) {+ if (++entryCount > ArchiveLimits.MAX_ENTRIES) throw ArchiveTooLargeException()+ val entryFile = ArchiveLimits.resolveEntry(destDirCanonical, entry.name)+ if (entryFile == null) {+ entry = zipInputStream.nextEntry+ continue+ }+ if (entry.isDirectory) {+ entryFile.mkdirs()+ } else {+ entryFile.parentFile?.mkdirs()+ extracted.add(entryFile)+ FileOutputStream(entryFile).use { out ->+ totalWritten += copyBounded(zipInputStream, out, budget - totalWritten)+ }+ }+ zipInputStream.closeEntry()+ entry = zipInputStream.nextEntry+ }+ }+ }+ Result.success()+ } catch (e: Exception) {+ e.printStackTrace()+ extracted.forEach { runCatching { it.delete() } }+ Result.failure()+ } finally {+ cancelNotification()+ }+ }++ /**+ * Copies one entry, refusing to write more than [remaining] bytes.+ *+ * A plain copy would follow the entry to its end, which is exactly what a zip bomb relies on: a+ * few kilobytes of input can ask for gigabytes of output.+ */+ private fun copyBounded(input: InputStream, out: OutputStream, remaining: Long): Long {+ if (remaining <= 0) throw ArchiveTooLargeException()+ val buffer = ByteArray(DEFAULT_BUFFER_SIZE)+ var written = 0L+ while (true) {+ val read = input.read(buffer)+ if (read == -1) break+ if (written + read > remaining) throw ArchiveTooLargeException()+ out.write(buffer, 0, read)+ written += read+ }+ return written+ }+}
files/src/main/res/values/strings.xml
diff --git a/files/src/main/res/values/strings.xml b/files/src/main/res/values/strings.xmlindex 79530e100e..a2a55b76bb 100644--- a/files/src/main/res/values/strings.xml+++ b/files/src/main/res/values/strings.xml@@ -25,6 +25,8 @@ <string name="move_failed">Move failed: <xliff:g id="error" example="Permission denied">%1$s</xliff:g></string> <!-- Snackbar error when zip file cannot be opened. %1$s = error details --> <string name="could_not_open_zip">Could not open zip: <xliff:g id="error" example="Invalid format">%1$s</xliff:g></string>+ <!-- Snackbar when an archive would not fit. %1$s = expanded size, %2$s = space available for it -->+ <string name="zip_too_large_to_extract">Too large to extract: needs <xliff:g id="needed" example="4.2 GB">%1$s</xliff:g>, only <xliff:g id="available" example="1.1 GB">%2$s</xliff:g> usable</string> <!-- Snackbar when no app can handle ACTION_VIEW intent for a file --> <string name="no_app_found_to_open_file">No app found to open this file</string> <!-- Snackbar/info when user tries to open file while browsing inside zip (read-only mode) -->
files/src/test/java/com/vayunmathur/files/platform/ArchiveLimitsTest.kt
diff --git a/files/src/test/java/com/vayunmathur/files/platform/ArchiveLimitsTest.kt b/files/src/test/java/com/vayunmathur/files/platform/ArchiveLimitsTest.ktnew file mode 100644index 0000000000..ea448ca67e--- /dev/null+++ b/files/src/test/java/com/vayunmathur/files/platform/ArchiveLimitsTest.kt@@ -0,0 +1,71 @@+package com.vayunmathur.files.platform++import java.io.File+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNotNull+import kotlin.test.assertNull++class ArchiveLimitsTest {++ private val dest = File("/storage/emulated/0/out").canonicalFile++ @Test+ fun anOrdinaryEntryLandsInsideTheDestination() {+ val resolved = assertNotNull(ArchiveLimits.resolveEntry(dest, "photos/a.jpg"))+ assertEquals(File(dest, "photos/a.jpg").canonicalPath, resolved.canonicalPath)+ }++ @Test+ fun anEntryClimbingOutIsRejected() {+ assertNull(ArchiveLimits.resolveEntry(dest, "../../etc/passwd"))+ assertNull(ArchiveLimits.resolveEntry(dest, "a/../../../b"))+ }++ @Test+ fun anEntryEscapingIntoASiblingWithTheSamePrefixIsRejected() {+ // The bug this check used to have: comparing paths by raw prefix accepted a sibling whose+ // name merely starts with the destination's, so "out-evil" passed for a destination of "out".+ assertNull(ArchiveLimits.resolveEntry(dest, "../out-evil/payload"))+ assertNull(ArchiveLimits.resolveEntry(dest, "../outx"))+ }++ @Test+ fun anAbsoluteEntryNameIsPulledInsideTheDestination() {+ // File(parent, "/etc/hosts") treats the leading separator as a separator rather than a root,+ // so the entry lands under the destination instead of at the filesystem root.+ val resolved = assertNotNull(ArchiveLimits.resolveEntry(dest, "/etc/hosts"))+ assertEquals(true, resolved.canonicalPath.startsWith(dest.canonicalPath + File.separator))+ }++ @Test+ fun theDestinationItselfIsAllowed() {+ assertNotNull(ArchiveLimits.resolveEntry(dest, ""))+ }++ @Test+ fun theBudgetLeavesHeadroomFree() {+ val free = 4L * 1024 * 1024 * 1024+ val budget = ArchiveLimits.extractionBudget(free)+ assertEquals(true, budget in 1 until free, "budget $budget should be under $free but positive")+ }++ @Test+ fun anAlmostFullVolumeHasNoBudget() {+ assertEquals(0L, ArchiveLimits.extractionBudget(0))+ assertEquals(0L, ArchiveLimits.extractionBudget(1024))+ }++ @Test+ fun declaredSizesAreSummed() {+ assertEquals(600L, ArchiveLimits.declaredUncompressedSize(sequenceOf(100L, 200L, 300L)))+ assertEquals(0L, ArchiveLimits.declaredUncompressedSize(emptySequence()))+ }++ @Test+ fun anUndeclaredSizeMakesTheTotalUnknown() {+ // -1 is what ZipEntry reports when the archive does not say, and guessing would let a bomb+ // through the pre-flight check, so the caller has to fall back to the write-time budget.+ assertNull(ArchiveLimits.declaredUncompressedSize(sequenceOf(100L, -1L, 300L)))+ }+}