]>
Commit | Line | Data |
---|---|---|
3fa2d384 VP |
1 | /* |
2 | * Copyright (c) 2018 Virtuozzo International GmbH | |
3 | * | |
4 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
5 | * | |
6 | */ | |
7 | ||
8 | #include "qemu/osdep.h" | |
9 | #include <curl/curl.h> | |
10 | #include "download.h" | |
11 | ||
1b806c36 | 12 | bool download_url(const char *name, const char *url) |
3fa2d384 | 13 | { |
1b806c36 | 14 | bool success = false; |
3fa2d384 VP |
15 | FILE *file; |
16 | CURL *curl = curl_easy_init(); | |
17 | ||
18 | if (!curl) { | |
1b806c36 | 19 | return false; |
3fa2d384 VP |
20 | } |
21 | ||
22 | file = fopen(name, "wb"); | |
23 | if (!file) { | |
3fa2d384 VP |
24 | goto out_curl; |
25 | } | |
26 | ||
e59a7e0e PM |
27 | if (curl_easy_setopt(curl, CURLOPT_URL, url) != CURLE_OK |
28 | || curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL) != CURLE_OK | |
29 | || curl_easy_setopt(curl, CURLOPT_WRITEDATA, file) != CURLE_OK | |
30 | || curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1) != CURLE_OK | |
31 | || curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0) != CURLE_OK | |
32 | || curl_easy_perform(curl) != CURLE_OK) { | |
3fa2d384 | 33 | unlink(name); |
e59a7e0e | 34 | fclose(file); |
e59a7e0e | 35 | } else { |
1b806c36 | 36 | success = !fclose(file); |
3fa2d384 VP |
37 | } |
38 | ||
3fa2d384 VP |
39 | out_curl: |
40 | curl_easy_cleanup(curl); | |
41 | ||
1b806c36 | 42 | return success; |
3fa2d384 | 43 | } |