| 1 | // Schedule transfers and deliver callbacks on the calling thread. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | struct ScheduledDownload { |
| 8 | order int |
| 9 | payload &DownloadPayload |
| 10 | } |
| 11 | |
| 12 | fn compare_download_order(a &ScheduledDownload, b &ScheduledDownload) int { |
| 13 | if a.payload.request.max_size > b.payload.request.max_size { return -1 } |
| 14 | if a.payload.request.max_size < b.payload.request.max_size { return 1 } |
| 15 | return if a.order < b.order { |
| 16 | -1 |
| 17 | } else if a.order > b.order { |
| 18 | 1 |
| 19 | } else { |
| 20 | 0 |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | pub fn (mut handle Handle) download_files(requests []DownloadRequest, directory string) !DownloadReport { |
| 25 | handle.check()! |
| 26 | if handle.download_active { return handle.fail(.trans_not_null, 'download is active') } |
| 27 | if !os.is_dir(directory) { return handle.fail(.not_a_dir, directory) } |
| 28 | handle.validate_download_requests(requests)! |
| 29 | if !handle.sandbox_child && handle.uses_download_sandbox()! { |
| 30 | handle.download_active = true |
| 31 | handle.download_control.interrupted = 0 |
| 32 | defer { handle.download_active = false } |
| 33 | return handle.download_sandboxed(requests, directory) |
| 34 | } |
| 35 | handle.download_active = true |
| 36 | handle.download_control.interrupted = 0 |
| 37 | defer { handle.download_active = false } |
| 38 | if voidptr(handle.fetch_callback) != unsafe { nil } { |
| 39 | return handle.download_with_fetch_callback(requests, directory, directory) |
| 40 | } |
| 41 | mut all := []&DownloadPayload{} |
| 42 | mut queue := []ScheduledDownload{} |
| 43 | for index, request in requests { |
| 44 | payload := &DownloadPayload{ |
| 45 | handle: handle |
| 46 | request: DownloadRequest{ ...request, servers: request.servers.clone(), cache_servers: request.cache_servers.clone() } |
| 47 | destination: os.join_path(directory, request.filename) |
| 48 | partial: os.join_path(directory, request.filename + if request.temporary { |
| 49 | '' |
| 50 | } else { |
| 51 | '.part' |
| 52 | }) |
| 53 | } |
| 54 | all << payload |
| 55 | queue << ScheduledDownload{ order: index, payload: payload } |
| 56 | } |
| 57 | defer { |
| 58 | for mut payload in all { |
| 59 | $if nspkg_libcurl ? { |
| 60 | payload.detach_curl() |
| 61 | } |
| 62 | payload.close_file() or { handle.diagnostics << 'could not close download: ${err.msg()}' } |
| 63 | } |
| 64 | } |
| 65 | queue.sort_with_compare(compare_download_order) |
| 66 | transport := handle.new_download_transport() or { return handle.fail(.libcurl, err.msg()) } |
| 67 | mut active := 0 |
| 68 | mut next := 0 |
| 69 | mut failed := false |
| 70 | mut failure := Errno.retrieve |
| 71 | mut detail := '' |
| 72 | mut updated := false |
| 73 | max_streams := int(handle.opts.integers[.parallel_downloads]) |
| 74 | for active > 0 || (!failed && next < queue.len) { |
| 75 | for !failed && active < max_streams && next < queue.len { |
| 76 | mut payload := queue[next].payload |
| 77 | next++ |
| 78 | payload.start(transport) or { |
| 79 | failed = true |
| 80 | failure = unsafe { Errno(err.code()) } |
| 81 | detail = err.msg() |
| 82 | break |
| 83 | } |
| 84 | active++ |
| 85 | } |
| 86 | if active == 0 { break } |
| 87 | mut payload, transfer := transport.next() or { return handle.fail(.libcurl, err.msg()) } |
| 88 | completion := payload.finish(transfer, transport) or { |
| 89 | failed = true |
| 90 | failure = unsafe { Errno(err.code()) } |
| 91 | detail = err.msg() |
| 92 | $if nspkg_libcurl ? { |
| 93 | payload.detach_curl() |
| 94 | } |
| 95 | payload.state = .done |
| 96 | active-- |
| 97 | continue |
| 98 | } |
| 99 | if completion.retry { continue } |
| 100 | active-- |
| 101 | if completion.signature != unsafe { nil } { |
| 102 | mut signature := completion.signature |
| 103 | all << signature |
| 104 | signature.start(transport) or { |
| 105 | failed = true |
| 106 | failure = unsafe { Errno(err.code()) } |
| 107 | detail = err.msg() |
| 108 | payload.emit(.completed, 0, payload.transferred, payload.result) |
| 109 | continue |
| 110 | } |
| 111 | active++ |
| 112 | } |
| 113 | payload.emit(.completed, 0, payload.transferred, payload.result) |
| 114 | if payload.result == -1 && !payload.request.optional { |
| 115 | if !failed { |
| 116 | failure = payload.failure |
| 117 | detail = payload.detail |
| 118 | } |
| 119 | failed = true |
| 120 | } else if payload.result == 0 { |
| 121 | updated = true |
| 122 | } |
| 123 | } |
| 124 | if failed { return handle.fail(failure, detail) } |
| 125 | return DownloadReport{ |
| 126 | updated: updated |
| 127 | files: all.map(DownloadFile{ path: it.destination, result: it.result, optional: it.request.optional }) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | fn (mut handle Handle) validate_download_requests(requests []DownloadRequest) ! { |
| 132 | mut reserved := map[string]bool{} |
| 133 | for request in requests { |
| 134 | if !safe_download_name(request.filename) || request.max_size < 0 || (request.url == '') == (request.filepath == '') { |
| 135 | return handle.fail(.wrong_args, 'invalid download request') |
| 136 | } |
| 137 | if request.url != '' && (request.servers.len != 0 || request.cache_servers.len != 0) { |
| 138 | return handle.fail(.wrong_args, 'URL and mirror list are mutually exclusive') |
| 139 | } |
| 140 | mut names := [request.filename] |
| 141 | if !request.temporary { names << request.filename + '.part' } |
| 142 | if request.download_signature { |
| 143 | names << [request.filename + '.sig', request.filename + '.sig.part'] |
| 144 | } |
| 145 | for name in names { |
| 146 | if reserved[name] { |
| 147 | return handle.fail(.wrong_args, 'overlapping download destinations') |
| 148 | } |
| 149 | reserved[name] = true |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |