nspkg / nspkg.v
2425 lines · 2102 sloc · 67.67 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module nspkg
2
3import nspkg.engine
4import os
5
6#flag -std=gnu99
7
8#include <archive.h>
9#include <archive_entry.h>
10
11struct C.Archive {}
12
13struct C.Archive_acl {}
14
15struct C.Archive_entry {}
16
17struct C.Archive_entry_linkresolver {}
18
19type Size_t = u32
20type Ulong = u32
21type Ushort = u16
22type Uint = u32
23type U_int8_t = u8
24type U_int16_t = u16
25type U_int32_t = u32
26type U_int64_t = u64
27
28type Time_t = i64
29type Off_t = i64
30type Pid_t = int
31type Uid_t = u32
32type Gid_t = u32
33type Mode_t = u32
34type Dev_t = u64
35type Ino_t = u64
36type Nlink_t = u64
37type Blksize_t = i64
38type Blkcnt_t = i64
39type Clock_t = i64
40type Clockid_t = int
41type Timer_t = voidptr
42type Suseconds_t = i64
43type Useconds_t = u32
44type Syscall_slong_t = i64
45type Off64_t = i64
46
47struct C.Timespec {
48 tv_sec Time_t
49 tv_nsec Syscall_slong_t
50}
51
52type La_int64_t = i64
53type La_uint64_t = u64
54type La_ssize_t = int
55
56pub fn (list &List) get_pkg() ?&Package {
57 data := unsafe { &Package(list.data) }
58 return if data == unsafe { nil } { none } else { data }
59}
60
61pub fn (list &List) get_db() ?&DB {
62 data := unsafe { &DB(list.data) }
63 return if data == unsafe { nil } { none } else { data }
64}
65
66pub fn (list &List) get_str() ?string {
67 data := unsafe { &u8(list.data) }
68 return if data == unsafe { nil } { none } else { unsafe { data.vstring() } }
69}
70
71pub fn (list &List) get_group() ?&Group {
72 data := unsafe { &Group(list.data) }
73 return if data == unsafe { nil } { none } else { data }
74}
75
76pub fn (list &List) get_backup() ?&Backup {
77 if list.data == unsafe { nil } {
78 return none
79 }
80 return &Backup(list.data)
81}
82
83pub fn (b &Backup) get_name() string {
84 return cstring_to_vstring_or_none(b.name)
85}
86
87pub fn (b &Backup) get_hash() string {
88 return cstring_to_vstring_or_none(b.hash)
89}
90
91pub fn (list &List) get_depend() ?&Depend {
92 data := unsafe { &Depend(list.data) }
93 return if data == unsafe { nil } { none } else { data }
94}
95
96pub fn (mut list List) pkgs() []&Package {
97 mut out := []&Package{}
98 for list != unsafe { nil } {
99 if pkg := list.get_pkg() {
100 out << pkg
101 } else {
102 break
103 }
104 list = list.next
105 }
106 return out
107}
108
109pub fn (mut list List) dbs() []&DB {
110 mut out := []&DB{}
111 for list != unsafe { nil } {
112 if db := list.get_db() {
113 out << db
114 } else {
115 break
116 }
117 list = list.next
118 }
119 return out
120}
121
122pub fn (mut list List) strs() []string {
123 mut out := []string{}
124 for list != unsafe { nil } {
125 if s := list.get_str() {
126 out << s
127 } else {
128 break
129 }
130 list = list.next
131 }
132 return out
133}
134
135pub fn (mut list List) groups() []&Group {
136 mut out := []&Group{}
137 for list != unsafe { nil } {
138 if s := list.get_group() {
139 out << s
140 } else {
141 break
142 }
143 list = list.next
144 }
145 return out
146}
147
148pub fn (mut list List) depends() []&Depend {
149 mut out := []&Depend{}
150 for list != unsafe { nil } {
151 if s := list.get_depend() {
152 out << s
153 } else {
154 break
155 }
156 list = list.next
157 }
158 return out
159}
160
161pub struct HandleW {
162pub mut:
163 handle &Handle = unsafe { nil }
164 err Errno
165}
166
167pub struct Handle {
168mut:
169 core &engine.Handle = unsafe { nil }
170 databases map[u64]&DB
171 packages map[u64]&Package
172 lists map[string]&List
173 owned_lists []&List
174 owned_dependencies []&Depend
175 owned_buffers []voidptr
176 groups map[u64]&Group
177 root_anchor voidptr
178 logcb CbLog = unsafe { nil }
179 logctx voidptr
180 log_line_context voidptr
181 dlcb CbDownload = unsafe { nil }
182 dlctx voidptr
183 fetchcb CbFetch = unsafe { nil }
184 fetchctx voidptr
185 eventcb CbEvent = unsafe { nil }
186 eventctx voidptr
187 questioncb CbQuestion = unsafe { nil }
188 questionctx voidptr
189 progresscb CbProgress = unsafe { nil }
190 progressctx voidptr
191}
192
193pub struct DB {
194mut:
195 core &engine.Database = unsafe { nil }
196 owner &Handle = unsafe { nil }
197 package_revision u64
198 package_list &List = unsafe { nil }
199}
200
201pub struct Package {
202mut:
203 core &engine.Package = unsafe { nil }
204 owner &Handle = unsafe { nil }
205 filelist Filelist
206 file_versions []&NativeFiles
207 provides_exposed bool
208}
209
210pub struct PkgXdata {
211 name &u8 = unsafe { nil }
212 value &u8 = unsafe { nil }
213}
214
215type Time = i64
216
217pub struct File {
218 name &u8 = unsafe { nil }
219 size i64
220 mode u32
221}
222
223pub struct Filelist {
224 count uint
225 files &File = unsafe { nil }
226}
227
228pub struct Backup {
229 name &u8 = unsafe { nil }
230 hash &u8 = unsafe { nil }
231}
232
233pub fn (filelist &Filelist) contains(path string) ?&File {
234 if filelist == unsafe { nil } { return none }
235 needle := path.all_before('\x00')
236 mut low := 0
237 mut high := int(filelist.count)
238 for low < high {
239 mid := low + (high - low) / 2
240 item := unsafe { &filelist.files[mid] }
241 current := cstring_to_vstring_or_none(item.name)
242 if current == needle { return item }
243 if current < needle { low = mid + 1 } else { high = mid }
244 }
245 return none
246}
247
248pub struct Group {
249pub mut:
250 name &u8 = unsafe { nil }
251 packages &List = unsafe { nil }
252}
253
254pub fn (group &Group) get_name() string {
255 return cstring_to_vstring_or_none(group.name)
256}
257
258pub fn (dbs &List) find_group_pkgs(name string) &List {
259 mut values := dbs.native_databases()
260 if values.len == 0 { return unsafe { nil } }
261 h := dbs.get_db() or { return unsafe { nil } }
262 mut core := h.owner.native_handle() or { return unsafe { nil } }
263 found := core.find_group_packages_for_transaction(values, name) or { return unsafe { nil } }
264 return h.owner.native_package_list(found)
265}
266
267pub enum Errno {
268 ok = 0
269 memory
270 system
271 badperms
272 not_a_file
273 not_a_dir
274 wrong_args
275 disk_space
276 handle_null
277 handle_not_null
278 handle_lock
279 db_open
280 db_create
281 db_null
282 db_not_null
283 db_not_found
284 db_invalid
285 db_invalid_sig
286 db_version
287 db_write
288 db_remove
289 server_bad_url
290 server_none
291 trans_not_null
292 trans_null
293 trans_dup_target
294 trans_dup_filename
295 trans_not_initialized
296 trans_not_prepared
297 trans_abort
298 trans_type
299 trans_not_locked
300 trans_hook_failed
301 pkg_not_found
302 pkg_ignored
303 pkg_invalid
304 pkg_invalid_checksum
305 pkg_invalid_sig
306 pkg_missing_sig
307 pkg_open
308 pkg_cant_remove
309 pkg_invalid_name
310 pkg_invalid_arch
311 sig_missing
312 sig_invalid
313 unsatisfied_deps
314 conflicting_deps
315 file_conflicts
316 retrieve_prepare
317 retrieve
318 invalid_regex
319 libarchive
320 libcurl
321 external_download
322 gpgme
323 missing_capability_signatures
324}
325
326pub fn (handle &Handle) errno() Errno {
327 if handle == unsafe { nil } || handle.core == unsafe { nil } { return .handle_null }
328 return unsafe { Errno(handle.core.errno()) }
329}
330
331pub fn (err Errno) strerror() string {
332 return unsafe { engine.Errno(err) }.strerror()
333}
334
335pub fn (err Errno) ierror() IError {
336 return error_with_code(err.strerror(), int(err))
337}
338
339pub fn (handle &Handle) error() IError {
340 if handle == unsafe { nil } || handle.core == unsafe { nil } {
341 return Errno.handle_null.ierror()
342 }
343 return handle.errno().ierror()
344}
345
346pub fn initialize(root string, dbpath string) !&nspkg.Handle {
347 mut core := engine.initialize(root, dbpath)!
348 mut handle := &Handle{ core: core }
349 handle.native_retain() or {
350 mut failed := unsafe { core }
351 failed.release() or { eprintln(err) }
352 return err
353 }
354 core.bind_context(handle) or {
355 handle.release() or { eprintln(err) }
356 return err
357 }
358 handle.install_native_callbacks() or {
359 handle.release() or { eprintln(err) }
360 return err
361 }
362 return handle
363}
364
365pub fn (handle &Handle) release() ! {
366 mut core := handle.native_handle()!
367 line_context := handle.owned_log_line_context()
368 core.release()!
369 mut h := unsafe { handle }
370 for list in h.owned_lists { list.free() }
371 for dependency in h.owned_dependencies { dependency.free() }
372 for buffer in h.owned_buffers { C.free(buffer) }
373 h.owned_lists = []&List{}
374 h.owned_dependencies = []&Depend{}
375 h.owned_buffers = []voidptr{}
376 h.lists = map[string]&List{}
377 h.packages = map[u64]&Package{}
378 h.databases = map[u64]&DB{}
379 h.groups = map[u64]&Group{}
380 C.free(line_context)
381 h.logcb = unsafe { nil }
382 h.logctx = unsafe { nil }
383 h.log_line_context = unsafe { nil }
384 h.native_unretain()
385}
386
387// the values of a @[flag] enum are bit positions, not masks: V turns
388// `database = 10` into 1 << 10, libalpm's ALPM_SIG_DATABASE. alpm_test.v
389// checks every flag enum against libalpm's own constants
390@[flag]
391pub enum SigLevel {
392 package = 0
393 package_optional = 1
394 package_marginal_ok = 2
395 package_unknown_ok = 3
396 database = 10
397 database_optional = 11
398 database_marginal_ok = 12
399 database_unknown_ok = 13
400 use_default = 30
401}
402
403pub enum SigStatus {
404 valid
405 key_expired
406 sig_expired
407 key_unknown
408 key_disabled
409 invalid
410}
411
412pub enum SigValidity {
413 full
414 marginal
415 never
416 unknown
417}
418
419pub struct PGPKey {
420pub mut:
421 data voidptr = unsafe { nil }
422 fingerprint &u8 = unsafe { nil }
423 uid &u8 = unsafe { nil }
424 name &u8 = unsafe { nil }
425 email &u8 = unsafe { nil }
426 created Time
427 expires Time
428 length u32
429 revoked u32
430}
431
432pub struct SigResult {
433pub mut:
434 key PGPKey
435 status SigStatus
436 validity SigValidity
437}
438
439pub struct SigList {
440pub mut:
441 count uint
442 results &SigResult = unsafe { nil }
443}
444
445pub fn (pkg &Package) check_pgp_signature() ?&SigList {
446 pkg.native_package() or { return none }
447 mut core := pkg.owner.native_handle() or { return none }
448 path := pkg.native_signature_path() or { return none }
449 embedded := if pkg.core.has_base64_sig { ?string(pkg.core.base64_sig) } else { ?string(none) }
450 results := core.check_signature_owned(path, embedded) or { return none }
451 return pkg.owner.native_siglist(results)
452}
453
454pub fn (db &DB) check_pgp_signature() ?&SigList {
455 db.native_database() or { return none }
456 mut core := db.owner.native_handle() or { return none }
457 results := core.check_signature_owned(db.core.path, none) or { return none }
458 return db.owner.native_siglist(results)
459}
460
461pub fn (siglist &SigList) cleanup() ! {
462 if siglist == unsafe { nil } { return Errno.wrong_args.ierror() }
463 for index in 0 .. int(siglist.count) {
464 result := unsafe { siglist.results[index] }
465 if result.key.data != unsafe { nil } {
466 engine.release_signature_key(result.key.data)
467 } else {
468 C.free(result.key.fingerprint)
469 }
470 }
471 C.free(siglist.results)
472 mut list := unsafe { siglist }
473 list.count = 0
474 list.results = unsafe { nil }
475}
476
477pub fn decode_signature(base64_data string, data_len &uint) !&u8 {
478 if data_len == unsafe { nil } { return Errno.wrong_args.ierror() }
479 unsafe { *data_len = 0 }
480 decoded := engine.decode_signature(base64_data) or {
481 return error_with_code('failed to decode signature', int(Errno.sig_invalid))
482 }
483 data := unsafe { &u8(C.malloc(if decoded.len > 0 { decoded.len } else { 1 })) }
484 if data == unsafe { nil } { return Errno.memory.ierror() }
485 if decoded.len > 0 { unsafe { C.memcpy(data, decoded.data, uint(decoded.len)) } }
486 unsafe { *data_len = uint(decoded.len) }
487 return data
488}
489
490pub fn (handle &Handle) extract_keyid(identifier string, sig &u8, len uint) !&List {
491 mut core := handle.native_handle_preserving_error()!
492 if int(len) < 0 || (sig == unsafe { nil } && len != 0) { return Errno.wrong_args.ierror() }
493 if len == 0 { return unsafe { &List(nil) } }
494 ids := core.extract_signature_key_ids(identifier, unsafe { sig.vbytes(int(len)) })!
495 return native_owned_strings(ids)
496}
497
498pub enum Depmod {
499 any = 1
500 equal
501 at_least
502 at_most
503 greater_than
504 less_than
505}
506
507pub enum FileConflictType {
508 target = 1
509 filesystem
510}
511
512pub struct Depend {
513pub mut:
514 name &u8 = unsafe { nil }
515 version &u8 = unsafe { nil }
516 desc &u8 = unsafe { nil }
517 name_hash u64
518 mod Depmod
519}
520
521pub fn (dep Depend) get_name() string {
522 return cstring_to_vstring_or_none(dep.name)
523}
524
525pub fn (dep Depend) get_version() string {
526 return cstring_to_vstring_or_none(dep.version)
527}
528
529pub fn (dep Depend) get_desc() string {
530 return cstring_to_vstring_or_none(dep.desc)
531}
532
533pub struct DepMissing {
534pub mut:
535 target &u8 = unsafe { nil }
536 depend &Depend = unsafe { nil }
537 causingpkg &u8 = unsafe { nil }
538}
539
540pub struct Conflict {
541pub mut:
542 package1 &Package = unsafe { nil }
543 package2 &Package = unsafe { nil }
544 reason &Depend = unsafe { nil }
545}
546
547pub struct FileConflict {
548pub mut:
549 target &u8 = unsafe { nil }
550 type_ FileConflictType
551 file &u8 = unsafe { nil }
552 ctarget &u8 = unsafe { nil }
553}
554
555pub fn (handle &Handle) checkdeps(pkglist &List, remove &List, upgrade &List, reversedeps int) &List {
556 mut core := handle.native_handle() or { return unsafe { nil } }
557 assumed := core.assumed() or { return unsafe { nil } }
558 flags := core.transaction_flags() or {
559 core.validate_handle() or { return unsafe { nil } }
560 engine.transaction_flag_0
561 }
562 missing := engine.check_dependencies(pkglist.native_packages(), remove.native_packages(), upgrade.native_packages(), engine.DependencyCheckOptions{ reverse: reversedeps != 0, ignore_versions: flags.has(.no_depversion), assumed: assumed })
563 return native_missing_list(missing)
564}
565
566pub fn (pkgs &List) find_satisfier(depstring string) ?&Package {
567 dependency := engine.dep_from_string(depstring)
568 for node := unsafe { pkgs }; node != unsafe { nil }; node = node.next {
569 pkg := node.get_pkg() or { continue }
570 if pkg.satisfies_constraint(dependency) { return pkg }
571 }
572 return none
573}
574
575pub fn (handle &Handle) find_dbs_satisfier(dbs &List, depstring string) ?&Package {
576 mut core := handle.native_handle() or { return none }
577 found := core.find_database_satisfier(dbs.native_databases(), depstring) or { return none }
578 return handle.native_package(found)
579}
580
581pub fn (handle &Handle) checkconflicts(pkglist &List) &List {
582 handle.native_handle() or { return unsafe { nil } }
583 return handle.native_conflicts(engine.find_conflicts(pkglist.native_packages()))
584}
585
586// libc allocation preserves the public dependency payload layout and ownership.
587pub fn (dep &Depend) compute_string() string {
588 return engine_dependency(dep).compute_string()
589}
590
591pub fn dep_from_string(depstring string) &Depend {
592 parsed := engine.dep_from_string(depstring)
593 mut dep := unsafe { &Depend(C.calloc(1, sizeof(Depend))) }
594 if dep == unsafe { nil } {
595 return dep
596 }
597 dep.name = unsafe { C.strdup(parsed.name().str) }
598 if parsed.mod != .any {
599 dep.version = unsafe { C.strdup(parsed.version().str) }
600 }
601 if parsed.has_desc() {
602 dep.desc = unsafe { C.strdup(parsed.desc().str) }
603 }
604 if dep.name == unsafe { nil }
605 || (parsed.mod != .any && dep.version == unsafe { nil })
606 || (parsed.has_desc() && dep.desc == unsafe { nil }) {
607 dep.free()
608 return unsafe { nil }
609 }
610 dep.mod = unsafe { Depmod(parsed.mod) }
611 for c in parsed.name() {
612 dep.name_hash = u64(i64(i8(c))) + (dep.name_hash << 6) + (dep.name_hash << 16) - dep.name_hash
613 }
614 return dep
615}
616
617pub fn (dep &Depend) free() {
618 if dep == unsafe { nil } { return }
619 C.free(dep.name)
620 C.free(dep.version)
621 C.free(dep.desc)
622 C.free(dep)
623}
624
625pub fn (conflict &FileConflict) free() {
626 if conflict == unsafe { nil } { return }
627 C.free(conflict.ctarget)
628 C.free(conflict.file)
629 C.free(conflict.target)
630 C.free(conflict)
631}
632
633pub fn (miss &DepMissing) free() {
634 if miss == unsafe { nil } { return }
635 miss.depend.free()
636 C.free(miss.target)
637 C.free(miss.causingpkg)
638 C.free(miss)
639}
640
641pub fn (conflict &Conflict) free() {
642 if conflict == unsafe { nil } { return }
643 conflict.reason.free()
644 C.free(conflict)
645}
646
647pub enum EventType {
648 checkdeps_start = 1
649 checkdeps_done
650 fileconflicts_start
651 fileconflicts_done
652 resolvedeps_start
653 resolvedeps_done
654 interconflicts_start
655 interconflicts_done
656 transaction_start
657 transaction_done
658 package_operation_start
659 package_operation_done
660 integrity_start
661 integrity_done
662 load_start
663 load_done
664 scriptlet_info
665 db_retrieve_start
666 db_retrieve_done
667 db_retrieve_failed
668 pkg_retrieve_start
669 pkg_retrieve_done
670 pkg_retrieve_failed
671 diskspace_start
672 diskspace_done
673 optdep_removal
674 database_missing
675 keyring_start
676 keyring_done
677 key_download_start
678 key_download_done
679 pacnew_created
680 pacsave_created
681 hook_start
682 hook_done
683 hook_run_start
684 hook_run_done
685}
686
687pub struct EventAny {
688 type_ EventType
689}
690
691pub enum PackageOperation {
692 install = 1
693 upgrade
694 reinstall
695 downgrade
696 remove
697}
698
699pub struct EventPackageOperation {
700pub mut:
701 type_ EventType
702 operation PackageOperation
703 oldpkg &Package = unsafe { nil }
704 newpkg &Package = unsafe { nil }
705}
706
707pub struct EventOptDepRemoval {
708pub mut:
709 type_ EventType
710 pkg &Package = unsafe { nil }
711 optdep &Depend = unsafe { nil }
712}
713
714pub struct EventScriptletInfo {
715pub mut:
716 type_ EventType
717 line &u8 = unsafe { nil }
718}
719
720pub struct EventDatabaseMissing {
721pub mut:
722 type_ EventType
723 dbname &u8 = unsafe { nil }
724}
725
726pub struct EventPkgDownload {
727pub mut:
728 type_ EventType
729 file &u8 = unsafe { nil }
730}
731
732pub struct EventPacnewCreated {
733pub mut:
734 type_ EventType
735 from_noupgrade i32
736 oldpkg &Package = unsafe { nil }
737 newpkg &Package = unsafe { nil }
738 file &u8 = unsafe { nil }
739}
740
741pub struct EventPacsaveCreated {
742pub mut:
743 type_ EventType
744 oldpkg &Package = unsafe { nil }
745 file &u8 = unsafe { nil }
746}
747
748pub enum HookWhen {
749 pre_transaction = 1
750 post_transaction
751}
752
753pub struct EventHook {
754pub mut:
755 type_ EventType
756 when HookWhen
757}
758
759pub struct EventHookRun {
760pub mut:
761 type_ EventType
762 name &u8 = unsafe { nil }
763 desc &u8 = unsafe { nil }
764 position uint
765 total uint
766}
767
768pub struct EventPkgRetrieve {
769pub mut:
770 type_ EventType
771 num uint
772 total_size i64
773}
774
775union Event {
776pub mut:
777 type_ EventType
778 any EventAny
779 package_operation EventPackageOperation
780 optdep_removal EventOptDepRemoval
781 scriptlet_info EventScriptletInfo
782 database_missing EventDatabaseMissing
783 pkgdownload EventPkgDownload
784 pacnew_created EventPacnewCreated
785 pacsave_created EventPacsaveCreated
786 hook EventHook
787 hook_run EventHookRun
788 pkg_retrieve EventPkgRetrieve
789}
790
791type CbEvent = fn (voidptr, &Event)
792
793pub enum QuestionType {
794 install_ignorepkg = 1 << 0
795 replace_pkg = 1 << 1
796 conflict_pkg = 1 << 2
797 corrupted_pkg = 1 << 3
798 remove_pkgs = 1 << 4
799 select_provider = 1 << 5
800 import_key = 1 << 6
801}
802
803pub struct QuestionAny {
804pub mut:
805 type_ QuestionType
806 answer i32
807}
808
809pub struct QuestionInstallIgnorePkg {
810pub mut:
811 type_ QuestionType
812 install i32
813 pkg &Package = unsafe { nil }
814}
815
816pub struct QuestionReplace {
817pub mut:
818 type_ QuestionType
819 replace i32
820 oldpkg &Package = unsafe { nil }
821 newpkg &Package = unsafe { nil }
822 newdb &DB = unsafe { nil }
823}
824
825pub struct QuestionConflict {
826pub mut:
827 type_ QuestionType
828 remove_ i32
829 conflict &Conflict = unsafe { nil }
830}
831
832pub struct QuestionCorrupted {
833pub mut:
834 type_ QuestionType
835 remove_ i32
836 filepath &u8 = unsafe { nil }
837 reason Errno
838}
839
840pub struct QuestionRemovePkgs {
841pub mut:
842 type_ QuestionType
843 skip i32
844 packages &List = unsafe { nil }
845}
846
847pub struct QuestionSelectProvider {
848pub mut:
849 type_ QuestionType
850 use_index i32
851 providers &List = unsafe { nil }
852 depend &Depend = unsafe { nil }
853}
854
855pub struct QuestionImportKey {
856pub mut:
857 type_ QuestionType
858 import_ i32
859 uid &u8 = unsafe { nil }
860 fingerprint &u8 = unsafe { nil }
861}
862
863union Question {
864pub mut:
865 type_ QuestionType
866 any QuestionAny
867 install_ignorepkg QuestionInstallIgnorePkg
868 replace QuestionReplace
869 conflict QuestionConflict
870 corrupted QuestionCorrupted
871 remove_pkgs QuestionRemovePkgs
872 select_provider QuestionSelectProvider
873 import_key QuestionImportKey
874}
875
876type CbQuestion = fn (voidptr, &Question)
877
878pub enum Progress {
879 add_start
880 upgrade_start
881 downgrade_start
882 reinstall_start
883 remove_start
884 conflicts_start
885 diskspace_start
886 integrity_start
887 load_start
888 keyring_start
889}
890
891type CbProgress = fn (voidptr, Progress, &u8, i32, uint, uint)
892
893pub enum DownloadEventType {
894 init
895 progress
896 retry
897 completed
898}
899
900pub struct DownloadEventInit {
901pub mut:
902 optional i32
903}
904
905pub struct DownloadEventProgress {
906pub mut:
907 downloaded i64
908 total i64
909}
910
911pub struct DownloadEventRetry {
912pub mut:
913 resume i32
914}
915
916pub struct DownloadEventCompleted {
917pub mut:
918 total i64
919 result i32
920}
921
922type CbDownload = fn (voidptr, &u8, DownloadEventType, voidptr)
923type CbFetch = fn (voidptr, &u8, &u8, i32) i32
924
925pub fn (handle &Handle) get_localdb() !&DB {
926 mut core := handle.native_handle()!
927 return handle.native_database(core.local_database()!)
928}
929
930pub fn (handle &Handle) get_syncdbs() !&List {
931 mut core := handle.native_handle()!
932 return handle.native_database_list(core.sync_databases()!)
933}
934
935pub fn (handle &Handle) register_syncdb(treename string, level int) !&DB {
936 mut core := handle.native_handle()!
937 return handle.native_database(core.register_syncdb(treename, u32(level))!)
938}
939
940pub fn (handle &Handle) unregister_all_syncdbs() ! {
941 mut core := handle.native_handle()!
942 core.unregister_syncdbs()!
943}
944
945pub fn (db &DB) unregister() ! {
946 mut core := db.native_database()!
947 core.unregister()!
948}
949
950pub fn (db &DB) get_handle() &Handle {
951 if db == unsafe { nil } { return unsafe { nil } }
952 return db.owner
953}
954
955pub fn (db &DB) get_name() string {
956 core := db.native_database_preserving_error() or { return '' }
957 return core.name
958}
959
960pub fn (db &DB) get_siglevel() SigLevel {
961 mut core := db.native_database_preserving_error() or { return SigLevel.zero() }
962 return unsafe { SigLevel(core.effective_siglevel() or { return SigLevel.zero() }) }
963}
964
965pub fn (db &DB) get_valid() !bool {
966 mut core := db.native_database()!
967 return core.get_valid()!
968}
969
970pub fn (db &DB) get_servers() &List {
971 mut core := db.native_database_preserving_error() or { return unsafe { nil } }
972 values := core.get_servers(false) or { return unsafe { nil } }
973 list := db.owner.native_strings('db:${u64(voidptr(db))}:servers', values)
974 return list
975}
976
977pub fn (db &DB) set_servers(servers &List) ! {
978 mut core := db.native_database_preserving_error()!
979 core.set_servers(servers.native_strings(), false)!
980}
981
982pub fn (db &DB) add_server(url string) ! {
983 mut core := db.native_database()!
984 core.add_server(url, false)!
985}
986
987pub fn (db &DB) remove_server(url string) ! {
988 mut core := db.native_database()!
989 core.remove_server(url, false)!
990}
991
992pub fn (db &DB) get_cache_servers() ?&List {
993 mut core := db.native_database_preserving_error() or { return none }
994 values := core.get_servers(true) or { return none }
995 list := db.owner.native_strings('db:${u64(voidptr(db))}:cache_servers', values)
996 return if list == unsafe { nil } { none } else { list }
997}
998
999pub fn (db &DB) set_cache_servers(servers &List) ! {
1000 mut core := db.native_database_preserving_error()!
1001 core.set_servers(servers.native_strings(), true)!
1002}
1003
1004pub fn (db &DB) add_cache_server(url string) ! {
1005 mut core := db.native_database()!
1006 core.add_server(url, true)!
1007}
1008
1009pub fn (db &DB) remove_cache_server(url string) ! {
1010 mut core := db.native_database()!
1011 core.remove_server(url, true)!
1012}
1013
1014// db_update updates the sync dbs. it answers whether anything was downloaded:
1015// libalpm returns 0 after a download, 1 when the dbs are all current (only
1016// possible without force, an "up to date" answer) and -1 on error. the up to
1017// date case is a success, not an error
1018pub fn (handle &Handle) db_update(dbs &List, force bool) !bool {
1019 mut core := handle.native_handle()!
1020 return core.update_databases(dbs.native_databases(), force)!
1021}
1022
1023pub fn (db &DB) get_pkg(name string) ?&Package {
1024 mut core := db.native_database() or { return none }
1025 found := core.get_pkg(name) or { return none }
1026 return db.owner.native_package(found)
1027}
1028
1029pub fn (db &DB) get_pkgcache() ?&List {
1030 mut core := db.native_database() or { return none }
1031 revision := core.package_cache_revision() or { return none }
1032 if revision != db.package_revision {
1033 values := core.package_cache() or { return none }
1034 mut wrapper := unsafe { db }
1035 wrapper.package_list = db.owner.native_cached_packages('cache:${u64(voidptr(db))}', values)
1036 wrapper.package_revision = revision
1037 }
1038 return if db.package_list == unsafe { nil } { none } else { db.package_list }
1039}
1040
1041pub fn (db &DB) get_group(name string) ?&Group {
1042 mut core := db.native_database() or { return none }
1043 group := core.get_group(name) or { return none }
1044 return db.owner.native_group(group)
1045}
1046
1047pub fn (db &DB) get_groupcache() ?&List {
1048 mut core := db.native_database() or { return none }
1049 groups := core.group_cache() or { return none }
1050 if groups.len == 0 { return none }
1051 key := native_pointer_list_key('groups:${u64(voidptr(db))}', groups)
1052 if existing := db.owner.lists[key] { return existing }
1053 mut list := &List(unsafe { nil })
1054 for group in groups { list = list.add(db.owner.native_group(group)) }
1055 mut owner := unsafe { db.owner }
1056 owner.lists[key] = list
1057 return db.owner.native_borrow_list(list)
1058}
1059
1060pub fn (db &DB) search(needles &List) !&List {
1061 mut core := db.native_database()!
1062 return db.owner.native_package_list(core.search(needles.native_strings())!)
1063}
1064
1065// bit positions, as for SigLevel
1066@[flag]
1067pub enum DBUsage {
1068 sync = 0
1069 search = 1
1070 install = 2
1071 upgrade = 3
1072}
1073
1074// db_usage_all is ALPM_DB_USAGE_ALL. a flag enum member is one bit, so the
1075// four together are a constant
1076pub const db_usage_all = DBUsage.sync | .search | .install | .upgrade
1077
1078pub fn (db &DB) set_usage(usage DBUsage) ! {
1079 mut core := db.native_database_preserving_error()!
1080 core.set_usage(u32(usage))!
1081}
1082
1083pub fn (db &DB) get_usage() ?DBUsage {
1084 mut core := db.native_database_preserving_error() or { return none }
1085 return unsafe { DBUsage(core.get_usage() or { return none }) }
1086}
1087
1088// bit positions, as for SigLevel
1089@[flag]
1090pub enum LogLevel {
1091 error = 0
1092 warning = 1
1093 debug = 2
1094 function = 3
1095}
1096
1097type CbLog = fn (voidptr, LogLevel, &u8, voidptr)
1098
1099pub fn (handle &Handle) log_action(prefix string, fmt string) ! {
1100 if handle == unsafe { nil } || handle.core == unsafe { nil } {
1101 return Errno.handle_null.ierror()
1102 }
1103 mut core := unsafe { handle.core }
1104 core.log_action(prefix, fmt)!
1105}
1106
1107pub fn (handle &Handle) option_get_logcb() CbLog {
1108 handle.native_handle() or { return unsafe { nil } }
1109 return handle.logcb
1110}
1111
1112pub fn (handle &Handle) option_get_logcb_ctx() voidptr {
1113 handle.native_handle() or { return unsafe { nil } }
1114 return handle.logctx
1115}
1116
1117pub fn (handle &Handle) option_set_logcb(cb CbLog, ctx voidptr) ! {
1118 mut core := handle.native_handle()!
1119 core.set_log_callback(if voidptr(cb) == unsafe { nil } { unsafe { nil } } else { native_log }, handle)!
1120 line_context := handle.owned_log_line_context()
1121 mut h := unsafe { handle }
1122 h.logcb = cb
1123 h.logctx = ctx
1124 h.log_line_context = if voidptr(cb) == voidptr(C.v_alpm_log_line_function()) {
1125 C.v_alpm_log_line_context(ctx)
1126 } else {
1127 unsafe { nil }
1128 }
1129 if line_context != ctx || voidptr(cb) != voidptr(C.v_alpm_log_line_function()) {
1130 C.free(line_context)
1131 }
1132}
1133
1134pub fn (handle &Handle) option_get_dlcb() CbDownload {
1135 handle.native_handle() or { return unsafe { nil } }
1136 return handle.dlcb
1137}
1138
1139pub fn (handle &Handle) option_get_dlcb_ctx() voidptr {
1140 handle.native_handle() or { return unsafe { nil } }
1141 return handle.dlctx
1142}
1143
1144pub fn (handle &Handle) option_set_dlcb(cb CbDownload, ctx voidptr) ! {
1145 mut core := handle.native_handle()!
1146 core.set_download_callback(if voidptr(cb) == unsafe { nil } {
1147 unsafe { nil }
1148 } else {
1149 native_download
1150 }, handle)!
1151 mut h := unsafe { handle }
1152 h.dlcb = cb
1153 h.dlctx = ctx
1154}
1155
1156pub fn (handle &Handle) option_get_fetchcb() CbFetch {
1157 handle.native_handle() or { return unsafe { nil } }
1158 return handle.fetchcb
1159}
1160
1161pub fn (handle &Handle) get_fetchcb_ctx() voidptr {
1162 handle.native_handle() or { return unsafe { nil } }
1163 return handle.fetchctx
1164}
1165
1166pub fn (handle &Handle) option_set_fetchcb(cb CbFetch, ctx voidptr) ! {
1167 mut core := handle.native_handle()!
1168 core.set_fetch_callback(if voidptr(cb) == unsafe { nil } {
1169 unsafe { nil }
1170 } else {
1171 native_fetch
1172 }, handle)!
1173 mut h := unsafe { handle }
1174 h.fetchcb = cb
1175 h.fetchctx = ctx
1176}
1177
1178pub fn (handle &Handle) option_get_eventcb() CbEvent {
1179 handle.native_handle() or { return unsafe { nil } }
1180 return handle.eventcb
1181}
1182
1183pub fn (handle &Handle) option_get_eventcb_ctx() voidptr {
1184 handle.native_handle() or { return unsafe { nil } }
1185 return handle.eventctx
1186}
1187
1188pub fn (handle &Handle) option_set_eventcb(cb CbEvent, ctx voidptr) ! {
1189 mut core := handle.native_handle()!
1190 if voidptr(cb) == unsafe { nil } {
1191 core.set_preparation_callback(unsafe { nil }, handle)!
1192 core.set_commit_callback(unsafe { nil }, handle)!
1193 core.set_retrieval_callback(unsafe { nil }, handle)!
1194 } else {
1195 core.set_preparation_callback(native_preparation, handle)!
1196 core.set_commit_callback(native_commit, handle)!
1197 core.set_retrieval_callback(native_retrieval, handle)!
1198 }
1199 mut h := unsafe { handle }
1200 h.eventcb = cb
1201 h.eventctx = ctx
1202}
1203
1204pub fn (handle &Handle) option_get_questioncb() CbQuestion {
1205 handle.native_handle() or { return unsafe { nil } }
1206 return handle.questioncb
1207}
1208
1209pub fn (handle &Handle) option_get_questioncb_ctx() voidptr {
1210 handle.native_handle() or { return unsafe { nil } }
1211 return handle.questionctx
1212}
1213
1214pub fn (handle &Handle) option_set_questioncb(cb CbQuestion, ctx voidptr) ! {
1215 mut core := handle.native_handle()!
1216 core.set_dependency_questions(if voidptr(cb) == unsafe { nil } {
1217 engine.DependencyQuestions{}
1218 } else {
1219 handle.native_dependency_questions()
1220 })!
1221 mut h := unsafe { handle }
1222 h.questioncb = cb
1223 h.questionctx = ctx
1224}
1225
1226pub fn (handle &Handle) option_get_progresscb() CbProgress {
1227 handle.native_handle() or { return unsafe { nil } }
1228 return handle.progresscb
1229}
1230
1231pub fn (handle &Handle) option_get_progresscb_ctx() voidptr {
1232 handle.native_handle() or { return unsafe { nil } }
1233 return handle.progressctx
1234}
1235
1236pub fn (handle &Handle) option_set_progresscb(cb CbProgress, ctx voidptr) ! {
1237 mut core := handle.native_handle()!
1238 core.set_commit_progress_callback(if voidptr(cb) == unsafe { nil } {
1239 unsafe { nil }
1240 } else {
1241 native_progress
1242 }, handle)!
1243 mut h := unsafe { handle }
1244 h.progresscb = cb
1245 h.progressctx = ctx
1246}
1247
1248pub fn (handle &Handle) option_get_root() string {
1249 core := handle.native_handle() or { return '' }
1250 return core.root
1251}
1252
1253pub fn (handle &Handle) option_get_dbpath() string {
1254 core := handle.native_handle() or { return '' }
1255 return core.dbpath
1256}
1257
1258pub fn (handle &Handle) option_get_lockfile() string {
1259 core := handle.native_handle() or { return '' }
1260 return core.lockfile
1261}
1262
1263pub fn (handle &Handle) option_get_cachedirs() &List {
1264 mut core := handle.native_handle() or { return unsafe { nil } }
1265 list := handle.native_strings('option:cache_dirs', core.string_list(.cache_dirs) or { return unsafe { nil } })
1266 return list
1267}
1268
1269pub fn (handle &Handle) option_set_cachedirs(cachedirs &List) ! {
1270 mut core := handle.native_handle()!
1271 core.set_string_list(.cache_dirs, cachedirs.native_strings())!
1272}
1273
1274pub fn (handle &Handle) option_add_cachedir(cachedir string) ! {
1275 mut core := handle.native_handle()!
1276 core.add_string_list(.cache_dirs, cachedir)!
1277}
1278
1279pub fn (handle &Handle) option_remove_cachedir(cachedir string) ! {
1280 mut core := handle.native_handle()!
1281 core.remove_string_list(.cache_dirs, cachedir)!
1282}
1283
1284pub fn (handle &Handle) option_get_hookdirs() ?&List {
1285 mut core := handle.native_handle() or { return none }
1286 list := handle.native_strings('option:hook_dirs', core.string_list(.hook_dirs) or { return none })
1287 return if list == unsafe { nil } { none } else { list }
1288}
1289
1290pub fn (handle &Handle) option_set_hookdirs(hookdirs &List) ! {
1291 mut core := handle.native_handle()!
1292 core.set_string_list(.hook_dirs, hookdirs.native_strings())!
1293}
1294
1295pub fn (handle &Handle) option_add_hookdir(hookdir string) ! {
1296 mut core := handle.native_handle()!
1297 core.add_string_list(.hook_dirs, hookdir)!
1298}
1299
1300pub fn (handle &Handle) option_remove_hookdir(hookdir string) ! {
1301 mut core := handle.native_handle()!
1302 core.remove_string_list(.hook_dirs, hookdir)!
1303}
1304
1305pub fn (handle &Handle) option_get_overwrite_files() &List {
1306 mut core := handle.native_handle() or { return unsafe { nil } }
1307 list := handle.native_strings('option:overwrite_files', core.string_list(.overwrite_files) or { return unsafe { nil } })
1308 return list
1309}
1310
1311pub fn (handle &Handle) option_set_overwrite_files(globs &List) ! {
1312 mut core := handle.native_handle()!
1313 core.set_string_list(.overwrite_files, globs.native_strings())!
1314}
1315
1316pub fn (handle &Handle) option_add_overwrite_file(glob string) ! {
1317 mut core := handle.native_handle()!
1318 core.add_string_list(.overwrite_files, glob)!
1319}
1320
1321pub fn (handle &Handle) option_remove_overwrite_file(glob string) ! {
1322 mut core := handle.native_handle()!
1323 core.remove_string_list(.overwrite_files, glob)!
1324}
1325
1326pub fn (handle &Handle) option_get_logfile() string {
1327 mut core := handle.native_handle() or { return '' }
1328 return core.string_option(.logfile) or { '' }
1329}
1330
1331pub fn (handle &Handle) option_set_logfile(logfile string) ! {
1332 mut core := handle.native_handle()!
1333 core.set_string_option(.logfile, logfile)!
1334}
1335
1336pub fn (handle &Handle) option_get_gpgdir() string {
1337 mut core := handle.native_handle() or { return '' }
1338 return core.string_option(.gpgdir) or { '' }
1339}
1340
1341pub fn (handle &Handle) option_set_gpgdir(gpgdir string) ! {
1342 mut core := handle.native_handle()!
1343 core.set_string_option(.gpgdir, gpgdir)!
1344}
1345
1346pub fn (handle &Handle) option_get_sandboxuser() string {
1347 mut core := handle.native_handle() or { return '' }
1348 return core.string_option(.sandbox_user) or { '' }
1349}
1350
1351pub fn (handle &Handle) option_set_sandboxuser(sandboxuser string) ! {
1352 mut core := handle.native_handle()!
1353 core.set_string_option(.sandbox_user, sandboxuser)!
1354}
1355
1356pub fn (handle &Handle) option_get_usesyslog() bool {
1357 mut core := handle.native_handle() or { return false }
1358 value := core.integer_option(.use_syslog) or { return false }
1359 return value != 0
1360}
1361
1362pub fn (handle &Handle) option_set_usesyslog(usesyslog int) ! {
1363 mut core := handle.native_handle()!
1364 core.set_integer_option(.use_syslog, i64(usesyslog))!
1365}
1366
1367pub fn (handle &Handle) option_get_noupgrades() &List {
1368 mut core := handle.native_handle() or { return unsafe { nil } }
1369 list := handle.native_strings('option:no_upgrade', core.string_list(.no_upgrade) or { return unsafe { nil } })
1370 return list
1371}
1372
1373pub fn (handle &Handle) option_add_noupgrade(path string) ! {
1374 mut core := handle.native_handle()!
1375 core.add_string_list(.no_upgrade, path)!
1376}
1377
1378pub fn (handle &Handle) option_set_noupgrades(noupgrade &List) ! {
1379 mut core := handle.native_handle()!
1380 core.set_string_list(.no_upgrade, noupgrade.native_strings())!
1381}
1382
1383pub fn (handle &Handle) option_remove_noupgrade(path string) ! {
1384 mut core := handle.native_handle()!
1385 core.remove_string_list(.no_upgrade, path)!
1386}
1387
1388pub fn (handle &Handle) option_match_no_upgrade(path string) bool {
1389 mut core := handle.native_handle_preserving_error() or { return false }
1390 return (core.match_option(.no_upgrade, path) or { return false }) == 0
1391}
1392
1393pub fn (handle &Handle) option_get_no_extracts() &List {
1394 mut core := handle.native_handle() or { return unsafe { nil } }
1395 list := handle.native_strings('option:no_extract', core.string_list(.no_extract) or { return unsafe { nil } })
1396 return list
1397}
1398
1399pub fn (handle &Handle) option_add_no_extract(path string) ! {
1400 mut core := handle.native_handle()!
1401 core.add_string_list(.no_extract, path)!
1402}
1403
1404pub fn (handle &Handle) option_set_no_extracts(noextract &List) ! {
1405 mut core := handle.native_handle()!
1406 core.set_string_list(.no_extract, noextract.native_strings())!
1407}
1408
1409pub fn (handle &Handle) option_remove_no_extract(path string) ! {
1410 mut core := handle.native_handle()!
1411 core.remove_string_list(.no_extract, path)!
1412}
1413
1414pub fn (handle &Handle) option_match_no_extract(path string) bool {
1415 mut core := handle.native_handle_preserving_error() or { return false }
1416 return (core.match_option(.no_extract, path) or { return false }) == 0
1417}
1418
1419pub fn (handle &Handle) option_get_ignore_pkgs() &List {
1420 mut core := handle.native_handle() or { return unsafe { nil } }
1421 list := handle.native_strings('option:ignore_packages', core.string_list(.ignore_packages) or { return unsafe { nil } })
1422 return list
1423}
1424
1425pub fn (handle &Handle) option_add_ignore_pkg(pkg string) ! {
1426 mut core := handle.native_handle()!
1427 core.add_string_list(.ignore_packages, pkg)!
1428}
1429
1430pub fn (handle &Handle) option_set_ignore_pkgs(ignorepkgs &List) ! {
1431 mut core := handle.native_handle()!
1432 core.set_string_list(.ignore_packages, ignorepkgs.native_strings())!
1433}
1434
1435pub fn (handle &Handle) option_remove_ignore_pkg(pkg string) ! {
1436 mut core := handle.native_handle()!
1437 core.remove_string_list(.ignore_packages, pkg)!
1438}
1439
1440pub fn (handle &Handle) option_get_ignore_groups() &List {
1441 mut core := handle.native_handle() or { return unsafe { nil } }
1442 list := handle.native_strings('option:ignore_groups', core.string_list(.ignore_groups) or { return unsafe { nil } })
1443 return list
1444}
1445
1446pub fn (handle &Handle) option_add_ignore_group(grp string) ! {
1447 mut core := handle.native_handle()!
1448 core.add_string_list(.ignore_groups, grp)!
1449}
1450
1451pub fn (handle &Handle) option_set_ignore_groups(ignoregrps &List) ! {
1452 mut core := handle.native_handle()!
1453 core.set_string_list(.ignore_groups, ignoregrps.native_strings())!
1454}
1455
1456pub fn (handle &Handle) option_remove_ignore_group(grp string) ! {
1457 mut core := handle.native_handle()!
1458 core.remove_string_list(.ignore_groups, grp)!
1459}
1460
1461pub fn option_get_assume_installed(handle &Handle) ?&List {
1462 mut core := handle.native_handle() or { return none }
1463 values := core.assumed() or { return none }
1464 if values.len == 0 { return none }
1465 return handle.native_dependencies('assumed', values)
1466}
1467
1468pub fn (handle &Handle) option_add_assumeinstalled(dep &Depend) ! {
1469 mut core := handle.native_handle()!
1470 core.add_assumed(engine_dependency(dep))!
1471}
1472
1473pub fn (handle &Handle) option_set_assumeinstalled(deps &List) ! {
1474 mut core := handle.native_handle()!
1475 core.set_assumed(deps.native_dependencies())!
1476}
1477
1478pub fn (handle &Handle) option_remove_assumeinstalled(dep &Depend) ! {
1479 mut core := handle.native_handle()!
1480 core.remove_assumed(engine_dependency(dep))!
1481}
1482
1483pub fn (handle &Handle) option_get_architectures() ?&List {
1484 mut core := handle.native_handle() or { return none }
1485 list := handle.native_strings('option:architectures', core.string_list(.architectures) or { return none })
1486 return if list == unsafe { nil } { none } else { list }
1487}
1488
1489pub fn (handle &Handle) option_add_architecture(architecture string) ! {
1490 mut core := handle.native_handle_preserving_error()!
1491 core.add_string_list(.architectures, architecture)!
1492}
1493
1494pub fn (handle &Handle) option_set_architectures(arches &List) ! {
1495 mut core := handle.native_handle()!
1496 core.set_string_list(.architectures, arches.native_strings())!
1497}
1498
1499pub fn (handle &Handle) option_remove_architecture(architecture string) ! {
1500 mut core := handle.native_handle()!
1501 core.remove_string_list(.architectures, architecture)!
1502}
1503
1504pub fn (handle &Handle) option_get_checkspace() bool {
1505 mut core := handle.native_handle() or { return false }
1506 value := core.integer_option(.check_space) or { return false }
1507 return value != 0
1508}
1509
1510pub fn (handle &Handle) option_set_checkspace(checkspace int) ! {
1511 mut core := handle.native_handle()!
1512 core.set_integer_option(.check_space, i64(checkspace))!
1513}
1514
1515pub fn (handle &Handle) option_get_dbext() string {
1516 mut core := handle.native_handle() or { return '' }
1517 return core.string_option(.db_extension) or { '' }
1518}
1519
1520pub fn (handle &Handle) option_set_dbext(dbext string) ! {
1521 mut core := handle.native_handle()!
1522 core.set_string_option(.db_extension, dbext)!
1523}
1524
1525pub fn (handle &Handle) option_get_default_siglevel() ?SigLevel {
1526 mut core := handle.native_handle() or { return none }
1527 value := core.integer_option(.default_siglevel) or { return none }
1528 return unsafe { SigLevel(value) }
1529}
1530
1531pub fn (handle &Handle) option_set_default_siglevel(level SigLevel) ! {
1532 mut core := handle.native_handle()!
1533 core.set_integer_option(.default_siglevel, i64(level))!
1534}
1535
1536pub fn (handle &Handle) option_get_local_file_siglevel() ?SigLevel {
1537 mut core := handle.native_handle() or { return none }
1538 value := core.integer_option(.local_file_siglevel) or { return none }
1539 return unsafe { SigLevel(value) }
1540}
1541
1542pub fn (handle &Handle) option_set_local_file_siglevel(level SigLevel) ! {
1543 mut core := handle.native_handle()!
1544 core.set_integer_option(.local_file_siglevel, i64(level))!
1545}
1546
1547pub fn (handle &Handle) option_get_remote_file_siglevel() ?SigLevel {
1548 mut core := handle.native_handle() or { return none }
1549 value := core.integer_option(.remote_file_siglevel) or { return none }
1550 return unsafe { SigLevel(value) }
1551}
1552
1553pub fn (handle &Handle) option_set_remote_file_siglevel(level SigLevel) ! {
1554 mut core := handle.native_handle()!
1555 core.set_integer_option(.remote_file_siglevel, i64(level))!
1556}
1557
1558pub fn (handle &Handle) option_get_disable_dl_timeout() bool {
1559 mut core := handle.native_handle() or { return false }
1560 value := core.integer_option(.disable_download_timeout) or { return false }
1561 return value != 0
1562}
1563
1564pub fn (handle &Handle) option_set_disable_dl_timeout(disable_dl_timeout u16) ! {
1565 mut core := handle.native_handle()!
1566 core.set_integer_option(.disable_download_timeout, i64(disable_dl_timeout))!
1567}
1568
1569pub fn (handle &Handle) option_get_parallel_downloads() int {
1570 mut core := handle.native_handle() or { return -1 }
1571 value := core.integer_option(.parallel_downloads) or { return -1 }
1572 return int(value)
1573}
1574
1575pub fn (handle &Handle) option_set_parallel_downloads(num_streams u32) ! {
1576 mut core := handle.native_handle()!
1577 core.set_integer_option(.parallel_downloads, i64(num_streams))!
1578}
1579
1580// the combined C symbols are not exported by the referenced libalpm build.
1581pub fn (handle &Handle) option_get_disable_sandbox() bool {
1582 return handle.option_get_disable_sandbox_filesystem() || handle.option_get_disable_sandbox_syscalls()
1583}
1584
1585pub fn (handle &Handle) option_set_disable_sandbox(disable_sandbox u16) ! {
1586 handle.option_set_disable_sandbox_filesystem(disable_sandbox)!
1587 handle.option_set_disable_sandbox_syscalls(disable_sandbox)!
1588}
1589
1590pub fn (handle &Handle) option_get_disable_sandbox_filesystem() bool {
1591 mut core := handle.native_handle() or { return false }
1592 value := core.integer_option(.disable_sandbox_filesystem) or { return false }
1593 return value != 0
1594}
1595
1596pub fn (handle &Handle) option_set_disable_sandbox_filesystem(disable_sandbox_filesystem u16) ! {
1597 mut core := handle.native_handle()!
1598 core.set_integer_option(.disable_sandbox_filesystem, i64(disable_sandbox_filesystem))!
1599}
1600
1601pub fn (handle &Handle) option_get_disable_sandbox_syscalls() bool {
1602 mut core := handle.native_handle() or { return false }
1603 value := core.integer_option(.disable_sandbox_syscalls) or { return false }
1604 return value != 0
1605}
1606
1607pub fn (handle &Handle) option_set_disable_sandbox_syscalls(disable_sandbox_syscalls u16) ! {
1608 mut core := handle.native_handle()!
1609 core.set_integer_option(.disable_sandbox_syscalls, i64(disable_sandbox_syscalls))!
1610}
1611
1612pub enum PackageReason {
1613 explicit = 0
1614 depend = 1
1615 unknown = 2
1616}
1617
1618pub enum PackageFrom {
1619 file = 1
1620 localdb
1621 syncdb
1622}
1623
1624pub enum PkgValidation {
1625 unknown = 0
1626 none = 1 << 0
1627 md5sum = 1 << 1
1628 sha256sum = 1 << 2
1629 signature = 1 << 3
1630}
1631
1632pub fn (handle &Handle) pkg_load(filename string, full int, level int) !&Package {
1633 mut core := handle.native_handle()!
1634 return handle.native_package(core.load_package(filename, full != 0, u32(level))!)
1635}
1636
1637pub fn (handle &Handle) fetch_pkgurl(urls &List) !&List {
1638 mut core := handle.native_handle()!
1639 return native_owned_strings(core.fetch_package_urls(urls.native_strings())!)
1640}
1641
1642pub fn (haystack &List) pkg_find(needle string) ?&Package {
1643 name := needle.all_before('\x00')
1644 for node := unsafe { haystack }; node != unsafe { nil }; node = node.next {
1645 pkg := node.get_pkg() or { continue }
1646 // package.c searches stored names without clearing either owner's error.
1647 if pkg.core != unsafe { nil } && pkg.core.name == name { return pkg }
1648 }
1649 return none
1650}
1651
1652pub fn (pkg &Package) free_pkg() ! {
1653 pkg.native_package()!
1654 if pkg == unsafe { nil } { return }
1655 if pkg.core.origin != .file { return }
1656 mut owner := unsafe { pkg.owner }
1657 owner.packages.delete(u64(voidptr(pkg.core)))
1658}
1659
1660pub fn (pkg &Package) checkmd5sum() int {
1661 value := pkg.native_package() or { return -1 }
1662 mut core := pkg.owner.native_handle() or { return -1 }
1663 return core.check_package_md5sum(value)
1664}
1665
1666pub fn pkg_vercmp(a string, b string) int {
1667 return engine.pkg_vercmp(a, b)
1668}
1669
1670// installed packages use their local db; sync packages use every sync db,
1671// as pacman's -Sii does. the returned names are V-owned copies.
1672pub fn (pkg &Package) compute_required_by() []string {
1673 pkg.native_package() or { return []string{} }
1674 mut core := pkg.owner.native_handle() or { return []string{} }
1675 return core.reverse_dependencies(pkg.core, false) or { []string{} }
1676}
1677
1678pub fn (pkg &Package) compute_optional_for() []string {
1679 pkg.native_package() or { return []string{} }
1680 mut core := pkg.owner.native_handle() or { return []string{} }
1681 return core.reverse_dependencies(pkg.core, true) or { []string{} }
1682}
1683
1684pub fn (handle &Handle) pkg_should_ignore(pkg &Package) int {
1685 mut core := handle.native_handle_preserving_error() or { return -1 }
1686 if pkg == unsafe { nil } || pkg.core == unsafe { nil } { return -1 }
1687 pkg.owner.native_handle_preserving_error() or { return -1 }
1688 mut value := unsafe { pkg.core }
1689 return int(core.should_ignore(mut value) or { return -1 })
1690}
1691
1692pub fn (pkg &Package) get_handle() &Handle {
1693 if pkg == unsafe { nil } { return unsafe { nil } }
1694 return pkg.owner
1695}
1696
1697@[inline]
1698fn cstring_to_vstring_or_none(s &u8) string {
1699 return if s == unsafe { nil } { '' } else { unsafe { cstring_to_vstring(s) } }
1700}
1701
1702pub fn (pkg &Package) get_filename() string {
1703 pkg.native_metadata(false, false) or { return '' }
1704 return pkg.core.filename
1705}
1706
1707pub fn (pkg &Package) get_base() string {
1708 pkg.native_metadata(true, false) or { return '' }
1709 return pkg.core.base
1710}
1711
1712pub fn (pkg &Package) get_name() string {
1713 pkg.native_metadata(false, false) or { return '' }
1714 return pkg.core.name
1715}
1716
1717pub fn (pkg &Package) get_version() string {
1718 pkg.native_metadata(false, false) or { return '' }
1719 return pkg.core.version
1720}
1721
1722pub fn (pkg &Package) get_origin() PackageFrom {
1723 pkg.native_metadata(false, false) or { return PackageFrom.file }
1724 return unsafe { PackageFrom(pkg.core.origin) }
1725}
1726
1727pub fn (pkg &Package) get_desc() string {
1728 pkg.native_metadata(true, false) or { return '' }
1729 return pkg.core.desc
1730}
1731
1732pub fn (pkg &Package) get_url() string {
1733 pkg.native_metadata(true, false) or { return '' }
1734 return pkg.core.url
1735}
1736
1737pub fn (pkg &Package) get_build_date() Time {
1738 pkg.native_metadata(true, false) or { return Time(0) }
1739 return pkg.core.build_date
1740}
1741
1742pub fn (pkg &Package) get_install_date() Time {
1743 pkg.native_metadata(true, false) or { return Time(0) }
1744 return pkg.core.install_date
1745}
1746
1747pub fn (pkg &Package) get_packager() string {
1748 pkg.native_metadata(true, false) or { return '' }
1749 return pkg.core.packager
1750}
1751
1752pub fn (pkg &Package) get_md5_sum() string {
1753 pkg.native_metadata(false, false) or { return '' }
1754 return pkg.core.md5sum
1755}
1756
1757pub fn (pkg &Package) get_sha256_sum() string {
1758 pkg.native_metadata(false, false) or { return '' }
1759 return pkg.core.sha256sum
1760}
1761
1762pub fn (pkg &Package) get_arch() string {
1763 pkg.native_metadata(true, false) or { return '' }
1764 return pkg.core.architecture
1765}
1766
1767pub fn (pkg &Package) get_size() i64 {
1768 pkg.native_metadata(false, false) or { return i64(0) }
1769 return pkg.core.size
1770}
1771
1772pub fn (pkg &Package) get_isize() i64 {
1773 pkg.native_metadata(true, false) or { return i64(0) }
1774 return pkg.core.isize
1775}
1776
1777pub fn (pkg &Package) get_reason() PackageReason {
1778 pkg.native_metadata(true, false) or { return unsafe { PackageReason(0) } }
1779 return unsafe { PackageReason(pkg.core.reason) }
1780}
1781
1782pub fn (pkg &Package) get_licenses() &List {
1783 pkg.native_metadata(true, false) or { return unsafe { nil } }
1784 return pkg.owner.native_strings('pkg:${u64(voidptr(pkg.core))}:licenses', pkg.core.licenses)
1785}
1786
1787pub fn (pkg &Package) get_groups() &List {
1788 pkg.native_metadata(true, false) or { return unsafe { nil } }
1789 return pkg.owner.native_strings('pkg:${u64(voidptr(pkg.core))}:groups', pkg.core.groups)
1790}
1791
1792pub fn (pkg &Package) get_depends() &List {
1793 pkg.native_metadata(true, false) or { return unsafe { nil } }
1794 return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:depends', pkg.core.depends)
1795}
1796
1797pub fn (pkg &Package) get_opt_depends() &List {
1798 pkg.native_metadata(true, false) or { return unsafe { nil } }
1799 return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:optdepends', pkg.core.optdepends)
1800}
1801
1802pub fn (pkg &Package) get_check_depends() &List {
1803 pkg.native_metadata(true, false) or { return unsafe { nil } }
1804 return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:checkdepends', pkg.core.checkdepends)
1805}
1806
1807pub fn (pkg &Package) get_make_depends() &List {
1808 pkg.native_metadata(true, false) or { return unsafe { nil } }
1809 return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:makedepends', pkg.core.makedepends)
1810}
1811
1812pub fn (pkg &Package) get_conflicts() &List {
1813 pkg.native_metadata(true, false) or { return unsafe { nil } }
1814 return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:conflicts', pkg.core.conflicts)
1815}
1816
1817pub fn (pkg &Package) get_provides() &List {
1818 pkg.native_metadata(true, false) or { return unsafe { nil } }
1819 list := pkg.native_provides()
1820 if list != unsafe { nil } {
1821 mut p := unsafe { pkg }
1822 p.provides_exposed = true
1823 }
1824 return list
1825}
1826
1827fn (pkg &Package) native_provides() &List {
1828 return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:provides', pkg.core.provides)
1829}
1830
1831pub fn (pkg &Package) get_replaces() &List {
1832 pkg.native_metadata(true, false) or { return unsafe { nil } }
1833 return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:replaces', pkg.core.replaces)
1834}
1835
1836pub fn (pkg &Package) get_files() &Filelist {
1837 if pkg == unsafe { nil } || pkg.core == unsafe { nil } { return unsafe { nil } }
1838 pkg.native_metadata(false, true) or { return unsafe { &pkg.filelist } }
1839 mut p := unsafe { pkg }
1840 if p.file_versions.len == 0 || !p.file_versions.last().matches(pkg.core.files) {
1841 p.file_versions << &NativeFiles{
1842 values: pkg.core.files.map(File{ name: it.name.str, size: it.size, mode: it.mode })
1843 }
1844 }
1845 storage := p.file_versions.last()
1846 p.filelist = Filelist{
1847 count: uint(storage.values.len)
1848 files: if storage.values.len == 0 {
1849 unsafe { nil }
1850 } else {
1851 storage.values.data
1852 }
1853 }
1854 return &p.filelist
1855}
1856
1857pub fn (pkg &Package) get_backup() &List {
1858 pkg.native_metadata(false, true) or { return unsafe { nil } }
1859 key := 'backups:${u64(voidptr(pkg.core))}:' + pkg.core.backup.map('${it.name.len}:${it.name}:${it.hash.len}:${it.hash}:${it.has_hash}').join(';')
1860 if existing := pkg.owner.lists[key] { return existing }
1861 mut list := &List(unsafe { nil })
1862 for item in pkg.core.backup {
1863 record := &Backup{
1864 name: pkg.owner.native_buffer(item.name)
1865 hash: if item.has_hash {
1866 pkg.owner.native_buffer(item.hash)
1867 } else {
1868 unsafe { nil }
1869 }
1870 }
1871 // unmanaged list nodes must not be the only roots of managed records.
1872 buffer := unsafe { C.malloc(sizeof(Backup)) }
1873 if buffer == unsafe { nil } { return unsafe { nil } }
1874 unsafe { vmemcpy(buffer, record, int(sizeof(Backup))) }
1875 pkg.owner.native_own_buffer(buffer)
1876 list = list.add(buffer)
1877 }
1878 mut owner := unsafe { pkg.owner }
1879 owner.lists[key] = list
1880 return pkg.owner.native_borrow_list(list)
1881}
1882
1883pub fn (pkg &Package) get_db() !&DB {
1884 pkg.native_package()!
1885 db := pkg.core.owner_database()!
1886 return pkg.owner.native_database(db)
1887}
1888
1889pub fn (pkg &Package) get_base64_sig() string {
1890 pkg.native_metadata(false, false) or { return '' }
1891 return pkg.core.base64_sig
1892}
1893
1894pub fn (pkg &Package) get_sig(sig_len &uint) !&u8 {
1895 if pkg == unsafe { nil } || pkg.core == unsafe { nil } { return Errno.wrong_args.ierror() }
1896 if pkg.owner == unsafe { nil } || pkg.owner.core == unsafe { nil } {
1897 return Errno.handle_null.ierror()
1898 }
1899 mut core := unsafe { pkg.owner.core }
1900 data := core.package_signature_bytes(pkg.core, sig_len)!
1901 return native_bytes(data, sig_len) or { return pkg.owner.native_failure(.memory, err.msg()) }
1902}
1903
1904pub fn (pkg &Package) get_validation() PkgValidation {
1905 pkg.native_metadata(true, false) or { return unsafe { PkgValidation(0) } }
1906 return unsafe { PkgValidation(pkg.core.validation) }
1907}
1908
1909pub fn (pkg &Package) get_xdata() &List {
1910 pkg.native_metadata(true, false) or { return unsafe { nil } }
1911 key := 'xdata:${u64(voidptr(pkg.core))}:' + pkg.core.xdata.map('${it.name.len}:${it.name}:${it.value.len}:${it.value}').join(';')
1912 if existing := pkg.owner.lists[key] { return existing }
1913 mut list := &List(unsafe { nil })
1914 for item in pkg.core.xdata {
1915 mut record := unsafe { &PkgXdata(C.calloc(1, sizeof(PkgXdata))) }
1916 if record == unsafe { nil } { return unsafe { nil } }
1917 unsafe {
1918 record.name = pkg.owner.native_buffer(item.name)
1919 record.value = pkg.owner.native_buffer(item.value)
1920 }
1921 pkg.owner.native_own_buffer(record)
1922 list = list.add(record)
1923 }
1924 mut owner := unsafe { pkg.owner }
1925 owner.lists[key] = list
1926 return pkg.owner.native_borrow_list(list)
1927}
1928
1929pub fn (pkg &Package) has_scriptlet() bool {
1930 pkg.native_metadata(false, false) or { return false }
1931 mut core := unsafe { pkg.core }
1932 core.load_scriptlet() or { return false }
1933 return core.scriptlet
1934}
1935
1936pub fn (newpkg &Package) download_size() i64 {
1937 if newpkg == unsafe { nil } || newpkg.core == unsafe { nil } { return -1 }
1938 if newpkg.owner == unsafe { nil } || newpkg.owner.core == unsafe { nil } { return -1 }
1939 mut core := unsafe { newpkg.owner.core }
1940 mut value := unsafe { newpkg.core }
1941 return core.package_download_size_value(mut value) or { -1 }
1942}
1943
1944pub fn (pkg &Package) set_reason(reason PackageReason) ! {
1945 pkg.native_package()!
1946 mut core := unsafe { pkg.core }
1947 core.set_reason(unsafe { engine.InstallReason(reason) })!
1948}
1949
1950pub fn (pkg &Package) changelog_open() voidptr {
1951 pkg.native_package() or { return unsafe { nil } }
1952 if pkg.core.origin == .file {
1953 mut core := unsafe { pkg.owner.core }
1954 return core.open_file_changelog(pkg.core.filename)
1955 }
1956 if pkg.core.origin != .local_db { return unsafe { nil } }
1957 path := os.join_path(pkg.owner.core.dbpath, 'local', pkg.core.name + '-' + pkg.core.version, 'changelog')
1958 return C.fopen(unsafe { &char(path.str) }, c'r')
1959}
1960
1961pub fn (pkg &Package) changelog_read(ptr voidptr, size uint, fp voidptr) uint {
1962 pkg.native_package() or { return 0 }
1963 if fp == unsafe { nil } { return 0 }
1964 if pkg.core.origin == .file {
1965 mut core := unsafe { pkg.owner.core }
1966 return core.read_file_changelog(ptr, size, fp)
1967 }
1968 if pkg.core.origin != .local_db { return 0 }
1969 return C.fread(ptr, 1, size, fp)
1970}
1971
1972pub fn (pkg &Package) changelog_close(fp voidptr) ! {
1973 pkg.native_package()!
1974 if fp == unsafe { nil } { return Errno.wrong_args.ierror() }
1975 if pkg.core.origin == .file {
1976 mut core := unsafe { pkg.owner.core }
1977 return core.close_file_changelog(fp)
1978 }
1979 if pkg.core.origin != .local_db || C.fclose(fp) != 0 { return pkg.owner.error() }
1980}
1981
1982pub fn (pkg &Package) mtree_open() &C.Archive {
1983 pkg.native_package() or { return unsafe { nil } }
1984 if pkg.core.origin != .local_db { return unsafe { nil } }
1985 path := os.join_path(pkg.owner.core.dbpath, 'local', pkg.core.name + '-' + pkg.core.version, 'mtree')
1986 if C.access(unsafe { &char(path.str) }, C.F_OK) != 0 { return unsafe { nil } }
1987 archive := C.archive_read_new()
1988 if archive == unsafe { nil } {
1989 pkg.owner.native_failure(.libarchive, '')
1990 return archive
1991 }
1992 C.archive_read_support_filter_all(archive)
1993 C.archive_read_support_format_mtree(archive)
1994 if C.archive_read_open_filename(archive, path.str, uint(C.BUFSIZ)) != 0 {
1995 message := cstring_to_vstring_or_none(unsafe { &u8(C.archive_error_string(archive)) })
1996 native_log(pkg.owner, .error, 'error while reading file ${path}: ${message}\n')
1997 C.archive_read_free(archive)
1998 pkg.owner.native_failure(.libarchive, message)
1999 return unsafe { nil }
2000 }
2001 return archive
2002}
2003
2004// the legacy wrapper advances but cannot propagate its pointer output.
2005// mtree_step returns true for an entry, false for EOF, and preserves read errors.
2006pub fn (pkg &Package) mtree_step(archive &C.Archive, entry &&C.Archive_entry) !bool {
2007 pkg.native_package()!
2008 if archive == unsafe { nil } || entry == unsafe { nil } {
2009 return Errno.wrong_args.ierror()
2010 }
2011 if pkg.core.origin != .local_db { return error('mtree read failed with status -1') }
2012 status := C.archive_read_next_header(archive, entry)
2013 if status == C.ARCHIVE_OK { return true }
2014 if status == C.ARCHIVE_EOF { return false }
2015 message := cstring_to_vstring_or_none(unsafe { &u8(C.archive_error_string(archive)) })
2016 return error(if message != '' { message } else { 'mtree read failed with status -1' })
2017}
2018
2019pub fn (pkg &Package) mtree_next(archive &C.Archive, entry &C.Archive_entry) ! {
2020 pkg.native_package()!
2021 if archive == unsafe { nil } { return Errno.wrong_args.ierror() }
2022 mut output := unsafe { entry }
2023 if pkg.core.origin != .local_db || C.archive_read_next_header(archive, &output) != C.ARCHIVE_OK {
2024 return pkg.owner.error()
2025 }
2026}
2027
2028pub fn (pkg &Package) mtree_close(archive &C.Archive) ! {
2029 pkg.native_package()!
2030 if archive == unsafe { nil } { return Errno.wrong_args.ierror() }
2031 if pkg.core.origin != .local_db || C.archive_read_free(archive) != 0 {
2032 return pkg.owner.error()
2033 }
2034}
2035
2036@[flag]
2037pub enum TransFlag {
2038 no_deps = 0
2039 no_save = 2
2040 no_depversion = 3
2041 cascade = 4
2042 recurse = 5
2043 db_only = 6
2044 no_hooks = 7
2045 all_deps = 8
2046 download_only = 9
2047 no_scriptlet = 10
2048 no_conflicts = 11
2049 needed = 13
2050 all_explicit = 14
2051 unneeded = 15
2052 recurse_all = 16
2053 nolock = 17
2054}
2055
2056pub const trans_flag_0 = TransFlag.zero()
2057
2058pub fn (handle &Handle) trans_get_flags() TransFlag {
2059 mut core := handle.native_handle() or { return trans_flag_0 }
2060 return unsafe { TransFlag(core.transaction_flags() or { return trans_flag_0 }) }
2061}
2062
2063pub fn (handle &Handle) trans_get_add() &List {
2064 mut core := handle.native_handle() or { return unsafe { nil } }
2065 return handle.native_cached_packages('trans:add', core.transaction_add() or { return unsafe { nil } })
2066}
2067
2068pub fn (handle &Handle) trans_get_remove() &List {
2069 mut core := handle.native_handle() or { return unsafe { nil } }
2070 return handle.native_cached_packages('trans:remove', core.transaction_remove() or { return unsafe { nil } })
2071}
2072
2073pub fn (handle &Handle) trans_init(flags TransFlag) ! {
2074 mut core := handle.native_handle()!
2075 core.start_transaction(unsafe { engine.TransactionFlag(flags) })!
2076}
2077
2078pub fn (handle &Handle) trans_prepare() !&List {
2079 mut core := handle.native_handle()!
2080 result := core.prepare_transaction()!
2081 if !result.success { return core.error() }
2082 return unsafe { nil }
2083}
2084
2085// DepProblem is one dependency that a failed prepare found broken: the package
2086// that needs it, the dependency itself, and the package whose removal or
2087// replacement breaks it
2088pub struct DepProblem {
2089pub:
2090 target string
2091 depend string
2092 causing string
2093}
2094
2095// preserve structured dependency, conflict and architecture failures while
2096// copying their strings before another getter can reset the handle's errno.
2097pub fn (handle &Handle) trans_prepare_problems() ([]DepProblem, ?IError) {
2098 mut core := handle.native_handle() or { return []DepProblem{}, err }
2099 result := core.prepare_transaction() or { return []DepProblem{}, err }
2100 if result.success { return []DepProblem{}, none }
2101 problem := core.error()
2102 mut problems := []DepProblem{}
2103 for miss in result.missing {
2104 problems << DepProblem{ target: miss.target, depend: miss.dependency.compute_string(), causing: miss.causing_package }
2105 }
2106 for conflict in result.conflicts {
2107 problems << DepProblem{ target: conflict.package1.name, depend: conflict.reason.compute_string(), causing: conflict.package2.name }
2108 }
2109 for architecture in result.invalid_architectures {
2110 problems << DepProblem{ target: architecture }
2111 }
2112 return problems, problem
2113}
2114
2115pub fn (handle &Handle) trans_commit() !&List {
2116 mut core := handle.native_handle()!
2117 core.commit_transaction()!
2118 return unsafe { nil }
2119}
2120
2121pub fn (handle &Handle) trans_interrupt() ! {
2122 mut core := handle.native_handle()!
2123 core.interrupt_transaction()!
2124}
2125
2126pub fn (handle &Handle) trans_release() ! {
2127 mut core := handle.native_handle()!
2128 core.release_transaction()!
2129}
2130
2131pub fn (handle &Handle) sync_sysupgrade(enable_downgrade bool) ! {
2132 mut core := handle.native_handle()!
2133 core.plan_system_upgrade(enable_downgrade)!
2134}
2135
2136pub fn (handle &Handle) add_pkg(pkg &Package) ! {
2137 mut core := handle.native_handle()!
2138 if pkg == unsafe { nil } { return core.set_error(.wrong_args, 'null package') }
2139 core.add_package(pkg.core)!
2140}
2141
2142pub fn (handle &Handle) remove_pkg(pkg &Package) ! {
2143 mut core := handle.native_handle()!
2144 if pkg == unsafe { nil } { return core.set_error(.wrong_args, 'null package') }
2145 core.remove_package(pkg.core)!
2146}
2147
2148pub fn (dbs_sync &List) sync_get_new_version(pkg &Package) &Package {
2149 pkg.native_package() or { return unsafe { nil } }
2150 found := engine.find_new_version(pkg.core, dbs_sync.native_databases()) or { return unsafe { nil } }
2151 return pkg.owner.native_package(found)
2152}
2153
2154pub fn compute_md5sum(filename string) ?string {
2155 return engine.compute_md5sum(filename)
2156}
2157
2158pub fn compute_sha256sum(filename string) ?string {
2159 return engine.compute_sha256sum(filename)
2160}
2161
2162pub fn (handle &Handle) unlock() ! {
2163 if handle.unlock_async() != 0 {
2164 return handle.error()
2165 }
2166}
2167
2168// status only: the signal path cannot allocate a V error or run callbacks.
2169pub fn (handle &Handle) unlock_async() int {
2170 if handle == unsafe { nil } || handle.core == unsafe { nil } { return -1 }
2171 mut core := unsafe { handle.core }
2172 return core.unlock_async()
2173}
2174
2175// bit positions, as for SigLevel
2176@[flag]
2177pub enum Caps {
2178 nls = 0
2179 downloader = 1
2180 signatures = 2
2181}
2182
2183pub fn version() string {
2184 return engine.engine_version
2185}
2186
2187pub fn capabilities() Caps {
2188 return Caps.nls | .downloader | .signatures
2189}
2190
2191pub fn (handle &Handle) sandbox_setup_child(sandboxuser string, sandbox_path string, restrict_syscalls bool) ! {
2192 mut core := handle.native_handle_preserving_error()!
2193 path := if sandbox_path.str == unsafe { nil } { ?string(none) } else { ?string(sandbox_path) }
2194 core.sandbox_setup_child(sandboxuser, path, restrict_syscalls)!
2195}
2196
2197// helpers built on the API above: finding packages by name, walking the
2198// dependency graph and reading the file list
2199
2200// strip_db_prefix removes a db prefix from a target, the way pacman accepts
2201// 'local/patchelf' or 'system/bash'
2202pub fn (handle &Handle) strip_db_prefix(target string) string {
2203 if target.starts_with('local/') && target.len > 6 {
2204 return target[6..]
2205 }
2206 dbs := handle.get_syncdbs() or { return target }
2207 for db in dbs.dbs() {
2208 prefix := db.get_name() + '/'
2209 if target.starts_with(prefix) && target.len > prefix.len {
2210 return target[prefix.len..]
2211 }
2212 }
2213 return target
2214}
2215
2216// find_local_pkg looks a package up by name in the local db
2217pub fn (handle &Handle) find_local_pkg(name string) ?&Package {
2218 localdb := handle.get_localdb() or { return none }
2219 mut db := localdb.native_database() or { return none }
2220 found := db.find_cached_package(name) or { return none }
2221 return handle.native_package(found)
2222}
2223
2224// resolve_dep finds what satisfies a dependency string. the installed package
2225// wins, so a tree reflects what is actually on the system, and the sync dbs
2226// answer for everything else
2227pub fn (handle &Handle) resolve_dep(installed &List, sync_dbs &List, depstring string) ?&Package {
2228 if pkg := installed.find_satisfier(depstring) {
2229 return pkg
2230 }
2231 return handle.find_dbs_satisfier(sync_dbs, depstring)
2232}
2233
2234// outdated_deps walks the dependency closure of pkg against the sync dbs and
2235// returns the sync versions of installed packages that are older than the
2236// closure needs. only those are returned, so this is not a full upgrade
2237pub fn (handle &Handle) outdated_deps(sync_dbs &List, pkg &Package) []&Package {
2238 mut out := []&Package{}
2239 mut seen := map[string]bool{}
2240 seen[pkg.get_name()] = true
2241 mut queue := []&Package{}
2242 queue << pkg
2243 mut i := 0
2244 for i < queue.len {
2245 current := queue[i]
2246 i++
2247 for dep in current.get_depends().depends() {
2248 cand := handle.find_dbs_satisfier(sync_dbs, dep.compute_string()) or { continue }
2249 name := cand.get_name()
2250 if seen[name] {
2251 continue
2252 }
2253 seen[name] = true
2254 if installed := handle.find_local_pkg(name) {
2255 if pkg_vercmp(installed.get_version(), cand.get_version()) < 0 {
2256 out << cand
2257 }
2258 }
2259 queue << cand
2260 }
2261 }
2262 return out
2263}
2264
2265// is_unrequired is pacman's -Qdt filter: installed as a dependency, required
2266// by nothing and not an optional dependency of anything
2267pub fn (pkg &Package) is_unrequired() bool {
2268 return pkg.get_reason() == .depend && pkg.compute_required_by().len == 0
2269 && pkg.compute_optional_for().len == 0
2270}
2271
2272// names returns the paths in a file list
2273pub fn (filelist &Filelist) names() []string {
2274 mut out := []string{cap: int(filelist.count)}
2275 for i in 0 .. int(filelist.count) {
2276 out << unsafe { filelist.files[i].name.vstring() }
2277 }
2278 return out
2279}
2280
2281// unneeded_closure extends a set of orphans to everything that becomes
2282// unneeded once they are gone. a dependency is unneeded when every package
2283// that requires it, and every package that optionally requires it, is going
2284// away too. the set is grown to a fixpoint, so one removal is enough
2285pub fn (handle &Handle) unneeded_closure(seed []&Package) []&Package {
2286 mut localdb := handle.get_localdb() or { return seed }
2287 mut pkgcache := localdb.get_pkgcache() or { return seed }
2288 installed := pkgcache.pkgs()
2289
2290 mut removed := map[string]bool{}
2291 mut out := []&Package{}
2292 for p in seed {
2293 removed[p.get_name()] = true
2294 out << p
2295 }
2296 mut changed := true
2297 for changed {
2298 changed = false
2299 for p in installed {
2300 name := p.get_name()
2301 if removed[name] || p.get_reason() != .depend {
2302 continue
2303 }
2304 required := p.compute_required_by()
2305 optional := p.compute_optional_for()
2306 mut kept := false
2307 for r in required {
2308 if !removed[r] {
2309 kept = true
2310 break
2311 }
2312 }
2313 for o in optional {
2314 if !removed[o] {
2315 kept = true
2316 break
2317 }
2318 }
2319 if !kept {
2320 removed[name] = true
2321 out << p
2322 changed = true
2323 }
2324 }
2325 }
2326 return out
2327}
2328
2329fn engine_dependency(dep &Depend) engine.Dependency {
2330 return engine.dependency_from_fields(dep.get_name(), dep.get_version(), dep.get_desc(),
2331 unsafe { engine.Depmod(dep.mod) }, dep.desc != unsafe { nil })
2332}
2333
2334// a failed literal match can still be satisfied by one of the package's provisions.
2335pub fn (pkg &Package) satisfies(dep &Depend) bool {
2336 if pkg == unsafe { nil } || pkg.core == unsafe { nil } { return false }
2337 return pkg.satisfies_constraint(engine_dependency(dep))
2338}
2339
2340fn (pkg &Package) satisfies_constraint(constraint engine.Dependency) bool {
2341 if pkg.core == unsafe { nil } { return false }
2342 // deps.c only calls a metadata getter after the literal match fails.
2343 if pkg.core.name == constraint.name() && engine.version_satisfies(pkg.core.version, constraint) {
2344 return true
2345 }
2346 pkg.native_metadata(true, false) or { return false }
2347 if !pkg.provides_exposed {
2348 return pkg.core.provides.any(engine.provision_satisfies(it, constraint))
2349 }
2350 // A caller can edit the public Depend records after borrowing the list.
2351 for node := pkg.native_provides(); node != unsafe { nil }; node = node.next {
2352 provision := node.get_depend() or { break }
2353 if engine.provision_satisfies(engine_dependency(provision), constraint) {
2354 return true
2355 }
2356 }
2357 return false
2358}
2359
2360// sync_package finds a package by name in the sync dbs
2361fn sync_package(sync_dbs &List, name string) ?&Package {
2362 mut dbs := unsafe { sync_dbs }
2363 for db in dbs.dbs() {
2364 if p := db.get_pkg(name) {
2365 return p
2366 }
2367 }
2368 return none
2369}
2370
2371// dep_still_met reports whether a dependency of a package that is about to
2372// be installed holds after the upgrades. only an installed provider that is
2373// itself being upgraded can break it
2374fn dep_still_met(pkgcache &List, new_ver map[string]&Package, dep &Depend) bool {
2375 sat := pkgcache.find_satisfier(dep.compute_string()) or { return true }
2376 provider := new_ver[sat.get_name()] or { return true }
2377 return provider.satisfies(dep)
2378}
2379
2380// broken_dependents returns the sync packages that have to go along with an
2381// upgrade. an installed package whose dependency is met by a package being
2382// upgraded, but not by the new version of it, is upgraded to its sync version.
2383// that version must be satisfied by the new versions, or the upgrade is
2384// refused, since nothing sensible can be done with it
2385pub fn (handle &Handle) broken_dependents(upgrades []&Package, sync_dbs &List) ![]&Package {
2386 mut localdb := handle.get_localdb()!
2387 mut pkgcache := localdb.get_pkgcache() or { return error('failed to get pkgcache') }
2388 installed := pkgcache.pkgs()
2389
2390 mut new_ver := map[string]&Package{}
2391 for p in upgrades {
2392 new_ver[p.get_name()] = p
2393 }
2394 mut extra := []&Package{}
2395 mut changed := true
2396 for changed {
2397 changed = false
2398 for pkg in installed {
2399 name := pkg.get_name()
2400 if name in new_ver {
2401 continue
2402 }
2403 for dep in pkg.get_depends().depends() {
2404 sat := pkgcache.find_satisfier(dep.compute_string()) or { continue }
2405 provider := new_ver[sat.get_name()] or { continue }
2406 if provider.satisfies(dep) {
2407 continue
2408 }
2409 replacement := sync_package(sync_dbs, name) or {
2410 return error('${name} needs ${dep.compute_string()}, which the upgrade replaces with ${provider.get_name()} ${provider.get_version()}, and the repos have no ${name} to upgrade with')
2411 }
2412 for d in replacement.get_depends().depends() {
2413 if !dep_still_met(pkgcache, new_ver, d) {
2414 return error('the repo version of ${name} needs ${d.compute_string()}, which the upgrade does not provide')
2415 }
2416 }
2417 new_ver[name] = replacement
2418 extra << replacement
2419 changed = true
2420 break
2421 }
2422 }
2423 }
2424 return extra
2425}
2426