Skip to content

Commit

Permalink
Fix: Bugs in cleanup_image_entries (#26)
Browse files Browse the repository at this point in the history
* fix: don't skip first item and infinite loop when remove skipped (if root_dir is symlink)
---------

Co-authored-by: Alexander Sidorov <[email protected]>
  • Loading branch information
mr-bit and mr-bit authored Mar 22, 2024
1 parent 2f7c021 commit 5bd15cf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["easymikey <[email protected]>"]
edition = "2021"
name = "lxc-tool"
version = "0.1.11"
version = "0.1.12"

[profile.release]
lto = true
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub struct Repodata {
// Image files to be desired in host_root_dir
pub image_files: ImageFiles,
// Numbers of containers to backup
pub number_of_container_to_backup: i16,
pub number_of_container_to_backup: usize,
// Timeout to the post_script or post process (maybe in the image metadata) that will run after the image is loaded
pub patcher_timeout: Timeout,
// Directory for temporary files. Must be on the same FS as host_root_dir
Expand Down
33 changes: 18 additions & 15 deletions src/repodata/lxc_image_entries_cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,46 @@ use std::{collections::HashMap, fs, path::PathBuf, time::Duration};

pub fn cleanup_image_entries(
root_dir: &PathBuf,

Check failure on line 8 in src/repodata/lxc_image_entries_cleanup.rs

View workflow job for this annotation

GitHub Actions / lint

writing `&PathBuf` instead of `&Path` involves a new object where a slice will do
number_of_container_to_backup: i16,
number_of_container_to_backup: usize,
image_entries: Vec<(LXCImageMetadata, Duration)>,
) -> Result<()> {
info!("Cleanup LXC images started.");

let root_dir = root_dir.canonicalize()?;
let hashed_image_entries: HashMap<_, Vec<_>> =
image_entries
.into_iter()
.fold(HashMap::new(), |mut acc, (image_meta, mtime)| {
acc.entry((
image_meta.dist.clone(),
image_meta.release.clone(),
image_meta.arch.clone(),
image_meta.type_.clone(),
))
.and_modify(|entries| {
entries.push((image_meta.path.to_owned(), mtime.to_owned()));
})
.or_insert(Vec::new());
let image_entries = acc
.entry((
image_meta.dist,
image_meta.release,
image_meta.arch,
image_meta.type_,
))
.or_default();
image_entries.push((image_meta.path, mtime));

acc
});

for mut image_entries in hashed_image_entries.into_values() {
if image_entries.len() <= number_of_container_to_backup {
continue;
}

let number_of_container_to_remove = image_entries.len() - number_of_container_to_backup;
image_entries.sort_by(|a, b| a.1.cmp(&b.1));

info!("cleanup_image_entries: {:#?}", image_entries);

while image_entries.len() > number_of_container_to_backup as usize {
let (removed_dir, _) = &image_entries[0];
if removed_dir.canonicalize()?.starts_with(root_dir) {
for (removed_dir, _) in image_entries.iter().take(number_of_container_to_remove) {
if removed_dir.canonicalize()?.starts_with(&root_dir) {
fs::remove_dir_all(removed_dir)?;
info!(
"Remove LXC image directory. Directory path: {:?}",
removed_dir
);
image_entries.remove(0);
}
}
}
Expand Down

0 comments on commit 5bd15cf

Please sign in to comment.