// backup hashes and pacnew decisions follow add.c's extract_single_file. // SPDX-License-Identifier: GPL-2.0-or-later module engine fn (mut handle Handle) backup_digest(mut root RootFilesystem, path string) string { return root.md5sum(path) or { handle.diagnostics << 'cannot hash backup ${path}: ${err.msg()}' '' } } fn (mut handle Handle) extract_install_record(mut root RootFilesystem, mut reader ArchiveReader, record ArchiveRecord, mut pkg Package, oldpkg &Package) ! { // add.c's initial backup getter clears the package owner's error for every entry. handle.check()! path := record.name if path.starts_with('.') { handle.extract_database_record(mut root, mut reader, record, pkg)! return } if pkg.find_file(path) == none { handle.diagnostics << 'archive member is absent from package file list: ${path}' handle.log(.warning, localized_message('file not found in file list for package %s. skipping extraction of %s\n', pkg.name, path)) reader.skip_data()! return } if match_stored_patterns(handle.opts.lists[.no_extract], path) == 0 { reader.skip_data()! return } status := root.stat(path, false)! is_directory := record.mode & 0o170000 == 0o040000 if status.present && status.info.mode & 0o170000 == 0o040000 && is_directory { if status.info.mode != record.mode { existing := status.info.mode & 0o7777 incoming_mode := record.mode & 0o7777 filename := handle.root + path handle.log(.warning, localized_message('directory permissions differ on %s\nfilesystem: %o package: %o\n', filename, existing, incoming_mode)) handle.record_action('warning: directory permissions differ on ${filename}, filesystem: ${existing:o} package: ${incoming_mode:o}\n') } handle.extract_install_payload(mut root, mut reader, record, path)! return } if status.present && status.info.mode & 0o170000 == 0o040000 { handle.log(.error, localized_message('extract: not overwriting dir with file %s\n', handle.root + path)) reader.skip_data()! return error('cannot overwrite a directory: ${path}') } mut no_upgrade := false mut need_backup := false mut original := '' mut has_original := false if status.present && status.info.mode & 0o170000 != 0o040000 && !is_directory { no_upgrade = match_stored_patterns(handle.opts.lists[.no_upgrade], path) == 0 if !no_upgrade { if backup := package_backup(oldpkg, path) { need_backup = true original = backup.hash has_original = backup.has_hash || backup.hash != '' } else { need_backup = package_backup(pkg, path) != none } } } destination := path + if no_upgrade || need_backup { '.pacnew' } else { '' } new_pacnew := (no_upgrade || need_backup) && !root.stat(destination, false)!.present handle.extract_install_payload(mut root, mut reader, record, destination)! mut incoming := '' mut found_backup := false for index, backup in pkg.backup { if backup.name == path { incoming = handle.backup_digest(mut root, destination) pkg.backup[index] = PackageBackup{ name: backup.name, hash: incoming, has_hash: incoming != '' } found_backup = true break } } if no_upgrade { handle.commit_event(CommitEvent{ kind: .pacnew_created, old_package: unsafe { oldpkg }, new_package: unsafe { pkg }, file: handle.root + path, from_no_upgrade: true }) filename := handle.root + path handle.record_action('warning: ${filename} installed as ${filename}.pacnew\n') } else if need_backup { local := handle.backup_digest(mut root, path) if !found_backup { incoming = handle.backup_digest(mut root, destination) } if local != '' && incoming != '' && local == incoming { handle.rename_install_backup(mut root, destination, path)! } else if has_original && incoming != '' && original == incoming { if new_pacnew { root.unlink(destination, false)! } } else if has_original && local != '' && original == local { handle.rename_install_backup(mut root, destination, path)! } else { handle.commit_event(CommitEvent{ kind: .pacnew_created, old_package: unsafe { oldpkg }, new_package: unsafe { pkg }, file: handle.root + path }) filename := handle.root + path handle.record_action('warning: ${filename} installed as ${filename}.pacnew\n') } } } fn (mut handle Handle) rename_install_backup(mut root RootFilesystem, from string, to string) ! { root.rename(from, to) or { text := 'could not rename ${handle.root + from} to ${handle.root + to} (${err.msg()})\n' handle.log(.error, localized_message('could not rename %s to %s (%s)\n', handle.root + from, handle.root + to, err.msg())) handle.record_action('error: ' + text) return err } } fn (mut handle Handle) extract_install_payload(mut root RootFilesystem, mut reader ArchiveReader, record ArchiveRecord, path string) ! { warning := handle.extract_payload_record(mut root, mut reader, record, path) or { handle.record_extraction_failure(handle.root + path, err) return err } warning.report(handle, handle.root + path) } fn (mut handle Handle) record_extraction_failure(path string, problem IError) { text := 'could not extract ${path} (${problem.msg()})\n' handle.log(.error, localized_message('could not extract %s (%s)\n', path, problem.msg())) handle.record_action('error: ' + text) }