code wiki / _hdl_build / nx_fs_gate.nx

nx_fs_gate.nx source

↩ module page · 46 lines · 2858 B

1// nx_fs_gate.nx -- SOVEREIGN gate for the nishi file-ops multitool (the coreutils replacement). 2// T1 cp: copy a file, content byte-identical 3// T2 mv: move it, content preserved + source gone 4// T3 rm: remove it, gone 5// T4 mkdir: directory exists afterward 6// license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_fs.nx" // fs_cp / fs_mv / fs_rm / fs_mkdir / fs_w 9 10func fg_uw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func fg_write(path: *u8, s: *u8, len: i64) -> i64 { let fd: i64=sys_openat_wr(path, 0x1a4); if fd<0 {return 0-1} sys_write(fd,s,len); sys_close(fd); return 0 } 12func fg_read_eq(path: *u8, exp: *u8, explen: i64) -> i64 { 13 let fd: i64=sys_openat_rd(path); if fd<0 {return 0} 14 let buf: *u8=sys_mmap(4096); let n: i64=sys_read(fd, buf, 4096); sys_close(fd) 15 if n != explen { return 0 } 16 var i: i64=0; while i<explen { if buf[i]!=exp[i] {return 0} i=i+1 } return 1 17} 18func fg_exists(path: *u8) -> i64 { let fd: i64=sys_openat_rd(path); if fd>=0 {sys_close(fd); return 1} return 0 } 19func fg_isdir(path: *u8) -> i64 { let fd: i64=__syscall(257, 0-100, path, 0x10000, 0, 0, 0); if fd>=0 {sys_close(fd); return 1} return 0 } 20 21func main(argc: i64, argv: *i64) -> i64 { 22 var pass: i64=0; var total: i64=0 23 fs_rm("/tmp/fs_a" as *u8); fs_rm("/tmp/fs_b" as *u8); fs_rm("/tmp/fs_c" as *u8) 24 fg_write("/tmp/fs_a" as *u8, "hello" as *u8, 5) 25 26 var t1: i64=0; if fs_cp("/tmp/fs_a" as *u8, "/tmp/fs_b" as *u8)==0 { if fg_read_eq("/tmp/fs_b" as *u8, "hello" as *u8, 5)==1 { t1=1 } } 27 total=total+1; if t1==1 {pass=pass+1; fg_uw("[PASS] " as *u8)} else {fg_uw("[FAIL] " as *u8)} 28 fg_uw("T1 cp: /tmp/fs_b is byte-identical 'hello'\n" as *u8) 29 30 var t2: i64=0; if fs_mv("/tmp/fs_b" as *u8, "/tmp/fs_c" as *u8)==0 { if fg_read_eq("/tmp/fs_c" as *u8, "hello" as *u8, 5)==1 { if fg_exists("/tmp/fs_b" as *u8)==0 { t2=1 } } } 31 total=total+1; if t2==1 {pass=pass+1; fg_uw("[PASS] " as *u8)} else {fg_uw("[FAIL] " as *u8)} 32 fg_uw("T2 mv: /tmp/fs_c preserved + /tmp/fs_b gone\n" as *u8) 33 34 var t3: i64=0; if fs_rm("/tmp/fs_c" as *u8)==0 { if fg_exists("/tmp/fs_c" as *u8)==0 { t3=1 } } 35 total=total+1; if t3==1 {pass=pass+1; fg_uw("[PASS] " as *u8)} else {fg_uw("[FAIL] " as *u8)} 36 fg_uw("T3 rm: /tmp/fs_c gone\n" as *u8) 37 38 fs_mkdir("/tmp/fs_d" as *u8) 39 var t4: i64=0; if fg_isdir("/tmp/fs_d" as *u8)==1 { t4=1 } 40 total=total+1; if t4==1 {pass=pass+1; fg_uw("[PASS] " as *u8)} else {fg_uw("[FAIL] " as *u8)} 41 fg_uw("T4 mkdir: /tmp/fs_d exists as a directory\n" as *u8) 42 43 fg_uw("=== nx_fs_gate " as *u8); let b: *u8=sys_mmap(8); b[0]=(48+pass) as u8; sys_write(1,b,1); fg_uw("/" as *u8); let c: *u8=sys_mmap(8); c[0]=(48+total) as u8; sys_write(1,c,1) 44 if pass==total {fg_uw(" GREEN\n" as *u8); return 0} 45 fg_uw(" RED\n" as *u8); return 1 46}