From c2dacab69aa8e2a03c81b81e4d2e940fbd8a5d2e Mon Sep 17 00:00:00 2001 From: Derek Ellis Date: Tue, 16 Jun 2026 15:50:13 -0400 Subject: [PATCH] Catch `FileNotFoundException` in `AssetFileSystem.existsInternal` Under certain circumstances, the native asset manager implementation can throw a `FileNotFoundException` if the asset directory cannot be opened for listing (see https://android.googlesource.com/platform/frameworks/base/+/refs/heads/main/core/jni/android_util_AssetManager.cpp#475) This can, for example, cause `AssetFileSystem.exists()` to throw instead of returning `false` which is (arguably?) different from the behaviour of other `FileSystem` implementations. --- .../main/kotlin/okio/assetfilesystem/AssetFileSystem.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/okio-assetfilesystem/src/main/kotlin/okio/assetfilesystem/AssetFileSystem.kt b/okio-assetfilesystem/src/main/kotlin/okio/assetfilesystem/AssetFileSystem.kt index b63dca2038..e19d26f97b 100644 --- a/okio-assetfilesystem/src/main/kotlin/okio/assetfilesystem/AssetFileSystem.kt +++ b/okio-assetfilesystem/src/main/kotlin/okio/assetfilesystem/AssetFileSystem.kt @@ -67,7 +67,13 @@ private class AssetFileSystem( // Both non-existent paths and paths to existing files return an empty array when listing. // Determine if a path exists by checking if its name is present in the parent's list. val parent = checkNotNull(parent) { "Path has no parent. Did you canonicalize? $this" } - val children = assets.list(parent.toAssetRelativePathString()).orEmpty() + + val children = try { + assets.list(parent.toAssetRelativePathString()).orEmpty() + } catch (_: FileNotFoundException) { + emptyArray() + } + return name in children }