1
Fork 0

Runtime removal: refactor fs

This moves the filesystem implementation from libnative into the new
`sys` modules, refactoring along the way and hooking into `std::io::fs`.

Because this eliminates APIs in `libnative` and `librustrt`, it is a:

[breaking-change]

This functionality is likely to be available publicly, in some form,
from `std` in the future.
This commit is contained in:
Aaron Turon 2014-09-30 17:34:14 -07:00
parent 16470cf01b
commit 0c1e1ff1e3
9 changed files with 664 additions and 1168 deletions

View file

@ -36,11 +36,11 @@ use kinds::Send;
use libc;
use option::{Option, Some, None};
use boxed::Box;
use sys::fs::FileDesc;
use result::{Ok, Err};
use rt;
use rt::local::Local;
use rt::task::Task;
use rt::rtio::{DontClose, IoFactory, LocalIo, RtioFileStream, RtioTTY};
use slice::SlicePrelude;
use str::StrPrelude;
use uint;
@ -75,14 +75,14 @@ use uint;
// case pipe also doesn't work, but magically file does!
enum StdSource {
TTY(Box<RtioTTY + Send>),
File(Box<RtioFileStream + Send>),
File(FileDesc),
}
fn src<T>(fd: libc::c_int, readable: bool, f: |StdSource| -> T) -> T {
LocalIo::maybe_raise(|io| {
Ok(match io.tty_open(fd, readable) {
Ok(tty) => f(TTY(tty)),
Err(_) => f(File(io.fs_from_raw_fd(fd, DontClose))),
Err(_) => f(File(FileDesc::new(fd, false))),
})
}).map_err(IoError::from_rtio_error).unwrap()
}
@ -278,10 +278,10 @@ impl Reader for StdReader {
// print!'d prompt not being shown until after the user hits
// enter.
flush();
tty.read(buf)
tty.read(buf).map_err(IoError::from_rtio_error)
},
File(ref mut file) => file.read(buf).map(|i| i as uint),
}.map_err(IoError::from_rtio_error);
};
match ret {
// When reading a piped stdin, libuv will return 0-length reads when
// stdin reaches EOF. For pretty much all other streams it will
@ -372,9 +372,9 @@ impl Writer for StdWriter {
let max_size = if cfg!(windows) {8192} else {uint::MAX};
for chunk in buf.chunks(max_size) {
try!(match self.inner {
TTY(ref mut tty) => tty.write(chunk),
TTY(ref mut tty) => tty.write(chunk).map_err(IoError::from_rtio_error),
File(ref mut file) => file.write(chunk),
}.map_err(IoError::from_rtio_error))
})
}
Ok(())
}