src/backup/datastore.rs: protect BackupDir fields, impl new()

This commit is contained in:
Dietmar Maurer 2019-03-04 13:51:36 +01:00
parent 1e9a94e579
commit 9b492eb256
4 changed files with 34 additions and 34 deletions

View File

@ -24,7 +24,7 @@ fn group_backups(backup_list: Vec<BackupInfo>) -> HashMap<String, Vec<BackupInfo
let mut group_hash = HashMap::new(); let mut group_hash = HashMap::new();
for info in backup_list { for info in backup_list {
let group_id = info.backup_dir.group.group_path().to_str().unwrap().to_owned(); let group_id = info.backup_dir.group().group_path().to_str().unwrap().to_owned();
let time_list = group_hash.entry(group_id).or_insert(vec![]); let time_list = group_hash.entry(group_id).or_insert(vec![]);
time_list.push(info); time_list.push(info);
} }
@ -40,7 +40,7 @@ fn mark_selections<F: Fn(DateTime<Local>, &BackupInfo) -> String> (
){ ){
let mut hash = HashSet::new(); let mut hash = HashSet::new();
for info in list { for info in list {
let local_time = info.backup_dir.backup_time.with_timezone(&Local); let local_time = info.backup_dir.backup_time().with_timezone(&Local);
if hash.len() >= keep as usize { break; } if hash.len() >= keep as usize { break; }
let backup_id = info.backup_dir.relative_path(); let backup_id = info.backup_dir.relative_path();
let sel_id: String = select_id(local_time, &info); let sel_id: String = select_id(local_time, &info);
@ -70,15 +70,15 @@ fn list_groups(
for (_group_id, mut list) in group_hash { for (_group_id, mut list) in group_hash {
list.sort_unstable_by(|a, b| b.backup_dir.backup_time.cmp(&a.backup_dir.backup_time)); // new backups first list.sort_unstable_by(|a, b| b.backup_dir.backup_time().cmp(&a.backup_dir.backup_time())); // new backups first
let info = &list[0]; let info = &list[0];
let group = &info.backup_dir.group; let group = info.backup_dir.group();
groups.push(json!({ groups.push(json!({
"backup-type": group.backup_type(), "backup-type": group.backup_type(),
"backup-id": group.backup_id(), "backup-id": group.backup_id(),
"last-backup": info.backup_dir.backup_time.timestamp(), "last-backup": info.backup_dir.backup_time().timestamp(),
"backup-count": list.len() as u64, "backup-count": list.len() as u64,
"files": info.files, "files": info.files,
})); }));
@ -99,10 +99,7 @@ fn delete_snapshots (
let backup_time = tools::required_integer_param(&param, "backup-time")?; let backup_time = tools::required_integer_param(&param, "backup-time")?;
let backup_time = Local.timestamp(backup_time, 0); let backup_time = Local.timestamp(backup_time, 0);
let snapshot = BackupDir { let snapshot = BackupDir::new(BackupGroup::new(backup_type, backup_id), backup_time);
group: BackupGroup::new(backup_type, backup_id),
backup_time,
};
let datastore = DataStore::lookup_datastore(store)?; let datastore = DataStore::lookup_datastore(store)?;
@ -134,7 +131,7 @@ fn list_snapshots (
let group_snapshots = match group_hash.get_mut(&group_id) { let group_snapshots = match group_hash.get_mut(&group_id) {
Some(data) => { Some(data) => {
// new backups first // new backups first
data.sort_unstable_by(|a, b| b.backup_dir.backup_time.cmp(&a.backup_dir.backup_time)); data.sort_unstable_by(|a, b| b.backup_dir.backup_time().cmp(&a.backup_dir.backup_time()));
data data
} }
None => bail!("Backup group '{}' does not exists.", group_id), None => bail!("Backup group '{}' does not exists.", group_id),
@ -144,12 +141,12 @@ fn list_snapshots (
for info in group_snapshots { for info in group_snapshots {
let group = &info.backup_dir.group; let group = info.backup_dir.group();
snapshots.push(json!({ snapshots.push(json!({
"backup-type": group.backup_type(), "backup-type": group.backup_type(),
"backup-id": group.backup_id(), "backup-id": group.backup_id(),
"backup-time": info.backup_dir.backup_time.timestamp(), "backup-time": info.backup_dir.backup_time().timestamp(),
"files": info.files, "files": info.files,
})); }));
} }
@ -177,7 +174,7 @@ fn prune(
let mut mark = HashSet::new(); let mut mark = HashSet::new();
list.sort_unstable_by(|a, b| b.backup_dir.backup_time.cmp(&a.backup_dir.backup_time)); // new backups first list.sort_unstable_by(|a, b| b.backup_dir.backup_time().cmp(&a.backup_dir.backup_time())); // new backups first
if let Some(keep_last) = param["keep-last"].as_u64() { if let Some(keep_last) = param["keep-last"].as_u64() {
list.iter().take(keep_last as usize).for_each(|info| { list.iter().take(keep_last as usize).for_each(|info| {
@ -212,7 +209,7 @@ fn prune(
let mut remove_list: Vec<&BackupInfo> = list.iter() let mut remove_list: Vec<&BackupInfo> = list.iter()
.filter(|info| !mark.contains(&info.backup_dir.relative_path())).collect(); .filter(|info| !mark.contains(&info.backup_dir.relative_path())).collect();
remove_list.sort_unstable_by(|a, b| a.backup_dir.backup_time.cmp(&b.backup_dir.backup_time)); // oldest backups first remove_list.sort_unstable_by(|a, b| a.backup_dir.backup_time().cmp(&b.backup_dir.backup_time())); // oldest backups first
for info in remove_list { for info in remove_list {
datastore.remove_backup_dir(&info.backup_dir)?; datastore.remove_backup_dir(&info.backup_dir)?;
@ -329,9 +326,9 @@ fn get_backup_list(
for info in datastore.list_backups()? { for info in datastore.list_backups()? {
list.push(json!({ list.push(json!({
"backup-type": info.backup_dir.group.backup_type(), "backup-type": info.backup_dir.group().backup_type(),
"backup-id": info.backup_dir.group.backup_id(), "backup-id": info.backup_dir.group().backup_id(),
"backup-time": info.backup_dir.backup_time.timestamp(), "backup-time": info.backup_dir.backup_time().timestamp(),
"files": info.files, "files": info.files,
})); }));
} }

View File

@ -145,10 +145,7 @@ fn download_catar(
let datastore = DataStore::lookup_datastore(store)?; let datastore = DataStore::lookup_datastore(store)?;
let backup_dir = BackupDir { let backup_dir = BackupDir::new(BackupGroup::new(backup_type, backup_id), backup_time);
group: BackupGroup::new(backup_type, backup_id),
backup_time,
};
let mut path = backup_dir.relative_path(); let mut path = backup_dir.relative_path();

View File

@ -77,13 +77,25 @@ impl BackupGroup {
#[derive(Debug)] #[derive(Debug)]
pub struct BackupDir { pub struct BackupDir {
/// Backup group /// Backup group
pub group: BackupGroup, group: BackupGroup,
/// Backup timestamp /// Backup timestamp
pub backup_time: DateTime<Local>, backup_time: DateTime<Local>,
} }
impl BackupDir { impl BackupDir {
pub fn new(group: BackupGroup, backup_time: DateTime<Local>) -> Self {
Self { group, backup_time }
}
pub fn group(&self) -> &BackupGroup {
&self.group
}
pub fn backup_time(&self) -> DateTime<Local> {
self.backup_time
}
pub fn parse(path: &str) -> Result<Self, Error> { pub fn parse(path: &str) -> Result<Self, Error> {
let cap = SNAPSHOT_PATH_REGEX.captures(path) let cap = SNAPSHOT_PATH_REGEX.captures(path)

View File

@ -112,10 +112,7 @@ fn list_backups(
let epoch = item["backup-time"].as_i64().unwrap(); let epoch = item["backup-time"].as_i64().unwrap();
let backup_time = Local.timestamp(epoch, 0); let backup_time = Local.timestamp(epoch, 0);
let backup_dir = BackupDir { let backup_dir = BackupDir::new(BackupGroup::new(btype, id), backup_time);
group: BackupGroup::new(btype, id),
backup_time
};
let files = item["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_owned()).collect(); let files = item["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_owned()).collect();
@ -208,10 +205,7 @@ fn list_snapshots(
let epoch = item["backup-time"].as_i64().unwrap(); let epoch = item["backup-time"].as_i64().unwrap();
let backup_time = Local.timestamp(epoch, 0); let backup_time = Local.timestamp(epoch, 0);
let snapshot = BackupDir { let snapshot = BackupDir::new(BackupGroup::new(btype, id), backup_time);
group: BackupGroup::new(btype, id),
backup_time,
};
let path = snapshot.relative_path().to_str().unwrap().to_owned(); let path = snapshot.relative_path().to_str().unwrap().to_owned();
@ -239,9 +233,9 @@ fn forget_snapshots(
let snapshot = BackupDir::parse(path)?; let snapshot = BackupDir::parse(path)?;
let query = tools::json_object_to_query(json!({ let query = tools::json_object_to_query(json!({
"backup-type": snapshot.group.backup_type(), "backup-type": snapshot.group().backup_type(),
"backup-id": snapshot.group.backup_id(), "backup-id": snapshot.group().backup_id(),
"backup-time": snapshot.backup_time.timestamp(), "backup-time": snapshot.backup_time().timestamp(),
}))?; }))?;
let mut client = HttpClient::new(&repo.host, &repo.user); let mut client = HttpClient::new(&repo.host, &repo.user);