Skip to content

Commit e2ce5c3

Browse files
committed
Refactor external file manager.
1 parent 0811e5a commit e2ce5c3

File tree

10 files changed

+357
-133
lines changed

10 files changed

+357
-133
lines changed

crates/account/src/account.rs

+28-16
Original file line numberDiff line numberDiff line change
@@ -904,12 +904,12 @@ impl LocalAccount {
904904
/// compatible sign_in() and also the newer sign_in_with_options().
905905
async fn login(&mut self, key: &AccessKey) -> Result<Vec<Summary>> {
906906
let account_id = &self.account_id;
907-
let data_dir = self.paths().documents_dir().clone();
907+
// let data_dir = self.paths().documents_dir().clone();
908908

909909
tracing::debug!(account_id = %account_id, "sign_in");
910910

911911
// Ensure all paths before sign_in
912-
let paths = Paths::new(&data_dir, account_id.to_string());
912+
let paths = self.paths().with_account_id(account_id);
913913
paths.ensure().await?;
914914

915915
tracing::debug!(data_dir = ?paths.documents_dir(), "sign_in");
@@ -925,7 +925,7 @@ impl LocalAccount {
925925
#[allow(unused_mut)]
926926
let mut storage = ClientStorage::new_authenticated(
927927
*account_id,
928-
Some(data_dir),
928+
BackendTarget::FileSystem(paths),
929929
identity_log,
930930
user.identity()?.devices()?.current_device(None),
931931
)
@@ -941,7 +941,7 @@ impl LocalAccount {
941941

942942
Self::initialize_account_log(
943943
&self.paths,
944-
Arc::clone(&storage.account_log),
944+
storage.account_log().await?,
945945
)
946946
.await?;
947947

@@ -1268,6 +1268,7 @@ impl LocalAccount {
12681268
{
12691269
let mut writer = self.storage.write().await;
12701270
let mut move_file_events = writer
1271+
.external_file_manager_mut()
12711272
.move_files(
12721273
&move_secret_data,
12731274
from.id(),
@@ -1279,6 +1280,7 @@ impl LocalAccount {
12791280
)
12801281
.await?;
12811282
writer
1283+
.external_file_manager_mut()
12821284
.append_file_mutation_events(&move_file_events)
12831285
.await?;
12841286
file_events.append(&mut move_file_events);
@@ -1460,7 +1462,7 @@ impl LocalAccount {
14601462
let storage = Arc::new(RwLock::new(
14611463
ClientStorage::new_unauthenticated(
14621464
account_id,
1463-
Arc::new(paths.clone()),
1465+
BackendTarget::FileSystem(paths.clone()),
14641466
)
14651467
.await?,
14661468
));
@@ -1516,10 +1518,12 @@ impl LocalAccount {
15161518
let account_builder = builder(AccountBuilder::new(
15171519
account_name,
15181520
passphrase.clone(),
1519-
BackendTarget::FileSystem(paths),
1521+
BackendTarget::FileSystem(paths.clone()),
15201522
));
15211523
let new_account = account_builder.finish().await?;
15221524

1525+
let paths = paths.with_account_id(&new_account.account_id);
1526+
15231527
tracing::debug!(
15241528
account_id = %new_account.account_id,
15251529
"new_account::created",
@@ -1530,7 +1534,7 @@ impl LocalAccount {
15301534

15311535
let mut storage = ClientStorage::new_authenticated(
15321536
account_id,
1533-
data_dir,
1537+
BackendTarget::FileSystem(paths),
15341538
identity_log,
15351539
new_account.user.identity()?.devices()?.current_device(None),
15361540
)
@@ -1584,12 +1588,15 @@ impl Account for LocalAccount {
15841588
let account_id = *self.account_id();
15851589
let paths = self.paths();
15861590

1587-
let mut storage =
1588-
ClientStorage::new_unauthenticated(account_id, paths.clone())
1589-
.await?;
1591+
let mut storage = ClientStorage::new_unauthenticated(
1592+
account_id,
1593+
BackendTarget::FileSystem((&*paths).clone()),
1594+
)
1595+
.await?;
15901596

15911597
{
1592-
let mut identity_log = storage.identity_log.write().await;
1598+
let identity_log = storage.identity_log().await?;
1599+
let mut identity_log = identity_log.write().await;
15931600
let records: Vec<EventRecord> = events.identity.into();
15941601
identity_log.apply_records(records).await?;
15951602
let vault = FolderReducer::new()
@@ -1607,7 +1614,8 @@ impl Account for LocalAccount {
16071614
}
16081615

16091616
{
1610-
let mut account_log = storage.account_log.write().await;
1617+
let account_log = storage.account_log().await?;
1618+
let mut account_log = account_log.write().await;
16111619
let records: Vec<EventRecord> = events.account.into();
16121620
account_log.apply_records(records).await?;
16131621

@@ -1617,7 +1625,8 @@ impl Account for LocalAccount {
16171625
}
16181626

16191627
{
1620-
let mut device_log = storage.device_log.write().await;
1628+
let device_log = storage.device_log().await?;
1629+
let mut device_log = device_log.write().await;
16211630
let records: Vec<EventRecord> = events.device.into();
16221631
device_log.apply_records(records).await?;
16231632
tracing::info!(
@@ -1630,7 +1639,8 @@ impl Account for LocalAccount {
16301639
#[cfg(feature = "files")]
16311640
{
16321641
tracing::info!("import_account_events::files");
1633-
let mut file_log = storage.file_log.write().await;
1642+
let file_log = storage.file_log().await?;
1643+
let mut file_log = file_log.write().await;
16341644
let records: Vec<EventRecord> = events.files.into();
16351645
file_log.apply_records(records).await?;
16361646
tracing::info!(
@@ -2266,8 +2276,10 @@ impl Account for LocalAccount {
22662276
file_name: &str,
22672277
) -> Result<Vec<u8>> {
22682278
let reader = self.storage.read().await;
2269-
let buffer =
2270-
reader.download_file(vault_id, secret_id, file_name).await?;
2279+
let buffer = reader
2280+
.external_file_manager()
2281+
.download_file(vault_id, secret_id, file_name)
2282+
.await?;
22712283

22722284
#[cfg(feature = "audit")]
22732285
{

crates/account/src/sync.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use indexmap::IndexSet;
88
use sos_backend::{
99
AccountEventLog, DeviceEventLog, FolderEventLog, StorageError,
1010
};
11-
use sos_client_storage::{ClientAccountStorage, ClientFolderStorage};
11+
use sos_client_storage::{
12+
ClientAccountStorage, ClientDeviceStorage, ClientFolderStorage,
13+
};
1214
use sos_core::{decode, events::EventLog};
1315
use sos_core::{
1416
events::{
@@ -271,7 +273,8 @@ impl Merge for LocalAccount {
271273

272274
let checked_patch = {
273275
let storage = self.storage.read().await;
274-
let mut event_log = storage.device_log.write().await;
276+
let device_log = storage.device_log().await?;
277+
let mut event_log = device_log.write().await;
275278
event_log
276279
.patch_checked(&diff.checkpoint, &diff.patch)
277280
.await?
@@ -280,13 +283,14 @@ impl Merge for LocalAccount {
280283
if let CheckedPatch::Success(_) = &checked_patch {
281284
let devices = {
282285
let storage = self.storage.read().await;
283-
let event_log = storage.device_log.read().await;
286+
let device_log = storage.device_log().await?;
287+
let event_log = device_log.read().await;
284288
let reducer = DeviceReducer::new(&*event_log);
285289
reducer.reduce().await?
286290
};
287291

288292
let mut storage = self.storage.write().await;
289-
storage.devices = devices;
293+
storage.set_devices(devices);
290294

291295
outcome.changes += diff.patch.len() as u64;
292296
outcome.tracked.device =
@@ -310,7 +314,8 @@ impl Merge for LocalAccount {
310314
);
311315

312316
let storage = self.storage.read().await;
313-
let mut event_log = storage.file_log.write().await;
317+
let file_log = storage.file_log().await?;
318+
let mut event_log = file_log.write().await;
314319

315320
// File events may not have a root commit
316321
let is_init_diff = diff.last_commit.is_none();
@@ -356,10 +361,7 @@ impl Merge for LocalAccount {
356361

357362
#[cfg(feature = "search")]
358363
let search = {
359-
let index = storage
360-
.index
361-
.as_ref()
362-
.ok_or(sos_client_storage::Error::NoSearchIndex)?;
364+
let index = storage.index()?;
363365
index.search()
364366
};
365367

crates/ipc/src/web_service/web_accounts.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use sos_core::{
1010
events::{AccountEvent, EventLog, WriteEvent},
1111
AccountId, ErrorExt, Paths, VaultId,
1212
};
13-
use sos_sync::SyncStorage;
13+
use sos_sync::{StorageEventLogs, SyncStorage};
1414
use sos_vault::SecretAccess;
1515
use std::{collections::HashMap, sync::Arc};
1616
use tokio::sync::{broadcast, RwLock};
@@ -574,7 +574,8 @@ where
574574
let storage = account.storage().await;
575575
let storage = storage.read().await;
576576

577-
let mut event_log = storage.account_log.write().await;
577+
let account_log = storage.account_log().await?;
578+
let mut event_log = account_log.write().await;
578579
let commit = event_log.tree().last_commit();
579580

580581
let patch = event_log.diff_events(commit.as_ref()).await?;

crates/storage/client/src/files/file_manager.rs

+47-20
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
//! as secrets are created, updated and moved.
33
44
use crate::{
5-
files::FileStorage, ClientAccountStorage, ClientSecretStorage,
6-
ClientStorage, Error, Result,
5+
files::FileStorage, filesystem::ClientFileSystemStorage,
6+
ClientAccountStorage, ClientSecretStorage, Error, Result,
77
};
88
use hex;
9+
use sos_backend::FileEventLog;
910
use sos_core::events::FileEvent;
10-
use sos_core::{basename, SecretId, SecretPath, VaultId};
11+
use sos_core::{basename, Paths, SecretId, SecretPath, VaultId};
1112
use sos_external_files::{
1213
list_folder_files, EncryptedFile, FileMutationEvent, FileProgress,
1314
FileSource, FileStorageDiff, FileStorageResult,
@@ -20,10 +21,29 @@ use sos_sdk::{
2021
},
2122
vfs,
2223
};
23-
use std::{collections::HashMap, path::Path};
24-
use tokio::sync::mpsc;
24+
use std::{collections::HashMap, path::Path, sync::Arc};
25+
use tokio::sync::{mpsc, RwLock};
26+
27+
/// Manages external files.
28+
pub struct ExternalFileManager {
29+
paths: Arc<Paths>,
30+
file_log: Arc<RwLock<FileEventLog>>,
31+
pub(crate) file_password: Option<secrecy::SecretString>,
32+
}
33+
34+
impl ExternalFileManager {
35+
/// Create new external file manager.
36+
pub fn new(
37+
paths: Arc<Paths>,
38+
file_log: Arc<RwLock<FileEventLog>>,
39+
) -> Self {
40+
Self {
41+
paths,
42+
file_log,
43+
file_password: None,
44+
}
45+
}
2546

26-
impl ClientStorage {
2747
/// Append file mutation events to the file event log.
2848
pub async fn append_file_mutation_events(
2949
&mut self,
@@ -127,7 +147,7 @@ impl ClientStorage {
127147
summary: &Summary,
128148
secret_data: SecretRow,
129149
file_progress: &mut Option<mpsc::Sender<FileProgress>>,
130-
) -> Result<Vec<FileMutationEvent>> {
150+
) -> Result<(Vec<FileMutationEvent>, Option<(SecretId, SecretRow)>)> {
131151
self.write_update_checksum(summary, secret_data, None, file_progress)
132152
.await
133153
}
@@ -140,7 +160,7 @@ impl ClientStorage {
140160
old_secret: &SecretRow,
141161
new_secret: SecretRow,
142162
file_progress: &mut Option<mpsc::Sender<FileProgress>>,
143-
) -> Result<Vec<FileMutationEvent>> {
163+
) -> Result<(Vec<FileMutationEvent>, Option<(SecretId, SecretRow)>)> {
144164
let mut results = Vec::new();
145165

146166
let old_secret_id = old_secret.id();
@@ -186,8 +206,8 @@ impl ClientStorage {
186206
}
187207

188208
// Write changed files to the new location
189-
if !changed_files.is_empty() {
190-
let written = self
209+
let write_update = if !changed_files.is_empty() {
210+
let (written, write_update) = self
191211
.write_update_checksum(
192212
new_summary,
193213
new_secret,
@@ -196,9 +216,12 @@ impl ClientStorage {
196216
)
197217
.await?;
198218
results.extend_from_slice(&written);
199-
}
219+
write_update
220+
} else {
221+
None
222+
};
200223

201-
Ok(results)
224+
Ok((results, write_update))
202225
}
203226

204227
/// Delete a collection of files from the external storage.
@@ -255,7 +278,7 @@ impl ClientStorage {
255278
secret_id: &SecretId,
256279
file_name: &str,
257280
) -> Result<FileEvent> {
258-
let vault_path = self.paths().files_dir().join(vault_id.to_string());
281+
let vault_path = self.paths.files_dir().join(vault_id.to_string());
259282
let secret_path = vault_path.join(secret_id.to_string());
260283
let path = secret_path.join(file_name);
261284

@@ -335,12 +358,12 @@ impl ClientStorage {
335358
file_name: &str,
336359
) -> Result<FileMutationEvent> {
337360
let old_vault_path =
338-
self.paths().files_dir().join(old_vault_id.to_string());
361+
self.paths.files_dir().join(old_vault_id.to_string());
339362
let old_secret_path = old_vault_path.join(old_secret_id.to_string());
340363
let old_path = old_secret_path.join(file_name);
341364

342365
let new_path = self
343-
.paths()
366+
.paths
344367
.files_dir()
345368
.join(new_vault_id.to_string())
346369
.join(new_secret_id.to_string())
@@ -382,7 +405,7 @@ impl ClientStorage {
382405
mut secret_data: SecretRow,
383406
sources: Option<Vec<FileSource>>,
384407
file_progress: &mut Option<mpsc::Sender<FileProgress>>,
385-
) -> Result<Vec<FileMutationEvent>> {
408+
) -> Result<(Vec<FileMutationEvent>, Option<(SecretId, SecretRow)>)> {
386409
tracing::debug!(folder = ?summary.id(), "write_update_checksum");
387410

388411
let mut results = Vec::new();
@@ -507,12 +530,16 @@ impl ClientStorage {
507530
(secret, false)
508531
};
509532

510-
if changed {
533+
let write_update = if changed {
511534
let secret_data = SecretRow::new(id, new_meta, new_secret);
512535

513536
// Update with new checksum(s)
514-
self.write_secret(&id, secret_data, false).await?;
515-
}
537+
// self.write_secret(&id, secret_data, false).await?;
538+
539+
Some((id, secret_data))
540+
} else {
541+
None
542+
};
516543

517544
let events = results
518545
.into_iter()
@@ -521,7 +548,7 @@ impl ClientStorage {
521548
event: data.1,
522549
})
523550
.collect::<Vec<_>>();
524-
Ok(events)
551+
Ok((events, write_update))
525552
}
526553
}
527554

crates/storage/client/src/files/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
mod external_files;
33
mod file_manager;
44
pub use external_files::FileStorage;
5+
pub use file_manager::ExternalFileManager;

0 commit comments

Comments
 (0)