code wiki / _hdl_build / nx_cp.nx

nx_cp.nx source

↩ module page · 35 lines · 1999 B

1// nx_cp.nx -- sovereign file copy (read src, write dst as an executable, mode 0755). Exists to INSTALL the 2// womb's /tmp build output into a PERSISTENT path (_offc/ on /mnt/c) so the CLI organs survive a WSL idle 3// shutdown without rebuilding -- the /tmp build target is volatile (tmpfs wiped when the WSL VM idles). No sh. 4// usage: nx_cp <src> <dst> 5// license_tier: ORIGINAL 6import "nx_syscalls.nx" 7import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 8 9func cp_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 10// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 11// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 12// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 13// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 14func cp_wn(v: i64) -> i64 { nxi_out(v); return 0 } 15 16func main(argc: i64, argv: *i64) -> i64 { 17 if argc < 3 { sys_write(2, "usage: nx_cp <src> <dst>\n" as *u8, 25); return 1 } 18 let src: *u8 = argv[1] as *u8 19 let dst: *u8 = argv[2] as *u8 20 let lenbox: *i64 = sys_mmap(16) as *i64; lenbox[0] = 0 21 let buf: *u8 = sys_read_file(src, lenbox) 22 if (buf as i64) == 0 { cp_w("nx_cp: cannot read src: " as *u8); cp_w(src); cp_w("\n" as *u8); return 2 } 23 let n: i64 = lenbox[0] 24 let fd: i64 = sys_openat_wr(dst, 0x1ed) // 0755 (rwxr-xr-x) -- the copy is executable 25 if fd < 0 { cp_w("nx_cp: cannot open dst: " as *u8); cp_w(dst); cp_w("\n" as *u8); return 3 } 26 var off: i64 = 0 27 while off < n { 28 let w: i64 = sys_write(fd, ((buf as i64) + off) as *u8, n - off) 29 if w <= 0 { sys_close(fd); cp_w("nx_cp: write failed\n" as *u8); return 4 } 30 off = off + w 31 } 32 sys_close(fd) 33 cp_w("CP " as *u8); cp_w(src); cp_w(" -> " as *u8); cp_w(dst); cp_w(" (" as *u8); cp_wn(n); cp_w(" bytes)\n" as *u8) 34 return 0 35}