Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
Loading