mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-07 07:40:55 +03:00
Decouple PathHelper and SFTPEngine, introduce Canonicalizer interface
This commit is contained in:
@@ -21,13 +21,18 @@ public class PathHelper {
|
||||
|
||||
public static final String DEFAULT_PATH_SEPARATOR = "/";
|
||||
|
||||
private final SFTPEngine engine;
|
||||
private final Canonicalizer canonicalizer;
|
||||
private final String pathSep;
|
||||
|
||||
private String dotDir;
|
||||
|
||||
public PathHelper(SFTPEngine engine, String pathSep) {
|
||||
this.engine = engine;
|
||||
public interface Canonicalizer {
|
||||
String canonicalize(String path)
|
||||
throws IOException;
|
||||
}
|
||||
|
||||
public PathHelper(Canonicalizer canonicalizer, String pathSep) {
|
||||
this.canonicalizer = canonicalizer;
|
||||
this.pathSep = pathSep;
|
||||
}
|
||||
|
||||
@@ -56,14 +61,14 @@ public class PathHelper {
|
||||
|
||||
if (lastSlash == -1) // Relative path
|
||||
if (path.equals(".."))
|
||||
return getComponents(canon(path));
|
||||
return getComponents(canonicalizer.canonicalize(path));
|
||||
else
|
||||
return getComponents(getDotDir(), path);
|
||||
|
||||
final String name = path.substring(lastSlash + pathSep.length());
|
||||
|
||||
if (name.equals(".") || name.equals(".."))
|
||||
return getComponents(canon(path));
|
||||
return getComponents(canonicalizer.canonicalize(path));
|
||||
else {
|
||||
final String parent = path.substring(0, lastSlash);
|
||||
return getComponents(parent, name);
|
||||
@@ -72,12 +77,7 @@ public class PathHelper {
|
||||
|
||||
private synchronized String getDotDir()
|
||||
throws IOException {
|
||||
return (dotDir != null) ? dotDir : (dotDir = canon("."));
|
||||
}
|
||||
|
||||
private String canon(String path)
|
||||
throws IOException {
|
||||
return engine.canonicalize(path);
|
||||
return (dotDir != null) ? dotDir : (dotDir = canonicalizer.canonicalize("."));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -61,7 +61,13 @@ public class SFTPEngine
|
||||
sub = ssh.startSession().startSubsystem("sftp");
|
||||
out = sub.getOutputStream();
|
||||
reader = new PacketReader(this);
|
||||
pathHelper = new PathHelper(this, pathSep);
|
||||
pathHelper = new PathHelper(new PathHelper.Canonicalizer() {
|
||||
@Override
|
||||
public String canonicalize(String path)
|
||||
throws IOException {
|
||||
return SFTPEngine.this.canonicalize(path);
|
||||
}
|
||||
}, pathSep);
|
||||
}
|
||||
|
||||
public SFTPEngine init()
|
||||
|
||||
@@ -17,13 +17,20 @@ public class SFTPClientTest {
|
||||
|
||||
@Before
|
||||
public void setPathHelper() throws Exception {
|
||||
PathHelper helper = new PathHelper(sftpEngine, DEFAULT_PATH_SEPARATOR);
|
||||
PathHelper helper = new PathHelper(new PathHelper.Canonicalizer() {
|
||||
@Override
|
||||
public String canonicalize(String path)
|
||||
throws IOException {
|
||||
if (path.equals("."))
|
||||
return "/workingdirectory";
|
||||
return path;
|
||||
}
|
||||
}, DEFAULT_PATH_SEPARATOR);
|
||||
when(sftpEngine.getPathHelper()).thenReturn(helper);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setRemoteWorkingDirectory() throws IOException {
|
||||
when(sftpEngine.canonicalize(".")).thenReturn("/workingdirectory");
|
||||
FileAttributes isADirectory = new FileAttributes.Builder().withType(FileMode.Type.DIRECTORY).build();
|
||||
when(sftpEngine.stat("/workingdirectory")).thenReturn(isADirectory);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user