diff options
author | 2025-06-23 14:00:10 +0200 | |
---|---|---|
committer | 2025-06-23 14:00:10 +0200 | |
commit | 8061f08b4e0a8f0ab5d1548b7e9978f3cc8647a2 (patch) | |
tree | 5e01ac6e10a00c065dc5edf80adbd2ee4ce273e3 /src/download.c | |
parent | a5905b7f905fc3cc83033ebd963f0dcba071e512 (diff) | |
download | idevicerestore-8061f08b4e0a8f0ab5d1548b7e9978f3cc8647a2.tar.gz idevicerestore-8061f08b4e0a8f0ab5d1548b7e9978f3cc8647a2.tar.bz2 |
Refactor logging and add logfile support
idevicerestore will now also create a logfile automatically, unless
disabled with --logfile=NONE.
Diffstat (limited to 'src/download.c')
-rw-r--r-- | src/download.c | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/src/download.c b/src/download.c index a258da2..d9fac45 100644 --- a/src/download.c +++ b/src/download.c @@ -48,7 +48,7 @@ int download_to_buffer(const char* url, char** buf, uint32_t* length) int res = 0; CURL* handle = curl_easy_init(); if (handle == NULL) { - error("ERROR: could not initialize CURL\n"); + logger(LL_ERROR, "could not initialize CURL\n"); return -1; } @@ -57,7 +57,7 @@ int download_to_buffer(const char* url, char** buf, uint32_t* length) response.content = malloc(1); response.content[0] = '\0'; - if (idevicerestore_debug) + if (log_level >= LL_DEBUG) curl_easy_setopt(handle, CURLOPT_VERBOSE, 1); /* disable SSL verification to allow download from untrusted https locations */ @@ -86,17 +86,14 @@ int download_to_buffer(const char* url, char** buf, uint32_t* length) return res; } -static int lastprogress = 0; - static int download_progress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) { - double p = (dlnow / dltotal) * 100; + double p = (dlnow / dltotal); + + set_progress('DNLD', p); - if (p < 100.0) { - if ((int)p > lastprogress) { - info("downloading: %d%%\n", (int)p); - lastprogress = (int)p; - } + if (global_quit_flag > 0) { + return 1; } return 0; @@ -107,19 +104,17 @@ int download_to_file(const char* url, const char* filename, int enable_progress) int res = 0; CURL* handle = curl_easy_init(); if (handle == NULL) { - error("ERROR: could not initialize CURL\n"); + logger(LL_ERROR, "Could not initialize CURL\n"); return -1; } FILE* f = fopen(filename, "wb"); if (!f) { - error("ERROR: cannot open '%s' for writing\n", filename); + logger(LL_ERROR, "Cannot open '%s' for writing\n", filename); return -1; } - lastprogress = 0; - - if (idevicerestore_debug) + if (log_level >= LL_DEBUG) curl_easy_setopt(handle, CURLOPT_VERBOSE, 1); /* disable SSL verification to allow download from untrusted https locations */ @@ -128,8 +123,10 @@ int download_to_file(const char* url, const char* filename, int enable_progress) curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, NULL); curl_easy_setopt(handle, CURLOPT_WRITEDATA, f); - if (enable_progress > 0) + if (enable_progress > 0) { + register_progress('DNLD', "Downloading"); curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, (curl_progress_callback)&download_progress); + } curl_easy_setopt(handle, CURLOPT_NOPROGRESS, enable_progress > 0 ? 0: 1); curl_easy_setopt(handle, CURLOPT_USERAGENT, USER_AGENT_STRING); @@ -137,6 +134,11 @@ int download_to_file(const char* url, const char* filename, int enable_progress) curl_easy_setopt(handle, CURLOPT_URL, url); curl_easy_perform(handle); + + if (enable_progress) { + finalize_progress('DNLD'); + } + curl_easy_cleanup(handle); #ifdef WIN32 @@ -151,6 +153,9 @@ int download_to_file(const char* url, const char* filename, int enable_progress) res = -1; remove(filename); } + if (global_quit_flag > 0) { + res = -2; + } return res; } |