8

I have a problem with Commons VFS. I want to connect to a directory using SFTP and list it. Here is the code:

FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
FileSystemManager fsManager = VFS.getManager();

FileObject sourceDir = fsManager.resolveFile(sourceUrl, opts);
FileObject targetDir = fsManager.resolveFile(config.get("to"));

for (FileObject sourceFile : sourceDir.getChildren()) { // here is the problem
    FileObject targetFile = fsManager.resolveFile(targetDir + "/" + nodeName + "_"
            + sourceFile.getName().getBaseName());
    logger.debug("Copying files. Source: " + sourceFile.getName().getPath() + " Target: "
            + targetFile.getName().getPath());
}

It seems that the fsManager resolves it correctly but getChildren() fails. I found out that the sourceDir is an IMAGINARY type; the following code says it

logger.debug(sourceDir.getType());

I checked the file and it's an ordinary folder - not a link or something like that.

Error

org.apache.commons.vfs2.FileNotFolderException: Could not list the contents of "sftp://path/to/dir" because it is not a folder.
    at org.apache.commons.vfs2.provider.AbstractFileObject.getChildren(AbstractFileObject.java:693)
    at pkg.services.impl.QuartzJobEventLog.downloadEventLogs(QuartzJobEventLog.java:64)
    at pkg.services.impl.QuartzJobEventLog.executeJob(QuartzJobEventLog.java:37)
    at pkg.services.impl.AbstractQuartzJob.execute(AbstractQuartzJob.java:25)
    at $QuartzJob_1360635dbcd.execute($QuartzJob_1360635dbcd.java)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:203)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
skaffman
  • 398,947
  • 96
  • 818
  • 769
user219882
  • 15,274
  • 23
  • 93
  • 138

4 Answers4

11

Depending on which version of VFS solution will be different. For VFS 2.0 you have to include additional config option below.

SftpFileSystemConfigBuilder.getInstance( ).setUserDirIsRoot(opts, false);
Max
  • 119
  • 2
  • 1
    That wasn't the problem here. Your solution is for situations when you want (especially on unix) access an absolute path. – user219882 Apr 05 '12 at 07:57
  • 3
    I had this exact problem, and setting this config option worked for me. I can now list directory contents via sftp. Apache documentation site is wrong. – Matt Taylor May 12 '13 at 20:36
3

According to the documentation getting "directory contents" is not supported for the SFTP filesystem.

beny23
  • 34,390
  • 5
  • 82
  • 85
1

Faced the same problem when we upgraded the VFS version 1.0 to 2.1 and found that documentation gives to right direction as we need to set below flag to avoid using relative path from user's home directory.

By default, the path is relative to the user's home directory. This can be changed with:

FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, false);

https://commons.apache.org/proper/commons-vfs/filesystems.html#SFTP

0

I was working with FTP and not SFTP protocol but encounterd the same problem as Tomas described. What helped was enabling passive mode

FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);