@@ -23,12 +23,13 @@ use sos_vault::{
23
23
VaultCommit , VaultId , VaultMeta ,
24
24
} ;
25
25
use sos_vfs as vfs;
26
- use std:: { borrow :: Cow , path:: Path , sync:: Arc } ;
27
- use tokio:: sync:: RwLock ;
26
+ use std:: { path:: Path , sync:: Arc } ;
27
+ use tokio:: sync:: { Mutex , RwLock } ;
28
28
29
29
/// Folder is a combined vault and event log.
30
+ #[ derive( Clone ) ]
30
31
pub struct Folder {
31
- pub ( crate ) keeper : AccessPoint ,
32
+ pub ( crate ) access_point : Arc < Mutex < AccessPoint > > ,
32
33
events : Arc < RwLock < FolderEventLog > > ,
33
34
}
34
35
@@ -64,10 +65,10 @@ impl Folder {
64
65
} ;
65
66
66
67
let mirror = VaultFileWriter :: < Error > :: new ( path. as_ref ( ) ) ;
67
- let keeper =
68
+ let access_point =
68
69
VaultAccessPoint :: < Error > :: new_mirror ( vault, Box :: new ( mirror) ) ;
69
70
70
- Ok ( Self :: init ( AccessPoint :: new ( keeper ) , event_log) )
71
+ Ok ( Self :: init ( AccessPoint :: new ( access_point ) , event_log) )
71
72
}
72
73
73
74
/// Create a new folder from a database table.
@@ -126,33 +127,29 @@ impl Folder {
126
127
}
127
128
128
129
let mirror = VaultDatabaseWriter :: < Error > :: new ( client, folder_id) ;
129
- let keeper =
130
+ let access_point =
130
131
VaultAccessPoint :: < Error > :: new_mirror ( vault, Box :: new ( mirror) ) ;
131
132
132
- Ok ( Self :: init ( AccessPoint :: new ( keeper ) , event_log) )
133
+ Ok ( Self :: init ( AccessPoint :: new ( access_point ) , event_log) )
133
134
}
134
135
135
136
/// Create a new folder.
136
- fn init ( keeper : AccessPoint , events : FolderEventLog ) -> Self {
137
+ fn init ( access_point : AccessPoint , events : FolderEventLog ) -> Self {
137
138
Self {
138
- keeper ,
139
+ access_point : Arc :: new ( Mutex :: new ( access_point ) ) ,
139
140
events : Arc :: new ( RwLock :: new ( events) ) ,
140
141
}
141
142
}
142
143
143
144
/// Folder identifier.
144
- pub fn id ( & self ) -> & VaultId {
145
- self . keeper . id ( )
145
+ pub async fn id ( & self ) -> VaultId {
146
+ let access_point = self . access_point . lock ( ) . await ;
147
+ * access_point. id ( )
146
148
}
147
149
148
- /// AccessPoint for this folder.
149
- pub fn keeper ( & self ) -> & AccessPoint {
150
- & self . keeper
151
- }
152
-
153
- /// Mutable access point for this folder.
154
- pub fn keeper_mut ( & mut self ) -> & mut AccessPoint {
155
- & mut self . keeper
150
+ /// Access point for this folder.
151
+ pub fn access_point ( & self ) -> Arc < Mutex < AccessPoint > > {
152
+ self . access_point . clone ( )
156
153
}
157
154
158
155
/// Clone of the event log.
@@ -165,20 +162,23 @@ impl Folder {
165
162
& mut self ,
166
163
key : & AccessKey ,
167
164
) -> crate :: Result < VaultMeta > {
168
- Ok ( self . keeper . unlock ( key) . await ?)
165
+ let mut access_point = self . access_point . lock ( ) . await ;
166
+ Ok ( access_point. unlock ( key) . await ?)
169
167
}
170
168
171
169
/// Lock the folder.
172
- pub fn lock ( & mut self ) {
173
- self . keeper . lock ( ) ;
170
+ pub async fn lock ( & mut self ) {
171
+ let mut access_point = self . access_point . lock ( ) . await ;
172
+ access_point. lock ( ) ;
174
173
}
175
174
176
175
/// Create a secret.
177
176
pub async fn create_secret (
178
177
& mut self ,
179
178
secret_data : & SecretRow ,
180
179
) -> crate :: Result < WriteEvent > {
181
- let event = self . keeper . create_secret ( secret_data) . await ?;
180
+ let mut access_point = self . access_point . lock ( ) . await ;
181
+ let event = access_point. create_secret ( secret_data) . await ?;
182
182
let mut events = self . events . write ( ) . await ;
183
183
events. apply ( vec ! [ & event] ) . await ?;
184
184
Ok ( event)
@@ -189,15 +189,17 @@ impl Folder {
189
189
& self ,
190
190
id : & SecretId ,
191
191
) -> crate :: Result < Option < ( SecretMeta , Secret , ReadEvent ) > > {
192
- Ok ( self . keeper . read_secret ( id) . await ?)
192
+ let access_point = self . access_point . lock ( ) . await ;
193
+ Ok ( access_point. read_secret ( id) . await ?)
193
194
}
194
195
195
196
/// Read the encrypted contents of a secret.
196
197
pub async fn raw_secret (
197
198
& self ,
198
199
id : & SecretId ,
199
- ) -> crate :: Result < Option < ( Cow < ' _ , VaultCommit > , ReadEvent ) > > {
200
- Ok ( self . keeper . raw_secret ( id) . await ?)
200
+ ) -> crate :: Result < Option < ( VaultCommit , ReadEvent ) > > {
201
+ let access_point = self . access_point . lock ( ) . await ;
202
+ Ok ( access_point. raw_secret ( id) . await ?)
201
203
}
202
204
203
205
/// Update a secret.
@@ -207,8 +209,9 @@ impl Folder {
207
209
secret_meta : SecretMeta ,
208
210
secret : Secret ,
209
211
) -> crate :: Result < Option < WriteEvent > > {
212
+ let mut access_point = self . access_point . lock ( ) . await ;
210
213
if let Some ( event) =
211
- self . keeper . update_secret ( id, secret_meta, secret) . await ?
214
+ access_point . update_secret ( id, secret_meta, secret) . await ?
212
215
{
213
216
let mut events = self . events . write ( ) . await ;
214
217
events. apply ( vec ! [ & event] ) . await ?;
@@ -223,7 +226,8 @@ impl Folder {
223
226
& mut self ,
224
227
id : & SecretId ,
225
228
) -> Result < Option < WriteEvent > > {
226
- if let Some ( event) = self . keeper . delete_secret ( id) . await ? {
229
+ let mut access_point = self . access_point . lock ( ) . await ;
230
+ if let Some ( event) = access_point. delete_secret ( id) . await ? {
227
231
let mut events = self . events . write ( ) . await ;
228
232
events. apply ( vec ! [ & event] ) . await ?;
229
233
Ok ( Some ( event) )
@@ -237,7 +241,10 @@ impl Folder {
237
241
& mut self ,
238
242
name : impl AsRef < str > ,
239
243
) -> Result < WriteEvent > {
240
- self . keeper . set_vault_name ( name. as_ref ( ) . to_owned ( ) ) . await ?;
244
+ let mut access_point = self . access_point . lock ( ) . await ;
245
+ access_point
246
+ . set_vault_name ( name. as_ref ( ) . to_owned ( ) )
247
+ . await ?;
241
248
let event = WriteEvent :: SetVaultName ( name. as_ref ( ) . to_owned ( ) ) ;
242
249
let mut events = self . events . write ( ) . await ;
243
250
events. apply ( vec ! [ & event] ) . await ?;
@@ -249,7 +256,8 @@ impl Folder {
249
256
& mut self ,
250
257
flags : VaultFlags ,
251
258
) -> Result < WriteEvent > {
252
- self . keeper . set_vault_flags ( flags. clone ( ) ) . await ?;
259
+ let mut access_point = self . access_point . lock ( ) . await ;
260
+ access_point. set_vault_flags ( flags. clone ( ) ) . await ?;
253
261
let event = WriteEvent :: SetVaultFlags ( flags) ;
254
262
let mut events = self . events . write ( ) . await ;
255
263
events. apply ( vec ! [ & event] ) . await ?;
@@ -258,7 +266,8 @@ impl Folder {
258
266
259
267
/// Description of this folder.
260
268
pub async fn description ( & self ) -> Result < String > {
261
- let meta = self . keeper . vault_meta ( ) . await ?;
269
+ let access_point = self . access_point . lock ( ) . await ;
270
+ let meta = access_point. vault_meta ( ) . await ?;
262
271
Ok ( meta. description ( ) . to_owned ( ) )
263
272
}
264
273
@@ -267,14 +276,18 @@ impl Folder {
267
276
& mut self ,
268
277
description : impl AsRef < str > ,
269
278
) -> Result < WriteEvent > {
270
- let mut meta = self . keeper . vault_meta ( ) . await ?;
279
+ let mut meta = {
280
+ let access_point = self . access_point . lock ( ) . await ;
281
+ access_point. vault_meta ( ) . await ?
282
+ } ;
271
283
meta. set_description ( description. as_ref ( ) . to_owned ( ) ) ;
272
284
self . set_meta ( & meta) . await
273
285
}
274
286
275
287
/// Set the folder meta data.
276
288
pub async fn set_meta ( & mut self , meta : & VaultMeta ) -> Result < WriteEvent > {
277
- let event = self . keeper . set_vault_meta ( meta) . await ?;
289
+ let mut access_point = self . access_point . lock ( ) . await ;
290
+ let event = access_point. set_vault_meta ( meta) . await ?;
278
291
let mut events = self . events . write ( ) . await ;
279
292
events. apply ( vec ! [ & event] ) . await ?;
280
293
Ok ( event)
@@ -322,6 +335,8 @@ impl Folder {
322
335
323
336
impl From < Folder > for Vault {
324
337
fn from ( value : Folder ) -> Self {
325
- value. keeper . into ( )
338
+ let mutex = Arc :: into_inner ( value. access_point ) . unwrap ( ) ;
339
+ let access_point = mutex. into_inner ( ) ;
340
+ access_point. into ( )
326
341
}
327
342
}
0 commit comments