use anyhow::{Context, Result};
use serde_json::json;
use smb2::{DirectoryEntry, ListOptions, SmbClient, Tree};
use crate::auth::Credentials;
use crate::output;
use crate::pool::{self, Job, Outcome};
use crate::target::Target;
#[derive(Clone, Copy)]
pub struct LsFormat {
pub long: bool,
pub recursive: bool,
pub sort: bool,
pub index: bool,
}
impl LsFormat {
fn list_options(self) -> ListOptions {
ListOptions::new().index_numbers(self.index)
}
}
pub async fn ls(
target: &Target,
credentials: &Credentials,
format: LsFormat,
as_json: bool,
) -> Result<()> {
let (mut client, mut tree) = pool::connect_all(target, credentials, 1)
.await?
.pop()
.expect("connect_all returns one connection");
let result = if format.sort || as_json {
ls_sorted(&mut client, &mut tree, target, format, as_json).await
} else {
ls_streaming(&mut client, &mut tree, target, format).await
};
let _ = client.disconnect_share(&tree).await;
result
}
async fn ls_sorted(
client: &mut SmbClient,
tree: &mut Tree,
target: &Target,
format: LsFormat,
as_json: bool,
) -> Result<()> {
let mut pending = vec![target.path.clone()];
let mut rows = Vec::new();
while let Some(path) = pending.pop() {
let entries = client
.list_directory_with(tree, &path, format.list_options())
.await
.with_context(|| format!("listing {}", target.with_path(&path).display()))?;
for entry in entries {
if let Some(full) = child_path(&path, &entry) {
if format.recursive && entry.is_directory && !entry.is_link() {
pending.push(full.clone());
}
rows.push((full, entry));
}
}
}
rows.sort_by(|a, b| a.0.cmp(&b.0));
if as_json {
let items: Vec<_> = rows
.iter()
.map(|(path, entry)| {
let mut item = json!({
"path": path,
"name": entry.name,
"isDirectory": entry.is_directory,
"isLink": entry.is_link(),
"reparseTag": entry.reparse_tag.map(|tag| tag.0),
"size": entry.size,
"modified": output::unix_seconds(entry.modified),
"created": output::unix_seconds(entry.created),
"changed": output::unix_seconds(entry.changed),
});
if format.index {
item["indexNumber"] = json!(entry.index_number);
}
item
})
.collect();
println!("{}", serde_json::to_string_pretty(&items)?);
return Ok(());
}
let mut text = String::new();
for (path, entry) in &rows {
push_row(&mut text, path, entry, format);
}
match write_stdout(&text) {
Err(error) if error.kind() != std::io::ErrorKind::BrokenPipe => Err(error.into()),
_ => Ok(()),
}
}
async fn ls_streaming(
client: &mut SmbClient,
tree: &mut Tree,
target: &Target,
format: LsFormat,
) -> Result<()> {
let mut pending = vec![target.path.clone()];
while let Some(path) = pending.pop() {
let listing = || format!("listing {}", target.with_path(&path).display());
let mut reader = client
.open_directory_reader_with(tree, &path, format.list_options())
.await
.with_context(listing)?;
let mut subdirectories = Vec::new();
while let Some(batch) = reader.next_batch().await.with_context(listing)? {
let mut text = String::new();
for entry in &batch {
if let Some(full) = child_path(&path, entry) {
push_row(&mut text, &full, entry, format);
if format.recursive && entry.is_directory && !entry.is_link() {
subdirectories.push(full);
}
}
}
if let Err(error) = write_stdout(&text) {
let _ = reader.close().await;
return match error.kind() {
std::io::ErrorKind::BrokenPipe => Ok(()),
_ => Err(error.into()),
};
}
}
pending.extend(subdirectories.into_iter().rev());
}
Ok(())
}
fn child_path(parent: &str, entry: &DirectoryEntry) -> Option<String> {
if entry.name == "." || entry.name == ".." {
return None;
}
Some(if parent.is_empty() {
entry.name.clone()
} else {
format!("{parent}/{}", entry.name)
})
}
fn push_row(text: &mut String, path: &str, entry: &DirectoryEntry, format: LsFormat) {
use std::fmt::Write;
let name = if format.recursive { path } else { &entry.name };
if format.index {
let index = entry
.index_number
.map_or_else(|| "-".to_string(), |n| n.to_string());
let _ = write!(text, "{index:>12} ");
}
if format.long {
let _ = writeln!(
text,
"{} {:>15} {} {}",
if entry.is_directory { "d" } else { "-" },
output::bytes(entry.size),
output::timestamp(entry.modified),
name,
);
} else {
let _ = writeln!(text, "{name}");
}
}
fn write_stdout(text: &str) -> std::io::Result<()> {
use std::io::Write;
let mut stdout = std::io::stdout().lock();
stdout.write_all(text.as_bytes())?;
stdout.flush()
}
pub async fn stat(
base: &Target,
credentials: &Credentials,
paths: Vec<String>,
concurrency: usize,
as_json: bool,
) -> Result<()> {
let jobs: Vec<Job> = paths.iter().map(|path| Job::Stat(path.clone())).collect();
let outcomes = pool::run(base, credentials, concurrency, jobs, |_, _| {}).await?;
let mut failures = 0;
let mut items = Vec::new();
for (path, outcome) in paths.iter().zip(&outcomes) {
match outcome {
Outcome::Stat(info) => {
if as_json {
items.push(json!({
"path": path,
"isDirectory": info.is_directory,
"isLink": info.is_link(),
"reparseTag": info.reparse_tag.map(|tag| tag.0),
"size": info.size,
"modified": output::unix_seconds(info.modified),
"created": output::unix_seconds(info.created),
"accessed": output::unix_seconds(info.accessed),
"changed": output::unix_seconds(info.changed),
}));
} else {
println!("{path}");
println!(
" type: {}",
if info.is_directory {
"directory"
} else {
"file"
}
);
println!(" size: {} bytes", output::bytes(info.size));
println!(" created: {}", output::timestamp(info.created));
println!(" modified: {}", output::timestamp(info.modified));
println!(" accessed: {}", output::timestamp(info.accessed));
println!(" changed: {}", output::timestamp(info.changed));
}
}
other => {
failures += 1;
let message = other.error().unwrap_or("unknown error");
if as_json {
items.push(json!({ "path": path, "error": message }));
} else {
eprintln!("{path}: {message}");
}
}
}
}
if as_json {
println!("{}", serde_json::to_string_pretty(&items)?);
}
if failures > 0 {
anyhow::bail!("{failures} of {} paths failed", paths.len());
}
Ok(())
}
pub async fn realpath(target: &Target, credentials: &Credentials, as_json: bool) -> Result<()> {
let (mut client, mut tree) = pool::connect_all(target, credentials, 1)
.await?
.pop()
.expect("connect_all returns one connection");
let resolved = client
.resolve(&mut tree, &target.path)
.await
.with_context(|| format!("resolving {}", target.display()))?;
let (host, port) = match tree.server.rsplit_once(':') {
Some((host, port)) if port.parse::<u16>().is_ok() => {
(host.to_string(), port.parse().expect("checked"))
}
_ => (tree.server.clone(), target.port),
};
let canonical = Target {
host,
port,
share: tree.share_name.clone(),
..target.with_path(&resolved.path)
};
if as_json {
println!(
"{}",
serde_json::to_string_pretty(&json!({
"target": canonical.display(),
"share": canonical.share,
"path": resolved.path,
"isDirectory": resolved.info.is_directory,
}))?
);
} else {
println!("{}", canonical.display());
}
let _ = client.disconnect_share(&tree).await;
Ok(())
}
pub async fn df(target: &Target, credentials: &Credentials, as_json: bool) -> Result<()> {
let (mut client, mut tree) = pool::connect_all(target, credentials, 1)
.await?
.pop()
.expect("connect_all returns one connection");
let info = client
.fs_info(&mut tree)
.await
.with_context(|| format!("querying {}", target.display()))?;
let used = info.total_bytes.saturating_sub(info.total_free_bytes);
if as_json {
println!(
"{}",
serde_json::to_string_pretty(&json!({
"share": target.share,
"totalBytes": info.total_bytes,
"usedBytes": used,
"freeBytes": info.free_bytes,
"totalFreeBytes": info.total_free_bytes,
"bytesPerSector": info.bytes_per_sector,
"sectorsPerUnit": info.sectors_per_unit,
}))?
);
} else {
let percent = if info.total_bytes == 0 {
0.0
} else {
used as f64 * 100.0 / info.total_bytes as f64
};
println!("Share: {}", target.share);
println!("Total: {}", output::human_bytes(info.total_bytes));
println!("Used: {} ({percent:.0}%)", output::human_bytes(used));
println!("Available: {}", output::human_bytes(info.free_bytes));
}
let _ = client.disconnect_share(&tree).await;
Ok(())
}
pub async fn shares(host: &str, port: u16, credentials: &Credentials, as_json: bool) -> Result<()> {
let addr = format!("{host}:{port}");
let mut client = credentials
.connect(&addr)
.await
.with_context(|| format!("connecting to {addr}"))?;
let shares = client
.list_shares()
.await
.with_context(|| format!("listing shares on {addr}"))?;
if as_json {
let items: Vec<_> = shares
.iter()
.map(|share| {
json!({ "name": share.name, "type": share.share_type, "comment": share.comment })
})
.collect();
println!("{}", serde_json::to_string_pretty(&items)?);
} else {
for share in &shares {
if share.comment.is_empty() {
println!("{}", share.name);
} else {
println!("{:<20} {}", share.name, share.comment);
}
}
}
Ok(())
}