|
| 1 | +use super::multi_level_acc::{AccData, DEFAULT_BACKUP_NAME, DEFAULT_NAME}; |
| 2 | +use crate::util; |
| 3 | +use anyhow::{Context, Result}; |
| 4 | +use std::fs::{self, File}; |
| 5 | +use std::io::Read; |
| 6 | +use std::path::Path; |
| 7 | + |
| 8 | +pub fn save_acc_data(dir: &str, index: i64, elems: Vec<Vec<u8>>, wits: Vec<Vec<u8>>) -> Result<()> { |
| 9 | + let data = AccData { values: elems, wits }; |
| 10 | + |
| 11 | + let jbytes = serde_json::to_vec(&data)?; |
| 12 | + |
| 13 | + let fpath = Path::new(dir).join(format!("{}-{}", DEFAULT_NAME, index)); |
| 14 | + |
| 15 | + util::save_file(&fpath, &jbytes) |
| 16 | +} |
| 17 | + |
| 18 | +pub fn read_acc_data(dir: &str, index: i64) -> Result<AccData> { |
| 19 | + let fpath = format!("{}/{}-{}", dir, DEFAULT_NAME, index); |
| 20 | + read_data(&fpath) |
| 21 | +} |
| 22 | +pub fn read_backup(dir: &str, index: i64) -> Result<AccData> { |
| 23 | + let fpath = format!("{}/{}-{}", dir, DEFAULT_BACKUP_NAME, index); |
| 24 | + read_data(&fpath) |
| 25 | +} |
| 26 | + |
| 27 | +pub fn read_data(fpath: &str) -> Result<AccData> { |
| 28 | + let mut file = File::open(fpath).context("read element data error")?; |
| 29 | + let mut data = Vec::new(); |
| 30 | + file.read_to_end(&mut data).context("read element data error")?; |
| 31 | + serde_json::from_slice(&data).context("read element data error") |
| 32 | +} |
| 33 | + |
| 34 | +// deleteAccData delete from the given index |
| 35 | +pub fn delete_acc_data(dir: &str, last: i32) -> Result<()> { |
| 36 | + let fs = fs::read_dir(dir).context("delete element data error")?; |
| 37 | + for entry in fs { |
| 38 | + let entry = entry.context("delete element data error")?; |
| 39 | + let path = entry.path(); |
| 40 | + if let Some(file_name) = path.file_name() { |
| 41 | + if let Some(file_name_str) = file_name.to_str() { |
| 42 | + if let Some(index_str) = file_name_str.rsplit('-').next() { |
| 43 | + if let Ok(index) = index_str.parse::<i32>() { |
| 44 | + if index <= last { |
| 45 | + fs::remove_file(path).context("delete element data error")?; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + Ok(()) |
| 53 | +} |
| 54 | + |
| 55 | +pub fn clean_backup(dir: &str, index: i64) -> Result<()> { |
| 56 | + let backup = format!("{}/{}-{}", dir, DEFAULT_BACKUP_NAME, index); |
| 57 | + fs::remove_file(backup).context("clean backup error") |
| 58 | +} |
| 59 | + |
| 60 | +pub fn backup_acc_data(dir: &str, index: i64) -> Result<()> { |
| 61 | + let fpath = format!("{}/{}-{}", dir, DEFAULT_NAME, index); |
| 62 | + let backup = format!("{}/{}-{}", dir, DEFAULT_BACKUP_NAME, index); |
| 63 | + util::copy_file(&fpath, &backup).context("backup element data error") |
| 64 | +} |
| 65 | + |
| 66 | +pub fn backup_acc_data_for_chall(src: &str, des: &str, index: i64) -> Result<()> { |
| 67 | + let fpath = Path::new(src).join(format!("{}-{}", DEFAULT_NAME, index)); |
| 68 | + let backup = Path::new(des).join(format!("{}-{}", DEFAULT_NAME, index)); |
| 69 | + util::copy_file(fpath.to_str().unwrap(), backup.to_str().unwrap()) |
| 70 | + .context("backup acc data for challenge error")?; |
| 71 | + Ok(()) |
| 72 | +} |
| 73 | + |
| 74 | +pub fn recovery_acc_data(dir: &str, index: i64) -> Result<()> { |
| 75 | + let backup = Path::join(Path::new(dir), &format!("{}-{}", DEFAULT_BACKUP_NAME, index)); |
| 76 | + let fpath = Path::join(Path::new(dir), &format!("{}-{}", DEFAULT_NAME, index)); |
| 77 | + |
| 78 | + if !backup.exists() { |
| 79 | + return Ok(()); |
| 80 | + } |
| 81 | + |
| 82 | + fs::rename(&backup, &fpath).context("recovery acc data error")?; |
| 83 | + Ok(()) |
| 84 | +} |
0 commit comments