Skip to content

Commit

Permalink
File#lastModified on Linux JDK8 loses milliseconds precision (JDK-817…
Browse files Browse the repository at this point in the history
…7809).
  • Loading branch information
MartinKanters committed Sep 24, 2020
1 parent b579cb1 commit e55f320
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
Expand Down Expand Up @@ -940,12 +941,12 @@ protected boolean isUptodate()
throws ArchiverException
{
final File zipFile = getDestFile();
final long destTimestamp = zipFile.lastModified();
if ( destTimestamp == 0 )
if ( !zipFile.exists() )
{
getLogger().debug( "isUp2date: false (Destination " + zipFile.getPath() + " not found.)" );
return false; // File doesn't yet exist
}
final long destTimestamp = getFileLastModifiedTime(zipFile);

final Iterator it = resources.iterator();
if ( !it.hasNext() )
Expand Down Expand Up @@ -994,6 +995,26 @@ else if ( o instanceof AddedResourceCollection )
return true;
}

/**
* Returns the last modified time in milliseconds of a file.
* It avoids the bug where milliseconds precision is lost on File#lastModified (JDK-8177809) on JDK8 and Linux.
* @param file The file where the last modified time will be returned for.
* @return The last modified time in milliseconds of the file.
* @throws ArchiverException In the case of an IOException, for example when the file does not exists.
*/
private long getFileLastModifiedTime( File file )
throws ArchiverException
{
try
{
return Files.getLastModifiedTime( file.toPath() ).toMillis();
}
catch ( IOException e )
{
throw new ArchiverException( e.getMessage(), e );
}
}

protected boolean checkForced()
throws ArchiverException
{
Expand Down

0 comments on commit e55f320

Please sign in to comment.