nx_fileop.nx source
↩ module page · 38 lines · 2365 B
1// nx_fileop.nx -- sovereign file-op CLI (retires Remove-Item / mv). Composes the canonical nx_fio + nx_syscalls's
2// sys_renameat. Usage:
3// nx_fileop rm <path> delete a file (refuses empty / "/" )
4// nx_fileop mv <src> <dst> atomic rename (sys_renameat)
5// nx_fileop exists <path> existence check (exit 0 if present, 3 if absent)
6// No shell. license_tier: ORIGINAL
7import "nx_fio.nx"
8import "nx_gate.nx"
9
10func fo_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8) { if a[i]!=b[i] { return 0 } i=i+1 } if b[i]!=(0 as u8) { return 0 } return 1 }
11func fo_bad_path(p: *u8) -> i64 { if p[0]==(0 as u8) { return 1 } if p[0]==(0x2f as u8) { if p[1]==(0 as u8) { return 1 } } return 0 }
12
13func main(argc: i64, argv: *i64) -> i64 {
14 if argc < 3 { gw("usage: nx_fileop rm <path> | mv <src> <dst> | exists <path>\n" as *u8); sys_exit(1); return 1 }
15 let op: *u8 = argv[1] as *u8
16 let a2: *u8 = argv[2] as *u8
17
18 if fo_streq(op, "rm" as *u8)==1 {
19 if fo_bad_path(a2)==1 { gw("nx_fileop rm: refusing empty / root path\n" as *u8); sys_exit(1); return 1 }
20 if fio_exists(a2)==0 { gw("nx_fileop rm: not found " as *u8); gw(a2); gw("\n" as *u8); sys_exit(2); return 2 }
21 let r: i64 = fio_unlink(a2)
22 if r==0 { gw("nx_fileop rm OK " as *u8); gw(a2); gw("\n" as *u8); sys_exit(0); return 0 }
23 gw("nx_fileop rm FAIL " as *u8); gw(a2); gw(" rc=" as *u8); gn(r); gw("\n" as *u8); sys_exit(3); return 3
24 }
25 if fo_streq(op, "mv" as *u8)==1 {
26 if argc < 4 { gw("usage: nx_fileop mv <src> <dst>\n" as *u8); sys_exit(1); return 1 }
27 let dst: *u8 = argv[3] as *u8
28 if fo_bad_path(a2)==1 { gw("nx_fileop mv: refusing empty / root src\n" as *u8); sys_exit(1); return 1 }
29 let r: i64 = sys_renameat(a2, dst)
30 if r==0 { gw("nx_fileop mv OK " as *u8); gw(a2); gw(" -> " as *u8); gw(dst); gw("\n" as *u8); sys_exit(0); return 0 }
31 gw("nx_fileop mv FAIL rc=" as *u8); gn(r); gw("\n" as *u8); sys_exit(3); return 3
32 }
33 if fo_streq(op, "exists" as *u8)==1 {
34 if fio_exists(a2)==1 { gw("nx_fileop exists YES " as *u8); gw(a2); gw("\n" as *u8); sys_exit(0); return 0 }
35 gw("nx_fileop exists NO " as *u8); gw(a2); gw("\n" as *u8); sys_exit(3); return 3
36 }
37 gw("nx_fileop: unknown op " as *u8); gw(op); gw("\n" as *u8); sys_exit(1); return 1
38}