1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//! The kernel version of a bootable commit, and the metadata pair that holds
//! it.
//!
//! A bootable commit holds two keys in its metadata:
//!
//! - `ostree.linux`, the name of the directory under `/usr/lib/modules` that
//! holds an entry named `vmlinuz`. The tree of the commit holds exactly one
//! such directory.
//! - `ostree.bootable`, the value `true`.
//!
//! [`Transaction::kernel_version`] finds the version in a staged tree.
//! [`RepoTree::kernel_version`] finds it in a committed tree.
//! [`BootableRefusal`] names the four tree shapes that give no version.
//! [`BootableMetadata`] adds the pair to a [`DictBuilder`].
use ostrya_core::DictBuilder;
use crate::error::Result;
use crate::transaction::Transaction;
use crate::tree::{RepoTree, TreeEntry};
/// The commit metadata key that holds the name of the kernel directory.
const LINUX_KEY: &str = "ostree.linux";
/// The commit metadata key that marks a commit as bootable.
const BOOTABLE_KEY: &str = "ostree.bootable";
/// The directory that the search reads, one level deep, for the kernel.
const MODULES_DIR: &str = "/usr/lib/modules";
/// The entry name that a kernel directory must hold.
const KERNEL_ENTRY: &str = "vmlinuz";
/// A tree shape that names no kernel version.
///
/// The `kernel_version` methods return it in the inner `Result`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BootableRefusal {
/// A component of `/usr/lib/modules` that the tree does not hold.
MissingComponent {
/// The absolute path of the first absent component.
path: String,
},
/// A component of `/usr/lib/modules` that is not a directory.
///
/// A regular file and a symlink both give this refusal.
NotADirectory {
/// The absolute path of the component that is not a directory.
path: String,
},
/// No directory under `/usr/lib/modules` holds an entry named `vmlinuz`.
NoKernel,
/// Two or more directories under `/usr/lib/modules` hold a `vmlinuz` entry.
///
/// No single kernel version names the tree.
MultipleKernels,
}
/// The place where the search reads the directories of the tree.
#[derive(Clone, Copy)]
enum DirSource<'a> {
/// A published repository. Every dirtree is a loose object under
/// `objects/`.
Published,
/// A transaction. The search reads a dirtree that the transaction staged
/// from the staging directory. It reads a dirtree that deduplicated
/// against a stored object from `objects/`.
Staged(&'a Transaction),
}
impl DirSource<'_> {
async fn read_dir(&self, tree: &RepoTree) -> Result<Vec<TreeEntry>> {
match self {
DirSource::Published => tree.read_dir().await,
DirSource::Staged(txn) => txn.read_dir(tree).await,
}
}
}
/// Methods that read the kernel version of a staged tree.
impl Transaction {
/// Returns the kernel version of a tree that this transaction staged.
///
/// The version is the value of `ostree.linux`. The search reads each
/// dirtree with [`read_dir`](Transaction::read_dir), so it sees the objects
/// that this transaction staged and the objects in `objects/`. A caller can
/// derive the metadata of a commit before the transaction commit.
///
/// The search rules are the same as in [`RepoTree::kernel_version`]. The
/// inner `Result` holds the version or a [`BootableRefusal`]. A refusal is
/// an outcome of the search. The outer `Result` holds the errors of the
/// object reads.
///
/// # Errors
///
/// - [`Error::ObjectNotFound`] if a dirtree on the search path is not
/// staged and not in `objects/`.
/// - [`Error::Io`] if the read of a dirtree fails, or if a dirtree is
/// larger than [`MAX_METADATA_SIZE`](crate::MAX_METADATA_SIZE).
/// - [`Error::Core`] if a dirtree does not parse.
///
/// [`Error::ObjectNotFound`]: crate::error::Error::ObjectNotFound
/// [`Error::Io`]: crate::error::Error::Io
/// [`Error::Core`]: crate::error::Error::Core
pub async fn kernel_version(
&self,
root: &RepoTree,
) -> Result<std::result::Result<String, BootableRefusal>> {
kernel_version(DirSource::Staged(self), root).await
}
}
impl RepoTree {
/// Returns the kernel version of this tree.
///
/// The version is the value of `ostree.linux`. The search reads each
/// dirtree with [`read_dir`](RepoTree::read_dir), which reads `objects/`
/// alone, so it sees a tree only after the transaction commit. A caller
/// that reads a deployed commit uses this method.
/// [`Transaction::kernel_version`] reads a tree that is still staged.
///
/// The inner `Result` holds the version or a [`BootableRefusal`]. A refusal
/// is an outcome of the search. The outer `Result` holds the errors of the
/// object reads.
///
/// # Search
///
/// The search walks `/usr/lib/modules` from this tree. Then it reads each
/// directory one level under `/usr/lib/modules` and looks for an entry
/// named `vmlinuz`. It reads no deeper level.
///
/// - An entry under `/usr/lib/modules` that is not a directory takes no
/// part in the search.
/// - The search does not read the type of the `vmlinuz` entry. A regular
/// file, a symlink, and a directory of that name each count.
/// - If exactly one directory holds `vmlinuz`, its name is the version.
/// Each other outcome is a [`BootableRefusal`].
///
/// # Errors
///
/// - [`Error::ObjectNotFound`] if a dirtree on the search path is not in
/// the object store. This includes a dirtree that a transaction staged
/// before its transaction commit.
/// - [`Error::Io`] if a dirtree is larger than
/// [`MAX_METADATA_SIZE`](crate::MAX_METADATA_SIZE) or is not a regular
/// file, and for other failures of the file system.
/// - [`Error::Core`] if a dirtree does not parse.
///
/// [`Error::ObjectNotFound`]: crate::error::Error::ObjectNotFound
/// [`Error::Io`]: crate::error::Error::Io
/// [`Error::Core`]: crate::error::Error::Core
pub async fn kernel_version(&self) -> Result<std::result::Result<String, BootableRefusal>> {
kernel_version(DirSource::Published, self).await
}
}
/// Walks `/usr/lib/modules` from `root` and returns the name of the single
/// child directory that holds `vmlinuz`. Both `kernel_version` methods call it.
async fn kernel_version(
source: DirSource<'_>,
root: &RepoTree,
) -> Result<std::result::Result<String, BootableRefusal>> {
let mut dir = root.clone();
let mut walked = String::new();
for component in MODULES_DIR.split('/').filter(|part| !part.is_empty()) {
walked.push('/');
walked.push_str(component);
let entry = source
.read_dir(&dir)
.await?
.into_iter()
.find(|entry| entry_name(entry) == component);
match entry {
None => return Ok(Err(BootableRefusal::MissingComponent { path: walked })),
Some(TreeEntry::File { .. }) => {
return Ok(Err(BootableRefusal::NotADirectory { path: walked }));
}
Some(TreeEntry::Dir { tree, .. }) => dir = tree,
}
}
let mut found = Vec::new();
for entry in source.read_dir(&dir).await? {
if let TreeEntry::Dir { name, tree } = entry
&& source
.read_dir(&tree)
.await?
.iter()
.any(|entry| entry_name(entry) == KERNEL_ENTRY)
{
found.push(name);
}
}
match found.len() {
0 => Ok(Err(BootableRefusal::NoKernel)),
1 => Ok(Ok(found.remove(0))),
_ => Ok(Err(BootableRefusal::MultipleKernels)),
}
}
/// Returns the name of a directory entry of either kind.
fn entry_name(entry: &TreeEntry) -> &str {
match entry {
TreeEntry::File { name, .. } | TreeEntry::Dir { name, .. } => name,
}
}
/// An extension of [`DictBuilder`] that adds the bootable metadata pair.
pub trait BootableMetadata {
/// Appends `ostree.linux` with `kernel_version`, then `ostree.bootable`
/// with `true`.
///
/// The pair goes in at the current position of the builder. If the caller
/// inserted its own keys before this call, the pair comes after them.
///
/// The `ostree` command writes the pair at the head of the dict, before
/// every other key. If the dict holds the pair at another position, the
/// commit gets a different checksum for the same tree.
fn insert_bootable(&mut self, kernel_version: &str) -> &mut Self;
}
impl BootableMetadata for DictBuilder {
fn insert_bootable(&mut self, kernel_version: &str) -> &mut Self {
self.insert_str(LINUX_KEY, kernel_version)
.insert_bool(BOOTABLE_KEY, true)
}
}
#[cfg(test)]
mod tests {
use super::*;
use ostrya_core::{Type, Value, to_bytes};
/// Checks that the trait writes the two keys in order, with the types of
/// the format. The bytes match a pair built by hand.
#[test]
fn writes_the_pair_in_order() {
let mut builder = DictBuilder::new();
builder.insert_bootable("6.1.0-test");
let dict = builder.build();
let hand = Value::Array(vec![
Value::Tuple(vec![
Value::Str(LINUX_KEY.to_owned()),
Value::variant(Type::Str, Value::Str("6.1.0-test".to_owned())),
]),
Value::Tuple(vec![
Value::Str(BOOTABLE_KEY.to_owned()),
Value::variant(Type::Bool, Value::Bool(true)),
]),
]);
assert_eq!(dict, hand);
let ty = Type::parse("a{sv}").unwrap();
assert_eq!(to_bytes(&ty, &dict).unwrap(), to_bytes(&ty, &hand).unwrap());
}
/// Checks that the pair goes after the keys that the builder holds.
#[test]
fn appends_after_the_keys_already_inserted() {
let mut builder = DictBuilder::new();
builder
.insert_str("first", "x")
.insert_bootable("6.1.0-test");
let dict = builder.build();
let keys: Vec<&str> = dict
.as_array()
.unwrap()
.iter()
.map(|entry| entry.as_tuple().unwrap()[0].as_str().unwrap())
.collect();
assert_eq!(keys, ["first", LINUX_KEY, BOOTABLE_KEY]);
}
}