Skip to content

Commit

Permalink
Don't write LIBARCHIVE.creationtime attribute in tar.gz archive entries
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Nov 10, 2024
1 parent 644171a commit 207a9b3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ public final class ProductArchiverMojo extends AbstractProductMojo {
@Parameter
private boolean parallel;

/**
* Controls if for {@code .tar.gz} archives the creation-time is stored as
* {@code LIBARCHIVE.creationtime } attribute in each entry. Currently {@code GNU tar} does not
* support that attributes and emits warnings about the {@code unknown extended header keyword
* 'LIBARCHIVE.creationtime'} when extracting such archive.
*/
@Parameter(defaultValue = "true")
private boolean storeTarGZCreationTime;

@Component
private MavenProjectHelper helper;

Expand Down Expand Up @@ -230,6 +239,7 @@ private void materialize(Product product, TargetEnvironment env) throws MojoExec

private void createCommonsCompressTarGz(File productArchive, File sourceDir) throws IOException {
TarGzArchiver archiver = new TarGzArchiver();
archiver.setStoreCreationTimeAttribute(storeTarGZCreationTime);
archiver.setLog(getLog());
archiver.addDirectory(sourceDir);
archiver.setDestFile(productArchive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class TarGzArchiver {
private File destFile;
private List<File> sourceDirs = new ArrayList<>();
private Log log = new SystemStreamLog();
private boolean storeCreationTime;

public TarGzArchiver() {
}
Expand All @@ -63,19 +64,21 @@ public void setDestFile(File destFile) {
this.destFile = destFile;
}

public void setStoreCreationTimeAttribute(boolean storeCreationTime) {
this.storeCreationTime = storeCreationTime;
}

public void addDirectory(File directory) {
this.sourceDirs.add(directory);
}

public void createArchive() throws IOException {
validate();
log.info("Building tar: " + destFile);
TarArchiveOutputStream tarStream = null;
try {
destFile.getAbsoluteFile().getParentFile().mkdirs();
GzipCompressorOutputStream gzipStream = new GzipCompressorOutputStream(
new BufferedOutputStream(new FileOutputStream(destFile)));
tarStream = new TarArchiveOutputStream(gzipStream, "UTF-8");
destFile.getAbsoluteFile().getParentFile().mkdirs();
try (GzipCompressorOutputStream gzipStream = new GzipCompressorOutputStream(
new BufferedOutputStream(new FileOutputStream(destFile)));
TarArchiveOutputStream tarStream = new TarArchiveOutputStream(gzipStream, "UTF-8");) {
// allow "long" file paths (> 100 chars)
tarStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
tarStream.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
Expand All @@ -84,10 +87,6 @@ public void createArchive() throws IOException {
addToTarRecursively(sourceDir, child, tarStream);
}
}
} finally {
if (tarStream != null) {
tarStream.close();
}
}
}

Expand Down Expand Up @@ -133,6 +132,9 @@ private TarArchiveEntry createTarEntry(File tarRootDir, File source) throws IOEx
tarEntry.setMode(FilePermissionHelper.toOctalFileMode(attrs.permissions()));
}
tarEntry.setModTime(source.lastModified());
if (!storeCreationTime) { // GNU tar cannot handle 'LIBARCHIVE.creationtime' attributes and emits a lot of warnings on it
tarEntry.setCreationTime(null);
}
return tarEntry;
}

Expand Down

0 comments on commit 207a9b3

Please sign in to comment.