blob: 6453838e155ecabfdfaa599e7b0e3c758f9efefe [file] [log] [blame]
Matthias Andreas Benkardb5d657a2022-02-03 21:14:30 +01001// SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de>
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
Matthias Andreas Benkard05114642021-12-29 21:51:29 +01005package eu.mulk.jgvariant.ostree;
6
7import static org.apiguardian.api.API.Status.STABLE;
8
9import org.apiguardian.api.API;
10
11/**
12 * An object type as found in an OSTree repository.
13 *
14 * <p>Each object type has its own file extension.
15 *
16 * <p>In an OSTree repository, objects are located in a subfolder of the {@code /objects} folder
17 * based on their {@link Checksum}. The schema for looking up objects is {@code
18 * /objects/{checksumHead}/{checksumRest}.{fileExtension}} where:
19 *
20 * <dl>
21 * <dt>{@code {checksumHead}}
22 * <dd>the first two characters of {@link Checksum#hex()}
23 * <dt>{@code {checksumRest}}
24 * <dd>the substring of {@link Checksum#hex()} starting from the 3rd character
25 * <dt>{@code {fileExtension}}
26 * <dd>the {@link #fileExtension()} of the object type
27 * </dl>
28 */
29@API(status = STABLE)
30public enum ObjectType {
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010031
32 /**
33 * A regular file.
34 *
35 * <p>File ending: {@code .file}
36 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010037 FILE((byte) 1, "file"),
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010038
39 /**
40 * A serialized {@link DirTree} object.
41 *
42 * <p>File ending: {@code .dirtree}
43 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010044 DIR_TREE((byte) 2, "dirtree"),
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010045
46 /**
47 * A serialized {@link DirMeta} object.
48 *
49 * <p>File ending: {@code .dirmeta}
50 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010051 DIR_META((byte) 3, "dirmeta"),
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010052
53 /**
54 * A serialized {@link Commit} object.
55 *
56 * <p>File ending: {@code .commit}
57 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010058 COMMIT((byte) 4, "commit"),
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010059
60 /**
61 * A tombstone file standing in for a commit that was deleted.
62 *
63 * <p>File ending: {@code .commit-tombstone}
64 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010065 TOMBSTONE_COMMIT((byte) 5, "commit-tombstone"),
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010066
67 /**
68 * Detached metadata for a {@link Commit}.
69 *
70 * <p>Often goes together with a {@link #TOMBSTONE_COMMIT}.
71 *
72 * <p>File ending: {@code .commitmeta}
73 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010074 COMMIT_META((byte) 6, "commitmeta"),
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010075
76 /**
77 * A symlink to a {@link #FILE} that lives somewhere else.
78 *
79 * <p>File ending: {@code .payload-link}
80 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010081 PAYLOAD_LINK((byte) 7, "payload-link");
82
83 private final byte byteValue;
84 private final String fileExtension;
85
86 /**
87 * The serialized byte value.
88 *
89 * @return a byte representing this value in serialized GVariant structures.
90 */
91 public byte byteValue() {
92 return byteValue;
93 }
94
95 /**
96 * The file extension carried by files of this type.
97 *
98 * @return a file extension.
99 */
100 public String fileExtension() {
101 return fileExtension;
102 }
103
104 ObjectType(byte byteValue, String fileExtension) {
105 this.byteValue = byteValue;
106 this.fileExtension = fileExtension;
107 }
108
109 /**
110 * Returns the {@link ObjectType} corresponding to a serialized GVariant value.
111 *
112 * @param byteValue a serialized value as used in GVariant.
113 * @return the {@link ObjectType} corresponding to the serialized value.
114 * @throws IllegalArgumentException if the byte value is invalid.
115 */
116 public static ObjectType valueOf(byte byteValue) {
117 return switch (byteValue) {
118 case 1 -> FILE;
119 case 2 -> DIR_TREE;
120 case 3 -> DIR_META;
121 case 4 -> COMMIT;
122 case 5 -> TOMBSTONE_COMMIT;
123 case 6 -> COMMIT_META;
124 case 7 -> PAYLOAD_LINK;
125 default -> throw new IllegalArgumentException("invalid ObjectType: %d".formatted(byteValue));
126 };
127 }
128}