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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! The export of commits from a repository as a one-way stream.
use std::collections::HashSet;
use futures_io::AsyncWrite;
use ostrya_core::{Checksum, Commit, ObjectName, ObjectType, Ref};
use super::invalid;
use super::negotiate::{Commits, Local, check_named_collection, check_ref_binding, link_of};
use super::source::RepoSource;
use crate::error::Result;
use crate::lock::LockKind;
use crate::read::CommitState;
use crate::related::{self, RelatedRule};
use crate::remote::{Compression, Expected, PushStats, RefUpdate, SessionOptions};
use crate::repo::Repo;
/// The options of [`Repo::export_stream`].
///
/// The struct is not `#[non_exhaustive]`. A caller can build it with
/// `..Default::default()`.
#[derive(Debug, Clone, Default)]
pub struct ExportStreamOptions {
/// The ref updates that the `Commit` frame of the stream carries.
///
/// Each update expects [`Absent`](Expected::Absent) or
/// [`Any`](Expected::Any) and sets a new commit. A name is `NAME`,
/// `@ID:NAME`, or `REMOTE:NAME`. The new commits are the commits of the
/// stream. The stream also carries the full set of attachments of the
/// new commits, with no ref. [`Repo::export_updates`] returns these updates for a set
/// of refspecs.
pub updates: Vec<RefUpdate>,
/// The encoding of the content objects.
pub compression: Compression,
/// The switch that skips the check of `ostree.ref-binding` of each new
/// commit.
///
/// If `true`, the export does not check that the `ostree.ref-binding` of
/// the commit of each update holds the name part of its ref. A receiver
/// whose rules do not skip the check refuses such a stream. The switch
/// skips no check of `ostree.collection-binding`.
pub disable_verify_bindings: bool,
}
/// Methods that export commits as a one-way stream.
impl Repo {
/// Writes the commits that `opts.updates` name to `output` as a one-way stream.
///
/// A one-way stream goes over a channel that carries data in one
/// direction. On the other side, `Repo::receive_stream` or
/// `Repo::receive_stream_to_commit` of the `receive` feature reads it.
/// The stream ends with the `Commit` frame, which carries the updates of
/// `opts.updates`. [`export_updates`](Repo::export_updates) returns the
/// updates for a set of refspecs.
///
/// The call returns the [`PushStats`] of the stream. The statistics count
/// each object of the stream as offered and as needed.
///
/// # Stream content
///
/// The stream carries each new commit once, then each attachment in the
/// full set of attachments of the new commits once. With each commit, it carries each object that the
/// tree of the commit reaches. It also carries the stored `.commitmeta`
/// bytes of the commit with no parse, so the receiver refuses a
/// `.commitmeta` that is not an `a{sv}` dict. The stream carries no parent
/// commit.
///
/// The call does no negotiation, so it sends each object, also the
/// objects that the receiver holds.
///
/// If the repository is in `archive` mode and `opts.compression` is
/// [`Deflate`](Compression::Deflate), the stream carries each file object
/// in `deflate`. The stream copies the bytes of the stored `.filez` file
/// of the object.
///
/// # Attachments
///
/// Field 2 of a commit names the attachments of the commit.
/// [`read_commit_relations`](Repo::read_commit_relations) reads them. The
/// stream carries the full set of attachments of each new commit: each
/// attachment of the commit, and each attachment of an attachment of the
/// full set, at each depth.
///
/// - The call reads the full set one level at a time, until a level has
/// no new attachment. It reads the commit objects alone, and each
/// commit one time, also if several commits name it.
/// - The call sends each attachment at depth 0, at each level, so an
/// attachment entry adds no parent to the stream. The stream carries an
/// attachment one time, also if several commits name it, or if it is
/// also a new commit.
/// - The `Commit` frame holds no ref update for an attachment.
/// - For each attachment, the stream carries the commit object, the
/// objects of its tree, and its detached metadata. An object that a new
/// commit and an attachment share goes in the stream one time.
/// - The call does no negotiation, so it sends an attachment that the
/// receiver holds. The receiver accepts it, and drops its
/// `.commitmeta`: a held attachment gets no merge of detached metadata
/// and no signature.
/// - A receiver that holds an attachment and marks it partial refuses the
/// stream with [`MissingObjects`](crate::remote::Error::MissingObjects)
/// and no object listed, also though the stream carries the whole tree
/// of the attachment. To complete the attachment at the receiver, send
/// it as the new commit of an update, in this stream or in an earlier
/// one. The receiver then removes its partial marker.
/// - The call makes no ref-binding check on an attachment.
/// - The call does not send a supplement: a commit that the detached
/// metadata key `ostrya.related` names. The key goes with the detached
/// metadata of its commit.
///
/// These cases refuse the export before the call writes the first byte:
///
/// - An entry of field 2 of a new commit, or of an attachment at each
/// level, has an empty name or a checksum that is not 32 bytes.
/// - The repository does not hold an attachment, or marks it partial, at
/// each level.
///
/// # Lock
///
/// After the checks of `opts.updates`, the call takes the repository lock
/// as [`Shared`](LockKind::Shared). It holds the lock to the end of
/// the call. [`LockKind`] describes the repository lock.
///
/// # Output
///
/// The call writes `output` as
/// [`export_stream`](crate::remote::session::export_stream) of the session
/// states. It does not close `output`. The caller closes `output` to end
/// the stream.
///
/// # Missing file objects
///
/// The call does not check before the stream that each file object
/// exists. A missing file object or a failed read of the repository ends
/// the stream inside the object that the call was to send. The call
/// writes the abandon marker and `Abort`, and returns the error. The
/// receiver then returns [`Aborted`](crate::remote::Error::Aborted).
///
/// # Errors
///
/// Before the call writes the first byte to `output`, it returns:
///
/// - [`Error::Remote`](crate::Error::Remote) with
/// [`InvalidInput`](crate::remote::Error::InvalidInput) if `updates` is
/// empty, or names a ref two times.
/// - [`Error::Remote`](crate::Error::Remote) with
/// [`InvalidInput`](crate::remote::Error::InvalidInput) if an update
/// expects [`Expected::Commit`], or has no new commit.
/// - [`Error::InvalidRefspec`](crate::Error::InvalidRefspec) if a ref name
/// is not `NAME`, `@ID:NAME`, or `REMOTE:NAME` under the ref-name rule
/// of [`Ref`].
/// - [`Error::InvalidRefspec`](crate::Error::InvalidRefspec) if an update
/// writes a local ref name of 64 lowercase hex characters. A revision
/// reads such a name as a commit checksum. A delete of such a name gives
/// the error of an update with no new commit.
/// - [`Error::LockTimeout`](crate::Error::LockTimeout) if the wait for the
/// lock passes `[core] lock-timeout-secs`.
/// - [`Error::InvalidFormat`](crate::Error::InvalidFormat) if
/// `[core] lock-timeout-secs` is less than `-1`.
/// - [`Error::Core`](crate::Error::Core) if `[core] locking` is not a
/// boolean or `[core] lock-timeout-secs` is not an integer.
/// - [`Error::ObjectNotFound`](crate::Error::ObjectNotFound) if the
/// repository does not hold a new commit, or a dirtree or a dirmeta that
/// the tree of a sent commit reaches.
/// - [`Error::InvalidRelations`](crate::Error::InvalidRelations) if an entry
/// of field 2 of a new commit or of an attachment has an empty name or a
/// checksum that is not 32 bytes, or if the repository does not hold an
/// attachment of the full set or marks it partial. For an absent or a
/// partial attachment, the error names the commit that first names the
/// attachment.
/// - [`Error::Core`](crate::Error::Core) if a new commit or a dirtree
/// object does not parse. For the commit object of an attachment, the
/// call decodes the tuple frame, the parent and root checksums, and
/// field 2 alone, and gives this error if one of them does not decode.
/// - [`Error::Remote`](crate::Error::Remote) with
/// [`InvalidInput`](crate::remote::Error::InvalidInput) if the repository
/// marks a new commit partial.
/// - [`Error::Remote`](crate::Error::Remote) with
/// [`BindingMismatch`](crate::remote::Error::BindingMismatch) if the
/// `ostree.ref-binding` list of a commit does not hold the name part of
/// its ref, unless
/// [`disable_verify_bindings`](ExportStreamOptions::disable_verify_bindings)
/// is set. The check leaves out the `@ID:` part of a collection ref and
/// the `REMOTE:` part of a remote ref. A commit with no
/// `ostree.ref-binding` passes, and a commit with an empty list fails.
/// - [`Error::Remote`](crate::Error::Remote) with
/// [`BindingMismatch`](crate::remote::Error::BindingMismatch) if the
/// commit of a ref `@X:NAME` has no `ostree.collection-binding`, or is
/// bound to a collection other than `X`. The export does not know the
/// collection id of the receiver, so it refuses a commit with no binding
/// on each ref `@X:NAME`, also when `X` is the collection id of the
/// receiver. It leaves the collection binding of the commit of a local
/// ref and of a remote ref to the receiver. The switch
/// `disable_verify_bindings` does not skip this check.
/// - [`Error::Remote`](crate::Error::Remote) with
/// [`InvalidInput`](crate::remote::Error::InvalidInput) if
/// `opts.compression` has a level outside 1 through 9.
/// - [`Error::Remote`](crate::Error::Remote) with
/// [`InvalidInput`](crate::remote::Error::InvalidInput) if the `Hello`
/// frame or the `Commit` frame is more than 1 MiB.
/// - [`Error::Io`](crate::Error::Io) if the open or the lock of the lock
/// file fails.
/// - [`Error::Io`](crate::Error::Io) if a read of a commit or of an
/// attachment, of its `.commitpartial` marker, or of a dirtree fails, or
/// if a lookup of a dirmeta fails.
/// - [`Error::InvalidFormat`](crate::Error::InvalidFormat) if a new
/// commit, an attachment with no partial marker, or a dirtree is not a
/// regular file, is larger than
/// [`MAX_METADATA_SIZE`](crate::MAX_METADATA_SIZE), or has a symlink on
/// its path.
///
/// After the first byte, the call returns:
///
/// - [`Error::Remote`](crate::Error::Remote) with
/// [`Source`](crate::remote::Error::Source) if the read of an object or
/// of detached metadata fails, also for a missing file object. It holds
/// the error of the repository.
/// - [`Error::Remote`](crate::Error::Remote) with
/// [`InvalidInput`](crate::remote::Error::InvalidInput) if the data of an
/// object cannot go in the stream, as
/// [`export_stream`](crate::remote::session::export_stream) of the session
/// states.
/// - [`Error::Remote`](crate::Error::Remote) with
/// [`Io`](crate::remote::Error::Io) if a write to `output` or a flush of
/// `output` fails.
pub async fn export_stream<W>(&self, output: W, opts: ExportStreamOptions) -> Result<PushStats>
where
W: AsyncWrite + Unpin + Send,
{
let updates = &opts.updates;
if updates.is_empty() {
return Err(invalid("a commit needs at least one ref update"));
}
let mut named = HashSet::new();
let mut refs = Vec::with_capacity(updates.len());
for u in updates {
let r: Ref = u.name.parse()?;
if u.new.is_some()
&& let Ref::Local { name } = &r
&& ostrya_core::is_checksum_shaped(name)
{
return Err(crate::Error::InvalidRefspec(u.name.clone()));
}
refs.push(r);
if !named.insert(u.name.as_str()) {
return Err(invalid(format!("ref '{}' is updated twice", u.name)));
}
if let Expected::Commit(_) = u.expected {
return Err(invalid(format!(
"the update of '{}' expects a commit, which a one-way stream cannot state",
u.name
)));
}
if u.new.is_none() {
return Err(invalid(format!(
"the update of '{}' deletes the ref, which a one-way stream cannot do",
u.name
)));
}
}
drop(named);
let _lock = self.lock_repo(LockKind::Shared).await?;
// Each new commit once, in the order of the updates.
let mut targets: Vec<Checksum> = Vec::new();
let mut loaded: Vec<Commit> = Vec::new();
let mut commits = Commits::default();
for (u, r) in updates.iter().zip(&refs) {
let checksum = u.new.expect("a delete is refused");
let position = match targets.iter().position(|c| *c == checksum) {
Some(position) => position,
None => {
let (mut commit, state) = self.load_commit(&checksum).await?;
if state == CommitState::Partial {
return Err(invalid(format!(
"commit {checksum} for '{}' is marked partial in the local \
repository",
u.name
)));
}
commits
.local
.insert(checksum, Local::Held(link_of(&commit)));
let related = std::mem::take(&mut commit.related);
commits.record(checksum, related::checked_attachments(&checksum, related));
targets.push(checksum);
loaded.push(commit);
loaded.len() - 1
}
};
let commit = &loaded[position];
if !opts.disable_verify_bindings {
check_ref_binding(&checksum, commit, r)?;
}
// The export does not know the collection id of the receiver, so
// a commit with no binding fails each ref of a collection.
check_named_collection(&checksum, commit.collection_binding(), r, true)?;
}
drop(loaded);
drop(refs);
// The format of field 2 of each new commit, then the full set of
// attachments, before the first byte.
commits.check_fields(&targets)?;
commits.check_attachments(self, &targets).await?;
// The new commits, then each attachment of the full set once.
let mut sent = targets.clone();
let mut known: HashSet<Checksum> = targets.iter().copied().collect();
sent.extend(commits.full_set(&targets, &mut known));
drop(known);
let mut names: Vec<ObjectName> = sent
.iter()
.map(|c| ObjectName::new(*c, ObjectType::Commit))
.collect();
let mut seen = HashSet::new();
for commit in &sent {
let Local::Held(link) = commits.local[commit] else {
unreachable!("a sent commit is held");
};
self.collect_tree_strict(link.root_dirtree, link.root_dirmeta, &mut seen, &mut names)
.await?;
}
drop(seen);
drop(commits);
let source = RepoSource::new(self.clone(), RelatedRule::default());
Ok(crate::remote::session::export_stream(
output,
&source,
&names,
&sent,
updates,
opts.compression,
SessionOptions::default(),
)
.await?)
}
}
/// Checks at compile time that the options are `Send + Sync` and that the
/// futures of an export and of its updates are `Send`.
const _: fn() = || {
fn assert_send<T: Send>(_: T) {}
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<ExportStreamOptions>();
let _ = |repo: &Repo| {
assert_send(repo.export_stream(futures_lite::io::sink(), ExportStreamOptions::default()))
};
let _ = |repo: &Repo, refspecs: &[String]| assert_send(repo.export_updates(refspecs, None));
};