gnodet commented on a change in pull request #6:
URL: https://github.com/apache/maven-clean-plugin/pull/6#discussion_r771448931



##########
File path: src/main/java/org/apache/maven/plugins/clean/Cleaner.java
##########
@@ -115,9 +135,102 @@ public void delete( File basedir, Selector selector, 
boolean followSymlinks, boo
 
         File file = followSymlinks ? basedir : basedir.getCanonicalFile();
 
+        if ( selector == null && !followSymlinks && fastDir != null )
+        {
+            // If anything wrong happens, we'll just use the usual deletion 
mechanism
+            if ( fastDelete( file ) )
+            {
+                return;
+            }
+        }
+
         delete( file, "", selector, followSymlinks, failOnError, retryOnError 
);
     }
 
+    private boolean fastDelete( File baseDir )
+    {
+        // Handle the case where we use 
${maven.multiModuleProjectDirectory}/target/.clean for example
+        if ( fastDir.getAbsolutePath().startsWith( baseDir.getAbsolutePath() ) 
)
+        {
+            try
+            {
+                File tmpDir = createTempDir( baseDir.getParentFile(), 
baseDir.getName() + "." );
+                try
+                {
+                    Files.move( baseDir.toPath(), tmpDir.toPath(), 
StandardCopyOption.ATOMIC_MOVE );
+                    if ( session != null )
+                    {
+                        session.getRequest().getData().put( 
LAST_DIRECTORY_TO_DELETE, baseDir );
+                    }
+                    baseDir = tmpDir;
+                }
+                catch ( IOException e )
+                {
+                    tmpDir.delete();
+                    if ( logDebug != null )
+                    {
+                        logDebug.log( "Unable to fast delete directory: " + e 
);
+                    }
+                    return false;
+                }
+            }
+            catch ( IOException e )
+            {
+                if ( logDebug != null )
+                {
+                    logDebug.log( "Unable to fast delete directory: " + e );
+                }
+                return false;
+            }
+        }
+        // Create fastDir and the needed parents if needed
+        if ( fastDir.mkdirs() || fastDir.isDirectory() )
+        {
+            try
+            {
+                File tmpDir = createTempDir( fastDir, "" );
+                File dstDir = new File( tmpDir, baseDir.getName() );
+                // Note that by specifying the ATOMIC_MOVE, we expect an 
exception to be thrown
+                // if the path leads to a directory on another mountpoint.  If 
this is the case
+                // or any other exception occurs, an exception will be thrown 
in which case
+                // the method will return false and the usual deletion will be 
performed.
+                Files.move( baseDir.toPath(), dstDir.toPath(), 
StandardCopyOption.ATOMIC_MOVE );
+                BackgroundCleaner.delete( this, tmpDir );
+                return true;
+            }
+            catch ( IOException e )
+            {
+                if ( logDebug != null )
+                {
+                    logDebug.log( "Unable to fast delete directory: " + e );
+                }
+            }
+        }
+        else
+        {
+            if ( logDebug != null )
+            {
+                logDebug.log( "Unable to fast delete directory as the path "
+                        + fastDir + " does not point to a directory or can not 
be created" );
+            }
+        }
+        return false;
+    }
+
+    private File createTempDir( File baseDir, String prefix ) throws 
IOException
+    {
+        for ( int i = 0; i < 10; i++ )
+        {
+            int rnd = ThreadLocalRandom.current().nextInt();
+            File tmpDir = new File( baseDir, prefix + Integer.toHexString( rnd 
) );

Review comment:
       You're right.  Initially I did not want to create the directory, but the 
code has changed so it definitely makes sense to use the `createTempDirectory` 
now.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to