agentmux_srv_core/backend/file_icons/
mod.rs1pub mod assoc;
15#[cfg(windows)]
16pub(crate) use assoc::registry_string;
17pub mod cache;
18pub mod child;
19pub mod pool;
20pub mod request;
21#[cfg(windows)]
22pub mod shell_windows;
23
24use std::collections::HashMap;
25use std::path::PathBuf;
26use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
27use std::sync::{Arc, Mutex, OnceLock};
28
29use request::IconRequest;
30
31pub const CHILD_ARG: &str = "__file_icons";
33
34#[derive(Clone, Copy, Debug, PartialEq, Eq)]
36pub enum IconError {
37 Unsupported,
39 NoIcon,
41 Timeout,
43 Busy,
45 Failed,
47}
48
49type Flight = Arc<tokio::sync::OnceCell<Result<Arc<Vec<u8>>, IconError>>>;
50
51struct Service {
52 pool: pool::Pool,
53 mem: Mutex<cache::MemCache>,
54 flights: Mutex<HashMap<String, Flight>>,
56 dir: PathBuf,
57 written: AtomicUsize,
59 trimming: AtomicBool,
61}
62
63fn service() -> &'static Service {
64 static SERVICE: OnceLock<Service> = OnceLock::new();
65 SERVICE.get_or_init(|| {
66 let dir = crate::backend::base::get_mux_data_dir().join("cache").join("file-icons").join("v1");
67 let prune_dir = dir.clone();
68 std::thread::spawn(move || cache::prune(&prune_dir, cache::DISK_CAP_BYTES));
69 Service {
70 pool: pool::Pool::new(),
71 mem: Mutex::default(),
72 flights: Mutex::default(),
73 dir,
74 written: AtomicUsize::new(0),
75 trimming: AtomicBool::new(false),
76 }
77 })
78}
79
80impl Service {
81 fn note_written(&'static self) {
84 if self.written.fetch_add(1, Ordering::Relaxed) % TRIM_EVERY != TRIM_EVERY - 1 {
85 return;
86 }
87 if self.trimming.swap(true, Ordering::Acquire) {
88 return;
89 }
90 tokio::task::spawn_blocking(move || {
91 cache::prune(&self.dir, cache::DISK_CAP_BYTES);
92 self.trimming.store(false, Ordering::Release);
93 });
94 }
95}
96
97const TRIM_EVERY: usize = 32;
99
100pub async fn icon_png(req: &IconRequest) -> Result<Arc<Vec<u8>>, IconError> {
102 if !cfg!(windows) {
103 return Err(IconError::Unsupported);
104 }
105 let svc = service();
106 let assoc = assoc::fingerprint(req);
107 let key = cache::key(req, &assoc);
108 if let Some(hit) = svc.mem.lock().unwrap_or_else(|e| e.into_inner()).get(&key) {
109 return Ok(hit);
110 }
111 let flight = svc.flights.lock().unwrap_or_else(|e| e.into_inner()).entry(key.clone()).or_default().clone();
112 let made = flight
113 .get_or_init(|| async {
114 let path = cache::disk_path(&svc.dir, req, &assoc);
115 let png = match tokio::fs::read(&path).await {
116 Ok(bytes) if !bytes.is_empty() => Arc::new(bytes),
117 _ => {
118 let png = Arc::new(svc.pool.render(req).await?);
119 let bytes = png.clone();
120 tokio::spawn(async move {
121 if let Some(parent) = path.parent() {
122 let _ = tokio::fs::create_dir_all(parent).await;
123 }
124 let tmp = path.with_extension("tmp");
125 if tokio::fs::write(&tmp, bytes.as_slice()).await.is_ok() {
126 let _ = tokio::fs::rename(&tmp, &path).await;
127 svc.note_written();
128 }
129 });
130 png
131 }
132 };
133 svc.mem.lock().unwrap_or_else(|e| e.into_inner()).put(key.clone(), png.clone(), cache::MEM_CAP_BYTES);
134 Ok(png)
135 })
136 .await
137 .clone();
138 svc.flights.lock().unwrap_or_else(|e| e.into_inner()).remove(&key);
139 made
140}