| 1 | module coracle |
| 2 | |
| 3 | import nspkg as facade |
| 4 | import nspkg.engine |
| 5 | import os |
| 6 | |
| 7 | #include "@DIR/../reference_log.h" |
| 8 | |
| 9 | type LocalPopulationLogReceiver = fn (voidptr, u32, &u8) |
| 10 | |
| 11 | fn C.v_alpm_reference_log_install(voidptr, LocalPopulationLogReceiver, voidptr) voidptr |
| 12 | fn C.v_alpm_reference_log_remove(voidptr, voidptr) |
| 13 | fn C.mkfifo(&char, u32) i32 |
| 14 | |
| 15 | struct LocalPopulationTrace { |
| 16 | base string |
| 17 | mut: |
| 18 | items []string |
| 19 | close_directory bool |
| 20 | vanish bool |
| 21 | did_close bool |
| 22 | } |
| 23 | |
| 24 | fn (mut trace LocalPopulationTrace) log(level u32, message string) { |
| 25 | // RET_ERR embeds C function/file locations rather than cache diagnostics. |
| 26 | if level == 4 && message.starts_with('returning error ') { return } |
| 27 | if level == 8 && message.starts_with('loading package data for ') { |
| 28 | if trace.close_directory { |
| 29 | target := os.join_path(trace.base, 'local') |
| 30 | for name in os.ls('/proc/self/fd') or { |
| 31 | assert false, err.msg() |
| 32 | []string{} |
| 33 | } { |
| 34 | actual := os.readlink('/proc/self/fd/' + name) or { continue } |
| 35 | if actual == target { |
| 36 | assert C.close(i32(name.int())) == 0 |
| 37 | trace.did_close = true |
| 38 | trace.close_directory = false |
| 39 | break |
| 40 | } |
| 41 | } |
| 42 | assert trace.did_close |
| 43 | } |
| 44 | if trace.vanish { |
| 45 | os.rmdir(os.join_path(trace.base, 'local/app-1-1')) or { assert false, err.msg() } |
| 46 | trace.vanish = false |
| 47 | } |
| 48 | } |
| 49 | trace.items << 'log:${level}:' + message.replace(trace.base, 'BASE') |
| 50 | } |
| 51 | |
| 52 | fn local_population_reference_log(context voidptr, level u32, message &u8) { |
| 53 | mut trace := unsafe { &LocalPopulationTrace(context) } |
| 54 | trace.log(level, unsafe { cstring_to_vstring(message) }) |
| 55 | } |
| 56 | |
| 57 | fn local_population_public_log(context voidptr, level facade.LogLevel, message &u8) { |
| 58 | mut trace := unsafe { &LocalPopulationTrace(context) } |
| 59 | trace.log(u32(level), unsafe { cstring_to_vstring(message) }) |
| 60 | } |
| 61 | |
| 62 | fn local_population_core_log(context voidptr, level engine.LogLevel, message string) { |
| 63 | mut trace := unsafe { &LocalPopulationTrace(context) } |
| 64 | trace.log(u32(level), message) |
| 65 | } |
| 66 | |
| 67 | struct LocalPopulationOutcome { |
| 68 | first []string |
| 69 | first_code int |
| 70 | second []string |
| 71 | second_code int |
| 72 | items []string |
| 73 | did_close bool |
| 74 | } |
| 75 | |
| 76 | fn local_population_fixture(root string, scenario string) ! { |
| 77 | os.mkdir_all(os.join_path(root, 'local'))! |
| 78 | os.write_file(os.join_path(root, 'local/ALPM_DB_VERSION'), '9\n')! |
| 79 | mut entries := match scenario { |
| 80 | 'empty' { []string{} } |
| 81 | 'unsorted' { ['z-2-1', 'a-1-1', 'mid-1-9'] } |
| 82 | 'duplicate' { ['app-2-1', 'app-1-1'] } |
| 83 | 'invalid' { ['invalid', 'foo-1', '-1-1', 'good-1-1'] } |
| 84 | 'name-dot' { ['.hidden-1-1'] } |
| 85 | 'name-dash' { ['-bad-1-1'] } |
| 86 | 'name-space' { ['name with space-1-1'] } |
| 87 | 'name-colon' { ['bad:char-1-1'] } |
| 88 | 'name-star' { ['star*-1-1'] } |
| 89 | 'name-question' { ['?-1-1'] } |
| 90 | 'name-glob' { ['*-1-1'] } |
| 91 | 'name-unicode' { ['ümlaut-1-1'] } |
| 92 | 'empty-release' { ['app-1-'] } |
| 93 | else { ['app-1-1'] } |
| 94 | } |
| 95 | if scenario == 'closed-directory' { |
| 96 | for index in 0 .. 2000 { entries << 'pkg${index:04}-1-1' } |
| 97 | } |
| 98 | for entry in entries { os.mkdir(os.join_path(root, 'local', entry))! } |
| 99 | match scenario { |
| 100 | 'nondirectory' { os.write_file(os.join_path(root, 'local/file-1-1'), 'not a directory')! } |
| 101 | 'symlink' { |
| 102 | os.mkdir(os.join_path(root, 'elsewhere'))! |
| 103 | os.symlink(os.join_path(root, 'elsewhere'), os.join_path(root, 'local/link-1-1'))! |
| 104 | os.symlink('absent', os.join_path(root, 'local/dangling-1-1'))! |
| 105 | } |
| 106 | 'fifo' { |
| 107 | path := os.join_path(root, 'local/fifo-1-1') |
| 108 | assert C.mkfifo(unsafe { &char(path.str) }, 0o600) == 0 |
| 109 | } |
| 110 | else {} |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | fn local_population_disrupt(root string, scenario string) ! { |
| 115 | path := os.join_path(root, 'local') |
| 116 | match scenario { |
| 117 | 'missing-directory', 'file-directory' { |
| 118 | os.rename(path, os.join_path(root, 'held-local'))! |
| 119 | if scenario == 'file-directory' { os.write_file(path, 'keep this file')! } |
| 120 | } |
| 121 | 'unreadable-directory' { os.chmod(path, 0)! } |
| 122 | 'denied-entry' { os.chmod(os.join_path(path, 'app-1-1'), 0)! } |
| 123 | else {} |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | fn local_population_restore(root string, scenario string) ! { |
| 128 | path := os.join_path(root, 'local') |
| 129 | match scenario { |
| 130 | 'missing-directory', 'file-directory' { |
| 131 | if scenario == 'file-directory' { |
| 132 | assert os.read_file(path)! == 'keep this file' |
| 133 | os.rm(path)! |
| 134 | } |
| 135 | os.rename(os.join_path(root, 'held-local'), path)! |
| 136 | } |
| 137 | 'unreadable-directory' { os.chmod(path, 0o755)! } |
| 138 | 'denied-entry' { os.chmod(os.join_path(path, 'app-1-1'), 0o755)! } |
| 139 | 'vanish' { os.mkdir(os.join_path(path, 'app-1-1'))! } |
| 140 | else {} |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | fn local_population_reference(root string, scenario string, prior bool) !LocalPopulationOutcome { |
| 145 | handle := initialize('/', root)! |
| 146 | defer { handle.release() or { eprintln(err) } } |
| 147 | db := handle.get_localdb()! |
| 148 | mut trace := &LocalPopulationTrace{ base: root, close_directory: scenario == 'closed-directory', vanish: scenario == 'vanish' } |
| 149 | state := C.v_alpm_reference_log_install(handle, local_population_reference_log, trace) |
| 150 | defer { C.v_alpm_reference_log_remove(handle, state) } |
| 151 | local_population_disrupt(root, scenario)! |
| 152 | if prior { |
| 153 | if _ := handle.register_syncdb('', 0) { |
| 154 | assert false |
| 155 | } |
| 156 | } |
| 157 | mut first := []string{} |
| 158 | mut first_code := 0 |
| 159 | mut second := []string{} |
| 160 | mut second_code := 0 |
| 161 | for attempt in 0 .. 2 { |
| 162 | trace.items << 'call:' + attempt.str() |
| 163 | mut packages := []string{} |
| 164 | mut code := 0 |
| 165 | if list := db.get_pkgcache() { |
| 166 | code = int(handle.errno()) |
| 167 | for pkg in list.pkgs() { packages << pkg.get_name() + ':' + pkg.get_version() } |
| 168 | } else { |
| 169 | code = int(handle.errno()) |
| 170 | } |
| 171 | trace.items << 'result:' + attempt.str() + ':' + code.str() |
| 172 | if attempt == 0 { |
| 173 | first = packages.clone() |
| 174 | first_code = code |
| 175 | local_population_restore(root, scenario)! |
| 176 | } else { |
| 177 | second = packages.clone() |
| 178 | second_code = code |
| 179 | } |
| 180 | } |
| 181 | return LocalPopulationOutcome{ |
| 182 | first: first |
| 183 | first_code: first_code |
| 184 | second: second |
| 185 | second_code: second_code |
| 186 | items: trace.items.clone() |
| 187 | did_close: trace.did_close |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | fn local_population_core(root string, scenario string, prior bool) !LocalPopulationOutcome { |
| 192 | mut handle := engine.initialize('/', root)! |
| 193 | defer { handle.release() or { eprintln(err) } } |
| 194 | mut db := handle.local_database()! |
| 195 | mut trace := &LocalPopulationTrace{ base: root, close_directory: scenario == 'closed-directory', vanish: scenario == 'vanish' } |
| 196 | handle.set_log_callback(local_population_core_log, trace)! |
| 197 | local_population_disrupt(root, scenario)! |
| 198 | if prior { |
| 199 | if _ := handle.register_syncdb('', 0) { |
| 200 | assert false |
| 201 | } |
| 202 | } |
| 203 | mut first := []string{} |
| 204 | mut first_code := 0 |
| 205 | mut second := []string{} |
| 206 | mut second_code := 0 |
| 207 | for attempt in 0 .. 2 { |
| 208 | trace.items << 'call:' + attempt.str() |
| 209 | mut packages := []string{} |
| 210 | mut code := 0 |
| 211 | if list := db.package_cache() { |
| 212 | code = int(handle.errno()) |
| 213 | for pkg in list { packages << pkg.name + ':' + pkg.version } |
| 214 | } else { |
| 215 | code = int(handle.errno()) |
| 216 | } |
| 217 | trace.items << 'result:' + attempt.str() + ':' + code.str() |
| 218 | if attempt == 0 { |
| 219 | first = packages.clone() |
| 220 | first_code = code |
| 221 | local_population_restore(root, scenario)! |
| 222 | } else { |
| 223 | second = packages.clone() |
| 224 | second_code = code |
| 225 | } |
| 226 | } |
| 227 | return LocalPopulationOutcome{ |
| 228 | first: first |
| 229 | first_code: first_code |
| 230 | second: second |
| 231 | second_code: second_code |
| 232 | items: trace.items.clone() |
| 233 | did_close: trace.did_close |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | fn local_population_public(root string, scenario string, prior bool) !LocalPopulationOutcome { |
| 238 | handle := facade.initialize('/', root)! |
| 239 | defer { handle.release() or { eprintln(err) } } |
| 240 | db := handle.get_localdb()! |
| 241 | mut trace := &LocalPopulationTrace{ base: root, close_directory: scenario == 'closed-directory', vanish: scenario == 'vanish' } |
| 242 | handle.option_set_logcb_line(local_population_public_log, trace)! |
| 243 | local_population_disrupt(root, scenario)! |
| 244 | if prior { |
| 245 | if _ := handle.register_syncdb('', 0) { |
| 246 | assert false |
| 247 | } |
| 248 | } |
| 249 | mut first := []string{} |
| 250 | mut first_code := 0 |
| 251 | mut second := []string{} |
| 252 | mut second_code := 0 |
| 253 | for attempt in 0 .. 2 { |
| 254 | trace.items << 'call:' + attempt.str() |
| 255 | mut packages := []string{} |
| 256 | mut code := 0 |
| 257 | if list := db.get_pkgcache() { |
| 258 | code = int(handle.errno()) |
| 259 | for pkg in list.pkgs() { packages << pkg.get_name() + ':' + pkg.get_version() } |
| 260 | } else { |
| 261 | code = int(handle.errno()) |
| 262 | } |
| 263 | trace.items << 'result:' + attempt.str() + ':' + code.str() |
| 264 | if attempt == 0 { |
| 265 | first = packages.clone() |
| 266 | first_code = code |
| 267 | local_population_restore(root, scenario)! |
| 268 | } else { |
| 269 | second = packages.clone() |
| 270 | second_code = code |
| 271 | } |
| 272 | } |
| 273 | return LocalPopulationOutcome{ |
| 274 | first: first |
| 275 | first_code: first_code |
| 276 | second: second |
| 277 | second_code: second_code |
| 278 | items: trace.items.clone() |
| 279 | did_close: trace.did_close |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | fn test_local_population_metadata_messages_cache_order_and_retries_match_c() ! { |
| 284 | base := os.join_path(os.vtmp_dir(), 'alpm-local-population-${os.getpid()}') |
| 285 | os.mkdir_all(base)! |
| 286 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 287 | for scenario in ['empty', 'single', 'unsorted', 'duplicate', 'invalid', 'name-dot', 'name-dash', |
| 288 | 'name-space', 'name-colon', 'name-star', 'name-question', 'name-glob', 'name-unicode', |
| 289 | 'empty-release', 'nondirectory', 'symlink', 'fifo', 'denied-entry', 'vanish', |
| 290 | 'missing-directory', 'file-directory', 'unreadable-directory', 'closed-directory'] { |
| 291 | for prior in [false, true] { |
| 292 | root := os.join_path(base, scenario, prior.str()) |
| 293 | local_population_fixture(root, scenario)! |
| 294 | expected := local_population_reference(root, scenario, prior)! |
| 295 | for backend in ['core', 'public'] { |
| 296 | actual := if backend == 'core' { |
| 297 | local_population_core(root, scenario, prior)! |
| 298 | } else { |
| 299 | local_population_public(root, scenario, prior)! |
| 300 | } |
| 301 | context := '${scenario}/${prior}/${backend}' |
| 302 | if scenario == 'symlink' { |
| 303 | // source builds without d_type checks follow links; native keeps its guard. |
| 304 | assert expected.first in [['app:1-1'], ['app:1-1', 'link:1-1']], context |
| 305 | assert actual.first == ['app:1-1'], context |
| 306 | assert actual.second == actual.first, context |
| 307 | assert actual.first_code == 0 && actual.second_code == 0, context |
| 308 | } else if scenario == 'closed-directory' { |
| 309 | // source readdir errors silently commit a partial cache; native retries. |
| 310 | assert expected.did_close && expected.first_code == 0, context |
| 311 | assert expected.first.len > 0 && expected.first.len < 2001, context |
| 312 | assert expected.second == expected.first, context |
| 313 | assert actual.did_close, context |
| 314 | assert actual.first_code == int(Errno.db_open), context |
| 315 | assert actual.first.len == 0, context |
| 316 | assert actual.second_code == 0, context |
| 317 | assert actual.second.len == 2001, context |
| 318 | assert expected.first.all(it in actual.second), context |
| 319 | } else { |
| 320 | assert actual == expected, '${context}:\n${actual}\n!=\n${expected}' |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |