22//! as secrets are created, updated and moved.
33
44use crate :: {
5- files:: FileStorage , ClientAccountStorage , ClientSecretStorage ,
6- ClientStorage , Error , Result ,
5+ files:: FileStorage , filesystem :: ClientFileSystemStorage ,
6+ ClientAccountStorage , ClientSecretStorage , Error , Result ,
77} ;
88use hex;
9+ use sos_backend:: FileEventLog ;
910use sos_core:: events:: FileEvent ;
10- use sos_core:: { basename, SecretId , SecretPath , VaultId } ;
11+ use sos_core:: { basename, Paths , SecretId , SecretPath , VaultId } ;
1112use 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
0 commit comments