module main import nspkg.tests.coracle as alpm import nspkg.engine import nspkg.tests.installfixtures import nspkg.tests.localefixtures import nspkg.tests.signaturefixtures import os #include #include #include #include "@DIR/reference_log.h" fn C.mount(&char, &char, &char, u64, voidptr) i32 fn C.setlocale(i32, &char) &char fn C.bindtextdomain(const_domain &char, const_directory &char) &char type SpaceLogReceiver = fn (voidptr, u32, &u8) fn C.v_alpm_reference_log_install(voidptr, SpaceLogReceiver, voidptr) voidptr fn C.v_alpm_reference_log_remove(voidptr, voidptr) struct SpaceResult { mut: success bool kind int trace []string logs []string base string } fn space_log(result &SpaceResult, level u32, message string) { if !message.contains(result.base) { return } if !message.starts_with('loading fsinfo for ') && !message.starts_with('partition ') && !message.starts_with('Partition ') && level !in [u32(1), 2] { return } mut state := unsafe { result } state.logs << '${level}:' + message.replace(result.base, '') } fn source_space_log(context voidptr, level u32, message &u8) { space_log(unsafe { &SpaceResult(context) }, level, unsafe { tos_clone(message) }) } fn native_space_log(context voidptr, level engine.LogLevel, message string) { space_log(unsafe { &SpaceResult(context) }, u32(level), message) } fn space_catalog() { path := os.join_path(os.args[1], 'catalog') C.bindtextdomain(c'libalpm', unsafe { &char(path.str) }) } fn source_event(context voidptr, event &alpm.Event) { mut result := unsafe { &SpaceResult(context) } kind := unsafe { event.type_ } if kind in [.diskspace_start, .diskspace_done, .transaction_start] { result.trace << kind.str() } } fn native_event(context voidptr, event engine.CommitEvent) { mut result := unsafe { &SpaceResult(context) } if event.kind in [.diskspace_start, .diskspace_done, .transaction_start] { result.trace << event.kind.str() } } fn source_progress(context voidptr, kind alpm.Progress, _ &u8, percent i32, total uint, current uint) { if kind != .diskspace_start { return } mut result := unsafe { &SpaceResult(context) } result.trace << '${percent}:${total}:${current}' } fn native_progress(context voidptr, item engine.CommitProgress) { if item.kind != .diskspace { return } mut result := unsafe { &SpaceResult(context) } result.trace << '${item.percent}:${item.total}:${item.current}' } fn source_commit(fixture installfixtures.Fixture, skip bool) !SpaceResult { handle := alpm.initialize(fixture.root, fixture.db)! defer { handle.release() or { eprintln(err) } } mut result := &SpaceResult{ base: os.dir(fixture.root) } handle.option_add_cachedir(fixture.cache)! handle.option_set_checkspace(1)! if skip { handle.option_add_no_extract('usr/bin/app')! } handle.option_set_eventcb(source_event, result)! handle.option_set_progresscb(source_progress, result)! handle.trans_init(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet)! defer { handle.trans_release() or { eprintln(err) } } for path in fixture.archives { handle.add_pkg(handle.pkg_load(path, 1, 0)!)! } handle.trans_prepare()!.free() space_catalog() adapter := C.v_alpm_reference_log_install(handle, source_space_log, result) defer { C.v_alpm_reference_log_remove(handle, adapter) } completed := handle.trans_commit() or { result.kind = int(handle.errno()) return *result } completed.free() result.success = true return *result } fn native_commit(fixture installfixtures.Fixture, skip bool) !SpaceResult { mut handle := engine.initialize(fixture.root, fixture.db)! defer { handle.release() or { eprintln(err) } } mut result := &SpaceResult{ base: os.dir(fixture.root) } handle.add_string_list(.cache_dirs, fixture.cache)! handle.set_integer_option(.check_space, 1)! if skip { handle.add_string_list(.no_extract, 'usr/bin/app')! } handle.set_commit_callback(native_event, result)! handle.set_commit_progress_callback(native_progress, result)! handle.start_transaction(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet)! defer { handle.release_transaction() or { eprintln(err) } } for path in fixture.archives { handle.add_package(handle.load_package(path, true, 0)!)! } assert handle.prepare_transaction()!.success space_catalog() handle.set_log_callback(native_space_log, result)! handle.commit_transaction() or { result.kind = err.code() assert handle.transaction_state()! == .downloading return *result } result.success = true return *result } fn setup(base string, geometry string) !installfixtures.Fixture { peak := geometry in ['peak', 'reordered'] scenario := installfixtures.Scenario{ name: geometry installed: false missing_local: true incoming: if peak { '' } else { 'incoming' } payload: 'x'.repeat(if peak { 8192 } else { 12000 }) second: peak } mut fixture := installfixtures.create(base, scenario)! usr := geometry in ['small-usr', 'readonly-usr', 'noextract'] point := if usr { os.join_path(fixture.root, 'usr') } else { fixture.root } size := if geometry in ['small-root', 'small-usr', 'noextract'] { '8k' } else if peak { '16k' } else { '32m' } signaturefixtures.command('mount', ['-t', 'tmpfs', '-o', 'size=' + size, 'tmpfs', point])! if peak { os.mkdir_all(os.join_path(fixture.root, 'usr/bin'))! os.write_file(os.join_path(fixture.root, 'usr/bin/second'), 'o'.repeat(8192))! entry := os.join_path(fixture.db, 'local', 'second-1-1') os.mkdir(entry)! os.write_file(os.join_path(entry, 'desc'), '%REASON%\n1\n\n')! os.write_file(os.join_path(entry, 'files'), '%FILES%\nusr/\nusr/bin/\nusr/bin/second\n\n')! } if geometry == 'reordered' { fixture = installfixtures.Fixture{ ...fixture, archives: fixture.archives.reverse() } } if geometry.starts_with('readonly-') { // avoid replaying mtab's host UID/GID options in a mapped user namespace. if C.mount(unsafe { nil }, unsafe { &char(point.str) }, unsafe { nil }, u64(C.MS_REMOUNT | C.MS_RDONLY), unsafe { nil }) != 0 { return os.last_error() } } return fixture } 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', '/'])! assert os.setenv('SOURCE_DATE_EPOCH', '1700000000', true) == 0 localefixtures.write(os.join_path(os.args[1], 'catalog/zz/LC_MESSAGES/libalpm.mo'), { 'Partition %s too full: %jd blocks needed, %ju blocks free\n': 'zz: free %3$ju; needed %2$jd; partition %1$s\n' 'Partition %s is mounted read only\n': 'zz: readonly %s\n' })! assert os.setenv('LANGUAGE', 'zz', true) == 0 for translated in [false, true] { locale := if translated { 'en_US.UTF-8' } else { 'C' } available := C.setlocale(C.LC_ALL, unsafe { &char(locale.str) }) != unsafe { nil } for geometry in ['small-root', 'large-root', 'small-usr', 'readonly-root', 'readonly-usr', 'noextract', 'peak', 'reordered'] { mut results := []SpaceResult{} mut roots := []map[string]string{} mut databases := []map[string]string{} for native in [false, true] { fixture := setup(os.join_path(os.args[1], translated.str(), geometry, native.str()), geometry)! before_root := installfixtures.snapshot(fixture.root)! before_db := installfixtures.snapshot(fixture.db)! result := if native { native_commit(fixture, geometry == 'noextract')! } else { source_commit(fixture, geometry == 'noextract')! } assert result.success == (geometry in ['large-root', 'reordered']), '${geometry}:${native}:${result}' if !result.success { assert result.kind == int(engine.Errno.disk_space) assert installfixtures.snapshot(fixture.root)! == before_root assert installfixtures.snapshot(fixture.db)! == before_db assert 'transaction_start' !in result.trace } results << result roots << installfixtures.snapshot(fixture.root)! databases << installfixtures.snapshot(fixture.db)! } assert results[1].logs == results[0].logs, '${geometry}:${results}' if translated && available && !results[0].success { assert results[0].logs.any(it.starts_with('1:zz:')), results[0].logs.str() } assert results[1].success == results[0].success && results[1].kind == results[0].kind assert results[1].trace == results[0].trace, '${geometry}:${results}' assert roots[1] == roots[0], geometry assert databases[1] == databases[0], geometry } } println('16 native/libalpm installation space comparisons verified') } fn main() { run() or { eprintln(err) exit(1) } }