mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-24 08:44:01 +02:00
[224799] Fix JSch encoding problems with arabic UTF-8 file names
This commit is contained in:
parent
94f38246c5
commit
69c14379e8
2 changed files with 441 additions and 419 deletions
|
@ -253,7 +253,7 @@ public class SshConnectorService extends StandardConnectorService implements ISs
|
|||
}
|
||||
|
||||
//TODO avoid having jsch type "Session" in the API.
|
||||
//Could be done by instanciating SshShellService and SshFileService here,
|
||||
// Could be done by instantiating SshShellService and SshFileService here,
|
||||
//and implementing IShellService getShellService()
|
||||
//and IFileService getFileService().
|
||||
public Session getSession() {
|
||||
|
@ -262,8 +262,7 @@ public class SshConnectorService extends StandardConnectorService implements ISs
|
|||
|
||||
public String getControlEncoding() {
|
||||
//TODO this code should be in IHost
|
||||
String encoding = getHost().getDefaultEncoding(false);
|
||||
if (encoding==null) encoding = getHost().getDefaultEncoding(true);
|
||||
String encoding = getHost().getDefaultEncoding(true);
|
||||
if (encoding==null) encoding = _defaultEncoding;
|
||||
//</code to be in IHost>
|
||||
return encoding;
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
* David McKnight (IBM) - [209593] [api] add support for "file permissions" and "owner" properties for unix files
|
||||
* Martin Oberhuber (Wind River) - [216343] immediate link targets and canonical paths for Sftp
|
||||
* David McKnight (IBM) - [216252] [api][nls] Resource Strings specific to subsystems should be moved from rse.ui into files.ui / shells.ui / processes.ui where possible
|
||||
* Martin Oberhuber (Wind River) - [224799] Fix JSch encoding problems with Arabic filenames
|
||||
*******************************************************************************/
|
||||
|
||||
package org.eclipse.rse.internal.services.ssh.files;
|
||||
|
@ -161,16 +162,34 @@ public class SftpFileService extends AbstractFileService implements IFileService
|
|||
private String fControlEncoding = null;
|
||||
/** Indicates the default string encoding on this platform */
|
||||
private static String defaultEncoding = new java.io.InputStreamReader(new java.io.ByteArrayInputStream(new byte[0])).getEncoding();
|
||||
/** Indicates the encoding that our JSch channel uses */
|
||||
private String fJSchChannelEncoding = defaultEncoding;
|
||||
|
||||
// public SftpFileService(SshConnectorService conn) {
|
||||
// fConnector = conn;
|
||||
// }
|
||||
// public SftpFileService(SshConnectorService conn) {
|
||||
// fConnector = conn;
|
||||
// }
|
||||
|
||||
public SftpFileService(ISshSessionProvider sessionProvider) {
|
||||
fSessionProvider = sessionProvider;
|
||||
}
|
||||
|
||||
public void setControlEncoding(String encoding) {
|
||||
public void setControlEncoding(String encoding) throws SystemMessageException {
|
||||
try {
|
||||
fChannelSftp.setFilenameEncoding(encoding);
|
||||
fJSchChannelEncoding = encoding;
|
||||
} catch (NoSuchMethodError e) {
|
||||
// Fallback for JSch < 0.1.34: use recode() for encoding conversion
|
||||
fControlEncoding = encoding;
|
||||
fJSchChannelEncoding = defaultEncoding;
|
||||
} catch (SftpException e) {
|
||||
try {
|
||||
fChannelSftp.setFilenameEncoding("UTF-8"); //$NON-NLS-1$
|
||||
fJSchChannelEncoding = "UTF-8"; //$NON-NLS-1$
|
||||
} catch (SftpException enest) {
|
||||
// should not happen, are we not connected?
|
||||
throw makeSystemMessageException(enest);
|
||||
}
|
||||
}
|
||||
fControlEncoding = encoding;
|
||||
}
|
||||
|
||||
|
@ -183,12 +202,14 @@ public class SftpFileService extends AbstractFileService implements IFileService
|
|||
protected String recode(String s) throws SystemMessageException {
|
||||
if (fControlEncoding==null) {
|
||||
return s;
|
||||
} else if (fControlEncoding.equals(defaultEncoding)) {
|
||||
} else if (fControlEncoding.equals(fJSchChannelEncoding)) {
|
||||
return s;
|
||||
}
|
||||
try {
|
||||
byte[] bytes = s.getBytes(fControlEncoding); //what we want on the wire
|
||||
return new String(bytes); //what we need to tell Jsch to get this on the wire
|
||||
return new String(bytes, fJSchChannelEncoding); // what we need to
|
||||
// tell Jsch to get
|
||||
// this on the wire
|
||||
} catch(UnsupportedEncodingException e) {
|
||||
throw makeSystemMessageException(e);
|
||||
}
|
||||
|
@ -206,8 +227,8 @@ public class SftpFileService extends AbstractFileService implements IFileService
|
|||
protected String recodeSafe(String s) throws SystemMessageException {
|
||||
try {
|
||||
String recoded = recode(s);
|
||||
byte[] bytes = recoded.getBytes(defaultEncoding);
|
||||
String decoded = decode(new String(bytes));
|
||||
byte[] bytes = recoded.getBytes(fJSchChannelEncoding);
|
||||
String decoded = decode(new String(bytes, fJSchChannelEncoding));
|
||||
if (!s.equals(decoded)) {
|
||||
int i=0;
|
||||
int lmax = Math.min(s.length(), decoded.length());
|
||||
|
@ -217,10 +238,10 @@ public class SftpFileService extends AbstractFileService implements IFileService
|
|||
//String sbad=s.substring(Math.max(i-2,0), Math.min(i+2,lmax));
|
||||
char sbad = s.charAt(i);
|
||||
String msg = "Cannot express character \'"+sbad+"\'(0x"+Integer.toHexString(sbad) +") with "; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
if (fControlEncoding==null || fControlEncoding.equals(defaultEncoding)) {
|
||||
msg += "default encoding \""+defaultEncoding+"\". "; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
if (fControlEncoding == null || fControlEncoding.equals(fJSchChannelEncoding)) {
|
||||
msg += "default encoding \"" + fJSchChannelEncoding + "\". "; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
} else {
|
||||
msg += "encoding \""+fControlEncoding+"\" over local default encoding \""+defaultEncoding+"\". "; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
msg += "encoding \"" + fControlEncoding + "\" over local default encoding \"" + fJSchChannelEncoding + "\". "; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
}
|
||||
msg += "Please specify a different encoding in host properties."; //$NON-NLS-1$
|
||||
throw new UnsupportedEncodingException(msg);
|
||||
|
@ -247,11 +268,13 @@ public class SftpFileService extends AbstractFileService implements IFileService
|
|||
protected String decode(String s) throws SystemMessageException {
|
||||
if (fControlEncoding==null) {
|
||||
return s;
|
||||
} else if (fControlEncoding.equals(defaultEncoding)) {
|
||||
} else if (fControlEncoding.equals(fJSchChannelEncoding)) {
|
||||
return s;
|
||||
}
|
||||
try {
|
||||
byte[] bytes = s.getBytes(); //original bytes sent by SSH
|
||||
byte[] bytes = s.getBytes(fJSchChannelEncoding); // original
|
||||
// bytes sent by
|
||||
// SSH
|
||||
return new String(bytes, fControlEncoding);
|
||||
} catch(UnsupportedEncodingException e) {
|
||||
throw makeSystemMessageException(e);
|
||||
|
@ -272,7 +295,7 @@ public class SftpFileService extends AbstractFileService implements IFileService
|
|||
StringBuffer buf = new StringBuffer(s.length()+8);
|
||||
for(int i=0; i<s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
// if(c=='?' || c=='*' || c=='\\') {
|
||||
// if(c=='?' || c=='*' || c=='\\') {
|
||||
if(c=='?' || c=='*') {
|
||||
buf.append('\\');
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue