// util.c unpacks before trans.c searches the staged archive scriptlet for a phase. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os fn C.archive_entry_set_pathname(&C.Archive_entry, &char) fn C.archive_entry_set_perm(&C.Archive_entry, u32) fn C.archive_read_extract(&C.Archive, &C.Archive_entry, int) int fn (mut handle Handle) stage_archive_scriptlet(pkg &Package, directory i32) !bool { mut reader := open_handle_archive(pkg.filename, handle, .pkg_open) or { if err is ArchiveOpenError { return handle.fail(err.kind, err.msg()) } return err } defer { reader.close() or { handle.diagnostics << err.msg() } } for { record := reader.next_record()! if record.eof { return false } native := record.name == '.NSPKG-HOOK' if !native && record.name != '.INSTALL' { reader.skip_data()! continue } if record.mode & 0o170000 != 0o100000 || C.archive_entry_hardlink(record.entry) != unsafe { nil } { return error('archive scriptlet is not a regular file') } handle.log(.debug, 'extracting: ${record.name}\n') // the pathname is ours; archive links and intermediate directory creation are forbidden. path := '/proc/self/fd/${directory}/.INSTALL' C.archive_entry_set_pathname(record.entry, unsafe { &char(path.str) }) C.archive_entry_set_perm(record.entry, if native { u32(0o700) } else { u32(0o644) }) status := C.archive_read_extract(reader.handle, record.entry, C.ARCHIVE_EXTRACT_NO_AUTODIR | C.ARCHIVE_EXTRACT_NO_OVERWRITE | C.ARCHIVE_EXTRACT_SECURE_NODOTDOT | C.ARCHIVE_EXTRACT_PERM) if status == C.ARCHIVE_WARN { handle.log(.warning, localized_message('warning given when extracting %s (%s)\n', '.INSTALL', reader.archive_detail().replace(path, '.INSTALL'))) } else if status != C.ARCHIVE_OK { detail := reader.archive_detail().replace(path, '.INSTALL') handle.log(.error, localized_message('could not extract %s (%s)\n', '.INSTALL', detail)) return error(detail) } return native } return false } fn (mut handle Handle) staged_scriptlet_contents(directory i32) ![]u8 { fd := C.openat(directory, c'.INSTALL', C.O_RDONLY | C.O_NOFOLLOW | C.O_NONBLOCK | C.O_CLOEXEC) if fd < 0 { return os.last_error() } defer { handle.close_process_descriptor(fd) } mut size := i64(0) mut mode := u32(0) mut links := u64(0) if C.v_alpm_download_stat(fd, &size, &mode, &links) != 0 { return os.last_error() } if mode & 0o170000 != 0o100000 || links != 1 { return error('staged scriptlet is not a private regular file') } return read_scriptlet_descriptor(fd) }