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
//! The metadata reading path: loading objects, commits, and trees, and
//! resolving commit state.
//!
//! Metadata objects (commit, dirtree, dirmeta, detached commit metadata) are
//! small and bounded, so they load whole into memory and parse through the
//! `ostrya-core` object model. File content objects have their own path in
//! [`crate::file`], which streams the payload. Every entry point is `async fn`
//! and offloads its syscalls to the blocking pool; a missing object surfaces as
//! [`Error::ObjectNotFound`].
use ostrya_core::{
Checksum, Commit, DirMeta, DirTree, ObjectType, Type, Value, from_bytes, loose_path,
};
use crate::error::{Error, Result};
use crate::object::{self, MAX_METADATA_SIZE};
use crate::repo::Repo;
/// The totals a commit's `ostree.sizes` metadata states, together with the part
/// of them whose objects are absent from the local store.
///
/// The metadata key is written by a commit that asked for it, so a commit
/// without the key has no sizes to report and
/// [`commit_sizes`](Repo::commit_sizes) yields `None`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CommitSizes {
/// On-disk (compressed) bytes over every recorded object.
pub compressed_total: u64,
/// On-disk bytes over the recorded objects absent locally.
pub compressed_needed: u64,
/// Uncompressed bytes over every recorded object.
pub unpacked_total: u64,
/// Uncompressed bytes over the recorded objects absent locally.
pub unpacked_needed: u64,
/// How many objects the metadata records.
pub objects_total: u64,
/// How many of the recorded objects are absent locally.
pub objects_needed: u64,
}
/// The completeness state of a commit in the local store.
///
/// A commit is [`Partial`](CommitState::Partial) while a
/// `state/<checksum>.commitpartial` marker is present, which a pull writes
/// before the commit's objects are all local and removes once the commit is
/// complete.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommitState {
/// The commit and its reachable objects are fully present.
Normal,
/// A `.commitpartial` marker is present: the commit may be incomplete.
Partial,
}
impl Repo {
/// Load the raw serialized bytes of a metadata object. Views borrow this
/// buffer. Intended for metadata objects, whose size the format caps.
pub async fn load_object_bytes(&self, ty: ObjectType, checksum: &Checksum) -> Result<Vec<u8>> {
let path = loose_path(checksum, ty, self.mode());
let repo = self.clone();
let key = *checksum;
let res = ostrya_rt::unblock(move || {
object::read_meta_object(repo.objects_fd(), &path, MAX_METADATA_SIZE)
})
.await;
match res {
Ok(bytes) => Ok(bytes),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
Err(Error::ObjectNotFound { checksum: key, ty })
}
Err(e) => Err(Error::Io(e)),
}
}
/// Load a metadata object as a dynamic [`Value`] tree, parsed against the
/// object type's GVariant signature. Supports the metadata object types;
/// other types return [`Error::Unsupported`].
pub async fn load_variant(&self, ty: ObjectType, checksum: &Checksum) -> Result<Value> {
// The metadata object GVariant type strings, from `format-reference.md`.
let signature = match ty {
ObjectType::DirTree => "(a(say)a(sayay))",
ObjectType::DirMeta => "(uuua(ayay))",
ObjectType::Commit => "(a{sv}aya(say)sstayay)",
ObjectType::CommitMeta => "a{sv}",
other => {
return Err(Error::Unsupported(format!(
"load_variant does not support {other:?} objects"
)));
}
};
let bytes = self.load_object_bytes(ty, checksum).await?;
let ty = Type::parse(signature).map_err(ostrya_core::Error::from)?;
Ok(from_bytes(&ty, &bytes).map_err(ostrya_core::Error::from)?)
}
/// Load and parse a commit object together with its completeness state.
pub async fn load_commit(&self, checksum: &Checksum) -> Result<(Commit, CommitState)> {
let bytes = self.load_object_bytes(ObjectType::Commit, checksum).await?;
let commit = Commit::parse(&bytes)?;
let state = self.commit_state(checksum).await?;
Ok((commit, state))
}
/// Load and parse a dirtree object.
pub async fn load_dirtree(&self, checksum: &Checksum) -> Result<DirTree> {
let bytes = self
.load_object_bytes(ObjectType::DirTree, checksum)
.await?;
Ok(DirTree::parse(&bytes)?)
}
/// Load and parse a dirmeta object.
pub async fn load_dirmeta(&self, checksum: &Checksum) -> Result<DirMeta> {
let bytes = self
.load_object_bytes(ObjectType::DirMeta, checksum)
.await?;
Ok(DirMeta::parse(&bytes)?)
}
/// Total the `ostree.sizes` metadata of a commit, or `None` when the commit
/// carries no such key.
///
/// An entry whose object is absent from the local store counts toward the
/// "needed" totals. An entry that records no object type is read as a file
/// object, the only type the key covers on the commits that omit it.
pub async fn commit_sizes(&self, checksum: &Checksum) -> Result<Option<CommitSizes>> {
let (commit, _) = self.load_commit(checksum).await?;
let Some(value) = commit.metadata.dict_get("ostree.sizes") else {
return Ok(None);
};
let packed = match value.as_variant() {
Some((_, inner)) => inner,
None => value,
};
let entries = packed.as_array().ok_or_else(|| {
Error::InvalidFormat("ostree.sizes is not an array of packed entries".into())
})?;
let mut sizes = CommitSizes::default();
for packed in entries {
let buf = packed.as_bytes().ok_or_else(|| {
Error::InvalidFormat("an ostree.sizes entry is not a byte array".into())
})?;
let entry = ostrya_core::sizes::unpack_entry(buf)?;
sizes.compressed_total += entry.compressed;
sizes.unpacked_total += entry.unpacked;
sizes.objects_total += 1;
let ty = entry.objtype.unwrap_or(ObjectType::File);
if !self.has_object(ty, &entry.checksum).await? {
sizes.compressed_needed += entry.compressed;
sizes.unpacked_needed += entry.unpacked;
sizes.objects_needed += 1;
}
}
Ok(Some(sizes))
}
/// The on-disk (compressed) size of a loose object, used to recover an
/// `ostree.sizes` record for an object that deduplicated against `objects/`.
pub(crate) async fn loose_object_size(
&self,
ty: ObjectType,
checksum: &Checksum,
) -> Result<u64> {
let path = loose_path(checksum, ty, self.mode());
let repo = self.clone();
let key = *checksum;
let res = ostrya_rt::unblock(move || object::object_size(repo.objects_fd(), &path)).await;
match res {
Ok(size) => Ok(size),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
Err(Error::ObjectNotFound { checksum: key, ty })
}
Err(e) => Err(Error::Io(e)),
}
}
/// Whether a loose object of the given type is present.
pub async fn has_object(&self, ty: ObjectType, checksum: &Checksum) -> Result<bool> {
let path = loose_path(checksum, ty, self.mode());
let repo = self.clone();
ostrya_rt::unblock(move || object::object_exists(repo.objects_fd(), &path)).await
}
/// The completeness state of a commit: [`Partial`](CommitState::Partial)
/// when a `.commitpartial` marker is present, else
/// [`Normal`](CommitState::Normal).
pub async fn commit_state(&self, checksum: &Checksum) -> Result<CommitState> {
let path = crate::pull::partial_path(checksum);
let repo = self.clone();
let partial =
ostrya_rt::unblock(move || object::object_exists(repo.repo_fd(), &path)).await?;
Ok(if partial {
CommitState::Partial
} else {
CommitState::Normal
})
}
}