4

I am using apache VFS for uploading files to sftp server. But one sftp server has a limitation that it's root directory doesn't have any permission. But the directory which is allowed for my application is let's say "/ftp/abc/". This directory is accessible using command line. But Apache VFS first tries to switch to root directory "/" which fails due to lack of permissions in my case.

Edit: Setting userDirIsRoot to true prevents vfs from switching to root directory. But in that case we have to specify path relative to user's root directory. Which means userDirIsRoot is either true or else you can't specify absolute path. This doesn't sound right. Is there a simple way, using which we can specify absolute path, without vfs trying to switch to bare root directory of base filesystem.

Aqeel Ashiq
  • 1,988
  • 5
  • 24
  • 57

1 Answers1

3

The relevant source code is in getChannel() in org.apache.commons.vfs.provider.sftp.SftpFileSystem

Boolean userDirIsRoot = SftpFileSystemConfigBuilder.getInstance().getUserDirIsRoot(getFileSystemOptions());
String workingDirectory = getRootName().getPath();
if (workingDirectory != null && (userDirIsRoot == null || !userDirIsRoot.booleanValue()))
{
    try
    {
        channel.cd(workingDirectory);
    }
    catch (SftpException e)
    {
        throw new FileSystemException("vfs.provider.sftp/change-work-directory.error", workingDirectory);
    }
}

The issue in your case is that getUserDirIsRoot(getFileSystemOptions()) evaluates to true.

Is there anyway to tell Apache VFS library not to switch to root directory?

In order to not enter in the if statement and not invoke channel.cd(workingDirectory), userDirIsRoot.booleanValue() should evaluate to true.

According to the documentation, you can set the SftpFileSystem options using SftpFileSystemConfigBuilder.

Example:

SftpFileSystemConfigBuilder
.getInstance()
.setUserDirIsRoot(options, true);
hovanessyan
  • 30,580
  • 6
  • 55
  • 83
  • Actually, setting it to false is the problem. Setting it to true, fixes my error, since in that case it doesn't tries to switch to root dir.. – Aqeel Ashiq Feb 12 '18 at 08:27
  • Yes I also miss-interpreted this boolean expression - in order to skip the IF the userDirIsRoot should be true – hovanessyan Feb 12 '18 at 08:36
  • I'll add second question then, that is there anyway to prevent vfs to switch to root directory, but still be able to specify absolute directory path. – Aqeel Ashiq Feb 12 '18 at 09:43
  • No this still doesn't work. The problem is in manager.resolveFile(). Whatever path is given in manager.resolveFile method, it tries to switch to root directory first. Which throws error. – Aqeel Ashiq Feb 16 '18 at 13:07
  • Have you tried to debug what happens inside DefaultFileSystemManager resolveFile? I've looked at the source code of VFS 2.2.1-SNAPSHOT and I don't see calls to changing to root directory there. – hovanessyan Feb 17 '18 at 09:42
  • No, it's same in VFS 2.2 – Aqeel Ashiq Feb 19 '18 at 07:01
  • Please see my answer, you can edit your answer to include relevant details. So that we can mark a selected answer and move on. – Aqeel Ashiq Feb 20 '18 at 09:02
  • I've included your answer on top. The workaround looks like the first solution we've debated. – hovanessyan Feb 20 '18 at 09:30
  • Yes, it was your proposal. I meant that you can remove the "now" irrelevant details from the answer so that it is more helpful and easier for others. E.g. the other solutions which didn't work – Aqeel Ashiq Feb 20 '18 at 10:50