interplane.Get Omega
Read in your language

hop

Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.

Brought by drakestapleton

branch main Branches Tags History

hop / Linux / files.om

1714 lines · 28389 bytes Raw Blame History
1note Hop Linux file-list foundation. SPDX-License-Identifier: Apache-2.0
2note No timer or scan runs until explicitly requested. Symlink directories are never traversed.
3note File format HOPFIND1:64-byte header, then NUL-terminated absolute pathname byte strings.
4note CLI JSON follows RFC8259 section7 and UTF-8 validation RFC3629 sections3/4.
5note Invalid filesystem UTF-8 bytes use the reversible surrogateescape convention U+DC80..U+DCFF;
6note agents must retain these escapes when round-tripping such names. Ordinary Unicode is encoded normally.
7note Header0..7 magic; u64 little-endian at8 record count,16 partial-warning count,24 body bytes,
8note 32 attempted-directory count;40..63 reserved zero. A newline is an ordinary pathname byte.
9note Linux AArch64 stat and fcntl layouts are those of installed kernel UAPI headers.
10note Directory flags O_NOFOLLOW|O_DIRECTORY=49152 calc:a636669bf8335bbaf4882cbbea217a1bbb71e5037a035961710684bb41f1597c.
11note Temp flags O_NOFOLLOW|O_CREAT|O_EXCL|O_WRONLY=32961 calc:a20ca9614e5701d380af7d625ecc52fc9d081fc5f1c2dbce397542e1bb81313a.
12note Lock flags O_NOFOLLOW|O_CREAT|O_NONBLOCK|O_RDWR=34882 calc:55d3315f96faa89f2851ea6d22b07a20adda0081bf621917b0eca71661a94ceb.
13note Fixed4096 pathname buffers reflect the Linux pathname syscall boundary; directories and records grow.
14note All cache operations occur relative to an open cache directory, then the original working directory is restored.
15note This avoids imposing the compiler's4096-byte packed-rename boundary on a pair of long absolute paths.
16global ff_path uchar 4096
17global ff_cache uchar 4096
18global ff_home uchar 4096
19global ff_cwd uchar 4096
20global ff_tmp uchar 96
21global ff_hdr uchar 64
22global ff_buf uchar 65536
23global ff_stat uchar 128
24global ff_arg uchar 131072
25global ff_rename uchar 128
26global ff_nul uchar 1
27global ff_out uchar 4096
28global ff_on ulong 1
29global ff_queue uchar *
30global ff_qmeta ulong *
31global ff_seen ulong *
32global ff_oldseen ulong *
33global ff_data uchar *
34global ff_hits ulong *
35global ff_terms uchar *
36global ff_s ulong 32
37global ff_fd int 4
38note ff_s fields:0 path bytes,1 cache bytes,2 temp-name bytes,3 records,4 partial warnings,
39note 5 body bytes,6 attempted directories,7 queued dirs,8 queue bytes,9 seen count,10 seen slots,
40note 11 alarm threshold,12 alarm emitted,13 error,14 UI mode,15 loaded bytes,16 hits,
41note 17 term bytes,18 home bytes,19 cwd bytes,20 temp exists,21 leading status rows.
42text ff_magic "HOPFIND1"
43text ff_index "files.idx"
44text ff_lock "files.lock"
45text ff_tempstart ".files."
46text ff_dot "."
47text ff_reindexarg "--reindex"
48text ff_tmpend ".tmp"
49text ff_homekey "HOME="
50text ff_cachekey "XDG_CACHE_HOME="
51text ff_cachetail "/.cache"
52text ff_hoptail "/hop"
53text ff_hive "/work/hive"
54text ff_lanes "/work/lanes"
55text ff_alarmkey "HOP_INDEX_ALARM_ENTRIES="
56text ff_node "node_modules"
57text ff_target "target"
58text ff_build "build"
59text ff_dist "dist"
60text ff_py "__pycache__"
61text ff_venv "venv"
62text ff_dotvenv ".venv"
63text ff_site "site-packages"
64text ff_slash "/"
65text ff_partialmsg "Hop kept the file list it could read. Some folders were unavailable; coverage is partial.\n"
66text ff_failmsg "Hop could not replace its file list. The previous list has been kept.\n"
67text ff_cachemsg "Hop needs an absolute private cache folder and a valid home folder or explicit root.\n"
68text ff_busymsg "Hop's file list is already being built. The current list is unchanged.\n"
69text ff_badmsg "Hop's file list is damaged. Rebuild it before searching.\n"
70text ff_missingmsg "Hop has no file list yet. Use --reindex or choose Build file list.\n"
71text ff_memmsg "Hop could not grow its file-list memory.\n"
72text ff_alarmmsg "ALARM Hop file-list entries "
73text ff_alarmmid " crossed HOP_INDEX_ALARM_ENTRIES="
74text ff_alarmend "; indexing continues.\n"
75text ff_alarmbad "Hop: invalid HOP_INDEX_ALARM_ENTRIES; using the default alarm threshold.\n"
76text ff_buildrow "Build file list\n"
77text ff_rebuildrow "File list is damaged. Build it again\n"
78text ff_warnrow "Some folders could not be read. Results below are partial\n"
79text ff_emptyrow "No files match that search\n"
80text ff_hex "0123456789abcdef"
81
82fn ff_message message view uchar * to void
83k = write 2 message 0 len message
84end
85
86fn ff_reserve which ulong need ulong to bool
87r = long 0
88if eq which 0
89if gt need len ff_queue
90r = enlarge ff_queue add need add len ff_queue 4096
91end
92end
93if eq which 1
94if gt need len ff_qmeta
95r = enlarge ff_qmeta add need add len ff_qmeta 64
96end
97end
98if eq which 2
99r = enlarge ff_seen need
100end
101if eq which 3
102r = enlarge ff_oldseen need
103end
104if eq which 4
105if gt need len ff_data
106r = enlarge ff_data add need add len ff_data 65536
107end
108end
109if eq which 5
110if gt need len ff_hits
111r = enlarge ff_hits add need add len ff_hits 128
112end
113end
114if eq which 6
115if gt need len ff_terms
116r = enlarge ff_terms add need add len ff_terms 128
117end
118end
119if lt r 0
120put ff_s 13 1
121ff_message ff_memmsg
122ret false
123end
124ret true
125end
126
127fn ff_pathset source view uchar * to bool
128if ge len source len ff_path
129ret false
130end
131for i ulong len source bound 9223372036854775807
132c = get source i
133put ff_path i c
134end
135put ff_s 0 len source
136ret true
137end
138
139fn ff_pathadd source view uchar * to bool
140p = get ff_s 0
141if ge add p len source len ff_path
142ret false
143end
144for i ulong len source bound 9223372036854775807
145c = get source i
146put ff_path add p i c
147end
148put ff_s 0 add p len source
149ret true
150end
151
152fn ff_envpath n ulong to bool
153if ge n len ff_path
154ret false
155end
156for i ulong n bound 9223372036854775807
157c = get ha_env i
158put ff_path i c
159end
160put ff_s 0 n
161ret true
162end
163
164fn ff_normalize to bool
165n = get ff_s 0
166if eq n 0
167ret false
168end
169if ne get ff_path 0 47
170ret false
171end
172r = ulong 1
173w = ulong 1
174while lt r n bound 4096
175while lt r n bound 4096
176if ne get ff_path r 47
177break
178end
179r = add r 1
180end
181start = r
182while lt r n bound 4096
183if eq get ff_path r 47
184break
185end
186r = add r 1
187end
188size = sub r start
189if eq size 1
190if eq get ff_path start 46
191size = 0
192end
193end
194if eq size 2
195if eq get ff_path start 46
196if eq get ff_path add start 1 46
197if gt w 1
198w = sub w 1
199while gt w 1 bound 4096
200if eq get ff_path sub w 1 47
201break
202end
203w = sub w 1
204end
205if gt w 1
206w = sub w 1
207end
208end
209size = 0
210end
211end
212end
213if gt size 0
214if gt w 1
215put ff_path w 47
216w = add w 1
217end
218ff_pathmove start w size
219w = add w size
220end
221end
222put ff_s 0 w
223ret true
224end
225
226fn ff_pathmove source ulong dest ulong size ulong to void
227for i ulong size bound 4096
228c = get ff_path add source i
229put ff_path add dest i c
230end
231end
232
233fn ff_statword offset ulong size ulong to ulong
234v = ulong 0
235for i ulong size bound 8
236c = ulong get ff_stat add offset i
237v = or v shl c mul i 8
238end
239ret v
240end
241
242fn ff_mode to ulong
243ret and ff_statword 16 4 61440
244end
245
246fn ff_getstat follow bool to bool
247r = long 0
248if follow
249r = stat ff_path 0 get ff_s 0 ff_stat 0 len ff_stat
250else
251r = lstat ff_path 0 get ff_s 0 ff_stat 0 len ff_stat
252end
253ret ge r 0
254end
255
256fn ff_setup create bool to bool
257for i ulong 4
258put ff_fd i -1
259end
260put ff_s 20 0
261put ff_s 18 0
262n = ha_envget ff_homekey
263if gt n 0
264if eq get ha_env 0 47
265if ff_envpath ulong n
266if ff_normalize
267ff_savehome
268end
269end
270end
271end
272n = cwd ff_cwd 0 len ff_cwd
273if le n 0
274ret false
275end
276put ff_s 19 sub ulong n 1
277n = ha_envget ff_cachekey
278valid = false
279if gt n 0
280valid = eq get ha_env 0 47
281end
282if valid
283if not ff_envpath ulong n
284ret false
285end
286else
287if eq get ff_s 18 0
288ret false
289end
290ff_homepath
291if not ff_pathadd ff_cachetail
292ret false
293end
294end
295if not ff_pathadd ff_hoptail
296ret false
297end
298if not ff_normalize
299ret false
300end
301ff_savecache
302if create
303if not ff_mkdirs
304ret false
305end
306end
307ret true
308end
309
310fn ff_savehome to void
311n = get ff_s 0
312for i ulong n bound 4096
313c = get ff_path i
314put ff_home i c
315end
316put ff_s 18 n
317end
318
319fn ff_homepath to void
320n = get ff_s 18
321for i ulong n bound 4096
322c = get ff_home i
323put ff_path i c
324end
325put ff_s 0 n
326end
327
328fn ff_savecache to void
329n = get ff_s 0
330for i ulong n bound 4096
331c = get ff_path i
332put ff_cache i c
333end
334put ff_s 1 n
335end
336
337fn ff_cachepath to void
338n = get ff_s 1
339for i ulong n bound 4096
340c = get ff_cache i
341put ff_path i c
342end
343put ff_s 0 n
344end
345
346fn ff_mkdirs to bool
347n = get ff_s 0
348for i ulong add n 1 bound 4097
349sep = eq i n
350if not sep
351sep = eq get ff_path i 47
352end
353if and sep gt i 0
354r = makedir ff_path 0 i 448
355if and lt r 0 ne r -17
356ret false
357end
358end
359end
360if not ff_getstat false
361ret false
362end
363if ne ff_mode 16384
364ret false
365end
366if ne ff_statword 24 4 ulong getuid
367ret false
368end
369ret eq and ff_statword 16 4 63 0
370end
371
372fn ff_enter to bool
373old = int fopen ff_dot 0 len ff_dot 0 0
374if lt old 0
375ret false
376end
377put ff_fd 0 old
378ff_cachepath
379cache = int fopen ff_path 0 get ff_s 0 49152 0
380if lt cache 0
381put ff_s 24 ulong neg long cache
382ret false
383end
384put ff_fd 1 cache
385ret eq fchdir cache 0
386end
387
388fn ff_cleanup to void
389if ge get ff_fd 3 0
390k = fclose get ff_fd 3
391put ff_fd 3 -1
392end
393if ne get ff_s 20 0
394k = unlink ff_tmp 0 get ff_s 2
395put ff_s 20 0
396end
397if ge get ff_fd 2 0
398k = fclose get ff_fd 2
399put ff_fd 2 -1
400end
401if ge get ff_fd 0 0
402k = fchdir get ff_fd 0
403if ne k 0
404put ff_s 13 1
405end
406k = fclose get ff_fd 0
407put ff_fd 0 -1
408end
409if ge get ff_fd 1 0
410k = fclose get ff_fd 1
411put ff_fd 1 -1
412end
413end
414
415fn ff_temptext source view uchar * to void
416p = get ff_s 2
417for i ulong len source bound 96
418c = get source i
419put ff_tmp add p i c
420end
421put ff_s 2 add p len source
422end
423
424fn ff_tempnum value ulong to void
425digits = new uchar 20 0
426n = ulong 0
427v = value
428while true bound 20
429put digits n uchar add 48 rem v 10
430n = add n 1
431v = div v 10
432if eq v 0
433break
434end
435end
436p = get ff_s 2
437for i ulong n bound 20
438put ff_tmp add p i get digits sub sub n i 1
439end
440put ff_s 2 add p n
441end
442
443fn ff_startoutput to int
444lockfd = int fopen ff_lock 0 len ff_lock 34882 384
445if lt lockfd 0
446ret 1
447end
448put ff_fd 2 lockfd
449k = fstat lockfd ff_stat 0 len ff_stat
450if lt k 0
451ret 1
452end
453if ne ff_mode 32768
454ret 1
455end
456k = flock lockfd 6
457if eq k -11
458ret 3
459end
460if ne k 0
461ret 1
462end
463put ff_s 2 0
464ff_temptext ff_tempstart
465ff_tempnum ulong getpid
466ff_temptext ff_dot
467ff_tempnum ulong monons
468ff_temptext ff_tmpend
469fd = int fopen ff_tmp 0 get ff_s 2 32961 384
470if lt fd 0
471ret 1
472end
473put ff_s 20 1
474put ff_fd 3 fd
475ff_header
476ret ff_writeheader
477end
478
479fn ff_headernum offset ulong value ulong to void
480for i ulong 8
481c = uchar and shr value mul i 8 255
482put ff_hdr add offset i c
483end
484end
485
486fn ff_header to void
487for i ulong len ff_hdr
488put ff_hdr i 0
489end
490for i ulong len ff_magic
491put ff_hdr i get ff_magic i
492end
493ff_headernum 8 get ff_s 3
494ff_headernum 16 get ff_s 4
495ff_headernum 24 get ff_s 5
496ff_headernum 32 get ff_s 6
497end
498
499fn ff_writeheader to int
500p = ulong 0
501while lt p len ff_hdr bound 64
502k = write get ff_fd 3 ff_hdr p sub len ff_hdr p
503if eq k -4
504continue
505end
506if le k 0
507put ff_s 13 1
508ret 1
509end
510p = add p ulong k
511end
512ret 0
513end
514
515fn ff_dataword offset ulong to ulong
516v = ulong 0
517for i ulong 8
518c = ulong get ff_data add offset i
519v = or v shl c mul i 8
520end
521ret v
522end
523
524fn ff_load to int
525put ff_s 13 0
526put ff_s 15 0
527put ff_s 16 0
528put ff_s 4 0
529put ff_s 24 0
530if not ff_setup false
531ret 3
532end
533if not ff_enter
534ff_cleanup
535if eq get ff_s 24 2
536ret 1
537end
538ret 3
539end
540fd = int fopen ff_index 0 len ff_index 34816 0
541if lt fd 0
542ff_cleanup
543if eq fd -2
544ret 1
545end
546ret 3
547end
548put ff_fd 3 fd
549k = fstat fd ff_stat 0 len ff_stat
550if or lt k 0 ne ff_mode 32768
551ff_cleanup
552ret 2
553end
554while true bound 9223372036854775807
555n = read fd ff_buf 0 len ff_buf
556if eq n -4
557continue
558end
559if lt n 0
560ff_cleanup
561ret 3
562end
563if eq n 0
564break
565end
566if not ff_loadpart ulong n
567ff_cleanup
568ret 3
569end
570end
571ff_cleanup
572if ne get ff_s 13 0
573ret 3
574end
575if not ff_validate
576ret 2
577end
578ret 0
579end
580
581fn ff_loadpart size ulong to bool
582p = get ff_s 15
583if not ff_reserve 4 add p size
584ret false
585end
586for i ulong size bound 65536
587c = get ff_buf i
588put ff_data add p i c
589end
590put ff_s 15 add p size
591ret true
592end
593
594fn ff_validate to bool
595n = get ff_s 15
596if lt n 64
597ret false
598end
599for i ulong len ff_magic
600if ne get ff_data i get ff_magic i
601ret false
602end
603end
604for i ulong 24
605if ne get ff_data add i 40 0
606ret false
607end
608end
609if ne ff_dataword 24 sub n 64
610ret false
611end
612p = ulong 64
613count = ulong 0
614while lt p n bound 9223372036854775807
615if ne get ff_data p 47
616ret false
617end
618start = p
619while lt p n bound 4096
620if eq get ff_data p 0
621break
622end
623p = add p 1
624if ge sub p start 4096
625ret false
626end
627end
628if eq p n
629ret false
630end
631count = add count 1
632p = add p 1
633end
634if ne count ff_dataword 8
635ret false
636end
637put ff_s 3 count
638put ff_s 4 ff_dataword 16
639put ff_s 5 ff_dataword 24
640put ff_s 6 ff_dataword 32
641ret true
642end
643
644fn ff_termwhite c uchar to bool
645ret or eq c 32 and ge c 9 le c 13
646end
647
648fn ff_pathhas start ulong size ulong term ulong termsize ulong to bool
649if gt termsize size
650ret false
651end
652for i ulong add sub size termsize 1 bound 4096
653if ff_pathterm add start i term termsize
654ret true
655end
656end
657ret false
658end
659
660fn ff_pathterm start ulong term ulong size ulong to bool
661for j ulong size bound 9223372036854775807
662a = ha_lower get ff_data add start j
663b = ha_lower get ff_terms add term j
664if ne a b
665ret false
666end
667end
668ret true
669end
670
671fn ff_match start ulong size ulong to bool
672n = get ff_s 17
673p = ulong 0
674while lt p n bound 9223372036854775807
675while lt p n bound 9223372036854775807
676if not ff_termwhite get ff_terms p
677break
678end
679p = add p 1
680end
681lo = p
682while lt p n bound 9223372036854775807
683if ff_termwhite get ff_terms p
684break
685end
686p = add p 1
687end
688if gt p lo
689if not ff_pathhas start size lo sub p lo
690ret false
691end
692end
693end
694ret true
695end
696
697fn ff_search to bool
698put ff_s 16 0
699p = ulong 64
700n = get ff_s 15
701while lt p n bound 9223372036854775807
702start = p
703while ne get ff_data p 0 bound 4096
704p = add p 1
705end
706if ff_match start sub p start
707k = get ff_s 16
708if not ff_reserve 5 mul add k 1 2
709ret false
710end
711put ff_hits mul k 2 start
712put ff_hits add mul k 2 1 sub p start
713put ff_s 16 add k 1
714end
715p = add p 1
716end
717ret true
718end
719
720fn ff_rowbyte c uchar to void
721p = get lx_rn 0
722if eq p len lx_rows
723r = enlarge lx_rows add add p p 128
724if lt r 0
725put ff_s 13 1
726ret
727end
728end
729put lx_rows p c
730put lx_rn 0 add p 1
731end
732
733fn ff_rowtext value view uchar * to void
734for i ulong len value bound 4096
735ff_rowbyte get value i
736end
737end
738
739fn ff_rowpath start ulong size ulong to void
740for i ulong size bound 4096
741c = get ff_data add start i
742if eq c 10
743ff_rowbyte 92
744ff_rowbyte 110
745else
746if eq c 13
747ff_rowbyte 92
748ff_rowbyte 114
749else
750if eq c 9
751ff_rowbyte 92
752ff_rowbyte 116
753else
754if eq c 92
755ff_rowbyte 92
756ff_rowbyte 92
757else
758if or lt c 32 eq c 127
759ff_rowbyte 92
760ff_rowbyte 120
761ff_rowbyte get ff_hex shr c 4
762ff_rowbyte get ff_hex and c 15
763else
764ff_rowbyte c
765end
766end
767end
768end
769end
770end
771ff_rowbyte 10
772end
773
774fn ff_queryterms start ulong size ulong to bool
775if not ff_reserve 6 size
776ret false
777end
778for i ulong size bound 9223372036854775807
779c = get lx_query add start i
780put ff_terms i c
781end
782put ff_s 17 size
783ret true
784end
785
786fn ff_isfind to bool
787n = get lx_qn 0
788if lt n 4
789ret false
790end
791if ne ha_lower get lx_query 0 102
792ret false
793end
794if ne ha_lower get lx_query 1 105
795ret false
796end
797if ne ha_lower get lx_query 2 110
798ret false
799end
800if ne ha_lower get lx_query 3 100
801ret false
802end
803if eq n 4
804ret true
805end
806ret eq get lx_query 4 32
807end
808
809fn ff_query to int
810if not ff_isfind
811ret 0
812end
813start = ulong 4
814if gt get lx_qn 0 4
815start = 5
816end
817put lx_rn 0 0
818put lx_pick 0 0
819put ff_s 14 3
820put ff_s 21 0
821if not ff_queryterms start sub get lx_qn 0 start
822ret 1
823end
824loaded = ff_load
825if ne loaded 0
826if eq loaded 1
827put ff_s 14 1
828ff_rowtext ff_buildrow
829else
830if eq loaded 2
831put ff_s 14 1
832ff_rowtext ff_rebuildrow
833else
834ff_rowtext ff_failmsg
835end
836end
837ret 1
838end
839if not ff_search
840ff_rowtext ff_memmsg
841ret 1
842end
843if ne get ff_s 4 0
844ff_rowtext ff_warnrow
845put ff_s 21 1
846end
847if eq get ff_s 16 0
848ff_rowtext ff_emptyrow
849ret 1
850end
851put ff_s 14 2
852for i ulong get ff_s 16 bound 9223372036854775807
853ff_rowhit i
854end
855if eq get ff_s 21 1
856put lx_pick 0 1
857end
858ret 1
859end
860
861fn ff_rowhit index ulong to void
862p = get ff_hits mul index 2
863n = get ff_hits add mul index 2 1
864ff_rowpath p n
865end
866
867fn ff_activate to int
868mode = get ff_s 14
869if eq mode 1
870if eq ff_rebuild 0
871k = ff_query
872end
873ret 0
874end
875if ne mode 2
876ret 0
877end
878pick = get lx_pick 0
879if lt pick get ff_s 21
880ret 0
881end
882pick = sub pick get ff_s 21
883if ge pick get ff_s 16
884ret 0
885end
886p = get ff_hits mul pick 2
887n = get ff_hits add mul pick 2 1
888path = new uchar 4096 0
889for i ulong n bound 4096
890c = get ff_data add p i
891put path i c
892end
893ret ha_openfile part path 0 n
894end
895
896fn ff_partial to ulong
897ret get ff_s 4
898end
899
900fn ff_flush to bool
901p = ulong 0
902n = get ff_on 0
903while lt p n bound 4096
904k = write 1 ff_out p sub n p
905if eq k -4
906continue
907end
908if le k 0
909put ff_s 13 1
910ret false
911end
912p = add p ulong k
913end
914put ff_on 0 0
915ret true
916end
917
918fn ff_outbyte c uchar to void
919if eq get ff_on 0 len ff_out
920if not ff_flush
921ret
922end
923end
924p = get ff_on 0
925put ff_out p c
926put ff_on 0 add p 1
927end
928
929fn ff_jsonhex scalar ulong to void
930ff_outbyte 92
931ff_outbyte 117
932ff_outbyte get ff_hex and shr scalar 12 15
933ff_outbyte get ff_hex and shr scalar 8 15
934ff_outbyte get ff_hex and shr scalar 4 15
935ff_outbyte get ff_hex and scalar 15
936end
937
938fn ff_scalar pos ulong endpos ulong to ulong
939put ff_s 25 1
940c = ulong get ff_data pos
941if lt c 128
942ret c
943end
944count = ulong 0
945value = ulong 0
946if and ge c 194 le c 223
947count = 2
948value = and c 31
949end
950if and ge c 224 le c 239
951count = 3
952value = and c 15
953end
954if and ge c 240 le c 244
955count = 4
956value = and c 7
957end
958if or eq count 0 gt add pos count endpos
959ret add 56320 c
960end
961for i ulong sub count 1 bound 3
962d = ulong get ff_data add add pos i 1
963if or lt d 128 gt d 191
964ret add 56320 c
965end
966value = or shl value 6 and d 63
967end
968if and eq count 2 lt value 128
969ret add 56320 c
970end
971if and eq count 3 lt value 2048
972ret add 56320 c
973end
974if and eq count 4 lt value 65536
975ret add 56320 c
976end
977if or and ge value 55296 le value 57343 gt value 1114111
978ret add 56320 c
979end
980put ff_s 25 count
981ret value
982end
983
984fn ff_jsonpath start ulong size ulong to void
985ff_outbyte 34
986p = start
987endpos = add start size
988while lt p endpos bound 4096
989v = ff_scalar p endpos
990if or eq v 34 eq v 92
991ff_outbyte 92
992ff_outbyte uchar v
993else
994if or lt v 32 ge v 127
995if le v 65535
996ff_jsonhex v
997else
998partv = sub v 65536
999ff_jsonhex add 55296 shr partv 10
1000ff_jsonhex add 56320 and partv 1023
1001end
1002else
1003ff_outbyte uchar v
1004end
1005end
1006p = add p get ff_s 25
1007end
1008ff_outbyte 34
1009end
1010
1011fn ff_json to int
1012put ff_on 0 0
1013ff_outbyte 91
1014for i ulong get ff_s 16 bound 9223372036854775807
1015if ne i 0
1016ff_outbyte 44
1017end
1018ff_jsonhit i
1019end
1020ff_outbyte 93
1021ff_outbyte 10
1022if not ff_flush
1023ret 1
1024end
1025if ne get ff_s 13 0
1026ret 1
1027end
1028ret 0
1029end
1030
1031fn ff_jsonhit index ulong to void
1032p = get ff_hits mul index 2
1033n = get ff_hits add mul index 2 1
1034ff_jsonpath p n
1035end
1036
1037fn ff_cliterms to bool
1038put ff_s 17 0
1039if le argcnt 2
1040ret true
1041end
1042n = argread 2 ff_arg 0 len ff_arg
1043if lt n 0
1044ret false
1045end
1046if not ff_reserve 6 ulong n
1047ret false
1048end
1049for i ulong ulong n bound 131072
1050c = get ff_arg i
1051put ff_terms i c
1052end
1053put ff_s 17 ulong n
1054ret true
1055end
1056
1057fn ff_cli mode int to int
1058if eq mode 0
1059ret ff_rebuild
1060end
1061if gt argcnt 3
1062ret 2
1063end
1064if not ff_cliterms
1065ret 1
1066end
1067loaded = ff_load
1068if ne loaded 0
1069if eq loaded 1
1070ff_message ff_missingmsg
1071else
1072if eq loaded 2
1073ff_message ff_badmsg
1074else
1075ff_message ff_failmsg
1076end
1077end
1078ret 1
1079end
1080if not ff_search
1081ret 1
1082end
1083if ne get ff_s 4 0
1084ff_message ff_partialmsg
1085end
1086ret ff_json
1087end
1088
1089fn ff_record to bool
1090n = get ff_s 0
1091p = ulong 0
1092while lt p n bound 4096
1093k = write get ff_fd 3 ff_path p sub n p
1094if eq k -4
1095continue
1096end
1097if le k 0
1098put ff_s 13 1
1099ret false
1100end
1101p = add p ulong k
1102end
1103k = write get ff_fd 3 ff_nul 0 1
1104if ne k 1
1105put ff_s 13 1
1106ret false
1107end
1108put ff_s 3 add get ff_s 3 1
1109put ff_s 5 add get ff_s 5 add n 1
1110ff_alarm
1111ret true
1112end
1113
1114fn ff_printnum value ulong to void
1115b = new uchar 20 0
1116v = value
1117n = ulong 20
1118while true bound 20
1119n = sub n 1
1120put b n uchar add 48 rem v 10
1121v = div v 10
1122if eq v 0
1123break
1124end
1125end
1126k = write 2 b n sub 20 n
1127end
1128
1129fn ff_alarm to void
1130if ne get ff_s 12 0
1131ret
1132end
1133if le get ff_s 3 get ff_s 11
1134ret
1135end
1136put ff_s 12 1
1137ff_message ff_alarmmsg
1138ff_printnum get ff_s 3
1139ff_message ff_alarmmid
1140ff_printnum get ff_s 11
1141ff_message ff_alarmend
1142end
1143
1144fn ff_alarmsetting to void
1145put ff_s 11 1000000
1146n = ha_envget ff_alarmkey
1147if lt n 0
1148ret
1149end
1150if eq n 0
1151ff_message ff_alarmbad
1152ret
1153end
1154v = ulong 0
1155for i ulong ulong n bound 131072
1156c = get ha_env i
1157if or lt c 48 gt c 57
1158ff_message ff_alarmbad
1159ret
1160end
1161d = ulong sub c 48
1162if gt v div sub 18446744073709551615 d 10
1163ff_message ff_alarmbad
1164ret
1165end
1166v = add mul v 10 d
1167end
1168put ff_s 11 v
1169end
1170
1171fn ff_seenplace device ulong inode ulong cap ulong to ulong
1172slot = rem xor device inode cap
1173while ne get ff_seen add mul slot 3 2 0 bound 9223372036854775807
1174if eq get ff_seen mul slot 3 device
1175if eq get ff_seen add mul slot 3 1 inode
1176ret slot
1177end
1178end
1179slot = rem add slot 1 cap
1180end
1181ret slot
1182end
1183
1184fn ff_seengrow to bool
1185oldcap = get ff_s 10
1186cap = ulong 64
1187if gt oldcap 0
1188cap = mul oldcap 2
1189end
1190if not ff_reserve 3 mul oldcap 3
1191ret false
1192end
1193for i ulong mul oldcap 3 bound 9223372036854775807
1194c = get ff_seen i
1195put ff_oldseen i c
1196end
1197if not ff_reserve 2 mul cap 3
1198ret false
1199end
1200for i ulong cap bound 9223372036854775807
1201put ff_seen add mul i 3 2 0
1202end
1203for i ulong oldcap bound 9223372036854775807
1204if ne get ff_oldseen add mul i 3 2 0
1205ff_rehash i cap
1206end
1207end
1208put ff_s 10 cap
1209ret true
1210end
1211
1212fn ff_rehash oldslot ulong cap ulong to void
1213device = get ff_oldseen mul oldslot 3
1214inode = get ff_oldseen add mul oldslot 3 1
1215slot = ff_seenplace device inode cap
1216put ff_seen mul slot 3 device
1217put ff_seen add mul slot 3 1 inode
1218put ff_seen add mul slot 3 2 1
1219end
1220
1221fn ff_remember device ulong inode ulong to int
1222if ge mul get ff_s 9 2 get ff_s 10
1223if not ff_seengrow
1224ret -1
1225end
1226end
1227slot = ff_seenplace device inode get ff_s 10
1228if ne get ff_seen add mul slot 3 2 0
1229ret 0
1230end
1231put ff_seen mul slot 3 device
1232put ff_seen add mul slot 3 1 inode
1233put ff_seen add mul slot 3 2 1
1234put ff_s 9 add get ff_s 9 1
1235ret 1
1236end
1237
1238fn ff_homesuffix suffix view uchar * to bool
1239n = get ff_s 18
1240if eq n 0
1241ret false
1242end
1243if ne get ff_s 0 add n len suffix
1244ret false
1245end
1246for i ulong n bound 4096
1247if ne get ff_path i get ff_home i
1248ret false
1249end
1250end
1251for i ulong len suffix bound 4096
1252if ne get ff_path add n i get suffix i
1253ret false
1254end
1255end
1256ret true
1257end
1258
1259fn ff_excludedpath to bool
1260if ff_skipcurrent
1261ret true
1262end
1263if eq get ff_s 0 get ff_s 1
1264same = true
1265for i ulong get ff_s 1 bound 4096
1266if ne get ff_path i get ff_cache i
1267same = false
1268break
1269end
1270end
1271if same
1272ret true
1273end
1274end
1275if ff_homesuffix ff_hive
1276ret true
1277end
1278ret ff_homesuffix ff_lanes
1279end
1280
1281fn ff_enqueue to bool
1282if ff_excludedpath
1283ret true
1284end
1285if not ff_getstat false
1286ff_warn
1287ret true
1288end
1289if ne ff_mode 16384
1290ff_warn
1291ret true
1292end
1293device = ff_statword 0 8
1294inode = ff_statword 8 8
1295newdir = ff_remember device inode
1296if lt newdir 0
1297ret false
1298end
1299if eq newdir 0
1300ret true
1301end
1302q = get ff_s 7
1303p = get ff_s 8
1304n = get ff_s 0
1305if not ff_reserve 0 add p n
1306ret false
1307end
1308if not ff_reserve 1 mul add q 1 4
1309ret false
1310end
1311ff_queuecopy p n
1312put ff_qmeta mul q 4 p
1313put ff_qmeta add mul q 4 1 n
1314put ff_qmeta add mul q 4 2 device
1315put ff_qmeta add mul q 4 3 inode
1316put ff_s 7 add q 1
1317put ff_s 8 add p n
1318ret ff_record
1319end
1320
1321fn ff_queuecopy dest ulong size ulong to void
1322for i ulong size bound 4096
1323c = get ff_path i
1324put ff_queue add dest i c
1325end
1326end
1327
1328fn ff_queuepath index ulong to void
1329p = get ff_qmeta mul index 4
1330n = get ff_qmeta add mul index 4 1
1331for i ulong n bound 4096
1332c = get ff_queue add p i
1333put ff_path i c
1334end
1335put ff_s 0 n
1336end
1337
1338fn ff_warn to void
1339put ff_s 4 add get ff_s 4 1
1340end
1341
1342fn ff_skipname pos ulong size ulong to bool
1343b = new uchar 4096 0
1344if ge size len b
1345ret true
1346end
1347for i ulong size bound 4096
1348put b i get ff_buf add pos i
1349end
1350ret ff_skipvalue part b 0 size
1351end
1352
1353fn ff_skipcurrent to bool
1354b = new uchar 4096 0
1355n = get ff_s 0
1356start = n
1357while gt start 0 bound 4096
1358if eq get ff_path sub start 1 47
1359break
1360end
1361start = sub start 1
1362end
1363size = sub n start
1364for i ulong size bound 4096
1365put b i get ff_path add start i
1366end
1367ret ff_skipvalue part b 0 size
1368end
1369
1370fn ff_equal a view uchar * b view uchar * to bool
1371if ne len a len b
1372ret false
1373end
1374for i ulong len a bound 4096
1375if ne get a i get b i
1376ret false
1377end
1378end
1379ret true
1380end
1381
1382fn ff_skipvalue value view uchar * to bool
1383if eq len value 0
1384ret false
1385end
1386if eq get value 0 46
1387ret true
1388end
1389if ff_equal value ff_node
1390ret true
1391end
1392if ff_equal value ff_target
1393ret true
1394end
1395if ff_equal value ff_build
1396ret true
1397end
1398if ff_equal value ff_dist
1399ret true
1400end
1401if ff_equal value ff_py
1402ret true
1403end
1404if ff_equal value ff_venv
1405ret true
1406end
1407if ff_equal value ff_dotvenv
1408ret true
1409end
1410ret ff_equal value ff_site
1411end
1412
1413fn ff_childpath dir ulong pos ulong size ulong to bool
1414ff_queuepath dir
1415base = get ff_s 0
1416extra = ulong 1
1417if eq base 1
1418extra = 0
1419end
1420if ge add add base extra size len ff_path
1421ff_warn
1422ret false
1423end
1424if ne extra 0
1425put ff_path base 47
1426end
1427for i ulong size bound 65536
1428c = get ff_buf add pos i
1429put ff_path add add base extra i c
1430end
1431put ff_s 0 add add base extra size
1432ret true
1433end
1434
1435fn ff_entry dir ulong pos ulong endpos ulong to bool
1436start = add pos 19
1437n = ulong 0
1438while lt add start n endpos bound 65536
1439if eq get ff_buf add start n 0
1440break
1441end
1442n = add n 1
1443end
1444if eq add start n endpos
1445ff_warn
1446ret true
1447end
1448if eq n 0
1449ret true
1450end
1451if eq get ff_buf start 46
1452if eq n 1
1453ret true
1454end
1455if eq n 2
1456if eq get ff_buf add start 1 46
1457ret true
1458end
1459end
1460end
1461if not ff_childpath dir start n
1462ret true
1463end
1464if not ff_getstat false
1465ff_warn
1466ret true
1467end
1468kind = ff_mode
1469if eq kind 16384
1470if ff_skipname start n
1471ret true
1472end
1473ret ff_enqueue
1474end
1475if or eq kind 32768 eq kind 40960
1476ret ff_record
1477end
1478ret true
1479end
1480
1481fn ff_dirbytes dir ulong size ulong to bool
1482p = ulong 0
1483while lt p size bound 65536
1484if lt sub size p 20
1485ff_warn
1486ret true
1487end
1488low = ulong get ff_buf add p 16
1489high = ulong get ff_buf add p 17
1490rec = add low shl high 8
1491if or lt rec 20 gt add p rec size
1492ff_warn
1493ret true
1494end
1495if not ff_entry dir p add p rec
1496ret false
1497end
1498p = add p rec
1499end
1500ret true
1501end
1502
1503fn ff_scandir dir ulong to bool
1504ff_queuepath dir
1505fd = int fopen ff_path 0 get ff_s 0 49152 0
1506put ff_s 6 add get ff_s 6 1
1507if lt fd 0
1508ff_warn
1509ret true
1510end
1511k = fstat fd ff_stat 0 len ff_stat
1512if lt k 0
1513k = fclose fd
1514ff_warn
1515ret true
1516end
1517if not ff_samedir dir
1518k = fclose fd
1519ff_warn
1520ret true
1521end
1522ok = true
1523while true bound 9223372036854775807
1524n = getdnts fd ff_buf 0 len ff_buf
1525if eq n -4
1526continue
1527end
1528if lt n 0
1529ff_warn
1530break
1531end
1532if eq n 0
1533break
1534end
1535if not ff_dirbytes dir ulong n
1536ok = false
1537break
1538end
1539end
1540k = fclose fd
1541ret ok
1542end
1543
1544fn ff_samedir dir ulong to bool
1545if ne ff_mode 16384
1546ret false
1547end
1548if ne ff_statword 0 8 get ff_qmeta add mul dir 4 2
1549ret false
1550end
1551ret eq ff_statword 8 8 get ff_qmeta add mul dir 4 3
1552end
1553
1554fn ff_rootarg source view uchar * to bool
1555if eq len source 0
1556ret false
1557end
1558if eq get source 0 47
1559if not ff_pathset source
1560ret false
1561end
1562else
1563n = get ff_s 19
1564for i ulong n bound 4096
1565c = get ff_cwd i
1566put ff_path i c
1567end
1568put ff_s 0 n
1569if not ff_pathadd ff_slash
1570ret false
1571end
1572if not ff_pathadd source
1573ret false
1574end
1575end
1576ret ff_normalize
1577end
1578
1579fn ff_roots to bool
1580if not ff_rootargs
1581if eq get ff_s 18 0
1582put ff_s 13 1
1583ret false
1584end
1585ff_homepath
1586ret ff_enqueue
1587end
1588for i ulong sub ulong argcnt 2 bound 9223372036854775807
1589n = argread add i 2 ff_arg 0 len ff_arg
1590if lt n 0
1591ff_warn
1592else
1593if ff_rootargbuf ulong n
1594if not ff_enqueue
1595ret false
1596end
1597else
1598ff_warn
1599end
1600end
1601end
1602ret true
1603end
1604
1605fn ff_rootargs to bool
1606if le argcnt 2
1607ret false
1608end
1609b = new uchar 16 0
1610n = argread 1 b 0 len b
1611if ne n long len ff_reindexarg
1612ret false
1613end
1614for i ulong len ff_reindexarg
1615if ne get b i get ff_reindexarg i
1616ret false
1617end
1618end
1619ret true
1620end
1621
1622fn ff_rootargbuf size ulong to bool
1623local = new uchar 4096 0
1624if ge size len local
1625ret false
1626end
1627for j ulong size bound 4096
1628put local j get ff_arg j
1629end
1630ret ff_rootarg part local 0 size
1631end
1632
1633fn ff_publish to bool
1634ff_header
1635k = fseek get ff_fd 3 0 0
1636if ne k 0
1637ret false
1638end
1639if ne ff_writeheader 0
1640ret false
1641end
1642if ne fsync get ff_fd 3 0
1643ret false
1644end
1645put ff_rename 0 2
1646put ff_rename 1 0
1647n = get ff_s 2
1648put ff_rename 2 uchar n
1649put ff_rename 3 0
1650for i ulong n bound 96
1651c = get ff_tmp i
1652put ff_rename add i 4 c
1653end
1654p = add n 4
1655put ff_rename p uchar len ff_index
1656put ff_rename add p 1 0
1657for i ulong len ff_index
1658put ff_rename add add p 2 i get ff_index i
1659end
1660k = renames ff_rename 0 add add p 2 len ff_index 0
1661if ne k 0
1662ret false
1663end
1664put ff_s 20 0
1665ret true
1666end
1667
1668fn ff_rebuild to int
1669for i ulong 32
1670put ff_s i 0
1671end
1672if not ff_setup true
1673ff_message ff_cachemsg
1674ff_cleanup
1675ret 1
1676end
1677if not ff_enter
1678ff_message ff_failmsg
1679ff_cleanup
1680ret 1
1681end
1682started = ff_startoutput
1683if ne started 0
1684if eq started 3
1685ff_message ff_busymsg
1686else
1687ff_message ff_failmsg
1688end
1689ff_cleanup
1690ret started
1691end
1692ff_alarmsetting
1693ok = ff_roots
1694di = ulong 0
1695while and ok lt di get ff_s 7 bound 9223372036854775807
1696ok = ff_scandir di
1697di = add di 1
1698end
1699if ok
1700ok = ff_publish
1701end
1702if not ok
1703put ff_s 13 1
1704end
1705ff_cleanup
1706if ne get ff_s 13 0
1707ff_message ff_failmsg
1708ret 1
1709end
1710if ne get ff_s 4 0
1711ff_message ff_partialmsg
1712end
1713ret 0
1714end