// mount ordering and peak per-package block accounting follow diskspace.c. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os struct InstallationMount { path string mut: loaded bool failed bool info C.v_alpm_space_info blocks_needed i64 maximum_needed i64 used_install bool used_remove bool } fn mount_path_unescape(path string) string { mut bytes := []u8{} mut index := 0 for index < path.len { if path[index] == `\\` && index + 3 < path.len && path[index + 1..index + 4].bytes().all(it >= `0` && it <= `7`) { value := (u32(path[index + 1] - `0`) << 6) | (u32(path[index + 2] - `0`) << 3) | u32(path[index + 3] - `0`) bytes << u8(value) index += 4 } else { bytes << path[index] index++ } } return bytes.bytestr() } fn installation_mounts(text string) []InstallationMount { mut mounts := []InstallationMount{} for line in text.split_into_lines() { if line.trim_left(' \t').starts_with('#') { continue } fields := line.fields() if fields.len >= 2 { path := mount_path_unescape(fields[1]) if path.starts_with('/') && !path.contains('\x00') { mounts << InstallationMount{ path: path } } } } mounts.sort(a.path > b.path) return mounts } fn installation_mount_index(mounts []InstallationMount, path string) ?int { for index, mount in mounts { if path.starts_with(mount.path) && (mount.path.ends_with('/') || path.len == mount.path.len || path[mount.path.len] == `/`) { return index } } return none } fn (mut handle Handle) load_space_mounts() ![]InstallationMount { text := os.read_file('/etc/mtab') or { problem := os.last_error().msg() handle.log(.error, localized_message('could not open file: %s: %s\n', '/etc/mtab', problem)) handle.log(.error, localized_message('could not determine filesystem mount points\n')) return handle.error() } mounts := installation_mounts(text) if mounts.len == 0 { handle.log(.error, localized_message('could not determine filesystem mount points\n')) return handle.error() } for mount in mounts { handle.log(.debug, 'discovered mountpoint: ${mount.path}\n') } return mounts } fn installation_blocks(size i64, block_size u64) !i64 { if size < 0 || block_size == 0 { return error('invalid filesystem size accounting') } amount := u64(size) / block_size + if u64(size) % block_size != 0 { u64(1) } else { u64(0) } if amount > u64(max_i64) { return error('filesystem size accounting overflow') } return i64(amount) } fn (mut handle Handle) load_installation_mount(mut mount InstallationMount) bool { if mount.loaded { return !mount.failed } mount.loaded = true if C.v_alpm_space_stat(unsafe { &char(mount.path.str) }, &mount.info) != 0 || mount.info.block_size == 0 { mount.failed = true problem := os.last_error().msg() handle.diagnostics << 'cannot get filesystem information for ${mount.path}: ${problem}' handle.log(.warning, localized_message('could not get filesystem information for %s: %s\n', mount.path, problem)) } else { handle.log(.debug, 'loading fsinfo for ${mount.path}\n') } return !mount.failed } fn (mut handle Handle) space_mount_full(mount InstallationMount) !bool { cushion := download_space_needed([], mount.info.block_size, mount.info.blocks) or { return handle.fail(.disk_space, err.msg()) } if cushion > u64(max_i64) || mount.maximum_needed > max_i64 - i64(cushion) { return handle.fail(.disk_space, 'filesystem space accounting overflow') } needed := mount.maximum_needed + i64(cushion) handle.log(.debug, 'partition ${mount.path}, needed ${mount.maximum_needed}, cushion ${cushion}, free ${mount.info.available}\n') if needed >= 0 && u64(needed) > mount.info.available { handle.log(.error, localized_message('Partition %s too full: %jd blocks needed, %ju blocks free\n', mount.path, needed, mount.info.available)) return true } return false } fn (mut handle Handle) account_installation_files(mut root RootFilesystem, mut mounts []InstallationMount, pkg &Package, removing bool) ! { for file in pkg.files { mut size := file.size if removing { status := root.stat(file.name, false) or { handle.diagnostics << 'cannot inspect disk usage for ${file.name}: ${err.msg()}' if match_stored_patterns(handle.opts.lists[.no_extract], file.name) != 0 { handle.log(.warning, localized_message('could not get file information for %s\n', file.name)) } continue } if !status.present { if match_stored_patterns(handle.opts.lists[.no_extract], file.name) != 0 { handle.log(.warning, localized_message('could not get file information for %s\n', file.name)) } continue } if status.info.mode & 0o170000 in [u32(0o040000), 0o120000] { continue } size = status.info.size } else if file.mode & 0o170000 in [u32(0o040000), 0o120000] { continue } // the source attributes dot members to the dbpath using this root-prefixed form. filename := if !removing && file.name.starts_with('.') { handle.dbpath } else { file.name } path := handle.root + filename index := installation_mount_index(mounts, path) or { handle.diagnostics << 'cannot determine mount point for ${filename}' handle.log(.warning, localized_message('could not determine mount point for file %s\n', filename)) continue } mut mount := unsafe { &mounts[index] } if !handle.load_installation_mount(mut mount) { continue } blocks := installation_blocks(size, mount.info.block_size) or { return handle.fail(.disk_space, err.msg()) } if removing { if mount.blocks_needed < (-max_i64 - 1) + blocks { return handle.fail(.disk_space, 'removal space accounting overflow') } mount.blocks_needed -= blocks mount.used_remove = true } else { if mount.blocks_needed > max_i64 - blocks { return handle.fail(.disk_space, 'installation space accounting overflow') } mount.blocks_needed += blocks mount.used_install = true } } } fn (mut handle Handle) check_install_space(mut root RootFilesystem, trans &Transaction) ! { mut mounts := handle.load_space_mounts()! if installation_mount_index(mounts, handle.root) == none { handle.log(.error, localized_message('could not determine root mount point %s\n', handle.root)) return handle.fail(.disk_space, 'cannot determine root mount point') } total := trans.add.len + trans.remove.len mut current := 0 for pkg in trans.remove { handle.commit_progress_for(.diskspace, '', current * 100 / total, total, current) handle.account_installation_files(mut root, mut mounts, pkg, true)! current++ } for pkg in trans.add { handle.commit_progress_for(.diskspace, '', current * 100 / total, total, current) if old := handle.local.by_name[pkg.name] { mut installed := unsafe { old } installed.load_files() or { return handle.fail(.db_invalid, err.msg()) } handle.account_installation_files(mut root, mut mounts, installed, true)! } handle.account_installation_files(mut root, mut mounts, pkg, false)! for mut mount in mounts { if mount.blocks_needed > mount.maximum_needed { mount.maximum_needed = mount.blocks_needed } } current++ } handle.commit_progress_for(.diskspace, '', 100, total, current) mut failures := []string{} for mount in mounts { if !mount.used_install && !mount.used_remove { continue } if mount.info.read_only != 0 { handle.log(.error, localized_message('Partition %s is mounted read only\n', mount.path)) failures << '${mount.path} is mounted read only' continue } if mount.used_install { cushion := download_space_needed([], mount.info.block_size, mount.info.blocks) or { return handle.fail(.disk_space, err.msg()) } needed := u64(mount.maximum_needed) + cushion if handle.space_mount_full(mount)! { failures << '${mount.path}: ${needed} blocks needed, ${mount.info.available} available' } } } if failures.len > 0 { return handle.fail(.disk_space, failures.join('; ')) } }