13fa2d384SViktor Prutyanov /*
23fa2d384SViktor Prutyanov * Copyright (c) 2018 Virtuozzo International GmbH
33fa2d384SViktor Prutyanov *
43fa2d384SViktor Prutyanov * This work is licensed under the terms of the GNU GPL, version 2 or later.
53fa2d384SViktor Prutyanov *
63fa2d384SViktor Prutyanov */
73fa2d384SViktor Prutyanov
83fa2d384SViktor Prutyanov #include "qemu/osdep.h"
93fa2d384SViktor Prutyanov #include <curl/curl.h>
103fa2d384SViktor Prutyanov #include "download.h"
113fa2d384SViktor Prutyanov
download_url(const char * name,const char * url)12*1b806c36SAkihiko Odaki bool download_url(const char *name, const char *url)
133fa2d384SViktor Prutyanov {
14*1b806c36SAkihiko Odaki bool success = false;
153fa2d384SViktor Prutyanov FILE *file;
163fa2d384SViktor Prutyanov CURL *curl = curl_easy_init();
173fa2d384SViktor Prutyanov
183fa2d384SViktor Prutyanov if (!curl) {
19*1b806c36SAkihiko Odaki return false;
203fa2d384SViktor Prutyanov }
213fa2d384SViktor Prutyanov
223fa2d384SViktor Prutyanov file = fopen(name, "wb");
233fa2d384SViktor Prutyanov if (!file) {
243fa2d384SViktor Prutyanov goto out_curl;
253fa2d384SViktor Prutyanov }
263fa2d384SViktor Prutyanov
27e59a7e0eSPeter Maydell if (curl_easy_setopt(curl, CURLOPT_URL, url) != CURLE_OK
28e59a7e0eSPeter Maydell || curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL) != CURLE_OK
29e59a7e0eSPeter Maydell || curl_easy_setopt(curl, CURLOPT_WRITEDATA, file) != CURLE_OK
30e59a7e0eSPeter Maydell || curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1) != CURLE_OK
31e59a7e0eSPeter Maydell || curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0) != CURLE_OK
32e59a7e0eSPeter Maydell || curl_easy_perform(curl) != CURLE_OK) {
333fa2d384SViktor Prutyanov unlink(name);
34e59a7e0eSPeter Maydell fclose(file);
35e59a7e0eSPeter Maydell } else {
36*1b806c36SAkihiko Odaki success = !fclose(file);
37e59a7e0eSPeter Maydell }
383fa2d384SViktor Prutyanov
393fa2d384SViktor Prutyanov out_curl:
403fa2d384SViktor Prutyanov curl_easy_cleanup(curl);
413fa2d384SViktor Prutyanov
42*1b806c36SAkihiko Odaki return success;
433fa2d384SViktor Prutyanov }
44