diff options
| author | bors <bors@rust-lang.org> | 2024-06-11 15:50:25 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-06-11 15:50:25 +0000 |
| commit | 3ea5e236ecb4c5f22437059f82d3915d311e4ec0 (patch) | |
| tree | d53e401384bc689dfab71897283ef6c03638e8a1 /src | |
| parent | 0c960618b56f662d933e8b864cd9632a99174e87 (diff) | |
| parent | c84afee89851f0d0d5089d64d35225a6adb28453 (diff) | |
| download | rust-3ea5e236ecb4c5f22437059f82d3915d311e4ec0.tar.gz rust-3ea5e236ecb4c5f22437059f82d3915d311e4ec0.zip | |
Auto merge of #125736 - Oneirical:run-make-file-management, r=jieyouxu
run-make-support: add wrapper for `fs` operations Suggested by #125728. The point of this wrapper is to stop silent fails caused by forgetting to `unwrap` `fs` functions. However, functions like `fs::read` which return something and get stored in a variable should cause a failure on their own if they are not unwrapped (as the `Result` will be stored in the variable, and something will be done on that `Result` that should have been done to its contents). Is it still pertinent to wrap `fs::read_to_string`, `fs::metadata` and so on? Closes: https://github.com/rust-lang/rust/issues/125728 try-job: x86_64-msvc try-job: i686-mingw
Diffstat (limited to 'src')
| -rw-r--r-- | src/tools/run-make-support/src/fs_wrapper.rs | 113 | ||||
| -rw-r--r-- | src/tools/run-make-support/src/lib.rs | 22 |
2 files changed, 121 insertions, 14 deletions
diff --git a/src/tools/run-make-support/src/fs_wrapper.rs b/src/tools/run-make-support/src/fs_wrapper.rs new file mode 100644 index 00000000000..8a2bfce8b4a --- /dev/null +++ b/src/tools/run-make-support/src/fs_wrapper.rs @@ -0,0 +1,113 @@ +use std::fs; +use std::path::Path; + +/// A wrapper around [`std::fs::remove_file`] which includes the file path in the panic message.. +#[track_caller] +pub fn remove_file<P: AsRef<Path>>(path: P) { + fs::remove_file(path.as_ref()) + .expect(&format!("the file in path \"{}\" could not be removed", path.as_ref().display())); +} + +/// A wrapper around [`std::fs::copy`] which includes the file path in the panic message. +#[track_caller] +pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) { + fs::copy(from.as_ref(), to.as_ref()).expect(&format!( + "the file \"{}\" could not be copied over to \"{}\"", + from.as_ref().display(), + to.as_ref().display(), + )); +} + +/// A wrapper around [`std::fs::File::create`] which includes the file path in the panic message.. +#[track_caller] +pub fn create_file<P: AsRef<Path>>(path: P) { + fs::File::create(path.as_ref()) + .expect(&format!("the file in path \"{}\" could not be created", path.as_ref().display())); +} + +/// A wrapper around [`std::fs::read`] which includes the file path in the panic message.. +#[track_caller] +pub fn read<P: AsRef<Path>>(path: P) -> Vec<u8> { + fs::read(path.as_ref()) + .expect(&format!("the file in path \"{}\" could not be read", path.as_ref().display())) +} + +/// A wrapper around [`std::fs::read_to_string`] which includes the file path in the panic message.. +#[track_caller] +pub fn read_to_string<P: AsRef<Path>>(path: P) -> String { + fs::read_to_string(path.as_ref()).expect(&format!( + "the file in path \"{}\" could not be read into a String", + path.as_ref().display() + )) +} + +/// A wrapper around [`std::fs::read_dir`] which includes the file path in the panic message.. +#[track_caller] +pub fn read_dir<P: AsRef<Path>>(path: P) -> fs::ReadDir { + fs::read_dir(path.as_ref()) + .expect(&format!("the directory in path \"{}\" could not be read", path.as_ref().display())) +} + +/// A wrapper around [`std::fs::write`] which includes the file path in the panic message.. +#[track_caller] +pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) { + fs::write(path.as_ref(), contents.as_ref()).expect(&format!( + "the file in path \"{}\" could not be written to", + path.as_ref().display() + )); +} + +/// A wrapper around [`std::fs::remove_dir_all`] which includes the file path in the panic message.. +#[track_caller] +pub fn remove_dir_all<P: AsRef<Path>>(path: P) { + fs::remove_dir_all(path.as_ref()).expect(&format!( + "the directory in path \"{}\" could not be removed alongside all its contents", + path.as_ref().display(), + )); +} + +/// A wrapper around [`std::fs::create_dir`] which includes the file path in the panic message.. +#[track_caller] +pub fn create_dir<P: AsRef<Path>>(path: P) { + fs::create_dir(path.as_ref()).expect(&format!( + "the directory in path \"{}\" could not be created", + path.as_ref().display() + )); +} + +/// A wrapper around [`std::fs::create_dir_all`] which includes the file path in the panic message.. +#[track_caller] +pub fn create_dir_all<P: AsRef<Path>>(path: P) { + fs::create_dir_all(path.as_ref()).expect(&format!( + "the directory (and all its parents) in path \"{}\" could not be created", + path.as_ref().display() + )); +} + +/// A wrapper around [`std::fs::metadata`] which includes the file path in the panic message.. +#[track_caller] +pub fn metadata<P: AsRef<Path>>(path: P) -> fs::Metadata { + fs::metadata(path.as_ref()).expect(&format!( + "the file's metadata in path \"{}\" could not be read", + path.as_ref().display() + )) +} + +/// A wrapper around [`std::fs::rename`] which includes the file path in the panic message. +#[track_caller] +pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) { + fs::rename(from.as_ref(), to.as_ref()).expect(&format!( + "the file \"{}\" could not be moved over to \"{}\"", + from.as_ref().display(), + to.as_ref().display(), + )); +} + +/// A wrapper around [`std::fs::set_permissions`] which includes the file path in the panic message. +#[track_caller] +pub fn set_permissions<P: AsRef<Path>>(path: P, perm: fs::Permissions) { + fs::set_permissions(path.as_ref(), perm).expect(&format!( + "the file's permissions in path \"{}\" could not be changed", + path.as_ref().display() + )); +} diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index ee764b983d0..b920f9a07db 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -8,6 +8,7 @@ pub mod clang; mod command; pub mod diff; mod drop_bomb; +pub mod fs_wrapper; pub mod llvm_readobj; pub mod run; pub mod rustc; @@ -153,7 +154,7 @@ pub fn dynamic_lib_extension() -> &'static str { } } -/// Construct a rust library (rlib) name. +/// Generate the name a rust library (rlib) would have. pub fn rust_lib_name(name: &str) -> String { format!("lib{name}.rlib") } @@ -228,15 +229,15 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) { fn copy_dir_all_inner(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> { let dst = dst.as_ref(); if !dst.is_dir() { - fs::create_dir_all(&dst)?; + std::fs::create_dir_all(&dst)?; } - for entry in fs::read_dir(src)? { + for entry in std::fs::read_dir(src)? { let entry = entry?; let ty = entry.file_type()?; if ty.is_dir() { copy_dir_all_inner(entry.path(), dst.join(entry.file_name()))?; } else { - fs::copy(entry.path(), dst.join(entry.file_name()))?; + std::fs::copy(entry.path(), dst.join(entry.file_name()))?; } } Ok(()) @@ -255,13 +256,6 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) { /// Check that all files in `dir1` exist and have the same content in `dir2`. Panic otherwise. pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) { - fn read_file(path: &Path) -> Vec<u8> { - match fs::read(path) { - Ok(c) => c, - Err(e) => panic!("Failed to read `{}`: {:?}", path.display(), e), - } - } - let dir2 = dir2.as_ref(); read_dir(dir1, |entry_path| { let entry_name = entry_path.file_name().unwrap(); @@ -269,8 +263,8 @@ pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) { recursive_diff(&entry_path, &dir2.join(entry_name)); } else { let path2 = dir2.join(entry_name); - let file1 = read_file(&entry_path); - let file2 = read_file(&path2); + let file1 = fs_wrapper::read(&entry_path); + let file2 = fs_wrapper::read(&path2); // We don't use `assert_eq!` because they are `Vec<u8>`, so not great for display. // Why not using String? Because there might be minified files or even potentially @@ -286,7 +280,7 @@ pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) { } pub fn read_dir<F: Fn(&Path)>(dir: impl AsRef<Path>, callback: F) { - for entry in fs::read_dir(dir).unwrap() { + for entry in fs_wrapper::read_dir(dir) { callback(&entry.unwrap().path()); } } |
