use super::{Mount, WRITE3args, WriteStable, nfs_fh3, stable_how};
use crate::error::{NfsError, Result};
use crate::mount::{WriteOutcome, WriteStability};
use bytes::Bytes;
impl Mount {
pub async fn write_how(
&self,
fh: Bytes,
offset: u64,
data: Bytes,
stability: WriteStability,
) -> Result<WriteOutcome> {
if data.len() > u32::MAX as usize {
return Err(NfsError::InvalidInput(
"data length exceeds maximum".to_string(),
));
}
let count = data.len() as u32;
let stable = match stability {
WriteStability::Unstable => WriteStable::Unstable,
};
let ok = self
._write(WRITE3args {
file: nfs_fh3 { data: fh },
stable,
count,
data,
offset,
})
.await?;
let verifier = ok
.verf
.0
.as_ref()
.try_into()
.map_err(|_| NfsError::Xdr("WRITE verifier must be 8 bytes".to_string()))?;
Ok(WriteOutcome {
pnfs: None,
count: ok.count.0,
committed: match ok.committed {
stable_how::UNSTABLE => crate::WriteCommitted::Unstable,
stable_how::DATA_SYNC => crate::WriteCommitted::DataSync,
stable_how::FILE_SYNC => crate::WriteCommitted::FileSync,
},
verifier: Some(verifier),
})
}
}
#[cfg(test)]
#[cfg(not(target_arch = "wasm32"))] mod tests {
use super::*;
#[tokio::test]
async fn mount_write_fh_data_exceeding_max_length() {
let mount = Mount {
rpc: crate::rpc::Client::new_dummy().await,
auth: crate::rpc::auth::Auth::new_null(),
dir: "/".to_string(),
fh: Bytes::new(),
dircount: 512,
maxcount: 4096,
rsize: 8192,
wsize: 16384,
};
let data = vec![0u8; (u32::MAX as usize) + 1];
let res = mount
.write_how(Bytes::new(), 0, Bytes::from(data), WriteStability::Unstable)
.await;
assert!(matches!(res, Err(NfsError::InvalidInput(_))));
}
}