Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more fs cheatcodes #4803

Merged
merged 8 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 26 additions & 16 deletions common/src/errors/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,75 @@ use std::{
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum FsPathError {
/// Provides additional path context for `std::fs::write`.
/// Provides additional path context for [`std::fs::write`].
#[error("failed to write to {path:?}: {source}")]
Write { source: io::Error, path: PathBuf },
/// Provides additional path context for `std::fs::read`.
/// Provides additional path context for [`std::fs::read`].
#[error("failed to read from {path:?}: {source}")]
Read { source: io::Error, path: PathBuf },
/// Provides additional path context for `std::fs::File::create`.
/// Provides additional path context for [`std::fs::read_link`].
#[error("failed to read from {path:?}: {source}")]
ReadLink { source: io::Error, path: PathBuf },
/// Provides additional path context for [`File::create`].
#[error("failed to create file {path:?}: {source}")]
CreateFile { source: io::Error, path: PathBuf },
/// Provides additional path context for `std::fs::remove_file`.
/// Provides additional path context for [`std::fs::remove_file`].
#[error("failed to remove file {path:?}: {source}")]
RemoveFile { source: io::Error, path: PathBuf },
/// Provides additional path context for `std::fs::create_dir`.
/// Provides additional path context for [`std::fs::create_dir`].
#[error("failed to create dir {path:?}: {source}")]
CreateDir { source: io::Error, path: PathBuf },
/// Provides additional path context for `std::fs::write_dir`.
/// Provides additional path context for [`std::fs::remove_dir`].
#[error("failed to remove dir {path:?}: {source}")]
RemoveDir { source: io::Error, path: PathBuf },
/// Provides additional path context for `std::fs::open`.
/// Provides additional path context for [`std::fs::File::open`].
#[error("failed to open file {path:?}: {source}")]
Open { source: io::Error, path: PathBuf },
/// Provides additional path context for the file whose contents should be parsed as json
/// Provides additional path context for the file whose contents should be parsed as JSON.
#[error("failed to parse json file: {path:?}: {source}")]
ReadJson { source: serde_json::Error, path: PathBuf },
/// Provides additional path context for the new json file
/// Provides additional path context for the new JSON file.
#[error("failed to write to json file: {path:?}: {source}")]
WriteJson { source: serde_json::Error, path: PathBuf },
}

impl FsPathError {
/// Returns the complementary error variant for `std::fs::write`.
/// Returns the complementary error variant for [`std::fs::write`].
pub fn write(source: io::Error, path: impl Into<PathBuf>) -> Self {
FsPathError::Write { source, path: path.into() }
}

/// Returns the complementary error variant for `std::fs::read`.
/// Returns the complementary error variant for [`std::fs::read`].
pub fn read(source: io::Error, path: impl Into<PathBuf>) -> Self {
FsPathError::Read { source, path: path.into() }
}

/// Returns the complementary error variant for `std::fs::File::create`.
/// Returns the complementary error variant for [`std::fs::read_link`].
pub fn read_link(source: io::Error, path: impl Into<PathBuf>) -> Self {
FsPathError::ReadLink { source, path: path.into() }
}

/// Returns the complementary error variant for [`File::create`].
pub fn create_file(source: io::Error, path: impl Into<PathBuf>) -> Self {
FsPathError::CreateFile { source, path: path.into() }
}

/// Returns the complementary error variant for `std::fs::remove_file`.
/// Returns the complementary error variant for [`std::fs::remove_file`].
pub fn remove_file(source: io::Error, path: impl Into<PathBuf>) -> Self {
FsPathError::RemoveFile { source, path: path.into() }
}

/// Returns the complementary error variant for `std::fs::create_dir`.
/// Returns the complementary error variant for [`std::fs::create_dir`].
pub fn create_dir(source: io::Error, path: impl Into<PathBuf>) -> Self {
FsPathError::CreateDir { source, path: path.into() }
}

/// Returns the complementary error variant for `std::fs::remove_dir`.
/// Returns the complementary error variant for [`std::fs::remove_dir`].
pub fn remove_dir(source: io::Error, path: impl Into<PathBuf>) -> Self {
FsPathError::RemoveDir { source, path: path.into() }
}

/// Returns the complementary error variant for `std::fs::File::open`.
/// Returns the complementary error variant for [`File::open`].
pub fn open(source: io::Error, path: impl Into<PathBuf>) -> Self {
FsPathError::Open { source, path: path.into() }
}
Expand All @@ -79,6 +87,7 @@ impl AsRef<Path> for FsPathError {
match self {
FsPathError::Write { path, .. } => path,
FsPathError::Read { path, .. } => path,
FsPathError::ReadLink { path, .. } => path,
FsPathError::CreateDir { path, .. } => path,
FsPathError::RemoveDir { path, .. } => path,
FsPathError::CreateFile { path, .. } => path,
Expand All @@ -95,6 +104,7 @@ impl From<FsPathError> for io::Error {
match err {
FsPathError::Write { source, .. } => source,
FsPathError::Read { source, .. } => source,
FsPathError::ReadLink { source, .. } => source,
FsPathError::CreateDir { source, .. } => source,
FsPathError::RemoveDir { source, .. } => source,
FsPathError::CreateFile { source, .. } => source,
Expand Down
29 changes: 18 additions & 11 deletions common/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,51 @@
use crate::errors::FsPathError;
use serde::{de::DeserializeOwned, Serialize};
use std::{
fs,
fs::{self, File},
path::{Component, Path, PathBuf},
};

type Result<T> = std::result::Result<T, FsPathError>;

/// Wrapper for `std::fs::File::create`
/// Wrapper for [`File::create`].
pub fn create_file(path: impl AsRef<Path>) -> Result<fs::File> {
let path = path.as_ref();
fs::File::create(path).map_err(|err| FsPathError::create_file(err, path))
File::create(path).map_err(|err| FsPathError::create_file(err, path))
}
/// Wrapper for `std::fs::remove_file`
/// Wrapper for [`std::fs::remove_file`].
pub fn remove_file(path: impl AsRef<Path>) -> Result<()> {
let path = path.as_ref();
fs::remove_file(path).map_err(|err| FsPathError::remove_file(err, path))
}

/// Wrapper for `std::fs::read`
/// Wrapper for [`std::fs::read`].
pub fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> {
let path = path.as_ref();
fs::read(path).map_err(|err| FsPathError::read(err, path))
}

/// Wrapper for `std::fs::read_to_string`
/// Wrapper for [`std::fs::read_link`].
pub fn read_link(path: impl AsRef<Path>) -> Result<PathBuf> {
let path = path.as_ref();
fs::read_link(path).map_err(|err| FsPathError::read_link(err, path))
}

/// Wrapper for [`std::fs::read_to_string`].
pub fn read_to_string(path: impl AsRef<Path>) -> Result<String> {
let path = path.as_ref();
fs::read_to_string(path).map_err(|err| FsPathError::read(err, path))
}

/// Reads the json file and deserialize it into the provided type
/// Reads the JSON file and deserialize it into the provided type.
pub fn read_json_file<T: DeserializeOwned>(path: &Path) -> Result<T> {
let file = open(path)?;
let file = std::io::BufReader::new(file);
serde_json::from_reader(file)
// read the file into a byte array first
// https://github.com/serde-rs/json/issues/160
let bytes = read(path)?;
serde_json::from_slice(&bytes)
.map_err(|source| FsPathError::ReadJson { source, path: path.to_path_buf() })
}

/// Writes the object as a json object
/// Writes the object as a JSON object.
pub fn write_json_file<T: Serialize>(path: &Path, obj: &T) -> Result<()> {
let file = create_file(path)?;
let file = std::io::BufWriter::new(file);
Expand Down
1 change: 1 addition & 0 deletions evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ url = "2.3.1"
auto_impl = "1.0.1"
itertools = "0.10.5"
ordered-float = "3.6.0"
walkdir = "2.3.3"

# Coverage
semver = "1.0.17"
Expand Down
7 changes: 7 additions & 0 deletions evm/src/executor/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ abigen!(
"[
struct Log { bytes32[] topics; bytes data; }
struct Rpc { string name; string url; }
struct DirEntry { string error; string path; uint64 depth; bool isDir; bool isSymlink; }
struct FsMetadata { bool isDir; bool isSymlink; uint256 length; bool readOnly; uint256 modified; uint256 accessed; uint256 created; }

allowCheatcodes(address)
Expand Down Expand Up @@ -134,6 +135,12 @@ abigen!(
writeLine(string,string)
closeFile(string)
removeFile(string)
createDir(string, bool)
removeDir(string, bool)
readDir(string)(DirEntry[])
readDir(string, uint64)(DirEntry[])
readDir(string, uint64, bool)(DirEntry[])
readLink(string)(string)
Comment on lines +143 to +148
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very useful imo

fsMetadata(string)(FsMetadata)

toString(bytes)
Expand Down
2 changes: 1 addition & 1 deletion evm/src/executor/inspector/cheatcodes/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl CheatsConfig {

/// Returns an error if no access is granted to access `path`, See also [Self::is_path_allowed]
///
/// Returns the normalized version of `path`, see [`Self::normalized_path`]
/// Returns the normalized version of `path`, see [`CheatsConfig::normalized_path`]
pub fn ensure_path_allowed(
&self,
path: impl AsRef<Path>,
Expand Down
Loading