code wiki / _hdl_build / nx_mkdirp_gate.nx
nx_mkdirp_gate.nx source
↩ module page · 56 lines · 3250 B
1// nx_mkdirp_gate.nx -- proves the nx_hostctl recv nested-dir fix (hc_mkdirp_parent). Liar-killed: WITHOUT
2// mkdir -p a fresh deep dest cannot be opened (neg-control = the original bug), WITH it the nested install
3// succeeds, and a 2nd call is idempotent. This is the exact capability cmd_recv needs to install /vn/1/index.html
4// style nested paths instead of forcing the flat-file layout. license_tier: ORIGINAL expect_exit: 0
5import "nx_syscalls.nx"
6import "nx_gate_verdict.nx"
7
8func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func wn(v: i64) -> i64 { var m: i64=v; if m==0{sys_write(1,"0" as *u8,1);return 0} let t:*u8=sys_mmap(24); var k:i64=0; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} let o:*u8=sys_mmap(24); var i:i64=0; while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 }
10
11// identical algorithm to nx_hostctl.nx::hc_mkdirp_parent (mkdir -p the parent dirs of a file dest).
12func mkdirp_parent(dest: *u8) -> i64 {
13 let buf: *u8 = sys_mmap(1024)
14 var i: i64 = 0
15 while dest[i] != (0 as u8) {
16 buf[i] = dest[i]
17 if dest[i] == (47 as u8) { if i > 0 { buf[i] = 0 as u8; sys_mkdir(buf, 0x1ed); buf[i] = 47 as u8 } }
18 i = i + 1
19 }
20 return 0
21}
22func can_open(p: *u8) -> i64 {
23 let fd: i64 = sys_openat_wr(p, 420)
24 if fd >= 0 { sys_write(fd, "ok" as *u8, 2); sys_close(fd); return 1 }
25 return 0
26}
27
28func main(argc: i64, argv: *i64) -> i64 {
29 w("=== nx_mkdirp_gate -- recv nested-dir fix (hc_mkdirp_parent) ===\n" as *u8)
30 var pass: i64 = 0; var tot: i64 = 0
31
32 // NEG-CONTROL: a fresh deep dest with NO mkdirp -> open FAILS (the original cmd_recv bug)
33 let neg: i64 = can_open("/tmp/mkpg_neg_xyz/a/b/c/file.txt" as *u8)
34 tot=tot+1; if neg==0 { pass=pass+1; w(" PASS neg-control: nested open WITHOUT mkdir -p fails (the bug)\n" as *u8) } else { w(" FAIL neg-control: open unexpectedly succeeded (stale dir?)\n" as *u8) }
35
36 // POS: mkdirp then open the SAME-style nested dest -> SUCCEEDS
37 mkdirp_parent("/tmp/mkpg_pos_xyz/a/b/c/file.txt" as *u8)
38 let pos: i64 = can_open("/tmp/mkpg_pos_xyz/a/b/c/file.txt" as *u8)
39 tot=tot+1; if pos==1 { pass=pass+1; w(" PASS mkdir -p enables nested install (/a/b/c/file.txt)\n" as *u8) } else { w(" FAIL mkdir -p did not create the parent dirs\n" as *u8) }
40
41 // IDEMPOTENT: a 2nd mkdirp on an existing tree is harmless
42 mkdirp_parent("/tmp/mkpg_pos_xyz/a/b/c/file.txt" as *u8)
43 let again: i64 = can_open("/tmp/mkpg_pos_xyz/a/b/c/file.txt" as *u8)
44 tot=tot+1; if again==1 { pass=pass+1; w(" PASS idempotent (2nd mkdir -p harmless)\n" as *u8) } else { w(" FAIL not idempotent\n" as *u8) }
45
46 w("nx_mkdirp_gate pass="); wn(pass); w("/"); wn(tot)
47 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
48 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
49 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
50 let ctr__dry: *i64 = gv_ctr()
51 ctr__dry[0] = pass
52 ctr__dry[1] = tot
53 let rc__dry: i64 = gv_verdict("MKDIRP-GATE" as *u8, ctr__dry, "recv can now install nested dest paths)" as *u8)
54 sys_exit(rc__dry)
55 return rc__dry
56}