Skip to content

Commit 9886d5c

Browse files
committed
support CURLOPT_XFERINFOFUNCTION, add callback wrapper for both methods
1 parent 85e2ead commit 9886d5c

File tree

11 files changed

+92
-45
lines changed

11 files changed

+92
-45
lines changed

src/Get.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int Get::install(Package& package, bool resume)
7272
printf("--> Downloaded [%s] to sdroot/\n", package.getPackageName().c_str());
7373

7474
// clear any progress callbacks before updating repo metadata
75-
extern int (*networking_callback)(void*, double, double, double, double);
75+
extern libget_progress_callback_t networking_callback;
7676
extern void* networking_callback_data;
7777
networking_callback = nullptr;
7878
networking_callback_data = nullptr;

src/Package.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ bool Package::install(const std::string& pkg_path, const std::string& tmp_path)
201201
libget_status_callback(STATUS_ANALYZING, 1, 1);
202202

203203
if (networking_callback != nullptr)
204-
networking_callback(nullptr, 10, 10, 0, 0);
204+
networking_callback(nullptr, 1.0);
205205

206206
std::string downloadedFilePath = tmp_path + this->pkg_name + getUrlFileExt();
207207

@@ -349,8 +349,10 @@ bool Package::install(const std::string& pkg_path, const std::string& tmp_path)
349349
const auto& entries = manifest.getEntries();
350350
for (const auto& entry : entries)
351351
{
352-
if (networking_callback != nullptr)
353-
networking_callback(nullptr, entries.size(), i + 1, 0, 0);
352+
if (networking_callback != nullptr) {
353+
double progress = (double)(i + 1) / (double)entries.size();
354+
networking_callback(nullptr, progress);
355+
}
354356

355357
i++;
356358

@@ -480,8 +482,10 @@ bool Package::remove(std::string_view pkg_path)
480482
const auto& entries = manifest.getEntries();
481483
for (const auto& entry : entries)
482484
{
483-
if (networking_callback != nullptr)
484-
networking_callback(nullptr, entries.size(), i + 1, 0, 0);
485+
if (networking_callback != nullptr) {
486+
double progress = (double)(i + 1) / (double)entries.size();
487+
networking_callback(nullptr, progress);
488+
}
485489
i++;
486490
const std::string& DeletePath = entry.path;
487491

src/Utils.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
// operating system level utilities
22
// contains directory utils, http utils, and helper methods
33

4+
// CURLOPT_PROGRESSFUNCTION deprecated in 7.32.0 and is replaced with CURLOPT_XFERINFOFUNCTION
5+
#include <curl/curl.h>
6+
#if LIBCURL_VERSION_NUM >= 0x072000
7+
#define COMPAT_CURL_PROGRESS_OPTION CURLOPT_XFERINFOFUNCTION
8+
#else
9+
#define COMPAT_CURL_PROGRESS_OPTION CURLOPT_PROGRESSFUNCTION
10+
#endif
11+
412
#if defined(WII) && !defined(NETWORK_MOCK)
513
#include <wiisocket.h>
614
#endif
@@ -48,12 +56,31 @@
4856

4957
#define BUF_SIZE 0x800000 // 8MB.
5058

51-
int (*networking_callback)(void*, double, double, double, double);
52-
int (*libget_status_callback)(int, int, int);
59+
libget_progress_callback_t networking_callback = nullptr;
60+
int (*libget_status_callback)(int, int, int) = nullptr;
5361
void* networking_callback_data = nullptr; // User data to pass to callback
5462

5563
static const char* USER_AGENT = "libget-unknown/0.0.0";
5664

65+
// different signature depending on curl version
66+
#ifndef NETWORK_MOCK
67+
#if LIBCURL_VERSION_NUM >= 0x072000
68+
static int libget_curl_progress_wrapper(void* /*clientp*/, curl_off_t dltotal, curl_off_t dlnow, curl_off_t, curl_off_t)
69+
#else
70+
static int libget_curl_progress_wrapper(void* /*clientp*/, double dltotal, double dlnow, double, double)
71+
#endif
72+
{
73+
if (networking_callback == nullptr)
74+
return 0;
75+
76+
if (dltotal == 0)
77+
dltotal = 1;
78+
79+
double progress = (double)dlnow / (double)dltotal;
80+
return networking_callback(networking_callback_data, progress);
81+
}
82+
#endif
83+
5784
// reference to the curl handle so that we can re-use the connection
5885
#ifndef NETWORK_MOCK
5986
CURL* curl = nullptr;
@@ -181,7 +208,7 @@ void resetCurlToCleanState(CURL* c)
181208
if (!c) return;
182209

183210
// clear callbacks which might reference freed objects
184-
curl_easy_setopt(c, CURLOPT_PROGRESSFUNCTION, nullptr);
211+
curl_easy_setopt(c, COMPAT_CURL_PROGRESS_OPTION, nullptr);
185212
curl_easy_setopt(c, CURLOPT_PROGRESSDATA, nullptr);
186213
curl_easy_setopt(c, CURLOPT_NOPROGRESS, 1);
187214

@@ -274,12 +301,12 @@ bool downloadFileCommon(const std::string& path, std::string* buffer = nullptr,
274301

275302
if (networking_callback != nullptr) {
276303
printf("[downloadFileCommon] Setting progress callback (networking_callback=%p)\n", networking_callback);
277-
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, networking_callback);
278-
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, networking_callback_data);
304+
curl_easy_setopt(curl, COMPAT_CURL_PROGRESS_OPTION, libget_curl_progress_wrapper);
305+
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, curl);
279306
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
280307
} else {
281308
printf("[downloadFileCommon] Disabling progress callbacks (networking_callback is nullptr)\n");
282-
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, nullptr);
309+
curl_easy_setopt(curl, COMPAT_CURL_PROGRESS_OPTION, nullptr);
283310
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, nullptr);
284311
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
285312
}

src/Utils.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ int my_mkdir(const std::string& path, int perms = 0700);
5656
char* my_strptime(const char* s, const char* f, struct tm* tm);
5757

5858

59+
// curl callback wrapper, progress is between 0 and 1 (inclusive)
60+
typedef int (*libget_progress_callback_t)(void* clientp, double progress);
61+
5962
// callback for networking progress
6063
// if set, will be invoked during the download
61-
extern int (*networking_callback)(void*, double, double, double, double);
64+
extern libget_progress_callback_t networking_callback;
6265
extern int (*libget_status_callback)(int, int, int);
6366
extern void* networking_callback_data; // User data to pass to networking_callback
6467
void setUserAgent(const char* data);

src/libs/minizip/crypt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static int decrypt_byte(unsigned long* pkeys, const z_crc_t* pcrc_32_tab)
3838
* unpredictable manner on 16-bit systems; not a problem
3939
* with any known compiler so far, though */
4040

41+
(void)pcrc_32_tab; // unused
4142
temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2;
4243
return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
4344
}

src/libs/minizip/ioapi.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, in
9696
{
9797
FILE* file = NULL;
9898
const char* mode_fopen = NULL;
99+
(void)opaque;
99100
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
100101
mode_fopen = "rb";
101102
else
@@ -114,6 +115,7 @@ static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename,
114115
{
115116
FILE* file = NULL;
116117
const char* mode_fopen = NULL;
118+
(void)opaque;
117119
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
118120
mode_fopen = "rb";
119121
else
@@ -132,20 +134,23 @@ static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename,
132134
static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
133135
{
134136
uLong ret;
137+
(void)opaque;
135138
ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
136139
return ret;
137140
}
138141

139142
static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
140143
{
141144
uLong ret;
145+
(void)opaque;
142146
ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
143147
return ret;
144148
}
145149

146150
static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
147151
{
148152
long ret;
153+
(void)opaque;
149154
ret = ftell((FILE *)stream);
150155
return ret;
151156
}
@@ -154,6 +159,7 @@ static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
154159
static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream)
155160
{
156161
ZPOS64_T ret;
162+
(void)opaque;
157163
ret = FTELLO_FUNC((FILE *)stream);
158164
return ret;
159165
}
@@ -162,6 +168,7 @@ static long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream, uLong offs
162168
{
163169
int fseek_origin=0;
164170
long ret;
171+
(void)opaque;
165172
switch (origin)
166173
{
167174
case ZLIB_FILEFUNC_SEEK_CUR :
@@ -185,6 +192,7 @@ static long ZCALLBACK fseek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T
185192
{
186193
int fseek_origin=0;
187194
long ret;
195+
(void)opaque;
188196
switch (origin)
189197
{
190198
case ZLIB_FILEFUNC_SEEK_CUR :
@@ -210,13 +218,15 @@ static long ZCALLBACK fseek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T
210218
static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
211219
{
212220
int ret;
221+
(void)opaque;
213222
ret = fclose((FILE *)stream);
214223
return ret;
215224
}
216225

217226
static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
218227
{
219228
int ret;
229+
(void)opaque;
220230
ret = ferror((FILE *)stream);
221231
return ret;
222232
}

src/libs/tinyxml/tinyxmlparser.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,17 @@ void TiXmlBase::ConvertUTF32ToUTF8( unsigned long input, char* output, int* leng
111111
--output;
112112
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
113113
input >>= 6;
114+
/* fall through */
114115
case 3:
115116
--output;
116117
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
117118
input >>= 6;
119+
/* fall through */
118120
case 2:
119121
--output;
120122
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
121123
input >>= 6;
124+
/* fall through */
122125
case 1:
123126
--output;
124127
*output = (char)(input | FIRST_BYTE_MARK[*length]);

src/repos/GetRepo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ std::vector<std::unique_ptr<Package>> GetRepo::loadPackages()
7171
auto total = packages_doc.Size();
7272
for (int i = 0; i < (int)total; i++)
7373
{
74-
if (networking_callback != nullptr)
75-
networking_callback(nullptr, total, i + 1, 0, 0);
74+
if (networking_callback != nullptr) {
75+
double progress = (double)(i + 1) / (double)total;
76+
networking_callback(nullptr, progress);
77+
}
7678

7779
auto package = std::make_unique<Package>(GET);
7880

src/repos/OSCRepo.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ std::vector<std::unique_ptr<Package>> OSCRepo::loadPackages()
6868

6969
// for every repo
7070
auto total = (int32_t)packages_doc.Size();
71-
for (int i = 0; i < total; i++)
71+
for (int32_t i = 0; i < total; i++)
7272
{
73-
if (networking_callback != nullptr)
74-
networking_callback(nullptr, total, i + 1, 0, 0);
73+
if (networking_callback != nullptr) {
74+
double progress = (double)(i + 1) / (double)total;
75+
networking_callback(nullptr, progress);
76+
}
7577

7678
auto package = std::make_unique<Package>(GET);
7779

src/repos/UniStoreRepo.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ std::vector<std::unique_ptr<Package>> UniStoreRepo::loadPackages()
6969

7070
// for every package in the repo
7171
auto total = (int32_t)packages_doc.Size();
72-
for (int i = 0; i < total; i++)
72+
for (int32_t i = 0; i < total; i++)
7373
{
74-
if (networking_callback != nullptr)
75-
networking_callback(nullptr, total, i + 1, 0, 0);
74+
if (networking_callback != nullptr) {
75+
double progress = (double)(i + 1) / (double)total;
76+
networking_callback(nullptr, progress);
77+
}
7678

7779
std::vector<std::string> keys;
7880
auto start = packages_doc[i].GetObj().MemberBegin();

0 commit comments

Comments
 (0)