Tolerate unexpected exceptions in DocumentsStorage and ApkRestore

This commit is contained in:
Torsten Grote 2024-02-09 15:40:03 -03:00 committed by Chirayu Desai
parent 758186392d
commit 6428399bed
3 changed files with 22 additions and 10 deletions

View file

@ -134,12 +134,22 @@ internal class DocumentsStorage(
@Throws(IOException::class) @Throws(IOException::class)
fun getInputStream(file: DocumentFile): InputStream { fun getInputStream(file: DocumentFile): InputStream {
return contentResolver.openInputStream(file.uri) ?: throw IOException() return try {
contentResolver.openInputStream(file.uri) ?: throw IOException()
} catch (e: Exception) {
// SAF can throw all sorts of exceptions, so wrap it in IOException
throw IOException(e)
}
} }
@Throws(IOException::class) @Throws(IOException::class)
fun getOutputStream(file: DocumentFile): OutputStream { fun getOutputStream(file: DocumentFile): OutputStream {
return contentResolver.openOutputStream(file.uri, "wt") ?: throw IOException() return try {
contentResolver.openOutputStream(file.uri, "wt") ?: throw IOException()
} catch (e: Exception) {
// SAF can throw all sorts of exceptions, so wrap it in IOException
throw IOException(e)
}
} }
} }
@ -161,8 +171,10 @@ internal suspend fun DocumentFile.createOrGetFile(
throw IOException("File named ${this.name}, but should be $name") throw IOException("File named ${this.name}, but should be $name")
} }
} ?: throw IOException() } ?: throw IOException()
} catch (e: IllegalArgumentException) { } catch (e: Exception) {
// Can be thrown by FileSystemProvider#isChildDocument() when flash drive is not plugged-in // SAF can throw all sorts of exceptions, so wrap it in IOException.
// E.g. IllegalArgumentException can be thrown by FileSystemProvider#isChildDocument()
// when flash drive is not plugged-in:
// http://aosp.opersys.com/xref/android-11.0.0_r8/xref/frameworks/base/core/java/com/android/internal/content/FileSystemProvider.java#135 // http://aosp.opersys.com/xref/android-11.0.0_r8/xref/frameworks/base/core/java/com/android/internal/content/FileSystemProvider.java#135
throw IOException(e) throw IOException(e)
} }
@ -248,7 +260,7 @@ internal fun getTreeDocumentFile(parent: DocumentFile, context: Context, uri: Ur
suspend fun DocumentFile.findFileBlocking(context: Context, displayName: String): DocumentFile? { suspend fun DocumentFile.findFileBlocking(context: Context, displayName: String): DocumentFile? {
val files = try { val files = try {
listFilesBlocking(context) listFilesBlocking(context)
} catch (e: IOException) { } catch (e: Exception) {
Log.e(TAG, "Error finding file blocking", e) Log.e(TAG, "Error finding file blocking", e)
return null return null
} }

View file

@ -76,6 +76,9 @@ internal class ApkRestore(
} catch (e: TimeoutCancellationException) { } catch (e: TimeoutCancellationException) {
Log.e(TAG, "Timeout while re-installing APK for $packageName.", e) Log.e(TAG, "Timeout while re-installing APK for $packageName.", e)
emit(installResult.fail(packageName)) emit(installResult.fail(packageName))
} catch (e: Exception) {
Log.e(TAG, "Unexpected exception while re-installing APK for $packageName.", e)
emit(installResult.fail(packageName))
} }
} }
installResult.isFinished = true installResult.isFinished = true

View file

@ -17,7 +17,6 @@ import com.stevesoltys.seedvault.metadata.PackageState
import com.stevesoltys.seedvault.settings.SettingsManager import com.stevesoltys.seedvault.settings.SettingsManager
import java.io.File import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.io.OutputStream import java.io.OutputStream
@ -145,10 +144,8 @@ internal class ApkBackup(
val apk = File(apkPath) val apk = File(apkPath)
return try { return try {
apk.inputStream() apk.inputStream()
} catch (e: FileNotFoundException) { } catch (e: Exception) {
Log.e(TAG, "Error opening ${apk.absolutePath} for backup.", e) // SAF may throw all sorts of exceptions, so wrap them in IOException
throw IOException(e)
} catch (e: SecurityException) {
Log.e(TAG, "Error opening ${apk.absolutePath} for backup.", e) Log.e(TAG, "Error opening ${apk.absolutePath} for backup.", e)
throw IOException(e) throw IOException(e)
} }