Add or update a virtual file with the given content
(&self, path: PathBuf, content: String)
| 359 | |
| 360 | /// Add or update a virtual file with the given content |
| 361 | pub fn upsert_file(&self, path: PathBuf, content: String) { |
| 362 | self.has_virtual_files.store(true, Ordering::Relaxed); |
| 363 | let spelled = normalize_path_structural(&path).into_owned(); |
| 364 | let normalized = normalize_path(&spelled).into_owned(); |
| 365 | let mut files = self.virtual_files.write().unwrap(); |
| 366 | |
| 367 | // Ensure parent directories exist as virtual dirs |
| 368 | for dir in spelled.ancestors().skip(1) { |
| 369 | let key = normalize_path(dir).into_owned(); |
| 370 | files.entry(key).or_insert_with(|| VirtualFile { |
| 371 | content: "".into(), |
| 372 | is_dir: true, |
| 373 | path: dir.to_path_buf(), |
| 374 | }); |
| 375 | } |
| 376 | |
| 377 | // Add the file itself |
| 378 | let old_file = files.insert( |
| 379 | normalized, |
| 380 | VirtualFile { |
| 381 | content: content.into(), |
| 382 | is_dir: false, |
| 383 | path: spelled, |
| 384 | }, |
| 385 | ); |
| 386 | |
| 387 | // Only invalidate the cache if a new file was actually added. |
| 388 | // Modifying an existing file does not change its allowed status. |
| 389 | if old_file.is_none() { |
| 390 | self.is_allowed_cache.write().unwrap().clear(); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /// Check if a path is in the virtual filesystem |
| 395 | #[cfg(test)] |