Skip to content

Commit

Permalink
update: add "rnsym" for renaming symbol link src
Browse files Browse the repository at this point in the history
  • Loading branch information
B1ACK917 committed Nov 2, 2023
1 parent 5e2093f commit d97493c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum Action {
SymlinkToLink,
LinkToSymlink,
Rename,
RenameSym,
DumpMemory,
MemoryDetail,
None,
Expand Down Expand Up @@ -51,6 +52,7 @@ impl Config {
"s2l" => { Action::SymlinkToLink }
"l2s" => { Action::LinkToSymlink }
"rn" => { Action::Rename }
"rnsym" => { Action::RenameSym }
"ps" => { Action::Process }
"pa" => { Action::ProcessAncestor }
"dm" => { Action::DumpMemory }
Expand Down
16 changes: 16 additions & 0 deletions src/core/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@ impl Executor {
}
}

Action::RenameSym => {
let args_ = func::parse_args_or(args, String::from("."));
for arg in &args_ {
let target = PathBuf::from(arg);
fs::rename_symbol_link(
&self.work_path,
&target,
&self.flags.input,
&self.flags.output,
&self.flags.append,
self.flags.num,
self.flags.recursive,
);
}
}

Action::DumpMemory => {
let args_ = func::parse_args_or(args, String::from("proc"));
for arg in &args_ {
Expand Down
51 changes: 51 additions & 0 deletions src/event/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,55 @@ pub fn rename(work_path: &PathBuf,
let max_depth = if recursive { MAX_RECURSIVE_DEPTH } else { 0 };
let target = func::get_execute_target(work_path, input_path);
rename_recursive(&target, in_str, out_str, append_str, num, 0, max_depth);
}

pub fn rename_symbol_link_recursive(cur_path: &PathBuf,
in_str: &String,
out_str: &String,
append_str: &String,
num: usize,
cur_depth: i8,
max_depth: i8) {
if cur_depth > max_depth {
return;
}

for entry in cur_path.read_dir().unwrap() {
let filepath = entry.unwrap().path();
if filepath.is_dir() {
rename_symbol_link_recursive(
&filepath,
in_str,
out_str,
append_str,
num,
cur_depth + 1,
max_depth,
);
} else {
let src = filepath.read_link().unwrap();
let file_src = src.to_str().unwrap();
if file_src.contains(in_str) {
let mut new_file_src = file_src.replacen(in_str, out_str, 1);
new_file_src.insert_str(num, append_str);
fs::remove_file(&filepath).unwrap();
symlink(&new_file_src, &filepath).unwrap();
if *DEBUG {
println!("Symbol link {} -> {}", filepath.display(), new_file_src)
}
}
}
}
}

pub fn rename_symbol_link(work_path: &PathBuf,
input_path: &PathBuf,
in_str: &String,
out_str: &String,
append_str: &String,
num: usize,
recursive: bool) {
let max_depth = if recursive { MAX_RECURSIVE_DEPTH } else { 0 };
let target = func::get_execute_target(work_path, input_path);
rename_symbol_link_recursive(&target, in_str, out_str, append_str, num, 0, max_depth);
}

0 comments on commit d97493c

Please sign in to comment.