Optimize away some fs::metadata calls.

This also eliminates a use of a `Path` convenience function, in support
of #80741, refactoring `std::path` to focus on pure data structures and
algorithms.
This commit is contained in:
Dan Gohman 2021-01-06 08:31:25 -08:00
parent da305a2b00
commit 304643c00d
3 changed files with 34 additions and 32 deletions

View File

@ -52,11 +52,11 @@ pub fn read_file(
path: &Path, path: &Path,
nightly_build: bool, nightly_build: bool,
) -> io::Result<Option<(Vec<u8>, usize)>> { ) -> io::Result<Option<(Vec<u8>, usize)>> {
if !path.exists() { let data = match fs::read(path) {
return Ok(None); Ok(data) => data,
} Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(err) => return Err(err),
let data = fs::read(path)?; };
let mut file = io::Cursor::new(data); let mut file = io::Cursor::new(data);

View File

@ -922,22 +922,24 @@ fn all_except_most_recent(
/// before passing it to std::fs::remove_dir_all(). This will convert the path /// before passing it to std::fs::remove_dir_all(). This will convert the path
/// into the '\\?\' format, which supports much longer paths. /// into the '\\?\' format, which supports much longer paths.
fn safe_remove_dir_all(p: &Path) -> io::Result<()> { fn safe_remove_dir_all(p: &Path) -> io::Result<()> {
if p.exists() { let canonicalized = match std_fs::canonicalize(p) {
let canonicalized = p.canonicalize()?; Ok(canonicalized) => canonicalized,
std_fs::remove_dir_all(canonicalized) Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
} else { Err(err) => return Err(err),
Ok(()) };
}
std_fs::remove_dir_all(canonicalized)
} }
fn safe_remove_file(p: &Path) -> io::Result<()> { fn safe_remove_file(p: &Path) -> io::Result<()> {
if p.exists() { let canonicalized = match std_fs::canonicalize(p) {
let canonicalized = p.canonicalize()?; Ok(canonicalized) => canonicalized,
match std_fs::remove_file(canonicalized) { Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
Err(ref err) if err.kind() == io::ErrorKind::NotFound => Ok(()), Err(err) => return Err(err),
result => result, };
}
} else { match std_fs::remove_file(canonicalized) {
Ok(()) Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
result => result,
} }
} }

View File

@ -6,6 +6,7 @@ use rustc_serialize::opaque::Encoder;
use rustc_serialize::Encodable as RustcEncodable; use rustc_serialize::Encodable as RustcEncodable;
use rustc_session::Session; use rustc_session::Session;
use std::fs; use std::fs;
use std::io;
use std::path::PathBuf; use std::path::PathBuf;
use super::data::*; use super::data::*;
@ -101,19 +102,18 @@ where
// Note: It's important that we actually delete the old file and not just // Note: It's important that we actually delete the old file and not just
// truncate and overwrite it, since it might be a shared hard-link, the // truncate and overwrite it, since it might be a shared hard-link, the
// underlying data of which we don't want to modify // underlying data of which we don't want to modify
if path_buf.exists() { match fs::remove_file(&path_buf) {
match fs::remove_file(&path_buf) { Ok(()) => {
Ok(()) => { debug!("save: remove old file");
debug!("save: remove old file"); }
} Err(err) if err.kind() == io::ErrorKind::NotFound => (),
Err(err) => { Err(err) => {
sess.err(&format!( sess.err(&format!(
"unable to delete old dep-graph at `{}`: {}", "unable to delete old dep-graph at `{}`: {}",
path_buf.display(), path_buf.display(),
err err
)); ));
return; return;
}
} }
} }