nspkg / native_handle.v
355 lines · 320 sloc · 11.26 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// public object identity and borrowed-list ownership live at the API boundary.
2module nspkg
3
4import nspkg.engine
5
6fn C.GC_add_roots(voidptr, voidptr)
7fn C.GC_remove_roots(voidptr, voidptr)
8
9fn (handle &Handle) native_handle() !&engine.Handle {
10 if handle == unsafe { nil } || handle.core == unsafe { nil } {
11 return Errno.handle_null.ierror()
12 }
13 mut core := unsafe { handle.core }
14 core.validate_handle()!
15 return core
16}
17
18fn (handle &Handle) native_handle_preserving_error() !&engine.Handle {
19 if handle == unsafe { nil } || handle.core == unsafe { nil } {
20 return Errno.handle_null.ierror()
21 }
22 mut core := unsafe { handle.core }
23 core.validate_handle_preserving_error()!
24 return core
25}
26
27fn (handle &Handle) native_failure(kind Errno, detail string) IError {
28 if handle == unsafe { nil } || handle.core == unsafe { nil } {
29 return Errno.handle_null.ierror()
30 }
31 mut core := unsafe { handle.core }
32 return core.set_error(unsafe { engine.Errno(kind) }, detail)
33}
34
35fn (mut handle Handle) native_retain() ! {
36 // libc lists do not participate in V's GC graph. retain the owning handle
37 // until explicit release, as the old manually allocated handle required.
38 $if gcboehm ? {
39 slot := unsafe { C.malloc(sizeof(voidptr)) }
40 if slot == unsafe { nil } { return Errno.memory.ierror() }
41 unsafe { *(&voidptr(slot)) = voidptr(handle) }
42 C.GC_add_roots(slot, unsafe { &u8(slot) + sizeof(voidptr) })
43 handle.root_anchor = slot
44 }
45}
46
47fn (mut handle Handle) native_unretain() {
48 $if gcboehm ? {
49 slot := handle.root_anchor
50 if slot != unsafe { nil } {
51 C.GC_remove_roots(slot, unsafe { &u8(slot) + sizeof(voidptr) })
52 C.free(slot)
53 handle.root_anchor = unsafe { nil }
54 }
55 }
56}
57
58fn (handle &Handle) native_database(core &engine.Database) &DB {
59 if core == unsafe { nil } { return unsafe { nil } }
60 mut h := unsafe { handle }
61 context := core.binding_context()
62 if context != unsafe { nil } { h = unsafe { &Handle(context) } }
63 key := u64(voidptr(core))
64 if found := h.databases[key] { return found }
65 db := &DB{ core: unsafe { core }, owner: h }
66 h.databases[key] = db
67 return db
68}
69
70fn (handle &Handle) native_package(core &engine.Package) &Package {
71 if core == unsafe { nil } { return unsafe { nil } }
72 mut h := unsafe { handle }
73 context := core.binding_context()
74 if context != unsafe { nil } { h = unsafe { &Handle(context) } }
75 key := u64(voidptr(core))
76 if found := h.packages[key] { return found }
77 pkg := &Package{ core: unsafe { core }, owner: h }
78 h.packages[key] = pkg
79 return pkg
80}
81
82fn (handle &Handle) native_package_list(values []&engine.Package) &List {
83 mut list := &List(unsafe { nil })
84 for value in values { list = list.add(handle.native_package(value)) }
85 return list
86}
87
88fn (handle &Handle) native_borrow_list(list &List) &List {
89 mut h := unsafe { handle }
90 if list != unsafe { nil } { h.owned_lists << unsafe { list } }
91 return unsafe { list }
92}
93
94// Wrapper identity depends on the ordered pointer sequence. The concatenation
95// owns the bytes, independently of any later changes to the input array.
96fn native_pointer_list_key[T](label string, values []&T) string {
97 if values.len == 0 { return label + '\x00' }
98 pointers := unsafe { tos(values.data, values.len * int(sizeof(voidptr))) }
99 return label + '\x00' + pointers
100}
101
102fn (handle &Handle) native_cached_packages(label string, values []&engine.Package) &List {
103 key := native_pointer_list_key(label, values)
104 if found := handle.lists[key] { return found }
105 list := handle.native_package_list(values)
106 mut h := unsafe { handle }
107 h.lists[key] = list
108 return handle.native_borrow_list(list)
109}
110
111fn (handle &Handle) native_database_list(values []&engine.Database) &List {
112 key := native_pointer_list_key('databases', values)
113 if found := handle.lists[key] { return found }
114 mut list := &List(unsafe { nil })
115 for value in values { list = list.add(handle.native_database(value)) }
116 mut h := unsafe { handle }
117 h.lists[key] = list
118 return handle.native_borrow_list(list)
119}
120
121fn (handle &Handle) native_group(value &engine.Group) &Group {
122 key := u64(voidptr(value))
123 if existing := handle.groups[key] { return existing }
124 mut h := unsafe { handle }
125 group := &Group{ name: handle.native_buffer(value.name), packages: handle.native_cached_packages('group:${value.name}', value.packages) }
126 h.groups[key] = group
127 return group
128}
129
130fn native_owned_strings(values []string) &List {
131 mut list := &List(unsafe { nil })
132 for value in values { (&list).append_strdup(value) }
133 return list
134}
135
136fn (handle &Handle) native_own_buffer(buffer voidptr) {
137 mut h := unsafe { handle }
138 if buffer != unsafe { nil } { h.owned_buffers << buffer }
139}
140
141fn (handle &Handle) native_buffer(text string) &u8 {
142 data := unsafe { C.strdup(text.str) }
143 handle.native_own_buffer(data)
144 return data
145}
146
147fn (handle &Handle) native_strings(label string, values []string) &List {
148 key := label + ':' + values.map('${it.len}:${it}').join('')
149 if found := handle.lists[key] { return found }
150 list := native_owned_strings(values)
151 for node := unsafe { list }; node != unsafe { nil }; node = node.next {
152 handle.native_own_buffer(node.data)
153 }
154 mut h := unsafe { handle }
155 h.lists[key] = list
156 return handle.native_borrow_list(list)
157}
158
159fn (handle &Handle) native_dependencies(label string, values []engine.Dependency) &List {
160 key := label + ':' + values.map(it.compute_string()).join('\x00')
161 if found := handle.lists[key] { return found }
162 mut list := &List(unsafe { nil })
163 mut h := unsafe { handle }
164 for value in values {
165 dependency := dep_from_string(value.compute_string())
166 h.owned_dependencies << dependency
167 list = list.add(dependency)
168 }
169 h.lists[key] = list
170 return handle.native_borrow_list(list)
171}
172
173fn (list &List) native_packages() []&engine.Package {
174 mut values := []&engine.Package{}
175 for node := unsafe { list }; node != unsafe { nil }; node = node.next {
176 if node.data != unsafe { nil } { values << unsafe { &Package(node.data) }.core }
177 }
178 return values
179}
180
181fn (list &List) native_databases() []&engine.Database {
182 mut values := []&engine.Database{}
183 for node := unsafe { list }; node != unsafe { nil }; node = node.next {
184 if node.data != unsafe { nil } { values << unsafe { &DB(node.data) }.core }
185 }
186 return values
187}
188
189fn (list &List) native_strings() []string {
190 mut values := []string{}
191 for node := unsafe { list }; node != unsafe { nil }; node = node.next {
192 if node.data != unsafe { nil } {
193 values << cstring_to_vstring_or_none(unsafe { &u8(node.data) })
194 }
195 }
196 return values
197}
198
199fn (list &List) native_dependencies() []engine.Dependency {
200 mut values := []engine.Dependency{}
201 for node := unsafe { list }; node != unsafe { nil }; node = node.next {
202 if node.data != unsafe { nil } {
203 values << engine_dependency(unsafe { &Depend(node.data) })
204 }
205 }
206 return values
207}
208
209fn native_missing_list(values []engine.MissingDependency) &List {
210 mut list := &List(unsafe { nil })
211 for value in values {
212 mut record := unsafe { &DepMissing(C.calloc(1, sizeof(DepMissing))) }
213 if record == unsafe { nil } { break }
214 record.target = unsafe { C.strdup(value.target.str) }
215 record.causingpkg = if value.causing_package == '' {
216 unsafe { nil }
217 } else {
218 unsafe { C.strdup(value.causing_package.str) }
219 }
220 record.depend = dep_from_string(value.dependency.compute_string())
221 list = list.add(record)
222 }
223 return list
224}
225
226fn (handle &Handle) native_conflicts(values []engine.PackageConflict) &List {
227 mut list := &List(unsafe { nil })
228 for value in values {
229 mut record := unsafe { &Conflict(C.calloc(1, sizeof(Conflict))) }
230 if record == unsafe { nil } { break }
231 record.package1 = handle.native_package(value.package1)
232 record.package2 = handle.native_package(value.package2)
233 record.reason = dep_from_string(value.reason.compute_string())
234 list = list.add(record)
235 }
236 return list
237}
238
239fn (pkg &Package) native_metadata(description bool, files bool) ! {
240 mut core := pkg.native_package()!
241 if description && core.origin == .local_db {
242 // be_local.c's LAZY_LOAD leaves partial fields available after a read error.
243 core.load_description() or { return }
244 }
245 if files && core.origin == .local_db {
246 core.load_files() or { return }
247 }
248}
249
250fn (pkg &Package) native_signature_path() !string {
251 mut core := pkg.owner.native_handle()!
252 // signing.c verifies pkg->filename, including a sync record's relative name.
253 if !pkg.core.has_filename { return core.set_error(.not_a_file, '') }
254 return pkg.core.filename
255}
256
257fn native_bytes(data []u8, length &uint) !&u8 {
258 if length == unsafe { nil } { return Errno.wrong_args.ierror() }
259 buffer := unsafe { &u8(C.malloc(uint(data.len + 1))) }
260 if buffer == unsafe { nil } { return Errno.memory.ierror() }
261 unsafe {
262 if data.len > 0 { vmemcpy(buffer, data.data, data.len) }
263 buffer[data.len] = 0
264 *length = uint(data.len)
265 }
266 return buffer
267}
268
269fn (handle &Handle) native_siglist(verification engine.SignatureVerification) ?&SigList {
270 values := verification.results
271 mut list := &SigList{ count: uint(values.len), results: unsafe { &SigResult(C.calloc(uint(values.len), sizeof(SigResult))) } }
272 if values.len > 0 && list.results == unsafe { nil } {
273 engine.release_signature_keys(verification.key_data)
274 mut core := unsafe { handle.core }
275 core.set_error(.memory, 'allocate signature results')
276 return none
277 }
278 // transfer every reference before an unknown-key fingerprint allocation can fail.
279 for index, value in values {
280 data := verification.key_data[index]
281 view := engine.signature_key_view(data)
282 unsafe {
283 list.results[index] = SigResult{
284 key: PGPKey{
285 data: data
286 fingerprint: view.fingerprint
287 uid: view.uid
288 name: view.name
289 email: view.email
290 created: value.key.created
291 expires: value.key.expires
292 length: value.key.length
293 revoked: u32(value.key.revoked)
294 }
295 status: SigStatus(value.status)
296 validity: SigValidity(value.validity)
297 }
298 }
299 }
300 for index, value in values {
301 if verification.key_data[index] == unsafe { nil } {
302 buffer := unsafe { C.strdup(value.key.fingerprint.str) }
303 if buffer == unsafe { nil } {
304 mut core := unsafe { handle.core }
305 list.cleanup() or {
306 core.set_error(.memory, 'allocate signature fingerprint; cleanup: ' + err.msg())
307 return none
308 }
309 core.set_error(.memory, 'allocate signature fingerprint')
310 return none
311 }
312 unsafe { list.results[index].key.fingerprint = buffer }
313 }
314 }
315 return list
316}
317
318struct NativeFiles {
319 values []File
320}
321
322fn (storage &NativeFiles) matches(files []engine.PackageFile) bool {
323 if storage.values.len != files.len { return false }
324 for index, file in files {
325 value := storage.values[index]
326 if value.name != file.name.str || value.size != file.size || value.mode != file.mode {
327 return false
328 }
329 }
330 return true
331}
332
333fn (db &DB) native_database() !&engine.Database {
334 if db == unsafe { nil } || db.core == unsafe { nil } { return Errno.db_null.ierror() }
335 db.owner.native_handle()!
336 mut core := unsafe { db.core }
337 core.validate_database()!
338 return core
339}
340
341fn (pkg &Package) native_package() !&engine.Package {
342 if pkg == unsafe { nil } || pkg.core == unsafe { nil } { return Errno.wrong_args.ierror() }
343 pkg.owner.native_handle()!
344 return pkg.core
345}
346
347fn (db &DB) native_database_preserving_error() !&engine.Database {
348 if db == unsafe { nil } || db.core == unsafe { nil } { return Errno.db_null.ierror() }
349 if db.owner == unsafe { nil } || db.owner.core == unsafe { nil } {
350 return Errno.handle_null.ierror()
351 }
352 mut core := unsafe { db.core }
353 core.validate_database_preserving_error()!
354 return core
355}
356