// cache lookup and partial size accounting follow pacman util.c and sync.c. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os pub struct CachedFile { pub: found bool path string size i64 } pub fn (mut handle Handle) find_cached_file(filename string) !CachedFile { handle.check()! return handle.cached_file(filename) } fn (mut handle Handle) cached_file(filename string) !CachedFile { for directory in handle.opts.lists[.cache_dirs] { path := directory + filename info := os.stat(path) or { if err.code() != int(C.ENOENT) { handle.diagnostics << 'could not stat ${path}: ${err.msg()}' handle.log(.warning, "could not open '${path}'\n: ${err.msg()}") } continue } if info.mode & 0o170000 == 0o100000 { handle.log(.debug, 'found cached pkg: ${path}\n') return CachedFile{ found: true, path: path, size: i64(info.size) } } handle.diagnostics << 'cached package is not a regular file: ${path}' handle.log(.warning, "cached pkg '${path}' is not a regular file: mode=${info.mode}\n") } return CachedFile{} } fn (mut handle Handle) compute_download_size(mut pkg Package) ! { if pkg.origin != .sync_db { pkg.download_bytes = 0 pkg.download_size_loaded = true return } if pkg.filename == '' && !pkg.has_filename { return handle.fail(.pkg_invalid_name, pkg.name) } cached := handle.cached_file(pkg.filename)! if cached.found { pkg.download_bytes = 0 } else { partial := handle.cached_file(pkg.filename + '.part')! if partial.found { // sync.c stats again after lookup; callbacks can change the partial file. pkg.download_bytes = 0 if info := os.stat(partial.path) { handle.log(.debug, 'using (package - .part) size\n') pkg.download_bytes = if pkg.size > i64(info.size) { pkg.size - i64(info.size) } else { 0 } } } else { pkg.download_bytes = pkg.size } } handle.log(.debug, 'setting download size ${pkg.download_bytes} for pkg ${pkg.name}\n') pkg.download_size_loaded = true } pub fn (mut handle Handle) package_download_size(mut pkg Package) !i64 { handle.require_live()! if !pkg.download_size_loaded { handle.compute_download_size(mut pkg)! } return pkg.download_bytes }