vpac / src / url_install.v
93 lines · 87 sloc · 3.17 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import alpm
4
5#include <stdlib.h>
6
7// is_url reports whether an install target is a URL rather than a name or
8// path. like pacman, anything with a scheme is one: libalpm's fetcher, curl,
9// knows which schemes it can fetch (file://, ftps://, ...). the scheme is
10// spelled the RFC writes it — a letter, then letters, digits, '.', '+', '-',
11// closed by '://' — so a path with a stray '://', which is a legal file
12// name, keeps being a path
13fn is_url(target string) bool {
14 n := target.index('://') or { return false }
15 scheme := target[..n]
16 // a scheme opens with a letter, whatever follows
17 if scheme.len < 1 || !(scheme[0] >= `a` && scheme[0] <= `z`) && !(scheme[0] >= `A` && scheme[0] <= `Z`) {
18 return false
19 }
20 for c in scheme[1..] {
21 match c {
22 `a`...`z`, `A`...`Z`, `0`...`9`, `.`, `+`, `-` {
23 continue
24 }
25 else {
26 return false
27 }
28 }
29 }
30 return true
31}
32
33// fetch_url downloads a package from a URL into the cache with libalpm, so the
34// download user and the progress bars are the same as for repo packages
35fn fetch_url(handle &alpm.Handle, url string) !string {
36 // libalpm appends to the head of the list, so it needs the address of the head
37 mut urls := &alpm.List(unsafe { nil })
38 url_head := &urls
39 url_head.append_strdup(url)
40 defer {
41 // the strdup'd urls are ours: inner first, then the list
42 urls.free_inner(fn (p voidptr) {
43 unsafe { C.free(p) }
44 })
45 urls.free()
46 }
47 mut fetched := handle.fetch_pkgurl(urls)!
48 defer {
49 // libalpm strdup'd the fetched paths: them and their list are ours
50 // to free the same way
51 fetched.free_inner(fn (p voidptr) {
52 unsafe { C.free(p) }
53 })
54 fetched.free()
55 }
56 // strs() wraps the C strings without copying them (vstring), and the
57 // defer above frees them: the path leaves as a copy, or the install
58 // would load whatever libalpm puts in that memory next
59 paths := fetched.strs()
60 if paths.len == 0 {
61 return error('nothing was downloaded from ${url}')
62 }
63 return paths[0].clone()
64}
65
66// url_trust_warning is what a url install is warned of, by the remote file
67// signature level its archive is loaded at: libalpm fetches the .sig beside
68// the url when the level checks packages, and the load verifies it. none
69// when the level requires a signature, which leaves nothing to warn of
70fn url_trust_warning(target string, level alpm.SigLevel) ?string {
71 if !level.has(.package) {
72 return '${target}: package signatures are not checked'
73 }
74 if level.has(.package_optional) {
75 return '${target}: the signature is checked only if the server offers one'
76 }
77 return none
78}
79
80// fetch_target_url downloads a package archive named by a URL through the
81// install's own handle: the download draws on the install's display, and
82// whatever libalpm asks on the way goes through the install's prompter. the
83// warning before it says what the remote file signature level will check.
84// it returns the cached file, installed then with the other targets
85fn fetch_target_url(mut ui TransUI, handle &alpm.Handle, target string) !string {
86 level := handle.option_get_remote_file_siglevel() or {
87 return error('could not read the remote file signature level')
88 }
89 if note := url_trust_warning(target, level) {
90 ui.warn(note)
91 }
92 return fetch_url(handle, target)!
93}
94