[ 
https://issues.apache.org/jira/browse/VFS-651?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16366636#comment-16366636
 ] 

Syed Aqeel Ashiq edited comment on VFS-651 at 2/16/18 6:26 AM:
---------------------------------------------------------------

[~ottobackwards] Did you read the comments in the code snippet I shared? In 
comments I mentioned that setting userDirIsRoot = true and giving relative path 
relative to user's home directory can work as a hack-y workaround. But it has 
two problems:
1. When you set userDirIsRoot = true, although it works as a workaround, it 
means that you are saying that user directory is root directory. Which is 
logically incorrect, since user directory is not root directory in my case. 
Rather user dir is "/sftp", and not root "/".
2. It does not work when let's say user default dir is /sftp/abc/def. In that 
case, if you set userDirIsRoot = true, then we can never upload to /sftp/abc or 
/sftp.

So, I would say, even if you can make it to work in some cases by incorrectly 
using userDirIsRoot = true, even then it doesn't change the fact that it is a 
flawed design in api(if not a bug) and should be corrected.


was (Author: aqeel613):
[~ottobackwards] Did you read the comments in the code snippet I shared? In 
comments I mentioned that setting userDirIsRoot = true and giving relative path 
relative to user's home directory does work. But it has two problems:
1. When you set userDirIsRoot = true, although it works as a workaround, it 
means that you are saying that user directory is root directory. Which is 
logically incorrect, since user directory is not root directory in my case. 
Rather user dir is "/sftp", and not root "/".
2. It does not work when let's say user default dir is /sftp/abc/def. In that 
case, if you set userDirIsRoot = true, then we can never upload to /sftp/abc or 
/sftp.

So, I would say, even if you can make it to work in some cases by incorrectly 
using userDirIsRoot = true, even then it doesn't change the fact that it is a 
flawed design in api(if not a bug) and should be corrected.

> SftpFileSystem Should not switch to root directory when not absolutely needed
> -----------------------------------------------------------------------------
>
>                 Key: VFS-651
>                 URL: https://issues.apache.org/jira/browse/VFS-651
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Syed Aqeel Ashiq
>            Priority: Major
>             Fix For: 2.3
>
>
> Consider a user X only has read/write access to let's say /sftp and /sftp/abc 
> directory on a sftp server. And default directory for user is /sftp
> In this case, we have to set userDirIsRoot to false, and thus vfs will try to 
> switch to root directory, which will fail due to lack of read permission.
> Consider following code:
> {code:java}
>   public void uploadFile(String localFilePath) throws FileSystemException {   
>  
>     FileSystemManager manager = VFS.getManager();
>     FileObject localFile = manager.resolveFile(localFilePath);
>     FileSystemOptions opts = new FileSystemOptions();
>     SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);
>     // I set it to false,
>     // because user default dir is /sftp and it is obviously not root 
> directory.
>     // As a workaround, If I set it to true, it will work after also tweaking 
> with
>     // the file path while resolving a file, but semantics will be wrong, 
> since 
>     // the user directory is not root.
>     SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 90000);
>     String remoteFilePath = 
> "sftp://myuser:myp...@myurl.sftpexample.com/sftp/abc/abc.txt";;
>     // Exception at the following line
>     FileObject remoteFile = manager.resolveFile(remoteFilePath, opts);
>     // Exception at the line above line. Because this line tries to switch to 
>     // root directory of sftp file system, whichfails due to lack of read 
>     // permission on root directory. Switching to root directory is not 
>     // needed here at all. This is bug, although it works in most scenarios,
>     // since most of times, permissions are available.
>     remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
>   }
> {code}
> This is the underlying code responsible in
> {code:java}
> org.apache.commons.vfs.provider.sftp.SftpFileSystem
> {code}
> :
> {code:java}
> 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);
>     }
> }{code}
> It purposelessly switches to root directory of filesystem. There is a fair 
> use-case that root directory doesn't have read access.
> *Possible Fix:* It should not switch to root directory, rather it should 
> switch to actual final directory. This approach would be the safest. E.g. if 
> the needed directory is '/sftp/abc' then it can switch to that directory in 
> above code, rather than switching to root.
> Please also see related SO question:
> https://stackoverflow.com/questions/48709971/why-apache-vfs-sftp-tries-to-switch-to-root-directory-even-when-not-needed



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to