Skip to content

Commit

Permalink
Add metadata to snapshot preview list
Browse files Browse the repository at this point in the history
Resolves #35
  • Loading branch information
KyrietS committed Jul 5, 2023
1 parent c87eb44 commit 5049651
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/backup/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Snapshot {
let timestamp = Timestamp::parse_from(&snapshot_name)
.ok_or(format!("Invalid snapshot name: \"{}\"", snapshot_name))?;
let index = Index::open(location.join("index.txt"))?;
let files = Files::new(location.join("files"));
let files = Files::open(location.join("files"))?;

Ok(Snapshot {
location: location.to_owned(),
Expand Down Expand Up @@ -279,8 +279,8 @@ impl Snapshot {
impl Display for Snapshot {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Snapshot: {}", self.timestamp)?;
writeln!(f, " Index: ???")?;
writeln!(f, " Files: ???")?;
writeln!(f, " Index: {} entries", self.index.entries.len())?;
writeln!(f, " Files: {} bytes", self.files.size())?;
Ok(())
}
}
Expand Down
50 changes: 46 additions & 4 deletions src/backup/snapshot/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,49 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

pub struct Files {
root: PathBuf,
size: u64, // in bytes
}

impl Files {
pub fn new(location: PathBuf) -> Files {
if !location.exists() {
fs::create_dir(&location).expect("Error creating files dir");
}
Files { root: location }
Files {
root: location,
size: 0,
}
}

pub fn open(location: PathBuf) -> std::result::Result<Self, String> {
if !location.exists() {
return Err("Folder with files doesn't exist or isn't accessible".into());
}
let size = Self::get_size(location.as_path());
Ok(Files {
root: location,
size,
})
}

pub fn size(&self) -> u64 {
self.size
}

fn get_size(location: &Path) -> u64 {
let mut size = 0;
for entry in WalkDir::new(location).min_depth(1).follow_links(false) {
let entry = match entry {
Ok(entry) => entry,
Err(_) => continue,
};
let entry_meta = match entry.metadata() {
Ok(meta) => meta,
Err(_) => continue,
};
size += entry_meta.len();
}
size
}

pub fn check_integrity<'a>(
Expand Down Expand Up @@ -70,11 +105,11 @@ impl Files {
Ok(())
}

pub fn copy_entry(&self, entry: &Path) -> Result<PathBuf> {
pub fn copy_entry(&mut self, entry: &Path) -> Result<PathBuf> {
let entry_meta = entry.symlink_metadata()?;
let entry_type = entry_meta.file_type();

return if entry_type.is_dir() {
let result = if entry_type.is_dir() {
self.copy_dir_entry(entry)
} else if entry_type.is_file() {
self.copy_file_entry(entry)
Expand All @@ -90,6 +125,12 @@ impl Files {
} else {
Err(format!("Unknown entry type: {}", &entry.display()).into())
};

if result.is_ok() {
self.size += entry_meta.len();
}

result
}

fn copy_dir_entry(&self, dir_to_copy: &Path) -> Result<PathBuf> {
Expand Down Expand Up @@ -178,8 +219,9 @@ mod tests {
fn copy_files_from_invalid_path() {
let tempdir = tempfile::tempdir().unwrap();
let invalid_file = tempdir.path().join("foobar");
let files = Files {
let mut files = Files {
root: PathBuf::new(),
size: 0,
};

let result = files.copy_entry(&invalid_file);
Expand Down

0 comments on commit 5049651

Please sign in to comment.