Matthias Andreas Benkard | b5d657a | 2022-02-03 21:14:30 +0100 | [diff] [blame^] | 1 | // SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de> |
| 2 | // |
| 3 | // SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 5 | package eu.mulk.jgvariant.ostree; |
| 6 | |
| 7 | import eu.mulk.jgvariant.core.Decoder; |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 8 | import java.nio.ByteBuffer; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 9 | import java.nio.ByteOrder; |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 10 | import java.util.ArrayList; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 11 | import 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 Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 17 | * |
| 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 Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 23 | */ |
| 24 | public record DeltaMetaEntry( |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 25 | int version, |
| 26 | Checksum checksum, |
| 27 | long compressedSize, |
| 28 | long uncompressedSize, |
| 29 | List<DeltaObject> objects) { |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 30 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 31 | /** |
| 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 Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 38 | |
| 39 | private static final Decoder<DeltaObject> DECODER = |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 40 | Decoder.ofStructure( |
| 41 | DeltaObject.class, Decoder.ofByte().map(ObjectType::valueOf), Checksum.decoder()); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 42 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 43 | /** |
| 44 | * Acquires a {@link Decoder} for the enclosing type. |
| 45 | * |
| 46 | * @return a possibly shared {@link Decoder}. |
| 47 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 48 | 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 Benkard | a8514a3 | 2021-12-30 21:01:48 +0100 | [diff] [blame] | 56 | Decoder.ofInt(), |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 57 | Checksum.decoder(), |
Matthias Andreas Benkard | a8514a3 | 2021-12-30 21:01:48 +0100 | [diff] [blame] | 58 | Decoder.ofLong(), |
| 59 | Decoder.ofLong(), |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 60 | Decoder.ofByteArray().map(DeltaMetaEntry::parseObjectList)); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 61 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 62 | 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 Benkard | a8514a3 | 2021-12-30 21:01:48 +0100 | [diff] [blame] | 78 | * <p><strong>Note:</strong> This decoder has an unspecified {@link ByteOrder}. |
| 79 | * |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 80 | * @return a possibly shared {@link Decoder}. |
| 81 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 82 | public static Decoder<DeltaMetaEntry> decoder() { |
| 83 | return DECODER; |
| 84 | } |
Matthias Andreas Benkard | a8514a3 | 2021-12-30 21:01:48 +0100 | [diff] [blame] | 85 | |
| 86 | DeltaMetaEntry byteSwapped() { |
| 87 | // FIXME |
| 88 | return new DeltaMetaEntry(version, checksum, compressedSize, uncompressedSize, objects); |
| 89 | } |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 90 | } |