diff options
author | Nikias Bassen | 2012-02-09 10:50:24 +0100 |
---|---|---|
committer | Nikias Bassen | 2012-02-09 10:50:24 +0100 |
commit | c0f481c0bb576505f736437e587aca993727a714 (patch) | |
tree | 0d842ec260dfbc4d2bdd250c1af65a2555475452 | |
parent | a7dcea5c4cf995ec7fe40548afc781f7fd799210 (diff) | |
download | idevicerestore-c0f481c0bb576505f736437e587aca993727a714.tar.gz idevicerestore-c0f481c0bb576505f736437e587aca993727a714.tar.bz2 |
download: better error handling
-rw-r--r-- | src/download.c | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/download.c b/src/download.c index 211987a..bd794b1 100644 --- a/src/download.c +++ b/src/download.c @@ -43,6 +43,7 @@ static size_t download_write_buffer_callback(char* data, size_t size, size_t nme int download_to_buffer(const char* url, char** buf, uint32_t* length) { + int res = 0; curl_global_init(CURL_GLOBAL_ALL); CURL* handle = curl_easy_init(); if (handle == NULL) { @@ -58,23 +59,27 @@ int download_to_buffer(const char* url, char** buf, uint32_t* length) curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, (curl_write_callback)&download_write_buffer_callback); curl_easy_setopt(handle, CURLOPT_WRITEDATA, &response); curl_easy_setopt(handle, CURLOPT_USERAGENT, "InetURL/1.0"); + curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(handle, CURLOPT_URL, url); curl_easy_perform(handle); curl_easy_cleanup(handle); - if (response.content) { + if (response.length > 0) { *length = response.length; *buf = response.content; + } else { + res = -1; } curl_global_cleanup(); - return 0; + return res; } int download_to_file(const char* url, const char* filename) { + int res = 0; curl_global_init(CURL_GLOBAL_ALL); CURL* handle = curl_easy_init(); if (handle == NULL) { @@ -91,14 +96,21 @@ int download_to_file(const char* url, const char* filename) curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, (curl_write_callback)&fwrite); curl_easy_setopt(handle, CURLOPT_WRITEDATA, f); curl_easy_setopt(handle, CURLOPT_USERAGENT, "InetURL/1.0"); + curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(handle, CURLOPT_URL, url); curl_easy_perform(handle); curl_easy_cleanup(handle); + off_t sz = ftello(f); + if ((sz == 0) || (sz == (off_t)-1)) { + res = -1; + remove(filename); + } + fclose(f); curl_global_cleanup(); - return 0; + return res; } |