mod auth;
mod batch;
mod commands;
mod output;
mod pool;
mod remote;
mod target;
use std::path::PathBuf;
use anyhow::Result;
use clap::{Parser, Subcommand};
use crate::auth::AuthArgs;
use crate::target::Target;
#[derive(Parser)]
#[command(
name = "smb2",
version,
about = "List, move, and delete files on an SMB share without mounting it",
after_help = "Targets look like //host/share/path. Passwords come from --password-command, \
SMB2_PASS, or a prompt. -j goes after the subcommand, on the commands that \
spread work over connections: stat, mkdir, rm, rmdir, mv, xattr."
)]
struct Cli {
#[command(flatten)]
auth: AuthArgs,
#[arg(long, global = true)]
json: bool,
#[arg(long, global = true)]
dry_run: bool,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Ls {
target: String,
#[arg(short = 'l', long)]
long: bool,
#[arg(short = 'R', long)]
recursive: bool,
#[arg(long)]
sort: bool,
#[arg(short = 'i', long = "inode")]
index: bool,
},
Stat {
targets: Vec<String>,
#[command(flatten)]
input: batch::BatchInput,
#[command(flatten)]
concurrency: pool::Concurrency,
},
Mkdir {
targets: Vec<String>,
#[command(flatten)]
input: batch::BatchInput,
#[command(flatten)]
concurrency: pool::Concurrency,
#[arg(short = 'p', long)]
parents: bool,
},
Rm {
targets: Vec<String>,
#[command(flatten)]
input: batch::BatchInput,
#[command(flatten)]
concurrency: pool::Concurrency,
},
Rmdir {
targets: Vec<String>,
#[command(flatten)]
input: batch::BatchInput,
#[command(flatten)]
concurrency: pool::Concurrency,
},
Mv {
targets: Vec<String>,
#[arg(long, value_name = "FILE")]
from_file: Option<String>,
#[arg(long)]
replace: bool,
#[command(flatten)]
concurrency: pool::Concurrency,
},
Xattr {
targets: Vec<String>,
#[command(flatten)]
input: batch::BatchInput,
#[command(flatten)]
concurrency: pool::Concurrency,
#[arg(short = 'l', conflicts_with_all = ["print", "write", "delete"])]
list: bool,
#[arg(short = 'p', value_name = "NAME", conflicts_with_all = ["write", "delete"])]
print: Option<String>,
#[arg(short = 'w', num_args = 2, value_names = ["NAME", "VALUE"], conflicts_with = "delete")]
write: Option<Vec<String>>,
#[arg(short = 'd', value_name = "NAME")]
delete: Option<String>,
#[arg(short = 'x')]
hex: bool,
},
Streams { target: String },
Cat { target: String },
Get {
target: String,
destination: Option<String>,
},
Put {
source: PathBuf,
target: String,
},
Realpath { target: String },
Df { target: String },
Shares {
host: String,
},
}
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
let cli = Cli::parse();
let credentials = cli.auth.resolve()?;
match &cli.command {
Commands::Ls {
target,
long,
recursive,
sort,
index,
} => {
let target = Target::parse(target)?;
let format = commands::meta::LsFormat {
long: *long,
recursive: *recursive,
sort: *sort,
index: *index,
};
commands::meta::ls(&target, &credentials, format, cli.json).await
}
Commands::Stat {
targets,
input,
concurrency,
} => {
let (base, paths) = input.collect(targets)?;
commands::meta::stat(&base, &credentials, paths, concurrency.workers(), cli.json).await
}
Commands::Mkdir {
targets,
input,
concurrency,
parents,
} => {
commands::mutate::mkdir(
targets,
input,
&credentials,
concurrency.workers(),
*parents,
cli.dry_run,
)
.await
}
Commands::Rm {
targets,
input,
concurrency,
} => {
commands::mutate::rm(
targets,
input,
&credentials,
concurrency.workers(),
cli.dry_run,
)
.await
}
Commands::Rmdir {
targets,
input,
concurrency,
} => {
commands::mutate::rmdir(
targets,
input,
&credentials,
concurrency.workers(),
cli.dry_run,
)
.await
}
Commands::Mv {
targets,
from_file,
replace,
concurrency,
} => {
commands::mutate::mv(
targets,
from_file.as_deref(),
*replace,
&credentials,
concurrency.workers(),
cli.dry_run,
)
.await
}
Commands::Xattr {
targets,
input,
concurrency,
list,
print,
write,
delete,
hex,
} => {
let action = commands::xattr::Action::from_flags(
*list,
print.as_ref(),
write.as_ref(),
delete.as_ref(),
*hex,
)?;
commands::xattr::xattr(
targets,
input,
&credentials,
action,
commands::xattr::Options {
concurrency: concurrency.workers(),
hex: *hex,
as_json: cli.json,
dry_run: cli.dry_run,
},
)
.await
}
Commands::Streams { target } => {
commands::xattr::streams(&Target::parse(target)?, &credentials, cli.json).await
}
Commands::Cat { target } => {
commands::transfer::cat(&Target::parse(target)?, &credentials).await
}
Commands::Get {
target,
destination,
} => {
commands::transfer::get(
&Target::parse(target)?,
&credentials,
destination.as_deref(),
cli.dry_run,
)
.await
}
Commands::Put { source, target } => {
commands::transfer::put(source, &Target::parse(target)?, &credentials, cli.dry_run)
.await
}
Commands::Realpath { target } => {
commands::meta::realpath(&Target::parse(target)?, &credentials, cli.json).await
}
Commands::Df { target } => {
commands::meta::df(&Target::parse(target)?, &credentials, cli.json).await
}
Commands::Shares { host } => {
let (host, port) = parse_host(host);
commands::meta::shares(&host, port, &credentials, cli.json).await
}
}
}
fn parse_host(input: &str) -> (String, u16) {
let trimmed = input
.trim_start_matches("smb://")
.trim_start_matches("//")
.trim_end_matches('/');
match trimmed.rsplit_once(':') {
Some((host, port)) => (host.to_string(), port.parse().unwrap_or(445)),
None => (trimmed.to_string(), 445),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn verifies_the_cli_definition() {
use clap::CommandFactory;
Cli::command().debug_assert();
}
#[test]
fn takes_concurrency_after_the_subcommand_but_not_before_it() {
assert!(Cli::try_parse_from(["smb2", "rm", "//host/share/a", "-j", "16"]).is_ok());
assert!(Cli::try_parse_from(["smb2", "-j", "16", "rm", "//host/share/a"]).is_err());
}
#[test]
fn offers_concurrency_on_every_command_that_spreads_work_over_connections() {
for command in commands_that_pool() {
let mut args = command.clone();
args.extend(["-j", "4"]);
assert!(
Cli::try_parse_from(&args).is_ok(),
"{} feeds pool::run, so it has to take -j",
command[1]
);
}
}
#[test]
fn keeps_concurrency_off_commands_that_open_one_connection() {
for command in commands_that_open_one_connection() {
let mut args = command.clone();
args.extend(["-j", "4"]);
assert!(
Cli::try_parse_from(&args).is_err(),
"{} opens one connection, so advertising -j on it is a lie",
command[1]
);
}
}
fn commands_that_pool() -> Vec<Vec<&'static str>> {
vec![
vec!["smb2", "stat", "//host/share/a"],
vec!["smb2", "mkdir", "//host/share/a"],
vec!["smb2", "rm", "//host/share/a"],
vec!["smb2", "rmdir", "//host/share/a"],
vec!["smb2", "mv", "//host/share/a", "//host/share/b"],
vec!["smb2", "xattr", "//host/share/a"],
]
}
fn commands_that_open_one_connection() -> Vec<Vec<&'static str>> {
vec![
vec!["smb2", "ls", "//host/share/a"],
vec!["smb2", "streams", "//host/share/a"],
vec!["smb2", "cat", "//host/share/a"],
vec!["smb2", "get", "//host/share/a"],
vec!["smb2", "put", "./a", "//host/share/a"],
vec!["smb2", "realpath", "//host/share/a"],
vec!["smb2", "df", "//host/share"],
vec!["smb2", "shares", "host"],
]
}
#[test]
fn parses_host_forms() {
assert_eq!(parse_host("raspi"), ("raspi".to_string(), 445));
assert_eq!(parse_host("//raspi/"), ("raspi".to_string(), 445));
assert_eq!(parse_host("raspi:4450"), ("raspi".to_string(), 4450));
}
}