module main import nspkg.tests.coracle as alpm import nspkg.engine import nspkg.tests.signaturefixtures import nspkg.tests.transactionfixtures import os #include "@DIR/reference_log.h" type DownloadSpaceReceiver = fn (voidptr, u32, &u8) fn C.v_alpm_reference_log_install(voidptr, DownloadSpaceReceiver, voidptr) voidptr fn C.v_alpm_reference_log_remove(voidptr, voidptr) struct DownloadSpaceTrace { base string mut: lines []string } fn download_space_line(context voidptr, level u32, message string) { mut trace := unsafe { &DownloadSpaceTrace(context) } if message.contains(trace.base) && (message.starts_with('loading fsinfo for ') || message.starts_with('partition ') || message.starts_with('Partition ')) { trace.lines << '${level}:' + message.replace(trace.base, '') } } fn reference_download_space_line(context voidptr, level u32, message &u8) { download_space_line(context, level, unsafe { tos_clone(message) }) } fn native_download_space_line(context voidptr, level engine.LogLevel, message string) { download_space_line(context, u32(level), message) } fn main() { run() or { eprintln(err) exit(1) } } fn reference_space(fixture transactionfixtures.Fixture, success bool) ![]string { handle := alpm.initialize(fixture.root, fixture.db)! defer { handle.release() or { eprintln(err) } } handle.option_add_cachedir(fixture.cache)! handle.option_set_checkspace(1)! db := handle.register_syncdb('core', 0)! db.add_server('file://' + fixture.server)! handle.trans_init(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet | .download_only)! defer { handle.trans_release() or { eprintln(err) } } handle.add_pkg(db.get_pkg('app') or { return error('missing C target') })! handle.trans_prepare()!.free() mut trace := DownloadSpaceTrace{ base: fixture.base } adapter := C.v_alpm_reference_log_install(handle, reference_download_space_line, &trace) defer { C.v_alpm_reference_log_remove(handle, adapter) } result := handle.trans_commit() or { assert !success assert handle.errno() == .disk_space return trace.lines } result.free() assert success return trace.lines } fn native_space(fixture transactionfixtures.Fixture, success bool) ![]string { mut handle := engine.initialize(fixture.root, fixture.db)! defer { handle.release() or { eprintln(err) } } handle.add_string_list(.cache_dirs, fixture.cache)! handle.set_integer_option(.check_space, 1)! mut db := handle.register_syncdb('core', 0)! db.add_server('file://' + fixture.server, false)! handle.start_transaction(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet | .download_only)! defer { handle.release_transaction() or { eprintln(err) } } handle.add_package(db.get_pkg('app') or { return error('missing native target') })! assert handle.prepare_transaction()!.success mut trace := DownloadSpaceTrace{ base: fixture.base } handle.set_log_callback(native_download_space_line, &trace)! handle.commit_transaction() or { assert !success assert handle.errno() == .disk_space assert handle.transaction_state()! == .downloading return trace.lines } assert success assert handle.transaction_state()! == .downloading return trace.lines } fn run() ! { if os.args.len != 3 || os.readlink('/proc/self/ns/mnt')! == os.args[2] { return error('requires a private mount namespace') } signaturefixtures.command('mount', ['--make-rprivate', '/'])! for linked in [false, true] { for success in [false, true] { mut traces := [][]string{} for native in [false, true] { base := os.join_path(os.args[1], linked.str(), success.str(), native.str()) fixture := transactionfixtures.create(base, 'fetch')! volume := if linked { os.join_path(base, 'cache-volume') } else { fixture.cache } if linked { os.mv(fixture.cache, volume)! os.symlink(volume, fixture.cache)! } size := if success { '32m' } else { '8k' } signaturefixtures.command('mount', ['-t', 'tmpfs', '-o', 'size=' + size, 'tmpfs', volume])! if native { traces << native_space(fixture, success)! } else { traces << reference_space(fixture, success)! } assert os.is_file(os.join_path(fixture.cache, 'app-2-1.pkg.tar')) == success assert os.read_file(os.join_path(fixture.root, 'old-payload'))! == 'old' assert !os.exists(os.join_path(fixture.root, 'usr')) signaturefixtures.command('umount', [volume])! } assert traces[0].len >= 2 assert traces[1] == traces[0], '${linked}/${success}: ${traces}' } } println('native/C transaction download space verified on constrained filesystems') }