diff --git a/crates/account/src/local_account.rs b/crates/account/src/local_account.rs index d45a6f8a5c..3a49853f73 100644 --- a/crates/account/src/local_account.rs +++ b/crates/account/src/local_account.rs @@ -157,7 +157,7 @@ impl LocalAccount { #[allow(unused_mut)] let mut storage = ClientStorage::new_authenticated( - *account_id, + account_id, BackendTarget::FileSystem(paths), user, ) @@ -641,7 +641,7 @@ impl LocalAccount { let paths = Paths::new(data_dir, account_id.to_string()); let storage = ClientStorage::new_unauthenticated( - account_id, + &account_id, BackendTarget::FileSystem(paths.clone()), ) .await?; @@ -713,7 +713,7 @@ impl LocalAccount { let (authenticated_user, public_account) = new_account.into(); let mut storage = ClientStorage::new_authenticated( - account_id, + &account_id, BackendTarget::FileSystem(paths), authenticated_user, /* @@ -781,7 +781,7 @@ impl Account for LocalAccount { let paths = self.paths(); let mut storage = ClientStorage::new_unauthenticated( - account_id, + &account_id, BackendTarget::FileSystem((&*paths).clone()), ) .await?; @@ -1213,10 +1213,11 @@ impl Account for LocalAccount { } async fn root_commit(&self, summary: &Summary) -> Result { - let folder = - self.storage.folders().get(summary.id()).ok_or_else(|| { - StorageError::FolderNotFound(*summary.id()) - })?; + let folder = self + .storage + .folders() + .get(summary.id()) + .ok_or_else(|| StorageError::FolderNotFound(*summary.id()))?; let event_log = folder.event_log(); let log_file = event_log.read().await; Ok(log_file @@ -1337,10 +1338,11 @@ impl Account for LocalAccount { .await? .ok_or(Error::NoFolderPassword(*summary.id()))?; - let folder = - self.storage.folders().get(summary.id()).ok_or_else(|| { - StorageError::FolderNotFound(*summary.id()) - })?; + let folder = self + .storage + .folders() + .get(summary.id()) + .ok_or_else(|| StorageError::FolderNotFound(*summary.id()))?; let event_log = folder.event_log(); let log_file = event_log.read().await; diff --git a/crates/database_upgrader/src/upgrader/mod.rs b/crates/database_upgrader/src/upgrader/mod.rs index 5ecade2c76..d5bf4bb256 100644 --- a/crates/database_upgrader/src/upgrader/mod.rs +++ b/crates/database_upgrader/src/upgrader/mod.rs @@ -129,7 +129,7 @@ async fn compute_fs_account_status( ) -> Result { let sync_status = if account_paths.is_server() { let storage = ServerStorage::new( - account_paths.documents_dir(), + account_paths, account.account_id(), BackendTarget::FileSystem(account_paths.clone()), ) @@ -138,7 +138,7 @@ async fn compute_fs_account_status( storage.sync_status().await? } else { let storage = ClientStorage::new_unauthenticated( - *account.account_id(), + account.account_id(), BackendTarget::FileSystem(account_paths.clone()), ) .await?; @@ -156,14 +156,15 @@ async fn compute_db_account_status( ) -> Result { let sync_status = if account_paths.is_server() { let storage = ServerStorage::new( - account_paths.documents_dir(), + account_paths, account.account_id(), BackendTarget::Database(client.clone()), ) .await?; storage.sync_status().await? } else { - todo!("compute for client accounts"); + Default::default() + // todo!("compute for client accounts"); /* let storage = ClientFileSystemStorage::new_unauthenticated( *account.account_id(), diff --git a/crates/login/src/identity_folder.rs b/crates/login/src/identity_folder.rs index 65cf9981ff..62dcee902a 100644 --- a/crates/login/src/identity_folder.rs +++ b/crates/login/src/identity_folder.rs @@ -593,8 +593,6 @@ impl IdentityFolder { account_id: AccountId, keeper: &AccessPoint, ) -> Result<(UrnLookup, PrivateIdentity)> { - println!("doing login private identity..."); - let (index, identity_secret) = Self::lookup_identity_secrets(keeper).await?; diff --git a/crates/server/src/backend.rs b/crates/server/src/backend.rs index 0cac134967..1a8ce76e58 100644 --- a/crates/server/src/backend.rs +++ b/crates/server/src/backend.rs @@ -80,12 +80,12 @@ impl Backend { "server_backend::read_dir", ); + let paths = Paths::new_global_server(&self.directory); + let account = ServerStorage::new( - &self.directory, + &paths, &account_id, - BackendTarget::FileSystem( - Paths::new_global_server(&self.directory), - ), + BackendTarget::FileSystem(paths.clone()), ) .await?; @@ -121,9 +121,13 @@ impl Backend { "server_backend::create_account", ); - let account = ServerStorage::create_fs_account( - &self.directory, + let paths = Paths::new_global_server(&self.directory) + .with_account_id(account_id); + + let account = ServerStorage::create_account( + &paths, account_id, + BackendTarget::FileSystem(paths.clone()), &account_data, ) .await?; diff --git a/crates/storage/client/src/filesystem/mod.rs b/crates/storage/client/src/filesystem/mod.rs index 915e008121..01ccfdccd0 100644 --- a/crates/storage/client/src/filesystem/mod.rs +++ b/crates/storage/client/src/filesystem/mod.rs @@ -117,7 +117,7 @@ pub struct ClientFileSystemStorage { impl ClientFileSystemStorage { /// Create unauthenticated folder storage for client-side access. pub async fn new_unauthenticated( - account_id: AccountId, + account_id: &AccountId, paths: Paths, ) -> Result { paths.ensure().await?; @@ -142,7 +142,7 @@ impl ClientFileSystemStorage { let paths = Arc::new(paths); let mut storage = Self { - account_id, + account_id: *account_id, summaries: Vec::new(), current: Arc::new(Mutex::new(None)), folders: Default::default(), @@ -169,7 +169,7 @@ impl ClientFileSystemStorage { /// Create folder storage for client-side access. pub async fn new_authenticated( - account_id: AccountId, + account_id: &AccountId, paths: Paths, authenticated_user: Identity, /* @@ -214,7 +214,7 @@ impl ClientFileSystemStorage { let paths = Arc::new(paths); Ok(Self { - account_id, + account_id: *account_id, summaries: Vec::new(), current: Arc::new(Mutex::new(None)), folders: Default::default(), diff --git a/crates/storage/client/src/storage.rs b/crates/storage/client/src/storage.rs index 1b758036db..1281f3d1d4 100644 --- a/crates/storage/client/src/storage.rs +++ b/crates/storage/client/src/storage.rs @@ -56,7 +56,7 @@ pub enum ClientStorage { impl ClientStorage { /// Create new client storage. pub async fn new_unauthenticated( - account_id: AccountId, + account_id: &AccountId, target: BackendTarget, ) -> Result { match target { @@ -71,7 +71,7 @@ impl ClientStorage { /// Create new client storage in authenticated state. pub async fn new_authenticated( - account_id: AccountId, + account_id: &AccountId, target: BackendTarget, authenticated_user: Identity, ) -> Result { @@ -97,9 +97,11 @@ impl ClientStorage { /// Create new file system storage. async fn new_unauthenticated_fs( - account_id: AccountId, + account_id: &AccountId, paths: Paths, ) -> Result { + debug_assert!(!paths.is_server()); + Ok(Self::FileSystem( ClientFileSystemStorage::new_unauthenticated(account_id, paths) .await?, @@ -108,19 +110,17 @@ impl ClientStorage { /// Create new file system storage in authenticated state. async fn new_authenticated_fs( - account_id: AccountId, + account_id: &AccountId, paths: Paths, authenticated_user: Identity, ) -> Result { + debug_assert!(!paths.is_server()); + Ok(Self::FileSystem( ClientFileSystemStorage::new_authenticated( account_id, paths, authenticated_user, - /* - identity_log, - device, - */ ) .await?, )) @@ -128,36 +128,18 @@ impl ClientStorage { /// Create new file system storage. async fn new_unauthenticated_db( - account_id: AccountId, + account_id: &AccountId, client: Client, ) -> Result { - /* - Ok(Self::FileSystem( - ClientFileSystemStorage::new_unauthenticated(account_id, paths).await?, - )) - */ - todo!("unauthenticated db storage"); } /// Create new file system storage in authenticated state. async fn new_authenticated_db( - account_id: AccountId, + account_id: &AccountId, client: Client, authenticated_user: Identity, ) -> Result { - /* - Ok(Self::FileSystem( - ClientFileSystemStorage::new_authenticated( - account_id, - paths, - identity_log, - device, - ) - .await?, - )) - */ - todo!("authenticated db storage"); } } diff --git a/crates/storage/server/src/database/mod.rs b/crates/storage/server/src/database/mod.rs index c1ed8809c4..a6d943a89c 100644 --- a/crates/storage/server/src/database/mod.rs +++ b/crates/storage/server/src/database/mod.rs @@ -73,17 +73,11 @@ impl ServerDatabaseStorage { pub async fn new( client: Client, account_id: AccountId, - data_dir: Option, identity_log: Arc>, + paths: Paths, ) -> Result { - let data_dir = if let Some(data_dir) = data_dir { - data_dir - } else { - Paths::data_dir().map_err(|_| Error::NoCache)? - }; - - let dirs = Paths::new_server(data_dir, account_id.to_string()); - Self::new_client(client, Arc::new(dirs), account_id, identity_log) + debug_assert!(!paths.is_global()); + Self::new_client(client, Arc::new(paths), account_id, identity_log) .await } @@ -101,8 +95,6 @@ impl ServerDatabaseStorage { .into()); } - paths.ensure().await?; - let account_row = Self::lookup_account(&mut client, &account_id).await?; diff --git a/crates/storage/server/src/filesystem/mod.rs b/crates/storage/server/src/filesystem/mod.rs index 7c6145c2c8..76be19abd2 100644 --- a/crates/storage/server/src/filesystem/mod.rs +++ b/crates/storage/server/src/filesystem/mod.rs @@ -60,18 +60,12 @@ pub struct ServerFileStorage { impl ServerFileStorage { /// Create folder storage for server-side access. pub async fn new( + paths: Paths, account_id: AccountId, identity_log: Arc>, - data_dir: Option, ) -> Result { - let data_dir = if let Some(data_dir) = data_dir { - data_dir - } else { - Paths::data_dir().map_err(|_| Error::NoCache)? - }; - - let dirs = Paths::new_server(data_dir, account_id.to_string()); - Self::new_paths(Arc::new(dirs), account_id, identity_log).await + debug_assert!(!paths.is_global()); + Self::new_paths(Arc::new(paths), account_id, identity_log).await } /// Create new storage backed by files on disc. diff --git a/crates/storage/server/src/storage.rs b/crates/storage/server/src/storage.rs index 57178cace0..a8b5e1f962 100644 --- a/crates/storage/server/src/storage.rs +++ b/crates/storage/server/src/storage.rs @@ -28,7 +28,7 @@ use sos_sync::{ SyncStorage, UpdateSet, }; use sos_vault::Summary; -use std::{collections::HashSet, path::Path, sync::Arc}; +use std::{collections::HashSet, sync::Arc}; use tokio::sync::RwLock; /// Server storage backed by filesystem or database. @@ -42,52 +42,69 @@ pub enum ServerStorage { impl ServerStorage { /// Create new server storage. pub async fn new( - directory: impl AsRef, + paths: &Paths, account_id: &AccountId, target: BackendTarget, ) -> Result { + debug_assert!(paths.is_server()); + match target { - BackendTarget::FileSystem(_) => { - Self::new_fs(directory, account_id).await + BackendTarget::FileSystem(paths) => { + Self::new_fs(paths, account_id).await } BackendTarget::Database(client) => { - Self::new_db(directory, account_id, client).await + Self::new_db(paths, account_id, client).await } } } /// Create new file system storage. - async fn new_fs( - directory: impl AsRef, - account_id: &AccountId, - ) -> Result { - let user_paths = - Paths::new_server(directory.as_ref(), account_id.to_string()); + async fn new_fs(paths: Paths, account_id: &AccountId) -> Result { + let paths = paths.with_account_id(account_id); let mut event_log = - FolderEventLog::new_fs_folder(user_paths.identity_events()) - .await?; + FolderEventLog::new_fs_folder(paths.identity_events()).await?; event_log.load_tree().await?; let identity_log = Arc::new(RwLock::new(event_log)); Ok(Self::FileSystem( - ServerFileStorage::new( - *account_id, - identity_log, - Some(directory.as_ref().to_owned()), - ) - .await?, + ServerFileStorage::new(paths, *account_id, identity_log).await?, )) } + /// Create an account in server storage. + pub async fn create_account( + paths: &Paths, + account_id: &AccountId, + target: BackendTarget, + account_data: &CreateSet, + ) -> Result { + debug_assert!(paths.is_server()); + + match target { + BackendTarget::FileSystem(paths) => { + Self::create_fs_account(&paths, account_id, account_data) + .await + } + BackendTarget::Database(client) => { + Self::create_db_account( + paths, + account_id, + client, + account_data, + ) + .await + } + } + } + /// Create a new file system account. - pub async fn create_fs_account( - directory: impl AsRef, + async fn create_fs_account( + paths: &Paths, account_id: &AccountId, account_data: &CreateSet, ) -> Result { - let paths = - Paths::new_server(directory.as_ref(), account_id.to_string()); + let paths = paths.with_account_id(account_id); paths.ensure().await?; let identity_log = ServerFileStorage::initialize_account( @@ -97,9 +114,9 @@ impl ServerStorage { .await?; let mut storage = ServerFileStorage::new( + paths, *account_id, Arc::new(RwLock::new(identity_log)), - Some(directory.as_ref().to_owned()), ) .await?; storage.import_account(&account_data).await?; @@ -109,10 +126,12 @@ impl ServerStorage { /// Create new database storage. async fn new_db( - directory: impl AsRef, + paths: &Paths, account_id: &AccountId, client: Client, ) -> Result { + let paths = paths.with_account_id(account_id); + let folder_account_id = *account_id; let login_folder = client @@ -140,23 +159,21 @@ impl ServerStorage { ServerDatabaseStorage::new( client, *account_id, - Some(directory.as_ref().to_owned()), identity_log, + paths, ) .await?, )) } /// Create a new database account. - pub async fn create_db_account( - directory: impl AsRef, - mut client: Client, + async fn create_db_account( + paths: &Paths, account_id: &AccountId, + mut client: Client, account_data: &CreateSet, ) -> Result { - let paths = - Paths::new_server(directory.as_ref(), account_id.to_string()); - paths.ensure().await?; + let paths = paths.with_account_id(account_id); let identity_log = ServerDatabaseStorage::initialize_account( &mut client, @@ -168,8 +185,8 @@ impl ServerStorage { let mut storage = ServerDatabaseStorage::new( client, *account_id, - Some(directory.as_ref().to_owned()), Arc::new(RwLock::new(identity_log)), + paths, ) .await?; storage.import_account(&account_data).await?; diff --git a/tests/unit/src/tests/server_storage.rs b/tests/unit/src/tests/server_storage.rs index f572ff8aa6..325ed06494 100644 --- a/tests/unit/src/tests/server_storage.rs +++ b/tests/unit/src/tests/server_storage.rs @@ -23,7 +23,7 @@ async fn fs_server_storage() -> Result<()> { let account_id = AccountId::random(); let mut storage = ServerStorage::new( - temp.path(), + &Paths::new_global_server(temp.path()), &account_id, BackendTarget::FileSystem(Paths::new_global_server(temp.path())), ) @@ -44,7 +44,7 @@ async fn db_server_storage() -> Result<()> { insert_database_vault(&mut client, &vault, true).await?; let mut storage = ServerStorage::new( - temp.path(), + &Paths::new_global_server(temp.path()), &account_id, BackendTarget::Database(client), )