2018-08-29 15:41:25 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Virtuozzo International GmbH
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "download.h"
|
|
|
|
|
2024-03-07 13:20:49 +03:00
|
|
|
bool download_url(const char *name, const char *url)
|
2018-08-29 15:41:25 +03:00
|
|
|
{
|
2024-03-07 13:20:49 +03:00
|
|
|
bool success = false;
|
2018-08-29 15:41:25 +03:00
|
|
|
FILE *file;
|
|
|
|
CURL *curl = curl_easy_init();
|
|
|
|
|
|
|
|
if (!curl) {
|
2024-03-07 13:20:49 +03:00
|
|
|
return false;
|
2018-08-29 15:41:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
file = fopen(name, "wb");
|
|
|
|
if (!file) {
|
|
|
|
goto out_curl;
|
|
|
|
}
|
|
|
|
|
2021-09-20 11:54:32 +03:00
|
|
|
if (curl_easy_setopt(curl, CURLOPT_URL, url) != CURLE_OK
|
|
|
|
|| curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL) != CURLE_OK
|
|
|
|
|| curl_easy_setopt(curl, CURLOPT_WRITEDATA, file) != CURLE_OK
|
|
|
|
|| curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1) != CURLE_OK
|
|
|
|
|| curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0) != CURLE_OK
|
|
|
|
|| curl_easy_perform(curl) != CURLE_OK) {
|
2018-08-29 15:41:25 +03:00
|
|
|
unlink(name);
|
2021-09-20 11:54:32 +03:00
|
|
|
fclose(file);
|
|
|
|
} else {
|
2024-03-07 13:20:49 +03:00
|
|
|
success = !fclose(file);
|
2018-08-29 15:41:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
out_curl:
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
2024-03-07 13:20:49 +03:00
|
|
|
return success;
|
2018-08-29 15:41:25 +03:00
|
|
|
}
|