aboutsummaryrefslogtreecommitdiff
path: root/contrib/elf2dmp/download.c
blob: 902dc04ffa5c20824d6c6c3cd2841071da32bdfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 * 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"

int download_url(const char *name, const char *url)
{
    int err = 1;
    FILE *file;
    CURL *curl = curl_easy_init();

    if (!curl) {
        return 1;
    }

    file = fopen(name, "wb");
    if (!file) {
        goto out_curl;
    }

    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) {
        unlink(name);
        fclose(file);
    } else {
        err = fclose(file);
    }

out_curl:
    curl_easy_cleanup(curl);

    return err;
}