EditorManager.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <CoreMain.h>
  6. #include <TextsWin.h>
  7. #include <SessionData.h>
  8. #include "WinConfiguration.h"
  9. #include "EditorManager.h"
  10. #include <algorithm>
  11. #include <DateUtils.hpp>
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. //---------------------------------------------------------------------------
  15. TEditedFileData::TEditedFileData()
  16. {
  17. ForceText = false;
  18. Terminal = NULL;
  19. SessionData = NULL;
  20. }
  21. //---------------------------------------------------------------------------
  22. TEditedFileData::~TEditedFileData()
  23. {
  24. delete SessionData;
  25. }
  26. //---------------------------------------------------------------------------
  27. __fastcall TEditorManager::TEditorManager()
  28. {
  29. FSection = new TCriticalSection();
  30. FOnFileChange = NULL;
  31. FOnFileReload = NULL;
  32. FOnFileEarlyClosed = NULL;
  33. FOnFileUploadComplete = NULL;
  34. }
  35. //---------------------------------------------------------------------------
  36. __fastcall TEditorManager::~TEditorManager()
  37. {
  38. for (unsigned int i = FFiles.size(); i > 0; i--)
  39. {
  40. int Index = i - 1;
  41. TFileData * FileData = &FFiles[Index];
  42. // pending should be only external editors and files being uploaded
  43. DebugAssert(FileData->Closed || FileData->External);
  44. if (!FileData->Closed)
  45. {
  46. if (!CloseFile(Index, true, true))
  47. {
  48. ReleaseFile(Index);
  49. }
  50. }
  51. }
  52. delete FSection;
  53. }
  54. //---------------------------------------------------------------------------
  55. bool __fastcall TEditorManager::Empty(bool IgnoreClosed)
  56. {
  57. TGuard Guard(FSection);
  58. bool Result;
  59. if (!IgnoreClosed)
  60. {
  61. Result = (FFiles.size() == 0);
  62. }
  63. else
  64. {
  65. Result = true;
  66. for (unsigned int i = 0; i < FFiles.size(); i++)
  67. {
  68. if (!FFiles[i].Closed)
  69. {
  70. Result = false;
  71. break;
  72. }
  73. }
  74. }
  75. return Result;
  76. }
  77. //---------------------------------------------------------------------------
  78. bool __fastcall TEditorManager::CanAddFile(const UnicodeString RemoteDirectory,
  79. const UnicodeString OriginalFileName, const UnicodeString SessionName,
  80. TObject *& Token, UnicodeString & ExistingLocalRootDirectory,
  81. UnicodeString & ExistingLocalDirectory)
  82. {
  83. TGuard Guard(FSection);
  84. bool Result = true;
  85. Token = NULL;
  86. for (unsigned int i = 0; i < FFiles.size(); i++)
  87. {
  88. TFileData * FileData = &FFiles[i];
  89. // include even "closed" (=being uploaded) files as it is nonsense
  90. // to download file being uploaded
  91. if ((FileData->Data->RemoteDirectory == RemoteDirectory) &&
  92. (FileData->Data->OriginalFileName == OriginalFileName) &&
  93. (FileData->Data->SessionName == SessionName))
  94. {
  95. if (!FileData->External)
  96. {
  97. Result = false;
  98. if (!FileData->Closed && (FileData->Token != NULL))
  99. {
  100. Token = FileData->Token;
  101. }
  102. }
  103. else
  104. {
  105. // MDI editor?
  106. if (FileData->Process == INVALID_HANDLE_VALUE)
  107. {
  108. // file is just being uploaded, do not allow new editor instance
  109. if (FileData->Closed)
  110. {
  111. Result = false;
  112. }
  113. else
  114. {
  115. UnicodeString AExistingLocalDirectory = ExtractFilePath(FileData->FileName);
  116. // If the temporary directory was deleted, behave like the file was never opened
  117. if (DirectoryExists(AExistingLocalDirectory))
  118. {
  119. // get directory where the file already is so we download it there again
  120. ExistingLocalRootDirectory = FileData->Data->LocalRootDirectory;
  121. ExistingLocalDirectory = AExistingLocalDirectory;
  122. }
  123. CloseFile(i, false, false); // do not delete file
  124. Result = true;
  125. }
  126. }
  127. else
  128. {
  129. Result = false;
  130. }
  131. }
  132. break;
  133. }
  134. }
  135. if (Result)
  136. {
  137. if (FFiles.size() >= WinConfiguration->Editor.MaxEditors)
  138. {
  139. throw Exception(LoadStr(TOO_MANY_EDITORS));
  140. }
  141. }
  142. return Result;
  143. }
  144. //---------------------------------------------------------------------------
  145. TEditedFileData * TEditorManager::FindByUploadCompleteEvent(HANDLE UploadCompleteEvent)
  146. {
  147. TGuard Guard(FSection);
  148. for (unsigned int i = 0; i < FFiles.size(); i++)
  149. {
  150. TFileData * FileData = &FFiles[i];
  151. if (FileData->UploadCompleteEvent == UploadCompleteEvent)
  152. {
  153. return FileData->Data;
  154. }
  155. }
  156. return NULL;
  157. }
  158. //---------------------------------------------------------------------------
  159. void __fastcall TEditorManager::ProcessFiles(TEditedFileProcessEvent Callback, void * Arg)
  160. {
  161. TGuard Guard(FSection);
  162. for (unsigned int i = 0; i < FFiles.size(); i++)
  163. {
  164. TFileData * FileData = &FFiles[i];
  165. Callback(FileData->FileName, FileData->Data,
  166. (FileData->Closed ? NULL : FileData->Token), Arg);
  167. }
  168. }
  169. //---------------------------------------------------------------------------
  170. bool __fastcall TEditorManager::CloseInternalEditors(TNotifyEvent CloseCallback)
  171. {
  172. TGuard Guard(FSection);
  173. // Traverse from end, as closing internal editor causes deletion of
  174. // respective file vector element.
  175. TObject * PrevToken = NULL;
  176. for (unsigned int i = FFiles.size(); i > 0; i--)
  177. {
  178. // Note that element may be deleted by external cause (like if external editor
  179. // is closed while "save confirmation" message is displayed).
  180. if (i <= FFiles.size())
  181. {
  182. TFileData * FileData = &FFiles[i - 1];
  183. // PrevToken is simple check not to ask same editor twice, however
  184. // it does not solve all posibilities
  185. if (!FileData->Closed && (FileData->Token != NULL) &&
  186. (FileData->Token != PrevToken))
  187. {
  188. CloseCallback(FileData->Token);
  189. }
  190. }
  191. }
  192. bool Result = true;
  193. for (unsigned int i = 0; i < FFiles.size(); i++)
  194. {
  195. TFileData * FileData = &FFiles[i];
  196. if (!FileData->Closed && (FileData->Token != NULL))
  197. {
  198. Result = false;
  199. break;
  200. }
  201. }
  202. return Result;
  203. }
  204. //---------------------------------------------------------------------------
  205. bool __fastcall TEditorManager::CloseExternalFilesWithoutProcess()
  206. {
  207. TGuard Guard(FSection);
  208. for (unsigned int i = FFiles.size(); i > 0; i--)
  209. {
  210. TFileData * FileData = &FFiles[i - 1];
  211. if (!FileData->Closed && FileData->External &&
  212. (FileData->Process == INVALID_HANDLE_VALUE))
  213. {
  214. CloseFile(i - 1, true, true);
  215. }
  216. }
  217. return true;
  218. }
  219. //---------------------------------------------------------------------------
  220. void __fastcall TEditorManager::AddFileInternal(const UnicodeString FileName,
  221. TEditedFileData * AData, TObject * Token)
  222. {
  223. TGuard Guard(FSection);
  224. std::unique_ptr<TEditedFileData> Data(AData);
  225. TFileData FileData;
  226. FileData.FileName = FileName;
  227. FileData.External = false;
  228. FileData.Process = INVALID_HANDLE_VALUE;
  229. FileData.Token = Token;
  230. AddFile(FileData, Data.release());
  231. }
  232. //---------------------------------------------------------------------------
  233. void __fastcall TEditorManager::AddFileExternal(const UnicodeString FileName,
  234. TEditedFileData * AData, HANDLE Process)
  235. {
  236. TGuard Guard(FSection);
  237. std::unique_ptr<TEditedFileData> Data(AData);
  238. TFileData FileData;
  239. FileData.FileName = FileName;
  240. FileData.External = true;
  241. FileData.Process = Process;
  242. FileData.Token = NULL;
  243. UnicodeString FilePath = ExtractFilePath(FileData.FileName);
  244. if (Process != INVALID_HANDLE_VALUE)
  245. {
  246. FProcesses.push_back(Process);
  247. }
  248. AddFile(FileData, Data.release());
  249. }
  250. //---------------------------------------------------------------------------
  251. void __fastcall TEditorManager::Check()
  252. {
  253. TGuard Guard(FSection);
  254. int Index;
  255. for (Index = 0; Index < static_cast<int>(FFiles.size()); Index++)
  256. {
  257. TDateTime NewTimestamp;
  258. if (HasFileChanged(Index, NewTimestamp))
  259. {
  260. TDateTime N = NormalizeTimestamp(Now());
  261. // Let the editor finish writing to the file
  262. // (first to avoid uploading partially saved file, second
  263. // because the timestamp may change more than once during saving).
  264. // WORKAROUND WithinPastMilliSeconds seems buggy that it return true even if NewTimestamp is within future
  265. if ((NewTimestamp <= N) &&
  266. !WithinPastMilliSeconds(N, NewTimestamp, GUIConfiguration->KeepUpToDateChangeDelay))
  267. {
  268. CheckFileChange(Index, false);
  269. }
  270. }
  271. }
  272. if (FProcesses.size() > 0)
  273. {
  274. do
  275. {
  276. Index = WaitFor(FProcesses.size(), &(FProcesses[0]), PROCESS);
  277. if (Index >= 0)
  278. {
  279. try
  280. {
  281. CheckFileChange(Index, false);
  282. }
  283. __finally
  284. {
  285. if (!EarlyClose(Index))
  286. {
  287. // CheckFileChange may fail,
  288. // but we want to close handles anyway
  289. CloseFile(Index, false, true);
  290. }
  291. }
  292. }
  293. }
  294. while ((Index >= 0) && (FProcesses.size() > 0));
  295. }
  296. if (FUploadCompleteEvents.size() > 0)
  297. {
  298. do
  299. {
  300. Index = WaitFor(FUploadCompleteEvents.size(), &(FUploadCompleteEvents[0]),
  301. EVENT);
  302. if (Index >= 0)
  303. {
  304. UploadComplete(Index, false);
  305. }
  306. }
  307. while ((Index >= 0) && (FUploadCompleteEvents.size() > 0));
  308. }
  309. }
  310. //---------------------------------------------------------------------------
  311. bool __fastcall TEditorManager::EarlyClose(int Index)
  312. {
  313. TFileData * FileData = &FFiles[Index];
  314. bool Result =
  315. (FileData->Process != INVALID_HANDLE_VALUE) &&
  316. (Now() - FileData->Opened <=
  317. TDateTime(0, 0, static_cast<unsigned short>(WinConfiguration->Editor.EarlyClose), 0)) &&
  318. (FOnFileEarlyClosed != NULL);
  319. if (Result)
  320. {
  321. Result = false;
  322. FOnFileEarlyClosed(FileData->Data, Result);
  323. if (Result)
  324. {
  325. // forget the associated process
  326. CloseProcess(Index);
  327. }
  328. }
  329. return Result;
  330. }
  331. //---------------------------------------------------------------------------
  332. void __fastcall TEditorManager::FileChanged(TObject * Token)
  333. {
  334. TGuard Guard(FSection);
  335. int Index = FindFile(Token);
  336. DebugAssert(Index >= 0);
  337. DebugAssert(!FFiles[Index].External);
  338. CheckFileChange(Index, true);
  339. }
  340. //---------------------------------------------------------------------------
  341. void __fastcall TEditorManager::FileReload(TObject * Token)
  342. {
  343. TGuard Guard(FSection);
  344. int Index = FindFile(Token);
  345. DebugAssert(Index >= 0);
  346. TFileData * FileData = &FFiles[Index];
  347. DebugAssert(!FileData->External);
  348. TAutoFlag ReloadingFlag(FileData->Reloading);
  349. OnFileReload(FileData->FileName, FileData->Data);
  350. GetFileTimestamp(FileData->FileName, FileData->Timestamp);
  351. }
  352. //---------------------------------------------------------------------------
  353. void __fastcall TEditorManager::FileClosed(TObject * Token, bool Forced)
  354. {
  355. TGuard Guard(FSection);
  356. int Index = FindFile(Token);
  357. DebugAssert(Index >= 0);
  358. DebugAssert(!FFiles[Index].External);
  359. CheckFileChange(Index, false);
  360. CloseFile(Index, false, !Forced);
  361. }
  362. //---------------------------------------------------------------------------
  363. void __fastcall TEditorManager::AddFile(TFileData & FileData, TEditedFileData * AData)
  364. {
  365. std::unique_ptr<TEditedFileData> Data(AData);
  366. GetFileTimestamp(FileData.FileName, FileData.Timestamp);
  367. FileData.Closed = false;
  368. FileData.UploadCompleteEvent = INVALID_HANDLE_VALUE;
  369. FileData.Opened = Now();
  370. FileData.Reupload = false;
  371. FileData.Reloading = false;
  372. FileData.Saves = 0;
  373. FileData.Data = Data.get();
  374. FFiles.push_back(FileData);
  375. Data.release(); // ownership passed
  376. }
  377. //---------------------------------------------------------------------------
  378. void TEditorManager::UploadComplete(int Index, bool Retry)
  379. {
  380. TFileData * FileData = &FFiles[Index];
  381. DebugAssert(FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE);
  382. CloseHandle(FileData->UploadCompleteEvent);
  383. FUploadCompleteEvents.erase(std::find(FUploadCompleteEvents.begin(),
  384. FUploadCompleteEvents.end(), FileData->UploadCompleteEvent));
  385. FileData->UploadCompleteEvent = INVALID_HANDLE_VALUE;
  386. if (FileData->Closed)
  387. {
  388. CloseFile(Index, false, true);
  389. }
  390. else
  391. {
  392. if (FileData->Reupload)
  393. {
  394. FileData->Reupload = false;
  395. CheckFileChange(Index, true);
  396. }
  397. // so far used only to signal to the internal editor that saving was complete,
  398. // so we do signal once an actual upload is done only
  399. else if ((FileData->Token != NULL) && (FOnFileUploadComplete != NULL) && !Retry)
  400. {
  401. FOnFileUploadComplete(FileData->Token);
  402. }
  403. }
  404. }
  405. //---------------------------------------------------------------------------
  406. void __fastcall TEditorManager::CloseProcess(int Index)
  407. {
  408. TFileData * FileData = &FFiles[Index];
  409. FProcesses.erase(std::find(FProcesses.begin(), FProcesses.end(), FileData->Process));
  410. DebugCheck(CloseHandle(FileData->Process));
  411. FileData->Process = INVALID_HANDLE_VALUE;
  412. }
  413. //---------------------------------------------------------------------------
  414. void __fastcall TEditorManager::ReleaseFile(int Index)
  415. {
  416. TFileData * FileData = &FFiles[Index];
  417. delete FileData->Data;
  418. FileData->Data = NULL;
  419. }
  420. //---------------------------------------------------------------------------
  421. bool __fastcall TEditorManager::CloseFile(int Index, bool IgnoreErrors, bool Delete)
  422. {
  423. bool Result = false;
  424. TFileData * FileData = &FFiles[Index];
  425. if (FileData->Process != INVALID_HANDLE_VALUE)
  426. {
  427. CloseProcess(Index);
  428. }
  429. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  430. {
  431. FileData->Closed = true;
  432. AppLogFmt(L"Opened/edited file \"%s\" has been closed, but the file is still being uploaded.", (FileData->FileName));
  433. }
  434. else
  435. {
  436. UnicodeString LocalRootDirectory = FileData->Data->LocalRootDirectory;
  437. UnicodeString FileName = FileData->FileName; // Before it's released
  438. ReleaseFile(Index);
  439. FFiles.erase(FFiles.begin() + Index);
  440. Result = true;
  441. if (Delete && !LocalRootDirectory.IsEmpty())
  442. {
  443. if (!RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory)) &&
  444. !IgnoreErrors)
  445. {
  446. throw Exception(FMTLOAD(DELETE_TEMP_EXECUTE_FILE_ERROR, (LocalRootDirectory)));
  447. }
  448. AppLogFmt(L"Deleted opened/edited file [%s] folder \"%s\".", (FileName, LocalRootDirectory));
  449. }
  450. else
  451. {
  452. AppLogFmt(L"Opened/edited file \"%s\" has been closed.", (FileName));
  453. }
  454. }
  455. return Result;
  456. }
  457. //---------------------------------------------------------------------------
  458. TDateTime TEditorManager::NormalizeTimestamp(const TDateTime & Timestamp)
  459. {
  460. return TTimeZone::Local->ToUniversalTime(Timestamp);
  461. }
  462. //---------------------------------------------------------------------------
  463. bool TEditorManager::GetFileTimestamp(const UnicodeString & FileName, TDateTime & Timestamp)
  464. {
  465. TSearchRecSmart ASearchRec;
  466. bool Result = FileSearchRec(FileName, ASearchRec);
  467. if (Result)
  468. {
  469. Timestamp = NormalizeTimestamp(ASearchRec.GetLastWriteTime());
  470. }
  471. return Result;
  472. }
  473. //---------------------------------------------------------------------------
  474. bool __fastcall TEditorManager::HasFileChanged(int Index, TDateTime & NewTimestamp)
  475. {
  476. TFileData * FileData = &FFiles[Index];
  477. bool Result;
  478. if (FileData->Reloading)
  479. {
  480. Result = false;
  481. }
  482. else
  483. {
  484. Result =
  485. GetFileTimestamp(FileData->FileName, NewTimestamp) &&
  486. (FileData->Timestamp != NewTimestamp);
  487. }
  488. return Result;
  489. }
  490. //---------------------------------------------------------------------------
  491. void __fastcall TEditorManager::CheckFileChange(int Index, bool Force)
  492. {
  493. TDateTime NewTimestamp;
  494. bool Changed = HasFileChanged(Index, NewTimestamp);
  495. if (Force || Changed)
  496. {
  497. TFileData * FileData = &FFiles[Index];
  498. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  499. {
  500. AppLogFmt(L"Opened/edited file \"%s\" was changed, but it still being uploaded.", (FileData->FileName));
  501. FileData->Reupload = true;
  502. }
  503. else
  504. {
  505. if (Changed)
  506. {
  507. AppLogFmt(L"Opened/edited file \"%s\" was changed.", (FileData->FileName));
  508. }
  509. else
  510. {
  511. AppLogFmt(L"Forcing upload of opened/edited file \"%s\".", (FileData->FileName));
  512. }
  513. bool Upload = true;
  514. if (!Force)
  515. {
  516. HANDLE Handle = CreateFile(ApiPath(FileData->FileName).c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
  517. if (Handle == INVALID_HANDLE_VALUE)
  518. {
  519. int Error = GetLastError();
  520. AppLogFmt(L"Opened/edited file \"%s\" is locked, delaying upload.", (FileData->FileName));
  521. if (Error == ERROR_ACCESS_DENIED)
  522. {
  523. Upload = false;
  524. }
  525. }
  526. else
  527. {
  528. CloseHandle(Handle);
  529. }
  530. }
  531. if (Upload)
  532. {
  533. FileData->UploadCompleteEvent = CreateEvent(NULL, false, false, NULL);
  534. FUploadCompleteEvents.push_back(FileData->UploadCompleteEvent);
  535. TDateTime PrevTimestamp = FileData->Timestamp;
  536. FileData->Timestamp = NewTimestamp;
  537. FileData->Saves++;
  538. if (FileData->Saves == 1)
  539. {
  540. Configuration->Usage->Inc(L"RemoteFilesSaved");
  541. }
  542. Configuration->Usage->Inc(L"RemoteFileSaves");
  543. try
  544. {
  545. DebugAssert(OnFileChange != NULL);
  546. bool Retry = false;
  547. AppLogFmt(L"Uploading opened/edited file \"%s\".", (FileData->FileName));
  548. OnFileChange(FileData->FileName, FileData->Timestamp, FileData->Data, FileData->UploadCompleteEvent, Retry);
  549. if (Retry)
  550. {
  551. AppLogFmt(L"Will retry uploading opened/edited file \"%s\".", (FileData->FileName));
  552. UploadComplete(Index, true);
  553. FileData->Timestamp = PrevTimestamp;
  554. }
  555. }
  556. catch(...)
  557. {
  558. AppLogFmt(L"Failed to upload opened/edited file \"%s\".", (FileData->FileName));
  559. // upload failed (was not even started)
  560. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  561. {
  562. UploadComplete(Index, false);
  563. }
  564. throw;
  565. }
  566. }
  567. }
  568. }
  569. }
  570. //---------------------------------------------------------------------------
  571. int __fastcall TEditorManager::FindFile(const TObject * Token)
  572. {
  573. int Index = -1;
  574. for (unsigned int i = 0; i < FFiles.size(); i++)
  575. {
  576. if (!FFiles[i].Closed && (FFiles[i].Token == Token))
  577. {
  578. Index = i;
  579. break;
  580. }
  581. }
  582. return Index;
  583. }
  584. //---------------------------------------------------------------------------
  585. int __fastcall TEditorManager::WaitFor(unsigned int Count, const HANDLE * Handles,
  586. TWaitHandle WaitFor)
  587. {
  588. static const unsigned int Offset = MAXIMUM_WAIT_OBJECTS;
  589. int Result = -1;
  590. unsigned int Start = 0;
  591. while ((Result < 0) && (Start < Count))
  592. {
  593. unsigned int C = (Count - Start > Offset ? Offset : Count - Start);
  594. unsigned int WaitResult = WaitForMultipleObjects(C, Handles + Start, false, 0);
  595. if (WaitResult == WAIT_FAILED)
  596. {
  597. throw Exception(LoadStr(WATCH_ERROR_GENERAL));
  598. }
  599. else if (WaitResult != WAIT_TIMEOUT)
  600. {
  601. // WAIT_OBJECT_0 is zero
  602. DebugAssert(WaitResult < WAIT_OBJECT_0 + Count);
  603. HANDLE Handle = Handles[WaitResult - WAIT_OBJECT_0];
  604. for (unsigned int i = 0; i < FFiles.size(); i++)
  605. {
  606. TFileData * Data = &FFiles[i];
  607. HANDLE FHandle;
  608. switch (WaitFor)
  609. {
  610. case PROCESS:
  611. FHandle = Data->Process;
  612. break;
  613. case EVENT:
  614. FHandle = Data->UploadCompleteEvent;
  615. break;
  616. };
  617. if (FHandle == Handle)
  618. {
  619. Result = Start + i;
  620. break;
  621. }
  622. }
  623. DebugAssert(Result >= 0);
  624. }
  625. Start += C;
  626. }
  627. return Result;
  628. }
  629. //---------------------------------------------------------------------------