code wiki / _hdl_build / nx_mkdirp_gate.nx

nx_mkdirp_gate.nx source

↩ module page · 48 lines · 2873 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" 6 7func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 8func 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 } 9 10// identical algorithm to nx_hostctl.nx::hc_mkdirp_parent (mkdir -p the parent dirs of a file dest). 11func mkdirp_parent(dest: *u8) -> i64 { 12 let buf: *u8 = sys_mmap(1024) 13 var i: i64 = 0 14 while dest[i] != (0 as u8) { 15 buf[i] = dest[i] 16 if dest[i] == (47 as u8) { if i > 0 { buf[i] = 0 as u8; sys_mkdir(buf, 0x1ed); buf[i] = 47 as u8 } } 17 i = i + 1 18 } 19 return 0 20} 21func can_open(p: *u8) -> i64 { 22 let fd: i64 = sys_openat_wr(p, 420) 23 if fd >= 0 { sys_write(fd, "ok" as *u8, 2); sys_close(fd); return 1 } 24 return 0 25} 26 27func main(argc: i64, argv: *i64) -> i64 { 28 w("=== nx_mkdirp_gate -- recv nested-dir fix (hc_mkdirp_parent) ===\n" as *u8) 29 var pass: i64 = 0; var tot: i64 = 0 30 31 // NEG-CONTROL: a fresh deep dest with NO mkdirp -> open FAILS (the original cmd_recv bug) 32 let neg: i64 = can_open("/tmp/mkpg_neg_xyz/a/b/c/file.txt" as *u8) 33 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) } 34 35 // POS: mkdirp then open the SAME-style nested dest -> SUCCEEDS 36 mkdirp_parent("/tmp/mkpg_pos_xyz/a/b/c/file.txt" as *u8) 37 let pos: i64 = can_open("/tmp/mkpg_pos_xyz/a/b/c/file.txt" as *u8) 38 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) } 39 40 // IDEMPOTENT: a 2nd mkdirp on an existing tree is harmless 41 mkdirp_parent("/tmp/mkpg_pos_xyz/a/b/c/file.txt" as *u8) 42 let again: i64 = can_open("/tmp/mkpg_pos_xyz/a/b/c/file.txt" as *u8) 43 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) } 44 45 w("nx_mkdirp_gate pass="); wn(pass); w("/"); wn(tot) 46 if pass==tot { w(" verdict=GREEN (recv can now install nested dest paths)\n" as *u8); sys_exit(0); return 0 } 47 w(" verdict=RED\n" as *u8); sys_exit(1); return 1 48}