ostrya_core/lib.rs
1#![forbid(unsafe_code)]
2
3//! The object model, the on-disk format primitives, and the typed config of
4//! ostree.
5//!
6//! A caller reads and writes the objects of an ostree repository as bytes in
7//! the on-disk format and computes their checksums. The crate also holds the
8//! format rules for loose paths, ref names, and new commits. It re-exports the
9//! GVariant codec of `ostrya-gvariant` for the metadata of a commit. The
10//! module [`config`] reads and edits the keys of a repository `config` file
11//! through key types, with no repository handle. The crate opens no file, and
12//! it compiles on Linux, macOS, and Windows.
13//!
14//! # Entry points
15//!
16//! - [`Checksum`] is the SHA-256 id of an object.
17//! - [`Commit`] reads and writes a commit object.
18//! - [`DirTree`] and [`DirMeta`] read and write the objects of a directory.
19//! - [`FileHeader`] is the header of a content object.
20//! - [`Xattrs`] is a sorted set of extended attributes.
21//! - [`ObjectType`] and [`RepoMode`] select the [`loose_path`] of an object.
22//! - [`DeflateSink`] compresses the payload of an archive-mode content object.
23//! - [`KeyFile`] parses and prints the text of the repository `config` file.
24//! - [`config::ConfigView`] and [`config::ConfigEdit`] read and edit a
25//! [`KeyFile`] through the key types of [`config`].
26//! - [`Ref`] is one ref of a repository: local, remote, or collection.
27//! - [`RefPattern`] checks and matches one ref pattern with `*` and `**`.
28//! - [`RefSelector`] selects ref names with a list of positive and negative
29//! patterns.
30//!
31//! # Modules
32//!
33//! - [`base64`]: standard base64 for byte strings of any length.
34//! - [`config`]: the typed config. It holds the traits of the groups and the
35//! keys, the text form of a value, and the typed reads and edits. It also
36//! holds the groups `[core]` and `[ex-integrity]`, and the value types of
37//! the keys of the port that use no item of `ostrya`.
38//! - [`filehdr`]: the content-object header, its framing, and its checksum.
39//! - [`sizes`]: the packed entries of the `ostree.sizes` commit metadata.
40//! - [`varint`]: the LEB128 varints of the format.
41//!
42//! # Typed config
43//!
44//! Each key of `config` is a zero-sized type. The type states the group, the
45//! key name, the value type, and the default of the key. The module
46//! [`config::repo`] holds the key types of `[core]`, and the module
47//! [`config::ex_integrity`] holds the key types of `[ex-integrity]`.
48//!
49//! - [`config::ConfigView`] reads a [`KeyFile`] through the key types. It has
50//! no override, so the value in effect of a key is the value of the file or
51//! the default of the key.
52//! - [`config::ConfigEdit`] edits a [`KeyFile`] in memory through the key
53//! types.
54//! - [`config_group!`] defines a group of a library user and its key types.
55//! The name of the group starts with `ex-`.
56//! - A library user implements [`config::Value`] for each value type of its
57//! own.
58//! - A typed read and a typed edit give [`config::ConfigError`].
59//!
60//! The `ostrya` crate re-exports each documented item of [`config`] under
61//! `ostrya::config`, and the macro as `ostrya::config_group!`. It adds
62//! `RepoConfig` and the other groups of the port. `RepoConfig` applies the
63//! overrides of a repository handle and reads the remotes of the remotes
64//! directory.
65//!
66//! `ostrya` reads and writes `config` with a cap of 1 MiB (1048576 bytes). A
67//! program that links `ostrya-core` alone reads at most that many bytes before
68//! [`KeyFile::parse`]. It does not write a larger document, because the open
69//! of `ostrya` refuses such a file.
70//!
71//! # Examples
72//!
73//! ```
74//! use ostrya_core::{Checksum, DirMeta, ObjectType, RepoMode, Xattrs, loose_path};
75//!
76//! // The root directory of a commit: uid 0, gid 0, mode 0755, no xattrs.
77//! let meta = DirMeta { uid: 0, gid: 0, mode: 0o040755, xattrs: Xattrs::empty() };
78//! let checksum = Checksum::sha256(&meta.serialize()?);
79//! let hex = "446a0ef11b7cc167f3b603e585c7eeeeb675faa412d5ec73f62988eb0b6c5488";
80//! assert_eq!(checksum.to_hex(), hex);
81//! let path = loose_path(&checksum, ObjectType::DirMeta, RepoMode::Bare);
82//! assert_eq!(path, format!("44/{}.dirmeta", &hex[2..]));
83//! # Ok::<(), ostrya_core::Error>(())
84//! ```
85//!
86//! A library user defines a group, reads the group and a key of `[core]`, and
87//! edits the key file:
88//!
89//! ```
90//! use ostrya_core::config::{ConfigEdit, ConfigView, Tristate, ex_integrity, repo};
91//! use ostrya_core::{KeyFile, RepoMode};
92//!
93//! ostrya_core::config_group! {
94//! /// The keys of a build tool.
95//! pub group tool = "ex-tool" {
96//! /// The number of parallel jobs.
97//! Jobs: u32 = "jobs", default 4;
98//! }
99//! }
100//!
101//! let mut kf = KeyFile::parse("[core]\nrepo_version=1\nmode=bare\n[ex-tool]\njobs=8\n")?;
102//! let view = ConfigView::new(&kf);
103//! assert_eq!(view.get::<repo::Mode>()?, RepoMode::Bare);
104//! assert_eq!(view.get::<tool::Jobs>()?, 8);
105//!
106//! ConfigEdit::new(&mut kf).set::<ex_integrity::Fsverity>(Tristate::No)?;
107//! assert_eq!(kf.get_value("ex-integrity", "fsverity"), Some("no"));
108//! # Ok::<(), ostrya_core::config::ConfigError>(())
109//! ```
110
111pub mod base64;
112mod be;
113mod checksum;
114mod commit;
115pub mod config;
116mod deflate;
117mod dirmeta;
118mod dirtree;
119mod error;
120pub mod filehdr;
121mod keyfile;
122mod loosepath;
123mod mode;
124mod objname;
125mod objtype;
126mod refname;
127mod refpattern;
128pub mod sizes;
129mod valiter;
130pub mod varint;
131mod xattr;
132
133pub use checksum::Checksum;
134pub use commit::{
135 Commit, CommitLink, TimestampError, commit_metadata, commit_timestamp, ref_binding,
136 source_date_epoch,
137};
138pub use deflate::{DeflateReader, DeflateSink};
139pub use dirmeta::{DirMeta, DirMetaRef};
140pub use dirtree::{DirTree, DirTreeRef};
141pub use error::{Error, Result};
142#[doc(hidden)]
143pub use filehdr::{ContentHasher, FileHeader};
144pub use keyfile::KeyFile;
145pub use loosepath::loose_path;
146pub use mode::RepoMode;
147pub use objname::ObjectName;
148pub use objtype::ObjectType;
149pub use refname::{
150 MAX_COLLECTION_ID_LEN, ParseRefError, Ref, RefNamespace, is_checksum_shaped, is_collection_id,
151 is_ref_component, is_ref_name, is_ref_name_component, is_refspec, is_valid_collection_id,
152 split_collection,
153};
154pub use refpattern::{RefPattern, RefSelector};
155pub use xattr::{Xattrs, XattrsRef};
156
157// The dynamic GVariant value tree and the codec functions. `Commit::metadata`
158// is a `Value`. With these re-exports, a dependent crate can read and write
159// any metadata dict with no direct dependency on `ostrya-gvariant`.
160#[doc(no_inline)]
161pub use ostrya_gvariant::{
162 ArrayIter, DictBuilder, GvDecode, GvEncode, GvType, Span, TextError, Type, Value, VariantBytes,
163 choose_offset_size, from_bytes, from_text, offset_size_for, to_bytes, to_text,
164 to_text_unannotated, tuple_field_from_bytes, validate, write_array, write_offset,
165};
166
167/// The size limit of a metadata object: 128 MiB.
168///
169/// The format sets this limit. A reader loads a metadata object whole, so it
170/// must refuse a larger object.
171pub const MAX_METADATA_SIZE: u64 = 128 * 1024 * 1024;
172
173#[cfg(test)]
174mod tests {
175 use ostrya_gvariant::{GvType, Type};
176
177 use crate::{Checksum, Commit, DirMeta, DirTree, FileHeader};
178
179 /// Checks that the `ALIGNMENT` and `FIXED_SIZE` constants of an object
180 /// type match the values that its signature gives.
181 fn assert_pinned<T: GvType>() {
182 let ty = Type::parse(T::SIGNATURE).unwrap();
183 assert_eq!(
184 T::ALIGNMENT,
185 ty.alignment(),
186 "alignment for {}",
187 T::SIGNATURE
188 );
189 assert_eq!(
190 T::FIXED_SIZE,
191 ty.fixed_size(),
192 "fixed size for {}",
193 T::SIGNATURE
194 );
195 }
196
197 #[test]
198 fn object_constants_match_their_signatures() {
199 assert_pinned::<Checksum>();
200 assert_pinned::<FileHeader>();
201 assert_pinned::<DirMeta>();
202 assert_pinned::<DirTree>();
203 assert_pinned::<Commit>();
204 }
205}