Skip to content

Commit

Permalink
Fill out the remaining functionality in io::file
Browse files Browse the repository at this point in the history
This adds bindings to the remaining functions provided by libuv, all of which
are useful operations on files which need to get exposed somehow.

Some highlights:

* Dropped `FileReader` and `FileWriter` and `FileStream` for one `File` type
* Moved all file-related methods to be static methods under `File`
* All directory related methods are still top-level functions
* Created `io::FilePermission` types (backed by u32) that are what you'd expect
* Created `io::FileType` and refactored `FileStat` to use FileType and
  FilePermission
* Removed the expanding matrix of `FileMode` operations. The mode of reading a
  file will not have the O_CREAT flag, but a write mode will always have the
  O_CREAT flag.

Closes #10130
Closes #10131
Closes #10121
  • Loading branch information
alexcrichton committed Nov 3, 2013
1 parent 9c18510 commit f19d083
Show file tree
Hide file tree
Showing 38 changed files with 1,359 additions and 1,008 deletions.
4 changes: 2 additions & 2 deletions src/compiletest/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
// except according to those terms.

use std::rt::io::buffered::BufferedReader;
use std::rt::io::file;
use std::rt::io::File;

pub struct ExpectedError { line: uint, kind: ~str, msg: ~str }

// Load any test directives embedded in the file
pub fn load_errors(testfile: &Path) -> ~[ExpectedError] {

let mut error_patterns = ~[];
let mut rdr = BufferedReader::new(file::open(testfile).unwrap());
let mut rdr = BufferedReader::new(File::open(testfile).unwrap());
let mut line_num = 1u;
loop {
let ln = match rdr.read_line() {
Expand Down
4 changes: 2 additions & 2 deletions src/compiletest/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ pub fn is_test_ignored(config: &config, testfile: &Path) -> bool {

fn iter_header(testfile: &Path, it: &fn(&str) -> bool) -> bool {
use std::rt::io::buffered::BufferedReader;
use std::rt::io::file;
use std::rt::io::File;

let mut rdr = BufferedReader::new(file::open(testfile).unwrap());
let mut rdr = BufferedReader::new(File::open(testfile).unwrap());
loop {
let ln = match rdr.read_line() {
Some(ln) => ln, None => break
Expand Down
9 changes: 5 additions & 4 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use util::logv;
use std::cell::Cell;
use std::rt::io;
use std::rt::io::file;
use std::rt::io::File;
use std::os;
use std::str;
use std::task::{spawn_sched, SingleThreaded};
Expand Down Expand Up @@ -171,7 +172,7 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
let rounds =
match props.pp_exact { Some(_) => 1, None => 2 };

let src = file::open(testfile).read_to_end();
let src = File::open(testfile).read_to_end();
let src = str::from_utf8_owned(src);
let mut srcs = ~[src];

Expand All @@ -193,7 +194,7 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
let mut expected = match props.pp_exact {
Some(ref file) => {
let filepath = testfile.dir_path().join(file);
let s = file::open(&filepath).read_to_end();
let s = File::open(&filepath).read_to_end();
str::from_utf8_owned(s)
}
None => { srcs[srcs.len() - 2u].clone() }
Expand Down Expand Up @@ -764,7 +765,7 @@ fn dump_output(config: &config, testfile: &Path, out: &str, err: &str) {
fn dump_output_file(config: &config, testfile: &Path,
out: &str, extension: &str) {
let outfile = make_out_name(config, testfile, extension);
file::create(&outfile).write(out.as_bytes());
File::create(&outfile).write(out.as_bytes());
}

fn make_out_name(config: &config, testfile: &Path, extension: &str) -> Path {
Expand Down Expand Up @@ -1015,7 +1016,7 @@ fn disassemble_extract(config: &config, _props: &TestProps,


fn count_extracted_lines(p: &Path) -> uint {
let x = file::open(&p.with_extension("ll")).read_to_end();
let x = File::open(&p.with_extension("ll")).read_to_end();
let x = str::from_utf8_owned(x);
x.line_iter().len()
}
Expand Down
1 change: 1 addition & 0 deletions src/etc/libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ void posix88_consts() {
put_const(S_IFBLK, int);
put_const(S_IFDIR, int);
put_const(S_IFREG, int);
put_const(S_IFLNK, int);
put_const(S_IFMT, int);

put_const(S_IEXEC, int);
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/terminfo/searcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use std::{os, str};
use std::os::getenv;
use std::rt::io;
use std::rt::io::file;
use std::rt::io::File;

/// Return path to database entry for `term`
pub fn get_dbpath_for_term(term: &str) -> Option<~Path> {
Expand Down Expand Up @@ -76,7 +76,7 @@ pub fn get_dbpath_for_term(term: &str) -> Option<~Path> {
/// Return open file for `term`
pub fn open(term: &str) -> Result<@mut io::Reader, ~str> {
match get_dbpath_for_term(term) {
Some(x) => Ok(@mut file::open(x) as @mut io::Reader),
Some(x) => Ok(@mut File::open(x) as @mut io::Reader),
None => Err(format!("could not find terminfo entry for {}", term))
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/libextra/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use treemap::TreeMap;
use std::clone::Clone;
use std::comm::{stream, SharedChan, GenericPort, GenericChan};
use std::rt::io;
use std::rt::io::file;
use std::rt::io::File;
use std::task;
use std::to_str::ToStr;
use std::f64;
Expand Down Expand Up @@ -353,7 +353,7 @@ struct ConsoleTestState {
impl ConsoleTestState {
pub fn new(opts: &TestOpts) -> ConsoleTestState {
let log_out = match opts.logfile {
Some(ref path) => Some(@mut file::create(path) as @mut io::Writer),
Some(ref path) => Some(@mut File::create(path) as @mut io::Writer),
None => None
};
let out = @mut io::stdio::stdout() as @mut io::Writer;
Expand Down Expand Up @@ -936,14 +936,14 @@ impl MetricMap {
/// Load MetricDiff from a file.
pub fn load(p: &Path) -> MetricMap {
assert!(p.exists());
let f = @mut file::open(p) as @mut io::Reader;
let f = @mut File::open(p) as @mut io::Reader;
let mut decoder = json::Decoder(json::from_reader(f).unwrap());
MetricMap(Decodable::decode(&mut decoder))
}

/// Write MetricDiff to a file.
pub fn save(&self, p: &Path) {
self.to_json().to_pretty_writer(@mut file::create(p) as @mut io::Writer);
self.to_json().to_pretty_writer(@mut File::create(p) as @mut io::Writer);
}

/// Compare against another MetricMap. Optionally compare all
Expand Down
13 changes: 6 additions & 7 deletions src/libextra/workcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::cell::Cell;
use std::comm::{PortOne, oneshot};
use std::{str, task};
use std::rt::io;
use std::rt::io::file;
use std::rt::io::File;
use std::rt::io::Decorator;
use std::rt::io::mem::MemWriter;

Expand Down Expand Up @@ -176,14 +176,14 @@ impl Database {

// FIXME #4330: This should have &mut self and should set self.db_dirty to false.
fn save(&self) {
let f = @mut file::create(&self.db_filename);
let f = @mut File::create(&self.db_filename);
self.db_cache.to_json().to_pretty_writer(f as @mut io::Writer);
}

fn load(&mut self) {
assert!(!self.db_dirty);
assert!(self.db_filename.exists());
match io::result(|| file::open(&self.db_filename)) {
match io::result(|| File::open(&self.db_filename)) {
Err(e) => fail!("Couldn't load workcache database {}: {}",
self.db_filename.display(),
e.desc),
Expand Down Expand Up @@ -480,21 +480,20 @@ impl<'self, T:Send +
#[test]
fn test() {
use std::{os, run};
use std::rt::io::file;
use std::str::from_utf8_owned;

// Create a path to a new file 'filename' in the directory in which
// this test is running.
fn make_path(filename: ~str) -> Path {
let pth = os::self_exe_path().expect("workcache::test failed").with_filename(filename);
if pth.exists() {
file::unlink(&pth);
File::unlink(&pth);
}
return pth;
}

let pth = make_path(~"foo.c");
file::create(&pth).write(bytes!("int main() { return 0; }"));
File::create(&pth).write(bytes!("int main() { return 0; }"));

let db_path = make_path(~"db.json");

Expand All @@ -507,7 +506,7 @@ fn test() {
let subcx = cx.clone();
let pth = pth.clone();

let file_content = from_utf8_owned(file::open(&pth).read_to_end());
let file_content = from_utf8_owned(File::open(&pth).read_to_end());

// FIXME (#9639): This needs to handle non-utf8 paths
prep.declare_input("file", pth.as_str().unwrap(), file_content);
Expand Down
7 changes: 3 additions & 4 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::ptr;
use std::run;
use std::str;
use std::vec;
use std::rt::io::file;
use std::rt::io::File;
use syntax::ast;
use syntax::ast_map::{path, path_mod, path_name, path_pretty_name};
use syntax::attr;
Expand Down Expand Up @@ -950,18 +950,17 @@ pub fn link_binary(sess: Session,

// Remove the temporary object file if we aren't saving temps
if !sess.opts.save_temps {
file::unlink(obj_filename);
File::unlink(obj_filename);
}
}

fn is_writeable(p: &Path) -> bool {
use std::rt::io;
use std::libc::consts::os::posix88::S_IWUSR;

!p.exists() ||
(match io::result(|| p.stat()) {
Err(*) => false,
Ok(m) => (m.mode as uint) & S_IWUSR as uint == S_IWUSR as uint
Ok(m) => m.perm & io::UserWrite == io::UserWrite
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use util::ppaux;

use std::hashmap::{HashMap,HashSet};
use std::rt::io;
use std::rt::io::file;
use std::rt::io::File;
use std::rt::io::mem::MemReader;
use std::os;
use std::vec;
Expand Down Expand Up @@ -370,7 +370,7 @@ pub fn phase_5_run_llvm_passes(sess: Session,

// Remove assembly source unless --save-temps was specified
if !sess.opts.save_temps {
file::unlink(&asm_filename);
File::unlink(&asm_filename);
}
} else {
time(sess.time_passes(), "LLVM passes", (), |_|
Expand Down
15 changes: 8 additions & 7 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use std::local_data;
use std::rt::io::buffered::BufferedWriter;
use std::rt::io;
use std::rt::io::file;
use std::rt::io::File;
use std::os;
use std::str;
use std::task;
Expand Down Expand Up @@ -263,7 +264,7 @@ pub fn run(mut crate: clean::Crate, dst: Path) {
// Publish the search index
{
dst.push("search-index.js");
let mut w = BufferedWriter::new(file::create(&dst).unwrap());
let mut w = BufferedWriter::new(File::create(&dst).unwrap());
let w = &mut w as &mut Writer;
write!(w, "var searchIndex = [");
for (i, item) in cache.search_index.iter().enumerate() {
Expand Down Expand Up @@ -313,7 +314,7 @@ pub fn run(mut crate: clean::Crate, dst: Path) {
/// Writes the entire contents of a string to a destination, not attempting to
/// catch any errors.
fn write(dst: Path, contents: &str) {
file::create(&dst).write(contents.as_bytes());
File::create(&dst).write(contents.as_bytes());
}

/// Makes a directory on the filesystem, failing the task if an error occurs and
Expand Down Expand Up @@ -419,7 +420,7 @@ impl<'self> SourceCollector<'self> {
// If we couldn't open this file, then just returns because it
// probably means that it's some standard library macro thing and we
// can't have the source to it anyway.
let mut r = match io::result(|| file::open(&p)) {
let mut r = match io::result(|| File::open(&p)) {
Ok(r) => r,
// eew macro hacks
Err(*) => return filename == "<std-macros>"
Expand All @@ -445,7 +446,7 @@ impl<'self> SourceCollector<'self> {
}
cur.push(p.filename().expect("source has no filename") + bytes!(".html"));
let mut w = BufferedWriter::new(file::create(&cur).unwrap());
let mut w = BufferedWriter::new(File::create(&cur).unwrap());

let title = cur.filename_display().with_str(|s| format!("{} -- source", s));
let page = layout::Page {
Expand Down Expand Up @@ -767,7 +768,7 @@ impl Context {
///
/// The rendering driver uses this closure to queue up more work.
fn item(&mut self, item: clean::Item, f: &fn(&mut Context, clean::Item)) {
fn render(w: file::FileWriter, cx: &mut Context, it: &clean::Item,
fn render(w: io::File, cx: &mut Context, it: &clean::Item,
pushname: bool) {
// A little unfortunate that this is done like this, but it sure
// does make formatting *a lot* nicer.
Expand Down Expand Up @@ -804,7 +805,7 @@ impl Context {
do self.recurse(name) |this| {
let item = item.take();
let dst = this.dst.join("index.html");
render(file::create(&dst).unwrap(), this, &item, false);
render(File::create(&dst).unwrap(), this, &item, false);

let m = match item.inner {
clean::ModuleItem(m) => m,
Expand All @@ -821,7 +822,7 @@ impl Context {
// pages dedicated to them.
_ if item.name.is_some() => {
let dst = self.dst.join(item_path(&item));
render(file::create(&dst).unwrap(), self, &item, true);
render(File::create(&dst).unwrap(), self, &item, true);
}

_ => {}
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extern mod extra;
use std::cell::Cell;
use std::local_data;
use std::rt::io;
use std::rt::io::file;
use std::rt::io::File;
use std::rt::io::mem::MemWriter;
use std::rt::io::Decorator;
use std::str;
Expand Down Expand Up @@ -259,7 +259,7 @@ fn rust_input(cratefile: &str, matches: &getopts::Matches) -> Output {
/// This input format purely deserializes the json output file. No passes are
/// run over the deserialized output.
fn json_input(input: &str) -> Result<Output, ~str> {
let input = match file::open(&Path::new(input)) {
let input = match File::open(&Path::new(input)) {
Some(f) => f,
None => return Err(format!("couldn't open {} for reading", input)),
};
Expand Down Expand Up @@ -321,7 +321,7 @@ fn json_output(crate: clean::Crate, res: ~[plugins::PluginJson], dst: Path) {
json.insert(~"crate", crate_json);
json.insert(~"plugins", json::Object(plugins_json));

let mut file = file::create(&dst).unwrap();
let mut file = File::create(&dst).unwrap();
let output = json::Object(json).to_str();
file.write(output.as_bytes());
}
5 changes: 3 additions & 2 deletions src/librustpkg/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use std::{os, result, run, str, task};
use std::hashmap::HashSet;
use std::rt::io;
use std::rt::io::file;
use std::rt::io::File;
pub use std::path::Path;

use extra::workcache;
Expand Down Expand Up @@ -661,7 +662,7 @@ impl CtxMethods for BuildContext {
for exec in subex.iter() {
debug!("Copying: {} -> {}", exec.display(), sub_target_ex.display());
file::mkdir_recursive(&sub_target_ex.dir_path(), io::UserRWX);
file::copy(exec, &sub_target_ex);
File::copy(exec, &sub_target_ex);
// FIXME (#9639): This needs to handle non-utf8 paths
exe_thing.discover_output("binary",
sub_target_ex.as_str().unwrap(),
Expand All @@ -674,7 +675,7 @@ impl CtxMethods for BuildContext {
didn't install it!", lib.display()));
target_lib.set_filename(lib.filename().expect("weird target lib"));
file::mkdir_recursive(&target_lib.dir_path(), io::UserRWX);
file::copy(lib, &target_lib);
File::copy(lib, &target_lib);
debug!("3. discovering output {}", target_lib.display());
exe_thing.discover_output("binary",
target_lib.as_str().unwrap(),
Expand Down
Loading

0 comments on commit f19d083

Please sign in to comment.