Skip to content

Commit

Permalink
Unique stripper performing overwritting (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebourg committed Jun 14, 2023
1 parent 56c9db9 commit 5335681
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,10 +24,6 @@
*/
final class DefaultZipStripper implements Stripper
{
/**
* Whether the original file should be overwritten.
*/
private final boolean overwrite;
/**
* The ZipStripper to configure.
*/
Expand All @@ -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<String> manifestAttributes)
public DefaultZipStripper(ZipStripper stripper, List<String> manifestAttributes)
{
this.overwrite = overwrite;
this.manifestAttributes = Collections.unmodifiableList(manifestAttributes);
this.stripper = configure(stripper);
}
Expand All @@ -53,10 +45,6 @@ public DefaultZipStripper(ZipStripper stripper, boolean overwrite, List<String>
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);
}
}

/**
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/io/github/zlika/reproducible/OverwriteStripper.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
14 changes: 1 addition & 13 deletions src/main/java/io/github/zlika/reproducible/SmartTarStripper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}

Expand All @@ -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);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/io/github/zlika/reproducible/StripJarMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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))
);
}
}
Expand Down

0 comments on commit 5335681

Please sign in to comment.