lucastheisen / jsch-nio

Java nio FileSystem implementation over SSH
MIT License
99 stars 22 forks source link

Cannot list a folder that I just created #30

Closed albertoandreottiATgmail closed 4 years ago

albertoandreottiATgmail commented 4 years ago

Hello,

I'm trying to run the most basic of the operations, first I create a folder, and then I try to list its contents. It's failing with the following,

21:27:10.982 [main] INFO  com.pastdev.jsch.Slf4jBridge - Next authentication method: keyboard-interactive
21:27:11.407 [main] INFO  com.pastdev.jsch.Slf4jBridge - Authentication succeeded (keyboard-interactive).
Exception in thread "main" com.pastdev.jsch.nio.file.UnixSshFileSystemProvider$UnixSshCommandFailedException: `ls -A -1 "/test336"` failed with exit code -1: stdout='', stderr=''
    at com.pastdev.jsch.nio.file.UnixSshFileSystemProvider.executeForStdout(UnixSshFileSystemProvider.java:248)
    at com.pastdev.jsch.nio.file.UnixSshFileSystemProvider.newDirectoryStream(UnixSshFileSystemProvider.java:342)
    at java.nio.file.Files.newDirectoryStream(Files.java:457)

The same thing happens for a folder that was already there, here's the code,

    import scala.collection.JavaConverters._

    val path = fs.getPath("test336")
    fs.provider().createDirectory(path)
    Files.newDirectoryStream(path).iterator.asScala.foreach(println)

When debugging, I managed to talk directly to the (ChannelSftp)sftp object and call ls(), and it worked. So there's some wrapping that must be broken. Alberto.

lucastheisen commented 4 years ago

@albertoandreottiATgmail , ChannelSftp is using the sftp ssh subsystem, whereas this filesystem implementation uses the exec subsystem. The exec subsystem is actually executing the command:

ls -A -1 "/test336"

as if you actually ran:

ssh user@server ls -A -1 "/test336"

So it could be that your destination does not support ls -A -1, is that possible? It also seems odd that it is /test336, how did you obtain your fs object? Did you set it up as follows?

URI uri = new URI( "ssh.unix://" + username + "@" + hostname + ":" + port + "/home/joe" );
try (FileSystem sshfs = FileSystems.newFileSystem( uri, environment )) {
    Path path = sshfs.getPath( "afile" ); // refers to /home/joe/afile
    try (InputStream inputStream = path.getFileSystem().provider().newInputStream( path )) {
        String fileContents = IOUtils.copyToString( inputStream );
    }
}

If you did, the path should have looked like: /home/joe/test336...

albertoandreottiATgmail commented 4 years ago

Hi Lucas,

thanks for the answer. I tried to connect alternatively using sftp command line utility, and it seems that the "-A" switch is not supported. However, this command,

ssh user@server ls -1 "/test336"

is not working at all. In case this is useful for debugging,

sftp> version SFTP protocol version 3

Alberto.

lucastheisen commented 4 years ago

@albertoandreottiATgmail , are you sure ssh exec subsystem is available? It sounds like your destination server may be sftp subsystem only. That is relatively common scenario... Can you even:

ssh user@server

Does that drop you into a shell of some sort?

This project does not currently support using the sftp subsystem.

albertoandreottiATgmail commented 4 years ago

Hello Lucas,

thanks again for your answer, and no, the shell is not working. Is this something I can ask to be added to the server administrator?

thanks!

lucastheisen commented 4 years ago

@albertoandreottiATgmail , you can ask. It's just a configuration option on the server. But when they only allow sftp, its often for security reasons. It will really be up to them.

I'm gonna close this issue as Invalid given that its simply that you do not have the exec subsystem.