Skip to content

added Archiver.get/setLastModifiedDate(Date) API to force entries value #118

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

Merged
merged 1 commit into from
Aug 16, 2019
Merged
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 pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>plexus-archiver</artifactId>
<version>4.1.1-SNAPSHOT</version>
<version>4.2.0-SNAPSHOT</version>
<name>Plexus Archiver Component</name>

<scm>
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.lang.reflect.UndeclaredThrowableException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -103,6 +104,11 @@ public abstract class AbstractArchiver
*/
private boolean useJvmChmod = true;

/**
* @since 4.2.0
*/
private Date lastModifiedDate;

// contextualized.
private ArchiverManager archiverManager;

Expand Down Expand Up @@ -1133,4 +1139,16 @@ public void setIgnorePermissions( final boolean ignorePermissions )
this.ignorePermissions = ignorePermissions;
}

@Override
public void setLastModifiedDate( Date lastModifiedDate )
{
this.lastModifiedDate = lastModifiedDate;
}

@Override
public Date getLastModifiedDate()
{
return lastModifiedDate;
}

}
14 changes: 14 additions & 0 deletions src/main/java/org/codehaus/plexus/archiver/Archiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -399,4 +400,17 @@ ResourceIterator getResources()
*/
void setIgnorePermissions( final boolean ignorePermissions );

/**
* Define forced last modification date for entries (if non null).
*
* @param lastModifiedDate
* @since 4.2.0
*/
void setLastModifiedDate( final Date lastModifiedDate );

/**
* @since 4.2.0
*/
Date getLastModifiedDate();

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.File;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: the class name has a typo

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Map;
import javax.annotation.Nonnull;
import org.codehaus.plexus.archiver.ArchiveEntry;
Expand Down Expand Up @@ -331,4 +332,16 @@ public void setIgnorePermissions( boolean ignorePermissions )
target.setIgnorePermissions( ignorePermissions );
}

@Override
public void setLastModifiedDate( final Date lastModifiedDate )
{
target.setLastModifiedDate( lastModifiedDate );
}

@Override
public Date getLastModifiedDate()
{
return target.getLastModifiedDate();
}

}
13 changes: 13 additions & 0 deletions src/main/java/org/codehaus/plexus/archiver/diags/NoOpArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import javax.annotation.Nonnull;
import org.codehaus.plexus.archiver.ArchiveEntry;
Expand Down Expand Up @@ -346,4 +347,16 @@ public void setIgnorePermissions( boolean ignorePermissions )
this.ignorePermissions = ignorePermissions;
}

@Override
public void setLastModifiedDate( final Date lastModifiedDate )
{

}

@Override
public Date getLastModifiedDate()
{
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -52,6 +53,8 @@ public class TrackingArchiver

private boolean ignorePermissions;

private Date lastModified;

@Override
public void createArchive()
throws ArchiverException, IOException
Expand Down Expand Up @@ -400,4 +403,16 @@ public void setIgnorePermissions( final boolean ignorePermissions )
this.ignorePermissions = ignorePermissions;
}

@Override
public void setLastModifiedDate( final Date lastModifiedDate )
{
this.lastModified = lastModifiedDate;
}

@Override
public Date getLastModifiedDate()
{
return lastModified;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,16 @@ private void setFileModes( ArchiveEntry entry, File outFile, long inLastModified
ArchiveEntryUtils.chmod( outFile, entry.getMode() );
}

outFile.setLastModified( inLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE
? System.currentTimeMillis()
: inLastModified );
if ( getLastModifiedDate() == null )
{
outFile.setLastModified( inLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE
? System.currentTimeMillis()
: inLastModified );
}
else
{
outFile.setLastModified( getLastModifiedDate().getTime() );
}
}

@Override
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,17 @@ else if ( longFileMode.isFailMode() )
te = new TarArchiveEntry( vPath );
}

long teLastModified = entry.getResource().getLastModified();
te.setModTime( teLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE
? System.currentTimeMillis()
: teLastModified );
if ( getLastModifiedDate() == null )
{
long teLastModified = entry.getResource().getLastModified();
te.setModTime( teLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE
? System.currentTimeMillis()
: teLastModified );
}
else
{
te.setModTime( getLastModifiedDate() );
}

if ( entry.getType() == ArchiveEntry.SYMLINK )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@ public InputStream get()

private void setTime( java.util.zip.ZipEntry zipEntry, long lastModified )
{
if ( getLastModifiedDate() != null )
{
lastModified = getLastModifiedDate().getTime();
}

// Zip archives store file modification times with a
// granularity of two seconds, so the times will either be rounded
// up or down. If you round down, the archive will always seem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,4 +886,21 @@ public void testForcedFileModes()
}
}

public void testFixedEntryModificationTime()
throws IOException
{
final long almostMinDosTime = 315532802000L;
final File zipFile = getTestFile( "target/output/zip-with-fixed-entry-modification-times.zip" );
final ZipArchiver archiver = getZipArchiver( zipFile );
archiver.setLastModifiedDate( new Date( almostMinDosTime ) );
archiver.addDirectory( new File( "src/test/resources/zip-timestamp" ) );
archiver.createArchive();

assertTrue( zipFile.exists() );
final ZipFile zf = new ZipFile( zipFile );
assertEquals( almostMinDosTime, zf.getEntry( "file-with-even-time.txt" ).getTime() );
assertEquals( almostMinDosTime, zf.getEntry( "file-with-odd-time.txt" ).getTime() );
assertEquals( almostMinDosTime, zf.getEntry( "foo/" ).getTime() );
}

}