interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / scripts / allen / contacts / allen_contacts_ingest.om

1738 lines · 26859 bytes Raw Blame History
1note allen-contacts-ingest: seeded Omega port of allen_contacts_ingest.py, which stays the parity oracle.
2note Moves consented contact drops from the gateway into the private store. Logs counts, never a field.
3note Usage: allen-contacts-ingest ingest [DROP STORE EPOCH]. EPOCH fixes the audit/quarantine stamp (tests).
4const OSTO ulong 0
5const NSTO ulong 14
6const OAUD ulong 14
7const NAUD ulong 9
8const OLCK ulong 23
9const NLCK ulong 7
10const OQDR ulong 30
11const NQDR ulong 10
12const OTMP ulong 40
13const NTMP ulong 20
14const OJSN ulong 60
15const NJSN ulong 5
16const OING ulong 65
17const NING ulong 6
18const ODDR ulong 71
19const NDDR ulong 33
20const OSDR ulong 104
21const NSDR ulong 23
22const OFRM ulong 127
23const NFRM ulong 16
24const OHDR ulong 143
25const NHDR ulong 22
26const ONTH ulong 165
27const NNTH ulong 8
28const OINT ulong 173
29const NINT ulong 9
30const OFGT ulong 182
31const NFGT ulong 6
32const OHUM ulong 188
33const NHUM ulong 5
34const OAGT ulong 193
35const NAGT ulong 5
36const OCHT ulong 198
37const NCHT ulong 4
38const OAPI ulong 202
39const NAPI ulong 3
40const OHTP ulong 205
41const NHTP ulong 8
42const OIDP ulong 213
43const NIDP ulong 9
44const OSHP ulong 222
45const NSHP ulong 24
46const ODOT ulong 246
47const NDOT ulong 1
48const OSLS ulong 247
49const NSLS ulong 1
50const ONL ulong 248
51const NNL ulong 1
52const OUSE ulong 249
53const NUSE ulong 55
54const OEDR ulong 304
55const NEDR ulong 47
56const OELK ulong 351
57const NELK ulong 32
58const OEST ulong 383
59const NEST ulong 46
60const OEQT ulong 429
61const NEQT ulong 44
62const OFVR ulong 473
63const NFVR ulong 24
64const OVDP ulong 497
65const NVDP ulong 10
66const OFVT ulong 507
67const NFVT ulong 14
68note Counter slots, in the sorted order the summary line prints them.
69const CFM ulong 0
70const CFU ulong 1
71const CVM ulong 2
72const CVU ulong 3
73const CIA ulong 4
74const CIS ulong 5
75const CQD long 6
76const CQI long 7
77const CQN long 8
78const CQO long 9
79const CQF ulong 10
80const CQ ulong 11
81const CRN ulong 12
82const CST ulong 13
83const CTP ulong 14
84const CTS ulong 15
85const MAXD ulong 65536
86const MAXR ulong 65537
87const MAXE ulong 4096
88const QCAP long 1000
89const SCAP ulong 16777216
90note Slots of gu (ulong state).
91const GDL ulong 0
92const GSL ulong 1
93const GDN ulong 2
94const GPL ulong 3
95const GNE ulong 4
96const GWSC ulong 5
97const GFW ulong 6
98const GLW ulong 7
99const GOL ulong 8
100const GADV ulong 9
101const GSN ulong 10
102const GNK ulong 11
103const GIDF ulong 12
104const GAL ulong 13
105const GTL ulong 14
106const GLK ulong 15
107const GLE ulong 16
108const GMOD ulong 17
109note Slots of gl (long state).
110const LNOW ulong 0
111text lit "contacts.jsonlaudit.log.lock.dquarantine.contacts.jsonl.tmp-.jsoningest/var/lib/allen-chat/contacts-drop/var/lib/allen-contacts forget removed=allen-contacts-ingest: nothingintroduceforgethumanagentchatapihttps://, \"id\": \"\"delete_token_sha256\": \"./\nusage: allen-contacts-ingest ingest [DROP STORE EPOCH]\nallen-contacts: unsafe drop or store directory\nallen-contacts: store is locked\nallen-contacts: store too large or unreadable\nallen-contacts: quarantine directory unsafe\n forget_visitor removed=, \"vid\": \"forget_visitor"
112text kin "contact delete_token_sha256 framework github homepage human id kind model name org origin purpose received type v via vid"
113text kfo "id received token_sha256 type v"
114text kfv "received type v vid"
115text cn "forget_matched forget_unmatched forget_visitor_matched forget_visitor_unmatched introduce_already_stored introduce_stored q_duplicate_id q_invalid q_name q_oversize quarantine_full_discarded quarantined removed_nonregular stuck_nonregular temp_pending temp_stale_removed"
116global gu ulong 32
117global gl long 8
118global cnt long 16
119global ddir uchar 4096
120global sdir uchar 4096
121global pth uchar 4096
122global pk uchar 8192
123global dbuf uchar 65537
124global sbuf uchar 16777216
125global nbuf uchar 1048576
126global nlen ulong 4096
127global nmt long 4096
128global ordx ulong 4096
129global dents uchar 65536
130global stb uchar 128
131global fs ulong 32
132global fe ulong 32
133global keys uchar 256
134global kof ulong 32
135global kln ulong 32
136global rnd uchar 16
137global ob uchar 2048
138global ab uchar 128
139global tb uchar 32
140
141note ---------------------------------------------------------------- small helpers
142
143fn bump i ulong to void
144put cnt i add get cnt i 1
145end
146
147fn pset to void
148put gu GPL 0
149end
150
151fn pb b uchar to void
152n = get gu GPL
153put pth n b
154put gu GPL add n 1
155end
156
157fn plit o ulong n ulong to void
158for i ulong n
159pb get lit add o i
160end
161end
162
163fn pdd to void
164for i ulong get gu GDL
165pb get ddir i
166end
167end
168
169fn psd to void
170for i ulong get gu GSL
171pb get sdir i
172end
173end
174
175fn pnam i ulong to void
176b = mul i 256
177for j ulong get nlen i
178pb get nbuf add b j
179end
180end
181
182fn hexch v ulong to uchar
183if lt v 10
184ret uchar add v 48
185end
186ret uchar add v 87
187end
188
189fn phex k ulong to long
190r = random rnd 0 k 0
191if ne r long k
192ret -1
193end
194for i ulong k
195v = ulong get rnd i
196pb hexch shr v 4
197pb hexch and v 15
198end
199ret 0
200end
201
202fn rd32 o ulong to ulong
203v = ulong get stb o
204v = or v shl ulong get stb add o 1 8
205v = or v shl ulong get stb add o 2 16
206v = or v shl ulong get stb add o 3 24
207ret v
208end
209
210fn rd64 o ulong to long
211v = rd32 o
212h = rd32 add o 4
213ret bitcast long or v shl h 32
214end
215
216fn mtns to long
217ret add mul rd64 88 1000000000 rd64 96
218end
219
220fn plstat to long
221ret lstat pth 0 get gu GPL stb 0 128
222end
223
224note ------------------------------------------------------------------ output buffers
225
226fn ob1 b uchar to void
227n = get gu GOL
228put ob n b
229put gu GOL add n 1
230end
231
232fn ab1 b uchar to void
233n = get gu GAL
234put ab n b
235put gu GAL add n 1
236end
237
238fn olit o ulong n ulong to void
239for i ulong n
240ob1 get lit add o i
241end
242end
243
244fn onum v long to void
245d = new uchar 24 0
246k = ulong 0
247x = v
248while true bound 30
249put d k uchar add rem x 10 48
250k = add k 1
251x = div x 10
252if eq x 0
253break
254end
255end
256while gt k 0 bound 30
257k = sub k 1
258ob1 get d k
259end
260end
261
262fn anum v long to void
263d = new uchar 24 0
264k = ulong 0
265x = v
266while true bound 30
267put d k uchar add rem x 10 48
268k = add k 1
269x = div x 10
270if eq x 0
271break
272end
273end
274while gt k 0 bound 30
275k = sub k 1
276ab1 get d k
277end
278end
279
280fn emsg o ulong n ulong to void
281w = write 2 lit o n
282end
283
284note -------------------------------------------------------------------------- clock
285
286fn nowsec to long
287t = get gl LNOW
288if ge t 0
289ret t
290end
291ret div realns 1000000000
292end
293
294fn t2 v long to void
295put tb get gu GTL uchar add div v 10 48
296put tb add get gu GTL 1 uchar add rem v 10 48
297put gu GTL add get gu GTL 2
298end
299
300fn tch b uchar to void
301put tb get gu GTL b
302put gu GTL add get gu GTL 1
303end
304
305note Civil date from days since 1970-01-01 (proleptic Gregorian), packed y*10000 + m*100 + d.
306fn civil days long to long
307z = add days 719468
308era = div z 146097
309doe = sub z mul era 146097
310yoe = div sub add sub doe div doe 1460 div doe 36524 div doe 146096 365
311ret civil2 doe yoe add yoe mul era 400
312end
313
314fn civil2 doe long yoe long y long to long
315doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100
316mp = div add mul 5 doy 2 153
317d = add sub doy div add mul 153 mp 2 5 1
318m = add mp 3
319yy = y
320if ge mp 10
321m = sub mp 9
322yy = add y 1
323end
324ret add add mul yy 10000 mul m 100 d
325end
326
327note full: 2026-10-26T15:28:56Z (audit), else 20261026T152856Z (quarantine names).
328fn tstamp full bool to void
329t = nowsec
330put gu GTL 0
331v = civil div t 86400
332y = div v 10000
333t2 div y 100
334t2 rem y 100
335if full
336tch 45
337end
338t2 div rem v 10000 100
339if full
340tch 45
341end
342t2 rem v 100
343tch 84
344s = rem t 86400
345t2 div s 3600
346if full
347tch 58
348end
349t2 div rem s 3600 60
350if full
351tch 58
352end
353t2 rem s 60
354tch 90
355end
356
357note ----------------------------------------------------------------- drop bytes access
358
359fn at p ulong to ulong
360if ge p get gu GDN
361ret 256
362end
363ret ulong get dbuf p
364end
365
366fn ishex b ulong to bool
367ret or and ge b 48 le b 57 and ge b 97 le b 102
368end
369
370fn eqlit p ulong o ulong n ulong to bool
371for i ulong n
372if ne at add p i ulong get lit add o i
373ret false
374end
375end
376ret true
377end
378
379fn spaneq k ulong o ulong n ulong to bool
380s = get fs k
381if ne sub get fe k s n
382ret false
383end
384ret eqlit s o n
385end
386
387fn hexspan k ulong n ulong to bool
388s = get fs k
389if ne sub get fe k s n
390ret false
391end
392for i ulong n
393if not ishex at add s i
394ret false
395end
396end
397ret true
398end
399
400note ---------------------------------------------------------------- strict UTF-8 text
401
402fn isws c ulong to bool
403if or and ge c 9 le c 13 and ge c 28 le c 32
404ret true
405end
406if or eq c 133 eq c 160
407ret true
408end
409if or eq c 5760 and ge c 8192 le c 8202
410ret true
411end
412if or or eq c 8232 eq c 8233 eq c 8239
413ret true
414end
415ret or eq c 8287 eq c 12288
416end
417
418fn dmb p ulong e ulong b ulong to long
419k = ulong 0
420lo = ulong 128
421hi = ulong 191
422if and ge b 194 le b 223
423k = 1
424end
425if or eq b 224 or and ge b 225 le b 236 and ge b 238 le b 239
426k = 2
427end
428if eq b 237
429k = 2
430end
431if and ge b 240 le b 244
432k = 3
433end
434if eq k 0
435ret -1
436end
437if eq b 224
438lo = 160
439end
440if eq b 237
441hi = 159
442end
443if eq b 240
444lo = 144
445end
446if eq b 244
447hi = 143
448end
449if ge add p k e
450ret -1
451end
452cp = and b shr ulong 127 add k 1
453for j ulong k
454x = at add add p j 1
455if eq j 0
456if or lt x lo gt x hi
457ret -1
458end
459end
460if or lt x 128 gt x 191
461ret -1
462end
463cp = or shl cp 6 and x 63
464end
465put gu GADV add k 1
466ret long cp
467end
468
469fn dcp p ulong e ulong to long
470b = at p
471put gu GADV 1
472if eq b 92
473put gu GADV 2
474q = at add p 1
475if eq q 34
476ret 34
477end
478if eq q 92
479ret 92
480end
481if eq q 110
482ret 10
483end
484if eq q 116
485ret 9
486end
487ret -1
488end
489if or lt b 32 eq b 127
490ret -1
491end
492if lt b 128
493ret long b
494end
495ret dmb p e b
496end
497
498note Code points in [s, e) of dbuf, -1 if invalid; whitespace count and first/last flags in gu.
499fn chkstr s ulong e ulong to long
500p = s
501c = long 0
502ws = ulong 0
503put gu GFW 0
504put gu GLW 0
505while lt p e bound 999999
506cp = dcp p e
507if lt cp 0
508ret -1
509end
510if isws ulong cp
511ws = add ws 1
512if eq c 0
513put gu GFW 1
514end
515put gu GLW 1
516else
517put gu GLW 0
518end
519c = add c 1
520p = add p get gu GADV
521end
522put gu GWSC ws
523ret c
524end
525
526note ------------------------------------------------------------------ fixed-schema parse
527
528note Schema w: 1 introduce (kin), 2 forget (kfo), 3 forget_visitor (kfv); sorted key names.
529fn keyb w ulong i ulong to uchar
530if eq w 1
531ret get kin i
532end
533if eq w 2
534ret get kfo i
535end
536ret get kfv i
537end
538
539fn loadkeys w ulong to void
540n = len kin
541if eq w 2
542n = len kfo
543end
544if eq w 3
545n = len kfv
546end
547k = ulong 0
548put kof 0 0
549for i ulong n
550b = keyb w i
551if eq b 32
552put kln k sub i get kof k
553k = add k 1
554put kof k add i 1
555else
556put keys i b
557end
558end
559put kln k sub n get kof k
560put gu GNK add k 1
561end
562
563note Key k, written as "key": , at p. Returns the position after it or -1.
564fn pkey k ulong p ulong to long
565q = p
566if gt k 0
567if or ne at q 44 ne at add q 1 32
568ret -1
569end
570q = add q 2
571end
572if ne at q 34
573ret -1
574end
575q = add q 1
576o = get kof k
577for i ulong get kln k
578if ne at add q i ulong get keys add o i
579ret -1
580end
581end
582q = add q get kln k
583if or ne at q 34 or ne at add q 1 58 ne at add q 2 32
584ret -1
585end
586ret long add q 3
587end
588
589note Value of key k at p: the number 1 for "v", else a string. Records the raw span.
590fn pval k ulong p ulong to long
591if and eq get kln k 1 eq get keys get kof k 118
592if ne at p 49
593ret -1
594end
595put fs k p
596put fe k add p 1
597ret long add p 1
598end
599if ne at p 34
600ret -1
601end
602q = add p 1
603put fs k q
604n = get gu GDN
605while lt q n bound 999999
606b = at q
607if eq b 34
608put fe k q
609ret long add q 1
610end
611if eq b 92
612q = add q 2
613else
614q = add q 1
615end
616end
617ret -1
618end
619
620fn parse w ulong to bool
621loadkeys w
622if ne at 0 123
623ret false
624end
625p = long 1
626for k ulong get gu GNK
627p = pkey k ulong p
628if lt p 0
629ret false
630end
631p = pval k ulong p
632if lt p 0
633ret false
634end
635end
636if ne at ulong p 125
637ret false
638end
639ret eq add ulong p 1 get gu GDN
640end
641
642note ---------------------------------------------------------------------- validation
643
644fn txt k ulong cap long to bool
645c = chkstr get fs k get fe k
646if or lt c 0 gt c cap
647ret false
648end
649if gt c 0
650if or eq get gu GFW 1 eq get gu GLW 1
651ret false
652end
653end
654ret true
655end
656
657fn valtxt to bool
658if not and and txt 0 200 txt 2 100 and txt 3 39 txt 4 200
659ret false
660end
661if not and and txt 5 200 txt 8 100 and txt 9 100 txt 10 100
662ret false
663end
664ret and txt 11 200 txt 12 1000
665end
666
667fn homeok to bool
668s = get fs 4
669e = get fe 4
670if eq s e
671ret true
672end
673if not eqlit s OHTP NHTP
674ret false
675end
676c = chkstr add s NHTP e
677ret and ge c 4 eq get gu GWSC 0
678end
679
680fn idok k ulong to bool
681s = get fs k
682if ne sub get fe k s 18
683ret false
684end
685if or ne at s 99 ne at add s 1 95
686ret false
687end
688for i ulong 16
689if not ishex at add add s 2 i
690ret false
691end
692end
693ret true
694end
695
696fn dig p ulong n ulong to long
697v = long 0
698for i ulong n
699b = at add p i
700if or lt b 48 gt b 57
701ret -1
702end
703v = add mul v 10 long sub b 48
704end
705ret v
706end
707
708fn dim y long m long to long
709if eq m 2
710if and eq rem y 4 0 or ne rem y 100 0 eq rem y 400 0
711ret 29
712end
713ret 28
714end
715if or or eq m 4 eq m 6 or eq m 9 eq m 11
716ret 30
717end
718ret 31
719end
720
721fn recok k ulong to bool
722s = get fs k
723if ne sub get fe k s 20
724ret false
725end
726if or or ne at add s 4 45 ne at add s 7 45 or ne at add s 10 84 ne at add s 19 90
727ret false
728end
729if or ne at add s 13 58 ne at add s 16 58
730ret false
731end
732y = dig s 4
733m = dig add s 5 2
734d = dig add s 8 2
735if or or lt y 1 lt m 1 gt m 12
736ret false
737end
738if or lt d 1 gt d dim y m
739ret false
740end
741ret tmok s
742end
743
744fn rng v long hi long to bool
745ret and ge v 0 le v hi
746end
747
748fn tmok s ulong to bool
749if not rng dig add s 11 2 23
750ret false
751end
752if not rng dig add s 14 2 59
753ret false
754end
755ret rng dig add s 17 2 59
756end
757
758fn valint to bool
759if not spaneq 14 OINT NINT
760ret false
761end
762if not valtxt
763ret false
764end
765if not or spaneq 7 OHUM NHUM spaneq 7 OAGT NAGT
766ret false
767end
768if not or spaneq 16 OCHT NCHT spaneq 16 OAPI NAPI
769ret false
770end
771if and eq get fs 9 get fe 9 eq get fs 0 get fe 0
772ret false
773end
774if not homeok
775ret false
776end
777if ne get fs 17 get fe 17
778if not hexspan 17 64
779ret false
780end
781end
782ret and and hexspan 1 64 idok 6 recok 13
783end
784
785fn valfgt to bool
786if not spaneq 3 OFGT NFGT
787ret false
788end
789ret and and hexspan 2 64 idok 0 recok 1
790end
791
792note forget_visitor: received, type, v, vid. The vid must be 64 hex, never empty.
793fn valfv to bool
794if not spaneq 1 OFVT NFVT
795ret false
796end
797ret and hexspan 3 64 recok 0
798end
799
800note 1 introduce, 2 forget, 3 forget_visitor, 0 invalid. Sets GIDF, the id (or vid) field.
801fn classify to ulong
802if parse 1
803put gu GIDF 6
804if valint
805ret 1
806end
807ret 0
808end
809if parse 2
810put gu GIDF 0
811if valfgt
812ret 2
813end
814ret 0
815end
816if parse 3
817put gu GIDF 3
818if valfv
819ret 3
820end
821end
822ret 0
823end
824
825note ------------------------------------------------------------------------ the store
826
827fn loadst to long
828put gu GSN 0
829pset
830plit OSTO NSTO
831fd = fopen pth 0 get gu GPL 557056 0
832if eq fd neg long 2
833ret 0
834end
835if lt fd 0
836ret -1
837end
838n = ulong 0
839while true bound 999
840r = read int fd sbuf n sub SCAP n
841if lt r 0
842r = fclose int fd
843ret -1
844end
845if eq r 0
846break
847end
848n = add n ulong r
849if eq n SCAP
850r = fclose int fd
851ret -1
852end
853end
854r = fclose int fd
855put gu GSN n
856ret 0
857end
858
859fn lend s ulong to ulong
860p = s
861n = get gu GSN
862while lt p n bound 999999
863if eq get sbuf p 10
864ret p
865end
866p = add p 1
867end
868ret n
869end
870
871fn blank s ulong e ulong to bool
872for i ulong sub e s
873b = get sbuf add s i
874if not or eq b 32 and ge b 9 le b 13
875ret false
876end
877end
878ret true
879end
880
881note Is , "id": "<the drop's id>" at sbuf p ? (28 bytes, caller checks room)
882fn idat p ulong to bool
883for i ulong NIDP
884if ne get sbuf add p i get lit add OIDP i
885ret false
886end
887end
888f = get fs get gu GIDF
889q = add p NIDP
890for i ulong 18
891if ne get sbuf add q i get dbuf add f i
892ret false
893end
894end
895ret eq get sbuf add q 18 34
896end
897
898note Does store line [s, e) carry the drop's id?
899fn hasid s ulong e ulong to bool
900if lt sub e s 28
901ret false
902end
903last = sub e 28
904p = s
905while le p last bound 999999
906if idat p
907ret true
908end
909p = add p 1
910end
911ret false
912end
913
914note Does store line [s, e) end with , "vid": "<the drop's 64-hex vid>"} ?
915fn hasvid s ulong e ulong to bool
916if lt sub e s 76
917ret false
918end
919p = sub e 76
920for i ulong NVDP
921if ne get sbuf add p i get lit add OVDP i
922ret false
923end
924end
925f = get fs get gu GIDF
926q = add p NVDP
927for i ulong 64
928if ne get sbuf add q i get dbuf add f i
929ret false
930end
931end
932ret and eq get sbuf add q 64 34 eq get sbuf add q 65 125
933end
934
935note The line matches the drop: by id (GMOD 0) or by vid (GMOD 1).
936fn hit s ulong e ulong to bool
937if eq get gu GMOD 1
938ret hasvid s e
939end
940ret hasid s e
941end
942
943fn cntvid to long
944c = long 0
945s = ulong 0
946n = get gu GSN
947while lt s n bound 999999
948e = lend s
949if not blank s e
950if hasvid s e
951c = add c 1
952end
953end
954s = add e 1
955end
956ret c
957end
958
959note Start of the first store line with the drop's id, or -1. The line's end goes to gu GLE.
960fn findid to long
961s = ulong 0
962n = get gu GSN
963while lt s n bound 999999
964e = lend s
965if not blank s e
966if hasid s e
967put gu GLE e
968ret long s
969end
970end
971s = add e 1
972end
973ret -1
974end
975
976fn fsyncd o ulong n ulong to long
977pset
978plit o n
979fd = fopen pth 0 get gu GPL 540672 0
980if lt fd 0
981ret fd
982end
983r = fsync int fd
984c = fclose int fd
985ret r
986end
987
988fn appendst to long
989pset
990plit OSTO NSTO
991made = false
992fd = fopen pth 0 get gu GPL 558081 0
993if eq fd neg long 2
994fd = fopen pth 0 get gu GPL 558273 384
995made = true
996end
997if lt fd 0
998ret -1
999end
1000r = fchmod int fd 384
1001w = write int fd dbuf 0 get gu GDN
1002x = write int fd lit ONL 1
1003r = fsync int fd
1004c = fclose int fd
1005if or ne w long get gu GDN ne x 1
1006ret -1
1007end
1008if made
1009r = fsyncd ODOT NDOT
1010end
1011ret 0
1012end
1013
1014note Write the store lines without the drop's id to fd. Returns how many were left out.
1015fn wkeep fd int to long
1016gone = long 0
1017s = ulong 0
1018n = get gu GSN
1019while lt s n bound 999999
1020e = lend s
1021if not blank s e
1022if hit s e
1023gone = add gone 1
1024else
1025w = write fd sbuf s sub e s
1026w = write fd lit ONL 1
1027end
1028end
1029s = add e 1
1030end
1031ret gone
1032end
1033
1034note Rename the tmp file (the first tl bytes of pth) over contacts.jsonl, then sync the dir.
1035fn rnstore tl ulong to long
1036put pk 0 2
1037put pk 1 0
1038put pk 2 uchar tl
1039put pk 3 0
1040for i ulong tl
1041put pk add 4 i get pth i
1042end
1043o = add 4 tl
1044put pk o uchar NSTO
1045put pk add o 1 0
1046for i ulong NSTO
1047put pk add add o 2 i get lit add OSTO i
1048end
1049r = renames pk 0 add add o 2 NSTO 0
1050if ne r 0
1051ret -1
1052end
1053ret fsyncd ODOT NDOT
1054end
1055
1056note Rewrite the store without the lines carrying the drop's id. Returns how many went.
1057fn rewrite to long
1058pset
1059plit OTMP NTMP
1060if lt phex 8 0
1061ret -1
1062end
1063tl = get gu GPL
1064fd = fopen pth 0 tl 524481 384
1065if lt fd 0
1066ret -1
1067end
1068gone = wkeep int fd
1069r = fsync int fd
1070c = fclose int fd
1071if lt rnstore tl 0
1072ret -1
1073end
1074ret gone
1075end
1076
1077note Tombstone line: stamp, event (0 forget, 1 forget_visitor), count. No id, no field.
1078fn audit gone long ev ulong to long
1079put gu GAL 0
1080tstamp true
1081for i ulong get gu GTL
1082ab1 get tb i
1083end
1084if eq ev 0
1085for i ulong NFRM
1086ab1 get lit add OFRM i
1087end
1088else
1089for i ulong NFVR
1090ab1 get lit add OFVR i
1091end
1092end
1093anum gone
1094ab1 10
1095pset
1096plit OAUD NAUD
1097fd = fopen pth 0 get gu GPL 558145 384
1098if lt fd 0
1099ret -1
1100end
1101w = write int fd ab 0 get gu GAL
1102r = fsync int fd
1103c = fclose int fd
1104ret 0
1105end
1106
1107note ------------------------------------------------------------------------ quarantine
1108
1109fn reclen p ulong to ulong
1110ret add ulong get dents add p 16 shl ulong get dents add p 17 8
1111end
1112
1113note Is the getdents64 record at p named . or .. ?
1114fn isdot p ulong to bool
1115b0 = get dents add p 19
1116b1 = get dents add p 20
1117b2 = get dents add p 21
1118ret and eq b0 46 or eq b1 0 and eq b1 46 eq b2 0
1119end
1120
1121fn qcount to long
1122pset
1123plit OQDR NQDR
1124fd = fopen pth 0 get gu GPL 540672 0
1125if lt fd 0
1126ret -1
1127end
1128c = long 0
1129while true bound 99999
1130r = getdnts int fd dents 0 len dents
1131if le r 0
1132break
1133end
1134p = ulong 0
1135while lt p ulong r bound 99999
1136if not isdot p
1137c = add c 1
1138end
1139p = add p reclen p
1140end
1141end
1142r = fclose int fd
1143ret c
1144end
1145
1146fn quar to long
1147pset
1148plit OQDR NQDR
1149r = makedir pth 0 get gu GPL 448
1150if and ne r 0 ne r neg long 17
1151ret -1
1152end
1153if ne plstat 0
1154ret -1
1155end
1156mode = rd32 16
1157if or ne and mode 61440 16384 or ne rd32 24 ulong getuid ne and mode 63 0
1158ret -1
1159end
1160c = qcount
1161if lt c 0
1162ret -1
1163end
1164if ge c QCAP
1165bump CQF
1166ret 0
1167end
1168pset
1169plit OQDR NQDR
1170plit OSLS NSLS
1171tstamp false
1172for i ulong get gu GTL
1173pb get tb i
1174end
1175pb 45
1176if lt phex 4 0
1177ret -1
1178end
1179plit OJSN NJSN
1180fd = fopen pth 0 get gu GPL 557249 384
1181if lt fd 0
1182ret -1
1183end
1184n = get gu GDN
1185if gt n MAXD
1186n = MAXD
1187end
1188w = write int fd dbuf 0 n
1189r = fsync int fd
1190c = fclose int fd
1191r = fsyncd OQDR NQDR
1192ret 0
1193end
1194
1195note ------------------------------------------------------------------------ the drops
1196
1197note Remove the entry at pth without following it: unlink, else rmdir. key counts a removal.
1198fn rment key ulong to void
1199r = unlink pth 0 get gu GPL
1200if eq r neg long 2
1201ret
1202end
1203if ne r 0
1204r = rmdir pth 0 get gu GPL
1205if ne r 0
1206bump CST
1207ret
1208end
1209end
1210bump key
1211end
1212
1213fn entpath i ulong to void
1214pset
1215pdd
1216plit OSLS NSLS
1217pnam i
1218end
1219
1220fn namok i ulong to bool
1221if ne get nlen i 21
1222ret false
1223end
1224b = mul i 256
1225for j ulong 16
1226if not ishex ulong get nbuf add b j
1227ret false
1228end
1229end
1230for j ulong NJSN
1231if ne get nbuf add add b 16 j get lit add OJSN j
1232ret false
1233end
1234end
1235ret true
1236end
1237
1238note Read the open drop into dbuf (at most MAXR bytes). Returns 1 regular, 0 not.
1239fn slurp fd int to ulong
1240r = fstat fd stb 0 128
1241if ne r 0
1242ret 0
1243end
1244if or ne and rd32 16 61440 32768 ne rd32 20 1
1245ret 0
1246end
1247n = ulong 0
1248while lt n MAXR bound 99999
1249r = read fd dbuf n sub MAXR n
1250if le r 0
1251break
1252end
1253n = add n ulong r
1254end
1255put gu GDN n
1256ret 1
1257end
1258
1259fn dointro to long
1260if ne loadst 0
1261ret -1
1262end
1263f = findid
1264if lt f 0
1265if ne appendst 0
1266ret -1
1267end
1268bump CIS
1269ret 0
1270end
1271s = ulong f
1272e = get gu GLE
1273same = eq sub e s get gu GDN
1274if same
1275for i ulong sub e s
1276if ne get sbuf add s i get dbuf i
1277same = false
1278break
1279end
1280end
1281end
1282if same
1283bump CIA
1284ret 0
1285end
1286ret CQD
1287end
1288
1289fn shaat p ulong to bool
1290for i ulong NSHP
1291if ne get sbuf add p i get lit add OSHP i
1292ret false
1293end
1294end
1295ret true
1296end
1297
1298note Position of the 64-hex delete_token_sha256 value in the store line found at f, or 0.
1299fn shapos f long to ulong
1300if lt f 0
1301ret 0
1302end
1303p = ulong f
1304e = get gu GLE
1305while le add add p NSHP 65 e bound 999999
1306if shaat p
1307h = add p NSHP
1308for i ulong 64
1309if not ishex ulong get sbuf add h i
1310ret 0
1311end
1312end
1313if ne get sbuf add h 64 34
1314ret 0
1315end
1316ret h
1317end
1318p = add p 1
1319end
1320ret 0
1321end
1322
1323note Constant-time compare of the forget token with the target's hash (zeros when none).
1324fn tokeq f long to bool
1325t = get fs 2
1326h = shapos f
1327acc = ulong 0
1328for i ulong 64
1329x = ulong 48
1330if ne h 0
1331x = ulong get sbuf add h i
1332end
1333acc = or acc xor x ulong get dbuf add t i
1334end
1335ret eq acc 0
1336end
1337
1338fn doforget to long
1339if ne loadst 0
1340ret -1
1341end
1342f = findid
1343m = tokeq f
1344if and m ge f 0
1345put gu GMOD 0
1346g = rewrite
1347if lt g 0
1348ret -1
1349end
1350if ne audit g 0 0
1351ret -1
1352end
1353bump CFM
1354ret 0
1355end
1356bump CFU
1357ret 0
1358end
1359
1360note Every record with the drop's vid goes; none of them, or an unchanged store, if none match.
1361fn dofv to long
1362if ne loadst 0
1363ret -1
1364end
1365if eq cntvid 0
1366bump CVU
1367ret 0
1368end
1369put gu GMOD 1
1370g = rewrite
1371if lt g 0
1372ret -1
1373end
1374if ne audit g 1 0
1375ret -1
1376end
1377bump CVM
1378ret 0
1379end
1380
1381note Process drop i. Returns 0, or -1 on a store failure (fatal, the drop stays).
1382fn procent i ulong to long
1383entpath i
1384fd = fopen pth 0 get gu GPL 559104 0
1385if eq fd neg long 2
1386ret 0
1387end
1388if lt fd 0
1389rment CRN
1390ret 0
1391end
1392reg = slurp int fd
1393r = fclose int fd
1394if eq reg 0
1395rment CRN
1396ret 0
1397end
1398why = long 0
1399if not namok i
1400why = CQN
1401end
1402if and eq why 0 gt get gu GDN MAXD
1403why = CQO
1404end
1405if eq why 0
1406k = classify
1407if eq k 0
1408why = CQI
1409end
1410if eq k 1
1411why = dointro
1412end
1413if eq k 2
1414why = doforget
1415end
1416if eq k 3
1417why = dofv
1418end
1419end
1420if lt why 0
1421ret -1
1422end
1423if gt why 0
1424if ne quar 0
1425ret -2
1426end
1427bump CQ
1428bump ulong why
1429end
1430entpath i
1431r = unlink pth 0 get gu GPL
1432ret 0
1433end
1434
1435note -------------------------------------------------------------------------- scanning
1436
1437fn addent p ulong to void
1438n = get gu GNE
1439nl = ulong 0
1440while ne get dents add add p 19 nl 0 bound 300
1441nl = add nl 1
1442end
1443b = mul n 256
1444for j ulong nl
1445put nbuf add b j get dents add add p 19 j
1446end
1447put nlen n nl
1448entpath n
1449r = plstat
1450if ne r 0
1451ret
1452end
1453put nmt n mtns
1454put ordx n n
1455put gu GNE add n 1
1456end
1457
1458fn scan to long
1459pset
1460pdd
1461fd = fopen pth 0 get gu GPL 540672 0
1462if lt fd 0
1463ret -1
1464end
1465while lt get gu GNE MAXE bound 99999
1466r = getdnts int fd dents 0 len dents
1467if le r 0
1468break
1469end
1470p = ulong 0
1471while and lt p ulong r lt get gu GNE MAXE bound 99999
1472if not isdot p
1473addent p
1474end
1475p = add p reclen p
1476end
1477end
1478r = fclose int fd
1479ret 0
1480end
1481
1482fn lessent a ulong b ulong to bool
1483if ne get nmt a get nmt b
1484ret lt get nmt a get nmt b
1485end
1486la = get nlen a
1487lb = get nlen b
1488oa = mul a 256
1489o2 = mul b 256
1490for i ulong 256
1491if or ge i la ge i lb
1492ret lt la lb
1493end
1494x = get nbuf add oa i
1495y = get nbuf add o2 i
1496if ne x y
1497ret lt x y
1498end
1499end
1500ret false
1501end
1502
1503fn sortent to void
1504n = get gu GNE
1505for i ulong n
1506j = i
1507while gt j 0 bound 5000
1508a = get ordx sub j 1
1509b = get ordx j
1510if not lessent b a
1511break
1512end
1513put ordx sub j 1 b
1514put ordx j a
1515j = sub j 1
1516end
1517end
1518end
1519
1520note ----------------------------------------------------------------- lock and directories
1521
1522fn dirok which ulong mask ulong to bool
1523pset
1524if eq which 0
1525pdd
1526else
1527psd
1528end
1529if ne plstat 0
1530ret false
1531end
1532mode = rd32 16
1533if ne and mode 61440 16384
1534ret false
1535end
1536if ne rd32 24 ulong getuid
1537ret false
1538end
1539ret eq and and mode 63 mask 0
1540end
1541
1542fn lockst to long
1543deadln = add monons 10000000000
1544while true bound 999999
1545pset
1546plit OLCK NLCK
1547r = makedir pth 0 get gu GPL 448
1548if eq r 0
1549put gu GLK 1
1550ret 0
1551end
1552if ne r neg long 17
1553ret -1
1554end
1555if eq plstat 0
1556if gt sub realns mtns 300000000000
1557r = rmdir pth 0 get gu GPL
1558continue
1559end
1560end
1561if ge monons deadln
1562ret -1
1563end
1564r = sleepns 50000000
1565end
1566ret -1
1567end
1568
1569fn unlock to void
1570if eq get gu GLK 1
1571pset
1572plit OLCK NLCK
1573r = rmdir pth 0 get gu GPL
1574put gu GLK 0
1575end
1576end
1577
1578note ---------------------------------------------------------------------- arguments
1579
1580fn argcp i ulong which ulong to long
1581n = long 0
1582if eq which 0
1583n = argread i ddir 0 4095
1584put gu GDL ulong n
1585else
1586n = argread i sdir 0 4095
1587put gu GSL ulong n
1588end
1589ret n
1590end
1591
1592fn args to long
1593c = argcnt
1594if and ne c 2 ne c 5
1595ret -1
1596end
1597n = argread 1 tb 0 len tb
1598if ne n long NING
1599ret -1
1600end
1601for i ulong NING
1602if ne get tb i get lit add OING i
1603ret -1
1604end
1605end
1606put gl LNOW -1
1607if eq c 2
1608for i ulong NDDR
1609put ddir i get lit add ODDR i
1610end
1611put gu GDL NDDR
1612for i ulong NSDR
1613put sdir i get lit add OSDR i
1614end
1615put gu GSL NSDR
1616ret 0
1617end
1618if le argcp 2 0 0
1619ret -1
1620end
1621if le argcp 3 1 0
1622ret -1
1623end
1624n = argread 4 tb 0 len tb
1625if le n 0
1626ret -1
1627end
1628t = long 0
1629for i ulong ulong n
1630b = get tb i
1631if or lt b 48 gt b 57
1632ret -1
1633end
1634t = add mul t 10 long sub b 48
1635end
1636put gl LNOW t
1637ret 0
1638end
1639
1640note -------------------------------------------------------------------------- summary
1641
1642fn summary to void
1643put gu GOL 0
1644olit OHDR NHDR
1645any = false
1646k = ulong 0
1647s = ulong 0
1648for i ulong add len cn 1
1649e = eq i len cn
1650if not e
1651e = eq get cn i 32
1652end
1653if e
1654if gt get cnt k 0
1655any = true
1656ob1 32
1657for j ulong sub i s
1658ob1 get cn add s j
1659end
1660ob1 61
1661onum get cnt k
1662end
1663k = add k 1
1664s = add i 1
1665end
1666end
1667if not any
1668olit ONTH NNTH
1669end
1670ob1 10
1671w = write 1 ob 0 get gu GOL
1672end
1673
1674fn runall to long
1675n = get gu GNE
1676for k ulong n
1677i = get ordx k
1678if eq get nbuf mul i 256 46
1679entpath i
1680if ne plstat 0
1681continue
1682end
1683if gt sub realns mtns 60000000000
1684rment CTS
1685else
1686bump CTP
1687end
1688continue
1689end
1690r = procent i
1691if ne r 0
1692ret r
1693end
1694end
1695ret 0
1696end
1697
1698fn main to int
1699if ne args 0
1700emsg OUSE NUSE
1701ret 64
1702end
1703if not dirok 0 39
1704emsg OEDR NEDR
1705ret 1
1706end
1707if not dirok 1 63
1708emsg OEDR NEDR
1709ret 1
1710end
1711pset
1712psd
1713if ne chdir pth 0 get gu GPL 0
1714emsg OEDR NEDR
1715ret 1
1716end
1717if ne scan 0
1718emsg OEDR NEDR
1719ret 1
1720end
1721if ne lockst 0
1722emsg OELK NELK
1723ret 1
1724end
1725sortent
1726r = runall
1727unlock
1728if eq r -2
1729emsg OEQT NEQT
1730ret 1
1731end
1732if ne r 0
1733emsg OEST NEST
1734ret 1
1735end
1736summary
1737ret 0
1738end