interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / patch / p2base.om
| 1 | note patch/p2base.om: the path builder, the byte sources and SHA-256 over them, file writes, the git |
| 2 | note argument lists and the remover of a request's work folder. |
| 3 | const PATMO long 20000000000 |
| 4 | note ---- path builder gpp (host words take the global directly) |
| 5 | fn papc c uchar to void |
| 6 | n = get gppl 0 |
| 7 | if lt n 4095 |
| 8 | put gpp n c |
| 9 | put gppl 0 add n 1 |
| 10 | end |
| 11 | end |
| 12 | fn papt t view uchar * to void |
| 13 | for i ulong len t bound 4096 |
| 14 | papc get t i |
| 15 | end |
| 16 | end |
| 17 | note papb: gpp = <data> |
| 18 | fn papb to void |
| 19 | put gppl 0 0 |
| 20 | papt tfdata |
| 21 | end |
| 22 | note paprn: the repository's name (the public-list row fridx) appended to gpp |
| 23 | fn paprn to void |
| 24 | i = get fridx 0 |
| 25 | for k ulong get fanl i bound 128 |
| 26 | papc frnbyte i k |
| 27 | end |
| 28 | end |
| 29 | note paprepo: gpp = <data>patches/<owner>/<repo>/ |
| 30 | fn paprepo to void |
| 31 | papb |
| 32 | papt tpsdir |
| 33 | papt tfowner |
| 34 | papc 47 |
| 35 | paprn |
| 36 | papc 47 |
| 37 | end |
| 38 | note papid t: gpp = <data>patches/<owner>/<repo>/<gpid>/<t> |
| 39 | fn papid t view uchar * to void |
| 40 | paprepo |
| 41 | for k ulong 16 |
| 42 | papc get gpid k |
| 43 | end |
| 44 | papc 47 |
| 45 | papt t |
| 46 | end |
| 47 | note papnew t: gpp = <data>patches/<owner>/<repo>/.new-<gpstm>/<t>, this request's staging folder |
| 48 | fn papnew t view uchar * to void |
| 49 | paprepo |
| 50 | papt tpnewd |
| 51 | for k ulong 16 |
| 52 | papc get gpstm k |
| 53 | end |
| 54 | papc 47 |
| 55 | papt t |
| 56 | end |
| 57 | note papw t: gpp = <data>patch-work/pw<gpstm><t>, this request's work folder (t is empty or starts with /) |
| 58 | fn papw t view uchar * to void |
| 59 | papb |
| 60 | papt tptmp |
| 61 | papt tppw |
| 62 | for k ulong 16 |
| 63 | papc get gpstm k |
| 64 | end |
| 65 | papt t |
| 66 | end |
| 67 | note papgs: gpp = <store>projects/<name>/repo.git, the repository's bare git store, built by the |
| 68 | note store's own path builder (store/repos.om xspproj, as xsfolders does) in fpth and copied |
| 69 | fn papgs to void |
| 70 | xspproj get fridx 0 |
| 71 | fpt tfsrg |
| 72 | put gppl 0 0 |
| 73 | for i ulong get fpthl 0 bound 4096 |
| 74 | papc get fpth i |
| 75 | end |
| 76 | end |
| 77 | note paexist: the path in gpp exists |
| 78 | fn paexist to bool |
| 79 | r = stat gpp 0 get gppl 0 fstb 0 128 |
| 80 | ret ge r 0 |
| 81 | end |
| 82 | note pamkdir: make the folder gpp (0700); true when it exists afterwards |
| 83 | fn pamkdir to bool |
| 84 | r = makedir gpp 0 get gppl 0 448 |
| 85 | if ge r 0 |
| 86 | ret true |
| 87 | end |
| 88 | ret paexist |
| 89 | end |
| 90 | note ---- byte sources. pasb w i: byte i of source w: 0 the mbox, 1 the statement, 2 the signature, |
| 91 | note 3 the title, 4 the description, 5 the line buffer, 6 fxo, 7 title + line feed + description |
| 92 | fn pasb w ulong i ulong to uchar |
| 93 | if eq w 0 |
| 94 | ret get gpmb i |
| 95 | end |
| 96 | if eq w 1 |
| 97 | ret get gpst i |
| 98 | end |
| 99 | if eq w 2 |
| 100 | ret get gpsig i |
| 101 | end |
| 102 | if eq w 3 |
| 103 | ret get gpttl i |
| 104 | end |
| 105 | if eq w 4 |
| 106 | ret get gpdsc i |
| 107 | end |
| 108 | if eq w 5 |
| 109 | ret get gpln i |
| 110 | end |
| 111 | if eq w 6 |
| 112 | ret get fxo i |
| 113 | end |
| 114 | ret patd i |
| 115 | end |
| 116 | fn patd i ulong to uchar |
| 117 | n = get gpttll 0 |
| 118 | if lt i n |
| 119 | ret get gpttl i |
| 120 | end |
| 121 | if eq i n |
| 122 | ret uchar 10 |
| 123 | end |
| 124 | ret get gpdsc sub sub i n 1 |
| 125 | end |
| 126 | note pahs w n o: o = SHA-256 of the first n bytes of source w |
| 127 | fn pahs w ulong n ulong o edit uchar 32 to void |
| 128 | c = new ulong 40 0 |
| 129 | b = new uchar 4096 0 |
| 130 | sha256_init c |
| 131 | p = ulong 0 |
| 132 | while lt p n bound 16777216 |
| 133 | k = hmin sub n p 4096 |
| 134 | for i ulong k bound 4096 |
| 135 | put b i pasb w add p i |
| 136 | end |
| 137 | sha256_update c part b 0 k |
| 138 | p = add p k |
| 139 | end |
| 140 | sha256_final c o |
| 141 | end |
| 142 | note ---- the line buffer gpln |
| 143 | fn palb to void |
| 144 | put gplnl 0 0 |
| 145 | end |
| 146 | fn palc c uchar to void |
| 147 | n = get gplnl 0 |
| 148 | if lt n 255 |
| 149 | put gpln n c |
| 150 | put gplnl 0 add n 1 |
| 151 | end |
| 152 | end |
| 153 | fn paln v ulong to void |
| 154 | d = new uchar 20 0 |
| 155 | k = sodec v d |
| 156 | for i ulong k bound 20 |
| 157 | palc get d i |
| 158 | end |
| 159 | end |
| 160 | note ---- files |
| 161 | note pawall: all k bytes of b to fd |
| 162 | fn pawall fd int b view uchar 4096 k ulong to bool |
| 163 | q = ulong 0 |
| 164 | for it ulong 8192 |
| 165 | if ge q k |
| 166 | ret true |
| 167 | end |
| 168 | r = write fd b q sub k q |
| 169 | if le r 0 |
| 170 | if ne r -4 |
| 171 | ret false |
| 172 | end |
| 173 | else |
| 174 | q = add q ulong r |
| 175 | end |
| 176 | end |
| 177 | ret false |
| 178 | end |
| 179 | note pawf w n: create the file gpp (exclusive, 0600, no link followed) holding the first n bytes of |
| 180 | note source w, written through to the disk before it is closed |
| 181 | fn pawf w ulong n ulong to bool |
| 182 | fd = fopen gpp 0 get gppl 0 557249 384 |
| 183 | if lt fd 0 |
| 184 | ret false |
| 185 | end |
| 186 | b = new uchar 4096 0 |
| 187 | p = ulong 0 |
| 188 | ok = true |
| 189 | while lt p n bound 16777216 |
| 190 | k = hmin sub n p 4096 |
| 191 | for i ulong k bound 4096 |
| 192 | put b i pasb w add p i |
| 193 | end |
| 194 | if not pawall int fd b k |
| 195 | ok = false |
| 196 | break |
| 197 | end |
| 198 | p = add p k |
| 199 | end |
| 200 | if ok |
| 201 | s = fsync int fd |
| 202 | if lt s 0 |
| 203 | ok = false |
| 204 | end |
| 205 | end |
| 206 | x = fclose int fd |
| 207 | ret ok |
| 208 | end |
| 209 | note pawtxt t: the file gpp holding the text t |
| 210 | fn pawtxt t view uchar * to bool |
| 211 | palb |
| 212 | for i ulong len t bound 255 |
| 213 | palc get t i |
| 214 | end |
| 215 | ret pawf 5 get gplnl 0 |
| 216 | end |
| 217 | note ---- git argument lists (server/util.om fkb / fka / fkopen / fkc, run by fexec: no shell, empty |
| 218 | note environment, standard input and error /dev/null) |
| 219 | fn pakp to void |
| 220 | for i ulong get gppl 0 bound 4096 |
| 221 | fkc get gpp i |
| 222 | end |
| 223 | end |
| 224 | fn pakbase to void |
| 225 | for i ulong 40 |
| 226 | fkc get gpbase i |
| 227 | end |
| 228 | end |
| 229 | note pakst: argv = git --git-dir=<published store>/projects/<name>/repo.git |
| 230 | fn pakst to void |
| 231 | fkb |
| 232 | fka tfgit |
| 233 | fkopen |
| 234 | fkt tfgitdir |
| 235 | papgs |
| 236 | pakp |
| 237 | end |
| 238 | fn pakc1 t view uchar * to void |
| 239 | fka tpc |
| 240 | fka t |
| 241 | end |
| 242 | note pakwt: argv = git -C <work folder>/w --attr-source=<empty tree> -c <setting> ... for every git run in |
| 243 | note the throwaway work tree. The attribute source is the empty tree, so no .gitattributes file the |
| 244 | note patch writes (nor the index's) changes what git reports or converts: binary tests, diff drivers, |
| 245 | note textconv, filters and line endings all see no attributes. |
| 246 | fn pakwt to void |
| 247 | fkb |
| 248 | fka tfgit |
| 249 | fka tpdC |
| 250 | fkopen |
| 251 | papw tpww |
| 252 | pakp |
| 253 | fka tpatsrc |
| 254 | pakc1 tpk1 |
| 255 | pakc1 tpk2 |
| 256 | pakc1 tpk3 |
| 257 | pakc1 tpk4 |
| 258 | pakc1 tpk5 |
| 259 | pakc1 tpk6 |
| 260 | pakc1 tpk7 |
| 261 | pakc1 tpk8 |
| 262 | pakc1 tpk9 |
| 263 | pakc1 tpk10 |
| 264 | pakc1 tpk11 |
| 265 | pakc1 tpk12 |
| 266 | pakc1 tpk13 |
| 267 | pakc1 tpk14 |
| 268 | pakc1 tpk15 |
| 269 | pakc1 tpk16 |
| 270 | end |
| 271 | note ---- the work folder remover. git fills <data>patch-work/pw<stem>/ with the files and folders a patch |
| 272 | note names, so nothing about their names, depth or kind is assumed. A forked child changes into the |
| 273 | note folder and removes by relative name from the inside: a file, link or other entry is unlinked, an |
| 274 | note empty folder removed, and a folder that is not empty is entered through a descriptor opened with |
| 275 | note no-follow (a link is never entered) and emptied first. No path is built, so no path length or |
| 276 | note depth limits it, and it only ever steps back up as often as it stepped down. |
| 277 | fn parmnl o ulong to ulong |
| 278 | k = ulong 0 |
| 279 | while lt k 256 bound 256 |
| 280 | if eq get gpde add o k 0 |
| 281 | ret k |
| 282 | end |
| 283 | k = add k 1 |
| 284 | end |
| 285 | ret k |
| 286 | end |
| 287 | note parmdot: the entry name at o is . or .. |
| 288 | fn parmdot o ulong nl ulong to bool |
| 289 | if eq nl 1 |
| 290 | ret eq get gpde o 46 |
| 291 | end |
| 292 | if eq nl 2 |
| 293 | if eq get gpde o 46 |
| 294 | ret eq get gpde add o 1 46 |
| 295 | end |
| 296 | end |
| 297 | ret false |
| 298 | end |
| 299 | note parment: remove the entry at o (type ty, 4 a folder): 1 removed, 2 entered it, 3 cannot |
| 300 | fn parment o ulong nl ulong ty uchar to ulong |
| 301 | r = long 0 |
| 302 | if ne ty 4 |
| 303 | r = unlink gpde o nl |
| 304 | if ge r 0 |
| 305 | ret 1 |
| 306 | end |
| 307 | if ne r -21 |
| 308 | ret 3 |
| 309 | end |
| 310 | end |
| 311 | r = rmdir gpde o nl |
| 312 | if ge r 0 |
| 313 | ret 1 |
| 314 | end |
| 315 | fd = fopen gpde o nl 573440 0 |
| 316 | if lt fd 0 |
| 317 | ret 3 |
| 318 | end |
| 319 | r = fchdir int fd |
| 320 | x = fclose int fd |
| 321 | if lt r 0 |
| 322 | ret 3 |
| 323 | end |
| 324 | ret 2 |
| 325 | end |
| 326 | note parmscan: one pass over the folder open as fd: 0 it is empty, 1 entries were removed (scan again), |
| 327 | note 2 entered a folder, 3 an entry could not be removed |
| 328 | fn parmscan fd int to ulong |
| 329 | seen = ulong 0 |
| 330 | for it ulong 1000000 |
| 331 | n = getdnts fd gpde 0 32768 |
| 332 | if le n 0 |
| 333 | break |
| 334 | end |
| 335 | p = ulong 0 |
| 336 | while lt p ulong n bound 32768 |
| 337 | rl = or ulong get gpde add p 16 shl ulong get gpde add p 17 8 |
| 338 | if eq rl 0 |
| 339 | ret 3 |
| 340 | end |
| 341 | nl = parmnl add p 19 |
| 342 | if not parmdot add p 19 nl |
| 343 | seen = add seen 1 |
| 344 | r = parment add p 19 nl get gpde add p 18 |
| 345 | if ge r 2 |
| 346 | ret r |
| 347 | end |
| 348 | end |
| 349 | p = add p rl |
| 350 | end |
| 351 | end |
| 352 | if gt seen 0 |
| 353 | ret 1 |
| 354 | end |
| 355 | ret 0 |
| 356 | end |
| 357 | note parmch: the child: empty the current folder and everything below it, then exit 0 |
| 358 | fn parmch to void |
| 359 | dep = ulong 0 |
| 360 | for it ulong 4000000000 |
| 361 | fd = fopen tpdot 0 1 540672 0 |
| 362 | if lt fd 0 |
| 363 | exit 3 |
| 364 | end |
| 365 | st = parmscan int fd |
| 366 | x = fclose int fd |
| 367 | if eq st 3 |
| 368 | exit 4 |
| 369 | end |
| 370 | if eq st 2 |
| 371 | dep = add dep 1 |
| 372 | end |
| 373 | if eq st 0 |
| 374 | if eq dep 0 |
| 375 | exit 0 |
| 376 | end |
| 377 | r = chdir tpdd 0 2 |
| 378 | if lt r 0 |
| 379 | exit 5 |
| 380 | end |
| 381 | dep = sub dep 1 |
| 382 | end |
| 383 | end |
| 384 | exit 6 |
| 385 | end |
| 386 | note parmtree: remove this request's work folder and all it holds (30 s, then the child is killed) |
| 387 | fn parmtree to bool |
| 388 | papw part tpdot 0 0 |
| 389 | pid = fork |
| 390 | if lt pid 0 |
| 391 | ret false |
| 392 | end |
| 393 | if eq pid 0 |
| 394 | r = chdir gpp 0 get gppl 0 |
| 395 | if lt r 0 |
| 396 | exit 2 |
| 397 | end |
| 398 | parmch |
| 399 | exit 0 |
| 400 | end |
| 401 | dl = add long monons 30000000000 |
| 402 | st = freap int pid dl |
| 403 | papw part tpdot 0 0 |
| 404 | q = rmdir gpp 0 get gppl 0 |
| 405 | ret and eq st 0 ge q 0 |
| 406 | end |
| 407 | note ---- small files of an item (patch/p5imp.om): read without fxo, so a caller may keep a listing there |
| 408 | note pards: the file gpp into gpln (its first 256 bytes); the bytes read (256: the file has at least |
| 409 | note that many), or -errno |
| 410 | fn pards to long |
| 411 | put gplnl 0 0 |
| 412 | fd = fopen gpp 0 get gppl 0 524288 0 |
| 413 | if lt fd 0 |
| 414 | ret fd |
| 415 | end |
| 416 | for it ulong 64 |
| 417 | n = get gplnl 0 |
| 418 | if ge n 256 |
| 419 | break |
| 420 | end |
| 421 | r = read int fd gpln n sub 256 n |
| 422 | if le r 0 |
| 423 | break |
| 424 | end |
| 425 | put gplnl 0 add n ulong r |
| 426 | end |
| 427 | x = fclose int fd |
| 428 | ret long get gplnl 0 |
| 429 | end |
| 430 | note pal40: gpln is 40 lowercase hex digits and a line feed |
| 431 | fn pal40 to bool |
| 432 | if ne get gplnl 0 41 |
| 433 | ret false |
| 434 | end |
| 435 | for i ulong 40 |
| 436 | if lt sohexv get gpln i 0 |
| 437 | ret false |
| 438 | end |
| 439 | end |
| 440 | ret eq get gpln 40 10 |
| 441 | end |
| 442 | note palnis t: gpln is exactly t |
| 443 | fn palnis t view uchar * to bool |
| 444 | if ne get gplnl 0 len t |
| 445 | ret false |
| 446 | end |
| 447 | for i ulong len t bound 256 |
| 448 | if ne get gpln i get t i |
| 449 | ret false |
| 450 | end |
| 451 | end |
| 452 | ret true |
| 453 | end |
| 454 | note pastrd: the status file of item gpid: 0 "open\n", 1 "closed\n", 2 "merged\n", 9 anything else |
| 455 | fn pastrd to ulong |
| 456 | papid tpfss |
| 457 | r = pards |
| 458 | if palnis tpopen |
| 459 | ret 0 |
| 460 | end |
| 461 | if palnis tpsw1 |
| 462 | ret 1 |
| 463 | end |
| 464 | if palnis tpsw2 |
| 465 | ret 2 |
| 466 | end |
| 467 | ret 9 |
| 468 | end |
| 469 | note pastsz: the size of the file paexist last found (st_size of struct stat, aarch64 offset 48) |
| 470 | fn pastsz to ulong |
| 471 | v = ulong 0 |
| 472 | for i ulong 8 |
| 473 | v = or v shl ulong get fstb add 48 i mul i 8 |
| 474 | end |
| 475 | ret v |
| 476 | end |
| 477 | note ---- the bundle diff cache: <data>pdiff/<owner>/<repo>/<id> (derived data, safe to delete) |
| 478 | note papdr: gpp = <data>pdiff/<owner>/<repo>/ |
| 479 | fn papdr to void |
| 480 | papb |
| 481 | papt tpdpd |
| 482 | papt tfowner |
| 483 | papc 47 |
| 484 | paprn |
| 485 | papc 47 |
| 486 | end |
| 487 | note papdf: gpp = the cache file of item gpid |
| 488 | fn papdf to void |
| 489 | papdr |
| 490 | for k ulong 16 |
| 491 | papc get gpid k |
| 492 | end |
| 493 | end |
| 494 | note papdfg: gpp = the marker beside it, <id>.big: an empty file that says the bundle diff of item gpid |
| 495 | note was cut at 16 MiB (patch/p5imp.om pabbig) |
| 496 | fn papdfg to void |
| 497 | papdf |
| 498 | papt tpsfxg |
| 499 | end |
| 500 | note papdfn: gpp = this request's staging file beside it, .new-<gpstm> |
| 501 | fn papdfn to void |
| 502 | papdr |
| 503 | papt tpnewd |
| 504 | for k ulong 16 |
| 505 | papc get gpstm k |
| 506 | end |
| 507 | end |
| 508 | note pamkpd: make <data>pdiff/<owner>/<repo>/ (0700 each); false when it is not there afterwards |
| 509 | fn pamkpd to bool |
| 510 | papb |
| 511 | papt tpdpd |
| 512 | if not pamkdir |
| 513 | ret false |
| 514 | end |
| 515 | papt tfowner |
| 516 | if not pamkdir |
| 517 | ret false |
| 518 | end |
| 519 | papc 47 |
| 520 | paprn |
| 521 | ret pamkdir |
| 522 | end |