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 | 50a626d | 2021-12-30 19:13:49 +0100 | [diff] [blame] | 8 | import java.io.ByteArrayInputStream; |
| 9 | import java.io.ByteArrayOutputStream; |
| 10 | import java.io.IOException; |
| 11 | import java.io.UncheckedIOException; |
Matthias Andreas Benkard | e74b800 | 2021-12-30 18:52:10 +0100 | [diff] [blame] | 12 | import java.nio.ByteBuffer; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 13 | import java.nio.ByteOrder; |
Matthias Andreas Benkard | c981cde | 2021-12-30 20:37:39 +0100 | [diff] [blame] | 14 | import java.util.ArrayList; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 15 | import java.util.List; |
Matthias Andreas Benkard | 50a626d | 2021-12-30 19:13:49 +0100 | [diff] [blame] | 16 | import org.tukaani.xz.XZInputStream; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 17 | |
| 18 | /** |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 19 | * A payload file from a static delta. |
| 20 | * |
| 21 | * <p>The first byte is a compression byte: {@code 0} for none, {@code 'x'} for LZMA. The actual |
| 22 | * GVariant data begins right after. |
| 23 | * |
| 24 | * <p>Reference: {@code |
| 25 | * ostree-repo-static-delta-private.h#OSTREE_STATIC_DELTA_PART_PAYLOAD_FORMAT_V0} |
| 26 | * |
| 27 | * @param fileModes the {@link FileMode}s of the files generated by this delta payload. |
| 28 | * @param xattrs the {@link Xattr}s of the files generated by this delta payload. |
| 29 | * @param rawDataSource the data bytes used in the delta operations. |
| 30 | * @param operations the operations to apply during delta patching. |
| 31 | * @see DeltaSuperblock |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 32 | */ |
| 33 | public record DeltaPartPayload( |
| 34 | List<FileMode> fileModes, |
| 35 | List<List<Xattr>> xattrs, |
| 36 | ByteString rawDataSource, |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 37 | List<DeltaOperation> operations) { |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 38 | |
Matthias Andreas Benkard | e74b800 | 2021-12-30 18:52:10 +0100 | [diff] [blame] | 39 | private static ByteBuffer preparse(ByteBuffer byteBuffer) { |
| 40 | byte compressionByte = byteBuffer.get(0); |
Matthias Andreas Benkard | 50a626d | 2021-12-30 19:13:49 +0100 | [diff] [blame] | 41 | var dataSlice = byteBuffer.slice(1, byteBuffer.limit() - 1); |
Matthias Andreas Benkard | e74b800 | 2021-12-30 18:52:10 +0100 | [diff] [blame] | 42 | return switch (compressionByte) { |
Matthias Andreas Benkard | 50a626d | 2021-12-30 19:13:49 +0100 | [diff] [blame] | 43 | case 0 -> dataSlice; |
| 44 | case (byte) 'x' -> { |
| 45 | try { |
| 46 | var dataBytes = new byte[dataSlice.limit()]; |
| 47 | dataSlice.get(dataBytes); |
| 48 | var decompressingInputStream = new XZInputStream(new ByteArrayInputStream(dataBytes)); |
| 49 | |
| 50 | var decompressedOutputStream = new ByteArrayOutputStream(); |
| 51 | decompressingInputStream.transferTo(decompressedOutputStream); |
| 52 | |
| 53 | yield ByteBuffer.wrap(decompressedOutputStream.toByteArray()); |
| 54 | } catch (IOException e) { |
| 55 | // impossible |
| 56 | throw new UncheckedIOException(e); |
| 57 | } |
| 58 | } |
Matthias Andreas Benkard | e74b800 | 2021-12-30 18:52:10 +0100 | [diff] [blame] | 59 | default -> throw new IllegalArgumentException( |
| 60 | "unrecognized compression byte '%d'".formatted(compressionByte)); |
| 61 | }; |
| 62 | } |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 63 | |
Matthias Andreas Benkard | c981cde | 2021-12-30 20:37:39 +0100 | [diff] [blame] | 64 | private static List<DeltaOperation> parseDeltaOperationList( |
| 65 | ByteString byteString, List<ObjectType> objectTypes) { |
| 66 | List<DeltaOperation> deltaOperations = new ArrayList<>(); |
| 67 | var byteBuffer = ByteBuffer.wrap(byteString.bytes()); |
| 68 | int objectIndex = 0; |
| 69 | |
| 70 | while (byteBuffer.hasRemaining()) { |
| 71 | var currentOperation = DeltaOperation.readFrom(byteBuffer, objectTypes.get(objectIndex)); |
| 72 | deltaOperations.add(currentOperation); |
| 73 | if (currentOperation instanceof DeltaOperation.Close |
| 74 | || currentOperation instanceof DeltaOperation.OpenSpliceAndCloseMeta |
| 75 | || currentOperation instanceof DeltaOperation.OpenSpliceAndCloseReal) { |
| 76 | ++objectIndex; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return deltaOperations; |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * A file mode triple (UID, GID, and permission bits). |
| 85 | * |
Matthias Andreas Benkard | c981cde | 2021-12-30 20:37:39 +0100 | [diff] [blame] | 86 | * @param uid the user ID that owns the file. |
| 87 | * @param gid the group ID that owns the file. |
| 88 | * @param mode the POSIX permission bits. |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 89 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 90 | public record FileMode(int uid, int gid, int mode) { |
| 91 | |
| 92 | private static final Decoder<FileMode> DECODER = |
| 93 | Decoder.ofStructure( |
| 94 | FileMode.class, |
Matthias Andreas Benkard | c981cde | 2021-12-30 20:37:39 +0100 | [diff] [blame] | 95 | Decoder.ofInt().withByteOrder(ByteOrder.BIG_ENDIAN), |
| 96 | Decoder.ofInt().withByteOrder(ByteOrder.BIG_ENDIAN), |
| 97 | Decoder.ofInt().withByteOrder(ByteOrder.BIG_ENDIAN)); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 98 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 99 | /** |
| 100 | * Acquires a {@link Decoder} for the enclosing type. |
| 101 | * |
| 102 | * @return a possibly shared {@link Decoder}. |
| 103 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 104 | public static Decoder<FileMode> decoder() { |
| 105 | return DECODER; |
| 106 | } |
| 107 | } |
| 108 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 109 | /** |
| 110 | * Acquires a {@link Decoder} for the enclosing type. |
| 111 | * |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 112 | * @return a possibly shared {@link Decoder}. |
| 113 | */ |
Matthias Andreas Benkard | c981cde | 2021-12-30 20:37:39 +0100 | [diff] [blame] | 114 | public static Decoder<DeltaPartPayload> decoder(DeltaMetaEntry deltaMetaEntry) { |
| 115 | var objectTypes = |
| 116 | deltaMetaEntry.objects().stream().map(DeltaMetaEntry.DeltaObject::objectType).toList(); |
| 117 | return Decoder.ofStructure( |
| 118 | DeltaPartPayload.class, |
| 119 | Decoder.ofArray(FileMode.decoder()), |
| 120 | Decoder.ofArray(Decoder.ofArray(Xattr.decoder())), |
| 121 | ByteString.decoder(), |
| 122 | ByteString.decoder() |
| 123 | .map(byteString -> parseDeltaOperationList(byteString, objectTypes))) |
| 124 | .contramap(DeltaPartPayload::preparse); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 125 | } |
| 126 | } |