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_for_pull("config", 1));
161    assert_send(&bucket.get_stream("config"));
162    assert_send(&bucket.get_stream_strict("config"));
163    assert_send(&bucket.exists("config"));
164    assert_send(&bucket.exists_strict("config"));
165    assert_send(&bucket.put("config", Vec::new()));
166    assert_send(&bucket.put_if_absent("config", Vec::new()));
167    assert_send(&bucket.delete("config"));
168    assert_send(&bucket.list("objects/"));
169    assert_send(&bucket.list_delimited("objects/"));
170    assert_send(&bucket.list_uploads());
171    assert_send(&bucket.put_stream("config", &b""[..], 8 << 20));
172    assert_send(&bucket.abort_upload("config", "id"));
173}
174
175/// The futures of a stream session can run on a multi-threaded executor.
176#[allow(dead_code)]
177fn stream_session_futures_are_send(
178    address: &SshAddress,
179    options: &SshOptions,
180    session: &StreamSession,
181    owned: StreamSession,
182    selector: &ostrya_core::RefSelector,
183) {
184    fn assert_send<T: Send>(_: &T) {}
185    assert_send(&StreamSession::connect(
186        address,
187        options,
188        StreamSessionOptions::default(),
189    ));
190    assert_send(&StreamSession::over_stream(
191        futures_lite::io::empty(),
192        futures_lite::io::sink(),
193        StreamSessionOptions::default(),
194    ));
195    assert_send(&session.get("config", 1));
196    assert_send(&session.list_refs(selector, Some(selector), 1));
197    assert_send(&owned.finish());
198}
199
200/// The futures of a pull session can run on a multi-threaded executor.
201#[allow(dead_code)]
202fn pull_session_futures_are_send(
203    endpoint: &Endpoint,
204    session: &PullSession,
205    owned: PullSession,
206    selector: &ostrya_core::RefSelector,
207) {
208    fn assert_send<T: Send>(_: &T) {}
209    assert_send(&PullSession::connect(
210        endpoint.clone(),
211        StreamSessionOptions::default(),
212    ));
213    assert_send(&PullSession::over_stream(
214        futures_lite::io::empty(),
215        futures_lite::io::sink(),
216        StreamSessionOptions::default(),
217    ));
218    assert_send(&session.fetch_summary());
219    assert_send(&session.fetch_refs(selector, Some(selector)));
220    assert_send(&owned.finish());
221}
222
223/// The options of a tree scan and the future of the scan move to another
224/// thread.
225#[allow(dead_code)]
226fn scan_future_is_send(root: &std::path::Path, options: tree::ScanOptions) {
227    fn assert_send<T: Send>(_: &T) {}
228    assert_send(&options);
229    assert_send(&tree::TreeModel::scan(root, options));
230}
231
232/// The future of the checks and the scan of a tree push moves to another
233/// thread.
234#[allow(dead_code)]
235fn scan_tree_future_is_send(root: &std::path::Path, opts: TreePushOptions) {
236    fn assert_send<T: Send>(_: &T) {}
237    assert_send(&scan_tree(root, opts));
238}
239
240/// The futures of the session calls can run on a multi-threaded executor.
241#[allow(dead_code)]
242fn session_futures_are_send(
243    session: &PushSession,
244    owned: PushSession,
245    source: &dyn ObjectSource,
246    names: &[ostrya_core::ObjectName],
247    commits: &[ostrya_core::Checksum],
248    updates: &[RefUpdate],
249) {
250    fn assert_send<T: Send>(_: &T) {}
251    assert_send(&session.missing(names));
252    assert_send(&session.send(source, names, commits, Compression::None));
253    assert_send(&owned.commit(updates, false));
254    assert_send(&session::export_stream(
255        futures_lite::io::sink(),
256        source,
257        names,
258        commits,
259        updates,
260        Compression::None,
261        SessionOptions::default(),
262    ));
263}
264
265/// The future of a server commit can run on a multi-threaded executor.
266#[allow(dead_code)]
267fn server_commit_future_is_send(owned: PushSession, server_commit: proto::ServerCommit) {
268    fn assert_send<T: Send>(_: &T) {}
269    assert_send(&owned.server_commit(server_commit));
270}
271
272/// The future of a connect, and the futures of its two steps, can run on a
273/// multi-threaded executor, over ssh and over HTTP.
274#[allow(dead_code)]
275fn connect_future_is_send(endpoint: &Endpoint, refs: &[String], prepared: PreparedSession) {
276    fn assert_send<T: Send>(_: &T) {}
277    assert_send(&PushSession::connect(
278        endpoint.clone(),
279        refs,
280        SessionOptions::default(),
281    ));
282    assert_send(&PushSession::prepare(endpoint.clone()));
283    assert_send(&prepared.open(refs, SessionOptions::default()));
284}
285
286/// The options of a tree push and the futures of the push can run on a
287/// multi-threaded executor.
288#[allow(dead_code)]
289fn push_tree_futures_are_send(
290    endpoint: &Endpoint,
291    root: &std::path::Path,
292    prepared: PreparedSession,
293) {
294    fn assert_send<T: Send>(_: &T) {}
295    assert_send(&TreePushOptions::default());
296    assert_send(&push_tree(
297        endpoint.clone(),
298        root,
299        TreePushOptions::default(),
300    ));
301    assert_send(&push_tree_prepared(
302        prepared,
303        root,
304        TreePushOptions::default(),
305    ));
306    assert_send(&push_tree_over_stream(
307        futures_lite::io::empty(),
308        futures_lite::io::sink(),
309        root,
310        TreePushOptions::default(),
311    ));
312}