use std::sync::Arc;
use std::sync::atomic::Ordering;
use tokio::sync::{RwLock, SemaphorePermit};
use tracing::warn;
use super::{Call, Ends, Guards, Side, copy_response, find_source, permitted, reply_status};
use crate::access_mask::FILE_WRITE_DATA;
use crate::backend::{FileInfo, FileTimes, VolumeInfo};
use crate::conn::pending::{Stop, StopReceiver, stopped};
use crate::conn::state::{Connection, Open};
use crate::dispatch::HandlerResponse;
use crate::error::SmbError;
use crate::handlers::create::{CopyStream, WRITE_THROUGH_OPTIONS, open_copy_stream};
use crate::handlers::shared::{discard_open, lookup_open, lookup_tree};
use crate::handlers::{Outcome, Pending};
use crate::ntstatus;
use crate::proto::messages::copychunk::{Chunk, CopychunkResponse, MAX_CHUNK_LEN, RESUME_KEY_LEN};
use crate::proto::messages::{FileId, Fsctl};
use crate::server::{ServerState, ShareBindings};
use crate::stream::StreamName;
const PIECE: u64 = MAX_CHUNK_LEN as u64;
enum Admitted {
Copy,
Itself(u64),
}
pub(super) async fn start(
server: &Arc<ServerState>,
conn: &Arc<Connection>,
call: &Call<'_>,
key: [u8; RESUME_KEY_LEN],
source: Side,
target: Side,
) -> Outcome {
let Call {
hdr, req, retained, ..
} = *call;
let (file_id, write_through) = {
let guards = Guards::take(&source.open, &target.open).await;
let file_id = guards.target().file_id;
match admit(req.fsctl(), &source.share, &target.share, &guards).await {
Ok(Admitted::Copy) => {}
Ok(Admitted::Itself(size)) => {
return Outcome::Done(reply(req.ctl_code, file_id, ntstatus::STATUS_SUCCESS, size));
}
Err(status) => return Outcome::Done(HandlerResponse::err(status)),
}
let write_through = guards.target().create_options & WRITE_THROUGH_OPTIONS != 0;
(file_id, write_through)
};
let Some((op, stop)) = conn.pending.reserve(hdr, Some(file_id), retained) else {
warn!("too many waiting requests on the connection");
return Outcome::Done(HandlerResponse::err(
ntstatus::STATUS_INSUFFICIENT_RESOURCES,
));
};
let job = Job {
server: Arc::clone(server),
conn: Arc::clone(conn),
session_id: hdr.session_id,
ctl_code: req.ctl_code,
key,
file_id,
write_through,
source,
target,
};
Outcome::Pending(Pending {
op,
work: Box::pin(job.run(stop)),
})
}
async fn admit(
code: Fsctl,
source_share: &Arc<ShareBindings>,
target_share: &Arc<ShareBindings>,
guards: &Guards<'_>,
) -> Result<Admitted, u32> {
let (source, target) = (guards.source(), guards.target());
if !permitted(code, source, target)
|| source.on_stream
|| target.on_stream
|| target.granted_access & FILE_WRITE_DATA == 0
{
return Err(ntstatus::STATUS_ACCESS_DENIED);
}
let ends = guards.ends(target_share)?;
let info = ends.source.stat().await.map_err(|e| ends.status_of(e))?;
let itself = ends.source_file.same_data_as(ends.target_file)
|| same_file_on_two_shares(&ends, &info, source_share, target_share)
.await
.map_err(|e| ends.status_of(e))?;
if itself {
return Ok(Admitted::Itself(info.end_of_file));
}
Ok(Admitted::Copy)
}
async fn same_file_on_two_shares(
ends: &Ends<'_>,
source: &FileInfo,
source_share: &Arc<ShareBindings>,
target_share: &Arc<ShareBindings>,
) -> Result<bool, SmbError> {
if Arc::ptr_eq(source_share, target_share) || source.file_index == 0 {
return Ok(false);
}
if ends.target.stat().await?.file_index != source.file_index {
return Ok(false);
}
let (Ok(from), Ok(to)) = (
source_share.backend.volume_info().await,
target_share.backend.volume_info().await,
) else {
return Ok(false);
};
Ok(from.serial_number == to.serial_number && from != VolumeInfo::default())
}
fn reply(ctl_code: u32, file_id: FileId, status: u32, bytes: u64) -> HandlerResponse {
copy_response(
ctl_code,
file_id,
status,
CopychunkResponse {
chunks_written: 0,
chunk_bytes_written: 0,
total_bytes_written: u32::try_from(bytes).unwrap_or(u32::MAX),
},
)
}
enum Halt {
Stopped(u32),
Failed(u32, u64),
}
impl Halt {
fn failed(status: u32) -> Self {
Self::Failed(status, 0)
}
fn after(self, copied: u64) -> Self {
match self {
Self::Failed(status, _) => Self::Failed(status, copied),
halt => halt,
}
}
}
impl Ends<'_> {
fn halt(&self, error: SmbError) -> Halt {
Halt::failed(self.status_of(error))
}
}
struct StreamOpen {
tree_id: u32,
file_id: FileId,
open: Arc<RwLock<Open>>,
}
struct Job {
server: Arc<ServerState>,
conn: Arc<Connection>,
session_id: u64,
ctl_code: u32,
key: [u8; RESUME_KEY_LEN],
file_id: FileId,
write_through: bool,
source: Side,
target: Side,
}
impl Job {
async fn run(self, mut stop: StopReceiver) -> HandlerResponse {
match self.copy_file(&mut stop).await {
Ok(size) => reply(self.ctl_code, self.file_id, ntstatus::STATUS_SUCCESS, size),
Err(Halt::Stopped(status)) => HandlerResponse::err(status),
Err(Halt::Failed(status, copied)) => reply(self.ctl_code, self.file_id, status, copied),
}
}
async fn copy_file(&self, stop: &mut StopReceiver) -> Result<u64, Halt> {
let info = self.empty_target(stop).await?;
let size = info.end_of_file;
self.copy_data(stop, &self.source.open, &self.target.open, size)
.await?;
self.finish(stop, &info)
.await
.map_err(|halt| halt.after(size))?;
Ok(size)
}
async fn finish(&self, stop: &mut StopReceiver, info: &FileInfo) -> Result<(), Halt> {
if self.source.share.named_streams && self.target.share.named_streams {
self.copy_streams(stop).await?;
}
self.checkpoint(stop).await?;
self.set_times(info).await;
Ok(())
}
async fn checkpoint(&self, stop: &StopReceiver) -> Result<(), Halt> {
if *stop.borrow() == Some(Stop::Cancelled) {
return Err(Halt::Stopped(ntstatus::STATUS_CANCELLED));
}
let tree = lookup_tree(&self.conn, self.session_id, self.target.tree_id)
.await
.map_err(Halt::Stopped)?;
if lookup_open(&tree, self.file_id).await.is_none() {
return Err(Halt::Stopped(ntstatus::STATUS_FILE_CLOSED));
}
if find_source(&self.server, &self.conn, self.session_id, &self.key)
.await
.is_none()
{
return Err(Halt::Stopped(ntstatus::STATUS_OBJECT_NAME_NOT_FOUND));
}
Ok(())
}
fn ends<'a>(&'a self, guards: &'a Guards<'_>) -> Result<Ends<'a>, Halt> {
guards.ends(&self.target.share).map_err(Halt::Stopped)
}
async fn take_turn(&self, stop: &mut StopReceiver) -> Result<SemaphorePermit<'_>, Halt> {
let acquire = self.conn.copy_turn.acquire();
tokio::pin!(acquire);
let turn = loop {
tokio::select! {
biased;
why = stopped(stop) => {
if why == Stop::Cancelled {
return Err(Halt::Stopped(ntstatus::STATUS_CANCELLED));
}
self.checkpoint(stop).await?;
}
turn = &mut acquire => {
break turn.expect("the copy turn is never closed");
}
}
};
self.checkpoint(stop).await?;
Ok(turn)
}
async fn empty_target(&self, stop: &mut StopReceiver) -> Result<FileInfo, Halt> {
let _turn = self.take_turn(stop).await?;
let guards = Guards::take(&self.source.open, &self.target.open).await;
let ends = self.ends(&guards)?;
let info = ends.source.stat().await.map_err(|e| ends.halt(e))?;
ends.breaking_leases(ends.target.truncate(0))
.await
.map_err(|e| ends.halt(e))?;
guards.target().wrote.store(true, Ordering::Release);
Ok(info)
}
async fn copy_pieces(
&self,
stop: &mut StopReceiver,
from: &Arc<RwLock<Open>>,
to: &Arc<RwLock<Open>>,
size: u64,
) -> Result<(), Halt> {
let mut copied = 0;
while copied < size {
let _turn = self.take_turn(stop).await?;
let guards = Guards::take(from, to).await;
let ends = self.ends(&guards)?;
let chunk = Chunk {
source_offset: copied,
target_offset: copied,
length: (size - copied).min(PIECE) as u32,
};
let run = ends.copy_chunks(guards.target(), &[chunk]).await;
copied += u64::from(run.written());
if let Some(failure) = run.failure {
return Err(Halt::Failed(failure.status, copied));
}
}
Ok(())
}
async fn copy_data(
&self,
stop: &mut StopReceiver,
from: &Arc<RwLock<Open>>,
to: &Arc<RwLock<Open>>,
size: u64,
) -> Result<(), Halt> {
self.copy_pieces(stop, from, to, size).await?;
if self.write_through {
self.flush(stop, from, to)
.await
.map_err(|halt| halt.after(size))?;
}
Ok(())
}
async fn flush(
&self,
stop: &mut StopReceiver,
from: &Arc<RwLock<Open>>,
to: &Arc<RwLock<Open>>,
) -> Result<(), Halt> {
let _turn = self.take_turn(stop).await?;
let guards = Guards::take(from, to).await;
let ends = self.ends(&guards)?;
ends.target.flush().await.map_err(|e| ends.halt(e))
}
async fn copy_streams(&self, stop: &mut StopReceiver) -> Result<(), Halt> {
let streams = {
let guards = Guards::take(&self.source.open, &self.target.open).await;
let ends = self.ends(&guards)?;
ends.source
.streams()
.await
.map_err(|e| Halt::failed(reply_status(e.to_nt_status(), true)))?
};
for stream in streams {
self.copy_stream(stop, &stream.name).await?;
}
Ok(())
}
async fn copy_stream(&self, stop: &mut StopReceiver, name: &StreamName) -> Result<(), Halt> {
self.checkpoint(stop).await?;
let source = self.open_stream(stop, &self.source, name, false).await?;
let target = match self.open_stream(stop, &self.target, name, true).await {
Ok(target) => target,
Err(halt) => {
self.close_stream(source).await;
return Err(halt);
}
};
let copied = match self.empty_stream(stop, &source.open, &target.open).await {
Ok(()) => {
self.copy_stream_data(stop, &source.open, &target.open)
.await
}
Err(halt) => Err(halt),
};
self.close_stream(target).await;
self.close_stream(source).await;
copied
}
async fn open_stream(
&self,
stop: &mut StopReceiver,
side: &Side,
name: &StreamName,
write: bool,
) -> Result<StreamOpen, Halt> {
let path = side.open.read().await.path();
let Some(path) = path else {
self.checkpoint(stop).await?;
return Err(Halt::Stopped(ntstatus::STATUS_FILE_CLOSED));
};
let stream = CopyStream {
session_id: self.session_id,
tree_id: side.tree_id,
path,
name: name.clone(),
write,
};
let (file_id, open) = match open_copy_stream(&self.server, &self.conn, &stream, stop).await
{
Ok(opened) => opened,
Err(status) => {
self.checkpoint(stop).await?;
return Err(Halt::failed(reply_status(status, true)));
}
};
let opened = StreamOpen {
tree_id: side.tree_id,
file_id,
open,
};
if !on_file_of(&side.open, &opened.open).await {
self.close_stream(opened).await;
return Err(Halt::failed(ntstatus::STATUS_OBJECT_NAME_NOT_FOUND));
}
Ok(opened)
}
async fn empty_stream(
&self,
stop: &mut StopReceiver,
from: &Arc<RwLock<Open>>,
to: &Arc<RwLock<Open>>,
) -> Result<(), Halt> {
if to.read().await.created {
return Ok(());
}
let _turn = self.take_turn(stop).await?;
let guards = Guards::take(from, to).await;
let ends = self.ends(&guards)?;
ends.breaking_leases(ends.target.truncate(0))
.await
.map_err(|e| ends.halt(e))?;
guards.target().resized.store(true, Ordering::Release);
Ok(())
}
async fn close_stream(&self, stream: StreamOpen) {
if let Ok(tree) = lookup_tree(&self.conn, self.session_id, stream.tree_id).await {
discard_open(&tree, stream.file_id).await;
}
}
async fn copy_stream_data(
&self,
stop: &mut StopReceiver,
from: &Arc<RwLock<Open>>,
to: &Arc<RwLock<Open>>,
) -> Result<(), Halt> {
let size = {
let guards = Guards::take(from, to).await;
let ends = self.ends(&guards)?;
let info = ends.source.stat().await.map_err(|e| ends.halt(e))?;
info.end_of_file
};
self.copy_data(stop, from, to, size).await
}
async fn set_times(&self, info: &FileInfo) {
let known = |time: u64| (time != 0).then_some(time);
let times = FileTimes {
creation_time: known(info.creation_time),
last_write_time: known(info.last_write_time),
..FileTimes::default()
};
if times == FileTimes::default() {
return;
}
let mut open = self.target.open.write().await;
let Some(handle) = open.handle.as_ref() else {
return;
};
if let Err(e) = handle.set_times(times).await {
warn!(error = %e, path = %open.path().unwrap_or_default(), "times of a copied file not set");
return;
}
if times.last_write_time.is_some() {
open.fixed_times.last_write_time = times.last_write_time;
}
}
}
async fn on_file_of(open: &Arc<RwLock<Open>>, stream: &Arc<RwLock<Open>>) -> bool {
let guards = Guards::take(open, stream).await;
matches!(
(guards.source().file.as_ref(), guards.target().file.as_ref()),
(Some(file), Some(on)) if file.same_file_as(on)
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_count_of_a_file_over_four_gib_saturates() {
let response = reply(
Fsctl::SRV_COPYCHUNK,
FileId::default(),
ntstatus::STATUS_SUCCESS,
5 << 30,
);
let output = &response.body[response.body.len() - 12..];
assert_eq!(output[..8], [0; 8]);
assert_eq!(output[8..], u32::MAX.to_le_bytes());
}
}