use super::{Mount, WRITE3args, WriteStable, nfs_fh3, stable_how};
use crate::error::{NfsError, Result};
use bytes::Bytes;
#[allow(unused)]
impl Mount {
pub async fn write_path(&self, path: &str, offset: u64, data: Bytes) -> Result<u32> {
self.write(self.lookup_path(path).await?.fh, offset, data)
.await
}
pub async fn write(&self, fh: Bytes, offset: u64, data: Bytes) -> Result<u32> {
if data.len() > u32::MAX as usize {
return Err(NfsError::InvalidInput(
"data length exceeds maximum".to_string(),
));
}
let count = data.len() as u32;
let args = WRITE3args {
file: nfs_fh3 { data: fh.clone() },
stable: WriteStable::FileSync,
count,
data,
offset,
};
let ok = self._write(args).await?;
if ok.committed != stable_how::FILE_SYNC {
tracing::debug!(
committed = ?ok.committed,
offset,
count = ok.count.0,
"server downgraded write stability, issuing COMMIT"
);
self.commit(fh, offset, ok.count.0).await?;
}
Ok(ok.count.0)
}
}
#[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(Bytes::new(), 0, Bytes::from(data)).await;
assert!(matches!(res, Err(NfsError::InvalidInput(_))));
}
}