From c84afee89851f0d0d5089d64d35225a6adb28453 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Thu, 6 Jun 2024 20:47:00 +0200 Subject: Implement fs wrapper for run_make_support --- src/tools/run-make-support/src/fs_wrapper.rs | 113 +++++++++++++++++++++++++++ src/tools/run-make-support/src/lib.rs | 22 ++---- 2 files changed, 121 insertions(+), 14 deletions(-) create mode 100644 src/tools/run-make-support/src/fs_wrapper.rs (limited to 'src') 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>(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, Q: AsRef>(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>(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>(path: P) -> Vec { + 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>(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>(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, 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>(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>(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>(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>(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, Q: AsRef>(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>(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 bf74d94f911..db6ca2b35db 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; @@ -148,7 +149,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") } @@ -223,15 +224,15 @@ pub fn copy_dir_all(src: impl AsRef, dst: impl AsRef) { fn copy_dir_all_inner(src: impl AsRef, dst: impl AsRef) -> 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(()) @@ -250,13 +251,6 @@ pub fn copy_dir_all(src: impl AsRef, dst: impl AsRef) { /// Check that all files in `dir1` exist and have the same content in `dir2`. Panic otherwise. pub fn recursive_diff(dir1: impl AsRef, dir2: impl AsRef) { - fn read_file(path: &Path) -> Vec { - 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(); @@ -264,8 +258,8 @@ pub fn recursive_diff(dir1: impl AsRef, dir2: impl AsRef) { 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`, so not great for display. // Why not using String? Because there might be minified files or even potentially @@ -281,7 +275,7 @@ pub fn recursive_diff(dir1: impl AsRef, dir2: impl AsRef) { } pub fn read_dir(dir: impl AsRef, callback: F) { - for entry in fs::read_dir(dir).unwrap() { + for entry in fs_wrapper::read_dir(dir) { callback(&entry.unwrap().path()); } } -- cgit 1.4.1-3-g733a5