| 1 | module main |
| 2 | |
| 3 | import nspkg |
| 4 | import os |
| 5 | import v.profiler |
| 6 | |
| 7 | // one archive file sitting in the cache, with its .sig companion removed |
| 8 | // alongside it, like pacman's cleancache does. the failures are collected and |
| 9 | // reported after: a clean that could not remove a file does not look |
| 10 | // successful, and the meter only moves past the files that went |
| 11 | fn cache_purge(mut ui TransUI, files []string) { |
| 12 | mut bar := ui.meter(files.len, 'removing cached packages') |
| 13 | bar.start() |
| 14 | mut failed := []string{} |
| 15 | for f in files { |
| 16 | os.rm(f) or { |
| 17 | failed << f |
| 18 | continue |
| 19 | } |
| 20 | // the sig goes only once its package did. one that was never signed |
| 21 | // has no companion to remove, so only a real failure is reported |
| 22 | if os.exists(f + '.sig') { |
| 23 | os.rm(f + '.sig') or { failed << f + '.sig' } |
| 24 | } |
| 25 | bar.add(1) |
| 26 | } |
| 27 | if failed.len > 0 { |
| 28 | warn('could not remove ${failed.len} of ${files.len} cached files: ${failed.map(os.base(it)).join(', ')}') |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // is_pkg_archive is whether a file name is a package archive's: compressed |
| 33 | // (.pkg.tar.zst and the like) or the plain .pkg.tar makepkg writes with no |
| 34 | // compression, as pacman's cache clean takes both; never a signature |
| 35 | fn is_pkg_archive(f string) bool { |
| 36 | return (f.contains('.pkg.tar.') || f.ends_with('.pkg.tar')) && !f.ends_with('.sig') |
| 37 | } |
| 38 | |
| 39 | fn cached_pkg_files() ![]string { |
| 40 | mut out := []string{} |
| 41 | for dir in cfg_cachedirs { |
| 42 | names := os.ls(dir) or { |
| 43 | // one unreadable cache dir does not take the others' cleaning |
| 44 | // down with it: the run cleans what it can see, and says so |
| 45 | warn('could not list the cache directory ${dir}: ${err.msg()}, cleaning the rest') |
| 46 | continue |
| 47 | } |
| 48 | // package archives only, with their directory prefixed; sigs go with |
| 49 | // their package, dbs stay |
| 50 | out << names.filter(|f| is_pkg_archive(f)).map(|f| os.join_path(dir, f)) |
| 51 | } |
| 52 | return out |
| 53 | } |
| 54 | |
| 55 | // sync_db_exts are the files a sync db is kept as, sigs and file dbs too |
| 56 | const sync_db_exts = ['.db.sig', '.files.sig', '.db', '.files'] |
| 57 | |
| 58 | // unused_sync_dbs are the sync db files of repos the config does not name |
| 59 | // any more, what pacman's -Sc asks about as 'unused repositories'. pacman |
| 60 | // clears everything else out of the sync dir with them; only what is |
| 61 | // plainly a db goes here, so the sync stamp and anything unknown stay |
| 62 | fn unused_sync_dbs() []string { |
| 63 | dir := db_path + 'sync/' |
| 64 | if !os.is_dir(dir) { |
| 65 | return []string{} |
| 66 | } |
| 67 | mut names := os.ls(dir) or { |
| 68 | warn('could not list the sync databases in ${dir}: ${err.msg()}, none is cleaned') |
| 69 | return []string{} |
| 70 | } |
| 71 | names.sort() |
| 72 | known := cfg_repos.filter(it.kind != 'pkgbuild').map(it.name) |
| 73 | mut out := []string{} |
| 74 | for f in names { |
| 75 | for ext in sync_db_exts { |
| 76 | if f.len > ext.len && f.ends_with(ext) { |
| 77 | if f[..f.len - ext.len] !in known { |
| 78 | out << dir + f |
| 79 | } |
| 80 | break |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return out |
| 85 | } |
| 86 | |
| 87 | // a cached package archive: pkgname-pkgver-pkgrel-arch.pkg.tar[.*] |
| 88 | struct CacheFile { |
| 89 | name string |
| 90 | version string |
| 91 | file string |
| 92 | } |
| 93 | |
| 94 | // installed_file is whether a cached archive is the installed version of its |
| 95 | // package. the cache is listed as full paths and the prefixes are |
| 96 | // name-version-, so the file name is what they are matched against: its |
| 97 | // name and version read out whole, as pacman compares the archive's own, so |
| 98 | // a package whose name merely begins with another's name-version- is not |
| 99 | // taken for it. a name that does not read as an archive's is matched by its |
| 100 | // beginning, the careful way round: it is kept |
| 101 | fn installed_file(f string, keep_prefixes []string) bool { |
| 102 | if c := parse_cache_file(f) { |
| 103 | return '${c.name}-${c.version}-' in keep_prefixes |
| 104 | } |
| 105 | name := os.base(f) |
| 106 | return keep_prefixes.any(|k| name.starts_with(k)) |
| 107 | } |
| 108 | |
| 109 | fn parse_cache_file(f string) ?CacheFile { |
| 110 | base := os.base(f).all_before('.pkg.tar') |
| 111 | toks := base.split('-') |
| 112 | if toks.len < 4 { |
| 113 | return none |
| 114 | } |
| 115 | arch_t := toks.last() |
| 116 | rel := toks[toks.len - 2] |
| 117 | ver := toks[toks.len - 3] |
| 118 | name := toks[..toks.len - 3].join('-') |
| 119 | if arch_t == '' || rel == '' || ver == '' || name == '' { |
| 120 | return none |
| 121 | } |
| 122 | return CacheFile{ |
| 123 | name: name |
| 124 | version: ver + '-' + rel |
| 125 | file: f |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // paccache's KeepInstalled+KeepN: keep the newest `keep_n` versions of every |
| 130 | // package, plus whatever is currently installed. an installed version is |
| 131 | // kept once even though it also counts among the newest, so the answer is a |
| 132 | // set and the caller's filtering reads it straight |
| 133 | fn paccache_keep(keep_n int, files []string, keep_prefixes []string) map[string]bool { |
| 134 | mut groups := map[string][]CacheFile{} |
| 135 | mut kept := map[string]bool{} |
| 136 | for f in files { |
| 137 | // installed versions are always kept, and they still count among the |
| 138 | // newest: -k 2 keeps two versions, not the installed one and two more |
| 139 | if installed_file(f, keep_prefixes) { |
| 140 | kept[f] = true |
| 141 | } |
| 142 | cf := parse_cache_file(f) or { continue } |
| 143 | if cf.name !in groups { |
| 144 | groups[cf.name] = []CacheFile{} |
| 145 | } |
| 146 | groups[cf.name] << cf |
| 147 | } |
| 148 | for name in groups.keys() { |
| 149 | mut gg := groups[name] |
| 150 | // newest first, using the same vercmp as pacman |
| 151 | gg.sort_with_compare(fn (a &CacheFile, b &CacheFile) int { |
| 152 | return nspkg.pkg_vercmp(b.version, a.version) |
| 153 | }) |
| 154 | for i, cf in gg { |
| 155 | if i < keep_n { |
| 156 | kept[cf.file] = true |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | return kept |
| 161 | } |
| 162 | |
| 163 | // BuildClean is what vpac c does with the pkgbuild build dirs: of the builds |
| 164 | // found under root, the ones it removes and their size |
| 165 | struct BuildClean { |
| 166 | user string |
| 167 | root string |
| 168 | mut: |
| 169 | found int |
| 170 | dirs []string |
| 171 | size u64 |
| 172 | // partial says the size is a floor: part of a build could not be read, |
| 173 | // and what is there is more than was counted |
| 174 | partial bool |
| 175 | } |
| 176 | |
| 177 | // build_dirs_to_clean picks the pkgbuild builds vpac c removes. like paru's |
| 178 | // and yay's clean, a build stays while its package is installed: its dir is |
| 179 | // named after an installed package base (vpac i --pkgbuild), or it holds a |
| 180 | // built archive of an installed package (vpac b, split packages). with all, |
| 181 | // every build goes. a build is pulled afresh each time, so nothing in a |
| 182 | // removed one is ever reused |
| 183 | fn build_dirs_to_clean(user string, all bool, bases []string, names []string) !BuildClean { |
| 184 | root := pkgbuild_root(user)! |
| 185 | mut bc := BuildClean{ |
| 186 | user: user |
| 187 | root: root |
| 188 | } |
| 189 | if !os.is_dir(root) { |
| 190 | return bc |
| 191 | } |
| 192 | for entry in os.ls(root)! { |
| 193 | dir := os.join_path(root, entry) |
| 194 | if os.is_link(dir) || !os.is_dir(dir) { |
| 195 | continue |
| 196 | } |
| 197 | bc.found++ |
| 198 | if !all && build_in_use(dir, entry, bases, names) { |
| 199 | continue |
| 200 | } |
| 201 | bc.dirs << dir |
| 202 | size, whole := tree_size(dir) |
| 203 | bc.size += size |
| 204 | if !whole { |
| 205 | bc.partial = true |
| 206 | } |
| 207 | } |
| 208 | return bc |
| 209 | } |
| 210 | |
| 211 | // build_in_use is whether a build dir belongs to an installed package |
| 212 | fn build_in_use(dir string, entry string, bases []string, names []string) bool { |
| 213 | if entry in bases { |
| 214 | return true |
| 215 | } |
| 216 | // fail closed: when the build cannot even be listed, it cannot be proven |
| 217 | // unused, and clean never deletes something it is not sure of. clean runs |
| 218 | // as root through run_as_user, so an unreadable dir is reported plainly |
| 219 | pkgs := archives_in(dir) or { |
| 220 | warn("could not list the built packages of '${dir}': ${err.msg()}, keeping the build") |
| 221 | return true |
| 222 | } |
| 223 | for archive in pkgs { |
| 224 | cf := parse_cache_file(os.base(archive)) or { continue } |
| 225 | if cf.name in names { |
| 226 | return true |
| 227 | } |
| 228 | } |
| 229 | return false |
| 230 | } |
| 231 | |
| 232 | // tree_size adds up the files under path, and says whether that is the |
| 233 | // whole of it: a directory that cannot be listed is warned about and |
| 234 | // counted as nothing, which makes the sum a floor and not the size. the |
| 235 | // rest of the tree is still counted. links are not followed: a build tree |
| 236 | // may point anywhere |
| 237 | fn tree_size(path string) (u64, bool) { |
| 238 | if os.is_link(path) { |
| 239 | return 0, true |
| 240 | } |
| 241 | if !os.is_dir(path) { |
| 242 | return os.file_size(path), true |
| 243 | } |
| 244 | names := os.ls(path) or { |
| 245 | warn('could not read ${path}: ${err.msg()}, its size is left out') |
| 246 | return 0, false |
| 247 | } |
| 248 | mut total := u64(0) |
| 249 | mut whole := true |
| 250 | for name in names { |
| 251 | size, sub_whole := tree_size(os.join_path(path, name)) |
| 252 | total += size |
| 253 | whole = whole && sub_whole |
| 254 | } |
| 255 | return total, whole |
| 256 | } |
| 257 | |
| 258 | // size_text spells a size for a person; one that is a floor says so |
| 259 | fn size_text(size u64, partial bool) string { |
| 260 | text := profiler.format_bytes(size) |
| 261 | return if partial { 'at least ${text}' } else { text } |
| 262 | } |
| 263 | |
| 264 | // clean_event is what a frontend is told before the proceed question. the |
| 265 | // build root goes out only when an invoker was known and its builds looked |
| 266 | // at; BuildClean{} is the unlooked-at one |
| 267 | fn clean_event(to_remove []string, total u64, builds BuildClean, dbs []string, preview bool) CleanEvent { |
| 268 | return CleanEvent{ |
| 269 | cache_dirs: cfg_cachedirs |
| 270 | build_root: if builds.user != '' { ?string(builds.root) } else { none } |
| 271 | files: to_remove |
| 272 | files_size: total |
| 273 | builds: builds.dirs |
| 274 | build_size: builds.size |
| 275 | build_size_partial: builds.partial |
| 276 | dbs: dbs |
| 277 | preview: preview |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | fn clean(config ConfigClean) ! { |
| 282 | mut ui := new_trans_ui(noconfirm: config.noconfirm) |
| 283 | mut handle := init_handle(ui)! |
| 284 | defer { handle.release() or { went_on('release the nspkg handle', err) } } |
| 285 | defer { ui.stop() } |
| 286 | |
| 287 | mut pkgcache := local_pkgcache(handle)! |
| 288 | installed := pkgcache.pkgs() |
| 289 | |
| 290 | // the package cache: everything with -a or -cc (pacman's -Scc), |
| 291 | // otherwise pacman's default KeepInstalled clean method, optionally |
| 292 | // paccache style with -k N: keep the newest N versions of every package |
| 293 | every := config.all || config.level >= 2 |
| 294 | pkg_files := cached_pkg_files()! |
| 295 | to_remove := if every { |
| 296 | pkg_files |
| 297 | } else { |
| 298 | mut keep_prefixes := installed.map(|pkg| '${pkg.get_name()}-${pkg.get_version()}-') |
| 299 | // pacman's KeepCurrent: a version a sync db still carries stays too |
| 300 | if config.keep_current { |
| 301 | for db in init_repos(mut handle)! { |
| 302 | // a sync db that cannot be read would keep nothing: what it |
| 303 | // carries would be cleaned as if no repo had it any more |
| 304 | mut cache := pkgcache_of(handle, db)! |
| 305 | keep_prefixes << cache.pkgs().map(|pkg| '${pkg.get_name()}-${pkg.get_version()}-') |
| 306 | } |
| 307 | } |
| 308 | to_keep := if config.keep > 0 { |
| 309 | // a set of paths, not a list: a cache of thousands of archives is |
| 310 | // filtered by map lookup per file, not a scan per file |
| 311 | paccache_keep(config.keep, pkg_files, keep_prefixes) |
| 312 | } else { |
| 313 | mut keep := map[string]bool{} |
| 314 | for f in pkg_files.filter(|f| installed_file(f, keep_prefixes)) { |
| 315 | keep[f] = true |
| 316 | } |
| 317 | keep |
| 318 | } |
| 319 | pkg_files.filter(|f| !to_keep[f]) |
| 320 | } |
| 321 | mut total := u64(0) |
| 322 | for f in to_remove { |
| 323 | total += os.file_size(f) |
| 324 | } |
| 325 | |
| 326 | // the pkgbuild builds are the invoker's: root running vpac c itself has |
| 327 | // none, since builds never run as root. a root session nobody asked for |
| 328 | // says so, instead of quietly leaving every build dir behind it |
| 329 | mut builds := BuildClean{} |
| 330 | if user := invoker_user() { |
| 331 | builds = build_dirs_to_clean(user, config.all, installed.map(it.get_base()), |
| 332 | installed.map(it.get_name()))! |
| 333 | } else { |
| 334 | warn('pkgbuild builds are not cleaned: run vpac as your own user') |
| 335 | } |
| 336 | |
| 337 | // the sync dbs of repos the config dropped go with any clean, as |
| 338 | // pacman's -Sc offers |
| 339 | dbs := unused_sync_dbs() |
| 340 | |
| 341 | // a frontend gets what would go as one event; the proceed question follows |
| 342 | // it unless this is a --print preview |
| 343 | if ipc_on() { |
| 344 | emit(clean_event(to_remove, total, builds, dbs, config.print_only)) |
| 345 | if config.print_only || (to_remove.len == 0 && builds.dirs.len == 0 && dbs.len == 0) { |
| 346 | return |
| 347 | } |
| 348 | } |
| 349 | // --print lists what would go, a path a line, and removes nothing |
| 350 | if config.print_only { |
| 351 | for f in to_remove { |
| 352 | println(f) |
| 353 | } |
| 354 | for dir in builds.dirs { |
| 355 | println(dir) |
| 356 | } |
| 357 | for f in dbs { |
| 358 | println(f) |
| 359 | } |
| 360 | return |
| 361 | } |
| 362 | |
| 363 | mut summary := [kv('cache directory', cfg_cachedirs.join(', '))] |
| 364 | if pkg_files.len > 0 { |
| 365 | if every { |
| 366 | summary << kv('remove', 'all ${pkg_files.len} files (${profiler.format_bytes(total)})') |
| 367 | } else { |
| 368 | summary << kv('keep', 'all locally installed packages') |
| 369 | if config.keep_current { |
| 370 | summary << row('', 'the versions the sync dbs carry') |
| 371 | } |
| 372 | if config.keep > 0 { |
| 373 | summary << row('', 'the ${config.keep} newest versions of every package') |
| 374 | } |
| 375 | summary << kv('remove', if to_remove.len == 0 { |
| 376 | 'nothing' |
| 377 | } else { |
| 378 | '${to_remove.len} files (${profiler.format_bytes(total)})' |
| 379 | }) |
| 380 | } |
| 381 | } |
| 382 | if builds.found > 0 { |
| 383 | summary << kv('build directory', builds.root) |
| 384 | if config.all { |
| 385 | summary << kv('remove', 'all ${builds.found} builds (${size_text(builds.size, builds.partial)})') |
| 386 | } else { |
| 387 | summary << kv('keep', 'the builds of installed packages') |
| 388 | summary << kv('remove', if builds.dirs.len == 0 { |
| 389 | 'nothing' |
| 390 | } else { |
| 391 | '${builds.dirs.len} builds (${size_text(builds.size, builds.partial)}): ${builds.dirs.map(os.base(it)).join(', ')}' |
| 392 | }) |
| 393 | } |
| 394 | } |
| 395 | if dbs.len > 0 { |
| 396 | summary << kv('database directory', db_path + 'sync/') |
| 397 | summary << kv('remove', '${dbs.len} files of unused repos: ${dbs.map(os.base(it)).join(', ')}') |
| 398 | } |
| 399 | if !ipc_on() { |
| 400 | println(table_text(summary, '', ' ')) |
| 401 | } |
| 402 | if to_remove.len == 0 && builds.dirs.len == 0 && dbs.len == 0 { |
| 403 | println(if pkg_files.len == 0 && builds.found == 0 { |
| 404 | 'cache is empty' |
| 405 | } else { |
| 406 | 'nothing to remove' |
| 407 | }) |
| 408 | return |
| 409 | } |
| 410 | if !ui.proceed('proceed?') { |
| 411 | return |
| 412 | } |
| 413 | if to_remove.len > 0 { |
| 414 | cache_purge(mut ui, to_remove) |
| 415 | } |
| 416 | if dbs.len > 0 { |
| 417 | mut failed := []string{} |
| 418 | for f in dbs { |
| 419 | os.rm(f) or { failed << os.base(f) } |
| 420 | } |
| 421 | if failed.len > 0 { |
| 422 | warn('could not remove ${failed.len} of ${dbs.len} unused sync database files: ${failed.join(', ')}') |
| 423 | } |
| 424 | } |
| 425 | if builds.dirs.len > 0 { |
| 426 | // removed as the user who built them, the way the builds clear their |
| 427 | // own dirs: root never deletes inside the user's tree |
| 428 | mut s := ui.spinner(text: 'removing pkgbuild builds') |
| 429 | s.start() |
| 430 | defer { stop_spinner(mut s) } |
| 431 | mut args := ['rm', '-rf', '--'] |
| 432 | args << builds.dirs |
| 433 | run_as_user(builds.user, args)! |
| 434 | stop_spinner(mut s) |
| 435 | } |
| 436 | } |
| 437 | |