From 148e65419b93e4630f348b1fb4cbe5f57c03bf9f Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee <107522312+lavafroth@users.noreply.github.com> Date: Tue, 9 Jul 2024 08:05:51 +0530 Subject: [PATCH] feat: build script for controlling filesize cap --- Cargo.lock | 2 +- Cargo.toml | 3 ++- build.rs | 3 +++ src/lib.rs | 12 +++++++----- 4 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 build.rs diff --git a/Cargo.lock b/Cargo.lock index 6822df2..79b30bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -254,7 +254,7 @@ dependencies = [ [[package]] name = "sweet" -version = "0.1.0" +version = "0.2.0" dependencies = [ "anyhow", "bitflags 2.5.0", diff --git a/Cargo.toml b/Cargo.toml index bd89542..63cf878 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,9 @@ [package] name = "sweet" description = "simple wayland event encoding text" -version = "0.2.0" +version = "0.3.0" edition = "2021" +build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..40b89a0 --- /dev/null +++ b/build.rs @@ -0,0 +1,3 @@ +fn main() { + println!("cargo:rustc-env=FILESIZE_CAP_MIB=50"); +} diff --git a/src/lib.rs b/src/lib.rs index 43559f3..3d6e057 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -63,8 +63,8 @@ pub enum ConfigReadError { ReadingConfig(#[from] std::io::Error), #[error("path `{0}` supplied as config is not a regular file")] NotRegularFile(PathBuf), - #[error("the supplied config file {0} size exceeds the 50MiB limit")] - TooLarge(PathBuf), + #[error("the supplied config file {0} size exceeds the {1}MiB limit")] + TooLarge(PathBuf, u64), } pub fn read_config>(path: P) -> Result { @@ -74,9 +74,11 @@ pub fn read_config>(path: P) -> Result { return Err(ConfigReadError::NotRegularFile(path.to_path_buf())); } let size = stat.size(); - /* 50MiB size cap */ - if size > (50 << 20) { - return Err(ConfigReadError::TooLarge(path.to_path_buf())); + let mib_cap = std::option_env!("FILESIZE_CAP_MIB") + .and_then(|cap| cap.parse().ok()) + .unwrap_or(50); + if size > (mib_cap << 20) { + return Err(ConfigReadError::TooLarge(path.to_path_buf(), mib_cap)); } // TODO: Use mmap instead of fs::read_to_string Ok(fs::read_to_string(path)?)