diff options
Diffstat (limited to 'src/backup.rs')
-rw-r--r-- | src/backup.rs | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/src/backup.rs b/src/backup.rs index 69bc2ea..8cc94f1 100644 --- a/src/backup.rs +++ b/src/backup.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | fs::{create_dir_all, File, OpenOptions}, | 2 | fs::{create_dir_all, File}, |
3 | io::{ErrorKind, Read, Write}, | 3 | io::{Read, Write}, |
4 | path::PathBuf, | 4 | path::PathBuf, |
5 | time::{SystemTime, UNIX_EPOCH}, | 5 | time::{SystemTime, UNIX_EPOCH}, |
6 | }; | 6 | }; |
@@ -43,7 +43,7 @@ impl Backup { | |||
43 | 43 | ||
44 | pub fn save(&self, config: &Config) -> Result<()> { | 44 | pub fn save(&self, config: &Config) -> Result<()> { |
45 | let rel_location = format!( | 45 | let rel_location = format!( |
46 | "bu_{}_{}", | 46 | "{}_{}", |
47 | gethostname() | 47 | gethostname() |
48 | .into_string() | 48 | .into_string() |
49 | .map_err(|_| Error::InvalidOsString)?, | 49 | .map_err(|_| Error::InvalidOsString)?, |
@@ -66,17 +66,35 @@ impl Backup { | |||
66 | Ok(()) | 66 | Ok(()) |
67 | } | 67 | } |
68 | 68 | ||
69 | pub fn get(config: &Config, _id: Option<BackupId>) -> Result<()> { | 69 | pub fn get_index(config: &Config, id: Option<BackupId>) -> Result<Self> { |
70 | let backup_index_root = format!("{}/index.json", config.root); | 70 | let backup_index_root = format!("{}/index.json", config.root); |
71 | let mut file = File::open(backup_index_root)?; | 71 | let list: Vec<BackupLocation> = Self::get_json_content(&backup_index_root)?; |
72 | let mut content = String::new(); | ||
73 | file.read_to_string(&mut content)?; | ||
74 | let list: Vec<BackupLocation> = serde_json::from_str(&content)?; | ||
75 | println!("{list:#?}"); | 72 | println!("{list:#?}"); |
76 | 73 | ||
77 | todo!(); | 74 | let index_loc = if let Some(id) = id { |
75 | list.iter() | ||
76 | .find(|bl| bl.id == id) | ||
77 | .ok_or(Error::BackupNotFound)? | ||
78 | .rel_location | ||
79 | .clone() | ||
80 | } else { | ||
81 | list.last() | ||
82 | .ok_or(Error::BackupNotFound)? | ||
83 | .rel_location | ||
84 | .clone() | ||
85 | }; | ||
78 | 86 | ||
79 | Ok(()) | 87 | let path = format!("{}/{index_loc}/index.json", config.root); |
88 | let index_file: Self = Self::get_json_content(&path)?; | ||
89 | |||
90 | Ok(index_file) | ||
91 | } | ||
92 | |||
93 | fn get_json_content<T: for<'a> Deserialize<'a>>(path: &str) -> Result<T> { | ||
94 | let mut file = File::open(path)?; | ||
95 | let mut content = String::new(); | ||
96 | file.read_to_string(&mut content)?; | ||
97 | Ok(serde_json::from_str(&content)?) | ||
80 | } | 98 | } |
81 | 99 | ||
82 | fn append_to_root_index(config: &Config, new_backup: BackupLocation) -> Result<()> { | 100 | fn append_to_root_index(config: &Config, new_backup: BackupLocation) -> Result<()> { |