bz-64742 Fix SCP task (with sftp=true) to correctly parse the remote directory when fetching from root directory
diff --git a/WHATSNEW b/WHATSNEW
index f17bf4b..1c6e28c 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -1,5 +1,11 @@
 Changes from Ant 1.10.9 TO Ant 1.10.10
 ======================================
+Fixed bugs:
+-----------
+
+ * SCP (with sftp=true) task would fail if fetching file located in root directory
+   Bugzilla Report 64742
+
 
 Changes from Ant 1.10.8 TO Ant 1.10.9
 =====================================
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessageBySftp.java b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessageBySftp.java
index 0ad0899..7e5edab 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessageBySftp.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessageBySftp.java
@@ -134,9 +134,16 @@
                         final String remoteFile,
                         final File localFile) throws SftpException {
         String pwd = remoteFile;
-        if (remoteFile.lastIndexOf('/') != -1) {
+        final int lastIndexOfFileSeparator = remoteFile.lastIndexOf('/');
+        if (lastIndexOfFileSeparator != -1) {
             if (remoteFile.length() > 1) {
-                pwd = remoteFile.substring(0, remoteFile.lastIndexOf('/'));
+                if (lastIndexOfFileSeparator == 0) {
+                    // the file path is of the form "/foo....." i.e. the file separator
+                    // occurs at the start (and only there).
+                    pwd = "/";
+                } else {
+                    pwd = remoteFile.substring(0, lastIndexOfFileSeparator);
+                }
             }
         }
         channel.cd(pwd);