| 1 | // metadata order and warning selection follow libarchive's disk writer as used by add.c. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | fn C.archive_entry_clone(&C.Archive_entry) &C.Archive_entry |
| 8 | fn C.archive_entry_free(&C.Archive_entry) |
| 9 | fn C.fchownat(i32, &char, u32, u32, i32) i32 |
| 10 | |
| 11 | struct ExtractionWarning { |
| 12 | detail string |
| 13 | ownership bool |
| 14 | uid i64 |
| 15 | gid i64 |
| 16 | } |
| 17 | |
| 18 | fn (warning ExtractionWarning) report(handle &Handle, filename string) { |
| 19 | if warning.detail == '' && !warning.ownership { return } |
| 20 | detail := if warning.ownership { |
| 21 | "Can't set user=${warning.uid}/group=${warning.gid} for ${os.norm_path(filename)}" |
| 22 | } else { |
| 23 | warning.detail |
| 24 | } |
| 25 | handle.log(.warning, localized_message('warning given when extracting %s (%s)\n', filename, detail)) |
| 26 | } |
| 27 | |
| 28 | fn (mut handle Handle) extraction_metadata_problem(action string, problem IError) ! { |
| 29 | if problem.code() == int(C.ENOSPC) { |
| 30 | return handle.fail(.libarchive, '${action}: ${problem.msg()}') |
| 31 | } |
| 32 | handle.diagnostics << '${action}: ${problem.msg()}' |
| 33 | } |
| 34 | |
| 35 | fn (mut handle Handle) extract_record_owner(record ArchiveRecord, fd i32, anchor string) !bool { |
| 36 | uid := u32(C.archive_entry_uid(record.entry)) |
| 37 | gid := u32(C.archive_entry_gid(record.entry)) |
| 38 | if C.v_alpm_extract_chown(fd, unsafe { &char(anchor.str) }, uid, gid) == 0 { return true } |
| 39 | mut problem := os.last_error() |
| 40 | if problem.code() == int(C.ENOSPC) { |
| 41 | handle.extraction_metadata_problem('set archive ownership on ${record.name}', problem)! |
| 42 | } |
| 43 | $if linux { |
| 44 | if fd >= 0 { |
| 45 | // retry the alternate syscall on the held inode, even if its pathname was replaced. |
| 46 | if C.fchownat(fd, c'', uid, gid, C.AT_EMPTY_PATH | C.AT_SYMLINK_NOFOLLOW) == 0 { |
| 47 | return true |
| 48 | } |
| 49 | problem = os.last_error() |
| 50 | } |
| 51 | } |
| 52 | handle.extraction_metadata_problem('set archive ownership on ${record.name}', problem)! |
| 53 | return false |
| 54 | } |
| 55 | |
| 56 | fn (mut handle Handle) extract_record_metadata(record ArchiveRecord, path &RootPath, fd i32, mode u32, started_at i64) !ExtractionWarning { |
| 57 | entry := record.entry |
| 58 | anchor := path.descriptor_path() |
| 59 | mut warning := ExtractionWarning{} |
| 60 | mut permissions := mode & 0o7777 |
| 61 | if !handle.extract_record_owner(record, fd, anchor)! { |
| 62 | warning = ExtractionWarning{ ownership: true, uid: C.archive_entry_uid(entry), gid: C.archive_entry_gid(entry) } |
| 63 | mut uid := u32(0) |
| 64 | mut gid := u32(0) |
| 65 | if C.v_alpm_extract_owner(fd, unsafe { &char(anchor.str) }, &uid, &gid) != 0 { |
| 66 | permissions &= ~u32(0o6000) |
| 67 | if mode & 0o2000 != 0 { warning = ExtractionWarning{ detail: "Couldn't stat file" } } |
| 68 | } else if mode & 0o2000 != 0 { |
| 69 | if gid != u32(C.archive_entry_gid(entry)) { |
| 70 | permissions &= ~u32(0o2000) |
| 71 | warning = ExtractionWarning{ detail: "Can't restore SGID bit" } |
| 72 | } |
| 73 | if mode & 0o4000 != 0 && uid != u32(C.archive_entry_uid(entry)) { |
| 74 | permissions &= ~u32(0o4000) |
| 75 | warning = ExtractionWarning{ detail: "Can't restore SUID bit" } |
| 76 | } |
| 77 | } else if mode & 0o4000 != 0 && C.geteuid() != u32(C.archive_entry_uid(entry)) { |
| 78 | permissions &= ~u32(0o4000) |
| 79 | warning = ExtractionWarning{ detail: "Can't make file SUID" } |
| 80 | } |
| 81 | } |
| 82 | if C.geteuid() != 0 { |
| 83 | attributes := handle.extract_record_xattrs(record, fd, anchor)! |
| 84 | if attributes.detail != '' { warning = attributes } |
| 85 | } |
| 86 | if mode & 0o170000 != 0o120000 && C.v_alpm_extract_chmod(fd, unsafe { &char(anchor.str) }, permissions) != 0 { |
| 87 | handle.extraction_metadata_problem('set archive permissions on ${record.name}', os.last_error())! |
| 88 | // directory mode and time fixup errors occur during writer cleanup in the source. |
| 89 | if mode & 0o170000 != 0o040000 { |
| 90 | warning = ExtractionWarning{ detail: "Can't set permissions to 0${permissions:o}" } |
| 91 | } |
| 92 | } |
| 93 | if C.geteuid() == 0 { |
| 94 | attributes := handle.extract_record_xattrs(record, fd, anchor)! |
| 95 | if attributes.detail != '' { warning = attributes } |
| 96 | } |
| 97 | 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 { |
| 98 | handle.extraction_metadata_problem('set archive times on ${record.name}', os.last_error())! |
| 99 | if mode & 0o170000 != 0o040000 { |
| 100 | warning = ExtractionWarning{ detail: "Can't restore time" } |
| 101 | } |
| 102 | } |
| 103 | return warning |
| 104 | } |
| 105 | |
| 106 | fn (mut handle Handle) extract_record_xattrs(record ArchiveRecord, fd i32, anchor string) !ExtractionWarning { |
| 107 | if C.archive_entry_xattr_reset(record.entry) == 0 { return ExtractionWarning{} } |
| 108 | // the disk writer walks a cloned entry, whose attribute order can differ from the reader's. |
| 109 | entry := C.archive_entry_clone(record.entry) |
| 110 | if entry == unsafe { nil } { return handle.fail(.memory, 'clone archive metadata') } |
| 111 | defer { C.archive_entry_free(entry) } |
| 112 | mut names := []string{} |
| 113 | mut failed := false |
| 114 | mut specific := false |
| 115 | count := C.archive_entry_xattr_reset(entry) |
| 116 | for _ in 0 .. count { |
| 117 | mut name := &char(unsafe { nil }) |
| 118 | mut value := voidptr(unsafe { nil }) |
| 119 | mut size := uint(0) |
| 120 | if C.archive_entry_xattr_next(entry, &name, &value, &size) != C.ARCHIVE_OK { |
| 121 | return handle.fail(.libarchive, 'read archive extended attributes') |
| 122 | } |
| 123 | if name == unsafe { nil } { continue } |
| 124 | attribute := unsafe { cstring_to_vstring(name) } |
| 125 | if attribute in ['system.posix_acl_access', 'system.posix_acl_default', |
| 126 | 'trusted.SGI_ACL_DEFAULT', 'trusted.SGI_ACL_FILE'] { |
| 127 | continue |
| 128 | } |
| 129 | if attribute.starts_with('xfsroot.') { |
| 130 | // the obsolete namespace contributes to a later error but does not trigger one alone. |
| 131 | names << attribute |
| 132 | specific = true |
| 133 | continue |
| 134 | } |
| 135 | if C.v_alpm_extract_xattr(fd, unsafe { &char(anchor.str) }, name, value, size) != 0 { |
| 136 | problem := os.last_error() |
| 137 | handle.extraction_metadata_problem('set archive extended attribute on ${record.name}', problem)! |
| 138 | names << attribute |
| 139 | failed = true |
| 140 | if problem.code() !in [int(C.ENOTSUP), int(C.ENOSYS)] { specific = true } |
| 141 | } |
| 142 | } |
| 143 | if !failed { return ExtractionWarning{} } |
| 144 | return ExtractionWarning{ |
| 145 | detail: if specific { |
| 146 | 'Cannot restore extended attributes: ' + names.join(' ') |
| 147 | } else { |
| 148 | 'Cannot restore extended attributes on this file system' |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |