1use std::path::{Path, PathBuf};
19
20use chrono::{DateTime, Utc};
21
22use crate::history::{RegistryEntry, ScanRegistry};
23
24#[must_use]
31pub fn workspace_root() -> PathBuf {
32 if let Ok(root) = std::env::var("OXIDE_SLOC_ROOT") {
33 let p = PathBuf::from(root);
34 if p.is_dir() {
35 return p;
36 }
37 }
38 std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))
39}
40
41#[must_use]
47pub fn resolve_output_root(raw: Option<&str>) -> PathBuf {
48 let value = raw.unwrap_or("out/web").trim();
49 let path = if value.is_empty() {
50 PathBuf::from("out/web")
51 } else {
52 PathBuf::from(value)
53 };
54 if path.is_absolute() {
55 path
56 } else {
57 workspace_root().join(path)
58 }
59}
60
61#[must_use]
64pub fn resolve_registry_path(output_root: &Path) -> PathBuf {
65 std::env::var("SLOC_REGISTRY_PATH")
66 .map_or_else(|_| output_root.join("registry.json"), PathBuf::from)
67}
68
69#[must_use]
75pub fn dir_size_bytes(path: &Path) -> u64 {
76 fn walk(path: &Path, acc: &mut u64) {
77 let Ok(entries) = std::fs::read_dir(path) else {
78 return;
79 };
80 for entry in entries.flatten() {
81 let Ok(ft) = entry.file_type() else { continue };
82 if ft.is_dir() {
83 walk(&entry.path(), acc);
84 } else if let Ok(meta) = entry.metadata() {
85 *acc += meta.len();
86 }
87 }
88 }
89 let mut total = 0;
90 if path.is_file() {
91 return std::fs::metadata(path).map_or(0, |m| m.len());
92 }
93 walk(path, &mut total);
94 total
95}
96
97#[must_use]
103pub fn run_output_dir(entry: &RegistryEntry) -> Option<PathBuf> {
104 let p = entry
105 .html_path
106 .as_ref()
107 .or(entry.json_path.as_ref())
108 .or(entry.pdf_path.as_ref())
109 .or(entry.csv_path.as_ref())
110 .or(entry.xlsx_path.as_ref())?;
111 let parent = p.parent()?;
112 let parent_name = parent.file_name().and_then(|n| n.to_str()).unwrap_or("");
113 if matches!(parent_name, "html" | "json" | "pdf" | "excel") {
114 parent.parent().map(PathBuf::from)
115 } else {
116 Some(parent.to_path_buf())
117 }
118}
119
120#[derive(Debug, Clone)]
124pub struct PrunedRun {
125 pub run_id: String,
126 pub project_label: String,
127 pub timestamp_utc: DateTime<Utc>,
128 pub output_dir: Option<PathBuf>,
129 pub bytes: u64,
130}
131
132#[derive(Debug, Clone, Default)]
134pub struct PrunePlan {
135 pub runs: Vec<PrunedRun>,
136 pub total_bytes: u64,
137}
138
139impl PrunePlan {
140 #[must_use]
141 pub const fn is_empty(&self) -> bool {
142 self.runs.is_empty()
143 }
144}
145
146#[must_use]
155pub fn plan_run_prune(
156 reg: &ScanRegistry,
157 older_than_days: Option<u32>,
158 keep_last: Option<u32>,
159) -> PrunePlan {
160 use std::collections::HashSet;
161
162 let mut ordered: Vec<&RegistryEntry> = reg.entries.iter().collect();
165 ordered.sort_by_key(|e| std::cmp::Reverse(e.timestamp_utc));
166
167 let mut selected: HashSet<&str> = HashSet::new();
168 if let Some(days) = older_than_days {
169 let cutoff = Utc::now() - chrono::Duration::days(i64::from(days));
170 for e in &ordered {
171 if e.timestamp_utc < cutoff {
172 selected.insert(e.run_id.as_str());
173 }
174 }
175 }
176 if let Some(keep) = keep_last {
177 for e in ordered.iter().skip(keep as usize) {
178 selected.insert(e.run_id.as_str());
179 }
180 }
181
182 let mut runs = Vec::new();
183 let mut total_bytes = 0u64;
184 for e in &ordered {
185 if !selected.contains(e.run_id.as_str()) {
186 continue;
187 }
188 let output_dir = run_output_dir(e);
189 let bytes = output_dir.as_deref().map_or(0, dir_size_bytes);
190 total_bytes += bytes;
191 runs.push(PrunedRun {
192 run_id: e.run_id.clone(),
193 project_label: e.project_label.clone(),
194 timestamp_utc: e.timestamp_utc,
195 output_dir,
196 bytes,
197 });
198 }
199 PrunePlan { runs, total_bytes }
200}
201
202#[derive(Debug, Clone, Default)]
204pub struct PruneReport {
205 pub deleted_runs: usize,
206 pub bytes_freed: u64,
207 pub failures: Vec<(String, String)>,
209}
210
211#[must_use]
217pub fn execute_run_prune(reg: &mut ScanRegistry, plan: &PrunePlan) -> PruneReport {
218 use std::collections::HashSet;
219
220 let mut report = PruneReport::default();
221 let mut removed_ids: HashSet<String> = HashSet::new();
222
223 for run in &plan.runs {
224 if let Some(dir) = &run.output_dir {
225 if dir.exists() {
226 if let Err(e) = std::fs::remove_dir_all(dir) {
227 if e.kind() != std::io::ErrorKind::NotFound {
228 report
229 .failures
230 .push((dir.display().to_string(), e.to_string()));
231 continue;
232 }
233 }
234 }
235 }
236 report.bytes_freed += run.bytes;
237 report.deleted_runs += 1;
238 removed_ids.insert(run.run_id.clone());
239 }
240
241 reg.entries.retain(|e| !removed_ids.contains(&e.run_id));
242 report
243}
244
245pub fn rotate_log(path: &Path, max_bytes: u64, keep: u32) -> anyhow::Result<bool> {
260 let Ok(meta) = std::fs::metadata(path) else {
261 return Ok(false); };
263 if meta.len() <= max_bytes {
264 return Ok(false);
265 }
266
267 if keep == 0 {
268 std::fs::write(path, b"")?;
270 return Ok(true);
271 }
272
273 let gen_path = |n: u32| -> PathBuf {
275 let mut s = path.as_os_str().to_owned();
276 s.push(format!(".{n}"));
277 PathBuf::from(s)
278 };
279 let oldest = gen_path(keep);
280 if oldest.exists() {
281 std::fs::remove_file(&oldest)?;
282 }
283 for n in (1..keep).rev() {
284 let from = gen_path(n);
285 if from.exists() {
286 std::fs::rename(&from, gen_path(n + 1))?;
287 }
288 }
289 std::fs::rename(path, gen_path(1))?;
290 Ok(true)
291}
292
293#[must_use]
296pub fn rotated_log_paths(path: &Path) -> Vec<PathBuf> {
297 let mut out = Vec::new();
298 let mut n = 1u32;
299 loop {
300 let mut s = path.as_os_str().to_owned();
301 s.push(format!(".{n}"));
302 let p = PathBuf::from(s);
303 if p.exists() {
304 out.push(p);
305 n += 1;
306 } else {
307 break;
308 }
309 }
310 out
311}
312
313#[cfg(test)]
314mod tests {
315 use super::*;
316 use crate::history::{RegistryEntry, ScanRegistry, ScanSummarySnapshot};
317 use std::sync::{Mutex, MutexGuard, OnceLock};
318
319 fn env_lock() -> MutexGuard<'static, ()> {
323 static ENV_MUTEX: OnceLock<Mutex<()>> = OnceLock::new();
324 ENV_MUTEX
325 .get_or_init(|| Mutex::new(()))
326 .lock()
327 .unwrap_or_else(std::sync::PoisonError::into_inner)
328 }
329
330 fn entry(run_id: &str, age_days: i64, root: &Path) -> RegistryEntry {
331 let dir = root.join(run_id);
332 std::fs::create_dir_all(dir.join("json")).unwrap();
333 std::fs::write(dir.join("json").join("result.json"), b"{}").unwrap();
334 RegistryEntry {
335 run_id: run_id.to_owned(),
336 timestamp_utc: Utc::now() - chrono::Duration::days(age_days),
337 project_label: format!("proj-{run_id}"),
338 input_roots: vec![],
339 json_path: Some(dir.join("json").join("result.json")),
340 html_path: None,
341 pdf_path: None,
342 csv_path: None,
343 xlsx_path: None,
344 summary: ScanSummarySnapshot::default(),
345 git_branch: None,
346 git_commit: None,
347 git_commit_long: None,
348 git_author: None,
349 git_tags: None,
350 git_nearest_tag: None,
351 git_commit_date: None,
352 }
353 }
354
355 fn tmp() -> PathBuf {
356 let d = std::env::temp_dir().join(format!("sloc_maint_{}", uuid::Uuid::new_v4()));
357 std::fs::create_dir_all(&d).unwrap();
358 d
359 }
360
361 #[test]
362 fn run_output_dir_handles_nested_layout() {
363 let root = tmp();
364 let e = entry("abc", 0, &root);
365 assert_eq!(run_output_dir(&e), Some(root.join("abc")));
367 std::fs::remove_dir_all(&root).ok();
368 }
369
370 #[test]
371 fn plan_selects_by_age() {
372 let root = tmp();
373 let mut reg = ScanRegistry::default();
374 reg.entries.push(entry("old", 40, &root));
375 reg.entries.push(entry("new", 1, &root));
376
377 let plan = plan_run_prune(®, Some(30), None);
378 assert_eq!(plan.runs.len(), 1);
379 assert_eq!(plan.runs[0].run_id, "old");
380 std::fs::remove_dir_all(&root).ok();
381 }
382
383 #[test]
384 fn plan_selects_by_keep_last() {
385 let root = tmp();
386 let mut reg = ScanRegistry::default();
387 for (i, id) in ["a", "b", "c", "d"].iter().enumerate() {
388 reg.entries
389 .push(entry(id, i64::try_from(i).unwrap(), &root)); }
391 let plan = plan_run_prune(®, None, Some(2));
392 let ids: Vec<_> = plan.runs.iter().map(|r| r.run_id.clone()).collect();
393 assert_eq!(plan.runs.len(), 2, "keep 2, delete the 2 oldest");
394 assert!(ids.contains(&"c".to_owned()) && ids.contains(&"d".to_owned()));
395 std::fs::remove_dir_all(&root).ok();
396 }
397
398 #[test]
399 fn execute_removes_dirs_and_entries() {
400 let root = tmp();
401 let mut reg = ScanRegistry::default();
402 reg.entries.push(entry("gone", 40, &root));
403 reg.entries.push(entry("kept", 1, &root));
404
405 let plan = plan_run_prune(®, Some(30), None);
406 let report = execute_run_prune(&mut reg, &plan);
407
408 assert_eq!(report.deleted_runs, 1);
409 assert!(report.failures.is_empty());
410 assert!(!root.join("gone").exists(), "artifacts removed");
411 assert!(root.join("kept").exists(), "recent run untouched");
412 assert_eq!(reg.entries.len(), 1);
413 assert_eq!(reg.entries[0].run_id, "kept");
414 std::fs::remove_dir_all(&root).ok();
415 }
416
417 #[test]
418 fn empty_plan_when_no_rules() {
419 let root = tmp();
420 let mut reg = ScanRegistry::default();
421 reg.entries.push(entry("x", 100, &root));
422 assert!(plan_run_prune(®, None, None).is_empty());
423 std::fs::remove_dir_all(&root).ok();
424 }
425
426 #[test]
427 fn rotate_log_shifts_generations() {
428 let root = tmp();
429 let log = root.join("audit.log");
430 std::fs::write(&log, vec![b'x'; 100]).unwrap();
431
432 assert!(!rotate_log(&log, 1000, 3).unwrap());
434 assert!(rotate_log(&log, 50, 3).unwrap());
436 assert!(log.with_extension("log.1").exists());
437 assert!(!log.exists());
438
439 std::fs::write(&log, vec![b'y'; 100]).unwrap();
441 assert!(rotate_log(&log, 50, 3).unwrap());
442 assert!(log.with_extension("log.1").exists());
443 assert!(log.with_extension("log.2").exists());
444 assert_eq!(rotated_log_paths(&log).len(), 2);
445 std::fs::remove_dir_all(&root).ok();
446 }
447
448 #[test]
449 fn rotate_log_keep_zero_truncates() {
450 let root = tmp();
451 let log = root.join("audit.log");
452 std::fs::write(&log, vec![b'x'; 100]).unwrap();
453 assert!(rotate_log(&log, 10, 0).unwrap());
454 assert!(log.exists());
455 assert_eq!(std::fs::metadata(&log).unwrap().len(), 0);
456 std::fs::remove_dir_all(&root).ok();
457 }
458
459 #[test]
460 fn workspace_root_prefers_env_dir_then_falls_back() {
461 let _guard = env_lock();
462 let dir = tmp();
463 std::env::set_var("OXIDE_SLOC_ROOT", &dir);
464 assert_eq!(workspace_root(), dir);
465 std::env::set_var("OXIDE_SLOC_ROOT", dir.join("does-not-exist"));
467 assert!(workspace_root().is_dir());
468 std::env::remove_var("OXIDE_SLOC_ROOT");
469 assert!(workspace_root().is_dir());
470 std::fs::remove_dir_all(&dir).ok();
471 }
472
473 #[test]
474 fn resolve_output_root_handles_absolute_relative_and_default() {
475 let _guard = env_lock();
476 let dir = tmp();
477 let abs = dir.join("art");
479 assert_eq!(resolve_output_root(Some(abs.to_str().unwrap())), abs);
480 std::env::set_var("OXIDE_SLOC_ROOT", &dir);
482 assert_eq!(resolve_output_root(Some(" ")), dir.join("out/web"));
483 assert_eq!(resolve_output_root(None), dir.join("out/web"));
484 assert_eq!(
486 resolve_output_root(Some("custom/out")),
487 dir.join("custom/out")
488 );
489 std::env::remove_var("OXIDE_SLOC_ROOT");
490 std::fs::remove_dir_all(&dir).ok();
491 }
492
493 #[test]
494 fn resolve_registry_path_honours_env_override() {
495 let _guard = env_lock();
496 let dir = tmp();
497 std::env::remove_var("SLOC_REGISTRY_PATH");
498 assert_eq!(resolve_registry_path(&dir), dir.join("registry.json"));
499 std::env::set_var("SLOC_REGISTRY_PATH", dir.join("shared.json"));
500 assert_eq!(resolve_registry_path(&dir), dir.join("shared.json"));
501 std::env::remove_var("SLOC_REGISTRY_PATH");
502 std::fs::remove_dir_all(&dir).ok();
503 }
504
505 #[test]
506 fn dir_size_bytes_counts_files_and_handles_single_file() {
507 let root = tmp();
508 std::fs::write(root.join("a.txt"), vec![b'x'; 10]).unwrap();
509 std::fs::create_dir_all(root.join("sub")).unwrap();
510 std::fs::write(root.join("sub").join("b.txt"), vec![b'y'; 5]).unwrap();
511 assert_eq!(dir_size_bytes(&root), 15);
512 assert_eq!(dir_size_bytes(&root.join("a.txt")), 10);
514 assert_eq!(dir_size_bytes(&root.join("nope")), 0);
516 std::fs::remove_dir_all(&root).ok();
517 }
518
519 #[test]
520 fn run_output_dir_handles_flat_layout_and_missing_paths() {
521 let root = tmp();
522 let mut e = entry("flat", 0, &root);
524 e.json_path = Some(root.join("flat").join("result.json"));
525 assert_eq!(run_output_dir(&e), Some(root.join("flat")));
526 e.json_path = None;
528 assert_eq!(run_output_dir(&e), None);
529 std::fs::remove_dir_all(&root).ok();
530 }
531
532 #[test]
533 fn execute_run_prune_records_failure_for_locked_dir() {
534 let root = tmp();
535 let mut reg = ScanRegistry::default();
536 reg.entries.push(entry("target", 40, &root));
537 let mut plan = plan_run_prune(®, Some(30), None);
538 let bogus = root.join("target").join("json").join("result.json");
542 plan.runs[0].output_dir = Some(bogus);
543 let report = execute_run_prune(&mut reg, &plan);
544 assert_eq!(report.deleted_runs, 0);
545 assert_eq!(report.failures.len(), 1);
546 assert!(reg.entries.iter().any(|e| e.run_id == "target"));
547 std::fs::remove_dir_all(&root).ok();
548 }
549
550 #[test]
551 fn rotate_log_drops_oldest_generation_at_keep_cap() {
552 let root = tmp();
553 let log = root.join("audit.log");
554 std::fs::write(&log, vec![b'x'; 100]).unwrap();
556 std::fs::write(log.with_extension("log.1"), b"g1").unwrap();
557 std::fs::write(log.with_extension("log.2"), b"g2").unwrap();
558 assert!(rotate_log(&log, 50, 2).unwrap());
560 assert!(log.with_extension("log.1").exists());
561 assert!(log.with_extension("log.2").exists());
562 assert!(!log.with_extension("log.3").exists());
563 std::fs::remove_dir_all(&root).ok();
564 }
565}