From 1c2bfeeeaf8877d152e97fb3ba449548a34959e6 Mon Sep 17 00:00:00 2001 From: David Walluck Date: Sun, 31 Mar 2024 20:48:27 -0400 Subject: [PATCH 1/2] Use java.nio.file instead of java.io --- .../org/eclipse/packager/deb/Packages.java | 10 +++- .../deb/build/DebianPackageWriter.java | 50 +++++++++++++------ .../deb/build/FileContentProvider.java | 18 +++++-- .../deb/build/TextFileContentProvider.java | 15 +++--- .../deb/control/BinaryPackageControlFile.java | 7 +-- .../packager/deb/tests/BinaryPackageTest.java | 20 ++++---- .../org/eclipse/packager/rpm/app/Dumper.java | 3 +- .../signature/RpmFileSignatureProcessor.java | 26 ++++++---- .../eclipse/packager/rpm/EmptyWriterTest.java | 3 +- .../eclipse/packager/rpm/InputStreamTest.java | 8 +-- .../eclipse/packager/rpm/Issue130Test.java | 3 +- .../eclipse/packager/rpm/Issue136Test.java | 3 +- .../org/eclipse/packager/rpm/Issue24Test.java | 3 +- .../packager/rpm/SetVerifyFlagsTest.java | 3 +- .../org/eclipse/packager/rpm/WriterTest.java | 7 ++- .../RpmFileSignatureProcessorTest.java | 38 +++++++------- 16 files changed, 124 insertions(+), 93 deletions(-) diff --git a/deb/src/main/java/org/eclipse/packager/deb/Packages.java b/deb/src/main/java/org/eclipse/packager/deb/Packages.java index 2ab5320..5220657 100644 --- a/deb/src/main/java/org/eclipse/packager/deb/Packages.java +++ b/deb/src/main/java/org/eclipse/packager/deb/Packages.java @@ -13,11 +13,12 @@ package org.eclipse.packager.deb; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.List; @@ -36,8 +37,13 @@ public final class Packages { private Packages() { } + @Deprecated public static Map parseControlFile(final File packageFile) throws IOException, ParserException { - try (final ArArchiveInputStream in = new ArArchiveInputStream(new FileInputStream(packageFile))) { + return parseControlFile(packageFile.toPath()); + } + + public static Map parseControlFile(final Path packageFile) throws IOException, ParserException { + try (final ArArchiveInputStream in = new ArArchiveInputStream(Files.newInputStream(packageFile))) { ArchiveEntry ar; while ((ar = in.getNextEntry()) != null) { if (!ar.getName().equals("control.tar.gz")) { diff --git a/deb/src/main/java/org/eclipse/packager/deb/build/DebianPackageWriter.java b/deb/src/main/java/org/eclipse/packager/deb/build/DebianPackageWriter.java index 932c28a..5bfce9c 100644 --- a/deb/src/main/java/org/eclipse/packager/deb/build/DebianPackageWriter.java +++ b/deb/src/main/java/org/eclipse/packager/deb/build/DebianPackageWriter.java @@ -13,14 +13,14 @@ package org.eclipse.packager.deb.build; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.StringWriter; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.security.MessageDigest; import java.time.Instant; import java.util.HashMap; @@ -46,7 +46,7 @@ import com.google.common.io.ByteStreams; public class DebianPackageWriter implements AutoCloseable, BinaryPackageBuilder { - public static final Charset CHARSET = Charset.forName("UTF-8"); + public static final Charset CHARSET = StandardCharsets.UTF_8; private static final int AR_ARCHIVE_DEFAULT_MODE = 33188; // see ArArchive @@ -56,7 +56,7 @@ public class DebianPackageWriter implements AutoCloseable, BinaryPackageBuilder private final Supplier timestampSupplier; - private final File dataTemp; + private final Path dataTemp; private final TarArchiveOutputStream dataStream; @@ -95,19 +95,37 @@ public DebianPackageWriter(final OutputStream stream, final BinaryPackageControl this.ar.write(this.binaryHeader); this.ar.closeArchiveEntry(); - this.dataTemp = Files.createTempFile("data", null).toFile(); + this.dataTemp = Files.createTempFile("data", null); - this.dataStream = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(this.dataTemp))); + this.dataStream = new TarArchiveOutputStream(new GZIPOutputStream(Files.newOutputStream(this.dataTemp))); this.dataStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); } + @Deprecated public void addFile(final File file, final String fileName, final EntryInformation entryInformation) throws IOException { - addFile(new FileContentProvider(file), fileName, entryInformation, Optional.of((Supplier) () -> { - return file == null || !file.canRead() ? null : Instant.ofEpochMilli(file.lastModified()); + addFile(file.toPath(), fileName, entryInformation); + } + + public void addFile(final Path file, final String fileName, final EntryInformation entryInformation) throws IOException { + addFile(new FileContentProvider(file), fileName, entryInformation, Optional.of(() -> { + if (file == null || !Files.isReadable(file)) { + return null; + } + + try { + return Files.getLastModifiedTime(file).toInstant(); + } catch (IOException e) { + return null; + } })); } + @Deprecated public void addFile(final File file, final String fileName, final EntryInformation entryInformation, final Optional> timestampSupplier) throws IOException { + addFile(file.toPath(), fileName, entryInformation, timestampSupplier); + } + + public void addFile(final Path file, final String fileName, final EntryInformation entryInformation, final Optional> timestampSupplier) throws IOException { addFile(new FileContentProvider(file), fileName, entryInformation, timestampSupplier); } @@ -259,14 +277,14 @@ public void close() throws IOException { this.ar.close(); } } finally { - this.dataTemp.delete(); + Files.delete(this.dataTemp); } } - private void buildAndAddControlFile(final Supplier timestampSupplier) throws IOException, FileNotFoundException { - final File controlFile = File.createTempFile("control", null); + private void buildAndAddControlFile(final Supplier timestampSupplier) throws IOException { + final Path controlFile = Files.createTempFile("control", null); try { - try (GZIPOutputStream gout = new GZIPOutputStream(new FileOutputStream(controlFile)); + try (GZIPOutputStream gout = new GZIPOutputStream(Files.newOutputStream(controlFile)); TarArchiveOutputStream tout = new TarArchiveOutputStream(gout)) { tout.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); @@ -280,7 +298,7 @@ private void buildAndAddControlFile(final Supplier timestampSupplier) t } addArFile(controlFile, "control.tar.gz", timestampSupplier); } finally { - controlFile.delete(); + Files.delete(controlFile); } } @@ -352,11 +370,11 @@ protected ContentProvider createConfFilesContent() throws IOException { return new StaticContentProvider(sw.toString()); } - private void addArFile(final File file, final String entryName, final Supplier timestampSupplier) throws IOException { - final ArArchiveEntry entry = new ArArchiveEntry(entryName, file.length(), 0, 0, AR_ARCHIVE_DEFAULT_MODE, timestampSupplier.get().getEpochSecond()); + private void addArFile(final Path file, final String entryName, final Supplier timestampSupplier) throws IOException { + final ArArchiveEntry entry = new ArArchiveEntry(entryName, Files.size(file), 0, 0, AR_ARCHIVE_DEFAULT_MODE, timestampSupplier.get().getEpochSecond()); this.ar.putArchiveEntry(entry); - Files.copy(file.toPath(), this.ar); + Files.copy(file, this.ar); this.ar.closeArchiveEntry(); } diff --git a/deb/src/main/java/org/eclipse/packager/deb/build/FileContentProvider.java b/deb/src/main/java/org/eclipse/packager/deb/build/FileContentProvider.java index 8a047c3..6eb404b 100644 --- a/deb/src/main/java/org/eclipse/packager/deb/build/FileContentProvider.java +++ b/deb/src/main/java/org/eclipse/packager/deb/build/FileContentProvider.java @@ -13,21 +13,31 @@ package org.eclipse.packager.deb.build; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; public class FileContentProvider implements ContentProvider { - private final File file; + private final Path file; + @Deprecated public FileContentProvider(final File file) { + this(file.toPath()); + } + + public FileContentProvider(final Path file) { this.file = file; } @Override public long getSize() { - return this.file.length(); + try { + return Files.size(this.file); + } catch (final IOException e) { + return 0L; + } } @Override @@ -36,7 +46,7 @@ public InputStream createInputStream() throws IOException { return null; } - return new FileInputStream(this.file); + return Files.newInputStream(this.file); } @Override diff --git a/deb/src/main/java/org/eclipse/packager/deb/build/TextFileContentProvider.java b/deb/src/main/java/org/eclipse/packager/deb/build/TextFileContentProvider.java index 42181af..94ce975 100644 --- a/deb/src/main/java/org/eclipse/packager/deb/build/TextFileContentProvider.java +++ b/deb/src/main/java/org/eclipse/packager/deb/build/TextFileContentProvider.java @@ -14,19 +14,23 @@ import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; - -import com.google.common.io.Files; +import java.nio.file.Files; +import java.nio.file.Path; public class TextFileContentProvider implements ContentProvider { private final byte[] data; - public TextFileContentProvider(final File file) throws FileNotFoundException, IOException { + @Deprecated + public TextFileContentProvider(final File file) throws IOException { + this(file.toPath()); + } + + public TextFileContentProvider(final Path file) throws IOException { if (file != null) { - String data = Files.asCharSource(file, StandardCharsets.UTF_8).read(); + String data = Files.readString(file); if (needFix()) { data = fix(data); } @@ -62,5 +66,4 @@ public InputStream createInputStream() throws IOException { public boolean hasContent() { return this.data != null; } - } diff --git a/deb/src/main/java/org/eclipse/packager/deb/control/BinaryPackageControlFile.java b/deb/src/main/java/org/eclipse/packager/deb/control/BinaryPackageControlFile.java index b661aa2..f600d67 100644 --- a/deb/src/main/java/org/eclipse/packager/deb/control/BinaryPackageControlFile.java +++ b/deb/src/main/java/org/eclipse/packager/deb/control/BinaryPackageControlFile.java @@ -14,6 +14,7 @@ import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -136,10 +137,6 @@ private static void hasField(final BinaryPackageControlFile controlFile, final S public String makeFileName() { final String name = String.format("%s_%s_%s.deb", getPackage(), getVersion(), getArchitecture()); - try { - return URLEncoder.encode(name, "UTF-8"); - } catch (final UnsupportedEncodingException e) { - throw new RuntimeException(e); - } + return URLEncoder.encode(name, StandardCharsets.UTF_8); } } diff --git a/deb/src/test/java/org/eclipse/packager/deb/tests/BinaryPackageTest.java b/deb/src/test/java/org/eclipse/packager/deb/tests/BinaryPackageTest.java index b4c8657..84fff62 100644 --- a/deb/src/test/java/org/eclipse/packager/deb/tests/BinaryPackageTest.java +++ b/deb/src/test/java/org/eclipse/packager/deb/tests/BinaryPackageTest.java @@ -12,11 +12,9 @@ */ package org.eclipse.packager.deb.tests; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.time.Instant; import java.util.Optional; import java.util.function.Supplier; @@ -33,32 +31,32 @@ public class BinaryPackageTest { @SuppressWarnings("deprecation") @Test public void test1() throws IOException, InterruptedException { - final File file1 = Files.createTempFile("test-1-", ".deb").toFile(); - final File file2 = Files.createTempFile("test-2-", ".deb").toFile(); + final Path file1 = Files.createTempFile("test-1-", ".deb"); + final Path file2 = Files.createTempFile("test-2-", ".deb"); final Instant now = Instant.now(); final Supplier timestampProvider = () -> now; createDebFile(file1, timestampProvider); System.out.println("File: " + file1); - Assertions.assertTrue(file1.exists(), "File exists"); + Assertions.assertTrue(Files.exists(file1), "File exists"); Thread.sleep(1_001); // sleep for a second to make sure that a timestamp might be changed createDebFile(file2, timestampProvider); System.out.println("File: " + file2); - Assertions.assertTrue(file2.exists(), "File exists"); + Assertions.assertTrue(Files.exists(file2), "File exists"); - final byte[] b1 = Files.readAllBytes(file1.toPath()); + final byte[] b1 = Files.readAllBytes(file1); final String h1 = Hashing.md5().hashBytes(b1).toString(); - final byte[] b2 = Files.readAllBytes(file2.toPath()); + final byte[] b2 = Files.readAllBytes(file2); final String h2 = Hashing.md5().hashBytes(b2).toString(); System.out.println(h1); System.out.println(h2); Assertions.assertEquals(h1, h2); } - private void createDebFile(final File file, final Supplier timestampProvider) throws IOException, FileNotFoundException { + private void createDebFile(final Path file, final Supplier timestampProvider) throws IOException { final BinaryPackageControlFile packageFile = new BinaryPackageControlFile(); packageFile.setPackage("test"); packageFile.setVersion("0.0.1"); @@ -66,7 +64,7 @@ private void createDebFile(final File file, final Supplier timestampPro packageFile.setMaintainer("Jens Reimann "); packageFile.setDescription("Test package\nThis is just a test package\n\nNothing to worry about!"); - try (DebianPackageWriter deb = new DebianPackageWriter(new FileOutputStream(file), packageFile, timestampProvider)) { + try (DebianPackageWriter deb = new DebianPackageWriter(Files.newOutputStream(file), packageFile, timestampProvider)) { deb.addFile("Hello World\n".getBytes(), "/usr/share/foo-test/foo.txt", null, Optional.of(timestampProvider)); deb.addFile("Hello World\n".getBytes(), "/etc/foo.txt", EntryInformation.DEFAULT_FILE_CONF, Optional.of(timestampProvider)); } diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/app/Dumper.java b/rpm/src/main/java/org/eclipse/packager/rpm/app/Dumper.java index 83e8ecf..7590836 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/app/Dumper.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/app/Dumper.java @@ -18,7 +18,6 @@ import java.nio.ByteBuffer; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; @@ -150,7 +149,7 @@ private static void dumpEntry(final CpioArchiveEntry entry) { public static void main(final String[] args) throws IOException { for (final String file : args) { - dump(Paths.get(file)); + dump(Path.of(file)); } } diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessor.java b/rpm/src/main/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessor.java index c7443cc..c122f74 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessor.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessor.java @@ -14,12 +14,13 @@ import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -43,7 +44,7 @@ /** * Sign existing RPM file by calling - * {@link #perform(File, InputStream, String, OutputStream, HashAlgorithm)} + * {@link #perform(Path, InputStream, String, OutputStream, HashAlgorithm)} */ public class RpmFileSignatureProcessor { @@ -51,6 +52,12 @@ private RpmFileSignatureProcessor() { // Hide default constructor because of the static context } + @Deprecated + public static void perform(File rpm, InputStream privateKeyIn, String passphrase, OutputStream out, HashAlgorithm hashAlgorithm) + throws IOException, PGPException { + perform(rpm.toPath(), privateKeyIn, passphrase, out, hashAlgorithm); + } + /** *

* Perform the signature of the given RPM file with the given private key. This @@ -64,7 +71,7 @@ private RpmFileSignatureProcessor() { * @throws IOException * @throws PGPException */ - public static void perform(File rpm, InputStream privateKeyIn, String passphrase, OutputStream out, HashAlgorithm hashAlgorithm) + public static void perform(Path rpm, InputStream privateKeyIn, String passphrase, OutputStream out, HashAlgorithm hashAlgorithm) throws IOException, PGPException { final long leadLength = 96; @@ -77,15 +84,15 @@ public static void perform(File rpm, InputStream privateKeyIn, String passphrase long payloadSize = 0L; byte[] signatureHeader; - if (!rpm.exists()) { - throw new IOException("The file " + rpm.getName() + " does not exist"); + if (!Files.exists(rpm)) { + throw new IOException("The file " + rpm.getFileName() + " does not exist"); } // Extract private key PGPPrivateKey privateKey = getPrivateKey(privateKeyIn, passphrase); // Get the information of the RPM - try (RpmInputStream rpmIn = new RpmInputStream(new FileInputStream(rpm))) { + try (RpmInputStream rpmIn = new RpmInputStream(Files.newInputStream(rpm))) { signatureHeaderStart = rpmIn.getSignatureHeader().getStart(); signatureHeaderLength = rpmIn.getSignatureHeader().getLength(); payloadHeaderStart = rpmIn.getPayloadHeader().getStart(); @@ -97,12 +104,11 @@ public static void perform(File rpm, InputStream privateKeyIn, String passphrase if (signatureHeaderStart == 0L || signatureHeaderLength == 0L || payloadHeaderStart == 0L || payloadHeaderLength == 0L || payloadStart == 0L || archiveSize == 0L) { - throw new IOException("Unable to read " + rpm.getName() + " informations."); + throw new IOException("Unable to read " + rpm.getFileName() + " informations."); } // Build the signature header by digest payload header + payload - try (FileInputStream in = new FileInputStream(rpm)) { - FileChannel channelIn = in.getChannel(); + try (FileChannel channelIn = FileChannel.open(rpm)) { payloadSize = channelIn.size() - payloadStart; channelIn.position(leadLength + signatureHeaderLength); ByteBuffer payloadHeaderBuff = ByteBuffer.allocate((int) payloadHeaderLength); @@ -113,7 +119,7 @@ public static void perform(File rpm, InputStream privateKeyIn, String passphrase } // Write to the OutputStream - try (FileInputStream in = new FileInputStream(rpm)) { + try (InputStream in = Files.newInputStream(rpm)) { IOUtils.copyLarge(in, out, 0, leadLength); IOUtils.skip(in, signatureHeaderLength); out.write(signatureHeader); diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/EmptyWriterTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/EmptyWriterTest.java index b20e13f..a82473a 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/EmptyWriterTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/EmptyWriterTest.java @@ -17,7 +17,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import org.bouncycastle.openpgp.PGPException; import org.eclipse.packager.rpm.app.Dumper; @@ -28,7 +27,7 @@ import org.junit.jupiter.api.Test; public class EmptyWriterTest { - private static final Path OUT_BASE = Paths.get("target", "data", "out.empty"); + private static final Path OUT_BASE = Path.of("target", "data", "out.empty"); @BeforeAll public static void setup() throws IOException { diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/InputStreamTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/InputStreamTest.java index 7175500..fcfc4a5 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/InputStreamTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/InputStreamTest.java @@ -14,9 +14,9 @@ package org.eclipse.packager.rpm; import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import org.eclipse.packager.rpm.app.Dumper; import org.eclipse.packager.rpm.parse.RpmInputStream; @@ -27,7 +27,7 @@ public class InputStreamTest { @Test public void test1() throws IOException { - try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(new FileInputStream(new File("src/test/resources/data/org.eclipse.scada-0.2.1-1.noarch.rpm"))))) { + try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(Path.of("src/test/resources/data/org.eclipse.scada-0.2.1-1.noarch.rpm"))))) { Dumper.dumpAll(in); Assertions.assertEquals(280, in.getPayloadHeader().getStart()); @@ -61,7 +61,7 @@ public void test1() throws IOException { @Test public void test2() throws IOException { - try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(new FileInputStream(new File("src/test/resources/data/org.eclipse.scada-centos6-0.2.1-1.noarch.rpm"))))) { + try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(Path.of("src/test/resources/data/org.eclipse.scada-centos6-0.2.1-1.noarch.rpm"))))) { Dumper.dumpAll(in); } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/Issue130Test.java b/rpm/src/test/java/org/eclipse/packager/rpm/Issue130Test.java index 5caa78e..6ded393 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/Issue130Test.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/Issue130Test.java @@ -19,7 +19,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Arrays; import java.util.List; @@ -32,7 +31,7 @@ import org.junit.jupiter.api.Test; public class Issue130Test { - private static final Path OUT_BASE = Paths.get("target", "data", "out"); + private static final Path OUT_BASE = Path.of("target", "data", "out"); private static final List PREFIXES = Arrays.asList("/opt", "/var/log"); diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/Issue136Test.java b/rpm/src/test/java/org/eclipse/packager/rpm/Issue136Test.java index cfb8358..1f87d47 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/Issue136Test.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/Issue136Test.java @@ -21,7 +21,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import org.eclipse.packager.rpm.app.Dumper; import org.eclipse.packager.rpm.build.RpmBuilder; @@ -33,7 +32,7 @@ import org.junit.jupiter.params.provider.ValueSource; public class Issue136Test { - private static final Path OUT_BASE = Paths.get("target", "data", "out"); + private static final Path OUT_BASE = Path.of("target", "data", "out"); @BeforeAll public static void setup() throws IOException { diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/Issue24Test.java b/rpm/src/test/java/org/eclipse/packager/rpm/Issue24Test.java index a3880c4..17ccbed 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/Issue24Test.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/Issue24Test.java @@ -18,7 +18,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import org.eclipse.packager.rpm.build.BuilderOptions; import org.eclipse.packager.rpm.build.RpmBuilder; @@ -27,7 +26,7 @@ import org.junit.jupiter.api.Test; public class Issue24Test { - private static final Path OUT_BASE = Paths.get("target", "data", "out"); + private static final Path OUT_BASE = Path.of("target", "data", "out"); @BeforeAll public static void setup() throws IOException { diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/SetVerifyFlagsTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/SetVerifyFlagsTest.java index 7be9048..6019dd5 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/SetVerifyFlagsTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/SetVerifyFlagsTest.java @@ -19,7 +19,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; @@ -41,7 +40,7 @@ class SetVerifyFlagsTest { private final static Logger LOGGER = LoggerFactory.getLogger(SetVerifyFlagsTest.class); - private static final Path OUT_BASE = Paths.get("target", "data", "out"); + private static final Path OUT_BASE = Path.of("target", "data", "out"); @BeforeAll static void setup() throws IOException { diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/WriterTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/WriterTest.java index cc2977a..eda8104 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/WriterTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/WriterTest.java @@ -22,7 +22,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.LinkedList; @@ -47,9 +46,9 @@ import org.junit.jupiter.api.Test; public class WriterTest { - private static final Path OUT_BASE = Paths.get("target", "data", "out"); + private static final Path OUT_BASE = Path.of("target", "data", "out"); - private static final Path IN_BASE = Paths.get("src", "test", "resources", "data", "in"); + private static final Path IN_BASE = Path.of("src", "test", "resources", "data", "in"); @BeforeAll public static void setup() throws IOException { @@ -193,7 +192,7 @@ public void test3() throws IOException, PGPException { final String keyPassphrase = System.getProperty("writerTest.keyPassphrase"); if (keyId != null && keyChain != null) { - try (InputStream stream = Files.newInputStream(Paths.get(keyChain))) { + try (InputStream stream = Files.newInputStream(Path.of(keyChain))) { builder.addSignatureProcessor(new RsaHeaderSignatureProcessor(PgpHelper.loadPrivateKey(stream, keyId, keyPassphrase), HashAlgorithm.from(System.getProperty("writerTest.hashAlgo")))); } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessorTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessorTest.java index a3931cb..9961e4e 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessorTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessorTest.java @@ -13,16 +13,17 @@ ********************************************************************************/ package org.eclipse.packager.rpm.signature; +import static java.nio.file.StandardOpenOption.CREATE_NEW; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -62,30 +63,29 @@ public class RpmFileSignatureProcessorTest { public void testSigningExistingRpm() throws IOException, PGPException { // Read files final String passPhrase = "testkey"; // Do not change - File rpm = new File(SOURCE_FILE_PATH); - File private_key = new File(PRIVATE_KEY_PATH); - if (!rpm.exists() || !private_key.exists()) { + Path rpm = Path.of(SOURCE_FILE_PATH); + Path private_key = Path.of(PRIVATE_KEY_PATH); + if (!Files.exists(rpm) || !Files.exists(private_key)) { fail("Input files rpm or private_key does not exist"); } // Init the signed RPM - File resultDirectory = new File(RESULT_DIR); - resultDirectory.mkdirs(); - File signedRpm = new File(RESULT_FILE_PATH); - signedRpm.createNewFile(); + Path resultDirectory = Path.of(RESULT_DIR); + Files.createDirectories(resultDirectory); + Path signedRpm = Path.of(RESULT_FILE_PATH); - try (FileOutputStream resultOut = new FileOutputStream(signedRpm); - InputStream privateKeyStream = new FileInputStream(private_key)) { + try (OutputStream resultOut = Files.newOutputStream(signedRpm, CREATE_NEW); + InputStream privateKeyStream = Files.newInputStream(private_key)) { // Sign the RPM RpmFileSignatureProcessor.perform(rpm, privateKeyStream, passPhrase, resultOut, HashAlgorithm.SHA256); // Read the initial (unsigned) rpm file - RpmInputStream initialRpm = new RpmInputStream(new FileInputStream(rpm)); + RpmInputStream initialRpm = new RpmInputStream(Files.newInputStream(rpm)); initialRpm.available(); initialRpm.close(); InputHeader initialHeader = initialRpm.getSignatureHeader(); // Read the signed rpm file - RpmInputStream rpmSigned = new RpmInputStream(new FileInputStream(signedRpm)); + RpmInputStream rpmSigned = new RpmInputStream(Files.newInputStream(signedRpm)); rpmSigned.available(); rpmSigned.close(); InputHeader signedHeader = rpmSigned.getSignatureHeader(); @@ -118,17 +118,17 @@ public void testSigningExistingRpm() throws IOException, PGPException { @Order(2) public void verifyRpmSignature() throws Exception { // get the files, as absolute paths, as podman will need absolute paths - File publicKey = new File(PUBLIC_KEY_PATH).getAbsoluteFile(); - File signedRpm = new File(RESULT_FILE_PATH).getAbsoluteFile(); + Path publicKey = Path.of(PUBLIC_KEY_PATH).toAbsolutePath(); + Path signedRpm = Path.of(RESULT_FILE_PATH).toAbsolutePath(); // check if the output from the previous test is found - if (!publicKey.exists() || !signedRpm.exists()) { + if (!Files.exists(publicKey) || !Files.exists(signedRpm)) { fail("Input files signedRpm or publicKey does not exist"); } // extract the plain file name - String publicKeyName = publicKey.getName(); - String rpmFileName = signedRpm.getName(); + String publicKeyName = publicKey.getFileName().toString(); + String rpmFileName = signedRpm.getFileName().toString(); // prepare the script for validating the signature, this includes importing the key and running a verbose check String script = String.format("rpm --import /%s && rpm --verbose --checksig /%s", publicKeyName, rpmFileName); From 8685f8c96ce1b9a6f4e980c52444580935454bae Mon Sep 17 00:00:00 2001 From: David Walluck Date: Wed, 3 Apr 2024 11:10:52 -0400 Subject: [PATCH 2/2] Remove deprecated methods --- .../main/java/org/eclipse/packager/deb/Packages.java | 6 ------ .../packager/deb/build/DebianPackageWriter.java | 11 ----------- .../packager/deb/build/FileContentProvider.java | 7 ------- .../packager/deb/build/TextFileContentProvider.java | 8 +------- .../rpm/signature/RpmFileSignatureProcessor.java | 8 -------- 5 files changed, 1 insertion(+), 39 deletions(-) diff --git a/deb/src/main/java/org/eclipse/packager/deb/Packages.java b/deb/src/main/java/org/eclipse/packager/deb/Packages.java index 5220657..9b07a3f 100644 --- a/deb/src/main/java/org/eclipse/packager/deb/Packages.java +++ b/deb/src/main/java/org/eclipse/packager/deb/Packages.java @@ -12,7 +12,6 @@ */ package org.eclipse.packager.deb; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; @@ -37,11 +36,6 @@ public final class Packages { private Packages() { } - @Deprecated - public static Map parseControlFile(final File packageFile) throws IOException, ParserException { - return parseControlFile(packageFile.toPath()); - } - public static Map parseControlFile(final Path packageFile) throws IOException, ParserException { try (final ArArchiveInputStream in = new ArArchiveInputStream(Files.newInputStream(packageFile))) { ArchiveEntry ar; diff --git a/deb/src/main/java/org/eclipse/packager/deb/build/DebianPackageWriter.java b/deb/src/main/java/org/eclipse/packager/deb/build/DebianPackageWriter.java index 5bfce9c..4d6ec14 100644 --- a/deb/src/main/java/org/eclipse/packager/deb/build/DebianPackageWriter.java +++ b/deb/src/main/java/org/eclipse/packager/deb/build/DebianPackageWriter.java @@ -12,7 +12,6 @@ */ package org.eclipse.packager.deb.build; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -101,11 +100,6 @@ public DebianPackageWriter(final OutputStream stream, final BinaryPackageControl this.dataStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); } - @Deprecated - public void addFile(final File file, final String fileName, final EntryInformation entryInformation) throws IOException { - addFile(file.toPath(), fileName, entryInformation); - } - public void addFile(final Path file, final String fileName, final EntryInformation entryInformation) throws IOException { addFile(new FileContentProvider(file), fileName, entryInformation, Optional.of(() -> { if (file == null || !Files.isReadable(file)) { @@ -120,11 +114,6 @@ public void addFile(final Path file, final String fileName, final EntryInformati })); } - @Deprecated - public void addFile(final File file, final String fileName, final EntryInformation entryInformation, final Optional> timestampSupplier) throws IOException { - addFile(file.toPath(), fileName, entryInformation, timestampSupplier); - } - public void addFile(final Path file, final String fileName, final EntryInformation entryInformation, final Optional> timestampSupplier) throws IOException { addFile(new FileContentProvider(file), fileName, entryInformation, timestampSupplier); } diff --git a/deb/src/main/java/org/eclipse/packager/deb/build/FileContentProvider.java b/deb/src/main/java/org/eclipse/packager/deb/build/FileContentProvider.java index 6eb404b..c5c0eca 100644 --- a/deb/src/main/java/org/eclipse/packager/deb/build/FileContentProvider.java +++ b/deb/src/main/java/org/eclipse/packager/deb/build/FileContentProvider.java @@ -12,21 +12,14 @@ */ package org.eclipse.packager.deb.build; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; public class FileContentProvider implements ContentProvider { - private final Path file; - @Deprecated - public FileContentProvider(final File file) { - this(file.toPath()); - } - public FileContentProvider(final Path file) { this.file = file; } diff --git a/deb/src/main/java/org/eclipse/packager/deb/build/TextFileContentProvider.java b/deb/src/main/java/org/eclipse/packager/deb/build/TextFileContentProvider.java index 94ce975..0994d0b 100644 --- a/deb/src/main/java/org/eclipse/packager/deb/build/TextFileContentProvider.java +++ b/deb/src/main/java/org/eclipse/packager/deb/build/TextFileContentProvider.java @@ -13,7 +13,6 @@ package org.eclipse.packager.deb.build; import java.io.ByteArrayInputStream; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; @@ -23,11 +22,6 @@ public class TextFileContentProvider implements ContentProvider { private final byte[] data; - @Deprecated - public TextFileContentProvider(final File file) throws IOException { - this(file.toPath()); - } - public TextFileContentProvider(final Path file) throws IOException { if (file != null) { String data = Files.readString(file); @@ -54,7 +48,7 @@ public long getSize() { } @Override - public InputStream createInputStream() throws IOException { + public InputStream createInputStream() { if (this.data != null) { return new ByteArrayInputStream(this.data); } else { diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessor.java b/rpm/src/main/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessor.java index c122f74..fbcac44 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessor.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessor.java @@ -13,7 +13,6 @@ package org.eclipse.packager.rpm.signature; import java.io.ByteArrayOutputStream; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -47,17 +46,10 @@ * {@link #perform(Path, InputStream, String, OutputStream, HashAlgorithm)} */ public class RpmFileSignatureProcessor { - private RpmFileSignatureProcessor() { // Hide default constructor because of the static context } - @Deprecated - public static void perform(File rpm, InputStream privateKeyIn, String passphrase, OutputStream out, HashAlgorithm hashAlgorithm) - throws IOException, PGPException { - perform(rpm.toPath(), privateKeyIn, passphrase, out, hashAlgorithm); - } - /** *

* Perform the signature of the given RPM file with the given private key. This