| 1 | // directory-relative operations keep local db writes within the opened entry. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | import crypto.rand |
| 6 | import os |
| 7 | |
| 8 | #include "@DIR/native_storage.h" |
| 9 | |
| 10 | @[typedef] |
| 11 | struct C.v_alpm_storage_info { |
| 12 | mut: |
| 13 | mode u32 |
| 14 | uid u32 |
| 15 | gid u32 |
| 16 | links u64 |
| 17 | } |
| 18 | |
| 19 | @[typedef] |
| 20 | struct C.DIR {} |
| 21 | |
| 22 | fn C.v_alpm_storage_stat(i32, &char, &C.v_alpm_storage_info) i32 |
| 23 | fn C.v_alpm_storage_next(&C.DIR, &&char) i32 |
| 24 | fn C.openat(i32, &char, i32, ...int) i32 |
| 25 | fn C.mkdirat(i32, &char, u32) i32 |
| 26 | fn C.renameat(i32, &char, i32, &char) i32 |
| 27 | fn C.fdopendir(i32) &C.DIR |
| 28 | fn C.closedir(&C.DIR) i32 |
| 29 | fn C.fsync(i32) i32 |
| 30 | fn C.faccessat(i32, &char, i32, i32) i32 |
| 31 | |
| 32 | fn local_entry_name(pkg &Package) !string { |
| 33 | if pkg.metadata_problems().len > 0 || pkg.name.contains('\x00') || pkg.version.contains_any('\x00\r\n') { |
| 34 | return error('invalid local database package identity') |
| 35 | } |
| 36 | return '${pkg.name}-${pkg.version}' |
| 37 | } |
| 38 | |
| 39 | fn (mut db Database) open_local_directory() !i32 { |
| 40 | db.check()! |
| 41 | if !db.is_local { return db.owner.fail(.wrong_args, 'database is not local') } |
| 42 | directory := db.path.trim_right('/') |
| 43 | fd := C.open(unsafe { &char(directory.str) }, C.O_RDONLY | C.O_DIRECTORY | C.O_NOFOLLOW | C.O_CLOEXEC) |
| 44 | if fd < 0 { return db.owner.fail(.db_open, os.last_error().msg()) } |
| 45 | return fd |
| 46 | } |
| 47 | |
| 48 | fn (mut db Database) close_local_descriptor(fd i32) { |
| 49 | if C.close(fd) != 0 { |
| 50 | db.owner.diagnostics << 'could not close local database descriptor: ${os.last_error().msg()}' |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn (mut db Database) prepare_local_package(pkg &Package) ! { |
| 55 | name := local_entry_name(pkg) or { return db.owner.fail(.db_write, err.msg()) } |
| 56 | fd := db.open_local_directory()! |
| 57 | defer { db.close_local_descriptor(fd) } |
| 58 | old_mask := C.umask(0) |
| 59 | status := C.mkdirat(fd, unsafe { &char(name.str) }, 0o755) |
| 60 | problem := os.last_error() |
| 61 | C.umask(old_mask) |
| 62 | if status != 0 { return db.owner.fail(.db_write, problem.msg()) } |
| 63 | } |
| 64 | |
| 65 | fn (mut db Database) open_local_entry(pkg &Package) !i32 { |
| 66 | name := local_entry_name(pkg) or { return db.owner.fail(.db_write, err.msg()) } |
| 67 | parent := db.open_local_directory()! |
| 68 | defer { db.close_local_descriptor(parent) } |
| 69 | fd := C.openat(parent, unsafe { &char(name.str) }, C.O_RDONLY | C.O_DIRECTORY | C.O_NOFOLLOW | C.O_CLOEXEC) |
| 70 | if fd < 0 { return db.owner.fail(.db_write, os.last_error().msg()) } |
| 71 | return fd |
| 72 | } |
| 73 | |
| 74 | fn (mut db Database) write_local_record(directory i32, name string, text string) ! { |
| 75 | mut info := C.v_alpm_storage_info{ mode: 0o644, uid: u32(C.geteuid()), gid: u32(C.getegid()), links: 1 } |
| 76 | mut existing := C.v_alpm_storage_info{} |
| 77 | if C.v_alpm_storage_stat(directory, unsafe { &char(name.str) }, &existing) == 0 { |
| 78 | if existing.mode & 0o170000 != 0o100000 || existing.links != 1 { |
| 79 | return db.owner.fail(.db_write, 'database record is not an unshared regular file') |
| 80 | } |
| 81 | // atomic replacement must still honor the original record's write permissions. |
| 82 | if C.faccessat(directory, unsafe { &char(name.str) }, C.W_OK, C.AT_EACCESS | C.AT_SYMLINK_NOFOLLOW) != 0 { |
| 83 | return db.owner.fail(.db_write, os.last_error().msg()) |
| 84 | } |
| 85 | info = existing |
| 86 | } else if C.errno != C.ENOENT { |
| 87 | return db.owner.fail(.db_write, os.last_error().msg()) |
| 88 | } |
| 89 | mut temporary := '' |
| 90 | mut fd := i32(-1) |
| 91 | for _ in 0 .. 16 { |
| 92 | nonce := rand.bytes(12) or { return db.owner.fail(.db_write, err.msg()) } |
| 93 | temporary = '.alpm-' + nonce.hex() |
| 94 | fd = C.openat(directory, unsafe { &char(temporary.str) }, C.O_WRONLY | C.O_CREAT | C.O_EXCL | C.O_NOFOLLOW | C.O_CLOEXEC, 0o600) |
| 95 | if fd >= 0 { break } |
| 96 | if C.errno != C.EEXIST { return db.owner.fail(.db_write, os.last_error().msg()) } |
| 97 | } |
| 98 | if fd < 0 { return db.owner.fail(.db_write, 'cannot reserve database record') } |
| 99 | mut committed := false |
| 100 | defer { |
| 101 | if fd >= 0 { db.close_local_descriptor(fd) } |
| 102 | if !committed && C.unlinkat(directory, unsafe { &char(temporary.str) }, 0) != 0 { |
| 103 | db.owner.diagnostics << 'could not remove temporary database record: ${os.last_error().msg()}' |
| 104 | } |
| 105 | } |
| 106 | mut offset := 0 |
| 107 | for offset < text.len { |
| 108 | written := C.write(fd, unsafe { text.str + offset }, uint(text.len - offset)) |
| 109 | if written < 0 && C.errno == C.EINTR { continue } |
| 110 | if written <= 0 { return db.owner.fail(.db_write, os.last_error().msg()) } |
| 111 | offset += int(written) |
| 112 | } |
| 113 | if C.fchown(fd, info.uid, info.gid) != 0 || C.fchmod(fd, info.mode & 0o777) != 0 || C.fsync(fd) != 0 { |
| 114 | return db.owner.fail(.db_write, os.last_error().msg()) |
| 115 | } |
| 116 | closed := C.close(fd) |
| 117 | fd = -1 |
| 118 | if closed != 0 { return db.owner.fail(.db_write, os.last_error().msg()) } |
| 119 | if C.renameat(directory, unsafe { &char(temporary.str) }, directory, unsafe { &char(name.str) }) != 0 { |
| 120 | return db.owner.fail(.db_write, os.last_error().msg()) |
| 121 | } |
| 122 | committed = true |
| 123 | if C.fsync(directory) != 0 { |
| 124 | db.owner.diagnostics << 'could not sync database directory: ${os.last_error().msg()}' |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | fn (mut db Database) write_local_package(mut pkg Package, description bool, files bool) ! { |
| 129 | fd := db.open_local_entry(pkg)! |
| 130 | defer { db.close_local_descriptor(fd) } |
| 131 | if description { |
| 132 | if pkg.origin == .local_db { |
| 133 | pkg.load_description() or { return db.owner.fail(.db_write, err.msg()) } |
| 134 | } |
| 135 | db.write_local_record(fd, 'desc', pkg.local_description())! |
| 136 | } |
| 137 | if files { |
| 138 | if pkg.origin == .local_db { |
| 139 | pkg.load_files() or { return db.owner.fail(.db_write, err.msg()) } |
| 140 | } |
| 141 | db.write_local_record(fd, 'files', pkg.local_files())! |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | fn (mut db Database) remove_local_package(pkg &Package) ! { |
| 146 | name := local_entry_name(pkg) or { return db.owner.fail(.db_remove, err.msg()) } |
| 147 | parent := db.open_local_directory()! |
| 148 | defer { db.close_local_descriptor(parent) } |
| 149 | fd := C.openat(parent, unsafe { &char(name.str) }, C.O_RDONLY | C.O_DIRECTORY | C.O_NOFOLLOW | C.O_CLOEXEC) |
| 150 | if fd < 0 { return db.owner.fail(.db_remove, os.last_error().msg()) } |
| 151 | dir := C.fdopendir(fd) |
| 152 | if dir == unsafe { nil } { |
| 153 | problem := os.last_error() |
| 154 | db.close_local_descriptor(fd) |
| 155 | return db.owner.fail(.db_remove, problem.msg()) |
| 156 | } |
| 157 | defer { |
| 158 | if C.closedir(dir) != 0 { |
| 159 | db.owner.diagnostics << 'could not close removed database entry: ${os.last_error().msg()}' |
| 160 | } |
| 161 | } |
| 162 | mut failure := '' |
| 163 | for { |
| 164 | mut pointer := &char(unsafe { nil }) |
| 165 | status := C.v_alpm_storage_next(dir, &pointer) |
| 166 | if status == 0 { break } |
| 167 | if status < 0 { |
| 168 | failure = os.last_error().msg() |
| 169 | break |
| 170 | } |
| 171 | entry := unsafe { cstring_to_vstring(pointer) } |
| 172 | if entry in ['.', '..'] { continue } |
| 173 | if C.unlinkat(fd, unsafe { &char(entry.str) }, 0) != 0 { failure = os.last_error().msg() } |
| 174 | } |
| 175 | if C.unlinkat(parent, unsafe { &char(name.str) }, C.AT_REMOVEDIR) != 0 { |
| 176 | failure = os.last_error().msg() |
| 177 | } |
| 178 | if failure != '' { return db.owner.fail(.db_remove, failure) } |
| 179 | } |
| 180 | |
| 181 | pub fn (mut pkg Package) set_reason(reason InstallReason) ! { |
| 182 | if voidptr(pkg) == unsafe { nil } { return AlpmError{ kind: .wrong_args } } |
| 183 | if pkg.owner == unsafe { nil } { return AlpmError{ kind: .wrong_args } } |
| 184 | pkg.owner.check()! |
| 185 | if pkg.origin != .local_db || pkg.database == unsafe { nil } || voidptr(pkg.database) != voidptr(pkg.owner.local) || !pkg.database.registered { |
| 186 | return pkg.owner.fail(.wrong_args, "package is not in this handle's local database") |
| 187 | } |
| 188 | pkg.load_description() or { return pkg.owner.fail(.db_write, err.msg()) } |
| 189 | if pkg.reason == reason { return } |
| 190 | mut candidate := Package{ ...pkg, reason: reason } |
| 191 | pkg.database.write_local_package(mut candidate, true, false) or { return pkg.owner.fail(.db_write, err.msg()) } |
| 192 | pkg.reason = reason |
| 193 | } |
| 194 | |