xref: /aosp_15_r20/external/curl/docs/libcurl/opts/CURLOPT_PROGRESSFUNCTION.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker---
2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
4*6236dae4SAndroid Build Coastguard WorkerTitle: CURLOPT_PROGRESSFUNCTION
5*6236dae4SAndroid Build Coastguard WorkerSection: 3
6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl
7*6236dae4SAndroid Build Coastguard WorkerSee-also:
8*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_NOPROGRESS (3)
9*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_VERBOSE (3)
10*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_XFERINFOFUNCTION (3)
11*6236dae4SAndroid Build Coastguard WorkerProtocol:
12*6236dae4SAndroid Build Coastguard Worker  - All
13*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.1
14*6236dae4SAndroid Build Coastguard Worker---
15*6236dae4SAndroid Build Coastguard Worker
16*6236dae4SAndroid Build Coastguard Worker# NAME
17*6236dae4SAndroid Build Coastguard Worker
18*6236dae4SAndroid Build Coastguard WorkerCURLOPT_PROGRESSFUNCTION - progress meter callback
19*6236dae4SAndroid Build Coastguard Worker
20*6236dae4SAndroid Build Coastguard Worker# SYNOPSIS
21*6236dae4SAndroid Build Coastguard Worker
22*6236dae4SAndroid Build Coastguard Worker~~~c
23*6236dae4SAndroid Build Coastguard Worker#include <curl/curl.h>
24*6236dae4SAndroid Build Coastguard Worker
25*6236dae4SAndroid Build Coastguard Workerint progress_callback(void *clientp,
26*6236dae4SAndroid Build Coastguard Worker                      double dltotal,
27*6236dae4SAndroid Build Coastguard Worker                      double dlnow,
28*6236dae4SAndroid Build Coastguard Worker                      double ultotal,
29*6236dae4SAndroid Build Coastguard Worker                      double ulnow);
30*6236dae4SAndroid Build Coastguard Worker
31*6236dae4SAndroid Build Coastguard WorkerCURLcode curl_easy_setopt(CURL *handle, CURLOPT_PROGRESSFUNCTION,
32*6236dae4SAndroid Build Coastguard Worker                          progress_callback);
33*6236dae4SAndroid Build Coastguard Worker~~~
34*6236dae4SAndroid Build Coastguard Worker
35*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION
36*6236dae4SAndroid Build Coastguard Worker
37*6236dae4SAndroid Build Coastguard WorkerPass a pointer to your callback function, which should match the prototype
38*6236dae4SAndroid Build Coastguard Workershown above.
39*6236dae4SAndroid Build Coastguard Worker
40*6236dae4SAndroid Build Coastguard WorkerThis option is deprecated and we encourage users to use the
41*6236dae4SAndroid Build Coastguard Workernewer CURLOPT_XFERINFOFUNCTION(3) instead, if you can.
42*6236dae4SAndroid Build Coastguard Worker
43*6236dae4SAndroid Build Coastguard WorkerThis function gets called by libcurl instead of its internal equivalent with a
44*6236dae4SAndroid Build Coastguard Workerfrequent interval. While data is being transferred it is invoked frequently,
45*6236dae4SAndroid Build Coastguard Workerand during slow periods like when nothing is being transferred it can slow
46*6236dae4SAndroid Build Coastguard Workerdown to about one call per second.
47*6236dae4SAndroid Build Coastguard Worker
48*6236dae4SAndroid Build Coastguard Worker*clientp* is the pointer set with CURLOPT_PROGRESSDATA(3), it is not
49*6236dae4SAndroid Build Coastguard Workerused by libcurl but is only passed along from the application to the callback.
50*6236dae4SAndroid Build Coastguard Worker
51*6236dae4SAndroid Build Coastguard WorkerThe callback gets told how much data libcurl is about to transfer and has
52*6236dae4SAndroid Build Coastguard Workertransferred, in number of bytes. *dltotal* is the total number of bytes
53*6236dae4SAndroid Build Coastguard Workerlibcurl expects to download in this transfer. *dlnow* is the number of
54*6236dae4SAndroid Build Coastguard Workerbytes downloaded so far. *ultotal* is the total number of bytes libcurl
55*6236dae4SAndroid Build Coastguard Workerexpects to upload in this transfer. *ulnow* is the number of bytes
56*6236dae4SAndroid Build Coastguard Workeruploaded so far.
57*6236dae4SAndroid Build Coastguard Worker
58*6236dae4SAndroid Build Coastguard WorkerUnknown/unused argument values passed to the callback are be set to zero (like
59*6236dae4SAndroid Build Coastguard Workerif you only download data, the upload size remains 0). Many times the callback
60*6236dae4SAndroid Build Coastguard Workeris called one or more times first, before it knows the data sizes so a program
61*6236dae4SAndroid Build Coastguard Workermust be made to handle that.
62*6236dae4SAndroid Build Coastguard Worker
63*6236dae4SAndroid Build Coastguard WorkerReturn zero from the callback if everything is fine.
64*6236dae4SAndroid Build Coastguard Worker
65*6236dae4SAndroid Build Coastguard WorkerIf your callback function returns CURL_PROGRESSFUNC_CONTINUE it causes libcurl
66*6236dae4SAndroid Build Coastguard Workerto continue executing the default progress function.
67*6236dae4SAndroid Build Coastguard Worker
68*6236dae4SAndroid Build Coastguard WorkerReturn 1 from this callback to make libcurl abort the transfer and return
69*6236dae4SAndroid Build Coastguard Worker*CURLE_ABORTED_BY_CALLBACK*.
70*6236dae4SAndroid Build Coastguard Worker
71*6236dae4SAndroid Build Coastguard WorkerIf you transfer data with the multi interface, this function is not called
72*6236dae4SAndroid Build Coastguard Workerduring periods of idleness unless you call the appropriate libcurl function
73*6236dae4SAndroid Build Coastguard Workerthat performs transfers.
74*6236dae4SAndroid Build Coastguard Worker
75*6236dae4SAndroid Build Coastguard WorkerCURLOPT_NOPROGRESS(3) must be set to 0 to make this function actually
76*6236dae4SAndroid Build Coastguard Workerget called.
77*6236dae4SAndroid Build Coastguard Worker
78*6236dae4SAndroid Build Coastguard Worker# DEFAULT
79*6236dae4SAndroid Build Coastguard Worker
80*6236dae4SAndroid Build Coastguard WorkerNULL. libcurl has an internal progress meter. That is rarely wanted by users.
81*6236dae4SAndroid Build Coastguard Worker
82*6236dae4SAndroid Build Coastguard Worker# %PROTOCOLS%
83*6236dae4SAndroid Build Coastguard Worker
84*6236dae4SAndroid Build Coastguard Worker# EXAMPLE
85*6236dae4SAndroid Build Coastguard Worker
86*6236dae4SAndroid Build Coastguard Worker~~~c
87*6236dae4SAndroid Build Coastguard Workerstruct progress {
88*6236dae4SAndroid Build Coastguard Worker  char *private;
89*6236dae4SAndroid Build Coastguard Worker  size_t size;
90*6236dae4SAndroid Build Coastguard Worker};
91*6236dae4SAndroid Build Coastguard Worker
92*6236dae4SAndroid Build Coastguard Workerstatic size_t progress_callback(void *clientp,
93*6236dae4SAndroid Build Coastguard Worker                                double dltotal,
94*6236dae4SAndroid Build Coastguard Worker                                double dlnow,
95*6236dae4SAndroid Build Coastguard Worker                                double ultotal,
96*6236dae4SAndroid Build Coastguard Worker                                double ulnow)
97*6236dae4SAndroid Build Coastguard Worker{
98*6236dae4SAndroid Build Coastguard Worker  struct progress *memory = clientp;
99*6236dae4SAndroid Build Coastguard Worker  printf("private: %p\n", memory->private);
100*6236dae4SAndroid Build Coastguard Worker
101*6236dae4SAndroid Build Coastguard Worker  /* use the values */
102*6236dae4SAndroid Build Coastguard Worker
103*6236dae4SAndroid Build Coastguard Worker  return 0; /* all is good */
104*6236dae4SAndroid Build Coastguard Worker}
105*6236dae4SAndroid Build Coastguard Worker
106*6236dae4SAndroid Build Coastguard Workerint main(void)
107*6236dae4SAndroid Build Coastguard Worker{
108*6236dae4SAndroid Build Coastguard Worker  struct progress data;
109*6236dae4SAndroid Build Coastguard Worker
110*6236dae4SAndroid Build Coastguard Worker  CURL *curl = curl_easy_init();
111*6236dae4SAndroid Build Coastguard Worker  if(curl) {
112*6236dae4SAndroid Build Coastguard Worker    /* pass struct to callback  */
113*6236dae4SAndroid Build Coastguard Worker    curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &data);
114*6236dae4SAndroid Build Coastguard Worker    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
115*6236dae4SAndroid Build Coastguard Worker
116*6236dae4SAndroid Build Coastguard Worker    curl_easy_perform(curl);
117*6236dae4SAndroid Build Coastguard Worker  }
118*6236dae4SAndroid Build Coastguard Worker}
119*6236dae4SAndroid Build Coastguard Worker~~~
120*6236dae4SAndroid Build Coastguard Worker
121*6236dae4SAndroid Build Coastguard Worker# DEPRECATED
122*6236dae4SAndroid Build Coastguard Worker
123*6236dae4SAndroid Build Coastguard WorkerDeprecated since 7.32.0.
124*6236dae4SAndroid Build Coastguard Worker
125*6236dae4SAndroid Build Coastguard Worker# %AVAILABILITY%
126*6236dae4SAndroid Build Coastguard Worker
127*6236dae4SAndroid Build Coastguard Worker# RETURN VALUE
128*6236dae4SAndroid Build Coastguard Worker
129*6236dae4SAndroid Build Coastguard WorkerReturns CURLE_OK.
130