Skip to content

Commit

Permalink
Tolerate unexpected exceptions in DocumentsStorage and ApkRestore
Browse files Browse the repository at this point in the history
  • Loading branch information
grote committed Feb 9, 2024
1 parent 9c4f9d8 commit 1fcd80f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,22 @@ internal class DocumentsStorage(

@Throws(IOException::class)
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)
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)
}
}

}
Expand All @@ -161,8 +171,10 @@ internal suspend fun DocumentFile.createOrGetFile(
throw IOException("File named ${this.name}, but should be $name")
}
} ?: throw IOException()
} catch (e: IllegalArgumentException) {
// Can be thrown by FileSystemProvider#isChildDocument() when flash drive is not plugged-in
} catch (e: Exception) {
// 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
throw IOException(e)
}
Expand Down Expand Up @@ -248,7 +260,7 @@ internal fun getTreeDocumentFile(parent: DocumentFile, context: Context, uri: Ur
suspend fun DocumentFile.findFileBlocking(context: Context, displayName: String): DocumentFile? {
val files = try {
listFilesBlocking(context)
} catch (e: IOException) {
} catch (e: Exception) {
Log.e(TAG, "Error finding file blocking", e)
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ internal class ApkRestore(
} catch (e: TimeoutCancellationException) {
Log.e(TAG, "Timeout while re-installing APK for $packageName.", e)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import com.stevesoltys.seedvault.metadata.PackageState
import com.stevesoltys.seedvault.settings.SettingsManager
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
Expand Down Expand Up @@ -145,10 +144,8 @@ internal class ApkBackup(
val apk = File(apkPath)
return try {
apk.inputStream()
} catch (e: FileNotFoundException) {
Log.e(TAG, "Error opening ${apk.absolutePath} for backup.", e)
throw IOException(e)
} catch (e: SecurityException) {
} catch (e: Exception) {
// SAF may throw all sorts of exceptions, so wrap them in IOException
Log.e(TAG, "Error opening ${apk.absolutePath} for backup.", e)
throw IOException(e)
}
Expand Down

0 comments on commit 1fcd80f

Please sign in to comment.