51 lines
1.7 KiB
Rust
51 lines
1.7 KiB
Rust
use super::Cmd;
|
|
use clap::{Arg, ArgAction, Command};
|
|
use std::{ffi::CString, io, process};
|
|
|
|
#[derive(Debug, Default)]
|
|
pub struct Unlink;
|
|
|
|
impl Cmd for Unlink {
|
|
fn cli(&self) -> clap::Command {
|
|
Command::new("unlink")
|
|
.about("call the unlink function to remove the specified file")
|
|
.author("Nathan Fisher")
|
|
.version(env!("CARGO_PKG_VERSION"))
|
|
.args([
|
|
Arg::new("verbose")
|
|
.help("display user feedback upon success")
|
|
.short('v')
|
|
.long("verbose")
|
|
.action(ArgAction::SetTrue),
|
|
Arg::new("file")
|
|
.value_name("FILE")
|
|
.required(true)
|
|
.num_args(1..),
|
|
])
|
|
}
|
|
|
|
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
|
|
let Some(matches) = matches else {
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
|
|
};
|
|
if let Some(files) = matches.get_many::<String>("file") {
|
|
for f in files {
|
|
let fname = CString::new(f.as_str())?;
|
|
let ret = unsafe { libc::unlink(fname.as_ptr()) };
|
|
if matches.get_flag("verbose") && ret == 0 {
|
|
println!("unlink: {f}");
|
|
} else if ret != 0 {
|
|
let err = io::Error::last_os_error();
|
|
eprintln!("unlink: cannot unlink {f}: {err}");
|
|
process::exit(ret);
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn path(&self) -> Option<crate::Path> {
|
|
Some(crate::Path::UsrBin)
|
|
}
|
|
}
|