backup/datastore: create_backup_dir: show if path existed
To enable asserting the creation of a new backup rather than adding to an existing one. Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
		@ -81,7 +81,7 @@ fn upload_catar(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    let datastore = DataStore::lookup_datastore(store)?;
 | 
					    let datastore = DataStore::lookup_datastore(store)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut path = datastore.create_backup_dir(backup_type, backup_id, backup_time)?;
 | 
					    let (mut path, _new) = datastore.create_backup_dir(backup_type, backup_id, backup_time)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    path.push(archive_name);
 | 
					    path.push(archive_name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,7 @@ use failure::*;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
use chrono::prelude::*;
 | 
					use chrono::prelude::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use std::io;
 | 
				
			||||||
use std::path::{PathBuf, Path};
 | 
					use std::path::{PathBuf, Path};
 | 
				
			||||||
use std::collections::HashMap;
 | 
					use std::collections::HashMap;
 | 
				
			||||||
use lazy_static::lazy_static;
 | 
					use lazy_static::lazy_static;
 | 
				
			||||||
@ -155,27 +156,31 @@ impl DataStore {
 | 
				
			|||||||
        backup_type: &str,
 | 
					        backup_type: &str,
 | 
				
			||||||
        backup_id: &str,
 | 
					        backup_id: &str,
 | 
				
			||||||
        backup_time: i64,
 | 
					        backup_time: i64,
 | 
				
			||||||
    ) ->  Result<PathBuf, Error> {
 | 
					    ) ->  Result<(PathBuf, bool), io::Error> {
 | 
				
			||||||
        let mut relative_path = PathBuf::new();
 | 
					        let mut relative_path = PathBuf::new();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        relative_path.push(backup_type);
 | 
					        relative_path.push(backup_type);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        relative_path.push(backup_id);
 | 
					        relative_path.push(backup_id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // create intermediate path first:
 | 
				
			||||||
 | 
					        let mut full_path = self.base_path();
 | 
				
			||||||
 | 
					        full_path.push(&relative_path);
 | 
				
			||||||
 | 
					        std::fs::create_dir_all(&full_path)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let dt = Utc.timestamp(backup_time, 0);
 | 
					        let dt = Utc.timestamp(backup_time, 0);
 | 
				
			||||||
        let date_str = dt.format("%Y-%m-%dT%H:%M:%S").to_string();
 | 
					        let date_str = dt.format("%Y-%m-%dT%H:%M:%S").to_string();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        println!("date: {}", date_str);
 | 
					        println!("date: {}", date_str);
 | 
				
			||||||
 | 
					 | 
				
			||||||
        relative_path.push(&date_str);
 | 
					        relative_path.push(&date_str);
 | 
				
			||||||
 | 
					        full_path.push(&date_str);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // create the last component now
 | 
				
			||||||
        let mut full_path = self.base_path();
 | 
					        match std::fs::create_dir(&full_path) {
 | 
				
			||||||
        full_path.push(&relative_path);
 | 
					            Ok(_) => Ok((relative_path, true)),
 | 
				
			||||||
 | 
					            Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok((relative_path, false)),
 | 
				
			||||||
        std::fs::create_dir_all(&full_path)?;
 | 
					            Err(e) => Err(e)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        Ok(relative_path)
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn list_backups(&self) -> Result<Vec<BackupInfo>, Error> {
 | 
					    pub fn list_backups(&self) -> Result<Vec<BackupInfo>, Error> {
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user