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