ostrya 0.11.1

Pure-Rust async reimplementation of the ostree repository library
Documentation
//! The one-way stream of a push: the messages of a session in one direction,
//! with no reply. [`Repo::receive_stream`] and
//! [`Repo::receive_stream_to_commit`] read it.

use std::pin::Pin;

use futures_io::{AsyncBufRead, AsyncRead};
use futures_lite::io::BufReader;

use super::StreamPolicy;
use super::core::SessionCore;
use super::session::{Failure, ReceiveReport, STREAM_BUFFER, next, out_of_order};
use crate::error::{Error, Result};
use crate::remote::proto::{CommitRequest, FrameReader, Message};
use crate::remote::{self, Expected};
use crate::repo::Repo;

/// Methods that receive a one-way stream.
impl Repo {
    /// Reads one one-way stream from `input` into one transaction.
    ///
    /// A one-way stream holds the messages of a push session in one direction.
    /// The call sends no message, and returns the result in place of a reply.
    /// The stream holds these messages, in this order:
    ///
    /// 1. One `Hello` with `one-way` true.
    /// 2. Zero or more object streams, each one closed by `ObjectsEnd`.
    /// 3. One `Commit`.
    /// 4. The end of `input`.
    ///
    /// [`Repo::receive_stream_to_commit`] reads the same stream, and stops at
    /// `Commit`.
    ///
    /// # Hello
    ///
    /// The frame limit and the chunk limit are 1 MiB
    /// ([`MAX_FRAME`](crate::remote::proto::MAX_FRAME)). A `bare-split-xattrs`
    /// repository is `mode-refused`. A `bare` repository is `mode-refused`
    /// unless the process runs as root. The call accepts a repository with
    /// `[core] locking=false`.
    ///
    /// The size of a `HelloReply` does not bound the ref names of `Hello`,
    /// because the stream gets no reply.
    ///
    /// # Objects
    ///
    /// The call stages the objects as [`Repo::receive`] does. Before the
    /// stage, it verifies the checksum of each object and checks the content
    /// rules of the repository mode and of `policy`.
    ///
    /// # Locks
    ///
    /// The session transaction holds the repository lock shared from `Hello`
    /// to the end. Under `[core] locking=false`, it holds no lock. The commit
    /// takes the update lock, which ignores `[core] locking`.
    ///
    /// # Commit
    ///
    /// `Commit` runs the checks and the steps of [`Repo::receive`], with five
    /// differences:
    ///
    /// - The rule of each update is the rule that
    ///   [`StreamPolicy::rule_for`](super::StreamPolicy::rule_for) selects for
    ///   the canonical ref of the update. An update that no rule covers, and
    ///   an update whose rule has `accept` false, are `ref-denied`. So a
    ///   remote ref and a ref `@ID:NAME` of a collection other than the
    ///   collection id of the repository need a pattern rule that matches
    ///   them. A delete of a ref that is present needs
    ///   `allow_delete`. An update that is not a fast-forward needs `force`
    ///   and `allow_non_fast_forward`. With
    ///   [`ReceiveVerify::Keys`](super::ReceiveVerify::Keys), each new commit
    ///   must pass the verification of the rule of each of its updates, and a
    ///   commit with no signature fails. A commit that no update names gets
    ///   no verification, a dependency included.
    /// - In a repository with no collection id, the check of
    ///   `ostree.collection-binding` does not run for a local ref and a remote
    ///   ref. A ref `@ID:NAME` keeps the check.
    /// - The call ignores the `signers` of each rule, `summary_signers`, and
    ///   `update_summary` of `policy`. As a result, the commit adds no server
    ///   signature, writes no anchor commit, and does not regenerate the
    ///   summary.
    /// - A ref update whose expected state is [`Expected::Commit`] is
    ///   `protocol`. A ref update with no new commit, and a server commit,
    ///   are also `protocol`.
    /// - The commit checks run only after `input` reaches its end. A byte
    ///   after `Commit` is `protocol`. [`Repo::receive_stream_to_commit`]
    ///   runs them at `Commit`, and starts no read of `input` after it.
    ///
    /// The reply limit of the two-way session also applies to the ref updates
    /// of `Commit`.
    ///
    /// # Errors
    ///
    /// A failure before the transaction commit aborts the transaction, and the
    /// repository does not change. A failure of the transaction commit can
    /// leave the detached metadata and some refs written. The call returns a
    /// wire code inside [`Error::Remote`]. It returns each other error as it is.
    ///
    /// - [`remote::Error::Protocol`] if the first message is not `Hello`, or if
    ///   `Hello` does not have `one-way` true.
    /// - [`remote::Error::Protocol`] if a message comes out of order. `Have` and
    ///   an `Abort` frame between two objects are out of order.
    /// - [`remote::Error::VersionUnsupported`] if `Hello` states another
    ///   protocol version.
    /// - [`remote::Error::ModeRefused`] if the repository mode is
    ///   `bare-split-xattrs`, or if it is `bare` and the process does not run
    ///   as root.
    /// - [`remote::Error::InvalidRef`] if `Hello` names a ref that is not valid.
    /// - [`remote::Error::Protocol`] if two names of `Hello` name one ref.
    /// - The errors of [`Repo::transaction`] if the session transaction cannot
    ///   open. These include [`Error::LockTimeout`] for the repository lock.
    /// - [`Error::Core`] or [`Error::InvalidFormat`] if `[core] fsync`,
    ///   `[core] per-object-fsync`, `[ex-integrity] fsverity`,
    ///   `[ex-ostrya] keep-detached-related`, or `[archive] zlib-level` is
    ///   malformed.
    /// - The errors of an object stream that [`Repo::receive`] lists:
    ///   `checksum-mismatch`, `mode-refused`, `limit-exceeded`, `protocol`,
    ///   and the wire code of the frame decoder.
    /// - [`remote::Error::Protocol`] if an update of `Commit` expects a commit
    ///   or has no new commit, if `Commit` holds a server commit, or if a byte
    ///   follows `Commit`.
    /// - At `Commit`, the errors of
    ///   [`ReceiveService::commit`](super::ReceiveService::commit), other than
    ///   the errors of steps in flight and of hooks. These include
    ///   [`Error::LockTimeout`] for the update lock.
    /// - [`remote::Error::Aborted`] inside [`Error::Remote`] if the sender
    ///   abandons an object: the abandon marker and then `Abort` inside the
    ///   object.
    /// - [`Error::Io`] of kind `UnexpectedEof` if `input` ends before
    ///   `Commit` is complete.
    /// - [`Error::Io`] if a read of `input` fails.
    /// - An I/O error from the file system, if a step of the transaction
    ///   fails.
    pub async fn receive_stream<R>(&self, input: R, policy: &StreamPolicy) -> Result<ReceiveReport>
    where
        R: AsyncRead + Unpin + Send,
    {
        let reader = FrameReader::new(BufReader::with_capacity(STREAM_BUFFER, input));
        // A failure drops the transaction here, which removes its staging
        // directory and releases the repository lock.
        run(self, policy, reader, AfterCommit::CheckEnd)
            .await
            .map_err(stream_error)
    }

    /// Reads one one-way stream from `input` into one transaction, and stops
    /// at `Commit`.
    ///
    /// The call reads the stream as [`Repo::receive_stream`] does, with one
    /// difference. It runs the commit checks when it has read the `Commit`
    /// frame, and starts no read of `input` after that frame. A byte after
    /// `Commit` is not `protocol`.
    ///
    /// The call reads `input` through a buffer, and a read of `input` returns
    /// the bytes that are ready. A read that completes the `Commit` frame can
    /// also get bytes after the frame. The call discards these bytes with no
    /// check, and a later reader of `input` does not get them.
    ///
    /// Use this call on a channel that gives no end of input, for example a
    /// virtio-serial port. On such a channel, [`Repo::receive_stream`] waits
    /// until the writer closes the channel or writes one more byte.
    ///
    /// # Errors
    ///
    /// The errors of [`Repo::receive_stream`], with one difference: a byte
    /// after `Commit` is not an error. These include [`Error::Io`] of kind
    /// `UnexpectedEof` if `input` ends before `Commit` is complete.
    pub async fn receive_stream_to_commit<R>(
        &self,
        input: R,
        policy: &StreamPolicy,
    ) -> Result<ReceiveReport>
    where
        R: AsyncRead + Unpin + Send,
    {
        let reader = FrameReader::new(BufReader::with_capacity(STREAM_BUFFER, input));
        run(self, policy, reader, AfterCommit::Stop)
            .await
            .map_err(stream_error)
    }
}

/// What the receive does with `input` after the `Commit` frame.
#[derive(Clone, Copy, PartialEq, Eq)]
enum AfterCommit {
    /// Read to the end of `input`. A byte there is `protocol`.
    CheckEnd,
    /// Start no read, and discard the bytes in the buffer with no check.
    Stop,
}

/// Returns the error of the call for `failure`.
fn stream_error(failure: Failure) -> Error {
    match failure {
        Failure::Wire(e) => Error::Remote(e),
        Failure::Internal(e) | Failure::Silent(e) => e,
    }
}

async fn run<R>(
    repo: &Repo,
    policy: &StreamPolicy,
    mut reader: FrameReader<BufReader<R>>,
    after: AfterCommit,
) -> std::result::Result<ReceiveReport, Failure>
where
    R: AsyncRead + Unpin + Send,
{
    let core = match next(&mut reader).await? {
        Message::Hello(hello) => SessionCore::open_one_way(repo.clone(), policy, hello).await?,
        other => return Err(out_of_order(&other)),
    };
    let mut buf = Vec::new();
    let request = loop {
        match next(&mut reader).await? {
            Message::ObjectHeader(header) => {
                core.objects(Some(header), &mut reader, &mut buf).await?;
            }
            Message::ObjectsEnd => {
                core.objects(None, &mut reader, &mut buf).await?;
            }
            Message::Commit(request) => break request,
            other => return Err(out_of_order(&other)),
        }
    };
    check_updates(&request)?;
    let mut rest = reader.into_inner();
    if after == AfterCommit::CheckEnd {
        // Any byte after `Commit` is `protocol`, also bytes that do not make
        // a whole frame. The `fill_buf` future of `futures-lite` polls the
        // reader a second time after a ready poll, and panics if that poll is
        // `Pending`. A reader that reads on a blocking pool returns `Pending`
        // when a read follows the end of input, so the check polls the reader
        // once for each wake.
        let end = std::future::poll_fn(|cx| {
            Pin::new(&mut rest)
                .poll_fill_buf(cx)
                .map_ok(|bytes| bytes.is_empty())
        })
        .await;
        match end {
            Ok(true) => {}
            Ok(false) => {
                return Err(Failure::Wire(remote::Error::Protocol(
                    "bytes follow Commit in a one-way stream".into(),
                )));
            }
            Err(e) => return Err(Failure::Silent(Error::Io(e))),
        }
    }
    // Under `AfterCommit::Stop`, the drop discards the bytes after `Commit`
    // that the buffer holds, with no check.
    drop(rest);
    drop(buf);
    Ok(core.finish(request).await?.report)
}

/// Checks that `request` holds no server commit, and that each update of
/// `request` expects its ref absent or takes any state, and sets a new
/// commit.
///
/// A one-way sender cannot learn the current tips of the refs, and the
/// server makes no commit for it.
fn check_updates(request: &CommitRequest) -> std::result::Result<(), Failure> {
    if request.server_commit.is_some() {
        return Err(Failure::Wire(remote::Error::Protocol(
            "a one-way stream carries no server commit".into(),
        )));
    }
    for update in &request.updates {
        if let Expected::Commit(_) = update.expected {
            return Err(Failure::Wire(remote::Error::Protocol(format!(
                "the update of '{}' expects a commit, which a one-way stream cannot state",
                update.name
            ))));
        }
        if update.new.is_none() {
            return Err(Failure::Wire(remote::Error::Protocol(format!(
                "the update of '{}' deletes the ref, which a one-way stream cannot do",
                update.name
            ))));
        }
    }
    Ok(())
}

// A compile-time check that the future of a one-way stream is `Send`, so
// that it can run on a thread pool.
const _: fn() = || {
    fn assert_send<T: Send>(_: T) {}
    let _ = |repo: &Repo, policy: &StreamPolicy| {
        assert_send(repo.receive_stream(futures_lite::io::empty(), policy));
        assert_send(repo.receive_stream_to_commit(futures_lite::io::empty(), policy))
    };
};