use anyhow::{Context, Result};
use smb2::types::status::NtStatus;
use smb2::{FileInfo, RenameOptions, SmbClient, Tree};
use crate::auth::Credentials;
use crate::remote::{self, RemoteKind};
use crate::target::Target;
#[derive(Debug, Clone, Copy, clap::Args)]
pub struct Concurrency {
#[arg(short = 'j', long = "concurrency", default_value_t = 8)]
value: usize,
}
impl Concurrency {
pub fn workers(self) -> usize {
self.value.max(1)
}
}
#[derive(Debug, Clone)]
pub enum Job {
Stat(String),
Rename {
from: String,
to: String,
replace: bool,
},
DeleteFile(String),
DeleteDirectory(String),
CreateDirectory(String),
CreateDirectoryIfMissing(String),
XattrNames(String),
XattrList(String),
XattrGet {
path: String,
name: String,
},
XattrSet {
path: String,
name: String,
value: Vec<u8>,
},
XattrRemove {
path: String,
name: String,
},
}
#[derive(Debug)]
pub enum Outcome {
Done,
AlreadyThere,
Stat(FileInfo),
Xattrs(Vec<(String, Option<Vec<u8>>)>),
Failed(String),
}
impl Outcome {
pub fn error(&self) -> Option<&str> {
match self {
Outcome::Failed(message) => Some(message),
_ => None,
}
}
}
pub async fn connect_all(
target: &Target,
credentials: &Credentials,
count: usize,
) -> Result<Vec<(SmbClient, Tree)>> {
let mut connections = Vec::with_capacity(count);
for index in 0..count {
connections.push(
connect_one(target, credentials)
.await
.with_context(|| format!("opening connection {} of {count}", index + 1))?,
);
}
Ok(connections)
}
async fn connect_one(target: &Target, credentials: &Credentials) -> Result<(SmbClient, Tree)> {
let mut client = credentials
.connect(&target.addr())
.await
.with_context(|| format!("connecting to {}", target.addr()))?;
let tree = client
.connect_share(&target.share)
.await
.with_context(|| format!("connecting to share {:?}", target.share))?;
Ok((client, tree))
}
pub async fn run(
target: &Target,
credentials: &Credentials,
concurrency: usize,
jobs: Vec<Job>,
mut on_progress: impl FnMut(usize, usize),
) -> Result<Vec<Outcome>> {
if jobs.is_empty() {
return Ok(vec![]);
}
let total = jobs.len();
let workers = concurrency.clamp(1, total);
let mut lanes: Vec<Vec<(usize, Job)>> = vec![Vec::new(); workers];
for (index, job) in jobs.into_iter().enumerate() {
lanes[index % workers].push((index, job));
}
let (progress_tx, mut progress_rx) = tokio::sync::mpsc::unbounded_channel::<()>();
let mut handles = Vec::with_capacity(workers);
for lane in lanes {
let target = target.clone();
let credentials = credentials.clone();
let progress_tx = progress_tx.clone();
handles.push(tokio::spawn(async move {
let (mut client, mut tree) = connect_one(&target, &credentials).await?;
let mut results = Vec::with_capacity(lane.len());
for (index, job) in lane {
let outcome = execute(&mut client, &mut tree, &job).await;
let _ = progress_tx.send(());
results.push((index, outcome));
}
let _ = client.disconnect_share(&tree).await;
Ok::<_, anyhow::Error>(results)
}));
}
drop(progress_tx);
let mut finished = 0;
while progress_rx.recv().await.is_some() {
finished += 1;
on_progress(finished, total);
}
let mut outcomes: Vec<Option<Outcome>> = (0..total).map(|_| None).collect();
for handle in handles {
for (index, outcome) in handle.await.context("worker task panicked")?? {
outcomes[index] = Some(outcome);
}
}
Ok(outcomes
.into_iter()
.map(|outcome| outcome.unwrap_or_else(|| Outcome::Failed("no result".to_string())))
.collect())
}
async fn execute(client: &mut SmbClient, tree: &mut Tree, job: &Job) -> Outcome {
match job {
Job::Stat(path) => match client.stat(tree, path).await {
Ok(info) => Outcome::Stat(info),
Err(error) => Outcome::Failed(error.to_string()),
},
Job::Rename { from, to, replace } => {
let options = RenameOptions::new().replace_if_exists(*replace);
report(client.rename_with(tree, from, to, options).await)
}
Job::DeleteFile(path) => report(client.delete_file(tree, path).await),
Job::DeleteDirectory(path) => report(client.delete_directory(tree, path).await),
Job::CreateDirectory(path) => create_directory(client, tree, path, false).await,
Job::CreateDirectoryIfMissing(path) => create_directory(client, tree, path, true).await,
Job::XattrNames(path) => match client.list_xattrs(tree, path).await {
Ok(names) => Outcome::Xattrs(names.into_iter().map(|name| (name, None)).collect()),
Err(error) => Outcome::Failed(error.to_string()),
},
Job::XattrList(path) => list_xattrs(client, tree, path).await,
Job::XattrGet { path, name } => match client.get_xattr(tree, path, name).await {
Ok(Some(value)) => Outcome::Xattrs(vec![(name.clone(), Some(value))]),
Ok(None) => Outcome::Failed(format!("no such xattr: {name}")),
Err(error) => Outcome::Failed(error.to_string()),
},
Job::XattrSet { path, name, value } => {
report(client.set_xattr(tree, path, name, value).await)
}
Job::XattrRemove { path, name } => report(client.remove_xattr(tree, path, name).await),
}
}
async fn list_xattrs(client: &mut SmbClient, tree: &mut Tree, path: &str) -> Outcome {
let names = match client.list_xattrs(tree, path).await {
Ok(names) => names,
Err(error) => return Outcome::Failed(error.to_string()),
};
let mut attributes = Vec::with_capacity(names.len());
for name in names {
match client.get_xattr(tree, path, &name).await {
Ok(Some(value)) => attributes.push((name, Some(value))),
Ok(None) => {}
Err(error) => return Outcome::Failed(format!("reading {name}: {error}")),
}
}
Outcome::Xattrs(attributes)
}
async fn create_directory(
client: &mut SmbClient,
tree: &mut Tree,
path: &str,
parents: bool,
) -> Outcome {
match client.create_directory(tree, path).await {
Ok(()) => Outcome::Done,
Err(error) if error.status() == Some(NtStatus::OBJECT_NAME_COLLISION) => {
let occupant = remote::kind(client, tree, path).await.ok();
match resolve_collision(occupant, parents) {
Ok(()) => Outcome::AlreadyThere,
Err(message) => Outcome::Failed(message),
}
}
Err(error) => Outcome::Failed(error.to_string()),
}
}
fn resolve_collision(occupant: Option<RemoteKind>, parents: bool) -> Result<(), String> {
match occupant {
Some(RemoteKind::Directory) if parents => Ok(()),
Some(RemoteKind::Directory) => Err("already exists as a directory".to_string()),
Some(RemoteKind::File) => Err("already exists as a file, not a directory".to_string()),
Some(RemoteKind::Missing) | None => Err("already exists".to_string()),
}
}
fn report(result: smb2::Result<()>) -> Outcome {
match result {
Ok(()) => Outcome::Done,
Err(error) => Outcome::Failed(error.to_string()),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mkdir_p_accepts_a_directory_that_is_already_there() {
assert_eq!(resolve_collision(Some(RemoteKind::Directory), true), Ok(()));
}
#[test]
fn mkdir_p_refuses_a_name_taken_by_a_file() {
let message = resolve_collision(Some(RemoteKind::File), true)
.expect_err("a file where a directory should be has to fail, as GNU mkdir -p does");
assert!(message.contains("file"), "{message}");
}
#[test]
fn mkdir_p_refuses_a_collision_it_could_not_identify() {
assert!(resolve_collision(None, true).is_err());
assert!(resolve_collision(Some(RemoteKind::Missing), true).is_err());
}
#[test]
fn plain_mkdir_refuses_any_name_that_is_taken() {
for occupant in [RemoteKind::Directory, RemoteKind::File] {
let message = resolve_collision(Some(occupant), false)
.expect_err("without -p, an existing name is an error whatever it holds");
assert!(message.contains("already exists"), "{message}");
}
}
}