blob: 8c6fd1982a241c135f2cb6b03ef3020199d8ac02 [file] [log] [blame]
Matthias Andreas Benkardb5d657a2022-02-03 21:14:30 +01001// SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de>
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +01005package eu.mulk.jgvariant.ostree;
6
7import eu.mulk.jgvariant.core.Decoder;
Matthias Andreas Benkard05114642021-12-29 21:51:29 +01008import java.nio.ByteBuffer;
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +01009import java.nio.ByteOrder;
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010010import java.util.ArrayList;
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010011import java.util.List;
12
13/**
14 * An entry in a {@link DeltaSuperblock}.
15 *
16 * <p>Reference: {@code ostree-repo-static-delta-private.h#OSTREE_STATIC_DELTA_META_ENTRY_FORMAT}
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010017 *
18 * @param version the version corresponding to the version of {@link DeltaPartPayload}; always 0.
19 * @param checksum the checksum of the {@link DeltaPartPayload}.
20 * @param compressedSize the total compressed size of the delta.
21 * @param uncompressedSize the total uncompressed size of the files generated by the delta.
22 * @param objects a list of objects generated by the delta payload.
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010023 */
24public record DeltaMetaEntry(
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010025 int version,
26 Checksum checksum,
27 long compressedSize,
28 long uncompressedSize,
29 List<DeltaObject> objects) {
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010030
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010031 /**
32 * A reference to an object generated by a {@link DeltaPartPayload}.
33 *
34 * @param objectType the file type.
35 * @param checksum the checksum of the resulting file.
36 */
37 public record DeltaObject(ObjectType objectType, Checksum checksum) {
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010038
39 private static final Decoder<DeltaObject> DECODER =
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010040 Decoder.ofStructure(
41 DeltaObject.class, Decoder.ofByte().map(ObjectType::valueOf), Checksum.decoder());
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010042
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010043 /**
44 * Acquires a {@link Decoder} for the enclosing type.
45 *
46 * @return a possibly shared {@link Decoder}.
47 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010048 public static Decoder<DeltaObject> decoder() {
49 return DECODER;
50 }
51 }
52
53 private static final Decoder<DeltaMetaEntry> DECODER =
54 Decoder.ofStructure(
55 DeltaMetaEntry.class,
Matthias Andreas Benkarda8514a32021-12-30 21:01:48 +010056 Decoder.ofInt(),
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010057 Checksum.decoder(),
Matthias Andreas Benkarda8514a32021-12-30 21:01:48 +010058 Decoder.ofLong(),
59 Decoder.ofLong(),
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010060 Decoder.ofByteArray().map(DeltaMetaEntry::parseObjectList));
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010061
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010062 private static List<DeltaObject> parseObjectList(byte[] bytes) {
63 var byteBuffer = ByteBuffer.wrap(bytes);
64 List<DeltaObject> objects = new ArrayList<>();
65
66 while (byteBuffer.hasRemaining()) {
67 var type = ObjectType.valueOf(byteBuffer.get());
68 var checksum = Checksum.readFrom(byteBuffer);
69 objects.add(new DeltaObject(type, checksum));
70 }
71
72 return objects;
73 }
74
75 /**
76 * Acquires a {@link Decoder} for the enclosing type.
77 *
Matthias Andreas Benkarda8514a32021-12-30 21:01:48 +010078 * <p><strong>Note:</strong> This decoder has an unspecified {@link ByteOrder}.
79 *
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010080 * @return a possibly shared {@link Decoder}.
81 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010082 public static Decoder<DeltaMetaEntry> decoder() {
83 return DECODER;
84 }
Matthias Andreas Benkarda8514a32021-12-30 21:01:48 +010085
86 DeltaMetaEntry byteSwapped() {
87 // FIXME
88 return new DeltaMetaEntry(version, checksum, compressedSize, uncompressedSize, objects);
89 }
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010090}