DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/Cache/AssetMemoryMap/
LoadOrInsert.rs1use std::{
7 path::{Path, PathBuf},
8 sync::Arc,
9};
10
11use memmap2::Mmap;
12
13use crate::{
14 Cache::AssetMemoryMap::{Entry, Map, MimeFromExtension},
15 dev_log,
16};
17
18pub fn Fn(Path:&Path) -> std::io::Result<Arc<Entry::Struct>> {
19 if let Some(Existing) = Map::Fn().get(Path) {
20 return Ok(Existing.clone());
21 }
22
23 let File = std::fs::File::open(Path)?;
24
25 let Length = File.metadata()?.len() as usize;
26
27 let Mapping = unsafe { Mmap::map(&File)? };
31
32 let BrotliPath = {
33 let mut B = Path.as_os_str().to_owned();
34
35 B.push(".br");
36
37 PathBuf::from(B)
38 };
39
40 let Brotli = std::fs::File::open(&BrotliPath)
41 .ok()
42 .and_then(|F| unsafe { Mmap::map(&F).ok() });
43
44 let Mime = MimeFromExtension::Fn(Path);
45
46 let MarkerEntry = Arc::new(Entry::Struct { Mapping, Mime, Length, Brotli });
47
48 dev_log!(
49 "asset-cache",
50 "mmap insert path={} bytes={} brotli={}",
51 Path.display(),
52 Length,
53 MarkerEntry.Brotli.is_some()
54 );
55
56 Map::Fn().insert(Path.to_path_buf(), MarkerEntry.clone());
57
58 Ok(MarkerEntry)
59}