Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Nov 8, 2007
1 parent 5efa953 commit 2734f11
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
31 changes: 29 additions & 2 deletions source/ch/cyberduck/core/sftp/SFTPPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public void download(BandwidthThrottle throttle, StreamListener listener) {
if(Preferences.instance().getProperty("ssh.transfer").equals(Session.SCP)) {
SCPClient scp = session.openScp();
scp.setCharset(session.getEncoding());
in = scp.get(this.getAbsolute());
in = scp.get(this.escape(this.getAbsolute()));
}
this.download(in, out, throttle, listener);
}
Expand Down Expand Up @@ -685,7 +685,7 @@ public void upload(BandwidthThrottle throttle, StreamListener listener) {
SCPClient scp = session.openScp();
scp.setCharset(session.getEncoding());
out = scp.put(this.getName(), (long)this.getLocal().attributes.getSize(),
this.getParent().getAbsolute(),
this.escape(this.getParent().getAbsolute()),
"0"+p.getOctalString());
}
this.upload(out, in, throttle, listener);
Expand Down Expand Up @@ -752,4 +752,31 @@ public void upload(BandwidthThrottle throttle, StreamListener listener) {
}
}
}

/**
* Escapes metacharacters used in a typical shell
*
* metacharacter
* A character that, when unquoted, separates words. One of the
* following:
* | & ; ( ) < > space tab
* @param path
* @return
*/
private String escape(String path) {
// Escape the 'escape' character in the filname first.
// '\' becomes '\\'. This is a mess because '\' is the escape character for Java itself
path = path.replaceAll("\\\\", "\\\\\\\\");
// Escape all whitespace. ' ' becomes '\ '.
path = path.replaceAll("\\s", "\\\\ ");
path = path.replaceAll("\\|", "\\\\|");
path = path.replaceAll("&", "\\\\&");
path = path.replaceAll(";", "\\\\;");
path = path.replaceAll("'", "\\\\'");
path = path.replaceAll("\\(", "\\\\(");
path = path.replaceAll("\\)", "\\\\)");
path = path.replaceAll("<", "\\\\<");
path = path.replaceAll(">", "\\\\>");
return path;
}
}
33 changes: 2 additions & 31 deletions source/ch/ethz/ssh2/SCPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ public SCPOutputStream put(final String remoteFile, long length, String remoteTa
if (Character.isDigit(mode.charAt(i)) == false)
throw new IllegalArgumentException("Invalid mode.");

remoteTargetDirectory = this.escape(remoteTargetDirectory.trim());
remoteTargetDirectory = (remoteTargetDirectory.length() > 0) ? remoteTargetDirectory : ".";

String cmd = "scp -t -d " + remoteTargetDirectory;
Expand All @@ -227,43 +226,15 @@ public SCPInputStream get(final String remoteFile) throws IOException
if (null == remoteFile)
throw new IllegalArgumentException("Null argument.");

String escapedRemoteFile = this.escape(remoteFile.trim());

if (escapedRemoteFile.length() == 0)
if (remoteFile.length() == 0)
throw new IllegalArgumentException("Cannot accept empty filename.");

String cmd = "scp -f";
cmd += (" " + escapedRemoteFile);
cmd += (" " + remoteFile);

sess = conn.openSession();
sess.execCommand(cmd, charsetName);

return new SCPInputStream(this, sess);
}

/**
* Escapes metacharacters used in a typical shell
*
* metacharacter
* A character that, when unquoted, separates words. One of the
* following:
* | & ; ( ) < > space tab
* @param path
* @return
*/
private String escape(String path) {
// Escape the 'escape' character in the filname first.
// '\' becomes '\\'. This is a mess because '\' is the escape character for Java itself
path = path.replaceAll("\\\\", "\\\\\\\\");
// Escape all whitespace. ' ' becomes '\ '.
path = path.replaceAll("\\s", "\\\\ ");
path = path.replaceAll("\\|", "\\\\|");
path = path.replaceAll("&", "\\\\&");
path = path.replaceAll(";", "\\\\;");
path = path.replaceAll("\\(", "\\\\(");
path = path.replaceAll("\\)", "\\\\)");
path = path.replaceAll("<", "\\\\<");
path = path.replaceAll(">", "\\\\>");
return path;
}
}

0 comments on commit 2734f11

Please sign in to comment.