use std::collections::{HashMap, HashSet};
use std::sync::{Arc, LazyLock};
use ostrya_core::{
Checksum, Commit, GvDecode, GvEncode, ObjectType, Type, Value, VariantBytes, ref_binding,
to_bytes, validate, write_array,
};
use super::finish::{
Checked, Named, check_ref_paths, missing_objects, protocol, publish, queue_ref, ref_denied,
report, sign_server_commit,
};
use super::hooks::ReceiveHooks;
use super::policy::SessionPolicy;
use super::session::{Failure, ReceiveReport};
use super::walk;
use crate::access::Namespace;
use crate::error::Error;
use crate::remote::proto::{CommitRequest, MAX_FRAME, Message, ServerCommit};
use crate::remote::{self, RefOutcome};
use crate::repo::Repo;
use crate::summary::OSTREE_METADATA_REF;
use crate::transaction::Transaction;
const REF_BINDING_KEY: &str = "ostree.ref-binding";
const COLLECTION_BINDING_KEY: &str = "ostree.collection-binding";
const COMPOSEFS_DIGEST_KEY: &str = "ostree.composefs.digest.v0";
const SIZES_KEY: &str = "ostree.sizes";
static VALUE_IN_COMMIT: LazyLock<Type> =
LazyLock::new(|| Type::parse("((v))").expect("valid signature"));
pub(super) async fn finish(
repo: &Repo,
policy: &dyn SessionPolicy,
hooks: Option<&dyn ReceiveHooks>,
txn: Transaction,
named: Named<'_>,
commit_meta: HashMap<Checksum, Vec<u8>>,
request: CommitRequest,
) -> Checked<ReceiveReport> {
let CommitRequest {
updates,
force,
server_commit,
} = request;
let Some(sc) = server_commit.map(|sc| *sc) else {
unreachable!("the commit of a session passes a request with a server commit")
};
let paths: Arc<[String]> = check_names(&named, &sc, updates.len(), force)?.into();
check_metadata(&sc)?;
check_reply_fits(&sc)?;
check_session_objects(&txn, &commit_meta)?;
if hooks.is_some() {
return Err(ref_denied(
"the session has the hooks of a host, which take no server commit".into(),
));
}
if named.is_local() && repo.config().collection_id().is_some() {
for name in refs(&sc) {
if name == OSTREE_METADATA_REF {
return Err(ref_denied(format!(
"ref '{name}' holds the collection anchor commit, which the server writes"
)));
}
}
}
let rule = policy.server_commit_rule(&sc.branch, &sc.tags)?;
let root = (sc.root_dirtree, sc.root_dirmeta);
let missing = walk::completeness(&txn, [root]).await?;
if missing.total > 0 {
return Err(missing_objects(
missing,
"the tree of the server commit reaches",
));
}
txn.sync_staged().await.map_err(Failure::Internal)?;
let held = repo.lock_update().await.map_err(Failure::Internal)?;
let anchor = policy.regenerates_summary() && repo.config().collection_id().is_some();
let (states, anchor_parent) = repo
.read_ref_states(Arc::clone(&paths), anchor.then_some(OSTREE_METADATA_REF))
.await
.map_err(Failure::Internal)?;
let written: Vec<(&str, &str, bool)> = refs(&sc)
.zip(paths.iter())
.map(|(name, path)| (name, path.as_str(), true))
.collect();
let current = check_ref_paths(&written, &states)?;
drop(written);
let tip = current[0];
if let Some(tip) = tip
&& sc.orphan
&& !rule.allow_orphan_over_branch
{
return Err(Failure::Wire(remote::Error::NonFastForward(format!(
"ref '{}' is at {tip}, and the policy does not allow a commit with no parent on it",
sc.branch
))));
}
for (tag, current) in sc.tags.iter().zip(¤t[1..]) {
if let Some(commit) = current {
return Err(Failure::Wire(remote::Error::RefMismatch {
message: format!(
"ref '{tag}' is at {commit}, and a tag of a server commit creates its ref"
),
name: tag.clone(),
current: Some(*commit),
}));
}
}
let collection_id = match named.namespace {
Some(Namespace::Collection(id)) => Some(id.as_str()),
_ => repo.config().collection_id(),
};
let ServerCommit {
branch,
tags,
bind_refs,
root_dirtree,
root_dirmeta,
subject,
body,
metadata,
timestamp,
orphan,
} = sc;
let bound: Vec<&str> = std::iter::once(branch.as_str())
.chain(tags.iter().map(String::as_str))
.chain(bind_refs.iter().map(String::as_str))
.collect();
let timestamp = match timestamp {
Some(timestamp) => timestamp,
None => server_clock().map_err(Failure::Internal)?,
};
let core = |e: ostrya_core::Error| Failure::Internal(e.into());
let metadata = server_commit_metadata(metadata, &bound, collection_id).map_err(core)?;
let bytes = Commit {
metadata: Value::Array(Vec::new()),
parent: if orphan { None } else { tip },
related: Vec::new(),
subject,
body,
timestamp,
root_dirtree,
root_dirmeta,
}
.serialize_with_metadata(&metadata)
.map_err(core)?;
drop(metadata);
let commit = txn
.write_metadata(ObjectType::Commit, None, &bytes)
.await
.map_err(Failure::Internal)?;
sign_server_commit(repo, &txn, commit, &bytes, rule.signers).await?;
drop(bytes);
let moves = tip != Some(commit);
if moves {
queue_ref(&txn, named.namespace, &branch, Some(&commit));
}
for tag in &tags {
queue_ref(&txn, named.namespace, tag, Some(&commit));
}
let changed = moves || !tags.is_empty();
let (stats, warnings) = Box::pin(publish(
repo,
policy,
txn,
held,
changed,
anchor_parent,
vec![commit],
))
.await?;
let refs = outcomes(branch, tags, tip, commit);
Ok(report(policy, named.namespace, refs, stats, warnings))
}
fn refs(sc: &ServerCommit) -> impl Iterator<Item = &str> {
std::iter::once(sc.branch.as_str()).chain(sc.tags.iter().map(String::as_str))
}
fn outcomes(
branch: String,
tags: Vec<String>,
tip: Option<Checksum>,
commit: Checksum,
) -> Vec<RefOutcome> {
std::iter::once(RefOutcome {
name: branch,
old: tip,
new: Some(commit),
})
.chain(tags.into_iter().map(|name| RefOutcome {
name,
old: None,
new: Some(commit),
}))
.collect()
}
fn name_failure(e: Error, what: &str, name: &str) -> Failure {
match e {
Error::InvalidRefspec(_) => Failure::Wire(remote::Error::InvalidRef(format!(
"invalid {what} '{name}'"
))),
other => Failure::Internal(other),
}
}
fn check_names(
named: &Named<'_>,
sc: &ServerCommit,
updates: usize,
force: bool,
) -> Checked<Vec<String>> {
for name in refs(sc) {
crate::validate_refspec(name).map_err(|e| name_failure(e, "ref name", name))?;
if ostrya_core::is_checksum_shaped(name) {
return Err(Failure::Wire(remote::Error::InvalidRef(format!(
"invalid ref name '{name}'"
))));
}
}
for name in &sc.bind_refs {
crate::validate_refspec(name).map_err(|e| name_failure(e, "bound name", name))?;
if name.contains(':') {
return Err(Failure::Wire(remote::Error::InvalidRef(format!(
"invalid bound name '{name}'"
))));
}
}
if updates > 0 {
return Err(protocol(format!(
"the Commit message holds a server commit and {updates} ref updates"
)));
}
if force {
return Err(protocol(
"the Commit message holds a server commit and force".into(),
));
}
let paths: HashMap<&str, &String> = named
.names
.iter()
.map(String::as_str)
.zip(named.paths)
.collect();
let mut seen = HashSet::with_capacity(sc.tags.len() + 1);
let mut found = Vec::with_capacity(sc.tags.len() + 1);
for name in refs(sc) {
let Some(&path) = paths.get(name) else {
return Err(protocol(format!("ref '{name}' was not named in Hello")));
};
if !seen.insert(name) {
return Err(protocol(format!("ref '{name}' is written more than once")));
}
found.push(path.clone());
}
Ok(found)
}
fn check_metadata(sc: &ServerCommit) -> Checked<()> {
for (key, value) in &sc.metadata {
validate(&VALUE_IN_COMMIT, value).map_err(|e| {
protocol(format!(
"a commit cannot hold the metadata value under '{key}': {e}"
))
})?;
}
Ok(())
}
fn check_reply_fits(sc: &ServerCommit) -> Checked<()> {
let any_commit = Checksum::from_bytes([0; 32]);
let longest = Message::CommitReply(outcomes(
sc.branch.clone(),
sc.tags.clone(),
Some(any_commit),
any_commit,
));
let body = longest.encode_body().map_err(Failure::Wire)?;
let len = body.len() as u64 + 1;
if len > u64::from(MAX_FRAME) {
return Err(Failure::Wire(remote::Error::LimitExceeded(format!(
"the reply to the server commit with {} tags can need a frame of {len} bytes, over \
the limit {MAX_FRAME}",
sc.tags.len()
))));
}
Ok(())
}
fn check_session_objects(
txn: &Transaction,
commit_meta: &HashMap<Checksum, Vec<u8>>,
) -> Checked<()> {
if let Some(commit) = commit_meta.keys().min() {
return Err(protocol(format!(
"detached metadata for commit {commit} in a session with a server commit"
)));
}
if let Some(commit) = txn.staged_of_type(ObjectType::Commit).into_iter().min() {
return Err(protocol(format!(
"commit {commit} is staged in a session with a server commit"
)));
}
Ok(())
}
fn server_clock() -> crate::error::Result<u64> {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|now| now.as_secs())
.map_err(|_| Error::InvalidFormat("the system clock is before the Unix epoch".into()))
}
fn server_commit_metadata(
client: Vec<(String, Vec<u8>)>,
refs: &[&str],
collection_id: Option<&str>,
) -> ostrya_core::Result<Vec<u8>> {
let refs = to_bytes(&Type::Variant, &ref_binding(refs))?;
let collection = collection_id
.map(|id| to_bytes(&Type::Variant, &Value::variant(Type::Str, id.into())))
.transpose()?;
let mut entries = Vec::with_capacity(client.len() + 2);
let mut digests = Vec::new();
let mut sizes = Vec::new();
for (key, value) in &client {
let entry = (key.as_str(), value.as_slice());
match entry.0 {
REF_BINDING_KEY | COLLECTION_BINDING_KEY => {}
COMPOSEFS_DIGEST_KEY => digests.push(entry),
SIZES_KEY => sizes.push(entry),
_ => entries.push(entry),
}
}
entries.push((REF_BINDING_KEY, &refs));
if let Some(collection) = &collection {
entries.push((COLLECTION_BINDING_KEY, collection));
}
entries.extend(digests);
entries.extend(sizes);
let mut dict = Vec::new();
write_array(&mut dict, 8, false, entries.len(), |out, i| {
let (key, value) = entries[i];
(key, VariantBytes::decode(value)?).encode(out)
})?;
Ok(dict)
}
#[cfg(test)]
mod tests {
use super::*;
use ostrya_core::from_bytes;
fn text(value: &str) -> Value {
Value::variant(Type::Str, Value::Str(value.into()))
}
fn entry(key: &str, value: Value) -> (String, Vec<u8>) {
(key.into(), to_bytes(&Type::Variant, &value).unwrap())
}
fn parse_dict(dict: &[u8]) -> Value {
from_bytes(&Type::parse("a{sv}").unwrap(), dict).unwrap()
}
fn keys(dict: &Value) -> Vec<String> {
let Value::Array(items) = dict else {
panic!("the dict is an array")
};
items
.iter()
.map(|item| match item {
Value::Tuple(fields) => match &fields[0] {
Value::Str(key) => key.clone(),
other => panic!("a key {other:?}"),
},
other => panic!("an entry {other:?}"),
})
.collect()
}
fn with_metadata(metadata: Vec<(String, Vec<u8>)>) -> ServerCommit {
ServerCommit {
branch: "main".into(),
tags: Vec::new(),
bind_refs: Vec::new(),
root_dirtree: Checksum::from_bytes([1; 32]),
root_dirmeta: Checksum::from_bytes([2; 32]),
subject: String::new(),
body: String::new(),
metadata,
timestamp: None,
orphan: false,
}
}
#[test]
fn the_metadata_has_the_order_of_commit() {
let client = vec![
entry(SIZES_KEY, text("sizes")),
entry("b", text("1")),
entry(REF_BINDING_KEY, text("x")),
entry(COMPOSEFS_DIGEST_KEY, text("digest")),
entry("a", text("2")),
entry(COLLECTION_BINDING_KEY, text("y")),
entry("b", text("3")),
entry(REF_BINDING_KEY, text("z")),
];
let refs = ["main", "v1", "alias", "main"];
let bytes = server_commit_metadata(client, &refs, Some("org.a")).unwrap();
let dict = parse_dict(&bytes);
assert_eq!(
keys(&dict),
[
"b",
"a",
"b",
REF_BINDING_KEY,
COLLECTION_BINDING_KEY,
COMPOSEFS_DIGEST_KEY,
SIZES_KEY,
]
);
assert_eq!(dict.dict_get("b"), Some(&text("1")));
let tree = |key: &str, value: Value| (key.to_owned(), value);
let mut expected = ostrya_core::commit_metadata(
[
tree("b", text("1")),
tree("a", text("2")),
tree("b", text("3")),
],
Some(&refs),
Some("org.a"),
);
if let Value::Array(items) = &mut expected {
for (key, value) in [(COMPOSEFS_DIGEST_KEY, "digest"), (SIZES_KEY, "sizes")] {
items.push(Value::Tuple(vec![Value::Str(key.into()), text(value)]));
}
}
assert_eq!(
bytes,
to_bytes(&Type::parse("a{sv}").unwrap(), &expected).unwrap()
);
let commit = Commit {
metadata: dict,
parent: None,
related: Vec::new(),
subject: String::new(),
body: String::new(),
timestamp: 0,
root_dirtree: Checksum::from_bytes([1; 32]),
root_dirmeta: Checksum::from_bytes([2; 32]),
};
let commit = Commit::parse(&commit.serialize().unwrap()).unwrap();
assert_eq!(commit.ref_bindings(), ["alias", "main", "main", "v1"]);
assert_eq!(commit.collection_binding(), Some("org.a"));
}
#[test]
fn no_collection_id_writes_no_collection_binding() {
let bytes = server_commit_metadata(vec![entry("k", text("v"))], &["main"], None).unwrap();
assert_eq!(keys(&parse_dict(&bytes)), ["k", REF_BINDING_KEY]);
}
#[test]
fn the_metadata_check_follows_the_depth_of_a_commit() {
for levels in 123..=127 {
let ty = (0..levels).fold(Type::Byte, |ty, _| Type::maybe(ty));
let value = (0..levels).fold(Value::Byte(1), |value, _| {
Value::Maybe(Some(Box::new(value)))
});
let value = Value::variant(ty, value);
let commit = Commit {
metadata: Value::Array(vec![Value::Tuple(vec![
Value::Str("deep".into()),
value.clone(),
])]),
parent: None,
related: Vec::new(),
subject: String::new(),
body: String::new(),
timestamp: 0,
root_dirtree: Checksum::from_bytes([1; 32]),
root_dirmeta: Checksum::from_bytes([2; 32]),
};
let sc = with_metadata(vec![entry("deep", value)]);
let checked = check_metadata(&sc);
assert_eq!(checked.is_ok(), levels <= 125, "{levels} levels");
let bytes = commit.serialize();
assert_eq!(bytes.is_ok(), levels <= 125, "{levels} levels");
if let Ok(bytes) = bytes {
assert_eq!(Commit::parse(&bytes).unwrap(), commit, "{levels} levels");
}
if levels > 125 {
assert!(
matches!(checked, Err(Failure::Wire(remote::Error::Protocol(_)))),
"{levels} levels"
);
}
}
}
#[test]
fn the_reply_bound_of_a_server_commit_is_the_encoded_size() {
let commit = |count: usize| ServerCommit {
tags: (0..count).map(|i| format!("tag-{i:015}")).collect(),
..with_metadata(Vec::new())
};
let encoded = |count: usize| {
let sc = commit(count);
let any = Checksum::from_bytes([3; 32]);
Message::CommitReply(outcomes(sc.branch, sc.tags, Some(any), any))
.encode_body()
.unwrap()
.len()
+ 1
};
let (mut low, mut high) = (0, 30_000);
assert!(check_reply_fits(&commit(low)).is_ok());
assert!(check_reply_fits(&commit(high)).is_err());
while high - low > 1 {
let mid = (low + high) / 2;
if check_reply_fits(&commit(mid)).is_ok() {
low = mid;
} else {
high = mid;
}
}
assert!(encoded(low) <= MAX_FRAME as usize);
assert!(encoded(high) > MAX_FRAME as usize);
match check_reply_fits(&commit(high)) {
Err(Failure::Wire(remote::Error::LimitExceeded(m))) => {
assert!(m.contains(&format!("{high} tags")), "{m}")
}
_ => panic!("expected limit-exceeded"),
}
}
}