Skip to content

Commit

Permalink
sort the operation list in depth-first order
Browse files Browse the repository at this point in the history
  • Loading branch information
kenchou committed Mar 16, 2024
1 parent 02290df commit 80a7cc8
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use slab_tree::{NodeId, NodeRef, Tree, TreeBuilder};
use std::collections::HashMap;
use std::env;
use std::fs::{read_link, remove_dir_all, remove_file, rename, File};
use std::io::{self, Read};
use std::io::Read;
use std::path::{Path, PathBuf};
use walkdir::{DirEntry, WalkDir};

Expand Down Expand Up @@ -446,19 +446,22 @@ fn main() -> std::io::Result<()> {
}

if app_options.is_debug_mode() {
println!("{:#?}", operation_list);
println!("* operation_list: {:#?}", operation_list);
}

// dir tree
if app_options.verbose >= 1 {
print_tree(path_list_to_tree(&operation_list, &app_options.target_path));
}

operation_list.sort_by(|a, b| {
let depth_a = a.0.components().count();
let depth_b = b.0.components().count();
depth_b.cmp(&depth_a)
});
// execute
if app_options.enable_deletion {
for (file_path, pattern, _) in operation_list
.iter()
.filter(|(_, _, op)| *op == Operation::Delete)
for (file_path, pattern, _) in operation_list.iter().filter(|(_, _, op)| *op == Operation::Delete)
{
println!("{} {:#?} <== {}", "[-]".red(), file_path, pattern);
if app_options.prune {
Expand All @@ -470,9 +473,7 @@ fn main() -> std::io::Result<()> {
}

if app_options.enable_renaming {
for (file_path, new_file_name, _) in operation_list
.iter()
.filter(|(_, _, op)| *op == Operation::Rename)
for (file_path, new_file_name, _) in operation_list.iter().filter(|(_, _, op)| *op == Operation::Rename)
{
println!("{} {:#?} ==> {}", "[*]".yellow(), file_path, new_file_name);
let mut new_filepath = file_path.clone();
Expand All @@ -487,14 +488,14 @@ fn main() -> std::io::Result<()> {
Ok(())
}

fn remove_path(path: PathBuf) -> io::Result<()> {
fn remove_path(path: PathBuf) -> std::io::Result<()> {
match remove_file(&path) {
Ok(()) => Ok(()),
Err(_) => remove_dir_all(path),
}
}

fn symbol_link_status(symbol_link_path: &Path) -> io::Result<(bool, PathBuf)> {
fn symbol_link_status(symbol_link_path: &Path) -> std::io::Result<(bool, PathBuf)> {
let target = read_link(symbol_link_path)?;
let target_path = symbol_link_path.parent().unwrap().join(&target);
Ok((target_path.exists(), target))
Expand Down

0 comments on commit 80a7cc8

Please sign in to comment.