Skip to content

Commit

Permalink
refactor: use pread and pwrite for legacy path (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
ihciah committed Jul 12, 2023
1 parent d1e80e2 commit 8b95e3f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 46 deletions.
41 changes: 17 additions & 24 deletions monoio/src/driver/op/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,23 @@ impl<T: IoBufMut> OpAble for Read<T> {
#[cfg(all(unix, feature = "legacy"))]
fn legacy_call(&mut self) -> io::Result<u32> {
let fd = self.fd.as_raw_fd();
if self.offset != 0 {
let seek_offset = libc::off_t::try_from(self.offset)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "offset too big"))?;
let neg_seek_offset = seek_offset
.checked_neg()
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "offset too big"))?;
syscall_u32!(lseek(fd, seek_offset, libc::SEEK_CUR))?;
syscall_u32!(read(
fd,
self.buf.write_ptr() as _,
self.buf.bytes_total().min(u32::MAX as usize)
))
.map_err(|e| {
// seek back if read fail...
let _ = syscall_u32!(lseek(fd, neg_seek_offset, libc::SEEK_CUR));
e
})
} else {
syscall_u32!(read(
fd,
self.buf.write_ptr() as _,
self.buf.bytes_total().min(u32::MAX as usize)
))
}
let seek_offset = libc::off_t::try_from(self.offset)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "offset too big"))?;
#[cfg(not(target_os = "macos"))]
return syscall_u32!(pread64(
fd,
self.buf.write_ptr() as _,
self.buf.bytes_total(),
seek_offset
));

#[cfg(target_os = "macos")]
return syscall_u32!(pread(
fd,
self.buf.write_ptr() as _,
self.buf.bytes_total(),
seek_offset
));
}
}

Expand Down
37 changes: 15 additions & 22 deletions monoio/src/driver/op/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,21 @@ impl<T: IoBuf> OpAble for Write<T> {
let fd = self.fd.as_raw_fd();
let seek_offset = libc::off_t::try_from(self.offset)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "offset too big"))?;
let neg_seek_offset = seek_offset
.checked_neg()
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "offset too big"))?;
if self.offset != 0 {
syscall_u32!(lseek(fd, seek_offset, libc::SEEK_CUR))?;
syscall_u32!(write(
fd,
self.buf.read_ptr() as _,
self.buf.bytes_init().min(u32::MAX as usize)
))
.map_err(|e| {
// seek back if read fail...
let _ = syscall_u32!(lseek(fd, neg_seek_offset, libc::SEEK_CUR));
e
})
} else {
syscall_u32!(write(
fd,
self.buf.read_ptr() as _,
self.buf.bytes_init().min(u32::MAX as usize)
))
}
#[cfg(not(target_os = "macos"))]
return syscall_u32!(pwrite64(
fd,
self.buf.read_ptr() as _,
self.buf.bytes_init(),
seek_offset
));

#[cfg(target_os = "macos")]
return syscall_u32!(pwrite(
fd,
self.buf.read_ptr() as _,
self.buf.bytes_init(),
seek_offset
));
}
}

Expand Down

0 comments on commit 8b95e3f

Please sign in to comment.