file_loader: Handle file-not-found and other error conditions

This commit is contained in:
Keegan McAllister 2013-11-05 12:57:39 -08:00
parent 5c101526a1
commit 35666e9c20
2 changed files with 41 additions and 22 deletions

View file

@ -2,34 +2,47 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use resource_task::{Metadata, Payload, Done, LoaderTask, start_sending}; use resource_task::{ProgressMsg, Metadata, Payload, Done, LoaderTask, start_sending};
use servo_util::io::ignoring_eof; use servo_util::io::result;
use std::rt::io; use std::comm::Chan;
use std::rt::io::Reader; use std::rt::io::file;
use std::rt::io::{FileStream, Reader, EndOfFile, Open, Read, ignore_io_error};
use std::task; use std::task;
static READ_SIZE: uint = 1024; static READ_SIZE: uint = 1024;
fn read_all(reader: &mut FileStream, progress_chan: &Chan<ProgressMsg>)
-> Result<(), ()> {
loop {
match (do result {
let data = reader.read_bytes(READ_SIZE);
progress_chan.send(Payload(data));
}) {
Ok(()) => (),
Err(e) => match e.kind {
EndOfFile => return Ok(()),
_ => return Err(()),
}
}
}
}
pub fn factory() -> LoaderTask { pub fn factory() -> LoaderTask {
let f: LoaderTask = |url, start_chan| { let f: LoaderTask = |url, start_chan| {
assert!("file" == url.scheme); assert!("file" == url.scheme);
let progress_chan = start_sending(start_chan, Metadata::default(url.clone())); let progress_chan = start_sending(start_chan, Metadata::default(url.clone()));
do task::spawn { do task::spawn {
match io::file::open(&url.path.as_slice(), io::Open, io::Read) { // ignore_io_error causes us to get None instead of a task failure.
Some(mut reader) => { match ignore_io_error(|| file::open(&url.path.as_slice(), Open, Read)) {
while !reader.eof() { Some(ref mut reader) => {
do ignoring_eof { let res = read_all(reader, &progress_chan);
let data = reader.read_bytes(READ_SIZE); progress_chan.send(Done(res));
progress_chan.send(Payload(data));
}
}
progress_chan.send(Done(Ok(())));
} }
None => { None => {
progress_chan.send(Done(Err(()))); progress_chan.send(Done(Err(())));
} }
}; }
} }
}; };
f f

View file

@ -2,13 +2,19 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::rt::io::{io_error, EndOfFile}; use std::rt::io::{io_error, IoError};
/// Ignore the end-of-file condition within a block of code. /// Helper for catching an I/O error and wrapping it in a Result object. The
pub fn ignoring_eof<U>(cb: &fn() -> U) -> U { /// return result will be the last I/O error that happened or the result of the
io_error::cond.trap(|e| /// closure if no error occurred.
match e.kind { ///
EndOfFile => (), /// FIXME: This is a copy of std::rt::io::result which doesn't exist yet in our
_ => io_error::cond.raise(e) /// version of Rust. We should switch after the next Rust upgrade.
}).inside(cb) pub fn result<T>(cb: &fn() -> T) -> Result<T, IoError> {
let mut err = None;
let ret = io_error::cond.trap(|e| err = Some(e)).inside(cb);
match err {
Some(e) => Err(e),
None => Ok(ret),
}
} }