mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-28 12:10:37 +00:00
address review: move clamping logic
This commit is contained in:
parent
ff699c0594
commit
c2ed6599b2
@ -57,20 +57,18 @@ async fn write_uring(path: &Path, mut buf: OwnedBuf) -> io::Result<()> {
|
||||
.into();
|
||||
|
||||
let total: usize = buf.as_ref().len();
|
||||
let mut offset: usize = 0;
|
||||
while offset < total {
|
||||
// There is a cap on how many bytes we can write in a single uring write operation.
|
||||
// ref: https://github.com/axboe/liburing/discussions/497
|
||||
let len = std::cmp::min(total - offset, u32::MAX as usize) as u32;
|
||||
|
||||
let (n, _buf, _fd) = Op::write_at(fd, buf, offset, len, offset as u64)?.await?;
|
||||
let mut buf_offset: usize = 0;
|
||||
let mut file_offset: u64 = 0;
|
||||
while buf_offset < total {
|
||||
let (n, _buf, _fd) = Op::write_at(fd, buf, buf_offset, file_offset)?.await?;
|
||||
if n == 0 {
|
||||
return Err(io::ErrorKind::WriteZero.into());
|
||||
}
|
||||
|
||||
buf = _buf;
|
||||
fd = _fd;
|
||||
offset += n as usize;
|
||||
buf_offset += n as usize;
|
||||
file_offset += n as u64;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -28,15 +28,16 @@ impl Cancellable for Write {
|
||||
}
|
||||
|
||||
impl Op<Write> {
|
||||
/// Issue a write that starts at `buf_offset` within `buf` and writes `len` bytes
|
||||
/// Issue a write that starts at `buf_offset` within `buf` and writes some bytes
|
||||
/// into `file` at `file_offset`.
|
||||
pub(crate) fn write_at(
|
||||
fd: OwnedFd,
|
||||
buf: OwnedBuf,
|
||||
buf_offset: usize,
|
||||
len: u32,
|
||||
file_offset: u64,
|
||||
) -> io::Result<Self> {
|
||||
let len = u32::try_from(buf.as_ref().len() - buf_offset).unwrap_or(u32::MAX);
|
||||
|
||||
let ptr = buf.as_ref()[buf_offset..buf_offset + len as usize].as_ptr();
|
||||
|
||||
let sqe = opcode::Write::new(types::Fd(fd.as_raw_fd()), ptr, len)
|
||||
|
Loading…
x
Reference in New Issue
Block a user