Skip to content

Commit

Permalink
FileUtils.linkFile(source, destination) (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarg committed Aug 15, 2020
1 parent 01d82ae commit d0b6121
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Expand Up @@ -1059,6 +1059,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

0 comments on commit d0b6121

Please sign in to comment.