From: James William Dumay Date: Thu, 3 Jul 2008 14:16:56 +0000 (+0000) Subject: MRM-859 - Use File.createTempFile() when downloading files from a remote repository X-Git-Tag: archiva-r676265~21 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d07d790c5441fff7f4a5bec622dd1ca2ac0d4807;p=archiva.git MRM-859 - Use File.createTempFile() when downloading files from a remote repository git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@673685 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultRepositoryProxyConnectors.java b/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultRepositoryProxyConnectors.java index 36cccd26f..398734973 100644 --- a/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultRepositoryProxyConnectors.java +++ b/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultRepositoryProxyConnectors.java @@ -691,8 +691,9 @@ public class DefaultRepositoryProxyConnectors try { - temp = new File( localFile.getAbsolutePath() + ".tmp" ); - + localFile.getParentFile().mkdirs(); + temp = File.createTempFile(localFile.getName() + ".", null, localFile.getParentFile()); + boolean success = false; if ( !localFile.exists() ) @@ -729,6 +730,10 @@ public class DefaultRepositoryProxyConnectors return localFile; } + catch (IOException e) + { + throw new ProxyException("Could not create temporary file at " + localFile.getAbsolutePath(), e); + } catch ( ResourceDoesNotExistException e ) { throw new NotFoundException( @@ -743,10 +748,7 @@ public class DefaultRepositoryProxyConnectors } finally { - if ( temp != null ) - { - temp.delete(); - } + FileUtils.deleteQuietly(temp); } } @@ -857,11 +859,18 @@ public class DefaultRepositoryProxyConnectors } catch ( IOException e ) { - throw new ProxyException( "Cannot copy tmp file to its final location", e ); + if (target.exists()) + { + log.debug("Tried to copy file " + temp.getName() + " to " + target.getAbsolutePath() + " but file with this name already exists."); + } + else + { + throw new ProxyException( "Cannot copy tmp file " + temp.getAbsolutePath() + " to its final location", e ); + } } finally { - temp.delete(); + FileUtils.deleteQuietly(temp); } } } diff --git a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/AbstractProxyTestCase.java b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/AbstractProxyTestCase.java index 1eb5651e9..db4a91dc2 100644 --- a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/AbstractProxyTestCase.java +++ b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/AbstractProxyTestCase.java @@ -31,6 +31,7 @@ import java.util.Date; import java.util.Locale; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.ArrayUtils; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration; @@ -43,10 +44,9 @@ import org.apache.maven.archiva.policies.ReleasesPolicy; import org.apache.maven.archiva.policies.SnapshotsPolicy; import org.apache.maven.archiva.repository.ManagedRepositoryContent; import org.apache.maven.wagon.Wagon; -import org.codehaus.plexus.spring.PlexusClassPathXmlApplicationContext; import org.codehaus.plexus.spring.PlexusInSpringTestCase; +import org.easymock.ArgumentsMatcher; import org.easymock.MockControl; -import org.springframework.beans.factory.BeanFactory; /** * AbstractProxyTestCase @@ -88,6 +88,53 @@ public abstract class AbstractProxyTestCase protected static final String REPOPATH_LEGACY_MANAGED = "src/test/repositories/legacy-managed"; protected static final String REPOPATH_LEGACY_MANAGED_TARGET = "target/test-repository/legacy-managed"; + + protected static final ArgumentsMatcher customWagonGetIfNewerMatcher = new ArgumentsMatcher() { + + public boolean matches(Object[] expected, Object[] actual) { + if (expected.length < 1 || actual.length < 1) + { + return false; + } + return MockControl.ARRAY_MATCHER.matches(ArrayUtils.remove(expected, 1), ArrayUtils.remove(actual, 1)); + } + + public String toString(Object[] arguments) { + return ArrayUtils.toString(arguments); + } + }; + + protected static final ArgumentsMatcher customWagonGetMatcher = new ArgumentsMatcher() { + + public boolean matches(Object[] expected, Object[] actual) + { + if (expected.length == 2 && actual.length == 2) + { + if (expected[0] == null && actual[0] == null) + { + return true; + } + + if (expected[0] == null) + { + return actual[0] == null; + } + + if (actual[0] == null) + { + return expected[0] == null; + } + + return expected[0].equals(actual[0]); + } + return false; + } + + public String toString(Object[] arguments) + { + return ArrayUtils.toString(arguments); + } + }; protected MockControl wagonMockControl; diff --git a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/CacheFailuresTransferTest.java b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/CacheFailuresTransferTest.java index f2796727b..70aec32fc 100644 --- a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/CacheFailuresTransferTest.java +++ b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/CacheFailuresTransferTest.java @@ -63,8 +63,11 @@ public class CacheFailuresTransferTest SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES ); saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES ); - + wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) ); + + wagonMockControl.setMatcher(customWagonGetMatcher); + wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 ); wagonMockControl.replay(); @@ -72,7 +75,7 @@ public class CacheFailuresTransferTest File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact ); wagonMockControl.verify(); - + // Second attempt to download same artifact use cache wagonMockControl.reset(); wagonMockControl.replay(); @@ -105,6 +108,8 @@ public class CacheFailuresTransferTest SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO ); wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) ); + + wagonMockControl.setMatcher(customWagonGetMatcher); wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 ); wagonMockControl.replay(); @@ -116,10 +121,12 @@ public class CacheFailuresTransferTest // Second attempt to download same artifact DOES NOT use cache wagonMockControl.reset(); wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) ); + + wagonMockControl.setMatcher(customWagonGetMatcher); wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 ); wagonMockControl.replay(); - downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact ); + downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact ); wagonMockControl.verify(); diff --git a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ChecksumTransferTest.java b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ChecksumTransferTest.java index e51447ec3..4528145dc 100644 --- a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ChecksumTransferTest.java +++ b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ChecksumTransferTest.java @@ -397,10 +397,13 @@ public class ChecksumTransferTest SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO ); wagonMock.get( path, new File( expectedFile.getAbsolutePath() + ".tmp" ) ); + wagonMockControl.setMatcher(customWagonGetMatcher); wagonMockControl.setVoidCallable(); wagonMock.get( path + ".sha1", new File( expectedFile.getAbsolutePath() + ".sha1.tmp" ) ); + wagonMockControl.setMatcher(customWagonGetMatcher); wagonMockControl.setVoidCallable(); wagonMock.get( path + ".md5", new File( expectedFile.getAbsolutePath() + ".md5.tmp" ) ); + wagonMockControl.setMatcher(customWagonGetMatcher); wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Resource does not exist." ) ); wagonMockControl.replay(); diff --git a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ErrorHandlingTest.java b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ErrorHandlingTest.java index 0e62f8273..c0c60a720 100644 --- a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ErrorHandlingTest.java +++ b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ErrorHandlingTest.java @@ -537,6 +537,7 @@ public class ErrorHandlingTest throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException { wagonMock.get( path, createExpectedTempFile( expectedFile ) ); + wagonMockControl.setMatcher(customWagonGetMatcher); wagonMockControl.setThrowable( throwable, 1 ); } @@ -544,6 +545,7 @@ public class ErrorHandlingTest throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException { wagonMock.getIfNewer( path, createExpectedTempFile( expectedFile ), expectedFile.lastModified() ); + wagonMockControl.setMatcher(customWagonGetIfNewerMatcher); wagonMockControl.setThrowable( exception, 1 ); } diff --git a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ManagedDefaultTransferTest.java b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ManagedDefaultTransferTest.java index 8e920f700..9819f7cc0 100644 --- a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ManagedDefaultTransferTest.java +++ b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ManagedDefaultTransferTest.java @@ -306,6 +306,7 @@ public class ManagedDefaultTransferTest saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" ); wagonMock.get( path, new File( expectedFile.getAbsolutePath() + ".tmp" ) ); + wagonMockControl.setMatcher(customWagonGetMatcher); wagonMockControl.setThrowable( new ResourceDoesNotExistException( "transfer failed" ) ); wagonMockControl.replay(); @@ -344,9 +345,13 @@ public class ManagedDefaultTransferTest File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ); wagonMock.get( path, tmpFile ); + + wagonMockControl.setMatcher(customWagonGetMatcher); wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) ); wagonMock.get( path, tmpFile ); + + wagonMockControl.setMatcher(customWagonGetMatcher); wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) ); wagonMockControl.replay(); diff --git a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/MetadataTransferTest.java b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/MetadataTransferTest.java index 2496e3b68..3cab9107b 100644 --- a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/MetadataTransferTest.java +++ b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/MetadataTransferTest.java @@ -120,6 +120,9 @@ public class MetadataTransferTest File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), metadataTools.getRepositorySpecificName( "badproxied1", requestedResource ) ); wagonMock.get( requestedResource, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) ); + + wagonMockControl.setMatcher(customWagonGetMatcher); + wagonMockControl.setThrowable( new TransferFailedException( "can't connect" ) ); wagonMockControl.replay();