diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/ReadableHeader.java b/rpm/src/main/java/org/eclipse/packager/rpm/ReadableHeader.java index da9772d..66ee3ba 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/ReadableHeader.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/ReadableHeader.java @@ -13,14 +13,22 @@ package org.eclipse.packager.rpm; -import java.util.Optional; +import java.util.List; public interface ReadableHeader { - /** - * Get the value from a header structure - * - * @param tag the tag - * @return the optional value - */ - public Optional getValue(T tag); + boolean hasTag(T tag); + + String getString(T tag); + + Integer getInteger(T tag); + + Long getLong(T tag); + + List getStringList(T tag); + + List getIntegerList(T tag); + + List getLongList(T tag); + + byte[] getByteArray(T tag); } diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/RpmBaseTag.java b/rpm/src/main/java/org/eclipse/packager/rpm/RpmBaseTag.java index 9f9b017..ca17c70 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/RpmBaseTag.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/RpmBaseTag.java @@ -23,5 +23,14 @@ public interface RpmBaseTag { * * @return the key value */ - public Integer getValue(); + Integer getValue(); + + /** + * Get the data type of the tag. + * + * @param the data type + * @return the class representing the data type of this tag + */ + Class getDataType(); } + diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/RpmSignatureTag.java b/rpm/src/main/java/org/eclipse/packager/rpm/RpmSignatureTag.java index 3ea3338..83ad116 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/RpmSignatureTag.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/RpmSignatureTag.java @@ -17,23 +17,26 @@ import java.util.Map; public enum RpmSignatureTag implements RpmBaseTag { - PUBKEYS(266), - DSAHEADER(267), - RSAHEADER(268), - SHA1HEADER(269), - LONGARCHIVESIZE(271), - SHA256HEADER(273), - - SIZE(1000), - PGP(1002), - MD5(1004), - PAYLOAD_SIZE(1007), - LONGSIZE(5009); + PUBKEYS(266, String[].class), + DSAHEADER(267, byte[].class), + RSAHEADER(268, byte[].class), + SHA1HEADER(269, String.class), + LONGARCHIVESIZE(271, Long.class), + SHA256HEADER(273, String.class), + + SIZE(1000, Integer.class), + PGP(1002, byte[].class), + MD5(1004, byte[].class), + PAYLOAD_SIZE(1007, Integer.class), + LONGSIZE(5009, Long.class); private final Integer value; - RpmSignatureTag(final Integer value) { + private final Class dataType; + + RpmSignatureTag(final Integer value, final Class dataType) { this.value = value; + this.dataType = dataType; } @Override @@ -41,6 +44,11 @@ public Integer getValue() { return this.value; } + @Override + public Class getDataType() { + return (Class) this.dataType; + } + private final static Map all = new HashMap<>(RpmSignatureTag.values().length); static { @@ -52,4 +60,10 @@ public Integer getValue() { public static RpmSignatureTag find(final Integer value) { return all.get(value); } + + @Override + public String toString() { + RpmSignatureTag tag = find(this.value); + return dataType.getSimpleName() + " " + (tag != null ? tag.name() + "(" + this.value + ")" : "UNKNOWN(" + this.value + ")"); + } } diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/RpmTag.java b/rpm/src/main/java/org/eclipse/packager/rpm/RpmTag.java index 0008ad8..251132e 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/RpmTag.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/RpmTag.java @@ -17,106 +17,109 @@ import java.util.Map; public enum RpmTag implements RpmBaseTag { - NAME(1000), - VERSION(1001), - RELEASE(1002), - EPOCH(1003), - SUMMARY(1004), - DESCRIPTION(1005), - BUILDTIME(1006), - BUILDHOST(1007), - SIZE(1009), - DISTRIBUTION(1010), - VENDOR(1011), - LICENSE(1014), - PACKAGER(1015), - GROUP(1016), - URL(1020), - OS(1021), - ARCH(1022), - PREINSTALL_SCRIPT(1023), - POSTINSTALL_SCRIPT(1024), - PREREMOVE_SCRIPT(1025), - POSTREMOVE_SCRIPT(1026), - FILE_SIZES(1028), - FILE_MODES(1030), - FILE_RDEVS(1033), - FILE_MTIMES(1034), - FILE_DIGESTS(1035), - FILE_LINKTO(1036), - FILE_FLAGS(1037), - FILE_USERNAME(1039), - FILE_GROUPNAME(1040), - SOURCE_PACKAGE(1044), - FILE_VERIFYFLAGS(1045), - ARCHIVE_SIZE(1046), - PROVIDE_NAME(1047), - REQUIRE_FLAGS(1048), - REQUIRE_NAME(1049), - REQUIRE_VERSION(1050), - CONFLICT_FLAGS(1053), - CONFLICT_NAME(1054), - CONFLICT_VERSION(1055), - RPMVERSION(1064), - TRIGGER_SCRIPTS(1065), - TRIGGER_NAME(1066), - TRIGGER_VERSION(1067), - TRIGGER_FLAGS(1068), - TRIGGER_INDEX(1069), - VERIFY_SCRIPT(1079), - CHANGELOG_TIMESTAMP(1080), - CHANGELOG_AUTHOR(1081), - CHANGELOG_TEXT(1082), - PREINSTALL_SCRIPT_PROG(1085), - POSTINSTALL_SCRIPT_PROG(1086), - PREREMOVE_SCRIPT_PROG(1087), - POSTREMOVE_SCRIPT_PROG(1088), - VERIFY_SCRIPT_PROG(1091), - TRIGGERSCRIPT_PROG(1092), - OBSOLETE_NAME(1090), - FILE_DEVICES(1095), - FILE_INODES(1096), - FILE_LANGS(1097), - PREFIXES(1098), - PROVIDE_FLAGS(1112), - PROVIDE_VERSION(1113), - OBSOLETE_FLAGS(1114), - OBSOLETE_VERSION(1115), - DIR_INDEXES(1116), - BASENAMES(1117), - DIRNAMES(1118), - OPTFLAGS(1122), - PAYLOAD_FORMAT(1124), - PAYLOAD_CODING(1125), - PAYLOAD_FLAGS(1126), - PLATFORM(1132), - PRETRANSACTION_SCRIPT(1151), - POSTTRANSACTION_SCRIPT(1152), - PRETRANSACTION_SCRIPT_PROG(1153), - POSTTRANSACTION_SCRIPT_PROG(1154), - LONGSIZE(5009), - FILE_DIGESTALGO(5011), - RECOMMEND_NAME(5046), - RECOMMEND_VERSION(5047), - RECOMMEND_FLAGS(5048), - SUGGEST_NAME(5049), - SUGGEST_VERSION(5050), - SUGGEST_FLAGS(5051), - SUPPLEMENT_NAME(5052), - SUPPLEMENT_VERSION(5053), - SUPPLEMENT_FLAGS(5054), - ENHANCE_NAME(5055), - ENHANCE_VERSION(5056), - ENHANCE_FLAGS(5057), + NAME(1000, String.class), + VERSION(1001, String.class), + RELEASE(1002, String.class), + EPOCH(1003, Integer.class), + SUMMARY(1004, String[].class), + DESCRIPTION(1005, String[].class), + BUILDTIME(1006, Integer.class), + BUILDHOST(1007, String.class), + SIZE(1009, Integer.class), + DISTRIBUTION(1010, String.class), + VENDOR(1011, String.class), + LICENSE(1014, String.class), + PACKAGER(1015, String.class), + GROUP(1016, String[].class), + URL(1020, String.class), + OS(1021, String.class), + ARCH(1022, String.class), + PREINSTALL_SCRIPT(1023, String.class), + POSTINSTALL_SCRIPT(1024, String.class), + PREREMOVE_SCRIPT(1025, String.class), + POSTREMOVE_SCRIPT(1026, String.class), + FILE_SIZES(1028, Integer[].class), + FILE_MODES(1030, Short[].class), + FILE_RDEVS(1033, Short[].class), + FILE_MTIMES(1034, Integer[].class), + FILE_DIGESTS(1035, String[].class), + FILE_LINKTO(1036, String[].class), + FILE_FLAGS(1037, Integer[].class), + FILE_USERNAME(1039, String[].class), + FILE_GROUPNAME(1040, String[].class), + SOURCE_PACKAGE(1044, String.class), + FILE_VERIFYFLAGS(1045, Integer[].class), + ARCHIVE_SIZE(1046, Integer.class), + PROVIDE_NAME(1047, String[].class), + REQUIRE_FLAGS(1048, Integer[].class), + REQUIRE_NAME(1049, String[].class), + REQUIRE_VERSION(1050, String[].class), + CONFLICT_FLAGS(1053, Integer[].class), + CONFLICT_NAME(1054, String[].class), + CONFLICT_VERSION(1055, String[].class), + RPMVERSION(1064, String.class), + TRIGGER_SCRIPTS(1065, String[].class), + TRIGGER_NAME(1066, String[].class), + TRIGGER_VERSION(1067, String[].class), + TRIGGER_FLAGS(1068, Integer[].class), + TRIGGER_INDEX(1069, Integer[].class), + VERIFY_SCRIPT(1079, String.class), + CHANGELOG_TIMESTAMP(1080, Integer[].class), + CHANGELOG_AUTHOR(1081, String[].class), + CHANGELOG_TEXT(1082, String[].class), + PREINSTALL_SCRIPT_PROG(1085, String[].class), + POSTINSTALL_SCRIPT_PROG(1086, String[].class), + PREREMOVE_SCRIPT_PROG(1087, String[].class), + POSTREMOVE_SCRIPT_PROG(1088, String[].class), + OBSOLETE_NAME(1090, String[].class), + VERIFY_SCRIPT_PROG(1091, String[].class), + TRIGGERSCRIPT_PROG(1092, String[].class), + FILE_DEVICES(1095, Integer[].class), + FILE_INODES(1096, Integer[].class), + FILE_LANGS(1097, String[].class), + PREFIXES(1098, String[].class), + PROVIDE_FLAGS(1112, Integer[].class), + PROVIDE_VERSION(1113, String[].class), + OBSOLETE_FLAGS(1114, Integer[].class), + OBSOLETE_VERSION(1115, String[].class), + DIR_INDEXES(1116, Integer.class), + BASENAMES(1117, String[].class), + DIRNAMES(1118, String[].class), + OPTFLAGS(1122, String.class), + PAYLOAD_FORMAT(1124, String.class), + PAYLOAD_CODING(1125, String.class), + PAYLOAD_FLAGS(1126, String.class), + PLATFORM(1132, String.class), + PRETRANSACTION_SCRIPT(1151, String.class), + POSTTRANSACTION_SCRIPT(1152, String.class), + PRETRANSACTION_SCRIPT_PROG(1153, String[].class), + POSTTRANSACTION_SCRIPT_PROG(1154, String[].class), + LONGSIZE(5009, Long.class), + FILE_DIGESTALGO(5011, Integer.class), + RECOMMEND_NAME(5046, String[].class), + RECOMMEND_VERSION(5047, String[].class), + RECOMMEND_FLAGS(5048, Integer[].class), + SUGGEST_NAME(5049, String[].class), + SUGGEST_VERSION(5050, String[].class), + SUGGEST_FLAGS(5051, Integer[].class), + SUPPLEMENT_NAME(5052, String[].class), + SUPPLEMENT_VERSION(5053, String[].class), + SUPPLEMENT_FLAGS(5054, Integer[].class), + ENHANCE_NAME(5055, String[].class), + ENHANCE_VERSION(5056, String[].class), + ENHANCE_FLAGS(5057, Integer[].class), - PAYLOAD_DIGEST(5092), - PAYLOAD_DIGEST_ALGO(5093), - PAYLOAD_DIGEST_ALT(5097); + PAYLOAD_DIGEST(5092, String[].class), + PAYLOAD_DIGEST_ALGO(5093, Integer.class), + PAYLOAD_DIGEST_ALT(5097, String[].class); private final Integer value; - RpmTag(final Integer value) { + private final Class dataType; + + RpmTag(final Integer value, Class dataType) { this.value = value; + this.dataType = dataType; } @Override @@ -124,6 +127,10 @@ public Integer getValue() { return this.value; } + public Class getDataType() { + return (Class) this.dataType; + } + private final static Map all = new HashMap<>(RpmTag.values().length); static { @@ -135,4 +142,10 @@ public Integer getValue() { public static RpmTag find(final Integer value) { return all.get(value); } + + @Override + public String toString() { + RpmTag tag = find(this.value); + return dataType.getSimpleName() + " " + (tag != null ? tag.name() + "(" + this.value + ")" : "UNKNOWN(" + this.value + ")"); + } } diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/RpmTagValue.java b/rpm/src/main/java/org/eclipse/packager/rpm/RpmTagValue.java index 022a7cc..5328234 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/RpmTagValue.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/RpmTagValue.java @@ -17,20 +17,40 @@ import java.nio.ByteBuffer; import java.util.Arrays; +import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; -public class RpmTagValue { - private final Object value; +public class RpmTagValue { + private final T value; - public RpmTagValue(final Object value) { + public RpmTagValue(final T value) { this.value = value; } - public Object getValue() { + public T getValue() { return this.value; } + public Optional asByteArray() { + if (this.value == null) { + return Optional.empty(); + } + + if (this.value instanceof Byte) { + return Optional.of(new byte[] { (byte) this.value }); + } + + if (this.value instanceof byte[]) { + return Optional.of((byte[]) this.value); + } + + return Optional.empty(); + + } + public Optional asStringArray() { if (this.value == null) { return Optional.empty(); @@ -39,6 +59,7 @@ public Optional asStringArray() { if (this.value instanceof String) { return Optional.of(new String[] { (String) this.value }); } + if (this.value instanceof String[]) { return Optional.of((String[]) this.value); } @@ -56,12 +77,13 @@ public Optional asString() { } if (this.value instanceof String[]) { - final String[] arr = (String[]) this.value; - if (arr.length > 0) { - return Optional.of(arr[0]); - } else { - return Optional.empty(); + final String[] array = (String[]) this.value; + + if (array.length == 1) { + return Optional.of(array[0]); } + + throw new IllegalArgumentException("Array contains more than one string value"); } return Optional.empty(); @@ -75,6 +97,7 @@ public Optional asIntegerArray() { if (this.value instanceof Integer) { return Optional.of(new Integer[] { (Integer) this.value }); } + if (this.value instanceof Integer[]) { return Optional.of((Integer[]) this.value); } @@ -82,37 +105,47 @@ public Optional asIntegerArray() { return Optional.empty(); } - public Optional asInteger() { + public Optional asLongArray() { if (this.value == null) { return Optional.empty(); } if (this.value instanceof Integer) { - return Optional.of((Integer) this.value); + return Optional.of(new Long[] { toLong((Integer) this.value) }); + } + + if (this.value instanceof Long) { + return Optional.of(new Long[] {(Long) this.value}); } if (this.value instanceof Integer[]) { - final Integer[] arr = (Integer[]) this.value; - if (arr.length > 0) { - return Optional.of(arr[0]); - } else { - return Optional.empty(); - } + return Optional.of(Stream.of((Integer[]) this.value).map(Integer::toUnsignedLong).toArray(Long[]::new)); + } + + if (this.value instanceof Long[]) { + return Optional.of((Long[]) this.value); } return Optional.empty(); } - public Optional asLongArray() { + public Optional asInteger() { if (this.value == null) { return Optional.empty(); } - if (this.value instanceof Long) { - return Optional.of(new Long[] { (Long) this.value }); + if (this.value instanceof Integer) { + return Optional.of((Integer) this.value); } - if (this.value instanceof Long[]) { - return Optional.of((Long[]) this.value); + + if (this.value instanceof Integer[]) { + final Integer[] array = (Integer[]) this.value; + + if (array.length == 1) { + return Optional.of(array[0]); + } + + throw new IllegalArgumentException("Array contains more than one integer value"); } return Optional.empty(); @@ -124,7 +157,17 @@ public Optional asLong() { } if (this.value instanceof Integer) { - return Optional.of((Integer.toUnsignedLong((Integer) this.value))); + return Optional.of((toLong((Integer) this.value))); + } + + if (this.value instanceof Integer[]) { + final Integer[] array = (Integer[]) this.value; + + if (array.length == 1) { + return Optional.of(toLong(array[0])); + } + + throw new IllegalArgumentException("Array contains more than one integer value"); } if (this.value instanceof Long) { @@ -132,17 +175,26 @@ public Optional asLong() { } if (this.value instanceof Long[]) { - final Long[] arr = (Long[]) this.value; - if (arr.length > 0) { - return Optional.of(arr[0]); - } else { - return Optional.empty(); + final Long[] array = (Long[]) this.value; + + if (array.length == 1) { + return Optional.of(array[0]); } + + throw new IllegalArgumentException("Array contains more than one long value"); } return Optional.empty(); } + public static Long toLong(Integer x) { + return x != null ? Integer.toUnsignedLong(x) : null; + } + + public static List toLong(List x) { + return x != null ? x.stream().map(RpmTagValue::toLong).collect(Collectors.toUnmodifiableList()) : null; + } + @Override public String toString() { if (this.value instanceof byte[]) { diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/Rpms.java b/rpm/src/main/java/org/eclipse/packager/rpm/Rpms.java index feb5903..695f848 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/Rpms.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/Rpms.java @@ -13,6 +13,8 @@ package org.eclipse.packager.rpm; +import org.eclipse.packager.rpm.parse.HeaderValue; + import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; @@ -65,9 +67,9 @@ public static String toHex(final byte[] data, final int offset, final int length return sb.toString(); } - public static String dumpValue(final Object value) { + public static String dumpValue(final HeaderValue value) { final StringBuilder sb = new StringBuilder(); - dumpValue(sb, value); + dumpValue(sb, value.getValue()); return sb.toString(); } @@ -78,18 +80,8 @@ static void writeByteBuffer(final OutputStream stream, final ByteBuffer dataStor } } - public static void dumpValue(final StringBuilder sb, final Object value) { - if (value != null) { - if (value instanceof byte[]) { - sb.append(toHex((byte[]) value, -1)); - } else if (value.getClass().isArray()) { - sb.append(Arrays.toString((Object[]) value)); - } else { - sb.append(value); - } - } else { - sb.append("null"); - } + public static void dumpValue(final StringBuilder sb, final RpmTagValue value) { + sb.append(value); } public static int padding(final int offset) { 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 7590836..facfd68 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 @@ -13,11 +13,15 @@ package org.eclipse.packager.rpm.app; +import static org.eclipse.packager.rpm.Rpms.IMMUTABLE_TAG_HEADER; +import static org.eclipse.packager.rpm.Rpms.IMMUTABLE_TAG_SIGNATURE; + import java.io.BufferedInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; @@ -25,6 +29,7 @@ import java.util.TreeMap; import java.util.function.Function; import java.util.function.IntFunction; +import java.util.stream.IntStream; import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry; import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream; @@ -34,7 +39,6 @@ import org.eclipse.packager.rpm.RpmLead; import org.eclipse.packager.rpm.RpmSignatureTag; import org.eclipse.packager.rpm.RpmTag; -import org.eclipse.packager.rpm.RpmTagValue; import org.eclipse.packager.rpm.Rpms; import org.eclipse.packager.rpm.Type; import org.eclipse.packager.rpm.deps.RpmDependencyFlags; @@ -53,6 +57,7 @@ public class Dumper { public static String dumpFlag(final int value, final IntFunction> func) { final Optional flag = func.apply(value); + if (flag.isPresent()) { return String.format("%s (%s)", flag.get(), value); } else { @@ -99,43 +104,39 @@ public static void dumpAll(final RpmInputStream in) throws IOException { } private static void dumpGroup(final RpmInputStream in, final String name, final RpmTag nameTag, final RpmTag versionTag, final RpmTag flagTag) throws IOException { - final String[] names = new RpmTagValue(in.getPayloadHeader().getTag(nameTag)).asStringArray().orElse(null); - final String[] versions = new RpmTagValue(in.getPayloadHeader().getTag(versionTag)).asStringArray().orElse(null); - final Integer[] flags = new RpmTagValue(in.getPayloadHeader().getTag(flagTag)).asIntegerArray().orElse(null); + final InputHeader payloadHeader = in.getPayloadHeader(); + final List names = payloadHeader.getStringList(nameTag); + final List versions = payloadHeader.getStringList(versionTag); + final List flags = payloadHeader.getIntegerList(flagTag); dumpDeps(name, names, versions, flags); } - private static void dumpDeps(final String string, final String[] names, final String[] versions, final Integer[] flags) { + private static void dumpDeps(final String string, final List names, final List versions, final List flags) { if (names == null) { return; } - for (int i = 0; i < names.length; i++) { - System.out.format("%s: %s - %s - %s %s%n", string, names[i], versions[i], flags[i], RpmDependencyFlags.parse(flags[i])); - } + IntStream.range(0, names.size()).forEach(i -> System.out.format("%s: %s - %s - %s %s%n", string, names.get(i), versions.get(i), flags.get(i), RpmDependencyFlags.parse(flags.get(i)))); } - private static void dumpHeader(final String string, final InputHeader header, final Function func, final boolean sorted) { - System.out.println(string); - System.out.println("================================="); + private static void dumpHeader(final String string, final InputHeader header, final Function func, final boolean sorted) { + System.out.format(string + "%n"); + System.out.format("=================================%n"); - Set> data; + Set>> data; if (sorted) { data = new TreeMap<>(header.getRawTags()).entrySet(); } else { data = header.getRawTags().entrySet(); } - for (final Map.Entry entry : data) { - Object tag = func.apply(entry.getKey()); - if (tag == null) { - tag = entry.getKey(); - } - - System.out.format("%20s - %s%n", tag, Rpms.dumpValue(entry.getValue())); + for (final Map.Entry> entry : data) { + final RpmBaseTag tag = func.apply(entry.getKey()); + final HeaderValue value = entry.getValue(); + System.out.format("%20s - %s%n", tag != null ? tag : entry.getKey(), Rpms.dumpValue(value)); - if (entry.getKey() == 62 || entry.getKey() == 63) { - final ByteBuffer buf = ByteBuffer.wrap((byte[]) entry.getValue().getValue()); + if (entry.getKey() == IMMUTABLE_TAG_SIGNATURE || entry.getKey() == IMMUTABLE_TAG_HEADER) { + final ByteBuffer buf = ByteBuffer.wrap(entry.getValue().getValue().asByteArray().orElseThrow()); System.out.format("Immutable - tag: %s, type: %s, position: %s, count: %s%n", buf.getInt(), buf.getInt(), buf.getInt(), buf.getInt()); } } diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/build/LeadBuilder.java b/rpm/src/main/java/org/eclipse/packager/rpm/build/LeadBuilder.java index 04c775f..840b65d 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/build/LeadBuilder.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/build/LeadBuilder.java @@ -74,15 +74,11 @@ public void fillFlagsFromHeader(final Header header, final Function header) { diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/build/RpmBuilder.java b/rpm/src/main/java/org/eclipse/packager/rpm/build/RpmBuilder.java index 5a926ed..e95214c 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/build/RpmBuilder.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/build/RpmBuilder.java @@ -391,7 +391,7 @@ public static class PackageInformation { private String vendor; - private String license = "unspecified"; + private String license = "Unspecified"; private String buildHost = "localhost"; @@ -765,7 +765,7 @@ private void fillHeader(final PayloadRecorder.Finished finished) { this.header.putInt(RpmTag.FILE_DIGESTALGO, this.options.getFileDigestAlgorithm().getTag()); } - final FileEntry[] files = this.files.values().toArray(new FileEntry[this.files.size()]); + final FileEntry[] files = this.files.values().toArray(new FileEntry[0]); Arrays.sort(files, comparing(FileEntry::getTargetName)); final long installedSize = Arrays.stream(files).mapToLong(FileEntry::getTargetSize).sum(); diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/deps/Dependencies.java b/rpm/src/main/java/org/eclipse/packager/rpm/deps/Dependencies.java index d987d7f..96c8502 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/deps/Dependencies.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/deps/Dependencies.java @@ -16,7 +16,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.Objects; import java.util.Set; @@ -113,36 +112,21 @@ public static List getEnhances(final ReadableHeader header) private static List getDependencies(final ReadableHeader header, final RpmTag namesTag, final RpmTag versionsTag, final RpmTag flagsTag) { Objects.requireNonNull(header); - final Object rawNames = header.getValue(namesTag).orElse(null); - final Object rawVersions = header.getValue(versionsTag).orElse(null); - Object rawFlags = header.getValue(flagsTag).orElse(null); - - if (rawFlags instanceof Integer[]) { - final Integer[] iflags = (Integer[]) rawFlags; - final int[] flags = new int[iflags.length]; - for (int i = 0; i < iflags.length; i++) { - flags[i] = iflags[i]; - } - rawFlags = flags; - } - - if (rawNames instanceof String[] && rawVersions instanceof String[] && rawFlags instanceof int[]) { - final String[] names = (String[]) rawNames; - final String[] versions = (String[]) rawVersions; - final int[] flags = (int[]) rawFlags; - - if (names.length == versions.length && names.length == flags.length) { - final List result = new ArrayList<>(names.length); - for (int i = 0; i < names.length; i++) { - final String name = names[i]; - final String version = versions[i]; - final Set flagSet = RpmDependencyFlags.parse(flags[i]); - result.add(new Dependency(name, version, flagSet)); - } - return result; + final List names = header.getStringList(namesTag); + final List versions = header.getStringList(versionsTag); + final List flags = header.getIntegerList(flagsTag); + + if (names.size() == versions.size() && names.size() == flags.size()) { + final List result = new ArrayList<>(names.size()); + for (int i = 0; i < names.size(); i++) { + final String name = names.get(i); + final String version = versions.get(i); + final Set flagSet = RpmDependencyFlags.parse(flags.get(i)); + result.add(new Dependency(name, version, flagSet)); } + return result; } - return new LinkedList<>(); + return List.of(); } } diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/header/Header.java b/rpm/src/main/java/org/eclipse/packager/rpm/header/Header.java index 709689a..8c02ec4 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/header/Header.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/header/Header.java @@ -22,9 +22,9 @@ import java.util.Arrays; import java.util.Collection; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Optional; import java.util.function.Function; import java.util.function.ToIntFunction; import java.util.function.ToLongFunction; @@ -58,25 +58,25 @@ public I18nString(final String value) { @Override public String toString() { - return this.value; + return Objects.toString(this.value); } } - private final Map entries = new LinkedHashMap<>(); + private final Map> entries = new LinkedHashMap<>(); private final Charset charset; - public Header(final HeaderEntry[] entries) { + public Header(final HeaderEntry[] entries) { this(entries, StandardCharsets.UTF_8); } - public Header(final HeaderEntry[] entries, final Charset charset) { + public Header(final HeaderEntry[] entries, final Charset charset) { Objects.requireNonNull(charset); this.charset = charset; if (entries != null) { - for (final HeaderEntry entry : entries) { + for (final HeaderEntry entry : entries) { this.entries.put(entry.getTag(), entry); } } @@ -256,17 +256,79 @@ public void remove(final RpmTag tag) { this.entries.remove(tag.getValue()); } - public Object get(final int tag) { - return this.entries.get(tag).getValue(); + public HeaderEntry get(final int tag) { + return this.entries.get(tag); } - public Object get(final T tag) { - return this.entries.get(tag.getValue()).getValue(); + public HeaderEntry get(final T tag) { + return this.entries.get(tag.getValue()); } @Override - public Optional getValue(final T tag) { - return Optional.ofNullable(get(tag)); + public boolean hasTag(final T tag) { + return this.entries.containsKey(tag.getValue()); + } + + @Override + public String getString(T tag) { + if (!String.class.isAssignableFrom(tag.getDataType()) && !String[].class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not a string or array of strings"); + } + + return get(tag).getValue().asString().orElse(null); + } + + @Override + public Integer getInteger(T tag) { + if (!Integer.class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not an integer"); + } + + return get(tag).getValue().asInteger().orElse(null); + } + + public Long getLong(T tag) { + if (!Long.class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not a long"); + } + + return get(tag).getValue().asLong().orElse(null); + } + + @Override + public List getStringList(T tag) { + if (!String[].class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not an array of strings"); + } + + return get(tag).getValue().asStringArray().map(Arrays::asList).orElse(null); + } + + @Override + public List getIntegerList(T tag) { + if (!Integer[].class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not an array of integers"); + } + + return get(tag).getValue().asIntegerArray().map(Arrays::asList).orElse(null); + } + + @Override + public List getLongList(T tag) { + if (!Long[].class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not an array of longs"); + } + + return get(tag).getValue().asLongArray().map(Arrays::asList).orElse(null); + } + + @Override + public byte[] getByteArray(T tag) { + if (!byte[].class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not an array of bytes"); + } + + return get(tag).getValue().asByteArray().orElse(null); } /** @@ -279,14 +341,14 @@ public Optional getValue(final T tag) { * @param charset the charset of choice * @return a new array of all header entries, unsorted */ - public HeaderEntry[] makeEntries(Charset charset) { + public HeaderEntry[] makeEntries(Charset charset) { if (charset == null) { throw new IllegalArgumentException("'charset' cannot be null"); } - return this.entries.entrySet().stream().map(entry -> makeEntry(entry, charset)).toArray(HeaderEntry[]::new); + return this.entries.entrySet().stream().map(entry -> makeEntry(entry, charset)).toArray(HeaderEntry[]::new); } - private HeaderEntry makeEntry(final Map.Entry entry) { + private HeaderEntry makeEntry(final Map.Entry> entry) { return makeEntry(entry, this.charset); } @@ -299,22 +361,22 @@ private HeaderEntry makeEntry(final Map.Entry entry) { * * @return a new array of all header entries, unsorted */ - public HeaderEntry[] makeEntries() { + public HeaderEntry[] makeEntries() { return makeEntries(StandardCharsets.UTF_8); } - private static HeaderEntry makeEntry(final Map.Entry entry, final Charset charset) { + private static HeaderEntry makeEntry(final Map.Entry> entry, final Charset charset) { return entry.getValue(); } - private static HeaderEntry makeEntry(int tag, Object val) { + private static HeaderEntry makeEntry(int tag, E val) { return makeEntry(tag, val, StandardCharsets.UTF_8); } - private static HeaderEntry makeEntry(int tag, Object val, Charset charset) { + private static HeaderEntry makeEntry(int tag, E val, Charset charset) { // NULL if (val == null) { - return new HeaderEntry(Type.NULL, tag, 0, null, val); + return new HeaderEntry<>(Type.NULL, tag, 0, null, val); } // FIXME: CHAR @@ -323,7 +385,7 @@ private static HeaderEntry makeEntry(int tag, Object val, Charset charset) { if (val instanceof byte[]) { final byte[] value = (byte[]) val; - return new HeaderEntry(Type.BYTE, tag, value.length, value, val); + return new HeaderEntry<>(Type.BYTE, tag, value.length, value, val); } // SHORT @@ -331,13 +393,13 @@ private static HeaderEntry makeEntry(int tag, Object val, Charset charset) { if (val instanceof short[]) { final short[] value = (short[]) val; - final byte[] data = new byte[value.length * 2]; + final byte[] data = new byte[value.length * Short.BYTES]; final ByteBuffer buffer = ByteBuffer.wrap(data); for (final short v : value) { buffer.putShort(v); } - return new HeaderEntry(Type.SHORT, tag, value.length, data, val); + return new HeaderEntry<>(Type.SHORT, tag, value.length, data, val); } // INT @@ -345,13 +407,13 @@ private static HeaderEntry makeEntry(int tag, Object val, Charset charset) { if (val instanceof int[]) { final int[] value = (int[]) val; - final byte[] data = new byte[value.length * 4]; + final byte[] data = new byte[value.length * Integer.BYTES]; final ByteBuffer buffer = ByteBuffer.wrap(data); for (final int v : value) { buffer.putInt(v); } - return new HeaderEntry(Type.INT, tag, value.length, data, val); + return new HeaderEntry<>(Type.INT, tag, value.length, data, val); } // LONG @@ -359,13 +421,13 @@ private static HeaderEntry makeEntry(int tag, Object val, Charset charset) { if (val instanceof long[]) { final long[] value = (long[]) val; - final byte[] data = new byte[value.length * 8]; + final byte[] data = new byte[value.length * Long.BYTES]; final ByteBuffer buffer = ByteBuffer.wrap(data); for (final long v : value) { buffer.putLong(v); } - return new HeaderEntry(Type.LONG, tag, value.length, data, val); + return new HeaderEntry<>(Type.LONG, tag, value.length, data, val); } // STRING @@ -373,7 +435,7 @@ private static HeaderEntry makeEntry(int tag, Object val, Charset charset) { if (val instanceof String) { final String value = (String) val; - return new HeaderEntry(Type.STRING, tag, 1, makeStringData(new ByteArrayOutputStream(), value, charset).toByteArray(), val); + return new HeaderEntry<>(Type.STRING, tag, 1, makeStringData(new ByteArrayOutputStream(), value, charset).toByteArray(), val); } // BLOB @@ -387,7 +449,7 @@ private static HeaderEntry makeEntry(int tag, Object val, Charset charset) { data = new byte[value.remaining()]; value.get(data); } - return new HeaderEntry(Type.BLOB, tag, data.length, data, val); + return new HeaderEntry<>(Type.BLOB, tag, data.length, data, val); } // STRING_ARRAY @@ -395,7 +457,7 @@ private static HeaderEntry makeEntry(int tag, Object val, Charset charset) { if (val instanceof String[]) { final String[] value = (String[]) val; - return new HeaderEntry(Type.STRING_ARRAY, tag, value.length, makeStringsData(new ByteArrayOutputStream(), value, charset).toByteArray(), val); + return new HeaderEntry<>(Type.STRING_ARRAY, tag, value.length, makeStringsData(new ByteArrayOutputStream(), value, charset).toByteArray(), val); } // I18N_STRING @@ -403,7 +465,7 @@ private static HeaderEntry makeEntry(int tag, Object val, Charset charset) { if (val instanceof I18nString[]) { final I18nString[] value = (I18nString[]) val; - return new HeaderEntry(Type.I18N_STRING, tag, value.length, makeStringsData(new ByteArrayOutputStream(), value, charset).toByteArray(), val); + return new HeaderEntry<>(Type.I18N_STRING, tag, value.length, makeStringsData(new ByteArrayOutputStream(), value, charset).toByteArray(), val); } throw new IllegalArgumentException(String.format("Unable to process value type: %s", val.getClass())); diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/header/HeaderEntry.java b/rpm/src/main/java/org/eclipse/packager/rpm/header/HeaderEntry.java index a896b4c..2226ac1 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/header/HeaderEntry.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/header/HeaderEntry.java @@ -13,12 +13,9 @@ package org.eclipse.packager.rpm.header; -import org.apache.commons.codec.binary.Hex; import org.eclipse.packager.rpm.RpmTagValue; -import java.util.Objects; - -public class HeaderEntry { +public class HeaderEntry { private final Type type; private final int tag; @@ -27,14 +24,14 @@ public class HeaderEntry { private final byte[] data; - private final RpmTagValue value; + private final RpmTagValue value; - public HeaderEntry(final Type type, final int tag, final int count, final byte[] data, Object value) { + public HeaderEntry(final Type type, final int tag, final int count, final byte[] data, T value) { this.type = type; this.tag = tag; this.count = count; this.data = data; - this.value = new RpmTagValue(value); + this.value = new RpmTagValue<>(value); } public Type getType() { @@ -53,12 +50,12 @@ public byte[] getData() { return this.data; } - public RpmTagValue getValue() { + public RpmTagValue getValue() { return this.value; } @Override public String toString() { - return Objects.toString(this.value); + return this.value.toString(); } } diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/header/Headers.java b/rpm/src/main/java/org/eclipse/packager/rpm/header/Headers.java index 2ec3323..12bb25f 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/header/Headers.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/header/Headers.java @@ -32,7 +32,7 @@ public final class Headers { private Headers() { } - public static ByteBuffer render(final HeaderEntry[] entries, final boolean sorted, final Integer immutableTag) throws IOException { + public static ByteBuffer render(final HeaderEntry[] entries, final boolean sorted, final Integer immutableTag) throws IOException { Objects.requireNonNull(entries); // sorted header @@ -100,7 +100,7 @@ public static ByteBuffer render(final HeaderEntry[] entries, final boolean sorte int i = immutableTag == null ? 0 : 1; - for (final HeaderEntry entry : entries) { + for (final HeaderEntry entry : entries) { // align align(buffer, entry); @@ -153,7 +153,7 @@ public static ByteBuffer render(final HeaderEntry[] entries, final boolean sorte return buffer; } - private static void fillEntryRecord(final ByteBuffer buffer, final int entryIndex, final HeaderEntry entry, final int index) { + private static void fillEntryRecord(final ByteBuffer buffer, final int entryIndex, final HeaderEntry entry, final int index) { fillEntryRecord(buffer, entryIndex, entry.getTag(), entry.getType().type(), index, entry.getCount()); } @@ -176,7 +176,7 @@ public static byte[] makeEntryRecord(final int tag, final int type, final int in return buffer.array(); } - private static void align(final ByteBuffer buffer, final HeaderEntry entry) { + private static void align(final ByteBuffer buffer, final HeaderEntry entry) { final int position = buffer.position(); final int alignment = entry.getType().align(); @@ -190,7 +190,7 @@ private static void align(final ByteBuffer buffer, final HeaderEntry entry) { buffer.put(Rpms.EMPTY_128, 0, len); } - private static int rawEntrySize(final HeaderEntry entry) { + private static int rawEntrySize(final HeaderEntry entry) { return entry.getData() != null ? entry.getData().length : 0; } } diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/info/RpmInformation.java b/rpm/src/main/java/org/eclipse/packager/rpm/info/RpmInformation.java index 6de5126..957ee4e 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/info/RpmInformation.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/info/RpmInformation.java @@ -25,12 +25,12 @@ public static class Version { private String release; - private String epoch; + private Integer epoch; public Version() { } - public Version(final String version, final String release, final String epoch) { + public Version(final String version, final String release, final Integer epoch) { this.version = version; this.release = release; this.epoch = epoch; @@ -52,11 +52,11 @@ public void setRelease(final String release) { this.release = release; } - public String getEpoch() { + public Integer getEpoch() { return this.epoch; } - public void setEpoch(final String epoch) { + public void setEpoch(final Integer epoch) { this.epoch = epoch; } diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/info/RpmInformations.java b/rpm/src/main/java/org/eclipse/packager/rpm/info/RpmInformations.java index 686cc39..e832e2e 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/info/RpmInformations.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/info/RpmInformations.java @@ -15,12 +15,12 @@ import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry; import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream; @@ -31,6 +31,40 @@ import org.eclipse.packager.rpm.parse.InputHeader; import org.eclipse.packager.rpm.parse.RpmInputStream; +import static org.eclipse.packager.rpm.RpmSignatureTag.PAYLOAD_SIZE; +import static org.eclipse.packager.rpm.RpmTag.ARCH; +import static org.eclipse.packager.rpm.RpmTag.ARCHIVE_SIZE; +import static org.eclipse.packager.rpm.RpmTag.BUILDHOST; +import static org.eclipse.packager.rpm.RpmTag.BUILDTIME; +import static org.eclipse.packager.rpm.RpmTag.CHANGELOG_AUTHOR; +import static org.eclipse.packager.rpm.RpmTag.CHANGELOG_TEXT; +import static org.eclipse.packager.rpm.RpmTag.CHANGELOG_TIMESTAMP; +import static org.eclipse.packager.rpm.RpmTag.CONFLICT_FLAGS; +import static org.eclipse.packager.rpm.RpmTag.CONFLICT_NAME; +import static org.eclipse.packager.rpm.RpmTag.CONFLICT_VERSION; +import static org.eclipse.packager.rpm.RpmTag.DESCRIPTION; +import static org.eclipse.packager.rpm.RpmTag.EPOCH; +import static org.eclipse.packager.rpm.RpmTag.GROUP; +import static org.eclipse.packager.rpm.RpmTag.LICENSE; +import static org.eclipse.packager.rpm.RpmTag.NAME; +import static org.eclipse.packager.rpm.RpmTag.OBSOLETE_FLAGS; +import static org.eclipse.packager.rpm.RpmTag.OBSOLETE_NAME; +import static org.eclipse.packager.rpm.RpmTag.OBSOLETE_VERSION; +import static org.eclipse.packager.rpm.RpmTag.PACKAGER; +import static org.eclipse.packager.rpm.RpmTag.PROVIDE_FLAGS; +import static org.eclipse.packager.rpm.RpmTag.PROVIDE_NAME; +import static org.eclipse.packager.rpm.RpmTag.PROVIDE_VERSION; +import static org.eclipse.packager.rpm.RpmTag.RELEASE; +import static org.eclipse.packager.rpm.RpmTag.REQUIRE_FLAGS; +import static org.eclipse.packager.rpm.RpmTag.REQUIRE_NAME; +import static org.eclipse.packager.rpm.RpmTag.REQUIRE_VERSION; +import static org.eclipse.packager.rpm.RpmTag.SIZE; +import static org.eclipse.packager.rpm.RpmTag.SOURCE_PACKAGE; +import static org.eclipse.packager.rpm.RpmTag.SUMMARY; +import static org.eclipse.packager.rpm.RpmTag.URL; +import static org.eclipse.packager.rpm.RpmTag.VENDOR; +import static org.eclipse.packager.rpm.RpmTag.VERSION; + public final class RpmInformations { private RpmInformations() { } @@ -44,57 +78,57 @@ public static RpmInformation makeInformation(final RpmInputStream in) throws IOE result.setHeaderStart(header.getStart()); result.setHeaderEnd(header.getStart() + header.getLength()); - result.setName(asString(header.getTag(RpmTag.NAME))); - result.setArchitecture(asString(header.getTag(RpmTag.ARCH))); - result.setSummary(asString(header.getTag(RpmTag.SUMMARY))); - result.setDescription(asString(header.getTag(RpmTag.DESCRIPTION))); - result.setPackager(asString(header.getTag(RpmTag.PACKAGER))); - result.setUrl(asString(header.getTag(RpmTag.URL))); - result.setLicense(asString(header.getTag(RpmTag.LICENSE))); - result.setVendor(asString(header.getTag(RpmTag.VENDOR))); - result.setGroup(asString(header.getTag(RpmTag.GROUP))); + result.setName(header.getString(NAME)); + result.setArchitecture(header.getString(ARCH)); + result.setSummary(header.getString(SUMMARY)); + result.setDescription(header.getString(DESCRIPTION)); + result.setPackager(header.getString(PACKAGER)); + result.setUrl(header.getString(URL)); + result.setLicense(header.getString(LICENSE)); + result.setVendor(header.getString(VENDOR)); + result.setGroup(header.getString(GROUP)); - result.setBuildHost(asString(header.getTag(RpmTag.BUILDHOST))); - result.setBuildTimestamp(asLong(header.getTag(RpmTag.BUILDTIME))); - result.setSourcePackage(asString(header.getTag(RpmTag.SOURCE_PACKAGE))); + result.setBuildHost(header.getString(BUILDHOST)); + result.setBuildTimestamp(RpmTagValue.toLong(header.getInteger(BUILDTIME))); + result.setSourcePackage(header.getString(SOURCE_PACKAGE)); - result.setInstalledSize(asLong(header.getTag(RpmTag.SIZE))); - result.setArchiveSize(asLong(header.getTag(RpmTag.ARCHIVE_SIZE))); + result.setInstalledSize(RpmTagValue.toLong(header.getInteger(SIZE))); + result.setArchiveSize(RpmTagValue.toLong(header.getInteger(ARCHIVE_SIZE))); if (result.getArchiveSize() == null) { - result.setArchiveSize(asLong(signature.getTag(RpmSignatureTag.PAYLOAD_SIZE))); + result.setArchiveSize(RpmTagValue.toLong(signature.getInteger(PAYLOAD_SIZE))); } // version - final RpmInformation.Version ver = new RpmInformation.Version(asString(header.getTag(RpmTag.VERSION)), asString(header.getTag(RpmTag.RELEASE)), asString(header.getTag(RpmTag.EPOCH))); + final RpmInformation.Version ver = new RpmInformation.Version(header.getString(VERSION), header.getString(RELEASE), header.getInteger(EPOCH)); result.setVersion(ver); // changelog - final Object val = header.getTag(RpmTag.CHANGELOG_TIMESTAMP); - if (val instanceof Long[]) { - final Long[] ts = (Long[]) val; - final String[] authors = (String[]) header.getTag(RpmTag.CHANGELOG_AUTHOR); - final String[] texts = (String[]) header.getTag(RpmTag.CHANGELOG_TEXT); + final List ts = RpmTagValue.toLong(header.getIntegerList(CHANGELOG_TIMESTAMP)); - final List changes = new ArrayList<>(ts.length); + if (ts != null) { + final List authors = header.getStringList(CHANGELOG_AUTHOR); + final List texts = header.getStringList(CHANGELOG_TEXT); + final int size = ts.size(); + final List changes = new ArrayList<>(size); - for (int i = 0; i < ts.length; i++) { - changes.add(new RpmInformation.Changelog(ts[i], authors[i], texts[i])); + for (int i = 0; i < size; i++) { + changes.add(new RpmInformation.Changelog(ts.get(i), authors.get(i), texts.get(i))); } - Collections.sort(changes, Comparator.comparingLong(RpmInformation.Changelog::getTimestamp)); + changes.sort(Comparator.comparingLong(RpmInformation.Changelog::getTimestamp)); result.setChangelog(changes); } // dependencies - result.setProvides(makeDependencies(header, RpmTag.PROVIDE_NAME, RpmTag.PROVIDE_VERSION, RpmTag.PROVIDE_FLAGS)); - result.setRequires(makeDependencies(header, RpmTag.REQUIRE_NAME, RpmTag.REQUIRE_VERSION, RpmTag.REQUIRE_FLAGS)); - result.setConflicts(makeDependencies(header, RpmTag.CONFLICT_NAME, RpmTag.CONFLICT_VERSION, RpmTag.CONFLICT_FLAGS)); - result.setObsoletes(makeDependencies(header, RpmTag.OBSOLETE_NAME, RpmTag.OBSOLETE_VERSION, RpmTag.OBSOLETE_FLAGS)); + result.setProvides(makeDependencies(header, PROVIDE_NAME, PROVIDE_VERSION, PROVIDE_FLAGS)); + result.setRequires(makeDependencies(header, REQUIRE_NAME, REQUIRE_VERSION, REQUIRE_FLAGS)); + result.setConflicts(makeDependencies(header, CONFLICT_NAME, CONFLICT_VERSION, CONFLICT_FLAGS)); + result.setObsoletes(makeDependencies(header, OBSOLETE_NAME, OBSOLETE_VERSION, OBSOLETE_FLAGS)); // files @@ -115,66 +149,37 @@ public static RpmInformation makeInformation(final RpmInputStream in) throws IOE } public static List makeDependencies(final InputHeader header, final RpmTag namesTag, final RpmTag versionsTag, final RpmTag flagsTag) { - Object namesVal = header.getTag(namesTag); - Object versionsVal = header.getTag(versionsTag); - Object flagsVal = header.getTag(flagsTag); - - if (namesVal == null || !(namesVal instanceof String[])) { - if (namesVal instanceof String) { - namesVal = new String[] { (String) namesVal }; - } else { - return Collections.emptyList(); - } - } + final List names = header.getStringList(namesTag); + final List versions = header.getStringList(versionsTag); + final List flags = header.getIntegerList(flagsTag); - if (versionsVal != null && !(versionsVal instanceof String[])) { - if (versionsVal instanceof String) { - versionsVal = new String[] { (String) versionsVal }; - } else { - throw new IllegalStateException(String.format("Invalid dependencies version format [%s]: %s", versionsTag, versionsVal)); - } + if (names == null) { + return Collections.emptyList(); } - if (flagsVal != null && !(flagsVal instanceof Long[])) { - if (flagsVal instanceof Long) { - flagsVal = new Long[] { (Long) flagsVal }; - } else if (flagsVal instanceof Integer) { - flagsVal = new Long[] { ((Integer) flagsVal).longValue() }; - } else if (flagsVal instanceof Integer[]) { - flagsVal = Arrays.stream((Integer[]) flagsVal).map(Integer::longValue).toArray(Long[]::new); - } else { - throw new IllegalStateException(String.format("Invalid dependencies flags format [%s]: %s", flagsTag, flagsVal)); - } + if (names.size() != versions.size()) { + throw new IllegalStateException(String.format("Invalid size of dependency versions array [%s] - expected: %s, actual: %s", versionsTag, names.size(), versions.size())); } - final String[] names = (String[]) namesVal; - final String[] versions = (String[]) versionsVal; - final Long[] flags = (Long[]) flagsVal; - - if (versions != null && names.length != versions.length) { - throw new IllegalStateException(String.format("Invalid size of dependency versions array [%s] - expected: %s, actual: %s", versionsTag, names.length, versions.length)); - } - - if (flags != null && names.length != flags.length) { - throw new IllegalStateException(String.format("Invalid size of dependency flags array [%s] - expected: %s, actual: %s", flagsTag, names.length, flags.length)); + if (flags != null && names.size() != flags.size()) { + throw new IllegalStateException(String.format("Invalid size of dependency flags array [%s] - expected: %s, actual: %s", flagsTag, names.size(), flags.size())); } - final List result = new ArrayList<>(names.length); - + final List result = new ArrayList<>(names.size()); final Set known = new HashSet<>(); - for (int i = 0; i < names.length; i++) { - final String name = names[i]; - String version = versions != null ? versions[i] : null; + for (int i = 0; i < names.size(); i++) { + final String name = names.get(i); + String version = versions.get(i); + if (version != null && version.isEmpty()) { version = null; } - final Long flag = flags != null ? flags[i] : null; - final String key = name; // for now the key is the name + final Integer flag = flags != null ? flags.get(i) : null; - if (known.add(key)) { - result.add(new Dependency(name, version, flag != null ? flag : 0L)); + if (known.add(name)) { + result.add(new Dependency(name, version, flag != null ? flag : 0)); } } @@ -188,20 +193,4 @@ public static String normalize(final String name) { return name; } - - public static String asString(final Object value) { - if (value == null) { - return null; - } - - return new RpmTagValue(value).asString().orElse(null); - } - - public static Long asLong(final Object value) { - if (value == null) { - return null; - } - - return new RpmTagValue(value).asLong().orElse(null); - } } diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/parse/HeaderValue.java b/rpm/src/main/java/org/eclipse/packager/rpm/parse/HeaderValue.java index 1b11032..c1efd2a 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/parse/HeaderValue.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/parse/HeaderValue.java @@ -13,45 +13,20 @@ package org.eclipse.packager.rpm.parse; -import static com.google.common.io.BaseEncoding.base16; import static org.eclipse.packager.rpm.header.Type.UNKNOWN; -import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; -import java.util.function.Function; +import java.util.stream.IntStream; +import org.eclipse.packager.rpm.RpmTagValue; import org.eclipse.packager.rpm.Rpms; import org.eclipse.packager.rpm.header.Type; -public class HeaderValue { - static final class Unknown { - private final int type; - - private final byte[] value; - - public Unknown(int type, byte[] value) { - this.type = type; - this.value = value; - } - - public int getType() { - return this.type; - } - - public byte[] getValue() { - return this.value; - } - - @Override - public String toString() { - return "UNKNOWN: type: " + this.type + ", data: " + base16().encode(this.value); - } - } - +public class HeaderValue { private final int tag; - private Object value; + private RpmTagValue value; private final int originalType; @@ -73,10 +48,14 @@ public int getTag() { return this.tag; } - public Object getValue() { + public RpmTagValue getValue() { return this.value; } + public int getOriginalType() { + return this.originalType; + } + public Type getType() { return this.type; } @@ -89,84 +68,60 @@ public int getIndex() { return this.index; } - void fillFromStore(final ByteBuffer storeData) throws IOException { + @SuppressWarnings("unchecked") + void fillFromStore(final ByteBuffer storeData) { + storeData.position(this.index); switch (this.type) { case NULL: break; case CHAR: - this.value = getFromStore(storeData, true, buf -> (char) storeData.get(), Character[]::new); + this.value = (RpmTagValue) new RpmTagValue<>(this.count == 1 ? Character.valueOf((char) storeData.get()) : IntStream.range(0, this.count).mapToObj(i -> (char) storeData.get()).toArray(Character[]::new)); break; case BYTE: - this.value = getFromStore(storeData, true, ByteBuffer::get, Byte[]::new); + case UNKNOWN: + this.value = (RpmTagValue) new RpmTagValue<>(this.count == 1 ? Byte.valueOf(storeData.get()) : IntStream.range(0, this.count).mapToObj(i -> storeData.get()).toArray(Byte[]::new)); break; case SHORT: - this.value = getFromStore(storeData, true, ByteBuffer::getShort, Short[]::new); + this.value = (RpmTagValue) new RpmTagValue<>(this.count == 1 ? Short.valueOf(storeData.getShort()) : IntStream.range(0, this.count).mapToObj(i -> storeData.getShort()).toArray(Short[]::new)); break; case INT: - this.value = getFromStore(storeData, true, ByteBuffer::getInt, Integer[]::new); + this.value = (RpmTagValue) new RpmTagValue<>(this.count == 1 ? Integer.valueOf(storeData.getInt()) : IntStream.range(0, this.count).mapToObj(i -> storeData.getInt()).toArray(Integer[]::new)); break; case LONG: - this.value = getFromStore(storeData, true, ByteBuffer::getLong, Long[]::new); + this.value = (RpmTagValue) new RpmTagValue<>(this.count == 1 ? Long.valueOf(storeData.getLong()) : IntStream.range(0, this.count).mapToObj(i -> storeData.getLong()).toArray(Long[]::new)); break; case STRING: - { - // only one allowed - storeData.position(this.index); - this.value = makeString(storeData); - } + this.value = (RpmTagValue) new RpmTagValue<>(makeString(storeData)); break; case BLOB: - { - this.value = getBlob(storeData); - } + this.value = (RpmTagValue) new RpmTagValue<>(makeBlob(storeData)); break; case STRING_ARRAY: case I18N_STRING: - this.value = getFromStore(storeData, false, HeaderValue::makeString, String[]::new); - break; - case UNKNOWN: - this.value = new Unknown(this.originalType, getBlob(storeData)); + this.value = (RpmTagValue) new RpmTagValue<>(IntStream.range(0, this.count).mapToObj(i -> makeString(storeData)).toArray(String[]::new)); break; } } - private byte[] getBlob(ByteBuffer storeData) { + private byte[] makeBlob(final ByteBuffer storeData) { final byte[] data = new byte[this.count]; storeData.position(this.index); storeData.get(data); return data; } - @FunctionalInterface - public static interface IOFunction { - public R apply(T t) throws IOException; - } - - private Object getFromStore(final ByteBuffer data, final boolean collapse, final IOFunction func, final Function creator) throws IOException { - data.position(this.index); - if (this.count == 1 && collapse) { - return func.apply(data); - } - - final R[] result = creator.apply(this.count); - for (int i = 0; i < this.count; i++) { - result[i] = func.apply(data); - } - return result; - } - - private static String makeString(final ByteBuffer buf) throws IOException { + private static String makeString(final ByteBuffer buf) { final byte[] data = buf.array(); final int start = buf.position(); - for (int i = 0; i < buf.remaining(); i++) // check if there is at least one more byte, null byte - { + for (int i = 0; i < buf.remaining(); i++) { // check if there is at least one more byte, null byte if (data[start + i] == 0) { buf.position(start + i + 1); // skip content plus null byte return new String(data, start, i, StandardCharsets.UTF_8); } } - throw new IOException("Corrupt tag entry. Null byte missing!"); + + throw new IllegalArgumentException("Corrupt tag entry. Null byte missing!"); } @Override @@ -183,7 +138,7 @@ public String toString() { if (this.value != null) { if (this.type == UNKNOWN) { - sb.append(this.type); + sb.append(this.originalType); } else { sb.append(this.value.getClass().getName()); } diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/parse/InputHeader.java b/rpm/src/main/java/org/eclipse/packager/rpm/parse/InputHeader.java index 5a67eb2..072ad35 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/parse/InputHeader.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/parse/InputHeader.java @@ -13,24 +13,27 @@ package org.eclipse.packager.rpm.parse; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import org.eclipse.packager.rpm.ReadableHeader; import org.eclipse.packager.rpm.RpmBaseTag; +import org.eclipse.packager.rpm.RpmTagValue; public class InputHeader implements ReadableHeader { - private final Map entries; + private final Map> entries; private final long start; private final long length; - public InputHeader(final HeaderValue[] entries, final long start, final long length) { - final Map tags = new LinkedHashMap<>(entries.length); - for (final HeaderValue entry : entries) { + public InputHeader(final HeaderValue[] entries, final long start, final long length) { + final Map> tags = new LinkedHashMap<>(entries.length); + for (final HeaderValue entry : entries) { tags.put(entry.getTag(), entry); } @@ -58,45 +61,111 @@ public long getLength() { return this.length; } - public Object getTag(final T tag) { - return getTagOrDefault(tag, null); + public boolean hasTag(final int tag) { + return this.entries.containsKey(tag); } - public Object getTag(final int tag) { - return getTagOrDefault(tag, null); + @Override + public boolean hasTag(final T tag) { + return hasTag(tag.getValue()); + } + + public E get(T tag) { + Optional> optHeaderValue = getOptionalTag(tag, tag.getDataType()); + if (optHeaderValue.isPresent()) { + HeaderValue headerValue = optHeaderValue.get(); + RpmTagValue rpmTagValue = headerValue.getValue(); + return rpmTagValue.getValue(); + } + + return null; + } + + @Override + public String getString(T tag) { + if (!String.class.isAssignableFrom(tag.getDataType()) && !String[].class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not a string or array of strings"); + } + + if (String.class.isAssignableFrom(tag.getDataType())) { + return getOptionalTag(tag, String.class).flatMap(headerValue -> headerValue.getValue().asString()).orElse(null); + } + + return getOptionalTag(tag, String[].class).flatMap(headerValue -> headerValue.getValue().asString()).orElse(null); + } + + @Override + public Integer getInteger(T tag) { + if (!Integer.class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not an integer"); + } + + return getOptionalTag(tag, Integer.class).flatMap(headerValue -> headerValue.getValue().asInteger()).orElse(null); + } + + @Override + public Long getLong(T tag) { + if (!Long.class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not a long"); + } + + return getOptionalTag(tag, Long.class).flatMap(headerValue -> headerValue.getValue().asLong()).orElse(null); + } + + @Override + public List getStringList(T tag) { + if (!String[].class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not an array of strings"); + } + + return getOptionalTag(tag, String[].class).flatMap(headerValue -> headerValue.getValue().asStringArray().map(Arrays::asList)).orElse(null); } @Override - public Optional getValue(final T tag) { - return Optional.ofNullable(getTag(tag)); + public List getIntegerList(T tag) { + if (!Integer[].class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not an array of integers"); + } + + return getOptionalTag(tag, Integer[].class).flatMap(headerValue -> headerValue.getValue().asIntegerArray().map(Arrays::asList)).orElse(null); } - public Optional getOptionalTag(final T tag) { - return getEntry(tag).map(HeaderValue::getValue); + @Override + public List getLongList(T tag) { + if (!Long[].class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not an array of longs"); + } + + return getOptionalTag(tag, Long[].class).flatMap(headerValue -> headerValue.getValue().asLongArray().map(Arrays::asList)).orElse(null); } - public Optional getOptionalTag(final int tag) { - return getEntry(tag).map(HeaderValue::getValue); + @Override + public byte[] getByteArray(T tag) { + if (!byte[].class.isAssignableFrom(tag.getDataType())) { + throw new IllegalArgumentException("Tag " + tag + " is not an array of bytes"); + } + + return getOptionalTag(tag, byte[].class).flatMap(headerValue -> headerValue.getValue().asByteArray()).orElse(null); } - public Optional getEntry(final T tag) { - return Optional.ofNullable(this.entries.get(tag.getValue())); + public Optional> getOptionalTag(final int tag, Class dataType) { + return getEntry(tag, dataType); } - public Optional getEntry(final int tag) { - return Optional.ofNullable(this.entries.get(tag)); + public Optional> getOptionalTag(final T tag, Class dataType) { + return getOptionalTag(tag.getValue(), dataType); } - public Object getTagOrDefault(final T tag, final Object defaultValue) { - return getOptionalTag(tag).orElse(defaultValue); + @SuppressWarnings("unchecked") + private Optional> getEntry(final int tag, Class dataType) { + return Optional.ofNullable((HeaderValue) this.entries.get(tag)); } - public Object getTagOrDefault(final int tag, final Object defaultValue) { - return getOptionalTag(tag).orElse(defaultValue); + private Optional> getEntry(final T tag, Class dataType) { + return getEntry(tag.getValue(), dataType); } - public Map getRawTags() { + public Map> getRawTags() { return this.entries; } - } diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/parse/RpmInputStream.java b/rpm/src/main/java/org/eclipse/packager/rpm/parse/RpmInputStream.java index 9a8de8a..79a1ada 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/parse/RpmInputStream.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/parse/RpmInputStream.java @@ -19,7 +19,6 @@ import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.Arrays; -import java.util.Optional; import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream; import org.eclipse.packager.rpm.RpmBaseTag; @@ -89,16 +88,7 @@ protected void ensureInit() throws IOException { } private InputStream setupPayloadStream() throws IOException { - - // payload format - - final Object payloadFormatValue = this.payloadHeader.getTag(RpmTag.PAYLOAD_FORMAT); - - if (payloadFormatValue != null && !(payloadFormatValue instanceof String)) { - throw new IOException("Payload format must be a single string"); - } - - String payloadFormat = (String) payloadFormatValue; + String payloadFormat = this.payloadHeader.getString(RpmTag.PAYLOAD_FORMAT); if (payloadFormat == null) { payloadFormat = "cpio"; @@ -110,17 +100,9 @@ private InputStream setupPayloadStream() throws IOException { // payload coding - final Optional payloadCodingHeader = this.payloadHeader.getOptionalTag(RpmTag.PAYLOAD_CODING); + final String payloadCoding = this.payloadHeader.getString(RpmTag.PAYLOAD_CODING); - if (!payloadCodingHeader.isPresent()) { - return this.in; - } - - final Object payloadCodingValue = payloadCodingHeader.get(); - - final String payloadCoding = (String) payloadCodingValue; - - if (payloadCodingValue != null && !(payloadCodingValue instanceof String)) { + if (payloadCoding == null) { throw new IOException("Payload coding must be a single string"); } @@ -191,6 +173,7 @@ protected InputHeader readHeader(final boolean withPad } final byte version = this.in.readByte(); + if (version != 1) { throw new IOException(String.format("File corrupt: Invalid header entry version: %s (valid: 1)", version)); } @@ -200,7 +183,7 @@ protected InputHeader readHeader(final boolean withPad final int indexCount = this.in.readInt(); final int storeSize = this.in.readInt(); - final HeaderValue[] entries = new HeaderValue[indexCount]; + final HeaderValue[] entries = new HeaderValue[indexCount]; for (int i = 0; i < indexCount; i++) { entries[i] = readEntry(); @@ -227,13 +210,13 @@ protected InputHeader readHeader(final boolean withPad return new InputHeader<>(entries, start, end - start); } - private HeaderValue readEntry() throws IOException { + private HeaderValue readEntry() throws IOException { final int tag = this.in.readInt(); final int type = this.in.readInt(); final int offset = this.in.readInt(); final int count = this.in.readInt(); - return new HeaderValue(tag, type, offset, count); + return new HeaderValue<>(tag, type, offset, count); } private byte[] readComplete(final int size) throws IOException { diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/yum/RepositoryCreator.java b/rpm/src/main/java/org/eclipse/packager/rpm/yum/RepositoryCreator.java index 43711d1..9912091 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/yum/RepositoryCreator.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/yum/RepositoryCreator.java @@ -386,10 +386,10 @@ private Element addVersion(final Element pkg, final RpmInformation.Version versi final Element ver = addElement(pkg, "version"); - if (version.getEpoch() == null || version.getEpoch().isEmpty()) { + if (version.getEpoch() == null) { ver.setAttribute("epoch", "0"); } else { - ver.setAttribute("epoch", version.getEpoch()); + ver.setAttribute("epoch", Integer.toString(version.getEpoch())); } ver.setAttribute("ver", version.getVersion()); ver.setAttribute("rel", version.getRelease()); 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 fb80eb1..1d89a13 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/InputStreamTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/InputStreamTest.java @@ -44,15 +44,15 @@ void test1() throws IOException { final InputHeader header = in.getPayloadHeader(); assertThat(header).extracting("start").isEqualTo(280L); assertThat(header).extracting("length").isEqualTo(3501L); - assertThat(header.getTag(PAYLOAD_FORMAT)).isEqualTo("cpio"); - assertThat(header.getTag(PAYLOAD_CODING)).isEqualTo("lzma"); - assertThat(header.getTag(NAME)).isEqualTo("org.eclipse.scada"); - assertThat(header.getTag(VERSION)).isEqualTo("0.2.1"); - assertThat(header.getTag(RELEASE)).isEqualTo( "1"); - assertThat(header.getTag(ARCH)).isEqualTo( "noarch"); - assertThat(header.getTag(OS)).isEqualTo("linux"); - assertThat(header.getTag(LICENSE)).isEqualTo("EPL"); - assertThat(List.of((String[]) header.getTag(DIRNAMES))).containsExactlyElementsOf(EXPECTED_DIRNAMES); + assertThat(header.getString(PAYLOAD_FORMAT)).isEqualTo("cpio"); + assertThat(header.getString(PAYLOAD_CODING)).isEqualTo("lzma"); + assertThat(header.getString(NAME)).isEqualTo("org.eclipse.scada"); + assertThat(header.getString(VERSION)).isEqualTo("0.2.1"); + assertThat(header.getString(RELEASE)).isEqualTo( "1"); + assertThat(header.getString(ARCH)).isEqualTo( "noarch"); + assertThat(header.getString(OS)).isEqualTo("linux"); + assertThat(header.getString(LICENSE)).isEqualTo("EPL"); + assertThat(header.getStringList(DIRNAMES)).containsExactlyElementsOf(EXPECTED_DIRNAMES); } } 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 2da1ad2..f5880eb 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/Issue130Test.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/Issue130Test.java @@ -47,7 +47,7 @@ void test() throws IOException { try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(outFile)))) { Dumper.dumpAll(in); - assertThat(List.of((String[]) in.getPayloadHeader().getTag(RpmTag.PREFIXES))).containsExactlyElementsOf(PREFIXES); + assertThat(in.getPayloadHeader().getStringList(RpmTag.PREFIXES)).containsExactlyElementsOf(PREFIXES); } } 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 8a9ed07..5302d11 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/Issue136Test.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/Issue136Test.java @@ -48,7 +48,7 @@ void test(final String originalName) throws IOException { try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(outFile)))) { Dumper.dumpAll(in); - assertThat((String) in.getPayloadHeader().getTag(RpmTag.NAME)).hasSameSizeAs(originalName); + assertThat(in.getPayloadHeader().getString(RpmTag.NAME)).hasSameSizeAs(originalName); assertThat(in.getLead().getName().getBytes(StandardCharsets.UTF_8)).hasSizeLessThan(66); } } 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 b5df3b1..f64f6a4 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/SetVerifyFlagsTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/SetVerifyFlagsTest.java @@ -14,6 +14,10 @@ package org.eclipse.packager.rpm; import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.packager.rpm.RpmTag.BASENAMES; +import static org.eclipse.packager.rpm.RpmTag.DIRNAMES; +import static org.eclipse.packager.rpm.RpmTag.FILE_FLAGS; +import static org.eclipse.packager.rpm.RpmTag.FILE_VERIFYFLAGS; import java.io.BufferedInputStream; import java.io.IOException; @@ -22,7 +26,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashSet; -import java.util.List; import java.util.Set; import org.eclipse.packager.rpm.app.Dumper; @@ -95,10 +98,10 @@ void writeRpmWithVerifyFlags() throws IOException { try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(outFile)))) { Dumper.dumpAll(in); final InputHeader header = in.getPayloadHeader(); - assertThat(List.of((String[]) header.getTag(RpmTag.DIRNAMES))).containsExactly(DIRNAME); - assertThat(List.of((String[]) header.getTag(RpmTag.BASENAMES))).containsExactly(NAME_myconf, NAME_myreadme); - assertThat(List.of((Integer[]) header.getTag(RpmTag.FILE_FLAGS))).containsExactly(17, 256); - assertThat((Integer[]) header.getTag(RpmTag.FILE_VERIFYFLAGS)).containsExactly(24, -1); + assertThat(header.getStringList(DIRNAMES)).containsExactly(DIRNAME); + assertThat(header.getStringList(BASENAMES)).containsExactly(NAME_myconf, NAME_myreadme); + assertThat(header.getIntegerList(FILE_FLAGS)).containsExactly(17, 256); + assertThat(header.getIntegerList(FILE_VERIFYFLAGS)).containsExactly(24, -1); } } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/StableDependencyOrderTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/StableDependencyOrderTest.java index 25fd0fd..c98190e 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/StableDependencyOrderTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/StableDependencyOrderTest.java @@ -18,7 +18,6 @@ import static org.eclipse.packager.rpm.RpmTag.REQUIRE_NAME; import static org.eclipse.packager.rpm.RpmTag.REQUIRE_VERSION; -import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -51,12 +50,13 @@ void testStableRequirementSort() { Collections.reverse(requirementsReverse); Dependencies.putRequirements(header2, requirementsReverse); - assertThat(((RpmTagValue) header2.get(REQUIRE_NAME)).getValue()).isEqualTo(((RpmTagValue) header1.get(REQUIRE_NAME)).getValue()); - assertThat(((RpmTagValue) header2.get(REQUIRE_VERSION)).getValue()).isEqualTo(((RpmTagValue) header1.get(REQUIRE_VERSION)).getValue()); + assertThat(header2.getStringList(REQUIRE_NAME)).containsExactlyElementsOf(header1.getStringList(REQUIRE_NAME)); + assertThat(header2.getStringList(REQUIRE_VERSION)).containsExactlyElementsOf(header1.getStringList(REQUIRE_VERSION)); assertThat(getRequireFlags(header2)).isEqualTo(getRequireFlags(header1)); } private static List> getRequireFlags(Header header) { - return Arrays.stream((int[]) ((RpmTagValue) header.get(REQUIRE_FLAGS)).getValue()).mapToObj(RpmDependencyFlags::parse).collect(Collectors.toUnmodifiableList()); + List requireFlags = header.getIntegerList(REQUIRE_FLAGS); + return requireFlags != null ? requireFlags.stream().map(RpmDependencyFlags::parse).collect(Collectors.toUnmodifiableList()) : Collections.emptyList(); } } 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 18b68c8..7535add 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 @@ -15,6 +15,11 @@ import static java.nio.file.StandardOpenOption.CREATE_NEW; import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.packager.rpm.RpmSignatureTag.MD5; +import static org.eclipse.packager.rpm.RpmSignatureTag.PAYLOAD_SIZE; +import static org.eclipse.packager.rpm.RpmSignatureTag.PGP; +import static org.eclipse.packager.rpm.RpmSignatureTag.SHA1HEADER; +import static org.eclipse.packager.rpm.RpmSignatureTag.SIZE; import static org.testcontainers.containers.Container.ExecResult; import static org.testcontainers.images.builder.Transferable.DEFAULT_FILE_MODE; @@ -74,18 +79,18 @@ static void testSigningExistingRpm() throws IOException, PGPException { try (final RpmInputStream initialRpm = new RpmInputStream(new BufferedInputStream(Files.newInputStream(RPM))); final RpmInputStream rpmSigned = new RpmInputStream(new BufferedInputStream(Files.newInputStream(signedRpm)))) { // Get information of the initial rpm file final InputHeader initialHeader = initialRpm.getSignatureHeader(); - final int initialSize = (int) initialHeader.getEntry(RpmSignatureTag.SIZE).get().getValue(); - final int initialPayloadSize = (int) initialHeader.getEntry(RpmSignatureTag.PAYLOAD_SIZE).get().getValue(); - final String initialSha1 = (String) initialHeader.getEntry(RpmSignatureTag.SHA1HEADER).get().getValue(); - final byte[] initialMd5 = (byte[]) initialHeader.getEntry(RpmSignatureTag.MD5).get().getValue(); + final int initialSize = initialHeader.getInteger(SIZE); + final int initialPayloadSize = initialHeader.getInteger(PAYLOAD_SIZE); + final String initialSha1 = initialHeader.getString(SHA1HEADER); + final byte[] initialMd5 = initialHeader.getByteArray(MD5); // Get information of the signed rpm file final InputHeader signedHeader = rpmSigned.getSignatureHeader(); - final int signedSize = (int) signedHeader.getEntry(RpmSignatureTag.SIZE).get().getValue(); - final int signedPayloadSize = (int) signedHeader.getEntry(RpmSignatureTag.PAYLOAD_SIZE).get().getValue(); - final String signedSha1 = (String) signedHeader.getEntry(RpmSignatureTag.SHA1HEADER).get().getValue(); - final byte[] signedMd5 = (byte[]) signedHeader.getEntry(RpmSignatureTag.MD5).get().getValue(); - final byte[] pgpSignature = (byte[]) signedHeader.getEntry(RpmSignatureTag.PGP).get().getValue(); + final int signedSize = signedHeader.getInteger(SIZE); + final int signedPayloadSize = signedHeader.getInteger(PAYLOAD_SIZE); + final String signedSha1 = signedHeader.getString(SHA1HEADER); + final byte[] signedMd5 = signedHeader.getByteArray(MD5); + final byte[] pgpSignature = signedHeader.getByteArray(PGP); // Compare information values of initial rpm and signed rpm assertThat(signedSize).isEqualTo(initialSize);