From 533568134071254f3fedb16374f2849744032d88 Mon Sep 17 00:00:00 2001 From: Emmanuel Bourg Date: Wed, 14 Jun 2023 11:30:40 +0200 Subject: [PATCH] Unique stripper performing overwritting (#63) --- .../reproducible/DefaultZipStripper.java | 14 +----- .../zlika/reproducible/OverwriteStripper.java | 49 +++++++++++++++++++ .../zlika/reproducible/SmartTarStripper.java | 14 +----- .../SpringBootExecutableStripper.java | 10 +--- .../zlika/reproducible/StripJarMojo.java | 19 +++---- 5 files changed, 62 insertions(+), 44 deletions(-) create mode 100644 src/main/java/io/github/zlika/reproducible/OverwriteStripper.java diff --git a/src/main/java/io/github/zlika/reproducible/DefaultZipStripper.java b/src/main/java/io/github/zlika/reproducible/DefaultZipStripper.java index 8a1024b..b944fb5 100644 --- a/src/main/java/io/github/zlika/reproducible/DefaultZipStripper.java +++ b/src/main/java/io/github/zlika/reproducible/DefaultZipStripper.java @@ -15,8 +15,6 @@ import java.io.File; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; import java.util.Collections; import java.util.List; @@ -26,10 +24,6 @@ */ final class DefaultZipStripper implements Stripper { - /** - * Whether the original file should be overwritten. - */ - private final boolean overwrite; /** * The ZipStripper to configure. */ @@ -39,12 +33,10 @@ final class DefaultZipStripper implements Stripper /** * Constructor. * @param stripper The ZipStripper to wrap with default config. - * @param overwrite Overwrite original file. * @param manifestAttributes Additional manifest attributes to skip. */ - public DefaultZipStripper(ZipStripper stripper, boolean overwrite, List manifestAttributes) + public DefaultZipStripper(ZipStripper stripper, List manifestAttributes) { - this.overwrite = overwrite; this.manifestAttributes = Collections.unmodifiableList(manifestAttributes); this.stripper = configure(stripper); } @@ -53,10 +45,6 @@ public DefaultZipStripper(ZipStripper stripper, boolean overwrite, List public void strip(File zip, File stripped) throws IOException { this.stripper.strip(zip, stripped); - if (this.overwrite) - { - Files.move(stripped.toPath(), zip.toPath(), StandardCopyOption.REPLACE_EXISTING); - } } /** diff --git a/src/main/java/io/github/zlika/reproducible/OverwriteStripper.java b/src/main/java/io/github/zlika/reproducible/OverwriteStripper.java new file mode 100644 index 0000000..2d69001 --- /dev/null +++ b/src/main/java/io/github/zlika/reproducible/OverwriteStripper.java @@ -0,0 +1,49 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.github.zlika.reproducible; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; + +/** + * Stripper optionnally overwritting the input file with the stripped file. + */ +public class OverwriteStripper implements Stripper +{ + private final boolean overwrite; + private final Stripper stripper; + + /** + * Constructor. + * @param overwrite true to overwrite the original file. + * @param stripper Stripper to use to process the file. + */ + public OverwriteStripper(boolean overwrite, Stripper stripper) + { + this.overwrite = overwrite; + this.stripper = stripper; + } + + @Override + public void strip(File in, File out) throws IOException + { + stripper.strip(in, out); + if (this.overwrite) + { + Files.move(out.toPath(), in.toPath(), StandardCopyOption.REPLACE_EXISTING); + } + } +} diff --git a/src/main/java/io/github/zlika/reproducible/SmartTarStripper.java b/src/main/java/io/github/zlika/reproducible/SmartTarStripper.java index afd1ca5..e7d5ac3 100644 --- a/src/main/java/io/github/zlika/reproducible/SmartTarStripper.java +++ b/src/main/java/io/github/zlika/reproducible/SmartTarStripper.java @@ -15,8 +15,6 @@ import java.io.File; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; import java.time.LocalDateTime; /** @@ -26,20 +24,14 @@ */ final class SmartTarStripper implements Stripper { - /** - * Whether the original file should be overwritten. - */ - private final boolean overwrite; private final LocalDateTime reproducibleDateTime; /** * Constructor. - * @param overwrite Overwrite original file. * @param reproducibleDateTime the date/time to use in TAR entries. */ - public SmartTarStripper(boolean overwrite, LocalDateTime reproducibleDateTime) + public SmartTarStripper(LocalDateTime reproducibleDateTime) { - this.overwrite = overwrite; this.reproducibleDateTime = reproducibleDateTime; } @@ -48,10 +40,6 @@ public void strip(final File file, final File stripped) throws IOException { final Stripper stripper = findImplementation(file); stripper.strip(file, stripped); - if (this.overwrite) - { - Files.move(stripped.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING); - } } /** diff --git a/src/main/java/io/github/zlika/reproducible/SpringBootExecutableStripper.java b/src/main/java/io/github/zlika/reproducible/SpringBootExecutableStripper.java index 5774394..ac2a39c 100644 --- a/src/main/java/io/github/zlika/reproducible/SpringBootExecutableStripper.java +++ b/src/main/java/io/github/zlika/reproducible/SpringBootExecutableStripper.java @@ -22,7 +22,6 @@ import java.io.IOException; import java.io.RandomAccessFile; import java.nio.file.Files; -import java.nio.file.StandardCopyOption; /** * Strips non-reproducible data from a JAR/WAR/ZIP file repackaged by @@ -37,17 +36,14 @@ public class SpringBootExecutableStripper implements Stripper private static final byte[] ZIP_FILE_HEADER = new byte[] { 0x50, 0x4B, 0x03, 0x04 }; private final DefaultZipStripper zipStripper; - private final boolean overwrite; /** * Constructor. - * @param overwrite true to overwrite the original file. * @param zipStripper Stripper to use to process the ZIP file. */ - public SpringBootExecutableStripper(boolean overwrite, DefaultZipStripper zipStripper) + public SpringBootExecutableStripper(DefaultZipStripper zipStripper) { this.zipStripper = zipStripper; - this.overwrite = overwrite; } @Override @@ -69,10 +65,6 @@ public void strip(File in, File out) throws IOException Files.delete(tmp.toPath()); Files.delete(tmp2.toPath()); } - if (this.overwrite) - { - Files.move(out.toPath(), in.toPath(), StandardCopyOption.REPLACE_EXISTING); - } } private byte[] extractLaunchScript(File file) throws IOException diff --git a/src/main/java/io/github/zlika/reproducible/StripJarMojo.java b/src/main/java/io/github/zlika/reproducible/StripJarMojo.java index 2396019..e7e33a4 100644 --- a/src/main/java/io/github/zlika/reproducible/StripJarMojo.java +++ b/src/main/java/io/github/zlika/reproducible/StripJarMojo.java @@ -154,13 +154,13 @@ public void execute() throws MojoExecutionException DateTimeFormatter.ofPattern(zipDateTimeFormatPattern)); final ZipStripper zipStripper = new ZipStripper(reproducibleDateTime, fixZipExternalFileAttributes); newLineTextFiles.forEach(f -> zipStripper.addFileStripper(f, LineEndingsStripper.INSTANCE)); - final DefaultZipStripper stripper = new DefaultZipStripper(zipStripper, this.overwrite, - this.manifestAttributes); + final Stripper stripper = new OverwriteStripper(this.overwrite, new DefaultZipStripper(zipStripper, + this.manifestAttributes)); if (this.nestedIncludes != null && !this.nestedIncludes.isEmpty()) { final Stripper nestedFileStripper = - new DefaultZipStripper(zipStripper, false, this.manifestAttributes); + new DefaultZipStripper(zipStripper, this.manifestAttributes); for (final String include : this.nestedIncludes) { if (include.endsWith("jar") || include.endsWith("zip")) @@ -175,21 +175,22 @@ public void execute() throws MojoExecutionException stripper ); this.process( - this.findSpringBootExecutable(this.outputDirectory), - new SpringBootExecutableStripper(this.overwrite, - new DefaultZipStripper(zipStripper, false, this.manifestAttributes)) + this.findSpringBootExecutable(this.outputDirectory), + new OverwriteStripper(this.overwrite, + new SpringBootExecutableStripper( + new DefaultZipStripper(zipStripper, this.manifestAttributes))) ); this.process( this.findTarFiles(this.outputDirectory), - new SmartTarStripper(this.overwrite, reproducibleDateTime) + new OverwriteStripper(this.overwrite, new SmartTarStripper(reproducibleDateTime)) ); this.process( this.findTarBzFiles(this.outputDirectory), - new SmartTarStripper(this.overwrite, reproducibleDateTime) + new OverwriteStripper(this.overwrite, new SmartTarStripper(reproducibleDateTime)) ); this.process( this.findTarGzFiles(this.outputDirectory), - new SmartTarStripper(this.overwrite, reproducibleDateTime) + new OverwriteStripper(this.overwrite, new SmartTarStripper(reproducibleDateTime)) ); } }