module engine import os #include "@VMODROOT/tests/local_storage_probe.h" fn C.v_alpm_limit_database_write() i32 fn test_local_writer_and_reason_changes_round_trip() ! { dir := os.join_path(os.vtmp_dir(), 'native-local-writer-${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln(err) } } mut handle := initialize('/', dir)! defer { handle.release() or { eprintln(err) } } mut db := handle.local_database()! mut pkg := parse_pkginfo('pkgname = app\npkgver = 2:1-2\npkgdesc = \nurl = \narch = any\ndepend = base>=2\noptdepend = tool: helps\nxdata = pkgtype=pkg\nbackup = etc/app.conf\n')! pkg.files = [PackageFile{ name: 'etc/' }, PackageFile{ name: 'etc/app.conf' }] pkg.reason = .depend pkg.validation = 13 pkg.build_date = -1 pkg.install_date = 1700000000 old_mask := C.umask(0o777) defer { C.umask(old_mask) } db.prepare_local_package(&pkg)! db.write_local_package(mut pkg, true, true)! entry := os.join_path(db.path, 'app-2:1-2') assert os.stat(entry)!.mode & 0o777 == 0o755 assert os.stat(os.join_path(entry, 'desc'))!.mode & 0o777 == 0o644 assert os.stat(os.join_path(entry, 'files'))!.mode & 0o777 == 0o644 assert os.read_file(os.join_path(entry, 'files'))! == '%FILES%\netc/\netc/app.conf\n\n%BACKUP%\netc/app.conf\t(null)\n\n' description := os.read_file(os.join_path(entry, 'desc'))! assert description.contains('%DESC%\n\n\n%URL%\n\n\n') assert description.contains('%BUILDDATE%\n-1\n\n') assert description.contains('%DEPENDS%\nbase>=2\n\n%OPTDEPENDS%\ntool: helps\n\n') mut installed := db.get_pkg('app') or { return error('missing installed package') } installed.set_reason(.explicit)! assert installed.reason == .explicit assert !os.read_file(os.join_path(entry, 'desc'))!.contains('%REASON%') assert os.read_file(os.join_path(entry, 'files'))! == pkg.local_files() os.chmod(os.join_path(entry, 'desc'), 0o600)! installed.set_reason(.unknown)! assert os.stat(os.join_path(entry, 'desc'))!.mode & 0o777 == 0o600 assert os.read_file(os.join_path(entry, 'desc'))!.contains('%REASON%\n2\n\n') assert C.umask(0o777) == 0o777 os.write_file(os.join_path(entry, 'install'), 'script')! os.write_file(os.join_path(entry, 'changelog'), 'changes')! db.remove_local_package(installed)! assert !os.exists(entry) db.invalidate()! assert db.get_pkg('app') == none } fn test_local_reason_failure_preserves_cache_and_rejects_unsafe_records() ! { dir := os.join_path(os.vtmp_dir(), 'native-local-writer-failure-${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln(err) } } mut handle := initialize('/', dir)! defer { handle.release() or { eprintln(err) } } mut db := handle.local_database()! mut pkg := parse_pkginfo('pkgname = app\npkgver = 1-1\n')! db.prepare_local_package(&pkg)! db.write_local_package(mut pkg, true, true)! entry := os.join_path(db.path, 'app-1-1') path := os.join_path(entry, 'desc') mut installed := db.get_pkg('app') or { return error('missing package') } installed.load_description()! victim := os.join_path(dir, 'victim') os.write_file(victim, 'untouched')! for kind in ['symlink', 'hardlink', 'directory'] { os.rm(path)! match kind { 'symlink' { os.symlink(victim, path)! } 'hardlink' { assert C.link(unsafe { &char(victim.str) }, unsafe { &char(path.str) }) == 0 } else { os.mkdir(path)! } } if _ := installed.set_reason(.depend) { assert false, kind } assert handle.errno() == .db_write assert installed.reason == .explicit assert os.read_file(victim)! == 'untouched' if kind == 'directory' { os.rmdir(path)! } else { os.rm(path)! } os.write_file(path, pkg.local_description())! } // a loaded no-op never opens or recreates the record. os.rm(path)! installed.set_reason(.explicit)! assert !os.exists(path) os.write_file(path, pkg.local_description())! if C.geteuid() != 0 { os.chmod(path, 0o400)! if _ := installed.set_reason(.depend) { assert false } assert handle.errno() == .db_write assert installed.reason == .explicit os.chmod(path, 0o644)! os.chmod(entry, 0o555)! if _ := installed.set_reason(.depend) { assert false } assert handle.errno() == .db_write assert installed.reason == .explicit os.chmod(entry, 0o755)! assert os.read_file(path)! == pkg.local_description() assert os.ls(entry)!.all(!it.starts_with('.alpm-')) } mut unowned := Package{ origin: .local_db } if _ := unowned.set_reason(.depend) { assert false } mut archive := handle.load_package('missing', false, 0) or { &Package{ owner: handle, origin: .file } } if _ := archive.set_reason(.depend) { assert false } assert handle.errno() == .wrong_args os.mv(entry, entry + '.saved')! os.symlink(entry + '.saved', entry)! if _ := installed.set_reason(.depend) { assert false } assert handle.errno() == .db_write assert os.read_file(os.join_path(entry + '.saved', 'desc'))! == pkg.local_description() os.rm(entry)! os.mv(entry + '.saved', entry)! local_path := db.path.trim_right('/') os.mv(local_path, local_path + '.saved')! os.symlink(local_path + '.saved', local_path)! if _ := installed.set_reason(.depend) { assert false } assert handle.errno() == .db_write assert os.read_file(os.join_path(local_path + '.saved', 'app-1-1', 'desc'))! == pkg.local_description() os.rm(local_path)! os.mv(local_path + '.saved', local_path)! os.mkdir(os.join_path(entry, 'nested'))! if _ := db.remove_local_package(installed) { assert false } assert handle.errno() == .db_remove assert os.is_dir(os.join_path(entry, 'nested')) } fn test_local_writer_cannot_escape_package_identity() ! { dir := os.join_path(os.vtmp_dir(), 'native-local-writer-names-${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln(err) } } mut handle := initialize('/', dir)! defer { handle.release() or { eprintln(err) } } mut db := handle.local_database()! for name in ['../escape', '.', '', 'bad\x00name', 'a'.repeat(256)] { pkg := Package{ name: name, version: '1-1' } if _ := db.prepare_local_package(&pkg) { assert false } assert handle.errno() == .db_write } assert os.ls(db.path)! == ['ALPM_DB_VERSION'] } fn test_partial_database_write_failure_never_replaces_the_old_record() ! { dir := os.join_path(os.vtmp_dir(), 'native-local-writer-limit-${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln(err) } } mut handle := initialize('/', dir)! defer { handle.release() or { eprintln(err) } } mut db := handle.local_database()! mut pkg := parse_pkginfo('pkgname = app\npkgver = 1-1\npkgdesc = ${'x'.repeat(512)}\n')! db.prepare_local_package(&pkg)! db.write_local_package(mut pkg, true, true)! mut installed := db.get_pkg('app') or { return error('missing package') } installed.load_description()! entry := os.join_path(db.path, 'app-1-1') before := os.read_file(os.join_path(entry, 'desc'))! child := C.fork() assert child >= 0 if child == 0 { if C.v_alpm_limit_database_write() != 0 { C._exit(2) } if _ := installed.set_reason(.depend) { C._exit(3) } if handle.errno() != .db_write || installed.reason != .explicit { C._exit(4) } C._exit(0) } mut status := i32(0) for { if C.v_alpm_wait_child(child, &status, 0) >= 0 { break } if C.errno != C.EINTR { return os.last_error() } } assert C.v_alpm_exit_status(status) == 0 assert os.read_file(os.join_path(entry, 'desc'))! == before assert os.ls(entry)!.all(!it.starts_with('.alpm-')) } fn test_local_writer_matches_reference_records_when_supplied() ! { // the root parity driver supplies C-written records; the native suite needs no libalpm. reference := os.getenv('ALPM_LOCAL_WRITE_REFERENCE') if reference == '' { return } dir := os.join_path(os.vtmp_dir(), 'native-local-c-records-${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln(err) } } mut handle := initialize('/', dir)! defer { handle.release() or { eprintln(err) } } mut db := handle.local_database()! mut count := 0 for entry in os.ls(reference)! { if entry == 'ALPM_DB_VERSION' { continue } name, version := split_package_entry(entry)! mut reference_pkg := Package{ name: name, version: version, origin: .local_db, desc_loaded: true, files_loaded: true } description := os.read_file(os.join_path(reference, entry, 'desc'))! files := os.read_file(os.join_path(reference, entry, 'files'))! reference_pkg.parse_db_metadata(description)! path := os.join_path(os.getenv('ALPM_LOCAL_WRITE_ARCHIVES'), '${name}.pkg.tar') mut pkg := handle.load_package(path, true, 0)! pkg.install_date = reference_pkg.install_date pkg.reason = reference_pkg.reason db.prepare_local_package(pkg)! db.write_local_package(mut pkg, true, true)! assert os.read_file(os.join_path(db.path, entry, 'desc'))! == description, entry assert os.read_file(os.join_path(db.path, entry, 'files'))! == files, entry count++ } assert count == 8 println('native local writer matched 8 C records') }