module engine import nspkg.tests.archivefixtures import nspkg.tests.signaturefixtures import os import strconv #include "@VMODROOT/tests/extraction_probe.h" #include "@VMODROOT/tests/local_storage_probe.h" fn C.v_alpm_extraction_probe(&char, &i64) i32 fn C.v_alpm_limit_database_write() i32 fn C.archive_entry_clone(&C.Archive_entry) &C.Archive_entry fn C.archive_entry_free(&C.Archive_entry) fn C.archive_entry_set_pathname(&C.Archive_entry, &char) fn C.archive_entry_set_hardlink(&C.Archive_entry, &char) fn C.archive_write_disk_new() &C.Archive fn C.archive_write_disk_set_options(&C.Archive, int) int fn C.archive_write_free(&C.Archive) int fn C.archive_read_extract2(&C.Archive, &C.Archive_entry, &C.Archive) int fn C.archive_errno(&C.Archive) int fn C.getxattr(&char, &char, voidptr, uint) i64 fn C.setxattr(&char, &char, voidptr, uint, int) int fn extraction_fixture_time(path string) { assert C.v_alpm_extract_times(-1, unsafe { &char(path.str) }, true, 12345, 0, true, 12345, 0, 12345) == 0 } fn extraction_pax_field(name string, value string) string { payload := '${name}=${value}\n' mut length := payload.len + 2 for { actual := payload.len + length.str().len + 1 if actual == length { return '${length} ${payload}' } length = actual } } fn extraction_cpio_entry(name string, text string, inode u64, links u64) []u8 { mut header := '070701' for field in [inode, u64(0o100644), u64(C.geteuid()), u64(C.getegid()), links, u64(12345), u64(text.len), u64(0), u64(0), u64(0), u64(0), u64(name.len + 1), u64(0)] { digits := strconv.format_uint(field, 16) header += '0'.repeat(8 - digits.len) + digits } mut bytes := (header + name + '\x00').bytes() bytes << []u8{len: (4 - bytes.len % 4) % 4} bytes << text.bytes() bytes << []u8{len: (4 - text.len % 4) % 4} return bytes } fn extraction_reference(path string, archive string) ! { mut reader := open_archive(archive)! defer { reader.close() or { eprintln(err) } } for { record := reader.next_record()! if record.eof { break } destination := os.join_path(path, record.name) // llstat in util-common.c strips trailing slashes before lstat. if info := os.lstat(destination.trim_right('/')) { if info.mode & 0o170000 == 0o040000 { if record.mode & 0o170000 != 0o040000 { return error('cannot overwrite directory with file') } reader.skip_data()! continue } } entry := C.archive_entry_clone(record.entry) assert entry != unsafe { nil } C.archive_entry_set_pathname(entry, unsafe { &char(destination.str) }) link := C.archive_entry_hardlink(entry) if link != unsafe { nil } { source := os.join_path(path, unsafe { cstring_to_vstring(link) }) C.archive_entry_set_hardlink(entry, unsafe { &char(source.str) }) } writer := C.archive_write_disk_new() assert writer != unsafe { nil } assert C.archive_write_disk_set_options(writer, C.ARCHIVE_EXTRACT_OWNER | C.ARCHIVE_EXTRACT_PERM | C.ARCHIVE_EXTRACT_TIME | C.ARCHIVE_EXTRACT_UNLINK | C.ARCHIVE_EXTRACT_XATTR | C.ARCHIVE_EXTRACT_SECURE_SYMLINKS) == C.ARCHIVE_OK status := C.archive_read_extract2(reader.handle, entry, writer) failed := status != C.ARCHIVE_OK && (status != C.ARCHIVE_WARN || C.archive_errno(reader.handle) == C.ENOSPC) problem := reader.archive_error('reference extraction') assert C.archive_write_free(writer) == C.ARCHIVE_OK C.archive_entry_free(entry) if failed { return problem } } } fn extraction_snapshot(base string, relative string) ![]string { mut names := os.ls(os.join_path(base, relative))! names.sort() mut out := []string{} for name in names { path := if relative == '' { name } else { relative + '/' + name } full := os.join_path(base, path) info := os.lstat(full)! out << '${path}:${info.mode}:${info.uid}:${info.gid}' match info.mode & 0o170000 { 0o040000 { out << extraction_snapshot(base, path)! } 0o100000 { digest := compute_sha256sum(full) or { return error('cannot hash ${full}') } out << '${info.size}:${info.mtime}:${info.nlink}:${digest}' } 0o120000 { out << '${info.mtime}:${os.readlink(full)!}' } else { out << '${info.mtime}:${info.rdev}' } } } return out } fn extraction_native(mut handle Handle, archive string) ! { mut root := handle.open_root_filesystem()! defer { root.close() } mut reader := open_archive(archive)! defer { reader.close() or { eprintln(err) } } for { record := reader.next_record()! if record.eof { break } handle.extract_payload_record(mut root, mut reader, record, record.name)! } } fn test_native_extraction_matches_source_flags_for_files_links_modes_and_times() ! { base := os.join_path(os.vtmp_dir(), 'native-extraction-parity-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } for side in ['native', 'reference'] { os.mkdir_all(os.join_path(base, side, 'existing'))! os.chmod(os.join_path(base, side, 'existing'), 0o700)! os.write_file(os.join_path(base, side, 'replace'), 'old')! os.symlink('/outside-target', os.join_path(base, side, 'leaf-link'))! os.write_file(os.join_path(base, side, 'file-to-dir'), 'old')! os.symlink('existing', os.join_path(base, side, 'link-to-dir'))! } os.mkdir_all(os.join_path(base, 'db'))! uid := u32(C.geteuid()) gid := u32(C.getegid()) entries := [ archivefixtures.Entry{ name: 'usr/', directory: true, mode: 0o755, uid: uid, gid: gid }, archivefixtures.Entry{ name: 'usr/bin/', directory: true, mode: 0o750, uid: uid, gid: gid }, archivefixtures.Entry{ name: 'usr/bin/app', text: 'payload', mode: 0o4755, uid: uid, gid: gid, mtime: 123456 }, archivefixtures.Entry{ name: 'usr/bin/hard', hardlink: 'usr/bin/app' }, archivefixtures.Entry{ name: 'usr/bin/link', symlink: 'app', uid: uid, gid: gid, mtime: 234567 }, archivefixtures.Entry{ name: 'absolute-link', symlink: '/usr/bin/app', uid: uid, gid: gid }, archivefixtures.Entry{ name: 'existing/', directory: true, mode: 0o755 }, archivefixtures.Entry{ name: 'file-to-dir/', directory: true, uid: uid, gid: gid }, archivefixtures.Entry{ name: 'file-to-dir/child', text: 'new', uid: uid, gid: gid }, archivefixtures.Entry{ name: 'link-to-dir/', directory: true, uid: uid, gid: gid }, archivefixtures.Entry{ name: 'implicit/parents/file', text: 'implicit', uid: uid, gid: gid }, archivefixtures.Entry{ name: 'replace', text: 'new', uid: uid, gid: gid }, archivefixtures.Entry{ name: 'bad-uid', text: 'owner mismatch', mode: 0o6755, uid: 60001, gid: gid }, archivefixtures.Entry{ name: 'bad-gid', text: 'group mismatch', mode: 0o6755, uid: uid, gid: 60001 }, archivefixtures.Entry{ name: 'leaf-link', text: 'regular', uid: uid, gid: gid }, archivefixtures.Entry{ name: 'fifo', typeflag: `6`, mode: 0o640, uid: uid, gid: gid, mtime: 345678 }, ] archive := os.join_path(base, 'payload.tar') archivefixtures.write(archive, entries, false)! mut handle := initialize(os.join_path(base, 'native'), os.join_path(base, 'db'))! defer { handle.release() or { eprintln(err) } } cwd := os.getwd() mask := C.umask(0o077) defer { C.umask(mask) } extraction_reference(os.join_path(base, 'reference'), archive)! extraction_native(mut handle, archive)! assert os.getwd() == cwd assert C.umask(0o077) == 0o077 native_snapshot := extraction_snapshot(os.join_path(base, 'native'), '')! reference_snapshot := extraction_snapshot(os.join_path(base, 'reference'), '')! assert native_snapshot == reference_snapshot, 'native: ${native_snapshot}\nreference: ${reference_snapshot}' first := os.stat(os.join_path(base, 'native', 'usr/bin/app'))! second := os.stat(os.join_path(base, 'native', 'usr/bin/hard'))! assert first.inode == second.inode assert os.stat(os.join_path(base, 'native', 'existing'))!.mode & 0o777 == 0o700 } fn test_extraction_stays_rooted_and_replaces_shared_or_linked_leaves() ! { base := os.join_path(os.vtmp_dir(), 'native-extraction-root-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } for path in ['root/a', 'root/b', 'db', 'outside'] { os.mkdir_all(os.join_path(base, path))! } outside := os.join_path(base, 'outside', 'file') os.write_file(outside, 'outside')! os.link(outside, os.join_path(base, 'root', 'shared'))! os.symlink('/a', os.join_path(base, 'root', 'absolute'))! os.symlink('a', os.join_path(base, 'root', 'relative'))! os.symlink(os.join_path(base, 'outside'), os.join_path(base, 'root', 'escape'))! archive := os.join_path(base, 'payload.tar') archivefixtures.write(archive, [ archivefixtures.Entry{ name: 'absolute/file', text: 'inside' }, archivefixtures.Entry{ name: 'shared', text: 'new inode' }, archivefixtures.Entry{ name: 'escape/file', text: 'rooted' }, ], false)! mut handle := initialize(os.join_path(base, 'root'), os.join_path(base, 'db'))! defer { handle.release() or { eprintln(err) } } extraction_native(mut handle, archive)! assert os.read_file(outside)! == 'outside' assert os.read_file(os.join_path(base, 'root', 'absolute', 'file'))! == 'inside' assert !os.is_link(os.join_path(base, 'root', 'absolute')) assert !os.exists(os.join_path(base, 'root', 'a', 'file')) assert os.read_file(os.join_path(base, 'root', 'shared'))! == 'new inode' mut root := handle.open_root_filesystem()! defer { root.close() } assert root.md5sum('escape/file')! != root.md5sum('absolute/file')! archivefixtures.write(archive, [archivefixtures.Entry{ name: 'relative/pinned', text: 'pinned' }], false)! mut reader := open_archive(archive)! defer { reader.close() or { eprintln(err) } } record := reader.next_record()! mut destination := root.resolve('relative/pinned', false)! defer { destination.close() } os.rm(os.join_path(base, 'root', 'relative'))! os.symlink('b', os.join_path(base, 'root', 'relative'))! handle.extract_archive_record(mut root, mut reader, record, &destination, record.mode)! assert os.read_file(os.join_path(base, 'root', 'a', 'pinned'))! == 'pinned' assert !os.exists(os.join_path(base, 'root', 'b', 'pinned')) } fn test_extraction_hardlink_source_symlink_policy_matches_reference() ! { base := os.join_path(os.vtmp_dir(), 'native-extraction-hardlinks-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } for scenario in ['parent', 'leaf', 'missing', 'self'] { for side in ['reference', 'native'] { os.mkdir_all(os.join_path(base, scenario, side, 'target'))! os.write_file(os.join_path(base, scenario, side, 'target/file'), 'original')! os.symlink('target', os.join_path(base, scenario, side, 'alias'))! os.symlink('target/file', os.join_path(base, scenario, side, 'leaf'))! for path in ['target/file', 'alias', 'leaf'] { extraction_fixture_time(os.join_path(base, scenario, side, path)) } } os.mkdir_all(os.join_path(base, scenario, 'db'))! archive := os.join_path(base, scenario, 'payload.tar') source := match scenario { 'parent' { 'alias/file' } 'leaf' { 'leaf' } 'self' { 'new' } else { 'missing' } } archivefixtures.write(archive, [archivefixtures.Entry{ name: 'new', hardlink: source }], false)! mut reference_ok := true extraction_reference(os.join_path(base, scenario, 'reference'), archive) or { reference_ok = false } mut handle := initialize(os.join_path(base, scenario, 'native'), os.join_path(base, scenario, 'db'))! mut native_ok := true extraction_native(mut handle, archive) or { native_ok = false } handle.release()! if scenario in ['parent', 'missing', 'self'] { // extract2 downgrades header failures to WARN, which add.c accepts. assert reference_ok assert !native_ok assert !os.exists(os.join_path(base, scenario, 'native', 'new')) assert os.read_file(os.join_path(base, scenario, 'native', 'target/file'))! == 'original' continue } assert native_ok == reference_ok, scenario native_snapshot := extraction_snapshot(os.join_path(base, scenario, 'native'), '')! reference_snapshot := extraction_snapshot(os.join_path(base, scenario, 'reference'), '')! assert native_snapshot == reference_snapshot, '${scenario}\nnative: ${native_snapshot}\nreference: ${reference_snapshot}' } } fn test_extraction_rejects_escaping_paths_and_database_links_before_mutation() ! { base := os.join_path(os.vtmp_dir(), 'native-extraction-invalid-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } os.mkdir_all(os.join_path(base, 'root'))! os.mkdir_all(os.join_path(base, 'db'))! mut handle := initialize(os.join_path(base, 'root'), os.join_path(base, 'db'))! defer { handle.release() or { eprintln(err) } } mut root := handle.open_root_filesystem()! defer { root.close() } pkg := parse_pkginfo('pkgname = app\npkgver = 1-1\n')! handle.local.prepare_local_package(&pkg)! archive := os.join_path(base, 'payload.tar') for name in ['../outside', '/outside', '.', 'a/../../outside'] { archivefixtures.write(archive, [archivefixtures.Entry{ name: name, text: 'bad' }], false)! if _ := extraction_native(mut handle, archive) { assert false, name } assert os.ls(os.join_path(base, 'root'))!.len == 0 } os.write_file(os.join_path(base, 'root', 'keep'), 'keep')! for target in ['../outside', '/outside'] { archivefixtures.write(archive, [archivefixtures.Entry{ name: 'keep', hardlink: target }], false)! if _ := extraction_native(mut handle, archive) { assert false, target } assert os.read_file(os.join_path(base, 'root', 'keep'))! == 'keep' } for entry in [archivefixtures.Entry{ name: '.INSTALL', symlink: '/outside' }, archivefixtures.Entry{ name: '.INSTALL', hardlink: 'keep' }, archivefixtures.Entry{ name: '.INSTALL', directory: true }] { archivefixtures.write(archive, [entry], false)! mut reader := open_archive(archive)! record := reader.next_record()! if _ := handle.extract_database_record(mut root, mut reader, record, &pkg) { assert false } assert handle.errno() == .pkg_invalid reader.close()! assert os.ls(os.join_path(handle.local.path, 'app-1-1'))!.len == 0 } } fn test_database_archive_members_are_binary_regular_records_with_fixed_permissions() ! { base := os.join_path(os.vtmp_dir(), 'native-extraction-database-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } os.mkdir_all(os.join_path(base, 'root'))! os.mkdir_all(os.join_path(base, 'db'))! mut handle := initialize(os.join_path(base, 'root'), os.join_path(base, 'db'))! defer { handle.release() or { eprintln(err) } } pkg := parse_pkginfo('pkgname = app\npkgver = 1-1\n')! handle.local.prepare_local_package(&pkg)! archive := os.join_path(base, 'payload.tar') archivefixtures.write(archive, [ archivefixtures.Entry{ name: '.INSTALL', text: 'script\x00bytes', mode: 0o777 }, archivefixtures.Entry{ name: '.CHANGELOG', text: 'changes', mode: 0o600 }, archivefixtures.Entry{ name: '.MTREE', text: 'binary\x00mtree', mode: 0o444 }, archivefixtures.Entry{ name: '.PKGINFO', text: 'ignored' }, archivefixtures.Entry{ name: '.future', text: 'ignored' }, ], false)! mut root := handle.open_root_filesystem()! defer { root.close() } mut reader := open_archive(archive)! defer { reader.close() or { eprintln(err) } } for { record := reader.next_record()! if record.eof { break } handle.extract_database_record(mut root, mut reader, record, &pkg)! } directory := os.join_path(handle.local.path, 'app-1-1') mut names := os.ls(directory)! names.sort() assert names == ['changelog', 'install', 'mtree'] assert os.read_bytes(os.join_path(directory, 'install'))! == 'script\x00bytes'.bytes() assert os.read_bytes(os.join_path(directory, 'mtree'))! == 'binary\x00mtree'.bytes() for name in names { assert os.stat(os.join_path(directory, name))!.mode & 0o7777 == 0o644 } } fn test_native_extraction_preserves_real_sparse_data_and_extended_attributes() ! { base := os.join_path(os.vtmp_dir(), 'native-extraction-sparse-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } for path in ['source', 'native', 'reference', 'db'] { os.mkdir_all(os.join_path(base, path))! } file := os.join_path(base, 'source', 'sparse') fd := C.open(unsafe { &char(file.str) }, C.O_WRONLY | C.O_CREAT | C.O_EXCL | C.O_CLOEXEC, 0o640) assert fd >= 0 assert C.v_alpm_extract_write(fd, c'head', 4, 0) == 4 assert C.v_alpm_extract_write(fd, c'tail', 4, 8 * 1024 * 1024) == 4 assert C.close(fd) == 0 assert C.setxattr(unsafe { &char(file.str) }, c'user.alpm-fixture', c'attribute', 9, 0) == 0 os.chmod(file, 0o440)! archive := os.join_path(base, 'sparse.tar') signaturefixtures.command('tar', ['--format=pax', '--sparse', '--xattrs', '-cf', archive, '-C', os.join_path(base, 'source'), 'sparse'])! mut handle := initialize(os.join_path(base, 'native'), os.join_path(base, 'db'))! defer { handle.release() or { eprintln(err) } } extraction_reference(os.join_path(base, 'reference'), archive)! extraction_native(mut handle, archive)! assert extraction_snapshot(os.join_path(base, 'native'), '')! == extraction_snapshot(os.join_path(base, 'reference'), '')! result := os.join_path(base, 'native', 'sparse') mut attribute := []u8{len: 32} count := C.getxattr(unsafe { &char(result.str) }, c'user.alpm-fixture', attribute.data, uint(attribute.len)) assert count == 9 assert attribute[..int(count)].bytestr() == 'attribute' // the archive remains small and the sparse reader supplies hole offsets. assert os.file_size(archive) < 65536 mut actual := []i64{len: 5} mut expected := []i64{len: 5} assert C.v_alpm_extraction_probe(unsafe { &char(result.str) }, actual.data) == 0 reference := os.join_path(base, 'reference', 'sparse') assert C.v_alpm_extraction_probe(unsafe { &char(reference.str) }, expected.data) == 0 assert actual[2..4] == expected[2..4] assert actual[4] * 512 < i64(os.file_size(result)) / 10 } fn test_extraction_source_policy_for_intermediate_directory_links() ! { base := os.join_path(os.vtmp_dir(), 'native-extraction-parent-links-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } for side in ['reference', 'native'] { os.mkdir_all(os.join_path(base, side, 'target'))! os.symlink('target', os.join_path(base, side, 'alias'))! os.write_file(os.join_path(base, side, 'target', 'keep'), 'keep')! extraction_fixture_time(os.join_path(base, side, 'target', 'keep')) } os.mkdir_all(os.join_path(base, 'db'))! archive := os.join_path(base, 'payload.tar') archivefixtures.write(archive, [archivefixtures.Entry{ name: 'alias/file', text: 'new' }], false)! extraction_reference(os.join_path(base, 'reference'), archive)! assert !os.is_link(os.join_path(base, 'reference', 'alias')) assert os.read_file(os.join_path(base, 'reference', 'alias', 'file'))! == 'new' assert !os.exists(os.join_path(base, 'reference', 'target', 'file')) mut handle := initialize(os.join_path(base, 'native'), os.join_path(base, 'db'))! defer { handle.release() or { eprintln(err) } } extraction_native(mut handle, archive)! assert extraction_snapshot(os.join_path(base, 'native'), '')! == extraction_snapshot(os.join_path(base, 'reference'), '')! } fn test_extraction_reports_truncated_data_and_write_failures() ! { base := os.join_path(os.vtmp_dir(), 'native-extraction-failure-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } for path in ['root', 'db'] { os.mkdir_all(os.join_path(base, path))! } mut handle := initialize(os.join_path(base, 'root'), os.join_path(base, 'db'))! defer { handle.release() or { eprintln(err) } } archive := os.join_path(base, 'payload.tar') bytes := archivefixtures.tar_bytes([archivefixtures.Entry{ name: 'file', text: 'data'.repeat(4096) }]) os.write_file_array(archive, bytes[..800])! mut rejected := false extraction_native(mut handle, archive) or { rejected = err.code() == int(Errno.libarchive) } assert rejected os.write_file_array(archive, bytes)! child := C.fork() assert child >= 0 if child == 0 { if C.v_alpm_limit_database_write() != 0 { C._exit(2) } extraction_native(mut handle, archive) or { if err.code() != int(Errno.libarchive) { C._exit(3) } C._exit(0) return } C._exit(4) } 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.file_size(os.join_path(base, 'root', 'file')) <= 128 } fn test_extraction_restores_fractional_times_and_skips_acl_xattrs() ! { base := os.join_path(os.vtmp_dir(), 'native-extraction-pax-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } for path in ['native', 'reference', 'db'] { os.mkdir_all(os.join_path(base, path))! } archive := os.join_path(base, 'payload.tar') fields := extraction_pax_field('atime', '12345.123456789') + extraction_pax_field('mtime', '23456.987654321') + extraction_pax_field('SCHILY.xattr.user.alpm-fixture', 'value') + extraction_pax_field('SCHILY.xattr.system.posix_acl_access', 'ignored') archivefixtures.write(archive, [ archivefixtures.Entry{ name: 'PaxHeaders/file', typeflag: `x`, text: fields }, archivefixtures.Entry{ name: 'file', text: 'contents', mode: 0o400, uid: u32(C.geteuid()), gid: u32(C.getegid()) }, ], false)! extraction_reference(os.join_path(base, 'reference'), archive)! mut handle := initialize(os.join_path(base, 'native'), os.join_path(base, 'db'))! defer { handle.release() or { eprintln(err) } } extraction_native(mut handle, archive)! mut actual := []i64{len: 5} mut expected := []i64{len: 5} file := os.join_path(base, 'native', 'file') reference := os.join_path(base, 'reference', 'file') assert C.v_alpm_extraction_probe(unsafe { &char(file.str) }, actual.data) == 0 assert C.v_alpm_extraction_probe(unsafe { &char(reference.str) }, expected.data) == 0 assert actual[..4] == expected[..4] assert actual[..4] == [i64(12345), 123456789, 23456, 987654321] mut attribute := []u8{len: 32} assert C.getxattr(unsafe { &char(file.str) }, c'user.alpm-fixture', attribute.data, uint(attribute.len)) == 5 assert attribute[..5].bytestr() == 'value' assert C.getxattr(unsafe { &char(file.str) }, c'system.posix_acl_access', attribute.data, uint(attribute.len)) < 0 assert handle.diagnostics.len == 0, handle.diagnostics.str() } fn test_extraction_device_nodes_match_reference_in_private_user_namespace() ! { if C.geteuid() != 0 { return } base := os.join_path(os.vtmp_dir(), 'native-extraction-nodes-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } for path in ['reference', 'native', 'db'] { os.mkdir_all(os.join_path(base, path))! } archive := os.join_path(base, 'payload.tar') archivefixtures.write(archive, [ archivefixtures.Entry{ name: 'char', typeflag: `3`, mode: 0o640, mtime: 12345 }, archivefixtures.Entry{ name: 'block', typeflag: `4`, mode: 0o600, mtime: 23456 }, ], false)! extraction_reference(os.join_path(base, 'reference'), archive)! mut handle := initialize(os.join_path(base, 'native'), os.join_path(base, 'db'))! defer { handle.release() or { eprintln(err) } } mut denied := false extraction_native(mut handle, archive) or { assert err.code() in [int(C.EPERM), int(C.EACCES)], err.msg() denied = true } assert extraction_snapshot(os.join_path(base, 'native'), '')! == extraction_snapshot(os.join_path(base, 'reference'), '')! if denied { // mapped root can still lack mknod under the host's capability/device policy. assert !os.exists(os.join_path(base, 'native', 'block')) return } assert os.lstat(os.join_path(base, 'native', 'char'))!.mode & 0o170000 == 0o020000 assert os.lstat(os.join_path(base, 'native', 'block'))!.mode & 0o170000 == 0o060000 } fn test_extraction_accepts_a_package_root_on_descriptor_zero() ! { base := os.join_path(os.vtmp_dir(), 'native-extraction-fd-zero-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } for path in ['root', 'db'] { os.mkdir_all(os.join_path(base, path))! } archive := os.join_path(base, 'payload.tar') archivefixtures.write(archive, [archivefixtures.Entry{ name: 'file', text: 'contents' }], false)! mut handle := initialize(os.join_path(base, 'root'), os.join_path(base, 'db'))! defer { handle.release() or { eprintln(err) } } child := C.fork() assert child >= 0 if child == 0 { if C.close(0) != 0 { C._exit(2) } mut root := handle.open_root_filesystem() or { C._exit(3) return } if root.fd != 0 { C._exit(4) } mut reader := open_archive(archive) or { C._exit(5) return } record := reader.next_record() or { C._exit(6) return } handle.extract_payload_record(mut root, mut reader, record, record.name) or { C._exit(7) } reader.close() or { C._exit(8) } root.close() 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(base, 'root', 'file'))! == 'contents' } fn test_extraction_data_bearing_cpio_hardlink_matches_reference_and_truncates() ! { base := os.join_path(os.vtmp_dir(), 'native-extraction-cpio-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } for path in ['native', 'reference', 'db'] { os.mkdir_all(os.join_path(base, path))! } mut data := extraction_cpio_entry('first', 'old-contents-that-must-disappear', 1, 2) data << extraction_cpio_entry('second', 'new', 1, 2) data << extraction_cpio_entry('TRAILER!!!', '', 0, 1) archive := os.join_path(base, 'payload.cpio') os.write_file_array(archive, data)! mut reader := open_archive(archive)! assert reader.next_record()!.name == 'first' reader.skip_data()! record := reader.next_record()! assert record.name == 'second' assert record.size == 3 assert C.archive_entry_hardlink(record.entry) != unsafe { nil } reader.close()! extraction_reference(os.join_path(base, 'reference'), archive)! mut handle := initialize(os.join_path(base, 'native'), os.join_path(base, 'db'))! defer { handle.release() or { eprintln(err) } } extraction_native(mut handle, archive)! assert extraction_snapshot(os.join_path(base, 'native'), '')! == extraction_snapshot(os.join_path(base, 'reference'), '')! assert os.read_file(os.join_path(base, 'native', 'first'))! == 'new' assert os.read_file(os.join_path(base, 'native', 'second'))! == 'new' assert os.stat(os.join_path(base, 'native', 'first'))!.inode == os.stat(os.join_path(base, 'native', 'second'))!.inode }