module main import nspkg #include // is_url reports whether an install target is a URL rather than a name or // path. like pacman, anything with a scheme is one: the selected nspkg downloader // decides which schemes it supports. the scheme is // spelled the RFC writes it — a letter, then letters, digits, '.', '+', '-', // closed by '://' — so a path with a stray '://', which is a legal file // name, keeps being a path fn is_url(target string) bool { n := target.index('://') or { return false } scheme := target[..n] // a scheme opens with a letter, whatever follows if scheme.len < 1 || !(scheme[0] >= `a` && scheme[0] <= `z`) && !(scheme[0] >= `A` && scheme[0] <= `Z`) { return false } for c in scheme[1..] { match c { `a`...`z`, `A`...`Z`, `0`...`9`, `.`, `+`, `-` { continue } else { return false } } } return true } // fetch_url downloads a package from a URL into the cache with libalpm, so the // download user and the progress bars are the same as for repo packages fn fetch_url(handle &nspkg.Handle, url string) !string { // libalpm appends to the head of the list, so it needs the address of the head mut urls := &nspkg.List(unsafe { nil }) url_head := &urls url_head.append_strdup(url) defer { // the strdup'd urls are ours: inner first, then the list urls.free_inner(fn (p voidptr) { unsafe { C.free(p) } }) urls.free() } mut fetched := handle.fetch_pkgurl(urls)! defer { // libalpm strdup'd the fetched paths: them and their list are ours // to free the same way fetched.free_inner(fn (p voidptr) { unsafe { C.free(p) } }) fetched.free() } // strs() wraps the C strings without copying them (vstring), and the // defer above frees them: the path leaves as a copy, or the install // would load whatever libalpm puts in that memory next paths := fetched.strs() if paths.len == 0 { return error('nothing was downloaded from ${url}') } return paths[0].clone() } // url_trust_warning is what a url install is warned of, by the remote file // signature level its archive is loaded at: libalpm fetches the .sig beside // the url when the level checks packages, and the load verifies it. none // when the level requires a signature, which leaves nothing to warn of fn url_trust_warning(target string, level nspkg.SigLevel) ?string { if !level.has(.package) { return '${target}: package signatures are not checked' } if level.has(.package_optional) { return '${target}: the signature is checked only if the server offers one' } return none } // fetch_target_url downloads a package archive named by a URL through the // install's own handle: the download draws on the install's display, and // whatever libalpm asks on the way goes through the install's prompter. the // warning before it says what the remote file signature level will check. // it returns the cached file, installed then with the other targets fn fetch_target_url(mut ui TransUI, handle &nspkg.Handle, target string) !string { level := handle.option_get_remote_file_siglevel() or { return error('could not read the remote file signature level') } if note := url_trust_warning(target, level) { ui.warn(note) } return fetch_url(handle, target)! }