[ 
https://issues.apache.org/jira/browse/VFS-620?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Gary Gregory updated VFS-620:
-----------------------------
    Description: 
FileObject.moveTo(FileObject) API doesn't work well for a Linux FTP, especially 
for setting a user directory as its root directory case,
For example, for a ubuntu vsftpd, which is having "/home/user1" as its root 
directory, when renaming "/test/test.txt" to "/test1/test1.txt", it will throw 
an exception.
In this case, it should consider the workingDirectory(would be "/home/user1") 
together and append it to the from/to path to make the API work.

Sample codes,
{code:java}
FileObject fileObject = null;
        FileObject toFileObject = null;
        try {
            StandardFileSystemManager fsManager = new 
StandardFileSystemManager();
            fsManager.init();
            FileSystemOptions fsOpts = new FileSystemOptions();

            FtpFileSystemConfigBuilder.getInstance().setPassiveMode(fsOpts, 
true);
            FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fsOpts, 
true);
            UserAuthenticator auth = new StaticUserAuthenticator(null, 
"<user>", "<password>");
            
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(fsOpts, auth);

            fileObject = fsManager.resolveFile("ftp://<Replaced 
address>/test/test.txt", fsOpts);
            System.out.println("File exists:" + fileObject.exists());

            toFileObject = fsManager.resolveFile("ftp://<Replaced 
address>/test1/test1.txt", fsOpts);
            System.out.println("File exists:" + toFileObject.exists());
            if (!toFileObject.exists()) {
                toFileObject.createFile();
            }
            fileObject.moveTo(toFileObject);
        } catch (FileSystemException ex) {
            ex.printStackTrace();
        } finally {
            if (fileObject != null) {
                try {
                    fileObject.close();
                } catch (FileSystemException ex) {
                }
            }
            if (toFileObject != null) {
                try {
                    toFileObject.close();
                } catch (FileSystemException ex) {
                }
            }
        }
{code}
And the output,

{noformat}
File exists:true
File exists:false
org.apache.commons.vfs2.FileSystemException: Could not rename "ftp://<Replaced 
address>/test/test.txt" to "ftp://<Replaced address>/test1/test1.txt".
        at 
org.apache.commons.vfs2.provider.AbstractFileObject.moveTo(AbstractFileObject.java:1902)
        at TestFTP.main(TestFTP.java:59)
Caused by: org.apache.commons.vfs2.FileSystemException: Could not rename FTP 
file "ftp://<Replaced address>/test/test.txt" to "ftp://<Replaced 
address>/test1/test1.txt".
        at 
org.apache.commons.vfs2.provider.ftp.FtpFileObject.doRename(FtpFileObject.java:524)
        at 
org.apache.commons.vfs2.provider.AbstractFileObject.moveTo(AbstractFileObject.java:1887)
{noformat}

  was:
FileObject.moveTo(FileObject) API doesn't work well for a Linux FTP, especially 
for setting a user directory as its root directory case,
For example, for a ubuntu vsftpd, which is having "/home/user1" as its root 
directory, when renaming "/test/test.txt" to "/test1/test1.txt", it will throw 
an exception.
In this case, it should consider the workingDirectory(would be "/home/user1") 
together and append it to the from/to path to make the API work.

Sample codes,
FileObject fileObject = null;
        FileObject toFileObject = null;
        try {
            StandardFileSystemManager fsManager = new 
StandardFileSystemManager();
            fsManager.init();
            FileSystemOptions fsOpts = new FileSystemOptions();

            FtpFileSystemConfigBuilder.getInstance().setPassiveMode(fsOpts, 
true);
            FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fsOpts, 
true);
            UserAuthenticator auth = new StaticUserAuthenticator(null, 
"<user>", "<password>");
            
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(fsOpts, auth);

            fileObject = fsManager.resolveFile("ftp://<Replaced 
address>/test/test.txt", fsOpts);
            System.out.println("File exists:" + fileObject.exists());

            toFileObject = fsManager.resolveFile("ftp://<Replaced 
address>/test1/test1.txt", fsOpts);
            System.out.println("File exists:" + toFileObject.exists());
            if (!toFileObject.exists()) {
                toFileObject.createFile();
            }
            fileObject.moveTo(toFileObject);
        } catch (FileSystemException ex) {
            ex.printStackTrace();
        } finally {
            if (fileObject != null) {
                try {
                    fileObject.close();
                } catch (FileSystemException ex) {
                }
            }
            if (toFileObject != null) {
                try {
                    toFileObject.close();
                } catch (FileSystemException ex) {
                }
            }
        }

And the output,
File exists:true
File exists:false
org.apache.commons.vfs2.FileSystemException: Could not rename "ftp://<Replaced 
address>/test/test.txt" to "ftp://<Replaced address>/test1/test1.txt".
        at 
org.apache.commons.vfs2.provider.AbstractFileObject.moveTo(AbstractFileObject.java:1902)
        at TestFTP.main(TestFTP.java:59)
Caused by: org.apache.commons.vfs2.FileSystemException: Could not rename FTP 
file "ftp://<Replaced address>/test/test.txt" to "ftp://<Replaced 
address>/test1/test1.txt".
        at 
org.apache.commons.vfs2.provider.ftp.FtpFileObject.doRename(FtpFileObject.java:524)
        at 
org.apache.commons.vfs2.provider.AbstractFileObject.moveTo(AbstractFileObject.java:1887)


> FileObject.moveTo(FileObject) API doesn't work well for a Linux FTP
> -------------------------------------------------------------------
>
>                 Key: VFS-620
>                 URL: https://issues.apache.org/jira/browse/VFS-620
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.1
>         Environment: ubuntu vsftpd
>            Reporter: stevezhuang
>         Attachments: VFS-620c.patch
>
>
> FileObject.moveTo(FileObject) API doesn't work well for a Linux FTP, 
> especially for setting a user directory as its root directory case,
> For example, for a ubuntu vsftpd, which is having "/home/user1" as its root 
> directory, when renaming "/test/test.txt" to "/test1/test1.txt", it will 
> throw an exception.
> In this case, it should consider the workingDirectory(would be "/home/user1") 
> together and append it to the from/to path to make the API work.
> Sample codes,
> {code:java}
> FileObject fileObject = null;
>         FileObject toFileObject = null;
>         try {
>             StandardFileSystemManager fsManager = new 
> StandardFileSystemManager();
>             fsManager.init();
>             FileSystemOptions fsOpts = new FileSystemOptions();
>             FtpFileSystemConfigBuilder.getInstance().setPassiveMode(fsOpts, 
> true);
>             FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fsOpts, 
> true);
>             UserAuthenticator auth = new StaticUserAuthenticator(null, 
> "<user>", "<password>");
>             
> DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(fsOpts, 
> auth);
>             fileObject = fsManager.resolveFile("ftp://<Replaced 
> address>/test/test.txt", fsOpts);
>             System.out.println("File exists:" + fileObject.exists());
>             toFileObject = fsManager.resolveFile("ftp://<Replaced 
> address>/test1/test1.txt", fsOpts);
>             System.out.println("File exists:" + toFileObject.exists());
>             if (!toFileObject.exists()) {
>                 toFileObject.createFile();
>             }
>             fileObject.moveTo(toFileObject);
>         } catch (FileSystemException ex) {
>             ex.printStackTrace();
>         } finally {
>             if (fileObject != null) {
>                 try {
>                     fileObject.close();
>                 } catch (FileSystemException ex) {
>                 }
>             }
>             if (toFileObject != null) {
>                 try {
>                     toFileObject.close();
>                 } catch (FileSystemException ex) {
>                 }
>             }
>         }
> {code}
> And the output,
> {noformat}
> File exists:true
> File exists:false
> org.apache.commons.vfs2.FileSystemException: Could not rename 
> "ftp://<Replaced address>/test/test.txt" to "ftp://<Replaced 
> address>/test1/test1.txt".
>       at 
> org.apache.commons.vfs2.provider.AbstractFileObject.moveTo(AbstractFileObject.java:1902)
>       at TestFTP.main(TestFTP.java:59)
> Caused by: org.apache.commons.vfs2.FileSystemException: Could not rename FTP 
> file "ftp://<Replaced address>/test/test.txt" to "ftp://<Replaced 
> address>/test1/test1.txt".
>       at 
> org.apache.commons.vfs2.provider.ftp.FtpFileObject.doRename(FtpFileObject.java:524)
>       at 
> org.apache.commons.vfs2.provider.AbstractFileObject.moveTo(AbstractFileObject.java:1887)
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to