diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/feature/session/FileTransferDialogs.kt b/app/src/main/java/io/github/lordofpolls/shellwave/feature/session/FileTransferDialogs.kt index d9a840e..3285bdb 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/feature/session/FileTransferDialogs.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/feature/session/FileTransferDialogs.kt @@ -253,7 +253,7 @@ private fun displayNameForUpload(context: Context, uri: Uri): String = /** * Queried, not opened for writing: the download path only truncates on its own later - * `getOutputStream("w")` call, so the original content is still intact when this reads + * `getOutputStream("wt")` call, so the original content is still intact when this reads * [OpenableColumns.SIZE]. * * A provider that doesn't report a size is treated as "no existing content" - erring the other way diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshConnection.kt b/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshConnection.kt index 0e1d03f..951cad1 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshConnection.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshConnection.kt @@ -940,13 +940,8 @@ open class SshConnection( } catch (e: IOException) { null } - when { - remoteSize == null -> Result.success(lastReported) - expectedSize >= 0 && remoteSize != expectedSize -> - Result.failure(IOException("Uploaded, but the server now reports $remoteSize bytes for a $expectedSize-byte file - the transfer looks incomplete")) - - else -> Result.success(lastReported) - } + val problem = uploadVerificationError(remoteSize, expectedSize) + if (problem != null) Result.failure(IOException(problem)) else Result.success(lastReported) } catch (e: CancellationException) { throw e } catch (e: Exception) { diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/ssh/UriFileTransfer.kt b/app/src/main/java/io/github/lordofpolls/shellwave/ssh/UriFileTransfer.kt index bd471dd..71fc356 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/ssh/UriFileTransfer.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/ssh/UriFileTransfer.kt @@ -38,10 +38,30 @@ internal class UriDestFile(private val context: Context, private val uri: Uri) : override fun getOutputStream(): OutputStream = getOutputStream(false) override fun getOutputStream(append: Boolean): OutputStream = - context.contentResolver.openOutputStream(uri, if (append) "wa" else "w") + context.contentResolver.openOutputStream(uri, openMode(append)) ?: throw IOException("Could not open $uri for writing") } +/** + * "wt", not "w": plain "w" does not truncate on many document providers, so overwriting a longer + * file would leave its old trailing bytes behind. + */ +internal fun openMode(append: Boolean): String = if (append) "wa" else "wt" + +/** + * Why an upload can't be confirmed complete, or null when it can. [remoteSize] is null when the + * post-upload `stat` failed, which is not proof of success. [expectedSize] is -1 when the source + * size is unknown, so only the `stat` failure can be reported then. + */ +internal fun uploadVerificationError(remoteSize: Long?, expectedSize: Long): String? = + when { + remoteSize == null -> "Uploaded, but could not confirm the file's size on the server" + expectedSize >= 0 && remoteSize != expectedSize -> + "Uploaded, but the server now reports $remoteSize bytes for a $expectedSize-byte file - the transfer looks incomplete" + + else -> null + } + private fun queryDisplayName(context: Context, uri: Uri): String? = runCatching { context.contentResolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null) diff --git a/app/src/test/java/io/github/lordofpolls/shellwave/ssh/UriFileTransferTest.kt b/app/src/test/java/io/github/lordofpolls/shellwave/ssh/UriFileTransferTest.kt new file mode 100644 index 0000000..7e2b889 --- /dev/null +++ b/app/src/test/java/io/github/lordofpolls/shellwave/ssh/UriFileTransferTest.kt @@ -0,0 +1,29 @@ +package io.github.lordofpolls.shellwave.ssh + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Test + +class UriFileTransferTest { + @Test + fun overwriteTruncates() = assertEquals("wt", openMode(append = false)) + + @Test + fun appendDoesNotTruncate() = assertEquals("wa", openMode(append = true)) + + @Test + fun failedStat_isNotVerified() = assertNotNull(uploadVerificationError(null, 10L)) + + @Test + fun failedStat_withUnknownSize_isNotVerified() = assertNotNull(uploadVerificationError(null, -1L)) + + @Test + fun sizeMismatch_isReported() = assertNotNull(uploadVerificationError(5L, 10L)) + + @Test + fun matchingSize_passes() = assertNull(uploadVerificationError(10L, 10L)) + + @Test + fun unknownSourceSize_passes() = assertNull(uploadVerificationError(10L, -1L)) +}