-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Properly propagate exception in archive and throw ArchivePa…
…sswordRequiredException.
- Loading branch information
Showing
6 changed files
with
172 additions
and
38 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
app/src/main/java/me/zhanghai/android/files/provider/archive/ArchiveExceptionExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright (c) 2023 Hai Zhang <dreaming.in.code.zh@gmail.com> | ||
* All Rights Reserved. | ||
*/ | ||
|
||
package me.zhanghai.android.files.provider.archive | ||
|
||
import android.system.OsConstants | ||
import java8.nio.file.FileSystemException | ||
import java8.nio.file.Path | ||
import me.zhanghai.android.files.provider.common.DelegateInputStream | ||
import me.zhanghai.android.libarchive.ArchiveException | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.io.InterruptedIOException | ||
|
||
// See also libarchive/archive_platform.h . | ||
private const val ARCHIVE_ERRNO_MISC = -1 | ||
|
||
fun ArchiveException.toFileSystemOrInterruptedIOException( | ||
file: String?, | ||
other: String? = null | ||
): IOException = | ||
when { | ||
// See also ReadArchive.toArchiveException . | ||
code == OsConstants.EINTR -> InterruptedIOException(message) | ||
// See also libarchive/archive_read_support_format_zip.c . | ||
code == ARCHIVE_ERRNO_MISC && ( | ||
message == "Incorrect passphrase" || message == "Passphrase required for this entry" | ||
) -> ArchivePasswordRequiredException(file, other, message) | ||
else -> FileSystemException(file, other, message) | ||
}.apply { initCause(this@toFileSystemOrInterruptedIOException) } | ||
|
||
class ArchiveExceptionInputStream( | ||
inputStream: InputStream, | ||
private val file: Path | ||
) : DelegateInputStream(inputStream) { | ||
@Throws(IOException::class) | ||
override fun read(): Int = | ||
try { | ||
super.read() | ||
} catch (e: ArchiveException) { | ||
throw e.toFileSystemOrInterruptedIOException(file.toString()) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun read(b: ByteArray): Int = | ||
try { | ||
super.read(b) | ||
} catch (e: ArchiveException) { | ||
throw e.toFileSystemOrInterruptedIOException(file.toString()) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun read(b: ByteArray, off: Int, len: Int): Int = | ||
try { | ||
super.read(b, off, len) | ||
} catch (e: ArchiveException) { | ||
throw e.toFileSystemOrInterruptedIOException(file.toString()) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun skip(n: Long): Long = try { | ||
super.skip(n) | ||
} catch (e: ArchiveException) { | ||
throw e.toFileSystemOrInterruptedIOException(file.toString()) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun available(): Int = | ||
try { | ||
super.available() | ||
} catch (e: ArchiveException) { | ||
throw e.toFileSystemOrInterruptedIOException(file.toString()) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun close() { | ||
try { | ||
super.close() | ||
} catch (e: ArchiveException) { | ||
throw e.toFileSystemOrInterruptedIOException(file.toString()) | ||
} | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun reset() { | ||
try { | ||
super.reset() | ||
} catch (e: ArchiveException) { | ||
throw e.toFileSystemOrInterruptedIOException(file.toString()) | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../main/java/me/zhanghai/android/files/provider/archive/ArchivePasswordRequiredException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright (c) 2023 Hai Zhang <dreaming.in.code.zh@gmail.com> | ||
* All Rights Reserved. | ||
*/ | ||
|
||
package me.zhanghai.android.files.provider.archive | ||
|
||
import android.content.Intent | ||
import me.zhanghai.android.files.provider.common.UserActionRequiredException | ||
|
||
class ArchivePasswordRequiredException : UserActionRequiredException { | ||
constructor(file: String?) : super(file) | ||
|
||
constructor(file: String?, other: String?, reason: String?) : super(file, other, reason) | ||
|
||
override fun getUserAction(): Intent = TODO() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
app/src/main/java/me/zhanghai/android/files/provider/common/UserActionRequiredException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright (c) 2023 Hai Zhang <dreaming.in.code.zh@gmail.com> | ||
* All Rights Reserved. | ||
*/ | ||
|
||
package me.zhanghai.android.files.provider.common | ||
|
||
import android.content.Intent | ||
import java8.nio.file.FileSystemException | ||
|
||
abstract class UserActionRequiredException : FileSystemException { | ||
constructor(file: String?) : super(file) | ||
|
||
constructor(file: String?, other: String?, reason: String?) : super(file, other, reason) | ||
|
||
abstract fun getUserAction(): Intent | ||
} |