code wiki / _hdl_build / nx_janitor_sweep.nx
nx_janitor_sweep.nx source
↩ module page · 71 lines · 4146 B
1// nx_janitor_sweep.nx -- the JANITOR's AT-SCALE sweep: migrate a whole debt-heavy directory's non-sov FORMAT
2// files into the sovereign seg_store in one pass (operator: "keep going and we want to keep the debt gone").
3// Honest mechanic: jan_migrate is ADDITIVE (original left intact = reversible/never-brick), so a sweep makes
4// every format file RETIRE-READY (its bytes byte-verified in a store) -- the actual count-drop is the RETIRE
5// step, which stays OPERATOR-GATED because some of these are human-edited sources (e.g. the RACI is "edit here").
6// The sweep is BOUNDED to one directory (not the 400k-file tree the auditor walks) so it is cheap + safe.
7// Composes nx_janitor_scrub.jan_migrate. Sovereign. license_tier: ORIGINAL
8import "nx_janitor_scrub.nx" // jan_migrate, jan_exists
9import "nx_syscalls.nx"
10const K_MAGIC_131072: i64 = 131072
11const K_MAGIC_1024: i64 = 1024
12
13func jsw_lc(c: i64) -> i64 { if c>=65 { if c<=90 { return c+32 } } return c }
14// is name's extension a migratable non-sovereign FORMAT (tsv/conf/toml/json)? (the auditor's NONSOV_FORMAT class)
15func jsw_is_format(name: *u8) -> i64 {
16 var dot: i64=0-1; var i: i64=0
17 while name[i]!=(0 as u8) { if name[i]==(46 as u8) { dot=i } i=i+1 }
18 if dot<0 { return 0 }
19 let e: *u8 = sys_mmap(16); var w: i64=0; var y: i64=dot+1
20 while name[y]!=(0 as u8) { if w<14 { e[w]=jsw_lc(name[y] as i64) as u8; w=w+1 } y=y+1 } e[w]=0 as u8
21 if e[0]==(116 as u8) { if e[1]==(115 as u8) { if e[2]==(118 as u8) { if e[3]==(0 as u8) { return 1 } } } } // tsv
22 if e[0]==(99 as u8) { if e[1]==(111 as u8) { if e[2]==(110 as u8) { if e[3]==(102 as u8) { if e[4]==(0 as u8) { return 1 } } } } } // conf
23 if e[0]==(116 as u8) { if e[1]==(111 as u8) { if e[2]==(109 as u8) { if e[3]==(108 as u8) { if e[4]==(0 as u8) { return 1 } } } } } // toml
24 if e[0]==(106 as u8) { if e[1]==(115 as u8) { if e[2]==(111 as u8) { if e[3]==(110 as u8) { if e[4]==(0 as u8) { return 1 } } } } } // json
25 return 0
26}
27
28// build "knowledge/store/swept-" + name into out (NUL-term)
29func jsw_storename(name: *u8, out: *u8) -> i64 {
30 var o: i64=0; let p: *u8="knowledge/store/swept-" as *u8; var i: i64=0
31 while p[i]!=(0 as u8){ out[o]=p[i]; o=o+1; i=i+1 }
32 var j: i64=0; while name[j]!=(0 as u8){ if name[j]==(46 as u8) { out[o]=95 as u8 } else { out[o]=name[j] } o=o+1; j=j+1 } // '.'->'_'
33 out[o]=0 as u8; return 0
34}
35
36// SWEEP one directory: migrate every non-sov FORMAT file -> seg_store, byte-verified, original INTACT.
37// out_seen[0]=format files found, out_mig[0]=migrated (rows>0), out_ver[0]=byte-verified (retire-ready).
38func jan_sweep(dir: *u8, out_seen: *i64, out_mig: *i64, out_ver: *i64) -> i64 {
39 out_seen[0]=0; out_mig[0]=0; out_ver[0]=0
40 let fd: i64 = sys_openat_rd(dir); if fd<0 { return 0 }
41 let dbuf: *u8 = sys_mmap(K_MAGIC_131072)
42 let path: *u8 = sys_mmap(K_MAGIC_1024); let store: *u8 = sys_mmap(K_MAGIC_1024)
43 let totp: *i64 = sys_mmap(16) as *i64; let verp: *i64 = sys_mmap(16) as *i64
44 var go: i64=1
45 while go==1 {
46 let nb: i64 = sys_getdents64(fd, dbuf, K_MAGIC_131072)
47 if nb<=0 { go=0 } else {
48 var off: i64=0
49 while off<nb {
50 let rec: *u8 = (dbuf as i64 + off) as *u8
51 let rl: i64 = dirent_reclen(rec)
52 if rl<=0 { off=nb } else {
53 let nm: *u8 = dirent_name(rec)
54 if jsw_is_format(nm)==1 {
55 out_seen[0]=out_seen[0]+1
56 var o: i64=0; var z: i64=0
57 while dir[z]!=(0 as u8){ path[o]=dir[z]; o=o+1; z=z+1 }
58 path[o]=47 as u8; o=o+1
59 var y: i64=0; while nm[y]!=(0 as u8){ path[o]=nm[y]; o=o+1; y=y+1 } path[o]=0 as u8
60 jsw_storename(nm, store)
61 jan_migrate(path, store, totp, verp)
62 if totp[0]>0 { out_mig[0]=out_mig[0]+1; if verp[0]==totp[0] { out_ver[0]=out_ver[0]+1 } }
63 }
64 off=off+rl
65 }
66 }
67 }
68 }
69 sys_close(fd)
70 return out_ver[0]
71}