| 1 | // util.c unpacks before trans.c searches the staged archive scriptlet for a phase. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | fn C.archive_entry_set_pathname(&C.Archive_entry, &char) |
| 8 | fn C.archive_entry_set_perm(&C.Archive_entry, u32) |
| 9 | fn C.archive_read_extract(&C.Archive, &C.Archive_entry, int) int |
| 10 | |
| 11 | fn (mut handle Handle) stage_archive_scriptlet(pkg &Package, directory i32) !bool { |
| 12 | mut reader := open_handle_archive(pkg.filename, handle, .pkg_open) or { |
| 13 | if err is ArchiveOpenError { return handle.fail(err.kind, err.msg()) } |
| 14 | return err |
| 15 | } |
| 16 | defer { reader.close() or { handle.diagnostics << err.msg() } } |
| 17 | for { |
| 18 | record := reader.next_record()! |
| 19 | if record.eof { return false } |
| 20 | native := record.name == '.NSPKG-HOOK' |
| 21 | if !native && record.name != '.INSTALL' { |
| 22 | reader.skip_data()! |
| 23 | continue |
| 24 | } |
| 25 | if record.mode & 0o170000 != 0o100000 || C.archive_entry_hardlink(record.entry) != unsafe { nil } { |
| 26 | return error('archive scriptlet is not a regular file') |
| 27 | } |
| 28 | handle.log(.debug, 'extracting: ${record.name}\n') |
| 29 | // the pathname is ours; archive links and intermediate directory creation are forbidden. |
| 30 | path := '/proc/self/fd/${directory}/.INSTALL' |
| 31 | C.archive_entry_set_pathname(record.entry, unsafe { &char(path.str) }) |
| 32 | C.archive_entry_set_perm(record.entry, if native { u32(0o700) } else { u32(0o644) }) |
| 33 | 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) |
| 34 | if status == C.ARCHIVE_WARN { |
| 35 | handle.log(.warning, localized_message('warning given when extracting %s (%s)\n', '.INSTALL', reader.archive_detail().replace(path, '.INSTALL'))) |
| 36 | } else if status != C.ARCHIVE_OK { |
| 37 | detail := reader.archive_detail().replace(path, '.INSTALL') |
| 38 | handle.log(.error, localized_message('could not extract %s (%s)\n', '.INSTALL', detail)) |
| 39 | return error(detail) |
| 40 | } |
| 41 | return native |
| 42 | } |
| 43 | return false |
| 44 | } |
| 45 | |
| 46 | fn (mut handle Handle) staged_scriptlet_contents(directory i32) ![]u8 { |
| 47 | fd := C.openat(directory, c'.INSTALL', C.O_RDONLY | C.O_NOFOLLOW | C.O_NONBLOCK | C.O_CLOEXEC) |
| 48 | if fd < 0 { return os.last_error() } |
| 49 | defer { handle.close_process_descriptor(fd) } |
| 50 | mut size := i64(0) |
| 51 | mut mode := u32(0) |
| 52 | mut links := u64(0) |
| 53 | if C.v_alpm_download_stat(fd, &size, &mode, &links) != 0 { return os.last_error() } |
| 54 | if mode & 0o170000 != 0o100000 || links != 1 { |
| 55 | return error('staged scriptlet is not a private regular file') |
| 56 | } |
| 57 | return read_scriptlet_descriptor(fd) |
| 58 | } |
| 59 | |