// external fetch return/companion policy follows pacman dload.c. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os pub struct FetchRequest { pub: url string directory string filename string force bool } pub type FetchCallback = fn (voidptr, FetchRequest) int pub fn (mut handle Handle) set_fetch_callback(callback FetchCallback, context voidptr) ! { handle.check()! handle.fetch_callback = callback handle.fetch_context = context } fn (mut handle Handle) invoke_fetch(request FetchRequest, current_directory string) !int { if handle.download_control.interrupted != 0 { return handle.fail(.trans_abort, 'download interrupted') } callback := handle.fetch_callback if voidptr(callback) == unsafe { nil } { return handle.fail(.external_download, 'fetch callback was removed during retrieval') } result := callback(handle.fetch_context, request) if result !in [-1, 0, 1] { return handle.fail(.external_download, 'fetch callback returned an invalid status') } if result != -1 { mut path := request.directory + request.filename // a current response may leave the parent cache untouched while staging is empty. if result == 1 && !os.exists(path) { path = os.join_path(current_directory, request.filename) } info := os.stat(path) or { return handle.fail(.external_download, 'fetch callback did not supply ${request.filename}') } if info.mode & 0o170000 != 0o100000 { return handle.fail(.external_download, 'fetch callback supplied a non-regular file') } } return result } fn (mut handle Handle) download_with_fetch_callback(requests []DownloadRequest, directory string, current_directory string) !DownloadReport { localpath := directory_option(directory) or { return handle.fail(.wrong_args, err.msg()) } mut files := []DownloadFile{} mut updated := false for request in requests { mut status := -1 mut url := request.url if url != '' { status = handle.invoke_fetch(FetchRequest{ url: url, directory: localpath, filename: request.filename, force: request.force }, current_directory)! } else { mut servers := request.cache_servers.clone() servers << request.servers for server in servers { url = server + '/' + request.filepath status = handle.invoke_fetch(FetchRequest{ url: url, directory: localpath, filename: request.filename, force: request.force }, current_directory)! if status != -1 { break } } } mut sig_status := -1 if status != -1 && request.download_signature { sig_status = handle.invoke_fetch(FetchRequest{ url: url + '.sig', directory: localpath, filename: request.filename + '.sig', force: request.force }, current_directory)! if !request.signature_optional { status = sig_status } } if status == -1 && !request.optional { return handle.fail(.external_download, url) } if status == 0 { updated = true } files << DownloadFile{ path: localpath + request.filename, result: status, optional: request.optional } if request.download_signature { files << DownloadFile{ path: localpath + request.filename + '.sig', result: sig_status, optional: request.signature_optional } } } return DownloadReport{ updated: updated, files: files } }