Skip to content

Files created on 1970-01-01 0000.0 are never copied when the target is not present. #40

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: java
jdk:
- oraclejdk7
- openjdk7
- oraclejdk8

# No need for preliminary install step.
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ private static void doCopyFileUsingNewIO( File source, File destination )
public static boolean copyFileIfModified( final File source, final File destination )
throws IOException
{
if ( destination.lastModified() < source.lastModified() )
if ( isSourceNewerThanDestination( source, destination ) )
{
copyFile( source, destination );

Expand Down Expand Up @@ -2289,7 +2289,8 @@ public static File createTempFile( String prefix, String suffix, File parentDir
}

/**
* <b>If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified()</b>
* <b>If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified(),
* if the files were both created on 1970-01-01 : 0000.00 </b>
*
* @param from the file to copy
* @param to the destination file
Expand All @@ -2309,8 +2310,8 @@ public static abstract class FilterWrapper
}

/**
* <b>If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified() or if
* overwrite is true</b>
* <b>If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified(),
* if the files were both created on 1970-01-01 : 0000.00 or if overwrite is true</b>
*
* @param from the file to copy
* @param to the destination file
Expand Down Expand Up @@ -2367,13 +2368,17 @@ public static void copyFile( File from, File to, String encoding, FilterWrapper[
}
else
{
if ( to.lastModified() < from.lastModified() || overwrite )
if ( isSourceNewerThanDestination( from, to ) || overwrite )
{
copyFile( from, to );
}
}
}

private static boolean isSourceNewerThanDestination( File source, File destination ) {
return ( destination.lastModified() == 0L && source.lastModified() == 0L ) || destination.lastModified() < source.lastModified();
}

/**
* Note: the file content is read with platform encoding
*
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/codehaus/plexus/util/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,23 @@ public void testCopyIfModifiedWhenSourceIsOlder()
assertFalse( "Source file should not have been copied.", FileUtils.copyFileIfModified( source, destination ) );
}

public void testCopyIfModifiedWhenSourceHasZeroDate()
throws Exception
{
FileUtils.forceMkdir( new File( getTestDirectory() + "/temp" ) );

// Source modified on 1970-1-1 0000.0
File source = new File( getTestDirectory(), "copy1.txt" );
FileUtils.copyFile( testFile1, source );
source.setLastModified( 0L );

// A non existing destination
File destination = new File( getTestDirectory(), "/temp/copy1.txt" );

// Should copy the source to the non existing destination.
assertTrue( "Source file should have been copied.", FileUtils.copyFileIfModified( source, destination ) );
}

// forceDelete

public void testForceDeleteAFile1()
Expand Down