Skip to content

Commit

Permalink
Auto merge of #56258 - euclio:fs-read-write, r=euclio
Browse files Browse the repository at this point in the history
use top level `fs` functions where appropriate

This commit replaces many usages of `File::open` and reading or writing
with `fs::read_to_string`, `fs::read` and `fs::write`. This reduces code
complexity, and will improve performance for most reads, since the
functions allocate the buffer to be the size of the file.

I believe that this commit will not impact behavior in any way, so some
matches will check the error kind in case the file was not valid UTF-8.
Some of these cases may not actually care about the error.
  • Loading branch information
bors committed Dec 7, 2018
2 parents 4a45578 + 2f62265 commit 0a77980
Show file tree
Hide file tree
Showing 26 changed files with 137 additions and 235 deletions.
19 changes: 9 additions & 10 deletions src/bootstrap/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use std::borrow::Cow;
use std::env;
use std::fs::{self, File};
use std::fs;
use std::io::BufReader;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -707,7 +707,7 @@ impl Step for CodegenBackend {
}
let stamp = codegen_backend_stamp(builder, compiler, target, backend);
let codegen_backend = codegen_backend.to_str().unwrap();
t!(t!(File::create(&stamp)).write_all(codegen_backend.as_bytes()));
t!(fs::write(&stamp, &codegen_backend));
}
}

Expand Down Expand Up @@ -796,8 +796,7 @@ fn copy_codegen_backends_to_sysroot(builder: &Builder,

for backend in builder.config.rust_codegen_backends.iter() {
let stamp = codegen_backend_stamp(builder, compiler, target, *backend);
let mut dylib = String::new();
t!(t!(File::open(&stamp)).read_to_string(&mut dylib));
let dylib = t!(fs::read_to_string(&stamp));
let file = Path::new(&dylib);
let filename = file.file_name().unwrap().to_str().unwrap();
// change `librustc_codegen_llvm-xxxxxx.so` to `librustc_codegen_llvm-llvm.so`
Expand Down Expand Up @@ -1137,10 +1136,7 @@ pub fn run_cargo(builder: &Builder,
// contents (the list of files to copy) is different or if any dep's mtime
// is newer then we rewrite the stamp file.
deps.sort();
let mut stamp_contents = Vec::new();
if let Ok(mut f) = File::open(stamp) {
t!(f.read_to_end(&mut stamp_contents));
}
let stamp_contents = fs::read(stamp);
let stamp_mtime = mtime(&stamp);
let mut new_contents = Vec::new();
let mut max = None;
Expand All @@ -1156,7 +1152,10 @@ pub fn run_cargo(builder: &Builder,
}
let max = max.unwrap();
let max_path = max_path.unwrap();
if stamp_contents == new_contents && max <= stamp_mtime {
let contents_equal = stamp_contents
.map(|contents| contents == new_contents)
.unwrap_or_default();
if contents_equal && max <= stamp_mtime {
builder.verbose(&format!("not updating {:?}; contents equal and {:?} <= {:?}",
stamp, max, stamp_mtime));
return deps
Expand All @@ -1166,7 +1165,7 @@ pub fn run_cargo(builder: &Builder,
} else {
builder.verbose(&format!("updating {:?} as deps changed", stamp));
}
t!(t!(File::create(stamp)).write_all(&new_contents));
t!(fs::write(&stamp, &new_contents));
deps
}

Expand Down
7 changes: 2 additions & 5 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
use std::collections::{HashMap, HashSet};
use std::env;
use std::fs::{self, File};
use std::io::prelude::*;
use std::fs;
use std::path::{Path, PathBuf};
use std::process;
use std::cmp;
Expand Down Expand Up @@ -416,9 +415,7 @@ impl Config {
config.run_host_only = !(flags.host.is_empty() && !flags.target.is_empty());

let toml = file.map(|file| {
let mut f = t!(File::open(&file));
let mut contents = String::new();
t!(f.read_to_string(&mut contents));
let contents = t!(fs::read_to_string(&file));
match toml::from_str(&contents) {
Ok(table) => table,
Err(err) => {
Expand Down
14 changes: 6 additions & 8 deletions src/bootstrap/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
//! pieces of `rustup.rs`!
use std::env;
use std::fs::{self, File};
use std::io::{Read, Write};
use std::fs;
use std::io::Write;
use std::path::{PathBuf, Path};
use std::process::{Command, Stdio};

Expand Down Expand Up @@ -1511,8 +1511,7 @@ impl Step for Extended {
}

let xform = |p: &Path| {
let mut contents = String::new();
t!(t!(File::open(p)).read_to_string(&mut contents));
let mut contents = t!(fs::read_to_string(p));
if rls_installer.is_none() {
contents = filter(&contents, "rls");
}
Expand All @@ -1523,8 +1522,8 @@ impl Step for Extended {
contents = filter(&contents, "rustfmt");
}
let ret = tmp.join(p.file_name().unwrap());
t!(t!(File::create(&ret)).write_all(contents.as_bytes()));
return ret
t!(fs::write(&ret, &contents));
ret
};

if target.contains("apple-darwin") {
Expand Down Expand Up @@ -1869,8 +1868,7 @@ impl Step for HashSign {
let file = builder.config.dist_gpg_password_file.as_ref().unwrap_or_else(|| {
panic!("\n\nfailed to specify `dist.gpg-password-file` in `config.toml`\n\n")
});
let mut pass = String::new();
t!(t!(File::open(&file)).read_to_string(&mut pass));
let pass = t!(fs::read_to_string(&file));

let today = output(Command::new("date").arg("+%Y-%m-%d"));

Expand Down
14 changes: 6 additions & 8 deletions src/bootstrap/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
//! `rustdoc`.
use std::collections::HashSet;
use std::fs::{self, File};
use std::io::prelude::*;
use std::fs;
use std::io;
use std::path::{PathBuf, Path};

Expand Down Expand Up @@ -379,12 +378,11 @@ impl Step for Standalone {
let version_info = out.join("version_info.html");

if !builder.config.dry_run && !up_to_date(&version_input, &version_info) {
let mut info = String::new();
t!(t!(File::open(&version_input)).read_to_string(&mut info));
let info = info.replace("VERSION", &builder.rust_release())
.replace("SHORT_HASH", builder.rust_info.sha_short().unwrap_or(""))
.replace("STAMP", builder.rust_info.sha().unwrap_or(""));
t!(t!(File::create(&version_info)).write_all(info.as_bytes()));
let info = t!(fs::read_to_string(&version_input))
.replace("VERSION", &builder.rust_release())
.replace("SHORT_HASH", builder.rust_info.sha_short().unwrap_or(""))
.replace("STAMP", builder.rust_info.sha().unwrap_or(""));
t!(fs::write(&version_info, &info));
}

for file in t!(fs::read_dir(builder.src.join("src/doc"))) {
Expand Down
6 changes: 2 additions & 4 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,9 +1067,8 @@ impl Build {

/// Returns the `a.b.c` version that the given package is at.
fn release_num(&self, package: &str) -> String {
let mut toml = String::new();
let toml_file_name = self.src.join(&format!("src/tools/{}/Cargo.toml", package));
t!(t!(File::open(toml_file_name)).read_to_string(&mut toml));
let toml = t!(fs::read_to_string(&toml_file_name));
for line in toml.lines() {
let prefix = "version = \"";
let suffix = "\"";
Expand Down Expand Up @@ -1151,8 +1150,7 @@ impl Build {
}

let mut paths = Vec::new();
let mut contents = Vec::new();
t!(t!(File::open(stamp)).read_to_end(&mut contents));
let contents = t!(fs::read(stamp));
// This is the method we use for extracting paths from the stamp file passed to us. See
// run_cargo for more information (in compile.rs).
for part in contents.split(|b| *b == 0) {
Expand Down
9 changes: 3 additions & 6 deletions src/bootstrap/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use std::env;
use std::ffi::OsString;
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::Command;

Expand Down Expand Up @@ -75,8 +74,7 @@ impl Step for Llvm {
}

let rebuild_trigger = builder.src.join("src/rustllvm/llvm-rebuild-trigger");
let mut rebuild_trigger_contents = String::new();
t!(t!(File::open(&rebuild_trigger)).read_to_string(&mut rebuild_trigger_contents));
let rebuild_trigger_contents = t!(fs::read_to_string(&rebuild_trigger));

let (out_dir, llvm_config_ret_dir) = if emscripten {
let dir = builder.emscripten_llvm_out(target);
Expand All @@ -93,8 +91,7 @@ impl Step for Llvm {
let build_llvm_config = llvm_config_ret_dir
.join(exe("llvm-config", &*builder.config.build));
if done_stamp.exists() {
let mut done_contents = String::new();
t!(t!(File::open(&done_stamp)).read_to_string(&mut done_contents));
let done_contents = t!(fs::read_to_string(&done_stamp));

// If LLVM was already built previously and contents of the rebuild-trigger file
// didn't change from the previous build, then no action is required.
Expand Down Expand Up @@ -261,7 +258,7 @@ impl Step for Llvm {

cfg.build();

t!(t!(File::create(&done_stamp)).write_all(rebuild_trigger_contents.as_bytes()));
t!(fs::write(&done_stamp, &rebuild_trigger_contents));

build_llvm_config
}
Expand Down
7 changes: 2 additions & 5 deletions src/bootstrap/sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
use std::collections::HashMap;
use std::env;
use std::ffi::{OsString, OsStr};
use std::fs::{self, File};
use std::io::Read;
use std::fs;
use std::path::PathBuf;
use std::process::Command;

Expand Down Expand Up @@ -235,9 +234,7 @@ $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
}

if build.config.channel == "stable" {
let mut stage0 = String::new();
t!(t!(File::open(build.src.join("src/stage0.txt")))
.read_to_string(&mut stage0));
let stage0 = t!(fs::read_to_string(build.src.join("src/stage0.txt")));
if stage0.contains("\ndev:") {
panic!("bootstrapping from a dev compiler in a stable release, but \
should only be bootstrapping from a released compiler!");
Expand Down
9 changes: 3 additions & 6 deletions src/bootstrap/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
use std::env;
use std::ffi::OsString;
use std::fmt;
use std::fs::{self, File};
use std::io::Read;
use std::fs;
use std::iter;
use std::path::{Path, PathBuf};
use std::process::Command;
Expand Down Expand Up @@ -1427,10 +1426,8 @@ impl Step for ErrorIndex {
}

fn markdown_test(builder: &Builder, compiler: Compiler, markdown: &Path) -> bool {
match File::open(markdown) {
Ok(mut file) => {
let mut contents = String::new();
t!(file.read_to_string(&mut contents));
match fs::read_to_string(markdown) {
Ok(contents) => {
if !contents.contains("```") {
return true;
}
Expand Down
7 changes: 3 additions & 4 deletions src/libcore/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ pub trait Into<T>: Sized {
/// An example usage for error handling:
///
/// ```
/// use std::io::{self, Read};
/// use std::fs;
/// use std::io;
/// use std::num;
///
/// enum CliError {
Expand All @@ -348,9 +349,7 @@ pub trait Into<T>: Sized {
/// }
///
/// fn open_and_parse_file(file_name: &str) -> Result<i32, CliError> {
/// let mut file = std::fs::File::open("test")?;
/// let mut contents = String::new();
/// file.read_to_string(&mut contents)?;
/// let mut contents = fs::read_to_string(&file_name)?;
/// let num: i32 = contents.trim().parse()?;
/// Ok(num)
/// }
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/infer/lexical_region_resolve/graphviz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ use std::borrow::Cow;
use std::collections::hash_map::Entry::Vacant;
use std::collections::btree_map::BTreeMap;
use std::env;
use std::fs::File;
use std::fs;
use std::io;
use std::io::prelude::*;
use std::sync::atomic::{AtomicBool, Ordering};

fn print_help_message() {
Expand Down Expand Up @@ -268,5 +267,5 @@ fn dump_region_data_to<'a, 'gcx, 'tcx>(region_rels: &RegionRelations<'a, 'gcx, '
debug!("dump_region_data calling render");
let mut v = Vec::new();
dot::render(&g, &mut v).unwrap();
File::create(path).and_then(|mut f| f.write_all(&v))
fs::write(path, &v)
}
13 changes: 4 additions & 9 deletions src/librustc_codegen_utils/codegen_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#![feature(box_syntax)]

use std::any::Any;
use std::io::{self, Write};
use std::fs::File;
use std::io::Write;
use std::fs;
use std::path::Path;
use std::sync::{mpsc, Arc};

Expand Down Expand Up @@ -81,11 +81,7 @@ pub struct NoLlvmMetadataLoader;

impl MetadataLoader for NoLlvmMetadataLoader {
fn get_rlib_metadata(&self, _: &Target, filename: &Path) -> Result<MetadataRef, String> {
let mut file = File::open(filename)
.map_err(|e| format!("metadata file open err: {:?}", e))?;

let mut buf = Vec::new();
io::copy(&mut file, &mut buf).unwrap();
let buf = fs::read(filename).map_err(|e| format!("metadata file open err: {:?}", e))?;
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf);
Ok(rustc_erase_owner!(buf.map_owner_box()))
}
Expand Down Expand Up @@ -209,8 +205,7 @@ impl CodegenBackend for MetadataOnlyCodegenBackend {
} else {
&ongoing_codegen.metadata.raw_data
};
let mut file = File::create(&output_name).unwrap();
file.write_all(metadata).unwrap();
fs::write(&output_name, metadata).unwrap();
}

sess.abort_if_errors();
Expand Down
16 changes: 4 additions & 12 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,10 +778,7 @@ fn write_shared(
let mut themes: FxHashSet<String> = FxHashSet::default();

for entry in &cx.shared.themes {
let mut content = Vec::with_capacity(100000);

let mut f = try_err!(File::open(&entry), &entry);
try_err!(f.read_to_end(&mut content), &entry);
let content = try_err!(fs::read(&entry), &entry);
let theme = try_none!(try_none!(entry.file_stem(), &entry).to_str(), &entry);
let extension = try_none!(try_none!(entry.extension(), &entry).to_str(), &entry);
write(cx.dst.join(format!("{}{}.{}", theme, cx.shared.resource_suffix, extension)),
Expand Down Expand Up @@ -881,10 +878,7 @@ themePicker.onblur = handleThemeButtonsBlur;
if !options.enable_minification {
try_err!(fs::copy(css, out), css);
} else {
let mut f = try_err!(File::open(css), css);
let mut buffer = String::with_capacity(1000);

try_err!(f.read_to_string(&mut buffer), css);
let buffer = try_err!(fs::read_to_string(css), css);
write_minify(out, &buffer, options.enable_minification)?;
}
}
Expand Down Expand Up @@ -2102,8 +2096,7 @@ impl Context {
if !buf.is_empty() {
try_err!(this.shared.ensure_dir(&this.dst), &this.dst);
let joint_dst = this.dst.join("index.html");
let mut dst = try_err!(File::create(&joint_dst), &joint_dst);
try_err!(dst.write_all(&buf), &joint_dst);
try_err!(fs::write(&joint_dst, buf), &joint_dst);
}

let m = match item.inner {
Expand Down Expand Up @@ -2137,8 +2130,7 @@ impl Context {
let file_name = &item_path(item_type, name);
try_err!(self.shared.ensure_dir(&self.dst), &self.dst);
let joint_dst = self.dst.join(file_name);
let mut dst = try_err!(File::create(&joint_dst), &joint_dst);
try_err!(dst.write_all(&buf), &joint_dst);
try_err!(fs::write(&joint_dst, buf), &joint_dst);

if !self.render_redirect_pages {
all.append(full_path(self, &item), &item_type);
Expand Down
10 changes: 3 additions & 7 deletions src/librustdoc/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
// except according to those terms.

use rustc_data_structures::fx::FxHashSet;
use std::fs::File;
use std::fs;
use std::hash::{Hash, Hasher};
use std::io::Read;
use std::path::Path;

use errors::Handler;
Expand Down Expand Up @@ -278,12 +277,9 @@ pub fn get_differences(against: &CssPath, other: &CssPath, v: &mut Vec<String>)
pub fn test_theme_against<P: AsRef<Path>>(f: &P, against: &CssPath, diag: &Handler)
-> (bool, Vec<String>)
{
let mut file = try_something!(File::open(f), diag, (false, Vec::new()));
let mut data = Vec::with_capacity(1000);

try_something!(file.read_to_end(&mut data), diag, (false, Vec::new()));
let data = try_something!(fs::read(f), diag, (false, vec![]));
let paths = load_css_paths(&data);
let mut ret = Vec::new();
let mut ret = vec![];
get_differences(against, &paths, &mut ret);
(true, ret)
}
Expand Down
Loading

0 comments on commit 0a77980

Please sign in to comment.