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