vpac / src / files.v
221 lines · 206 sloc · 6.56 KB · 841315c100c81d9e6b14c4b55a25d4e3d12db762
Raw
1module main
2
3import nspkg
4import os
5
6fn files(config ConfigFiles) ! {
7 need_query(config.query, 'package')!
8 // no display: the ui is there for the did-you-mean question
9 mut ui := new_trans_ui(noconfirm: config.noconfirm)
10 defer { ui.stop() }
11 mut handle := nspkg.initialize(root_path, db_path) or { return err }
12 defer { handle.release() or { went_on('release the nspkg handle', err) } }
13
14 mut pkgcache := local_pkgcache(handle)!
15
16 for target in config.query {
17 mut name := local_target_name(target)
18 pkg := find_local_target(pkgcache, name, true) or {
19 name = did_you_mean(mut ui, name, pkg_names(pkgcache.pkgs())) or {
20 target_error(err.msg())
21 continue
22 }
23 pkgcache.pkg_find(name) or {
24 target_error('unknown package: "${name}"')
25 continue
26 }
27 }
28
29 paths := pkg_paths(pkg)
30 if ipc_on() {
31 emit(FilesEvent{
32 package: pkg.get_name()
33 paths: paths
34 })
35 continue
36 }
37 println(table_text(paths.map(row(name_style(pkg.get_name()), it)), '', ' '))
38 }
39}
40
41// pkg_paths are the files of an installed package, as paths under the root
42fn pkg_paths(pkg &nspkg.Package) []string {
43 return pkg.get_files().names().map('${root_path}${it}')
44}
45
46// OwnerQuery is one target of owner mode, the way pacman's query_fileowner
47// shapes it: rel is what the filelists are searched for, relative to the
48// root — a directory spelled with its trailing slash, as filelists spell
49// one — and dir says a directory is asked for
50struct OwnerQuery {
51 rel string
52 dir bool
53}
54
55// file_owners are the installed packages whose filelists hold q.rel, an
56// exact match as alpm_filelist_contains makes it. a file has one owner and
57// the walk stops there; a directory is shared, and every owner answers, as
58// in pacman
59fn file_owners(pkgs []&nspkg.Package, q OwnerQuery) []&nspkg.Package {
60 mut out := []&nspkg.Package{}
61 for pkg in pkgs {
62 if pkg.get_files().contains(q.rel) != none {
63 out << pkg
64 if !q.dir {
65 break
66 }
67 }
68 }
69 return out
70}
71
72// owner_query makes a target into the path the filelists are searched for,
73// step for step as pacman's query_fileowner does: trailing slashes go, a
74// missing bare name is looked up in PATH, the directory the name sits in is
75// canonicalized while the name itself is not (a symlink is owned as the
76// link, the way a filelist lists it), and a directory takes its slash back
77fn owner_query(target string) !OwnerQuery {
78 if target == '' {
79 return error('empty path')
80 }
81 // a trailing slash would make lstat follow a directory symlink; it still
82 // asks for a directory, unless what is there says otherwise. / stays
83 mut p := target
84 mut dir := false
85 for p.len > 1 && p.ends_with('/') {
86 p = p[..p.len - 1]
87 dir = true
88 }
89 mut missing := false
90 mut kind := os.FileType.unknown
91 if st := os.lstat(p) {
92 kind = st.get_filetype()
93 } else {
94 missing = true
95 // not a path but a program name: the first PATH entry that holds it
96 if !p.contains('/') {
97 if found := search_path(p) {
98 p = found
99 missing = false
100 kind = (os.lstat(p) or { os.Stat{} }).get_filetype()
101 }
102 }
103 }
104 // a directory that cannot be resolved (it does not exist) leaves the
105 // path as named. pacman proceeds with it verbatim, which drops a relative
106 // one as outside the root; it is made absolute here instead, so a file a
107 // package lists under a since deleted directory still finds its owner
108 mut path := lrealpath(p) or { os.abs_path(p) }
109 // an existing path is a directory by what it is, a missing one by the
110 // slash it was given
111 dir = if missing { dir } else { kind == .directory }
112 if dir && !path.ends_with('/') {
113 path += '/'
114 }
115 // the root itself is the empty relative path: no package lists it, so it
116 // gets the plain no-owner answer, not the outside-root one. the real
117 // filespace '/' and a fake root '/x/' both end in their slash, which a
118 // missing root (no directory to say so) can lack
119 if path == root_path || path + '/' == root_path {
120 return OwnerQuery{
121 dir: true
122 }
123 }
124 if !path.starts_with(root_path) {
125 return error('file is outside of root (${root_path}): ${path}')
126 }
127 return OwnerQuery{
128 rel: path[root_path.len..]
129 dir: dir
130 }
131}
132
133// search_path is pacman's own PATH search for a bare name: the first entry
134// where the name exists at all, as lstat sees it — a link or a file that is
135// not executable counts, as in pacman. an empty entry would be the working
136// directory, which the caller has looked in already
137fn search_path(name string) ?string {
138 for entry in os.getenv('PATH').split(':') {
139 if entry == '' {
140 continue
141 }
142 candidate := '${entry.trim_right('/')}/${name}'
143 if _ := os.lstat(candidate) {
144 return candidate
145 }
146 }
147 return none
148}
149
150// lrealpath is pacman's: the directory a path names its file in is resolved
151// (links, '.' and '..'), and the file itself is kept as named, so a link
152// stays the link. a path ending in '.' or '..' names a directory, which is
153// resolved whole. none when the directory does not resolve
154fn lrealpath(path string) ?string {
155 base := path.all_after_last('/')
156 if base in ['.', '..'] {
157 return realpath(path)
158 }
159 slash := path.last_index('/') or { -1 }
160 dname := match slash {
161 -1 { '.' }
162 0 { '/' }
163 else { path[..slash] }
164 }
165 resolved := realpath(dname)?
166 return if resolved.ends_with('/') { resolved + base } else { '${resolved}/${base}' }
167}
168
169// realpath is realpath(3), none when it fails: os.real_path hands back its
170// input on failure, which here must tell a resolved path from an unresolved
171fn realpath(path string) ?string {
172 res := C.realpath(&char(path.str), unsafe { nil })
173 if res == unsafe { nil } {
174 return none
175 }
176 out := unsafe { cstring_to_vstring(res) }
177 unsafe { C.free(res) }
178 return out
179}
180
181fn owner(config ConfigOwner) ! {
182 need_query(config.query, 'file')!
183 mut handle := nspkg.initialize(root_path, db_path) or { return err }
184 defer { handle.release() or { went_on('release the nspkg handle', err) } }
185
186 mut pkgcache := local_pkgcache(handle)!
187
188 installed := pkgcache.pkgs()
189 for target in config.query {
190 q := owner_query(target) or {
191 target_error(err.msg())
192 continue
193 }
194 path := '${root_path}${q.rel}'
195 owners := file_owners(installed, q)
196 if owners.len == 0 {
197 target_error('no package owns ${path}')
198 continue
199 }
200 // a directory's owners are one event each, the path repeated
201 if ipc_on() {
202 for pkg in owners {
203 emit(OwnerEvent{
204 path: path
205 package: pkg.get_name()
206 version: pkg.get_version()
207 })
208 }
209 continue
210 }
211 // pacman's -Qqo: the owners' names alone
212 if config.quiet {
213 for pkg in owners {
214 println(pkg.get_name())
215 }
216 continue
217 }
218 println(table_text(owners.map(row(name_style(path), name_style(it.get_name()),
219 version_style(it.get_version()))), '', ' '))
220 }
221}
222