module nspkg import nspkg.engine import os #flag -std=gnu99 #include #include struct C.Archive {} struct C.Archive_acl {} struct C.Archive_entry {} struct C.Archive_entry_linkresolver {} type Size_t = u32 type Ulong = u32 type Ushort = u16 type Uint = u32 type U_int8_t = u8 type U_int16_t = u16 type U_int32_t = u32 type U_int64_t = u64 type Time_t = i64 type Off_t = i64 type Pid_t = int type Uid_t = u32 type Gid_t = u32 type Mode_t = u32 type Dev_t = u64 type Ino_t = u64 type Nlink_t = u64 type Blksize_t = i64 type Blkcnt_t = i64 type Clock_t = i64 type Clockid_t = int type Timer_t = voidptr type Suseconds_t = i64 type Useconds_t = u32 type Syscall_slong_t = i64 type Off64_t = i64 struct C.Timespec { tv_sec Time_t tv_nsec Syscall_slong_t } type La_int64_t = i64 type La_uint64_t = u64 type La_ssize_t = int pub fn (list &List) get_pkg() ?&Package { data := unsafe { &Package(list.data) } return if data == unsafe { nil } { none } else { data } } pub fn (list &List) get_db() ?&DB { data := unsafe { &DB(list.data) } return if data == unsafe { nil } { none } else { data } } pub fn (list &List) get_str() ?string { data := unsafe { &u8(list.data) } return if data == unsafe { nil } { none } else { unsafe { data.vstring() } } } pub fn (list &List) get_group() ?&Group { data := unsafe { &Group(list.data) } return if data == unsafe { nil } { none } else { data } } pub fn (list &List) get_backup() ?&Backup { if list.data == unsafe { nil } { return none } return &Backup(list.data) } pub fn (b &Backup) get_name() string { return cstring_to_vstring_or_none(b.name) } pub fn (b &Backup) get_hash() string { return cstring_to_vstring_or_none(b.hash) } pub fn (list &List) get_depend() ?&Depend { data := unsafe { &Depend(list.data) } return if data == unsafe { nil } { none } else { data } } pub fn (mut list List) pkgs() []&Package { mut out := []&Package{} for list != unsafe { nil } { if pkg := list.get_pkg() { out << pkg } else { break } list = list.next } return out } pub fn (mut list List) dbs() []&DB { mut out := []&DB{} for list != unsafe { nil } { if db := list.get_db() { out << db } else { break } list = list.next } return out } pub fn (mut list List) strs() []string { mut out := []string{} for list != unsafe { nil } { if s := list.get_str() { out << s } else { break } list = list.next } return out } pub fn (mut list List) groups() []&Group { mut out := []&Group{} for list != unsafe { nil } { if s := list.get_group() { out << s } else { break } list = list.next } return out } pub fn (mut list List) depends() []&Depend { mut out := []&Depend{} for list != unsafe { nil } { if s := list.get_depend() { out << s } else { break } list = list.next } return out } pub struct HandleW { pub mut: handle &Handle = unsafe { nil } err Errno } pub struct Handle { mut: core &engine.Handle = unsafe { nil } databases map[u64]&DB packages map[u64]&Package lists map[string]&List owned_lists []&List owned_dependencies []&Depend owned_buffers []voidptr groups map[u64]&Group root_anchor voidptr logcb CbLog = unsafe { nil } logctx voidptr log_line_context voidptr dlcb CbDownload = unsafe { nil } dlctx voidptr fetchcb CbFetch = unsafe { nil } fetchctx voidptr eventcb CbEvent = unsafe { nil } eventctx voidptr questioncb CbQuestion = unsafe { nil } questionctx voidptr progresscb CbProgress = unsafe { nil } progressctx voidptr } pub struct DB { mut: core &engine.Database = unsafe { nil } owner &Handle = unsafe { nil } package_revision u64 package_list &List = unsafe { nil } } pub struct Package { mut: core &engine.Package = unsafe { nil } owner &Handle = unsafe { nil } filelist Filelist file_versions []&NativeFiles provides_exposed bool } pub struct PkgXdata { name &u8 = unsafe { nil } value &u8 = unsafe { nil } } type Time = i64 pub struct File { name &u8 = unsafe { nil } size i64 mode u32 } pub struct Filelist { count uint files &File = unsafe { nil } } pub struct Backup { name &u8 = unsafe { nil } hash &u8 = unsafe { nil } } pub fn (filelist &Filelist) contains(path string) ?&File { if filelist == unsafe { nil } { return none } needle := path.all_before('\x00') mut low := 0 mut high := int(filelist.count) for low < high { mid := low + (high - low) / 2 item := unsafe { &filelist.files[mid] } current := cstring_to_vstring_or_none(item.name) if current == needle { return item } if current < needle { low = mid + 1 } else { high = mid } } return none } pub struct Group { pub mut: name &u8 = unsafe { nil } packages &List = unsafe { nil } } pub fn (group &Group) get_name() string { return cstring_to_vstring_or_none(group.name) } pub fn (dbs &List) find_group_pkgs(name string) &List { mut values := dbs.native_databases() if values.len == 0 { return unsafe { nil } } h := dbs.get_db() or { return unsafe { nil } } mut core := h.owner.native_handle() or { return unsafe { nil } } found := core.find_group_packages_for_transaction(values, name) or { return unsafe { nil } } return h.owner.native_package_list(found) } pub enum Errno { ok = 0 memory system badperms not_a_file not_a_dir wrong_args disk_space handle_null handle_not_null handle_lock db_open db_create db_null db_not_null db_not_found db_invalid db_invalid_sig db_version db_write db_remove server_bad_url server_none trans_not_null trans_null trans_dup_target trans_dup_filename trans_not_initialized trans_not_prepared trans_abort trans_type trans_not_locked trans_hook_failed pkg_not_found pkg_ignored pkg_invalid pkg_invalid_checksum pkg_invalid_sig pkg_missing_sig pkg_open pkg_cant_remove pkg_invalid_name pkg_invalid_arch sig_missing sig_invalid unsatisfied_deps conflicting_deps file_conflicts retrieve_prepare retrieve invalid_regex libarchive libcurl external_download gpgme missing_capability_signatures } pub fn (handle &Handle) errno() Errno { if handle == unsafe { nil } || handle.core == unsafe { nil } { return .handle_null } return unsafe { Errno(handle.core.errno()) } } pub fn (err Errno) strerror() string { return unsafe { engine.Errno(err) }.strerror() } pub fn (err Errno) ierror() IError { return error_with_code(err.strerror(), int(err)) } pub fn (handle &Handle) error() IError { if handle == unsafe { nil } || handle.core == unsafe { nil } { return Errno.handle_null.ierror() } return handle.errno().ierror() } pub fn initialize(root string, dbpath string) !&nspkg.Handle { mut core := engine.initialize(root, dbpath)! mut handle := &Handle{ core: core } handle.native_retain() or { mut failed := unsafe { core } failed.release() or { eprintln(err) } return err } core.bind_context(handle) or { handle.release() or { eprintln(err) } return err } handle.install_native_callbacks() or { handle.release() or { eprintln(err) } return err } return handle } pub fn (handle &Handle) release() ! { mut core := handle.native_handle()! line_context := handle.owned_log_line_context() core.release()! mut h := unsafe { handle } for list in h.owned_lists { list.free() } for dependency in h.owned_dependencies { dependency.free() } for buffer in h.owned_buffers { C.free(buffer) } h.owned_lists = []&List{} h.owned_dependencies = []&Depend{} h.owned_buffers = []voidptr{} h.lists = map[string]&List{} h.packages = map[u64]&Package{} h.databases = map[u64]&DB{} h.groups = map[u64]&Group{} C.free(line_context) h.logcb = unsafe { nil } h.logctx = unsafe { nil } h.log_line_context = unsafe { nil } h.native_unretain() } // the values of a @[flag] enum are bit positions, not masks: V turns // `database = 10` into 1 << 10, libalpm's ALPM_SIG_DATABASE. alpm_test.v // checks every flag enum against libalpm's own constants @[flag] pub enum SigLevel { package = 0 package_optional = 1 package_marginal_ok = 2 package_unknown_ok = 3 database = 10 database_optional = 11 database_marginal_ok = 12 database_unknown_ok = 13 use_default = 30 } pub enum SigStatus { valid key_expired sig_expired key_unknown key_disabled invalid } pub enum SigValidity { full marginal never unknown } pub struct PGPKey { pub mut: data voidptr = unsafe { nil } fingerprint &u8 = unsafe { nil } uid &u8 = unsafe { nil } name &u8 = unsafe { nil } email &u8 = unsafe { nil } created Time expires Time length u32 revoked u32 } pub struct SigResult { pub mut: key PGPKey status SigStatus validity SigValidity } pub struct SigList { pub mut: count uint results &SigResult = unsafe { nil } } pub fn (pkg &Package) check_pgp_signature() ?&SigList { pkg.native_package() or { return none } mut core := pkg.owner.native_handle() or { return none } path := pkg.native_signature_path() or { return none } embedded := if pkg.core.has_base64_sig { ?string(pkg.core.base64_sig) } else { ?string(none) } results := core.check_signature_owned(path, embedded) or { return none } return pkg.owner.native_siglist(results) } pub fn (db &DB) check_pgp_signature() ?&SigList { db.native_database() or { return none } mut core := db.owner.native_handle() or { return none } results := core.check_signature_owned(db.core.path, none) or { return none } return db.owner.native_siglist(results) } pub fn (siglist &SigList) cleanup() ! { if siglist == unsafe { nil } { return Errno.wrong_args.ierror() } for index in 0 .. int(siglist.count) { result := unsafe { siglist.results[index] } if result.key.data != unsafe { nil } { engine.release_signature_key(result.key.data) } else { C.free(result.key.fingerprint) } } C.free(siglist.results) mut list := unsafe { siglist } list.count = 0 list.results = unsafe { nil } } pub fn decode_signature(base64_data string, data_len &uint) !&u8 { if data_len == unsafe { nil } { return Errno.wrong_args.ierror() } unsafe { *data_len = 0 } decoded := engine.decode_signature(base64_data) or { return error_with_code('failed to decode signature', int(Errno.sig_invalid)) } data := unsafe { &u8(C.malloc(if decoded.len > 0 { decoded.len } else { 1 })) } if data == unsafe { nil } { return Errno.memory.ierror() } if decoded.len > 0 { unsafe { C.memcpy(data, decoded.data, uint(decoded.len)) } } unsafe { *data_len = uint(decoded.len) } return data } pub fn (handle &Handle) extract_keyid(identifier string, sig &u8, len uint) !&List { mut core := handle.native_handle_preserving_error()! if int(len) < 0 || (sig == unsafe { nil } && len != 0) { return Errno.wrong_args.ierror() } if len == 0 { return unsafe { &List(nil) } } ids := core.extract_signature_key_ids(identifier, unsafe { sig.vbytes(int(len)) })! return native_owned_strings(ids) } pub enum Depmod { any = 1 equal at_least at_most greater_than less_than } pub enum FileConflictType { target = 1 filesystem } pub struct Depend { pub mut: name &u8 = unsafe { nil } version &u8 = unsafe { nil } desc &u8 = unsafe { nil } name_hash u64 mod Depmod } pub fn (dep Depend) get_name() string { return cstring_to_vstring_or_none(dep.name) } pub fn (dep Depend) get_version() string { return cstring_to_vstring_or_none(dep.version) } pub fn (dep Depend) get_desc() string { return cstring_to_vstring_or_none(dep.desc) } pub struct DepMissing { pub mut: target &u8 = unsafe { nil } depend &Depend = unsafe { nil } causingpkg &u8 = unsafe { nil } } pub struct Conflict { pub mut: package1 &Package = unsafe { nil } package2 &Package = unsafe { nil } reason &Depend = unsafe { nil } } pub struct FileConflict { pub mut: target &u8 = unsafe { nil } type_ FileConflictType file &u8 = unsafe { nil } ctarget &u8 = unsafe { nil } } pub fn (handle &Handle) checkdeps(pkglist &List, remove &List, upgrade &List, reversedeps int) &List { mut core := handle.native_handle() or { return unsafe { nil } } assumed := core.assumed() or { return unsafe { nil } } flags := core.transaction_flags() or { core.validate_handle() or { return unsafe { nil } } engine.transaction_flag_0 } 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 }) return native_missing_list(missing) } pub fn (pkgs &List) find_satisfier(depstring string) ?&Package { dependency := engine.dep_from_string(depstring) for node := unsafe { pkgs }; node != unsafe { nil }; node = node.next { pkg := node.get_pkg() or { continue } if pkg.satisfies_constraint(dependency) { return pkg } } return none } pub fn (handle &Handle) find_dbs_satisfier(dbs &List, depstring string) ?&Package { mut core := handle.native_handle() or { return none } found := core.find_database_satisfier(dbs.native_databases(), depstring) or { return none } return handle.native_package(found) } pub fn (handle &Handle) checkconflicts(pkglist &List) &List { handle.native_handle() or { return unsafe { nil } } return handle.native_conflicts(engine.find_conflicts(pkglist.native_packages())) } // libc allocation preserves the public dependency payload layout and ownership. pub fn (dep &Depend) compute_string() string { return engine_dependency(dep).compute_string() } pub fn dep_from_string(depstring string) &Depend { parsed := engine.dep_from_string(depstring) mut dep := unsafe { &Depend(C.calloc(1, sizeof(Depend))) } if dep == unsafe { nil } { return dep } dep.name = unsafe { C.strdup(parsed.name().str) } if parsed.mod != .any { dep.version = unsafe { C.strdup(parsed.version().str) } } if parsed.has_desc() { dep.desc = unsafe { C.strdup(parsed.desc().str) } } if dep.name == unsafe { nil } || (parsed.mod != .any && dep.version == unsafe { nil }) || (parsed.has_desc() && dep.desc == unsafe { nil }) { dep.free() return unsafe { nil } } dep.mod = unsafe { Depmod(parsed.mod) } for c in parsed.name() { dep.name_hash = u64(i64(i8(c))) + (dep.name_hash << 6) + (dep.name_hash << 16) - dep.name_hash } return dep } pub fn (dep &Depend) free() { if dep == unsafe { nil } { return } C.free(dep.name) C.free(dep.version) C.free(dep.desc) C.free(dep) } pub fn (conflict &FileConflict) free() { if conflict == unsafe { nil } { return } C.free(conflict.ctarget) C.free(conflict.file) C.free(conflict.target) C.free(conflict) } pub fn (miss &DepMissing) free() { if miss == unsafe { nil } { return } miss.depend.free() C.free(miss.target) C.free(miss.causingpkg) C.free(miss) } pub fn (conflict &Conflict) free() { if conflict == unsafe { nil } { return } conflict.reason.free() C.free(conflict) } pub enum EventType { checkdeps_start = 1 checkdeps_done fileconflicts_start fileconflicts_done resolvedeps_start resolvedeps_done interconflicts_start interconflicts_done transaction_start transaction_done package_operation_start package_operation_done integrity_start integrity_done load_start load_done scriptlet_info db_retrieve_start db_retrieve_done db_retrieve_failed pkg_retrieve_start pkg_retrieve_done pkg_retrieve_failed diskspace_start diskspace_done optdep_removal database_missing keyring_start keyring_done key_download_start key_download_done pacnew_created pacsave_created hook_start hook_done hook_run_start hook_run_done } pub struct EventAny { type_ EventType } pub enum PackageOperation { install = 1 upgrade reinstall downgrade remove } pub struct EventPackageOperation { pub mut: type_ EventType operation PackageOperation oldpkg &Package = unsafe { nil } newpkg &Package = unsafe { nil } } pub struct EventOptDepRemoval { pub mut: type_ EventType pkg &Package = unsafe { nil } optdep &Depend = unsafe { nil } } pub struct EventScriptletInfo { pub mut: type_ EventType line &u8 = unsafe { nil } } pub struct EventDatabaseMissing { pub mut: type_ EventType dbname &u8 = unsafe { nil } } pub struct EventPkgDownload { pub mut: type_ EventType file &u8 = unsafe { nil } } pub struct EventPacnewCreated { pub mut: type_ EventType from_noupgrade i32 oldpkg &Package = unsafe { nil } newpkg &Package = unsafe { nil } file &u8 = unsafe { nil } } pub struct EventPacsaveCreated { pub mut: type_ EventType oldpkg &Package = unsafe { nil } file &u8 = unsafe { nil } } pub enum HookWhen { pre_transaction = 1 post_transaction } pub struct EventHook { pub mut: type_ EventType when HookWhen } pub struct EventHookRun { pub mut: type_ EventType name &u8 = unsafe { nil } desc &u8 = unsafe { nil } position uint total uint } pub struct EventPkgRetrieve { pub mut: type_ EventType num uint total_size i64 } union Event { pub mut: type_ EventType any EventAny package_operation EventPackageOperation optdep_removal EventOptDepRemoval scriptlet_info EventScriptletInfo database_missing EventDatabaseMissing pkgdownload EventPkgDownload pacnew_created EventPacnewCreated pacsave_created EventPacsaveCreated hook EventHook hook_run EventHookRun pkg_retrieve EventPkgRetrieve } type CbEvent = fn (voidptr, &Event) pub enum QuestionType { install_ignorepkg = 1 << 0 replace_pkg = 1 << 1 conflict_pkg = 1 << 2 corrupted_pkg = 1 << 3 remove_pkgs = 1 << 4 select_provider = 1 << 5 import_key = 1 << 6 } pub struct QuestionAny { pub mut: type_ QuestionType answer i32 } pub struct QuestionInstallIgnorePkg { pub mut: type_ QuestionType install i32 pkg &Package = unsafe { nil } } pub struct QuestionReplace { pub mut: type_ QuestionType replace i32 oldpkg &Package = unsafe { nil } newpkg &Package = unsafe { nil } newdb &DB = unsafe { nil } } pub struct QuestionConflict { pub mut: type_ QuestionType remove_ i32 conflict &Conflict = unsafe { nil } } pub struct QuestionCorrupted { pub mut: type_ QuestionType remove_ i32 filepath &u8 = unsafe { nil } reason Errno } pub struct QuestionRemovePkgs { pub mut: type_ QuestionType skip i32 packages &List = unsafe { nil } } pub struct QuestionSelectProvider { pub mut: type_ QuestionType use_index i32 providers &List = unsafe { nil } depend &Depend = unsafe { nil } } pub struct QuestionImportKey { pub mut: type_ QuestionType import_ i32 uid &u8 = unsafe { nil } fingerprint &u8 = unsafe { nil } } union Question { pub mut: type_ QuestionType any QuestionAny install_ignorepkg QuestionInstallIgnorePkg replace QuestionReplace conflict QuestionConflict corrupted QuestionCorrupted remove_pkgs QuestionRemovePkgs select_provider QuestionSelectProvider import_key QuestionImportKey } type CbQuestion = fn (voidptr, &Question) pub enum Progress { add_start upgrade_start downgrade_start reinstall_start remove_start conflicts_start diskspace_start integrity_start load_start keyring_start } type CbProgress = fn (voidptr, Progress, &u8, i32, uint, uint) pub enum DownloadEventType { init progress retry completed } pub struct DownloadEventInit { pub mut: optional i32 } pub struct DownloadEventProgress { pub mut: downloaded i64 total i64 } pub struct DownloadEventRetry { pub mut: resume i32 } pub struct DownloadEventCompleted { pub mut: total i64 result i32 } type CbDownload = fn (voidptr, &u8, DownloadEventType, voidptr) type CbFetch = fn (voidptr, &u8, &u8, i32) i32 pub fn (handle &Handle) get_localdb() !&DB { mut core := handle.native_handle()! return handle.native_database(core.local_database()!) } pub fn (handle &Handle) get_syncdbs() !&List { mut core := handle.native_handle()! return handle.native_database_list(core.sync_databases()!) } pub fn (handle &Handle) register_syncdb(treename string, level int) !&DB { mut core := handle.native_handle()! return handle.native_database(core.register_syncdb(treename, u32(level))!) } pub fn (handle &Handle) unregister_all_syncdbs() ! { mut core := handle.native_handle()! core.unregister_syncdbs()! } pub fn (db &DB) unregister() ! { mut core := db.native_database()! core.unregister()! } pub fn (db &DB) get_handle() &Handle { if db == unsafe { nil } { return unsafe { nil } } return db.owner } pub fn (db &DB) get_name() string { core := db.native_database_preserving_error() or { return '' } return core.name } pub fn (db &DB) get_siglevel() SigLevel { mut core := db.native_database_preserving_error() or { return SigLevel.zero() } return unsafe { SigLevel(core.effective_siglevel() or { return SigLevel.zero() }) } } pub fn (db &DB) get_valid() !bool { mut core := db.native_database()! return core.get_valid()! } pub fn (db &DB) get_servers() &List { mut core := db.native_database_preserving_error() or { return unsafe { nil } } values := core.get_servers(false) or { return unsafe { nil } } list := db.owner.native_strings('db:${u64(voidptr(db))}:servers', values) return list } pub fn (db &DB) set_servers(servers &List) ! { mut core := db.native_database_preserving_error()! core.set_servers(servers.native_strings(), false)! } pub fn (db &DB) add_server(url string) ! { mut core := db.native_database()! core.add_server(url, false)! } pub fn (db &DB) remove_server(url string) ! { mut core := db.native_database()! core.remove_server(url, false)! } pub fn (db &DB) get_cache_servers() ?&List { mut core := db.native_database_preserving_error() or { return none } values := core.get_servers(true) or { return none } list := db.owner.native_strings('db:${u64(voidptr(db))}:cache_servers', values) return if list == unsafe { nil } { none } else { list } } pub fn (db &DB) set_cache_servers(servers &List) ! { mut core := db.native_database_preserving_error()! core.set_servers(servers.native_strings(), true)! } pub fn (db &DB) add_cache_server(url string) ! { mut core := db.native_database()! core.add_server(url, true)! } pub fn (db &DB) remove_cache_server(url string) ! { mut core := db.native_database()! core.remove_server(url, true)! } // db_update updates the sync dbs. it answers whether anything was downloaded: // libalpm returns 0 after a download, 1 when the dbs are all current (only // possible without force, an "up to date" answer) and -1 on error. the up to // date case is a success, not an error pub fn (handle &Handle) db_update(dbs &List, force bool) !bool { mut core := handle.native_handle()! return core.update_databases(dbs.native_databases(), force)! } pub fn (db &DB) get_pkg(name string) ?&Package { mut core := db.native_database() or { return none } found := core.get_pkg(name) or { return none } return db.owner.native_package(found) } pub fn (db &DB) get_pkgcache() ?&List { mut core := db.native_database() or { return none } revision := core.package_cache_revision() or { return none } if revision != db.package_revision { values := core.package_cache() or { return none } mut wrapper := unsafe { db } wrapper.package_list = db.owner.native_cached_packages('cache:${u64(voidptr(db))}', values) wrapper.package_revision = revision } return if db.package_list == unsafe { nil } { none } else { db.package_list } } pub fn (db &DB) get_group(name string) ?&Group { mut core := db.native_database() or { return none } group := core.get_group(name) or { return none } return db.owner.native_group(group) } pub fn (db &DB) get_groupcache() ?&List { mut core := db.native_database() or { return none } groups := core.group_cache() or { return none } if groups.len == 0 { return none } key := native_pointer_list_key('groups:${u64(voidptr(db))}', groups) if existing := db.owner.lists[key] { return existing } mut list := &List(unsafe { nil }) for group in groups { list = list.add(db.owner.native_group(group)) } mut owner := unsafe { db.owner } owner.lists[key] = list return db.owner.native_borrow_list(list) } pub fn (db &DB) search(needles &List) !&List { mut core := db.native_database()! return db.owner.native_package_list(core.search(needles.native_strings())!) } // bit positions, as for SigLevel @[flag] pub enum DBUsage { sync = 0 search = 1 install = 2 upgrade = 3 } // db_usage_all is ALPM_DB_USAGE_ALL. a flag enum member is one bit, so the // four together are a constant pub const db_usage_all = DBUsage.sync | .search | .install | .upgrade pub fn (db &DB) set_usage(usage DBUsage) ! { mut core := db.native_database_preserving_error()! core.set_usage(u32(usage))! } pub fn (db &DB) get_usage() ?DBUsage { mut core := db.native_database_preserving_error() or { return none } return unsafe { DBUsage(core.get_usage() or { return none }) } } // bit positions, as for SigLevel @[flag] pub enum LogLevel { error = 0 warning = 1 debug = 2 function = 3 } type CbLog = fn (voidptr, LogLevel, &u8, voidptr) pub fn (handle &Handle) log_action(prefix string, fmt string) ! { if handle == unsafe { nil } || handle.core == unsafe { nil } { return Errno.handle_null.ierror() } mut core := unsafe { handle.core } core.log_action(prefix, fmt)! } pub fn (handle &Handle) option_get_logcb() CbLog { handle.native_handle() or { return unsafe { nil } } return handle.logcb } pub fn (handle &Handle) option_get_logcb_ctx() voidptr { handle.native_handle() or { return unsafe { nil } } return handle.logctx } pub fn (handle &Handle) option_set_logcb(cb CbLog, ctx voidptr) ! { mut core := handle.native_handle()! core.set_log_callback(if voidptr(cb) == unsafe { nil } { unsafe { nil } } else { native_log }, handle)! line_context := handle.owned_log_line_context() mut h := unsafe { handle } h.logcb = cb h.logctx = ctx h.log_line_context = if voidptr(cb) == voidptr(C.v_alpm_log_line_function()) { C.v_alpm_log_line_context(ctx) } else { unsafe { nil } } if line_context != ctx || voidptr(cb) != voidptr(C.v_alpm_log_line_function()) { C.free(line_context) } } pub fn (handle &Handle) option_get_dlcb() CbDownload { handle.native_handle() or { return unsafe { nil } } return handle.dlcb } pub fn (handle &Handle) option_get_dlcb_ctx() voidptr { handle.native_handle() or { return unsafe { nil } } return handle.dlctx } pub fn (handle &Handle) option_set_dlcb(cb CbDownload, ctx voidptr) ! { mut core := handle.native_handle()! core.set_download_callback(if voidptr(cb) == unsafe { nil } { unsafe { nil } } else { native_download }, handle)! mut h := unsafe { handle } h.dlcb = cb h.dlctx = ctx } pub fn (handle &Handle) option_get_fetchcb() CbFetch { handle.native_handle() or { return unsafe { nil } } return handle.fetchcb } pub fn (handle &Handle) get_fetchcb_ctx() voidptr { handle.native_handle() or { return unsafe { nil } } return handle.fetchctx } pub fn (handle &Handle) option_set_fetchcb(cb CbFetch, ctx voidptr) ! { mut core := handle.native_handle()! core.set_fetch_callback(if voidptr(cb) == unsafe { nil } { unsafe { nil } } else { native_fetch }, handle)! mut h := unsafe { handle } h.fetchcb = cb h.fetchctx = ctx } pub fn (handle &Handle) option_get_eventcb() CbEvent { handle.native_handle() or { return unsafe { nil } } return handle.eventcb } pub fn (handle &Handle) option_get_eventcb_ctx() voidptr { handle.native_handle() or { return unsafe { nil } } return handle.eventctx } pub fn (handle &Handle) option_set_eventcb(cb CbEvent, ctx voidptr) ! { mut core := handle.native_handle()! if voidptr(cb) == unsafe { nil } { core.set_preparation_callback(unsafe { nil }, handle)! core.set_commit_callback(unsafe { nil }, handle)! core.set_retrieval_callback(unsafe { nil }, handle)! } else { core.set_preparation_callback(native_preparation, handle)! core.set_commit_callback(native_commit, handle)! core.set_retrieval_callback(native_retrieval, handle)! } mut h := unsafe { handle } h.eventcb = cb h.eventctx = ctx } pub fn (handle &Handle) option_get_questioncb() CbQuestion { handle.native_handle() or { return unsafe { nil } } return handle.questioncb } pub fn (handle &Handle) option_get_questioncb_ctx() voidptr { handle.native_handle() or { return unsafe { nil } } return handle.questionctx } pub fn (handle &Handle) option_set_questioncb(cb CbQuestion, ctx voidptr) ! { mut core := handle.native_handle()! core.set_dependency_questions(if voidptr(cb) == unsafe { nil } { engine.DependencyQuestions{} } else { handle.native_dependency_questions() })! mut h := unsafe { handle } h.questioncb = cb h.questionctx = ctx } pub fn (handle &Handle) option_get_progresscb() CbProgress { handle.native_handle() or { return unsafe { nil } } return handle.progresscb } pub fn (handle &Handle) option_get_progresscb_ctx() voidptr { handle.native_handle() or { return unsafe { nil } } return handle.progressctx } pub fn (handle &Handle) option_set_progresscb(cb CbProgress, ctx voidptr) ! { mut core := handle.native_handle()! core.set_commit_progress_callback(if voidptr(cb) == unsafe { nil } { unsafe { nil } } else { native_progress }, handle)! mut h := unsafe { handle } h.progresscb = cb h.progressctx = ctx } pub fn (handle &Handle) option_get_root() string { core := handle.native_handle() or { return '' } return core.root } pub fn (handle &Handle) option_get_dbpath() string { core := handle.native_handle() or { return '' } return core.dbpath } pub fn (handle &Handle) option_get_lockfile() string { core := handle.native_handle() or { return '' } return core.lockfile } pub fn (handle &Handle) option_get_cachedirs() &List { mut core := handle.native_handle() or { return unsafe { nil } } list := handle.native_strings('option:cache_dirs', core.string_list(.cache_dirs) or { return unsafe { nil } }) return list } pub fn (handle &Handle) option_set_cachedirs(cachedirs &List) ! { mut core := handle.native_handle()! core.set_string_list(.cache_dirs, cachedirs.native_strings())! } pub fn (handle &Handle) option_add_cachedir(cachedir string) ! { mut core := handle.native_handle()! core.add_string_list(.cache_dirs, cachedir)! } pub fn (handle &Handle) option_remove_cachedir(cachedir string) ! { mut core := handle.native_handle()! core.remove_string_list(.cache_dirs, cachedir)! } pub fn (handle &Handle) option_get_hookdirs() ?&List { mut core := handle.native_handle() or { return none } list := handle.native_strings('option:hook_dirs', core.string_list(.hook_dirs) or { return none }) return if list == unsafe { nil } { none } else { list } } pub fn (handle &Handle) option_set_hookdirs(hookdirs &List) ! { mut core := handle.native_handle()! core.set_string_list(.hook_dirs, hookdirs.native_strings())! } pub fn (handle &Handle) option_add_hookdir(hookdir string) ! { mut core := handle.native_handle()! core.add_string_list(.hook_dirs, hookdir)! } pub fn (handle &Handle) option_remove_hookdir(hookdir string) ! { mut core := handle.native_handle()! core.remove_string_list(.hook_dirs, hookdir)! } pub fn (handle &Handle) option_get_overwrite_files() &List { mut core := handle.native_handle() or { return unsafe { nil } } list := handle.native_strings('option:overwrite_files', core.string_list(.overwrite_files) or { return unsafe { nil } }) return list } pub fn (handle &Handle) option_set_overwrite_files(globs &List) ! { mut core := handle.native_handle()! core.set_string_list(.overwrite_files, globs.native_strings())! } pub fn (handle &Handle) option_add_overwrite_file(glob string) ! { mut core := handle.native_handle()! core.add_string_list(.overwrite_files, glob)! } pub fn (handle &Handle) option_remove_overwrite_file(glob string) ! { mut core := handle.native_handle()! core.remove_string_list(.overwrite_files, glob)! } pub fn (handle &Handle) option_get_logfile() string { mut core := handle.native_handle() or { return '' } return core.string_option(.logfile) or { '' } } pub fn (handle &Handle) option_set_logfile(logfile string) ! { mut core := handle.native_handle()! core.set_string_option(.logfile, logfile)! } pub fn (handle &Handle) option_get_gpgdir() string { mut core := handle.native_handle() or { return '' } return core.string_option(.gpgdir) or { '' } } pub fn (handle &Handle) option_set_gpgdir(gpgdir string) ! { mut core := handle.native_handle()! core.set_string_option(.gpgdir, gpgdir)! } pub fn (handle &Handle) option_get_sandboxuser() string { mut core := handle.native_handle() or { return '' } return core.string_option(.sandbox_user) or { '' } } pub fn (handle &Handle) option_set_sandboxuser(sandboxuser string) ! { mut core := handle.native_handle()! core.set_string_option(.sandbox_user, sandboxuser)! } pub fn (handle &Handle) option_get_usesyslog() bool { mut core := handle.native_handle() or { return false } value := core.integer_option(.use_syslog) or { return false } return value != 0 } pub fn (handle &Handle) option_set_usesyslog(usesyslog int) ! { mut core := handle.native_handle()! core.set_integer_option(.use_syslog, i64(usesyslog))! } pub fn (handle &Handle) option_get_noupgrades() &List { mut core := handle.native_handle() or { return unsafe { nil } } list := handle.native_strings('option:no_upgrade', core.string_list(.no_upgrade) or { return unsafe { nil } }) return list } pub fn (handle &Handle) option_add_noupgrade(path string) ! { mut core := handle.native_handle()! core.add_string_list(.no_upgrade, path)! } pub fn (handle &Handle) option_set_noupgrades(noupgrade &List) ! { mut core := handle.native_handle()! core.set_string_list(.no_upgrade, noupgrade.native_strings())! } pub fn (handle &Handle) option_remove_noupgrade(path string) ! { mut core := handle.native_handle()! core.remove_string_list(.no_upgrade, path)! } pub fn (handle &Handle) option_match_no_upgrade(path string) bool { mut core := handle.native_handle_preserving_error() or { return false } return (core.match_option(.no_upgrade, path) or { return false }) == 0 } pub fn (handle &Handle) option_get_no_extracts() &List { mut core := handle.native_handle() or { return unsafe { nil } } list := handle.native_strings('option:no_extract', core.string_list(.no_extract) or { return unsafe { nil } }) return list } pub fn (handle &Handle) option_add_no_extract(path string) ! { mut core := handle.native_handle()! core.add_string_list(.no_extract, path)! } pub fn (handle &Handle) option_set_no_extracts(noextract &List) ! { mut core := handle.native_handle()! core.set_string_list(.no_extract, noextract.native_strings())! } pub fn (handle &Handle) option_remove_no_extract(path string) ! { mut core := handle.native_handle()! core.remove_string_list(.no_extract, path)! } pub fn (handle &Handle) option_match_no_extract(path string) bool { mut core := handle.native_handle_preserving_error() or { return false } return (core.match_option(.no_extract, path) or { return false }) == 0 } pub fn (handle &Handle) option_get_ignore_pkgs() &List { mut core := handle.native_handle() or { return unsafe { nil } } list := handle.native_strings('option:ignore_packages', core.string_list(.ignore_packages) or { return unsafe { nil } }) return list } pub fn (handle &Handle) option_add_ignore_pkg(pkg string) ! { mut core := handle.native_handle()! core.add_string_list(.ignore_packages, pkg)! } pub fn (handle &Handle) option_set_ignore_pkgs(ignorepkgs &List) ! { mut core := handle.native_handle()! core.set_string_list(.ignore_packages, ignorepkgs.native_strings())! } pub fn (handle &Handle) option_remove_ignore_pkg(pkg string) ! { mut core := handle.native_handle()! core.remove_string_list(.ignore_packages, pkg)! } pub fn (handle &Handle) option_get_ignore_groups() &List { mut core := handle.native_handle() or { return unsafe { nil } } list := handle.native_strings('option:ignore_groups', core.string_list(.ignore_groups) or { return unsafe { nil } }) return list } pub fn (handle &Handle) option_add_ignore_group(grp string) ! { mut core := handle.native_handle()! core.add_string_list(.ignore_groups, grp)! } pub fn (handle &Handle) option_set_ignore_groups(ignoregrps &List) ! { mut core := handle.native_handle()! core.set_string_list(.ignore_groups, ignoregrps.native_strings())! } pub fn (handle &Handle) option_remove_ignore_group(grp string) ! { mut core := handle.native_handle()! core.remove_string_list(.ignore_groups, grp)! } pub fn option_get_assume_installed(handle &Handle) ?&List { mut core := handle.native_handle() or { return none } values := core.assumed() or { return none } if values.len == 0 { return none } return handle.native_dependencies('assumed', values) } pub fn (handle &Handle) option_add_assumeinstalled(dep &Depend) ! { mut core := handle.native_handle()! core.add_assumed(engine_dependency(dep))! } pub fn (handle &Handle) option_set_assumeinstalled(deps &List) ! { mut core := handle.native_handle()! core.set_assumed(deps.native_dependencies())! } pub fn (handle &Handle) option_remove_assumeinstalled(dep &Depend) ! { mut core := handle.native_handle()! core.remove_assumed(engine_dependency(dep))! } pub fn (handle &Handle) option_get_architectures() ?&List { mut core := handle.native_handle() or { return none } list := handle.native_strings('option:architectures', core.string_list(.architectures) or { return none }) return if list == unsafe { nil } { none } else { list } } pub fn (handle &Handle) option_add_architecture(architecture string) ! { mut core := handle.native_handle_preserving_error()! core.add_string_list(.architectures, architecture)! } pub fn (handle &Handle) option_set_architectures(arches &List) ! { mut core := handle.native_handle()! core.set_string_list(.architectures, arches.native_strings())! } pub fn (handle &Handle) option_remove_architecture(architecture string) ! { mut core := handle.native_handle()! core.remove_string_list(.architectures, architecture)! } pub fn (handle &Handle) option_get_checkspace() bool { mut core := handle.native_handle() or { return false } value := core.integer_option(.check_space) or { return false } return value != 0 } pub fn (handle &Handle) option_set_checkspace(checkspace int) ! { mut core := handle.native_handle()! core.set_integer_option(.check_space, i64(checkspace))! } pub fn (handle &Handle) option_get_dbext() string { mut core := handle.native_handle() or { return '' } return core.string_option(.db_extension) or { '' } } pub fn (handle &Handle) option_set_dbext(dbext string) ! { mut core := handle.native_handle()! core.set_string_option(.db_extension, dbext)! } pub fn (handle &Handle) option_get_default_siglevel() ?SigLevel { mut core := handle.native_handle() or { return none } value := core.integer_option(.default_siglevel) or { return none } return unsafe { SigLevel(value) } } pub fn (handle &Handle) option_set_default_siglevel(level SigLevel) ! { mut core := handle.native_handle()! core.set_integer_option(.default_siglevel, i64(level))! } pub fn (handle &Handle) option_get_local_file_siglevel() ?SigLevel { mut core := handle.native_handle() or { return none } value := core.integer_option(.local_file_siglevel) or { return none } return unsafe { SigLevel(value) } } pub fn (handle &Handle) option_set_local_file_siglevel(level SigLevel) ! { mut core := handle.native_handle()! core.set_integer_option(.local_file_siglevel, i64(level))! } pub fn (handle &Handle) option_get_remote_file_siglevel() ?SigLevel { mut core := handle.native_handle() or { return none } value := core.integer_option(.remote_file_siglevel) or { return none } return unsafe { SigLevel(value) } } pub fn (handle &Handle) option_set_remote_file_siglevel(level SigLevel) ! { mut core := handle.native_handle()! core.set_integer_option(.remote_file_siglevel, i64(level))! } pub fn (handle &Handle) option_get_disable_dl_timeout() bool { mut core := handle.native_handle() or { return false } value := core.integer_option(.disable_download_timeout) or { return false } return value != 0 } pub fn (handle &Handle) option_set_disable_dl_timeout(disable_dl_timeout u16) ! { mut core := handle.native_handle()! core.set_integer_option(.disable_download_timeout, i64(disable_dl_timeout))! } pub fn (handle &Handle) option_get_parallel_downloads() int { mut core := handle.native_handle() or { return -1 } value := core.integer_option(.parallel_downloads) or { return -1 } return int(value) } pub fn (handle &Handle) option_set_parallel_downloads(num_streams u32) ! { mut core := handle.native_handle()! core.set_integer_option(.parallel_downloads, i64(num_streams))! } // the combined C symbols are not exported by the referenced libalpm build. pub fn (handle &Handle) option_get_disable_sandbox() bool { return handle.option_get_disable_sandbox_filesystem() || handle.option_get_disable_sandbox_syscalls() } pub fn (handle &Handle) option_set_disable_sandbox(disable_sandbox u16) ! { handle.option_set_disable_sandbox_filesystem(disable_sandbox)! handle.option_set_disable_sandbox_syscalls(disable_sandbox)! } pub fn (handle &Handle) option_get_disable_sandbox_filesystem() bool { mut core := handle.native_handle() or { return false } value := core.integer_option(.disable_sandbox_filesystem) or { return false } return value != 0 } pub fn (handle &Handle) option_set_disable_sandbox_filesystem(disable_sandbox_filesystem u16) ! { mut core := handle.native_handle()! core.set_integer_option(.disable_sandbox_filesystem, i64(disable_sandbox_filesystem))! } pub fn (handle &Handle) option_get_disable_sandbox_syscalls() bool { mut core := handle.native_handle() or { return false } value := core.integer_option(.disable_sandbox_syscalls) or { return false } return value != 0 } pub fn (handle &Handle) option_set_disable_sandbox_syscalls(disable_sandbox_syscalls u16) ! { mut core := handle.native_handle()! core.set_integer_option(.disable_sandbox_syscalls, i64(disable_sandbox_syscalls))! } pub enum PackageReason { explicit = 0 depend = 1 unknown = 2 } pub enum PackageFrom { file = 1 localdb syncdb } pub enum PkgValidation { unknown = 0 none = 1 << 0 md5sum = 1 << 1 sha256sum = 1 << 2 signature = 1 << 3 } pub fn (handle &Handle) pkg_load(filename string, full int, level int) !&Package { mut core := handle.native_handle()! return handle.native_package(core.load_package(filename, full != 0, u32(level))!) } pub fn (handle &Handle) fetch_pkgurl(urls &List) !&List { mut core := handle.native_handle()! return native_owned_strings(core.fetch_package_urls(urls.native_strings())!) } pub fn (haystack &List) pkg_find(needle string) ?&Package { name := needle.all_before('\x00') for node := unsafe { haystack }; node != unsafe { nil }; node = node.next { pkg := node.get_pkg() or { continue } // package.c searches stored names without clearing either owner's error. if pkg.core != unsafe { nil } && pkg.core.name == name { return pkg } } return none } pub fn (pkg &Package) free_pkg() ! { pkg.native_package()! if pkg == unsafe { nil } { return } if pkg.core.origin != .file { return } mut owner := unsafe { pkg.owner } owner.packages.delete(u64(voidptr(pkg.core))) } pub fn (pkg &Package) checkmd5sum() int { value := pkg.native_package() or { return -1 } mut core := pkg.owner.native_handle() or { return -1 } return core.check_package_md5sum(value) } pub fn pkg_vercmp(a string, b string) int { return engine.pkg_vercmp(a, b) } // installed packages use their local db; sync packages use every sync db, // as pacman's -Sii does. the returned names are V-owned copies. pub fn (pkg &Package) compute_required_by() []string { pkg.native_package() or { return []string{} } mut core := pkg.owner.native_handle() or { return []string{} } return core.reverse_dependencies(pkg.core, false) or { []string{} } } pub fn (pkg &Package) compute_optional_for() []string { pkg.native_package() or { return []string{} } mut core := pkg.owner.native_handle() or { return []string{} } return core.reverse_dependencies(pkg.core, true) or { []string{} } } pub fn (handle &Handle) pkg_should_ignore(pkg &Package) int { mut core := handle.native_handle_preserving_error() or { return -1 } if pkg == unsafe { nil } || pkg.core == unsafe { nil } { return -1 } pkg.owner.native_handle_preserving_error() or { return -1 } mut value := unsafe { pkg.core } return int(core.should_ignore(mut value) or { return -1 }) } pub fn (pkg &Package) get_handle() &Handle { if pkg == unsafe { nil } { return unsafe { nil } } return pkg.owner } @[inline] fn cstring_to_vstring_or_none(s &u8) string { return if s == unsafe { nil } { '' } else { unsafe { cstring_to_vstring(s) } } } pub fn (pkg &Package) get_filename() string { pkg.native_metadata(false, false) or { return '' } return pkg.core.filename } pub fn (pkg &Package) get_base() string { pkg.native_metadata(true, false) or { return '' } return pkg.core.base } pub fn (pkg &Package) get_name() string { pkg.native_metadata(false, false) or { return '' } return pkg.core.name } pub fn (pkg &Package) get_version() string { pkg.native_metadata(false, false) or { return '' } return pkg.core.version } pub fn (pkg &Package) get_origin() PackageFrom { pkg.native_metadata(false, false) or { return PackageFrom.file } return unsafe { PackageFrom(pkg.core.origin) } } pub fn (pkg &Package) get_desc() string { pkg.native_metadata(true, false) or { return '' } return pkg.core.desc } pub fn (pkg &Package) get_url() string { pkg.native_metadata(true, false) or { return '' } return pkg.core.url } pub fn (pkg &Package) get_build_date() Time { pkg.native_metadata(true, false) or { return Time(0) } return pkg.core.build_date } pub fn (pkg &Package) get_install_date() Time { pkg.native_metadata(true, false) or { return Time(0) } return pkg.core.install_date } pub fn (pkg &Package) get_packager() string { pkg.native_metadata(true, false) or { return '' } return pkg.core.packager } pub fn (pkg &Package) get_md5_sum() string { pkg.native_metadata(false, false) or { return '' } return pkg.core.md5sum } pub fn (pkg &Package) get_sha256_sum() string { pkg.native_metadata(false, false) or { return '' } return pkg.core.sha256sum } pub fn (pkg &Package) get_arch() string { pkg.native_metadata(true, false) or { return '' } return pkg.core.architecture } pub fn (pkg &Package) get_size() i64 { pkg.native_metadata(false, false) or { return i64(0) } return pkg.core.size } pub fn (pkg &Package) get_isize() i64 { pkg.native_metadata(true, false) or { return i64(0) } return pkg.core.isize } pub fn (pkg &Package) get_reason() PackageReason { pkg.native_metadata(true, false) or { return unsafe { PackageReason(0) } } return unsafe { PackageReason(pkg.core.reason) } } pub fn (pkg &Package) get_licenses() &List { pkg.native_metadata(true, false) or { return unsafe { nil } } return pkg.owner.native_strings('pkg:${u64(voidptr(pkg.core))}:licenses', pkg.core.licenses) } pub fn (pkg &Package) get_groups() &List { pkg.native_metadata(true, false) or { return unsafe { nil } } return pkg.owner.native_strings('pkg:${u64(voidptr(pkg.core))}:groups', pkg.core.groups) } pub fn (pkg &Package) get_depends() &List { pkg.native_metadata(true, false) or { return unsafe { nil } } return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:depends', pkg.core.depends) } pub fn (pkg &Package) get_opt_depends() &List { pkg.native_metadata(true, false) or { return unsafe { nil } } return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:optdepends', pkg.core.optdepends) } pub fn (pkg &Package) get_check_depends() &List { pkg.native_metadata(true, false) or { return unsafe { nil } } return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:checkdepends', pkg.core.checkdepends) } pub fn (pkg &Package) get_make_depends() &List { pkg.native_metadata(true, false) or { return unsafe { nil } } return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:makedepends', pkg.core.makedepends) } pub fn (pkg &Package) get_conflicts() &List { pkg.native_metadata(true, false) or { return unsafe { nil } } return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:conflicts', pkg.core.conflicts) } pub fn (pkg &Package) get_provides() &List { pkg.native_metadata(true, false) or { return unsafe { nil } } list := pkg.native_provides() if list != unsafe { nil } { mut p := unsafe { pkg } p.provides_exposed = true } return list } fn (pkg &Package) native_provides() &List { return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:provides', pkg.core.provides) } pub fn (pkg &Package) get_replaces() &List { pkg.native_metadata(true, false) or { return unsafe { nil } } return pkg.owner.native_dependencies('pkg:${u64(voidptr(pkg.core))}:replaces', pkg.core.replaces) } pub fn (pkg &Package) get_files() &Filelist { if pkg == unsafe { nil } || pkg.core == unsafe { nil } { return unsafe { nil } } pkg.native_metadata(false, true) or { return unsafe { &pkg.filelist } } mut p := unsafe { pkg } if p.file_versions.len == 0 || !p.file_versions.last().matches(pkg.core.files) { p.file_versions << &NativeFiles{ values: pkg.core.files.map(File{ name: it.name.str, size: it.size, mode: it.mode }) } } storage := p.file_versions.last() p.filelist = Filelist{ count: uint(storage.values.len) files: if storage.values.len == 0 { unsafe { nil } } else { storage.values.data } } return &p.filelist } pub fn (pkg &Package) get_backup() &List { pkg.native_metadata(false, true) or { return unsafe { nil } } key := 'backups:${u64(voidptr(pkg.core))}:' + pkg.core.backup.map('${it.name.len}:${it.name}:${it.hash.len}:${it.hash}:${it.has_hash}').join(';') if existing := pkg.owner.lists[key] { return existing } mut list := &List(unsafe { nil }) for item in pkg.core.backup { record := &Backup{ name: pkg.owner.native_buffer(item.name) hash: if item.has_hash { pkg.owner.native_buffer(item.hash) } else { unsafe { nil } } } // unmanaged list nodes must not be the only roots of managed records. buffer := unsafe { C.malloc(sizeof(Backup)) } if buffer == unsafe { nil } { return unsafe { nil } } unsafe { vmemcpy(buffer, record, int(sizeof(Backup))) } pkg.owner.native_own_buffer(buffer) list = list.add(buffer) } mut owner := unsafe { pkg.owner } owner.lists[key] = list return pkg.owner.native_borrow_list(list) } pub fn (pkg &Package) get_db() !&DB { pkg.native_package()! db := pkg.core.owner_database()! return pkg.owner.native_database(db) } pub fn (pkg &Package) get_base64_sig() string { pkg.native_metadata(false, false) or { return '' } return pkg.core.base64_sig } pub fn (pkg &Package) get_sig(sig_len &uint) !&u8 { if pkg == unsafe { nil } || pkg.core == unsafe { nil } { return Errno.wrong_args.ierror() } if pkg.owner == unsafe { nil } || pkg.owner.core == unsafe { nil } { return Errno.handle_null.ierror() } mut core := unsafe { pkg.owner.core } data := core.package_signature_bytes(pkg.core, sig_len)! return native_bytes(data, sig_len) or { return pkg.owner.native_failure(.memory, err.msg()) } } pub fn (pkg &Package) get_validation() PkgValidation { pkg.native_metadata(true, false) or { return unsafe { PkgValidation(0) } } return unsafe { PkgValidation(pkg.core.validation) } } pub fn (pkg &Package) get_xdata() &List { pkg.native_metadata(true, false) or { return unsafe { nil } } key := 'xdata:${u64(voidptr(pkg.core))}:' + pkg.core.xdata.map('${it.name.len}:${it.name}:${it.value.len}:${it.value}').join(';') if existing := pkg.owner.lists[key] { return existing } mut list := &List(unsafe { nil }) for item in pkg.core.xdata { mut record := unsafe { &PkgXdata(C.calloc(1, sizeof(PkgXdata))) } if record == unsafe { nil } { return unsafe { nil } } unsafe { record.name = pkg.owner.native_buffer(item.name) record.value = pkg.owner.native_buffer(item.value) } pkg.owner.native_own_buffer(record) list = list.add(record) } mut owner := unsafe { pkg.owner } owner.lists[key] = list return pkg.owner.native_borrow_list(list) } pub fn (pkg &Package) has_scriptlet() bool { pkg.native_metadata(false, false) or { return false } mut core := unsafe { pkg.core } core.load_scriptlet() or { return false } return core.scriptlet } pub fn (newpkg &Package) download_size() i64 { if newpkg == unsafe { nil } || newpkg.core == unsafe { nil } { return -1 } if newpkg.owner == unsafe { nil } || newpkg.owner.core == unsafe { nil } { return -1 } mut core := unsafe { newpkg.owner.core } mut value := unsafe { newpkg.core } return core.package_download_size_value(mut value) or { -1 } } pub fn (pkg &Package) set_reason(reason PackageReason) ! { pkg.native_package()! mut core := unsafe { pkg.core } core.set_reason(unsafe { engine.InstallReason(reason) })! } pub fn (pkg &Package) changelog_open() voidptr { pkg.native_package() or { return unsafe { nil } } if pkg.core.origin == .file { mut core := unsafe { pkg.owner.core } return core.open_file_changelog(pkg.core.filename) } if pkg.core.origin != .local_db { return unsafe { nil } } path := os.join_path(pkg.owner.core.dbpath, 'local', pkg.core.name + '-' + pkg.core.version, 'changelog') return C.fopen(unsafe { &char(path.str) }, c'r') } pub fn (pkg &Package) changelog_read(ptr voidptr, size uint, fp voidptr) uint { pkg.native_package() or { return 0 } if fp == unsafe { nil } { return 0 } if pkg.core.origin == .file { mut core := unsafe { pkg.owner.core } return core.read_file_changelog(ptr, size, fp) } if pkg.core.origin != .local_db { return 0 } return C.fread(ptr, 1, size, fp) } pub fn (pkg &Package) changelog_close(fp voidptr) ! { pkg.native_package()! if fp == unsafe { nil } { return Errno.wrong_args.ierror() } if pkg.core.origin == .file { mut core := unsafe { pkg.owner.core } return core.close_file_changelog(fp) } if pkg.core.origin != .local_db || C.fclose(fp) != 0 { return pkg.owner.error() } } pub fn (pkg &Package) mtree_open() &C.Archive { pkg.native_package() or { return unsafe { nil } } if pkg.core.origin != .local_db { return unsafe { nil } } path := os.join_path(pkg.owner.core.dbpath, 'local', pkg.core.name + '-' + pkg.core.version, 'mtree') if C.access(unsafe { &char(path.str) }, C.F_OK) != 0 { return unsafe { nil } } archive := C.archive_read_new() if archive == unsafe { nil } { pkg.owner.native_failure(.libarchive, '') return archive } C.archive_read_support_filter_all(archive) C.archive_read_support_format_mtree(archive) if C.archive_read_open_filename(archive, path.str, uint(C.BUFSIZ)) != 0 { message := cstring_to_vstring_or_none(unsafe { &u8(C.archive_error_string(archive)) }) native_log(pkg.owner, .error, 'error while reading file ${path}: ${message}\n') C.archive_read_free(archive) pkg.owner.native_failure(.libarchive, message) return unsafe { nil } } return archive } // the legacy wrapper advances but cannot propagate its pointer output. // mtree_step returns true for an entry, false for EOF, and preserves read errors. pub fn (pkg &Package) mtree_step(archive &C.Archive, entry &&C.Archive_entry) !bool { pkg.native_package()! if archive == unsafe { nil } || entry == unsafe { nil } { return Errno.wrong_args.ierror() } if pkg.core.origin != .local_db { return error('mtree read failed with status -1') } status := C.archive_read_next_header(archive, entry) if status == C.ARCHIVE_OK { return true } if status == C.ARCHIVE_EOF { return false } message := cstring_to_vstring_or_none(unsafe { &u8(C.archive_error_string(archive)) }) return error(if message != '' { message } else { 'mtree read failed with status -1' }) } pub fn (pkg &Package) mtree_next(archive &C.Archive, entry &C.Archive_entry) ! { pkg.native_package()! if archive == unsafe { nil } { return Errno.wrong_args.ierror() } mut output := unsafe { entry } if pkg.core.origin != .local_db || C.archive_read_next_header(archive, &output) != C.ARCHIVE_OK { return pkg.owner.error() } } pub fn (pkg &Package) mtree_close(archive &C.Archive) ! { pkg.native_package()! if archive == unsafe { nil } { return Errno.wrong_args.ierror() } if pkg.core.origin != .local_db || C.archive_read_free(archive) != 0 { return pkg.owner.error() } } @[flag] pub enum TransFlag { no_deps = 0 no_save = 2 no_depversion = 3 cascade = 4 recurse = 5 db_only = 6 no_hooks = 7 all_deps = 8 download_only = 9 no_scriptlet = 10 no_conflicts = 11 needed = 13 all_explicit = 14 unneeded = 15 recurse_all = 16 nolock = 17 } pub const trans_flag_0 = TransFlag.zero() pub fn (handle &Handle) trans_get_flags() TransFlag { mut core := handle.native_handle() or { return trans_flag_0 } return unsafe { TransFlag(core.transaction_flags() or { return trans_flag_0 }) } } pub fn (handle &Handle) trans_get_add() &List { mut core := handle.native_handle() or { return unsafe { nil } } return handle.native_cached_packages('trans:add', core.transaction_add() or { return unsafe { nil } }) } pub fn (handle &Handle) trans_get_remove() &List { mut core := handle.native_handle() or { return unsafe { nil } } return handle.native_cached_packages('trans:remove', core.transaction_remove() or { return unsafe { nil } }) } pub fn (handle &Handle) trans_init(flags TransFlag) ! { mut core := handle.native_handle()! core.start_transaction(unsafe { engine.TransactionFlag(flags) })! } pub fn (handle &Handle) trans_prepare() !&List { mut core := handle.native_handle()! result := core.prepare_transaction()! if !result.success { return core.error() } return unsafe { nil } } // DepProblem is one dependency that a failed prepare found broken: the package // that needs it, the dependency itself, and the package whose removal or // replacement breaks it pub struct DepProblem { pub: target string depend string causing string } // preserve structured dependency, conflict and architecture failures while // copying their strings before another getter can reset the handle's errno. pub fn (handle &Handle) trans_prepare_problems() ([]DepProblem, ?IError) { mut core := handle.native_handle() or { return []DepProblem{}, err } result := core.prepare_transaction() or { return []DepProblem{}, err } if result.success { return []DepProblem{}, none } problem := core.error() mut problems := []DepProblem{} for miss in result.missing { problems << DepProblem{ target: miss.target, depend: miss.dependency.compute_string(), causing: miss.causing_package } } for conflict in result.conflicts { problems << DepProblem{ target: conflict.package1.name, depend: conflict.reason.compute_string(), causing: conflict.package2.name } } for architecture in result.invalid_architectures { problems << DepProblem{ target: architecture } } return problems, problem } pub fn (handle &Handle) trans_commit() !&List { mut core := handle.native_handle()! core.commit_transaction()! return unsafe { nil } } pub fn (handle &Handle) trans_interrupt() ! { mut core := handle.native_handle()! core.interrupt_transaction()! } pub fn (handle &Handle) trans_release() ! { mut core := handle.native_handle()! core.release_transaction()! } pub fn (handle &Handle) sync_sysupgrade(enable_downgrade bool) ! { mut core := handle.native_handle()! core.plan_system_upgrade(enable_downgrade)! } pub fn (handle &Handle) add_pkg(pkg &Package) ! { mut core := handle.native_handle()! if pkg == unsafe { nil } { return core.set_error(.wrong_args, 'null package') } core.add_package(pkg.core)! } pub fn (handle &Handle) remove_pkg(pkg &Package) ! { mut core := handle.native_handle()! if pkg == unsafe { nil } { return core.set_error(.wrong_args, 'null package') } core.remove_package(pkg.core)! } pub fn (dbs_sync &List) sync_get_new_version(pkg &Package) &Package { pkg.native_package() or { return unsafe { nil } } found := engine.find_new_version(pkg.core, dbs_sync.native_databases()) or { return unsafe { nil } } return pkg.owner.native_package(found) } pub fn compute_md5sum(filename string) ?string { return engine.compute_md5sum(filename) } pub fn compute_sha256sum(filename string) ?string { return engine.compute_sha256sum(filename) } pub fn (handle &Handle) unlock() ! { if handle.unlock_async() != 0 { return handle.error() } } // status only: the signal path cannot allocate a V error or run callbacks. pub fn (handle &Handle) unlock_async() int { if handle == unsafe { nil } || handle.core == unsafe { nil } { return -1 } mut core := unsafe { handle.core } return core.unlock_async() } // bit positions, as for SigLevel @[flag] pub enum Caps { nls = 0 downloader = 1 signatures = 2 } pub fn version() string { return engine.engine_version } pub fn capabilities() Caps { return Caps.nls | .downloader | .signatures } pub fn (handle &Handle) sandbox_setup_child(sandboxuser string, sandbox_path string, restrict_syscalls bool) ! { mut core := handle.native_handle_preserving_error()! path := if sandbox_path.str == unsafe { nil } { ?string(none) } else { ?string(sandbox_path) } core.sandbox_setup_child(sandboxuser, path, restrict_syscalls)! } // helpers built on the API above: finding packages by name, walking the // dependency graph and reading the file list // strip_db_prefix removes a db prefix from a target, the way pacman accepts // 'local/patchelf' or 'system/bash' pub fn (handle &Handle) strip_db_prefix(target string) string { if target.starts_with('local/') && target.len > 6 { return target[6..] } dbs := handle.get_syncdbs() or { return target } for db in dbs.dbs() { prefix := db.get_name() + '/' if target.starts_with(prefix) && target.len > prefix.len { return target[prefix.len..] } } return target } // find_local_pkg looks a package up by name in the local db pub fn (handle &Handle) find_local_pkg(name string) ?&Package { localdb := handle.get_localdb() or { return none } mut db := localdb.native_database() or { return none } found := db.find_cached_package(name) or { return none } return handle.native_package(found) } // resolve_dep finds what satisfies a dependency string. the installed package // wins, so a tree reflects what is actually on the system, and the sync dbs // answer for everything else pub fn (handle &Handle) resolve_dep(installed &List, sync_dbs &List, depstring string) ?&Package { if pkg := installed.find_satisfier(depstring) { return pkg } return handle.find_dbs_satisfier(sync_dbs, depstring) } // outdated_deps walks the dependency closure of pkg against the sync dbs and // returns the sync versions of installed packages that are older than the // closure needs. only those are returned, so this is not a full upgrade pub fn (handle &Handle) outdated_deps(sync_dbs &List, pkg &Package) []&Package { mut out := []&Package{} mut seen := map[string]bool{} seen[pkg.get_name()] = true mut queue := []&Package{} queue << pkg mut i := 0 for i < queue.len { current := queue[i] i++ for dep in current.get_depends().depends() { cand := handle.find_dbs_satisfier(sync_dbs, dep.compute_string()) or { continue } name := cand.get_name() if seen[name] { continue } seen[name] = true if installed := handle.find_local_pkg(name) { if pkg_vercmp(installed.get_version(), cand.get_version()) < 0 { out << cand } } queue << cand } } return out } // is_unrequired is pacman's -Qdt filter: installed as a dependency, required // by nothing and not an optional dependency of anything pub fn (pkg &Package) is_unrequired() bool { return pkg.get_reason() == .depend && pkg.compute_required_by().len == 0 && pkg.compute_optional_for().len == 0 } // names returns the paths in a file list pub fn (filelist &Filelist) names() []string { mut out := []string{cap: int(filelist.count)} for i in 0 .. int(filelist.count) { out << unsafe { filelist.files[i].name.vstring() } } return out } // unneeded_closure extends a set of orphans to everything that becomes // unneeded once they are gone. a dependency is unneeded when every package // that requires it, and every package that optionally requires it, is going // away too. the set is grown to a fixpoint, so one removal is enough pub fn (handle &Handle) unneeded_closure(seed []&Package) []&Package { mut localdb := handle.get_localdb() or { return seed } mut pkgcache := localdb.get_pkgcache() or { return seed } installed := pkgcache.pkgs() mut removed := map[string]bool{} mut out := []&Package{} for p in seed { removed[p.get_name()] = true out << p } mut changed := true for changed { changed = false for p in installed { name := p.get_name() if removed[name] || p.get_reason() != .depend { continue } required := p.compute_required_by() optional := p.compute_optional_for() mut kept := false for r in required { if !removed[r] { kept = true break } } for o in optional { if !removed[o] { kept = true break } } if !kept { removed[name] = true out << p changed = true } } } return out } fn engine_dependency(dep &Depend) engine.Dependency { return engine.dependency_from_fields(dep.get_name(), dep.get_version(), dep.get_desc(), unsafe { engine.Depmod(dep.mod) }, dep.desc != unsafe { nil }) } // a failed literal match can still be satisfied by one of the package's provisions. pub fn (pkg &Package) satisfies(dep &Depend) bool { if pkg == unsafe { nil } || pkg.core == unsafe { nil } { return false } return pkg.satisfies_constraint(engine_dependency(dep)) } fn (pkg &Package) satisfies_constraint(constraint engine.Dependency) bool { if pkg.core == unsafe { nil } { return false } // deps.c only calls a metadata getter after the literal match fails. if pkg.core.name == constraint.name() && engine.version_satisfies(pkg.core.version, constraint) { return true } pkg.native_metadata(true, false) or { return false } if !pkg.provides_exposed { return pkg.core.provides.any(engine.provision_satisfies(it, constraint)) } // A caller can edit the public Depend records after borrowing the list. for node := pkg.native_provides(); node != unsafe { nil }; node = node.next { provision := node.get_depend() or { break } if engine.provision_satisfies(engine_dependency(provision), constraint) { return true } } return false } // sync_package finds a package by name in the sync dbs fn sync_package(sync_dbs &List, name string) ?&Package { mut dbs := unsafe { sync_dbs } for db in dbs.dbs() { if p := db.get_pkg(name) { return p } } return none } // dep_still_met reports whether a dependency of a package that is about to // be installed holds after the upgrades. only an installed provider that is // itself being upgraded can break it fn dep_still_met(pkgcache &List, new_ver map[string]&Package, dep &Depend) bool { sat := pkgcache.find_satisfier(dep.compute_string()) or { return true } provider := new_ver[sat.get_name()] or { return true } return provider.satisfies(dep) } // broken_dependents returns the sync packages that have to go along with an // upgrade. an installed package whose dependency is met by a package being // upgraded, but not by the new version of it, is upgraded to its sync version. // that version must be satisfied by the new versions, or the upgrade is // refused, since nothing sensible can be done with it pub fn (handle &Handle) broken_dependents(upgrades []&Package, sync_dbs &List) ![]&Package { mut localdb := handle.get_localdb()! mut pkgcache := localdb.get_pkgcache() or { return error('failed to get pkgcache') } installed := pkgcache.pkgs() mut new_ver := map[string]&Package{} for p in upgrades { new_ver[p.get_name()] = p } mut extra := []&Package{} mut changed := true for changed { changed = false for pkg in installed { name := pkg.get_name() if name in new_ver { continue } for dep in pkg.get_depends().depends() { sat := pkgcache.find_satisfier(dep.compute_string()) or { continue } provider := new_ver[sat.get_name()] or { continue } if provider.satisfies(dep) { continue } replacement := sync_package(sync_dbs, name) or { 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') } for d in replacement.get_depends().depends() { if !dep_still_met(pkgcache, new_ver, d) { return error('the repo version of ${name} needs ${d.compute_string()}, which the upgrade does not provide') } } new_ver[name] = replacement extra << replacement changed = true break } } } return extra }