Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d0b6121

Browse files
authored
FileUtils.linkFile(source, destination) (#82)
1 parent 01d82ae commit d0b6121

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/main/java/org/codehaus/plexus/util/FileUtils.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,38 @@ private static void doCopyFileUsingNewIO( File source, File destination )
10591059
NioFiles.copy( source, destination );
10601060
}
10611061

1062+
/**
1063+
* Link file from destination to source. The directories up to <code>destination</code> will be created if they
1064+
* don't already exist. <code>destination</code> will be overwritten if it already exists.
1065+
*
1066+
* @param source An existing non-directory <code>File</code> to link to.
1067+
* @param destination A non-directory <code>File</code> becoming the link (possibly overwriting).
1068+
* @throws IOException if <code>source</code> does not exist, <code>destination</code> cannot be created, or an
1069+
* IO error occurs during linking.
1070+
* @throws java.io.FileNotFoundException if <code>destination</code> is a directory (use
1071+
* {@link #copyFileToDirectory}).
1072+
*/
1073+
public static void linkFile( final File source, final File destination )
1074+
throws IOException
1075+
{
1076+
// check source exists
1077+
if ( !source.exists() )
1078+
{
1079+
final String message = "File " + source + " does not exist";
1080+
throw new IOException( message );
1081+
}
1082+
1083+
// check source != destination, see PLXUTILS-10
1084+
if ( source.getCanonicalPath().equals( destination.getCanonicalPath() ) )
1085+
{
1086+
// if they are equal, we can exit the method without doing any work
1087+
return;
1088+
}
1089+
mkdirsFor( destination );
1090+
1091+
NioFiles.createSymbolicLink( destination, source );
1092+
}
1093+
10621094
/**
10631095
* Copy file from source to destination only if source timestamp is later than the destination timestamp. The
10641096
* directories up to <code>destination</code> will be created if they don't already exist. <code>destination</code>

src/test/java/org/codehaus/plexus/util/FileUtilsTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.io.Reader;
3535
import java.io.Writer;
3636
import java.net.URL;
37+
import java.nio.file.Files;
3738
import java.util.Properties;
3839

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

432+
// linkFile
433+
@Test
434+
public void testLinkFile1()
435+
throws Exception
436+
{
437+
final File destination = new File( getTestDirectory(), "link1.txt" );
438+
FileUtils.linkFile( testFile1, destination );
439+
assertTrue( "Check Exist", destination.exists() );
440+
assertTrue( "Check File length", destination.length() == testFile1Size );
441+
assertTrue( "Check is link", Files.isSymbolicLink(destination.toPath()));
442+
}
443+
444+
@Test
445+
public void testLinkFile2()
446+
throws Exception
447+
{
448+
final File destination = new File( getTestDirectory(), "link2.txt" );
449+
FileUtils.linkFile( testFile1, destination );
450+
assertTrue( "Check Exist", destination.exists() );
451+
assertTrue( "Check File length", destination.length() == testFile2Size );
452+
assertTrue( "Check is link", Files.isSymbolicLink(destination.toPath()));
453+
}
454+
455+
/**
456+
* ensure we create directory tree for destination
457+
*
458+
* @throws Exception
459+
*/
460+
@Test
461+
public void testLinkFile3()
462+
throws Exception
463+
{
464+
File destDirectory = new File( getTestDirectory(), "foo/bar/testlink" );
465+
if ( destDirectory.exists() )
466+
{
467+
destDirectory.delete();
468+
}
469+
final File destination = new File( destDirectory, "link2.txt" );
470+
FileUtils.linkFile( testFile1, destination );
471+
assertTrue( "Check Exist", destination.exists() );
472+
assertTrue( "Check File length", destination.length() == testFile2Size );
473+
assertTrue( "Check is link", Files.isSymbolicLink(destination.toPath()));
474+
}
475+
431476
// copyFileIfModified
432477

433478
@Test

0 commit comments

Comments
 (0)