nspkg / engine / download_fetch.v
80 lines · 74 sloc · 3.11 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// external fetch return/companion policy follows pacman dload.c.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5import os
6
7pub struct FetchRequest {
8pub:
9 url string
10 directory string
11 filename string
12 force bool
13}
14
15pub type FetchCallback = fn (voidptr, FetchRequest) int
16
17pub fn (mut handle Handle) set_fetch_callback(callback FetchCallback, context voidptr) ! {
18 handle.check()!
19 handle.fetch_callback = callback
20 handle.fetch_context = context
21}
22
23fn (mut handle Handle) invoke_fetch(request FetchRequest, current_directory string) !int {
24 if handle.download_control.interrupted != 0 {
25 return handle.fail(.trans_abort, 'download interrupted')
26 }
27 callback := handle.fetch_callback
28 if voidptr(callback) == unsafe { nil } {
29 return handle.fail(.external_download, 'fetch callback was removed during retrieval')
30 }
31 result := callback(handle.fetch_context, request)
32 if result !in [-1, 0, 1] {
33 return handle.fail(.external_download, 'fetch callback returned an invalid status')
34 }
35 if result != -1 {
36 mut path := request.directory + request.filename
37 // a current response may leave the parent cache untouched while staging is empty.
38 if result == 1 && !os.exists(path) {
39 path = os.join_path(current_directory, request.filename)
40 }
41 info := os.stat(path) or { return handle.fail(.external_download, 'fetch callback did not supply ${request.filename}') }
42 if info.mode & 0o170000 != 0o100000 {
43 return handle.fail(.external_download, 'fetch callback supplied a non-regular file')
44 }
45 }
46 return result
47}
48
49fn (mut handle Handle) download_with_fetch_callback(requests []DownloadRequest, directory string, current_directory string) !DownloadReport {
50 localpath := directory_option(directory) or { return handle.fail(.wrong_args, err.msg()) }
51 mut files := []DownloadFile{}
52 mut updated := false
53 for request in requests {
54 mut status := -1
55 mut url := request.url
56 if url != '' {
57 status = handle.invoke_fetch(FetchRequest{ url: url, directory: localpath, filename: request.filename, force: request.force }, current_directory)!
58 } else {
59 mut servers := request.cache_servers.clone()
60 servers << request.servers
61 for server in servers {
62 url = server + '/' + request.filepath
63 status = handle.invoke_fetch(FetchRequest{ url: url, directory: localpath, filename: request.filename, force: request.force }, current_directory)!
64 if status != -1 { break }
65 }
66 }
67 mut sig_status := -1
68 if status != -1 && request.download_signature {
69 sig_status = handle.invoke_fetch(FetchRequest{ url: url + '.sig', directory: localpath, filename: request.filename + '.sig', force: request.force }, current_directory)!
70 if !request.signature_optional { status = sig_status }
71 }
72 if status == -1 && !request.optional { return handle.fail(.external_download, url) }
73 if status == 0 { updated = true }
74 files << DownloadFile{ path: localpath + request.filename, result: status, optional: request.optional }
75 if request.download_signature {
76 files << DownloadFile{ path: localpath + request.filename + '.sig', result: sig_status, optional: request.signature_optional }
77 }
78 }
79 return DownloadReport{ updated: updated, files: files }
80}
81