Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
edits
Browse files Browse the repository at this point in the history
  • Loading branch information
marknefedov committed Apr 24, 2023
1 parent 6ac1679 commit 3333e34
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions date-blacklist-deleter/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::{DateTime, Datelike, Duration, NaiveDate, Utc};
use chrono::{DateTime, Datelike, Duration, Utc};
use indicatif::{ProgressBar, ProgressStyle};
use rayon::prelude::*;
use std::collections::HashSet;
Expand All @@ -11,23 +11,31 @@ fn main() -> io::Result<()> {
let args: Vec<String> = env::args().collect();
if args.len() != 4 {
eprintln!(
"Usage: {} <directory_path> <months> <do_not_delete_list_file>",
"Usage: {} <directory_path> <cutoff_date> <do_not_delete_list_file>",
args[0]
);
std::process::exit(1);
}

let folder_path = &args[1];
let months = args[2].parse::<i64>().expect("Invalid number of months");
let cutoff_date = DateTime::parse_from_rfc3339(&args[2])
.expect("Invalid cutoff date")
.with_timezone(&Utc);
let do_not_delete_list_file = &args[3];

let do_not_delete_set = load_do_not_delete_set(do_not_delete_list_file)?;

let pb = ProgressBar::new_spinner();
pb.set_style(
ProgressStyle::default_spinner()
.template("{spinner} Deleting files...")
.template("Deleting {spinner} {prefix} files...")
.unwrap(),
);
delete_old_files(folder_path, months, &pb, &do_not_delete_set)?;

delete_old_files(folder_path, cutoff_date, &pb, &do_not_delete_set)?;

pb.finish_with_message("Deletion complete.");

Ok(())
}

Expand All @@ -41,28 +49,29 @@ fn load_do_not_delete_set<P: AsRef<Path>>(file_path: P) -> io::Result<HashSet<Pa
let path = PathBuf::from(line.trim());
do_not_delete_set.insert(path);
}

Ok(do_not_delete_set)
}

fn delete_old_files<P: AsRef<Path>>(
folder_path: P,
months: i64,
cutoff_date: DateTime<Utc>,
pb: &ProgressBar,
do_not_delete_set: &HashSet<PathBuf>,
) -> io::Result<()> {
let entries: Vec<_> = fs::read_dir(folder_path)?.collect();
let now = Utc::now();
let duration = Duration::days(30 * months);
entries.into_par_iter().for_each(|entry| {
pb.inc_length(1);
pb.tick();

let entry = match entry {
Ok(e) => e,
Err(e) => {
eprintln!("Error reading directory entry: {}", e);
return;
}
};

let path = entry.path();
let metadata = match entry.metadata() {
Ok(m) => m,
Expand All @@ -71,6 +80,7 @@ fn delete_old_files<P: AsRef<Path>>(
return;
}
};

if metadata.is_file() && !do_not_delete_set.contains(&path) {
let modified_time = match metadata.modified() {
Ok(t) => t,
Expand All @@ -79,19 +89,22 @@ fn delete_old_files<P: AsRef<Path>>(
return;
}
};

let datetime: DateTime<Utc> = modified_time.into();
if now.signed_duration_since(datetime) > duration {
if datetime < cutoff_date {
if let Err(e) = fs::remove_file(&path) {
eprintln!("Error deleting file: {}", e);
}
}
} else if metadata.is_dir() {
if let Err(e) = delete_old_files(path, months, pb, do_not_delete_set) {
if let Err(e) = delete_old_files(path, cutoff_date, pb, do_not_delete_set) {
eprintln!("Error processing folder: {}", e);
}
}

pb.inc(1);
pb.tick();
});

Ok(())
}

0 comments on commit 3333e34

Please sign in to comment.