| 1 | module main |
| 2 | |
| 3 | import nspkg.engine |
| 4 | import os |
| 5 | |
| 6 | fn command(args []string) ! { |
| 7 | mut process := os.new_process(args[0]) |
| 8 | process.set_args(args[1..]) |
| 9 | process.set_redirect_stdio() |
| 10 | process.run() |
| 11 | defer { process.close() } |
| 12 | output := process.stdout_slurp() |
| 13 | diagnostic := process.stderr_slurp() |
| 14 | process.wait() |
| 15 | if process.code != 0 { return error('${args[0]} failed: ${diagnostic}${output}') } |
| 16 | } |
| 17 | |
| 18 | fn main() { |
| 19 | run() or { |
| 20 | eprintln(err) |
| 21 | exit(1) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | fn run() ! { |
| 26 | if os.args.len != 3 { return error('expected base and parent mount namespace') } |
| 27 | if os.readlink('/proc/self/ns/mnt')! == os.args[2] { |
| 28 | return error('requires a private mount namespace') |
| 29 | } |
| 30 | command(['mount', '--make-rprivate', '/'])! |
| 31 | base := os.args[1] |
| 32 | for readonly in [true, false] { |
| 33 | root := os.join_path(base, readonly.str(), 'root') |
| 34 | dbpath := os.join_path(base, readonly.str(), 'db') |
| 35 | mountpoint := os.join_path(root, 'payload') |
| 36 | entry := os.join_path(dbpath, 'local', 'app-1-1') |
| 37 | os.mkdir_all(mountpoint)! |
| 38 | os.mkdir_all(entry)! |
| 39 | os.write_file(os.join_path(dbpath, 'local', 'ALPM_DB_VERSION'), '9\n')! |
| 40 | os.write_file(os.join_path(entry, 'desc'), '%DESC%\nApp\n\n')! |
| 41 | os.write_file(os.join_path(entry, 'files'), if readonly { |
| 42 | '%FILES%\npayload/file\n\n' |
| 43 | } else { |
| 44 | '%FILES%\npayload/\n\n' |
| 45 | })! |
| 46 | if readonly { |
| 47 | os.write_file(os.join_path(mountpoint, 'file'), 'retained')! |
| 48 | command(['mount', '--bind', mountpoint, mountpoint])! |
| 49 | command(['mount', '-o', 'remount,bind,ro', mountpoint])! |
| 50 | } else { |
| 51 | command(['mount', '-t', 'tmpfs', '-o', 'size=1m', 'tmpfs', mountpoint])! |
| 52 | } |
| 53 | mut handle := engine.initialize(root, dbpath)! |
| 54 | defer { handle.release() or { eprintln(err) } } |
| 55 | mut db := handle.local_database()! |
| 56 | pkg := db.get_pkg('app') or { return error('missing package') } |
| 57 | handle.start_transaction(.no_hooks | .no_scriptlet)! |
| 58 | defer { handle.release_transaction() or { eprintln(err) } } |
| 59 | handle.remove_package(pkg)! |
| 60 | assert handle.prepare_transaction()!.success |
| 61 | if readonly { |
| 62 | if _ := handle.commit_transaction() { |
| 63 | return error('read-only filesystem removal succeeded') |
| 64 | } |
| 65 | assert handle.errno() == .pkg_cant_remove |
| 66 | assert os.read_file(os.join_path(mountpoint, 'file'))! == 'retained' |
| 67 | assert db.get_pkg('app') != none |
| 68 | } else { |
| 69 | handle.commit_transaction()! |
| 70 | assert os.is_dir(mountpoint) |
| 71 | assert db.get_pkg('app') == none |
| 72 | } |
| 73 | command(['umount', mountpoint])! |
| 74 | } |
| 75 | println('native mountpoint and read-only removal verified') |
| 76 | } |
| 77 | |