Skip to main content

ostrya_remote/
lib.rs

1#![forbid(unsafe_code)]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3
4//! The wire codec and the client sessions of the ostrya push and pull.
5//!
6//! A caller pushes a local directory as a commit that the server makes with
7//! [`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
8//! crate has no repository knowledge, and it compiles on Linux, macOS, and Windows.
9//!
10//! With the `s3` feature, the `s3` module signs S3 requests, reads the
11//! settings and the credentials of an S3 endpoint, and holds the client of a
12//! bucket repository. [`PullSession::connect`] opens a pull session to an S3
13//! endpoint, over HTTP with a fetcher that signs each request.
14//!
15//! # Entry points
16//!
17//! - [`Endpoint::parse`] reads an ssh or HTTP address into an [`Endpoint`].
18//! - [`push_tree`](fn@push_tree) pushes a local directory to an endpoint as one server commit.
19//! - [`scan_tree`] checks the options of a tree push and scans the directory, for a writer that makes the commit itself.
20//! - [`PushSession::connect`] opens a push session to an endpoint.
21//! - [`PushSession::prepare`] checks an endpoint before the session opens.
22//! - [`PushSession::over_stream`] opens a push session over a pair of byte streams.
23//! - [`PullSession::connect`] opens the session of a pull to an endpoint.
24//! - [`StreamSession::connect`] opens a stream session over ssh.
25//! - [`StreamSession::check_command_line`] checks the ssh command line of a pull before the session opens.
26//! - [`Error`] is the error of each fallible operation.
27//!
28//! # Modules
29//!
30//! - [`proto`]: the wire format and its codec.
31//! - [`session`]: the push and pull sessions, their progress, and the one-way stream.
32//! - [`transport`]: the endpoints, the addresses, and the ssh and HTTP transports.
33//! - [`tree`]: the walk and the hash of a local directory.
34//! - `s3` (feature `s3`): S3 request signing, the settings and credentials of an S3 endpoint, the address of a bucket repository, and the bucket client.
35//!
36//! # Features
37//!
38//! - `smol` (default): the `smol` backend of `ostrya-rt` for the ssh and HTTP transports.
39//! - `tokio`: the `tokio` backend of `ostrya-rt` for the ssh and HTTP transports.
40//! - `s3`: the `s3` module, which signs S3 requests with AWS Signature Version 4, reads the AWS credentials, and holds the bucket client.
41//!
42//! # Examples
43//!
44//! ```no_run
45//! use ostrya_core::Ref;
46//! use ostrya_remote::{TreePushOptions, push_tree, transport::Endpoint};
47//! # async fn run() -> ostrya_remote::Result<()> {
48//! let endpoint = Endpoint::parse("ssh://builder@repo.example.com/srv/repo")?;
49//! let opts = TreePushOptions { onto: Ref::local("exampleos/stable"), ..Default::default() };
50//! let outcome = push_tree(endpoint, "rootfs".as_ref(), opts).await?;
51//! println!("{:?}", outcome.commit);
52//! # Ok(()) }
53//! ```
54
55mod error;
56pub mod proto;
57mod push_tree;
58#[cfg(feature = "s3")]
59pub mod s3;
60pub mod session;
61pub mod transport;
62pub mod tree;
63
64pub use error::{Error, ErrorCode, Result};
65#[doc(hidden)]
66pub use proto::{Encoding, Expected, RefOutcome, RefState, RefUpdate};
67pub use push_tree::{
68    ScannedTree, TreePushOptions, push_tree, push_tree_over_stream, push_tree_prepared, scan_tree,
69};
70#[doc(hidden)]
71pub use session::{
72    BoxFuture, Compression, DetachedMetadata, ObjectData, ObjectReader, ObjectSource, PullBody,
73    PullSession, PushOutcome, PushPhase, PushProgress, PushProgressFn, PushProgressSnapshot,
74    PushSession, PushStats, ServerInfo, SessionOptions, StreamSession, StreamSessionOptions,
75};
76#[doc(hidden)]
77pub use transport::{Endpoint, PreparedSession, SshAddress, SshOptions, without_userinfo};
78
79// The public types of the protocol move freely across tasks and threads.
80const _: fn() = || {
81    fn assert_send_sync<T: Send + Sync>() {}
82    assert_send_sync::<Error>();
83    assert_send_sync::<proto::Message>();
84    assert_send_sync::<proto::PullHello>();
85    assert_send_sync::<proto::PullHelloReply>();
86    assert_send_sync::<proto::GetReply>();
87    assert_send_sync::<proto::FrameReader<&[u8]>>();
88    assert_send_sync::<proto::ObjectBody<'static, &[u8]>>();
89    assert_send_sync::<proto::FrameWriter<Vec<u8>>>();
90    assert_send_sync::<PushSession>();
91    assert_send_sync::<PushProgress>();
92    assert_send_sync::<PushStats>();
93    assert_send_sync::<PushOutcome>();
94    assert_send_sync::<ServerInfo>();
95    assert_send_sync::<ObjectData>();
96    assert_send_sync::<DetachedMetadata>();
97    assert_send_sync::<Box<dyn ObjectReader>>();
98    assert_send_sync::<PreparedSession>();
99    assert_send_sync::<tree::TreeModel>();
100    assert_send_sync::<ScannedTree>();
101    assert_send_sync::<tree::EntryMeta>();
102    assert_send_sync::<tree::EntryKind>();
103    assert_send_sync::<tree::EntryAction>();
104    assert_send_sync::<tree::EntryPath>();
105    assert_send_sync::<StreamSession>();
106    assert_send_sync::<PullSession>();
107    assert_send_sync::<PullBody>();
108    assert_send_sync::<StreamSessionOptions>();
109    assert_send_sync::<Endpoint>();
110    assert_send_sync::<SshAddress>();
111    assert_send_sync::<SshOptions>();
112};
113
114// The signer, the credentials, the settings of an S3 endpoint, the bucket
115// client, and its results move freely across tasks and threads.
116#[cfg(feature = "s3")]
117const _: fn() = || {
118    fn assert_send_sync<T: Send + Sync>() {}
119    assert_send_sync::<s3::SigV4>();
120    assert_send_sync::<s3::Credentials>();
121    assert_send_sync::<s3::Resolved>();
122    assert_send_sync::<s3::Settings>();
123    assert_send_sync::<s3::Environment>();
124    assert_send_sync::<s3::Address>();
125    assert_send_sync::<s3::Location>();
126    assert_send_sync::<s3::S3Endpoint>();
127    assert_send_sync::<s3::Bucket>();
128    assert_send_sync::<s3::Object>();
129    assert_send_sync::<s3::ObjectStream>();
130    assert_send_sync::<s3::PutIfAbsent>();
131    assert_send_sync::<s3::Listing>();
132    assert_send_sync::<s3::Listed>();
133    assert_send_sync::<s3::Upload>();
134};
135
136/// The future of the resolution of an S3 endpoint can run on a
137/// multi-threaded executor.
138#[cfg(feature = "s3")]
139#[allow(dead_code)]
140fn s3_resolve_future_is_send(settings: &s3::Settings, environment: &s3::Environment) {
141    fn assert_send<T: Send>(_: &T) {}
142    assert_send(&s3::resolve(settings, environment));
143}
144
145/// The futures of the bucket client and of the resolution of its endpoint can
146/// run on a multi-threaded executor.
147#[cfg(feature = "s3")]
148#[allow(dead_code)]
149fn bucket_futures_are_send(
150    endpoint: &s3::S3Endpoint,
151    bucket: &s3::Bucket,
152    settings: &s3::Settings,
153    environment: &s3::Environment,
154) {
155    fn assert_send<T: Send>(_: &T) {}
156    assert_send(&s3::S3Endpoint::resolve("s3://b/p", settings, environment));
157    assert_send(&s3::Bucket::new(endpoint));
158    assert_send(&bucket.get("config", 1));
159    assert_send(&bucket.get_strict("config", 1));
160    assert_send(&bucket.get_stream("config"));
161    assert_send(&bucket.get_stream_strict("config"));
162    assert_send(&bucket.exists("config"));
163    assert_send(&bucket.exists_strict("config"));
164    assert_send(&bucket.put("config", Vec::new()));
165    assert_send(&bucket.put_if_absent("config", Vec::new()));
166    assert_send(&bucket.delete("config"));
167    assert_send(&bucket.list("objects/"));
168    assert_send(&bucket.list_delimited("objects/"));
169    assert_send(&bucket.list_uploads());
170    assert_send(&bucket.put_stream("config", &b""[..], 8 << 20));
171    assert_send(&bucket.abort_upload("config", "id"));
172}
173
174/// The futures of a stream session can run on a multi-threaded executor.
175#[allow(dead_code)]
176fn stream_session_futures_are_send(
177    address: &SshAddress,
178    options: &SshOptions,
179    session: &StreamSession,
180    owned: StreamSession,
181    selector: &ostrya_core::RefSelector,
182) {
183    fn assert_send<T: Send>(_: &T) {}
184    assert_send(&StreamSession::connect(
185        address,
186        options,
187        StreamSessionOptions::default(),
188    ));
189    assert_send(&StreamSession::over_stream(
190        futures_lite::io::empty(),
191        futures_lite::io::sink(),
192        StreamSessionOptions::default(),
193    ));
194    assert_send(&session.get("config", 1));
195    assert_send(&session.list_refs(selector, Some(selector), 1));
196    assert_send(&owned.finish());
197}
198
199/// The futures of a pull session can run on a multi-threaded executor.
200#[allow(dead_code)]
201fn pull_session_futures_are_send(
202    endpoint: &Endpoint,
203    session: &PullSession,
204    owned: PullSession,
205    selector: &ostrya_core::RefSelector,
206) {
207    fn assert_send<T: Send>(_: &T) {}
208    assert_send(&PullSession::connect(
209        endpoint.clone(),
210        StreamSessionOptions::default(),
211    ));
212    assert_send(&PullSession::over_stream(
213        futures_lite::io::empty(),
214        futures_lite::io::sink(),
215        StreamSessionOptions::default(),
216    ));
217    assert_send(&session.fetch_summary());
218    assert_send(&session.fetch_refs(selector, Some(selector)));
219    assert_send(&owned.finish());
220}
221
222/// The options of a tree scan and the future of the scan move to another
223/// thread.
224#[allow(dead_code)]
225fn scan_future_is_send(root: &std::path::Path, options: tree::ScanOptions) {
226    fn assert_send<T: Send>(_: &T) {}
227    assert_send(&options);
228    assert_send(&tree::TreeModel::scan(root, options));
229}
230
231/// The future of the checks and the scan of a tree push moves to another
232/// thread.
233#[allow(dead_code)]
234fn scan_tree_future_is_send(root: &std::path::Path, opts: TreePushOptions) {
235    fn assert_send<T: Send>(_: &T) {}
236    assert_send(&scan_tree(root, opts));
237}
238
239/// The futures of the session calls can run on a multi-threaded executor.
240#[allow(dead_code)]
241fn session_futures_are_send(
242    session: &PushSession,
243    owned: PushSession,
244    source: &dyn ObjectSource,
245    names: &[ostrya_core::ObjectName],
246    commits: &[ostrya_core::Checksum],
247    updates: &[RefUpdate],
248) {
249    fn assert_send<T: Send>(_: &T) {}
250    assert_send(&session.missing(names));
251    assert_send(&session.send(source, names, commits, Compression::None));
252    assert_send(&owned.commit(updates, false));
253    assert_send(&session::export_stream(
254        futures_lite::io::sink(),
255        source,
256        names,
257        commits,
258        updates,
259        Compression::None,
260        SessionOptions::default(),
261    ));
262}
263
264/// The future of a server commit can run on a multi-threaded executor.
265#[allow(dead_code)]
266fn server_commit_future_is_send(owned: PushSession, server_commit: proto::ServerCommit) {
267    fn assert_send<T: Send>(_: &T) {}
268    assert_send(&owned.server_commit(server_commit));
269}
270
271/// The future of a connect, and the futures of its two steps, can run on a
272/// multi-threaded executor, over ssh and over HTTP.
273#[allow(dead_code)]
274fn connect_future_is_send(endpoint: &Endpoint, refs: &[String], prepared: PreparedSession) {
275    fn assert_send<T: Send>(_: &T) {}
276    assert_send(&PushSession::connect(
277        endpoint.clone(),
278        refs,
279        SessionOptions::default(),
280    ));
281    assert_send(&PushSession::prepare(endpoint.clone()));
282    assert_send(&prepared.open(refs, SessionOptions::default()));
283}
284
285/// The options of a tree push and the futures of the push can run on a
286/// multi-threaded executor.
287#[allow(dead_code)]
288fn push_tree_futures_are_send(
289    endpoint: &Endpoint,
290    root: &std::path::Path,
291    prepared: PreparedSession,
292) {
293    fn assert_send<T: Send>(_: &T) {}
294    assert_send(&TreePushOptions::default());
295    assert_send(&push_tree(
296        endpoint.clone(),
297        root,
298        TreePushOptions::default(),
299    ));
300    assert_send(&push_tree_prepared(
301        prepared,
302        root,
303        TreePushOptions::default(),
304    ));
305    assert_send(&push_tree_over_stream(
306        futures_lite::io::empty(),
307        futures_lite::io::sink(),
308        root,
309        TreePushOptions::default(),
310    ));
311}