nspkg / engine / install_space.v
212 lines · 201 sloc · 7.74 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// mount ordering and peak per-package block accounting follow diskspace.c.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5import os
6
7struct InstallationMount {
8 path string
9mut:
10 loaded bool
11 failed bool
12 info C.v_alpm_space_info
13 blocks_needed i64
14 maximum_needed i64
15 used_install bool
16 used_remove bool
17}
18
19fn mount_path_unescape(path string) string {
20 mut bytes := []u8{}
21 mut index := 0
22 for index < path.len {
23 if path[index] == `\\` && index + 3 < path.len && path[index + 1..index + 4].bytes().all(it >= `0` && it <= `7`) {
24 value := (u32(path[index + 1] - `0`) << 6) | (u32(path[index + 2] - `0`) << 3) | u32(path[index + 3] - `0`)
25 bytes << u8(value)
26 index += 4
27 } else {
28 bytes << path[index]
29 index++
30 }
31 }
32 return bytes.bytestr()
33}
34
35fn installation_mounts(text string) []InstallationMount {
36 mut mounts := []InstallationMount{}
37 for line in text.split_into_lines() {
38 if line.trim_left(' \t').starts_with('#') { continue }
39 fields := line.fields()
40 if fields.len >= 2 {
41 path := mount_path_unescape(fields[1])
42 if path.starts_with('/') && !path.contains('\x00') {
43 mounts << InstallationMount{ path: path }
44 }
45 }
46 }
47 mounts.sort(a.path > b.path)
48 return mounts
49}
50
51fn installation_mount_index(mounts []InstallationMount, path string) ?int {
52 for index, mount in mounts {
53 if path.starts_with(mount.path) && (mount.path.ends_with('/') || path.len == mount.path.len || path[mount.path.len] == `/`) {
54 return index
55 }
56 }
57 return none
58}
59
60fn (mut handle Handle) load_space_mounts() ![]InstallationMount {
61 text := os.read_file('/etc/mtab') or {
62 problem := os.last_error().msg()
63 handle.log(.error, localized_message('could not open file: %s: %s\n', '/etc/mtab', problem))
64 handle.log(.error, localized_message('could not determine filesystem mount points\n'))
65 return handle.error()
66 }
67 mounts := installation_mounts(text)
68 if mounts.len == 0 {
69 handle.log(.error, localized_message('could not determine filesystem mount points\n'))
70 return handle.error()
71 }
72 for mount in mounts { handle.log(.debug, 'discovered mountpoint: ${mount.path}\n') }
73 return mounts
74}
75
76fn installation_blocks(size i64, block_size u64) !i64 {
77 if size < 0 || block_size == 0 { return error('invalid filesystem size accounting') }
78 amount := u64(size) / block_size + if u64(size) % block_size != 0 { u64(1) } else { u64(0) }
79 if amount > u64(max_i64) { return error('filesystem size accounting overflow') }
80 return i64(amount)
81}
82
83fn (mut handle Handle) load_installation_mount(mut mount InstallationMount) bool {
84 if mount.loaded { return !mount.failed }
85 mount.loaded = true
86 if C.v_alpm_space_stat(unsafe { &char(mount.path.str) }, &mount.info) != 0 || mount.info.block_size == 0 {
87 mount.failed = true
88 problem := os.last_error().msg()
89 handle.diagnostics << 'cannot get filesystem information for ${mount.path}: ${problem}'
90 handle.log(.warning, localized_message('could not get filesystem information for %s: %s\n', mount.path, problem))
91 } else {
92 handle.log(.debug, 'loading fsinfo for ${mount.path}\n')
93 }
94 return !mount.failed
95}
96
97fn (mut handle Handle) space_mount_full(mount InstallationMount) !bool {
98 cushion := download_space_needed([], mount.info.block_size, mount.info.blocks) or { return handle.fail(.disk_space, err.msg()) }
99 if cushion > u64(max_i64) || mount.maximum_needed > max_i64 - i64(cushion) {
100 return handle.fail(.disk_space, 'filesystem space accounting overflow')
101 }
102 needed := mount.maximum_needed + i64(cushion)
103 handle.log(.debug, 'partition ${mount.path}, needed ${mount.maximum_needed}, cushion ${cushion}, free ${mount.info.available}\n')
104 if needed >= 0 && u64(needed) > mount.info.available {
105 handle.log(.error, localized_message('Partition %s too full: %jd blocks needed, %ju blocks free\n', mount.path, needed, mount.info.available))
106 return true
107 }
108 return false
109}
110
111fn (mut handle Handle) account_installation_files(mut root RootFilesystem, mut mounts []InstallationMount, pkg &Package, removing bool) ! {
112 for file in pkg.files {
113 mut size := file.size
114 if removing {
115 status := root.stat(file.name, false) or {
116 handle.diagnostics << 'cannot inspect disk usage for ${file.name}: ${err.msg()}'
117 if match_stored_patterns(handle.opts.lists[.no_extract], file.name) != 0 {
118 handle.log(.warning, localized_message('could not get file information for %s\n', file.name))
119 }
120 continue
121 }
122 if !status.present {
123 if match_stored_patterns(handle.opts.lists[.no_extract], file.name) != 0 {
124 handle.log(.warning, localized_message('could not get file information for %s\n', file.name))
125 }
126 continue
127 }
128 if status.info.mode & 0o170000 in [u32(0o040000), 0o120000] {
129 continue
130 }
131 size = status.info.size
132 } else if file.mode & 0o170000 in [u32(0o040000), 0o120000] {
133 continue
134 }
135 // the source attributes dot members to the dbpath using this root-prefixed form.
136 filename := if !removing && file.name.starts_with('.') {
137 handle.dbpath
138 } else {
139 file.name
140 }
141 path := handle.root + filename
142 index := installation_mount_index(mounts, path) or {
143 handle.diagnostics << 'cannot determine mount point for ${filename}'
144 handle.log(.warning, localized_message('could not determine mount point for file %s\n', filename))
145 continue
146 }
147 mut mount := unsafe { &mounts[index] }
148 if !handle.load_installation_mount(mut mount) { continue }
149 blocks := installation_blocks(size, mount.info.block_size) or { return handle.fail(.disk_space, err.msg()) }
150 if removing {
151 if mount.blocks_needed < (-max_i64 - 1) + blocks {
152 return handle.fail(.disk_space, 'removal space accounting overflow')
153 }
154 mount.blocks_needed -= blocks
155 mount.used_remove = true
156 } else {
157 if mount.blocks_needed > max_i64 - blocks {
158 return handle.fail(.disk_space, 'installation space accounting overflow')
159 }
160 mount.blocks_needed += blocks
161 mount.used_install = true
162 }
163 }
164}
165
166fn (mut handle Handle) check_install_space(mut root RootFilesystem, trans &Transaction) ! {
167 mut mounts := handle.load_space_mounts()!
168 if installation_mount_index(mounts, handle.root) == none {
169 handle.log(.error, localized_message('could not determine root mount point %s\n', handle.root))
170 return handle.fail(.disk_space, 'cannot determine root mount point')
171 }
172 total := trans.add.len + trans.remove.len
173 mut current := 0
174 for pkg in trans.remove {
175 handle.commit_progress_for(.diskspace, '', current * 100 / total, total, current)
176 handle.account_installation_files(mut root, mut mounts, pkg, true)!
177 current++
178 }
179 for pkg in trans.add {
180 handle.commit_progress_for(.diskspace, '', current * 100 / total, total, current)
181 if old := handle.local.by_name[pkg.name] {
182 mut installed := unsafe { old }
183 installed.load_files() or { return handle.fail(.db_invalid, err.msg()) }
184 handle.account_installation_files(mut root, mut mounts, installed, true)!
185 }
186 handle.account_installation_files(mut root, mut mounts, pkg, false)!
187 for mut mount in mounts {
188 if mount.blocks_needed > mount.maximum_needed {
189 mount.maximum_needed = mount.blocks_needed
190 }
191 }
192 current++
193 }
194 handle.commit_progress_for(.diskspace, '', 100, total, current)
195 mut failures := []string{}
196 for mount in mounts {
197 if !mount.used_install && !mount.used_remove { continue }
198 if mount.info.read_only != 0 {
199 handle.log(.error, localized_message('Partition %s is mounted read only\n', mount.path))
200 failures << '${mount.path} is mounted read only'
201 continue
202 }
203 if mount.used_install {
204 cushion := download_space_needed([], mount.info.block_size, mount.info.blocks) or { return handle.fail(.disk_space, err.msg()) }
205 needed := u64(mount.maximum_needed) + cushion
206 if handle.space_mount_full(mount)! {
207 failures << '${mount.path}: ${needed} blocks needed, ${mount.info.available} available'
208 }
209 }
210 }
211 if failures.len > 0 { return handle.fail(.disk_space, failures.join('; ')) }
212}
213