private void ftpRecursivePut()

in wagon-providers/wagon-ftp/src/main/java/org/apache/maven/wagon/providers/ftp/FtpWagon.java [559:700]


    private void ftpRecursivePut( File sourceFile, String fileName )
        throws TransferFailedException
    {
        final RepositoryPermissions permissions = repository.getPermissions();

        fireTransferDebug( "processing = " + sourceFile.getAbsolutePath() + " as " + fileName );

        if ( sourceFile.isDirectory() )
        {
            if ( !fileName.equals( "." ) )
            {
                try
                {
                    // change directory if it already exists.
                    if ( !ftp.changeWorkingDirectory( fileName ) )
                    {
                        // first, try to create it
                        if ( makeFtpDirectoryRecursive( fileName, permissions ) )
                        {
                            if ( !ftp.changeWorkingDirectory( fileName ) )
                            {
                                throw new TransferFailedException(
                                    "Unable to change cwd on ftp server to " + fileName + " when processing "
                                        + sourceFile.getAbsolutePath() );
                            }
                        }
                        else
                        {
                            throw new TransferFailedException(
                                "Unable to create directory " + fileName + " when processing "
                                    + sourceFile.getAbsolutePath() );
                        }
                    }
                }
                catch ( IOException e )
                {
                    throw new TransferFailedException(
                        "IOException caught while processing path at " + sourceFile.getAbsolutePath(), e );
                }
            }

            File[] files = sourceFile.listFiles();
            if ( files != null && files.length > 0 )
            {
                fireTransferDebug( "listing children of = " + sourceFile.getAbsolutePath() + " found " + files.length );

                // Directories first, then files. Let's go deep early.
                for ( File file : files )
                {
                    if ( file.isDirectory() )
                    {
                        ftpRecursivePut( file, file.getName() );
                    }
                }
                for ( File file : files )
                {
                    if ( !file.isDirectory() )
                    {
                        ftpRecursivePut( file, file.getName() );
                    }
                }
            }

            // Step back up a directory once we're done with the contents of this one.
            try
            {
                ftp.changeToParentDirectory();
            }
            catch ( IOException e )
            {
                throw new TransferFailedException( "IOException caught while attempting to step up to parent directory"
                                                       + " after successfully processing "
                                                       + sourceFile.getAbsolutePath(), e );
            }
        }
        else
        {
            // Oh how I hope and pray, in denial, but today I am still just a file.

            FileInputStream sourceFileStream = null;
            try
            {
                sourceFileStream = new FileInputStream( sourceFile );

                // It's a file. Upload it in the current directory.
                if ( ftp.storeFile( fileName, sourceFileStream ) )
                {
                    if ( permissions != null )
                    {
                        // Process permissions; note that if we get errors or exceptions here, they are ignored.
                        // This appears to be a conscious decision, based on other parts of this code.
                        String group = permissions.getGroup();
                        if ( group != null )
                        {
                            try
                            {
                                ftp.sendSiteCommand( "CHGRP " + permissions.getGroup() );
                            }
                            catch ( IOException e )
                            {
                                // ignore
                            }
                        }
                        String mode = permissions.getFileMode();
                        if ( mode != null )
                        {
                            try
                            {
                                ftp.sendSiteCommand( "CHMOD " + permissions.getDirectoryMode() );
                            }
                            catch ( IOException e )
                            {
                                // ignore
                            }
                        }
                    }
                }
                else
                {
                    String msg =
                        "Cannot transfer resource:  '" + sourceFile.getAbsolutePath() + "' FTP Server response: "
                            + ftp.getReplyString();
                    throw new TransferFailedException( msg );
                }

                sourceFileStream.close();
                sourceFileStream = null;
            }
            catch ( IOException e )
            {
                throw new TransferFailedException(
                    "IOException caught while attempting to upload " + sourceFile.getAbsolutePath(), e );
            }
            finally
            {
                IOUtils.closeQuietly( sourceFileStream );
            }

        }

        fireTransferDebug( "completed = " + sourceFile.getAbsolutePath() );
    }