code wiki / _hdl_build / nx_store_sync.nx

nx_store_sync.nx source

↩ module page · 83 lines · 4274 B

1// nx_store_sync.nx -- sovereign reusable deploy tool: sync the native content-addressed store 2// (knowledge/store/hub-* + games-reg-*) to the NAS docroot store (/volume1/ai/hub/knowledge/store/), so the 3// hub gateway reads the latest cards/roles/maturity/games. Enumerates the dir (getdents64) and pushes each 4// file via nx_aw_push (fork/exec; it self-opens the vault + streams over SSH). No bash loops, no scratch .sh. 5// Requires /tmp/nx_aw_push.sov.elf built first (build nx_aw_push in the same call). license_tier: ORIGINAL 6import "nx_syscalls.nx" 7const K_MAGIC_65536: i64 = 65536 8const K_MAGIC_1024: i64 = 1024 9const K_MAGIC_2048: i64 = 2048 10 11func ss_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 12func ss_wfile(path: *u8, content: *u8, clen: i64) -> i64 { 13 let fd: i64 = sys_openat_wr(path, 420) 14 if fd < 0 { return 0 - 1 } 15 sys_write(fd, content, clen); sys_close(fd); return 0 16} 17func ss_starts(name: *u8, pre: *u8) -> i64 { 18 var i: i64=0 19 while pre[i]!=(0 as u8) { if name[i]!=pre[i] { return 0 } i=i+1 } 20 return 1 21} 22func ss_cat(buf: *u8, o: i64, s: *u8) -> i64 { var oo: i64=o; var i: i64=0; while s[i]!=(0 as u8){buf[oo]=s[i];oo=oo+1;i=i+1} return oo } 23 24// fork+exec nx_aw_push (reads awpush.src/.dst), wait for it. 25func ss_push() -> i64 { 26 let pid: i64 = sys_fork() 27 if pid == 0 { 28 let argv: *i64 = sys_mmap(32) as *i64 29 argv[0] = ("/tmp/nx_aw_push.sov.elf\x00") as i64; argv[1] = 0 30 let envp: *i64 = sys_mmap(32) as *i64 31 envp[0] = ("PATH=/usr/bin:/bin\x00") as i64; envp[1] = 0 32 sys_execve("/tmp/nx_aw_push.sov.elf\x00" as *u8, argv, envp) 33 sys_exit(127) 34 } 35 let st: *i64 = sys_mmap(16) as *i64 36 sys_wait4(pid, st, 0) 37 return 0 38} 39 40func main() -> i64 { 41 // openat(AT_FDCWD, "knowledge/store", O_RDONLY|O_DIRECTORY) 42 let fd: i64 = __syscall(257, 0 - 100, "knowledge/store\x00" as *u8 as i64, 0x10000, 0, 0, 0) 43 if fd < 0 { ss_w("nx_store_sync: cannot open knowledge/store dir\n\x00" as *u8); return 1 } 44 let buf: *u8 = sys_mmap(K_MAGIC_65536) 45 let srcbuf: *u8 = sys_mmap(K_MAGIC_1024) 46 let dstbuf: *u8 = sys_mmap(K_MAGIC_2048) 47 var pushed: i64 = 0 48 var go: i64 = 1 49 while go == 1 { 50 let nread: i64 = __syscall(217, fd, buf as i64, K_MAGIC_65536, 0, 0, 0) 51 if nread <= 0 { go = 0 } else { 52 var pos: i64 = 0 53 while pos < nread { 54 let reclen: i64 = (buf[pos+16] as i64) | ((buf[pos+17] as i64) << 8) 55 let name: *u8 = ((buf as i64) + pos + 19) as *u8 56 var take: i64 = 0 57 if ss_starts(name, "hub-\x00" as *u8) == 1 { take = 1 } 58 if ss_starts(name, "games-reg-\x00" as *u8) == 1 { take = 1 } 59 if take == 1 { 60 // awpush.src = /mnt/c/.../knowledge/store/<name>\n 61 var so: i64 = ss_cat(srcbuf, 0, "/mnt/c/Users/elder/nishi-core/nxc2/knowledge/store/\x00" as *u8) 62 so = ss_cat(srcbuf, so, name); srcbuf[so]=(10 as u8); so=so+1 63 ss_wfile("/mnt/c/Users/elder/AppData/Local/Temp/awpush.src\x00" as *u8, srcbuf, so) 64 // awpush.dst = atomic cat+mv into the NAS store dir 65 var doo: i64 = ss_cat(dstbuf, 0, "mkdir -p /volume1/ai/hub/knowledge/store; cat > /volume1/ai/hub/knowledge/store/\x00" as *u8) 66 doo = ss_cat(dstbuf, doo, name) 67 doo = ss_cat(dstbuf, doo, ".tmp; mv -f /volume1/ai/hub/knowledge/store/\x00" as *u8) 68 doo = ss_cat(dstbuf, doo, name) 69 doo = ss_cat(dstbuf, doo, ".tmp /volume1/ai/hub/knowledge/store/\x00" as *u8) 70 doo = ss_cat(dstbuf, doo, name); dstbuf[doo]=(10 as u8); doo=doo+1 71 ss_wfile("/mnt/c/Users/elder/AppData/Local/Temp/awpush.dst\x00" as *u8, dstbuf, doo) 72 ss_w(" push \x00" as *u8); ss_w(name); ss_w("\n\x00" as *u8) 73 ss_push() 74 pushed = pushed + 1 75 } 76 pos = pos + reclen 77 } 78 } 79 } 80 sys_close(fd) 81 ss_w("nx_store_sync: synced native-store files -> NAS. gateway reads them per-request (no restart).\n\x00" as *u8) 82 return 0 83}