Skip to content

Commit b7aae1b

Browse files
committed
Provide convenience functions exists, is_file, and is_dir, in fs.
1 parent 5dfd1c1 commit b7aae1b

File tree

20 files changed

+158
-73
lines changed

20 files changed

+158
-73
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,8 @@ fn thin_lto(
496496
// If the previous file was deleted, or we get an IO error
497497
// reading the file, then we'll just use `None` as the
498498
// prev_key_map, which will force the code to be recompiled.
499-
let prev = if fs::metadata(&path).is_ok() {
500-
ThinLTOKeysMap::load_from_file(&path).ok()
501-
} else {
502-
None
503-
};
499+
let prev =
500+
if fs::exists(&path) { ThinLTOKeysMap::load_from_file(&path).ok() } else { None };
504501
let curr = ThinLTOKeysMap::from_thin_lto_modules(&data, &thin_modules, &module_names);
505502
(Some(path), prev, curr)
506503
} else {

compiler/rustc_codegen_ssa/src/back/archive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ pub fn find_library(name: Symbol, search_paths: &[PathBuf], sess: &Session) -> P
1515
for path in search_paths {
1616
debug!("looking for {} inside {:?}", name, path);
1717
let test = path.join(&oslibname);
18-
if fs::metadata(&test).is_ok() {
18+
if fs::exists(&test) {
1919
return test;
2020
}
2121
if oslibname != unixlibname {
2222
let test = path.join(&unixlibname);
23-
if fs::metadata(&test).is_ok() {
23+
if fs::exists(&test) {
2424
return test;
2525
}
2626
}

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,19 +1110,19 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
11101110
fn get_object_file_path(sess: &Session, name: &str, self_contained: bool) -> PathBuf {
11111111
let fs = sess.target_filesearch(PathKind::Native);
11121112
let file_path = fs.get_lib_path().join(name);
1113-
if fs::metadata(&file_path).is_ok() {
1113+
if fs::exists(&file_path) {
11141114
return file_path;
11151115
}
11161116
// Special directory with objects used only in self-contained linkage mode
11171117
if self_contained {
11181118
let file_path = fs.get_self_contained_lib_path().join(name);
1119-
if fs::metadata(&file_path).is_ok() {
1119+
if fs::exists(&file_path) {
11201120
return file_path;
11211121
}
11221122
}
11231123
for search_path in fs.search_paths() {
11241124
let file_path = search_path.dir.join(name);
1125-
if fs::metadata(&file_path).is_ok() {
1125+
if fs::exists(&file_path) {
11261126
return file_path;
11271127
}
11281128
}
@@ -1312,9 +1312,7 @@ fn detect_self_contained_mingw(sess: &Session) -> bool {
13121312
for dir in env::split_paths(&env::var_os("PATH").unwrap_or_default()) {
13131313
let full_path = dir.join(&linker_with_extension);
13141314
// If linker comes from sysroot assume self-contained mode
1315-
if fs::metadata(&full_path).map(|m| m.is_file()).unwrap_or(false)
1316-
&& !full_path.starts_with(&sess.sysroot)
1317-
{
1315+
if fs::is_file(&full_path) && !full_path.starts_with(&sess.sysroot) {
13181316
return false;
13191317
}
13201318
}
@@ -2230,7 +2228,7 @@ fn get_apple_sdk_root(sdk_name: &str) -> Result<String, String> {
22302228
if sdkroot.contains("iPhoneOS.platform")
22312229
|| sdkroot.contains("iPhoneSimulator.platform") => {}
22322230
// Ignore `SDKROOT` if it's not a valid path.
2233-
_ if !p.is_absolute() || p == Path::new("/") || fs::metadata(p).is_err() => {}
2231+
_ if !p.is_absolute() || p == Path::new("/") || !fs::exists(p) => {}
22342232
_ => return Ok(sdkroot),
22352233
}
22362234
}

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ impl<'a> Linker for MsvcLinker<'a> {
695695
// check to see if the file is there and just omit linking to it if it's
696696
// not present.
697697
let name = format!("{}.dll.lib", lib);
698-
if fs::metadata(&path.join(&name)).is_ok() {
698+
if fs::exists(&path.join(&name)) {
699699
self.cmd.arg(name);
700700
}
701701
}

compiler/rustc_incremental/src/persist/load.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
143143
let mut all_files_exist = true;
144144
if let Some(ref file_name) = swp.work_product.saved_file {
145145
let path = in_incr_comp_dir_sess(sess, file_name);
146-
if fs::metadata(&path).is_err() {
146+
if !fs::exists(&path) {
147147
all_files_exist = false;
148148

149149
if sess.opts.debugging_opts.incremental_info {

compiler/rustc_incremental/src/persist/save.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn save_work_product_index(
7474
if !new_work_products.contains_key(id) {
7575
work_product::delete_workproduct_files(sess, wp);
7676
debug_assert!(wp.saved_file.as_ref().map_or(true, |file_name| {
77-
fs::metadata(in_incr_comp_dir_sess(sess, &file_name)).is_err()
77+
!fs::exists(in_incr_comp_dir_sess(sess, &file_name))
7878
}));
7979
}
8080
}
@@ -85,7 +85,7 @@ pub fn save_work_product_index(
8585
.iter()
8686
.flat_map(|(_, wp)| wp.saved_file.iter())
8787
.map(|name| in_incr_comp_dir_sess(sess, name))
88-
.all(|path| fs::metadata(path).is_ok())
88+
.all(|path| fs::exists(path))
8989
});
9090
}
9191

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,7 @@ fn output_contains_path(output_paths: &[PathBuf], input_path: &PathBuf) -> bool
517517
}
518518

519519
fn output_conflicts_with_dir(output_paths: &[PathBuf]) -> Option<PathBuf> {
520-
let check = |output_path: &PathBuf| {
521-
fs::metadata(output_path).map(|m| m.is_dir()).unwrap_or(false).then(|| output_path.clone())
522-
};
520+
let check = |output_path: &PathBuf| fs::is_dir(output_path).then(|| output_path.clone());
523521
check_output(output_paths, check)
524522
}
525523

compiler/rustc_interface/src/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
270270
} else {
271271
"rustc"
272272
});
273-
fs::metadata(&candidate).is_ok().then_some(candidate)
273+
fs::exists(&candidate).then_some(candidate)
274274
})
275275
}
276276

@@ -398,7 +398,7 @@ pub fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend
398398
})
399399
.find(|f| {
400400
info!("codegen backend candidate: {}", f.display());
401-
fs::metadata(f).is_ok()
401+
fs::exists(f)
402402
});
403403
let sysroot = sysroot.unwrap_or_else(|| {
404404
let candidates = sysroot_candidates

compiler/rustc_llvm/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ fn main() {
206206
stripped
207207
} else if let Some(stripped) = lib.strip_prefix('-') {
208208
stripped
209-
} else if fs::metadata(Path::new(lib)).is_ok() {
209+
} else if fs::exists(Path::new(lib)) {
210210
// On MSVC llvm-config will print the full name to libraries, but
211211
// we're only interested in the name part
212212
let name = Path::new(lib).file_name().unwrap().to_str().unwrap();

compiler/rustc_metadata/src/locator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ impl<'a> CrateLocator<'a> {
664664
let mut rmetas = FxHashMap::default();
665665
let mut dylibs = FxHashMap::default();
666666
for loc in &self.exact_paths {
667-
if fs::metadata(loc).is_err() {
667+
if !fs::exists(loc) {
668668
return Err(CrateError::ExternLocationNotExist(self.crate_name, loc.clone()));
669669
}
670670
let file = match loc.file_name().and_then(|s| s.to_str()) {
@@ -738,7 +738,7 @@ fn get_metadata_section(
738738
filename: &Path,
739739
loader: &dyn MetadataLoader,
740740
) -> Result<MetadataBlob, String> {
741-
if fs::metadata(filename).is_err() {
741+
if !fs::exists(filename) {
742742
return Err(format!("no such file: '{}'", filename.display()));
743743
}
744744
let raw_bytes: MetadataRef = match flavor {

0 commit comments

Comments
 (0)