use std::collections::VecDeque;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use anyhow::{bail, Context, Result};
use smb2::{SmbClient, Tree};
use tokio::io::{AsyncWrite, AsyncWriteExt};
use crate::auth::Credentials;
use crate::pool;
use crate::remote::{self, RemoteKind};
use crate::target::Target;
pub async fn cat(target: &Target, credentials: &Credentials) -> Result<()> {
let (mut client, tree) = pool::connect_all(target, credentials, 1)
.await?
.pop()
.expect("connect_all returns one connection");
let download = Download::open(&mut client, &tree, target).await?;
let mut stdout = tokio::io::stdout();
download.pump(target, &mut stdout).await?;
stdout.flush().await.context("writing to stdout")?;
let _ = client.disconnect_share(&tree).await;
Ok(())
}
pub async fn get(
target: &Target,
credentials: &Credentials,
destination: Option<&str>,
dry_run: bool,
) -> Result<()> {
let destination = download_path(destination, &target.path)?;
if dry_run {
println!("get {} {}", target.display(), destination.display());
println!("Would have downloaded 1 file.");
return Ok(());
}
let (mut client, tree) = pool::connect_all(target, credentials, 1)
.await?
.pop()
.expect("connect_all returns one connection");
let download = Download::open(&mut client, &tree, target).await?;
let partial = partial_path(&destination);
let received = write_to_partial(download, target, &partial).await;
let received = match received {
Ok(received) => received,
Err(error) => {
let _ = tokio::fs::remove_file(&partial).await;
return Err(error);
}
};
tokio::fs::rename(&partial, &destination)
.await
.with_context(|| format!("writing {}", destination.display()))?;
println!(
"Downloaded {} bytes to {}",
crate::output::bytes(received),
destination.display()
);
let _ = client.disconnect_share(&tree).await;
Ok(())
}
fn partial_path(destination: &Path) -> PathBuf {
let mut name = destination.file_name().unwrap_or_default().to_os_string();
name.push(".smb2-part");
destination.with_file_name(name)
}
async fn write_to_partial(download: Download, target: &Target, partial: &Path) -> Result<u64> {
let mut file = tokio::fs::File::create(partial)
.await
.with_context(|| format!("writing {}", partial.display()))?;
let received = download.pump(target, &mut file).await?;
file.flush()
.await
.with_context(|| format!("writing {}", partial.display()))?;
Ok(received)
}
const IN_FLIGHT_BYTES: u64 = 16 * 1024 * 1024;
const MAX_IN_FLIGHT_READS: u64 = 32;
struct Download {
reader: Arc<smb2::FileReader>,
chunk: u64,
window: usize,
}
impl Download {
async fn open(client: &mut SmbClient, tree: &Tree, target: &Target) -> Result<Self> {
let chunk = u64::from(
client
.params()
.map(|params| params.max_read_size)
.unwrap_or(65_536),
)
.max(1);
let reader = client
.open_file_reader(tree, &target.path)
.await
.with_context(|| format!("reading {}", target.display()))?;
Ok(Self {
reader: Arc::new(reader),
chunk,
window: (IN_FLIGHT_BYTES / chunk).clamp(2, MAX_IN_FLIGHT_READS) as usize,
})
}
async fn pump<W: AsyncWrite + Unpin>(self, target: &Target, sink: &mut W) -> Result<u64> {
let Self {
reader,
chunk,
window,
} = self;
let size = reader.size();
let mut in_flight: VecDeque<tokio::task::JoinHandle<smb2::Result<Vec<u8>>>> =
VecDeque::new();
let mut offset = 0u64;
let mut received = 0u64;
let mut failure: Option<anyhow::Error> = None;
loop {
while in_flight.len() < window && offset < size {
let reader = Arc::clone(&reader);
let at = offset;
offset += chunk;
in_flight.push_back(tokio::spawn(async move { reader.read_at(at, chunk).await }));
}
let Some(next) = in_flight.pop_front() else {
break;
};
let data = match next.await {
Ok(Ok(data)) => data,
Ok(Err(error)) => {
failure = Some(
anyhow::Error::new(error).context(format!("reading {}", target.display())),
);
break;
}
Err(joined) => {
failure = Some(anyhow::Error::new(joined).context("a download task failed"));
break;
}
};
if let Err(error) = sink.write_all(&data).await {
failure = Some(anyhow::Error::new(error).context("writing the downloaded bytes"));
break;
}
received += data.len() as u64;
}
while let Some(pending) = in_flight.pop_front() {
let _ = pending.await;
}
if let Ok(reader) = Arc::try_unwrap(reader) {
let _ = reader.close().await;
}
match failure {
Some(error) => Err(error),
None => Ok(received),
}
}
}
fn download_path(destination: Option<&str>, remote_path: &str) -> Result<PathBuf> {
let name = remote_file_name(remote_path)?;
let Some(destination) = destination else {
return Ok(PathBuf::from(name));
};
let path = PathBuf::from(destination);
if path.is_dir() {
return Ok(path.join(name));
}
if destination.ends_with('/') || destination.ends_with(std::path::MAIN_SEPARATOR) {
bail!("{destination} is not a directory, so there's nowhere to put {name}");
}
Ok(path)
}
fn remote_file_name(remote_path: &str) -> Result<&str> {
remote_path
.rsplit('/')
.next()
.filter(|name| !name.is_empty())
.context("no file name in the target; pass a destination")
}
pub async fn put(
source: &Path,
target: &Target,
credentials: &Credentials,
dry_run: bool,
) -> Result<()> {
let source_name = source
.file_name()
.and_then(|name| name.to_str())
.context("the source has no file name")?;
if dry_run {
let size = tokio::fs::metadata(source)
.await
.with_context(|| format!("reading {}", source.display()))?
.len();
let remote_path = upload_path(target, None, source_name)?;
println!(
"put {} {}",
source.display(),
target.with_path(&remote_path).display()
);
println!("Would have uploaded {} bytes.", crate::output::bytes(size));
return Ok(());
}
let size = tokio::fs::metadata(source)
.await
.with_context(|| format!("reading {}", source.display()))?
.len();
let (mut client, mut tree) = pool::connect_all(target, credentials, 1)
.await?
.pop()
.expect("connect_all returns one connection");
let kind = remote_kind(&mut client, &mut tree, target, &target.path).await?;
let remote_path = upload_path(target, Some(kind), source_name)?;
if remote_path != target.path {
let destination = target.with_path(&remote_path);
if remote_kind(&mut client, &mut tree, target, &remote_path).await? == RemoteKind::Directory
{
bail!(
"{} is a directory; move it or rename the source instead of overwriting it",
destination.display()
);
}
}
let written = upload(&mut client, &mut tree, source, &remote_path, size)
.await
.with_context(|| format!("writing {}", target.with_path(&remote_path).display()))?;
println!(
"Uploaded {} bytes to {}",
crate::output::bytes(written),
target.with_path(&remote_path).display()
);
let _ = client.disconnect_share(&tree).await;
Ok(())
}
async fn upload(
client: &mut SmbClient,
tree: &mut Tree,
source: &Path,
remote_path: &str,
size: u64,
) -> Result<u64> {
let chunk = client
.params()
.map(|params| params.max_write_size)
.unwrap_or(65_536)
.max(1);
if size <= u64::from(chunk) {
let data = tokio::fs::read(source)
.await
.with_context(|| format!("reading {}", source.display()))?;
return Ok(client.write_file(tree, remote_path, &data).await?);
}
let mut file =
std::fs::File::open(source).with_context(|| format!("reading {}", source.display()))?;
let mut next_chunk = || {
let mut buffer = vec![0u8; chunk as usize];
let mut filled = 0;
while filled < buffer.len() {
match file.read(&mut buffer[filled..]) {
Ok(0) => break,
Ok(read) => filled += read,
Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {}
Err(error) => return Some(Err(error)),
}
}
if filled == 0 {
return None;
}
buffer.truncate(filled);
Some(Ok(buffer))
};
Ok(client
.write_file_streamed(tree, remote_path, &mut next_chunk)
.await?)
}
fn upload_path(target: &Target, kind: Option<RemoteKind>, source_name: &str) -> Result<String> {
let into_directory = if target.path.is_empty() || kind == Some(RemoteKind::Directory) {
true
} else if target.trailing_slash {
match kind {
Some(RemoteKind::File) => bail!(
"{} is a file, not a directory, so {source_name} can't go inside it",
target.display()
),
Some(RemoteKind::Missing) => bail!(
"{} doesn't exist on the share, so {source_name} can't go inside it; \
create it first with `smb2 mkdir -p`",
target.display()
),
None => true,
Some(RemoteKind::Directory) => unreachable!("handled above"),
}
} else {
false
};
if !into_directory {
return Ok(target.path.clone());
}
Ok(if target.path.is_empty() {
source_name.to_string()
} else {
format!("{}/{source_name}", target.path)
})
}
async fn remote_kind(
client: &mut SmbClient,
tree: &mut Tree,
target: &Target,
path: &str,
) -> Result<RemoteKind> {
remote::kind(client, tree, path)
.await
.with_context(|| format!("checking what's at {}", target.with_path(path).display()))
}
#[cfg(test)]
mod tests {
use super::*;
fn target(path: &str) -> Target {
Target::parse(&format!("//host/share/{path}")).unwrap()
}
#[test]
fn puts_a_file_into_a_target_written_with_a_trailing_separator() {
let path = upload_path(&target("inbox/"), Some(RemoteKind::Directory), "a.jpg").unwrap();
assert_eq!(path, "inbox/a.jpg");
}
#[test]
fn puts_a_file_into_a_target_that_is_already_a_directory() {
let path = upload_path(&target("inbox"), Some(RemoteKind::Directory), "a.jpg").unwrap();
assert_eq!(path, "inbox/a.jpg");
}
#[test]
fn puts_a_file_into_the_share_root() {
let path = upload_path(&target(""), Some(RemoteKind::Directory), "a.jpg").unwrap();
assert_eq!(path, "a.jpg");
}
#[test]
fn treats_a_plain_target_as_the_destination_name() {
let path = upload_path(&target("inbox/b.jpg"), Some(RemoteKind::Missing), "a.jpg").unwrap();
assert_eq!(path, "inbox/b.jpg");
let overwrite =
upload_path(&target("inbox/b.jpg"), Some(RemoteKind::File), "a.jpg").unwrap();
assert_eq!(overwrite, "inbox/b.jpg");
}
#[test]
fn refuses_a_directory_target_that_is_really_a_file() {
let error = upload_path(&target("notes.txt/"), Some(RemoteKind::File), "a.jpg")
.unwrap_err()
.to_string();
assert!(error.contains("not a directory"), "{error}");
}
#[test]
fn refuses_a_directory_target_that_isnt_there() {
let error = upload_path(&target("inbox/"), Some(RemoteKind::Missing), "a.jpg")
.unwrap_err()
.to_string();
assert!(error.contains("doesn't exist"), "{error}");
}
#[test]
fn plans_for_a_directory_when_a_dry_run_cannot_ask() {
let path = upload_path(&target("inbox/"), None, "a.jpg").unwrap();
assert_eq!(path, "inbox/a.jpg");
}
#[test]
fn downloads_under_the_remote_name_by_default() {
let path = download_path(None, "photos/a.jpg").unwrap();
assert_eq!(path, PathBuf::from("a.jpg"));
}
#[test]
fn downloads_to_an_explicit_local_name() {
let path = download_path(Some("b.jpg"), "photos/a.jpg").unwrap();
assert_eq!(path, PathBuf::from("b.jpg"));
}
#[test]
fn downloads_into_a_local_directory() {
let directory = std::env::temp_dir();
let path = download_path(Some(directory.to_str().unwrap()), "photos/a.jpg").unwrap();
assert_eq!(path, directory.join("a.jpg"));
}
#[test]
fn refuses_a_local_directory_destination_that_isnt_there() {
let missing = std::env::temp_dir().join("smb2-cli-no-such-directory-here");
let error = download_path(Some(&format!("{}/", missing.display())), "photos/a.jpg")
.unwrap_err()
.to_string();
assert!(error.contains("not a directory"), "{error}");
}
}