nspkg / engine / transaction_install_files.v
119 lines · 114 sloc · 5.1 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// backup hashes and pacnew decisions follow add.c's extract_single_file.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5fn (mut handle Handle) backup_digest(mut root RootFilesystem, path string) string {
6 return root.md5sum(path) or {
7 handle.diagnostics << 'cannot hash backup ${path}: ${err.msg()}'
8 ''
9 }
10}
11
12fn (mut handle Handle) extract_install_record(mut root RootFilesystem, mut reader ArchiveReader, record ArchiveRecord, mut pkg Package, oldpkg &Package) ! {
13 // add.c's initial backup getter clears the package owner's error for every entry.
14 handle.check()!
15 path := record.name
16 if path.starts_with('.') {
17 handle.extract_database_record(mut root, mut reader, record, pkg)!
18 return
19 }
20 if pkg.find_file(path) == none {
21 handle.diagnostics << 'archive member is absent from package file list: ${path}'
22 handle.log(.warning, localized_message('file not found in file list for package %s. skipping extraction of %s\n', pkg.name, path))
23 reader.skip_data()!
24 return
25 }
26 if match_stored_patterns(handle.opts.lists[.no_extract], path) == 0 {
27 reader.skip_data()!
28 return
29 }
30 status := root.stat(path, false)!
31 is_directory := record.mode & 0o170000 == 0o040000
32 if status.present && status.info.mode & 0o170000 == 0o040000 && is_directory {
33 if status.info.mode != record.mode {
34 existing := status.info.mode & 0o7777
35 incoming_mode := record.mode & 0o7777
36 filename := handle.root + path
37 handle.log(.warning, localized_message('directory permissions differ on %s\nfilesystem: %o package: %o\n', filename, existing, incoming_mode))
38 handle.record_action('warning: directory permissions differ on ${filename}, filesystem: ${existing:o} package: ${incoming_mode:o}\n')
39 }
40 handle.extract_install_payload(mut root, mut reader, record, path)!
41 return
42 }
43 if status.present && status.info.mode & 0o170000 == 0o040000 {
44 handle.log(.error, localized_message('extract: not overwriting dir with file %s\n', handle.root + path))
45 reader.skip_data()!
46 return error('cannot overwrite a directory: ${path}')
47 }
48 mut no_upgrade := false
49 mut need_backup := false
50 mut original := ''
51 mut has_original := false
52 if status.present && status.info.mode & 0o170000 != 0o040000 && !is_directory {
53 no_upgrade = match_stored_patterns(handle.opts.lists[.no_upgrade], path) == 0
54 if !no_upgrade {
55 if backup := package_backup(oldpkg, path) {
56 need_backup = true
57 original = backup.hash
58 has_original = backup.has_hash || backup.hash != ''
59 } else {
60 need_backup = package_backup(pkg, path) != none
61 }
62 }
63 }
64 destination := path + if no_upgrade || need_backup { '.pacnew' } else { '' }
65 new_pacnew := (no_upgrade || need_backup) && !root.stat(destination, false)!.present
66 handle.extract_install_payload(mut root, mut reader, record, destination)!
67 mut incoming := ''
68 mut found_backup := false
69 for index, backup in pkg.backup {
70 if backup.name == path {
71 incoming = handle.backup_digest(mut root, destination)
72 pkg.backup[index] = PackageBackup{ name: backup.name, hash: incoming, has_hash: incoming != '' }
73 found_backup = true
74 break
75 }
76 }
77 if no_upgrade {
78 handle.commit_event(CommitEvent{ kind: .pacnew_created, old_package: unsafe { oldpkg }, new_package: unsafe { pkg }, file: handle.root + path, from_no_upgrade: true })
79 filename := handle.root + path
80 handle.record_action('warning: ${filename} installed as ${filename}.pacnew\n')
81 } else if need_backup {
82 local := handle.backup_digest(mut root, path)
83 if !found_backup { incoming = handle.backup_digest(mut root, destination) }
84 if local != '' && incoming != '' && local == incoming {
85 handle.rename_install_backup(mut root, destination, path)!
86 } else if has_original && incoming != '' && original == incoming {
87 if new_pacnew { root.unlink(destination, false)! }
88 } else if has_original && local != '' && original == local {
89 handle.rename_install_backup(mut root, destination, path)!
90 } else {
91 handle.commit_event(CommitEvent{ kind: .pacnew_created, old_package: unsafe { oldpkg }, new_package: unsafe { pkg }, file: handle.root + path })
92 filename := handle.root + path
93 handle.record_action('warning: ${filename} installed as ${filename}.pacnew\n')
94 }
95 }
96}
97
98fn (mut handle Handle) rename_install_backup(mut root RootFilesystem, from string, to string) ! {
99 root.rename(from, to) or {
100 text := 'could not rename ${handle.root + from} to ${handle.root + to} (${err.msg()})\n'
101 handle.log(.error, localized_message('could not rename %s to %s (%s)\n', handle.root + from, handle.root + to, err.msg()))
102 handle.record_action('error: ' + text)
103 return err
104 }
105}
106
107fn (mut handle Handle) extract_install_payload(mut root RootFilesystem, mut reader ArchiveReader, record ArchiveRecord, path string) ! {
108 warning := handle.extract_payload_record(mut root, mut reader, record, path) or {
109 handle.record_extraction_failure(handle.root + path, err)
110 return err
111 }
112 warning.report(handle, handle.root + path)
113}
114
115fn (mut handle Handle) record_extraction_failure(path string, problem IError) {
116 text := 'could not extract ${path} (${problem.msg()})\n'
117 handle.log(.error, localized_message('could not extract %s (%s)\n', path, problem.msg()))
118 handle.record_action('error: ' + text)
119}
120