use std::path::PathBuf;
use std::sync::{Arc, Mutex};
pub struct PluginTempFileManager {
pub managed_files: Arc<Mutex<Vec<PathBuf>>>,
}
impl PluginTempFileManager {
pub fn new() -> Self {
Self {
managed_files: Arc::new(Mutex::new(Vec::new())),
}
}
pub fn cleanup_all_managed_files(&self) {
let mut files = match self.managed_files.lock() {
Ok(guard) => guard,
Err(poisoned) => {
eprintln!("Mutex was poisoned during cleanup: {:?}", poisoned);
poisoned.into_inner()
}
};
let mut errors = Vec::new();
for path in files.drain(..) {
if let Err(e) = std::fs::remove_file(&path) {
errors.push(format!("Failed to delete file {}: {}", path.display(), e));
}
}
if !errors.is_empty() {
eprintln!("Errors during cleanup: {:?}", errors);
}
}
}