| 1 | // Print the BusyBox links from a reference image, optionally with their owners |
| 2 | // in a signed Vinix root previously checked by tests/installed-root.v. |
| 3 | module main |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | fn release_links(archive string) ![]string { |
| 8 | listing := os.exec(['tar', '--numeric-owner', '--quoting-style=literal', '-tvf', archive]) |
| 9 | if listing.exit_code != 0 { |
| 10 | return error(listing.output.trim_space()) |
| 11 | } |
| 12 | mut links := map[string]string{} |
| 13 | for line in listing.output.split_into_lines() { |
| 14 | if !line.starts_with('l') || !line.contains(' -> ') { |
| 15 | continue |
| 16 | } |
| 17 | name := '/' + line.all_before(' -> ').all_after_last(' ').trim_string_left('./').trim_left('/') |
| 18 | target := line.all_after(' -> ') |
| 19 | links[name] = os.norm_path(if target.starts_with('/') { |
| 20 | target |
| 21 | } else { |
| 22 | os.join_path(os.dir(name), target) |
| 23 | }) |
| 24 | } |
| 25 | mut result := []string{} |
| 26 | for name, target in links { |
| 27 | if os.dir(name) !in ['/bin', '/sbin', '/usr/bin', '/usr/sbin'] { continue } |
| 28 | mut resolved := target |
| 29 | mut visited := map[string]bool{} |
| 30 | for (resolved in links) && !visited[resolved] { |
| 31 | visited[resolved] = true |
| 32 | resolved = links[resolved] |
| 33 | } |
| 34 | if resolved == '/bin/busybox' { result << name } |
| 35 | } |
| 36 | result.sort() |
| 37 | if result.len == 0 { return error('no BusyBox applet links in ${archive}') } |
| 38 | return result |
| 39 | } |
| 40 | |
| 41 | fn installed_owners(database string, paths []string) !map[string]string { |
| 42 | mut owners := map[string]string{} |
| 43 | for path in paths { owners[path] = '' } |
| 44 | for entry in os.ls(database)! { |
| 45 | if entry == 'ALPM_DB_VERSION' { continue } |
| 46 | dir := os.join_path(database, entry) |
| 47 | desc := '\n' + os.read_file(os.join_path(dir, 'desc'))! |
| 48 | if !desc.contains('\n%NAME%\n') { return error('missing package name in ${entry}') } |
| 49 | name := desc.all_after('\n%NAME%\n').all_before('\n') |
| 50 | files := os.read_file(os.join_path(dir, 'files'))! |
| 51 | if !files.starts_with('%FILES%\n') { return error('missing file list in ${entry}') } |
| 52 | for file in files.all_after('%FILES%\n').all_before('\n\n').split_into_lines() { |
| 53 | path := '/' + file |
| 54 | if path !in owners { continue } |
| 55 | if owners[path] != '' { return error('${path} is owned by both ${owners[path]} and ${name}') } |
| 56 | owners[path] = name |
| 57 | } |
| 58 | } |
| 59 | for path in paths { |
| 60 | if owners[path] == '' { return error('published BusyBox path has no package owner: ${path}') } |
| 61 | } |
| 62 | return owners |
| 63 | } |
| 64 | |
| 65 | fn main() { |
| 66 | if os.args.len !in [2, 3] { |
| 67 | eprintln('usage: release-links ROOT_TARBALL [INSTALLED_LOCAL_DB]') |
| 68 | exit(2) |
| 69 | } |
| 70 | links := release_links(os.args[1]) or { |
| 71 | eprintln(err) |
| 72 | exit(1) |
| 73 | } |
| 74 | if os.args.len == 3 { |
| 75 | owners := installed_owners(os.args[2], links) or { |
| 76 | eprintln(err) |
| 77 | exit(1) |
| 78 | } |
| 79 | for name in links { println('${name}\t${owners[name]}') } |
| 80 | } else { |
| 81 | for name in links { println(name) } |
| 82 | } |
| 83 | } |
| 84 | |