Function nc::pwritev2[][src]

pub fn pwritev2(
    fd: i32,
    vec: &[iovec_t],
    pos_l: usize,
    pos_h: usize,
    flags: rwf_t
) -> Result<ssize_t, Errno>
Expand description

Write to a file descriptor without changing file offset.

let path = "/etc/passwd";
let ret = nc::openat(nc::AT_FDCWD, path, nc::O_RDONLY, 0);
assert!(ret.is_ok());
let fd = ret.unwrap();
let mut buf = [[0_u8; 64]; 4];
let capacity = 4 * 64;
let mut iov = Vec::with_capacity(buf.len());
for ref mut item in (&mut buf).iter() {
    iov.push(nc::iovec_t {
        iov_len: item.len(),
        iov_base: item.as_ptr() as usize,
    });
}
let ret = nc::readv(fd, &mut iov);
assert!(ret.is_ok());
assert_eq!(ret, Ok(capacity as nc::ssize_t));
assert!(nc::close(fd).is_ok());

let path_out = "/tmp/nc-pwritev2";
let ret = nc::openat(nc::AT_FDCWD, path_out, nc::O_WRONLY | nc::O_CREAT, 0o644);
assert!(ret.is_ok());
let fd = ret.unwrap();
let flags = nc::RWF_DSYNC | nc::RWF_APPEND;
let ret = nc::pwritev2(fd, &iov, 0, iov.len() - 1, flags);
assert!(ret.is_ok());
assert_eq!(ret, Ok(capacity as nc::ssize_t));
assert!(nc::close(fd).is_ok());
assert!(nc::unlinkat(nc::AT_FDCWD, path_out, 0).is_ok());