From a49031d26368339e3cd51b2101b0ebf6d72a7105 Mon Sep 17 00:00:00 2001 From: Xayah Date: Wed, 24 Jul 2024 22:37:24 +0800 Subject: [PATCH] fix: [SMB] 0 byte uploaded file * On certain devices, smbj uploads file without any exception but the remote file is 0 byte. Change-Id: Iaf593d7dd0ffb61b0216a72833d4a4dfb49dbccb --- .../com/xayah/core/network/client/FTPClientImpl.kt | 1 + .../com/xayah/core/network/client/SFTPClientImpl.kt | 2 ++ .../com/xayah/core/network/client/SMBClientImpl.kt | 11 +++++------ 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/source/core/network/src/main/kotlin/com/xayah/core/network/client/FTPClientImpl.kt b/source/core/network/src/main/kotlin/com/xayah/core/network/client/FTPClientImpl.kt index 1f3c63b154..5beeae560e 100644 --- a/source/core/network/src/main/kotlin/com/xayah/core/network/client/FTPClientImpl.kt +++ b/source/core/network/src/main/kotlin/com/xayah/core/network/client/FTPClientImpl.kt @@ -96,6 +96,7 @@ class FTPClientImpl(private val entity: CloudEntity, private val extra: FTPExtra client.storeFile(dstPath, countingStream) srcInputStream.close() countingStream.close() + if (countingStream.byteCount == 0L) throw IOException("Failed to write remote file: 0 byte.") onUploading(countingStream.byteCount, countingStream.byteCount) } diff --git a/source/core/network/src/main/kotlin/com/xayah/core/network/client/SFTPClientImpl.kt b/source/core/network/src/main/kotlin/com/xayah/core/network/client/SFTPClientImpl.kt index c7cb9b2882..de71699210 100644 --- a/source/core/network/src/main/kotlin/com/xayah/core/network/client/SFTPClientImpl.kt +++ b/source/core/network/src/main/kotlin/com/xayah/core/network/client/SFTPClientImpl.kt @@ -28,6 +28,7 @@ import net.schmizz.sshj.userauth.password.PasswordFinder import net.schmizz.sshj.userauth.password.Resource import java.io.File import java.io.FileOutputStream +import java.io.IOException class SFTPClientImpl(private val entity: CloudEntity, private val extra: SFTPExtra) : CloudClient { @@ -121,6 +122,7 @@ class SFTPClientImpl(private val entity: CloudEntity, private val extra: SFTPExt srcInputStream.close() countingStream.close() dstFile.close() + if (countingStream.byteCount == 0L) throw IOException("Failed to write remote file: 0 byte.") onUploading(countingStream.byteCount, countingStream.byteCount) } diff --git a/source/core/network/src/main/kotlin/com/xayah/core/network/client/SMBClientImpl.kt b/source/core/network/src/main/kotlin/com/xayah/core/network/client/SMBClientImpl.kt index c697e61e98..8c0950840c 100644 --- a/source/core/network/src/main/kotlin/com/xayah/core/network/client/SMBClientImpl.kt +++ b/source/core/network/src/main/kotlin/com/xayah/core/network/client/SMBClientImpl.kt @@ -11,7 +11,6 @@ import com.hierynomus.smbj.SMBClient import com.hierynomus.smbj.SmbConfig import com.hierynomus.smbj.auth.AuthenticationContext import com.hierynomus.smbj.common.SMBRuntimeException -import com.hierynomus.smbj.io.InputStreamByteChunkProvider import com.hierynomus.smbj.session.Session import com.hierynomus.smbj.share.Directory import com.hierynomus.smbj.share.DiskShare @@ -24,7 +23,6 @@ import com.xayah.core.model.SmbVersion import com.xayah.core.model.database.CloudEntity import com.xayah.core.model.database.SMBExtra import com.xayah.core.network.R -import com.xayah.core.network.io.CountingInputStreamImpl import com.xayah.core.network.io.CountingOutputStreamImpl import com.xayah.core.network.util.getExtraEntity import com.xayah.core.rootservice.parcelables.PathParcelable @@ -40,7 +38,6 @@ import com.xayah.libpickyou.parcelables.FileParcelable import com.xayah.libpickyou.ui.PickYouLauncher import com.xayah.libpickyou.ui.model.PickerType import java.io.File -import java.io.FileInputStream import java.io.IOException @@ -203,14 +200,16 @@ class SMBClientImpl(private val entity: CloudEntity, private val extra: SMBExtra val dstPath = "$dst/$name" log { "upload: $src to $dstPath" } val dstFile = openFile(dstPath) + val dstStream = dstFile.outputStream val srcFile = File(src) val srcFileSize = srcFile.length() - val srcInputStream = FileInputStream(srcFile) - val countingStream = CountingInputStreamImpl(srcInputStream, srcFileSize) { read, total -> onUploading(read, total) } - dstFile.write(InputStreamByteChunkProvider(countingStream)) + val srcInputStream = srcFile.inputStream() + val countingStream = CountingOutputStreamImpl(dstStream, srcFileSize, onUploading) + srcInputStream.copyTo(countingStream) srcInputStream.close() countingStream.close() dstFile.close() + if (countingStream.byteCount == 0L) throw IOException("Failed to write remote file: 0 byte.") onUploading(countingStream.byteCount, countingStream.byteCount) }