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
162
163
164
165
166
167
168
169
170
171
#![stable(feature = "steed", since = "1.0.0")]

use ctypes::{c_int, c_uint};
use io::{Error, Read, Write};
use linux::types::umode_t;
use {linux, io};

#[stable(feature = "steed", since = "1.0.0")]
pub struct File {
    fd: c_uint,
}

impl File {
    #[stable(feature = "steed", since = "1.0.0")]
    /// NOTE `path` must be null terminated
    pub fn create(path: &[u8]) -> io::Result<File> {
        OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(path)
    }

    /// NOTE `path` must be null terminated
    #[stable(feature = "steed", since = "1.0.0")]
    pub fn open(path: &[u8]) -> io::Result<File> {
        OpenOptions::new().read(true).open(path)
    }
}

#[stable(feature = "steed", since = "1.0.0")]
impl Drop for File {
    fn drop(&mut self) {
        unsafe { linux::close(self.fd) };
    }
}

#[stable(feature = "steed", since = "1.0.0")]
impl Read for File {
    fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
        match unsafe {
            linux::read(self.fd, buffer.as_mut_ptr() as *mut _, buffer.len())
        } {
            n if n >= 0 => Ok(n as usize),
            n => Err(Error::from_raw_os_error(-n as i32)),
        }
    }
}

#[stable(feature = "steed", since = "1.0.0")]
impl Write for File {
    fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
        match unsafe {
            linux::write(self.fd, buffer.as_ptr() as *const _, buffer.len())
        } {
            n if n >= 0 => Ok(n as usize),
            n => Err(Error::from_raw_os_error(-n as i32)),
        }
    }
}

#[stable(feature = "steed", since = "1.0.0")]
pub struct OpenOptions {
    read: bool,
    write: bool,
    append: bool,
    truncate: bool,
    create: bool,
    create_new: bool,
    custom_flags: i32,
    mode: umode_t,
}

impl OpenOptions {
    #[stable(feature = "steed", since = "1.0.0")]
    pub fn new() -> Self {
        OpenOptions {
            read: false,
            write: false,
            append: false,
            truncate: false,
            create: false,
            create_new: false,
            custom_flags: 0,
            mode: 0o666,
        }
    }

    #[stable(feature = "steed", since = "1.0.0")]
    pub fn read(&mut self, read: bool) -> &mut Self {
        self.read = read;
        self
    }

    #[stable(feature = "steed", since = "1.0.0")]
    pub fn write(&mut self, write: bool) -> &mut Self {
        self.write = write;
        self
    }

    #[stable(feature = "steed", since = "1.0.0")]
    pub fn append(&mut self, append: bool) -> &mut Self {
        self.append = append;
        self
    }

    #[stable(feature = "steed", since = "1.0.0")]
    pub fn truncate(&mut self, truncate: bool) -> &mut Self {
        self.truncate = truncate;
        self
    }

    #[stable(feature = "steed", since = "1.0.0")]
    pub fn create(&mut self, create: bool) -> &mut Self {
        self.create = create;
        self
    }

    #[stable(feature = "steed", since = "1.0.0")]
    pub fn open(&self, path: &[u8]) -> io::Result<File> {
        let flags = linux::O_CLOEXEC | self.get_access_mode()? |
                    self.get_creation_mode()? |
                    (self.custom_flags as c_int & !linux::O_ACCMODE);
        // TODO
        // fd.set_cloexec()?;

        match unsafe {
            linux::open(path.as_ptr() as *const _, flags, self.mode)
        } {
            n if n > 0 => Ok(File { fd: n as c_uint }),
            n => Err(Error::from_raw_os_error(-n as i32)),
        }
    }

    fn get_access_mode(&self) -> io::Result<c_int> {
        match (self.read, self.write, self.append) {
            (true, false, false) => Ok(linux::O_RDONLY),
            (false, true, false) => Ok(linux::O_WRONLY),
            (true, true, false) => Ok(linux::O_RDWR),
            (false, _, true) => Ok(linux::O_WRONLY | linux::O_APPEND),
            (true, _, true) => Ok(linux::O_RDWR | linux::O_APPEND),
            // FIXME error code
            (false, false, false) => Err(Error::from_raw_os_error(0)),
        }
    }

    // FIXME error code
    fn get_creation_mode(&self) -> io::Result<c_int> {
        match (self.write, self.append) {
            (true, false) => {}
            (false, false) => {
                if self.truncate || self.create || self.create_new {
                    return Err(Error::from_raw_os_error(0));
                }
            }
            (_, true) => {
                if self.truncate && !self.create_new {
                    return Err(Error::from_raw_os_error(0));
                }
            }
        }

        Ok(match (self.create, self.truncate, self.create_new) {
            (false, false, false) => 0,
            (true, false, false) => linux::O_CREAT,
            (false, true, false) => linux::O_TRUNC,
            (true, true, false) => linux::O_CREAT | linux::O_TRUNC,
            (_, _, true) => linux::O_CREAT | linux::O_EXCL,
        })
    }
}