//! The calls the sync core makes to Drive, behind a trait. //! //! `Core` used to hold a concrete [`ProtonDriveClient`], so no test could make //! Drive answer late, answer stale, or not answer at all, and the interleavings //! behind most recent bugs could only be found on a real account over a slow //! link. [`DriveApi`] is the part of the //! client the mount, the drain, the event feed and the mirror engine use; the //! real client implements it by forwarding, and the simulation tests implement //! it with an in-memory Drive that injects Drive's known faults. //! //! Sharing, Photos, invitations, public links and the rest of the device calls //! stay on the concrete client (`Core::client`) until a test needs them faked. //! Listing and registering devices are here, so a simulated daemon can sync a //! local folder. //! //! The method names match the client's own, so a call site reads the same //! whichever it goes through. The client's generic methods (a reader or writer //! of any type) cannot be part of an object-safe trait, so the trait takes them //! as trait objects and `impl dyn DriveApi` puts the generic signatures back. use std::io::{Read, Write}; use std::sync::Arc; use async_trait::async_trait; use futures::StreamExt as _; use futures::stream::BoxStream; use proton_drive_rs::proton_sdk::account::Quota; use proton_drive_rs::proton_sdk::api::ResponseCode; use proton_drive_rs::proton_sdk::error::{ProtonError, Result}; use proton_drive_rs::proton_sdk::ids::{DriveEventId, LinkId, NodeUid, ShareId, VolumeId}; use proton_drive_rs::{ AvailableNameTarget, Device, DeviceType, DriveEvent, DriveEventScopeId, Node, NodeKind, NodeMoveItem, ProtonDriveClient, RevisionReader, Thumbnail, ThumbnailType, TrashedNode, }; pub use proton_drive_rs::{RevisionUpload, SealedUpload}; pub mod counted; pub mod digest; /// One outcome per node of a batch request; see [`ProtonDriveClient::trash_nodes`]. pub type NodeOutcomes = Vec<(NodeUid, Result<()>)>; /// The outcomes of a streaming batch request, one item per node as it lands. pub type OutcomeStream<'a> = BoxStream<'a, Result<(NodeUid, Result<()>)>>; /// Drive as the sync core sees it. See the module documentation. #[async_trait] pub trait DriveApi: Send + Sync { async fn get_my_files_folder(&self) -> Result; async fn context_share_id(&self, uid: &NodeUid) -> Result; async fn get_node(&self, uid: &NodeUid) -> Result>; async fn enumerate_nodes(&self, uids: &[NodeUid]) -> Result>; /// As [`DriveApi::enumerate_nodes`], but files carry no claimed size or /// modification time; see [`ProtonDriveClient::enumerate_nodes_light`]. async fn enumerate_nodes_light(&self, uids: &[NodeUid]) -> Result>; async fn enumerate_folder_children_node_uids( &self, folder_uid: &NodeUid, ) -> Result>; async fn enumerate_events( &self, scope: &DriveEventScopeId, cursor: Option<&DriveEventId>, ) -> Result>; async fn invalidate_caches_for_event(&self, event: &DriveEvent) -> Result<()>; async fn create_folder( &self, parent_uid: &NodeUid, name: &str, last_modification_time: Option, ) -> Result; async fn upload_file( &self, parent_uid: &NodeUid, name: &str, media_type: &str, contents: &[u8], ) -> Result; #[allow(clippy::too_many_arguments)] async fn upload_file_from_dyn( &self, parent_uid: &NodeUid, name: &str, media_type: &str, reader: &mut (dyn Read + Send), intended_size: i64, thumbnails: Vec, last_modification_time: Option, aead: bool, ) -> Result; #[allow(clippy::too_many_arguments)] async fn upload_file_replacing_draft_from_dyn( &self, parent_uid: &NodeUid, name: &str, media_type: &str, reader: &mut (dyn Read + Send), intended_size: i64, thumbnails: Vec, last_modification_time: Option, aead: bool, ) -> Result; async fn upload_new_revision_from_dyn( &self, file_uid: &NodeUid, reader: &mut (dyn Read + Send), intended_size: i64, thumbnails: Vec, last_modification_time: Option, ) -> Result<()>; /// A new revision of `file_uid`, only over the revision `expected`. See /// [`ProtonDriveClient::upload_new_revision_over`]. async fn upload_new_revision_over_dyn( &self, file_uid: &NodeUid, expected: &str, reader: &mut (dyn Read + Send), intended_size: i64, thumbnails: Vec, last_modification_time: Option, ) -> Result; async fn open_revision(&self, uid: &NodeUid) -> Result>; /// The file's thumbnail of `thumbnail_type`, or `None` when its revision /// carries none. async fn download_thumbnail( &self, uid: &NodeUid, thumbnail_type: ThumbnailType, ) -> Result>>; async fn download_file_to_dyn( &self, uid: &NodeUid, output: &mut (dyn Write + Send), ) -> Result<()>; async fn download_revision_to_dyn( &self, file_uid: &NodeUid, revision_id: &str, writer: &mut (dyn Write + Send), ) -> Result<()>; async fn rename_node( &self, uid: &NodeUid, new_name: &str, new_media_type: Option<&str>, ) -> Result<()>; async fn move_node(&self, uid: &NodeUid, new_parent: &NodeUid) -> Result<()>; /// See [`ProtonDriveClient::copy_node`]. async fn copy_node( &self, uid: &NodeUid, new_parent: &NodeUid, name: Option<&str>, ) -> Result; /// See [`ProtonDriveClient::get_available_name`]. async fn get_available_name( &self, parent_uid: &NodeUid, name: &str, target: AvailableNameTarget, ) -> Result; fn move_nodes_streaming<'a>( &'a self, items: Vec, new_parent: NodeUid, ) -> OutcomeStream<'a>; async fn trash_nodes(&self, uids: &[NodeUid]) -> Result; async fn restore_nodes(&self, uids: &[NodeUid]) -> Result; fn restore_nodes_streaming<'a>(&'a self, uids: &[NodeUid]) -> OutcomeStream<'a>; fn delete_nodes_streaming<'a>(&'a self, uids: &[NodeUid]) -> OutcomeStream<'a>; async fn enumerate_trash_node_uids(&self) -> Result>; /// Permanently delete everything in the trash. async fn empty_trash(&self) -> Result<()>; /// As [`DriveApi::enumerate_nodes_light`], for nodes in the trash, each /// with the time it was trashed when the server reports one. async fn enumerate_trashed_nodes_light(&self, uids: &[NodeUid]) -> Result> { Ok(self .enumerate_nodes_light(uids) .await? .into_iter() .map(|node| TrashedNode { node, trash_time: None, }) .collect()) } /// The account's storage, across every Proton product. async fn quota(&self) -> Result; /// Every computer that backs up to the account. async fn enumerate_devices(&self) -> Result>; /// Register a computer, with a root folder of its own named `name`. async fn create_device(&self, name: &str, device_type: DeviceType) -> Result; /// Called by the drain once it has let go of the blob of an upload to /// `uid` that landed, or retired the create that made `uid`, before the /// tree has the node, so a simulation can hold it there. Blocks the drain /// worker it is called on. fn landing(&self, _uid: &NodeUid) {} } /// The generic signatures of the client, over the object-safe ones above. impl dyn DriveApi { #[allow(clippy::too_many_arguments)] pub async fn upload_file_from( &self, parent_uid: &NodeUid, name: &str, media_type: &str, mut reader: R, intended_size: i64, thumbnails: Vec, last_modification_time: Option, aead: bool, ) -> Result { self.upload_file_from_dyn( parent_uid, name, media_type, &mut reader, intended_size, thumbnails, last_modification_time, aead, ) .await } #[allow(clippy::too_many_arguments)] pub async fn upload_file_replacing_draft_from( &self, parent_uid: &NodeUid, name: &str, media_type: &str, mut reader: R, intended_size: i64, thumbnails: Vec, last_modification_time: Option, aead: bool, ) -> Result { self.upload_file_replacing_draft_from_dyn( parent_uid, name, media_type, &mut reader, intended_size, thumbnails, last_modification_time, aead, ) .await } pub async fn upload_new_revision_from( &self, file_uid: &NodeUid, mut reader: R, intended_size: i64, thumbnails: Vec, last_modification_time: Option, ) -> Result<()> { self.upload_new_revision_from_dyn( file_uid, &mut reader, intended_size, thumbnails, last_modification_time, ) .await } pub async fn upload_new_revision_over( &self, file_uid: &NodeUid, expected: &str, mut reader: R, intended_size: i64, thumbnails: Vec, last_modification_time: Option, ) -> Result { self.upload_new_revision_over_dyn( file_uid, expected, &mut reader, intended_size, thumbnails, last_modification_time, ) .await } pub async fn download_file_to( &self, uid: &NodeUid, output: &mut W, ) -> Result<()> { self.download_file_to_dyn(uid, output).await } pub async fn download_revision_to( &self, file_uid: &NodeUid, revision_id: &str, writer: &mut W, ) -> Result<()> { self.download_revision_to_dyn(file_uid, revision_id, writer) .await } } /// An open revision that block reads are served from; see [`RevisionReader`]. #[async_trait] pub trait RevisionRead: Send + Sync { /// Plaintext size of the revision. fn size(&self) -> u64; /// Plaintext size of each block, in block order. fn block_sizes(&self) -> &[u64]; async fn read_at(&self, offset: u64, length: u64) -> Result>; } #[async_trait] impl RevisionRead for RevisionReader { fn size(&self) -> u64 { RevisionReader::size(self) } fn block_sizes(&self) -> &[u64] { RevisionReader::block_sizes(self) } async fn read_at(&self, offset: u64, length: u64) -> Result> { RevisionReader::read_at(self, offset, length).await } } #[async_trait] impl DriveApi for ProtonDriveClient { async fn context_share_id(&self, uid: &NodeUid) -> Result { ProtonDriveClient::context_share_id(self, uid).await } async fn get_my_files_folder(&self) -> Result { ProtonDriveClient::get_my_files_folder(self).await } async fn get_node(&self, uid: &NodeUid) -> Result> { ProtonDriveClient::get_node(self, uid).await } async fn enumerate_nodes(&self, uids: &[NodeUid]) -> Result> { ProtonDriveClient::enumerate_nodes(self, uids).await } async fn enumerate_nodes_light(&self, uids: &[NodeUid]) -> Result> { ProtonDriveClient::enumerate_nodes_light(self, uids).await } async fn enumerate_folder_children_node_uids( &self, folder_uid: &NodeUid, ) -> Result> { ProtonDriveClient::enumerate_folder_children_node_uids(self, folder_uid).await } async fn enumerate_events( &self, scope: &DriveEventScopeId, cursor: Option<&DriveEventId>, ) -> Result> { ProtonDriveClient::enumerate_events(self, scope, cursor).await } async fn invalidate_caches_for_event(&self, event: &DriveEvent) -> Result<()> { ProtonDriveClient::invalidate_caches_for_event(self, event).await } async fn create_folder( &self, parent_uid: &NodeUid, name: &str, last_modification_time: Option, ) -> Result { ProtonDriveClient::create_folder( self, parent_uid, name, last_modification_time.and_then(pdfs_core::timestamp::modification_time), ) .await } async fn upload_file( &self, parent_uid: &NodeUid, name: &str, media_type: &str, contents: &[u8], ) -> Result { ProtonDriveClient::upload_file(self, parent_uid, name, media_type, contents).await } async fn upload_file_from_dyn( &self, parent_uid: &NodeUid, name: &str, media_type: &str, reader: &mut (dyn Read + Send), intended_size: i64, thumbnails: Vec, last_modification_time: Option, aead: bool, ) -> Result { ProtonDriveClient::upload_file_from( self, parent_uid, name, media_type, reader, intended_size, thumbnails, last_modification_time.and_then(pdfs_core::timestamp::modification_time), aead, ) .await } async fn upload_file_replacing_draft_from_dyn( &self, parent_uid: &NodeUid, name: &str, media_type: &str, reader: &mut (dyn Read + Send), intended_size: i64, thumbnails: Vec, last_modification_time: Option, aead: bool, ) -> Result { ProtonDriveClient::upload_file_replacing_draft_from( self, parent_uid, name, media_type, reader, intended_size, thumbnails, last_modification_time.and_then(pdfs_core::timestamp::modification_time), aead, ) .await } async fn upload_new_revision_from_dyn( &self, file_uid: &NodeUid, reader: &mut (dyn Read + Send), intended_size: i64, thumbnails: Vec, last_modification_time: Option, ) -> Result<()> { ProtonDriveClient::upload_new_revision_from( self, file_uid, reader, intended_size, thumbnails, last_modification_time.and_then(pdfs_core::timestamp::modification_time), ) .await } async fn upload_new_revision_over_dyn( &self, file_uid: &NodeUid, expected: &str, reader: &mut (dyn Read + Send), intended_size: i64, thumbnails: Vec, last_modification_time: Option, ) -> Result { ProtonDriveClient::upload_new_revision_over( self, file_uid, expected, reader, intended_size, thumbnails, last_modification_time.and_then(pdfs_core::timestamp::modification_time), ) .await } async fn open_revision(&self, uid: &NodeUid) -> Result> { let reader = ProtonDriveClient::open_revision(self, uid).await?; Ok(Arc::new(reader)) } async fn download_thumbnail( &self, uid: &NodeUid, thumbnail_type: ThumbnailType, ) -> Result>> { ProtonDriveClient::download_thumbnail(self, uid, thumbnail_type).await } async fn download_file_to_dyn( &self, uid: &NodeUid, output: &mut (dyn Write + Send), ) -> Result<()> { ProtonDriveClient::download_file_to(self, uid, &mut &mut *output).await } async fn download_revision_to_dyn( &self, file_uid: &NodeUid, revision_id: &str, writer: &mut (dyn Write + Send), ) -> Result<()> { ProtonDriveClient::download_revision_to(self, file_uid, revision_id, &mut &mut *writer) .await } async fn rename_node( &self, uid: &NodeUid, new_name: &str, new_media_type: Option<&str>, ) -> Result<()> { ProtonDriveClient::rename_node(self, uid, new_name, new_media_type).await } async fn move_node(&self, uid: &NodeUid, new_parent: &NodeUid) -> Result<()> { ProtonDriveClient::move_node(self, uid, new_parent).await } async fn copy_node( &self, uid: &NodeUid, new_parent: &NodeUid, name: Option<&str>, ) -> Result { ProtonDriveClient::copy_node(self, uid, new_parent, name).await } async fn get_available_name( &self, parent_uid: &NodeUid, name: &str, target: AvailableNameTarget, ) -> Result { ProtonDriveClient::get_available_name(self, parent_uid, name, target).await } fn move_nodes_streaming<'a>( &'a self, items: Vec, new_parent: NodeUid, ) -> OutcomeStream<'a> { ProtonDriveClient::move_nodes_streaming(self, items, new_parent).boxed() } async fn trash_nodes(&self, uids: &[NodeUid]) -> Result { ProtonDriveClient::trash_nodes(self, uids).await } async fn restore_nodes(&self, uids: &[NodeUid]) -> Result { ProtonDriveClient::restore_nodes(self, uids).await } fn restore_nodes_streaming<'a>(&'a self, uids: &[NodeUid]) -> OutcomeStream<'a> { ProtonDriveClient::restore_nodes_streaming(self, uids).boxed() } fn delete_nodes_streaming<'a>(&'a self, uids: &[NodeUid]) -> OutcomeStream<'a> { ProtonDriveClient::delete_nodes_streaming(self, uids).boxed() } async fn enumerate_trash_node_uids(&self) -> Result> { ProtonDriveClient::enumerate_trash_node_uids(self).await } async fn enumerate_trashed_nodes_light(&self, uids: &[NodeUid]) -> Result> { ProtonDriveClient::enumerate_trashed_nodes_light(self, uids).await } async fn empty_trash(&self) -> Result<()> { ProtonDriveClient::empty_trash(self).await } async fn quota(&self) -> Result { ProtonDriveClient::quota(self).await } async fn enumerate_devices(&self) -> Result> { ProtonDriveClient::enumerate_devices(self).await } async fn create_device(&self, name: &str, device_type: DeviceType) -> Result { ProtonDriveClient::create_device(self, name, device_type).await } } /// Whether a failed call failed because Proton could not be reached, rather /// than because it answered and refused. /// /// A transport error is the network. So is a request timeout: the SDK reports /// Proton's own "try again" codes that way, and the daemon's `no_answer` reports its /// deadline that way. Anything else Proton actually said, and says again on a /// retry, so it must reach the caller rather than be queued. pub fn is_network_error(e: &ProtonError) -> bool { match e { ProtonError::Transport(_) => true, ProtonError::Api(api) => { matches!( api.code, ResponseCode::RequestTimeout | ResponseCode::Timeout ) } _ => false, } } /// A node's plaintext size as far as its listing knows: the claimed size when /// the revision carries one, else the size on storage. Zero for a folder. pub fn node_size(node: &Node) -> u64 { match &node.kind { NodeKind::Folder => 0, NodeKind::File { claimed_size, total_size_on_storage, .. } => claimed_size.unwrap_or(*total_size_on_storage).max(0) as u64, } } /// The server revision id of a node's active revision, if it is a file that has /// one. The stable identity the drain conflict-checks against (see `Baseline` in pdfs-store). pub fn node_revision_id(node: &Node) -> Option { match &node.kind { NodeKind::Folder => None, NodeKind::File { active_revision_id, .. } => active_revision_id.clone(), } } /// The plaintext content SHA-1 of a file node, if its active revision carried one. /// A download-free content fingerprint the conflict sweep uses to prove two files /// hold identical bytes before it removes one. pub fn node_content_sha1(node: &Node) -> Option { match &node.kind { NodeKind::Folder => None, NodeKind::File { content_sha1, .. } => content_sha1.clone(), } } /// Parse a `volume~link` uid display string back into a [`NodeUid`]. Front-ends /// receive uids as strings over the control socket and pass them back verbatim. pub fn parse_uid(s: &str) -> Option { let (vol, link) = s.split_once('~')?; if vol.is_empty() || link.is_empty() || link.contains('~') { return None; } Some(NodeUid::new(VolumeId::from(vol), LinkId::from(link))) } /// Whether a decrypted remote name can stand as one local path component. /// /// Drive checks names only when a client creates or renames a node, and only /// for length, so a name made by another client, or by a server that should /// not be trusted, can hold anything. Joined onto a local path, a `/` or a `..` /// in it walks out of the folder it belongs to, and the mirror engine would /// then write or delete outside the sync root. In the mount, the kernel fails /// the whole listing with EIO on a single such name (`docs/BUGS.md` B215). pub fn remote_name_is_safe(name: &str) -> bool { !name.is_empty() && name != "." && name != ".." && name.len() <= 255 && !name.contains(['/', '\0']) } /// A coarse MIME type guessed from a file name's extension; Proton stores this /// on the node but an exact value is not required for correctness. pub fn media_type_for(name: &str) -> &'static str { let ext = name.rsplit_once('.').map(|(_, e)| e.to_ascii_lowercase()); match ext.as_deref() { Some("txt" | "md" | "log") => "text/plain", Some("png") => "image/png", Some("jpg" | "jpeg") => "image/jpeg", Some("gif") => "image/gif", Some("pdf") => "application/pdf", Some("json") => "application/json", Some("html" | "htm") => "text/html", _ => "application/octet-stream", } } /// An in-memory Drive and the seeded randomness the simulations share. #[cfg(feature = "sim")] pub mod sim;