RemoteFiles.cpp 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "RemoteFiles.h"
  6. #include <SysUtils.hpp>
  7. #include <StrUtils.hpp>
  8. #include <DateUtils.hpp>
  9. #include "Exceptions.h"
  10. #include "Interface.h"
  11. #include "Terminal.h"
  12. #include "TextsCore.h"
  13. #include "HelpCore.h"
  14. #include "Cryptography.h"
  15. /* TODO 1 : Path class instead of UnicodeString (handle relativity...) */
  16. //---------------------------------------------------------------------------
  17. const UnicodeString PartialExt(L".filepart");
  18. //---------------------------------------------------------------------------
  19. bool __fastcall IsUnixStyleWindowsPath(const UnicodeString & Path)
  20. {
  21. return (Path.Length() >= 3) && IsLetter(Path[1]) && (Path[2] == L':') && (Path[3] == L'/');
  22. }
  23. //---------------------------------------------------------------------------
  24. bool __fastcall UnixIsAbsolutePath(const UnicodeString & Path)
  25. {
  26. return
  27. ((Path.Length() >= 1) && (Path[1] == L'/')) ||
  28. // we need this for FTP only, but this is unfortunately used in a static context
  29. IsUnixStyleWindowsPath(Path);
  30. }
  31. //---------------------------------------------------------------------------
  32. UnicodeString __fastcall UnixIncludeTrailingBackslash(const UnicodeString & Path)
  33. {
  34. // it used to return "/" when input path was empty
  35. if (!Path.IsEmpty() && !Path.IsDelimiter(L"/", Path.Length()))
  36. {
  37. return Path + L"/";
  38. }
  39. else
  40. {
  41. return Path;
  42. }
  43. }
  44. //---------------------------------------------------------------------------
  45. // Keeps "/" for root path
  46. UnicodeString __fastcall UnixExcludeTrailingBackslash(const UnicodeString & Path, bool Simple)
  47. {
  48. if (Path.IsEmpty() ||
  49. (Path == L"/") ||
  50. !Path.IsDelimiter(L"/", Path.Length()) ||
  51. (!Simple && ((Path.Length() == 3) && IsUnixStyleWindowsPath(Path))))
  52. {
  53. return Path;
  54. }
  55. else
  56. {
  57. return Path.SubString(1, Path.Length() - 1);
  58. }
  59. }
  60. //---------------------------------------------------------------------------
  61. UnicodeString __fastcall SimpleUnixExcludeTrailingBackslash(const UnicodeString & Path)
  62. {
  63. return UnixExcludeTrailingBackslash(Path, true);
  64. }
  65. //---------------------------------------------------------------------------
  66. UnicodeString __fastcall UnixCombinePaths(const UnicodeString & Path1, const UnicodeString & Path2)
  67. {
  68. return UnixIncludeTrailingBackslash(Path1) + Path2;
  69. }
  70. //---------------------------------------------------------------------------
  71. Boolean __fastcall UnixSamePath(const UnicodeString & Path1, const UnicodeString & Path2)
  72. {
  73. return (UnixIncludeTrailingBackslash(Path1) == UnixIncludeTrailingBackslash(Path2));
  74. }
  75. //---------------------------------------------------------------------------
  76. bool __fastcall UnixIsChildPath(const UnicodeString & AParent, const UnicodeString & AChild)
  77. {
  78. UnicodeString Parent = UnixIncludeTrailingBackslash(AParent);
  79. UnicodeString Child = UnixIncludeTrailingBackslash(AChild);
  80. return (Child.SubString(1, Parent.Length()) == Parent);
  81. }
  82. //---------------------------------------------------------------------------
  83. UnicodeString __fastcall UnixExtractFileDir(const UnicodeString & Path)
  84. {
  85. int Pos = Path.LastDelimiter(L'/');
  86. // it used to return Path when no slash was found
  87. if (Pos > 1)
  88. {
  89. return Path.SubString(1, Pos - 1);
  90. }
  91. else
  92. {
  93. return (Pos == 1) ? UnicodeString(ROOTDIRECTORY) : UnicodeString();
  94. }
  95. }
  96. //---------------------------------------------------------------------------
  97. // must return trailing backslash
  98. UnicodeString __fastcall UnixExtractFilePath(const UnicodeString & Path)
  99. {
  100. int Pos = Path.LastDelimiter(L'/');
  101. // it used to return Path when no slash was found
  102. if (Pos > 0)
  103. {
  104. return Path.SubString(1, Pos);
  105. }
  106. else
  107. {
  108. return UnicodeString();
  109. }
  110. }
  111. //---------------------------------------------------------------------------
  112. UnicodeString __fastcall UnixExtractFileName(const UnicodeString & Path)
  113. {
  114. int Pos = Path.LastDelimiter(L'/');
  115. UnicodeString Result;
  116. if (Pos > 0)
  117. {
  118. Result = Path.SubString(Pos + 1, Path.Length() - Pos);
  119. }
  120. else
  121. {
  122. Result = Path;
  123. }
  124. return Result;
  125. }
  126. //---------------------------------------------------------------------------
  127. UnicodeString __fastcall UnixExtractFileExt(const UnicodeString & Path)
  128. {
  129. UnicodeString FileName = UnixExtractFileName(Path);
  130. int Pos = FileName.LastDelimiter(L".");
  131. return (Pos > 0) ? Path.SubString(Pos, Path.Length() - Pos + 1) : UnicodeString();
  132. }
  133. //---------------------------------------------------------------------------
  134. UnicodeString __fastcall ExtractFileName(const UnicodeString & Path, bool Unix)
  135. {
  136. if (Unix)
  137. {
  138. return UnixExtractFileName(Path);
  139. }
  140. else
  141. {
  142. return ExtractFileName(Path);
  143. }
  144. }
  145. //---------------------------------------------------------------------------
  146. UnicodeString ExtractShortName(const UnicodeString & Path, bool Unix)
  147. {
  148. UnicodeString Result = ExtractFileName(Path, Unix);
  149. if (Result.IsEmpty())
  150. {
  151. Result = Path;
  152. }
  153. return Result;
  154. }
  155. //---------------------------------------------------------------------------
  156. bool __fastcall ExtractCommonPath(TStrings * Files, UnicodeString & Path)
  157. {
  158. DebugAssert(Files->Count > 0);
  159. Path = ExtractFilePath(Files->Strings[0]);
  160. bool Result = !Path.IsEmpty();
  161. if (Result)
  162. {
  163. for (int Index = 1; Index < Files->Count; Index++)
  164. {
  165. while (!Path.IsEmpty() &&
  166. (Files->Strings[Index].SubString(1, Path.Length()) != Path))
  167. {
  168. int PrevLen = Path.Length();
  169. Path = ExtractFilePath(ExcludeTrailingBackslash(Path));
  170. if (Path.Length() == PrevLen)
  171. {
  172. Path = L"";
  173. Result = false;
  174. }
  175. }
  176. }
  177. }
  178. return Result;
  179. }
  180. //---------------------------------------------------------------------------
  181. static UnicodeString GetFileListItemPath(TStrings * Files, int Index)
  182. {
  183. UnicodeString Result;
  184. if (Files->Objects[Index] != NULL)
  185. {
  186. Result = DebugNotNull(dynamic_cast<TRemoteFile *>(Files->Objects[Index]))->FullFileName;
  187. }
  188. else
  189. {
  190. Result = Files->Strings[Index];
  191. }
  192. return Result;
  193. }
  194. //---------------------------------------------------------------------------
  195. bool __fastcall UnixExtractCommonPath(TStrings * Files, UnicodeString & Path)
  196. {
  197. DebugAssert(Files->Count > 0);
  198. Path = UnixExtractFilePath(GetFileListItemPath(Files, 0));
  199. bool Result = !Path.IsEmpty();
  200. if (Result)
  201. {
  202. for (int Index = 1; Index < Files->Count; Index++)
  203. {
  204. while (!Path.IsEmpty() &&
  205. (GetFileListItemPath(Files, Index).SubString(1, Path.Length()) != Path))
  206. {
  207. int PrevLen = Path.Length();
  208. Path = UnixExtractFilePath(UnixExcludeTrailingBackslash(Path));
  209. if (Path.Length() == PrevLen)
  210. {
  211. Path = L"";
  212. Result = false;
  213. }
  214. }
  215. }
  216. }
  217. return Result;
  218. }
  219. //---------------------------------------------------------------------------
  220. bool __fastcall IsUnixRootPath(const UnicodeString & Path)
  221. {
  222. return Path.IsEmpty() || (Path == ROOTDIRECTORY);
  223. }
  224. //---------------------------------------------------------------------------
  225. bool __fastcall IsUnixHiddenFile(const UnicodeString & FileName)
  226. {
  227. return IsRealFile(FileName) && !FileName.IsEmpty() && (FileName[1] == L'.');
  228. }
  229. //---------------------------------------------------------------------------
  230. UnicodeString __fastcall AbsolutePath(const UnicodeString & Base, const UnicodeString & Path)
  231. {
  232. // There's a duplicate implementation in TTerminal::ExpandFileName()
  233. UnicodeString Result;
  234. if (Path.IsEmpty())
  235. {
  236. Result = Base;
  237. }
  238. else if (Path[1] == L'/')
  239. {
  240. Result = UnixExcludeTrailingBackslash(Path);
  241. }
  242. else
  243. {
  244. Result = UnixIncludeTrailingBackslash(
  245. UnixIncludeTrailingBackslash(Base) + Path);
  246. int P;
  247. while ((P = Result.Pos(L"/../")) > 0)
  248. {
  249. // special case, "/../" => "/"
  250. if (P == 1)
  251. {
  252. Result = L"/";
  253. }
  254. else
  255. {
  256. int P2 = Result.SubString(1, P-1).LastDelimiter(L"/");
  257. DebugAssert(P2 > 0);
  258. Result.Delete(P2, P - P2 + 3);
  259. }
  260. }
  261. while ((P = Result.Pos(L"/./")) > 0)
  262. {
  263. Result.Delete(P, 2);
  264. }
  265. Result = UnixExcludeTrailingBackslash(Result);
  266. }
  267. return Result;
  268. }
  269. //---------------------------------------------------------------------------
  270. UnicodeString __fastcall FromUnixPath(const UnicodeString & Path)
  271. {
  272. return ReplaceStr(Path, L"/", L"\\");
  273. }
  274. //---------------------------------------------------------------------------
  275. UnicodeString __fastcall ToUnixPath(const UnicodeString & Path)
  276. {
  277. return ReplaceStr(Path, L"\\", L"/");
  278. }
  279. //---------------------------------------------------------------------------
  280. static void __fastcall CutFirstDirectory(UnicodeString & S, bool Unix)
  281. {
  282. bool Root;
  283. int P;
  284. UnicodeString Sep = Unix ? L"/" : L"\\";
  285. if (S == Sep)
  286. {
  287. S = L"";
  288. }
  289. else
  290. {
  291. if (S[1] == Sep[1])
  292. {
  293. Root = true;
  294. S.Delete(1, 1);
  295. }
  296. else
  297. {
  298. Root = false;
  299. }
  300. if (S[1] == L'.')
  301. {
  302. S.Delete(1, 4);
  303. }
  304. P = S.Pos(Sep[1]);
  305. if (P)
  306. {
  307. S.Delete(1, P);
  308. S = Ellipsis + Sep + S;
  309. }
  310. else
  311. {
  312. S = L"";
  313. }
  314. if (Root)
  315. {
  316. S = Sep + S;
  317. }
  318. }
  319. }
  320. //---------------------------------------------------------------------------
  321. UnicodeString __fastcall MinimizeName(const UnicodeString & FileName, int MaxLen, bool Unix)
  322. {
  323. UnicodeString Drive, Dir, Name, Result;
  324. UnicodeString Sep = Unix ? L"/" : L"\\";
  325. Result = FileName;
  326. if (Unix)
  327. {
  328. int P = Result.LastDelimiter(L"/");
  329. if (P)
  330. {
  331. Dir = Result.SubString(1, P);
  332. Name = Result.SubString(P + 1, Result.Length() - P);
  333. }
  334. else
  335. {
  336. Dir = L"";
  337. Name = Result;
  338. }
  339. }
  340. else
  341. {
  342. Dir = ExtractFilePath(Result);
  343. Name = ExtractFileName(Result);
  344. if (Dir.Length() >= 2 && Dir[2] == L':')
  345. {
  346. Drive = Dir.SubString(1, 2);
  347. Dir.Delete(1, 2);
  348. }
  349. }
  350. while ((!Dir.IsEmpty() || !Drive.IsEmpty()) && (Result.Length() > MaxLen))
  351. {
  352. if (Dir == Sep + Ellipsis + Sep)
  353. {
  354. Dir = Ellipsis + Sep;
  355. }
  356. else if (Dir == L"")
  357. {
  358. Drive = L"";
  359. }
  360. else
  361. {
  362. CutFirstDirectory(Dir, Unix);
  363. }
  364. Result = Drive + Dir + Name;
  365. }
  366. if (Result.Length() > MaxLen)
  367. {
  368. Result = Result.SubString(1, MaxLen);
  369. }
  370. return Result;
  371. }
  372. //---------------------------------------------------------------------------
  373. UnicodeString __fastcall MakeFileList(TStrings * FileList)
  374. {
  375. UnicodeString Result;
  376. for (int Index = 0; Index < FileList->Count; Index++)
  377. {
  378. UnicodeString FileName = FileList->Strings[Index];
  379. // currently this is used for local file only, so no delimiting is done
  380. AddToList(Result, AddQuotes(FileName), L" ");
  381. }
  382. return Result;
  383. }
  384. //---------------------------------------------------------------------------
  385. // copy from BaseUtils.pas
  386. TDateTime __fastcall ReduceDateTimePrecision(TDateTime DateTime,
  387. TModificationFmt Precision)
  388. {
  389. if (Precision == mfNone)
  390. {
  391. DateTime = double(0);
  392. }
  393. else if (Precision != mfFull)
  394. {
  395. unsigned short Y, M, D, H, N, S, MS;
  396. DecodeDate(DateTime, Y, M, D);
  397. DecodeTime(DateTime, H, N, S, MS);
  398. switch (Precision)
  399. {
  400. case mfYMDHM:
  401. case mfMDHM:
  402. S = 0;
  403. MS = 0;
  404. break;
  405. case mfMDY:
  406. H = 0;
  407. N = 0;
  408. S = 0;
  409. MS = 0;
  410. break;
  411. default:
  412. DebugFail();
  413. }
  414. DateTime = EncodeDateVerbose(Y, M, D) + EncodeTimeVerbose(H, N, S, MS);
  415. }
  416. return DateTime;
  417. }
  418. //---------------------------------------------------------------------------
  419. TModificationFmt __fastcall LessDateTimePrecision(
  420. TModificationFmt Precision1, TModificationFmt Precision2)
  421. {
  422. return (Precision1 < Precision2) ? Precision1 : Precision2;
  423. }
  424. //---------------------------------------------------------------------------
  425. UnicodeString __fastcall UserModificationStr(TDateTime DateTime,
  426. TModificationFmt Precision)
  427. {
  428. switch (Precision)
  429. {
  430. case mfNone:
  431. return L"";
  432. case mfMDY:
  433. return FormatDateTime(L"ddddd", DateTime);
  434. case mfYMDHM:
  435. case mfMDHM:
  436. return FormatDateTime(L"ddddd t", DateTime);
  437. case mfFull:
  438. default:
  439. // Keep consistent with TDirView.GetDisplayInfo
  440. return FormatDateTime(L"ddddd tt", DateTime);
  441. }
  442. }
  443. //---------------------------------------------------------------------------
  444. UnicodeString __fastcall ModificationStr(TDateTime DateTime,
  445. TModificationFmt Precision)
  446. {
  447. Word Year, Month, Day, Hour, Min, Sec, MSec;
  448. DateTime.DecodeDate(&Year, &Month, &Day);
  449. DateTime.DecodeTime(&Hour, &Min, &Sec, &MSec);
  450. switch (Precision)
  451. {
  452. case mfNone:
  453. return L"";
  454. case mfMDY:
  455. return FORMAT(L"%3s %2d %2d", (EngShortMonthNames[Month-1], Day, Year));
  456. case mfMDHM:
  457. return FORMAT(L"%3s %2d %2d:%2.2d",
  458. (EngShortMonthNames[Month-1], Day, Hour, Min));
  459. case mfYMDHM:
  460. return FORMAT(L"%3s %2d %2d:%2.2d %4d",
  461. (EngShortMonthNames[Month-1], Day, Hour, Min, Year));
  462. default:
  463. DebugFail();
  464. // fall thru
  465. case mfFull:
  466. return FORMAT(L"%3s %2d %2d:%2.2d:%2.2d %4d",
  467. (EngShortMonthNames[Month-1], Day, Hour, Min, Sec, Year));
  468. }
  469. }
  470. //---------------------------------------------------------------------------
  471. int GetPartialFileExtLen(const UnicodeString & FileName)
  472. {
  473. int Result = 0;
  474. if (EndsText(PartialExt, FileName))
  475. {
  476. Result = PartialExt.Length();
  477. }
  478. else
  479. {
  480. int P = FileName.LastDelimiter(L".");
  481. if ((P > 0) && (P < FileName.Length()))
  482. {
  483. if (IsNumber(MidStr(FileName, P + 1)) &&
  484. EndsText(PartialExt, FileName.SubString(1, P - 1)))
  485. {
  486. Result = PartialExt.Length() + (FileName.Length() - P + 1);
  487. }
  488. }
  489. }
  490. return Result;
  491. }
  492. //---------------------------------------------------------------------------
  493. int __fastcall FakeFileImageIndex(UnicodeString FileName, unsigned long Attrs,
  494. UnicodeString * TypeName)
  495. {
  496. Attrs |= FILE_ATTRIBUTE_NORMAL;
  497. TSHFileInfoW SHFileInfo;
  498. // On Win2k we get icon of "ZIP drive" for ".." (parent directory)
  499. if ((FileName == PARENTDIRECTORY) ||
  500. ((FileName.Length() == 2) && (FileName[2] == L':') && IsLetter(FileName[1])) ||
  501. IsReservedName(FileName))
  502. {
  503. FileName = L"dumb";
  504. }
  505. // this should be somewhere else, probably in TUnixDirView,
  506. // as the "partial" overlay is added there too
  507. int PartialFileExtLen = GetPartialFileExtLen(FileName);
  508. if (GetPartialFileExtLen(FileName) > 0)
  509. {
  510. FileName.SetLength(FileName.Length() - PartialFileExtLen);
  511. }
  512. int Icon;
  513. if (SHGetFileInfo(FileName.c_str(),
  514. Attrs, &SHFileInfo, sizeof(SHFileInfo),
  515. SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES | SHGFI_TYPENAME) != 0)
  516. {
  517. if (TypeName != NULL)
  518. {
  519. *TypeName = SHFileInfo.szTypeName;
  520. }
  521. Icon = SHFileInfo.iIcon;
  522. }
  523. else
  524. {
  525. if (TypeName != NULL)
  526. {
  527. *TypeName = L"";
  528. }
  529. Icon = -1;
  530. }
  531. return Icon;
  532. }
  533. //---------------------------------------------------------------------------
  534. bool __fastcall SameUserName(const UnicodeString & UserName1, const UnicodeString & UserName2)
  535. {
  536. // Bitvise reports file owner as "user@host", but we login with "user" only.
  537. UnicodeString AUserName1 = CopyToChar(UserName1, L'@', true);
  538. UnicodeString AUserName2 = CopyToChar(UserName2, L'@', true);
  539. return SameText(AUserName1, AUserName2);
  540. }
  541. //---------------------------------------------------------------------------
  542. UnicodeString __fastcall FormatMultiFilesToOneConfirmation(const UnicodeString & Target, bool Unix)
  543. {
  544. UnicodeString Dir;
  545. UnicodeString Name;
  546. UnicodeString Path;
  547. if (Unix)
  548. {
  549. Dir = UnixExtractFileDir(Target);
  550. Name = UnixExtractFileName(Target);
  551. Path = UnixIncludeTrailingBackslash(Target);
  552. }
  553. else
  554. {
  555. Dir = ExtractFilePath(Target);
  556. Name = ExtractFileName(Target);
  557. Path = IncludeTrailingBackslash(Target);
  558. }
  559. return FMTLOAD(MULTI_FILES_TO_ONE, (Name, Dir, Path));
  560. }
  561. //---------------------------------------------------------------------------
  562. //---------------------------------------------------------------------------
  563. __fastcall TRemoteToken::TRemoteToken() :
  564. FID(0),
  565. FIDValid(false)
  566. {
  567. }
  568. //---------------------------------------------------------------------------
  569. __fastcall TRemoteToken::TRemoteToken(const UnicodeString & Name) :
  570. FName(Name),
  571. FID(0),
  572. FIDValid(false)
  573. {
  574. }
  575. //---------------------------------------------------------------------------
  576. void __fastcall TRemoteToken::Clear()
  577. {
  578. FID = 0;
  579. FIDValid = false;
  580. }
  581. //---------------------------------------------------------------------------
  582. bool __fastcall TRemoteToken::operator ==(const TRemoteToken & rht) const
  583. {
  584. return
  585. (FName == rht.FName) &&
  586. (FIDValid == rht.FIDValid) &&
  587. (!FIDValid || (FID == rht.FID));
  588. }
  589. //---------------------------------------------------------------------------
  590. bool __fastcall TRemoteToken::operator !=(const TRemoteToken & rht) const
  591. {
  592. return !(*this == rht);
  593. }
  594. //---------------------------------------------------------------------------
  595. TRemoteToken & __fastcall TRemoteToken::operator =(const TRemoteToken & rht)
  596. {
  597. if (this != &rht)
  598. {
  599. FName = rht.FName;
  600. FIDValid = rht.FIDValid;
  601. FID = rht.FID;
  602. }
  603. return *this;
  604. }
  605. //---------------------------------------------------------------------------
  606. int __fastcall TRemoteToken::Compare(const TRemoteToken & rht) const
  607. {
  608. int Result;
  609. if (!FName.IsEmpty())
  610. {
  611. if (!rht.FName.IsEmpty())
  612. {
  613. Result = AnsiCompareText(FName, rht.FName);
  614. }
  615. else
  616. {
  617. Result = -1;
  618. }
  619. }
  620. else
  621. {
  622. if (!rht.FName.IsEmpty())
  623. {
  624. Result = 1;
  625. }
  626. else
  627. {
  628. if (FIDValid)
  629. {
  630. if (rht.FIDValid)
  631. {
  632. Result = (FID < rht.FID) ? -1 : ((FID > rht.FID) ? 1 : 0);
  633. }
  634. else
  635. {
  636. Result = -1;
  637. }
  638. }
  639. else
  640. {
  641. if (rht.FIDValid)
  642. {
  643. Result = 1;
  644. }
  645. else
  646. {
  647. Result = 0;
  648. }
  649. }
  650. }
  651. }
  652. return Result;
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TRemoteToken::SetID(unsigned int value)
  656. {
  657. FID = value;
  658. FIDValid = true;
  659. }
  660. //---------------------------------------------------------------------------
  661. bool __fastcall TRemoteToken::GetNameValid() const
  662. {
  663. return !FName.IsEmpty();
  664. }
  665. //---------------------------------------------------------------------------
  666. bool __fastcall TRemoteToken::GetIsSet() const
  667. {
  668. return !FName.IsEmpty() || FIDValid;
  669. }
  670. //---------------------------------------------------------------------------
  671. UnicodeString __fastcall TRemoteToken::GetDisplayText() const
  672. {
  673. if (!FName.IsEmpty())
  674. {
  675. return FName;
  676. }
  677. else if (FIDValid)
  678. {
  679. return IntToStr(int(FID));
  680. }
  681. else
  682. {
  683. return UnicodeString();
  684. }
  685. }
  686. //---------------------------------------------------------------------------
  687. UnicodeString __fastcall TRemoteToken::GetLogText() const
  688. {
  689. return FORMAT(L"\"%s\" [%d]", (FName, int(FID)));
  690. }
  691. //---------------------------------------------------------------------------
  692. //---------------------------------------------------------------------------
  693. TRemoteTokenList * __fastcall TRemoteTokenList::Duplicate() const
  694. {
  695. TRemoteTokenList * Result = new TRemoteTokenList();
  696. try
  697. {
  698. TTokens::const_iterator I = FTokens.begin();
  699. while (I != FTokens.end())
  700. {
  701. Result->Add(*I);
  702. ++I;
  703. }
  704. }
  705. catch(...)
  706. {
  707. delete Result;
  708. throw;
  709. }
  710. return Result;
  711. }
  712. //---------------------------------------------------------------------------
  713. void __fastcall TRemoteTokenList::Clear()
  714. {
  715. FTokens.clear();
  716. FNameMap.clear();
  717. FIDMap.clear();
  718. }
  719. //---------------------------------------------------------------------------
  720. void __fastcall TRemoteTokenList::Add(const TRemoteToken & Token)
  721. {
  722. FTokens.push_back(Token);
  723. if (Token.IDValid)
  724. {
  725. std::pair<TIDMap::iterator, bool> Position =
  726. FIDMap.insert(TIDMap::value_type(Token.ID, FTokens.size() - 1));
  727. }
  728. if (Token.NameValid)
  729. {
  730. std::pair<TNameMap::iterator, bool> Position =
  731. FNameMap.insert(TNameMap::value_type(Token.Name, FTokens.size() - 1));
  732. }
  733. }
  734. //---------------------------------------------------------------------------
  735. void __fastcall TRemoteTokenList::AddUnique(const TRemoteToken & Token)
  736. {
  737. if (Token.IDValid)
  738. {
  739. TIDMap::const_iterator I = FIDMap.find(Token.ID);
  740. if (I != FIDMap.end())
  741. {
  742. // is present already.
  743. // may have different name (should not),
  744. // but what can we do about it anyway?
  745. }
  746. else
  747. {
  748. Add(Token);
  749. }
  750. }
  751. else if (Token.NameValid)
  752. {
  753. TNameMap::const_iterator I = FNameMap.find(Token.Name);
  754. if (I != FNameMap.end())
  755. {
  756. // is present already.
  757. }
  758. else
  759. {
  760. Add(Token);
  761. }
  762. }
  763. else
  764. {
  765. // can happen, e.g. with winsshd/SFTP
  766. }
  767. }
  768. //---------------------------------------------------------------------------
  769. bool __fastcall TRemoteTokenList::Exists(const UnicodeString & Name) const
  770. {
  771. // We should make use of SameUserName
  772. return (FNameMap.find(Name) != FNameMap.end());
  773. }
  774. //---------------------------------------------------------------------------
  775. const TRemoteToken * TRemoteTokenList::Find(unsigned int ID) const
  776. {
  777. TIDMap::const_iterator I = FIDMap.find(ID);
  778. const TRemoteToken * Result;
  779. if (I != FIDMap.end())
  780. {
  781. Result = &FTokens[(*I).second];
  782. }
  783. else
  784. {
  785. Result = NULL;
  786. }
  787. return Result;
  788. }
  789. //---------------------------------------------------------------------------
  790. const TRemoteToken * TRemoteTokenList::Find(const UnicodeString & Name) const
  791. {
  792. TNameMap::const_iterator I = FNameMap.find(Name);
  793. const TRemoteToken * Result;
  794. if (I != FNameMap.end())
  795. {
  796. Result = &FTokens[(*I).second];
  797. }
  798. else
  799. {
  800. Result = NULL;
  801. }
  802. return Result;
  803. }
  804. //---------------------------------------------------------------------------
  805. void __fastcall TRemoteTokenList::Log(TTerminal * Terminal, const wchar_t * Title)
  806. {
  807. if (!FTokens.empty())
  808. {
  809. Terminal->LogEvent(FORMAT(L"Following %s found:", (Title)));
  810. for (size_t Index = 0; Index < FTokens.size(); Index++)
  811. {
  812. Terminal->LogEvent(UnicodeString(L" ") + FTokens[Index].LogText);
  813. }
  814. }
  815. else
  816. {
  817. Terminal->LogEvent(FORMAT(L"No %s found.", (Title)));
  818. }
  819. }
  820. //---------------------------------------------------------------------------
  821. int __fastcall TRemoteTokenList::Count() const
  822. {
  823. return (int)FTokens.size();
  824. }
  825. //---------------------------------------------------------------------------
  826. const TRemoteToken * __fastcall TRemoteTokenList::Token(int Index) const
  827. {
  828. return &FTokens[Index];
  829. }
  830. //---------------------------------------------------------------------------
  831. //---------------------------------------------------------------------------
  832. __fastcall TRemoteFile::TRemoteFile(TRemoteFile * ALinkedByFile):
  833. TPersistent()
  834. {
  835. FLinkedFile = NULL;
  836. FRights = new TRights();
  837. FIconIndex = -1;
  838. FCyclicLink = false;
  839. FModificationFmt = mfFull;
  840. FLinkedByFile = ALinkedByFile;
  841. FTerminal = NULL;
  842. FDirectory = NULL;
  843. FIsHidden = -1;
  844. FIsEncrypted = false;
  845. FCalculatedSize = -1;
  846. }
  847. //---------------------------------------------------------------------------
  848. __fastcall TRemoteFile::~TRemoteFile()
  849. {
  850. delete FRights;
  851. delete FLinkedFile;
  852. }
  853. //---------------------------------------------------------------------------
  854. TRemoteFile * __fastcall TRemoteFile::Duplicate(bool Standalone) const
  855. {
  856. TRemoteFile * Result;
  857. Result = new TRemoteFile();
  858. try
  859. {
  860. if (FLinkedFile)
  861. {
  862. Result->FLinkedFile = FLinkedFile->Duplicate(true);
  863. Result->FLinkedFile->FLinkedByFile = Result;
  864. }
  865. *Result->Rights = *FRights;
  866. #define COPY_FP(PROP) Result->F ## PROP = F ## PROP;
  867. COPY_FP(Terminal);
  868. COPY_FP(Owner);
  869. COPY_FP(ModificationFmt);
  870. COPY_FP(Size);
  871. COPY_FP(CalculatedSize);
  872. COPY_FP(FileName);
  873. COPY_FP(DisplayName);
  874. COPY_FP(INodeBlocks);
  875. COPY_FP(Modification);
  876. COPY_FP(LastAccess);
  877. COPY_FP(Group);
  878. COPY_FP(IconIndex);
  879. COPY_FP(TypeName);
  880. COPY_FP(IsSymLink);
  881. COPY_FP(LinkTo);
  882. COPY_FP(Type);
  883. COPY_FP(CyclicLink);
  884. COPY_FP(HumanRights);
  885. COPY_FP(IsEncrypted);
  886. #undef COPY_FP
  887. if (Standalone && (!FFullFileName.IsEmpty() || (Directory != NULL)))
  888. {
  889. Result->FFullFileName = FullFileName;
  890. }
  891. }
  892. catch(...)
  893. {
  894. delete Result;
  895. throw;
  896. }
  897. return Result;
  898. }
  899. //---------------------------------------------------------------------------
  900. void __fastcall TRemoteFile::LoadTypeInfo()
  901. {
  902. /* TODO : If file is link: Should be attributes taken from linked file? */
  903. unsigned long Attrs = 0;
  904. if (IsDirectory) Attrs |= FILE_ATTRIBUTE_DIRECTORY;
  905. if (IsHidden) Attrs |= FILE_ATTRIBUTE_HIDDEN;
  906. UnicodeString DumbFileName = (IsSymLink && !LinkTo.IsEmpty() ? LinkTo : FileName);
  907. FIconIndex = FakeFileImageIndex(DumbFileName, Attrs, &FTypeName);
  908. }
  909. //---------------------------------------------------------------------------
  910. __int64 __fastcall TRemoteFile::GetSize() const
  911. {
  912. return IsDirectory ? 0 : FSize;
  913. }
  914. //---------------------------------------------------------------------------
  915. Integer __fastcall TRemoteFile::GetIconIndex() const
  916. {
  917. if (FIconIndex == -1)
  918. {
  919. const_cast<TRemoteFile *>(this)->LoadTypeInfo();
  920. }
  921. return FIconIndex;
  922. }
  923. //---------------------------------------------------------------------------
  924. UnicodeString __fastcall TRemoteFile::GetTypeName()
  925. {
  926. // check availability of type info by icon index, because type name can be empty
  927. if (FIconIndex < 0)
  928. {
  929. LoadTypeInfo();
  930. }
  931. return FTypeName;
  932. }
  933. //---------------------------------------------------------------------------
  934. Boolean __fastcall TRemoteFile::GetIsHidden() const
  935. {
  936. bool Result;
  937. switch (FIsHidden)
  938. {
  939. case 0:
  940. Result = false;
  941. break;
  942. case 1:
  943. Result = true;
  944. break;
  945. default:
  946. Result = IsUnixHiddenFile(FileName);
  947. break;
  948. }
  949. return Result;
  950. }
  951. //---------------------------------------------------------------------------
  952. void __fastcall TRemoteFile::SetIsHidden(bool value)
  953. {
  954. FIsHidden = value ? 1 : 0;
  955. }
  956. //---------------------------------------------------------------------------
  957. Boolean __fastcall TRemoteFile::GetIsDirectory() const
  958. {
  959. if (IsSymLink && (FLinkedFile != NULL))
  960. {
  961. return FLinkedFile->IsDirectory;
  962. }
  963. else
  964. {
  965. return (towupper(Type) == FILETYPE_DIRECTORY);
  966. }
  967. }
  968. //---------------------------------------------------------------------------
  969. Boolean __fastcall TRemoteFile::GetIsParentDirectory() const
  970. {
  971. return (FileName == PARENTDIRECTORY);
  972. }
  973. //---------------------------------------------------------------------------
  974. Boolean __fastcall TRemoteFile::GetIsThisDirectory() const
  975. {
  976. return (FileName == THISDIRECTORY);
  977. }
  978. //---------------------------------------------------------------------------
  979. Boolean __fastcall TRemoteFile::GetIsInaccesibleDirectory() const
  980. {
  981. Boolean Result;
  982. if (IsDirectory)
  983. {
  984. DebugAssert(Terminal);
  985. Result = !
  986. (SameUserName(Terminal->UserName, L"root") ||
  987. ((Rights->RightUndef[TRights::rrOtherExec] != TRights::rsNo)) ||
  988. ((Rights->Right[TRights::rrGroupExec] != TRights::rsNo) &&
  989. Terminal->Membership->Exists(Group.Name)) ||
  990. ((Rights->Right[TRights::rrUserExec] != TRights::rsNo) &&
  991. SameUserName(Terminal->UserName, Owner.Name)));
  992. }
  993. else Result = False;
  994. return Result;
  995. }
  996. //---------------------------------------------------------------------------
  997. wchar_t __fastcall TRemoteFile::GetType() const
  998. {
  999. return FType;
  1000. }
  1001. //---------------------------------------------------------------------------
  1002. void __fastcall TRemoteFile::SetType(wchar_t AType)
  1003. {
  1004. FType = AType;
  1005. FIsSymLink = ((wchar_t)towupper(FType) == FILETYPE_SYMLINK);
  1006. }
  1007. //---------------------------------------------------------------------------
  1008. const TRemoteFile * __fastcall TRemoteFile::GetLinkedFile() const
  1009. {
  1010. // do not call FindLinkedFile as it would be called repeatedly for broken symlinks
  1011. return FLinkedFile;
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. bool __fastcall TRemoteFile::GetBrokenLink()
  1015. {
  1016. DebugAssert(Terminal);
  1017. // If file is symlink but we couldn't find linked file we assume broken link
  1018. return (IsSymLink && (FCyclicLink || !FLinkedFile) &&
  1019. Terminal->ResolvingSymlinks);
  1020. // "!FLinkTo.IsEmpty()" removed because it does not work with SFTP
  1021. }
  1022. //---------------------------------------------------------------------------
  1023. bool __fastcall TRemoteFile::IsTimeShiftingApplicable()
  1024. {
  1025. return IsTimeShiftingApplicable(ModificationFmt);
  1026. }
  1027. //---------------------------------------------------------------------------
  1028. bool __fastcall TRemoteFile::IsTimeShiftingApplicable(TModificationFmt ModificationFmt)
  1029. {
  1030. return (ModificationFmt == mfMDHM) || (ModificationFmt == mfYMDHM) || (ModificationFmt == mfFull);
  1031. }
  1032. //---------------------------------------------------------------------------
  1033. void __fastcall TRemoteFile::ShiftTimeInSeconds(__int64 Seconds)
  1034. {
  1035. ShiftTimeInSeconds(FModification, ModificationFmt, Seconds);
  1036. ShiftTimeInSeconds(FLastAccess, ModificationFmt, Seconds);
  1037. }
  1038. //---------------------------------------------------------------------------
  1039. void __fastcall TRemoteFile::ShiftTimeInSeconds(TDateTime & DateTime, TModificationFmt ModificationFmt, __int64 Seconds)
  1040. {
  1041. if ((Seconds != 0) && IsTimeShiftingApplicable(ModificationFmt))
  1042. {
  1043. DebugAssert(int(DateTime) != 0);
  1044. DateTime = IncSecond(DateTime, Seconds);
  1045. }
  1046. }
  1047. //---------------------------------------------------------------------------
  1048. void __fastcall TRemoteFile::SetModification(const TDateTime & value)
  1049. {
  1050. if (FModification != value)
  1051. {
  1052. FModificationFmt = mfFull;
  1053. FModification = value;
  1054. }
  1055. }
  1056. //---------------------------------------------------------------------------
  1057. UnicodeString __fastcall TRemoteFile::GetUserModificationStr()
  1058. {
  1059. return ::UserModificationStr(Modification, FModificationFmt);
  1060. }
  1061. //---------------------------------------------------------------------------
  1062. UnicodeString __fastcall TRemoteFile::GetModificationStr()
  1063. {
  1064. return ::ModificationStr(Modification, FModificationFmt);
  1065. }
  1066. //---------------------------------------------------------------------------
  1067. UnicodeString __fastcall TRemoteFile::GetExtension()
  1068. {
  1069. return UnixExtractFileExt(FFileName);
  1070. }
  1071. //---------------------------------------------------------------------------
  1072. void __fastcall TRemoteFile::SetRights(TRights * value)
  1073. {
  1074. FRights->Assign(value);
  1075. }
  1076. //---------------------------------------------------------------------------
  1077. UnicodeString __fastcall TRemoteFile::GetRightsStr()
  1078. {
  1079. // note that HumanRights is typically an empty string
  1080. // (with an exception of Perm-fact-only MLSD FTP listing)
  1081. return FRights->Unknown ? HumanRights : FRights->Text;
  1082. }
  1083. //---------------------------------------------------------------------------
  1084. void __fastcall TRemoteFile::SetListingStr(UnicodeString value)
  1085. {
  1086. // Value stored in 'value' can be used for error message
  1087. UnicodeString Line = value;
  1088. FIconIndex = -1;
  1089. try
  1090. {
  1091. UnicodeString Col;
  1092. // Do we need to do this (is ever TAB is LS output)?
  1093. Line = ReplaceChar(Line, L'\t', L' ');
  1094. Type = Line[1];
  1095. Line.Delete(1, 1);
  1096. #define GETNCOL \
  1097. { if (Line.IsEmpty()) throw Exception(L""); \
  1098. Integer P = Line.Pos(L' '); \
  1099. if (P) { Col = Line.SubString(1, P-1); Line.Delete(1, P); } \
  1100. else { Col = Line; Line = L""; } \
  1101. }
  1102. #define GETCOL { GETNCOL; Line = TrimLeft(Line); }
  1103. // Rights string may contain special permission attributes (S,t, ...)
  1104. // (TODO: maybe no longer necessary, once we can handle the special permissions)
  1105. Rights->AllowUndef = True;
  1106. // On some system there is no space between permissions and node blocks count columns
  1107. // so we get only first 9 characters and trim all following spaces (if any)
  1108. Rights->Text = Line.SubString(1, 9);
  1109. Line.Delete(1, 9);
  1110. // Rights column maybe followed by '+', '@' or '.' signs, we ignore them
  1111. // (On MacOS, there may be a space in between)
  1112. if (!Line.IsEmpty() && ((Line[1] == L'+') || (Line[1] == L'@') || (Line[1] == L'.')))
  1113. {
  1114. Line.Delete(1, 1);
  1115. }
  1116. else if ((Line.Length() >= 2) && (Line[1] == L' ') &&
  1117. ((Line[2] == L'+') || (Line[2] == L'@') || (Line[2] == L'.')))
  1118. {
  1119. Line.Delete(1, 2);
  1120. }
  1121. Line = Line.TrimLeft();
  1122. GETCOL;
  1123. if (!TryStrToInt(Col, FINodeBlocks))
  1124. {
  1125. // if the column is not an integer, suppose it's owner
  1126. // (Android BusyBox)
  1127. FINodeBlocks = 0;
  1128. }
  1129. else
  1130. {
  1131. GETCOL;
  1132. }
  1133. FOwner.Name = Col;
  1134. // #60 17.10.01: group name can contain space
  1135. FGroup.Name = L"";
  1136. GETCOL;
  1137. __int64 ASize;
  1138. do
  1139. {
  1140. FGroup.Name = FGroup.Name + Col;
  1141. GETCOL;
  1142. // SSH FS link like
  1143. // d????????? ? ? ? ? ? name
  1144. if ((FGroup.Name == L"?") && (Col == L"?"))
  1145. {
  1146. ASize = 0;
  1147. }
  1148. else
  1149. {
  1150. DebugAssert(!Col.IsEmpty());
  1151. // for devices etc.. there is additional column ending by comma, we ignore it
  1152. if (Col[Col.Length()] == L',') GETCOL;
  1153. ASize = StrToInt64Def(Col, -1);
  1154. // if it's not a number (file size) we take it as part of group name
  1155. // (at least on CygWin, there can be group with space in its name)
  1156. if (ASize < 0) Col = L" " + Col;
  1157. }
  1158. }
  1159. while (ASize < 0);
  1160. // Do not read modification time and filename (test close to the end of this block) if it is already set.
  1161. if (double(FModification) == 0)
  1162. {
  1163. bool FullTime = false;
  1164. bool DayMonthFormat = false;
  1165. Word Day, Month, Year, Hour, Min, Sec, P;
  1166. GETCOL;
  1167. // SSH FS link, see above
  1168. if (Col == L"?")
  1169. {
  1170. GETCOL;
  1171. FModificationFmt = mfNone;
  1172. FModification = 0;
  1173. FLastAccess = 0;
  1174. }
  1175. else
  1176. {
  1177. // format dd mmm or mmm dd ?
  1178. Day = (Word)StrToIntDef(Col, 0);
  1179. if (Day > 0)
  1180. {
  1181. DayMonthFormat = true;
  1182. GETCOL;
  1183. }
  1184. Month = 0;
  1185. #define COL2MONTH \
  1186. for (Word IMonth = 0; IMonth < 12; IMonth++) \
  1187. if (!Col.CompareIC(EngShortMonthNames[IMonth])) { Month = IMonth; Month++; break; }
  1188. COL2MONTH;
  1189. // if the column is not known month name, it may have been "yyyy-mm-dd"
  1190. // for --full-time format
  1191. if ((Month == 0) && (Col.Length() == 10) && (Col[5] == L'-') && (Col[8] == L'-'))
  1192. {
  1193. Year = (Word)Col.SubString(1, 4).ToInt();
  1194. Month = (Word)Col.SubString(6, 2).ToInt();
  1195. Day = (Word)Col.SubString(9, 2).ToInt();
  1196. GETCOL;
  1197. Hour = (Word)Col.SubString(1, 2).ToInt();
  1198. Min = (Word)Col.SubString(4, 2).ToInt();
  1199. if (Col.Length() >= 8)
  1200. {
  1201. Sec = (Word)StrToInt(Col.SubString(7, 2));
  1202. }
  1203. else
  1204. {
  1205. Sec = 0;
  1206. }
  1207. FModificationFmt = mfFull;
  1208. // skip TZ (TODO)
  1209. // do not trim leading space of filename
  1210. GETNCOL;
  1211. }
  1212. else
  1213. {
  1214. // or it may have been day name for another format of --full-time
  1215. if (Month == 0)
  1216. {
  1217. GETCOL;
  1218. COL2MONTH;
  1219. // neither standard, not --full-time format
  1220. if (Month == 0)
  1221. {
  1222. Abort();
  1223. }
  1224. else
  1225. {
  1226. FullTime = true;
  1227. }
  1228. }
  1229. #undef COL2MONTH
  1230. if (Day == 0)
  1231. {
  1232. GETNCOL;
  1233. Day = (Word)StrToInt(Col);
  1234. }
  1235. if ((Day < 1) || (Day > 31)) Abort();
  1236. // second full-time format
  1237. // ddd mmm dd hh:nn:ss yyyy
  1238. if (FullTime)
  1239. {
  1240. GETCOL;
  1241. if (Col.Length() != 8)
  1242. {
  1243. Abort();
  1244. }
  1245. Hour = (Word)StrToInt(Col.SubString(1, 2));
  1246. Min = (Word)StrToInt(Col.SubString(4, 2));
  1247. Sec = (Word)StrToInt(Col.SubString(7, 2));
  1248. FModificationFmt = mfFull;
  1249. // do not trim leading space of filename
  1250. GETNCOL;
  1251. Year = (Word)StrToInt(Col);
  1252. }
  1253. else
  1254. {
  1255. // for format dd mmm the below description seems not to be true,
  1256. // the year is not aligned to 5 characters
  1257. if (DayMonthFormat)
  1258. {
  1259. GETCOL;
  1260. }
  1261. else
  1262. {
  1263. // Time/Year indicator is always 5 characters long (???), on most
  1264. // systems year is aligned to right (_YYYY), but on some to left (YYYY_),
  1265. // we must ensure that trailing space is also deleted, so real
  1266. // separator space is not treated as part of file name
  1267. Col = Line.SubString(1, 6).Trim();
  1268. Line.Delete(1, 6);
  1269. }
  1270. // GETNCOL; // We don't want to trim input strings (name with space at beginning???)
  1271. // Check if we got time (contains :) or year
  1272. if ((P = (Word)Col.Pos(L':')) > 0)
  1273. {
  1274. Word CurrMonth, CurrDay;
  1275. Hour = (Word)StrToInt(Col.SubString(1, P-1));
  1276. Min = (Word)StrToInt(Col.SubString(P+1, Col.Length() - P));
  1277. if (Hour > 23 || Min > 59) Abort();
  1278. // When we don't got year, we assume current year
  1279. // with exception that the date would be in future
  1280. // in this case we assume last year.
  1281. DecodeDate(Date(), Year, CurrMonth, CurrDay);
  1282. if ((Month > CurrMonth) ||
  1283. (Month == CurrMonth && Day > CurrDay)) Year--;
  1284. Sec = 0;
  1285. FModificationFmt = mfMDHM;
  1286. }
  1287. else
  1288. {
  1289. Year = (Word)StrToInt(Col);
  1290. if (Year > 10000) Abort();
  1291. // When we didn't get time we assume midnight
  1292. Hour = 0; Min = 0; Sec = 0;
  1293. FModificationFmt = mfMDY;
  1294. }
  1295. }
  1296. }
  1297. FModification = EncodeDateVerbose(Year, Month, Day) + EncodeTimeVerbose(Hour, Min, Sec, 0);
  1298. // adjust only when time is known,
  1299. // adjusting default "midnight" time makes no sense
  1300. if ((FModificationFmt == mfMDHM) ||
  1301. DebugAlwaysFalse(FModificationFmt == mfYMDHM) ||
  1302. (FModificationFmt == mfFull))
  1303. {
  1304. DebugAssert(Terminal != NULL);
  1305. FModification = AdjustDateTimeFromUnix(FModification,
  1306. Terminal->SessionData->DSTMode);
  1307. }
  1308. if (double(FLastAccess) == 0)
  1309. {
  1310. FLastAccess = FModification;
  1311. }
  1312. }
  1313. // separating space is already deleted, other spaces are treated as part of name
  1314. // see comment at the beginning of the block
  1315. if (FileName.IsEmpty())
  1316. {
  1317. FSize = ASize;
  1318. int P;
  1319. FLinkTo = L"";
  1320. if (IsSymLink)
  1321. {
  1322. P = Line.Pos(SYMLINKSTR);
  1323. if (P)
  1324. {
  1325. FLinkTo = Line.SubString(
  1326. P + wcslen(SYMLINKSTR), Line.Length() - P + wcslen(SYMLINKSTR) + 1);
  1327. Line.SetLength(P - 1);
  1328. }
  1329. else
  1330. {
  1331. Abort();
  1332. }
  1333. }
  1334. FFileName = UnixExtractFileName(Line);
  1335. }
  1336. }
  1337. #undef GETNCOL
  1338. #undef GETCOL
  1339. }
  1340. catch (Exception &E)
  1341. {
  1342. throw ETerminal(&E, FmtLoadStr(LIST_LINE_ERROR, ARRAYOFCONST((value))), HELP_LIST_LINE_ERROR);
  1343. }
  1344. }
  1345. //---------------------------------------------------------------------------
  1346. void __fastcall TRemoteFile::Complete()
  1347. {
  1348. DebugAssert(Terminal != NULL);
  1349. if (IsSymLink && Terminal->ResolvingSymlinks)
  1350. {
  1351. FindLinkedFile();
  1352. }
  1353. }
  1354. //---------------------------------------------------------------------------
  1355. void __fastcall TRemoteFile::SetEncrypted()
  1356. {
  1357. FIsEncrypted = true;
  1358. if (Size > TEncryption::GetOverhead())
  1359. {
  1360. Size -= TEncryption::GetOverhead();
  1361. }
  1362. }
  1363. //---------------------------------------------------------------------------
  1364. void __fastcall TRemoteFile::FindLinkedFile()
  1365. {
  1366. DebugAssert(Terminal && IsSymLink);
  1367. if (FLinkedFile) delete FLinkedFile;
  1368. FLinkedFile = NULL;
  1369. FCyclicLink = false;
  1370. if (!LinkTo.IsEmpty())
  1371. {
  1372. // check for cyclic link
  1373. TRemoteFile * LinkedBy = FLinkedByFile;
  1374. while (LinkedBy)
  1375. {
  1376. if (LinkedBy->LinkTo == LinkTo)
  1377. {
  1378. // this is currently redundant information, because it is used only to
  1379. // detect broken symlink, which would be otherwise detected
  1380. // by FLinkedFile == NULL
  1381. FCyclicLink = true;
  1382. break;
  1383. }
  1384. LinkedBy = LinkedBy->FLinkedByFile;
  1385. }
  1386. }
  1387. if (FCyclicLink)
  1388. {
  1389. TRemoteFile * LinkedBy = FLinkedByFile;
  1390. while (LinkedBy)
  1391. {
  1392. LinkedBy->FCyclicLink = true;
  1393. LinkedBy = LinkedBy->FLinkedByFile;
  1394. }
  1395. }
  1396. else
  1397. {
  1398. DebugAssert(Terminal->ResolvingSymlinks);
  1399. Terminal->ExceptionOnFail = true;
  1400. try
  1401. {
  1402. try
  1403. {
  1404. Terminal->ReadSymlink(this, FLinkedFile);
  1405. }
  1406. __finally
  1407. {
  1408. Terminal->ExceptionOnFail = false;
  1409. }
  1410. }
  1411. catch (Exception &E)
  1412. {
  1413. if (E.InheritsFrom(__classid(EFatal))) throw;
  1414. else
  1415. {
  1416. Terminal->Log->AddException(&E);
  1417. }
  1418. }
  1419. }
  1420. }
  1421. //---------------------------------------------------------------------------
  1422. const TRemoteFile * __fastcall TRemoteFile::Resolve() const
  1423. {
  1424. const TRemoteFile * Result = this;
  1425. while (Result->LinkedFile != NULL)
  1426. {
  1427. Result = Result->LinkedFile;
  1428. }
  1429. return Result;
  1430. }
  1431. //---------------------------------------------------------------------------
  1432. UnicodeString __fastcall TRemoteFile::GetListingStr()
  1433. {
  1434. // note that ModificationStr is longer than 12 for mfFull
  1435. UnicodeString LinkPart;
  1436. // expanded from ?: to avoid memory leaks
  1437. if (IsSymLink)
  1438. {
  1439. LinkPart = UnicodeString(SYMLINKSTR) + LinkTo;
  1440. }
  1441. return Format(L"%s%s %3s %-8s %-8s %9s %-12s %s%s", ARRAYOFCONST((
  1442. Type, Rights->Text, IntToStr(INodeBlocks), Owner.Name, Group.Name,
  1443. IntToStr(FSize), // explicitly using size even for directories
  1444. ModificationStr, FileName,
  1445. LinkPart)));
  1446. }
  1447. //---------------------------------------------------------------------------
  1448. UnicodeString __fastcall TRemoteFile::GetFullFileName() const
  1449. {
  1450. UnicodeString Result;
  1451. if (FFullFileName.IsEmpty())
  1452. {
  1453. DebugAssert(Terminal);
  1454. DebugAssert(Directory != NULL);
  1455. if (IsParentDirectory)
  1456. {
  1457. Result = Directory->ParentPath;
  1458. }
  1459. else if (IsDirectory)
  1460. {
  1461. Result = UnixIncludeTrailingBackslash(Directory->FullDirectory + FileName);
  1462. }
  1463. else
  1464. {
  1465. Result = Directory->FullDirectory + FileName;
  1466. }
  1467. }
  1468. else
  1469. {
  1470. Result = FFullFileName;
  1471. }
  1472. return Result;
  1473. }
  1474. //---------------------------------------------------------------------------
  1475. bool __fastcall TRemoteFile::GetHaveFullFileName() const
  1476. {
  1477. return !FFullFileName.IsEmpty() || (Directory != NULL);
  1478. }
  1479. //---------------------------------------------------------------------------
  1480. Integer __fastcall TRemoteFile::GetAttr()
  1481. {
  1482. Integer Result = 0;
  1483. if (Rights->ReadOnly) Result |= faReadOnly;
  1484. if (IsHidden) Result |= faHidden;
  1485. return Result;
  1486. }
  1487. //---------------------------------------------------------------------------
  1488. void __fastcall TRemoteFile::SetTerminal(TTerminal * value)
  1489. {
  1490. FTerminal = value;
  1491. if (FLinkedFile)
  1492. {
  1493. FLinkedFile->Terminal = value;
  1494. }
  1495. }
  1496. //---------------------------------------------------------------------------
  1497. //---------------------------------------------------------------------------
  1498. __fastcall TRemoteDirectoryFile::TRemoteDirectoryFile() : TRemoteFile()
  1499. {
  1500. Modification = double(0);
  1501. ModificationFmt = mfNone;
  1502. LastAccess = Modification;
  1503. Type = L'D';
  1504. }
  1505. //---------------------------------------------------------------------------
  1506. //---------------------------------------------------------------------------
  1507. __fastcall TRemoteParentDirectory::TRemoteParentDirectory(TTerminal * ATerminal)
  1508. : TRemoteDirectoryFile()
  1509. {
  1510. FileName = PARENTDIRECTORY;
  1511. Terminal = ATerminal;
  1512. }
  1513. //=== TRemoteFileList ------------------------------------------------------
  1514. __fastcall TRemoteFileList::TRemoteFileList():
  1515. TObjectList()
  1516. {
  1517. FTimestamp = Now();
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. void __fastcall TRemoteFileList::AddFile(TRemoteFile * File)
  1521. {
  1522. Add(File);
  1523. File->Directory = this;
  1524. }
  1525. //---------------------------------------------------------------------------
  1526. TStrings * __fastcall TRemoteFileList::CloneStrings(TStrings * List)
  1527. {
  1528. std::unique_ptr<TStringList> Result(new TStringList());
  1529. Result->OwnsObjects = true;
  1530. for (int Index = 0; Index < List->Count; Index++)
  1531. {
  1532. TRemoteFile * File = static_cast<TRemoteFile *>(List->Objects[Index]);
  1533. Result->AddObject(List->Strings[Index], File->Duplicate(true));
  1534. }
  1535. return Result.release();
  1536. }
  1537. //---------------------------------------------------------------------------
  1538. bool TRemoteFileList::AnyDirectory(TStrings * List)
  1539. {
  1540. bool Result = false;
  1541. for (int Index = 0; !Result && (Index < List->Count); Index++)
  1542. {
  1543. Result = static_cast<TRemoteFile *>(List->Objects[Index])->IsDirectory;
  1544. }
  1545. return Result;
  1546. }
  1547. //---------------------------------------------------------------------------
  1548. void __fastcall TRemoteFileList::DuplicateTo(TRemoteFileList * Copy)
  1549. {
  1550. Copy->Reset();
  1551. for (int Index = 0; Index < Count; Index++)
  1552. {
  1553. TRemoteFile * File = Files[Index];
  1554. Copy->AddFile(File->Duplicate(false));
  1555. }
  1556. Copy->FDirectory = Directory;
  1557. Copy->FTimestamp = FTimestamp;
  1558. }
  1559. //---------------------------------------------------------------------------
  1560. void __fastcall TRemoteFileList::Reset()
  1561. {
  1562. FTimestamp = Now();
  1563. Clear();
  1564. }
  1565. //---------------------------------------------------------------------------
  1566. void __fastcall TRemoteFileList::SetDirectory(UnicodeString value)
  1567. {
  1568. FDirectory = UnixExcludeTrailingBackslash(value);
  1569. }
  1570. //---------------------------------------------------------------------------
  1571. UnicodeString __fastcall TRemoteFileList::GetFullDirectory()
  1572. {
  1573. return UnixIncludeTrailingBackslash(Directory);
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. TRemoteFile * __fastcall TRemoteFileList::GetFiles(Integer Index)
  1577. {
  1578. return (TRemoteFile *)Items[Index];
  1579. }
  1580. //---------------------------------------------------------------------------
  1581. Boolean __fastcall TRemoteFileList::GetIsRoot()
  1582. {
  1583. return (Directory == ROOTDIRECTORY);
  1584. }
  1585. //---------------------------------------------------------------------------
  1586. UnicodeString __fastcall TRemoteFileList::GetParentPath()
  1587. {
  1588. return UnixExtractFilePath(Directory);
  1589. }
  1590. //---------------------------------------------------------------------------
  1591. __int64 __fastcall TRemoteFileList::GetTotalSize()
  1592. {
  1593. __int64 Result = 0;
  1594. for (Integer Index = 0; Index < Count; Index++)
  1595. Result += Files[Index]->Size;
  1596. return Result;
  1597. }
  1598. //---------------------------------------------------------------------------
  1599. TRemoteFile * __fastcall TRemoteFileList::FindFile(const UnicodeString &FileName)
  1600. {
  1601. for (Integer Index = 0; Index < Count; Index++)
  1602. if (Files[Index]->FileName == FileName) return Files[Index];
  1603. return NULL;
  1604. }
  1605. //=== TRemoteDirectory ------------------------------------------------------
  1606. __fastcall TRemoteDirectory::TRemoteDirectory(TTerminal * aTerminal, TRemoteDirectory * Template) :
  1607. TRemoteFileList(), FTerminal(aTerminal)
  1608. {
  1609. FThisDirectory = NULL;
  1610. FParentDirectory = NULL;
  1611. if (Template == NULL)
  1612. {
  1613. FIncludeThisDirectory = false;
  1614. FIncludeParentDirectory = true;
  1615. }
  1616. else
  1617. {
  1618. FIncludeThisDirectory = Template->FIncludeThisDirectory;
  1619. FIncludeParentDirectory = Template->FIncludeParentDirectory;
  1620. }
  1621. }
  1622. //---------------------------------------------------------------------------
  1623. __fastcall TRemoteDirectory::~TRemoteDirectory()
  1624. {
  1625. ReleaseRelativeDirectories();
  1626. }
  1627. //---------------------------------------------------------------------------
  1628. void __fastcall TRemoteDirectory::ReleaseRelativeDirectories()
  1629. {
  1630. if ((ThisDirectory != NULL) && !IncludeThisDirectory)
  1631. {
  1632. delete FThisDirectory;
  1633. FThisDirectory = NULL;
  1634. }
  1635. if ((ParentDirectory != NULL) && !IncludeParentDirectory)
  1636. {
  1637. delete FParentDirectory;
  1638. FParentDirectory = NULL;
  1639. }
  1640. }
  1641. //---------------------------------------------------------------------------
  1642. void __fastcall TRemoteDirectory::Reset()
  1643. {
  1644. ReleaseRelativeDirectories();
  1645. TRemoteFileList::Reset();
  1646. }
  1647. //---------------------------------------------------------------------------
  1648. void __fastcall TRemoteDirectory::SetDirectory(UnicodeString value)
  1649. {
  1650. TRemoteFileList::SetDirectory(value);
  1651. }
  1652. //---------------------------------------------------------------------------
  1653. void __fastcall TRemoteDirectory::AddFile(TRemoteFile * File)
  1654. {
  1655. if (File->IsThisDirectory) FThisDirectory = File;
  1656. if (File->IsParentDirectory) FParentDirectory = File;
  1657. if ((!File->IsThisDirectory || IncludeThisDirectory) &&
  1658. (!File->IsParentDirectory || IncludeParentDirectory))
  1659. {
  1660. TRemoteFileList::AddFile(File);
  1661. }
  1662. File->Terminal = Terminal;
  1663. }
  1664. //---------------------------------------------------------------------------
  1665. void __fastcall TRemoteDirectory::DuplicateTo(TRemoteFileList * Copy)
  1666. {
  1667. TRemoteFileList::DuplicateTo(Copy);
  1668. if (ThisDirectory && !IncludeThisDirectory)
  1669. {
  1670. Copy->AddFile(ThisDirectory->Duplicate(false));
  1671. }
  1672. if (ParentDirectory && !IncludeParentDirectory)
  1673. {
  1674. Copy->AddFile(ParentDirectory->Duplicate(false));
  1675. }
  1676. }
  1677. //---------------------------------------------------------------------------
  1678. bool __fastcall TRemoteDirectory::GetLoaded()
  1679. {
  1680. return ((Terminal != NULL) && Terminal->Active && !Directory.IsEmpty());
  1681. }
  1682. //---------------------------------------------------------------------------
  1683. void __fastcall TRemoteDirectory::SetIncludeParentDirectory(Boolean value)
  1684. {
  1685. if (IncludeParentDirectory != value)
  1686. {
  1687. FIncludeParentDirectory = value;
  1688. if (value && ParentDirectory)
  1689. {
  1690. DebugAssert(IndexOf(ParentDirectory) < 0);
  1691. Add(ParentDirectory);
  1692. }
  1693. else if (!value && ParentDirectory)
  1694. {
  1695. DebugAssert(IndexOf(ParentDirectory) >= 0);
  1696. Extract(ParentDirectory);
  1697. }
  1698. }
  1699. }
  1700. //---------------------------------------------------------------------------
  1701. void __fastcall TRemoteDirectory::SetIncludeThisDirectory(Boolean value)
  1702. {
  1703. if (IncludeThisDirectory != value)
  1704. {
  1705. FIncludeThisDirectory = value;
  1706. if (value && ThisDirectory)
  1707. {
  1708. DebugAssert(IndexOf(ThisDirectory) < 0);
  1709. Add(ThisDirectory);
  1710. }
  1711. else if (!value && ThisDirectory)
  1712. {
  1713. DebugAssert(IndexOf(ThisDirectory) >= 0);
  1714. Extract(ThisDirectory);
  1715. }
  1716. }
  1717. }
  1718. //===========================================================================
  1719. __fastcall TRemoteDirectoryCache::TRemoteDirectoryCache(): TStringList()
  1720. {
  1721. FSection = new TCriticalSection();
  1722. Sorted = true;
  1723. Duplicates = Types::dupError;
  1724. CaseSensitive = true;
  1725. }
  1726. //---------------------------------------------------------------------------
  1727. __fastcall TRemoteDirectoryCache::~TRemoteDirectoryCache()
  1728. {
  1729. Clear();
  1730. delete FSection;
  1731. }
  1732. //---------------------------------------------------------------------------
  1733. void __fastcall TRemoteDirectoryCache::Clear()
  1734. {
  1735. TGuard Guard(FSection);
  1736. try
  1737. {
  1738. for (int Index = 0; Index < Count; Index++)
  1739. {
  1740. delete (TRemoteFileList *)Objects[Index];
  1741. Objects[Index] = NULL;
  1742. }
  1743. }
  1744. __finally
  1745. {
  1746. TStringList::Clear();
  1747. }
  1748. }
  1749. //---------------------------------------------------------------------------
  1750. bool __fastcall TRemoteDirectoryCache::GetIsEmpty() const
  1751. {
  1752. TGuard Guard(FSection);
  1753. return (const_cast<TRemoteDirectoryCache*>(this)->Count == 0);
  1754. }
  1755. //---------------------------------------------------------------------------
  1756. bool __fastcall TRemoteDirectoryCache::HasFileList(const UnicodeString Directory)
  1757. {
  1758. TGuard Guard(FSection);
  1759. int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));
  1760. return (Index >= 0);
  1761. }
  1762. //---------------------------------------------------------------------------
  1763. bool __fastcall TRemoteDirectoryCache::HasNewerFileList(const UnicodeString Directory,
  1764. TDateTime Timestamp)
  1765. {
  1766. TGuard Guard(FSection);
  1767. int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));
  1768. if (Index >= 0)
  1769. {
  1770. TRemoteFileList * FileList = dynamic_cast<TRemoteFileList *>(Objects[Index]);
  1771. if (FileList->Timestamp <= Timestamp)
  1772. {
  1773. Index = -1;
  1774. }
  1775. }
  1776. return (Index >= 0);
  1777. }
  1778. //---------------------------------------------------------------------------
  1779. bool __fastcall TRemoteDirectoryCache::GetFileList(const UnicodeString Directory,
  1780. TRemoteFileList * FileList)
  1781. {
  1782. TGuard Guard(FSection);
  1783. int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));
  1784. bool Result = (Index >= 0);
  1785. if (Result)
  1786. {
  1787. DebugAssert(Objects[Index] != NULL);
  1788. dynamic_cast<TRemoteFileList *>(Objects[Index])->DuplicateTo(FileList);
  1789. }
  1790. return Result;
  1791. }
  1792. //---------------------------------------------------------------------------
  1793. void __fastcall TRemoteDirectoryCache::AddFileList(TRemoteFileList * FileList)
  1794. {
  1795. DebugAssert(FileList);
  1796. TRemoteFileList * Copy = new TRemoteFileList();
  1797. FileList->DuplicateTo(Copy);
  1798. {
  1799. TGuard Guard(FSection);
  1800. // file list cannot be cached already with only one thread, but it can be
  1801. // when directory is loaded by secondary terminal
  1802. DoClearFileList(FileList->Directory, false);
  1803. AddObject(Copy->Directory, Copy);
  1804. }
  1805. }
  1806. //---------------------------------------------------------------------------
  1807. void __fastcall TRemoteDirectoryCache::ClearFileList(UnicodeString Directory, bool SubDirs)
  1808. {
  1809. TGuard Guard(FSection);
  1810. DoClearFileList(Directory, SubDirs);
  1811. }
  1812. //---------------------------------------------------------------------------
  1813. void __fastcall TRemoteDirectoryCache::DoClearFileList(UnicodeString Directory, bool SubDirs)
  1814. {
  1815. Directory = UnixExcludeTrailingBackslash(Directory);
  1816. int Index = IndexOf(Directory);
  1817. if (Index >= 0)
  1818. {
  1819. Delete(Index);
  1820. }
  1821. if (SubDirs)
  1822. {
  1823. UnicodeString DirectoryWithSlash = UnixIncludeTrailingBackslash(Directory); // optimization
  1824. Index = Count-1;
  1825. while (Index >= 0)
  1826. {
  1827. if (UnixIsChildPath(DirectoryWithSlash, Strings[Index]))
  1828. {
  1829. Delete(Index);
  1830. }
  1831. Index--;
  1832. }
  1833. }
  1834. }
  1835. //---------------------------------------------------------------------------
  1836. void __fastcall TRemoteDirectoryCache::Delete(int Index)
  1837. {
  1838. delete dynamic_cast<TRemoteFileList *>(Objects[Index]);
  1839. TStringList::Delete(Index);
  1840. }
  1841. //---------------------------------------------------------------------------
  1842. //---------------------------------------------------------------------------
  1843. __fastcall TRemoteDirectoryChangesCache::TRemoteDirectoryChangesCache(int MaxSize) :
  1844. TStringList(),
  1845. FMaxSize(MaxSize)
  1846. {
  1847. CaseSensitive = true;
  1848. }
  1849. //---------------------------------------------------------------------------
  1850. void __fastcall TRemoteDirectoryChangesCache::Clear()
  1851. {
  1852. TStringList::Clear();
  1853. }
  1854. //---------------------------------------------------------------------------
  1855. bool __fastcall TRemoteDirectoryChangesCache::GetIsEmpty() const
  1856. {
  1857. return (const_cast<TRemoteDirectoryChangesCache*>(this)->Count == 0);
  1858. }
  1859. //---------------------------------------------------------------------------
  1860. void __fastcall TRemoteDirectoryChangesCache::SetValue(const UnicodeString & Name,
  1861. const UnicodeString & Value)
  1862. {
  1863. int Index = IndexOfName(Name);
  1864. if (Index > 0)
  1865. {
  1866. Delete(Index);
  1867. }
  1868. Values[Name] = Value;
  1869. }
  1870. //---------------------------------------------------------------------------
  1871. UnicodeString __fastcall TRemoteDirectoryChangesCache::GetValue(const UnicodeString & Name)
  1872. {
  1873. UnicodeString Value = Values[Name];
  1874. SetValue(Name, Value);
  1875. return Value;
  1876. }
  1877. //---------------------------------------------------------------------------
  1878. void __fastcall TRemoteDirectoryChangesCache::AddDirectoryChange(
  1879. const UnicodeString SourceDir, const UnicodeString Change,
  1880. const UnicodeString TargetDir)
  1881. {
  1882. DebugAssert(!TargetDir.IsEmpty());
  1883. SetValue(TargetDir, L"//");
  1884. if (TTerminal::ExpandFileName(Change, SourceDir) != TargetDir)
  1885. {
  1886. UnicodeString Key;
  1887. if (DirectoryChangeKey(SourceDir, Change, Key))
  1888. {
  1889. SetValue(Key, TargetDir);
  1890. }
  1891. }
  1892. }
  1893. //---------------------------------------------------------------------------
  1894. void __fastcall TRemoteDirectoryChangesCache::ClearDirectoryChange(
  1895. UnicodeString SourceDir)
  1896. {
  1897. for (int Index = 0; Index < Count; Index++)
  1898. {
  1899. if (Names[Index].SubString(1, SourceDir.Length()) == SourceDir)
  1900. {
  1901. Delete(Index);
  1902. Index--;
  1903. }
  1904. }
  1905. }
  1906. //---------------------------------------------------------------------------
  1907. void __fastcall TRemoteDirectoryChangesCache::ClearDirectoryChangeTarget(
  1908. UnicodeString TargetDir)
  1909. {
  1910. UnicodeString Key;
  1911. // hack to clear at least local sym-link change in case symlink is deleted
  1912. DirectoryChangeKey(UnixExcludeTrailingBackslash(UnixExtractFilePath(TargetDir)),
  1913. UnixExtractFileName(TargetDir), Key);
  1914. for (int Index = 0; Index < Count; Index++)
  1915. {
  1916. UnicodeString Name = Names[Index];
  1917. if ((Name.SubString(1, TargetDir.Length()) == TargetDir) ||
  1918. (Values[Name].SubString(1, TargetDir.Length()) == TargetDir) ||
  1919. (!Key.IsEmpty() && (Name == Key)))
  1920. {
  1921. Delete(Index);
  1922. Index--;
  1923. }
  1924. }
  1925. }
  1926. //---------------------------------------------------------------------------
  1927. bool __fastcall TRemoteDirectoryChangesCache::GetDirectoryChange(
  1928. const UnicodeString SourceDir, const UnicodeString Change, UnicodeString & TargetDir)
  1929. {
  1930. UnicodeString Key;
  1931. bool Result;
  1932. Key = TTerminal::ExpandFileName(Change, SourceDir);
  1933. Result = (IndexOfName(Key) >= 0);
  1934. if (Result)
  1935. {
  1936. TargetDir = GetValue(Key);
  1937. // TargetDir is not "//" here only when Change is full path to symbolic link
  1938. if (TargetDir == L"//")
  1939. {
  1940. TargetDir = Key;
  1941. }
  1942. }
  1943. else
  1944. {
  1945. Result = DirectoryChangeKey(SourceDir, Change, Key);
  1946. if (Result)
  1947. {
  1948. UnicodeString Directory = GetValue(Key);
  1949. Result = !Directory.IsEmpty();
  1950. if (Result)
  1951. {
  1952. TargetDir = Directory;
  1953. }
  1954. }
  1955. }
  1956. return Result;
  1957. }
  1958. //---------------------------------------------------------------------------
  1959. void __fastcall TRemoteDirectoryChangesCache::Serialize(UnicodeString & Data)
  1960. {
  1961. Data = L"A";
  1962. int ACount = Count;
  1963. if (ACount > FMaxSize)
  1964. {
  1965. TStrings * Limited = new TStringList();
  1966. try
  1967. {
  1968. int Index = ACount - FMaxSize;
  1969. while (Index < ACount)
  1970. {
  1971. Limited->Add(Strings[Index]);
  1972. Index++;
  1973. }
  1974. Data += Limited->Text;
  1975. }
  1976. __finally
  1977. {
  1978. delete Limited;
  1979. }
  1980. }
  1981. else
  1982. {
  1983. Data += Text;
  1984. }
  1985. }
  1986. //---------------------------------------------------------------------------
  1987. void __fastcall TRemoteDirectoryChangesCache::Deserialize(const UnicodeString Data)
  1988. {
  1989. if (Data.IsEmpty())
  1990. {
  1991. Text = L"";
  1992. }
  1993. else
  1994. {
  1995. Text = Data.c_str() + 1;
  1996. }
  1997. }
  1998. //---------------------------------------------------------------------------
  1999. bool __fastcall TRemoteDirectoryChangesCache::DirectoryChangeKey(
  2000. const UnicodeString SourceDir, const UnicodeString Change, UnicodeString & Key)
  2001. {
  2002. bool Result = !Change.IsEmpty();
  2003. if (Result)
  2004. {
  2005. bool Absolute = UnixIsAbsolutePath(Change);
  2006. Result = !SourceDir.IsEmpty() || Absolute;
  2007. if (Result)
  2008. {
  2009. // expanded from ?: to avoid memory leaks
  2010. if (Absolute)
  2011. {
  2012. Key = Change;
  2013. }
  2014. else
  2015. {
  2016. Key = SourceDir + L"," + Change;
  2017. }
  2018. }
  2019. }
  2020. return Result;
  2021. }
  2022. //=== TRights ---------------------------------------------------------------
  2023. const wchar_t TRights::BasicSymbols[] = L"rwxrwxrwx";
  2024. const wchar_t TRights::CombinedSymbols[] = L"--s--s--t";
  2025. const wchar_t TRights::ExtendedSymbols[] = L"--S--S--T";
  2026. const wchar_t TRights::ModeGroups[] = L"ugo";
  2027. //---------------------------------------------------------------------------
  2028. __fastcall TRights::TRights()
  2029. {
  2030. FAllowUndef = false;
  2031. FSet = 0;
  2032. FUnset = 0;
  2033. Number = 0;
  2034. FUnknown = true;
  2035. }
  2036. //---------------------------------------------------------------------------
  2037. __fastcall TRights::TRights(unsigned short ANumber)
  2038. {
  2039. FAllowUndef = false;
  2040. FSet = 0;
  2041. FUnset = 0;
  2042. Number = ANumber;
  2043. }
  2044. //---------------------------------------------------------------------------
  2045. __fastcall TRights::TRights(const TRights & Source)
  2046. {
  2047. Assign(&Source);
  2048. }
  2049. //---------------------------------------------------------------------------
  2050. //---------------------------------------------------------------------------
  2051. void __fastcall TRights::Assign(const TRights * Source)
  2052. {
  2053. FAllowUndef = Source->AllowUndef;
  2054. FSet = Source->FSet;
  2055. FUnset = Source->FUnset;
  2056. FText = Source->FText;
  2057. FUnknown = Source->FUnknown;
  2058. }
  2059. //---------------------------------------------------------------------------
  2060. TRights::TRight TRights::CalculateRight(TRightGroup Group, TRightLevel Level)
  2061. {
  2062. int Result;
  2063. if (Level == rlSpecial)
  2064. {
  2065. Result = rrUserIDExec + Group;
  2066. }
  2067. else
  2068. {
  2069. DebugAssert(rlRead == 0);
  2070. Result = rrUserRead + Level + (Group * 3);
  2071. }
  2072. return static_cast<TRight>(Result);
  2073. }
  2074. //---------------------------------------------------------------------------
  2075. TRights::TFlag __fastcall TRights::RightToFlag(TRights::TRight Right)
  2076. {
  2077. return static_cast<TFlag>(1 << (rrLast - Right));
  2078. }
  2079. //---------------------------------------------------------------------------
  2080. TRights::TFlag TRights::CalculateFlag(TRightGroup Group, TRightLevel Level)
  2081. {
  2082. return RightToFlag(CalculateRight(Group, Level));
  2083. }
  2084. //---------------------------------------------------------------------------
  2085. unsigned short TRights::CalculatePermissions(TRightGroup Group, TRightLevel Level, TRightLevel Level2, TRightLevel Level3)
  2086. {
  2087. unsigned int Permissions = CalculateFlag(Group, Level);
  2088. if (Level2 != rlNone)
  2089. {
  2090. Permissions |= CalculateFlag(Group, Level2);
  2091. }
  2092. if (Level3 != rlNone)
  2093. {
  2094. Permissions |= CalculateFlag(Group, Level3);
  2095. }
  2096. unsigned short Result = static_cast<unsigned short>(Permissions);
  2097. DebugAssert((Permissions - Result) == 0);
  2098. return Result;
  2099. }
  2100. //---------------------------------------------------------------------------
  2101. bool __fastcall TRights::operator ==(const TRights & rhr) const
  2102. {
  2103. if (AllowUndef || rhr.AllowUndef)
  2104. {
  2105. for (int Right = rrFirst; Right <= rrLast; Right++)
  2106. {
  2107. if (RightUndef[static_cast<TRight>(Right)] !=
  2108. rhr.RightUndef[static_cast<TRight>(Right)])
  2109. {
  2110. return false;
  2111. }
  2112. }
  2113. return true;
  2114. }
  2115. else
  2116. {
  2117. return (Number == rhr.Number);
  2118. }
  2119. }
  2120. //---------------------------------------------------------------------------
  2121. bool __fastcall TRights::operator ==(unsigned short rhr) const
  2122. {
  2123. return (Number == rhr);
  2124. }
  2125. //---------------------------------------------------------------------------
  2126. bool __fastcall TRights::operator !=(const TRights & rhr) const
  2127. {
  2128. return !(*this == rhr);
  2129. }
  2130. //---------------------------------------------------------------------------
  2131. TRights & __fastcall TRights::operator =(unsigned short rhr)
  2132. {
  2133. Number = rhr;
  2134. return *this;
  2135. }
  2136. //---------------------------------------------------------------------------
  2137. TRights & __fastcall TRights::operator =(const TRights & rhr)
  2138. {
  2139. Assign(&rhr);
  2140. return *this;
  2141. }
  2142. //---------------------------------------------------------------------------
  2143. TRights __fastcall TRights::operator ~() const
  2144. {
  2145. TRights Result(static_cast<unsigned short>(~Number));
  2146. return Result;
  2147. }
  2148. //---------------------------------------------------------------------------
  2149. TRights __fastcall TRights::operator &(const TRights & rhr) const
  2150. {
  2151. TRights Result(*this);
  2152. Result &= rhr;
  2153. return Result;
  2154. }
  2155. //---------------------------------------------------------------------------
  2156. TRights __fastcall TRights::operator &(unsigned short rhr) const
  2157. {
  2158. TRights Result(*this);
  2159. Result &= rhr;
  2160. return Result;
  2161. }
  2162. //---------------------------------------------------------------------------
  2163. TRights & __fastcall TRights::operator &=(const TRights & rhr)
  2164. {
  2165. if (AllowUndef || rhr.AllowUndef)
  2166. {
  2167. for (int Right = rrFirst; Right <= rrLast; Right++)
  2168. {
  2169. if (RightUndef[static_cast<TRight>(Right)] !=
  2170. rhr.RightUndef[static_cast<TRight>(Right)])
  2171. {
  2172. RightUndef[static_cast<TRight>(Right)] = rsUndef;
  2173. }
  2174. }
  2175. }
  2176. else
  2177. {
  2178. Number &= rhr.Number;
  2179. }
  2180. return *this;
  2181. }
  2182. //---------------------------------------------------------------------------
  2183. TRights & __fastcall TRights::operator &=(unsigned short rhr)
  2184. {
  2185. Number &= rhr;
  2186. return *this;
  2187. }
  2188. //---------------------------------------------------------------------------
  2189. TRights __fastcall TRights::operator |(const TRights & rhr) const
  2190. {
  2191. TRights Result(*this);
  2192. Result |= rhr;
  2193. return Result;
  2194. }
  2195. //---------------------------------------------------------------------------
  2196. TRights __fastcall TRights::operator |(unsigned short rhr) const
  2197. {
  2198. TRights Result(*this);
  2199. Result |= rhr;
  2200. return Result;
  2201. }
  2202. //---------------------------------------------------------------------------
  2203. TRights & __fastcall TRights::operator |=(const TRights & rhr)
  2204. {
  2205. Number |= rhr.Number;
  2206. return *this;
  2207. }
  2208. //---------------------------------------------------------------------------
  2209. TRights & __fastcall TRights::operator |=(unsigned short rhr)
  2210. {
  2211. Number |= rhr;
  2212. return *this;
  2213. }
  2214. //---------------------------------------------------------------------------
  2215. void __fastcall TRights::SetAllowUndef(bool value)
  2216. {
  2217. if (FAllowUndef != value)
  2218. {
  2219. DebugAssert(!value || ((FSet | FUnset) == rfAllSpecials));
  2220. FAllowUndef = value;
  2221. }
  2222. }
  2223. //---------------------------------------------------------------------------
  2224. void __fastcall TRights::SetText(const UnicodeString & value)
  2225. {
  2226. if (value != Text)
  2227. {
  2228. if ((value.Length() != TextLen) ||
  2229. (!AllowUndef && (value.Pos(UndefSymbol) > 0)) ||
  2230. (value.Pos(L" ") > 0))
  2231. {
  2232. throw Exception(FMTLOAD(RIGHTS_ERROR, (value)));
  2233. }
  2234. FSet = 0;
  2235. FUnset = 0;
  2236. int Flag = 00001;
  2237. int ExtendedFlag = 01000;
  2238. bool KeepText = false;
  2239. for (int i = TextLen; i >= 1; i--)
  2240. {
  2241. if ((value[i] == UnsetSymbol) || (value[i] == UnsetSymbolWin))
  2242. {
  2243. FUnset |= static_cast<unsigned short>(Flag | ExtendedFlag);
  2244. }
  2245. else if (value[i] == UndefSymbol)
  2246. {
  2247. // do nothing
  2248. }
  2249. else if (value[i] == CombinedSymbols[i - 1])
  2250. {
  2251. FSet |= static_cast<unsigned short>(Flag | ExtendedFlag);
  2252. }
  2253. else if (value[i] == ExtendedSymbols[i - 1])
  2254. {
  2255. FSet |= static_cast<unsigned short>(ExtendedFlag);
  2256. FUnset |= static_cast<unsigned short>(Flag);
  2257. }
  2258. else
  2259. {
  2260. if (value[i] != BasicSymbols[i - 1])
  2261. {
  2262. KeepText = true;
  2263. }
  2264. FSet |= static_cast<unsigned short>(Flag);
  2265. if (i % 3 == 0)
  2266. {
  2267. FUnset |= static_cast<unsigned short>(ExtendedFlag);
  2268. }
  2269. }
  2270. Flag <<= 1;
  2271. if (i % 3 == 1)
  2272. {
  2273. ExtendedFlag <<= 1;
  2274. }
  2275. }
  2276. FText = KeepText ? value : UnicodeString();
  2277. }
  2278. FUnknown = false;
  2279. }
  2280. //---------------------------------------------------------------------------
  2281. void TRights::SetTextOverride(const UnicodeString & value)
  2282. {
  2283. if (FText != value)
  2284. {
  2285. FText = value;
  2286. FUnknown = false;
  2287. }
  2288. }
  2289. //---------------------------------------------------------------------------
  2290. UnicodeString __fastcall TRights::GetText() const
  2291. {
  2292. if (!FText.IsEmpty())
  2293. {
  2294. return FText;
  2295. }
  2296. else
  2297. {
  2298. UnicodeString Result;
  2299. Result.SetLength(TextLen);
  2300. int Flag = 00001;
  2301. int ExtendedFlag = 01000;
  2302. bool ExtendedPos = true;
  2303. wchar_t Symbol;
  2304. int i = TextLen;
  2305. while (i >= 1)
  2306. {
  2307. if (ExtendedPos &&
  2308. ((FSet & (Flag | ExtendedFlag)) == (Flag | ExtendedFlag)))
  2309. {
  2310. Symbol = CombinedSymbols[i - 1];
  2311. }
  2312. else if ((FSet & Flag) != 0)
  2313. {
  2314. Symbol = BasicSymbols[i - 1];
  2315. }
  2316. else if (ExtendedPos && ((FSet & ExtendedFlag) != 0))
  2317. {
  2318. Symbol = ExtendedSymbols[i - 1];
  2319. }
  2320. else if ((!ExtendedPos && ((FUnset & Flag) == Flag)) ||
  2321. (ExtendedPos && ((FUnset & (Flag | ExtendedFlag)) == (Flag | ExtendedFlag))))
  2322. {
  2323. Symbol = UnsetSymbol;
  2324. }
  2325. else
  2326. {
  2327. Symbol = UndefSymbol;
  2328. }
  2329. Result[i] = Symbol;
  2330. Flag <<= 1;
  2331. i--;
  2332. ExtendedPos = ((i % 3) == 0);
  2333. if (ExtendedPos)
  2334. {
  2335. ExtendedFlag <<= 1;
  2336. }
  2337. }
  2338. return Result;
  2339. }
  2340. }
  2341. //---------------------------------------------------------------------------
  2342. void __fastcall TRights::SetOctal(UnicodeString value)
  2343. {
  2344. UnicodeString AValue(value);
  2345. if (AValue.Length() == 3)
  2346. {
  2347. AValue = L"0" + AValue;
  2348. }
  2349. if (Octal != AValue)
  2350. {
  2351. bool Correct = (AValue.Length() == 4);
  2352. if (Correct)
  2353. {
  2354. for (int i = 1; (i <= AValue.Length()) && Correct; i++)
  2355. {
  2356. Correct = (AValue[i] >= L'0') && (AValue[i] <= L'7');
  2357. }
  2358. }
  2359. if (!Correct)
  2360. {
  2361. throw Exception(FMTLOAD(INVALID_OCTAL_PERMISSIONS, (value)));
  2362. }
  2363. Number = static_cast<unsigned short>(
  2364. ((AValue[1] - L'0') << 9) +
  2365. ((AValue[2] - L'0') << 6) +
  2366. ((AValue[3] - L'0') << 3) +
  2367. ((AValue[4] - L'0') << 0));
  2368. FText = L"";
  2369. }
  2370. FUnknown = false;
  2371. }
  2372. //---------------------------------------------------------------------------
  2373. unsigned long __fastcall TRights::GetNumberDecadic() const
  2374. {
  2375. unsigned long N = NumberSet; // used to be "Number"
  2376. unsigned long Result =
  2377. ((N & 07000) / 01000 * 1000) +
  2378. ((N & 00700) / 0100 * 100) +
  2379. ((N & 00070) / 010 * 10) +
  2380. ((N & 00007) / 01 * 1);
  2381. return Result;
  2382. }
  2383. //---------------------------------------------------------------------------
  2384. UnicodeString __fastcall TRights::GetOctal() const
  2385. {
  2386. UnicodeString Result;
  2387. unsigned short N = NumberSet; // used to be "Number"
  2388. Result.SetLength(4);
  2389. Result[1] = static_cast<wchar_t>(L'0' + ((N & 07000) >> 9));
  2390. Result[2] = static_cast<wchar_t>(L'0' + ((N & 00700) >> 6));
  2391. Result[3] = static_cast<wchar_t>(L'0' + ((N & 00070) >> 3));
  2392. Result[4] = static_cast<wchar_t>(L'0' + ((N & 00007) >> 0));
  2393. return Result;
  2394. }
  2395. //---------------------------------------------------------------------------
  2396. void __fastcall TRights::SetNumber(unsigned short value)
  2397. {
  2398. if ((FSet != value) || ((FSet | FUnset) != rfAllSpecials))
  2399. {
  2400. FSet = value;
  2401. FUnset = static_cast<unsigned short>(rfAllSpecials & ~FSet);
  2402. FText = L"";
  2403. }
  2404. FUnknown = false;
  2405. }
  2406. //---------------------------------------------------------------------------
  2407. unsigned short __fastcall TRights::GetNumber() const
  2408. {
  2409. DebugAssert(!IsUndef);
  2410. return FSet;
  2411. }
  2412. //---------------------------------------------------------------------------
  2413. void __fastcall TRights::SetRight(TRight Right, bool value)
  2414. {
  2415. RightUndef[Right] = (value ? rsYes : rsNo);
  2416. }
  2417. //---------------------------------------------------------------------------
  2418. bool __fastcall TRights::GetRight(TRight Right) const
  2419. {
  2420. TState State = RightUndef[Right];
  2421. DebugAssert(State != rsUndef);
  2422. return (State == rsYes);
  2423. }
  2424. //---------------------------------------------------------------------------
  2425. void __fastcall TRights::SetRightUndef(TRight Right, TState value)
  2426. {
  2427. if (value != RightUndef[Right])
  2428. {
  2429. DebugAssert((value != rsUndef) || AllowUndef);
  2430. TFlag Flag = RightToFlag(Right);
  2431. switch (value)
  2432. {
  2433. case rsYes:
  2434. FSet |= static_cast<unsigned short>(Flag);
  2435. FUnset &= static_cast<unsigned short>(~Flag);
  2436. break;
  2437. case rsNo:
  2438. FSet &= static_cast<unsigned short>(~Flag);
  2439. FUnset |= static_cast<unsigned short>(Flag);
  2440. break;
  2441. case rsUndef:
  2442. default:
  2443. FSet &= static_cast<unsigned short>(~Flag);
  2444. FUnset &= static_cast<unsigned short>(~Flag);
  2445. break;
  2446. }
  2447. FText = L"";
  2448. }
  2449. FUnknown = false;
  2450. }
  2451. //---------------------------------------------------------------------------
  2452. TRights::TState __fastcall TRights::GetRightUndef(TRight Right) const
  2453. {
  2454. TFlag Flag = RightToFlag(Right);
  2455. TState Result;
  2456. if ((FSet & Flag) != 0)
  2457. {
  2458. Result = rsYes;
  2459. }
  2460. else if ((FUnset & Flag) != 0)
  2461. {
  2462. Result = rsNo;
  2463. }
  2464. else
  2465. {
  2466. Result = rsUndef;
  2467. }
  2468. return Result;
  2469. }
  2470. //---------------------------------------------------------------------------
  2471. void __fastcall TRights::SetReadOnly(bool value)
  2472. {
  2473. Right[rrUserWrite] = !value;
  2474. Right[rrGroupWrite] = !value;
  2475. Right[rrOtherWrite] = !value;
  2476. }
  2477. //---------------------------------------------------------------------------
  2478. bool __fastcall TRights::GetReadOnly()
  2479. {
  2480. return Right[rrUserWrite] && Right[rrGroupWrite] && Right[rrOtherWrite];
  2481. }
  2482. //---------------------------------------------------------------------------
  2483. UnicodeString __fastcall TRights::GetChmodStr(int Directory) const
  2484. {
  2485. UnicodeString Result;
  2486. if (IsUndef)
  2487. {
  2488. Result = ModeStr;
  2489. }
  2490. else
  2491. {
  2492. Result = Octal;
  2493. if (Directory != 0) // unknown or folder
  2494. {
  2495. // New versions of coreutils need leading 5th zero to unset UID/GID for directories
  2496. Result = UnicodeString(L"0") + Result;
  2497. }
  2498. }
  2499. return Result;
  2500. }
  2501. //---------------------------------------------------------------------------
  2502. UnicodeString __fastcall TRights::GetModeStr() const
  2503. {
  2504. UnicodeString Result;
  2505. UnicodeString SetModeStr, UnsetModeStr;
  2506. TRight Right;
  2507. int Index;
  2508. for (int Group = 0; Group < 3; Group++)
  2509. {
  2510. SetModeStr = L"";
  2511. UnsetModeStr = L"";
  2512. for (int Mode = 0; Mode < 3; Mode++)
  2513. {
  2514. Index = (Group * 3) + Mode;
  2515. Right = static_cast<TRight>(rrUserRead + Index);
  2516. switch (RightUndef[Right])
  2517. {
  2518. case rsYes:
  2519. SetModeStr += BasicSymbols[Index];
  2520. break;
  2521. case rsNo:
  2522. UnsetModeStr += BasicSymbols[Index];
  2523. break;
  2524. }
  2525. }
  2526. Right = static_cast<TRight>(rrUserIDExec + Group);
  2527. Index = (Group * 3) + 2;
  2528. switch (RightUndef[Right])
  2529. {
  2530. case rsYes:
  2531. SetModeStr += CombinedSymbols[Index];
  2532. break;
  2533. case rsNo:
  2534. UnsetModeStr += CombinedSymbols[Index];
  2535. break;
  2536. }
  2537. if (!SetModeStr.IsEmpty() || !UnsetModeStr.IsEmpty())
  2538. {
  2539. if (!Result.IsEmpty())
  2540. {
  2541. Result += L',';
  2542. }
  2543. Result += ModeGroups[Group];
  2544. if (!SetModeStr.IsEmpty())
  2545. {
  2546. Result += L"+" + SetModeStr;
  2547. }
  2548. if (!UnsetModeStr.IsEmpty())
  2549. {
  2550. Result += L"-" + UnsetModeStr;
  2551. }
  2552. }
  2553. }
  2554. return Result;
  2555. }
  2556. //---------------------------------------------------------------------------
  2557. void __fastcall TRights::AddExecute()
  2558. {
  2559. for (int Group = 0; Group < 3; Group++)
  2560. {
  2561. if ((RightUndef[static_cast<TRight>(rrUserRead + (Group * 3))] == rsYes) ||
  2562. (RightUndef[static_cast<TRight>(rrUserWrite + (Group * 3))] == rsYes))
  2563. {
  2564. Right[static_cast<TRight>(rrUserExec + (Group * 3))] = true;
  2565. FText = L"";
  2566. }
  2567. }
  2568. FUnknown = false;
  2569. }
  2570. //---------------------------------------------------------------------------
  2571. void __fastcall TRights::AllUndef()
  2572. {
  2573. if ((FSet != 0) || (FUnset != 0))
  2574. {
  2575. FSet = 0;
  2576. FUnset = 0;
  2577. FText = L"";
  2578. }
  2579. FUnknown = false;
  2580. }
  2581. //---------------------------------------------------------------------------
  2582. bool __fastcall TRights::GetIsUndef() const
  2583. {
  2584. return ((FSet | FUnset) != rfAllSpecials);
  2585. }
  2586. //---------------------------------------------------------------------------
  2587. __fastcall TRights::operator unsigned short() const
  2588. {
  2589. return Number;
  2590. }
  2591. //---------------------------------------------------------------------------
  2592. __fastcall TRights::operator unsigned long() const
  2593. {
  2594. return Number;
  2595. }
  2596. //---------------------------------------------------------------------------
  2597. TRights TRights::Combine(const TRights & Other) const
  2598. {
  2599. TRights Result = (*this);
  2600. Result |= Other.NumberSet;
  2601. Result &= (unsigned short)~Other.NumberUnset;
  2602. return Result;
  2603. }
  2604. //=== TRemoteProperties -------------------------------------------------------
  2605. __fastcall TRemoteProperties::TRemoteProperties()
  2606. {
  2607. Default();
  2608. }
  2609. //---------------------------------------------------------------------------
  2610. __fastcall TRemoteProperties::TRemoteProperties(const TRemoteProperties & rhp) :
  2611. Valid(rhp.Valid),
  2612. Recursive(rhp.Recursive),
  2613. Rights(rhp.Rights),
  2614. AddXToDirectories(rhp.AddXToDirectories),
  2615. Group(rhp.Group),
  2616. Owner(rhp.Owner),
  2617. Modification(rhp.Modification),
  2618. LastAccess(rhp.Modification),
  2619. Encrypt(rhp.Encrypt)
  2620. {
  2621. }
  2622. //---------------------------------------------------------------------------
  2623. void __fastcall TRemoteProperties::Default()
  2624. {
  2625. Valid.Clear();
  2626. AddXToDirectories = false;
  2627. Rights.AllowUndef = false;
  2628. Rights.Number = 0;
  2629. Group.Clear();
  2630. Owner.Clear();
  2631. Recursive = false;
  2632. Encrypt = false;
  2633. }
  2634. //---------------------------------------------------------------------------
  2635. bool __fastcall TRemoteProperties::operator ==(const TRemoteProperties & rhp) const
  2636. {
  2637. bool Result = (Valid == rhp.Valid && Recursive == rhp.Recursive);
  2638. if (Result)
  2639. {
  2640. if ((Valid.Contains(vpRights) &&
  2641. (Rights != rhp.Rights || AddXToDirectories != rhp.AddXToDirectories)) ||
  2642. (Valid.Contains(vpOwner) && (Owner != rhp.Owner)) ||
  2643. (Valid.Contains(vpGroup) && (Group != rhp.Group)) ||
  2644. (Valid.Contains(vpModification) && (Modification != rhp.Modification)) ||
  2645. (Valid.Contains(vpLastAccess) && (LastAccess != rhp.LastAccess)) ||
  2646. (Valid.Contains(vpEncrypt) && (Encrypt != rhp.Encrypt)))
  2647. {
  2648. Result = false;
  2649. }
  2650. }
  2651. return Result;
  2652. }
  2653. //---------------------------------------------------------------------------
  2654. bool __fastcall TRemoteProperties::operator !=(const TRemoteProperties & rhp) const
  2655. {
  2656. return !(*this == rhp);
  2657. }
  2658. //---------------------------------------------------------------------------
  2659. TRemoteProperties __fastcall TRemoteProperties::CommonProperties(TStrings * FileList)
  2660. {
  2661. // TODO: Modification and LastAccess
  2662. TRemoteProperties CommonProperties;
  2663. for (int Index = 0; Index < FileList->Count; Index++)
  2664. {
  2665. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  2666. DebugAssert(File);
  2667. if (!Index)
  2668. {
  2669. if (!File->Rights->Unknown)
  2670. {
  2671. CommonProperties.Rights = *(File->Rights);
  2672. // previously we allowed undef implicitly for directories,
  2673. // now we do it explicitly in properties dialog and only in combination
  2674. // with "recursive" option
  2675. CommonProperties.Rights.AllowUndef = File->Rights->IsUndef;
  2676. CommonProperties.Valid << vpRights;
  2677. }
  2678. if (File->Owner.IsSet)
  2679. {
  2680. CommonProperties.Owner = File->Owner;
  2681. CommonProperties.Valid << vpOwner;
  2682. }
  2683. if (File->Group.IsSet)
  2684. {
  2685. CommonProperties.Group = File->Group;
  2686. CommonProperties.Valid << vpGroup;
  2687. }
  2688. }
  2689. else
  2690. {
  2691. CommonProperties.Rights.AllowUndef = True;
  2692. CommonProperties.Rights &= *File->Rights;
  2693. if (CommonProperties.Owner != File->Owner)
  2694. {
  2695. CommonProperties.Owner.Clear();
  2696. CommonProperties.Valid >> vpOwner;
  2697. }
  2698. if (CommonProperties.Group != File->Group)
  2699. {
  2700. CommonProperties.Group.Clear();
  2701. CommonProperties.Valid >> vpGroup;
  2702. }
  2703. }
  2704. }
  2705. return CommonProperties;
  2706. }
  2707. //---------------------------------------------------------------------------
  2708. TRemoteProperties __fastcall TRemoteProperties::ChangedProperties(
  2709. const TRemoteProperties & OriginalProperties, TRemoteProperties NewProperties)
  2710. {
  2711. DebugAssert(!OriginalProperties.Valid.Contains(vpEncrypt));
  2712. // TODO: Modification and LastAccess
  2713. if (!NewProperties.Recursive)
  2714. {
  2715. if (NewProperties.Rights == OriginalProperties.Rights &&
  2716. !NewProperties.AddXToDirectories)
  2717. {
  2718. NewProperties.Valid >> vpRights;
  2719. }
  2720. if (NewProperties.Group == OriginalProperties.Group)
  2721. {
  2722. NewProperties.Valid >> vpGroup;
  2723. }
  2724. if (NewProperties.Owner == OriginalProperties.Owner)
  2725. {
  2726. NewProperties.Valid >> vpOwner;
  2727. }
  2728. }
  2729. return NewProperties;
  2730. }
  2731. //---------------------------------------------------------------------------
  2732. void __fastcall TRemoteProperties::Load(THierarchicalStorage * Storage)
  2733. {
  2734. unsigned char Buf[sizeof(Valid)];
  2735. if (static_cast<size_t>(Storage->ReadBinaryData(L"Valid", &Buf, sizeof(Buf))) == sizeof(Buf))
  2736. {
  2737. memmove(&Valid, Buf, sizeof(Valid));
  2738. }
  2739. if (Valid.Contains(vpRights))
  2740. {
  2741. Rights.Text = Storage->ReadString(L"Rights", Rights.Text);
  2742. }
  2743. // TODO
  2744. }
  2745. //---------------------------------------------------------------------------
  2746. void __fastcall TRemoteProperties::Save(THierarchicalStorage * Storage) const
  2747. {
  2748. Storage->WriteBinaryData(UnicodeString(L"Valid"),
  2749. static_cast<const void *>(&Valid), sizeof(Valid));
  2750. if (Valid.Contains(vpRights))
  2751. {
  2752. Storage->WriteString(L"Rights", Rights.Text);
  2753. }
  2754. // TODO
  2755. }
  2756. //---------------------------------------------------------------------------
  2757. //---------------------------------------------------------------------------
  2758. TSynchronizeChecklist::TItem::TItem() :
  2759. Action(saNone), IsDirectory(false), RemoteFile(NULL), Checked(true), ImageIndex(-1), FDirectoryHasSize(false)
  2760. {
  2761. Local.ModificationFmt = mfFull;
  2762. Local.Modification = 0;
  2763. Local.Size = 0;
  2764. Remote.ModificationFmt = mfFull;
  2765. Remote.Modification = 0;
  2766. Remote.Size = 0;
  2767. }
  2768. //---------------------------------------------------------------------------
  2769. TSynchronizeChecklist::TItem::~TItem()
  2770. {
  2771. delete RemoteFile;
  2772. }
  2773. //---------------------------------------------------------------------------
  2774. const UnicodeString& TSynchronizeChecklist::TItem::GetFileName() const
  2775. {
  2776. if (!Remote.FileName.IsEmpty())
  2777. {
  2778. return Remote.FileName;
  2779. }
  2780. else
  2781. {
  2782. DebugAssert(!Local.FileName.IsEmpty());
  2783. return Local.FileName;
  2784. }
  2785. }
  2786. //---------------------------------------------------------------------------
  2787. __int64 __fastcall TSynchronizeChecklist::TItem::GetSize() const
  2788. {
  2789. return GetSize(Action);
  2790. }
  2791. //---------------------------------------------------------------------------
  2792. __int64 __fastcall TSynchronizeChecklist::TItem::GetSize(TAction AAction) const
  2793. {
  2794. if (IsItemSizeIrrelevant(AAction))
  2795. {
  2796. return 0;
  2797. }
  2798. else
  2799. {
  2800. switch (AAction)
  2801. {
  2802. case saUploadNew:
  2803. case saUploadUpdate:
  2804. return Local.Size;
  2805. case saDownloadNew:
  2806. case saDownloadUpdate:
  2807. return Remote.Size;
  2808. default:
  2809. DebugFail();
  2810. return 0;
  2811. }
  2812. }
  2813. }
  2814. //---------------------------------------------------------------------------
  2815. //---------------------------------------------------------------------------
  2816. TSynchronizeChecklist::TSynchronizeChecklist() :
  2817. FList(new TList())
  2818. {
  2819. }
  2820. //---------------------------------------------------------------------------
  2821. TSynchronizeChecklist::~TSynchronizeChecklist()
  2822. {
  2823. for (int Index = 0; Index < FList->Count; Index++)
  2824. {
  2825. delete static_cast<TItem *>(FList->Items[Index]);
  2826. }
  2827. delete FList;
  2828. }
  2829. //---------------------------------------------------------------------------
  2830. void TSynchronizeChecklist::Add(TItem * Item)
  2831. {
  2832. FList->Add(Item);
  2833. }
  2834. //---------------------------------------------------------------------------
  2835. int __fastcall TSynchronizeChecklist::Compare(void * AItem1, void * AItem2)
  2836. {
  2837. TItem * Item1 = static_cast<TItem *>(AItem1);
  2838. TItem * Item2 = static_cast<TItem *>(AItem2);
  2839. int Result;
  2840. if (!Item1->Local.Directory.IsEmpty())
  2841. {
  2842. Result = AnsiCompareText(Item1->Local.Directory, Item2->Local.Directory);
  2843. }
  2844. else
  2845. {
  2846. DebugAssert(!Item1->Remote.Directory.IsEmpty());
  2847. Result = AnsiCompareText(Item1->Remote.Directory, Item2->Remote.Directory);
  2848. }
  2849. if (Result == 0)
  2850. {
  2851. Result = AnsiCompareText(Item1->GetFileName(), Item2->GetFileName());
  2852. }
  2853. return Result;
  2854. }
  2855. //---------------------------------------------------------------------------
  2856. void TSynchronizeChecklist::Sort()
  2857. {
  2858. FList->Sort(Compare);
  2859. }
  2860. //---------------------------------------------------------------------------
  2861. int TSynchronizeChecklist::GetCount() const
  2862. {
  2863. return FList->Count;
  2864. }
  2865. //---------------------------------------------------------------------------
  2866. int TSynchronizeChecklist::GetCheckedCount() const
  2867. {
  2868. int Result = 0;
  2869. for (int Index = 0; (Index < Count); Index++)
  2870. {
  2871. if (Item[Index]->Checked)
  2872. {
  2873. Result++;
  2874. }
  2875. }
  2876. return Result;
  2877. }
  2878. //---------------------------------------------------------------------------
  2879. const TSynchronizeChecklist::TItem * TSynchronizeChecklist::GetItem(int Index) const
  2880. {
  2881. return static_cast<TItem *>(FList->Items[Index]);
  2882. }
  2883. //---------------------------------------------------------------------------
  2884. void __fastcall TSynchronizeChecklist::Update(const TItem * Item, bool Check, TAction Action)
  2885. {
  2886. // TSynchronizeChecklist owns non-const items so it can manipulate them freely,
  2887. // const_cast here is just an optimization
  2888. TItem * MutableItem = const_cast<TItem *>(Item);
  2889. DebugAssert(FList->IndexOf(MutableItem) >= 0);
  2890. MutableItem->Checked = Check;
  2891. MutableItem->Action = Action;
  2892. }
  2893. //---------------------------------------------------------------------------
  2894. void TSynchronizeChecklist::Delete(const TItem * Item)
  2895. {
  2896. // See comment in Update()
  2897. TItem * MutableItem = const_cast<TItem *>(Item);
  2898. FList->Extract(MutableItem);
  2899. delete Item;
  2900. }
  2901. //---------------------------------------------------------------------------
  2902. void __fastcall TSynchronizeChecklist::UpdateDirectorySize(const TItem * Item, __int64 Size)
  2903. {
  2904. // See comment in Update
  2905. TItem * MutableItem = const_cast<TItem *>(Item);
  2906. DebugAssert(FList->IndexOf(MutableItem) >= 0);
  2907. if (DebugAlwaysTrue(Item->IsDirectory))
  2908. {
  2909. MutableItem->FDirectoryHasSize = true;
  2910. if (Item->IsRemoteOnly())
  2911. {
  2912. MutableItem->Remote.Size = Size;
  2913. }
  2914. else if (Item->IsLocalOnly())
  2915. {
  2916. MutableItem->Local.Size = Size;
  2917. }
  2918. else
  2919. {
  2920. // "update" actions are not relevant for directories
  2921. DebugFail();
  2922. }
  2923. }
  2924. }
  2925. //---------------------------------------------------------------------------
  2926. TSynchronizeChecklist::TAction __fastcall TSynchronizeChecklist::Reverse(TSynchronizeChecklist::TAction Action)
  2927. {
  2928. switch (Action)
  2929. {
  2930. case saUploadNew:
  2931. return saDeleteLocal;
  2932. case saDownloadNew:
  2933. return saDeleteRemote;
  2934. case saUploadUpdate:
  2935. return saDownloadUpdate;
  2936. case saDownloadUpdate:
  2937. return saUploadUpdate;
  2938. case saDeleteRemote:
  2939. return saDownloadNew;
  2940. case saDeleteLocal:
  2941. return saUploadNew;
  2942. default:
  2943. case saNone:
  2944. DebugFail();
  2945. return saNone;
  2946. }
  2947. }
  2948. //---------------------------------------------------------------------------
  2949. bool __fastcall TSynchronizeChecklist::IsItemSizeIrrelevant(TAction Action)
  2950. {
  2951. switch (Action)
  2952. {
  2953. case saNone:
  2954. case saDeleteRemote:
  2955. case saDeleteLocal:
  2956. return true;
  2957. default:
  2958. return false;
  2959. }
  2960. }
  2961. //---------------------------------------------------------------------------
  2962. //---------------------------------------------------------------------------
  2963. TSynchronizeProgress::TSynchronizeProgress(const TSynchronizeChecklist * Checklist)
  2964. {
  2965. FTotalSize = -1;
  2966. FProcessedSize = 0;
  2967. FChecklist = Checklist;
  2968. }
  2969. //---------------------------------------------------------------------------
  2970. __int64 TSynchronizeProgress::ItemSize(const TSynchronizeChecklist::TItem * ChecklistItem) const
  2971. {
  2972. __int64 Result;
  2973. switch (ChecklistItem->Action)
  2974. {
  2975. case TSynchronizeChecklist::saDeleteRemote:
  2976. case TSynchronizeChecklist::saDeleteLocal:
  2977. Result = ChecklistItem->IsDirectory ? 1024*1024 : 100*1024;
  2978. break;
  2979. default:
  2980. if (ChecklistItem->HasSize())
  2981. {
  2982. Result = ChecklistItem->GetSize();
  2983. }
  2984. else
  2985. {
  2986. DebugAssert(ChecklistItem->IsDirectory);
  2987. Result = 1024*1024;
  2988. }
  2989. break;
  2990. }
  2991. return Result;
  2992. }
  2993. //---------------------------------------------------------------------------
  2994. void TSynchronizeProgress::ItemProcessed(const TSynchronizeChecklist::TItem * ChecklistItem)
  2995. {
  2996. FProcessedSize += ItemSize(ChecklistItem);
  2997. }
  2998. //---------------------------------------------------------------------------
  2999. __int64 TSynchronizeProgress::GetProcessed(const TFileOperationProgressType * CurrentItemOperationProgress) const
  3000. {
  3001. DebugAssert(!TFileOperationProgressType::IsIndeterminateOperation(CurrentItemOperationProgress->Operation));
  3002. // Need to calculate the total size on the first call only,
  3003. // as at the time the constructor it called, we usually do not have sizes of folders calculated yet.
  3004. if (FTotalSize < 0)
  3005. {
  3006. FTotalSize = 0;
  3007. for (int Index = 0; Index < FChecklist->Count; Index++)
  3008. {
  3009. const TSynchronizeChecklist::TItem * ChecklistItem = FChecklist->Item[Index];
  3010. if (ChecklistItem->Checked)
  3011. {
  3012. FTotalSize += ItemSize(ChecklistItem);
  3013. }
  3014. }
  3015. }
  3016. // For (single-item-)delete operation, this should return 0
  3017. __int64 CurrentItemProcessedSize = CurrentItemOperationProgress->OperationTransferred;
  3018. return (FProcessedSize + CurrentItemProcessedSize);
  3019. }
  3020. //---------------------------------------------------------------------------
  3021. int TSynchronizeProgress::Progress(const TFileOperationProgressType * CurrentItemOperationProgress) const
  3022. {
  3023. int Result;
  3024. if (TFileOperationProgressType::IsIndeterminateOperation(CurrentItemOperationProgress->Operation))
  3025. {
  3026. Result = -1;
  3027. }
  3028. else
  3029. {
  3030. __int64 Processed = GetProcessed(CurrentItemOperationProgress);
  3031. if (FTotalSize > 0)
  3032. {
  3033. Result = (Processed * 100) / FTotalSize;
  3034. }
  3035. else
  3036. {
  3037. Result = 0;
  3038. }
  3039. }
  3040. return Result;
  3041. }
  3042. //---------------------------------------------------------------------------
  3043. TDateTime TSynchronizeProgress::TimeLeft(const TFileOperationProgressType * CurrentItemOperationProgress) const
  3044. {
  3045. TDateTime Result;
  3046. __int64 Processed = GetProcessed(CurrentItemOperationProgress);
  3047. if (Processed > 0)
  3048. {
  3049. Result = TDateTime(double(Now() - CurrentItemOperationProgress->StartTime) / Processed * (FTotalSize - Processed));
  3050. }
  3051. return Result;
  3052. }