Skip to content

Commit b805279

Browse files
committed
added Archiver.get/setLastModifiedDate(Date) API to force entries value
this API is a step to Reproducible Builds, to get reproducible archives
1 parent bd5cc61 commit b805279

File tree

10 files changed

+117
-8
lines changed

10 files changed

+117
-8
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>plexus-archiver</artifactId>
13-
<version>4.1.1-SNAPSHOT</version>
13+
<version>4.2.0-SNAPSHOT</version>
1414
<name>Plexus Archiver Component</name>
1515

1616
<scm>

src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java

+18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.reflect.UndeclaredThrowableException;
2323
import java.nio.charset.Charset;
2424
import java.util.ArrayList;
25+
import java.util.Date;
2526
import java.util.HashMap;
2627
import java.util.HashSet;
2728
import java.util.Iterator;
@@ -103,6 +104,11 @@ public abstract class AbstractArchiver
103104
*/
104105
private boolean useJvmChmod = true;
105106

107+
/**
108+
* @since 4.2.0
109+
*/
110+
private Date lastModifiedDate;
111+
106112
// contextualized.
107113
private ArchiverManager archiverManager;
108114

@@ -1133,4 +1139,16 @@ public void setIgnorePermissions( final boolean ignorePermissions )
11331139
this.ignorePermissions = ignorePermissions;
11341140
}
11351141

1142+
@Override
1143+
public void setLastModifiedDate( Date lastModifiedDate )
1144+
{
1145+
this.lastModifiedDate = lastModifiedDate;
1146+
}
1147+
1148+
@Override
1149+
public Date getLastModifiedDate()
1150+
{
1151+
return lastModifiedDate;
1152+
}
1153+
11361154
}

src/main/java/org/codehaus/plexus/archiver/Archiver.java

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.nio.charset.Charset;
22+
import java.util.Date;
2223
import java.util.HashSet;
2324
import java.util.Map;
2425
import java.util.Set;
@@ -399,4 +400,17 @@ ResourceIterator getResources()
399400
*/
400401
void setIgnorePermissions( final boolean ignorePermissions );
401402

403+
/**
404+
* Define forced last modification date for entries (if non null).
405+
*
406+
* @param lastModifiedDate
407+
* @since 4.2.0
408+
*/
409+
void setLastModifiedDate( final Date lastModifiedDate );
410+
411+
/**
412+
* @since 4.2.0
413+
*/
414+
Date getLastModifiedDate();
415+
402416
}

src/main/java/org/codehaus/plexus/archiver/diags/DelgatingArchiver.java

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.File;
1919
import java.io.IOException;
2020
import java.nio.charset.Charset;
21+
import java.util.Date;
2122
import java.util.Map;
2223
import javax.annotation.Nonnull;
2324
import org.codehaus.plexus.archiver.ArchiveEntry;
@@ -331,4 +332,16 @@ public void setIgnorePermissions( boolean ignorePermissions )
331332
target.setIgnorePermissions( ignorePermissions );
332333
}
333334

335+
@Override
336+
public void setLastModifiedDate( final Date lastModifiedDate )
337+
{
338+
target.setLastModifiedDate( lastModifiedDate );
339+
}
340+
341+
@Override
342+
public Date getLastModifiedDate()
343+
{
344+
return target.getLastModifiedDate();
345+
}
346+
334347
}

src/main/java/org/codehaus/plexus/archiver/diags/NoOpArchiver.java

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.nio.charset.Charset;
2121
import java.util.Collections;
22+
import java.util.Date;
2223
import java.util.Map;
2324
import javax.annotation.Nonnull;
2425
import org.codehaus.plexus.archiver.ArchiveEntry;
@@ -346,4 +347,16 @@ public void setIgnorePermissions( boolean ignorePermissions )
346347
this.ignorePermissions = ignorePermissions;
347348
}
348349

350+
@Override
351+
public void setLastModifiedDate( final Date lastModifiedDate )
352+
{
353+
354+
}
355+
356+
@Override
357+
public Date getLastModifiedDate()
358+
{
359+
return null;
360+
}
361+
349362
}

src/main/java/org/codehaus/plexus/archiver/diags/TrackingArchiver.java

+15
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.nio.charset.Charset;
2424
import java.util.ArrayList;
25+
import java.util.Date;
2526
import java.util.HashMap;
2627
import java.util.List;
2728
import java.util.Map;
@@ -52,6 +53,8 @@ public class TrackingArchiver
5253

5354
private boolean ignorePermissions;
5455

56+
private Date lastModified;
57+
5558
@Override
5659
public void createArchive()
5760
throws ArchiverException, IOException
@@ -400,4 +403,16 @@ public void setIgnorePermissions( final boolean ignorePermissions )
400403
this.ignorePermissions = ignorePermissions;
401404
}
402405

406+
@Override
407+
public void setLastModifiedDate( final Date lastModifiedDate )
408+
{
409+
this.lastModified = lastModifiedDate;
410+
}
411+
412+
@Override
413+
public Date getLastModifiedDate()
414+
{
415+
return lastModified;
416+
}
417+
403418
}

src/main/java/org/codehaus/plexus/archiver/dir/DirectoryArchiver.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,16 @@ private void setFileModes( ArchiveEntry entry, File outFile, long inLastModified
194194
ArchiveEntryUtils.chmod( outFile, entry.getMode() );
195195
}
196196

197-
outFile.setLastModified( inLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE
198-
? System.currentTimeMillis()
199-
: inLastModified );
197+
if ( getLastModifiedDate() == null )
198+
{
199+
outFile.setLastModified( inLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE
200+
? System.currentTimeMillis()
201+
: inLastModified );
202+
}
203+
else
204+
{
205+
outFile.setLastModified( getLastModifiedDate().getTime() );
206+
}
200207
}
201208

202209
@Override

src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,17 @@ else if ( longFileMode.isFailMode() )
281281
te = new TarArchiveEntry( vPath );
282282
}
283283

284-
long teLastModified = entry.getResource().getLastModified();
285-
te.setModTime( teLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE
286-
? System.currentTimeMillis()
287-
: teLastModified );
284+
if ( getLastModifiedDate() == null )
285+
{
286+
long teLastModified = entry.getResource().getLastModified();
287+
te.setModTime( teLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE
288+
? System.currentTimeMillis()
289+
: teLastModified );
290+
}
291+
else
292+
{
293+
te.setModTime( getLastModifiedDate() );
294+
}
288295

289296
if ( entry.getType() == ArchiveEntry.SYMLINK )
290297
{

src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java

+5
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,11 @@ public InputStream get()
530530

531531
private void setTime( java.util.zip.ZipEntry zipEntry, long lastModified )
532532
{
533+
if ( getLastModifiedDate() != null )
534+
{
535+
lastModified = getLastModifiedDate().getTime();
536+
}
537+
533538
// Zip archives store file modification times with a
534539
// granularity of two seconds, so the times will either be rounded
535540
// up or down. If you round down, the archive will always seem

src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -886,4 +886,21 @@ public void testForcedFileModes()
886886
}
887887
}
888888

889+
public void testFixedEntryModificationTime()
890+
throws IOException
891+
{
892+
final long almostMinDosTime = 315532802000L;
893+
final File zipFile = getTestFile( "target/output/zip-with-fixed-entry-modification-times.zip" );
894+
final ZipArchiver archiver = getZipArchiver( zipFile );
895+
archiver.setLastModifiedDate( new Date( almostMinDosTime ) );
896+
archiver.addDirectory( new File( "src/test/resources/zip-timestamp" ) );
897+
archiver.createArchive();
898+
899+
assertTrue( zipFile.exists() );
900+
final ZipFile zf = new ZipFile( zipFile );
901+
assertEquals( almostMinDosTime, zf.getEntry( "file-with-even-time.txt" ).getTime() );
902+
assertEquals( almostMinDosTime, zf.getEntry( "file-with-odd-time.txt" ).getTime() );
903+
assertEquals( almostMinDosTime, zf.getEntry( "foo/" ).getTime() );
904+
}
905+
889906
}

0 commit comments

Comments
 (0)