FileMasks.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "FileMasks.h"
  5. #include "Common.h"
  6. #include "TextsCore.h"
  7. #include "RemoteFiles.h"
  8. #include "PuttyTools.h"
  9. #include "Terminal.h"
  10. #include <StrUtils.hpp>
  11. //---------------------------------------------------------------------------
  12. extern const wchar_t IncludeExcludeFileMasksDelimiter = L'|';
  13. static UnicodeString FileMasksDelimiters = L";,";
  14. static UnicodeString AllFileMasksDelimiters = FileMasksDelimiters + IncludeExcludeFileMasksDelimiter;
  15. static UnicodeString DirectoryMaskDelimiters = L"/\\";
  16. static UnicodeString FileMasksDelimiterStr = UnicodeString(FileMasksDelimiters[1]) + L' ';
  17. //---------------------------------------------------------------------------
  18. __fastcall EFileMasksException::EFileMasksException(
  19. UnicodeString Message, int AErrorStart, int AErrorLen) :
  20. Exception(Message)
  21. {
  22. ErrorStart = AErrorStart;
  23. ErrorLen = AErrorLen;
  24. }
  25. //---------------------------------------------------------------------------
  26. UnicodeString __fastcall MaskFilePart(const UnicodeString Part, const UnicodeString Mask, bool& Masked)
  27. {
  28. UnicodeString Result;
  29. int RestStart = 1;
  30. bool Delim = false;
  31. for (int Index = 1; Index <= Mask.Length(); Index++)
  32. {
  33. switch (Mask[Index])
  34. {
  35. case L'\\':
  36. if (!Delim)
  37. {
  38. Delim = true;
  39. Masked = true;
  40. break;
  41. }
  42. case L'*':
  43. if (!Delim)
  44. {
  45. Result += Part.SubString(RestStart, Part.Length() - RestStart + 1);
  46. RestStart = Part.Length() + 1;
  47. Masked = true;
  48. break;
  49. }
  50. case L'?':
  51. if (!Delim)
  52. {
  53. if (RestStart <= Part.Length())
  54. {
  55. Result += Part[RestStart];
  56. RestStart++;
  57. }
  58. Masked = true;
  59. break;
  60. }
  61. default:
  62. Result += Mask[Index];
  63. RestStart++;
  64. Delim = false;
  65. break;
  66. }
  67. }
  68. return Result;
  69. }
  70. //---------------------------------------------------------------------------
  71. UnicodeString __fastcall MaskFileName(UnicodeString FileName, const UnicodeString Mask)
  72. {
  73. if (IsEffectiveFileNameMask(Mask))
  74. {
  75. bool Masked;
  76. int P = Mask.LastDelimiter(L".");
  77. if (P > 0)
  78. {
  79. int P2 = FileName.LastDelimiter(".");
  80. // only dot at beginning of file name is not considered as
  81. // name/ext separator
  82. UnicodeString FileExt = P2 > 1 ?
  83. FileName.SubString(P2 + 1, FileName.Length() - P2) : UnicodeString();
  84. FileExt = MaskFilePart(FileExt, Mask.SubString(P + 1, Mask.Length() - P), Masked);
  85. if (P2 > 1)
  86. {
  87. FileName.SetLength(P2 - 1);
  88. }
  89. FileName = MaskFilePart(FileName, Mask.SubString(1, P - 1), Masked);
  90. if (!FileExt.IsEmpty())
  91. {
  92. FileName += L"." + FileExt;
  93. }
  94. }
  95. else
  96. {
  97. FileName = MaskFilePart(FileName, Mask, Masked);
  98. }
  99. }
  100. return FileName;
  101. }
  102. //---------------------------------------------------------------------------
  103. bool __fastcall IsEffectiveFileNameMask(const UnicodeString & Mask)
  104. {
  105. return !Mask.IsEmpty() && (Mask != L"*") && (Mask != L"*.*");
  106. }
  107. //---------------------------------------------------------------------------
  108. UnicodeString __fastcall DelimitFileNameMask(UnicodeString Mask)
  109. {
  110. for (int i = 1; i <= Mask.Length(); i++)
  111. {
  112. if (wcschr(L"\\*?", Mask[i]) != NULL)
  113. {
  114. Mask.Insert(L"\\", i);
  115. i++;
  116. }
  117. }
  118. return Mask;
  119. }
  120. //---------------------------------------------------------------------------
  121. //---------------------------------------------------------------------------
  122. TFileMasks::TParams::TParams() :
  123. Size(0)
  124. {
  125. }
  126. //---------------------------------------------------------------------------
  127. UnicodeString TFileMasks::TParams::ToString() const
  128. {
  129. return UnicodeString(L"[") + IntToStr(Size) + L"/" + DateTimeToStr(Modification) + L"]";
  130. }
  131. //---------------------------------------------------------------------------
  132. //---------------------------------------------------------------------------
  133. bool __fastcall TFileMasks::IsMask(const UnicodeString Mask)
  134. {
  135. return (Mask.LastDelimiter(L"?*[") > 0);
  136. }
  137. //---------------------------------------------------------------------------
  138. bool __fastcall TFileMasks::IsAnyMask(const UnicodeString & Mask)
  139. {
  140. return Mask.IsEmpty() || (Mask == L"*.*") || (Mask == L"*");
  141. }
  142. //---------------------------------------------------------------------------
  143. UnicodeString __fastcall TFileMasks::NormalizeMask(const UnicodeString & Mask, const UnicodeString & AnyMask)
  144. {
  145. if (IsAnyMask(Mask))
  146. {
  147. return AnyMask;
  148. }
  149. else
  150. {
  151. return Mask;
  152. }
  153. }
  154. //---------------------------------------------------------------------------
  155. UnicodeString __fastcall TFileMasks::ComposeMaskStr(
  156. TStrings * MasksStr, bool Directory)
  157. {
  158. UnicodeString Result;
  159. UnicodeString ResultNoDirMask;
  160. for (int I = 0; I < MasksStr->Count; I++)
  161. {
  162. UnicodeString Str = MasksStr->Strings[I].Trim();
  163. if (!Str.IsEmpty())
  164. {
  165. for (int P = 1; P <= Str.Length(); P++)
  166. {
  167. if (Str.IsDelimiter(AllFileMasksDelimiters, P))
  168. {
  169. Str.Insert(Str[P], P);
  170. P++;
  171. }
  172. }
  173. UnicodeString StrNoDirMask;
  174. if (Directory)
  175. {
  176. StrNoDirMask = Str;
  177. Str = MakeDirectoryMask(Str);
  178. }
  179. else
  180. {
  181. while (Str.IsDelimiter(DirectoryMaskDelimiters, Str.Length()))
  182. {
  183. Str.SetLength(Str.Length() - 1);
  184. }
  185. StrNoDirMask = Str;
  186. }
  187. AddToList(Result, Str, FileMasksDelimiterStr);
  188. AddToList(ResultNoDirMask, StrNoDirMask, FileMasksDelimiterStr);
  189. }
  190. }
  191. // For directories, the above will add slash ay the end of masks,
  192. // breaking size and time masks and thus circumverting their validation.
  193. // This performes as hoc validation to cover the scenario.
  194. // For files this makes no difference, but no harm either
  195. TFileMasks Temp(Directory ? 1 : 0);
  196. Temp = ResultNoDirMask;
  197. return Result;
  198. }
  199. //---------------------------------------------------------------------------
  200. UnicodeString __fastcall TFileMasks::ComposeMaskStr(
  201. TStrings * IncludeFileMasksStr, TStrings * ExcludeFileMasksStr,
  202. TStrings * IncludeDirectoryMasksStr, TStrings * ExcludeDirectoryMasksStr)
  203. {
  204. UnicodeString IncludeMasks = ComposeMaskStr(IncludeFileMasksStr, false);
  205. AddToList(IncludeMasks, ComposeMaskStr(IncludeDirectoryMasksStr, true), FileMasksDelimiterStr);
  206. UnicodeString ExcludeMasks = ComposeMaskStr(ExcludeFileMasksStr, false);
  207. AddToList(ExcludeMasks, ComposeMaskStr(ExcludeDirectoryMasksStr, true), FileMasksDelimiterStr);
  208. UnicodeString Result = IncludeMasks;
  209. if (!ExcludeMasks.IsEmpty())
  210. {
  211. if (!Result.IsEmpty())
  212. {
  213. Result += L' ';
  214. }
  215. Result += UnicodeString(IncludeExcludeFileMasksDelimiter) + L' ' + ExcludeMasks;
  216. }
  217. return Result;
  218. }
  219. //---------------------------------------------------------------------------
  220. __fastcall TFileMasks::TFileMasks()
  221. {
  222. Init();
  223. }
  224. //---------------------------------------------------------------------------
  225. __fastcall TFileMasks::TFileMasks(int ForceDirectoryMasks)
  226. {
  227. Init();
  228. FForceDirectoryMasks = ForceDirectoryMasks;
  229. }
  230. //---------------------------------------------------------------------------
  231. __fastcall TFileMasks::TFileMasks(const TFileMasks & Source)
  232. {
  233. Init();
  234. FForceDirectoryMasks = Source.FForceDirectoryMasks;
  235. SetStr(Source.Masks, false);
  236. }
  237. //---------------------------------------------------------------------------
  238. __fastcall TFileMasks::TFileMasks(const UnicodeString & AMasks)
  239. {
  240. Init();
  241. SetStr(AMasks, false);
  242. }
  243. //---------------------------------------------------------------------------
  244. __fastcall TFileMasks::~TFileMasks()
  245. {
  246. Clear();
  247. }
  248. //---------------------------------------------------------------------------
  249. void __fastcall TFileMasks::Init()
  250. {
  251. FForceDirectoryMasks = -1;
  252. DoInit(false);
  253. }
  254. //---------------------------------------------------------------------------
  255. void __fastcall TFileMasks::DoInit(bool Delete)
  256. {
  257. for (int Index = 0; Index < 4; Index++)
  258. {
  259. if (Delete)
  260. {
  261. delete FMasksStr[Index];
  262. }
  263. FMasksStr[Index] = NULL;
  264. }
  265. }
  266. //---------------------------------------------------------------------------
  267. void __fastcall TFileMasks::Clear()
  268. {
  269. DoInit(true);
  270. for (int Index = 0; Index < 4; Index++)
  271. {
  272. Clear(FMasks[Index]);
  273. }
  274. }
  275. //---------------------------------------------------------------------------
  276. void __fastcall TFileMasks::Clear(TMasks & Masks)
  277. {
  278. TMasks::iterator I = Masks.begin();
  279. while (I != Masks.end())
  280. {
  281. ReleaseMaskMask((*I).FileNameMask);
  282. ReleaseMaskMask((*I).DirectoryMask);
  283. I++;
  284. }
  285. Masks.clear();
  286. }
  287. //---------------------------------------------------------------------------
  288. bool __fastcall TFileMasks::MatchesMasks(const UnicodeString FileName, bool Directory,
  289. const UnicodeString Path, const TParams * Params, const TMasks & Masks, bool Recurse)
  290. {
  291. bool Result = false;
  292. TMasks::const_iterator I = Masks.begin();
  293. while (!Result && (I != Masks.end()))
  294. {
  295. const TMask & Mask = *I;
  296. Result =
  297. MatchesMaskMask(Mask.DirectoryMask, Path) &&
  298. MatchesMaskMask(Mask.FileNameMask, FileName);
  299. if (Result)
  300. {
  301. bool HasSize = (Params != NULL);
  302. switch (Mask.HighSizeMask)
  303. {
  304. case TMask::None:
  305. Result = true;
  306. break;
  307. case TMask::Open:
  308. Result = HasSize && (Params->Size < Mask.HighSize);
  309. break;
  310. case TMask::Close:
  311. Result = HasSize && (Params->Size <= Mask.HighSize);
  312. break;
  313. }
  314. if (Result)
  315. {
  316. switch (Mask.LowSizeMask)
  317. {
  318. case TMask::None:
  319. Result = true;
  320. break;
  321. case TMask::Open:
  322. Result = HasSize && (Params->Size > Mask.LowSize);
  323. break;
  324. case TMask::Close:
  325. Result = HasSize && (Params->Size >= Mask.LowSize);
  326. break;
  327. }
  328. }
  329. bool HasModification = (Params != NULL);
  330. if (Result)
  331. {
  332. switch (Mask.HighModificationMask)
  333. {
  334. case TMask::None:
  335. Result = true;
  336. break;
  337. case TMask::Open:
  338. Result = HasModification && (Params->Modification < Mask.HighModification);
  339. break;
  340. case TMask::Close:
  341. Result = HasModification && (Params->Modification <= Mask.HighModification);
  342. break;
  343. }
  344. }
  345. if (Result)
  346. {
  347. switch (Mask.LowModificationMask)
  348. {
  349. case TMask::None:
  350. Result = true;
  351. break;
  352. case TMask::Open:
  353. Result = HasModification && (Params->Modification > Mask.LowModification);
  354. break;
  355. case TMask::Close:
  356. Result = HasModification && (Params->Modification >= Mask.LowModification);
  357. break;
  358. }
  359. }
  360. }
  361. I++;
  362. }
  363. if (!Result && Directory && !IsUnixRootPath(Path) && Recurse)
  364. {
  365. UnicodeString ParentFileName = UnixExtractFileName(Path);
  366. UnicodeString ParentPath = SimpleUnixExcludeTrailingBackslash(UnixExtractFilePath(Path));
  367. // Pass Params down or not?
  368. // Currently it includes Size/Time only, what is not used for directories.
  369. // So it depends of future use. Possibly we should make a copy
  370. // and pass on only relevant fields.
  371. Result = MatchesMasks(ParentFileName, true, ParentPath, Params, Masks, Recurse);
  372. }
  373. return Result;
  374. }
  375. //---------------------------------------------------------------------------
  376. bool __fastcall TFileMasks::Matches(const UnicodeString FileName, bool Directory,
  377. const UnicodeString Path, const TParams * Params) const
  378. {
  379. bool ImplicitMatch;
  380. return Matches(FileName, Directory, Path, Params, ImplicitMatch);
  381. }
  382. //---------------------------------------------------------------------------
  383. bool __fastcall TFileMasks::Matches(const UnicodeString FileName, bool Directory,
  384. const UnicodeString Path, const TParams * Params,
  385. bool & ImplicitMatch) const
  386. {
  387. bool ImplicitIncludeMatch = FMasks[MASK_INDEX(Directory, true)].empty();
  388. bool ExplicitIncludeMatch = MatchesMasks(FileName, Directory, Path, Params, FMasks[MASK_INDEX(Directory, true)], true);
  389. bool Result =
  390. (ImplicitIncludeMatch || ExplicitIncludeMatch) &&
  391. !MatchesMasks(FileName, Directory, Path, Params, FMasks[MASK_INDEX(Directory, false)], false);
  392. ImplicitMatch =
  393. Result && ImplicitIncludeMatch && !ExplicitIncludeMatch &&
  394. FMasks[MASK_INDEX(Directory, false)].empty();
  395. return Result;
  396. }
  397. //---------------------------------------------------------------------------
  398. bool __fastcall TFileMasks::Matches(const UnicodeString FileName, bool Local,
  399. bool Directory, const TParams * Params) const
  400. {
  401. bool ImplicitMatch;
  402. return Matches(FileName, Local, Directory, Params, ImplicitMatch);
  403. }
  404. //---------------------------------------------------------------------------
  405. bool __fastcall TFileMasks::Matches(const UnicodeString FileName, bool Local,
  406. bool Directory, const TParams * Params, bool & ImplicitMatch) const
  407. {
  408. bool Result;
  409. if (Local)
  410. {
  411. UnicodeString Path = ExtractFilePath(FileName);
  412. if (!Path.IsEmpty())
  413. {
  414. Path = ToUnixPath(ExcludeTrailingBackslash(Path));
  415. }
  416. Result = Matches(ExtractFileName(FileName), Directory, Path, Params,
  417. ImplicitMatch);
  418. }
  419. else
  420. {
  421. Result = Matches(UnixExtractFileName(FileName), Directory,
  422. SimpleUnixExcludeTrailingBackslash(UnixExtractFilePath(FileName)), Params,
  423. ImplicitMatch);
  424. }
  425. return Result;
  426. }
  427. //---------------------------------------------------------------------------
  428. bool __fastcall TFileMasks::operator ==(const TFileMasks & rhm) const
  429. {
  430. return (Masks == rhm.Masks);
  431. }
  432. //---------------------------------------------------------------------------
  433. TFileMasks & __fastcall TFileMasks::operator =(const UnicodeString & rhs)
  434. {
  435. Masks = rhs;
  436. return *this;
  437. }
  438. //---------------------------------------------------------------------------
  439. TFileMasks & __fastcall TFileMasks::operator =(const TFileMasks & rhm)
  440. {
  441. FForceDirectoryMasks = rhm.FForceDirectoryMasks;
  442. Masks = rhm.Masks;
  443. return *this;
  444. }
  445. //---------------------------------------------------------------------------
  446. bool __fastcall TFileMasks::operator ==(const UnicodeString & rhs) const
  447. {
  448. return (Masks == rhs);
  449. }
  450. //---------------------------------------------------------------------------
  451. void __fastcall TFileMasks::ThrowError(int Start, int End)
  452. {
  453. throw EFileMasksException(
  454. FMTLOAD(MASK_ERROR, (Masks.SubString(Start, End - Start + 1))),
  455. Start, End - Start + 1);
  456. }
  457. //---------------------------------------------------------------------------
  458. void __fastcall TFileMasks::CreateMaskMask(const UnicodeString & Mask, int Start, int End,
  459. bool Ex, TMaskMask & MaskMask)
  460. {
  461. try
  462. {
  463. assert(MaskMask.Mask == NULL);
  464. if (Ex && IsAnyMask(Mask))
  465. {
  466. MaskMask.Kind = TMaskMask::Any;
  467. MaskMask.Mask = NULL;
  468. }
  469. else
  470. {
  471. MaskMask.Kind = (Ex && (Mask == L"*.")) ? TMaskMask::NoExt : TMaskMask::Regular;
  472. MaskMask.Mask = new Masks::TMask(Mask);
  473. }
  474. }
  475. catch(...)
  476. {
  477. ThrowError(Start, End);
  478. }
  479. }
  480. //---------------------------------------------------------------------------
  481. UnicodeString __fastcall TFileMasks::MakeDirectoryMask(UnicodeString Str)
  482. {
  483. assert(!Str.IsEmpty());
  484. if (Str.IsEmpty() || !Str.IsDelimiter(DirectoryMaskDelimiters, Str.Length()))
  485. {
  486. int D = Str.LastDelimiter(DirectoryMaskDelimiters);
  487. // if there's any [back]slash anywhere in str,
  488. // add the same [back]slash at the end, otherwise add slash
  489. wchar_t Delimiter = (D > 0) ? Str[D] : DirectoryMaskDelimiters[1];
  490. Str += Delimiter;
  491. }
  492. return Str;
  493. }
  494. //---------------------------------------------------------------------------
  495. void __fastcall TFileMasks::CreateMask(
  496. const UnicodeString & MaskStr, int MaskStart, int /*MaskEnd*/, bool Include)
  497. {
  498. bool Directory = false; // shut up
  499. TMask Mask;
  500. Mask.MaskStr = MaskStr;
  501. Mask.UserStr = MaskStr;
  502. Mask.FileNameMask.Kind = TMaskMask::Any;
  503. Mask.FileNameMask.Mask = NULL;
  504. Mask.DirectoryMask.Kind = TMaskMask::Any;
  505. Mask.DirectoryMask.Mask = NULL;
  506. Mask.HighSizeMask = TMask::None;
  507. Mask.LowSizeMask = TMask::None;
  508. Mask.HighModificationMask = TMask::None;
  509. Mask.LowModificationMask = TMask::None;
  510. wchar_t NextPartDelimiter = L'\0';
  511. int NextPartFrom = 1;
  512. while (NextPartFrom <= MaskStr.Length())
  513. {
  514. wchar_t PartDelimiter = NextPartDelimiter;
  515. int PartFrom = NextPartFrom;
  516. UnicodeString PartStr = CopyToChars(MaskStr, NextPartFrom, L"<>", false, &NextPartDelimiter, true);
  517. int PartStart = MaskStart + PartFrom - 1;
  518. int PartEnd = MaskStart + NextPartFrom - 1 - 2;
  519. TrimEx(PartStr, PartStart, PartEnd);
  520. if (PartDelimiter != L'\0')
  521. {
  522. bool Low = (PartDelimiter == L'>');
  523. TMask::TMaskBoundary Boundary;
  524. if ((PartStr.Length() >= 1) && (PartStr[1] == L'='))
  525. {
  526. Boundary = TMask::Close;
  527. PartStr.Delete(1, 1);
  528. }
  529. else
  530. {
  531. Boundary = TMask::Open;
  532. }
  533. TFormatSettings FormatSettings = TFormatSettings::Create(GetDefaultLCID());
  534. FormatSettings.DateSeparator = L'-';
  535. FormatSettings.TimeSeparator = L':';
  536. FormatSettings.ShortDateFormat = "yyyy/mm/dd";
  537. FormatSettings.ShortTimeFormat = "hh:nn:ss";
  538. TDateTime Modification;
  539. if (TryStrToDateTime(PartStr, Modification, FormatSettings) ||
  540. TryRelativeStrToDateTime(PartStr, Modification))
  541. {
  542. TMask::TMaskBoundary & ModificationMask =
  543. (Low ? Mask.LowModificationMask : Mask.HighModificationMask);
  544. if ((ModificationMask != TMask::None) || Directory)
  545. {
  546. // include delimiter into size part
  547. ThrowError(PartStart - 1, PartEnd);
  548. }
  549. ModificationMask = Boundary;
  550. (Low ? Mask.LowModification : Mask.HighModification) = Modification;
  551. }
  552. else
  553. {
  554. TMask::TMaskBoundary & SizeMask = (Low ? Mask.LowSizeMask : Mask.HighSizeMask);
  555. __int64 & Size = (Low ? Mask.LowSize : Mask.HighSize);
  556. if ((SizeMask != TMask::None) || Directory)
  557. {
  558. // include delimiter into size part
  559. ThrowError(PartStart - 1, PartEnd);
  560. }
  561. SizeMask = Boundary;
  562. Size = ParseSize(PartStr);
  563. }
  564. }
  565. else if (!PartStr.IsEmpty())
  566. {
  567. int D = PartStr.LastDelimiter(DirectoryMaskDelimiters);
  568. Directory = (D > 0) && (D == PartStr.Length());
  569. if (Directory)
  570. {
  571. do
  572. {
  573. PartStr.SetLength(PartStr.Length() - 1);
  574. Mask.UserStr.Delete(PartStart - MaskStart + D, 1);
  575. D--;
  576. }
  577. while (PartStr.IsDelimiter(DirectoryMaskDelimiters, PartStr.Length()));
  578. D = PartStr.LastDelimiter(DirectoryMaskDelimiters);
  579. if (FForceDirectoryMasks == 0)
  580. {
  581. Directory = false;
  582. Mask.MaskStr = Mask.UserStr;
  583. }
  584. }
  585. else if (FForceDirectoryMasks > 0)
  586. {
  587. Directory = true;
  588. Mask.MaskStr.Insert(DirectoryMaskDelimiters[1], PartStart - MaskStart + PartStr.Length());
  589. }
  590. if (D > 0)
  591. {
  592. // make sure sole "/" (root dir) is preserved as is
  593. CreateMaskMask(
  594. SimpleUnixExcludeTrailingBackslash(ToUnixPath(PartStr.SubString(1, D))),
  595. PartStart, PartStart + D - 1, false,
  596. Mask.DirectoryMask);
  597. CreateMaskMask(
  598. PartStr.SubString(D + 1, PartStr.Length() - D),
  599. PartStart + D, PartEnd, true,
  600. Mask.FileNameMask);
  601. }
  602. else
  603. {
  604. CreateMaskMask(PartStr, PartStart, PartEnd, true, Mask.FileNameMask);
  605. }
  606. }
  607. }
  608. FMasks[MASK_INDEX(Directory, Include)].push_back(Mask);
  609. }
  610. //---------------------------------------------------------------------------
  611. TStrings * __fastcall TFileMasks::GetMasksStr(int Index) const
  612. {
  613. if (FMasksStr[Index] == NULL)
  614. {
  615. FMasksStr[Index] = new TStringList();
  616. TMasks::const_iterator I = FMasks[Index].begin();
  617. while (I != FMasks[Index].end())
  618. {
  619. FMasksStr[Index]->Add((*I).UserStr);
  620. I++;
  621. }
  622. }
  623. return FMasksStr[Index];
  624. }
  625. //---------------------------------------------------------------------------
  626. void __fastcall TFileMasks::ReleaseMaskMask(TMaskMask & MaskMask)
  627. {
  628. delete MaskMask.Mask;
  629. }
  630. //---------------------------------------------------------------------------
  631. void __fastcall TFileMasks::TrimEx(UnicodeString & Str, int & Start, int & End)
  632. {
  633. UnicodeString Buf = TrimLeft(Str);
  634. Start += Str.Length() - Buf.Length();
  635. Str = TrimRight(Buf);
  636. End -= Buf.Length() - Str.Length();
  637. }
  638. //---------------------------------------------------------------------------
  639. bool __fastcall TFileMasks::MatchesMaskMask(const TMaskMask & MaskMask, const UnicodeString & Str)
  640. {
  641. bool Result;
  642. if (MaskMask.Kind == TMaskMask::Any)
  643. {
  644. Result = true;
  645. }
  646. else if ((MaskMask.Kind == TMaskMask::NoExt) && (Str.Pos(L".") == 0))
  647. {
  648. Result = true;
  649. }
  650. else
  651. {
  652. Result = MaskMask.Mask->Matches(Str);
  653. }
  654. return Result;
  655. }
  656. //---------------------------------------------------------------------------
  657. void __fastcall TFileMasks::SetMasks(const UnicodeString value)
  658. {
  659. if (FStr != value)
  660. {
  661. SetStr(value, false);
  662. }
  663. }
  664. //---------------------------------------------------------------------------
  665. void __fastcall TFileMasks::SetMask(const UnicodeString & Mask)
  666. {
  667. SetStr(Mask, true);
  668. }
  669. //---------------------------------------------------------------------------
  670. void __fastcall TFileMasks::SetStr(const UnicodeString Str, bool SingleMask)
  671. {
  672. UnicodeString Backup = FStr;
  673. try
  674. {
  675. FStr = Str;
  676. Clear();
  677. int NextMaskFrom = 1;
  678. bool Include = true;
  679. while (NextMaskFrom <= Str.Length())
  680. {
  681. int MaskStart = NextMaskFrom;
  682. wchar_t NextMaskDelimiter;
  683. UnicodeString MaskStr;
  684. if (SingleMask)
  685. {
  686. MaskStr = Str;
  687. NextMaskFrom = Str.Length() + 1;
  688. NextMaskDelimiter = L'\0';
  689. }
  690. else
  691. {
  692. MaskStr = CopyToChars(Str, NextMaskFrom, AllFileMasksDelimiters, false, &NextMaskDelimiter, true);
  693. }
  694. int MaskEnd = NextMaskFrom - 2;
  695. TrimEx(MaskStr, MaskStart, MaskEnd);
  696. if (!MaskStr.IsEmpty())
  697. {
  698. CreateMask(MaskStr, MaskStart, MaskEnd, Include);
  699. }
  700. if (NextMaskDelimiter == IncludeExcludeFileMasksDelimiter)
  701. {
  702. if (Include)
  703. {
  704. Include = false;
  705. }
  706. else
  707. {
  708. ThrowError(NextMaskFrom - 1, Str.Length());
  709. }
  710. }
  711. }
  712. }
  713. catch(...)
  714. {
  715. // this does not work correctly if previous mask was set using SetMask.
  716. // this should not fail (the mask was validated before),
  717. // otherwise we end in an infinite loop
  718. SetStr(Backup, false);
  719. throw;
  720. }
  721. }
  722. //---------------------------------------------------------------------------
  723. //---------------------------------------------------------------------------
  724. #define TEXT_TOKEN L'\255'
  725. //---------------------------------------------------------------------------
  726. const wchar_t TCustomCommand::NoQuote = L'\0';
  727. const UnicodeString TCustomCommand::Quotes = L"\"'";
  728. //---------------------------------------------------------------------------
  729. TCustomCommand::TCustomCommand()
  730. {
  731. }
  732. //---------------------------------------------------------------------------
  733. void __fastcall TCustomCommand::GetToken(
  734. const UnicodeString & Command, int Index, int & Len, wchar_t & PatternCmd)
  735. {
  736. assert(Index <= Command.Length());
  737. const wchar_t * Ptr = Command.c_str() + Index - 1;
  738. if (Ptr[0] == L'!')
  739. {
  740. PatternCmd = Ptr[1];
  741. if (PatternCmd == L'\0')
  742. {
  743. Len = 1;
  744. }
  745. else if (PatternCmd == L'!')
  746. {
  747. Len = 2;
  748. }
  749. else
  750. {
  751. Len = PatternLen(Command, Index);
  752. }
  753. if (Len <= 0)
  754. {
  755. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNKNOWN, (PatternCmd, Index)));
  756. }
  757. else
  758. {
  759. if ((Command.Length() - Index + 1) < Len)
  760. {
  761. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (PatternCmd, Index)));
  762. }
  763. }
  764. }
  765. else
  766. {
  767. PatternCmd = TEXT_TOKEN;
  768. const wchar_t * NextPattern = wcschr(Ptr, L'!');
  769. if (NextPattern == NULL)
  770. {
  771. Len = Command.Length() - Index + 1;
  772. }
  773. else
  774. {
  775. Len = NextPattern - Ptr;
  776. }
  777. }
  778. }
  779. //---------------------------------------------------------------------------
  780. UnicodeString __fastcall TCustomCommand::Complete(const UnicodeString & Command,
  781. bool LastPass)
  782. {
  783. UnicodeString Result;
  784. int Index = 1;
  785. while (Index <= Command.Length())
  786. {
  787. int Len;
  788. wchar_t PatternCmd;
  789. GetToken(Command, Index, Len, PatternCmd);
  790. if (PatternCmd == TEXT_TOKEN)
  791. {
  792. Result += Command.SubString(Index, Len);
  793. }
  794. else if (PatternCmd == L'!')
  795. {
  796. if (LastPass)
  797. {
  798. Result += L'!';
  799. }
  800. else
  801. {
  802. Result += Command.SubString(Index, Len);
  803. }
  804. }
  805. else
  806. {
  807. wchar_t Quote = NoQuote;
  808. if ((Index > 1) && (Index + Len - 1 < Command.Length()) &&
  809. Command.IsDelimiter(Quotes, Index - 1) &&
  810. Command.IsDelimiter(Quotes, Index + Len) &&
  811. (Command[Index - 1] == Command[Index + Len]))
  812. {
  813. Quote = Command[Index - 1];
  814. }
  815. UnicodeString Pattern = Command.SubString(Index, Len);
  816. UnicodeString Replacement;
  817. bool Delimit = true;
  818. if (PatternReplacement(Pattern, Replacement, Delimit))
  819. {
  820. if (!LastPass)
  821. {
  822. Replacement = ReplaceStr(Replacement, L"!", L"!!");
  823. }
  824. if (Delimit)
  825. {
  826. DelimitReplacement(Replacement, Quote);
  827. }
  828. Result += Replacement;
  829. }
  830. else
  831. {
  832. Result += Pattern;
  833. }
  834. }
  835. Index += Len;
  836. }
  837. return Result;
  838. }
  839. //---------------------------------------------------------------------------
  840. void __fastcall TCustomCommand::DelimitReplacement(UnicodeString & Replacement, wchar_t Quote)
  841. {
  842. Replacement = ShellDelimitStr(Replacement, Quote);
  843. }
  844. //---------------------------------------------------------------------------
  845. void __fastcall TCustomCommand::Validate(const UnicodeString & Command)
  846. {
  847. CustomValidate(Command, NULL);
  848. }
  849. //---------------------------------------------------------------------------
  850. void __fastcall TCustomCommand::CustomValidate(const UnicodeString & Command,
  851. void * Arg)
  852. {
  853. int Index = 1;
  854. while (Index <= Command.Length())
  855. {
  856. int Len;
  857. wchar_t PatternCmd;
  858. GetToken(Command, Index, Len, PatternCmd);
  859. ValidatePattern(Command, Index, Len, PatternCmd, Arg);
  860. Index += Len;
  861. }
  862. }
  863. //---------------------------------------------------------------------------
  864. bool __fastcall TCustomCommand::FindPattern(const UnicodeString & Command,
  865. wchar_t PatternCmd)
  866. {
  867. bool Result = false;
  868. int Index = 1;
  869. while (!Result && (Index <= Command.Length()))
  870. {
  871. int Len;
  872. wchar_t APatternCmd;
  873. GetToken(Command, Index, Len, APatternCmd);
  874. if (((PatternCmd != L'!') && (PatternCmd == APatternCmd)) ||
  875. ((PatternCmd == L'!') && (Len == 1) && (APatternCmd != TEXT_TOKEN)))
  876. {
  877. Result = true;
  878. }
  879. Index += Len;
  880. }
  881. return Result;
  882. }
  883. //---------------------------------------------------------------------------
  884. void __fastcall TCustomCommand::ValidatePattern(const UnicodeString & /*Command*/,
  885. int /*Index*/, int /*Len*/, wchar_t /*PatternCmd*/, void * /*Arg*/)
  886. {
  887. }
  888. //---------------------------------------------------------------------------
  889. //---------------------------------------------------------------------------
  890. TInteractiveCustomCommand::TInteractiveCustomCommand(
  891. TCustomCommand * ChildCustomCommand)
  892. {
  893. FChildCustomCommand = ChildCustomCommand;
  894. }
  895. //---------------------------------------------------------------------------
  896. void __fastcall TInteractiveCustomCommand::Prompt(
  897. const UnicodeString & /*Prompt*/, UnicodeString & Value)
  898. {
  899. Value = L"";
  900. }
  901. //---------------------------------------------------------------------------
  902. void __fastcall TInteractiveCustomCommand::Execute(
  903. const UnicodeString & /*Command*/, UnicodeString & Value)
  904. {
  905. Value = L"";
  906. }
  907. //---------------------------------------------------------------------------
  908. int __fastcall TInteractiveCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  909. {
  910. int Len;
  911. wchar_t PatternCmd = (Index < Command.Length()) ? Command[Index + 1] : L'\0';
  912. switch (PatternCmd)
  913. {
  914. case L'?':
  915. {
  916. const wchar_t * Ptr = Command.c_str() + Index - 1;
  917. const wchar_t * PatternEnd = wcschr(Ptr + 1, L'!');
  918. if (PatternEnd == NULL)
  919. {
  920. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (Command[Index + 1], Index)));
  921. }
  922. Len = PatternEnd - Ptr + 1;
  923. }
  924. break;
  925. case L'`':
  926. {
  927. const wchar_t * Ptr = Command.c_str() + Index - 1;
  928. const wchar_t * PatternEnd = wcschr(Ptr + 2, L'`');
  929. if (PatternEnd == NULL)
  930. {
  931. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (Command[Index + 1], Index)));
  932. }
  933. Len = PatternEnd - Ptr + 1;
  934. }
  935. break;
  936. default:
  937. Len = FChildCustomCommand->PatternLen(Command, Index);
  938. break;
  939. }
  940. return Len;
  941. }
  942. //---------------------------------------------------------------------------
  943. bool __fastcall TInteractiveCustomCommand::PatternReplacement(const UnicodeString & Pattern,
  944. UnicodeString & Replacement, bool & Delimit)
  945. {
  946. bool Result;
  947. if ((Pattern.Length() >= 3) && (Pattern[2] == L'?'))
  948. {
  949. UnicodeString PromptStr;
  950. int Pos = Pattern.SubString(3, Pattern.Length() - 2).Pos(L"?");
  951. if (Pos > 0)
  952. {
  953. Replacement = Pattern.SubString(3 + Pos, Pattern.Length() - 3 - Pos);
  954. if ((Pos > 1) && (Pattern[3 + Pos - 2] == L'\\'))
  955. {
  956. Delimit = false;
  957. Pos--;
  958. }
  959. PromptStr = Pattern.SubString(3, Pos - 1);
  960. }
  961. else
  962. {
  963. PromptStr = Pattern.SubString(3, Pattern.Length() - 3);
  964. }
  965. Prompt(PromptStr, Replacement);
  966. Result = true;
  967. }
  968. else if ((Pattern.Length() >= 3) && (Pattern[2] == L'`'))
  969. {
  970. UnicodeString Command = Pattern.SubString(3, Pattern.Length() - 3);
  971. Command = FChildCustomCommand->Complete(Command, true);
  972. Execute(Command, Replacement);
  973. Delimit = false;
  974. Result = true;
  975. }
  976. else
  977. {
  978. Result = false;
  979. }
  980. return Result;
  981. }
  982. //---------------------------------------------------------------------------
  983. //---------------------------------------------------------------------------
  984. __fastcall TCustomCommandData::TCustomCommandData()
  985. {
  986. }
  987. //---------------------------------------------------------------------------
  988. __fastcall TCustomCommandData::TCustomCommandData(TTerminal * Terminal)
  989. {
  990. Init(Terminal->SessionData, Terminal->UserName, Terminal->Password,
  991. Terminal->GetSessionInfo().HostKeyFingerprint);
  992. }
  993. //---------------------------------------------------------------------------
  994. __fastcall TCustomCommandData::TCustomCommandData(
  995. TSessionData * SessionData, const UnicodeString & UserName, const UnicodeString & Password)
  996. {
  997. Init(SessionData, UserName, Password, UnicodeString());
  998. }
  999. //---------------------------------------------------------------------------
  1000. void __fastcall TCustomCommandData::Init(
  1001. TSessionData * ASessionData, const UnicodeString & AUserName,
  1002. const UnicodeString & APassword, const UnicodeString & AHostKey)
  1003. {
  1004. FSessionData.reset(new TSessionData(L""));
  1005. FSessionData->Assign(ASessionData);
  1006. FSessionData->UserName = AUserName;
  1007. FSessionData->Password = APassword;
  1008. FSessionData->HostKey = AHostKey;
  1009. }
  1010. //---------------------------------------------------------------------------
  1011. void __fastcall TCustomCommandData::operator=(const TCustomCommandData & Data)
  1012. {
  1013. assert(Data.SessionData != NULL);
  1014. FSessionData.reset(new TSessionData(L""));
  1015. FSessionData->Assign(Data.SessionData);
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. TSessionData * __fastcall TCustomCommandData::GetSesssionData() const
  1019. {
  1020. return FSessionData.get();
  1021. }
  1022. //---------------------------------------------------------------------------
  1023. //---------------------------------------------------------------------------
  1024. TFileCustomCommand::TFileCustomCommand()
  1025. {
  1026. }
  1027. //---------------------------------------------------------------------------
  1028. TFileCustomCommand::TFileCustomCommand(const TCustomCommandData & Data,
  1029. const UnicodeString & Path)
  1030. {
  1031. FData = Data;
  1032. FPath = Path;
  1033. }
  1034. //---------------------------------------------------------------------------
  1035. TFileCustomCommand::TFileCustomCommand(const TCustomCommandData & Data,
  1036. const UnicodeString & Path, const UnicodeString & FileName,
  1037. const UnicodeString & FileList) :
  1038. TCustomCommand()
  1039. {
  1040. FData = Data;
  1041. FPath = Path;
  1042. FFileName = FileName;
  1043. FFileList = FileList;
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. int __fastcall TFileCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  1047. {
  1048. int Len;
  1049. wchar_t PatternCmd = (Index < Command.Length()) ? Command[Index + 1] : L'\0';
  1050. switch (PatternCmd)
  1051. {
  1052. case L'S':
  1053. case L'@':
  1054. case L'U':
  1055. case L'P':
  1056. case L'/':
  1057. case L'&':
  1058. Len = 2;
  1059. break;
  1060. default:
  1061. Len = 1;
  1062. break;
  1063. }
  1064. return Len;
  1065. }
  1066. //---------------------------------------------------------------------------
  1067. bool __fastcall TFileCustomCommand::PatternReplacement(
  1068. const UnicodeString & Pattern, UnicodeString & Replacement, bool & Delimit)
  1069. {
  1070. // keep consistent with TSessionLog::OpenLogFile
  1071. if (AnsiSameText(Pattern, L"!s"))
  1072. {
  1073. Replacement = FData.SessionData->GenerateSessionUrl(sufComplete);
  1074. }
  1075. else if (Pattern == L"!@")
  1076. {
  1077. Replacement = FData.SessionData->HostNameExpanded;
  1078. }
  1079. else if (AnsiSameText(Pattern, L"!u"))
  1080. {
  1081. Replacement = FData.SessionData->UserName;
  1082. }
  1083. else if (AnsiSameText(Pattern, L"!p"))
  1084. {
  1085. Replacement = FData.SessionData->Password;
  1086. }
  1087. else if (Pattern == L"!/")
  1088. {
  1089. Replacement = UnixIncludeTrailingBackslash(FPath);
  1090. }
  1091. else if (Pattern == L"!&")
  1092. {
  1093. Replacement = FFileList;
  1094. // already delimited
  1095. Delimit = false;
  1096. }
  1097. else
  1098. {
  1099. assert(Pattern.Length() == 1);
  1100. Replacement = FFileName;
  1101. }
  1102. return true;
  1103. }
  1104. //---------------------------------------------------------------------------
  1105. void __fastcall TFileCustomCommand::Validate(const UnicodeString & Command)
  1106. {
  1107. int Found[2] = { 0, 0 };
  1108. CustomValidate(Command, &Found);
  1109. if ((Found[0] > 0) && (Found[1] > 0))
  1110. {
  1111. throw Exception(FMTLOAD(CUSTOM_COMMAND_FILELIST_ERROR,
  1112. (Found[1], Found[0])));
  1113. }
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. void __fastcall TFileCustomCommand::ValidatePattern(const UnicodeString & Command,
  1117. int Index, int /*Len*/, wchar_t PatternCmd, void * Arg)
  1118. {
  1119. int * Found = static_cast<int *>(Arg);
  1120. assert(Index > 0);
  1121. if (PatternCmd == L'&')
  1122. {
  1123. Found[0] = Index;
  1124. }
  1125. else if ((PatternCmd != TEXT_TOKEN) && (PatternLen(Command, Index) == 1))
  1126. {
  1127. Found[1] = Index;
  1128. }
  1129. }
  1130. //---------------------------------------------------------------------------
  1131. bool __fastcall TFileCustomCommand::IsFileListCommand(const UnicodeString & Command)
  1132. {
  1133. return FindPattern(Command, L'&');
  1134. }
  1135. //---------------------------------------------------------------------------
  1136. bool __fastcall TFileCustomCommand::IsFileCommand(const UnicodeString & Command)
  1137. {
  1138. return FindPattern(Command, L'!') || FindPattern(Command, L'&');
  1139. }
  1140. //---------------------------------------------------------------------------
  1141. bool __fastcall TFileCustomCommand::IsSiteCommand(const UnicodeString & Command)
  1142. {
  1143. return FindPattern(Command, L'@');
  1144. }
  1145. //---------------------------------------------------------------------------
  1146. bool __fastcall TFileCustomCommand::IsPasswordCommand(const UnicodeString & Command)
  1147. {
  1148. return FindPattern(Command, L'p');
  1149. }
  1150. //---------------------------------------------------------------------------