CopyParam.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "Exceptions.h"
  6. #include "CopyParam.h"
  7. #include "HierarchicalStorage.h"
  8. #include "TextsCore.h"
  9. #include "Interface.h"
  10. //---------------------------------------------------------------------------
  11. const wchar_t * TransferModeNames[] = { L"binary", L"ascii", L"automatic" };
  12. const int TransferModeNamesCount = LENOF(TransferModeNames);
  13. //---------------------------------------------------------------------------
  14. __fastcall TCopyParamType::TCopyParamType()
  15. {
  16. Default();
  17. }
  18. //---------------------------------------------------------------------------
  19. __fastcall TCopyParamType::TCopyParamType(const TCopyParamType & Source)
  20. {
  21. Assign(&Source);
  22. }
  23. //---------------------------------------------------------------------------
  24. __fastcall TCopyParamType::~TCopyParamType()
  25. {
  26. }
  27. //---------------------------------------------------------------------------
  28. void __fastcall TCopyParamType::Default()
  29. {
  30. // when changing defaults, make sure GetInfoStr() can handle it
  31. FileNameCase = ncNoChange;
  32. PreserveReadOnly = false;
  33. PreserveTime = true;
  34. PreserveTimeDirs = false;
  35. Rights.Number = TRights::rfDefault;
  36. PreserveRights = false; // Was True until #106
  37. IgnorePermErrors = false;
  38. AsciiFileMask.Masks = L"*.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; "
  39. "*.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml";
  40. TransferMode = tmBinary;
  41. AddXToDirectories = true;
  42. ResumeSupport = rsSmart;
  43. ResumeThreshold = 100 * 1024; // (100 KB)
  44. InvalidCharsReplacement = TokenReplacement;
  45. LocalInvalidChars = ::LocalInvalidChars;
  46. CalculateSize = true;
  47. FileMask = L"*.*";
  48. IncludeFileMask.Masks = L"";
  49. TransferSkipList = NULL;
  50. TransferResumeFile = L"";
  51. ClearArchive = false;
  52. RemoveCtrlZ = false;
  53. RemoveBOM = false;
  54. CPSLimit = 0;
  55. NewerOnly = false;
  56. }
  57. //---------------------------------------------------------------------------
  58. UnicodeString __fastcall TCopyParamType::GetInfoStr(
  59. UnicodeString Separator, int Attrs) const
  60. {
  61. UnicodeString Result;
  62. bool SomeAttrIncluded;
  63. UnicodeString ScriptArgs;
  64. bool NoScriptArgs;
  65. UnicodeString AssemblyCode;
  66. bool NoCodeProperties;
  67. DoGetInfoStr(
  68. Separator, Attrs, Result, SomeAttrIncluded,
  69. UnicodeString(), ScriptArgs, NoScriptArgs, TAssemblyLanguage(0), AssemblyCode, NoCodeProperties);
  70. return Result;
  71. }
  72. //---------------------------------------------------------------------------
  73. bool __fastcall TCopyParamType::AnyUsableCopyParam(int Attrs) const
  74. {
  75. UnicodeString Result;
  76. bool SomeAttrIncluded;
  77. UnicodeString ScriptArgs;
  78. bool NoScriptArgs;
  79. UnicodeString AssemblyCode;
  80. bool NoCodeProperties;
  81. DoGetInfoStr(
  82. L";", Attrs, Result, SomeAttrIncluded,
  83. UnicodeString(), ScriptArgs, NoScriptArgs, TAssemblyLanguage(0), AssemblyCode, NoCodeProperties);
  84. return SomeAttrIncluded;
  85. }
  86. //---------------------------------------------------------------------------
  87. UnicodeString __fastcall TCopyParamType::GenerateTransferCommandArgs(int Attrs, const UnicodeString & Link, bool & NoScriptArgs) const
  88. {
  89. UnicodeString Result;
  90. bool SomeAttrIncluded;
  91. UnicodeString ScriptArgs;
  92. UnicodeString AssemblyCode;
  93. bool NoCodeProperties;
  94. DoGetInfoStr(
  95. L";", Attrs, Result, SomeAttrIncluded,
  96. Link, ScriptArgs, NoScriptArgs, TAssemblyLanguage(0), AssemblyCode, NoCodeProperties);
  97. return ScriptArgs;
  98. }
  99. //---------------------------------------------------------------------------
  100. UnicodeString __fastcall TCopyParamType::GenerateAssemblyCode(
  101. TAssemblyLanguage Language, int Attrs, bool & NoCodeProperties) const
  102. {
  103. UnicodeString Result;
  104. bool SomeAttrIncluded;
  105. UnicodeString ScriptArgs;
  106. bool NoScriptArgs;
  107. UnicodeString AssemblyCode;
  108. DoGetInfoStr(L";", Attrs, Result, SomeAttrIncluded, UnicodeString(), ScriptArgs, NoScriptArgs, Language, AssemblyCode, NoCodeProperties);
  109. return AssemblyCode;
  110. }
  111. //---------------------------------------------------------------------------
  112. void __fastcall TCopyParamType::DoGetInfoStr(
  113. UnicodeString Separator, int Options,
  114. UnicodeString & Result, bool & SomeAttrIncluded,
  115. const UnicodeString & Link, UnicodeString & ScriptArgs, bool & NoScriptArgs, TAssemblyLanguage Language, UnicodeString & AssemblyCode,
  116. bool & NoCodeProperties) const
  117. {
  118. TCopyParamType Defaults;
  119. bool SomeAttrExcluded = false;
  120. NoScriptArgs = false;
  121. NoCodeProperties = false;
  122. SomeAttrIncluded = false;
  123. #define ADD(STR, EXCEPT) \
  124. if (FLAGCLEAR(Options, EXCEPT)) \
  125. { \
  126. AddToList(Result, (STR), Separator); \
  127. SomeAttrIncluded = true; \
  128. } \
  129. else \
  130. { \
  131. SomeAttrExcluded = true; \
  132. }
  133. bool AsciiFileMaskDiffers = (TransferMode == tmAutomatic) && !(AsciiFileMask == Defaults.AsciiFileMask);
  134. bool TransferModeDiffers = ((TransferMode != Defaults.TransferMode) || AsciiFileMaskDiffers);
  135. if (FLAGCLEAR(Options, cpaIncludeMaskOnly | cpaNoTransferMode))
  136. {
  137. // Adding Transfer type unconditionally
  138. bool FormatMask;
  139. int Ident;
  140. switch (TransferMode)
  141. {
  142. case tmBinary:
  143. FormatMask = false;
  144. Ident = 2;
  145. break;
  146. case tmAscii:
  147. FormatMask = false;
  148. Ident = 3;
  149. break;
  150. case tmAutomatic:
  151. default:
  152. FormatMask = !(AsciiFileMask == Defaults.AsciiFileMask);
  153. Ident = FormatMask ? 4 : 5;
  154. break;
  155. }
  156. UnicodeString S = FORMAT(LoadStrPart(COPY_INFO_TRANSFER_TYPE2, 1),
  157. (LoadStrPart(COPY_INFO_TRANSFER_TYPE2, Ident)));
  158. if (FormatMask)
  159. {
  160. S = FORMAT(S, (AsciiFileMask.Masks));
  161. }
  162. AddToList(Result, S, Separator);
  163. if (TransferModeDiffers)
  164. {
  165. ADD("", cpaIncludeMaskOnly | cpaNoTransferMode);
  166. ScriptArgs += RtfSwitchValue(TRANSFER_SWITCH, Link, TransferModeNames[TransferMode]);
  167. const wchar_t * TransferModeMembers[] = { L"Binary", L"Ascii", L"Automatic" };
  168. AssemblyCode += AssemblyProperty(
  169. Language, TransferOptionsClassName, L"TransferMode", L"TransferMode", TransferModeMembers[TransferMode], false);
  170. if (AsciiFileMaskDiffers)
  171. {
  172. NoScriptArgs = true;
  173. NoCodeProperties = true;
  174. }
  175. }
  176. }
  177. else
  178. {
  179. if (TransferModeDiffers)
  180. {
  181. SomeAttrExcluded = true;
  182. NoScriptArgs = true;
  183. NoCodeProperties = true;
  184. }
  185. }
  186. if (FileNameCase != Defaults.FileNameCase)
  187. {
  188. ADD(FORMAT(LoadStrPart(COPY_INFO_FILENAME, 1),
  189. (LoadStrPart(COPY_INFO_FILENAME, FileNameCase + 2))),
  190. cpaIncludeMaskOnly);
  191. NoScriptArgs = true;
  192. NoCodeProperties = true;
  193. }
  194. if ((InvalidCharsReplacement == NoReplacement) !=
  195. (Defaults.InvalidCharsReplacement == NoReplacement))
  196. {
  197. DebugAssert(InvalidCharsReplacement == NoReplacement);
  198. if (InvalidCharsReplacement == NoReplacement)
  199. {
  200. ADD(LoadStr(COPY_INFO_DONT_REPLACE_INV_CHARS), cpaIncludeMaskOnly);
  201. }
  202. NoScriptArgs = true;
  203. NoCodeProperties = true;
  204. }
  205. if ((PreserveRights != Defaults.PreserveRights) ||
  206. (PreserveRights &&
  207. ((Rights != Defaults.Rights) || (AddXToDirectories != Defaults.AddXToDirectories))))
  208. {
  209. const int Except = cpaIncludeMaskOnly | cpaNoRights;
  210. if (DebugAlwaysTrue(PreserveRights))
  211. {
  212. UnicodeString RightsStr = Rights.Text;
  213. if (AddXToDirectories)
  214. {
  215. RightsStr += L", " + LoadStr(COPY_INFO_ADD_X_TO_DIRS);
  216. }
  217. ADD(FORMAT(LoadStr(COPY_INFO_PERMISSIONS), (RightsStr)),
  218. Except);
  219. if (FLAGCLEAR(Options, Except))
  220. {
  221. ScriptArgs += RtfSwitchValue(PERMISSIONS_SWITCH, Link, Rights.Octal);
  222. const UnicodeString FilePermissionsClassName = L"FilePermissions";
  223. const bool Inline = true;
  224. UnicodeString FilePermissions =
  225. AssemblyNewClassInstanceStart(Language, FilePermissionsClassName, Inline) +
  226. AssemblyProperty(Language, FilePermissionsClassName, L"Octal", Rights.Octal, Inline) +
  227. AssemblyNewClassInstanceEnd(Language, Inline);
  228. AssemblyCode += AssemblyPropertyRaw(Language, TransferOptionsClassName, L"FilePermissions", FilePermissions, false);
  229. }
  230. }
  231. if ((AddXToDirectories != Defaults.AddXToDirectories) && FLAGCLEAR(Options, Except))
  232. {
  233. NoScriptArgs = true;
  234. NoCodeProperties = true;
  235. }
  236. }
  237. bool AddPreserveTime = (PreserveTime != Defaults.PreserveTime);
  238. bool APreserveTimeDirs = PreserveTime && PreserveTimeDirs;
  239. if (AddPreserveTime || (APreserveTimeDirs != Defaults.PreserveTimeDirs))
  240. {
  241. UnicodeString Str = LoadStr(PreserveTime ? COPY_INFO_TIMESTAMP : COPY_INFO_DONT_PRESERVE_TIME);
  242. const int Except = cpaIncludeMaskOnly | cpaNoPreserveTime;
  243. const int ExceptDirs = cpaNoPreserveTimeDirs;
  244. if (APreserveTimeDirs != Defaults.PreserveTimeDirs)
  245. {
  246. if (DebugAlwaysTrue(PreserveTimeDirs))
  247. {
  248. if (FLAGCLEAR(Options, ExceptDirs))
  249. {
  250. Str = FMTLOAD(COPY_INFO_PRESERVE_TIME_DIRS, (Str));
  251. AddPreserveTime = true;
  252. }
  253. }
  254. ADD("", Except | ExceptDirs);
  255. }
  256. if (AddPreserveTime)
  257. {
  258. ADD(Str, Except);
  259. }
  260. if (FLAGCLEAR(Options, Except))
  261. {
  262. if (PreserveTime)
  263. {
  264. if (PreserveTimeDirs && FLAGCLEAR(Options, ExceptDirs))
  265. {
  266. ScriptArgs += RtfSwitchValue(PRESERVETIME_SWITCH, Link, PRESERVETIMEDIRS_SWITCH_VALUE);
  267. NoCodeProperties = true;
  268. }
  269. else
  270. {
  271. DebugFail(); // should never get here
  272. ScriptArgs += RtfSwitch(PRESERVETIME_SWITCH, Link);
  273. }
  274. }
  275. else
  276. {
  277. ScriptArgs += RtfSwitch(NOPRESERVETIME_SWITCH, Link);
  278. AssemblyCode += AssemblyProperty(Language, TransferOptionsClassName, L"PreserveTimestamp", false, false);
  279. }
  280. }
  281. }
  282. if ((PreserveRights || PreserveTime) &&
  283. (IgnorePermErrors != Defaults.IgnorePermErrors))
  284. {
  285. if (DebugAlwaysTrue(IgnorePermErrors))
  286. {
  287. const int Except = cpaIncludeMaskOnly | cpaNoIgnorePermErrors;
  288. ADD(LoadStr(COPY_INFO_IGNORE_PERM_ERRORS), Except);
  289. if (FLAGCLEAR(Options, Except))
  290. {
  291. NoScriptArgs = true;
  292. NoCodeProperties = true;
  293. }
  294. }
  295. }
  296. if (PreserveReadOnly != Defaults.PreserveReadOnly)
  297. {
  298. if (DebugAlwaysTrue(PreserveReadOnly))
  299. {
  300. const int Except = cpaIncludeMaskOnly | cpaNoPreserveReadOnly;
  301. ADD(LoadStr(COPY_INFO_PRESERVE_READONLY), Except);
  302. if (FLAGCLEAR(Options, Except))
  303. {
  304. NoScriptArgs = true;
  305. NoCodeProperties = true;
  306. }
  307. }
  308. }
  309. if (CalculateSize != Defaults.CalculateSize)
  310. {
  311. if (DebugAlwaysTrue(!CalculateSize))
  312. {
  313. ADD(LoadStr(COPY_INFO_DONT_CALCULATE_SIZE), cpaIncludeMaskOnly);
  314. // Always false in scripting, in assembly controlled by use of FileTransferProgress
  315. }
  316. }
  317. if (ClearArchive != Defaults.ClearArchive)
  318. {
  319. if (DebugAlwaysTrue(ClearArchive))
  320. {
  321. const int Except = cpaIncludeMaskOnly | cpaNoClearArchive;
  322. ADD(LoadStr(COPY_INFO_CLEAR_ARCHIVE), Except);
  323. if (FLAGCLEAR(Options, Except))
  324. {
  325. NoScriptArgs = true;
  326. NoCodeProperties = true;
  327. }
  328. }
  329. }
  330. if ((TransferMode == tmAscii) || (TransferMode == tmAutomatic))
  331. {
  332. if (RemoveBOM != Defaults.RemoveBOM)
  333. {
  334. if (DebugAlwaysTrue(RemoveBOM))
  335. {
  336. const int Except = cpaIncludeMaskOnly | cpaNoRemoveBOM | cpaNoTransferMode;
  337. ADD(LoadStr(COPY_INFO_REMOVE_BOM), Except);
  338. if (FLAGCLEAR(Options, Except))
  339. {
  340. NoScriptArgs = true;
  341. NoCodeProperties = true;
  342. }
  343. }
  344. }
  345. if (RemoveCtrlZ != Defaults.RemoveCtrlZ)
  346. {
  347. if (DebugAlwaysTrue(RemoveCtrlZ))
  348. {
  349. const int Except = cpaIncludeMaskOnly | cpaNoRemoveCtrlZ | cpaNoTransferMode;
  350. ADD(LoadStr(COPY_INFO_REMOVE_CTRLZ),Except);
  351. if (FLAGCLEAR(Options, Except))
  352. {
  353. NoScriptArgs = true;
  354. NoCodeProperties = true;
  355. }
  356. }
  357. }
  358. }
  359. if (!(IncludeFileMask == Defaults.IncludeFileMask))
  360. {
  361. ADD(FORMAT(LoadStr(COPY_INFO_FILE_MASK), (IncludeFileMask.Masks)),
  362. cpaNoIncludeMask);
  363. ScriptArgs += RtfSwitch(FILEMASK_SWITCH, Link, IncludeFileMask.Masks);
  364. AssemblyCode += AssemblyProperty(Language, TransferOptionsClassName, L"FileMask", IncludeFileMask.Masks, false);
  365. }
  366. DebugAssert(FTransferSkipList.get() == NULL);
  367. DebugAssert(FTransferResumeFile.IsEmpty());
  368. if (CPSLimit > 0)
  369. {
  370. int LimitKB = int(CPSLimit / 1024);
  371. ADD(FMTLOAD(COPY_INFO_CPS_LIMIT2, (LimitKB)), cpaIncludeMaskOnly);
  372. ScriptArgs += RtfSwitch(SPEED_SWITCH, Link, LimitKB);
  373. AssemblyCode += AssemblyProperty(Language, TransferOptionsClassName, L"Speed", LimitKB, false);
  374. }
  375. if (NewerOnly != Defaults.NewerOnly)
  376. {
  377. if (DebugAlwaysTrue(NewerOnly))
  378. {
  379. const int Except = cpaIncludeMaskOnly | cpaNoNewerOnly;
  380. ADD(StripHotkey(LoadStr(COPY_PARAM_NEWER_ONLY)), Except);
  381. if (FLAGCLEAR(Options, Except))
  382. {
  383. ScriptArgs += RtfSwitch(NEWERONLY_SWICH, Link);
  384. NoCodeProperties = true;
  385. }
  386. }
  387. }
  388. bool ResumeThresholdDiffers = ((ResumeSupport == rsSmart) && (ResumeThreshold != Defaults.ResumeThreshold));
  389. if (((ResumeSupport != Defaults.ResumeSupport) || ResumeThresholdDiffers) &&
  390. (TransferMode != tmAscii) && FLAGCLEAR(Options, cpaNoResumeSupport))
  391. {
  392. UnicodeString Value;
  393. UnicodeString CodeState;
  394. int ResumeThresholdKB = (ResumeThreshold / 1024);
  395. switch (ResumeSupport)
  396. {
  397. case rsOff:
  398. Value = ToggleNames[ToggleOff];
  399. CodeState = L"Off";
  400. break;
  401. case rsOn:
  402. Value = ToggleNames[ToggleOn];
  403. CodeState = L"On";
  404. break;
  405. case rsSmart:
  406. Value = IntToStr(ResumeThresholdKB);
  407. break;
  408. }
  409. ScriptArgs += RtfSwitchValue(RESUMESUPPORT_SWITCH, Link, Value);
  410. const UnicodeString ResumeSupportClassName = L"TransferResumeSupport";
  411. const bool Inline = true;
  412. UnicodeString ResumeSupportCode =
  413. AssemblyNewClassInstanceStart(Language, ResumeSupportClassName, Inline);
  414. if (ResumeSupport == rsSmart)
  415. {
  416. ResumeSupportCode += AssemblyProperty(Language, ResumeSupportClassName, L"Threshold", ResumeThresholdKB, Inline);
  417. }
  418. else
  419. {
  420. ResumeSupportCode += AssemblyProperty(Language, ResumeSupportClassName, L"State", L"TransferResumeSupportState", CodeState, Inline);
  421. }
  422. ResumeSupportCode += AssemblyNewClassInstanceEnd(Language, Inline);
  423. AssemblyCode += AssemblyPropertyRaw(Language, TransferOptionsClassName, L"ResumeSupport", ResumeSupportCode, false);
  424. }
  425. if (SomeAttrExcluded)
  426. {
  427. Result += (Result.IsEmpty() ? UnicodeString() : Separator) +
  428. FORMAT(LoadStrPart(COPY_INFO_NOT_USABLE, 1),
  429. (LoadStrPart(COPY_INFO_NOT_USABLE, (SomeAttrIncluded ? 2 : 3))));
  430. }
  431. else if (Result.IsEmpty())
  432. {
  433. Result = LoadStr(COPY_INFO_DEFAULT);
  434. }
  435. #undef ADD
  436. }
  437. //---------------------------------------------------------------------------
  438. void __fastcall TCopyParamType::Assign(const TCopyParamType * Source)
  439. {
  440. DebugAssert(Source != NULL);
  441. #define COPY(Prop) Prop = Source->Prop
  442. COPY(FileNameCase);
  443. COPY(PreserveReadOnly);
  444. COPY(PreserveTime);
  445. COPY(PreserveTimeDirs);
  446. COPY(Rights);
  447. COPY(AsciiFileMask);
  448. COPY(TransferMode);
  449. COPY(AddXToDirectories);
  450. COPY(PreserveRights);
  451. COPY(IgnorePermErrors);
  452. COPY(ResumeSupport);
  453. COPY(ResumeThreshold);
  454. COPY(InvalidCharsReplacement);
  455. COPY(LocalInvalidChars);
  456. COPY(CalculateSize);
  457. COPY(FileMask);
  458. COPY(IncludeFileMask);
  459. COPY(TransferSkipList);
  460. COPY(TransferResumeFile);
  461. COPY(ClearArchive);
  462. COPY(RemoveCtrlZ);
  463. COPY(RemoveBOM);
  464. COPY(CPSLimit);
  465. COPY(NewerOnly);
  466. #undef COPY
  467. }
  468. //---------------------------------------------------------------------------
  469. TCopyParamType & __fastcall TCopyParamType::operator =(const TCopyParamType & rhp)
  470. {
  471. Assign(&rhp);
  472. return *this;
  473. }
  474. //---------------------------------------------------------------------------
  475. void __fastcall TCopyParamType::SetLocalInvalidChars(UnicodeString value)
  476. {
  477. if (value != LocalInvalidChars)
  478. {
  479. FLocalInvalidChars = value;
  480. FTokenizibleChars = FLocalInvalidChars + TokenPrefix;
  481. }
  482. }
  483. //---------------------------------------------------------------------------
  484. bool __fastcall TCopyParamType::GetReplaceInvalidChars() const
  485. {
  486. return (InvalidCharsReplacement != NoReplacement);
  487. }
  488. //---------------------------------------------------------------------------
  489. void __fastcall TCopyParamType::SetReplaceInvalidChars(bool value)
  490. {
  491. if (ReplaceInvalidChars != value)
  492. {
  493. InvalidCharsReplacement = (value ? TokenReplacement : NoReplacement);
  494. }
  495. }
  496. //---------------------------------------------------------------------------
  497. UnicodeString __fastcall TCopyParamType::ValidLocalFileName(UnicodeString FileName) const
  498. {
  499. return ::ValidLocalFileName(FileName, InvalidCharsReplacement, FTokenizibleChars, LocalInvalidChars);
  500. }
  501. //---------------------------------------------------------------------------
  502. UnicodeString __fastcall TCopyParamType::RestoreChars(UnicodeString FileName) const
  503. {
  504. if (InvalidCharsReplacement == TokenReplacement)
  505. {
  506. wchar_t * InvalidChar = FileName.c_str();
  507. while ((InvalidChar = wcschr(InvalidChar, TokenPrefix)) != NULL)
  508. {
  509. int Index = InvalidChar - FileName.c_str() + 1;
  510. if (FileName.Length() >= Index + 2)
  511. {
  512. UnicodeString Hex = FileName.SubString(Index + 1, 2);
  513. wchar_t Char = static_cast<wchar_t>(HexToByte(Hex));
  514. if ((Char != L'\0') &&
  515. ((FTokenizibleChars.Pos(Char) > 0) ||
  516. (((Char == L' ') || (Char == L'.')) && (Index == FileName.Length() - 2))))
  517. {
  518. FileName[Index] = Char;
  519. FileName.Delete(Index + 1, 2);
  520. InvalidChar = FileName.c_str() + Index;
  521. }
  522. else if ((Hex == L"00") &&
  523. ((Index == FileName.Length() - 2) || (FileName[Index + 3] == L'.')) &&
  524. IsReservedName(FileName.SubString(1, Index - 1) + FileName.SubString(Index + 3, FileName.Length() - Index - 3 + 1)))
  525. {
  526. FileName.Delete(Index, 3);
  527. InvalidChar = FileName.c_str() + Index - 1;
  528. }
  529. else
  530. {
  531. InvalidChar++;
  532. }
  533. }
  534. else
  535. {
  536. InvalidChar++;
  537. }
  538. }
  539. }
  540. return FileName;
  541. }
  542. //---------------------------------------------------------------------------
  543. UnicodeString __fastcall TCopyParamType::ValidLocalPath(UnicodeString Path) const
  544. {
  545. UnicodeString Result;
  546. while (!Path.IsEmpty())
  547. {
  548. if (!Result.IsEmpty())
  549. {
  550. Result += L"\\";
  551. }
  552. Result += ValidLocalFileName(CutToChar(Path, L'\\', false));
  553. }
  554. return Result;
  555. }
  556. //---------------------------------------------------------------------------
  557. UnicodeString __fastcall TCopyParamType::ChangeFileName(UnicodeString FileName,
  558. TOperationSide Side, bool FirstLevel) const
  559. {
  560. if (FirstLevel)
  561. {
  562. FileName = MaskFileName(FileName, FileMask);
  563. }
  564. switch (FileNameCase) {
  565. case ncUpperCase: FileName = FileName.UpperCase(); break;
  566. case ncLowerCase: FileName = FileName.LowerCase(); break;
  567. case ncFirstUpperCase: FileName = FileName.SubString(1, 1).UpperCase() +
  568. FileName.SubString(2, FileName.Length()-1).LowerCase(); break;
  569. case ncLowerCaseShort:
  570. if ((FileName.Length() <= 12) && (FileName.Pos(L".") <= 9) &&
  571. (FileName == FileName.UpperCase()))
  572. {
  573. FileName = FileName.LowerCase();
  574. }
  575. break;
  576. case ncNoChange:
  577. default:
  578. /*nothing*/
  579. break;
  580. }
  581. if (Side == osRemote)
  582. {
  583. FileName = ValidLocalFileName(FileName);
  584. }
  585. else
  586. {
  587. FileName = RestoreChars(FileName);
  588. }
  589. return FileName;
  590. }
  591. //---------------------------------------------------------------------------
  592. bool __fastcall TCopyParamType::UseAsciiTransfer(UnicodeString FileName,
  593. TOperationSide Side, const TFileMasks::TParams & Params) const
  594. {
  595. switch (TransferMode)
  596. {
  597. case tmBinary: return false;
  598. case tmAscii: return true;
  599. case tmAutomatic: return AsciiFileMask.Matches(FileName, (Side == osLocal),
  600. false, &Params);
  601. default: DebugFail(); return false;
  602. }
  603. }
  604. //---------------------------------------------------------------------------
  605. TRights __fastcall TCopyParamType::RemoteFileRights(Integer Attrs) const
  606. {
  607. TRights R = Rights;
  608. if ((Attrs & faDirectory) && AddXToDirectories)
  609. R.AddExecute();
  610. return R;
  611. }
  612. //---------------------------------------------------------------------------
  613. UnicodeString __fastcall TCopyParamType::GetLogStr() const
  614. {
  615. wchar_t CaseC[] = L"NULFS";
  616. wchar_t ModeC[] = L"BAM";
  617. wchar_t ResumeC[] = L"YSN";
  618. // OpenArray (ARRAYOFCONST) supports only up to 19 arguments, so we had to split it
  619. return
  620. FORMAT(
  621. L" PrTime: %s%s; PrRO: %s; Rght: %s; PrR: %s (%s); FnCs: %s; RIC: %s; "
  622. "Resume: %s (%d); CalcS: %s; Mask: %s\n",
  623. (BooleanToEngStr(PreserveTime),
  624. UnicodeString(PreserveTime && PreserveTimeDirs ? L"+Dirs" : L""),
  625. BooleanToEngStr(PreserveReadOnly),
  626. Rights.Text,
  627. BooleanToEngStr(PreserveRights),
  628. BooleanToEngStr(IgnorePermErrors),
  629. CaseC[FileNameCase],
  630. CharToHex(InvalidCharsReplacement),
  631. ResumeC[ResumeSupport],
  632. (int)ResumeThreshold,
  633. BooleanToEngStr(CalculateSize),
  634. FileMask)) +
  635. FORMAT(
  636. L" TM: %s; ClAr: %s; RemEOF: %s; RemBOM: %s; CPS: %u; NewerOnly: %s; InclM: %s; ResumeL: %d\n"
  637. " AscM: %s\n",
  638. (ModeC[TransferMode],
  639. BooleanToEngStr(ClearArchive),
  640. BooleanToEngStr(RemoveCtrlZ),
  641. BooleanToEngStr(RemoveBOM),
  642. int(CPSLimit),
  643. BooleanToEngStr(NewerOnly),
  644. IncludeFileMask.Masks,
  645. ((FTransferSkipList.get() != NULL) ? FTransferSkipList->Count : 0) + (!FTransferResumeFile.IsEmpty() ? 1 : 0),
  646. AsciiFileMask.Masks));
  647. }
  648. //---------------------------------------------------------------------------
  649. int __fastcall TCopyParamType::LocalFileAttrs(const TRights & Rights) const
  650. {
  651. int Result = 0;
  652. if (PreserveReadOnly && !Rights.Right[TRights::rrUserWrite])
  653. {
  654. Result |= faReadOnly;
  655. }
  656. return Result;
  657. }
  658. //---------------------------------------------------------------------------
  659. bool __fastcall TCopyParamType::AllowResume(__int64 Size) const
  660. {
  661. switch (ResumeSupport)
  662. {
  663. case rsOn: return true;
  664. case rsOff: return false;
  665. case rsSmart: return (Size >= ResumeThreshold);
  666. default: DebugFail(); return false;
  667. }
  668. }
  669. //---------------------------------------------------------------------------
  670. bool __fastcall TCopyParamType::AllowAnyTransfer() const
  671. {
  672. return
  673. IncludeFileMask.Masks.IsEmpty() &&
  674. ((FTransferSkipList.get() == NULL) || (FTransferSkipList->Count == 0)) &&
  675. FTransferResumeFile.IsEmpty();
  676. }
  677. //---------------------------------------------------------------------------
  678. bool __fastcall TCopyParamType::AllowTransfer(UnicodeString FileName,
  679. TOperationSide Side, bool Directory, const TFileMasks::TParams & Params) const
  680. {
  681. bool Result = true;
  682. if (!IncludeFileMask.Masks.IsEmpty())
  683. {
  684. Result = IncludeFileMask.Matches(FileName, (Side == osLocal),
  685. Directory, &Params);
  686. }
  687. return Result;
  688. }
  689. //---------------------------------------------------------------------------
  690. bool __fastcall TCopyParamType::SkipTransfer(
  691. UnicodeString FileName, bool Directory) const
  692. {
  693. bool Result = false;
  694. // we deliberatelly do not filter directories, as path is added to resume list
  695. // when a transfer of file or directory is started,
  696. // so for directories we need to recurse and check every single file
  697. if (!Directory && (FTransferSkipList.get() != NULL))
  698. {
  699. Result = (FTransferSkipList->IndexOf(FileName) >= 0);
  700. }
  701. return Result;
  702. }
  703. //---------------------------------------------------------------------------
  704. bool __fastcall TCopyParamType::ResumeTransfer(UnicodeString FileName) const
  705. {
  706. // Returning true has the same effect as cpResume
  707. return
  708. (FileName == FTransferResumeFile) &&
  709. DebugAlwaysTrue(!FTransferResumeFile.IsEmpty());
  710. }
  711. //---------------------------------------------------------------------------
  712. TStrings * __fastcall TCopyParamType::GetTransferSkipList() const
  713. {
  714. return FTransferSkipList.get();
  715. }
  716. //---------------------------------------------------------------------------
  717. void __fastcall TCopyParamType::SetTransferSkipList(TStrings * value)
  718. {
  719. if ((value == NULL) || (value->Count == 0))
  720. {
  721. FTransferSkipList.reset(NULL);
  722. }
  723. else
  724. {
  725. FTransferSkipList.reset(new TStringList());
  726. FTransferSkipList->AddStrings(value);
  727. FTransferSkipList->Sorted = true;
  728. }
  729. }
  730. //---------------------------------------------------------------------------
  731. void __fastcall TCopyParamType::Load(THierarchicalStorage * Storage)
  732. {
  733. AddXToDirectories = Storage->ReadBool(L"AddXToDirectories", AddXToDirectories);
  734. AsciiFileMask.Masks = Storage->ReadString(L"Masks", AsciiFileMask.Masks);
  735. FileNameCase = (TFileNameCase)Storage->ReadInteger(L"FileNameCase", FileNameCase);
  736. PreserveReadOnly = Storage->ReadBool(L"PreserveReadOnly", PreserveReadOnly);
  737. PreserveTime = Storage->ReadBool(L"PreserveTime", PreserveTime);
  738. PreserveTimeDirs = Storage->ReadBool(L"PreserveTimeDirs", PreserveTimeDirs);
  739. PreserveRights = Storage->ReadBool(L"PreserveRights", PreserveRights);
  740. IgnorePermErrors = Storage->ReadBool(L"IgnorePermErrors", IgnorePermErrors);
  741. Rights.Text = Storage->ReadString(L"Text", Rights.Text);
  742. TransferMode = (TTransferMode)Storage->ReadInteger(L"TransferMode", TransferMode);
  743. ResumeSupport = (TResumeSupport)Storage->ReadInteger(L"ResumeSupport", ResumeSupport);
  744. ResumeThreshold = Storage->ReadInt64(L"ResumeThreshold", ResumeThreshold);
  745. InvalidCharsReplacement = (wchar_t)Storage->ReadInteger(L"ReplaceInvalidChars",
  746. (unsigned int)InvalidCharsReplacement);
  747. LocalInvalidChars = Storage->ReadString(L"LocalInvalidChars", LocalInvalidChars);
  748. CalculateSize = Storage->ReadBool(L"CalculateSize", CalculateSize);
  749. if (Storage->ValueExists(L"IncludeFileMask"))
  750. {
  751. IncludeFileMask.Masks = Storage->ReadString(L"IncludeFileMask", IncludeFileMask.Masks);
  752. }
  753. else if (Storage->ValueExists(L"ExcludeFileMask"))
  754. {
  755. UnicodeString ExcludeFileMask = Storage->ReadString(L"ExcludeFileMask", L"");
  756. if (!ExcludeFileMask.IsEmpty())
  757. {
  758. bool NegativeExclude = Storage->ReadBool(L"NegativeExclude", false);
  759. if (NegativeExclude)
  760. {
  761. IncludeFileMask.Masks = ExcludeFileMask;
  762. }
  763. // convert at least simple cases to new format
  764. else if (ExcludeFileMask.Pos(IncludeExcludeFileMasksDelimiter) == 0)
  765. {
  766. IncludeFileMask.Masks = UnicodeString(IncludeExcludeFileMasksDelimiter) + ExcludeFileMask;
  767. }
  768. }
  769. }
  770. TransferSkipList = NULL;
  771. TransferResumeFile = L"";
  772. ClearArchive = Storage->ReadBool(L"ClearArchive", ClearArchive);
  773. RemoveCtrlZ = Storage->ReadBool(L"RemoveCtrlZ", RemoveCtrlZ);
  774. RemoveBOM = Storage->ReadBool(L"RemoveBOM", RemoveBOM);
  775. CPSLimit = Storage->ReadInteger(L"CPSLimit", CPSLimit);
  776. NewerOnly = Storage->ReadBool(L"NewerOnly", NewerOnly);
  777. }
  778. //---------------------------------------------------------------------------
  779. void __fastcall TCopyParamType::Save(THierarchicalStorage * Storage) const
  780. {
  781. Storage->WriteBool(L"AddXToDirectories", AddXToDirectories);
  782. Storage->WriteString(L"Masks", AsciiFileMask.Masks);
  783. Storage->WriteInteger(L"FileNameCase", FileNameCase);
  784. Storage->WriteBool(L"PreserveReadOnly", PreserveReadOnly);
  785. Storage->WriteBool(L"PreserveTime", PreserveTime);
  786. Storage->WriteBool(L"PreserveTimeDirs", PreserveTimeDirs);
  787. Storage->WriteBool(L"PreserveRights", PreserveRights);
  788. Storage->WriteBool(L"IgnorePermErrors", IgnorePermErrors);
  789. Storage->WriteString(L"Text", Rights.Text);
  790. Storage->WriteInteger(L"TransferMode", TransferMode);
  791. Storage->WriteInteger(L"ResumeSupport", ResumeSupport);
  792. Storage->WriteInt64(L"ResumeThreshold", ResumeThreshold);
  793. Storage->WriteInteger(L"ReplaceInvalidChars", (unsigned int)InvalidCharsReplacement);
  794. Storage->WriteString(L"LocalInvalidChars", LocalInvalidChars);
  795. Storage->WriteBool(L"CalculateSize", CalculateSize);
  796. Storage->WriteString(L"IncludeFileMask", IncludeFileMask.Masks);
  797. Storage->DeleteValue(L"ExcludeFileMask"); // obsolete
  798. Storage->DeleteValue(L"NegativeExclude"); // obsolete
  799. DebugAssert(FTransferSkipList.get() == NULL);
  800. DebugAssert(FTransferResumeFile.IsEmpty());
  801. Storage->WriteBool(L"ClearArchive", ClearArchive);
  802. Storage->WriteBool(L"RemoveCtrlZ", RemoveCtrlZ);
  803. Storage->WriteBool(L"RemoveBOM", RemoveBOM);
  804. Storage->WriteInteger(L"CPSLimit", CPSLimit);
  805. Storage->WriteBool(L"NewerOnly", NewerOnly);
  806. }
  807. //---------------------------------------------------------------------------
  808. #define C(Property) (Property == rhp.Property)
  809. bool __fastcall TCopyParamType::operator==(const TCopyParamType & rhp) const
  810. {
  811. DebugAssert(FTransferSkipList.get() == NULL);
  812. DebugAssert(FTransferResumeFile.IsEmpty());
  813. DebugAssert(rhp.FTransferSkipList.get() == NULL);
  814. DebugAssert(rhp.FTransferResumeFile.IsEmpty());
  815. return
  816. C(AddXToDirectories) &&
  817. C(AsciiFileMask) &&
  818. C(FileNameCase) &&
  819. C(PreserveReadOnly) &&
  820. C(PreserveTime) &&
  821. C(PreserveTimeDirs) &&
  822. C(PreserveRights) &&
  823. C(IgnorePermErrors) &&
  824. C(Rights) &&
  825. C(TransferMode) &&
  826. C(ResumeSupport) &&
  827. C(ResumeThreshold) &&
  828. C(InvalidCharsReplacement) &&
  829. C(LocalInvalidChars) &&
  830. C(CalculateSize) &&
  831. C(IncludeFileMask) &&
  832. C(ClearArchive) &&
  833. C(RemoveCtrlZ) &&
  834. C(RemoveBOM) &&
  835. C(CPSLimit) &&
  836. C(NewerOnly) &&
  837. true;
  838. }
  839. #undef C
  840. //---------------------------------------------------------------------------
  841. static bool __fastcall TryGetSpeedLimit(const UnicodeString & Text, unsigned long & Speed)
  842. {
  843. bool Result;
  844. if (AnsiSameText(Text, LoadStr(SPEED_UNLIMITED)))
  845. {
  846. Speed = 0;
  847. Result = true;
  848. }
  849. else
  850. {
  851. int SSpeed;
  852. Result = TryStrToInt(Text, SSpeed) && (SSpeed >= 0);
  853. if (Result)
  854. {
  855. Speed = SSpeed * 1024;
  856. }
  857. }
  858. return Result;
  859. }
  860. //---------------------------------------------------------------------------
  861. unsigned long __fastcall GetSpeedLimit(const UnicodeString & Text)
  862. {
  863. unsigned long Speed;
  864. if (!TryGetSpeedLimit(Text, Speed))
  865. {
  866. throw Exception(FMTLOAD(SPEED_INVALID, (Text)));
  867. }
  868. return Speed;
  869. }
  870. //---------------------------------------------------------------------------
  871. UnicodeString __fastcall SetSpeedLimit(unsigned long Limit)
  872. {
  873. UnicodeString Text;
  874. if (Limit == 0)
  875. {
  876. Text = LoadStr(SPEED_UNLIMITED);
  877. }
  878. else
  879. {
  880. Text = IntToStr(int(Limit / 1024));
  881. }
  882. return Text;
  883. }
  884. //---------------------------------------------------------------------------
  885. void __fastcall CopySpeedLimits(TStrings * Source, TStrings * Dest)
  886. {
  887. std::unique_ptr<TStringList> Temp(new TStringList());
  888. bool Unlimited = false;
  889. for (int Index = 0; Index < Source->Count; Index++)
  890. {
  891. UnicodeString Text = Source->Strings[Index];
  892. unsigned long Speed;
  893. bool Valid = TryGetSpeedLimit(Text, Speed);
  894. if ((!Valid || (Speed == 0)) && !Unlimited)
  895. {
  896. Temp->Add(LoadStr(SPEED_UNLIMITED));
  897. Unlimited = true;
  898. }
  899. else if (Valid && (Speed > 0))
  900. {
  901. Temp->Add(Text);
  902. }
  903. }
  904. if (!Unlimited)
  905. {
  906. Temp->Insert(0, LoadStr(SPEED_UNLIMITED));
  907. }
  908. Dest->Assign(Temp.get());
  909. }