switched shlex 1.2 to 1.3

This commit is contained in:
ascendforever 2024-10-13 17:09:46 -04:00
parent 86aa59bad8
commit e395e7f9c4
3 changed files with 33 additions and 27 deletions

View file

@ -6,5 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
shlex = "1.2" shlex = "1.3"
structopt = "0.3" structopt = "0.3"

Binary file not shown.

View file

@ -36,6 +36,12 @@ fn main() -> Result<(), i32> {
} }
} }
for target in &args.targets {
if target.contains('\0') {
eprintln!("Paths can never contain null byte: {}", target);
return Err(1);
}
}
let run_targets: Vec<Vec<&String>> = split_vec(&args.targets, &args.separator.unwrap_or(s_default_target_separator!().to_string())); let run_targets: Vec<Vec<&String>> = split_vec(&args.targets, &args.separator.unwrap_or(s_default_target_separator!().to_string()));
if run_targets.is_empty() { if run_targets.is_empty() {
@ -45,18 +51,12 @@ fn main() -> Result<(), i32> {
return Ok(()); return Ok(());
} }
if args.prompt {
if !prompt_confirm(&run_targets) {
return Ok(());
}
}
let mut bad = false; let mut bad = false;
let run_paths: Vec<Vec<PathBuf>> = run_targets.iter().enumerate().map( let run_paths: Vec<Vec<PathBuf>> = run_targets.iter().enumerate().map(
|(_,spaths)| spaths.iter().map( |(_,spaths)| spaths.iter().map(
|spath| Path::new(spath).canonicalize().unwrap_or_else( |spath| Path::new(spath).canonicalize().unwrap_or_else(
|_| { |_| {
eprintln!("Failed to retrieve absolute path for {}", shlex::quote(spath)); eprintln!("Failed to retrieve absolute path for {}", shlex::try_quote(spath).unwrap());
bad = true; bad = true;
Default::default() Default::default()
} }
@ -67,6 +67,12 @@ fn main() -> Result<(), i32> {
return Err(1); return Err(1);
} }
if args.prompt {
if !prompt_confirm(&run_targets) {
return Ok(());
}
}
for paths in &run_paths { for paths in &run_paths {
if let Err(s) = check_all_same_device(paths) { if let Err(s) = check_all_same_device(paths) {
@ -139,7 +145,7 @@ struct CLIArguments {
fn prompt_confirm<'a, T: Borrow<[Y]>, Y: AsRef<str>>(run_targets: &[T]) -> bool { fn prompt_confirm<'a, T: Borrow<[Y]>, Y: AsRef<str>>(run_targets: &[T]) -> bool {
println!("Are you sure you want to link all duplicates in each of these sets of targets?"); println!("Are you sure you want to link all duplicates in each of these sets of targets?");
for spaths in run_targets { for spaths in run_targets {
println!(" {}", shlex::join(spaths.borrow().iter().map(|s| s.as_ref()))); println!(" {}", shlex::try_join(spaths.borrow().iter().map(|s| s.as_ref())).unwrap());
} }
print!("> "); print!("> ");
std::io::stdout().flush().unwrap_or_else(|_| ()); std::io::stdout().flush().unwrap_or_else(|_| ());
@ -158,7 +164,7 @@ fn prompt_confirm<'a, T: Borrow<[Y]>, Y: AsRef<str>>(run_targets: &[T]) -> bool
fn read_file_lines(path: &Path, dest: &mut Vec<String>) -> Result<(), String> { fn read_file_lines(path: &Path, dest: &mut Vec<String>) -> Result<(), String> {
if !path.is_file() { if !path.is_file() {
return Err(format!("File does not exist or is not a normal file ({})", shlex::quote(&path.to_string_lossy()))); return Err(format!("File does not exist or is not a normal file ({})", shlex::try_quote(&path.to_string_lossy()).unwrap()));
} }
if let Ok(f) = std::fs::File::open(path) { if let Ok(f) = std::fs::File::open(path) {
let reader = BufReader::new(f); let reader = BufReader::new(f);
@ -170,7 +176,7 @@ fn read_file_lines(path: &Path, dest: &mut Vec<String>) -> Result<(), String> {
} }
Ok(()) Ok(())
} else { } else {
Err(format!("Could not open {}", shlex::quote(&path.to_string_lossy()))) Err(format!("Could not open {}", shlex::try_quote(&path.to_string_lossy()).unwrap()))
} }
} }
@ -180,7 +186,7 @@ fn get_st_dev(file: &PathBuf) -> Result<u64, String> {
if let Ok(metadata) = std::fs::metadata(file) { if let Ok(metadata) = std::fs::metadata(file) {
Ok(metadata.st_dev()) Ok(metadata.st_dev())
} else { } else {
Err(format!("Failed to retrive device id for {}", shlex::quote(&file.to_string_lossy()))) Err(format!("Failed to retrive device id for {}", shlex::try_quote(&file.to_string_lossy()).unwrap()))
} }
} }
@ -201,7 +207,7 @@ fn check_all_same_device(paths: &[PathBuf]) -> Result<(), String> {
let mut s = String::with_capacity(wrong.len()*128); // 75 max estimated len of path, 53 for prefix msg + nl let mut s = String::with_capacity(wrong.len()*128); // 75 max estimated len of path, 53 for prefix msg + nl
for path in wrong { for path in wrong {
s.push_str("Device ids must all be the same; got different for: {}"); s.push_str("Device ids must all be the same; got different for: {}");
s.push_str(&shlex::quote(&path.to_string_lossy())); s.push_str(&shlex::try_quote(&path.to_string_lossy()).unwrap());
s.push_str("\n"); s.push_str("\n");
} }
s.pop(); // remove last newline s.pop(); // remove last newline
@ -283,8 +289,8 @@ fn format_pair(f1s: &str, f2s: &str, cfg: &Config) -> String {
if cfg.no_brace_output { if cfg.no_brace_output {
return format!( return format!(
"{} {}", "{} {}",
shlex::quote(&f1s), shlex::try_quote(&f1s).unwrap(),
shlex::quote(&f2s) shlex::try_quote(&f2s).unwrap()
) )
} }
@ -295,30 +301,30 @@ fn format_pair(f1s: &str, f2s: &str, cfg: &Config) -> String {
if prefixlong && suffixlong { if prefixlong && suffixlong {
format!( format!(
"{}{{{},{}}}{}", "{}{{{},{}}}{}",
shlex::quote(prefix), shlex::try_quote(prefix).unwrap(),
shlex::quote(&f1s[ prefix.len()..std::cmp::max(prefix.len(), f1s.len()-suffix.len()) ]), shlex::try_quote(&f1s[ prefix.len()..std::cmp::max(prefix.len(), f1s.len()-suffix.len()) ]).unwrap(),
shlex::quote(&f2s[ prefix.len()..std::cmp::max(prefix.len(), f2s.len()-suffix.len()) ]), shlex::try_quote(&f2s[ prefix.len()..std::cmp::max(prefix.len(), f2s.len()-suffix.len()) ]).unwrap(),
shlex::quote(suffix) shlex::try_quote(suffix).unwrap()
) )
} else if prefixlong { } else if prefixlong {
format!( format!(
"{}{{{},{}}}", "{}{{{},{}}}",
shlex::quote(prefix), shlex::try_quote(prefix).unwrap(),
shlex::quote(&f1s[prefix.len()..]), shlex::try_quote(&f1s[prefix.len()..]).unwrap(),
shlex::quote(&f2s[prefix.len()..]) shlex::try_quote(&f2s[prefix.len()..]).unwrap()
) )
} else if suffixlong { } else if suffixlong {
format!( format!(
"{{{},{}}}{}", "{{{},{}}}{}",
shlex::quote(&f1s[..f1s.len()-suffix.len()]), shlex::try_quote(&f1s[..f1s.len()-suffix.len()]).unwrap(),
shlex::quote(&f2s[..f2s.len()-suffix.len()]), shlex::try_quote(&f2s[..f2s.len()-suffix.len()]).unwrap(),
shlex::quote(suffix), shlex::try_quote(suffix).unwrap(),
) )
} else { } else {
format!( format!(
"{} <-> {}", "{} <-> {}",
shlex::quote(&f1s), shlex::try_quote(&f1s).unwrap(),
shlex::quote(&f2s) shlex::try_quote(&f2s).unwrap()
) )
} }
} }