vpac / src / info.v
200 lines · 185 sloc · 5.88 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import alpm
4
5// InfoLookup is how info and tree find their targets: the installed
6// packages first, then the sync dbs, which are only set up — and asked
7// about their freshness — by the first target that needs them
8struct InfoLookup {
9mut:
10 installed &alpm.List
11 dbs []&alpm.DB
12 synced bool
13}
14
15// sync_dbs registers the sync dbs once for all targets
16fn (mut l InfoLookup) sync_dbs(mut ui TransUI, mut handle &alpm.Handle) ![]&alpm.DB {
17 if !l.synced {
18 l.dbs = init_repos(mut handle)!
19 check_sync_fresh(mut ui, mut handle)!
20 l.synced = true
21 }
22 return l.dbs
23}
24
25// find resolves one target: 'local/name' in the installed packages alone,
26// 'repo/name' in that sync db alone, a bare name in the installed packages
27// and then every sync db, like pacman's -Qi with its -Si behind it. a name
28// may be a dependency string or something provided. what misses gets the
29// nearest name of the same scope offered; an error is the target's own,
30// for the caller to report and go on from
31fn (mut l InfoLookup) find(mut ui TransUI, mut handle &alpm.Handle, raw string) !&alpm.Package {
32 target := parse_target(raw)
33 if target.repo in ['', 'local'] {
34 if p := find_local_target(l.installed, target.name, true) {
35 return p
36 }
37 }
38 mut scoped := []&alpm.DB{}
39 if target.repo != 'local' {
40 scoped = scope_dbs(l.sync_dbs(mut ui, mut handle)!, target.repo)!
41 if p := find_sync_target(handle, scoped, target.name, target.repo != '') {
42 return p
43 }
44 }
45 mut names := []string{}
46 if target.repo in ['', 'local'] {
47 names << pkg_names(l.installed.pkgs())
48 }
49 names << sync_names(scoped)
50 fixed := did_you_mean(mut ui, target.name, names)!
51 if target.repo in ['', 'local'] {
52 if p := l.installed.pkg_find(fixed) {
53 return p
54 }
55 }
56 return find_repo_pkg(scoped, fixed) or { error('unknown package: "${fixed}"') }
57}
58
59fn info(config ConfigInfo) ! {
60 need_query(config.query, 'package')!
61 fields := parse_fields(config.fields, pkg_field_names)!
62 mut ui := new_trans_ui(noconfirm: config.noconfirm)
63 mut handle := init_handle(ui)!
64 defer { handle.release() or { went_on('release the alpm handle', err) } }
65 defer { ui.stop() }
66
67 // package archives on disk are shown directly, pacman's -Qp: loaded, not
68 // installed. -p says every target is one; without it a single target
69 // that is a file on disk is still taken for the archive it is
70 if config.file || (config.query.len == 1 && is_local_pkg_file(config.query[0])) {
71 info_files(handle, config, fields)
72 return
73 }
74
75 mut lookup := InfoLookup{
76 installed: local_pkgcache(handle)!
77 }
78 mut printed := 0
79 for target in config.query {
80 pkg := lookup.find(mut ui, mut handle, target) or {
81 target_error(err.msg())
82 continue
83 }
84 if show_pkg(pkg, config, fields, printed == 0) {
85 printed++
86 }
87 }
88}
89
90// show_pkg is one target's answer: its changelog with -c, its fields
91// otherwise. it says whether anything was shown, which is what the blank
92// line before the next one goes by
93fn show_pkg(pkg &alpm.Package, config ConfigInfo, fields []string, first bool) bool {
94 if config.changelog {
95 return show_changelog(pkg, first)
96 }
97 show_info(info_fields(pkg, fields, config.fields != ''), first)
98 return true
99}
100
101// show_info shows one package's fields: a table on a terminal, after a blank
102// line unless it is the first; an info event for a frontend
103fn show_info(fields []InfoField, first bool) {
104 if ipc_on() {
105 emit(InfoEvent{
106 fields: fields
107 })
108 return
109 }
110 if !first {
111 println('')
112 }
113 println(info_table(fields))
114}
115
116// pkg_changelog reads a package's changelog whole, in chunks as libalpm
117// hands it out (an fread into the buffer, 0 at its end). none when the
118// package carries none: libalpm has one for an installed package and for a
119// package file, never for a sync db's entry
120fn pkg_changelog(pkg &alpm.Package) ?string {
121 fp := pkg.changelog_open()
122 if fp == unsafe { nil } {
123 return none
124 }
125 mut text := []u8{}
126 mut buf := []u8{len: 4096}
127 for {
128 n := int(pkg.changelog_read(buf.data, u32(buf.len), fp))
129 if n <= 0 {
130 break
131 }
132 text << buf[..n]
133 }
134 pkg.changelog_close(fp) or {
135 warn('could not close the changelog of ${pkg.get_name()}: ${err.msg()}')
136 }
137 return text.bytestr()
138}
139
140// show_changelog is pacman's -Qc: the heading, then the changelog as the
141// package carries it. a package without one is said and shows nothing,
142// which pacman does not count as a failure either
143fn show_changelog(pkg &alpm.Package, first bool) bool {
144 text := pkg_changelog(pkg) or {
145 warn('no changelog available for ${pkg.get_name()}')
146 return false
147 }
148 if ipc_on() {
149 emit(ChangelogEvent{
150 package: pkg.get_name()
151 text: text
152 })
153 return true
154 }
155 if !first {
156 println('')
157 }
158 println(heading('changelog for ${pkg.get_name()}:'))
159 println(text.trim_right('\n'))
160 return true
161}
162
163// info_files shows package archives from disk, pacman's -Qp, each loaded
164// the way pacman's query loads one: whole, with no signature asked for,
165// since nothing of it is installed. one that does not load is its own
166// target's error, and the rest are still shown
167fn info_files(handle &alpm.Handle, config ConfigInfo, fields []string) {
168 mut printed := 0
169 for path in config.query {
170 pkg := handle.pkg_load(path, 1, 0) or {
171 target_error('could not load ${path}: ${err.msg()}')
172 continue
173 }
174 if show_pkg(pkg, config, fields, printed == 0) {
175 printed++
176 }
177 pkg.free_pkg() or { warn('could not free the package of ${path}: ${err.msg()}') }
178 }
179}
180
181// which finds the installed package that provides a dependency
182fn which(config ConfigWhich) ! {
183 need_query(config.query, 'dependency')!
184 ui := new_trans_ui()
185 mut handle := init_handle(ui)!
186 defer { handle.release() or { went_on('release the alpm handle', err) } }
187 defer { ui.stop() }
188
189 mut pkgcache := local_pkgcache(handle)!
190
191 mut printed := 0
192 for target in config.query {
193 pkg := pkgcache.find_satisfier(local_target_name(target)) or {
194 target_error('unknown package: "${target}"')
195 continue
196 }
197 show_info(info_fields(pkg, pkg_field_names, false), printed == 0)
198 printed++
199 }
200}
201