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

new feature FileUtils.linkFile(source, destination) #82

Merged
merged 1 commit into from Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Expand Up @@ -1058,6 +1058,38 @@ private static void doCopyFileUsingNewIO( File source, File destination )
NioFiles.copy( source, destination );
}

/**
* Link file from destination to source. The directories up to <code>destination</code> will be created if they
* don't already exist. <code>destination</code> will be overwritten if it already exists.
*
* @param source An existing non-directory <code>File</code> to link to.
* @param destination A non-directory <code>File</code> becoming the link (possibly overwriting).
* @throws IOException if <code>source</code> does not exist, <code>destination</code> cannot be created, or an
* IO error occurs during linking.
* @throws java.io.FileNotFoundException if <code>destination</code> is a directory (use
* {@link #copyFileToDirectory}).
*/
public static void linkFile( final File source, final File destination )
throws IOException
{
// check source exists
if ( !source.exists() )
{
final String message = "File " + source + " does not exist";
throw new IOException( message );
}

// check source != destination, see PLXUTILS-10
if ( source.getCanonicalPath().equals( destination.getCanonicalPath() ) )
{
// if they are equal, we can exit the method without doing any work
return;
}
mkdirsFor( destination );

NioFiles.createSymbolicLink( destination, source );
}

/**
* Copy file from source to destination only if source timestamp is later than the destination timestamp. The
* directories up to <code>destination</code> will be created if they don't already exist. <code>destination</code>
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/org/codehaus/plexus/util/FileUtilsTest.java
Expand Up @@ -34,6 +34,7 @@
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.nio.file.Files;
import java.util.Properties;

import org.junit.Before;
Expand Down Expand Up @@ -428,6 +429,50 @@ public void testCopyFile3()
assertTrue( "Check Full copy", destination.length() == testFile2Size );
}

// linkFile
@Test
public void testLinkFile1()
throws Exception
{
final File destination = new File( getTestDirectory(), "link1.txt" );
FileUtils.linkFile( testFile1, destination );
assertTrue( "Check Exist", destination.exists() );
assertTrue( "Check File length", destination.length() == testFile1Size );
assertTrue( "Check is link", Files.isSymbolicLink(destination.toPath()));
}

@Test
public void testLinkFile2()
throws Exception
{
final File destination = new File( getTestDirectory(), "link2.txt" );
FileUtils.linkFile( testFile1, destination );
assertTrue( "Check Exist", destination.exists() );
assertTrue( "Check File length", destination.length() == testFile2Size );
assertTrue( "Check is link", Files.isSymbolicLink(destination.toPath()));
}

/**
* ensure we create directory tree for destination
*
* @throws Exception
*/
@Test
public void testLinkFile3()
throws Exception
{
File destDirectory = new File( getTestDirectory(), "foo/bar/testlink" );
if ( destDirectory.exists() )
{
destDirectory.delete();
}
final File destination = new File( destDirectory, "link2.txt" );
FileUtils.linkFile( testFile1, destination );
assertTrue( "Check Exist", destination.exists() );
assertTrue( "Check File length", destination.length() == testFile2Size );
assertTrue( "Check is link", Files.isSymbolicLink(destination.toPath()));
}

// copyFileIfModified

@Test
Expand Down