You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ad...@apache.org on 2003/01/21 03:44:57 UTC

cvs commit: jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test ProviderWriteTests.java

adammurdoch    2003/01/20 18:44:57

  Modified:    vfs/src/java/org/apache/commons/vfs/provider/ftp
                        FtpFileObject.java
               vfs/src/test/org/apache/commons/vfs/test
                        ProviderWriteTests.java
  Log:
  Fixed problem when copying files within the same FTP file system.
  
  Patch supplied by Jeff Barrett.
  
  Revision  Changes    Path
  1.10      +57 -1     jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpFileObject.java
  
  Index: FtpFileObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpFileObject.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- FtpFileObject.java	20 Nov 2002 23:55:36 -0000	1.9
  +++ FtpFileObject.java	21 Jan 2003 02:44:57 -0000	1.10
  @@ -60,6 +60,8 @@
   import org.apache.commons.net.ftp.FTPClient;
   import org.apache.commons.net.ftp.FTPFile;
   import org.apache.commons.vfs.FileName;
  +import org.apache.commons.vfs.FileObject;
  +import org.apache.commons.vfs.FileSelector;
   import org.apache.commons.vfs.FileSystemException;
   import org.apache.commons.vfs.FileType;
   import org.apache.commons.vfs.provider.AbstractFileObject;
  @@ -75,7 +77,7 @@
   {
       private static final FTPFile[] EMPTY_FTP_FILE_ARRAY = {};
   
  -    private final FtpFileSystem ftpFs;
  +    private FtpFileSystem ftpFs;
       private final String relPath;
   
       // Cached info
  @@ -91,6 +93,60 @@
           ftpFs = fileSystem;
           relPath = rootName.getRelativeName( name );
       }
  +
  +	/**
  +	 * Copies another file, and all its descendents, to this file.
  +	 *
  +	 * If this file does not exist, it is created.  Its parent folder is also
  +	 * created, if necessary.  If this file does exist, it is deleted first.
  +	 *
  +	 * <p>This method is not transactional.  If it fails and throws an
  +	 * exception, this file will potentially only be partially copied.
  +	 *
  +	 * @param file The source file to copy.
  +	 * @param selector The selector to use to select which files to copy.
  +	 *
  +	 * @throws FileSystemException
  +	 *      If this file is read-only, or if the source file does not exist,
  +	 *      or on error copying the file.
  +	 */
  +	public void copyFrom( final FileObject file, final FileSelector selector )
  +		throws FileSystemException	{
  +		
  +		// We override copyFrom here for the specific case where the source 
  +		// and destination files in the copy are from the same FTPFileSystem.  
  +		// The problem is this can't be done through one FTP session
  +		// concurrently( I think it creates a race condition) So we 
  +		// temporarily create a new one just for use during this copy
  +		// and then return everything to the way it was afterward.
  +		
  +		// if we're copying to and from the same FileSystem
  +		if ( file.getFileSystem().equals( this.getFileSystem() ) ) {
  +			
  +			FtpFileNameParser parser = new FtpFileNameParser();
  +
  +			// save the old file system for later
  +			FtpFileSystem oldFs = this.ftpFs;
  +			
  +			// create a new file system for use temporarily
  +			FtpUri uri = parser.parseFtpUri( this.getURL().toString() );
  +			this.ftpFs = new FtpFileSystem( oldFs.getRoot().getName(),
  +											 uri.getHostName(),
  +											 uri.getUserName(),
  +											 uri.getPassword() ); 
  +
  +			// use our parent's copy functionality
  +			super.copyFrom( file, selector );
  +			
  +			// return the filesystem to the way it was
  +			this.ftpFs.close();
  +			this.ftpFs = oldFs;
  +		}
  +		// otherwise proceed normally
  +		else {
  +			super.copyFrom( file, selector );
  +		}
  +	}
   
       /**
        * Called by child file objects, to locate their ftp file info.
  
  
  
  1.5       +36 -0     jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/ProviderWriteTests.java
  
  Index: ProviderWriteTests.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/ProviderWriteTests.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ProviderWriteTests.java	23 Nov 2002 00:41:10 -0000	1.4
  +++ ProviderWriteTests.java	21 Jan 2003 02:44:57 -0000	1.5
  @@ -258,6 +258,42 @@
       }
   
       /**
  +     * Tests file copy to and from the same filesystem type.  This was a problem
  +     * w/ FTP.
  +     *
  +     * @see org.apache.commons.vfs.provider.ftp.FtpFileObject#copyFrom
  +     */
  +    public void testCopySameFileSystem() throws Exception
  +    {
  +        final FileObject scratchFolder = createScratchFolder();
  +
  +        // Create direct child of the test folder
  +        final FileObject file = scratchFolder.resolveFile( "file1.txt" );
  +        assertTrue( !file.exists() );
  +
  +        // Create the source file
  +        final String content = "Here is some sample content for the file.  Blah Blah Blah.";
  +        final OutputStream os = file.getContent().getOutputStream();
  +        try
  +        {
  +            os.write( content.getBytes( "utf-8" ) );
  +        }
  +        finally
  +        {
  +            os.close();
  +        }
  +
  +        assertSameContent( content, file );
  +
  +        // Make sure we can copy the new file to another file on the same filesystem
  +        FileObject fileCopy = scratchFolder.resolveFile( "file1copy.txt" );
  +        assertTrue( !fileCopy.exists() );
  +        fileCopy.copyFrom( file, Selectors.SELECT_SELF );
  +
  +        assertSameContent( content, fileCopy );
  +    }
  +
  +    /**
        * Test that children are handled correctly by create and delete.
        */
       public void testListChildren() throws Exception
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>