Skip to content

Commit

Permalink
feat: build script for controlling filesize cap
Browse files Browse the repository at this point in the history
  • Loading branch information
lavafroth committed Jul 9, 2024
1 parent 0176437 commit 148e654
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 3 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("cargo:rustc-env=FILESIZE_CAP_MIB=50");
}
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<Path>>(path: P) -> Result<String, ConfigReadError> {
Expand All @@ -74,9 +74,11 @@ pub fn read_config<P: AsRef<Path>>(path: P) -> Result<String, ConfigReadError> {
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)?)
Expand Down

0 comments on commit 148e654

Please sign in to comment.