interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / tools / bring / src / 20walk.om
| 1 | note Snapshot walk: file-backed records and contents, no entry-count or file-size table. |
| 2 | note Record: path length u16, mode u64 at 8, size u64 at 16, spool offset u64 at 24, |
| 3 | note path bytes at 32 (ustar wire maximum 255), zero padding to 288. |
| 4 | text terrio "local file operation failed" |
| 5 | text terrdir "cannot open project folder" |
| 6 | text tdot "." |
| 7 | text tdotdot ".." |
| 8 | text tgit ".git" |
| 9 | text tgitwhy "version-control metadata" |
| 10 | text tsymwhy "symlink or special file" |
| 11 | text tlongwhy "path cannot be represented by POSIX ustar" |
| 12 | text tsecret "likely secret (name or content)" |
| 13 | text tleft "Left out: " |
| 14 | text tcolon ": " |
| 15 | text tpriv "PRIVATE KEY-----" |
| 16 | text tgh "ghp_" |
| 17 | text tghpat "github_pat_" |
| 18 | text taki "AKIA" |
| 19 | text txox "xox" |
| 20 | text tsk "sk_live_" |
| 21 | text tai "AIza" |
| 22 | text tant "sk-ant-" |
| 23 | text tpwd "password" |
| 24 | text tpass "passwd" |
| 25 | text tsecr "secret" |
| 26 | text ttok "token" |
| 27 | text tenv ".env" |
| 28 | text tnetrc ".netrc" |
| 29 | text tcred "credentials" |
| 30 | text tid "id_" |
| 31 | text tpem ".pem" |
| 32 | text tkey ".key" |
| 33 | text tp12 ".p12" |
| 34 | text tpub ".pub" |
| 35 | text tfiles "files" |
| 36 | text tkeep "keep" |
| 37 | text tdirs "dirs" |
| 38 | global gsorted uchar 1 |
| 39 | text ttar "list.tar" |
| 40 | text ttmp "/tmp/interplane-tool-" |
| 41 | text tname "Name: " |
| 42 | text tcnt "Files: " |
| 43 | text tbytes "Bytes: " |
| 44 | global gcount ulong 1 |
| 45 | global gbytes ulong 1 |
| 46 | global grecfd int 1 |
| 47 | global gspool int 1 |
| 48 | global goffset ulong 1 |
| 49 | |
| 50 | fn mustwrite fd int b view uchar * to void |
| 51 | if not wall fd b |
| 52 | die terrio |
| 53 | end |
| 54 | end |
| 55 | fn exactread fd int b edit uchar * to bool |
| 56 | n = ulong 0 |
| 57 | while lt n len b bound 9223372036854775807 |
| 58 | r = read fd b n sub len b n |
| 59 | if eq r -4 |
| 60 | continue |
| 61 | end |
| 62 | if le r 0 |
| 63 | ret false |
| 64 | end |
| 65 | n = add n ulong r |
| 66 | end |
| 67 | ret true |
| 68 | end |
| 69 | fn tmpsetup to void |
| 70 | p = new uchar 4096 0 |
| 71 | n = cpy p 0 ttmp |
| 72 | n = decw ulong pl_pid p n |
| 73 | put p n 45 |
| 74 | n = add n 1 |
| 75 | v = ulong 0 |
| 76 | while true bound 9223372036854775807 |
| 77 | k = decw v p n |
| 78 | r = pl_mkdir part p 0 k 448 |
| 79 | if eq r 0 |
| 80 | for i ulong k bound 4096 |
| 81 | put gtmp i get p i |
| 82 | end |
| 83 | put gtmpl 0 k |
| 84 | put gtmpok 0 1 |
| 85 | ret |
| 86 | end |
| 87 | if ne r -17 |
| 88 | die terrio |
| 89 | end |
| 90 | v = add v 1 |
| 91 | end |
| 92 | end |
| 93 | fn leftout p view uchar * why view uchar * to void |
| 94 | say tleft |
| 95 | sayp p |
| 96 | say tcolon |
| 97 | say why |
| 98 | say tnl |
| 99 | end |
| 100 | fn ends s view uchar * t view uchar * to bool |
| 101 | if lt len s len t |
| 102 | ret false |
| 103 | end |
| 104 | ret bati s sub len s len t t |
| 105 | end |
| 106 | note isgit: a path component that is ".git" in any letter case (the hub's rule in open/intake.om: |
| 107 | note length 4, '.', then g i t compared lowercased). The walk leaves such a folder or file out; a |
| 108 | note get or unpack refuses an archive with such a component anywhere in a path. |
| 109 | fn isgit c view uchar * to bool |
| 110 | if ne len c 4 |
| 111 | ret false |
| 112 | end |
| 113 | ret bati c 0 tgit |
| 114 | end |
| 115 | fn secretname s view uchar * to bool |
| 116 | if or bati s 0 tenv bati s 0 tid |
| 117 | ret true |
| 118 | end |
| 119 | if or or beq s tnetrc beq s tcred ends s tpem |
| 120 | ret true |
| 121 | end |
| 122 | ret or ends s tkey ends s tp12 |
| 123 | end |
| 124 | fn contenthit b view uchar * to bool |
| 125 | for i ulong len b bound 9223372034775807 |
| 126 | if or bat b i tpriv bat b i tgh |
| 127 | ret true |
| 128 | end |
| 129 | if or bat b i tghpat bat b i taki |
| 130 | ret true |
| 131 | end |
| 132 | if or bat b i tsk bat b i tai |
| 133 | ret true |
| 134 | end |
| 135 | if or bat b i tant bat b i txox |
| 136 | ret true |
| 137 | end |
| 138 | if assignhit b i |
| 139 | ret true |
| 140 | end |
| 141 | end |
| 142 | ret false |
| 143 | end |
| 144 | note assignhit: password/passwd/secret/token (any case), more name bytes, '=' or ':', then a value |
| 145 | note of at least 8 bytes that are not space or quote: PASSWORD="hunter2hunter" hits, while the prose |
| 146 | note "password: use a long one" does not. |
| 147 | fn assignhit b view uchar * i ulong to bool |
| 148 | k = ulong 0 |
| 149 | if or bati b i tpwd bati b i tpass |
| 150 | k = ulong 6 |
| 151 | end |
| 152 | if or bati b i tsecr bati b i ttok |
| 153 | k = ulong 5 |
| 154 | end |
| 155 | if eq k 0 |
| 156 | ret false |
| 157 | end |
| 158 | j = add i k |
| 159 | while lt j len b bound 9223372036854775807 |
| 160 | c = get b j |
| 161 | if not or isaln c or eq c 95 eq c 45 |
| 162 | break |
| 163 | end |
| 164 | j = add j 1 |
| 165 | end |
| 166 | j = skipsq b j |
| 167 | if ge j len b |
| 168 | ret false |
| 169 | end |
| 170 | c = get b j |
| 171 | if not or eq c 61 eq c 58 |
| 172 | ret false |
| 173 | end |
| 174 | j = skipsq b add j 1 |
| 175 | v = ulong 0 |
| 176 | while lt j len b bound 9223372036854775807 |
| 177 | c = get b j |
| 178 | if or le c 32 or eq c 34 eq c 39 |
| 179 | break |
| 180 | end |
| 181 | v = add v 1 |
| 182 | j = add j 1 |
| 183 | end |
| 184 | ret ge v 8 |
| 185 | end |
| 186 | fn skipsq b view uchar * j ulong to ulong |
| 187 | k = j |
| 188 | while lt k len b bound 9223372036854775807 |
| 189 | c = get b k |
| 190 | if not or eq c 32 or eq c 9 or eq c 34 eq c 39 |
| 191 | ret k |
| 192 | end |
| 193 | k = add k 1 |
| 194 | end |
| 195 | ret k |
| 196 | end |
| 197 | note scans all bytes and spools them; 128-byte overlap covers every searched marker and assignment. |
| 198 | fn snapshot fd int to bool |
| 199 | b = new uchar 65664 0 |
| 200 | n = ulong 0 |
| 201 | hit = false |
| 202 | while true bound 9223372036854775807 |
| 203 | r = read fd b n 65536 |
| 204 | if eq r -4 |
| 205 | continue |
| 206 | end |
| 207 | if lt r 0 |
| 208 | die terrio |
| 209 | end |
| 210 | if eq r 0 |
| 211 | break |
| 212 | end |
| 213 | mustwrite get gspool 0 part b n ulong r |
| 214 | put goffset 0 add get goffset 0 ulong r |
| 215 | k = add n ulong r |
| 216 | if contenthit part b 0 k |
| 217 | hit = true |
| 218 | end |
| 219 | n = k |
| 220 | if gt n 128 |
| 221 | n = ulong 128 |
| 222 | end |
| 223 | for i ulong n bound 128 |
| 224 | put b i get b add sub k n i |
| 225 | end |
| 226 | end |
| 227 | ret not hit |
| 228 | end |
| 229 | fn addfile nm view uchar * path view uchar * to void |
| 230 | if secretname nm |
| 231 | leftout path tsecret |
| 232 | ret |
| 233 | end |
| 234 | fd = pl_open nm 0 0 |
| 235 | if lt fd 0 |
| 236 | die terrio |
| 237 | end |
| 238 | st = new uchar 256 0 |
| 239 | r = pl_fstat int fd st |
| 240 | if lt r 0 |
| 241 | die terrio |
| 242 | end |
| 243 | mode = pl_mode st |
| 244 | if ne and mode 61440 32768 |
| 245 | pl_close int fd |
| 246 | leftout path tsymwhy |
| 247 | ret |
| 248 | end |
| 249 | start = get goffset 0 |
| 250 | ok = snapshot int fd |
| 251 | pl_close int fd |
| 252 | if not ok |
| 253 | leftout path tsecret |
| 254 | r = pl_trunc get gspool 0 start |
| 255 | r = pl_seek get gspool 0 start |
| 256 | put goffset 0 start |
| 257 | ret |
| 258 | end |
| 259 | recfile path mode start |
| 260 | end |
| 261 | fn recfile path view uchar * mode ulong start ulong to void |
| 262 | rec = new uchar 288 0 |
| 263 | wr16 rec 0 len path |
| 264 | m = ulong 420 |
| 265 | if ne and mode 73 0 |
| 266 | m = ulong 493 |
| 267 | end |
| 268 | wr64 rec 8 m |
| 269 | sz = sub get goffset 0 start |
| 270 | wr64 rec 16 sz |
| 271 | wr64 rec 24 start |
| 272 | k = cpy rec 32 path |
| 273 | mustwrite get grecfd 0 rec |
| 274 | put gcount 0 add get gcount 0 1 |
| 275 | put gbytes 0 add get gbytes 0 sz |
| 276 | end |
| 277 | note each recursion adds a path component, so ustar's wire path bound limits depth, not a chosen cap. |
| 278 | fn walk prefix view uchar * to void |
| 279 | h = pl_dopen tdot |
| 280 | if eq h 0 |
| 281 | die terrdir |
| 282 | end |
| 283 | fd = pl_dfd h |
| 284 | b = new uchar 4096 0 |
| 285 | while true bound 9223372036854775807 |
| 286 | r = pl_dents h b |
| 287 | if lt r 0 |
| 288 | die terrio |
| 289 | end |
| 290 | if eq r 0 |
| 291 | break |
| 292 | end |
| 293 | walkbuf part b 0 ulong r prefix fd |
| 294 | end |
| 295 | pl_dclose h |
| 296 | end |
| 297 | fn walkbuf b view uchar * prefix view uchar * fd int to void |
| 298 | o = ulong 0 |
| 299 | while lt o len b bound 9223372036854775807 |
| 300 | sz = pl_dlen b o |
| 301 | if or eq sz 0 gt add o sz len b |
| 302 | die terrio |
| 303 | end |
| 304 | walkent part b o sz prefix fd |
| 305 | o = add o sz |
| 306 | end |
| 307 | end |
| 308 | fn walkent e view uchar * prefix view uchar * fd int to void |
| 309 | nm = new uchar 256 0 |
| 310 | n = ulong 0 |
| 311 | j = pl_dname |
| 312 | while lt j len e bound 4096 |
| 313 | c = get e j |
| 314 | if eq c 0 |
| 315 | break |
| 316 | end |
| 317 | if ge n 255 |
| 318 | die terrio |
| 319 | end |
| 320 | put nm n c |
| 321 | n = add n 1 |
| 322 | j = add j 1 |
| 323 | end |
| 324 | if or beq part nm 0 n tdot beq part nm 0 n tdotdot |
| 325 | ret |
| 326 | end |
| 327 | walkone part nm 0 n prefix fd |
| 328 | end |
| 329 | fn walkone nm view uchar * prefix view uchar * parent int to void |
| 330 | path = new uchar 512 0 |
| 331 | n = cpy path 0 prefix |
| 332 | n = cpy path n nm |
| 333 | if isgit nm |
| 334 | leftout part path 0 n tgitwhy |
| 335 | ret |
| 336 | end |
| 337 | if gt n 255 |
| 338 | leftout part path 0 n tlongwhy |
| 339 | ret |
| 340 | end |
| 341 | st = new uchar 256 0 |
| 342 | r = pl_stat nm st |
| 343 | if lt r 0 |
| 344 | die terrio |
| 345 | end |
| 346 | kind = and pl_mode st 61440 |
| 347 | if eq kind 32768 |
| 348 | addfile nm part path 0 n |
| 349 | ret |
| 350 | end |
| 351 | if eq kind 16384 |
| 352 | fd = pl_open nm 1 0 |
| 353 | if lt fd 0 |
| 354 | die terrio |
| 355 | end |
| 356 | r = pl_fcd int fd |
| 357 | if lt r 0 |
| 358 | die terrio |
| 359 | end |
| 360 | pl_close int fd |
| 361 | put path n 47 |
| 362 | walk part path 0 add n 1 |
| 363 | r = pl_fcd parent |
| 364 | if lt r 0 |
| 365 | die terrio |
| 366 | end |
| 367 | ret |
| 368 | end |
| 369 | leftout part path 0 n tsymwhy |
| 370 | end |
| 371 | note bottom-up merge sort of the record file: ping-pong between files and dirs, O(n log n) |
| 372 | note record reads and two records in memory, whatever the number of files. |
| 373 | fn rdrec fd int i ulong b edit uchar * to void |
| 374 | r = pl_readat fd b mul i 288 |
| 375 | if ne r 288 |
| 376 | die terrio |
| 377 | end |
| 378 | end |
| 379 | fn mergerun src int dst int lo ulong mid ulong hi ulong to void |
| 380 | a = new uchar 288 0 |
| 381 | b = new uchar 288 0 |
| 382 | i = lo |
| 383 | j = mid |
| 384 | while or lt i mid lt j hi bound 9223372036854775807 |
| 385 | takea = ge j hi |
| 386 | if and lt i mid lt j hi |
| 387 | rdrec src i a |
| 388 | rdrec src j b |
| 389 | takea = not pathless b a |
| 390 | end |
| 391 | if takea |
| 392 | rdrec src i a |
| 393 | mustwrite dst a |
| 394 | i = add i 1 |
| 395 | else |
| 396 | rdrec src j b |
| 397 | mustwrite dst b |
| 398 | j = add j 1 |
| 399 | end |
| 400 | end |
| 401 | end |
| 402 | fn mergepass src int dst int w ulong to void |
| 403 | n = get gcount 0 |
| 404 | s = ulong 0 |
| 405 | while lt s n bound 9223372036854775807 |
| 406 | m = add s w |
| 407 | if gt m n |
| 408 | m = n |
| 409 | end |
| 410 | e = add m w |
| 411 | if gt e n |
| 412 | e = n |
| 413 | end |
| 414 | mergerun src dst s m e |
| 415 | s = e |
| 416 | end |
| 417 | end |
| 418 | fn sortfiles to void |
| 419 | w = ulong 1 |
| 420 | put gsorted 0 0 |
| 421 | while lt w get gcount 0 bound 64 |
| 422 | src = int 0 |
| 423 | dst = int 0 |
| 424 | if eq get gsorted 0 0 |
| 425 | src = topen tfiles 0 |
| 426 | dst = topen tdirs 2 |
| 427 | else |
| 428 | src = topen tdirs 0 |
| 429 | dst = topen tfiles 2 |
| 430 | end |
| 431 | mergepass src dst w |
| 432 | pl_close src |
| 433 | pl_close dst |
| 434 | put gsorted 0 sub 1 get gsorted 0 |
| 435 | w = mul w 2 |
| 436 | end |
| 437 | end |
| 438 | fn opensorted to int |
| 439 | if eq get gsorted 0 0 |
| 440 | ret topen tfiles 0 |
| 441 | end |
| 442 | ret topen tdirs 0 |
| 443 | end |
| 444 | note pathless: record a sorts before record b (path bytes, then shorter first) |
| 445 | fn pathless a view uchar * b view uchar * to bool |
| 446 | an = rd16 a 0 |
| 447 | bn = rd16 b 0 |
| 448 | n = an |
| 449 | if gt n bn |
| 450 | n = bn |
| 451 | end |
| 452 | for i ulong n bound 255 |
| 453 | x = get a add 32 i |
| 454 | y = get b add 32 i |
| 455 | if lt x y |
| 456 | ret true |
| 457 | end |
| 458 | if gt x y |
| 459 | ret false |
| 460 | end |
| 461 | end |
| 462 | ret lt an bn |
| 463 | end |