From aa00911d12198f1772905c319c0a073f87984883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Mon, 13 Apr 2026 11:18:23 +0200 Subject: [PATCH] common : add download cancellation and temp file cleanup (#21813) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Adrien Gallouët --- common/download.cpp | 12 ++++++++++++ common/download.h | 1 + 2 files changed, 13 insertions(+) diff --git a/common/download.cpp b/common/download.cpp index ccf6fb6867..0e0034e1da 100644 --- a/common/download.cpp +++ b/common/download.cpp @@ -258,6 +258,9 @@ static bool common_pull_file(httplib::Client & cli, if (progress_step >= p.total / 1000 || p.downloaded == p.total) { if (callback) { callback->on_update(p); + if (callback->is_cancelled()) { + return false; + } } progress_step = 0; } @@ -373,6 +376,9 @@ static int common_download_file_single_online(const std::string & url, } for (int i = 0; i < max_attempts; ++i) { + if (opts.callback && opts.callback->is_cancelled()) { + break; + } if (i) { LOG_WRN("%s: retrying after %d seconds...\n", __func__, delay); std::this_thread::sleep_for(std::chrono::seconds(delay)); @@ -412,6 +418,12 @@ static int common_download_file_single_online(const std::string & url, if (opts.callback) { opts.callback->on_done(p, success); } + if (opts.callback && opts.callback->is_cancelled() && + std::filesystem::exists(path_temporary)) { + if (remove(path_temporary.c_str()) != 0) { + LOG_ERR("%s: unable to delete temporary file: %s\n", __func__, path_temporary.c_str()); + } + } if (!success) { LOG_ERR("%s: download failed after %d attempts\n", __func__, max_attempts); return -1; // max attempts reached diff --git a/common/download.h b/common/download.h index 48d5ff8a01..edc3e9f1a7 100644 --- a/common/download.h +++ b/common/download.h @@ -21,6 +21,7 @@ public: virtual void on_start(const common_download_progress & p) = 0; virtual void on_update(const common_download_progress & p) = 0; virtual void on_done(const common_download_progress & p, bool ok) = 0; + virtual bool is_cancelled() const { return false; } }; struct common_remote_params {