Skip to content

Commit e36f6cd

Browse files
authored
Merge pull request #60 from Tcode-Motion/perf/remove-toctou-fs-checks-13392758860696189895
⚡ Bolt: optimize filesystem reads by removing TOCTOU checks
2 parents fe9c9d6 + 131ea87 commit e36f6cd

3 files changed

Lines changed: 54 additions & 68 deletions

File tree

‎cli/src/cache.rs‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,10 @@ impl BuildCache {
3434
/// Loads the cache database from the build directory.
3535
pub fn load(cache_dir: &Path) -> anyhow::Result<Self> {
3636
let db_path = cache_dir.join("fingerprints.json");
37-
let db = if db_path.exists() {
38-
if let Ok(content) = std::fs::read_to_string(&db_path) {
39-
serde_json::from_str(&content).unwrap_or_else(|_| CacheDatabase {
40-
fingerprints: HashMap::new(),
41-
})
42-
} else {
43-
CacheDatabase {
44-
fingerprints: HashMap::new(),
45-
}
46-
}
37+
let db = if let Ok(content) = std::fs::read_to_string(&db_path) {
38+
serde_json::from_str(&content).unwrap_or_else(|_| CacheDatabase {
39+
fingerprints: HashMap::new(),
40+
})
4741
} else {
4842
CacheDatabase {
4943
fingerprints: HashMap::new(),
@@ -98,8 +92,7 @@ impl BuildCache {
9892
.cache_dir
9993
.join("ir")
10094
.join(hash_path_filename(path) + ".ir");
101-
if cache_file.exists() {
102-
let data = std::fs::read(cache_file)?;
95+
if let Ok(data) = std::fs::read(cache_file) {
10396
let module = bincode::deserialize(&data)
10497
.map_err(|e| anyhow::anyhow!("Deserialization failed: {}", e))?;
10598
Ok(Some(module))
@@ -132,8 +125,7 @@ impl BuildCache {
132125
.cache_dir
133126
.join("bytecode")
134127
.join(hash_path_filename(path) + ".tsb");
135-
if cache_file.exists() {
136-
let data = std::fs::read(cache_file)?;
128+
if let Ok(data) = std::fs::read(cache_file) {
137129
let module = techscript_bytecode::BytecodeSerializer::deserialize(&data)
138130
.map_err(|e| anyhow::anyhow!("Bytecode deserialization failed: {}", e))?;
139131
Ok(Some(module))

‎cli/src/config.rs‎

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,22 @@ impl ConfigManager {
9999
// 2. Global user config (~/.techscript/config.toml)
100100
if let Some(home) = dirs::home_dir() {
101101
let global_path = home.join(".techscript").join("config.toml");
102-
if global_path.exists() {
103-
if let Ok(content) = std::fs::read_to_string(global_path) {
104-
if let Ok(toml_cfg) = toml::from_str::<TomlConfig>(&content) {
105-
apply_toml(&mut base, &toml_cfg);
106-
}
102+
if let Ok(content) = std::fs::read_to_string(global_path) {
103+
if let Ok(toml_cfg) = toml::from_str::<TomlConfig>(&content) {
104+
apply_toml(&mut base, &toml_cfg);
107105
}
108106
}
109107
}
110108

111109
// 3. Workspace config
112110
if let Some(ws) = workspace_root {
113111
let ws_manifest = ws.join("tech.toml");
114-
if ws_manifest.exists() {
115-
if let Ok(content) = std::fs::read_to_string(ws_manifest) {
116-
if let Ok(manifest_toml) = toml::from_str::<serde_json::Value>(&content) {
117-
if let Some(cfg_val) = manifest_toml.get("config") {
118-
if let Ok(toml_cfg) =
119-
serde_json::from_value::<TomlConfig>(cfg_val.clone())
120-
{
121-
apply_toml(&mut base, &toml_cfg);
122-
}
112+
if let Ok(content) = std::fs::read_to_string(ws_manifest) {
113+
if let Ok(manifest_toml) = toml::from_str::<serde_json::Value>(&content) {
114+
if let Some(cfg_val) = manifest_toml.get("config") {
115+
if let Ok(toml_cfg) = serde_json::from_value::<TomlConfig>(cfg_val.clone())
116+
{
117+
apply_toml(&mut base, &toml_cfg);
123118
}
124119
}
125120
}
@@ -129,15 +124,12 @@ impl ConfigManager {
129124
// 4. Project config
130125
if let Some(proj) = project_root {
131126
let proj_manifest = proj.join("tech.toml");
132-
if proj_manifest.exists() {
133-
if let Ok(content) = std::fs::read_to_string(proj_manifest) {
134-
if let Ok(manifest_toml) = toml::from_str::<serde_json::Value>(&content) {
135-
if let Some(cfg_val) = manifest_toml.get("config") {
136-
if let Ok(toml_cfg) =
137-
serde_json::from_value::<TomlConfig>(cfg_val.clone())
138-
{
139-
apply_toml(&mut base, &toml_cfg);
140-
}
127+
if let Ok(content) = std::fs::read_to_string(proj_manifest) {
128+
if let Ok(manifest_toml) = toml::from_str::<serde_json::Value>(&content) {
129+
if let Some(cfg_val) = manifest_toml.get("config") {
130+
if let Ok(toml_cfg) = serde_json::from_value::<TomlConfig>(cfg_val.clone())
131+
{
132+
apply_toml(&mut base, &toml_cfg);
141133
}
142134
}
143135
}

‎cli/src/project.rs‎

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,34 @@ impl ProjectBuildGraph {
7575
let mut packages = HashMap::new();
7676
let mut members = Vec::new();
7777

78-
if manifest_path.exists() {
79-
let content = std::fs::read_to_string(&manifest_path)?;
80-
let manifest: Manifest = toml::from_str(&content)?;
81-
82-
if let Some(workspace_cfg) = &manifest.workspace {
83-
// Workspace mode
84-
members = workspace_cfg.members.clone();
85-
for member_glob in &members {
86-
// Simulating directory lookup for member folders
87-
let member_dir = root.join(member_glob);
88-
if member_dir.exists() {
89-
let pkg = load_package(&member_dir)?;
90-
packages.insert(pkg.name.clone(), pkg);
78+
match std::fs::read_to_string(&manifest_path) {
79+
Ok(content) => {
80+
let manifest: Manifest = toml::from_str(&content)?;
81+
82+
if let Some(workspace_cfg) = &manifest.workspace {
83+
// Workspace mode
84+
members = workspace_cfg.members.clone();
85+
for member_glob in &members {
86+
// Simulating directory lookup for member folders
87+
let member_dir = root.join(member_glob);
88+
if member_dir.exists() {
89+
let pkg = load_package(&member_dir)?;
90+
packages.insert(pkg.name.clone(), pkg);
91+
}
9192
}
93+
} else {
94+
// Single package mode
95+
let pkg = load_package(root)?;
96+
packages.insert(pkg.name.clone(), pkg);
9297
}
93-
} else {
94-
// Single package mode
95-
let pkg = load_package(root)?;
96-
packages.insert(pkg.name.clone(), pkg);
9798
}
98-
} else {
99-
// No tech.toml, single file virtual package mode
99+
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
100+
// No tech.toml, single file virtual package mode
101+
}
102+
Err(e) => return Err(e.into()),
103+
}
104+
105+
if packages.is_empty() {
100106
let entry = root.to_path_buf();
101107
let parent = entry.parent().unwrap_or(root).to_path_buf();
102108
let pkg = Package {
@@ -148,12 +154,10 @@ impl ProjectBuildGraph {
148154

149155
// 1. Add all entry points to compilation list
150156
for pkg in self.workspace.packages.values_mut() {
151-
if pkg.entry_file.exists() {
152-
if let Ok(source) = std::fs::read_to_string(&pkg.entry_file) {
153-
let fid = source_mgr.add_file(pkg.entry_file.clone(), source.clone());
154-
pkg.entry_file_id = Some(fid);
155-
to_resolve.push((fid, pkg.entry_file.clone(), pkg.name.clone()));
156-
}
157+
if let Ok(source) = std::fs::read_to_string(&pkg.entry_file) {
158+
let fid = source_mgr.add_file(pkg.entry_file.clone(), source.clone());
159+
pkg.entry_file_id = Some(fid);
160+
to_resolve.push((fid, pkg.entry_file.clone(), pkg.name.clone()));
157161
}
158162
}
159163

@@ -269,12 +273,10 @@ impl ProjectBuildGraph {
269273
}
270274
}
271275

272-
if import_file.exists() {
273-
if let Ok(source) = std::fs::read_to_string(&import_file) {
274-
let dep_fid = source_mgr.add_file(import_file.clone(), source);
275-
dep_fids.push(dep_fid);
276-
to_resolve.push((dep_fid, import_file, pkg_name.clone()));
277-
}
276+
if let Ok(source) = std::fs::read_to_string(&import_file) {
277+
let dep_fid = source_mgr.add_file(import_file.clone(), source);
278+
dep_fids.push(dep_fid);
279+
to_resolve.push((dep_fid, import_file, pkg_name.clone()));
278280
}
279281
}
280282

0 commit comments

Comments
 (0)