Skip to content

Commit 62b4a86

Browse files
Andreas Kluthmichael-o
Andreas Kluth
authored andcommitted
Files created on 1970-01-01T00:00Z are never copied when target is not present
This closes #40
1 parent ac81ee2 commit 62b4a86

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ private static void doCopyFileUsingNewIO( File source, File destination )
11641164
public static boolean copyFileIfModified( final File source, final File destination )
11651165
throws IOException
11661166
{
1167-
if ( destination.lastModified() < source.lastModified() )
1167+
if ( isSourceNewerThanDestination( source, destination ) )
11681168
{
11691169
copyFile( source, destination );
11701170

@@ -2289,7 +2289,7 @@ public static File createTempFile( String prefix, String suffix, File parentDir
22892289
}
22902290

22912291
/**
2292-
* <b>If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified()</b>
2292+
* <b>If wrappers is null or empty, the file will be copy only if {@code to.lastModified() < from.lastModified()}</b>
22932293
*
22942294
* @param from the file to copy
22952295
* @param to the destination file
@@ -2309,15 +2309,13 @@ public static abstract class FilterWrapper
23092309
}
23102310

23112311
/**
2312-
* <b>If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified() or if
2313-
* overwrite is true</b>
2312+
* <b>If wrappers is null or empty, the file will be copy only if {@code to.lastModified() < from.lastModified()}, if overwrite is true</b>
23142313
*
23152314
* @param from the file to copy
23162315
* @param to the destination file
23172316
* @param encoding the file output encoding (only if wrappers is not empty)
23182317
* @param wrappers array of {@link FilterWrapper}
2319-
* @param overwrite if true and f wrappers is null or empty, the file will be copy even if to.lastModified() <
2320-
* from.lastModified()
2318+
* @param overwrite if true and wrappers is null or empty, the file will be copied even if {@code to.lastModified() < from.lastModified()}
23212319
* @throws IOException if an IO error occurs during copying or filtering
23222320
* @since 1.5.2
23232321
*/
@@ -2367,13 +2365,17 @@ public static void copyFile( File from, File to, String encoding, FilterWrapper[
23672365
}
23682366
else
23692367
{
2370-
if ( to.lastModified() < from.lastModified() || overwrite )
2368+
if ( isSourceNewerThanDestination( from, to ) || overwrite )
23712369
{
23722370
copyFile( from, to );
23732371
}
23742372
}
23752373
}
23762374

2375+
private static boolean isSourceNewerThanDestination( File source, File destination ) {
2376+
return ( destination.lastModified() == 0L && source.lastModified() == 0L ) || destination.lastModified() < source.lastModified();
2377+
}
2378+
23772379
/**
23782380
* Note: the file content is read with platform encoding
23792381
*

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

+17
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,23 @@ public void testCopyIfModifiedWhenSourceIsOlder()
439439
assertFalse( "Source file should not have been copied.", FileUtils.copyFileIfModified( source, destination ) );
440440
}
441441

442+
public void testCopyIfModifiedWhenSourceHasZeroDate()
443+
throws Exception
444+
{
445+
FileUtils.forceMkdir( new File( getTestDirectory(), "temp" ) );
446+
447+
// Source modified on 1970-01-01T00:00Z
448+
File source = new File( getTestDirectory(), "copy1.txt" );
449+
FileUtils.copyFile( testFile1, source );
450+
source.setLastModified( 0L );
451+
452+
// A non existing destination
453+
File destination = new File( getTestDirectory(), "temp/copy1.txt" );
454+
455+
// Should copy the source to the non existing destination.
456+
assertTrue( "Source file should have been copied.", FileUtils.copyFileIfModified( source, destination ) );
457+
}
458+
442459
// forceDelete
443460

444461
public void testForceDeleteAFile1()

0 commit comments

Comments
 (0)