2
2
//! as secrets are created, updated and moved.
3
3
4
4
use crate :: {
5
- files:: FileStorage , ClientAccountStorage , ClientSecretStorage ,
6
- ClientStorage , Error , Result ,
5
+ files:: FileStorage , filesystem :: ClientFileSystemStorage ,
6
+ ClientAccountStorage , ClientSecretStorage , Error , Result ,
7
7
} ;
8
8
use hex;
9
+ use sos_backend:: FileEventLog ;
9
10
use sos_core:: events:: FileEvent ;
10
- use sos_core:: { basename, SecretId , SecretPath , VaultId } ;
11
+ use sos_core:: { basename, Paths , SecretId , SecretPath , VaultId } ;
11
12
use sos_external_files:: {
12
13
list_folder_files, EncryptedFile , FileMutationEvent , FileProgress ,
13
14
FileSource , FileStorageDiff , FileStorageResult ,
@@ -20,10 +21,29 @@ use sos_sdk::{
20
21
} ,
21
22
vfs,
22
23
} ;
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
+ }
25
46
26
- impl ClientStorage {
27
47
/// Append file mutation events to the file event log.
28
48
pub async fn append_file_mutation_events (
29
49
& mut self ,
@@ -127,7 +147,7 @@ impl ClientStorage {
127
147
summary : & Summary ,
128
148
secret_data : SecretRow ,
129
149
file_progress : & mut Option < mpsc:: Sender < FileProgress > > ,
130
- ) -> Result < Vec < FileMutationEvent > > {
150
+ ) -> Result < ( Vec < FileMutationEvent > , Option < ( SecretId , SecretRow ) > ) > {
131
151
self . write_update_checksum ( summary, secret_data, None , file_progress)
132
152
. await
133
153
}
@@ -140,7 +160,7 @@ impl ClientStorage {
140
160
old_secret : & SecretRow ,
141
161
new_secret : SecretRow ,
142
162
file_progress : & mut Option < mpsc:: Sender < FileProgress > > ,
143
- ) -> Result < Vec < FileMutationEvent > > {
163
+ ) -> Result < ( Vec < FileMutationEvent > , Option < ( SecretId , SecretRow ) > ) > {
144
164
let mut results = Vec :: new ( ) ;
145
165
146
166
let old_secret_id = old_secret. id ( ) ;
@@ -186,8 +206,8 @@ impl ClientStorage {
186
206
}
187
207
188
208
// 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
191
211
. write_update_checksum (
192
212
new_summary,
193
213
new_secret,
@@ -196,9 +216,12 @@ impl ClientStorage {
196
216
)
197
217
. await ?;
198
218
results. extend_from_slice ( & written) ;
199
- }
219
+ write_update
220
+ } else {
221
+ None
222
+ } ;
200
223
201
- Ok ( results)
224
+ Ok ( ( results, write_update ) )
202
225
}
203
226
204
227
/// Delete a collection of files from the external storage.
@@ -255,7 +278,7 @@ impl ClientStorage {
255
278
secret_id : & SecretId ,
256
279
file_name : & str ,
257
280
) -> 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 ( ) ) ;
259
282
let secret_path = vault_path. join ( secret_id. to_string ( ) ) ;
260
283
let path = secret_path. join ( file_name) ;
261
284
@@ -335,12 +358,12 @@ impl ClientStorage {
335
358
file_name : & str ,
336
359
) -> Result < FileMutationEvent > {
337
360
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 ( ) ) ;
339
362
let old_secret_path = old_vault_path. join ( old_secret_id. to_string ( ) ) ;
340
363
let old_path = old_secret_path. join ( file_name) ;
341
364
342
365
let new_path = self
343
- . paths ( )
366
+ . paths
344
367
. files_dir ( )
345
368
. join ( new_vault_id. to_string ( ) )
346
369
. join ( new_secret_id. to_string ( ) )
@@ -382,7 +405,7 @@ impl ClientStorage {
382
405
mut secret_data : SecretRow ,
383
406
sources : Option < Vec < FileSource > > ,
384
407
file_progress : & mut Option < mpsc:: Sender < FileProgress > > ,
385
- ) -> Result < Vec < FileMutationEvent > > {
408
+ ) -> Result < ( Vec < FileMutationEvent > , Option < ( SecretId , SecretRow ) > ) > {
386
409
tracing:: debug!( folder = ?summary. id( ) , "write_update_checksum" ) ;
387
410
388
411
let mut results = Vec :: new ( ) ;
@@ -507,12 +530,16 @@ impl ClientStorage {
507
530
( secret, false )
508
531
} ;
509
532
510
- if changed {
533
+ let write_update = if changed {
511
534
let secret_data = SecretRow :: new ( id, new_meta, new_secret) ;
512
535
513
536
// 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
+ } ;
516
543
517
544
let events = results
518
545
. into_iter ( )
@@ -521,7 +548,7 @@ impl ClientStorage {
521
548
event : data. 1 ,
522
549
} )
523
550
. collect :: < Vec < _ > > ( ) ;
524
- Ok ( events)
551
+ Ok ( ( events, write_update ) )
525
552
}
526
553
}
527
554
0 commit comments