Terminal.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Terminal.h"
  5. #include <SysUtils.hpp>
  6. #include <FileCtrl.hpp>
  7. #include "Common.h"
  8. #include "FileBuffer.h"
  9. #include "FileSystems.h"
  10. #include "Interface.h"
  11. #include "RemoteFiles.h"
  12. #include "ScpFileSystem.h"
  13. #include "SftpFileSystem.h"
  14. #include "TextsCore.h"
  15. //---------------------------------------------------------------------------
  16. #pragma package(smart_init)
  17. //---------------------------------------------------------------------------
  18. #define COMMAND_ERROR_ARI(MESSAGE, REPEAT) \
  19. { \
  20. int Result = CommandError(&E, MESSAGE, qaRetry | qaSkip | qaAbort); \
  21. switch (Result) { \
  22. case qaRetry: { REPEAT; } break; \
  23. case qaAbort: Abort(); \
  24. } \
  25. }
  26. #define FILE_OPERATION_LOOP_EX(ALLOW_SKIP, MESSAGE, OPERATION) \
  27. FILE_OPERATION_LOOP_CUSTOM(this, ALLOW_SKIP, MESSAGE, OPERATION)
  28. //---------------------------------------------------------------------------
  29. struct TMoveFileParams
  30. {
  31. AnsiString Target;
  32. AnsiString FileMask;
  33. };
  34. //---------------------------------------------------------------------------
  35. __fastcall TTerminal::TTerminal(): TSecureShell()
  36. {
  37. FFiles = new TRemoteDirectory(this);
  38. FExceptionOnFail = 0;
  39. FInTransaction = 0;
  40. FReadCurrentDirectoryPending = false;
  41. FReadDirectoryPending = false;
  42. FUserGroupsLookedup = False;
  43. FUserGroups = new TUserGroupsList();
  44. FOnProgress = NULL;
  45. FOnFinished = NULL;
  46. FOnDeleteLocalFile = NULL;
  47. FAdditionalInfo = NULL;
  48. FUseBusyCursor = True;
  49. FLockDirectory = "";
  50. FDirectoryCache = new TRemoteDirectoryCache();
  51. FDirectoryChangesCache = NULL;
  52. FFSProtocol = cfsUnknown;
  53. }
  54. //---------------------------------------------------------------------------
  55. __fastcall TTerminal::~TTerminal()
  56. {
  57. if (SessionData->CacheDirectoryChanges && SessionData->PreserveDirectoryChanges &&
  58. (FDirectoryChangesCache != NULL))
  59. {
  60. Configuration->SaveDirectoryChangesCache(SessionData->SessionKey,
  61. FDirectoryChangesCache);
  62. }
  63. SAFE_DESTROY(FFileSystem);
  64. delete FFiles;
  65. delete FUserGroups;
  66. delete FDirectoryCache;
  67. delete FDirectoryChangesCache;
  68. delete FAdditionalInfo;
  69. }
  70. //---------------------------------------------------------------------------
  71. void __fastcall TTerminal::KeepAlive()
  72. {
  73. if (SessionData->PingType == ptDummyCommand)
  74. {
  75. LogEvent("Executing dummy command to keep session alive.");
  76. assert(Active);
  77. assert(FFileSystem != NULL);
  78. try
  79. {
  80. FFileSystem->KeepAlive();
  81. }
  82. catch(Exception & E)
  83. {
  84. if (Active)
  85. {
  86. HandleExtendedException(&E, this);
  87. }
  88. else
  89. {
  90. throw;
  91. }
  92. }
  93. }
  94. else
  95. {
  96. TSecureShell::KeepAlive();
  97. }
  98. }
  99. //---------------------------------------------------------------------------
  100. bool __fastcall TTerminal::IsAbsolutePath(const AnsiString Path)
  101. {
  102. return !Path.IsEmpty() && Path[1] == '/';
  103. }
  104. //---------------------------------------------------------------------------
  105. AnsiString __fastcall TTerminal::ExpandFileName(AnsiString Path,
  106. const AnsiString BasePath)
  107. {
  108. Path = UnixExcludeTrailingBackslash(Path);
  109. if (!IsAbsolutePath(Path) && !BasePath.IsEmpty())
  110. {
  111. // TODO: Handle more complicated cases like "../../xxx"
  112. if (Path == "..")
  113. {
  114. Path = UnixExcludeTrailingBackslash(UnixExtractFilePath(
  115. UnixExcludeTrailingBackslash(BasePath)));
  116. }
  117. else
  118. {
  119. Path = UnixIncludeTrailingBackslash(BasePath) + Path;
  120. }
  121. }
  122. return Path;
  123. }
  124. //---------------------------------------------------------------------------
  125. AnsiString __fastcall TTerminal::GetProtocolName()
  126. {
  127. assert(FFileSystem);
  128. return FFileSystem->ProtocolName;
  129. }
  130. //---------------------------------------------------------------------------
  131. void __fastcall TTerminal::Close()
  132. {
  133. // file system cannot be destoryed here, moved to destructor
  134. TSecureShell::Close();
  135. }
  136. //---------------------------------------------------------------------------
  137. void __fastcall TTerminal::Open()
  138. {
  139. TSecureShell::Open();
  140. assert(!FFileSystem);
  141. if ((SessionData->FSProtocol == fsSCPonly) ||
  142. (SessionData->FSProtocol == fsSFTP && SshFallbackCmd()))
  143. {
  144. FFSProtocol = cfsSCP;
  145. FFileSystem = new TSCPFileSystem(this);
  146. LogEvent("Using SCP protocol.");
  147. }
  148. else
  149. {
  150. FFSProtocol = cfsSFTP;
  151. FFileSystem = new TSFTPFileSystem(this);
  152. LogEvent("Using SFTP protocol.");
  153. }
  154. if (SessionData->CacheDirectoryChanges)
  155. {
  156. FDirectoryChangesCache = new TRemoteDirectoryChangesCache();
  157. if (SessionData->PreserveDirectoryChanges)
  158. {
  159. Configuration->LoadDirectoryChangesCache(SessionData->SessionKey,
  160. FDirectoryChangesCache);
  161. }
  162. }
  163. }
  164. //---------------------------------------------------------------------------
  165. bool __fastcall TTerminal::GetIsCapable(TFSCapability Capability) const
  166. {
  167. assert(FFileSystem);
  168. return FFileSystem->IsCapable(Capability);
  169. }
  170. //---------------------------------------------------------------------------
  171. TStrings * __fastcall TTerminal::GetAdditionalInfo()
  172. {
  173. bool Initial = (FAdditionalInfo == NULL);
  174. if (Initial)
  175. {
  176. FAdditionalInfo = new TStringList();
  177. }
  178. assert(FFileSystem);
  179. FFileSystem->AdditionalInfo(FAdditionalInfo, Initial);
  180. return FAdditionalInfo;
  181. }
  182. //---------------------------------------------------------------------------
  183. AnsiString __fastcall TTerminal::AbsolutePath(AnsiString Path)
  184. {
  185. return FFileSystem->AbsolutePath(Path);
  186. }
  187. //---------------------------------------------------------------------------
  188. void __fastcall TTerminal::ReactOnCommand(int /*TFSCommand*/ Cmd)
  189. {
  190. bool ChangesDirectory = false;
  191. bool ModifiesFiles = false;
  192. switch ((TFSCommand)Cmd) {
  193. case fsChangeDirectory:
  194. case fsHomeDirectory:
  195. ChangesDirectory = true;
  196. break;
  197. case fsCopyToRemote:
  198. case fsDeleteFile:
  199. case fsRenameFile:
  200. case fsMoveFile:
  201. case fsCreateDirectory:
  202. case fsChangeMode:
  203. case fsChangeGroup:
  204. case fsChangeOwner:
  205. case fsChangeProperties:
  206. case fsAnyCommand:
  207. ModifiesFiles = true;
  208. break;
  209. }
  210. if (ChangesDirectory)
  211. {
  212. if (!FInTransaction)
  213. {
  214. ReadCurrentDirectory();
  215. ReadDirectory(false);
  216. }
  217. else
  218. {
  219. FReadCurrentDirectoryPending = true;
  220. FReadDirectoryPending = true;
  221. }
  222. }
  223. else
  224. if (ModifiesFiles)
  225. {
  226. if (!FInTransaction) ReadDirectory(true);
  227. else FReadDirectoryPending = true;
  228. }
  229. }
  230. //---------------------------------------------------------------------------
  231. void __fastcall TTerminal::TerminalError(AnsiString Msg)
  232. {
  233. TerminalError(NULL, Msg);
  234. }
  235. //---------------------------------------------------------------------------
  236. void __fastcall TTerminal::TerminalError(Exception * E, AnsiString Msg)
  237. {
  238. throw ETerminal(E, Msg);
  239. }
  240. //---------------------------------------------------------------------------
  241. int __fastcall TTerminal::FileOperationLoop(TFileOperationEvent CallBackFunc,
  242. TFileOperationProgressType * OperationProgress, bool AllowSkip,
  243. const AnsiString Message, void * Param1, void * Param2)
  244. {
  245. assert(CallBackFunc);
  246. int Result;
  247. FILE_OPERATION_LOOP_EX
  248. (
  249. AllowSkip, Message,
  250. Result = CallBackFunc(Param1, Param2);
  251. );
  252. return Result;
  253. }
  254. //---------------------------------------------------------------------------
  255. AnsiString __fastcall TTerminal::TranslateLockedPath(AnsiString Path, bool Lock)
  256. {
  257. if (!SessionData->LockInHome || Path.IsEmpty() || (Path[1] != '/'))
  258. return Path;
  259. if (Lock)
  260. {
  261. if (Path.SubString(1, FLockDirectory.Length()) == FLockDirectory)
  262. {
  263. Path.Delete(1, FLockDirectory.Length());
  264. if (Path.IsEmpty()) Path = "/";
  265. }
  266. }
  267. else
  268. {
  269. Path = UnixExcludeTrailingBackslash(FLockDirectory + Path);
  270. }
  271. return Path;
  272. }
  273. //---------------------------------------------------------------------------
  274. void __fastcall TTerminal::ClearCaches()
  275. {
  276. FDirectoryCache->Clear();
  277. if (FDirectoryChangesCache != NULL)
  278. {
  279. FDirectoryChangesCache->Clear();
  280. }
  281. }
  282. //---------------------------------------------------------------------------
  283. void __fastcall TTerminal::SetCurrentDirectory(AnsiString value)
  284. {
  285. assert(FFileSystem);
  286. value = TranslateLockedPath(value, false);
  287. if (value != FFileSystem->CurrentDirectory)
  288. {
  289. ChangeDirectory(value);
  290. }
  291. }
  292. //---------------------------------------------------------------------------
  293. AnsiString __fastcall TTerminal::GetCurrentDirectory()
  294. {
  295. if (FFileSystem)
  296. {
  297. FCurrentDirectory = FFileSystem->CurrentDirectory;
  298. if (FCurrentDirectory.IsEmpty())
  299. {
  300. ReadCurrentDirectory();
  301. }
  302. }
  303. return TranslateLockedPath(FCurrentDirectory, true);
  304. }
  305. //---------------------------------------------------------------------------
  306. AnsiString __fastcall TTerminal::PeekCurrentDirectory()
  307. {
  308. if (FFileSystem)
  309. {
  310. FCurrentDirectory = FFileSystem->CurrentDirectory;
  311. }
  312. return TranslateLockedPath(FCurrentDirectory, true);
  313. }
  314. //---------------------------------------------------------------------------
  315. TUserGroupsList * __fastcall TTerminal::GetUserGroups()
  316. {
  317. assert(FFileSystem);
  318. if (!FUserGroupsLookedup && SessionData->LookupUserGroups &&
  319. IsCapable[fcUserGroupListing])
  320. {
  321. LookupUserGroups();
  322. }
  323. return FUserGroups;
  324. }
  325. //---------------------------------------------------------------------------
  326. AnsiString __fastcall TTerminal::GetUserName() const
  327. {
  328. // in future might be implemented to detect username similar to GetUserGroups
  329. return SessionData->UserName;
  330. }
  331. //---------------------------------------------------------------------------
  332. bool __fastcall TTerminal::GetAreCachesEmpty() const
  333. {
  334. return FDirectoryCache->IsEmpty &&
  335. ((FDirectoryChangesCache == NULL) || FDirectoryChangesCache->IsEmpty);
  336. }
  337. //---------------------------------------------------------------------------
  338. void __fastcall TTerminal::DoChangeDirectory()
  339. {
  340. if (FOnChangeDirectory)
  341. {
  342. FOnChangeDirectory(this);
  343. }
  344. }
  345. //---------------------------------------------------------------------------
  346. void __fastcall TTerminal::DoReadDirectory(bool ReloadOnly)
  347. {
  348. if (FOnReadDirectory)
  349. {
  350. FOnReadDirectory(this, ReloadOnly);
  351. }
  352. }
  353. //---------------------------------------------------------------------------
  354. void __fastcall TTerminal::DoStartReadDirectory()
  355. {
  356. if (FOnStartReadDirectory)
  357. {
  358. FOnStartReadDirectory(this);
  359. }
  360. }
  361. //---------------------------------------------------------------------------
  362. void __fastcall TTerminal::BeginTransaction()
  363. {
  364. if (!FInTransaction)
  365. {
  366. FReadCurrentDirectoryPending = false;
  367. FReadDirectoryPending = false;
  368. }
  369. FInTransaction++;
  370. }
  371. //---------------------------------------------------------------------------
  372. void __fastcall TTerminal::EndTransaction()
  373. {
  374. // it connection was closed due to fatal error during transaction, do nothing
  375. if (Active)
  376. {
  377. if (!FInTransaction)
  378. TerminalError("Can't end transaction, not in transaction");
  379. assert(FInTransaction > 0);
  380. FInTransaction--;
  381. if (FInTransaction == 0)
  382. {
  383. try
  384. {
  385. if (FReadCurrentDirectoryPending) ReadCurrentDirectory();
  386. if (FReadDirectoryPending) ReadDirectory(!FReadCurrentDirectoryPending);
  387. }
  388. __finally
  389. {
  390. FReadCurrentDirectoryPending = false;
  391. FReadDirectoryPending = false;
  392. }
  393. }
  394. }
  395. }
  396. //---------------------------------------------------------------------------
  397. void __fastcall TTerminal::SetExceptionOnFail(bool value)
  398. {
  399. if (value) FExceptionOnFail++;
  400. else
  401. {
  402. if (FExceptionOnFail == 0)
  403. throw Exception("ExceptionOnFail is already zero.");
  404. FExceptionOnFail--;
  405. }
  406. }
  407. //---------------------------------------------------------------------------
  408. bool __fastcall TTerminal::GetExceptionOnFail() const
  409. {
  410. return (bool)(FExceptionOnFail > 0);
  411. }
  412. //---------------------------------------------------------------------------
  413. void __fastcall TTerminal::CommandError(Exception * E, const AnsiString Msg)
  414. {
  415. CommandError(E, Msg, 0);
  416. }
  417. //---------------------------------------------------------------------------
  418. int __fastcall TTerminal::CommandError(Exception * E, const AnsiString Msg,
  419. int Answers)
  420. {
  421. int Result = 0;
  422. if (E && E->InheritsFrom(__classid(EFatal)))
  423. {
  424. FatalError(E, Msg);
  425. }
  426. else if (E && E->InheritsFrom(__classid(EAbort)))
  427. {
  428. // resent EAbort exception
  429. Abort();
  430. }
  431. else if (ExceptionOnFail)
  432. {
  433. throw ECommand(E, Msg);
  434. }
  435. else if (!Answers)
  436. {
  437. ECommand * ECmd = new ECommand(E, Msg);
  438. try
  439. {
  440. ShowExtendedException(ECmd, this);
  441. }
  442. __finally
  443. {
  444. delete ECmd;
  445. }
  446. }
  447. else
  448. {
  449. Result = DoQueryUser(Msg, E, Answers, qpAllowContinueOnError);
  450. }
  451. return Result;
  452. }
  453. //---------------------------------------------------------------------------
  454. bool __fastcall TTerminal::HandleException(Exception * E)
  455. {
  456. if (ExceptionOnFail)
  457. {
  458. return false;
  459. }
  460. else
  461. {
  462. HandleExtendedException(E, this);
  463. return true;
  464. }
  465. }
  466. //---------------------------------------------------------------------------
  467. void __fastcall TTerminal::CloseOnCompletion(const AnsiString Message)
  468. {
  469. LogEvent("Closing session after completed operation (as requested by user)");
  470. Close();
  471. throw ESshTerminate(NULL,
  472. Message.IsEmpty() ? LoadStr(CLOSED_ON_COMPLETION) : Message);
  473. }
  474. //---------------------------------------------------------------------------
  475. int __fastcall TTerminal::ConfirmFileOverwrite(const AnsiString FileName,
  476. const TOverwriteFileParams * FileParams, int Answers, int Params)
  477. {
  478. AnsiString Message = FMTLOAD(FILE_OVERWRITE, (FileName));
  479. if (FileParams)
  480. {
  481. Message = FMTLOAD(FILE_OVERWRITE_DETAILS, (Message,
  482. IntToStr(FileParams->SourceSize),
  483. FormatDateTime("ddddd tt", FileParams->SourceTimestamp),
  484. IntToStr(FileParams->DestSize),
  485. FormatDateTime("ddddd tt", FileParams->DestTimestamp)));
  486. }
  487. return DoQueryUser(Message, Answers, Params);
  488. }
  489. //---------------------------------------------------------------------------
  490. void __fastcall TTerminal::FileModified(const TRemoteFile * File,
  491. const AnsiString FileName)
  492. {
  493. if (SessionData->CacheDirectories)
  494. {
  495. if ((File != NULL) && (File->Directory != NULL))
  496. {
  497. if (File->IsDirectory)
  498. {
  499. // do not use UnixIncludeTrailingBackslash(CurrentDirectory)
  500. FDirectoryCache->ClearFileList(
  501. File->Directory->FullDirectory + File->FileName, true);
  502. }
  503. FDirectoryCache->ClearFileList(File->Directory->Directory, false);
  504. }
  505. else if (!FileName.IsEmpty())
  506. {
  507. AnsiString Directory = UnixExtractFilePath(FileName);
  508. FDirectoryCache->ClearFileList(
  509. !Directory.IsEmpty() ? Directory : CurrentDirectory, false);
  510. }
  511. }
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TTerminal::DirectoryModified(const AnsiString UnlockedPath, bool SubDirs)
  515. {
  516. FDirectoryCache->ClearFileList(TranslateLockedPath(UnlockedPath, true),
  517. SubDirs);
  518. }
  519. //---------------------------------------------------------------------------
  520. void __fastcall TTerminal::ReloadDirectory()
  521. {
  522. if (SessionData->CacheDirectories)
  523. {
  524. FDirectoryCache->ClearFileList(CurrentDirectory, false);
  525. }
  526. if (SessionData->CacheDirectoryChanges)
  527. {
  528. assert(FDirectoryChangesCache != NULL);
  529. FDirectoryChangesCache->ClearDirectoryChange(CurrentDirectory);
  530. }
  531. ReadCurrentDirectory();
  532. FReadCurrentDirectoryPending = false;
  533. ReadDirectory(True);
  534. FReadDirectoryPending = false;
  535. }
  536. //---------------------------------------------------------------------------
  537. void __fastcall TTerminal::EnsureNonExistence(const AnsiString FileName)
  538. {
  539. // if filename doesn't contain path, we check for existence of file
  540. if ((UnixExtractFileDir(FileName).IsEmpty()) &&
  541. UnixComparePaths(CurrentDirectory, FFiles->Directory))
  542. {
  543. TRemoteFile *File = FFiles->FindFile(FileName);
  544. if (File)
  545. {
  546. if (File->IsDirectory) throw ECommand(NULL, FMTLOAD(RENAME_CREATE_DIR_EXISTS, (FileName)));
  547. else throw ECommand(NULL, FMTLOAD(RENAME_CREATE_FILE_EXISTS, (FileName)));
  548. }
  549. }
  550. }
  551. //---------------------------------------------------------------------------
  552. void __fastcall TTerminal::DoStartup()
  553. {
  554. LogEvent("Doing startup conversation with host.");
  555. BeginTransaction();
  556. try
  557. {
  558. UpdateStatus(sshStartup);
  559. // Make sure that directory would be loaded at last
  560. FReadCurrentDirectoryPending = true;
  561. FReadDirectoryPending = true;
  562. FFileSystem->DoStartup();
  563. if (SessionData->LookupUserGroups && IsCapable[fcUserGroupListing])
  564. {
  565. LookupUserGroups();
  566. }
  567. UpdateStatus(sshOpenDirectory);
  568. if (!SessionData->RemoteDirectory.IsEmpty())
  569. {
  570. ChangeDirectory(SessionData->RemoteDirectory);
  571. }
  572. }
  573. __finally
  574. {
  575. EndTransaction();
  576. }
  577. LogEvent("Startup conversation with host finished.");
  578. UpdateStatus(sshReady);
  579. }
  580. //---------------------------------------------------------------------------
  581. void __fastcall TTerminal::ReadCurrentDirectory()
  582. {
  583. assert(FFileSystem);
  584. try
  585. {
  586. LogEvent("Getting current directory name.");
  587. AnsiString OldDirectory = FFileSystem->CurrentDirectory;
  588. FFileSystem->ReadCurrentDirectory();
  589. ReactOnCommand(fsCurrentDirectory);
  590. if (SessionData->CacheDirectoryChanges)
  591. {
  592. assert(FDirectoryChangesCache != NULL);
  593. FDirectoryChangesCache->AddDirectoryChange(OldDirectory,
  594. FLastDirectoryChange, CurrentDirectory);
  595. }
  596. if (OldDirectory.IsEmpty())
  597. {
  598. FLockDirectory = (SessionData->LockInHome ?
  599. FFileSystem->CurrentDirectory : AnsiString(""));
  600. }
  601. if (OldDirectory != FFileSystem->CurrentDirectory) DoChangeDirectory();
  602. }
  603. catch (Exception &E)
  604. {
  605. CommandError(&E, LoadStr(READ_CURRENT_DIR_ERROR));
  606. }
  607. }
  608. //---------------------------------------------------------------------------
  609. void __fastcall TTerminal::ReadDirectory(bool ReloadOnly)
  610. {
  611. TRemoteFileList * CachedFileList = NULL;
  612. if (SessionData->CacheDirectories)
  613. {
  614. CachedFileList = FDirectoryCache->GetFileList(CurrentDirectory);
  615. }
  616. if (CachedFileList)
  617. {
  618. if (ReloadOnly)
  619. {
  620. LogEvent("Cached directory not reloaded.");
  621. }
  622. else
  623. {
  624. DoStartReadDirectory();
  625. try
  626. {
  627. CachedFileList->DuplicateTo(FFiles);
  628. }
  629. __finally
  630. {
  631. DoReadDirectory(ReloadOnly);
  632. }
  633. LogEvent("Directory content loaded from cache.");
  634. }
  635. }
  636. else
  637. {
  638. DoStartReadDirectory();
  639. FFiles->Directory = CurrentDirectory;
  640. try
  641. {
  642. try
  643. {
  644. CustomReadDirectory(FFiles);
  645. }
  646. __finally
  647. {
  648. // this must be called before error is displayed, otherwise
  649. // TUnixDirView would be drawn with invalid data (it keeps reference
  650. // to already destoroyed old listing)
  651. DoReadDirectory(ReloadOnly);
  652. if (Active)
  653. {
  654. if (SessionData->CacheDirectories)
  655. {
  656. FDirectoryCache->AddFileList(FFiles);
  657. }
  658. }
  659. }
  660. }
  661. catch (Exception &E)
  662. {
  663. CommandError(&E, FmtLoadStr(LIST_DIR_ERROR, ARRAYOFCONST((FFiles->Directory))));
  664. }
  665. }
  666. }
  667. //---------------------------------------------------------------------------
  668. void __fastcall TTerminal::CustomReadDirectory(TRemoteFileList * FileList)
  669. {
  670. assert(FileList);
  671. assert(FFileSystem);
  672. FFileSystem->ReadDirectory(FileList);
  673. ReactOnCommand(fsListDirectory);
  674. }
  675. //---------------------------------------------------------------------------
  676. TRemoteFileList * TTerminal::ReadDirectoryListing(AnsiString Directory)
  677. {
  678. TRemoteFileList * FileList;
  679. try
  680. {
  681. FileList = new TRemoteFileList();
  682. try
  683. {
  684. FileList->Directory = Directory;
  685. AnsiString Directory = UnixIncludeTrailingBackslash(Directory);
  686. ExceptionOnFail = true;
  687. try
  688. {
  689. ReadDirectory(FileList);
  690. }
  691. __finally
  692. {
  693. ExceptionOnFail = false;
  694. }
  695. }
  696. catch(...)
  697. {
  698. delete FileList;
  699. FileList = NULL;
  700. throw;
  701. }
  702. }
  703. catch(Exception & E)
  704. {
  705. COMMAND_ERROR_ARI
  706. (
  707. "",
  708. FileList = ReadDirectoryListing(Directory);
  709. );
  710. }
  711. return FileList;
  712. }
  713. //---------------------------------------------------------------------------
  714. void __fastcall TTerminal::ProcessDirectory(const AnsiString DirName,
  715. TProcessFileEvent CallBackFunc, void * Param)
  716. {
  717. TRemoteFileList * FileList = ReadDirectoryListing(DirName);
  718. // skip if directory listing fails and user selects "skip"
  719. if (FileList)
  720. {
  721. try
  722. {
  723. AnsiString Directory = UnixIncludeTrailingBackslash(DirName);
  724. TRemoteFile * File;
  725. for (int Index = 0; Index < FileList->Count; Index++)
  726. {
  727. File = FileList->Files[Index];
  728. if (!File->IsParentDirectory && !File->IsThisDirectory)
  729. {
  730. CallBackFunc(Directory + File->FileName, File, Param);
  731. }
  732. }
  733. }
  734. __finally
  735. {
  736. delete FileList;
  737. }
  738. }
  739. }
  740. //---------------------------------------------------------------------------
  741. void __fastcall TTerminal::ReadDirectory(TRemoteFileList * FileList)
  742. {
  743. try
  744. {
  745. CustomReadDirectory(FileList);
  746. }
  747. catch (Exception &E)
  748. {
  749. CommandError(&E, FmtLoadStr(LIST_DIR_ERROR, ARRAYOFCONST((FileList->Directory))));
  750. }
  751. }
  752. //---------------------------------------------------------------------------
  753. void __fastcall TTerminal::ReadSymlink(TRemoteFile * SymlinkFile,
  754. TRemoteFile *& File)
  755. {
  756. assert(FFileSystem);
  757. try
  758. {
  759. LogEvent(FORMAT("Reading symlink \"%s\".", (SymlinkFile->FileName)));
  760. FFileSystem->ReadSymlink(SymlinkFile, File);
  761. ReactOnCommand(fsReadSymlink);
  762. }
  763. catch (Exception &E)
  764. {
  765. CommandError(&E, FMTLOAD(READ_SYMLINK_ERROR, (SymlinkFile->FileName)));
  766. }
  767. }
  768. //---------------------------------------------------------------------------
  769. void __fastcall TTerminal::ReadFile(const AnsiString FileName,
  770. TRemoteFile *& File)
  771. {
  772. assert(FFileSystem);
  773. File = NULL;
  774. try
  775. {
  776. LogEvent(FORMAT("Listing file \"%s\".", (FileName)));
  777. FFileSystem->ReadFile(FileName, File);
  778. ReactOnCommand(fsListFile);
  779. }
  780. catch (Exception &E)
  781. {
  782. if (File) delete File;
  783. File = NULL;
  784. CommandError(&E, FMTLOAD(LIST_DIR_ERROR, (FileName)));
  785. }
  786. }
  787. //---------------------------------------------------------------------------
  788. bool __fastcall TTerminal::ProcessFiles(TStrings * FileList,
  789. TFileOperation Operation, TProcessFileEvent ProcessFile, void * Param,
  790. TOperationSide Side)
  791. {
  792. assert(FFileSystem);
  793. assert(FileList);
  794. bool Result = false;
  795. bool DisconnectWhenComplete = false;
  796. try
  797. {
  798. TFileOperationProgressType Progress(FOnProgress, FOnFinished);
  799. Progress.Start(Operation, Side, FileList->Count);
  800. FOperationProgress = &Progress;
  801. try
  802. {
  803. BeginTransaction();
  804. try
  805. {
  806. int Index = 0;
  807. AnsiString FileName;
  808. bool Success;
  809. while ((Index < FileList->Count) && (Progress.Cancel == csContinue))
  810. {
  811. FileName = FileList->Strings[Index];
  812. try
  813. {
  814. Success = false;
  815. ProcessFile(FileName, (TRemoteFile *)FileList->Objects[Index], Param);
  816. Success = true;
  817. }
  818. __finally
  819. {
  820. AnsiString FileNameOnly = (Side == osRemote) ?
  821. UnixExtractFileName(FileName) : ExtractFileName(FileName);
  822. Progress.Finish(FileNameOnly, Success, DisconnectWhenComplete);
  823. }
  824. Index++;
  825. }
  826. }
  827. __finally
  828. {
  829. EndTransaction();
  830. }
  831. if (Progress.Cancel == csContinue)
  832. {
  833. Result = true;
  834. }
  835. }
  836. __finally
  837. {
  838. FOperationProgress = NULL;
  839. Progress.Stop();
  840. }
  841. }
  842. catch (...)
  843. {
  844. DisconnectWhenComplete = false;
  845. // this was missing here. was it by purpose?
  846. // without it any error message is lost
  847. throw;
  848. }
  849. if (DisconnectWhenComplete)
  850. {
  851. CloseOnCompletion();
  852. }
  853. return Result;
  854. }
  855. //---------------------------------------------------------------------------
  856. void __fastcall TTerminal::DeleteFile(AnsiString FileName,
  857. const TRemoteFile * File, void * Recursive)
  858. {
  859. if (FileName.IsEmpty() && File)
  860. {
  861. FileName = File->FileName;
  862. }
  863. if (OperationProgress && OperationProgress->Operation == foDelete)
  864. {
  865. if (OperationProgress->Cancel != csContinue) Abort();
  866. OperationProgress->SetFile(FileName);
  867. }
  868. LogEvent(FORMAT("Deleting file \"%s\".", (FileName)));
  869. if (File) FileModified(File, FileName);
  870. DoDeleteFile(FileName, File, Recursive);
  871. ReactOnCommand(fsDeleteFile);
  872. }
  873. //---------------------------------------------------------------------------
  874. void __fastcall TTerminal::DoDeleteFile(const AnsiString FileName,
  875. const TRemoteFile * File, void * Recursive)
  876. {
  877. try
  878. {
  879. assert(FFileSystem);
  880. // 'File' parameter: SFTPFileSystem needs to know if file is file or directory
  881. FFileSystem->DeleteFile(FileName, File,
  882. Recursive ? *((bool*)Recursive) : true);
  883. }
  884. catch(Exception & E)
  885. {
  886. COMMAND_ERROR_ARI
  887. (
  888. FMTLOAD(DELETE_FILE_ERROR, (FileName)),
  889. DoDeleteFile(FileName, File, Recursive)
  890. );
  891. }
  892. }
  893. //---------------------------------------------------------------------------
  894. bool __fastcall TTerminal::DeleteFiles(TStrings * FilesToDelete, bool * Recursive)
  895. {
  896. return ProcessFiles(FilesToDelete, foDelete, DeleteFile, Recursive);
  897. }
  898. //---------------------------------------------------------------------------
  899. void __fastcall TTerminal::DeleteLocalFile(AnsiString FileName,
  900. const TRemoteFile * /*File*/, void * /*Param*/)
  901. {
  902. if (OnDeleteLocalFile == NULL)
  903. {
  904. if (!RecursiveDeleteFile(FileName, false))
  905. {
  906. throw Exception(FMTLOAD(DELETE_FILE_ERROR, (FileName)));
  907. }
  908. }
  909. else
  910. {
  911. OnDeleteLocalFile(FileName);
  912. }
  913. }
  914. //---------------------------------------------------------------------------
  915. bool __fastcall TTerminal::DeleteLocalFiles(TStrings * FileList)
  916. {
  917. return ProcessFiles(FileList, foDelete, DeleteLocalFile, NULL, osLocal);
  918. }
  919. //---------------------------------------------------------------------------
  920. void __fastcall TTerminal::CustomCommandOnFile(AnsiString FileName,
  921. const TRemoteFile * File, void * AParams)
  922. {
  923. TCustomCommandParams * Params = ((TCustomCommandParams *)AParams);
  924. if (FileName.IsEmpty() && File)
  925. {
  926. FileName = File->FileName;
  927. }
  928. if (OperationProgress && OperationProgress->Operation == foCustomCommand)
  929. {
  930. if (OperationProgress->Cancel != csContinue) Abort();
  931. OperationProgress->SetFile(FileName);
  932. }
  933. LogEvent(FORMAT("Executing custom command \"%s\" (%d) on file \"%s\".",
  934. (Params->Command, Params->Params, FileName)));
  935. if (File) FileModified(File, FileName);
  936. DoCustomCommandOnFile(FileName, File, Params->Command, Params->Params);
  937. ReactOnCommand(fsAnyCommand);
  938. }
  939. //---------------------------------------------------------------------------
  940. void __fastcall TTerminal::DoCustomCommandOnFile(AnsiString FileName,
  941. const TRemoteFile * File, AnsiString Command, int Params)
  942. {
  943. try
  944. {
  945. assert(FFileSystem);
  946. FFileSystem->CustomCommandOnFile(FileName, File, Command, Params);
  947. }
  948. catch(Exception & E)
  949. {
  950. COMMAND_ERROR_ARI
  951. (
  952. FMTLOAD(CUSTOM_COMMAND_ERROR, (Command, FileName)),
  953. DoCustomCommandOnFile(FileName, File, Command, Params)
  954. );
  955. }
  956. }
  957. //---------------------------------------------------------------------------
  958. void __fastcall TTerminal::CustomCommandOnFiles(AnsiString Command,
  959. int Params, TStrings * Files)
  960. {
  961. TCustomCommandParams AParams;
  962. AParams.Command = Command;
  963. AParams.Params = Params;
  964. ProcessFiles(Files, foCustomCommand, CustomCommandOnFile, &AParams);
  965. }
  966. //---------------------------------------------------------------------------
  967. void __fastcall TTerminal::ChangeFileProperties(AnsiString FileName,
  968. const TRemoteFile * File, /*const TRemoteProperties*/ void * Properties)
  969. {
  970. TRemoteProperties * RProperties = (TRemoteProperties *)Properties;
  971. assert(RProperties && !RProperties->Valid.Empty());
  972. if (FileName.IsEmpty() && File)
  973. {
  974. FileName = File->FileName;
  975. }
  976. if (OperationProgress && OperationProgress->Operation == foSetProperties)
  977. {
  978. if (OperationProgress->Cancel != csContinue) Abort();
  979. OperationProgress->SetFile(FileName);
  980. }
  981. if (IsLogging())
  982. {
  983. LogEvent(FORMAT("Changing properties of \"%s\" (%s)",
  984. (FileName, BooleanToEngStr(RProperties->Recursive))));
  985. if (RProperties->Valid.Contains(vpRights))
  986. {
  987. LogEvent(FORMAT(" - mode: \"%s\"", (RProperties->Rights.ModeStr)));
  988. }
  989. if (RProperties->Valid.Contains(vpGroup))
  990. {
  991. LogEvent(FORMAT(" - group: \"%s\"", (RProperties->Group)));
  992. }
  993. if (RProperties->Valid.Contains(vpOwner))
  994. {
  995. LogEvent(FORMAT(" - owner: \"%s\"", (RProperties->Owner)));
  996. }
  997. }
  998. if (File) FileModified(File, FileName);
  999. DoChangeFileProperties(FileName, File, RProperties);
  1000. ReactOnCommand(fsChangeProperties);
  1001. }
  1002. //---------------------------------------------------------------------------
  1003. void __fastcall TTerminal::DoChangeFileProperties(const AnsiString FileName,
  1004. const TRemoteFile * File, const TRemoteProperties * Properties)
  1005. {
  1006. try
  1007. {
  1008. assert(FFileSystem);
  1009. FFileSystem->ChangeFileProperties(FileName, File, Properties);
  1010. }
  1011. catch(Exception & E)
  1012. {
  1013. COMMAND_ERROR_ARI
  1014. (
  1015. FMTLOAD(CHANGE_PROPERTIES_ERROR, (FileName)),
  1016. DoChangeFileProperties(FileName, File, Properties)
  1017. );
  1018. }
  1019. }
  1020. //---------------------------------------------------------------------------
  1021. void __fastcall TTerminal::ChangeFilesProperties(TStrings * FileList,
  1022. const TRemoteProperties * Properties)
  1023. {
  1024. ProcessFiles(FileList, foSetProperties, ChangeFileProperties, (void *)Properties);
  1025. }
  1026. //---------------------------------------------------------------------------
  1027. void __fastcall TTerminal::CalculateFileSize(AnsiString FileName,
  1028. const TRemoteFile * File, /*TCalculateSizeParams*/ void * Param)
  1029. {
  1030. assert(Param);
  1031. assert(File);
  1032. if (FileName.IsEmpty() && File)
  1033. {
  1034. FileName = File->FileName;
  1035. }
  1036. if (File->IsDirectory && !File->IsSymLink)
  1037. {
  1038. LogEvent(FORMAT("Getting size of directory \"%s\"", (FileName)));
  1039. DoCalculateDirectorySize(FileName, File,
  1040. static_cast<TCalculateSizeParams*>(Param));
  1041. }
  1042. else
  1043. {
  1044. static_cast<TCalculateSizeParams*>(Param)->Size += File->Size;
  1045. }
  1046. if (OperationProgress && OperationProgress->Operation == foCalculateSize)
  1047. {
  1048. if (OperationProgress->Cancel != csContinue) Abort();
  1049. OperationProgress->SetFile(FileName);
  1050. }
  1051. }
  1052. //---------------------------------------------------------------------------
  1053. void __fastcall TTerminal::DoCalculateDirectorySize(const AnsiString FileName,
  1054. const TRemoteFile * File, TCalculateSizeParams * Params)
  1055. {
  1056. try
  1057. {
  1058. ProcessDirectory(FileName, CalculateFileSize, Params);
  1059. }
  1060. catch(Exception & E)
  1061. {
  1062. if (!Active || ((Params->Params & csIgnoreErrors) == 0))
  1063. {
  1064. COMMAND_ERROR_ARI
  1065. (
  1066. FMTLOAD(CALCULATE_SIZE_ERROR, (FileName)),
  1067. DoCalculateDirectorySize(FileName, File, Params)
  1068. );
  1069. }
  1070. }
  1071. }
  1072. //---------------------------------------------------------------------------
  1073. void __fastcall TTerminal::CalculateFilesSize(TStrings * FileList,
  1074. __int64 & Size, int Params)
  1075. {
  1076. TCalculateSizeParams Param;
  1077. Param.Size = 0;
  1078. Param.Params = Params;
  1079. ProcessFiles(FileList, foCalculateSize, CalculateFileSize, &Param);
  1080. Size = Param.Size;
  1081. }
  1082. //---------------------------------------------------------------------------
  1083. void __fastcall TTerminal::RenameFile(const AnsiString FileName,
  1084. const AnsiString NewName)
  1085. {
  1086. LogEvent(FORMAT("Renaming file \"%s\" to \"%s\".", (FileName, NewName)));
  1087. DoRenameFile(FileName, NewName, false);
  1088. ReactOnCommand(fsRenameFile);
  1089. }
  1090. //---------------------------------------------------------------------------
  1091. void __fastcall TTerminal::RenameFile(const TRemoteFile * File,
  1092. const AnsiString NewName, bool CheckExistence)
  1093. {
  1094. assert(File && File->Directory == FFiles);
  1095. bool Proceed = true;
  1096. // if filename doesn't contain path, we check for existence of file
  1097. if ((File->FileName != NewName) && CheckExistence &&
  1098. Configuration->ConfirmOverwriting &&
  1099. UnixComparePaths(CurrentDirectory, FFiles->Directory))
  1100. {
  1101. TRemoteFile * DuplicateFile = FFiles->FindFile(NewName);
  1102. if (DuplicateFile)
  1103. {
  1104. AnsiString QuestionFmt;
  1105. if (DuplicateFile->IsDirectory) QuestionFmt = LoadStr(DIRECTORY_OVERWRITE);
  1106. else QuestionFmt = LoadStr(FILE_OVERWRITE);
  1107. int Result;
  1108. Result = DoQueryUser(FORMAT(QuestionFmt, (NewName)),
  1109. qaYes | qaNo, qpNeverAskAgainCheck);
  1110. if (Result == qaNeverAskAgain)
  1111. {
  1112. Proceed = true;
  1113. Configuration->ConfirmOverwriting = false;
  1114. }
  1115. else
  1116. {
  1117. Proceed = (Result == qaYes);
  1118. }
  1119. }
  1120. }
  1121. if (Proceed)
  1122. {
  1123. FileModified(File, File->FileName);
  1124. RenameFile(File->FileName, NewName);
  1125. }
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. void __fastcall TTerminal::DoRenameFile(const AnsiString FileName,
  1129. const AnsiString NewName, bool Move)
  1130. {
  1131. try
  1132. {
  1133. assert(FFileSystem);
  1134. FFileSystem->RenameFile(FileName, NewName);
  1135. }
  1136. catch(Exception & E)
  1137. {
  1138. COMMAND_ERROR_ARI
  1139. (
  1140. FMTLOAD(Move ? MOVE_FILE_ERROR : RENAME_FILE_ERROR, (FileName, NewName)),
  1141. DoRenameFile(FileName, NewName, Move)
  1142. );
  1143. }
  1144. }
  1145. //---------------------------------------------------------------------------
  1146. void __fastcall TTerminal::MoveFile(const AnsiString FileName,
  1147. const TRemoteFile * File, /*const TMoveFileParams*/ void * Param)
  1148. {
  1149. assert(Param != NULL);
  1150. const TMoveFileParams & Params = *static_cast<const TMoveFileParams*>(Param);
  1151. AnsiString NewName = UnixIncludeTrailingBackslash(Params.Target) +
  1152. MaskFileName(UnixExtractFileName(FileName), Params.FileMask);
  1153. LogEvent(FORMAT("Moving file \"%s\" to \"%s\".", (FileName, NewName)));
  1154. FileModified(File, FileName);
  1155. DoRenameFile(FileName, NewName, true);
  1156. ReactOnCommand(fsMoveFile);
  1157. }
  1158. //---------------------------------------------------------------------------
  1159. bool __fastcall TTerminal::MoveFiles(TStrings * FileList, const AnsiString Target,
  1160. const AnsiString FileMask)
  1161. {
  1162. TMoveFileParams Params;
  1163. Params.Target = Target;
  1164. Params.FileMask = FileMask;
  1165. DirectoryModified(Target, true);
  1166. return ProcessFiles(FileList, foRemoteMove, MoveFile, &Params);
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. void __fastcall TTerminal::CreateDirectory(const AnsiString DirName,
  1170. const TRemoteProperties * Properties)
  1171. {
  1172. assert(FFileSystem);
  1173. EnsureNonExistence(DirName);
  1174. FileModified(NULL, DirName);
  1175. LogEvent(FORMAT("Creating directory \"%s\".", (DirName)));
  1176. DoCreateDirectory(DirName, Properties);
  1177. ReactOnCommand(fsCreateDirectory);
  1178. }
  1179. //---------------------------------------------------------------------------
  1180. void __fastcall TTerminal::DoCreateDirectory(const AnsiString DirName,
  1181. const TRemoteProperties * Properties)
  1182. {
  1183. try
  1184. {
  1185. assert(FFileSystem);
  1186. FFileSystem->CreateDirectory(DirName, Properties);
  1187. }
  1188. catch(Exception & E)
  1189. {
  1190. COMMAND_ERROR_ARI
  1191. (
  1192. FMTLOAD(CREATE_DIR_ERROR, (DirName)),
  1193. DoCreateDirectory(DirName, Properties)
  1194. );
  1195. }
  1196. }
  1197. //---------------------------------------------------------------------------
  1198. void __fastcall TTerminal::CreateLink(const AnsiString FileName,
  1199. const AnsiString PointTo, bool Symbolic)
  1200. {
  1201. assert(FFileSystem);
  1202. EnsureNonExistence(FileName);
  1203. if (SessionData->CacheDirectories)
  1204. {
  1205. FDirectoryCache->ClearFileList(CurrentDirectory, false);
  1206. }
  1207. LogEvent(FORMAT("Creating link \"%s\" to \"%s\" (symbolic: %s).",
  1208. (FileName, PointTo, BooleanToEngStr(Symbolic))));
  1209. DoCreateLink(FileName, PointTo, Symbolic);
  1210. ReactOnCommand(fsCreateDirectory);
  1211. }
  1212. //---------------------------------------------------------------------------
  1213. void __fastcall TTerminal::DoCreateLink(const AnsiString FileName,
  1214. const AnsiString PointTo, bool Symbolic)
  1215. {
  1216. try
  1217. {
  1218. assert(FFileSystem);
  1219. FFileSystem->CreateLink(FileName, PointTo, Symbolic);
  1220. }
  1221. catch(Exception & E)
  1222. {
  1223. COMMAND_ERROR_ARI
  1224. (
  1225. FMTLOAD(CREATE_LINK_ERROR, (FileName)),
  1226. DoCreateLink(FileName, PointTo, Symbolic);
  1227. );
  1228. }
  1229. }
  1230. //---------------------------------------------------------------------------
  1231. void __fastcall TTerminal::HomeDirectory()
  1232. {
  1233. assert(FFileSystem);
  1234. try
  1235. {
  1236. LogEvent("Changing directory to home directory.");
  1237. FFileSystem->HomeDirectory();
  1238. ReactOnCommand(fsHomeDirectory);
  1239. }
  1240. catch (Exception &E)
  1241. {
  1242. CommandError(&E, LoadStr(CHANGE_HOMEDIR_ERROR));
  1243. }
  1244. }
  1245. //---------------------------------------------------------------------------
  1246. void __fastcall TTerminal::ChangeDirectory(const AnsiString Directory)
  1247. {
  1248. assert(FFileSystem);
  1249. try
  1250. {
  1251. AnsiString CachedDirectory;
  1252. assert(!SessionData->CacheDirectoryChanges || (FDirectoryChangesCache != NULL));
  1253. if (SessionData->CacheDirectoryChanges &&
  1254. FDirectoryChangesCache->GetDirectoryChange(PeekCurrentDirectory(),
  1255. Directory, CachedDirectory))
  1256. {
  1257. LogEvent(FORMAT("Cached directory change via \"%s\" to \"%s\".",
  1258. (Directory, CachedDirectory)));
  1259. FFileSystem->CachedChangeDirectory(CachedDirectory);
  1260. }
  1261. else
  1262. {
  1263. LogEvent(FORMAT("Changing directory to \"%s\".", (Directory)));
  1264. FFileSystem->ChangeDirectory(Directory);
  1265. }
  1266. FLastDirectoryChange = Directory;
  1267. ReactOnCommand(fsChangeDirectory);
  1268. }
  1269. catch (Exception &E)
  1270. {
  1271. CommandError(&E, FMTLOAD(CHANGE_DIR_ERROR, (Directory)));
  1272. }
  1273. }
  1274. //---------------------------------------------------------------------------
  1275. void __fastcall TTerminal::LookupUserGroups()
  1276. {
  1277. assert(FFileSystem);
  1278. assert(IsCapable[fcUserGroupListing]);
  1279. try
  1280. {
  1281. FUserGroupsLookedup = true;
  1282. LogEvent("Looking up current user groups.");
  1283. FFileSystem->LookupUserGroups();
  1284. ReactOnCommand(fsLookupUserGroups);
  1285. if (IsLogging())
  1286. {
  1287. LogEvent("Following groups found:");
  1288. for (int Index = 0; Index < FUserGroups->Count; Index++)
  1289. {
  1290. LogEvent(AnsiString(" ") + FUserGroups->Strings[Index]);
  1291. }
  1292. }
  1293. }
  1294. catch (Exception &E)
  1295. {
  1296. CommandError(&E, LoadStr(LOOKUP_GROUPS_ERROR));
  1297. }
  1298. }
  1299. //---------------------------------------------------------------------------
  1300. void __fastcall TTerminal::AnyCommand(const AnsiString Command)
  1301. {
  1302. assert(FFileSystem);
  1303. try
  1304. {
  1305. DirectoryModified(CurrentDirectory, false);
  1306. LogEvent("Executing used defined command.");
  1307. FFileSystem->AnyCommand(Command);
  1308. ReactOnCommand(fsAnyCommand);
  1309. }
  1310. catch (Exception &E)
  1311. {
  1312. if (ExceptionOnFail || (E.InheritsFrom(__classid(EFatal)))) throw;
  1313. else ShowExtendedException(&E, this);
  1314. }
  1315. }
  1316. //---------------------------------------------------------------------------
  1317. bool __fastcall TTerminal::CreateLocalFile(const AnsiString FileName,
  1318. TFileOperationProgressType * OperationProgress, HANDLE * AHandle)
  1319. {
  1320. assert(AHandle);
  1321. bool Result = true;
  1322. FILE_OPERATION_LOOP (FMTLOAD(CREATE_FILE_ERROR, (FileName)),
  1323. bool Done;
  1324. do
  1325. {
  1326. *AHandle = CreateFile(FileName.c_str(), GENERIC_WRITE, 0, NULL,
  1327. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  1328. Done = (*AHandle != INVALID_HANDLE_VALUE);
  1329. if (!Done)
  1330. {
  1331. int FileAttr;
  1332. if (FileExists(FileName) &&
  1333. (((FileAttr = FileGetAttr(FileName)) & faReadOnly) != 0))
  1334. {
  1335. if (OperationProgress->NoToAll)
  1336. {
  1337. Result = false;
  1338. }
  1339. else if (!OperationProgress->YesToAll)
  1340. {
  1341. int Answer;
  1342. SUSPEND_OPERATION
  1343. (
  1344. Answer = DoQueryUser(
  1345. FMTLOAD(READ_ONLY_OVERWRITE, (FileName)),
  1346. qaYes | qaNo | qaAbort | qaYesToAll | qaNoToAll, 0);
  1347. );
  1348. switch (Answer) {
  1349. case qaYesToAll: OperationProgress->YesToAll = true; break;
  1350. case qaAbort: OperationProgress->Cancel = csCancel; // continue on next case
  1351. case qaNoToAll: OperationProgress->NoToAll = true;
  1352. case qaNo: Result = false; break;
  1353. }
  1354. }
  1355. if (Result)
  1356. {
  1357. FILE_OPERATION_LOOP (FMTLOAD(CANT_SET_ATTRS, (FileName)),
  1358. if (FileSetAttr(FileName, FileAttr & ~faReadOnly) != 0)
  1359. {
  1360. EXCEPTION;
  1361. }
  1362. );
  1363. }
  1364. else
  1365. {
  1366. Done = true;
  1367. }
  1368. }
  1369. else
  1370. {
  1371. EXCEPTION;
  1372. }
  1373. }
  1374. }
  1375. while (!Done);
  1376. );
  1377. return Result;
  1378. }
  1379. //---------------------------------------------------------------------------
  1380. void __fastcall TTerminal::OpenLocalFile(const AnsiString FileName,
  1381. int Access, int * AAttrs, HANDLE * AHandle, unsigned long * ACTime,
  1382. unsigned long * AMTime, unsigned long * AATime, __int64 * ASize,
  1383. bool TryWriteReadOnly)
  1384. {
  1385. int Attrs = 0;
  1386. HANDLE Handle = 0;
  1387. FILE_OPERATION_LOOP (FMTLOAD(FILE_NOT_EXISTS, (FileName)),
  1388. Attrs = FileGetAttr(FileName);
  1389. if (Attrs == -1) EXCEPTION;
  1390. )
  1391. if ((Attrs & faDirectory) == 0)
  1392. {
  1393. bool NoHandle = false;
  1394. if (!TryWriteReadOnly && (Access == GENERIC_WRITE) &&
  1395. ((Attrs & faReadOnly) != 0))
  1396. {
  1397. Access = GENERIC_READ;
  1398. NoHandle = true;
  1399. }
  1400. FILE_OPERATION_LOOP (FMTLOAD(OPENFILE_ERROR, (FileName)),
  1401. Handle = CreateFile(FileName.c_str(), Access,
  1402. Access == GENERIC_READ ? FILE_SHARE_READ : 0,
  1403. NULL, OPEN_EXISTING, 0, 0);
  1404. if (Handle == INVALID_HANDLE_VALUE)
  1405. {
  1406. Handle = 0;
  1407. EXCEPTION;
  1408. }
  1409. );
  1410. try
  1411. {
  1412. if (AATime || AMTime)
  1413. {
  1414. // Get last file access and modification time
  1415. FILE_OPERATION_LOOP (FMTLOAD(CANT_GET_ATTRS, (FileName)),
  1416. FILETIME ATime;
  1417. FILETIME MTime;
  1418. FILETIME CTime;
  1419. if (!GetFileTime(Handle, &CTime, &ATime, &MTime)) EXCEPTION;
  1420. if (ACTime) TIME_WIN_TO_POSIX(CTime, *ACTime);
  1421. if (AATime) TIME_WIN_TO_POSIX(ATime, *AATime);
  1422. if (AMTime) TIME_WIN_TO_POSIX(MTime, *AMTime);
  1423. );
  1424. }
  1425. if (ASize)
  1426. {
  1427. // Get file size
  1428. FILE_OPERATION_LOOP (FMTLOAD(CANT_GET_ATTRS, (FileName)),
  1429. unsigned long LSize;
  1430. unsigned long HSize;
  1431. LSize = GetFileSize(Handle, &HSize);
  1432. if ((LSize == 0xFFFFFFFF) && (GetLastError() != NO_ERROR)) EXCEPTION;
  1433. *ASize = (__int64(HSize) << 32) + LSize;
  1434. );
  1435. }
  1436. if ((AHandle == NULL) || NoHandle)
  1437. {
  1438. CloseHandle(Handle);
  1439. Handle = NULL;
  1440. }
  1441. }
  1442. catch(...)
  1443. {
  1444. CloseHandle(Handle);
  1445. throw;
  1446. }
  1447. }
  1448. if (AAttrs) *AAttrs = Attrs;
  1449. if (AHandle) *AHandle = Handle;
  1450. }
  1451. //---------------------------------------------------------------------------
  1452. void __fastcall TTerminal::CalculateLocalFileSize(const AnsiString FileName,
  1453. const TSearchRec Rec, /*__int64*/ void * Size)
  1454. {
  1455. if ((Rec.Attr & faDirectory) == 0)
  1456. {
  1457. (*static_cast<__int64*>(Size)) +=
  1458. (static_cast<__int64>(Rec.FindData.nFileSizeHigh) << 32) +
  1459. Rec.FindData.nFileSizeLow;
  1460. }
  1461. if (OperationProgress && OperationProgress->Operation == foCalculateSize)
  1462. {
  1463. if (OperationProgress->Cancel != csContinue) Abort();
  1464. OperationProgress->SetFile(FileName);
  1465. }
  1466. }
  1467. //---------------------------------------------------------------------------
  1468. void __fastcall TTerminal::CalculateLocalFilesSize(TStrings * FileList, __int64 & Size)
  1469. {
  1470. Size = 0;
  1471. TFileOperationProgressType OperationProgress(FOnProgress, FOnFinished);
  1472. OperationProgress.Start(foCalculateSize, osLocal, FileList->Count);
  1473. try
  1474. {
  1475. assert(!FOperationProgress);
  1476. FOperationProgress = &OperationProgress;
  1477. TSearchRec Rec;
  1478. for (int Index = 0; Index < FileList->Count; Index++)
  1479. {
  1480. if (FileSearchRec(FileList->Strings[Index], Rec))
  1481. {
  1482. if (Rec.Attr & faDirectory)
  1483. {
  1484. ProcessLocalDirectory(FileList->Strings[Index],
  1485. CalculateLocalFileSize, &Size);
  1486. }
  1487. CalculateLocalFileSize(FileList->Strings[Index], Rec, &Size);
  1488. }
  1489. }
  1490. }
  1491. __finally
  1492. {
  1493. FOperationProgress = NULL;
  1494. OperationProgress.Stop();
  1495. }
  1496. }
  1497. //---------------------------------------------------------------------------
  1498. struct TSynchronizeFileData
  1499. {
  1500. int Time;
  1501. int Attr;
  1502. unsigned long SizeHigh;
  1503. unsigned long SizeLow;
  1504. FILETIME LastWriteTime;
  1505. bool Modified;
  1506. bool New;
  1507. };
  1508. //---------------------------------------------------------------------------
  1509. struct TSynchronizeData
  1510. {
  1511. AnsiString LocalDirectory;
  1512. AnsiString RemoteDirectory;
  1513. TTerminal::TSynchronizeMode Mode;
  1514. int Params;
  1515. TSynchronizeDirectory OnSynchronizeDirectory;
  1516. TStringList * LocalFileList;
  1517. TStringList * ModifiedRemoteFileList;
  1518. TStringList * NewRemoteFileList;
  1519. };
  1520. //---------------------------------------------------------------------------
  1521. void __fastcall TTerminal::Synchronize(const AnsiString LocalDirectory,
  1522. const AnsiString RemoteDirectory, TSynchronizeMode Mode, int Params,
  1523. TSynchronizeDirectory OnSynchronizeDirectory)
  1524. {
  1525. BeginTransaction();
  1526. try
  1527. {
  1528. DoSynchronizeDirectory(LocalDirectory, RemoteDirectory, Mode, Params,
  1529. OnSynchronizeDirectory);
  1530. }
  1531. __finally
  1532. {
  1533. EndTransaction();
  1534. }
  1535. }
  1536. //---------------------------------------------------------------------------
  1537. void __fastcall TTerminal::DoSynchronizeDirectory(const AnsiString LocalDirectory,
  1538. const AnsiString RemoteDirectory, TSynchronizeMode Mode, int Params,
  1539. TSynchronizeDirectory OnSynchronizeDirectory)
  1540. {
  1541. TSearchRec SearchRec;
  1542. bool Found;
  1543. bool Delete = (Params & spDelete) != 0;
  1544. TSynchronizeData Data;
  1545. Data.LocalDirectory = IncludeTrailingBackslash(LocalDirectory);
  1546. Data.RemoteDirectory = UnixIncludeTrailingBackslash(RemoteDirectory);
  1547. Data.Mode = Mode;
  1548. Data.Params = Params;
  1549. Data.OnSynchronizeDirectory = OnSynchronizeDirectory;
  1550. Data.LocalFileList = NULL;
  1551. Data.NewRemoteFileList = NULL;
  1552. Data.ModifiedRemoteFileList = NULL;
  1553. TStrings * LocalFileList = NULL;
  1554. LogEvent(FORMAT("Synchronizing local directory '%s' with remote directory '%s', "
  1555. "mode = %d, params = %d", (LocalDirectory, RemoteDirectory,
  1556. int(Mode), int(Params))));
  1557. bool Continue = true;
  1558. OnSynchronizeDirectory(LocalDirectory, RemoteDirectory, Continue);
  1559. if (!Continue)
  1560. {
  1561. Abort();
  1562. }
  1563. try
  1564. {
  1565. Data.LocalFileList = new TStringList();
  1566. Data.LocalFileList->Sorted = true;
  1567. Data.LocalFileList->CaseSensitive = false;
  1568. Data.NewRemoteFileList = new TStringList();
  1569. Data.ModifiedRemoteFileList = new TStringList();
  1570. LocalFileList = new TStringList();
  1571. FILE_OPERATION_LOOP (FMTLOAD(LIST_DIR_ERROR, (LocalDirectory)),
  1572. int FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
  1573. Found = (FindFirst(Data.LocalDirectory + "*.*", FindAttrs, SearchRec) == 0);
  1574. );
  1575. if (Found)
  1576. {
  1577. try
  1578. {
  1579. AnsiString FileName;
  1580. while (Found)
  1581. {
  1582. FileName = SearchRec.Name;
  1583. if ((FileName != ".") && (FileName != ".."))
  1584. {
  1585. TSynchronizeFileData * FileData = new TSynchronizeFileData;
  1586. FileData->Time = SearchRec.Time;
  1587. FileData->SizeHigh = SearchRec.FindData.nFileSizeHigh;
  1588. FileData->SizeLow = SearchRec.FindData.nFileSizeLow;
  1589. FileData->Attr = SearchRec.Attr;
  1590. FileData->LastWriteTime = SearchRec.FindData.ftLastWriteTime;
  1591. FileData->New = true;
  1592. FileData->Modified = false;
  1593. Data.LocalFileList->AddObject(FileName,
  1594. reinterpret_cast<TObject*>(FileData));
  1595. }
  1596. FILE_OPERATION_LOOP (FMTLOAD(LIST_DIR_ERROR, (LocalDirectory)),
  1597. Found = (FindNext(SearchRec) == 0);
  1598. );
  1599. }
  1600. }
  1601. __finally
  1602. {
  1603. FindClose(SearchRec);
  1604. }
  1605. ProcessDirectory(RemoteDirectory, SynchronizeFile, &Data);
  1606. TSynchronizeFileData * FileData;
  1607. for (int Index = 0; Index < Data.LocalFileList->Count; Index++)
  1608. {
  1609. FileData = reinterpret_cast<TSynchronizeFileData *>
  1610. (Data.LocalFileList->Objects[Index]);
  1611. if ((FileData->Modified && ((Mode == smBoth) || (Mode == smRemote))) ||
  1612. (FileData->New))
  1613. {
  1614. LocalFileList->Add(Data.LocalDirectory + Data.LocalFileList->Strings[Index]);
  1615. }
  1616. }
  1617. TCopyParamType CopyParam = Configuration->CopyParam;
  1618. CopyParam.PreserveTime = true;
  1619. int CopyParams = (Params & spNoConfirmation) != 0 ? cpNoConfirmation : 0;
  1620. if (LocalFileList->Count > 0)
  1621. {
  1622. bool Result;
  1623. if ((Mode == smBoth) || (Mode == smRemote))
  1624. {
  1625. Result = CopyToRemote(LocalFileList, RemoteDirectory, &CopyParam, CopyParams);
  1626. }
  1627. else if ((Mode == smLocal) && Delete)
  1628. {
  1629. Result = DeleteLocalFiles(LocalFileList);
  1630. }
  1631. if (!Result)
  1632. {
  1633. Abort();
  1634. }
  1635. }
  1636. if ((Mode == smBoth) || (Mode == smLocal))
  1637. {
  1638. Data.ModifiedRemoteFileList->AddStrings(Data.NewRemoteFileList);
  1639. if (Data.ModifiedRemoteFileList->Count > 0)
  1640. {
  1641. if (!CopyToLocal(Data.ModifiedRemoteFileList, LocalDirectory,
  1642. &CopyParam, CopyParams))
  1643. {
  1644. Abort();
  1645. }
  1646. }
  1647. }
  1648. if ((Mode == smRemote) && Delete && (Data.NewRemoteFileList->Count > 0))
  1649. {
  1650. if (!DeleteFiles(Data.NewRemoteFileList))
  1651. {
  1652. Abort();
  1653. }
  1654. }
  1655. }
  1656. }
  1657. __finally
  1658. {
  1659. if (Data.LocalFileList != NULL)
  1660. {
  1661. for (int Index = 0; Index < Data.LocalFileList->Count; Index++)
  1662. {
  1663. delete reinterpret_cast<TSynchronizeFileData*>
  1664. (Data.LocalFileList->Objects[Index]);
  1665. }
  1666. delete Data.LocalFileList;
  1667. }
  1668. TStringList * FileList = Data.NewRemoteFileList;
  1669. while (FileList != Data.ModifiedRemoteFileList)
  1670. {
  1671. if (FileList != NULL)
  1672. {
  1673. for (int Index = 0; Index < FileList->Count; Index++)
  1674. {
  1675. delete static_cast<TRemoteFile*>(FileList->Objects[Index]);
  1676. }
  1677. delete FileList;
  1678. }
  1679. FileList = Data.ModifiedRemoteFileList;
  1680. }
  1681. delete LocalFileList;
  1682. }
  1683. }
  1684. //---------------------------------------------------------------------------
  1685. void __fastcall TTerminal::SynchronizeFile(const AnsiString FileName,
  1686. const TRemoteFile * File, /*TSynchronizeData*/ void * Param)
  1687. {
  1688. TSynchronizeData * Data = static_cast<TSynchronizeData *>(Param);
  1689. bool Modified = false;
  1690. int Index = Data->LocalFileList->IndexOf(File->FileName);
  1691. bool New = (Index < 0);
  1692. if (!New)
  1693. {
  1694. TSynchronizeFileData * LocalData =
  1695. reinterpret_cast<TSynchronizeFileData *>(Data->LocalFileList->Objects[Index]);
  1696. LocalData->New = false;
  1697. bool LocalDirectory = (LocalData->Attr & faDirectory) != 0;
  1698. if (File->IsDirectory != LocalDirectory)
  1699. {
  1700. LogEvent(FORMAT("%s is directory on one side, but file on the another",
  1701. (File->FileName)));
  1702. }
  1703. else if (!File->IsDirectory)
  1704. {
  1705. FILETIME LocalLastWriteTime;
  1706. SYSTEMTIME SystemLastWriteTime;
  1707. FileTimeToLocalFileTime(&LocalData->LastWriteTime, &LocalLastWriteTime);
  1708. FileTimeToSystemTime(&LocalLastWriteTime, &SystemLastWriteTime);
  1709. TDateTime LocalTime = SystemTimeToDateTime(SystemLastWriteTime);
  1710. TDateTime RemoteTime = File->Modification;
  1711. UnifyDateTimePrecision(LocalTime, RemoteTime);
  1712. if (LocalTime < RemoteTime)
  1713. {
  1714. Modified = true;
  1715. }
  1716. else if (LocalTime > RemoteTime)
  1717. {
  1718. LocalData->Modified = true;
  1719. }
  1720. }
  1721. else
  1722. {
  1723. DoSynchronizeDirectory(
  1724. Data->LocalDirectory + File->FileName,
  1725. Data->RemoteDirectory + File->FileName,
  1726. Data->Mode, Data->Params, Data->OnSynchronizeDirectory);
  1727. }
  1728. }
  1729. if (New || Modified)
  1730. {
  1731. assert(!New || !Modified);
  1732. TStringList * FileList = New ? Data->NewRemoteFileList :
  1733. Data->ModifiedRemoteFileList;
  1734. FileList->AddObject(FileName,
  1735. const_cast<TRemoteFile *>(File)->Duplicate());
  1736. }
  1737. }
  1738. //---------------------------------------------------------------------------
  1739. bool __fastcall TTerminal::CopyToRemote(TStrings * FilesToCopy,
  1740. const AnsiString TargetDir, const TCopyParamType * CopyParam, int Params)
  1741. {
  1742. assert(FFileSystem);
  1743. assert(FilesToCopy);
  1744. bool Result = false;
  1745. bool DisconnectWhenComplete = false;
  1746. try
  1747. {
  1748. __int64 Size;
  1749. if (CopyParam->CalculateSize)
  1750. {
  1751. CalculateLocalFilesSize(FilesToCopy, Size);
  1752. }
  1753. TFileOperationProgressType OperationProgress(FOnProgress, FOnFinished);
  1754. OperationProgress.Start((Params & cpDelete ? foMove : foCopy), osLocal,
  1755. FilesToCopy->Count, Params & cpDragDrop, TargetDir);
  1756. if (CopyParam->CalculateSize)
  1757. {
  1758. OperationProgress.SetTotalSize(Size);
  1759. }
  1760. FOperationProgress = &OperationProgress;
  1761. try
  1762. {
  1763. AnsiString UnlockedTargetDir = TranslateLockedPath(TargetDir, false);
  1764. BeginTransaction();
  1765. try
  1766. {
  1767. if (IsLogging())
  1768. {
  1769. LogEvent(FORMAT("Copying %d files/directories to remote directory "
  1770. "\"%s\"", (FilesToCopy->Count, TargetDir)));
  1771. LogEvent(CopyParam->LogStr);
  1772. }
  1773. FFileSystem->CopyToRemote(FilesToCopy, UnlockedTargetDir,
  1774. CopyParam, Params, &OperationProgress, DisconnectWhenComplete);
  1775. }
  1776. __finally
  1777. {
  1778. if (Active)
  1779. {
  1780. ReactOnCommand(fsCopyToRemote);
  1781. EndTransaction();
  1782. }
  1783. }
  1784. if (OperationProgress.Cancel == csContinue)
  1785. {
  1786. Result = true;
  1787. }
  1788. }
  1789. __finally
  1790. {
  1791. OperationProgress.Stop();
  1792. FOperationProgress = NULL;
  1793. }
  1794. }
  1795. catch (Exception &E)
  1796. {
  1797. CommandError(&E, LoadStr(TOREMOTE_COPY_ERROR));
  1798. DisconnectWhenComplete = false;
  1799. }
  1800. if (DisconnectWhenComplete) CloseOnCompletion();
  1801. return Result;
  1802. }
  1803. //---------------------------------------------------------------------------
  1804. bool __fastcall TTerminal::CopyToLocal(TStrings * FilesToCopy,
  1805. const AnsiString TargetDir, const TCopyParamType * CopyParam, int Params)
  1806. {
  1807. assert(FFileSystem);
  1808. // see scp.c: sink(), tolocal()
  1809. bool Result = false;
  1810. bool OwnsFileList = (FilesToCopy == NULL);
  1811. bool DisconnectWhenComplete = false;
  1812. try
  1813. {
  1814. if (OwnsFileList)
  1815. {
  1816. FilesToCopy = new TStringList();
  1817. FilesToCopy->Assign(Files->SelectedFiles);
  1818. }
  1819. BeginTransaction();
  1820. try
  1821. {
  1822. __int64 TotalSize;
  1823. bool TotalSizeKnown = false;
  1824. TFileOperationProgressType OperationProgress(FOnProgress, FOnFinished);
  1825. if (CopyParam->CalculateSize)
  1826. {
  1827. ExceptionOnFail = true;
  1828. try
  1829. {
  1830. CalculateFilesSize(FilesToCopy, TotalSize, csIgnoreErrors);
  1831. TotalSizeKnown = true;
  1832. }
  1833. __finally
  1834. {
  1835. ExceptionOnFail = false;
  1836. }
  1837. }
  1838. OperationProgress.Start((Params & cpDelete ? foMove : foCopy), osRemote,
  1839. FilesToCopy->Count, Params & cpDragDrop, TargetDir);
  1840. if (TotalSizeKnown)
  1841. {
  1842. OperationProgress.SetTotalSize(TotalSize);
  1843. }
  1844. FOperationProgress = &OperationProgress;
  1845. try
  1846. {
  1847. try
  1848. {
  1849. try
  1850. {
  1851. FFileSystem->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params,
  1852. &OperationProgress, DisconnectWhenComplete);
  1853. }
  1854. __finally
  1855. {
  1856. if (Active)
  1857. {
  1858. ReactOnCommand(fsCopyToLocal);
  1859. }
  1860. }
  1861. }
  1862. catch (Exception &E)
  1863. {
  1864. CommandError(&E, LoadStr(TOLOCAL_COPY_ERROR));
  1865. DisconnectWhenComplete = false;
  1866. }
  1867. if (OperationProgress.Cancel == csContinue)
  1868. {
  1869. Result = true;
  1870. }
  1871. }
  1872. __finally
  1873. {
  1874. FOperationProgress = NULL;
  1875. OperationProgress.Stop();
  1876. }
  1877. }
  1878. __finally
  1879. {
  1880. // If session is still active (no fatal error) we reload directory
  1881. // by calling EndTransaction
  1882. EndTransaction();
  1883. }
  1884. }
  1885. __finally
  1886. {
  1887. if (OwnsFileList) delete FilesToCopy;
  1888. }
  1889. if (DisconnectWhenComplete) CloseOnCompletion();
  1890. return Result;
  1891. }
  1892. //---------------------------------------------------------------------------
  1893. __fastcall TTerminalList::TTerminalList(TConfiguration * AConfiguration) :
  1894. TObjectList()
  1895. {
  1896. assert(AConfiguration);
  1897. FConfiguration = AConfiguration;
  1898. }
  1899. //---------------------------------------------------------------------------
  1900. __fastcall TTerminalList::~TTerminalList()
  1901. {
  1902. assert(Count == 0);
  1903. }
  1904. //---------------------------------------------------------------------------
  1905. TTerminal * __fastcall TTerminalList::NewTerminal(TSessionData * Data)
  1906. {
  1907. TTerminal * Terminal = new TTerminal();
  1908. try
  1909. {
  1910. Terminal->Configuration = FConfiguration;
  1911. Terminal->SessionData = Data;
  1912. Add(Terminal);
  1913. }
  1914. catch(...)
  1915. {
  1916. delete Terminal;
  1917. throw;
  1918. }
  1919. return Terminal;
  1920. }
  1921. //---------------------------------------------------------------------------
  1922. void __fastcall TTerminalList::FreeTerminal(TTerminal * Terminal)
  1923. {
  1924. assert(IndexOf(Terminal) >= 0);
  1925. Remove(Terminal);
  1926. }
  1927. //---------------------------------------------------------------------------
  1928. void __fastcall TTerminalList::FreeAndNullTerminal(TTerminal * & Terminal)
  1929. {
  1930. TTerminal * T = Terminal;
  1931. Terminal = NULL;
  1932. FreeTerminal(T);
  1933. }
  1934. //---------------------------------------------------------------------------
  1935. TTerminal * __fastcall TTerminalList::GetTerminal(int Index)
  1936. {
  1937. return dynamic_cast<TTerminal *>(Items[Index]);
  1938. }
  1939. //---------------------------------------------------------------------------
  1940. void __fastcall TTerminalList::Idle()
  1941. {
  1942. TTerminal * Terminal;
  1943. for (int i = 0; i < Count; i++)
  1944. {
  1945. Terminal = Terminals[i];
  1946. if (Terminal->Status == sshReady)
  1947. {
  1948. Terminal->Idle();
  1949. }
  1950. }
  1951. }