ostrya_composefs/tree.rs
1//! The tree model that the image writer reads: a [`Directory`] of named
2//! [`Node`]s, each with its [`Metadata`].
3
4use std::collections::BTreeMap;
5
6/// The logical metadata of a node.
7#[derive(Clone, Debug, Default)]
8pub struct Metadata {
9 /// The full `st_mode` of the node.
10 ///
11 /// The writer ignores the file-type bits and takes the type from the
12 /// [`Node`] variant. It keeps the bits of `mode & 0o7777`: the permission
13 /// bits and the setuid, setgid, and sticky bits.
14 pub mode: u32,
15 /// The user id of the owner.
16 pub uid: u32,
17 /// The group id of the owner.
18 pub gid: u32,
19 /// The modification time as `(seconds, nanoseconds)`.
20 pub mtime: (u64, u32),
21 /// The extended attributes as `(name, value)` pairs.
22 ///
23 /// The caller gives the logical names and values. The writer makes their
24 /// EROFS encoding (prefix indexing, ordering, sharing, and the name
25 /// filter). It escapes each `trusted.overlay.X` name as
26 /// `trusted.overlay.overlay.X`. The escape adds 8 bytes to the name suffix.
27 ///
28 /// # Limits
29 ///
30 /// Each limit is the range of an EROFS length or count field:
31 ///
32 /// - A value is at most 65535 bytes.
33 /// - A name suffix is at most 255 bytes. The suffix is the part of the name
34 /// after its EROFS prefix, and the limit applies after the escape.
35 /// - The xattr area of one inode is at most 262148 bytes. The number of
36 /// attributes has no limit of its own.
37 ///
38 /// If a name, a value, or an area is larger than its limit, the writer
39 /// panics. The field in the image cannot state a larger size.
40 ///
41 /// The writer shares an entry that repeats across inodes. One inode
42 /// references at most 128 shared entries, the limit that an observation of
43 /// the `ostree` command shows. If an inode has more repeated entries, it
44 /// references the first 128 in name order and keeps the others inline.
45 pub xattrs: Vec<(Vec<u8>, Vec<u8>)>,
46}
47
48/// A node in the tree.
49#[derive(Clone, Debug)]
50pub enum Node {
51 /// A directory.
52 Directory(Directory),
53 /// A symbolic link.
54 Symlink(Symlink),
55 /// A regular file.
56 Regular(Regular),
57}
58
59/// A directory and its named children.
60#[derive(Clone, Debug, Default)]
61pub struct Directory {
62 /// The logical metadata of the directory inode.
63 pub meta: Metadata,
64 /// The children, keyed by name and sorted by name.
65 ///
66 /// A name is raw bytes and one path component:
67 ///
68 /// - It is not empty.
69 /// - It is not `.` or `..`.
70 /// - It holds no `/`.
71 /// - It is at most 255 bytes, the limit that an observation of the
72 /// `ostree` command shows.
73 ///
74 /// If a name breaks one of these rules, [`build_image`](crate::build_image)
75 /// and [`write_image_to`](crate::write_image_to) return
76 /// [`Error::Unsupported`](crate::Error::Unsupported).
77 pub children: BTreeMap<Vec<u8>, Node>,
78}
79
80/// A symbolic link, with its target inline in the image.
81#[derive(Clone, Debug)]
82pub struct Symlink {
83 /// The logical metadata of the symlink inode.
84 pub meta: Metadata,
85 /// The bytes of the link target.
86 ///
87 /// The target shares one 4096-byte block with the inode header and the
88 /// xattr area of the inode. The three together must be less than 4096
89 /// bytes, so the target is at most 4095 bytes less the header and the area.
90 ///
91 /// A header is 32 bytes for a compact inode and 64 bytes for an extended
92 /// inode. If the mtime of the inode is not the earliest mtime in the image,
93 /// the inode is extended. If the uid or the gid is more than 65535, the
94 /// inode is also extended. If the symlink has a compact inode and no
95 /// xattrs, the target is at most 4063 bytes.
96 ///
97 /// If the target is longer than its limit, the writer returns
98 /// [`Error::Unsupported`](crate::Error::Unsupported).
99 pub target: Vec<u8>,
100}
101
102/// A regular file.
103#[derive(Clone, Debug)]
104pub struct Regular {
105 /// The logical metadata of the file inode.
106 pub meta: Metadata,
107 /// The content of the file: empty, or backed by a loose object.
108 pub content: Content,
109}
110
111/// The content of a [`Regular`] file in the image.
112///
113/// The image holds no file content. A backed file names its loose object
114/// through the `trusted.overlay.redirect` and `trusted.overlay.metacopy`
115/// xattrs.
116#[derive(Clone, Debug)]
117pub enum Content {
118 /// An empty file, with no backing object.
119 Empty,
120 /// A file backed by a loose object.
121 Backed {
122 /// The logical (uncompressed) size of the file in bytes.
123 size: u64,
124 /// The overlay redirect target, an absolute path such as
125 /// `/cf/ffd5....file`.
126 redirect: String,
127 /// The fs-verity digest of the backing object, or `None`.
128 ///
129 /// The writer puts the digest in the value of the
130 /// `trusted.overlay.metacopy` xattr. If the digest is `None`, the
131 /// xattr has an empty value.
132 verity: Option<[u8; 32]>,
133 },
134}
135
136impl Directory {
137 /// Creates an empty directory with the given metadata.
138 pub fn new(meta: Metadata) -> Self {
139 Self {
140 meta,
141 children: BTreeMap::new(),
142 }
143 }
144
145 /// Inserts a child by name, or replaces the child that has the same name.
146 pub fn insert(&mut self, name: impl Into<Vec<u8>>, node: Node) {
147 self.children.insert(name.into(), node);
148 }
149}