xref: /OK3568_Linux_fs/external/recovery/update_engine/download.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2023 Rockchip Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <curl/curl.h>
19 #include <string.h>
20 #include <stdbool.h>
21 #include "log.h"
22 
my_write_func(void * ptr,size_t size,size_t nmemb,FILE * stream)23 size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
24 {
25     return fwrite(ptr, size, nmemb, stream);
26 }
27 
28 extern double processvalue;
29 static int down_processvalue;
30 
my_progress_func(char * progress_data,double t,double d,double ultotal,double ulnow)31 int my_progress_func(char *progress_data,
32                      double t, /* dltotal */
33                      double d, /* dlnow */
34                      double ultotal,
35                      double ulnow)
36 {
37     processvalue = ulnow / ultotal * 100 / 110;
38     //LOGI("ultotal is %f, ulnow is %f, t is %f, d is %f\n", ultotal, ulnow, t, d);
39     if (down_processvalue != (int)(d / t * 100)) {
40         down_processvalue = (int)(d / t * 100);
41         LOGI("down_processvalue is %d%\n", down_processvalue);
42     }
43 
44     return 0;
45 }
46 
download_file(char * url,char const * output_filename)47 int download_file(char *url, char const *output_filename)
48 {
49     if (strncmp(url, "http", 4) != 0) {
50         LOGI("where the file is local.\n");
51         return -2;
52     }
53     CURL *curl;
54     CURLcode res;
55     FILE *outfile;
56     char const *progress_data = "* ";
57     down_processvalue = -1;
58 
59     curl = curl_easy_init();
60     if (curl) {
61         outfile = fopen(output_filename, "wb");
62 
63         curl_easy_setopt(curl, CURLOPT_URL, url);
64         curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
65         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
66         curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
67         curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
68         curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, progress_data);
69 
70         res = curl_easy_perform(curl);
71 
72         if (res != CURLE_OK)
73             LOGE("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
74         fclose(outfile);
75         /* always cleanup */
76         curl_easy_cleanup(curl);
77     }
78     if (res != CURLE_OK) {
79         LOGE("download Error.\n");
80         return -1;
81     }
82     return 0;
83 }
84 
85