1
0
Fork 0
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:
Martin Oberhuber 2008-04-02 21:00:03 +00:00
parent 94f38246c5
commit 69c14379e8
2 changed files with 441 additions and 419 deletions

View file

@ -253,7 +253,7 @@ public class SshConnectorService extends StandardConnectorService implements ISs
} }
//TODO avoid having jsch type "Session" in the API. //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 implementing IShellService getShellService()
//and IFileService getFileService(). //and IFileService getFileService().
public Session getSession() { public Session getSession() {
@ -262,8 +262,7 @@ public class SshConnectorService extends StandardConnectorService implements ISs
public String getControlEncoding() { public String getControlEncoding() {
//TODO this code should be in IHost //TODO this code should be in IHost
String encoding = getHost().getDefaultEncoding(false); String encoding = getHost().getDefaultEncoding(true);
if (encoding==null) encoding = getHost().getDefaultEncoding(true);
if (encoding==null) encoding = _defaultEncoding; if (encoding==null) encoding = _defaultEncoding;
//</code to be in IHost> //</code to be in IHost>
return encoding; return encoding;

View file

@ -23,6 +23,7 @@
* David McKnight (IBM) - [209593] [api] add support for "file permissions" and "owner" properties for unix files * 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 * 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 * 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; package org.eclipse.rse.internal.services.ssh.files;
@ -161,16 +162,34 @@ public class SftpFileService extends AbstractFileService implements IFileService
private String fControlEncoding = null; private String fControlEncoding = null;
/** Indicates the default string encoding on this platform */ /** Indicates the default string encoding on this platform */
private static String defaultEncoding = new java.io.InputStreamReader(new java.io.ByteArrayInputStream(new byte[0])).getEncoding(); 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) { // public SftpFileService(SshConnectorService conn) {
// fConnector = conn; // fConnector = conn;
// } // }
public SftpFileService(ISshSessionProvider sessionProvider) { public SftpFileService(ISshSessionProvider sessionProvider) {
fSessionProvider = 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; fControlEncoding = encoding;
} }
@ -183,12 +202,14 @@ public class SftpFileService extends AbstractFileService implements IFileService
protected String recode(String s) throws SystemMessageException { protected String recode(String s) throws SystemMessageException {
if (fControlEncoding==null) { if (fControlEncoding==null) {
return s; return s;
} else if (fControlEncoding.equals(defaultEncoding)) { } else if (fControlEncoding.equals(fJSchChannelEncoding)) {
return s; return s;
} }
try { try {
byte[] bytes = s.getBytes(fControlEncoding); //what we want on the wire 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) { } catch(UnsupportedEncodingException e) {
throw makeSystemMessageException(e); throw makeSystemMessageException(e);
} }
@ -206,8 +227,8 @@ public class SftpFileService extends AbstractFileService implements IFileService
protected String recodeSafe(String s) throws SystemMessageException { protected String recodeSafe(String s) throws SystemMessageException {
try { try {
String recoded = recode(s); String recoded = recode(s);
byte[] bytes = recoded.getBytes(defaultEncoding); byte[] bytes = recoded.getBytes(fJSchChannelEncoding);
String decoded = decode(new String(bytes)); String decoded = decode(new String(bytes, fJSchChannelEncoding));
if (!s.equals(decoded)) { if (!s.equals(decoded)) {
int i=0; int i=0;
int lmax = Math.min(s.length(), decoded.length()); 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)); //String sbad=s.substring(Math.max(i-2,0), Math.min(i+2,lmax));
char sbad = s.charAt(i); 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$ 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)) { if (fControlEncoding == null || fControlEncoding.equals(fJSchChannelEncoding)) {
msg += "default encoding \""+defaultEncoding+"\". "; //$NON-NLS-1$ //$NON-NLS-2$ msg += "default encoding \"" + fJSchChannelEncoding + "\". "; //$NON-NLS-1$ //$NON-NLS-2$
} else { } 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$ msg += "Please specify a different encoding in host properties."; //$NON-NLS-1$
throw new UnsupportedEncodingException(msg); throw new UnsupportedEncodingException(msg);
@ -247,11 +268,13 @@ public class SftpFileService extends AbstractFileService implements IFileService
protected String decode(String s) throws SystemMessageException { protected String decode(String s) throws SystemMessageException {
if (fControlEncoding==null) { if (fControlEncoding==null) {
return s; return s;
} else if (fControlEncoding.equals(defaultEncoding)) { } else if (fControlEncoding.equals(fJSchChannelEncoding)) {
return s; return s;
} }
try { 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); return new String(bytes, fControlEncoding);
} catch(UnsupportedEncodingException e) { } catch(UnsupportedEncodingException e) {
throw makeSystemMessageException(e); throw makeSystemMessageException(e);
@ -272,7 +295,7 @@ public class SftpFileService extends AbstractFileService implements IFileService
StringBuffer buf = new StringBuffer(s.length()+8); StringBuffer buf = new StringBuffer(s.length()+8);
for(int i=0; i<s.length(); i++) { for(int i=0; i<s.length(); i++) {
char c = s.charAt(i); char c = s.charAt(i);
// if(c=='?' || c=='*' || c=='\\') { // if(c=='?' || c=='*' || c=='\\') {
if(c=='?' || c=='*') { if(c=='?' || c=='*') {
buf.append('\\'); buf.append('\\');
} }