CopyParam.cpp 28 KB

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