diff options
author | Florian Hartwig <florian.j.hartwig@gmail.com> | 2015-06-05 23:25:53 +0200 |
---|---|---|
committer | Florian Hartwig <florian.j.hartwig@gmail.com> | 2015-06-05 23:52:19 +0200 |
commit | d8010d83eae7865d3d7a67ee6d5ca2304a0970ec (patch) | |
tree | 18af0ee796448bb652ac85222b166a0fe8f58211 /src | |
parent | 30d02b88a033cf0e9bb0fd1adb4d81c5c8dc3437 (diff) | |
download | nix-d8010d83eae7865d3d7a67ee6d5ca2304a0970ec.zip |
Add pwrite and pread
Diffstat (limited to 'src')
-rw-r--r-- | src/sys/uio.rs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/sys/uio.rs b/src/sys/uio.rs index 9f65cf09..99075b79 100644 --- a/src/sys/uio.rs +++ b/src/sys/uio.rs @@ -9,7 +9,7 @@ use std::os::unix::io::RawFd; mod ffi { use super::IoVec; - use libc::{ssize_t, c_int}; + use libc::{ssize_t, c_int, size_t, off_t, c_void}; use std::os::unix::io::RawFd; extern { @@ -20,6 +20,16 @@ mod ffi { // vectorized version of read // doc: http://man7.org/linux/man-pages/man2/readv.2.html pub fn readv(fd: RawFd, iov: *const IoVec<&mut [u8]>, iovcnt: c_int) -> ssize_t; + + // write to a file at a specified offset + // doc: http://man7.org/linux/man-pages/man2/pwrite.2.html + pub fn pwrite(fd: RawFd, buf: *const c_void, nbyte: size_t, + offset: off_t) -> ssize_t; + + // read from a file at a specified offset + // doc: http://man7.org/linux/man-pages/man2/pread.2.html + pub fn pread(fd: RawFd, buf: *mut c_void, nbyte: size_t, offset: off_t) + -> ssize_t; } } @@ -42,6 +52,30 @@ pub fn readv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>]) -> Result<usize> { return Ok(res as usize) } +pub fn pwrite(fd: RawFd, buf: &[u8], offset: i64) -> Result<usize> { + let res = unsafe { + ffi::pwrite(fd, buf.as_ptr() as *const c_void, buf.len() as size_t, + offset) + }; + if res < 0 { + Err(Error::Sys(Errno::last())) + } else { + Ok(res as usize) + } +} + +pub fn pread(fd: RawFd, buf: &mut [u8], offset: i64) -> Result<usize>{ + let res = unsafe { + ffi::pread(fd, buf.as_mut_ptr() as *mut c_void, buf.len() as size_t, + offset) + }; + if res < 0 { + Err(Error::Sys(Errno::last())) + } else { + Ok(res as usize) + } +} + #[repr(C)] pub struct IoVec<T> { iov_base: *mut c_void, |