1+ #![ cfg( not( target_arch = "wasm32" ) ) ]
12use std:: { path:: Path , sync:: Arc } ;
23
34use crate :: {
@@ -8,6 +9,11 @@ use crate::{
89use super :: { Error , Result } ;
910#[ cfg( test) ]
1011use mockall:: automock;
12+ use tokio:: {
13+ fs:: File ,
14+ io:: { AsyncReadExt , AsyncWriteExt } ,
15+ sync:: watch,
16+ } ;
1117
1218/// Allows to backup and restore the database as an encrypted file.
1319#[ cfg_attr( test, automock) ]
@@ -25,18 +31,21 @@ pub struct BackupService {
2531 store : Arc < dyn BackupStoreApi > ,
2632 identity_store : Arc < dyn IdentityStoreApi > ,
2733 surreal_db_config : SurrealDbConfig ,
34+ reboot_sender : watch:: Sender < bool > ,
2835}
2936
3037impl BackupService {
3138 pub fn new (
3239 store : Arc < dyn BackupStoreApi > ,
3340 identity_store : Arc < dyn IdentityStoreApi > ,
3441 surreal_db_config : SurrealDbConfig ,
42+ reboot_sender : watch:: Sender < bool > ,
3543 ) -> Self {
3644 Self {
3745 store,
3846 identity_store,
3947 surreal_db_config,
48+ reboot_sender,
4049 }
4150 }
4251
@@ -66,21 +75,23 @@ impl BackupServiceApi for BackupService {
6675 }
6776
6877 async fn restore ( & self , file_path : & Path ) -> Result < ( ) > {
69- // TODO WASM: implement without file system access
70- let _private_key = self
78+ let private_key = self
7179 . identity_store
7280 . get_key_pair ( )
7381 . await ?
7482 . get_private_key_string ( ) ;
75- let _buffer : Vec < u8 > = vec ! [ ] ;
76- // let mut file = File::open(file_path).await?;
77- // file.read_to_end(&mut buffer).await?;
78- // let decrypted_bytes = util::crypto::decrypt_ecies(&buffer, &private_key)?;
83+ let mut buffer = vec ! [ ] ;
84+ let mut file = File :: open ( file_path) . await ?;
85+ file. read_to_end ( & mut buffer) . await ?;
86+ let decrypted_bytes = util:: crypto:: decrypt_ecies ( & buffer, & private_key) ?;
7987 let out_path = file_path. with_file_name ( "restore.surql" ) ;
80- // let mut out = File::create(out_path.as_path()).await?;
81- // out.write_all(&decrypted_bytes).await?;
88+ let mut out = File :: create ( out_path. as_path ( ) ) . await ?;
89+ out. write_all ( & decrypted_bytes) . await ?;
8290 self . store . drop_db ( & self . surreal_db_config . database ) . await ?;
8391 self . store . restore ( out_path. as_path ( ) ) . await ?;
92+ self . reboot_sender
93+ . send ( true )
94+ . expect ( "Can initiate a reboot" ) ;
8495 Ok ( ( ) )
8596 }
8697}
@@ -117,8 +128,13 @@ mod tests {
117128 . returning ( || Ok ( vec ! [ 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 ] ) )
118129 . once ( ) ;
119130
120- let service =
121- BackupService :: new ( Arc :: new ( store) , Arc :: new ( identity_store) , surreal_db_config) ;
131+ let ( tx, _) = watch:: channel ( false ) ;
132+ let service = BackupService :: new (
133+ Arc :: new ( store) ,
134+ Arc :: new ( identity_store) ,
135+ surreal_db_config,
136+ tx,
137+ ) ;
122138
123139 let result = service. backup ( ) . await ;
124140 assert ! ( result. is_ok( ) ) ;
@@ -136,9 +152,13 @@ mod tests {
136152
137153 identity_store. expect_get_key_pair ( ) . never ( ) ;
138154 store. expect_backup ( ) . never ( ) ;
139-
140- let service =
141- BackupService :: new ( Arc :: new ( store) , Arc :: new ( identity_store) , surreal_db_config) ;
155+ let ( tx, _) = watch:: channel ( false ) ;
156+ let service = BackupService :: new (
157+ Arc :: new ( store) ,
158+ Arc :: new ( identity_store) ,
159+ surreal_db_config,
160+ tx,
161+ ) ;
142162
143163 let result = service. backup ( ) . await ;
144164 assert ! ( result. is_err( ) ) ;
@@ -173,7 +193,7 @@ DEFINE TABLE bill_chain TYPE ANY SCHEMALESS PERMISSIONS NONE;";
173193
174194 let temp_dir = env:: temp_dir ( ) ;
175195 let file_path = temp_dir. join ( "test.surql" ) ;
176- let mut test_file = tokio :: fs :: File :: create ( file_path. as_path ( ) ) . await . unwrap ( ) ;
196+ let mut test_file = File :: create ( file_path. as_path ( ) ) . await . unwrap ( ) ;
177197 test_file. write_all ( & encrypted_bytes) . await . unwrap ( ) ;
178198
179199 identity_store
@@ -189,10 +209,18 @@ DEFINE TABLE bill_chain TYPE ANY SCHEMALESS PERMISSIONS NONE;";
189209
190210 store. expect_restore ( ) . returning ( |_| Ok ( ( ) ) ) . once ( ) ;
191211
192- let service =
193- BackupService :: new ( Arc :: new ( store) , Arc :: new ( identity_store) , surreal_db_config) ;
212+ let ( tx, mut rx) = watch:: channel ( false ) ;
213+ let service = BackupService :: new (
214+ Arc :: new ( store) ,
215+ Arc :: new ( identity_store) ,
216+ surreal_db_config,
217+ tx,
218+ ) ;
194219
195220 let result = service. restore ( & temp_dir. join ( "test.surql" ) ) . await ;
196221 assert ! ( result. is_ok( ) ) ;
222+
223+ let should_reboot = * rx. borrow_and_update ( ) ;
224+ assert ! ( should_reboot) ;
197225 }
198226}
0 commit comments