// download block accounting and free-space cushion follow diskspace.c. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os #include "@DIR/native_space.h" @[typedef] struct C.v_alpm_space_info { mut: block_size u64 blocks u64 available u64 read_only u32 } fn C.v_alpm_space_stat(&char, &C.v_alpm_space_info) i32 fn download_space_needed(sizes []i64, block_size u64, blocks u64) !u64 { if block_size == 0 { return error('filesystem block size is zero') } five_percent := blocks / 20 + 1 twenty_mib := u64(20 * 1024 * 1024) / block_size + 1 mut needed := if five_percent < twenty_mib { five_percent } else { twenty_mib } for size in sizes { if size < 0 { return error('negative download size') } // keep the source's extra block, including at block_size-1 boundaries. if block_size > (~u64(0) - 1) / 2 { return error('filesystem block size overflows accounting') } amount := u64(size) / block_size + (u64(size) % block_size + block_size + 1) / block_size if amount > ~u64(0) - needed { return error('download space accounting overflow') } needed += amount } return needed } fn (mut handle Handle) check_download_space(directory string, sizes []i64) ! { destination := os.real_path(directory) mut mounts := handle.load_space_mounts()! index := installation_mount_index(mounts, destination) or { handle.log(.error, localized_message('could not determine cachedir mount point %s\n', destination)) return handle.fail(.disk_space, 'cannot determine cachedir mount point ${destination}') } mut mount := unsafe { &mounts[index] } if !handle.load_installation_mount(mut mount) { return handle.fail(.disk_space, 'cannot get filesystem information for ${mount.path}') } needed := download_space_needed(sizes, mount.info.block_size, mount.info.blocks) or { return handle.fail(.disk_space, err.msg()) } cushion := download_space_needed([], mount.info.block_size, mount.info.blocks) or { return handle.fail(.disk_space, err.msg()) } if needed - cushion > u64(max_i64) { return handle.fail(.disk_space, 'download space accounting overflow') } mount.maximum_needed = i64(needed - cushion) if handle.space_mount_full(mount)! { return handle.fail(.disk_space, '${mount.path}: ${needed} blocks needed, ${mount.info.available} available') } }