Skip to content

Commit c49c068

Browse files
committed
GH-721: fix SfpFileSystemProvider.getPath(URI)
Only handle URIs with scheme "sftp" and create a file system for the given URI if none exists already.
1 parent 6bcd715 commit c49c068

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
## Bug Fixes
3131

32+
* [GH-721](https://github.com/apache/mina-sshd/issues/721) SSH client: schedule session timeout checks on demand only
3233
* [GH-807](https://github.com/apache/mina-sshd/issues/807) Handle "verified" flag for sk-* keys
3334
* [GH-809](https://github.com/apache/mina-sshd/pull/809) Fix server-side authentication for FIDO/U2F sk-* keys with flags in `authorized_keys`
3435
* [GH-827](https://github.com/apache/mina-sshd/issues/827) Don't fail on invalid `known_hosts` lines; log and skip them

sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/SftpFileSystemProvider.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public class SftpFileSystemProvider extends FileSystemProvider {
136136

137137
protected final Logger log;
138138

139-
private final SshClient clientInstance;
139+
private SshClient clientInstance;
140140
private final SftpClientFactory factory;
141141
private final SftpVersionSelector versionSelector;
142142
private final SftpErrorDataHandler errorDataHandler;
@@ -183,11 +183,6 @@ public SftpFileSystemProvider(SshClient client, SftpClientFactory factory,
183183
this.factory = factory;
184184
this.versionSelector = selector;
185185
this.errorDataHandler = errorDataHandler;
186-
if (client == null) {
187-
// TODO: make this configurable using system properties
188-
client = SshClient.setUpDefaultClient();
189-
client.start();
190-
}
191186
this.clientInstance = client;
192187
}
193188

@@ -204,10 +199,20 @@ public SftpErrorDataHandler getSftpErrorDataHandler() {
204199
return errorDataHandler;
205200
}
206201

207-
public final SshClient getClientInstance() {
202+
public final synchronized SshClient getClientInstance() {
203+
if (clientInstance == null) {
204+
clientInstance = createClient();
205+
}
208206
return clientInstance;
209207
}
210208

209+
private SshClient createClient() {
210+
// TODO: make this configurable using system properties
211+
SshClient client = SshClient.setUpDefaultClient();
212+
client.start();
213+
return client;
214+
}
215+
211216
public SftpClientFactory getSftpClientFactory() {
212217
return factory;
213218
}
@@ -561,10 +566,30 @@ public SftpFileSystem getFileSystem(String id) {
561566
}
562567
}
563568

569+
private SftpFileSystem getOrCreateFileSystem(URI uri) throws IOException {
570+
String id = getFileSystemIdentifier(uri);
571+
synchronized (fileSystems) {
572+
SftpFileSystem fs = fileSystems.get(id);
573+
if (fs == null) {
574+
fs = newFileSystem(uri, Collections.emptyMap());
575+
}
576+
return fs;
577+
}
578+
}
579+
564580
@Override
565581
public Path getPath(URI uri) {
566-
FileSystem fs = getFileSystem(uri);
567-
return fs.getPath(uri.getPath());
582+
if (!getScheme().equalsIgnoreCase(uri.getScheme())) {
583+
throw new IllegalArgumentException("Not a " + getScheme() + " URI: " + uri);
584+
}
585+
try {
586+
FileSystem fs = getOrCreateFileSystem(uri);
587+
return fs.getPath(uri.getPath());
588+
} catch (IOException e) {
589+
FileSystemNotFoundException fe = new FileSystemNotFoundException("No file system for URI " + uri);
590+
fe.initCause(e);
591+
throw fe;
592+
}
568593
}
569594

570595
@Override

0 commit comments

Comments
 (0)