FileOperationProgress.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "FileOperationProgress.h"
  6. #include "CoreMain.h"
  7. //---------------------------------------------------------------------------
  8. #define TRANSFER_BUF_SIZE 32768
  9. //---------------------------------------------------------------------------
  10. TFileOperationStatistics::TFileOperationStatistics()
  11. {
  12. memset(this, 0, sizeof(*this));
  13. }
  14. //---------------------------------------------------------------------------
  15. //---------------------------------------------------------------------------
  16. TFileOperationProgressType::TPersistence::TPersistence()
  17. {
  18. FStatistics = NULL;
  19. Clear(true, true);
  20. }
  21. //---------------------------------------------------------------------------
  22. bool TFileOperationProgressType::IsIndeterminateOperation(TFileOperation Operation)
  23. {
  24. return (Operation == foCalculateSize);
  25. }
  26. //---------------------------------------------------------------------------
  27. void TFileOperationProgressType::TPersistence::Clear(bool Batch, bool Speed)
  28. {
  29. if (Batch)
  30. {
  31. TotalTransferred = 0;
  32. StartTime = Now();
  33. SkipToAll = false;
  34. BatchOverwrite = boNo;
  35. CPSLimit = 0;
  36. CounterSet = false;
  37. }
  38. if (Speed)
  39. {
  40. Ticks.clear();
  41. TotalTransferredThen.clear();
  42. }
  43. }
  44. //---------------------------------------------------------------------------
  45. __fastcall TFileOperationProgressType::TFileOperationProgressType()
  46. {
  47. FOnProgress = NULL;
  48. FOnFinished = NULL;
  49. FParent = NULL;
  50. Init();
  51. Clear();
  52. }
  53. //---------------------------------------------------------------------------
  54. __fastcall TFileOperationProgressType::TFileOperationProgressType(
  55. TFileOperationProgressEvent AOnProgress, TFileOperationFinished AOnFinished,
  56. TFileOperationProgressType * Parent)
  57. {
  58. FOnProgress = AOnProgress;
  59. FOnFinished = AOnFinished;
  60. FParent = Parent;
  61. FReset = false;
  62. Init();
  63. Clear();
  64. }
  65. //---------------------------------------------------------------------------
  66. __fastcall TFileOperationProgressType::~TFileOperationProgressType()
  67. {
  68. DebugAssert(!InProgress || FReset);
  69. DebugAssert(!Suspended || FReset);
  70. SAFE_DESTROY(FSection);
  71. SAFE_DESTROY(FUserSelectionsSection);
  72. }
  73. //---------------------------------------------------------------------------
  74. void __fastcall TFileOperationProgressType::Init()
  75. {
  76. FSection = new TCriticalSection();
  77. FUserSelectionsSection = new TCriticalSection();
  78. FRestored = false;
  79. FPersistence.Side = osCurrent; // = undefined value
  80. }
  81. //---------------------------------------------------------------------------
  82. void __fastcall TFileOperationProgressType::Assign(const TFileOperationProgressType & Other)
  83. {
  84. TValueRestorer<TCriticalSection *> SectionRestorer(FSection);
  85. TValueRestorer<TCriticalSection *> UserSelectionsSectionRestorer(FUserSelectionsSection);
  86. TGuard Guard(FSection);
  87. TGuard OtherGuard(Other.FSection);
  88. *this = Other;
  89. }
  90. //---------------------------------------------------------------------------
  91. void __fastcall TFileOperationProgressType::AssignButKeepSuspendState(const TFileOperationProgressType & Other)
  92. {
  93. TGuard Guard(FSection);
  94. TValueRestorer<unsigned int> SuspendTimeRestorer(FSuspendTime);
  95. TValueRestorer<bool> SuspendedRestorer(FSuspended);
  96. Assign(Other);
  97. }
  98. //---------------------------------------------------------------------------
  99. void __fastcall TFileOperationProgressType::DoClear(bool Batch, bool Speed)
  100. {
  101. FFileName = L"";
  102. FFullFileName = L"";
  103. FDirectory = L"";
  104. FAsciiTransfer = false;
  105. FCount = -1;
  106. FFilesFinished = 0;
  107. FFilesFinishedSuccessfully = 0;
  108. FSuspended = false;
  109. FSuspendTime = 0;
  110. FInProgress = false;
  111. FDone = false;
  112. FFileInProgress = false;
  113. FTotalSkipped = 0;
  114. FTotalSize = 0;
  115. FSkippedSize = 0;
  116. FTotalSizeSet = false;
  117. FOperation = foNone;
  118. FTemp = false;
  119. FPersistence.Clear(Batch, Speed);
  120. // to bypass check in ClearTransfer()
  121. FTransferSize = 0;
  122. ClearTransfer();
  123. }
  124. //---------------------------------------------------------------------------
  125. void __fastcall TFileOperationProgressType::Clear()
  126. {
  127. DoClear(true, true);
  128. }
  129. //---------------------------------------------------------------------------
  130. void __fastcall TFileOperationProgressType::ClearTransfer()
  131. {
  132. if ((TransferSize > 0) && (TransferredSize < TransferSize))
  133. {
  134. TGuard Guard(FSection);
  135. __int64 RemainingSize = (TransferSize - TransferredSize);
  136. AddSkipped(RemainingSize);
  137. }
  138. FLocalSize = 0;
  139. FTransferSize = 0;
  140. FLocallyUsed = 0;
  141. FSkippedSize = 0;
  142. FTransferredSize = 0;
  143. FTransferringFile = false;
  144. FLastSecond = 0;
  145. }
  146. //---------------------------------------------------------------------------
  147. void __fastcall TFileOperationProgressType::Start(TFileOperation AOperation,
  148. TOperationSide ASide, int ACount)
  149. {
  150. Start(AOperation, ASide, ACount, false, L"", 0);
  151. }
  152. //---------------------------------------------------------------------------
  153. void __fastcall TFileOperationProgressType::Start(TFileOperation AOperation,
  154. TOperationSide ASide, int ACount, bool ATemp,
  155. const UnicodeString ADirectory, unsigned long ACPSLimit)
  156. {
  157. {
  158. TGuard Guard(FSection); // not really needed, just for consistency
  159. DoClear(!FRestored, (FPersistence.Side != osCurrent) && (FPersistence.Side != ASide));
  160. FTotalTransferBase = FPersistence.TotalTransferred;
  161. FOperation = AOperation;
  162. FPersistence.Side = ASide;
  163. FCount = ACount;
  164. FInProgress = true;
  165. FCancel = csContinue;
  166. FDirectory = ADirectory;
  167. FTemp = ATemp;
  168. FPersistence.CPSLimit = ACPSLimit;
  169. }
  170. try
  171. {
  172. DoProgress();
  173. }
  174. catch (...)
  175. {
  176. // connection can be lost during progress callbacks
  177. ClearTransfer();
  178. FInProgress = false;
  179. throw;
  180. }
  181. }
  182. //---------------------------------------------------------------------------
  183. void __fastcall TFileOperationProgressType::Reset()
  184. {
  185. FReset = true;
  186. }
  187. //---------------------------------------------------------------------------
  188. void __fastcall TFileOperationProgressType::Stop()
  189. {
  190. // added to include remaining bytes to TotalSkipped, in case
  191. // the progress happens to update before closing
  192. ClearTransfer();
  193. FInProgress = false;
  194. DoProgress();
  195. }
  196. //---------------------------------------------------------------------------
  197. void __fastcall TFileOperationProgressType::SetDone()
  198. {
  199. FDone = true;
  200. DoProgress();
  201. }
  202. //---------------------------------------------------------------------------
  203. void __fastcall TFileOperationProgressType::Suspend()
  204. {
  205. {
  206. TGuard Guard(FSection);
  207. DebugAssert(!Suspended);
  208. FSuspended = true;
  209. FSuspendTime = GetTickCount();
  210. }
  211. DoProgress();
  212. }
  213. //---------------------------------------------------------------------------
  214. void __fastcall TFileOperationProgressType::Resume()
  215. {
  216. {
  217. TGuard Guard(FSection);
  218. DebugAssert(Suspended);
  219. FSuspended = false;
  220. // shift timestamps for CPS calculation in advance
  221. // by the time the progress was suspended
  222. unsigned long Stopped = (GetTickCount() - FSuspendTime);
  223. size_t i = 0;
  224. while (i < FPersistence.Ticks.size())
  225. {
  226. FPersistence.Ticks[i] += Stopped;
  227. ++i;
  228. }
  229. }
  230. DoProgress();
  231. }
  232. //---------------------------------------------------------------------------
  233. int __fastcall TFileOperationProgressType::OperationProgress() const
  234. {
  235. int Result;
  236. if (FCount > 0)
  237. {
  238. Result = (FFilesFinished * 100)/FCount;
  239. }
  240. else
  241. {
  242. Result = 0;
  243. }
  244. return Result;
  245. }
  246. //---------------------------------------------------------------------------
  247. int __fastcall TFileOperationProgressType::TransferProgress()
  248. {
  249. int Result;
  250. if (TransferSize)
  251. {
  252. Result = (int)((TransferredSize * 100)/TransferSize);
  253. }
  254. else
  255. {
  256. Result = 0;
  257. }
  258. return Result;
  259. }
  260. //---------------------------------------------------------------------------
  261. int __fastcall TFileOperationProgressType::TotalTransferProgress() const
  262. {
  263. TGuard Guard(FSection);
  264. DebugAssert(TotalSizeSet);
  265. int Result;
  266. if (FTotalSize > 0)
  267. {
  268. Result = (int)(((FPersistence.TotalTransferred - FTotalTransferBase + FTotalSkipped) * 100)/FTotalSize);
  269. }
  270. else
  271. {
  272. Result = 0;
  273. }
  274. return Result < 100 ? Result : 100;
  275. }
  276. //---------------------------------------------------------------------------
  277. int __fastcall TFileOperationProgressType::OverallProgress() const
  278. {
  279. if (TotalSizeSet)
  280. {
  281. DebugAssert((Operation == foCopy) || (Operation == foMove));
  282. return TotalTransferProgress();
  283. }
  284. else
  285. {
  286. return OperationProgress();
  287. }
  288. }
  289. //---------------------------------------------------------------------------
  290. void __fastcall TFileOperationProgressType::Progress()
  291. {
  292. DoProgress();
  293. }
  294. //---------------------------------------------------------------------------
  295. void __fastcall TFileOperationProgressType::DoProgress()
  296. {
  297. SetThreadExecutionState(ES_SYSTEM_REQUIRED);
  298. FOnProgress(*this);
  299. }
  300. //---------------------------------------------------------------------------
  301. void __fastcall TFileOperationProgressType::Finish(UnicodeString FileName,
  302. bool Success, TOnceDoneOperation & OnceDoneOperation)
  303. {
  304. DebugAssert(InProgress);
  305. // Cancel reader is guarded
  306. FOnFinished(Operation, Side, Temp, FileName,
  307. Success && (Cancel == csContinue), OnceDoneOperation);
  308. FFilesFinished++;
  309. if (Success)
  310. {
  311. FFilesFinishedSuccessfully++;
  312. }
  313. DoProgress();
  314. }
  315. //---------------------------------------------------------------------------
  316. void __fastcall TFileOperationProgressType::Succeeded(int Count)
  317. {
  318. if (FPersistence.Statistics != NULL)
  319. {
  320. if ((Operation == foCopy) || (Operation == foMove))
  321. {
  322. __int64 Transferred = FTransferredSize - FSkippedSize;
  323. if (Side == osLocal)
  324. {
  325. FPersistence.Statistics->FilesUploaded += Count;
  326. FPersistence.Statistics->TotalUploaded += Transferred;
  327. }
  328. else
  329. {
  330. FPersistence.Statistics->FilesDownloaded += Count;
  331. FPersistence.Statistics->TotalDownloaded += Transferred;
  332. }
  333. }
  334. else if (Operation == foDelete)
  335. {
  336. if (Side == osLocal)
  337. {
  338. FPersistence.Statistics->FilesDeletedLocal += Count;
  339. }
  340. else
  341. {
  342. FPersistence.Statistics->FilesDeletedRemote += Count;
  343. }
  344. }
  345. }
  346. }
  347. //---------------------------------------------------------------------------
  348. void __fastcall TFileOperationProgressType::SetFile(UnicodeString AFileName, bool AFileInProgress)
  349. {
  350. FFullFileName = AFileName;
  351. if (Side == osRemote)
  352. {
  353. // historically set were passing filename-only for remote site operations,
  354. // now we need to collect a full paths, so we pass in full path,
  355. // but still want to have filename-only in FileName
  356. AFileName = UnixExtractFileName(AFileName);
  357. }
  358. FFileName = AFileName;
  359. FFileInProgress = AFileInProgress;
  360. ClearTransfer();
  361. FFileStartTime = Now();
  362. DoProgress();
  363. }
  364. //---------------------------------------------------------------------------
  365. void __fastcall TFileOperationProgressType::SetFileInProgress()
  366. {
  367. DebugAssert(!FileInProgress);
  368. FFileInProgress = true;
  369. DoProgress();
  370. }
  371. //---------------------------------------------------------------------------
  372. void __fastcall TFileOperationProgressType::SetLocalSize(__int64 ASize)
  373. {
  374. FLocalSize = ASize;
  375. DoProgress();
  376. }
  377. //---------------------------------------------------------------------------
  378. void __fastcall TFileOperationProgressType::AddLocallyUsed(__int64 ASize)
  379. {
  380. FLocallyUsed += ASize;
  381. if (LocallyUsed > LocalSize)
  382. {
  383. FLocalSize = LocallyUsed;
  384. }
  385. DoProgress();
  386. }
  387. //---------------------------------------------------------------------------
  388. bool __fastcall TFileOperationProgressType::IsLocallyDone()
  389. {
  390. DebugAssert(LocallyUsed <= LocalSize);
  391. return (LocallyUsed == LocalSize);
  392. }
  393. //---------------------------------------------------------------------------
  394. void __fastcall TFileOperationProgressType::SetSpeedCounters()
  395. {
  396. if ((CPSLimit > 0) && !FPersistence.CounterSet)
  397. {
  398. FPersistence.CounterSet = true;
  399. Configuration->Usage->Inc(L"SpeedLimitUses");
  400. }
  401. }
  402. //---------------------------------------------------------------------------
  403. // Used in WebDAV and S3 protocols
  404. void __fastcall TFileOperationProgressType::ThrottleToCPSLimit(
  405. unsigned long Size)
  406. {
  407. unsigned long Remaining = Size;
  408. while (Remaining > 0)
  409. {
  410. Remaining -= AdjustToCPSLimit(Remaining);
  411. }
  412. }
  413. //---------------------------------------------------------------------------
  414. unsigned long __fastcall TFileOperationProgressType::AdjustToCPSLimit(
  415. unsigned long Size)
  416. {
  417. SetSpeedCounters();
  418. // CPSLimit reader is guarded, we cannot block whole method as it can last long.
  419. if (CPSLimit > 0)
  420. {
  421. // we must not return 0, hence, if we reach zero,
  422. // we wait until the next second
  423. do
  424. {
  425. unsigned int Second = (GetTickCount() / MSecsPerSec);
  426. if (Second != FLastSecond)
  427. {
  428. FRemainingCPS = CPSLimit;
  429. FLastSecond = Second;
  430. }
  431. if (FRemainingCPS == 0)
  432. {
  433. SleepEx(100, true);
  434. DoProgress();
  435. }
  436. }
  437. while ((CPSLimit > 0) && (FRemainingCPS == 0));
  438. // CPSLimit may have been dropped in DoProgress
  439. if (CPSLimit > 0)
  440. {
  441. if (FRemainingCPS < Size)
  442. {
  443. Size = FRemainingCPS;
  444. }
  445. FRemainingCPS -= Size;
  446. }
  447. }
  448. return Size;
  449. }
  450. //---------------------------------------------------------------------------
  451. // Use in SCP protocol only
  452. unsigned long __fastcall TFileOperationProgressType::LocalBlockSize()
  453. {
  454. unsigned long Result = TRANSFER_BUF_SIZE;
  455. if (LocallyUsed + Result > LocalSize)
  456. {
  457. Result = (unsigned long)(LocalSize - LocallyUsed);
  458. }
  459. Result = AdjustToCPSLimit(Result);
  460. return Result;
  461. }
  462. //---------------------------------------------------------------------------
  463. void __fastcall TFileOperationProgressType::SetTotalSize(__int64 ASize)
  464. {
  465. TGuard Guard(FSection); // not really needed, just for consistency
  466. FTotalSize = ASize;
  467. FTotalSizeSet = true;
  468. // parent has its own totals
  469. if (FParent != NULL)
  470. {
  471. DebugAssert(FParent->TotalSizeSet);
  472. }
  473. DoProgress();
  474. }
  475. //---------------------------------------------------------------------------
  476. void __fastcall TFileOperationProgressType::SetTransferSize(__int64 ASize)
  477. {
  478. FTransferSize = ASize;
  479. DoProgress();
  480. }
  481. //---------------------------------------------------------------------------
  482. void __fastcall TFileOperationProgressType::SetTransferringFile(bool ATransferringFile)
  483. {
  484. FTransferringFile = ATransferringFile;
  485. }
  486. //---------------------------------------------------------------------------
  487. bool __fastcall TFileOperationProgressType::PassCancelToParent(TCancelStatus ACancel)
  488. {
  489. bool Result;
  490. if (ACancel < csCancel)
  491. {
  492. // do not propagate csCancelFile,
  493. // though it's not supported for queue atm, so we do not expect it here
  494. DebugFail();
  495. Result = false;
  496. }
  497. else if (ACancel == csCancel)
  498. {
  499. Result = true;
  500. }
  501. else
  502. {
  503. // csCancelTransfer and csRemoteAbort are used with SCP only, which does not use parallel transfers
  504. DebugFail();
  505. Result = false;
  506. }
  507. return Result;
  508. }
  509. //---------------------------------------------------------------------------
  510. void __fastcall TFileOperationProgressType::SetCancel(TCancelStatus ACancel)
  511. {
  512. TGuard Guard(FSection);
  513. FCancel = ACancel;
  514. if ((FParent != NULL) && PassCancelToParent(ACancel))
  515. {
  516. FParent->SetCancel(ACancel);
  517. }
  518. }
  519. //---------------------------------------------------------------------------
  520. void __fastcall TFileOperationProgressType::SetCancelAtLeast(TCancelStatus ACancel)
  521. {
  522. TGuard Guard(FSection);
  523. if (FCancel < ACancel)
  524. {
  525. FCancel = ACancel;
  526. }
  527. if ((FParent != NULL) && PassCancelToParent(ACancel))
  528. {
  529. FParent->SetCancelAtLeast(ACancel);
  530. }
  531. }
  532. //---------------------------------------------------------------------------
  533. TCancelStatus __fastcall TFileOperationProgressType::GetCancel()
  534. {
  535. TCancelStatus Result = FCancel;
  536. if (FParent != NULL)
  537. {
  538. TGuard Guard(FSection);
  539. TCancelStatus ParentCancel = FParent->Cancel;
  540. if (ParentCancel > Result)
  541. {
  542. Result = ParentCancel;
  543. }
  544. }
  545. return Result;
  546. }
  547. //---------------------------------------------------------------------------
  548. bool __fastcall TFileOperationProgressType::ClearCancelFile()
  549. {
  550. TGuard Guard(FSection);
  551. // Not propagated to parent, as this is local flag, see also PassCancelToParent
  552. bool Result = (Cancel == csCancelFile);
  553. if (Result)
  554. {
  555. FCancel = csContinue;
  556. }
  557. return Result;
  558. }
  559. //---------------------------------------------------------------------------
  560. unsigned long __fastcall TFileOperationProgressType::GetCPSLimit()
  561. {
  562. unsigned int Result;
  563. if (FParent != NULL)
  564. {
  565. Result = FParent->CPSLimit;
  566. }
  567. else
  568. {
  569. TGuard Guard(FSection);
  570. Result = FPersistence.CPSLimit;
  571. }
  572. return Result;
  573. }
  574. //---------------------------------------------------------------------------
  575. void __fastcall TFileOperationProgressType::SetCPSLimit(unsigned long ACPSLimit)
  576. {
  577. if (FParent != NULL)
  578. {
  579. FParent->SetCPSLimit(ACPSLimit);
  580. }
  581. else
  582. {
  583. TGuard Guard(FSection);
  584. FPersistence.CPSLimit = ACPSLimit;
  585. }
  586. }
  587. //---------------------------------------------------------------------------
  588. TBatchOverwrite __fastcall TFileOperationProgressType::GetBatchOverwrite()
  589. {
  590. TBatchOverwrite Result;
  591. if (FParent != NULL)
  592. {
  593. Result = FParent->BatchOverwrite;
  594. }
  595. else
  596. {
  597. TGuard Guard(FSection); // not really needed
  598. Result = FPersistence.BatchOverwrite;
  599. }
  600. return Result;
  601. }
  602. //---------------------------------------------------------------------------
  603. void __fastcall TFileOperationProgressType::SetBatchOverwrite(TBatchOverwrite ABatchOverwrite)
  604. {
  605. if (FParent != NULL)
  606. {
  607. FParent->SetBatchOverwrite(ABatchOverwrite);
  608. }
  609. else
  610. {
  611. TGuard Guard(FSection); // not really needed
  612. FPersistence.BatchOverwrite = ABatchOverwrite;;
  613. }
  614. }
  615. //---------------------------------------------------------------------------
  616. bool __fastcall TFileOperationProgressType::GetSkipToAll()
  617. {
  618. bool Result;
  619. if (FParent != NULL)
  620. {
  621. Result = FParent->SkipToAll;
  622. }
  623. else
  624. {
  625. TGuard Guard(FSection); // not really needed
  626. Result = FPersistence.SkipToAll;
  627. }
  628. return Result;
  629. }
  630. //---------------------------------------------------------------------------
  631. void __fastcall TFileOperationProgressType::SetSkipToAll()
  632. {
  633. if (FParent != NULL)
  634. {
  635. FParent->SetSkipToAll();
  636. }
  637. else
  638. {
  639. TGuard Guard(FSection); // not really needed
  640. FPersistence.SkipToAll = true;
  641. }
  642. }
  643. //---------------------------------------------------------------------------
  644. void __fastcall TFileOperationProgressType::ChangeTransferSize(__int64 ASize)
  645. {
  646. // reflect change on file size (due to text transfer mode conversion particulary)
  647. // on total transfer size
  648. if (TotalSizeSet)
  649. {
  650. AddTotalSize(ASize - TransferSize);
  651. }
  652. FTransferSize = ASize;
  653. DoProgress();
  654. }
  655. //---------------------------------------------------------------------------
  656. void __fastcall TFileOperationProgressType::RollbackTransferFromTotals(__int64 ATransferredSize, __int64 ASkippedSize)
  657. {
  658. TGuard Guard(FSection);
  659. DebugAssert(ATransferredSize <= FPersistence.TotalTransferred - FTotalTransferBase);
  660. DebugAssert(ASkippedSize <= FTotalSkipped);
  661. FPersistence.TotalTransferred -= ATransferredSize;
  662. FPersistence.Ticks.clear();
  663. FPersistence.TotalTransferredThen.clear();
  664. FTotalSkipped -= ASkippedSize;
  665. if (FParent != NULL)
  666. {
  667. FParent->RollbackTransferFromTotals(ATransferredSize, ASkippedSize);
  668. }
  669. }
  670. //---------------------------------------------------------------------------
  671. void __fastcall TFileOperationProgressType::RollbackTransfer()
  672. {
  673. FTransferredSize -= FSkippedSize;
  674. RollbackTransferFromTotals(FTransferredSize, FSkippedSize);
  675. FSkippedSize = 0;
  676. FTransferredSize = 0;
  677. FTransferSize = 0;
  678. FLocallyUsed = 0;
  679. }
  680. //---------------------------------------------------------------------------
  681. void __fastcall TFileOperationProgressType::AddTransferredToTotals(__int64 ASize)
  682. {
  683. TGuard Guard(FSection);
  684. FPersistence.TotalTransferred += ASize;
  685. if (ASize >= 0)
  686. {
  687. unsigned long Ticks = GetTickCount();
  688. if (FPersistence.Ticks.empty() ||
  689. (FPersistence.Ticks.back() > Ticks) || // ticks wrap after 49.7 days
  690. ((Ticks - FPersistence.Ticks.back()) >= MSecsPerSec))
  691. {
  692. FPersistence.Ticks.push_back(Ticks);
  693. FPersistence.TotalTransferredThen.push_back(FPersistence.TotalTransferred);
  694. }
  695. if (FPersistence.Ticks.size() > 10)
  696. {
  697. FPersistence.Ticks.erase(FPersistence.Ticks.begin());
  698. FPersistence.TotalTransferredThen.erase(FPersistence.TotalTransferredThen.begin());
  699. }
  700. }
  701. else
  702. {
  703. FPersistence.Ticks.clear();
  704. }
  705. if (FParent != NULL)
  706. {
  707. FParent->AddTransferredToTotals(ASize);
  708. }
  709. }
  710. //---------------------------------------------------------------------------
  711. void __fastcall TFileOperationProgressType::AddTotalSize(__int64 ASize)
  712. {
  713. if (ASize != 0)
  714. {
  715. TGuard Guard(FSection);
  716. FTotalSize += ASize;
  717. if (FParent != NULL)
  718. {
  719. FParent->AddTotalSize(ASize);
  720. }
  721. }
  722. }
  723. //---------------------------------------------------------------------------
  724. void __fastcall TFileOperationProgressType::AddTransferred(__int64 ASize,
  725. bool AddToTotals)
  726. {
  727. FTransferredSize += ASize;
  728. if (TransferredSize > TransferSize)
  729. {
  730. // this can happen with SFTP when downloading file that
  731. // grows while being downloaded
  732. if (TotalSizeSet)
  733. {
  734. // we should probably guard this with AddToTotals
  735. AddTotalSize(TransferredSize - TransferSize);
  736. }
  737. FTransferSize = TransferredSize;
  738. }
  739. if (AddToTotals)
  740. {
  741. AddTransferredToTotals(ASize);
  742. }
  743. DoProgress();
  744. }
  745. //---------------------------------------------------------------------------
  746. void __fastcall TFileOperationProgressType::AddSkipped(__int64 ASize)
  747. {
  748. TGuard Guard(FSection);
  749. FTotalSkipped += ASize;
  750. if (FParent != NULL)
  751. {
  752. FParent->AddSkipped(ASize);
  753. }
  754. }
  755. //---------------------------------------------------------------------------
  756. void __fastcall TFileOperationProgressType::AddResumed(__int64 ASize)
  757. {
  758. AddSkipped(ASize);
  759. FSkippedSize += ASize;
  760. AddTransferred(ASize, false);
  761. AddLocallyUsed(ASize);
  762. }
  763. //---------------------------------------------------------------------------
  764. void __fastcall TFileOperationProgressType::AddSkippedFileSize(__int64 ASize)
  765. {
  766. AddSkipped(ASize);
  767. DoProgress();
  768. }
  769. //---------------------------------------------------------------------------
  770. // Use in SCP protocol only
  771. unsigned long __fastcall TFileOperationProgressType::TransferBlockSize()
  772. {
  773. unsigned long Result = TRANSFER_BUF_SIZE;
  774. if (TransferredSize + Result > TransferSize)
  775. {
  776. Result = (unsigned long)(TransferSize - TransferredSize);
  777. }
  778. Result = AdjustToCPSLimit(Result);
  779. return Result;
  780. }
  781. //---------------------------------------------------------------------------
  782. unsigned long __fastcall TFileOperationProgressType::StaticBlockSize()
  783. {
  784. return TRANSFER_BUF_SIZE;
  785. }
  786. //---------------------------------------------------------------------------
  787. bool __fastcall TFileOperationProgressType::IsTransferDone()
  788. {
  789. DebugAssert(TransferredSize <= TransferSize);
  790. return (TransferredSize == TransferSize);
  791. }
  792. //---------------------------------------------------------------------------
  793. void __fastcall TFileOperationProgressType::SetAsciiTransfer(bool AAsciiTransfer)
  794. {
  795. FAsciiTransfer = AAsciiTransfer;
  796. DoProgress();
  797. }
  798. //---------------------------------------------------------------------------
  799. TDateTime __fastcall TFileOperationProgressType::TimeElapsed()
  800. {
  801. return Now() - StartTime;
  802. }
  803. //---------------------------------------------------------------------------
  804. unsigned int __fastcall TFileOperationProgressType::CPS()
  805. {
  806. TGuard Guard(FSection);
  807. return GetCPS();
  808. }
  809. //---------------------------------------------------------------------------
  810. // Has to be called from a guarded method
  811. unsigned int __fastcall TFileOperationProgressType::GetCPS()
  812. {
  813. unsigned int Result;
  814. if (FPersistence.Ticks.empty())
  815. {
  816. Result = 0;
  817. }
  818. else
  819. {
  820. unsigned long Ticks = (Suspended ? FSuspendTime : GetTickCount());
  821. unsigned long TimeSpan;
  822. if (Ticks < FPersistence.Ticks.front())
  823. {
  824. // clocks has wrapped, guess 10 seconds difference
  825. TimeSpan = 10000;
  826. }
  827. else
  828. {
  829. TimeSpan = (Ticks - FPersistence.Ticks.front());
  830. }
  831. if (TimeSpan == 0)
  832. {
  833. Result = 0;
  834. }
  835. else
  836. {
  837. __int64 Transferred = (FPersistence.TotalTransferred - FPersistence.TotalTransferredThen.front());
  838. Result = (unsigned int)(Transferred * MSecsPerSec / TimeSpan);
  839. }
  840. }
  841. return Result;
  842. }
  843. //---------------------------------------------------------------------------
  844. TDateTime __fastcall TFileOperationProgressType::TimeExpected()
  845. {
  846. unsigned int CurCps = CPS();
  847. if (CurCps)
  848. {
  849. return TDateTime((double)(((double)(TransferSize - TransferredSize)) / CurCps) / SecsPerDay);
  850. }
  851. else
  852. {
  853. return 0;
  854. }
  855. }
  856. //---------------------------------------------------------------------------
  857. TDateTime __fastcall TFileOperationProgressType::TotalTimeLeft()
  858. {
  859. TGuard Guard(FSection);
  860. DebugAssert(FTotalSizeSet);
  861. unsigned int CurCps = GetCPS();
  862. // sanity check
  863. __int64 Processed = FTotalSkipped + FPersistence.TotalTransferred - FTotalTransferBase;
  864. if ((CurCps > 0) && (FTotalSize > Processed))
  865. {
  866. return TDateTime((double)((double)(FTotalSize - Processed) / CurCps) / SecsPerDay);
  867. }
  868. else
  869. {
  870. return 0;
  871. }
  872. }
  873. //---------------------------------------------------------------------------
  874. __int64 __fastcall TFileOperationProgressType::GetTotalTransferred()
  875. {
  876. TGuard Guard(FSection);
  877. return FPersistence.TotalTransferred;
  878. }
  879. //---------------------------------------------------------------------------
  880. __int64 __fastcall TFileOperationProgressType::GetOperationTransferred() const
  881. {
  882. TGuard Guard(FSection);
  883. return FPersistence.TotalTransferred - FTotalTransferBase + FTotalSkipped;
  884. }
  885. //---------------------------------------------------------------------------
  886. __int64 __fastcall TFileOperationProgressType::GetTotalSize()
  887. {
  888. TGuard Guard(FSection);
  889. return FTotalSize;
  890. }
  891. //---------------------------------------------------------------------------
  892. void __fastcall TFileOperationProgressType::LockUserSelections()
  893. {
  894. if (FParent != NULL)
  895. {
  896. FParent->LockUserSelections();
  897. }
  898. else
  899. {
  900. FUserSelectionsSection->Enter();
  901. }
  902. }
  903. //---------------------------------------------------------------------------
  904. void __fastcall TFileOperationProgressType::UnlockUserSelections()
  905. {
  906. if (FParent != NULL)
  907. {
  908. FParent->UnlockUserSelections();
  909. }
  910. else
  911. {
  912. FUserSelectionsSection->Leave();
  913. }
  914. }
  915. //---------------------------------------------------------------------------
  916. UnicodeString __fastcall TFileOperationProgressType::GetLogStr(bool Done)
  917. {
  918. UnicodeString Transferred = FormatSize(TotalTransferred);
  919. UnicodeString Left;
  920. TDateTime Time;
  921. UnicodeString TimeLabel;
  922. if (!Done && TotalSizeSet)
  923. {
  924. Time = TotalTimeLeft();
  925. TimeLabel = L"Left";
  926. }
  927. else
  928. {
  929. Time = TimeElapsed();
  930. TimeLabel = L"Elapsed";
  931. }
  932. UnicodeString TimeStr = FormatDateTimeSpan(Configuration->TimeFormat, Time);
  933. UnicodeString CPSStr = FormatSize(CPS());
  934. return FORMAT(L"Transferred: %s, %s: %s, CPS: %s/s", (Transferred, TimeLabel, TimeStr, CPSStr));
  935. }
  936. //---------------------------------------------------------------------------
  937. void __fastcall TFileOperationProgressType::Store(TPersistence & Persistence)
  938. {
  939. // in this current use, we do not need this to be guarded actually, as it's used only for a single-threaded synchronization
  940. TGuard Guard(FSection);
  941. Persistence = FPersistence;
  942. }
  943. //---------------------------------------------------------------------------
  944. void __fastcall TFileOperationProgressType::Restore(TPersistence & Persistence)
  945. {
  946. TGuard Guard(FSection);
  947. FPersistence = Persistence;
  948. FRestored = true;
  949. }