1
0

FileOperationProgress.cpp 16 KB

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