From 83c8a30a715470ff7208bd217d3373a34ecd7e98 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Sun, 6 Aug 2017 18:09:55 +0200 Subject: [PATCH] Detect data corruption caused by sshfs 3.x and abort transfer --- .../Plugins/SftpPlugin/SimpleSftpServer.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/org/kde/kdeconnect/Plugins/SftpPlugin/SimpleSftpServer.java b/src/org/kde/kdeconnect/Plugins/SftpPlugin/SimpleSftpServer.java index 9fa6571f..e021cb5e 100644 --- a/src/org/kde/kdeconnect/Plugins/SftpPlugin/SimpleSftpServer.java +++ b/src/org/kde/kdeconnect/Plugins/SftpPlugin/SimpleSftpServer.java @@ -48,7 +48,10 @@ import org.kde.kdeconnect.Helpers.RandomHelper; import org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; +import java.io.RandomAccessFile; import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; @@ -215,6 +218,32 @@ class SimpleSftpServer { this.file = file; } + @Override + public OutputStream createOutputStream(long offset) throws IOException { + if (!isWritable()) { + throw new IOException("No write permission : " + file.getName()); + } + + final RandomAccessFile raf = new RandomAccessFile(file, "rw"); + try { + if (offset < raf.length()) { + throw new IOException("Your SSHFS is bugged"); //SSHFS 3.0 and 3.2 cause data corruption, abort the transfer if this happens + } + raf.setLength(offset); + raf.seek(offset); + + return new FileOutputStream(raf.getFD()) { + public void close() throws IOException { + super.close(); + raf.close(); + } + }; + } catch (IOException e) { + raf.close(); + throw e; + } + } + @Override public boolean delete() { //Log.e("Sshd", "deleting file");