00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "curl_setup.h"
00024
00025 #include "urldata.h"
00026 #include "sendf.h"
00027 #include "progress.h"
00028 #include "curl_printf.h"
00029
00030
00031
00032 static void time2str(char *r, curl_off_t seconds)
00033 {
00034 curl_off_t d, h, m, s;
00035 if(seconds <= 0) {
00036 strcpy(r, "--:--:--");
00037 return;
00038 }
00039 h = seconds / CURL_OFF_T_C(3600);
00040 if(h <= CURL_OFF_T_C(99)) {
00041 m = (seconds - (h*CURL_OFF_T_C(3600))) / CURL_OFF_T_C(60);
00042 s = (seconds - (h*CURL_OFF_T_C(3600))) - (m*CURL_OFF_T_C(60));
00043 snprintf(r, 9, "%2" CURL_FORMAT_CURL_OFF_T ":%02" CURL_FORMAT_CURL_OFF_T
00044 ":%02" CURL_FORMAT_CURL_OFF_T, h, m, s);
00045 }
00046 else {
00047
00048
00049 d = seconds / CURL_OFF_T_C(86400);
00050 h = (seconds - (d*CURL_OFF_T_C(86400))) / CURL_OFF_T_C(3600);
00051 if(d <= CURL_OFF_T_C(999))
00052 snprintf(r, 9, "%3" CURL_FORMAT_CURL_OFF_T
00053 "d %02" CURL_FORMAT_CURL_OFF_T "h", d, h);
00054 else
00055 snprintf(r, 9, "%7" CURL_FORMAT_CURL_OFF_T "d", d);
00056 }
00057 }
00058
00059
00060
00061
00062 static char *max5data(curl_off_t bytes, char *max5)
00063 {
00064 #define ONE_KILOBYTE CURL_OFF_T_C(1024)
00065 #define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE)
00066 #define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE)
00067 #define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE)
00068 #define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE)
00069
00070 if(bytes < CURL_OFF_T_C(100000))
00071 snprintf(max5, 6, "%5" CURL_FORMAT_CURL_OFF_T, bytes);
00072
00073 else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
00074 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "k", bytes/ONE_KILOBYTE);
00075
00076 else if(bytes < CURL_OFF_T_C(100) * ONE_MEGABYTE)
00077
00078 snprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
00079 CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE,
00080 (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
00081
00082 #if (CURL_SIZEOF_CURL_OFF_T > 4)
00083
00084 else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE)
00085
00086 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
00087
00088 else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE)
00089
00090 snprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
00091 CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE,
00092 (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
00093
00094 else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE)
00095
00096 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE);
00097
00098 else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE)
00099
00100 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "T", bytes/ONE_TERABYTE);
00101
00102 else
00103
00104 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "P", bytes/ONE_PETABYTE);
00105
00106
00107
00108
00109 #else
00110
00111 else
00112 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
00113
00114 #endif
00115
00116 return max5;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 int Curl_pgrsDone(struct connectdata *conn)
00134 {
00135 int rc;
00136 struct Curl_easy *data = conn->data;
00137 data->progress.lastshow=0;
00138 rc = Curl_pgrsUpdate(conn);
00139 if(rc)
00140 return rc;
00141
00142 if(!(data->progress.flags & PGRS_HIDE) &&
00143 !data->progress.callback)
00144
00145
00146 fprintf(data->set.err, "\n");
00147
00148 data->progress.speeder_c = 0;
00149 return 0;
00150 }
00151
00152
00153 void Curl_pgrsResetTimesSizes(struct Curl_easy *data)
00154 {
00155 data->progress.t_nslookup = 0.0;
00156 data->progress.t_connect = 0.0;
00157 data->progress.t_pretransfer = 0.0;
00158 data->progress.t_starttransfer = 0.0;
00159
00160 Curl_pgrsSetDownloadSize(data, -1);
00161 Curl_pgrsSetUploadSize(data, -1);
00162 }
00163
00164 void Curl_pgrsTime(struct Curl_easy *data, timerid timer)
00165 {
00166 struct timeval now = Curl_tvnow();
00167
00168 switch(timer) {
00169 default:
00170 case TIMER_NONE:
00171
00172 break;
00173 case TIMER_STARTOP:
00174
00175 data->progress.t_startop = now;
00176 break;
00177 case TIMER_STARTSINGLE:
00178
00179 data->progress.t_startsingle = now;
00180 break;
00181
00182 case TIMER_STARTACCEPT:
00183 data->progress.t_acceptdata = Curl_tvnow();
00184 break;
00185
00186 case TIMER_NAMELOOKUP:
00187 data->progress.t_nslookup =
00188 Curl_tvdiff_secs(now, data->progress.t_startsingle);
00189 break;
00190 case TIMER_CONNECT:
00191 data->progress.t_connect =
00192 Curl_tvdiff_secs(now, data->progress.t_startsingle);
00193 break;
00194 case TIMER_APPCONNECT:
00195 data->progress.t_appconnect =
00196 Curl_tvdiff_secs(now, data->progress.t_startsingle);
00197 break;
00198 case TIMER_PRETRANSFER:
00199 data->progress.t_pretransfer =
00200 Curl_tvdiff_secs(now, data->progress.t_startsingle);
00201 break;
00202 case TIMER_STARTTRANSFER:
00203 data->progress.t_starttransfer =
00204 Curl_tvdiff_secs(now, data->progress.t_startsingle);
00205 break;
00206 case TIMER_POSTRANSFER:
00207
00208 break;
00209 case TIMER_REDIRECT:
00210 data->progress.t_redirect = Curl_tvdiff_secs(now, data->progress.start);
00211 break;
00212 }
00213 }
00214
00215 void Curl_pgrsStartNow(struct Curl_easy *data)
00216 {
00217 data->progress.speeder_c = 0;
00218 data->progress.start = Curl_tvnow();
00219 data->progress.ul_limit_start.tv_sec = 0;
00220 data->progress.ul_limit_start.tv_usec = 0;
00221 data->progress.dl_limit_start.tv_sec = 0;
00222 data->progress.dl_limit_start.tv_usec = 0;
00223
00224 data->progress.flags &= PGRS_HIDE|PGRS_HEADERS_OUT;
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 long Curl_pgrsLimitWaitTime(curl_off_t cursize,
00247 curl_off_t startsize,
00248 curl_off_t limit,
00249 struct timeval start,
00250 struct timeval now)
00251 {
00252 curl_off_t size = cursize - startsize;
00253 time_t minimum;
00254 time_t actual;
00255
00256
00257 if(start.tv_sec == 0 && start.tv_usec == 0)
00258 return 0;
00259
00260
00261 if(size < limit)
00262 return -1;
00263
00264 minimum = (time_t) (CURL_OFF_T_C(1000) * size / limit);
00265 actual = Curl_tvdiff(now, start);
00266
00267 if(actual < minimum)
00268
00269 return (long)(minimum - actual);
00270 else
00271 return 0;
00272 }
00273
00274 void Curl_pgrsSetDownloadCounter(struct Curl_easy *data, curl_off_t size)
00275 {
00276 struct timeval now = Curl_tvnow();
00277
00278 data->progress.downloaded = size;
00279
00280
00281 if((data->set.max_recv_speed > 0) &&
00282 (Curl_pgrsLimitWaitTime(data->progress.downloaded,
00283 data->progress.dl_limit_size,
00284 data->set.max_recv_speed,
00285 data->progress.dl_limit_start,
00286 now) == 0)) {
00287 data->progress.dl_limit_start = now;
00288 data->progress.dl_limit_size = size;
00289 }
00290 }
00291
00292 void Curl_pgrsSetUploadCounter(struct Curl_easy *data, curl_off_t size)
00293 {
00294 struct timeval now = Curl_tvnow();
00295
00296 data->progress.uploaded = size;
00297
00298
00299 if((data->set.max_send_speed > 0) &&
00300 (Curl_pgrsLimitWaitTime(data->progress.uploaded,
00301 data->progress.ul_limit_size,
00302 data->set.max_send_speed,
00303 data->progress.ul_limit_start,
00304 now) == 0)) {
00305 data->progress.ul_limit_start = now;
00306 data->progress.ul_limit_size = size;
00307 }
00308 }
00309
00310 void Curl_pgrsSetDownloadSize(struct Curl_easy *data, curl_off_t size)
00311 {
00312 if(size >= 0) {
00313 data->progress.size_dl = size;
00314 data->progress.flags |= PGRS_DL_SIZE_KNOWN;
00315 }
00316 else {
00317 data->progress.size_dl = 0;
00318 data->progress.flags &= ~PGRS_DL_SIZE_KNOWN;
00319 }
00320 }
00321
00322 void Curl_pgrsSetUploadSize(struct Curl_easy *data, curl_off_t size)
00323 {
00324 if(size >= 0) {
00325 data->progress.size_ul = size;
00326 data->progress.flags |= PGRS_UL_SIZE_KNOWN;
00327 }
00328 else {
00329 data->progress.size_ul = 0;
00330 data->progress.flags &= ~PGRS_UL_SIZE_KNOWN;
00331 }
00332 }
00333
00334
00335
00336
00337
00338 int Curl_pgrsUpdate(struct connectdata *conn)
00339 {
00340 struct timeval now;
00341 int result;
00342 char max5[6][10];
00343 curl_off_t dlpercen=0;
00344 curl_off_t ulpercen=0;
00345 curl_off_t total_percen=0;
00346 curl_off_t total_transfer;
00347 curl_off_t total_expected_transfer;
00348 curl_off_t timespent;
00349 struct Curl_easy *data = conn->data;
00350 int nowindex = data->progress.speeder_c% CURR_TIME;
00351 int checkindex;
00352 int countindex;
00353 char time_left[10];
00354 char time_total[10];
00355 char time_spent[10];
00356 curl_off_t ulestimate=0;
00357 curl_off_t dlestimate=0;
00358 curl_off_t total_estimate;
00359 bool shownow=FALSE;
00360
00361 now = Curl_tvnow();
00362
00363
00364 data->progress.timespent = curlx_tvdiff_secs(now, data->progress.start);
00365 timespent = (curl_off_t)data->progress.timespent;
00366
00367
00368 data->progress.dlspeed = (curl_off_t)
00369 ((double)data->progress.downloaded/
00370 (data->progress.timespent>0?data->progress.timespent:1));
00371
00372
00373 data->progress.ulspeed = (curl_off_t)
00374 ((double)data->progress.uploaded/
00375 (data->progress.timespent>0?data->progress.timespent:1));
00376
00377
00378 if(data->progress.lastshow != now.tv_sec) {
00379 shownow = TRUE;
00380
00381 data->progress.lastshow = now.tv_sec;
00382
00383
00384
00385 data->progress.speeder[ nowindex ] =
00386 data->progress.downloaded>data->progress.uploaded?
00387 data->progress.downloaded:data->progress.uploaded;
00388
00389
00390 data->progress.speeder_time [ nowindex ] = now;
00391
00392
00393
00394 data->progress.speeder_c++;
00395
00396
00397
00398
00399
00400 countindex = ((data->progress.speeder_c>=CURR_TIME)?
00401 CURR_TIME:data->progress.speeder_c) - 1;
00402
00403
00404 if(countindex) {
00405 time_t span_ms;
00406
00407
00408
00409
00410 checkindex = (data->progress.speeder_c>=CURR_TIME)?
00411 data->progress.speeder_c%CURR_TIME:0;
00412
00413
00414 span_ms = Curl_tvdiff(now,
00415 data->progress.speeder_time[checkindex]);
00416 if(0 == span_ms)
00417 span_ms=1;
00418
00419
00420 {
00421 curl_off_t amount = data->progress.speeder[nowindex]-
00422 data->progress.speeder[checkindex];
00423
00424 if(amount > CURL_OFF_T_C(4294967) )
00425
00426
00427 data->progress.current_speed = (curl_off_t)
00428 ((double)amount/((double)span_ms/1000.0));
00429 else
00430
00431
00432 data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
00433 }
00434 }
00435 else
00436
00437 data->progress.current_speed =
00438 (data->progress.ulspeed>data->progress.dlspeed)?
00439 data->progress.ulspeed:data->progress.dlspeed;
00440
00441 }
00442
00443 if(!(data->progress.flags & PGRS_HIDE)) {
00444
00445
00446 if(data->set.fxferinfo) {
00447
00448 result= data->set.fxferinfo(data->set.progress_client,
00449 data->progress.size_dl,
00450 data->progress.downloaded,
00451 data->progress.size_ul,
00452 data->progress.uploaded);
00453 if(result)
00454 failf(data, "Callback aborted");
00455 return result;
00456 }
00457 else if(data->set.fprogress) {
00458
00459 result= data->set.fprogress(data->set.progress_client,
00460 (double)data->progress.size_dl,
00461 (double)data->progress.downloaded,
00462 (double)data->progress.size_ul,
00463 (double)data->progress.uploaded);
00464 if(result)
00465 failf(data, "Callback aborted");
00466 return result;
00467 }
00468
00469 if(!shownow)
00470
00471 return 0;
00472
00473
00474
00475
00476 if(!(data->progress.flags & PGRS_HEADERS_OUT)) {
00477 if(data->state.resume_from) {
00478 fprintf(data->set.err,
00479 "** Resuming transfer from byte position %"
00480 CURL_FORMAT_CURL_OFF_T "\n", data->state.resume_from);
00481 }
00482 fprintf(data->set.err,
00483 " %% Total %% Received %% Xferd Average Speed "
00484 "Time Time Time Current\n"
00485 " Dload Upload "
00486 "Total Spent Left Speed\n");
00487 data->progress.flags |= PGRS_HEADERS_OUT;
00488 }
00489
00490
00491 if((data->progress.flags & PGRS_UL_SIZE_KNOWN) &&
00492 (data->progress.ulspeed > CURL_OFF_T_C(0))) {
00493 ulestimate = data->progress.size_ul / data->progress.ulspeed;
00494
00495 if(data->progress.size_ul > CURL_OFF_T_C(10000))
00496 ulpercen = data->progress.uploaded /
00497 (data->progress.size_ul/CURL_OFF_T_C(100));
00498 else if(data->progress.size_ul > CURL_OFF_T_C(0))
00499 ulpercen = (data->progress.uploaded*100) /
00500 data->progress.size_ul;
00501 }
00502
00503
00504 if((data->progress.flags & PGRS_DL_SIZE_KNOWN) &&
00505 (data->progress.dlspeed > CURL_OFF_T_C(0))) {
00506 dlestimate = data->progress.size_dl / data->progress.dlspeed;
00507
00508 if(data->progress.size_dl > CURL_OFF_T_C(10000))
00509 dlpercen = data->progress.downloaded /
00510 (data->progress.size_dl/CURL_OFF_T_C(100));
00511 else if(data->progress.size_dl > CURL_OFF_T_C(0))
00512 dlpercen = (data->progress.downloaded*100) /
00513 data->progress.size_dl;
00514 }
00515
00516
00517
00518 total_estimate = ulestimate>dlestimate?ulestimate:dlestimate;
00519
00520
00521 time2str(time_left, total_estimate > 0?(total_estimate - timespent):0);
00522 time2str(time_total, total_estimate);
00523 time2str(time_spent, timespent);
00524
00525
00526 total_expected_transfer =
00527 (data->progress.flags & PGRS_UL_SIZE_KNOWN?
00528 data->progress.size_ul:data->progress.uploaded)+
00529 (data->progress.flags & PGRS_DL_SIZE_KNOWN?
00530 data->progress.size_dl:data->progress.downloaded);
00531
00532
00533 total_transfer = data->progress.downloaded + data->progress.uploaded;
00534
00535
00536 if(total_expected_transfer > CURL_OFF_T_C(10000))
00537 total_percen = total_transfer /
00538 (total_expected_transfer/CURL_OFF_T_C(100));
00539 else if(total_expected_transfer > CURL_OFF_T_C(0))
00540 total_percen = (total_transfer*100) / total_expected_transfer;
00541
00542 fprintf(data->set.err,
00543 "\r"
00544 "%3" CURL_FORMAT_CURL_OFF_T " %s "
00545 "%3" CURL_FORMAT_CURL_OFF_T " %s "
00546 "%3" CURL_FORMAT_CURL_OFF_T " %s %s %s %s %s %s %s",
00547 total_percen,
00548 max5data(total_expected_transfer, max5[2]),
00549 dlpercen,
00550 max5data(data->progress.downloaded, max5[0]),
00551 ulpercen,
00552 max5data(data->progress.uploaded, max5[1]),
00553 max5data(data->progress.dlspeed, max5[3]),
00554 max5data(data->progress.ulspeed, max5[4]),
00555 time_total,
00556 time_spent,
00557 time_left,
00558 max5data(data->progress.current_speed, max5[5])
00559 );
00560
00561
00562 fflush(data->set.err);
00563
00564 }
00565
00566 return 0;
00567 }