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
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use resource_task::{Metadata, Payload, Done, LoaderTask, start_sending};
use servo_util::io::ignoring_eof;
use resource_task::{ProgressMsg, Metadata, Payload, Done, LoaderTask, start_sending};
use servo_util::io::result;
use std::rt::io;
use std::rt::io::Reader;
use std::comm::Chan;
use std::rt::io::file;
use std::rt::io::{FileStream, Reader, EndOfFile, Open, Read, ignore_io_error};
use std::task;
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 {
let f: LoaderTask = |url, start_chan| {
assert!("file" == url.scheme);
let progress_chan = start_sending(start_chan, Metadata::default(url.clone()));
do task::spawn {
match io::file::open(&url.path.as_slice(), io::Open, io::Read) {
Some(mut reader) => {
while !reader.eof() {
do ignoring_eof {
let data = reader.read_bytes(READ_SIZE);
progress_chan.send(Payload(data));
}
}
progress_chan.send(Done(Ok(())));
// ignore_io_error causes us to get None instead of a task failure.
match ignore_io_error(|| file::open(&url.path.as_slice(), Open, Read)) {
Some(ref mut reader) => {
let res = read_all(reader, &progress_chan);
progress_chan.send(Done(res));
}
None => {
progress_chan.send(Done(Err(())));
}
};
}
}
};
f

View file

@ -2,13 +2,19 @@
* 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/. */
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.
pub fn ignoring_eof<U>(cb: &fn() -> U) -> U {
io_error::cond.trap(|e|
match e.kind {
EndOfFile => (),
_ => io_error::cond.raise(e)
}).inside(cb)
/// Helper for catching an I/O error and wrapping it in a Result object. The
/// return result will be the last I/O error that happened or the result of the
/// closure if no error occurred.
///
/// FIXME: This is a copy of std::rt::io::result which doesn't exist yet in our
/// version of Rust. We should switch after the next Rust upgrade.
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),
}
}