FileOperationProgress.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 4096
  9. //---------------------------------------------------------------------------
  10. __fastcall TFileOperationProgressType::TFileOperationProgressType()
  11. {
  12. FOnProgress = NULL;
  13. FOnFinished = NULL;
  14. Clear();
  15. }
  16. //---------------------------------------------------------------------------
  17. __fastcall TFileOperationProgressType::TFileOperationProgressType(
  18. TFileOperationProgressEvent AOnProgress, TFileOperationFinished AOnFinished)
  19. {
  20. FOnProgress = AOnProgress;
  21. FOnFinished = AOnFinished;
  22. FReset = false;
  23. Clear();
  24. }
  25. //---------------------------------------------------------------------------
  26. __fastcall TFileOperationProgressType::~TFileOperationProgressType()
  27. {
  28. assert(!InProgress || FReset);
  29. assert(!Suspended || FReset);
  30. }
  31. //---------------------------------------------------------------------------
  32. void __fastcall TFileOperationProgressType::AssignButKeepSuspendState(const TFileOperationProgressType & Other)
  33. {
  34. TValueRestorer<unsigned int> SuspendTimeRestorer(FSuspendTime);
  35. TValueRestorer<bool> SuspendedRestorer(Suspended);
  36. *this = Other;
  37. }
  38. //---------------------------------------------------------------------------
  39. void __fastcall TFileOperationProgressType::Clear()
  40. {
  41. FileName = L"";
  42. AsciiTransfer = false;
  43. ResumeStatus = rsNotAvailable;
  44. Count = 0;
  45. FFilesFinished = 0;
  46. StartTime = Now();
  47. Suspended = false;
  48. FSuspendTime = 0;
  49. InProgress = false;
  50. FileInProgress = false;
  51. TotalTransfered = 0;
  52. TotalSkipped = 0;
  53. TotalSize = 0;
  54. SkippedSize = 0;
  55. TotalSizeSet = false;
  56. Operation = foNone;
  57. Temp = false;
  58. SkipToAll = false;
  59. BatchOverwrite = boNo;
  60. // to bypass check in ClearTransfer()
  61. TransferSize = 0;
  62. CPSLimit = 0;
  63. FTicks.clear();
  64. FTotalTransferredThen.clear();
  65. FCounterSet = false;
  66. ClearTransfer();
  67. }
  68. //---------------------------------------------------------------------------
  69. void __fastcall TFileOperationProgressType::ClearTransfer()
  70. {
  71. if ((TransferSize > 0) && (TransferedSize < TransferSize))
  72. {
  73. __int64 RemainingSize = (TransferSize - TransferedSize);
  74. TotalSkipped += RemainingSize;
  75. }
  76. LocalSize = 0;
  77. TransferSize = 0;
  78. LocallyUsed = 0;
  79. SkippedSize = 0;
  80. TransferedSize = 0;
  81. TransferingFile = false;
  82. FLastSecond = 0;
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TFileOperationProgressType::Start(TFileOperation AOperation,
  86. TOperationSide ASide, int ACount)
  87. {
  88. Start(AOperation, ASide, ACount, false, L"", 0);
  89. }
  90. //---------------------------------------------------------------------------
  91. void __fastcall TFileOperationProgressType::Start(TFileOperation AOperation,
  92. TOperationSide ASide, int ACount, bool ATemp,
  93. const UnicodeString ADirectory, unsigned long ACPSLimit)
  94. {
  95. Clear();
  96. Operation = AOperation;
  97. Side = ASide;
  98. Count = ACount;
  99. InProgress = true;
  100. Cancel = csContinue;
  101. Directory = ADirectory;
  102. Temp = ATemp;
  103. CPSLimit = ACPSLimit;
  104. try
  105. {
  106. DoProgress();
  107. }
  108. catch (...)
  109. {
  110. // connection can be lost during progress callbacks
  111. ClearTransfer();
  112. InProgress = false;
  113. throw;
  114. }
  115. }
  116. //---------------------------------------------------------------------------
  117. void __fastcall TFileOperationProgressType::Reset()
  118. {
  119. FReset = true;
  120. }
  121. //---------------------------------------------------------------------------
  122. void __fastcall TFileOperationProgressType::Stop()
  123. {
  124. // added to include remaining bytes to TotalSkipped, in case
  125. // the progress happens to update before closing
  126. ClearTransfer();
  127. InProgress = false;
  128. DoProgress();
  129. }
  130. //---------------------------------------------------------------------------
  131. void __fastcall TFileOperationProgressType::Suspend()
  132. {
  133. assert(!Suspended);
  134. Suspended = true;
  135. FSuspendTime = GetTickCount();
  136. DoProgress();
  137. }
  138. //---------------------------------------------------------------------------
  139. void __fastcall TFileOperationProgressType::Resume()
  140. {
  141. assert(Suspended);
  142. Suspended = false;
  143. // shift timestamps for CPS calculation in advance
  144. // by the time the progress was suspended
  145. unsigned long Stopped = (GetTickCount() - FSuspendTime);
  146. size_t i = 0;
  147. while (i < FTicks.size())
  148. {
  149. FTicks[i] += Stopped;
  150. ++i;
  151. }
  152. DoProgress();
  153. }
  154. //---------------------------------------------------------------------------
  155. int __fastcall TFileOperationProgressType::OperationProgress()
  156. {
  157. assert(Count);
  158. int Result = (FFilesFinished * 100)/Count;
  159. return Result;
  160. }
  161. //---------------------------------------------------------------------------
  162. int __fastcall TFileOperationProgressType::TransferProgress()
  163. {
  164. int Result;
  165. if (TransferSize)
  166. {
  167. Result = (int)((TransferedSize * 100)/TransferSize);
  168. }
  169. else
  170. {
  171. Result = 0;
  172. }
  173. return Result;
  174. }
  175. //---------------------------------------------------------------------------
  176. int __fastcall TFileOperationProgressType::TotalTransferProgress()
  177. {
  178. assert(TotalSizeSet);
  179. int Result = TotalSize > 0 ? (int)(((TotalTransfered + TotalSkipped) * 100)/TotalSize) : 0;
  180. return Result < 100 ? Result : 100;
  181. }
  182. //---------------------------------------------------------------------------
  183. int __fastcall TFileOperationProgressType::OverallProgress()
  184. {
  185. if (TotalSizeSet)
  186. {
  187. assert((Operation == foCopy) || (Operation == foMove));
  188. return TotalTransferProgress();
  189. }
  190. else
  191. {
  192. return OperationProgress();
  193. }
  194. }
  195. //---------------------------------------------------------------------------
  196. void __fastcall TFileOperationProgressType::Progress()
  197. {
  198. DoProgress();
  199. }
  200. //---------------------------------------------------------------------------
  201. void __fastcall TFileOperationProgressType::DoProgress()
  202. {
  203. SetThreadExecutionState(ES_SYSTEM_REQUIRED);
  204. FOnProgress(*this);
  205. }
  206. //---------------------------------------------------------------------------
  207. void __fastcall TFileOperationProgressType::Finish(UnicodeString FileName,
  208. bool Success, TOnceDoneOperation & OnceDoneOperation)
  209. {
  210. assert(InProgress);
  211. FOnFinished(Operation, Side, Temp, FileName,
  212. /* TODO : There wasn't 'Success' condition, was it by mistake or by purpose? */
  213. Success && (Cancel == csContinue), OnceDoneOperation);
  214. FFilesFinished++;
  215. DoProgress();
  216. }
  217. //---------------------------------------------------------------------------
  218. void __fastcall TFileOperationProgressType::SetFile(UnicodeString AFileName, bool AFileInProgress)
  219. {
  220. FullFileName = AFileName;
  221. if (Side == osRemote)
  222. {
  223. // historically set were passing filename-only for remote site operations,
  224. // now we need to collect a full paths, so we pass in full path,
  225. // but still want to have filename-only in FileName
  226. AFileName = UnixExtractFileName(AFileName);
  227. }
  228. FileName = AFileName;
  229. FileInProgress = AFileInProgress;
  230. ClearTransfer();
  231. FFileStartTime = Now();
  232. DoProgress();
  233. }
  234. //---------------------------------------------------------------------------
  235. void __fastcall TFileOperationProgressType::SetFileInProgress()
  236. {
  237. assert(!FileInProgress);
  238. FileInProgress = true;
  239. DoProgress();
  240. }
  241. //---------------------------------------------------------------------------
  242. void __fastcall TFileOperationProgressType::SetLocalSize(__int64 ASize)
  243. {
  244. LocalSize = ASize;
  245. DoProgress();
  246. }
  247. //---------------------------------------------------------------------------
  248. void __fastcall TFileOperationProgressType::AddLocallyUsed(__int64 ASize)
  249. {
  250. LocallyUsed += ASize;
  251. if (LocallyUsed > LocalSize)
  252. {
  253. LocalSize = LocallyUsed;
  254. }
  255. DoProgress();
  256. }
  257. //---------------------------------------------------------------------------
  258. bool __fastcall TFileOperationProgressType::IsLocallyDone()
  259. {
  260. assert(LocallyUsed <= LocalSize);
  261. return (LocallyUsed == LocalSize);
  262. }
  263. //---------------------------------------------------------------------------
  264. void __fastcall TFileOperationProgressType::SetSpeedCounters()
  265. {
  266. if ((CPSLimit > 0) && !FCounterSet)
  267. {
  268. FCounterSet = true;
  269. Configuration->Usage->Inc(L"SpeedLimitUses");
  270. }
  271. }
  272. //---------------------------------------------------------------------------
  273. void __fastcall TFileOperationProgressType::ThrottleToCPSLimit(
  274. unsigned long Size)
  275. {
  276. unsigned long Remaining = Size;
  277. while (Remaining > 0)
  278. {
  279. Remaining -= AdjustToCPSLimit(Remaining);
  280. }
  281. }
  282. //---------------------------------------------------------------------------
  283. unsigned long __fastcall TFileOperationProgressType::AdjustToCPSLimit(
  284. unsigned long Size)
  285. {
  286. SetSpeedCounters();
  287. if (CPSLimit > 0)
  288. {
  289. // we must not return 0, hence, if we reach zero,
  290. // we wait until the next second
  291. do
  292. {
  293. unsigned int Second = (GetTickCount() / MSecsPerSec);
  294. if (Second != FLastSecond)
  295. {
  296. FRemainingCPS = CPSLimit;
  297. FLastSecond = Second;
  298. }
  299. if (FRemainingCPS == 0)
  300. {
  301. SleepEx(100, true);
  302. DoProgress();
  303. }
  304. }
  305. while ((CPSLimit > 0) && (FRemainingCPS == 0));
  306. // CPSLimit may have been dropped in DoProgress
  307. if (CPSLimit > 0)
  308. {
  309. if (FRemainingCPS < Size)
  310. {
  311. Size = FRemainingCPS;
  312. }
  313. FRemainingCPS -= Size;
  314. }
  315. }
  316. return Size;
  317. }
  318. //---------------------------------------------------------------------------
  319. unsigned long __fastcall TFileOperationProgressType::LocalBlockSize()
  320. {
  321. unsigned long Result = TRANSFER_BUF_SIZE;
  322. if (LocallyUsed + Result > LocalSize)
  323. {
  324. Result = (unsigned long)(LocalSize - LocallyUsed);
  325. }
  326. Result = AdjustToCPSLimit(Result);
  327. return Result;
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall TFileOperationProgressType::SetTotalSize(__int64 ASize)
  331. {
  332. TotalSize = ASize;
  333. TotalSizeSet = true;
  334. DoProgress();
  335. }
  336. //---------------------------------------------------------------------------
  337. void __fastcall TFileOperationProgressType::SetTransferSize(__int64 ASize)
  338. {
  339. TransferSize = ASize;
  340. DoProgress();
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TFileOperationProgressType::ChangeTransferSize(__int64 ASize)
  344. {
  345. // reflect change on file size (due to text transfer mode conversion particulary)
  346. // on total transfer size
  347. if (TotalSizeSet)
  348. {
  349. TotalSize += (ASize - TransferSize);
  350. }
  351. TransferSize = ASize;
  352. DoProgress();
  353. }
  354. //---------------------------------------------------------------------------
  355. void __fastcall TFileOperationProgressType::RollbackTransfer()
  356. {
  357. TransferedSize -= SkippedSize;
  358. assert(TransferedSize <= TotalTransfered);
  359. TotalTransfered -= TransferedSize;
  360. assert(SkippedSize <= TotalSkipped);
  361. FTicks.clear();
  362. FTotalTransferredThen.clear();
  363. TotalSkipped -= SkippedSize;
  364. SkippedSize = 0;
  365. TransferedSize = 0;
  366. TransferSize = 0;
  367. LocallyUsed = 0;
  368. }
  369. //---------------------------------------------------------------------------
  370. void __fastcall TFileOperationProgressType::AddTransfered(__int64 ASize,
  371. bool AddToTotals)
  372. {
  373. TransferedSize += ASize;
  374. if (TransferedSize > TransferSize)
  375. {
  376. // this can happen with SFTP when downloading file that
  377. // grows while being downloaded
  378. if (TotalSizeSet)
  379. {
  380. // we should probably guard this with AddToTotals
  381. TotalSize += (TransferedSize - TransferSize);
  382. }
  383. TransferSize = TransferedSize;
  384. }
  385. if (AddToTotals)
  386. {
  387. TotalTransfered += ASize;
  388. unsigned long Ticks = GetTickCount();
  389. if (FTicks.empty() ||
  390. (FTicks.back() > Ticks) || // ticks wrap after 49.7 days
  391. ((Ticks - FTicks.back()) >= MSecsPerSec))
  392. {
  393. FTicks.push_back(Ticks);
  394. FTotalTransferredThen.push_back(TotalTransfered);
  395. }
  396. if (FTicks.size() > 10)
  397. {
  398. FTicks.erase(FTicks.begin());
  399. FTotalTransferredThen.erase(FTotalTransferredThen.begin());
  400. }
  401. }
  402. DoProgress();
  403. }
  404. //---------------------------------------------------------------------------
  405. void __fastcall TFileOperationProgressType::AddResumed(__int64 ASize)
  406. {
  407. TotalSkipped += ASize;
  408. SkippedSize += ASize;
  409. AddTransfered(ASize, false);
  410. AddLocallyUsed(ASize);
  411. }
  412. //---------------------------------------------------------------------------
  413. void __fastcall TFileOperationProgressType::AddSkippedFileSize(__int64 ASize)
  414. {
  415. TotalSkipped += ASize;
  416. DoProgress();
  417. }
  418. //---------------------------------------------------------------------------
  419. unsigned long __fastcall TFileOperationProgressType::TransferBlockSize()
  420. {
  421. unsigned long Result = TRANSFER_BUF_SIZE;
  422. if (TransferedSize + Result > TransferSize)
  423. {
  424. Result = (unsigned long)(TransferSize - TransferedSize);
  425. }
  426. Result = AdjustToCPSLimit(Result);
  427. return Result;
  428. }
  429. //---------------------------------------------------------------------------
  430. unsigned long __fastcall TFileOperationProgressType::StaticBlockSize()
  431. {
  432. return TRANSFER_BUF_SIZE;
  433. }
  434. //---------------------------------------------------------------------------
  435. bool __fastcall TFileOperationProgressType::IsTransferDone()
  436. {
  437. assert(TransferedSize <= TransferSize);
  438. return (TransferedSize == TransferSize);
  439. }
  440. //---------------------------------------------------------------------------
  441. void __fastcall TFileOperationProgressType::SetAsciiTransfer(bool AAsciiTransfer)
  442. {
  443. AsciiTransfer = AAsciiTransfer;
  444. DoProgress();
  445. }
  446. //---------------------------------------------------------------------------
  447. void __fastcall TFileOperationProgressType::SetResumeStatus(TResumeStatus AResumeStatus)
  448. {
  449. ResumeStatus = AResumeStatus;
  450. DoProgress();
  451. }
  452. //---------------------------------------------------------------------------
  453. TDateTime __fastcall TFileOperationProgressType::TimeElapsed()
  454. {
  455. return Now() - StartTime;
  456. }
  457. //---------------------------------------------------------------------------
  458. unsigned int __fastcall TFileOperationProgressType::CPS()
  459. {
  460. unsigned int Result;
  461. if (FTicks.empty())
  462. {
  463. Result = 0;
  464. }
  465. else
  466. {
  467. unsigned long Ticks = (Suspended ? FSuspendTime : GetTickCount());
  468. unsigned long TimeSpan;
  469. if (Ticks < FTicks.front())
  470. {
  471. // clocks has wrapped, guess 10 seconds difference
  472. TimeSpan = 10000;
  473. }
  474. else
  475. {
  476. TimeSpan = (Ticks - FTicks.front());
  477. }
  478. if (TimeSpan == 0)
  479. {
  480. Result = 0;
  481. }
  482. else
  483. {
  484. __int64 Transferred = (TotalTransfered - FTotalTransferredThen.front());
  485. Result = (unsigned int)(Transferred * MSecsPerSec / TimeSpan);
  486. }
  487. }
  488. return Result;
  489. }
  490. //---------------------------------------------------------------------------
  491. TDateTime __fastcall TFileOperationProgressType::TimeExpected()
  492. {
  493. unsigned int CurCps = CPS();
  494. if (CurCps)
  495. {
  496. return TDateTime((double)(((double)(TransferSize - TransferedSize)) / CurCps) / SecsPerDay);
  497. }
  498. else
  499. {
  500. return 0;
  501. }
  502. }
  503. //---------------------------------------------------------------------------
  504. TDateTime __fastcall TFileOperationProgressType::TotalTimeExpected()
  505. {
  506. assert(TotalSizeSet);
  507. unsigned int CurCps = CPS();
  508. // sanity check
  509. if ((CurCps > 0) && (TotalSize > TotalSkipped))
  510. {
  511. return TDateTime((double)((double)(TotalSize - TotalSkipped) / CurCps) /
  512. SecsPerDay);
  513. }
  514. else
  515. {
  516. return 0;
  517. }
  518. }
  519. //---------------------------------------------------------------------------
  520. TDateTime __fastcall TFileOperationProgressType::TotalTimeLeft()
  521. {
  522. assert(TotalSizeSet);
  523. unsigned int CurCps = CPS();
  524. // sanity check
  525. if ((CurCps > 0) && (TotalSize > TotalSkipped + TotalTransfered))
  526. {
  527. return TDateTime((double)((double)(TotalSize - TotalSkipped - TotalTransfered) / CurCps) /
  528. SecsPerDay);
  529. }
  530. else
  531. {
  532. return 0;
  533. }
  534. }