Skip to content

Commit 9386fa1

Browse files
author
valdok
committed
migrate_file_from_2_17_safe: producing backup, printing detailed info
1 parent 1a033e7 commit 9386fa1

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

cosmwasm/enclaves/shared/utils/src/storage.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::results::UnwrapOrSgxErrorUnexpected;
22

33
use core::mem;
44
use core::ptr::null;
5+
use log::{error, info};
56
use std::io::{Read, Write};
67
use std::mem::*;
78
use std::path::Path;
@@ -98,10 +99,10 @@ pub struct file_md {
9899
}
99100

100101
pub fn unseal_file_from_2_17(
101-
sPath: &str,
102+
s_path: &str,
102103
should_check_fname: bool,
103104
) -> Result<Vec<u8>, sgx_status_t> {
104-
let mut file = match File::open(sPath) {
105+
let mut file = match File::open(s_path) {
105106
Ok(f) => f,
106107
Err(e) => {
107108
return Err(/*e*/ sgx_status_t::SGX_ERROR_UNEXPECTED);
@@ -178,7 +179,7 @@ pub fn unseal_file_from_2_17(
178179
bytes.copy_from_slice(slice::from_raw_parts(md_decr.data.as_ptr(), ret_size));
179180

180181
if should_check_fname {
181-
let raw_path = sPath.as_bytes();
182+
let raw_path = s_path.as_bytes();
182183

183184
let mut fname0: usize = 0;
184185
for i in 0..raw_path.len() {
@@ -212,20 +213,35 @@ pub fn unseal_file_from_2_17(
212213
}
213214

214215
pub fn migrate_file_from_2_17_safe(
215-
sPath: &str,
216+
s_path: &str,
216217
should_check_fname: bool,
217218
) -> Result<(), sgx_status_t> {
218-
if Path::new(sPath).exists() {
219-
let data = match unseal_file_from_2_17(sPath, should_check_fname) {
219+
if Path::new(s_path).exists() {
220+
221+
let data = match unseal_file_from_2_17(s_path, should_check_fname) {
220222
Ok(x) => x,
221223
Err(e) => {
224+
error!("Couldn't unseal file {}, {}", s_path, e);
222225
return Err(e);
223226
}
224227
};
228+
229+
let s_path_bkp = s_path.to_string() + ".bkp";
230+
if let Err(e) = fs::copy(&s_path, &s_path_bkp) {
231+
error!("Couldn't backup {} into {}, {}", s_path, s_path_bkp, e);
232+
return Err(sgx_status_t::SGX_ERROR_UNEXPECTED);
233+
}
225234

226-
if let Err(e) = seal(data.as_slice(), sPath) {
235+
if let Err(e) = seal(data.as_slice(), s_path) {
236+
error!("Couldn't RE-seal file {}, {}", s_path, e);
227237
return Err(e);
228238
}
239+
240+
info!("File {} successfully RE-sealed", s_path);
241+
}
242+
else
243+
{
244+
info!("File {} doesn't exist, skipping", s_path);
229245
}
230246

231247
Ok(())

0 commit comments

Comments
 (0)