diff --git a/src/header/netdb/linux.rs b/src/header/netdb/linux.rs index 41d673034d0c58247bf2dc54eed9f117458d2e2d..400c07a6c7d70dea136c09b85abcdc25f1d05d8d 100644 --- a/src/header/netdb/linux.rs +++ b/src/header/netdb/linux.rs @@ -1,24 +1,27 @@ use alloc::string::String; use c_str::CString; use header::fcntl; -use platform::rawfile::RawFile; -use platform::rlb::RawLineBuffer; -use platform::Line; +use fs::File; +use io::{BufRead, BufReader}; pub fn get_dns_server() -> String { - let fd = match RawFile::open( + let file = match File::open( &CString::new("/etc/resolv.conf").unwrap(), - fcntl::O_RDONLY, - 0, + fcntl::O_RDONLY ) { - Ok(fd) => fd, + Ok(file) => file, Err(_) => return String::new(), // TODO: better error handling }; + let mut file = BufReader::new(file); - let mut rlb = RawLineBuffer::new(*fd); - while let Line::Some(line) = rlb.next() { + for line in file.split(b'\n') { + let mut line = match line { + Ok(line) => line, + Err(_) => return String::new() // TODO: pls handle errors + }; if line.starts_with(b"nameserver ") { - return String::from_utf8(line[11..].to_vec()).unwrap_or_default(); + line.drain(..11); + return String::from_utf8(line).unwrap_or_default(); } } diff --git a/src/header/netdb/redox.rs b/src/header/netdb/redox.rs index db0b452ab7201f174f65d6253417c3ee3c01a158..4d813957b8ebdc2a554751ee0bd579dd5391078a 100644 --- a/src/header/netdb/redox.rs +++ b/src/header/netdb/redox.rs @@ -1,7 +1,12 @@ use alloc::string::String; use c_str::CString; -use platform::rawfile::file_read_all; +use fs::File; +use header::fcntl; +use io::Read; pub fn get_dns_server() -> String { - String::from_utf8(file_read_all(&CString::new("/etc/net/dns").unwrap()).unwrap()).unwrap() + let mut string = String::new(); + let mut file = File::open(&CString::new("/etc/net/dns").unwrap(), fcntl::O_RDONLY).unwrap(); // TODO: error handling + file.read_to_string(&mut string).unwrap(); // TODO: error handling + string } diff --git a/src/header/pwd/mod.rs b/src/header/pwd/mod.rs index da297f17147eb79f1beb4dad6e452c8396f6192a..e4e781684e0dc3733494d27148508bd95c3aa27c 100644 --- a/src/header/pwd/mod.rs +++ b/src/header/pwd/mod.rs @@ -3,10 +3,11 @@ use core::ptr; use c_str::CStr; +use fs::File; use header::{errno, fcntl}; -use platform; +use io::{BufRead, BufReader}; use platform::types::*; -use platform::{Line, RawFile, RawLineBuffer}; +use platform; #[cfg(target_os = "linux")] mod linux; @@ -55,26 +56,27 @@ where // TODO F: FnMut(impl Iterator<Item = &[u8]>) -> bool F: FnMut(&[&[u8]]) -> bool, { - let file = match RawFile::open( + let file = match File::open( unsafe { CStr::from_bytes_with_nul_unchecked(b"/etc/passwd\0") }, - fcntl::O_RDONLY, - 0, + fcntl::O_RDONLY ) { Ok(file) => file, Err(_) => return OptionPasswd::Error, }; - let mut rlb = RawLineBuffer::new(*file); + let file = BufReader::new(file); - loop { - let line = match rlb.next() { - Line::Error => return OptionPasswd::Error, - Line::EOF => return OptionPasswd::NotFound, - Line::Some(line) => line, + for line in file.split(b'\n') { + let line = match line { + Ok(line) => line, + Err(err) => unsafe { + platform::errno = errno::EIO; + return OptionPasswd::Error; + } }; // Parse into passwd - let mut parts: [&[u8]; 7] = sys::split(line); + let mut parts: [&[u8]; 7] = sys::split(&line); if !callback(&parts) { continue; @@ -145,6 +147,7 @@ where return OptionPasswd::Found(alloc); } + OptionPasswd::NotFound } #[no_mangle] diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index 8c20ba6e0d83403b42b358bace3a10832d6b1b9b..aa0da97d78a27cc800c0f1084130d9eac3a7acfe 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -271,12 +271,7 @@ pub extern "C" fn fgets(original: *mut c_char, max: c_int, stream: *mut FILE) -> let (read, exit) = { let mut buf = match stream.fill_buf() { Ok(buf) => buf, - Err(err) => { - unsafe { - platform::errno = errno::EIO; - } - return ptr::null_mut(); - } + Err(_) => return ptr::null_mut() }; if buf.is_empty() { break; @@ -393,12 +388,7 @@ pub extern "C" fn fread(ptr: *mut c_void, size: size_t, count: size_t, stream: * ) }; match stream.read(buf) { Ok(bytes) => (bytes as usize / size as usize) as size_t, - Err(err) => { - unsafe { - platform::errno = errno::EIO; - } - 0 - } + Err(_) => 0 } } @@ -523,12 +513,7 @@ pub extern "C" fn fwrite(ptr: *const c_void, size: usize, count: usize, stream: ) }; match stream.write(buf) { Ok(bytes) => (bytes as usize / size as usize) as size_t, - Err(err) => { - unsafe { - platform::errno = errno::EIO; - } - 0 - } + Err(_) => 0 } } @@ -551,12 +536,8 @@ pub extern "C" fn getc_unlocked(stream: *mut FILE) -> c_int { let mut buf = [0]; match unsafe { &mut *stream }.read(&mut buf) { - Ok(0) => EOF, - Ok(_) => buf[0] as c_int, - Err(err) => unsafe { - platform::errno = errno::EIO; - EOF - } + Ok(0) | Err(_) => EOF, + Ok(_) => buf[0] as c_int } } @@ -632,12 +613,8 @@ pub extern "C" fn putchar(c: c_int) -> c_int { #[no_mangle] pub extern "C" fn putc_unlocked(c: c_int, stream: *mut FILE) -> c_int { match unsafe { &mut *stream }.write(&[c as u8]) { - Ok(0) => EOF, + Ok(0) | Err(_) => EOF, Ok(_) => c, - Err(_) => unsafe { - platform::errno = errno::EIO; - EOF - } } } diff --git a/src/platform/mod.rs b/src/platform/mod.rs index ca964759ab7038c05a8f43d5e968b5db80625264..8d90b31b1cd31e793966478a2e2bbbbecf127164 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -26,10 +26,6 @@ mod sys; #[path = "redox/mod.rs"] mod sys; -pub use self::rawfile::RawFile; - -pub mod rawfile; - pub use self::rlb::{Line, RawLineBuffer}; pub mod rlb; diff --git a/src/platform/rawfile.rs b/src/platform/rawfile.rs deleted file mode 100644 index 13014a7167d477e72b873d5422c3b6016fc0b4a3..0000000000000000000000000000000000000000 --- a/src/platform/rawfile.rs +++ /dev/null @@ -1,85 +0,0 @@ -use alloc::Vec; -use core::ops::Deref; - -use super::{types::*, Pal, Sys}; -use c_str::CStr; -use header::fcntl; - -pub struct RawFile(c_int); - -impl RawFile { - pub fn open(path: &CStr, oflag: c_int, mode: mode_t) -> Result<RawFile, ()> { - match Sys::open(path, oflag, mode) { - -1 => Err(()), - n => Ok(RawFile(n)), - } - } - - pub fn dup(&self) -> Result<RawFile, ()> { - match Sys::dup(self.0) { - -1 => Err(()), - n => Ok(RawFile(n)), - } - } - - pub fn as_raw_fd(&self) -> c_int { - self.0 - } - - pub fn into_raw_fd(self) -> c_int { - self.0 - } - - pub fn from_raw_fd(fd: c_int) -> Self { - RawFile(fd) - } -} - -impl Drop for RawFile { - fn drop(&mut self) { - let _ = Sys::close(self.0); - } -} - -impl Deref for RawFile { - type Target = c_int; - - fn deref(&self) -> &c_int { - &self.0 - } -} - -pub fn file_read_all(path: &CStr) -> Result<Vec<u8>, ()> { - let file = RawFile::open(path, fcntl::O_RDONLY, 0)?; - - let mut buf = Vec::new(); - let mut len = 0; - - loop { - if len >= buf.capacity() { - buf.reserve(32); - - unsafe { - let capacity = buf.capacity(); - buf.set_len(capacity); - } - } - - let read = Sys::read(*file, &mut buf[len..]); - - len += read as usize; - - if read == 0 { - unsafe { - buf.set_len(len); - } - return Ok(buf); - } - if read < 0 { - unsafe { - buf.set_len(len); - } - return Err(()); - } - } -} diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs index 42cb72b974d2a1c49b20a4c54aa376bf48a19824..7dcd9484fe10801c5e66b6a36472c7ad56703029 100644 --- a/src/platform/redox/mod.rs +++ b/src/platform/redox/mod.rs @@ -2,7 +2,7 @@ use alloc::btree_map::BTreeMap; use cbitset::BitSet; -use core::fmt::Write; +use core::fmt::Write as WriteFmt; use core::{mem, ptr, slice}; use spin::{Mutex, MutexGuard, Once}; use syscall::data::Stat as redox_stat; @@ -12,7 +12,8 @@ use syscall::{self, Result}; use c_str::{CStr, CString}; use fs::File; -use io; +use io::{self, BufReader, SeekFrom}; +use io::prelude::*; use header::dirent::dirent; use header::errno::{EIO, EINVAL, ENOSYS}; use header::fcntl; @@ -29,7 +30,7 @@ use header::time::timespec; use header::unistd::{F_OK, R_OK, SEEK_SET, W_OK, X_OK}; use super::types::*; -use super::{errno, FileReader, FileWriter, Line, Pal, RawFile, RawLineBuffer, Read}; +use super::{errno, FileReader, FileWriter, Line, Pal, Read}; mod signal; mod socket; @@ -58,9 +59,12 @@ pub struct Sys; impl Pal for Sys { fn access(path: &CStr, mode: c_int) -> c_int { - let fd = match RawFile::open(path, fcntl::O_PATH | fcntl::O_CLOEXEC, 0) { + let fd = match File::open(path, fcntl::O_PATH | fcntl::O_CLOEXEC) { Ok(fd) => fd, - Err(_) => return -1, + Err(_) => unsafe { + errno = EIO; + return -1; + } }; if mode == F_OK { return 0; @@ -171,10 +175,11 @@ impl Pal for Sys { ) -> c_int { use alloc::Vec; - let fd = match RawFile::open(path, fcntl::O_RDONLY | fcntl::O_CLOEXEC, 0) { - Ok(fd) => fd, - Err(_) => return -1, + let file = match File::open(path, fcntl::O_RDONLY | fcntl::O_CLOEXEC) { + Ok(file) => file, + Err(_) => return -1 }; + let mut file = BufReader::new(file); // Count arguments let mut len = 0; @@ -189,27 +194,28 @@ impl Pal for Sys { let mut read = 0; while read < 2 { - match Self::read(*fd, &mut shebang) { - 0 => break, - i if i < 0 => return -1, - i => read += i, + match file.read(&mut shebang) { + Ok(0) => break, + Ok(i) => read += i, + Err(_) => return -1 } } let mut _interpreter_path = None; let mut _interpreter_file = None; - let mut interpreter_fd = *fd; + let mut interpreter_fd = **file.get_ref(); if &shebang == b"#!" { - match RawLineBuffer::new(*fd).next() { - Line::Error => return -1, - Line::EOF => (), - Line::Some(line) => { + let mut line = Vec::new(); + match file.read_until(b'\n', &mut line) { + Err(_) => return -1, + Ok(0) => (), + Ok(_) => { let mut path = match CString::new(line) { Ok(path) => path, Err(_) => return -1, }; - match RawFile::open(&path, fcntl::O_RDONLY | fcntl::O_CLOEXEC, 0) { + match File::open(&path, fcntl::O_RDONLY | fcntl::O_CLOEXEC) { Ok(file) => { interpreter_fd = *file; _interpreter_path = Some(path); @@ -217,13 +223,13 @@ impl Pal for Sys { let path_ref = _interpreter_path.as_ref().unwrap(); args.push([path_ref.as_ptr() as usize, path_ref.to_bytes().len()]); - } + }, Err(_) => return -1, } - } + }, } } - if Self::lseek(*fd, 0, SEEK_SET) < 0 { + if file.seek(SeekFrom::Start(0)).is_err() { return -1; } @@ -637,7 +643,7 @@ impl Pal for Sys { let mut exceptfds = unsafe { exceptfds.as_mut() }.map(|s| BitSet::from_ref(&mut s.fds_bits)); let event_path = unsafe { CStr::from_bytes_with_nul_unchecked(b"event:\0") }; - let event_file = match RawFile::open(event_path, fcntl::O_RDWR | fcntl::O_CLOEXEC, 0) { + let mut event_file = match File::open(event_path, fcntl::O_RDWR | fcntl::O_CLOEXEC) { Ok(file) => file, Err(_) => return -1, }; @@ -645,15 +651,7 @@ impl Pal for Sys { for fd in 0..nfds as usize { macro_rules! register { ($fd:expr, $flags:expr) => { - if Self::write( - *event_file, - &syscall::Event { - id: $fd, - flags: $flags, - data: 0, - }, - ) < 0 - { + if event_file.write(&syscall::Event { id: $fd, flags: $flags, data: 0, }).is_err() { return -1; } }; @@ -678,26 +676,21 @@ impl Pal for Sys { format!("time:{}", syscall::CLOCK_MONOTONIC).into_bytes(), ) }; - let timeout_file = - match RawFile::open(&timeout_path, fcntl::O_RDWR | fcntl::O_CLOEXEC, 0) { - Ok(file) => file, - Err(_) => return -1, - }; + let mut timeout_file = match File::open(&timeout_path, fcntl::O_RDWR | fcntl::O_CLOEXEC) { + Ok(file) => file, + Err(_) => return -1, + }; - if Self::write( - *event_file, - &syscall::Event { - id: *timeout_file as usize, - flags: syscall::EVENT_READ, - data: TIMEOUT_TOKEN, - }, - ) < 0 - { + if event_file.write(&syscall::Event { + id: *timeout_file as usize, + flags: syscall::EVENT_READ, + data: TIMEOUT_TOKEN, + }).is_err() { return -1; } let mut time = syscall::TimeSpec::default(); - if Self::read(*timeout_file, &mut time) < 0 { + if timeout_file.read(&mut time).is_err() { return -1; } @@ -708,7 +701,7 @@ impl Pal for Sys { time.tv_nsec -= 1000000000; } - if Self::write(*timeout_file, &time) < 0 { + if timeout_file.write(&time).is_err() { return -1; } @@ -721,11 +714,10 @@ impl Pal for Sys { &mut events as *mut _ as *mut u8, mem::size_of::<syscall::Event>() * events.len() ) }; - let read = Self::read(*event_file, &mut events); - if read < 0 { - return -1; + match event_file.read(&mut events) { + Ok(i) => i / mem::size_of::<syscall::Event>(), + Err(_) => return -1 } - read as usize / mem::size_of::<syscall::Event>() }; let mut total = 0;