| 1 | module main |
| 2 | |
| 3 | import nspkg.tests.coracle as alpm |
| 4 | import nspkg.engine |
| 5 | import nspkg.tests.installfixtures |
| 6 | import nspkg.tests.localefixtures |
| 7 | import nspkg.tests.signaturefixtures |
| 8 | import os |
| 9 | |
| 10 | #include <sys/mount.h> |
| 11 | #include <locale.h> |
| 12 | #include <libintl.h> |
| 13 | #include "@DIR/reference_log.h" |
| 14 | |
| 15 | fn C.mount(&char, &char, &char, u64, voidptr) i32 |
| 16 | fn C.setlocale(i32, &char) &char |
| 17 | fn C.bindtextdomain(const_domain &char, const_directory &char) &char |
| 18 | |
| 19 | type SpaceLogReceiver = fn (voidptr, u32, &u8) |
| 20 | |
| 21 | fn C.v_alpm_reference_log_install(voidptr, SpaceLogReceiver, voidptr) voidptr |
| 22 | fn C.v_alpm_reference_log_remove(voidptr, voidptr) |
| 23 | |
| 24 | struct SpaceResult { |
| 25 | mut: |
| 26 | success bool |
| 27 | kind int |
| 28 | trace []string |
| 29 | logs []string |
| 30 | base string |
| 31 | } |
| 32 | |
| 33 | fn space_log(result &SpaceResult, level u32, message string) { |
| 34 | if !message.contains(result.base) { return } |
| 35 | if !message.starts_with('loading fsinfo for ') && !message.starts_with('partition ') |
| 36 | && !message.starts_with('Partition ') && level !in [u32(1), 2] { |
| 37 | return |
| 38 | } |
| 39 | mut state := unsafe { result } |
| 40 | state.logs << '${level}:' + message.replace(result.base, '<fixture>') |
| 41 | } |
| 42 | |
| 43 | fn source_space_log(context voidptr, level u32, message &u8) { |
| 44 | space_log(unsafe { &SpaceResult(context) }, level, unsafe { tos_clone(message) }) |
| 45 | } |
| 46 | |
| 47 | fn native_space_log(context voidptr, level engine.LogLevel, message string) { |
| 48 | space_log(unsafe { &SpaceResult(context) }, u32(level), message) |
| 49 | } |
| 50 | |
| 51 | fn space_catalog() { |
| 52 | path := os.join_path(os.args[1], 'catalog') |
| 53 | C.bindtextdomain(c'libalpm', unsafe { &char(path.str) }) |
| 54 | } |
| 55 | |
| 56 | fn source_event(context voidptr, event &alpm.Event) { |
| 57 | mut result := unsafe { &SpaceResult(context) } |
| 58 | kind := unsafe { event.type_ } |
| 59 | if kind in [.diskspace_start, .diskspace_done, .transaction_start] { |
| 60 | result.trace << kind.str() |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | fn native_event(context voidptr, event engine.CommitEvent) { |
| 65 | mut result := unsafe { &SpaceResult(context) } |
| 66 | if event.kind in [.diskspace_start, .diskspace_done, .transaction_start] { |
| 67 | result.trace << event.kind.str() |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | fn source_progress(context voidptr, kind alpm.Progress, _ &u8, percent i32, total uint, current uint) { |
| 72 | if kind != .diskspace_start { return } |
| 73 | mut result := unsafe { &SpaceResult(context) } |
| 74 | result.trace << '${percent}:${total}:${current}' |
| 75 | } |
| 76 | |
| 77 | fn native_progress(context voidptr, item engine.CommitProgress) { |
| 78 | if item.kind != .diskspace { return } |
| 79 | mut result := unsafe { &SpaceResult(context) } |
| 80 | result.trace << '${item.percent}:${item.total}:${item.current}' |
| 81 | } |
| 82 | |
| 83 | fn source_commit(fixture installfixtures.Fixture, skip bool) !SpaceResult { |
| 84 | handle := alpm.initialize(fixture.root, fixture.db)! |
| 85 | defer { handle.release() or { eprintln(err) } } |
| 86 | mut result := &SpaceResult{ base: os.dir(fixture.root) } |
| 87 | handle.option_add_cachedir(fixture.cache)! |
| 88 | handle.option_set_checkspace(1)! |
| 89 | if skip { handle.option_add_no_extract('usr/bin/app')! } |
| 90 | handle.option_set_eventcb(source_event, result)! |
| 91 | handle.option_set_progresscb(source_progress, result)! |
| 92 | handle.trans_init(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet)! |
| 93 | defer { handle.trans_release() or { eprintln(err) } } |
| 94 | for path in fixture.archives { handle.add_pkg(handle.pkg_load(path, 1, 0)!)! } |
| 95 | handle.trans_prepare()!.free() |
| 96 | space_catalog() |
| 97 | adapter := C.v_alpm_reference_log_install(handle, source_space_log, result) |
| 98 | defer { C.v_alpm_reference_log_remove(handle, adapter) } |
| 99 | completed := handle.trans_commit() or { |
| 100 | result.kind = int(handle.errno()) |
| 101 | return *result |
| 102 | } |
| 103 | completed.free() |
| 104 | result.success = true |
| 105 | return *result |
| 106 | } |
| 107 | |
| 108 | fn native_commit(fixture installfixtures.Fixture, skip bool) !SpaceResult { |
| 109 | mut handle := engine.initialize(fixture.root, fixture.db)! |
| 110 | defer { handle.release() or { eprintln(err) } } |
| 111 | mut result := &SpaceResult{ base: os.dir(fixture.root) } |
| 112 | handle.add_string_list(.cache_dirs, fixture.cache)! |
| 113 | handle.set_integer_option(.check_space, 1)! |
| 114 | if skip { handle.add_string_list(.no_extract, 'usr/bin/app')! } |
| 115 | handle.set_commit_callback(native_event, result)! |
| 116 | handle.set_commit_progress_callback(native_progress, result)! |
| 117 | handle.start_transaction(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet)! |
| 118 | defer { handle.release_transaction() or { eprintln(err) } } |
| 119 | for path in fixture.archives { handle.add_package(handle.load_package(path, true, 0)!)! } |
| 120 | assert handle.prepare_transaction()!.success |
| 121 | space_catalog() |
| 122 | handle.set_log_callback(native_space_log, result)! |
| 123 | handle.commit_transaction() or { |
| 124 | result.kind = err.code() |
| 125 | assert handle.transaction_state()! == .downloading |
| 126 | return *result |
| 127 | } |
| 128 | result.success = true |
| 129 | return *result |
| 130 | } |
| 131 | |
| 132 | fn setup(base string, geometry string) !installfixtures.Fixture { |
| 133 | peak := geometry in ['peak', 'reordered'] |
| 134 | scenario := installfixtures.Scenario{ |
| 135 | name: geometry |
| 136 | installed: false |
| 137 | missing_local: true |
| 138 | incoming: if peak { |
| 139 | '' |
| 140 | } else { |
| 141 | 'incoming' |
| 142 | } |
| 143 | payload: 'x'.repeat(if peak { 8192 } else { 12000 }) |
| 144 | second: peak |
| 145 | } |
| 146 | mut fixture := installfixtures.create(base, scenario)! |
| 147 | usr := geometry in ['small-usr', 'readonly-usr', 'noextract'] |
| 148 | point := if usr { os.join_path(fixture.root, 'usr') } else { fixture.root } |
| 149 | size := if geometry in ['small-root', 'small-usr', 'noextract'] { |
| 150 | '8k' |
| 151 | } else if peak { |
| 152 | '16k' |
| 153 | } else { |
| 154 | '32m' |
| 155 | } |
| 156 | signaturefixtures.command('mount', ['-t', 'tmpfs', '-o', 'size=' + size, 'tmpfs', point])! |
| 157 | if peak { |
| 158 | os.mkdir_all(os.join_path(fixture.root, 'usr/bin'))! |
| 159 | os.write_file(os.join_path(fixture.root, 'usr/bin/second'), 'o'.repeat(8192))! |
| 160 | entry := os.join_path(fixture.db, 'local', 'second-1-1') |
| 161 | os.mkdir(entry)! |
| 162 | os.write_file(os.join_path(entry, 'desc'), '%REASON%\n1\n\n')! |
| 163 | os.write_file(os.join_path(entry, 'files'), '%FILES%\nusr/\nusr/bin/\nusr/bin/second\n\n')! |
| 164 | } |
| 165 | if geometry == 'reordered' { |
| 166 | fixture = installfixtures.Fixture{ ...fixture, archives: fixture.archives.reverse() } |
| 167 | } |
| 168 | if geometry.starts_with('readonly-') { |
| 169 | // avoid replaying mtab's host UID/GID options in a mapped user namespace. |
| 170 | if C.mount(unsafe { nil }, unsafe { &char(point.str) }, unsafe { nil }, u64(C.MS_REMOUNT | C.MS_RDONLY), unsafe { nil }) != 0 { |
| 171 | return os.last_error() |
| 172 | } |
| 173 | } |
| 174 | return fixture |
| 175 | } |
| 176 | |
| 177 | fn run() ! { |
| 178 | if os.args.len != 3 || os.readlink('/proc/self/ns/mnt')! == os.args[2] { |
| 179 | return error('requires a private mount namespace') |
| 180 | } |
| 181 | signaturefixtures.command('mount', ['--make-rprivate', '/'])! |
| 182 | assert os.setenv('SOURCE_DATE_EPOCH', '1700000000', true) == 0 |
| 183 | localefixtures.write(os.join_path(os.args[1], 'catalog/zz/LC_MESSAGES/libalpm.mo'), { |
| 184 | 'Partition %s too full: %jd blocks needed, %ju blocks free\n': 'zz: free %3$ju; needed %2$jd; partition %1$s\n' |
| 185 | 'Partition %s is mounted read only\n': 'zz: readonly %s\n' |
| 186 | })! |
| 187 | assert os.setenv('LANGUAGE', 'zz', true) == 0 |
| 188 | for translated in [false, true] { |
| 189 | locale := if translated { 'en_US.UTF-8' } else { 'C' } |
| 190 | available := C.setlocale(C.LC_ALL, unsafe { &char(locale.str) }) != unsafe { nil } |
| 191 | for geometry in ['small-root', 'large-root', 'small-usr', 'readonly-root', 'readonly-usr', |
| 192 | 'noextract', 'peak', 'reordered'] { |
| 193 | mut results := []SpaceResult{} |
| 194 | mut roots := []map[string]string{} |
| 195 | mut databases := []map[string]string{} |
| 196 | for native in [false, true] { |
| 197 | fixture := setup(os.join_path(os.args[1], translated.str(), geometry, native.str()), geometry)! |
| 198 | before_root := installfixtures.snapshot(fixture.root)! |
| 199 | before_db := installfixtures.snapshot(fixture.db)! |
| 200 | result := if native { |
| 201 | native_commit(fixture, geometry == 'noextract')! |
| 202 | } else { |
| 203 | source_commit(fixture, geometry == 'noextract')! |
| 204 | } |
| 205 | assert result.success == (geometry in ['large-root', 'reordered']), '${geometry}:${native}:${result}' |
| 206 | if !result.success { |
| 207 | assert result.kind == int(engine.Errno.disk_space) |
| 208 | assert installfixtures.snapshot(fixture.root)! == before_root |
| 209 | assert installfixtures.snapshot(fixture.db)! == before_db |
| 210 | assert 'transaction_start' !in result.trace |
| 211 | } |
| 212 | results << result |
| 213 | roots << installfixtures.snapshot(fixture.root)! |
| 214 | databases << installfixtures.snapshot(fixture.db)! |
| 215 | } |
| 216 | assert results[1].logs == results[0].logs, '${geometry}:${results}' |
| 217 | if translated && available && !results[0].success { |
| 218 | assert results[0].logs.any(it.starts_with('1:zz:')), results[0].logs.str() |
| 219 | } |
| 220 | assert results[1].success == results[0].success && results[1].kind == results[0].kind |
| 221 | assert results[1].trace == results[0].trace, '${geometry}:${results}' |
| 222 | assert roots[1] == roots[0], geometry |
| 223 | assert databases[1] == databases[0], geometry |
| 224 | } |
| 225 | } |
| 226 | println('16 native/libalpm installation space comparisons verified') |
| 227 | } |
| 228 | |
| 229 | fn main() { |
| 230 | run() or { |
| 231 | eprintln(err) |
| 232 | exit(1) |
| 233 | } |
| 234 | } |
| 235 | |