// the parent owns callbacks and cache writes; the child owns only the transfer. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os struct SandboxOutcome { report DownloadReport failure string signal i32 interrupted bool } fn (mut handle Handle) download_sandboxed(requests []DownloadRequest, directory string) !DownloadReport { username := handle.opts.strings[.sandbox_user] account := C.getpwnam(unsafe { &char(username.str) }) if account == unsafe { nil } { return handle.fail(.retrieve_prepare, 'sandbox user does not exist: ${username}') } mut stage := create_sandbox_stage(directory, account.pw_uid, account.pw_gid) or { return handle.fail(.retrieve_prepare, err.msg()) } mut finalized := false mut child_signal := i32(0) defer { if !finalized { stage.finalize() or { handle.diagnostics << 'could not restore sandbox downloads: ${err.msg()}' } } stage.cleanup() or { handle.diagnostics << 'could not remove download sandbox ${stage.directory}: ${err.msg()}' } handle.diagnostics << stage.diagnostics if child_signal in [C.SIGINT, C.SIGQUIT] { C.kill(C.getpid(), child_signal) } } stage.prepare(requests) or { return handle.fail(.retrieve_prepare, err.msg()) } mut report := DownloadReport{} mut failure := '' mut failure_code := Errno.retrieve if voidptr(handle.fetch_callback) != unsafe { nil } { // fetch callbacks are trusted caller code, and run in the parent as in dload.c. report = handle.download_with_fetch_callback(stage.requests, stage.directory, stage.parent) or { failure = err.msg() failure_code = unsafe { Errno(err.code()) } DownloadReport{} } } else { outcome := handle.run_download_worker(stage) or { return handle.fail(.retrieve_prepare, err.msg()) } report = outcome.report failure = outcome.failure child_signal = outcome.signal if outcome.interrupted { failure_code = .trans_abort } } finalized = true stage.finalize() or { if failure == '' { return handle.fail(.retrieve, err.msg()) } handle.diagnostics << 'could not restore sandbox downloads: ${err.msg()}' } if failure != '' { return handle.fail(failure_code, failure) } return DownloadReport{ updated: report.updated, files: report.files.map(DownloadFile{ ...it, path: os.join_path(directory, os.file_name(it.path)) }) } }