Repository: mina-sshd
Updated Branches:
  refs/heads/master da410fcd5 -> 93d3aea6d


[SSHD-402] SCP does not truncate existing files before overwriting.

Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/93d3aea6
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/93d3aea6
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/93d3aea6

Branch: refs/heads/master
Commit: 93d3aea6d6c7959ed6c0dd70cc5c4270f6238144
Parents: da410fc
Author: Guillaume Nodet <[email protected]>
Authored: Tue Jan 13 15:41:37 2015 +0100
Committer: Guillaume Nodet <[email protected]>
Committed: Tue Jan 13 15:41:37 2015 +0100

----------------------------------------------------------------------
 .../org/apache/sshd/common/scp/ScpHelper.java   |  3 ++
 .../src/test/java/org/apache/sshd/ScpTest.java  | 29 ++++++++++++++++++++
 2 files changed, 32 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/93d3aea6/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java 
b/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
index ea9911d..af4daa5 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
@@ -211,6 +211,9 @@ public class ScpHelper {
         } else if (file.doesExist() && !file.isWritable()) {
             throw new IOException("Can not write to file: " + file);
         }
+        if (file.doesExist()) {
+            file.truncate();
+        }
         OutputStream os = file.createOutputStream(0);
         try {
             ack();

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/93d3aea6/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ScpTest.java 
b/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
index 0c6679b..a8b6504 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
@@ -124,6 +124,35 @@ public class ScpTest extends BaseTest {
     }
 
     @Test
+    public void testScpUploadOverwrite() throws Exception {
+        SshClient client = SshClient.setUpDefaultClient();
+        client.start();
+        ClientSession session = client.connect("test", "localhost", 
port).await().getSession();
+        session.addPasswordIdentity("test");
+        session.auth().verify();
+
+        ScpClient scp = session.createScpClient();
+
+        String data = "0123456789\n";
+
+        File root = new File("target/scp");
+        Utils.deleteRecursive(root);
+        root.mkdirs();
+        new File(root, "local").mkdirs();
+        assertTrue(root.exists());
+
+        new File(root, "remote").mkdirs();
+        writeFile(new File("target/scp/remote/out.txt"), data + data);
+
+        writeFile(new File("target/scp/local/out.txt"), data);
+        scp.upload("target/scp/local/out.txt", "target/scp/remote/out.txt");
+        assertFileLength(new File("target/scp/remote/out.txt"), data.length(), 
5000);
+
+        session.close(false).await();
+        client.stop();
+    }
+
+    @Test
     public void testScpNativeOnSingleFile() throws Exception {
         SshClient client = SshClient.setUpDefaultClient();
         client.start();

Reply via email to