1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-03 14:25:37 +02:00

Fix 142064 quote filenames for cp and mv operations

This commit is contained in:
Martin Oberhuber 2006-05-17 16:08:47 +00:00
parent 5dc7980fd2
commit 9219c76a0c

View file

@ -19,14 +19,14 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.rse.services.clientserver.NamePatternMatcher;
import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
import org.eclipse.rse.services.files.AbstractFileService;
import org.eclipse.rse.services.files.IFileService;
import org.eclipse.rse.services.files.IHostFile;
import org.eclipse.rse.services.files.IHostFile;
import org.eclipse.rse.services.ssh.ISshService;
import org.eclipse.rse.services.ssh.ISshSessionProvider;
@ -406,14 +406,63 @@ public class SftpFileService extends AbstractFileService implements IFileService
return result;
}
private static Pattern fValidShellPattern = Pattern.compile("[a-zA-Z0-9._/]*"); //$NON-NLS-1$
/**
* Quotes a string such that it can be used in a remote UNIX shell.
* On Windows, special characters likes quotes and dollar sign. and
* - most importantly - the backslash will not be quoted correctly.
*
* Newline is only quoted correctly in tcsh. But since this is mainly
* intended for file names, it should work OK in almost every case.
*
* @param s String to be quoted
* @return quoted string, or original if no quoting was necessary.
*/
public static String enQuote(String s) {
if(fValidShellPattern.matcher(s).matches()) {
return s;
} else {
StringBuffer buf = new StringBuffer(s.length()+16);
buf.append('"');
for(int i=0; i<s.length(); i++) {
char c=s.charAt(i);
switch(c) {
case '$':
//Need to treat specially to work in both bash and tcsh:
//close the quote, insert quoted $, reopen the quote
buf.append('"');
buf.append('\\');
buf.append('$');
buf.append('"');
break;
case '"':
case '\\':
case '\'':
case '`':
case '\n':
//just quote it. The newline will work in tcsh only -
//bash replaces it by the empty string. But newlines
//in filenames are an academic issue, hopefully.
buf.append('\\');
buf.append(c);
break;
default:
buf.append(c);
}
}
buf.append('"');
return buf.toString();
}
}
public boolean move(IProgressMonitor monitor, String srcParent, String srcName, String tgtParent, String tgtName) throws SystemMessageException
{
// move is not supported by sftp directly. Use the ssh shell instead.
// TODO check if newer versions of sftp support move directly
// TODO Interpret some error messages like "command not found" (use ren instead of mv on windows)
String fullPathOld = srcParent + '/' + srcName;
String fullPathNew = tgtParent + '/' + tgtName;
//TODO quote pathes if necessary
// TODO mimic by copy if the remote does not support copying between file systems?
String fullPathOld = enQuote(srcParent + '/' + srcName);
String fullPathNew = enQuote(tgtParent + '/' + tgtName);
int rv = runCommand(monitor, "mv "+fullPathOld+' '+fullPathNew); //$NON-NLS-1$
return (rv==0);
}
@ -422,9 +471,8 @@ public class SftpFileService extends AbstractFileService implements IFileService
// move is not supported by sftp directly. Use the ssh shell instead.
// TODO check if newer versions of sftp support move directly
// TODO Interpret some error messages like "command not found" (use (x)copy instead of cp on windows)
String fullPathOld = srcParent + '/' + srcName; //$NON-NLS-1$
String fullPathNew = tgtParent + '/' + tgtName; //$NON-NLS-1$
//TODO quote pathes if necessary
String fullPathOld = enQuote(srcParent + '/' + srcName); //$NON-NLS-1$
String fullPathNew = enQuote(tgtParent + '/' + tgtName); //$NON-NLS-1$
int rv = runCommand(monitor, "cp "+fullPathOld+' '+fullPathNew); //$NON-NLS-1$
return (rv==0);
}