Delete RawFile in favor of File

This commit is contained in:
jD91mZM2
2018-09-26 19:35:32 +02:00
parent d365813c90
commit 0451fac66c
7 changed files with 84 additions and 193 deletions
+13 -10
View File
@@ -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();
}
}
+7 -2
View File
@@ -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
}
+15 -12
View File
@@ -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]
+6 -29
View File
@@ -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
}
}
}