You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2015/12/14 06:34:23 UTC

ant git commit: preserveLastModified for scp uploads

Repository: ant
Updated Branches:
  refs/heads/master 9de4ec709 -> 881a0ff79


preserveLastModified for scp uploads

Contributed by J. Hoffmann


Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/881a0ff7
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/881a0ff7
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/881a0ff7

Branch: refs/heads/master
Commit: 881a0ff79e095227ce00c7deefc8054f60fefa2f
Parents: 9de4ec7
Author: Stefan Bodewig <bo...@apache.org>
Authored: Mon Dec 14 06:33:26 2015 +0100
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Mon Dec 14 06:33:26 2015 +0100

----------------------------------------------------------------------
 .../tools/ant/taskdefs/optional/ssh/Scp.java    |  9 +--
 .../ant/taskdefs/optional/ssh/ScpToMessage.java | 72 +++++++++++++++++---
 2 files changed, 69 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/881a0ff7/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java
index 1cf1e2c..dd37a2b 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java
@@ -124,8 +124,8 @@ public class Scp extends SSHBase {
     }
 
     /**
-     * Sets flag to determine if file timestamp from
-     * remote system is to be preserved during copy.
+     * Sets flag to determine if file timestamp
+     * is to be preserved during copy.
      * @since Ant 1.8.0
      */
     public void setPreservelastmodified(final boolean yesOrNo) {
@@ -340,7 +340,7 @@ public class Scp extends SSHBase {
                 ScpToMessage message = null;
                 if (!isSftp) {
                     message = new ScpToMessage(getVerbose(), session,
-                                               list, file);
+                                               list, file, preserveLastModified);
                 } else {
                     message = new ScpToMessageBySftp(getVerbose(), session,
                                                      list, file);
@@ -372,7 +372,8 @@ public class Scp extends SSHBase {
             if (!isSftp) {
                 message =
                     new ScpToMessage(getVerbose(), session,
-                                     getProject().resolveFile(fromPath), file);
+                                     getProject().resolveFile(fromPath), file,
+                                     preserveLastModified);
             } else {
                 message =
                     new ScpToMessageBySftp(getVerbose(), session,

http://git-wip-us.apache.org/repos/asf/ant/blob/881a0ff7/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java
index 5ba6b55..a042c4a 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java
@@ -44,6 +44,7 @@ public class ScpToMessage extends AbstractSshMessage {
     private String remotePath;
     private List directoryList;
     private Integer fileMode, dirMode;
+    private boolean preserveLastModified;
 
     /**
      * Constructor for ScpToMessage
@@ -69,15 +70,17 @@ public class ScpToMessage extends AbstractSshMessage {
      * @param session the scp session to use
      * @param aLocalFile the local file
      * @param aRemotePath the remote path
-     * @since Ant 1.6.2
+     * @param preserveLastModified whether to preserve the last modified timestamps
+     * @since Ant 1.9.7
      */
     public ScpToMessage(final boolean verbose,
                         final Session session,
                         final File aLocalFile,
-                        final String aRemotePath) {
+                        final String aRemotePath,
+                        final boolean preserveLastModified) {
         this(verbose, session, aRemotePath);
-
         this.localFile = aLocalFile;
+        this.preserveLastModified = preserveLastModified;
     }
 
     /**
@@ -86,15 +89,47 @@ public class ScpToMessage extends AbstractSshMessage {
      * @param session the scp session to use
      * @param aDirectoryList a list of directories
      * @param aRemotePath the remote path
-     * @since Ant 1.6.2
+     * @param preserveLastModified whether to preserve the last modified timestamps
+     * @since Ant 1.9.7
      */
     public ScpToMessage(final boolean verbose,
                         final Session session,
                         final List aDirectoryList,
-                        final String aRemotePath) {
+                        final String aRemotePath,
+                        final boolean preserveLastModified) {
         this(verbose, session, aRemotePath);
-
         this.directoryList = aDirectoryList;
+        this.preserveLastModified = preserveLastModified;
+    }
+
+    /**
+     * Constructor for a local file to remote.
+     * @param verbose if true do verbose logging
+     * @param session the scp session to use
+     * @param aLocalFile the local file
+     * @param aRemotePath the remote path
+     * @since Ant 1.6.2
+     */
+    public ScpToMessage(final boolean verbose,
+                        final Session session,
+                        final File aLocalFile,
+                        final String aRemotePath) {
+        this(verbose, session, aLocalFile, aRemotePath, false);
+    }
+
+    /**
+     * Constructor for a local directories to remote.
+     * @param verbose if true do verbose logging
+     * @param session the scp session to use
+     * @param aDirectoryList a list of directories
+     * @param aRemotePath the remote path
+     * @since Ant 1.6.2
+     */
+    public ScpToMessage(final boolean verbose,
+                        final Session session,
+                        final List aDirectoryList,
+                        final String aRemotePath) {
+        this(verbose, session, aDirectoryList, aRemotePath, false);
     }
 
     /**
@@ -152,7 +187,7 @@ public class ScpToMessage extends AbstractSshMessage {
     }
 
     private void doSingleTransfer() throws IOException, JSchException {
-        final String cmd = "scp -t " + remotePath;
+        final String cmd = "scp -t " + (getPreserveLastModified() ? "-p " : "") + remotePath;
         final Channel channel = openExecChannel(cmd);
         try {
 
@@ -171,7 +206,10 @@ public class ScpToMessage extends AbstractSshMessage {
     }
 
     private void doMultipleTransfer() throws IOException, JSchException {
-        final Channel channel = openExecChannel("scp -r -d -t " + remotePath);
+        final Channel channel =
+            openExecChannel("scp -r -d -t "
+                            + (getPreserveLastModified() ? "-p " : "")
+                            + remotePath);
         try {
             final OutputStream out = channel.getOutputStream();
             final InputStream in = channel.getInputStream();
@@ -226,6 +264,16 @@ public class ScpToMessage extends AbstractSshMessage {
                                    final OutputStream out) throws IOException {
         // send "C0644 filesize filename", where filename should not include '/'
         final long filesize = localFile.length();
+
+        if (getPreserveLastModified()) {
+            String command = "T" + (localFile.lastModified() / 1000) + " 0";
+            command += " " + (localFile.lastModified() / 1000) + " 0\n";
+            out.write(command.getBytes());
+            out.flush();
+
+            waitForAck(in);
+        }
+
         String command = "C0";
         command += Integer.toOctalString(getFileMode());
         command += " " + filesize + " ";
@@ -328,4 +376,12 @@ public class ScpToMessage extends AbstractSshMessage {
         return dirMode != null ? dirMode.intValue() : DEFAULT_DIR_MODE;
     }
 
+    /**
+     * Whether to preserve the last modified time.
+     * @since Ant 1.9.7
+     */
+    public boolean getPreserveLastModified() {
+        return preserveLastModified;
+    }
+
 }