// metadata order and warning selection follow libarchive's disk writer as used by add.c. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os fn C.archive_entry_clone(&C.Archive_entry) &C.Archive_entry fn C.archive_entry_free(&C.Archive_entry) fn C.fchownat(i32, &char, u32, u32, i32) i32 struct ExtractionWarning { detail string ownership bool uid i64 gid i64 } fn (warning ExtractionWarning) report(handle &Handle, filename string) { if warning.detail == '' && !warning.ownership { return } detail := if warning.ownership { "Can't set user=${warning.uid}/group=${warning.gid} for ${os.norm_path(filename)}" } else { warning.detail } handle.log(.warning, localized_message('warning given when extracting %s (%s)\n', filename, detail)) } fn (mut handle Handle) extraction_metadata_problem(action string, problem IError) ! { if problem.code() == int(C.ENOSPC) { return handle.fail(.libarchive, '${action}: ${problem.msg()}') } handle.diagnostics << '${action}: ${problem.msg()}' } fn (mut handle Handle) extract_record_owner(record ArchiveRecord, fd i32, anchor string) !bool { uid := u32(C.archive_entry_uid(record.entry)) gid := u32(C.archive_entry_gid(record.entry)) if C.v_alpm_extract_chown(fd, unsafe { &char(anchor.str) }, uid, gid) == 0 { return true } mut problem := os.last_error() if problem.code() == int(C.ENOSPC) { handle.extraction_metadata_problem('set archive ownership on ${record.name}', problem)! } $if linux { if fd >= 0 { // retry the alternate syscall on the held inode, even if its pathname was replaced. if C.fchownat(fd, c'', uid, gid, C.AT_EMPTY_PATH | C.AT_SYMLINK_NOFOLLOW) == 0 { return true } problem = os.last_error() } } handle.extraction_metadata_problem('set archive ownership on ${record.name}', problem)! return false } fn (mut handle Handle) extract_record_metadata(record ArchiveRecord, path &RootPath, fd i32, mode u32, started_at i64) !ExtractionWarning { entry := record.entry anchor := path.descriptor_path() mut warning := ExtractionWarning{} mut permissions := mode & 0o7777 if !handle.extract_record_owner(record, fd, anchor)! { warning = ExtractionWarning{ ownership: true, uid: C.archive_entry_uid(entry), gid: C.archive_entry_gid(entry) } mut uid := u32(0) mut gid := u32(0) if C.v_alpm_extract_owner(fd, unsafe { &char(anchor.str) }, &uid, &gid) != 0 { permissions &= ~u32(0o6000) if mode & 0o2000 != 0 { warning = ExtractionWarning{ detail: "Couldn't stat file" } } } else if mode & 0o2000 != 0 { if gid != u32(C.archive_entry_gid(entry)) { permissions &= ~u32(0o2000) warning = ExtractionWarning{ detail: "Can't restore SGID bit" } } if mode & 0o4000 != 0 && uid != u32(C.archive_entry_uid(entry)) { permissions &= ~u32(0o4000) warning = ExtractionWarning{ detail: "Can't restore SUID bit" } } } else if mode & 0o4000 != 0 && C.geteuid() != u32(C.archive_entry_uid(entry)) { permissions &= ~u32(0o4000) warning = ExtractionWarning{ detail: "Can't make file SUID" } } } if C.geteuid() != 0 { attributes := handle.extract_record_xattrs(record, fd, anchor)! if attributes.detail != '' { warning = attributes } } if mode & 0o170000 != 0o120000 && C.v_alpm_extract_chmod(fd, unsafe { &char(anchor.str) }, permissions) != 0 { handle.extraction_metadata_problem('set archive permissions on ${record.name}', os.last_error())! // directory mode and time fixup errors occur during writer cleanup in the source. if mode & 0o170000 != 0o040000 { warning = ExtractionWarning{ detail: "Can't set permissions to 0${permissions:o}" } } } if C.geteuid() == 0 { attributes := handle.extract_record_xattrs(record, fd, anchor)! if attributes.detail != '' { warning = attributes } } if C.v_alpm_extract_times(fd, unsafe { &char(anchor.str) }, C.archive_entry_atime_is_set(entry) != 0, C.archive_entry_atime(entry), C.archive_entry_atime_nsec(entry), C.archive_entry_mtime_is_set(entry) != 0, C.archive_entry_mtime(entry), C.archive_entry_mtime_nsec(entry), started_at) != 0 { handle.extraction_metadata_problem('set archive times on ${record.name}', os.last_error())! if mode & 0o170000 != 0o040000 { warning = ExtractionWarning{ detail: "Can't restore time" } } } return warning } fn (mut handle Handle) extract_record_xattrs(record ArchiveRecord, fd i32, anchor string) !ExtractionWarning { if C.archive_entry_xattr_reset(record.entry) == 0 { return ExtractionWarning{} } // the disk writer walks a cloned entry, whose attribute order can differ from the reader's. entry := C.archive_entry_clone(record.entry) if entry == unsafe { nil } { return handle.fail(.memory, 'clone archive metadata') } defer { C.archive_entry_free(entry) } mut names := []string{} mut failed := false mut specific := false count := C.archive_entry_xattr_reset(entry) for _ in 0 .. count { mut name := &char(unsafe { nil }) mut value := voidptr(unsafe { nil }) mut size := uint(0) if C.archive_entry_xattr_next(entry, &name, &value, &size) != C.ARCHIVE_OK { return handle.fail(.libarchive, 'read archive extended attributes') } if name == unsafe { nil } { continue } attribute := unsafe { cstring_to_vstring(name) } if attribute in ['system.posix_acl_access', 'system.posix_acl_default', 'trusted.SGI_ACL_DEFAULT', 'trusted.SGI_ACL_FILE'] { continue } if attribute.starts_with('xfsroot.') { // the obsolete namespace contributes to a later error but does not trigger one alone. names << attribute specific = true continue } if C.v_alpm_extract_xattr(fd, unsafe { &char(anchor.str) }, name, value, size) != 0 { problem := os.last_error() handle.extraction_metadata_problem('set archive extended attribute on ${record.name}', problem)! names << attribute failed = true if problem.code() !in [int(C.ENOTSUP), int(C.ENOSYS)] { specific = true } } } if !failed { return ExtractionWarning{} } return ExtractionWarning{ detail: if specific { 'Cannot restore extended attributes: ' + names.join(' ') } else { 'Cannot restore extended attributes on this file system' } } }