]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
common : add download cancellation and temp file cleanup (#21813)
authorAdrien Gallouët <redacted>
Mon, 13 Apr 2026 09:18:23 +0000 (11:18 +0200)
committerGitHub <redacted>
Mon, 13 Apr 2026 09:18:23 +0000 (11:18 +0200)
Signed-off-by: Adrien Gallouët <redacted>
common/download.cpp
common/download.h

index ccf6fb68675a57073646cbe69a8bd850fadd1f25..0e0034e1da3193b38820fc517bf2c4ef66f378ba 100644 (file)
@@ -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
index 48d5ff8a01374da4814730ed7ca08b7044e736d7..edc3e9f1a71f6e70408c6473cbfb6002c5b889ff 100644 (file)
@@ -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 {