Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
NEXUS-4184
Browse files Browse the repository at this point in the history
Deleting a gigabyte file takes forever...

git-svn-id: file:///opt/svn/repositories/sonatype.org/nexus/trunk/nexus@7963 2aa8b3fc-8ebb-4439-a84f-95066eaea8ab
  • Loading branch information
velo committed Mar 31, 2011
1 parent 59151b6 commit 2169fe0
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,38 +168,43 @@ public void moveItem( Repository repository, ResourceStoreRequest from, File fro
// create parents down to the file itself (this will make those if needed, otherwise return silently)
mkParentDirs( repository, toTarget );

if ( fromTarget.isDirectory() )
try
{
try
{
// copy
FileUtils.copyDirectoryStructure( fromTarget, toTarget );

// delete
FileUtils.forceDelete( fromTarget );
org.sonatype.nexus.util.FileUtils.move( fromTarget, toTarget );
}
catch ( IOException e )
{
logger.warn( "Unable to move item, falling back to copy+delete: " + toTarget.getPath(),
logger.isDebugEnabled() ? e : null );

// update timestamp
toTarget.setLastModified( fromTarget.lastModified() );
}
catch ( IOException e )
if ( fromTarget.isDirectory() )
{
throw new LocalStorageException( "Error during moveDirectory", e );
try
{
FileUtils.copyDirectoryStructure( fromTarget, toTarget );
}
catch ( IOException ioe )
{
throw new LocalStorageException( "Error during moveItem", ioe );
}
}
}
else if ( fromTarget.isFile() )
{
try
else if ( fromTarget.isFile() )
{
FileUtils.rename( fromTarget, toTarget );
try
{
FileUtils.copyFile( fromTarget, toTarget );
}
catch ( IOException ioe )
{
throw new LocalStorageException( "Error during moveItem", ioe );
}
}
catch ( IOException e )
else
{
throw new LocalStorageException( "Error during moveItem", e );
// TODO throw exception?
logger.error( "Unexpected item kind: " + toTarget.getClass() );
}
}
else
{
throw new ItemNotFoundException( from, repository );
shredItem( repository, from, fromTarget );
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
package org.sonatype.nexus.proxy.wastebasket;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.slf4j.Logger;
import org.sonatype.nexus.configuration.ConfigurationChangeEvent;
import org.sonatype.nexus.configuration.application.ApplicationConfiguration;
import org.sonatype.nexus.proxy.ItemNotFoundException;
import org.sonatype.nexus.proxy.LocalStorageException;
import org.sonatype.nexus.proxy.ResourceStoreRequest;
Expand Down Expand Up @@ -59,7 +56,7 @@ public class DefaultFSWastebasket
private ApplicationEventMulticaster applicationEventMulticaster;

@Requirement
private ApplicationConfiguration applicationConfiguration;
private Logger log;

private File wastebasketDirectory;

Expand Down Expand Up @@ -183,55 +180,48 @@ public void delete( LocalRepositoryStorage ls, Repository repository, ResourceSt
}

File basketFile = getFileForItem( item );
basketFile.getParentFile().mkdirs();

if ( StorageFileItem.class.isAssignableFrom( item.getClass() ) )
if ( DefaultFSLocalRepositoryStorage.class.isAssignableFrom( ls.getClass() ) )
{
basketFile.getParentFile().mkdirs();

// a file or link, it is a simple File
InputStream is = null;

FileOutputStream fos = null;
// an easy way, we have a File
File itemFile = ( (DefaultFSLocalRepositoryStorage) ls ).getFileFromBase( repository, request );

try
{
is = ( (StorageFileItem) item ).getInputStream();

fos = new FileOutputStream( basketFile );

IOUtil.copy( is, fos );

fos.flush();
org.sonatype.nexus.util.FileUtils.move( itemFile, basketFile );
}
finally
catch ( IOException e )
{
IOUtil.close( is );

IOUtil.close( fos );
log.warn( "Unable to move item, falling back to copy+delete: " + item.getPath(),
log.isDebugEnabled() ? e : null );

if ( item instanceof StorageCollectionItem )
{
FileUtils.copyDirectory( itemFile, basketFile );
}
else if ( item instanceof StorageFileItem )
{
FileUtils.copyFile( itemFile, basketFile );
}
else
{
// TODO throw exception?
log.error( "Unexpected item kind: " + item.getClass() );
}
ls.shredItem( repository, request );
}

}
else if ( StorageCollectionItem.class.isAssignableFrom( item.getClass() ) )
else
{
basketFile.mkdirs();

// a collection (dir)
if ( DefaultFSLocalRepositoryStorage.class.isAssignableFrom( ls.getClass() ) )
{
// an easy way, we have a File
File itemFile = ( (DefaultFSLocalRepositoryStorage) ls ).getFileFromBase( repository, request );

FileUtils.copyDirectory( itemFile, basketFile );
}
else
{
// a hard way
// TODO: walker and recursive deletions?
// Currently, we have only the DefaultFSLocalRepositoryStorage implementation :)
}
// a hard way
// TODO: walker and recursive deletions?
// Currently, we have only the DefaultFSLocalRepositoryStorage implementation :)
throw new UnsupportedStorageOperationException( "Unable to delete artifacts on a non-FSStorage" );
}
}

ls.shredItem( repository, request );
}
catch ( ItemNotFoundException e )
{
Expand Down
59 changes: 59 additions & 0 deletions nexus-utils/src/main/java/org/sonatype/nexus/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.sonatype.nexus.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
Expand Down Expand Up @@ -168,4 +170,61 @@ public static File getFileFromUrl( String urlPath )

return null;
}

public static void move( File source, File destination )
throws IOException
{
if ( source == null )
{
throw new NullPointerException( "source can't be null" );
}
if ( destination == null )
{
throw new NullPointerException( "destination can't be null" );
}

if ( !source.exists() )
{
throw new FileNotFoundException( "Source file doesn't exists " + source );
}

destination.getParentFile().mkdirs();
if ( !destination.exists() )
{
if ( !source.renameTo( destination ) )
{
throw new IOException( "Failed to move '" + source + "' to '" + destination + "'" );
}
}
else if ( source.isFile() )
{
org.codehaus.plexus.util.FileUtils.forceDelete( destination );
if ( !source.renameTo( destination ) )
{
throw new IOException( "Failed to move '" + source + "' to '" + destination + "'" );
}
}
else if ( source.isDirectory() )
{
// the folder already exists the, so let's do a recursive move....
if ( destination.isFile() )
{
org.codehaus.plexus.util.FileUtils.forceDelete( destination );
if ( !source.renameTo( destination ) )
{
throw new IOException( "Failed to move '" + source + "' to '" + destination + "'" );
}
}
else
{
String[] files = source.list();
for ( String file : files )
{
move( new File( source, file ), new File( destination, file ) );
}

org.codehaus.plexus.util.FileUtils.forceDelete( source );
}
}
}
}
Loading

0 comments on commit 2169fe0

Please sign in to comment.