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 | |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 7 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 8 | |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 9 | import eu.mulk.jgvariant.core.Decoder; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 10 | import java.util.List; |
| 11 | |
| 12 | /** |
| 13 | * Metadata describing files and directories of a file tree. |
| 14 | * |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 15 | * <p>Often comes in a pair with {@link DirMeta}. |
| 16 | * |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 17 | * <p>Referenced by {@link Commit#rootDirTreeChecksum()} and recursively by {@link |
| 18 | * Directory#treeChecksum()}. |
| 19 | * |
| 20 | * <p>Reference: {@code ostree-core.h#OSTREE_TREE_GVARIANT_STRING} |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 21 | * |
| 22 | * @param files a list of files in the directory. |
| 23 | * @param directories a list of subdirectories of the directory. |
| 24 | * @see DirMeta |
| 25 | * @see ObjectType#DIR_TREE |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 26 | */ |
| 27 | public record DirTree(List<File> files, List<Directory> directories) { |
| 28 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 29 | /** |
| 30 | * A file in a file tree. |
| 31 | * |
| 32 | * @param name the file name. |
| 33 | * @param checksum the checksum of the {@link ObjectType#FILE} object. |
| 34 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 35 | public record File(String name, Checksum checksum) { |
| 36 | |
| 37 | private static final Decoder<File> DECODER = |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 38 | Decoder.ofStructure(File.class, Decoder.ofString(UTF_8), Checksum.decoder()); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 39 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 40 | /** |
| 41 | * Acquires a {@link Decoder} for the enclosing type. |
| 42 | * |
| 43 | * @return a possibly shared {@link Decoder}. |
| 44 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 45 | public static Decoder<File> decoder() { |
| 46 | return DECODER; |
| 47 | } |
| 48 | } |
| 49 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 50 | /** |
| 51 | * A subdirectory in a file tree. |
| 52 | * |
| 53 | * @param name the name of the subdirectory. |
| 54 | * @param treeChecksum the checksum of the {@link DirTree} object. |
| 55 | * @param dirChecksum the checksum of the {@link DirMeta} object. |
| 56 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 57 | public record Directory(String name, Checksum treeChecksum, Checksum dirChecksum) { |
| 58 | |
| 59 | private static final Decoder<Directory> DECODER = |
| 60 | Decoder.ofStructure( |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 61 | Directory.class, Decoder.ofString(UTF_8), Checksum.decoder(), Checksum.decoder()); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 62 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 63 | /** |
| 64 | * Acquires a {@link Decoder} for the enclosing type. |
| 65 | * |
| 66 | * @return a possibly shared {@link Decoder}. |
| 67 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 68 | public static Decoder<Directory> decoder() { |
| 69 | return DECODER; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private static final Decoder<DirTree> DECODER = |
| 74 | Decoder.ofStructure( |
| 75 | DirTree.class, Decoder.ofArray(File.decoder()), Decoder.ofArray(Directory.decoder())); |
| 76 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 77 | /** |
| 78 | * Acquires a {@link Decoder} for the enclosing type. |
| 79 | * |
| 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<DirTree> decoder() { |
| 83 | return DECODER; |
| 84 | } |
| 85 | } |