1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Silence invalid warnings due to rust-lang/rust#16719
#![allow(improper_ctypes)]

use {Result, Error};
use errno::Errno;
use libc::{c_int, c_void, size_t, off_t};
use std::marker::PhantomData;
use std::os::unix::io::RawFd;

mod ffi {
    use super::IoVec;
    use libc::{ssize_t, c_int, size_t, off_t, c_void};
    use std::os::unix::io::RawFd;

    extern {
        // vectorized version of write
        // doc: http://man7.org/linux/man-pages/man2/writev.2.html
        pub fn writev(fd: RawFd, iov: *const IoVec<&[u8]>, iovcnt: c_int) -> ssize_t;

        // 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;

        // vectorized write at a specified offset
        // doc: http://man7.org/linux/man-pages/man2/pwritev.2.html
        #[cfg(target_os = "linux")]
        pub fn pwritev(fd: RawFd, iov: *const IoVec<&[u8]>, iovcnt: c_int,
                       offset: off_t) -> ssize_t;

        // vectorized read at a specified offset
        // doc: http://man7.org/linux/man-pages/man2/preadv.2.html
        #[cfg(target_os = "linux")]
        pub fn preadv(fd: RawFd, iov: *const IoVec<&mut [u8]>, iovcnt: c_int,
                      offset: off_t) -> 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;
    }
}

pub fn writev(fd: RawFd, iov: &[IoVec<&[u8]>]) -> Result<usize> {
    let res = unsafe { ffi::writev(fd, iov.as_ptr(), iov.len() as c_int) };

    if res < 0 {
        return Err(Error::Sys(Errno::last()));
    }

    return Ok(res as usize)
}

pub fn readv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>]) -> Result<usize> {
    let res = unsafe { ffi::readv(fd, iov.as_ptr(), iov.len() as c_int) };
    if res < 0 {
        return Err(Error::Sys(Errno::last()));
    }

    return Ok(res as usize)
}

#[cfg(target_os = "linux")]
pub fn pwritev(fd: RawFd, iov: &[IoVec<&[u8]>],
               offset: off_t) -> Result<usize> {
    let res = unsafe {
        ffi::pwritev(fd, iov.as_ptr(), iov.len() as c_int, offset)
    };
    if res < 0 {
        Err(Error::Sys(Errno::last()))
    } else {
        Ok(res as usize)
    }
}

#[cfg(target_os = "linux")]
pub fn preadv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>],
              offset: off_t) -> Result<usize> {
    let res = unsafe {
        ffi::preadv(fd, iov.as_ptr(), iov.len() as c_int, offset)
    };
    if res < 0 {
        Err(Error::Sys(Errno::last()))
    } else {
        Ok(res as usize)
    }
}

pub fn pwrite(fd: RawFd, buf: &[u8], offset: off_t) -> 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: off_t) -> 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,
    iov_len: size_t,
    phantom: PhantomData<T>
}

impl<T> IoVec<T> {
    #[inline]
    pub fn as_slice<'a>(&'a self) -> &'a [u8] {
        use std::slice;

        unsafe {
            slice::from_raw_parts(
                self.iov_base as *const u8,
                self.iov_len as usize)
        }
    }
}

impl<'a> IoVec<&'a [u8]> {
    pub fn from_slice(buf: &'a [u8]) -> IoVec<&'a [u8]> {
        IoVec {
            iov_base: buf.as_ptr() as *mut c_void,
            iov_len: buf.len() as size_t,
            phantom: PhantomData
        }
    }
}

impl<'a> IoVec<&'a mut [u8]> {
    pub fn from_mut_slice(buf: &'a mut [u8]) -> IoVec<&'a mut [u8]> {
        IoVec {
            iov_base: buf.as_ptr() as *mut c_void,
            iov_len: buf.len() as size_t,
            phantom: PhantomData
        }
    }
}

#[test]
pub fn test_size_of_io_vec() {
    use nixtest;
    nixtest::assert_size_of::<IoVec<&[u8]>>("iovec");
}