Skip to main content

ostrya_remote/
lib.rs

1#![forbid(unsafe_code)]
2
3//! The wire codec and the client sessions of the ostrya push and pull.
4//!
5//! A caller pushes a local directory as a commit that the server makes with
6//! [`push_tree`](fn@push_tree), or its own objects with a [`PushSession`]. A [`PullSession`] reads the summary and the ref listings of a remote. The
7//! crate has no repository knowledge, and it compiles on Linux, macOS, and Windows.
8//!
9//! # Entry points
10//!
11//! - [`Endpoint::parse`] reads an address into an [`Endpoint`].
12//! - [`push_tree`](fn@push_tree) pushes a local directory to an endpoint as one server commit.
13//! - [`PushSession::connect`] opens a push session to an endpoint.
14//! - [`PushSession::prepare`] checks an endpoint before the session opens.
15//! - [`PushSession::over_stream`] opens a push session over a pair of byte streams.
16//! - [`PullSession::connect`] opens the session of a pull to an endpoint.
17//! - [`StreamSession::connect`] opens a stream session over ssh.
18//! - [`StreamSession::check_command_line`] checks the ssh command line of a pull before the session opens.
19//! - [`Error`] is the error of each fallible operation.
20//!
21//! # Modules
22//!
23//! - [`proto`]: the wire format and its codec.
24//! - [`session`]: the push and pull sessions, their progress, and the one-way stream.
25//! - [`transport`]: the endpoints, the addresses, and the ssh and HTTP transports.
26//! - [`tree`]: the walk and the hash of a local directory.
27//!
28//! # Features
29//!
30//! - `smol` (default): the `smol` backend of `ostrya-rt` for the ssh and HTTP transports.
31//! - `tokio`: the `tokio` backend of `ostrya-rt` for the ssh and HTTP transports.
32//!
33//! # Examples
34//!
35//! ```no_run
36//! use ostrya_remote::{TreePushOptions, push_tree, transport::Endpoint};
37//! # async fn run() -> ostrya_remote::Result<()> {
38//! let endpoint = Endpoint::parse("ssh://builder@repo.example.com/srv/repo")?;
39//! let opts = TreePushOptions { onto: "exampleos/stable".into(), ..Default::default() };
40//! let outcome = push_tree(endpoint, "rootfs".as_ref(), opts).await?;
41//! println!("{:?}", outcome.commit);
42//! # Ok(()) }
43//! ```
44
45mod error;
46pub mod proto;
47mod push_tree;
48pub mod session;
49pub mod transport;
50pub mod tree;
51
52pub use error::{Error, ErrorCode, Result};
53#[doc(hidden)]
54pub use proto::{Encoding, Expected, RefOutcome, RefState, RefUpdate};
55pub use push_tree::{TreePushOptions, push_tree, push_tree_over_stream, push_tree_prepared};
56#[doc(hidden)]
57pub use session::{
58    BoxFuture, Compression, DetachedMetadata, ObjectData, ObjectReader, ObjectSource, PullBody,
59    PullSession, PushOutcome, PushPhase, PushProgress, PushProgressFn, PushProgressSnapshot,
60    PushSession, PushStats, ServerInfo, SessionOptions, StreamSession, StreamSessionOptions,
61};
62#[doc(hidden)]
63pub use transport::{Endpoint, PreparedSession, SshAddress, SshOptions, without_userinfo};
64
65// The public types of the protocol move freely across tasks and threads.
66const _: fn() = || {
67    fn assert_send_sync<T: Send + Sync>() {}
68    assert_send_sync::<Error>();
69    assert_send_sync::<proto::Message>();
70    assert_send_sync::<proto::PullHello>();
71    assert_send_sync::<proto::PullHelloReply>();
72    assert_send_sync::<proto::GetReply>();
73    assert_send_sync::<proto::FrameReader<&[u8]>>();
74    assert_send_sync::<proto::ObjectBody<'static, &[u8]>>();
75    assert_send_sync::<proto::FrameWriter<Vec<u8>>>();
76    assert_send_sync::<PushSession>();
77    assert_send_sync::<PushProgress>();
78    assert_send_sync::<PushStats>();
79    assert_send_sync::<PushOutcome>();
80    assert_send_sync::<ServerInfo>();
81    assert_send_sync::<ObjectData>();
82    assert_send_sync::<DetachedMetadata>();
83    assert_send_sync::<Box<dyn ObjectReader>>();
84    assert_send_sync::<PreparedSession>();
85    assert_send_sync::<tree::TreeModel>();
86    assert_send_sync::<tree::EntryMeta>();
87    assert_send_sync::<tree::EntryKind>();
88    assert_send_sync::<tree::EntryAction>();
89    assert_send_sync::<tree::EntryPath>();
90    assert_send_sync::<StreamSession>();
91    assert_send_sync::<PullSession>();
92    assert_send_sync::<PullBody>();
93    assert_send_sync::<StreamSessionOptions>();
94    assert_send_sync::<Endpoint>();
95    assert_send_sync::<SshAddress>();
96    assert_send_sync::<SshOptions>();
97};
98
99/// The futures of a stream session can run on a multi-threaded executor.
100#[allow(dead_code)]
101fn stream_session_futures_are_send(
102    address: &SshAddress,
103    options: &SshOptions,
104    session: &StreamSession,
105    owned: StreamSession,
106    selector: &ostrya_core::RefSelector,
107) {
108    fn assert_send<T: Send>(_: &T) {}
109    assert_send(&StreamSession::connect(
110        address,
111        options,
112        StreamSessionOptions::default(),
113    ));
114    assert_send(&StreamSession::over_stream(
115        futures_lite::io::empty(),
116        futures_lite::io::sink(),
117        StreamSessionOptions::default(),
118    ));
119    assert_send(&session.get("config", 1));
120    assert_send(&session.list_refs(selector, Some(selector), 1));
121    assert_send(&owned.finish());
122}
123
124/// The futures of a pull session can run on a multi-threaded executor.
125#[allow(dead_code)]
126fn pull_session_futures_are_send(
127    endpoint: &Endpoint,
128    session: &PullSession,
129    owned: PullSession,
130    selector: &ostrya_core::RefSelector,
131) {
132    fn assert_send<T: Send>(_: &T) {}
133    assert_send(&PullSession::connect(
134        endpoint.clone(),
135        StreamSessionOptions::default(),
136    ));
137    assert_send(&PullSession::over_stream(
138        futures_lite::io::empty(),
139        futures_lite::io::sink(),
140        StreamSessionOptions::default(),
141    ));
142    assert_send(&session.fetch_summary());
143    assert_send(&session.fetch_refs(selector, Some(selector)));
144    assert_send(&owned.finish());
145}
146
147/// The options of a tree scan and the future of the scan move to another
148/// thread.
149#[allow(dead_code)]
150fn scan_future_is_send(root: &std::path::Path, options: tree::ScanOptions) {
151    fn assert_send<T: Send>(_: &T) {}
152    assert_send(&options);
153    assert_send(&tree::TreeModel::scan(root, options));
154}
155
156/// The futures of the session calls can run on a multi-threaded executor.
157#[allow(dead_code)]
158fn session_futures_are_send(
159    session: &PushSession,
160    owned: PushSession,
161    source: &dyn ObjectSource,
162    names: &[ostrya_core::ObjectName],
163    commits: &[ostrya_core::Checksum],
164    updates: &[RefUpdate],
165) {
166    fn assert_send<T: Send>(_: &T) {}
167    assert_send(&session.missing(names));
168    assert_send(&session.send(source, names, commits, Compression::None));
169    assert_send(&owned.commit(updates, false));
170    assert_send(&session::export_stream(
171        futures_lite::io::sink(),
172        source,
173        names,
174        commits,
175        updates,
176        Compression::None,
177        SessionOptions::default(),
178    ));
179}
180
181/// The future of a server commit can run on a multi-threaded executor.
182#[allow(dead_code)]
183fn server_commit_future_is_send(owned: PushSession, server_commit: proto::ServerCommit) {
184    fn assert_send<T: Send>(_: &T) {}
185    assert_send(&owned.server_commit(server_commit));
186}
187
188/// The future of a connect, and the futures of its two steps, can run on a
189/// multi-threaded executor, over ssh and over HTTP.
190#[allow(dead_code)]
191fn connect_future_is_send(endpoint: &Endpoint, refs: &[String], prepared: PreparedSession) {
192    fn assert_send<T: Send>(_: &T) {}
193    assert_send(&PushSession::connect(
194        endpoint.clone(),
195        refs,
196        SessionOptions::default(),
197    ));
198    assert_send(&PushSession::prepare(endpoint.clone()));
199    assert_send(&prepared.open(refs, SessionOptions::default()));
200}
201
202/// The options of a tree push and the futures of the push can run on a
203/// multi-threaded executor.
204#[allow(dead_code)]
205fn push_tree_futures_are_send(
206    endpoint: &Endpoint,
207    root: &std::path::Path,
208    prepared: PreparedSession,
209) {
210    fn assert_send<T: Send>(_: &T) {}
211    assert_send(&TreePushOptions::default());
212    assert_send(&push_tree(
213        endpoint.clone(),
214        root,
215        TreePushOptions::default(),
216    ));
217    assert_send(&push_tree_prepared(
218        prepared,
219        root,
220        TreePushOptions::default(),
221    ));
222    assert_send(&push_tree_over_stream(
223        futures_lite::io::empty(),
224        futures_lite::io::sink(),
225        root,
226        TreePushOptions::default(),
227    ));
228}