Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test, fix, and deprecate buggy copyDirectory methods #80

Merged
merged 10 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import org.apache.commons.io.IOUtils;
import org.apache.maven.shared.utils.Os;
import org.apache.maven.shared.utils.StringUtils;

Expand Down Expand Up @@ -52,13 +53,14 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Random;

/**
* This class provides basic facilities for manipulating files and file paths.
* <p/>
*
* <h3>Path-related methods</h3>
* <p/>
*
* <p>Methods exist to retrieve the components of a typical file path. For example
* <code>/www/hosted/mysite/index.html</code>, can be broken into:
* <ul>
Expand All @@ -68,13 +70,12 @@
* </p>
* <p/>
* <h3>File-related methods</h3>
* <p/>
*
* There are methods to create a {@link #toFile File from a URL}, copy a
* {@link #copyFile File to another File},
* copy a {@link #copyURLToFile URL's contents to a File},
* as well as methods to {@link #deleteDirectory(File) delete} and {@link #cleanDirectory(File)
* clean} a directory.
* </p>
* <p/>
* Common {@link java.io.File} manipulation routines.
* <p/>
Expand All @@ -89,7 +90,6 @@
* @author <a href="mailto:Christoph.Reck@dlr.de">Christoph.Reck</a>
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
* @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
*
*/
public class FileUtils
{
Expand Down Expand Up @@ -633,7 +633,7 @@ public static boolean contentEquals( @Nonnull final File file1, @Nonnull final F
try ( InputStream input1 = new FileInputStream( file1 );
InputStream input2 = new FileInputStream( file2 ) )
{
return IOUtil.contentEquals( input1, input2 );
return IOUtils.contentEquals( input1, input2 );
}
}

Expand Down Expand Up @@ -750,7 +750,7 @@ public static void copyFileToDirectory( @Nonnull final File source, @Nonnull fin
{
if ( destinationDirectory.exists() && !destinationDirectory.isDirectory() )
{
throw new IllegalArgumentException( "Destination is not a directory" );
throw new IOException( "Destination is not a directory" );
}

copyFile( source, new File( destinationDirectory, source.getName() ) );
Expand Down Expand Up @@ -1022,9 +1022,9 @@ private static void copyStreamToFile( @Nonnull @WillClose final InputStream sour
}

/**
* Resolve a file <code>filename</code> to it's canonical form. If <code>filename</code> is
* relative (doesn't start with <code>/</code>), it will be resolved relative to
* <code>baseFile</code>, otherwise it is treated as a normal root-relative path.
* Resolve a file <code>filename</code> to its canonical form. If <code>filename</code> is
* relative (doesn't start with <code>/</code>), it is resolved relative to
* <code>baseFile</code>. Otherwise it is treated as a normal root-relative path.
*
* @param baseFile where to resolve <code>filename</code> from, if <code>filename</code> is relative
* @param filename absolute or relative file path to resolve
Expand Down Expand Up @@ -1605,14 +1605,27 @@ public static List<File> getFiles( @Nonnull File directory, @Nullable String inc
/**
* Copy the contents of a directory into another one.
*
* @param sourceDirectory the source directory
* @param destinationDirectory the target directory
* @param sourceDirectory the source directory. If the source does not exist,
* the method simply returns.
* @param destinationDirectory the target directory; will be created if it doesn't exist
* @throws IOException if any
* @deprecated use {@code org.apache.commons.io.FileUtils.copyDirectory()}
*/
@Deprecated
public static void copyDirectory( @Nonnull File sourceDirectory, @Nonnull File destinationDirectory )
throws IOException
{
copyDirectory( sourceDirectory, destinationDirectory, "**", null );
Objects.requireNonNull( sourceDirectory );
Objects.requireNonNull( destinationDirectory );
if ( destinationDirectory.equals( sourceDirectory ) ) {
throw new IOException( "Can't copy directory " + sourceDirectory + " to itself." );
}
else if ( !destinationDirectory.exists() ) {
if ( !destinationDirectory.mkdirs() ) {
throw new IOException( "Can't create directory " + destinationDirectory );
}
}
copyDirectoryStructure( sourceDirectory, destinationDirectory );
}

/**
Expand All @@ -1622,9 +1635,11 @@ public static void copyDirectory( @Nonnull File sourceDirectory, @Nonnull File d
* @param destinationDirectory the target directory
* @param includes Ant include pattern
* @param excludes Ant exclude pattern
* @throws IOException if any
* @throws IOException if the source is a file or cannot be copied
* @see #getFiles(File, String, String)
* @deprecated use {@code org.apache.commons.io.FileUtils.copyDirectory()}
*/
@Deprecated
public static void copyDirectory( @Nonnull File sourceDirectory, @Nonnull File destinationDirectory,
@Nullable String includes, @Nullable String excludes )
throws IOException
Expand All @@ -1633,6 +1648,9 @@ public static void copyDirectory( @Nonnull File sourceDirectory, @Nonnull File d
{
return;
}
else if ( !sourceDirectory.isDirectory() ) {
throw new IOException( sourceDirectory + " is not a directory." );
}

List<File> files = getFiles( sourceDirectory, includes, excludes );

Expand All @@ -1651,8 +1669,8 @@ public static void copyDirectory( @Nonnull File sourceDirectory, @Nonnull File d
* <li>The <code>sourceDirectory</code> must exist.
* </ul>
*
* @param sourceDirectory the source dir
* @param destinationDirectory the target dir
* @param sourceDirectory the existing directory to be copied
* @param destinationDirectory the new directory to be created
* @throws IOException if any
* @deprecated use {@code org.apache.commons.io.FileUtils.copyDirectory()}
*/
Expand Down Expand Up @@ -1686,7 +1704,7 @@ private static void copyDirectoryStructure( @Nonnull File sourceDirectory, @Nonn

if ( !sourceDirectory.exists() )
{
throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
throw new IOException( "Source directory doesn't exist (" + sourceDirectory.getAbsolutePath() + ")." );
}

File[] files = sourceDirectory.listFiles();
Expand Down Expand Up @@ -2123,7 +2141,7 @@ public static boolean isSymbolicLinkForSure( @Nonnull final File file )
* @return the linked file
* @throws IOException in case of an error
* @see {@code java.nio.file.Files.createSymbolicLink(Path)} which creates a new
* symbolic link but does not replace exsiting symbolic links
* symbolic link but does not replace existing symbolic links
*/
@Nonnull public static File createSymbolicLink( @Nonnull File symlink, @Nonnull File target )
throws IOException
Expand Down
Loading