3

I am trying to download a file using apache-commons-vfs2 (V2.6.0)

I can access the site using WinSCP so all the credentials are correct but I get a NumberFormatException error when I run the following Java program. I only want to download files from this host.

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.VFS;

public class TestVFS {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {
            FileSystemManager manager = VFS.getManager();

            System.out.println("User directory = " + System.getProperty("user.dir"));
            FileObject local = manager.resolveFile(
                    System.getProperty("user.dir") + "/" + "vfsFile.txt");
            FileObject remote = manager.resolveFile(
                    "sftp://" + "user" + ":" + "pass" + "@" + "host" + "/" + "file");

            local.copyFrom(remote, Selectors.SELECT_SELF);

            local.close();
            remote.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

It generates the following exception

User directory = C:\work\neon\TestProject
For input string: "id -u"
java.lang.NumberFormatException: For input string: "id -u"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at org.apache.commons.vfs2.provider.sftp.SftpFileSystem.getUId(SftpFileSystem.java:281)
    at org.apache.commons.vfs2.provider.sftp.SftpFileSystem.detectExecDisabled(SftpFileSystem.java:344)
    at org.apache.commons.vfs2.provider.sftp.SftpFileSystem.<init>(SftpFileSystem.java:94)
    at org.apache.commons.vfs2.provider.sftp.SftpFileProvider.doCreateFileSystem(SftpFileProvider.java:93)
    at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.getFileSystem(AbstractOriginatingFileProvider.java:93)
    at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:72)
    at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:56)
    at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:717)
    at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:683)
    at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:638)
    at TestVFS.main(TestVFS.java:18)

The sFTP server is CompleteFTP server running on a Windows Server environment I believe.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
JohnM
  • 31
  • 4

1 Answers1

1

This is a bug in Apache Commons VFS. It looks like it will be fixed in commons-vfs 2.7.0, which has not yet been released as I write this.

Commons-vfs is trying to get the numeric user ID which the remote SFTP session is running under, by running the command "id -u" on the remote server. The pre-2.7 logic expects this command to either return a non-zero exit code indicating it failed, or output a number and exit with code 0. Judging from the error that you're getting, your SFTP server is outputting the string "id -u" instead of a number.

You could try to reproduce the behavior interactively by running:

ssh user@host 'id -u'

and see what kind of output you can get.

It appears that commons-vfs 2.7.0 (when it's released) will correctly handle the case of "id -u" not returning a number. It will also have an option to disable running this command or detecting the remote UID in the first place.

In the meantime, you could consider building your own copy of commons-vfs from source. Or look into making the SFTP server respond differently when a client tries to run the command "id -u".

Kenster
  • 23,465
  • 21
  • 80
  • 106
  • Thanks, @Kenster - I built a V2.7.0 snapshot and disabledExecDetection and that sorted it. I have no admin access to the SFTP server so couldn't go that route. – JohnM Apr 29 '20 at 08:06
  • It is released now. – eckes Nov 05 '20 at 21:39