delete v0.1
This commit is contained in:
@ -4,6 +4,7 @@ use std::path::Path;
|
||||
use rayon::prelude::*;
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
|
||||
|
||||
use super::get_var_path;
|
||||
use crate::utils::parser;
|
||||
|
||||
@ -78,35 +79,50 @@ pub fn delete_recursive(repo: &str, pkgname: &str) {
|
||||
Ok(()) => {}
|
||||
Err(_) => {}
|
||||
}
|
||||
match remove_unused_dirs_and_symlink(&dir_path) {
|
||||
Ok(()) => {}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn remove_unused_files(dir: &Path) -> Result<(), String> {
|
||||
let entries = fs::read_dir(dir)
|
||||
.map_err(|e| format!("Can't read {}: {}", dir.display(), e))?;
|
||||
fn remove_unused_files(path: &Path) -> io::Result<()> {
|
||||
let metadata = fs::symlink_metadata(path)?;
|
||||
|
||||
entries.par_bridge().for_each(|entry| {
|
||||
if let Ok(entry) = entry {
|
||||
let path = entry.path();
|
||||
if path.is_file() {
|
||||
if let Ok(nlink) = get_nlink(&path) {
|
||||
if nlink == 1 {
|
||||
if let Err(e) = fs::remove_file(&path) {
|
||||
eprintln!("Can't remove {}: {}", path.display(), e)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if path.is_dir() {
|
||||
if let Err(e) = remove_unused_files(&path) { eprintln!("Error processing directory {}: {}", path.display(), e) }
|
||||
} else if path.is_symlink() {
|
||||
if fs::read_link(&path).is_err() {
|
||||
if let Err(e) = fs::remove_file(&path) { eprintln!("Can't remove symlink {}: {}", path.display(), e) }
|
||||
}
|
||||
}
|
||||
if metadata.file_type().is_file() {
|
||||
if get_nlink(path)? == 1 {
|
||||
fs::remove_file(path)?
|
||||
}
|
||||
});
|
||||
} else if metadata.file_type().is_dir() {
|
||||
let entries: Vec<_> = fs::read_dir(path)?.collect::<io::Result<Vec<_>>>()?;
|
||||
entries.par_iter().try_for_each(|entry| {
|
||||
remove_unused_files(&entry.path())
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn remove_unused_dirs_and_symlink(path: &Path) -> io::Result<()> {
|
||||
let metadata = fs::symlink_metadata(path)?;
|
||||
|
||||
if metadata.file_type().is_dir() {
|
||||
let entries: Vec<_> = fs::read_dir(path)?.collect::<io::Result<Vec<_>>>()?;
|
||||
entries.par_iter().try_for_each(|entry| {
|
||||
remove_unused_dirs_and_symlink(&entry.path())
|
||||
})?;
|
||||
|
||||
if fs::read_dir(path)?.next().is_none() {
|
||||
fs::remove_dir(path)?
|
||||
}
|
||||
|
||||
} else if metadata.file_type().is_symlink() {
|
||||
if let Err(_) = fs::metadata(path) {
|
||||
fs::remove_file(path)?
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user