Use Path methods instead of fs::metadata.

This commit is contained in:
Eric Huss 2020-04-16 18:41:03 -07:00
parent f9047dc2d6
commit 4367ec4d11
13 changed files with 29 additions and 40 deletions

View File

@ -162,7 +162,7 @@ pub struct Dependency {
pub fn init() {
let config = paths::home().join(".cargo/config");
t!(fs::create_dir_all(config.parent().unwrap()));
if fs::metadata(&config).is_ok() {
if config.exists() {
return;
}
t!(t!(File::create(&config)).write_all(

View File

@ -165,9 +165,7 @@ fn is_executable<P: AsRef<Path>>(path: P) -> bool {
}
#[cfg(windows)]
fn is_executable<P: AsRef<Path>>(path: P) -> bool {
fs::metadata(path)
.map(|metadata| metadata.is_file())
.unwrap_or(false)
path.as_ref().is_file()
}
fn search_directories(config: &Config) -> Vec<PathBuf> {

View File

@ -273,10 +273,7 @@ fn detect_source_paths_and_types(
let pp = i.proposed_path;
// path/pp does not exist or is not a file
if !fs::metadata(&path.join(&pp))
.map(|x| x.is_file())
.unwrap_or(false)
{
if !path.join(&pp).is_file() {
continue;
}
@ -358,7 +355,7 @@ fn plan_new_source_file(bin: bool, package_name: String) -> SourceFileInformatio
pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
let path = &opts.path;
if fs::metadata(path).is_ok() {
if path.exists() {
anyhow::bail!(
"destination `{}` already exists\n\n\
Use `cargo init` to initialize the directory",
@ -397,7 +394,7 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
let path = &opts.path;
if fs::metadata(&path.join("Cargo.toml")).is_ok() {
if path.join("Cargo.toml").exists() {
anyhow::bail!("`cargo init` cannot be run on existing Cargo packages")
}
@ -428,22 +425,22 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
if version_control == None {
let mut num_detected_vsces = 0;
if fs::metadata(&path.join(".git")).is_ok() {
if path.join(".git").exists() {
version_control = Some(VersionControl::Git);
num_detected_vsces += 1;
}
if fs::metadata(&path.join(".hg")).is_ok() {
if path.join(".hg").exists() {
version_control = Some(VersionControl::Hg);
num_detected_vsces += 1;
}
if fs::metadata(&path.join(".pijul")).is_ok() {
if path.join(".pijul").exists() {
version_control = Some(VersionControl::Pijul);
num_detected_vsces += 1;
}
if fs::metadata(&path.join(".fossil")).is_ok() {
if path.join(".fossil").exists() {
version_control = Some(VersionControl::Fossil);
num_detected_vsces += 1;
}
@ -743,10 +740,7 @@ mod tests {
"
};
if !fs::metadata(&path_of_source_file)
.map(|x| x.is_file())
.unwrap_or(false)
{
if !path_of_source_file.is_file() {
paths::write(&path_of_source_file, default_file_content)?;
// Format the newly created source file

View File

@ -60,7 +60,7 @@ pub fn read_packages(
}
// Don't automatically discover packages across git submodules
if fs::metadata(&dir.join(".git")).is_ok() {
if dir.join(".git").exists() {
return Ok(false);
}
}

View File

@ -1,5 +1,5 @@
use std::collections::{BTreeMap, HashSet};
use std::fs::{self, File};
use std::fs::File;
use std::io::{self, BufRead};
use std::iter::repeat;
use std::str;
@ -233,7 +233,7 @@ fn transmit(
None => None,
};
if let Some(ref file) = *license_file {
if fs::metadata(&pkg.root().join(file)).is_err() {
if !pkg.root().join(file).exists() {
bail!("the license file `{}` does not exist", file)
}
}

View File

@ -373,14 +373,14 @@ impl<'cfg> PathSource<'cfg> {
is_root: bool,
filter: &mut dyn FnMut(&Path) -> CargoResult<bool>,
) -> CargoResult<()> {
if !fs::metadata(&path).map(|m| m.is_dir()).unwrap_or(false) {
if !path.is_dir() {
if (*filter)(path)? {
ret.push(path.to_path_buf());
}
return Ok(());
}
// Don't recurse into any sub-packages that we have.
if !is_root && fs::metadata(&path.join("Cargo.toml")).is_ok() {
if !is_root && path.join("Cargo.toml").exists() {
return Ok(());
}

View File

@ -13,7 +13,6 @@ use crate::CargoResult;
use anyhow::bail;
use clap::{self, SubCommand};
use std::ffi::{OsStr, OsString};
use std::fs;
use std::path::PathBuf;
pub use crate::core::compiler::CompileMode;
@ -285,7 +284,7 @@ pub trait ArgMatchesExt {
if !path.ends_with("Cargo.toml") {
anyhow::bail!("the manifest-path must be a path to a Cargo.toml file")
}
if fs::metadata(&path).is_err() {
if !path.exists() {
anyhow::bail!(
"manifest path `{}` does not exist",
self._value_of("manifest-path").unwrap()

View File

@ -949,8 +949,8 @@ impl Config {
let possible = dir.join(filename_without_extension);
let possible_with_extension = dir.join(format!("{}.toml", filename_without_extension));
if fs::metadata(&possible).is_ok() {
if warn && fs::metadata(&possible_with_extension).is_ok() {
if possible.exists() {
if warn && possible_with_extension.exists() {
// We don't want to print a warning if the version
// without the extension is just a symlink to the version
// WITH an extension, which people may want to do to
@ -973,7 +973,7 @@ impl Config {
}
Ok(Some(possible))
} else if fs::metadata(&possible_with_extension).is_ok() {
} else if possible_with_extension.exists() {
Ok(Some(possible_with_extension))
} else {
Ok(None)

View File

@ -1,6 +1,5 @@
use crate::util::errors::CargoResult;
use crate::util::paths;
use std::fs;
use std::path::{Path, PathBuf};
/// Finds the root `Cargo.toml`.
@ -8,7 +7,7 @@ pub fn find_root_manifest_for_wd(cwd: &Path) -> CargoResult<PathBuf> {
let file = "Cargo.toml";
for current in paths::ancestors(cwd) {
let manifest = current.join(file);
if fs::metadata(&manifest).is_ok() {
if manifest.exists() {
return Ok(manifest);
}
}

View File

@ -1,6 +1,5 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::str;
@ -1441,11 +1440,12 @@ impl TomlManifest {
Some(StringOrBool::Bool(true)) => Some(build_rs),
Some(StringOrBool::String(ref s)) => Some(PathBuf::from(s)),
None => {
match fs::metadata(&build_rs) {
// If there is a `build.rs` file next to the `Cargo.toml`, assume it is
// a build script.
Ok(ref e) if e.is_file() => Some(build_rs),
Ok(_) | Err(_) => None,
// If there is a `build.rs` file next to the `Cargo.toml`, assume it is
// a build script.
if build_rs.is_file() {
Some(build_rs)
} else {
None
}
}
}

View File

@ -557,7 +557,7 @@ fn clean_targets_with_legacy_path(
fn inferred_lib(package_root: &Path) -> Option<PathBuf> {
let lib = package_root.join("src").join("lib.rs");
if fs::metadata(&lib).is_ok() {
if lib.exists() {
Some(lib)
} else {
None

View File

@ -85,7 +85,6 @@ fn custom_build_env_vars() {
use std::env;
use std::io::prelude::*;
use std::path::Path;
use std::fs;
fn main() {{
let _target = env::var("TARGET").unwrap();
@ -103,7 +102,7 @@ fn custom_build_env_vars() {
let out = env::var("OUT_DIR").unwrap();
assert!(out.starts_with(r"{0}"));
assert!(fs::metadata(&out).map(|m| m.is_dir()).unwrap_or(false));
assert!(Path::new(&out).is_dir());
let _host = env::var("HOST").unwrap();

View File

@ -185,7 +185,7 @@ fn build_script() {
if env::var("FIRST").is_ok() {
std::fs::File::create(out.join("out")).unwrap();
} else {
assert!(!std::fs::metadata(out.join("out")).is_ok());
assert!(!out.join("out").exists());
}
}
"#,