code wiki / _hdl_build / nx_janitor_scratch.nx

nx_janitor_scratch.nx source

↩ module page · 52 lines · 3256 B

1// nx_janitor_scratch.nx -- the "scratch-accumulation" part of the janitor's X-JAN-002 sprawl half (the third 2// and last part: "dead-organ" = nx_janitor_sprawl, "dup" = nx_janitor_dupname, "scratch-accumulation" = HERE). 3// Build/test scratch piles up unbounded -- every nx_sov_build_run leaves /tmp/*.s + /tmp/*.elf, test organs 4// leave /tmp/*.txt, and _scratch/ / _quarantine/ accumulate -- and nothing measures it, so it silently grows. 5// This MEASURES scratch accumulation across the known scratch zones (a count per zone + a total) so the operator 6// can SEE the footprint. READ-ONLY (never-brick): it counts + reports; reaping (delete) needs unlink the janitor 7// deliberately does not have, so reaping stays a separate operator-gated step. Sovereign. license_tier: ORIGINAL 8import "nx_syscalls.nx" 9const K_MAGIC_131072: i64 = 131072 10 11func jsc_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 12func jsc_starts(name: *u8, pfx: *u8) -> i64 { if pfx[0]==(0 as u8) { return 1 } var i: i64=0; while pfx[i]!=(0 as u8){ if name[i]!=pfx[i] { return 0 } i=i+1 } return 1 } 13func jsc_ends(name: *u8, sfx: *u8) -> i64 { let sl: i64=jsc_slen(sfx); if sl==0 { return 1 } let nl: i64=jsc_slen(name); if sl>nl { return 0 } var i: i64=0; while i<sl { if name[nl-sl+i]!=sfx[i] { return 0 } i=i+1 } return 1 } 14func jsc_is_dot(name: *u8) -> i64 { if name[0]==(46 as u8) { if name[1]==(0 as u8) { return 1 } if name[1]==(46 as u8) { if name[2]==(0 as u8) { return 1 } } } return 0 } 15 16// count NON-directory entries in `dir` whose name starts-with pfx (or pfx="") AND ends-with sfx (or sfx=""). 17// returns -1 if the zone doesn't exist (so a missing zone reads as "absent", not "0 accumulation"). 18func jsc_count(dir: *u8, pfx: *u8, sfx: *u8) -> i64 { 19 let fd: i64 = sys_openat_rd(dir); if fd < 0 { return 0 - 1 } 20 let dbuf: *u8 = sys_mmap(K_MAGIC_131072) 21 var n: i64 = 0; var go: i64 = 1 22 while go == 1 { 23 let nb: i64 = sys_getdents64(fd, dbuf, K_MAGIC_131072) 24 if nb <= 0 { go = 0 } else { 25 var off: i64 = 0 26 while off < nb { 27 let rec: *u8 = (dbuf as i64 + off) as *u8 28 let rl: i64 = dirent_reclen(rec) 29 if rl <= 0 { off = nb } else { 30 let nm: *u8 = dirent_name(rec) 31 let dt: i64 = dirent_type(rec) 32 if jsc_is_dot(nm) == 0 { if dt != 4 { // skip "." ".." and subdirs 33 if jsc_starts(nm, pfx) == 1 { if jsc_ends(nm, sfx) == 1 { n = n + 1 } } 34 } } 35 off = off + rl 36 } 37 } 38 } 39 } 40 sys_close(fd) 41 return n 42} 43 44// total scratch accumulation across the standard zones (a missing zone contributes 0). 45func jsc_total() -> i64 { 46 var t: i64 = 0 47 let a: i64 = jsc_count("/tmp\x00" as *u8, "\x00" as *u8, ".elf\x00" as *u8); if a > 0 { t = t + a } 48 let b: i64 = jsc_count("/tmp\x00" as *u8, "\x00" as *u8, ".s\x00" as *u8); if b > 0 { t = t + b } 49 let c: i64 = jsc_count("_scratch\x00" as *u8, "\x00" as *u8, "\x00" as *u8); if c > 0 { t = t + c } 50 let d: i64 = jsc_count("knowledge/_quarantine/registry\x00" as *u8, "\x00" as *u8, "\x00" as *u8); if d > 0 { t = t + d } 51 return t 52}