interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / apps / interplane-forge / tools / bring / src / 20walk.om

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