use std::sync::Arc;
use crate::proto::header::Smb2Header;
use crate::proto::messages::{CloseRequest, CloseResponse, FileId};
use crate::conn::pending::Stop;
use crate::conn::state::Connection;
use crate::dispatch::HandlerResponse;
use crate::files::{WatchReg, close_open};
use crate::handlers::shared::lookup_session_tree;
use crate::ntstatus;
use crate::server::ServerState;
const FLAG_POSTQUERY_ATTRIB: u16 = 0x0001;
pub async fn handle(
_server: &Arc<ServerState>,
conn: &Arc<Connection>,
hdr: &Smb2Header,
body: &[u8],
) -> HandlerResponse {
let req = match CloseRequest::parse(body) {
Ok(r) => r,
Err(_) => return HandlerResponse::err(ntstatus::STATUS_INVALID_PARAMETER),
};
let tree_arc = match lookup_session_tree(conn, hdr).await {
Ok(t) => t,
Err(s) => return HandlerResponse::err(s),
};
let removed = {
let tree = tree_arc.write().await;
let mut opens = tree.opens.write().await;
opens.remove(&req.file_id)
};
let open_arc = match removed {
Some(o) => o,
None => return HandlerResponse::err(ntstatus::STATUS_FILE_CLOSED),
};
let watch = open_arc.write().await.watch.take();
if let Some(watch) = &watch {
watch.leave_table();
}
let ended = EndWaiters {
conn: Arc::clone(conn),
file_id: req.file_id,
watch,
};
let want_attrs = req.flags & FLAG_POSTQUERY_ATTRIB != 0;
let info = close_open(&open_arc, want_attrs).await;
let resp = match info {
Some(info) => CloseResponse {
structure_size: 60,
flags: FLAG_POSTQUERY_ATTRIB,
reserved: 0,
creation_time: info.creation_time,
last_access_time: info.last_access_time,
last_write_time: info.last_write_time,
change_time: info.change_time,
allocation_size: info.sizes().1,
end_of_file: info.sizes().0,
file_attributes: info.attributes(),
},
None => CloseResponse::new(),
};
let mut buf = Vec::new();
resp.write_to(&mut buf).expect("encode");
let mut response = HandlerResponse::ok(buf);
response.after_send.push(Box::new(ended));
response
}
struct EndWaiters {
conn: Arc<Connection>,
file_id: FileId,
watch: Option<WatchReg>,
}
impl Drop for EndWaiters {
fn drop(&mut self) {
self.conn
.pending
.stop_where(Stop::Retry, |op| op.file_id == Some(self.file_id));
drop(self.watch.take());
}
}