RemoteFiles.cpp 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243
  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. FIDMap.insert(TIDMap::value_type(Token.ID, FTokens.size() - 1));
  726. }
  727. if (Token.NameValid)
  728. {
  729. FNameMap.insert(TNameMap::value_type(Token.Name, FTokens.size() - 1));
  730. }
  731. }
  732. //---------------------------------------------------------------------------
  733. void __fastcall TRemoteTokenList::AddUnique(const TRemoteToken & Token)
  734. {
  735. if (Token.IDValid)
  736. {
  737. TIDMap::const_iterator I = FIDMap.find(Token.ID);
  738. if (I != FIDMap.end())
  739. {
  740. // is present already.
  741. // may have different name (should not),
  742. // but what can we do about it anyway?
  743. }
  744. else
  745. {
  746. Add(Token);
  747. }
  748. }
  749. else if (Token.NameValid)
  750. {
  751. TNameMap::const_iterator I = FNameMap.find(Token.Name);
  752. if (I != FNameMap.end())
  753. {
  754. // is present already.
  755. }
  756. else
  757. {
  758. Add(Token);
  759. }
  760. }
  761. else
  762. {
  763. // can happen, e.g. with winsshd/SFTP
  764. }
  765. }
  766. //---------------------------------------------------------------------------
  767. bool __fastcall TRemoteTokenList::Exists(const UnicodeString & Name) const
  768. {
  769. // We should make use of SameUserName
  770. return (FNameMap.find(Name) != FNameMap.end());
  771. }
  772. //---------------------------------------------------------------------------
  773. const TRemoteToken * TRemoteTokenList::Find(unsigned int ID) const
  774. {
  775. TIDMap::const_iterator I = FIDMap.find(ID);
  776. const TRemoteToken * Result;
  777. if (I != FIDMap.end())
  778. {
  779. Result = &FTokens[(*I).second];
  780. }
  781. else
  782. {
  783. Result = NULL;
  784. }
  785. return Result;
  786. }
  787. //---------------------------------------------------------------------------
  788. const TRemoteToken * TRemoteTokenList::Find(const UnicodeString & Name) const
  789. {
  790. TNameMap::const_iterator I = FNameMap.find(Name);
  791. const TRemoteToken * Result;
  792. if (I != FNameMap.end())
  793. {
  794. Result = &FTokens[(*I).second];
  795. }
  796. else
  797. {
  798. Result = NULL;
  799. }
  800. return Result;
  801. }
  802. //---------------------------------------------------------------------------
  803. void __fastcall TRemoteTokenList::Log(TTerminal * Terminal, const wchar_t * Title)
  804. {
  805. if (!FTokens.empty())
  806. {
  807. Terminal->LogEvent(FORMAT(L"Following %s found:", (Title)));
  808. for (size_t Index = 0; Index < FTokens.size(); Index++)
  809. {
  810. Terminal->LogEvent(UnicodeString(L" ") + FTokens[Index].LogText);
  811. }
  812. }
  813. else
  814. {
  815. Terminal->LogEvent(FORMAT(L"No %s found.", (Title)));
  816. }
  817. }
  818. //---------------------------------------------------------------------------
  819. int __fastcall TRemoteTokenList::Count() const
  820. {
  821. return (int)FTokens.size();
  822. }
  823. //---------------------------------------------------------------------------
  824. const TRemoteToken * __fastcall TRemoteTokenList::Token(int Index) const
  825. {
  826. return &FTokens[Index];
  827. }
  828. //---------------------------------------------------------------------------
  829. //---------------------------------------------------------------------------
  830. __fastcall TRemoteFile::TRemoteFile(TRemoteFile * ALinkedByFile):
  831. TPersistent()
  832. {
  833. FLinkedFile = NULL;
  834. FRights = new TRights();
  835. FIconIndex = -1;
  836. FCyclicLink = false;
  837. FModificationFmt = mfFull;
  838. FLinkedByFile = ALinkedByFile;
  839. FTerminal = NULL;
  840. FDirectory = NULL;
  841. FIsHidden = -1;
  842. FIsEncrypted = false;
  843. FCalculatedSize = -1;
  844. }
  845. //---------------------------------------------------------------------------
  846. __fastcall TRemoteFile::~TRemoteFile()
  847. {
  848. delete FRights;
  849. delete FLinkedFile;
  850. }
  851. //---------------------------------------------------------------------------
  852. TRemoteFile * __fastcall TRemoteFile::Duplicate(bool Standalone) const
  853. {
  854. TRemoteFile * Result;
  855. Result = new TRemoteFile();
  856. try
  857. {
  858. if (FLinkedFile)
  859. {
  860. Result->FLinkedFile = FLinkedFile->Duplicate(true);
  861. Result->FLinkedFile->FLinkedByFile = Result;
  862. }
  863. *Result->Rights = *FRights;
  864. #define COPY_FP(PROP) Result->F ## PROP = F ## PROP;
  865. COPY_FP(Terminal);
  866. COPY_FP(Owner);
  867. COPY_FP(ModificationFmt);
  868. COPY_FP(Size);
  869. COPY_FP(CalculatedSize);
  870. COPY_FP(FileName);
  871. COPY_FP(DisplayName);
  872. COPY_FP(INodeBlocks);
  873. COPY_FP(Modification);
  874. COPY_FP(LastAccess);
  875. COPY_FP(Group);
  876. COPY_FP(IconIndex);
  877. COPY_FP(TypeName);
  878. COPY_FP(IsSymLink);
  879. COPY_FP(LinkTo);
  880. COPY_FP(Type);
  881. COPY_FP(Tags);
  882. COPY_FP(CyclicLink);
  883. COPY_FP(HumanRights);
  884. COPY_FP(IsEncrypted);
  885. #undef COPY_FP
  886. if (Standalone && (!FFullFileName.IsEmpty() || (Directory != NULL)))
  887. {
  888. Result->FFullFileName = FullFileName;
  889. }
  890. }
  891. catch(...)
  892. {
  893. delete Result;
  894. throw;
  895. }
  896. return Result;
  897. }
  898. //---------------------------------------------------------------------------
  899. void __fastcall TRemoteFile::LoadTypeInfo()
  900. {
  901. /* TODO : If file is link: Should be attributes taken from linked file? */
  902. unsigned long Attrs = 0;
  903. if (IsDirectory) Attrs |= FILE_ATTRIBUTE_DIRECTORY;
  904. if (IsHidden) Attrs |= FILE_ATTRIBUTE_HIDDEN;
  905. UnicodeString DumbFileName = (IsSymLink && !LinkTo.IsEmpty() ? LinkTo : FileName);
  906. FIconIndex = FakeFileImageIndex(DumbFileName, Attrs, &FTypeName);
  907. }
  908. //---------------------------------------------------------------------------
  909. __int64 __fastcall TRemoteFile::GetSize() const
  910. {
  911. return IsDirectory ? 0 : FSize;
  912. }
  913. //---------------------------------------------------------------------------
  914. Integer __fastcall TRemoteFile::GetIconIndex() const
  915. {
  916. if (FIconIndex == -1)
  917. {
  918. const_cast<TRemoteFile *>(this)->LoadTypeInfo();
  919. }
  920. return FIconIndex;
  921. }
  922. //---------------------------------------------------------------------------
  923. UnicodeString __fastcall TRemoteFile::GetTypeName()
  924. {
  925. // check availability of type info by icon index, because type name can be empty
  926. if (FIconIndex < 0)
  927. {
  928. LoadTypeInfo();
  929. }
  930. return FTypeName;
  931. }
  932. //---------------------------------------------------------------------------
  933. Boolean __fastcall TRemoteFile::GetIsHidden() const
  934. {
  935. bool Result;
  936. switch (FIsHidden)
  937. {
  938. case 0:
  939. Result = false;
  940. break;
  941. case 1:
  942. Result = true;
  943. break;
  944. default:
  945. Result = IsUnixHiddenFile(FileName);
  946. break;
  947. }
  948. return Result;
  949. }
  950. //---------------------------------------------------------------------------
  951. void __fastcall TRemoteFile::SetIsHidden(bool value)
  952. {
  953. FIsHidden = value ? 1 : 0;
  954. }
  955. //---------------------------------------------------------------------------
  956. Boolean __fastcall TRemoteFile::GetIsDirectory() const
  957. {
  958. if (IsSymLink && (FLinkedFile != NULL))
  959. {
  960. return FLinkedFile->IsDirectory;
  961. }
  962. else
  963. {
  964. return (towupper(Type) == FILETYPE_DIRECTORY);
  965. }
  966. }
  967. //---------------------------------------------------------------------------
  968. Boolean __fastcall TRemoteFile::GetIsParentDirectory() const
  969. {
  970. return (FileName == PARENTDIRECTORY);
  971. }
  972. //---------------------------------------------------------------------------
  973. Boolean __fastcall TRemoteFile::GetIsThisDirectory() const
  974. {
  975. return (FileName == THISDIRECTORY);
  976. }
  977. //---------------------------------------------------------------------------
  978. Boolean __fastcall TRemoteFile::GetIsInaccessibleDirectory() const
  979. {
  980. Boolean Result;
  981. if (IsDirectory)
  982. {
  983. DebugAssert(Terminal);
  984. Result = !
  985. (SameUserName(Terminal->UserName, L"root") ||
  986. ((Rights->RightUndef[TRights::rrOtherExec] != TRights::rsNo)) ||
  987. ((Rights->Right[TRights::rrGroupExec] != TRights::rsNo) &&
  988. Terminal->Membership->Exists(Group.Name)) ||
  989. ((Rights->Right[TRights::rrUserExec] != TRights::rsNo) &&
  990. SameUserName(Terminal->UserName, Owner.Name)));
  991. }
  992. else Result = False;
  993. return Result;
  994. }
  995. //---------------------------------------------------------------------------
  996. wchar_t __fastcall TRemoteFile::GetType() const
  997. {
  998. return FType;
  999. }
  1000. //---------------------------------------------------------------------------
  1001. void __fastcall TRemoteFile::SetType(wchar_t AType)
  1002. {
  1003. FType = AType;
  1004. FIsSymLink = ((wchar_t)towupper(FType) == FILETYPE_SYMLINK);
  1005. }
  1006. //---------------------------------------------------------------------------
  1007. const TRemoteFile * __fastcall TRemoteFile::GetLinkedFile() const
  1008. {
  1009. // do not call FindLinkedFile as it would be called repeatedly for broken symlinks
  1010. return FLinkedFile;
  1011. }
  1012. //---------------------------------------------------------------------------
  1013. bool __fastcall TRemoteFile::GetBrokenLink()
  1014. {
  1015. DebugAssert(Terminal);
  1016. // If file is symlink but we couldn't find linked file we assume broken link
  1017. return (IsSymLink && (FCyclicLink || !FLinkedFile) &&
  1018. Terminal->ResolvingSymlinks);
  1019. // "!FLinkTo.IsEmpty()" removed because it does not work with SFTP
  1020. }
  1021. //---------------------------------------------------------------------------
  1022. bool __fastcall TRemoteFile::IsTimeShiftingApplicable()
  1023. {
  1024. return IsTimeShiftingApplicable(ModificationFmt);
  1025. }
  1026. //---------------------------------------------------------------------------
  1027. bool __fastcall TRemoteFile::IsTimeShiftingApplicable(TModificationFmt ModificationFmt)
  1028. {
  1029. return (ModificationFmt == mfMDHM) || (ModificationFmt == mfYMDHM) || (ModificationFmt == mfFull);
  1030. }
  1031. //---------------------------------------------------------------------------
  1032. void __fastcall TRemoteFile::ShiftTimeInSeconds(__int64 Seconds)
  1033. {
  1034. ShiftTimeInSeconds(FModification, ModificationFmt, Seconds);
  1035. ShiftTimeInSeconds(FLastAccess, ModificationFmt, Seconds);
  1036. }
  1037. //---------------------------------------------------------------------------
  1038. void __fastcall TRemoteFile::ShiftTimeInSeconds(TDateTime & DateTime, TModificationFmt ModificationFmt, __int64 Seconds)
  1039. {
  1040. if ((Seconds != 0) && IsTimeShiftingApplicable(ModificationFmt))
  1041. {
  1042. DebugAssert(int(DateTime) != 0);
  1043. DateTime = IncSecond(DateTime, Seconds);
  1044. }
  1045. }
  1046. //---------------------------------------------------------------------------
  1047. void __fastcall TRemoteFile::SetModification(const TDateTime & value)
  1048. {
  1049. if (FModification != value)
  1050. {
  1051. FModificationFmt = mfFull;
  1052. FModification = value;
  1053. }
  1054. }
  1055. //---------------------------------------------------------------------------
  1056. UnicodeString __fastcall TRemoteFile::GetUserModificationStr()
  1057. {
  1058. return ::UserModificationStr(Modification, FModificationFmt);
  1059. }
  1060. //---------------------------------------------------------------------------
  1061. UnicodeString __fastcall TRemoteFile::GetModificationStr()
  1062. {
  1063. return ::ModificationStr(Modification, FModificationFmt);
  1064. }
  1065. //---------------------------------------------------------------------------
  1066. UnicodeString __fastcall TRemoteFile::GetExtension()
  1067. {
  1068. return UnixExtractFileExt(FFileName);
  1069. }
  1070. //---------------------------------------------------------------------------
  1071. void __fastcall TRemoteFile::SetRights(TRights * value)
  1072. {
  1073. FRights->Assign(value);
  1074. }
  1075. //---------------------------------------------------------------------------
  1076. UnicodeString __fastcall TRemoteFile::GetRightsStr()
  1077. {
  1078. // note that HumanRights is typically an empty string
  1079. // (with an exception of Perm-fact-only MLSD FTP listing)
  1080. return FRights->Unknown ? HumanRights : FRights->Text;
  1081. }
  1082. //---------------------------------------------------------------------------
  1083. void __fastcall TRemoteFile::SetListingStr(UnicodeString value)
  1084. {
  1085. // Value stored in 'value' can be used for error message
  1086. UnicodeString Line = value;
  1087. FIconIndex = -1;
  1088. try
  1089. {
  1090. UnicodeString Col;
  1091. // Do we need to do this (is ever TAB is LS output)?
  1092. Line = ReplaceChar(Line, L'\t', L' ');
  1093. Type = Line[1];
  1094. Line.Delete(1, 1);
  1095. #define GETNCOL \
  1096. { if (Line.IsEmpty()) throw Exception(L""); \
  1097. Integer P = Line.Pos(L' '); \
  1098. if (P) { Col = Line.SubString(1, P-1); Line.Delete(1, P); } \
  1099. else { Col = Line; Line = L""; } \
  1100. }
  1101. #define GETCOL { GETNCOL; Line = TrimLeft(Line); }
  1102. // Rights string may contain special permission attributes (S,t, ...)
  1103. // (TODO: maybe no longer necessary, once we can handle the special permissions)
  1104. Rights->AllowUndef = True;
  1105. // On some system there is no space between permissions and node blocks count columns
  1106. // so we get only first 9 characters and trim all following spaces (if any)
  1107. Rights->Text = Line.SubString(1, 9);
  1108. Line.Delete(1, 9);
  1109. // Rights column maybe followed by '+', '@' or '.' signs, we ignore them
  1110. // (On MacOS, there may be a space in between)
  1111. if (!Line.IsEmpty() && ((Line[1] == L'+') || (Line[1] == L'@') || (Line[1] == L'.')))
  1112. {
  1113. Line.Delete(1, 1);
  1114. }
  1115. else if ((Line.Length() >= 2) && (Line[1] == L' ') &&
  1116. ((Line[2] == L'+') || (Line[2] == L'@') || (Line[2] == L'.')))
  1117. {
  1118. Line.Delete(1, 2);
  1119. }
  1120. Line = Line.TrimLeft();
  1121. GETCOL;
  1122. if (!TryStrToInt(Col, FINodeBlocks))
  1123. {
  1124. // if the column is not an integer, suppose it's owner
  1125. // (Android BusyBox)
  1126. FINodeBlocks = 0;
  1127. }
  1128. else
  1129. {
  1130. GETCOL;
  1131. }
  1132. FOwner.Name = Col;
  1133. // #60 17.10.01: group name can contain space
  1134. FGroup.Name = L"";
  1135. GETCOL;
  1136. __int64 ASize;
  1137. do
  1138. {
  1139. FGroup.Name = FGroup.Name + Col;
  1140. GETCOL;
  1141. // SSH FS link like
  1142. // d????????? ? ? ? ? ? name
  1143. if ((FGroup.Name == L"?") && (Col == L"?"))
  1144. {
  1145. ASize = 0;
  1146. }
  1147. else
  1148. {
  1149. DebugAssert(!Col.IsEmpty());
  1150. // for devices etc.. there is additional column ending by comma, we ignore it
  1151. if (Col[Col.Length()] == L',') GETCOL;
  1152. ASize = StrToInt64Def(Col, -1);
  1153. // if it's not a number (file size) we take it as part of group name
  1154. // (at least on CygWin, there can be group with space in its name)
  1155. if (ASize < 0) Col = L" " + Col;
  1156. }
  1157. }
  1158. while (ASize < 0);
  1159. // Do not read modification time and filename (test close to the end of this block) if it is already set.
  1160. if (double(FModification) == 0)
  1161. {
  1162. bool FullTime = false;
  1163. bool DayMonthFormat = false;
  1164. Word Day, Month, Year, Hour, Min, Sec, P;
  1165. GETCOL;
  1166. // SSH FS link, see above
  1167. if (Col == L"?")
  1168. {
  1169. GETCOL;
  1170. FModificationFmt = mfNone;
  1171. FModification = 0;
  1172. FLastAccess = 0;
  1173. }
  1174. else
  1175. {
  1176. // format dd mmm or mmm dd ?
  1177. Day = (Word)StrToIntDef(Col, 0);
  1178. if (Day > 0)
  1179. {
  1180. DayMonthFormat = true;
  1181. GETCOL;
  1182. }
  1183. Month = 0;
  1184. #define COL2MONTH \
  1185. for (Word IMonth = 0; IMonth < 12; IMonth++) \
  1186. if (!Col.CompareIC(EngShortMonthNames[IMonth])) { Month = IMonth; Month++; break; }
  1187. COL2MONTH;
  1188. // if the column is not known month name, it may have been "yyyy-mm-dd"
  1189. // for --full-time format
  1190. if ((Month == 0) && (Col.Length() == 10) && (Col[5] == L'-') && (Col[8] == L'-'))
  1191. {
  1192. Year = (Word)Col.SubString(1, 4).ToInt();
  1193. Month = (Word)Col.SubString(6, 2).ToInt();
  1194. Day = (Word)Col.SubString(9, 2).ToInt();
  1195. GETCOL;
  1196. Hour = (Word)Col.SubString(1, 2).ToInt();
  1197. Min = (Word)Col.SubString(4, 2).ToInt();
  1198. if (Col.Length() >= 8)
  1199. {
  1200. Sec = (Word)StrToInt(Col.SubString(7, 2));
  1201. }
  1202. else
  1203. {
  1204. Sec = 0;
  1205. }
  1206. FModificationFmt = mfFull;
  1207. // skip TZ (TODO)
  1208. // do not trim leading space of filename
  1209. GETNCOL;
  1210. }
  1211. else
  1212. {
  1213. // or it may have been day name for another format of --full-time
  1214. if (Month == 0)
  1215. {
  1216. GETCOL;
  1217. COL2MONTH;
  1218. // neither standard, not --full-time format
  1219. if (Month == 0)
  1220. {
  1221. Abort();
  1222. }
  1223. else
  1224. {
  1225. FullTime = true;
  1226. }
  1227. }
  1228. #undef COL2MONTH
  1229. if (Day == 0)
  1230. {
  1231. GETNCOL;
  1232. Day = (Word)StrToInt(Col);
  1233. }
  1234. if ((Day < 1) || (Day > 31)) Abort();
  1235. // second full-time format
  1236. // ddd mmm dd hh:nn:ss yyyy
  1237. if (FullTime)
  1238. {
  1239. GETCOL;
  1240. if (Col.Length() != 8)
  1241. {
  1242. Abort();
  1243. }
  1244. Hour = (Word)StrToInt(Col.SubString(1, 2));
  1245. Min = (Word)StrToInt(Col.SubString(4, 2));
  1246. Sec = (Word)StrToInt(Col.SubString(7, 2));
  1247. FModificationFmt = mfFull;
  1248. // do not trim leading space of filename
  1249. GETNCOL;
  1250. Year = (Word)StrToInt(Col);
  1251. }
  1252. else
  1253. {
  1254. // for format dd mmm the below description seems not to be true,
  1255. // the year is not aligned to 5 characters
  1256. if (DayMonthFormat)
  1257. {
  1258. GETCOL;
  1259. }
  1260. else
  1261. {
  1262. // Time/Year indicator is always 5 characters long (???), on most
  1263. // systems year is aligned to right (_YYYY), but on some to left (YYYY_),
  1264. // we must ensure that trailing space is also deleted, so real
  1265. // separator space is not treated as part of file name
  1266. Col = Line.SubString(1, 6).Trim();
  1267. Line.Delete(1, 6);
  1268. }
  1269. // GETNCOL; // We don't want to trim input strings (name with space at beginning???)
  1270. // Check if we got time (contains :) or year
  1271. if ((P = (Word)Col.Pos(L':')) > 0)
  1272. {
  1273. Word CurrMonth, CurrDay;
  1274. Hour = (Word)StrToInt(Col.SubString(1, P-1));
  1275. Min = (Word)StrToInt(Col.SubString(P+1, Col.Length() - P));
  1276. if (Hour > 23 || Min > 59) Abort();
  1277. // When we don't got year, we assume current year
  1278. // with exception that the date would be in future
  1279. // in this case we assume last year.
  1280. DecodeDate(Date(), Year, CurrMonth, CurrDay);
  1281. if ((Month > CurrMonth) ||
  1282. (Month == CurrMonth && Day > CurrDay)) Year--;
  1283. Sec = 0;
  1284. FModificationFmt = mfMDHM;
  1285. }
  1286. else
  1287. {
  1288. Year = (Word)StrToInt(Col);
  1289. if (Year > 10000) Abort();
  1290. // When we didn't get time we assume midnight
  1291. Hour = 0; Min = 0; Sec = 0;
  1292. FModificationFmt = mfMDY;
  1293. }
  1294. }
  1295. }
  1296. FModification = EncodeDateVerbose(Year, Month, Day) + EncodeTimeVerbose(Hour, Min, Sec, 0);
  1297. // adjust only when time is known,
  1298. // adjusting default "midnight" time makes no sense
  1299. if ((FModificationFmt == mfMDHM) ||
  1300. DebugAlwaysFalse(FModificationFmt == mfYMDHM) ||
  1301. (FModificationFmt == mfFull))
  1302. {
  1303. DebugAssert(Terminal != NULL);
  1304. FModification = AdjustDateTimeFromUnix(FModification,
  1305. Terminal->SessionData->DSTMode);
  1306. }
  1307. if (double(FLastAccess) == 0)
  1308. {
  1309. FLastAccess = FModification;
  1310. }
  1311. }
  1312. // separating space is already deleted, other spaces are treated as part of name
  1313. // see comment at the beginning of the block
  1314. if (FileName.IsEmpty())
  1315. {
  1316. FSize = ASize;
  1317. int P;
  1318. FLinkTo = L"";
  1319. if (IsSymLink)
  1320. {
  1321. P = Line.Pos(SYMLINKSTR);
  1322. if (P)
  1323. {
  1324. FLinkTo = Line.SubString(
  1325. P + wcslen(SYMLINKSTR), Line.Length() - P + wcslen(SYMLINKSTR) + 1);
  1326. Line.SetLength(P - 1);
  1327. }
  1328. else
  1329. {
  1330. Abort();
  1331. }
  1332. }
  1333. FFileName = UnixExtractFileName(Line);
  1334. }
  1335. }
  1336. #undef GETNCOL
  1337. #undef GETCOL
  1338. }
  1339. catch (Exception &E)
  1340. {
  1341. throw ETerminal(&E, FmtLoadStr(LIST_LINE_ERROR, ARRAYOFCONST((value))), HELP_LIST_LINE_ERROR);
  1342. }
  1343. }
  1344. //---------------------------------------------------------------------------
  1345. void __fastcall TRemoteFile::Complete()
  1346. {
  1347. DebugAssert(Terminal != NULL);
  1348. if (IsSymLink && Terminal->ResolvingSymlinks)
  1349. {
  1350. FindLinkedFile();
  1351. }
  1352. }
  1353. //---------------------------------------------------------------------------
  1354. void __fastcall TRemoteFile::SetEncrypted()
  1355. {
  1356. FIsEncrypted = true;
  1357. if (Size > TEncryption::GetOverhead())
  1358. {
  1359. Size -= TEncryption::GetOverhead();
  1360. }
  1361. }
  1362. //---------------------------------------------------------------------------
  1363. void __fastcall TRemoteFile::FindLinkedFile()
  1364. {
  1365. DebugAssert(Terminal && IsSymLink);
  1366. if (FLinkedFile) delete FLinkedFile;
  1367. FLinkedFile = NULL;
  1368. FCyclicLink = false;
  1369. if (!LinkTo.IsEmpty())
  1370. {
  1371. // check for cyclic link
  1372. TRemoteFile * LinkedBy = FLinkedByFile;
  1373. while (LinkedBy)
  1374. {
  1375. if (LinkedBy->LinkTo == LinkTo)
  1376. {
  1377. // this is currently redundant information, because it is used only to
  1378. // detect broken symlink, which would be otherwise detected
  1379. // by FLinkedFile == NULL
  1380. FCyclicLink = true;
  1381. break;
  1382. }
  1383. LinkedBy = LinkedBy->FLinkedByFile;
  1384. }
  1385. }
  1386. if (FCyclicLink)
  1387. {
  1388. TRemoteFile * LinkedBy = FLinkedByFile;
  1389. while (LinkedBy)
  1390. {
  1391. LinkedBy->FCyclicLink = true;
  1392. LinkedBy = LinkedBy->FLinkedByFile;
  1393. }
  1394. }
  1395. else
  1396. {
  1397. DebugAssert(Terminal->ResolvingSymlinks);
  1398. Terminal->ExceptionOnFail = true;
  1399. try
  1400. {
  1401. try
  1402. {
  1403. Terminal->ReadSymlink(this, FLinkedFile);
  1404. }
  1405. __finally
  1406. {
  1407. Terminal->ExceptionOnFail = false;
  1408. }
  1409. }
  1410. catch (Exception &E)
  1411. {
  1412. if (E.InheritsFrom(__classid(EFatal))) throw;
  1413. else
  1414. {
  1415. Terminal->Log->AddException(&E);
  1416. }
  1417. }
  1418. }
  1419. }
  1420. //---------------------------------------------------------------------------
  1421. const TRemoteFile * __fastcall TRemoteFile::Resolve() const
  1422. {
  1423. const TRemoteFile * Result = this;
  1424. while (Result->LinkedFile != NULL)
  1425. {
  1426. Result = Result->LinkedFile;
  1427. }
  1428. return Result;
  1429. }
  1430. //---------------------------------------------------------------------------
  1431. UnicodeString __fastcall TRemoteFile::GetListingStr()
  1432. {
  1433. // note that ModificationStr is longer than 12 for mfFull
  1434. UnicodeString LinkPart;
  1435. // expanded from ?: to avoid memory leaks
  1436. if (IsSymLink)
  1437. {
  1438. LinkPart = UnicodeString(SYMLINKSTR) + LinkTo;
  1439. }
  1440. return Format(L"%s%s %3s %-8s %-8s %9s %-12s %s%s", ARRAYOFCONST((
  1441. Type, Rights->Text, IntToStr(INodeBlocks), Owner.Name, Group.Name,
  1442. IntToStr(FSize), // explicitly using size even for directories
  1443. ModificationStr, FileName,
  1444. LinkPart)));
  1445. }
  1446. //---------------------------------------------------------------------------
  1447. UnicodeString __fastcall TRemoteFile::GetFullFileName() const
  1448. {
  1449. UnicodeString Result;
  1450. if (FFullFileName.IsEmpty())
  1451. {
  1452. DebugAssert(Terminal);
  1453. DebugAssert(Directory != NULL);
  1454. if (IsParentDirectory)
  1455. {
  1456. Result = Directory->ParentPath;
  1457. }
  1458. else if (IsDirectory)
  1459. {
  1460. Result = UnixIncludeTrailingBackslash(Directory->FullDirectory + FileName);
  1461. }
  1462. else
  1463. {
  1464. Result = Directory->FullDirectory + FileName;
  1465. }
  1466. }
  1467. else
  1468. {
  1469. Result = FFullFileName;
  1470. }
  1471. return Result;
  1472. }
  1473. //---------------------------------------------------------------------------
  1474. bool __fastcall TRemoteFile::GetHaveFullFileName() const
  1475. {
  1476. return !FFullFileName.IsEmpty() || (Directory != NULL);
  1477. }
  1478. //---------------------------------------------------------------------------
  1479. Integer __fastcall TRemoteFile::GetAttr()
  1480. {
  1481. Integer Result = 0;
  1482. if (Rights->ReadOnly) Result |= faReadOnly;
  1483. if (IsHidden) Result |= faHidden;
  1484. return Result;
  1485. }
  1486. //---------------------------------------------------------------------------
  1487. void __fastcall TRemoteFile::SetTerminal(TTerminal * value)
  1488. {
  1489. FTerminal = value;
  1490. if (FLinkedFile)
  1491. {
  1492. FLinkedFile->Terminal = value;
  1493. }
  1494. }
  1495. //---------------------------------------------------------------------------
  1496. //---------------------------------------------------------------------------
  1497. __fastcall TRemoteDirectoryFile::TRemoteDirectoryFile() : TRemoteFile()
  1498. {
  1499. Modification = double(0);
  1500. ModificationFmt = mfNone;
  1501. LastAccess = Modification;
  1502. Type = L'D';
  1503. }
  1504. //---------------------------------------------------------------------------
  1505. //---------------------------------------------------------------------------
  1506. __fastcall TRemoteParentDirectory::TRemoteParentDirectory(TTerminal * ATerminal)
  1507. : TRemoteDirectoryFile()
  1508. {
  1509. FileName = PARENTDIRECTORY;
  1510. Terminal = ATerminal;
  1511. }
  1512. //=== TRemoteFileList ------------------------------------------------------
  1513. __fastcall TRemoteFileList::TRemoteFileList():
  1514. TObjectList()
  1515. {
  1516. FTimestamp = Now();
  1517. }
  1518. //---------------------------------------------------------------------------
  1519. void __fastcall TRemoteFileList::AddFile(TRemoteFile * File)
  1520. {
  1521. Add(File);
  1522. File->Directory = this;
  1523. }
  1524. //---------------------------------------------------------------------------
  1525. TStrings * __fastcall TRemoteFileList::CloneStrings(TStrings * List)
  1526. {
  1527. std::unique_ptr<TStringList> Result(new TStringList());
  1528. Result->OwnsObjects = true;
  1529. for (int Index = 0; Index < List->Count; Index++)
  1530. {
  1531. TRemoteFile * File = static_cast<TRemoteFile *>(List->Objects[Index]);
  1532. Result->AddObject(List->Strings[Index], File->Duplicate(true));
  1533. }
  1534. return Result.release();
  1535. }
  1536. //---------------------------------------------------------------------------
  1537. bool TRemoteFileList::AnyDirectory(TStrings * List)
  1538. {
  1539. bool Result = false;
  1540. for (int Index = 0; !Result && (Index < List->Count); Index++)
  1541. {
  1542. Result = static_cast<TRemoteFile *>(List->Objects[Index])->IsDirectory;
  1543. }
  1544. return Result;
  1545. }
  1546. //---------------------------------------------------------------------------
  1547. void __fastcall TRemoteFileList::DuplicateTo(TRemoteFileList * Copy)
  1548. {
  1549. Copy->Reset();
  1550. for (int Index = 0; Index < Count; Index++)
  1551. {
  1552. TRemoteFile * File = Files[Index];
  1553. Copy->AddFile(File->Duplicate(false));
  1554. }
  1555. Copy->FDirectory = Directory;
  1556. Copy->FTimestamp = FTimestamp;
  1557. }
  1558. //---------------------------------------------------------------------------
  1559. void __fastcall TRemoteFileList::Reset()
  1560. {
  1561. FTimestamp = Now();
  1562. Clear();
  1563. }
  1564. //---------------------------------------------------------------------------
  1565. void __fastcall TRemoteFileList::SetDirectory(UnicodeString value)
  1566. {
  1567. FDirectory = UnixExcludeTrailingBackslash(value);
  1568. }
  1569. //---------------------------------------------------------------------------
  1570. UnicodeString __fastcall TRemoteFileList::GetFullDirectory()
  1571. {
  1572. return UnixIncludeTrailingBackslash(Directory);
  1573. }
  1574. //---------------------------------------------------------------------------
  1575. TRemoteFile * __fastcall TRemoteFileList::GetFiles(Integer Index)
  1576. {
  1577. return (TRemoteFile *)Items[Index];
  1578. }
  1579. //---------------------------------------------------------------------------
  1580. Boolean __fastcall TRemoteFileList::GetIsRoot()
  1581. {
  1582. return (Directory == ROOTDIRECTORY);
  1583. }
  1584. //---------------------------------------------------------------------------
  1585. UnicodeString __fastcall TRemoteFileList::GetParentPath()
  1586. {
  1587. return UnixExtractFilePath(Directory);
  1588. }
  1589. //---------------------------------------------------------------------------
  1590. __int64 __fastcall TRemoteFileList::GetTotalSize()
  1591. {
  1592. __int64 Result = 0;
  1593. for (Integer Index = 0; Index < Count; Index++)
  1594. Result += Files[Index]->Size;
  1595. return Result;
  1596. }
  1597. //---------------------------------------------------------------------------
  1598. TRemoteFile * __fastcall TRemoteFileList::FindFile(const UnicodeString &FileName)
  1599. {
  1600. for (Integer Index = 0; Index < Count; Index++)
  1601. if (Files[Index]->FileName == FileName) return Files[Index];
  1602. return NULL;
  1603. }
  1604. //=== TRemoteDirectory ------------------------------------------------------
  1605. __fastcall TRemoteDirectory::TRemoteDirectory(TTerminal * aTerminal, TRemoteDirectory * Template) :
  1606. TRemoteFileList(), FTerminal(aTerminal)
  1607. {
  1608. FThisDirectory = NULL;
  1609. FParentDirectory = NULL;
  1610. if (Template == NULL)
  1611. {
  1612. FIncludeThisDirectory = false;
  1613. FIncludeParentDirectory = true;
  1614. }
  1615. else
  1616. {
  1617. FIncludeThisDirectory = Template->FIncludeThisDirectory;
  1618. FIncludeParentDirectory = Template->FIncludeParentDirectory;
  1619. }
  1620. }
  1621. //---------------------------------------------------------------------------
  1622. __fastcall TRemoteDirectory::~TRemoteDirectory()
  1623. {
  1624. ReleaseRelativeDirectories();
  1625. }
  1626. //---------------------------------------------------------------------------
  1627. void __fastcall TRemoteDirectory::ReleaseRelativeDirectories()
  1628. {
  1629. if ((ThisDirectory != NULL) && !IncludeThisDirectory)
  1630. {
  1631. delete FThisDirectory;
  1632. FThisDirectory = NULL;
  1633. }
  1634. if ((ParentDirectory != NULL) && !IncludeParentDirectory)
  1635. {
  1636. delete FParentDirectory;
  1637. FParentDirectory = NULL;
  1638. }
  1639. }
  1640. //---------------------------------------------------------------------------
  1641. void __fastcall TRemoteDirectory::Reset()
  1642. {
  1643. ReleaseRelativeDirectories();
  1644. TRemoteFileList::Reset();
  1645. }
  1646. //---------------------------------------------------------------------------
  1647. void __fastcall TRemoteDirectory::SetDirectory(UnicodeString value)
  1648. {
  1649. TRemoteFileList::SetDirectory(value);
  1650. }
  1651. //---------------------------------------------------------------------------
  1652. void __fastcall TRemoteDirectory::AddFile(TRemoteFile * File)
  1653. {
  1654. if (File->IsThisDirectory) FThisDirectory = File;
  1655. if (File->IsParentDirectory) FParentDirectory = File;
  1656. if ((!File->IsThisDirectory || IncludeThisDirectory) &&
  1657. (!File->IsParentDirectory || IncludeParentDirectory))
  1658. {
  1659. TRemoteFileList::AddFile(File);
  1660. }
  1661. File->Terminal = Terminal;
  1662. }
  1663. //---------------------------------------------------------------------------
  1664. void __fastcall TRemoteDirectory::DuplicateTo(TRemoteFileList * Copy)
  1665. {
  1666. TRemoteFileList::DuplicateTo(Copy);
  1667. if (ThisDirectory && !IncludeThisDirectory)
  1668. {
  1669. Copy->AddFile(ThisDirectory->Duplicate(false));
  1670. }
  1671. if (ParentDirectory && !IncludeParentDirectory)
  1672. {
  1673. Copy->AddFile(ParentDirectory->Duplicate(false));
  1674. }
  1675. }
  1676. //---------------------------------------------------------------------------
  1677. bool __fastcall TRemoteDirectory::GetLoaded()
  1678. {
  1679. return ((Terminal != NULL) && Terminal->Active && !Directory.IsEmpty());
  1680. }
  1681. //---------------------------------------------------------------------------
  1682. void __fastcall TRemoteDirectory::SetIncludeParentDirectory(Boolean value)
  1683. {
  1684. if (IncludeParentDirectory != value)
  1685. {
  1686. FIncludeParentDirectory = value;
  1687. if (value && ParentDirectory)
  1688. {
  1689. DebugAssert(IndexOf(ParentDirectory) < 0);
  1690. Add(ParentDirectory);
  1691. }
  1692. else if (!value && ParentDirectory)
  1693. {
  1694. DebugAssert(IndexOf(ParentDirectory) >= 0);
  1695. Extract(ParentDirectory);
  1696. }
  1697. }
  1698. }
  1699. //---------------------------------------------------------------------------
  1700. void __fastcall TRemoteDirectory::SetIncludeThisDirectory(Boolean value)
  1701. {
  1702. if (IncludeThisDirectory != value)
  1703. {
  1704. FIncludeThisDirectory = value;
  1705. if (value && ThisDirectory)
  1706. {
  1707. DebugAssert(IndexOf(ThisDirectory) < 0);
  1708. Add(ThisDirectory);
  1709. }
  1710. else if (!value && ThisDirectory)
  1711. {
  1712. DebugAssert(IndexOf(ThisDirectory) >= 0);
  1713. Extract(ThisDirectory);
  1714. }
  1715. }
  1716. }
  1717. //===========================================================================
  1718. __fastcall TRemoteDirectoryCache::TRemoteDirectoryCache(): TStringList()
  1719. {
  1720. FSection = new TCriticalSection();
  1721. Sorted = true;
  1722. Duplicates = Types::dupError;
  1723. CaseSensitive = true;
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. __fastcall TRemoteDirectoryCache::~TRemoteDirectoryCache()
  1727. {
  1728. Clear();
  1729. delete FSection;
  1730. }
  1731. //---------------------------------------------------------------------------
  1732. void __fastcall TRemoteDirectoryCache::Clear()
  1733. {
  1734. TGuard Guard(FSection);
  1735. try
  1736. {
  1737. for (int Index = 0; Index < Count; Index++)
  1738. {
  1739. delete (TRemoteFileList *)Objects[Index];
  1740. Objects[Index] = NULL;
  1741. }
  1742. }
  1743. __finally
  1744. {
  1745. TStringList::Clear();
  1746. }
  1747. }
  1748. //---------------------------------------------------------------------------
  1749. bool __fastcall TRemoteDirectoryCache::GetIsEmpty() const
  1750. {
  1751. TGuard Guard(FSection);
  1752. return (const_cast<TRemoteDirectoryCache*>(this)->Count == 0);
  1753. }
  1754. //---------------------------------------------------------------------------
  1755. bool __fastcall TRemoteDirectoryCache::HasFileList(const UnicodeString Directory)
  1756. {
  1757. TGuard Guard(FSection);
  1758. int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));
  1759. return (Index >= 0);
  1760. }
  1761. //---------------------------------------------------------------------------
  1762. bool __fastcall TRemoteDirectoryCache::HasNewerFileList(const UnicodeString Directory,
  1763. TDateTime Timestamp)
  1764. {
  1765. TGuard Guard(FSection);
  1766. int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));
  1767. if (Index >= 0)
  1768. {
  1769. TRemoteFileList * FileList = dynamic_cast<TRemoteFileList *>(Objects[Index]);
  1770. if (FileList->Timestamp <= Timestamp)
  1771. {
  1772. Index = -1;
  1773. }
  1774. }
  1775. return (Index >= 0);
  1776. }
  1777. //---------------------------------------------------------------------------
  1778. bool __fastcall TRemoteDirectoryCache::GetFileList(const UnicodeString Directory,
  1779. TRemoteFileList * FileList)
  1780. {
  1781. TGuard Guard(FSection);
  1782. int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));
  1783. bool Result = (Index >= 0);
  1784. if (Result)
  1785. {
  1786. DebugAssert(Objects[Index] != NULL);
  1787. dynamic_cast<TRemoteFileList *>(Objects[Index])->DuplicateTo(FileList);
  1788. }
  1789. return Result;
  1790. }
  1791. //---------------------------------------------------------------------------
  1792. void __fastcall TRemoteDirectoryCache::AddFileList(TRemoteFileList * FileList)
  1793. {
  1794. DebugAssert(FileList);
  1795. TRemoteFileList * Copy = new TRemoteFileList();
  1796. FileList->DuplicateTo(Copy);
  1797. {
  1798. TGuard Guard(FSection);
  1799. // file list cannot be cached already with only one thread, but it can be
  1800. // when directory is loaded by secondary terminal
  1801. DoClearFileList(FileList->Directory, false);
  1802. AddObject(Copy->Directory, Copy);
  1803. }
  1804. }
  1805. //---------------------------------------------------------------------------
  1806. void __fastcall TRemoteDirectoryCache::ClearFileList(UnicodeString Directory, bool SubDirs)
  1807. {
  1808. TGuard Guard(FSection);
  1809. DoClearFileList(Directory, SubDirs);
  1810. }
  1811. //---------------------------------------------------------------------------
  1812. void __fastcall TRemoteDirectoryCache::DoClearFileList(UnicodeString Directory, bool SubDirs)
  1813. {
  1814. Directory = UnixExcludeTrailingBackslash(Directory);
  1815. int Index = IndexOf(Directory);
  1816. if (Index >= 0)
  1817. {
  1818. Delete(Index);
  1819. }
  1820. if (SubDirs)
  1821. {
  1822. UnicodeString DirectoryWithSlash = UnixIncludeTrailingBackslash(Directory); // optimization
  1823. Index = Count-1;
  1824. while (Index >= 0)
  1825. {
  1826. if (UnixIsChildPath(DirectoryWithSlash, Strings[Index]))
  1827. {
  1828. Delete(Index);
  1829. }
  1830. Index--;
  1831. }
  1832. }
  1833. }
  1834. //---------------------------------------------------------------------------
  1835. void __fastcall TRemoteDirectoryCache::Delete(int Index)
  1836. {
  1837. delete dynamic_cast<TRemoteFileList *>(Objects[Index]);
  1838. TStringList::Delete(Index);
  1839. }
  1840. //---------------------------------------------------------------------------
  1841. //---------------------------------------------------------------------------
  1842. __fastcall TRemoteDirectoryChangesCache::TRemoteDirectoryChangesCache(int MaxSize) :
  1843. TStringList(),
  1844. FMaxSize(MaxSize)
  1845. {
  1846. CaseSensitive = true;
  1847. }
  1848. //---------------------------------------------------------------------------
  1849. void __fastcall TRemoteDirectoryChangesCache::Clear()
  1850. {
  1851. TStringList::Clear();
  1852. }
  1853. //---------------------------------------------------------------------------
  1854. bool __fastcall TRemoteDirectoryChangesCache::GetIsEmpty() const
  1855. {
  1856. return (const_cast<TRemoteDirectoryChangesCache*>(this)->Count == 0);
  1857. }
  1858. //---------------------------------------------------------------------------
  1859. void __fastcall TRemoteDirectoryChangesCache::SetValue(const UnicodeString & Name,
  1860. const UnicodeString & Value)
  1861. {
  1862. int Index = IndexOfName(Name);
  1863. if (Index > 0)
  1864. {
  1865. Delete(Index);
  1866. }
  1867. Values[Name] = Value;
  1868. }
  1869. //---------------------------------------------------------------------------
  1870. UnicodeString __fastcall TRemoteDirectoryChangesCache::GetValue(const UnicodeString & Name)
  1871. {
  1872. UnicodeString Value = Values[Name];
  1873. SetValue(Name, Value);
  1874. return Value;
  1875. }
  1876. //---------------------------------------------------------------------------
  1877. void __fastcall TRemoteDirectoryChangesCache::AddDirectoryChange(
  1878. const UnicodeString SourceDir, const UnicodeString Change,
  1879. const UnicodeString TargetDir)
  1880. {
  1881. DebugAssert(!TargetDir.IsEmpty());
  1882. SetValue(TargetDir, L"//");
  1883. if (TTerminal::ExpandFileName(Change, SourceDir) != TargetDir)
  1884. {
  1885. UnicodeString Key;
  1886. if (DirectoryChangeKey(SourceDir, Change, Key))
  1887. {
  1888. SetValue(Key, TargetDir);
  1889. }
  1890. }
  1891. }
  1892. //---------------------------------------------------------------------------
  1893. void __fastcall TRemoteDirectoryChangesCache::ClearDirectoryChange(
  1894. UnicodeString SourceDir)
  1895. {
  1896. for (int Index = 0; Index < Count; Index++)
  1897. {
  1898. if (Names[Index].SubString(1, SourceDir.Length()) == SourceDir)
  1899. {
  1900. Delete(Index);
  1901. Index--;
  1902. }
  1903. }
  1904. }
  1905. //---------------------------------------------------------------------------
  1906. void __fastcall TRemoteDirectoryChangesCache::ClearDirectoryChangeTarget(
  1907. UnicodeString TargetDir)
  1908. {
  1909. UnicodeString Key;
  1910. // hack to clear at least local sym-link change in case symlink is deleted
  1911. DirectoryChangeKey(UnixExcludeTrailingBackslash(UnixExtractFilePath(TargetDir)),
  1912. UnixExtractFileName(TargetDir), Key);
  1913. for (int Index = 0; Index < Count; Index++)
  1914. {
  1915. UnicodeString Name = Names[Index];
  1916. if ((Name.SubString(1, TargetDir.Length()) == TargetDir) ||
  1917. (Values[Name].SubString(1, TargetDir.Length()) == TargetDir) ||
  1918. (!Key.IsEmpty() && (Name == Key)))
  1919. {
  1920. Delete(Index);
  1921. Index--;
  1922. }
  1923. }
  1924. }
  1925. //---------------------------------------------------------------------------
  1926. bool __fastcall TRemoteDirectoryChangesCache::GetDirectoryChange(
  1927. const UnicodeString SourceDir, const UnicodeString Change, UnicodeString & TargetDir)
  1928. {
  1929. UnicodeString Key;
  1930. bool Result;
  1931. Key = TTerminal::ExpandFileName(Change, SourceDir);
  1932. Result = (IndexOfName(Key) >= 0);
  1933. if (Result)
  1934. {
  1935. TargetDir = GetValue(Key);
  1936. // TargetDir is not "//" here only when Change is full path to symbolic link
  1937. if (TargetDir == L"//")
  1938. {
  1939. TargetDir = Key;
  1940. }
  1941. }
  1942. else
  1943. {
  1944. Result = DirectoryChangeKey(SourceDir, Change, Key);
  1945. if (Result)
  1946. {
  1947. UnicodeString Directory = GetValue(Key);
  1948. Result = !Directory.IsEmpty();
  1949. if (Result)
  1950. {
  1951. TargetDir = Directory;
  1952. }
  1953. }
  1954. }
  1955. return Result;
  1956. }
  1957. //---------------------------------------------------------------------------
  1958. void __fastcall TRemoteDirectoryChangesCache::Serialize(UnicodeString & Data)
  1959. {
  1960. Data = L"A";
  1961. int ACount = Count;
  1962. if (ACount > FMaxSize)
  1963. {
  1964. TStrings * Limited = new TStringList();
  1965. try
  1966. {
  1967. int Index = ACount - FMaxSize;
  1968. while (Index < ACount)
  1969. {
  1970. Limited->Add(Strings[Index]);
  1971. Index++;
  1972. }
  1973. Data += Limited->Text;
  1974. }
  1975. __finally
  1976. {
  1977. delete Limited;
  1978. }
  1979. }
  1980. else
  1981. {
  1982. Data += Text;
  1983. }
  1984. }
  1985. //---------------------------------------------------------------------------
  1986. void __fastcall TRemoteDirectoryChangesCache::Deserialize(const UnicodeString Data)
  1987. {
  1988. if (Data.IsEmpty())
  1989. {
  1990. Text = L"";
  1991. }
  1992. else
  1993. {
  1994. Text = Data.c_str() + 1;
  1995. }
  1996. }
  1997. //---------------------------------------------------------------------------
  1998. bool __fastcall TRemoteDirectoryChangesCache::DirectoryChangeKey(
  1999. const UnicodeString SourceDir, const UnicodeString Change, UnicodeString & Key)
  2000. {
  2001. bool Result = !Change.IsEmpty();
  2002. if (Result)
  2003. {
  2004. bool Absolute = UnixIsAbsolutePath(Change);
  2005. Result = !SourceDir.IsEmpty() || Absolute;
  2006. if (Result)
  2007. {
  2008. // expanded from ?: to avoid memory leaks
  2009. if (Absolute)
  2010. {
  2011. Key = Change;
  2012. }
  2013. else
  2014. {
  2015. Key = SourceDir + L"," + Change;
  2016. }
  2017. }
  2018. }
  2019. return Result;
  2020. }
  2021. //=== TRights ---------------------------------------------------------------
  2022. const wchar_t TRights::BasicSymbols[] = L"rwxrwxrwx";
  2023. const wchar_t TRights::CombinedSymbols[] = L"--s--s--t";
  2024. const wchar_t TRights::ExtendedSymbols[] = L"--S--S--T";
  2025. const wchar_t TRights::ModeGroups[] = L"ugo";
  2026. //---------------------------------------------------------------------------
  2027. __fastcall TRights::TRights()
  2028. {
  2029. FAllowUndef = false;
  2030. FSet = 0;
  2031. FUnset = 0;
  2032. Number = 0;
  2033. FUnknown = true;
  2034. }
  2035. //---------------------------------------------------------------------------
  2036. __fastcall TRights::TRights(unsigned short ANumber)
  2037. {
  2038. FAllowUndef = false;
  2039. FSet = 0;
  2040. FUnset = 0;
  2041. Number = ANumber;
  2042. }
  2043. //---------------------------------------------------------------------------
  2044. __fastcall TRights::TRights(const TRights & Source)
  2045. {
  2046. Assign(&Source);
  2047. }
  2048. //---------------------------------------------------------------------------
  2049. //---------------------------------------------------------------------------
  2050. void __fastcall TRights::Assign(const TRights * Source)
  2051. {
  2052. FAllowUndef = Source->AllowUndef;
  2053. FSet = Source->FSet;
  2054. FUnset = Source->FUnset;
  2055. FText = Source->FText;
  2056. FUnknown = Source->FUnknown;
  2057. }
  2058. //---------------------------------------------------------------------------
  2059. TRights::TRight TRights::CalculateRight(TRightGroup Group, TRightLevel Level)
  2060. {
  2061. int Result;
  2062. if (Level == rlSpecial)
  2063. {
  2064. Result = rrUserIDExec + Group;
  2065. }
  2066. else
  2067. {
  2068. DebugAssert(rlRead == 0);
  2069. Result = rrUserRead + Level + (Group * 3);
  2070. }
  2071. return static_cast<TRight>(Result);
  2072. }
  2073. //---------------------------------------------------------------------------
  2074. TRights::TFlag __fastcall TRights::RightToFlag(TRights::TRight Right)
  2075. {
  2076. return static_cast<TFlag>(1 << (rrLast - Right));
  2077. }
  2078. //---------------------------------------------------------------------------
  2079. TRights::TFlag TRights::CalculateFlag(TRightGroup Group, TRightLevel Level)
  2080. {
  2081. return RightToFlag(CalculateRight(Group, Level));
  2082. }
  2083. //---------------------------------------------------------------------------
  2084. unsigned short TRights::CalculatePermissions(TRightGroup Group, TRightLevel Level, TRightLevel Level2, TRightLevel Level3)
  2085. {
  2086. unsigned int Permissions = CalculateFlag(Group, Level);
  2087. if (Level2 != rlNone)
  2088. {
  2089. Permissions |= CalculateFlag(Group, Level2);
  2090. }
  2091. if (Level3 != rlNone)
  2092. {
  2093. Permissions |= CalculateFlag(Group, Level3);
  2094. }
  2095. unsigned short Result = static_cast<unsigned short>(Permissions);
  2096. DebugAssert((Permissions - Result) == 0);
  2097. return Result;
  2098. }
  2099. //---------------------------------------------------------------------------
  2100. bool __fastcall TRights::operator ==(const TRights & rhr) const
  2101. {
  2102. if (AllowUndef || rhr.AllowUndef)
  2103. {
  2104. for (int Right = rrFirst; Right <= rrLast; Right++)
  2105. {
  2106. if (RightUndef[static_cast<TRight>(Right)] !=
  2107. rhr.RightUndef[static_cast<TRight>(Right)])
  2108. {
  2109. return false;
  2110. }
  2111. }
  2112. return true;
  2113. }
  2114. else
  2115. {
  2116. return (Number == rhr.Number);
  2117. }
  2118. }
  2119. //---------------------------------------------------------------------------
  2120. bool __fastcall TRights::operator ==(unsigned short rhr) const
  2121. {
  2122. return (Number == rhr);
  2123. }
  2124. //---------------------------------------------------------------------------
  2125. bool __fastcall TRights::operator !=(const TRights & rhr) const
  2126. {
  2127. return !(*this == rhr);
  2128. }
  2129. //---------------------------------------------------------------------------
  2130. TRights & __fastcall TRights::operator =(unsigned short rhr)
  2131. {
  2132. Number = rhr;
  2133. return *this;
  2134. }
  2135. //---------------------------------------------------------------------------
  2136. TRights & __fastcall TRights::operator =(const TRights & rhr)
  2137. {
  2138. Assign(&rhr);
  2139. return *this;
  2140. }
  2141. //---------------------------------------------------------------------------
  2142. TRights __fastcall TRights::operator ~() const
  2143. {
  2144. TRights Result(static_cast<unsigned short>(~Number));
  2145. return Result;
  2146. }
  2147. //---------------------------------------------------------------------------
  2148. TRights __fastcall TRights::operator &(const TRights & rhr) const
  2149. {
  2150. TRights Result(*this);
  2151. Result &= rhr;
  2152. return Result;
  2153. }
  2154. //---------------------------------------------------------------------------
  2155. TRights __fastcall TRights::operator &(unsigned short rhr) const
  2156. {
  2157. TRights Result(*this);
  2158. Result &= rhr;
  2159. return Result;
  2160. }
  2161. //---------------------------------------------------------------------------
  2162. TRights & __fastcall TRights::operator &=(const TRights & rhr)
  2163. {
  2164. if (AllowUndef || rhr.AllowUndef)
  2165. {
  2166. for (int Right = rrFirst; Right <= rrLast; Right++)
  2167. {
  2168. if (RightUndef[static_cast<TRight>(Right)] !=
  2169. rhr.RightUndef[static_cast<TRight>(Right)])
  2170. {
  2171. RightUndef[static_cast<TRight>(Right)] = rsUndef;
  2172. }
  2173. }
  2174. }
  2175. else
  2176. {
  2177. Number &= rhr.Number;
  2178. }
  2179. return *this;
  2180. }
  2181. //---------------------------------------------------------------------------
  2182. TRights & __fastcall TRights::operator &=(unsigned short rhr)
  2183. {
  2184. Number &= rhr;
  2185. return *this;
  2186. }
  2187. //---------------------------------------------------------------------------
  2188. TRights __fastcall TRights::operator |(const TRights & rhr) const
  2189. {
  2190. TRights Result(*this);
  2191. Result |= rhr;
  2192. return Result;
  2193. }
  2194. //---------------------------------------------------------------------------
  2195. TRights __fastcall TRights::operator |(unsigned short rhr) const
  2196. {
  2197. TRights Result(*this);
  2198. Result |= rhr;
  2199. return Result;
  2200. }
  2201. //---------------------------------------------------------------------------
  2202. TRights & __fastcall TRights::operator |=(const TRights & rhr)
  2203. {
  2204. Number |= rhr.Number;
  2205. return *this;
  2206. }
  2207. //---------------------------------------------------------------------------
  2208. TRights & __fastcall TRights::operator |=(unsigned short rhr)
  2209. {
  2210. Number |= rhr;
  2211. return *this;
  2212. }
  2213. //---------------------------------------------------------------------------
  2214. void __fastcall TRights::SetAllowUndef(bool value)
  2215. {
  2216. if (FAllowUndef != value)
  2217. {
  2218. DebugAssert(!value || ((FSet | FUnset) == rfAllSpecials));
  2219. FAllowUndef = value;
  2220. }
  2221. }
  2222. //---------------------------------------------------------------------------
  2223. void __fastcall TRights::SetText(const UnicodeString & value)
  2224. {
  2225. if (value != Text)
  2226. {
  2227. if ((value.Length() != TextLen) ||
  2228. (!AllowUndef && (value.Pos(UndefSymbol) > 0)) ||
  2229. (value.Pos(L" ") > 0))
  2230. {
  2231. throw Exception(FMTLOAD(RIGHTS_ERROR, (value)));
  2232. }
  2233. FSet = 0;
  2234. FUnset = 0;
  2235. int Flag = 00001;
  2236. int ExtendedFlag = 01000;
  2237. bool KeepText = false;
  2238. for (int i = TextLen; i >= 1; i--)
  2239. {
  2240. if ((value[i] == UnsetSymbol) || (value[i] == UnsetSymbolWin))
  2241. {
  2242. FUnset |= static_cast<unsigned short>(Flag | ExtendedFlag);
  2243. }
  2244. else if (value[i] == UndefSymbol)
  2245. {
  2246. // do nothing
  2247. }
  2248. else if (value[i] == CombinedSymbols[i - 1])
  2249. {
  2250. FSet |= static_cast<unsigned short>(Flag | ExtendedFlag);
  2251. }
  2252. else if (value[i] == ExtendedSymbols[i - 1])
  2253. {
  2254. FSet |= static_cast<unsigned short>(ExtendedFlag);
  2255. FUnset |= static_cast<unsigned short>(Flag);
  2256. }
  2257. else
  2258. {
  2259. if (value[i] != BasicSymbols[i - 1])
  2260. {
  2261. KeepText = true;
  2262. }
  2263. FSet |= static_cast<unsigned short>(Flag);
  2264. if (i % 3 == 0)
  2265. {
  2266. FUnset |= static_cast<unsigned short>(ExtendedFlag);
  2267. }
  2268. }
  2269. Flag <<= 1;
  2270. if (i % 3 == 1)
  2271. {
  2272. ExtendedFlag <<= 1;
  2273. }
  2274. }
  2275. FText = KeepText ? value : UnicodeString();
  2276. }
  2277. FUnknown = false;
  2278. }
  2279. //---------------------------------------------------------------------------
  2280. void TRights::SetTextOverride(const UnicodeString & value)
  2281. {
  2282. if (FText != value)
  2283. {
  2284. FText = value;
  2285. FUnknown = false;
  2286. }
  2287. }
  2288. //---------------------------------------------------------------------------
  2289. UnicodeString __fastcall TRights::GetText() const
  2290. {
  2291. if (!FText.IsEmpty())
  2292. {
  2293. return FText;
  2294. }
  2295. else
  2296. {
  2297. UnicodeString Result;
  2298. Result.SetLength(TextLen);
  2299. int Flag = 00001;
  2300. int ExtendedFlag = 01000;
  2301. bool ExtendedPos = true;
  2302. wchar_t Symbol;
  2303. int i = TextLen;
  2304. while (i >= 1)
  2305. {
  2306. if (ExtendedPos &&
  2307. ((FSet & (Flag | ExtendedFlag)) == (Flag | ExtendedFlag)))
  2308. {
  2309. Symbol = CombinedSymbols[i - 1];
  2310. }
  2311. else if ((FSet & Flag) != 0)
  2312. {
  2313. Symbol = BasicSymbols[i - 1];
  2314. }
  2315. else if (ExtendedPos && ((FSet & ExtendedFlag) != 0))
  2316. {
  2317. Symbol = ExtendedSymbols[i - 1];
  2318. }
  2319. else if ((!ExtendedPos && ((FUnset & Flag) == Flag)) ||
  2320. (ExtendedPos && ((FUnset & (Flag | ExtendedFlag)) == (Flag | ExtendedFlag))))
  2321. {
  2322. Symbol = UnsetSymbol;
  2323. }
  2324. else
  2325. {
  2326. Symbol = UndefSymbol;
  2327. }
  2328. Result[i] = Symbol;
  2329. Flag <<= 1;
  2330. i--;
  2331. ExtendedPos = ((i % 3) == 0);
  2332. if (ExtendedPos)
  2333. {
  2334. ExtendedFlag <<= 1;
  2335. }
  2336. }
  2337. return Result;
  2338. }
  2339. }
  2340. //---------------------------------------------------------------------------
  2341. void __fastcall TRights::SetOctal(UnicodeString value)
  2342. {
  2343. UnicodeString AValue(value);
  2344. if (AValue.Length() == 3)
  2345. {
  2346. AValue = L"0" + AValue;
  2347. }
  2348. if (Octal != AValue)
  2349. {
  2350. bool Correct = (AValue.Length() == 4);
  2351. if (Correct)
  2352. {
  2353. for (int i = 1; (i <= AValue.Length()) && Correct; i++)
  2354. {
  2355. Correct = (AValue[i] >= L'0') && (AValue[i] <= L'7');
  2356. }
  2357. }
  2358. if (!Correct)
  2359. {
  2360. throw Exception(FMTLOAD(INVALID_OCTAL_PERMISSIONS, (value)));
  2361. }
  2362. Number = static_cast<unsigned short>(
  2363. ((AValue[1] - L'0') << 9) +
  2364. ((AValue[2] - L'0') << 6) +
  2365. ((AValue[3] - L'0') << 3) +
  2366. ((AValue[4] - L'0') << 0));
  2367. FText = L"";
  2368. }
  2369. FUnknown = false;
  2370. }
  2371. //---------------------------------------------------------------------------
  2372. unsigned long __fastcall TRights::GetNumberDecadic() const
  2373. {
  2374. unsigned long N = NumberSet; // used to be "Number"
  2375. unsigned long Result =
  2376. ((N & 07000) / 01000 * 1000) +
  2377. ((N & 00700) / 0100 * 100) +
  2378. ((N & 00070) / 010 * 10) +
  2379. ((N & 00007) / 01 * 1);
  2380. return Result;
  2381. }
  2382. //---------------------------------------------------------------------------
  2383. UnicodeString __fastcall TRights::GetOctal() const
  2384. {
  2385. UnicodeString Result;
  2386. unsigned short N = NumberSet; // used to be "Number"
  2387. Result.SetLength(4);
  2388. Result[1] = static_cast<wchar_t>(L'0' + ((N & 07000) >> 9));
  2389. Result[2] = static_cast<wchar_t>(L'0' + ((N & 00700) >> 6));
  2390. Result[3] = static_cast<wchar_t>(L'0' + ((N & 00070) >> 3));
  2391. Result[4] = static_cast<wchar_t>(L'0' + ((N & 00007) >> 0));
  2392. return Result;
  2393. }
  2394. //---------------------------------------------------------------------------
  2395. void __fastcall TRights::SetNumber(unsigned short value)
  2396. {
  2397. if ((FSet != value) || ((FSet | FUnset) != rfAllSpecials))
  2398. {
  2399. FSet = value;
  2400. FUnset = static_cast<unsigned short>(rfAllSpecials & ~FSet);
  2401. FText = L"";
  2402. }
  2403. FUnknown = false;
  2404. }
  2405. //---------------------------------------------------------------------------
  2406. unsigned short __fastcall TRights::GetNumber() const
  2407. {
  2408. DebugAssert(!IsUndef);
  2409. return FSet;
  2410. }
  2411. //---------------------------------------------------------------------------
  2412. void __fastcall TRights::SetRight(TRight Right, bool value)
  2413. {
  2414. RightUndef[Right] = (value ? rsYes : rsNo);
  2415. }
  2416. //---------------------------------------------------------------------------
  2417. bool __fastcall TRights::GetRight(TRight Right) const
  2418. {
  2419. TState State = RightUndef[Right];
  2420. DebugAssert(State != rsUndef);
  2421. return (State == rsYes);
  2422. }
  2423. //---------------------------------------------------------------------------
  2424. void __fastcall TRights::SetRightUndef(TRight Right, TState value)
  2425. {
  2426. if (value != RightUndef[Right])
  2427. {
  2428. DebugAssert((value != rsUndef) || AllowUndef);
  2429. TFlag Flag = RightToFlag(Right);
  2430. switch (value)
  2431. {
  2432. case rsYes:
  2433. FSet |= static_cast<unsigned short>(Flag);
  2434. FUnset &= static_cast<unsigned short>(~Flag);
  2435. break;
  2436. case rsNo:
  2437. FSet &= static_cast<unsigned short>(~Flag);
  2438. FUnset |= static_cast<unsigned short>(Flag);
  2439. break;
  2440. case rsUndef:
  2441. default:
  2442. FSet &= static_cast<unsigned short>(~Flag);
  2443. FUnset &= static_cast<unsigned short>(~Flag);
  2444. break;
  2445. }
  2446. FText = L"";
  2447. }
  2448. FUnknown = false;
  2449. }
  2450. //---------------------------------------------------------------------------
  2451. TRights::TState __fastcall TRights::GetRightUndef(TRight Right) const
  2452. {
  2453. TFlag Flag = RightToFlag(Right);
  2454. TState Result;
  2455. if ((FSet & Flag) != 0)
  2456. {
  2457. Result = rsYes;
  2458. }
  2459. else if ((FUnset & Flag) != 0)
  2460. {
  2461. Result = rsNo;
  2462. }
  2463. else
  2464. {
  2465. Result = rsUndef;
  2466. }
  2467. return Result;
  2468. }
  2469. //---------------------------------------------------------------------------
  2470. void __fastcall TRights::SetReadOnly(bool value)
  2471. {
  2472. Right[rrUserWrite] = !value;
  2473. Right[rrGroupWrite] = !value;
  2474. Right[rrOtherWrite] = !value;
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. bool __fastcall TRights::GetReadOnly()
  2478. {
  2479. return Right[rrUserWrite] && Right[rrGroupWrite] && Right[rrOtherWrite];
  2480. }
  2481. //---------------------------------------------------------------------------
  2482. UnicodeString __fastcall TRights::GetChmodStr(int Directory) const
  2483. {
  2484. UnicodeString Result;
  2485. if (IsUndef)
  2486. {
  2487. Result = ModeStr;
  2488. }
  2489. else
  2490. {
  2491. Result = Octal;
  2492. if (Directory != 0) // unknown or folder
  2493. {
  2494. // New versions of coreutils need leading 5th zero to unset UID/GID for directories
  2495. Result = UnicodeString(L"0") + Result;
  2496. }
  2497. }
  2498. return Result;
  2499. }
  2500. //---------------------------------------------------------------------------
  2501. UnicodeString __fastcall TRights::GetModeStr() const
  2502. {
  2503. UnicodeString Result;
  2504. UnicodeString SetModeStr, UnsetModeStr;
  2505. TRight Right;
  2506. int Index;
  2507. for (int Group = 0; Group < 3; Group++)
  2508. {
  2509. SetModeStr = L"";
  2510. UnsetModeStr = L"";
  2511. for (int Mode = 0; Mode < 3; Mode++)
  2512. {
  2513. Index = (Group * 3) + Mode;
  2514. Right = static_cast<TRight>(rrUserRead + Index);
  2515. switch (RightUndef[Right])
  2516. {
  2517. case rsYes:
  2518. SetModeStr += BasicSymbols[Index];
  2519. break;
  2520. case rsNo:
  2521. UnsetModeStr += BasicSymbols[Index];
  2522. break;
  2523. }
  2524. }
  2525. Right = static_cast<TRight>(rrUserIDExec + Group);
  2526. Index = (Group * 3) + 2;
  2527. switch (RightUndef[Right])
  2528. {
  2529. case rsYes:
  2530. SetModeStr += CombinedSymbols[Index];
  2531. break;
  2532. case rsNo:
  2533. UnsetModeStr += CombinedSymbols[Index];
  2534. break;
  2535. }
  2536. if (!SetModeStr.IsEmpty() || !UnsetModeStr.IsEmpty())
  2537. {
  2538. if (!Result.IsEmpty())
  2539. {
  2540. Result += L',';
  2541. }
  2542. Result += ModeGroups[Group];
  2543. if (!SetModeStr.IsEmpty())
  2544. {
  2545. Result += L"+" + SetModeStr;
  2546. }
  2547. if (!UnsetModeStr.IsEmpty())
  2548. {
  2549. Result += L"-" + UnsetModeStr;
  2550. }
  2551. }
  2552. }
  2553. return Result;
  2554. }
  2555. //---------------------------------------------------------------------------
  2556. void __fastcall TRights::AddExecute()
  2557. {
  2558. for (int Group = 0; Group < 3; Group++)
  2559. {
  2560. if ((RightUndef[static_cast<TRight>(rrUserRead + (Group * 3))] == rsYes) ||
  2561. (RightUndef[static_cast<TRight>(rrUserWrite + (Group * 3))] == rsYes))
  2562. {
  2563. Right[static_cast<TRight>(rrUserExec + (Group * 3))] = true;
  2564. FText = L"";
  2565. }
  2566. }
  2567. FUnknown = false;
  2568. }
  2569. //---------------------------------------------------------------------------
  2570. void __fastcall TRights::AllUndef()
  2571. {
  2572. if ((FSet != 0) || (FUnset != 0))
  2573. {
  2574. FSet = 0;
  2575. FUnset = 0;
  2576. FText = L"";
  2577. }
  2578. FUnknown = false;
  2579. }
  2580. //---------------------------------------------------------------------------
  2581. bool __fastcall TRights::GetIsUndef() const
  2582. {
  2583. return ((FSet | FUnset) != rfAllSpecials);
  2584. }
  2585. //---------------------------------------------------------------------------
  2586. __fastcall TRights::operator unsigned short() const
  2587. {
  2588. return Number;
  2589. }
  2590. //---------------------------------------------------------------------------
  2591. __fastcall TRights::operator unsigned long() const
  2592. {
  2593. return Number;
  2594. }
  2595. //---------------------------------------------------------------------------
  2596. TRights TRights::Combine(const TRights & Other) const
  2597. {
  2598. TRights Result = (*this);
  2599. Result |= Other.NumberSet;
  2600. Result &= (unsigned short)~Other.NumberUnset;
  2601. return Result;
  2602. }
  2603. //=== TRemoteProperties -------------------------------------------------------
  2604. __fastcall TRemoteProperties::TRemoteProperties()
  2605. {
  2606. Default();
  2607. }
  2608. //---------------------------------------------------------------------------
  2609. __fastcall TRemoteProperties::TRemoteProperties(const TRemoteProperties & rhp) :
  2610. Valid(rhp.Valid),
  2611. Recursive(rhp.Recursive),
  2612. Rights(rhp.Rights),
  2613. AddXToDirectories(rhp.AddXToDirectories),
  2614. Group(rhp.Group),
  2615. Owner(rhp.Owner),
  2616. Modification(rhp.Modification),
  2617. LastAccess(rhp.Modification),
  2618. Encrypt(rhp.Encrypt),
  2619. Tags(rhp.Tags)
  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. Tags = EmptyStr;
  2634. }
  2635. //---------------------------------------------------------------------------
  2636. bool __fastcall TRemoteProperties::operator ==(const TRemoteProperties & rhp) const
  2637. {
  2638. bool Result = (Valid == rhp.Valid && Recursive == rhp.Recursive);
  2639. if (Result)
  2640. {
  2641. if ((Valid.Contains(vpRights) &&
  2642. (Rights != rhp.Rights || AddXToDirectories != rhp.AddXToDirectories)) ||
  2643. (Valid.Contains(vpOwner) && (Owner != rhp.Owner)) ||
  2644. (Valid.Contains(vpGroup) && (Group != rhp.Group)) ||
  2645. (Valid.Contains(vpModification) && (Modification != rhp.Modification)) ||
  2646. (Valid.Contains(vpLastAccess) && (LastAccess != rhp.LastAccess)) ||
  2647. (Valid.Contains(vpEncrypt) && (Encrypt != rhp.Encrypt)) ||
  2648. (Valid.Contains(vpTags) && (Tags != rhp.Tags)))
  2649. {
  2650. Result = false;
  2651. }
  2652. }
  2653. return Result;
  2654. }
  2655. //---------------------------------------------------------------------------
  2656. bool __fastcall TRemoteProperties::operator !=(const TRemoteProperties & rhp) const
  2657. {
  2658. return !(*this == rhp);
  2659. }
  2660. //---------------------------------------------------------------------------
  2661. TRemoteProperties __fastcall TRemoteProperties::CommonProperties(TStrings * FileList)
  2662. {
  2663. // TODO: Modification and LastAccess
  2664. TRemoteProperties CommonProperties;
  2665. for (int Index = 0; Index < FileList->Count; Index++)
  2666. {
  2667. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  2668. DebugAssert(File);
  2669. if (!Index)
  2670. {
  2671. if (!File->Rights->Unknown)
  2672. {
  2673. CommonProperties.Rights = *(File->Rights);
  2674. // previously we allowed undef implicitly for directories,
  2675. // now we do it explicitly in properties dialog and only in combination
  2676. // with "recursive" option
  2677. CommonProperties.Rights.AllowUndef = File->Rights->IsUndef;
  2678. CommonProperties.Valid << vpRights;
  2679. }
  2680. if (File->Owner.IsSet)
  2681. {
  2682. CommonProperties.Owner = File->Owner;
  2683. CommonProperties.Valid << vpOwner;
  2684. }
  2685. if (File->Group.IsSet)
  2686. {
  2687. CommonProperties.Group = File->Group;
  2688. CommonProperties.Valid << vpGroup;
  2689. }
  2690. CommonProperties.Tags = File->Tags;
  2691. CommonProperties.Valid << vpTags;
  2692. }
  2693. else
  2694. {
  2695. CommonProperties.Rights.AllowUndef = True;
  2696. CommonProperties.Rights &= *File->Rights;
  2697. if (CommonProperties.Owner != File->Owner)
  2698. {
  2699. CommonProperties.Owner.Clear();
  2700. CommonProperties.Valid >> vpOwner;
  2701. }
  2702. if (CommonProperties.Group != File->Group)
  2703. {
  2704. CommonProperties.Group.Clear();
  2705. CommonProperties.Valid >> vpGroup;
  2706. }
  2707. if (CommonProperties.Tags != File->Tags)
  2708. {
  2709. CommonProperties.Tags = EmptyStr;
  2710. CommonProperties.Valid >> vpTags;
  2711. }
  2712. }
  2713. }
  2714. return CommonProperties;
  2715. }
  2716. //---------------------------------------------------------------------------
  2717. TRemoteProperties __fastcall TRemoteProperties::ChangedProperties(
  2718. const TRemoteProperties & OriginalProperties, TRemoteProperties NewProperties)
  2719. {
  2720. DebugAssert(!OriginalProperties.Valid.Contains(vpEncrypt));
  2721. // TODO: Modification and LastAccess
  2722. if (!NewProperties.Recursive)
  2723. {
  2724. if (NewProperties.Rights == OriginalProperties.Rights &&
  2725. !NewProperties.AddXToDirectories)
  2726. {
  2727. NewProperties.Valid >> vpRights;
  2728. }
  2729. if (NewProperties.Group == OriginalProperties.Group)
  2730. {
  2731. NewProperties.Valid >> vpGroup;
  2732. }
  2733. if (NewProperties.Owner == OriginalProperties.Owner)
  2734. {
  2735. NewProperties.Valid >> vpOwner;
  2736. }
  2737. if (NewProperties.Tags == OriginalProperties.Tags)
  2738. {
  2739. NewProperties.Valid >> vpTags;
  2740. }
  2741. }
  2742. return NewProperties;
  2743. }
  2744. //---------------------------------------------------------------------------
  2745. void __fastcall TRemoteProperties::Load(THierarchicalStorage * Storage)
  2746. {
  2747. unsigned char Buf[sizeof(Valid)];
  2748. if (static_cast<size_t>(Storage->ReadBinaryData(L"Valid", &Buf, sizeof(Buf))) == sizeof(Buf))
  2749. {
  2750. memmove(&Valid, Buf, sizeof(Valid));
  2751. }
  2752. if (Valid.Contains(vpRights))
  2753. {
  2754. Rights.Text = Storage->ReadString(L"Rights", Rights.Text);
  2755. }
  2756. // TODO
  2757. }
  2758. //---------------------------------------------------------------------------
  2759. void __fastcall TRemoteProperties::Save(THierarchicalStorage * Storage) const
  2760. {
  2761. Storage->WriteBinaryData(UnicodeString(L"Valid"),
  2762. static_cast<const void *>(&Valid), sizeof(Valid));
  2763. if (Valid.Contains(vpRights))
  2764. {
  2765. Storage->WriteString(L"Rights", Rights.Text);
  2766. }
  2767. // TODO
  2768. }
  2769. //---------------------------------------------------------------------------
  2770. //---------------------------------------------------------------------------
  2771. TSynchronizeChecklist::TItem::TItem() :
  2772. Action(saNone), IsDirectory(false), ImageIndex(-1), Checked(true), RemoteFile(NULL), FDirectoryHasSize(false)
  2773. {
  2774. Local.ModificationFmt = mfFull;
  2775. Local.Modification = 0;
  2776. Local.Size = 0;
  2777. Remote.ModificationFmt = mfFull;
  2778. Remote.Modification = 0;
  2779. Remote.Size = 0;
  2780. }
  2781. //---------------------------------------------------------------------------
  2782. TSynchronizeChecklist::TItem::~TItem()
  2783. {
  2784. delete RemoteFile;
  2785. }
  2786. //---------------------------------------------------------------------------
  2787. const UnicodeString& TSynchronizeChecklist::TItem::GetFileName() const
  2788. {
  2789. if (!Remote.FileName.IsEmpty())
  2790. {
  2791. return Remote.FileName;
  2792. }
  2793. else
  2794. {
  2795. DebugAssert(!Local.FileName.IsEmpty());
  2796. return Local.FileName;
  2797. }
  2798. }
  2799. //---------------------------------------------------------------------------
  2800. __int64 __fastcall TSynchronizeChecklist::TItem::GetSize() const
  2801. {
  2802. return GetSize(Action);
  2803. }
  2804. //---------------------------------------------------------------------------
  2805. __int64 __fastcall TSynchronizeChecklist::TItem::GetSize(TAction AAction) const
  2806. {
  2807. if (IsItemSizeIrrelevant(AAction))
  2808. {
  2809. return 0;
  2810. }
  2811. else
  2812. {
  2813. return GetBaseSize(AAction);
  2814. }
  2815. }
  2816. //---------------------------------------------------------------------------
  2817. __int64 __fastcall TSynchronizeChecklist::TItem::GetBaseSize() const
  2818. {
  2819. return GetBaseSize(Action);
  2820. }
  2821. //---------------------------------------------------------------------------
  2822. __int64 __fastcall TSynchronizeChecklist::TItem::GetBaseSize(TAction AAction) const
  2823. {
  2824. switch (AAction)
  2825. {
  2826. case saUploadNew:
  2827. case saUploadUpdate:
  2828. case saDeleteLocal:
  2829. return Local.Size;
  2830. case saDownloadNew:
  2831. case saDownloadUpdate:
  2832. case saDeleteRemote:
  2833. return Remote.Size;
  2834. default:
  2835. DebugFail();
  2836. return 0;
  2837. }
  2838. }
  2839. //---------------------------------------------------------------------------
  2840. UnicodeString TSynchronizeChecklist::TItem::GetLocalPath() const
  2841. {
  2842. return CombinePaths(Local.Directory, Local.FileName);
  2843. }
  2844. //---------------------------------------------------------------------------
  2845. UnicodeString TSynchronizeChecklist::TItem::GetRemotePath() const
  2846. {
  2847. return UnixCombinePaths(Remote.Directory, Remote.FileName);
  2848. }
  2849. //---------------------------------------------------------------------------
  2850. UnicodeString TSynchronizeChecklist::TItem::GetLocalTarget() const
  2851. {
  2852. return IncludeTrailingBackslash(Local.Directory);
  2853. }
  2854. //---------------------------------------------------------------------------
  2855. UnicodeString TSynchronizeChecklist::TItem::GetRemoteTarget() const
  2856. {
  2857. return UnixIncludeTrailingBackslash(Remote.Directory);
  2858. };
  2859. //---------------------------------------------------------------------------
  2860. TStrings * TSynchronizeChecklist::TItem::GetFileList() const
  2861. {
  2862. std::unique_ptr<TStrings> FileList(new TStringList());
  2863. switch (Action)
  2864. {
  2865. case TSynchronizeChecklist::saDownloadNew:
  2866. case TSynchronizeChecklist::saDownloadUpdate:
  2867. case TSynchronizeChecklist::saDeleteRemote:
  2868. FileList->AddObject(GetRemotePath(), RemoteFile);
  2869. break;
  2870. case TSynchronizeChecklist::saUploadNew:
  2871. case TSynchronizeChecklist::saUploadUpdate:
  2872. case TSynchronizeChecklist::saDeleteLocal:
  2873. FileList->Add(GetLocalPath());
  2874. break;
  2875. default:
  2876. DebugFail();
  2877. NotImplemented();
  2878. break;
  2879. }
  2880. return FileList.release();
  2881. }
  2882. //---------------------------------------------------------------------------
  2883. //---------------------------------------------------------------------------
  2884. TSynchronizeChecklist::TSynchronizeChecklist() :
  2885. FList(new TList())
  2886. {
  2887. }
  2888. //---------------------------------------------------------------------------
  2889. TSynchronizeChecklist::~TSynchronizeChecklist()
  2890. {
  2891. for (int Index = 0; Index < FList->Count; Index++)
  2892. {
  2893. delete static_cast<TItem *>(FList->Items[Index]);
  2894. }
  2895. delete FList;
  2896. }
  2897. //---------------------------------------------------------------------------
  2898. void TSynchronizeChecklist::Add(TItem * Item)
  2899. {
  2900. FList->Add(Item);
  2901. }
  2902. //---------------------------------------------------------------------------
  2903. int TSynchronizeChecklist::Compare(const TItem * Item1, const TItem * Item2)
  2904. {
  2905. int Result;
  2906. if (!Item1->Local.Directory.IsEmpty())
  2907. {
  2908. Result = AnsiCompareText(Item1->Local.Directory, Item2->Local.Directory);
  2909. }
  2910. else
  2911. {
  2912. DebugAssert(!Item1->Remote.Directory.IsEmpty());
  2913. Result = AnsiCompareText(Item1->Remote.Directory, Item2->Remote.Directory);
  2914. }
  2915. if (Result == 0)
  2916. {
  2917. Result = AnsiCompareText(Item1->GetFileName(), Item2->GetFileName());
  2918. }
  2919. return Result;
  2920. }
  2921. //---------------------------------------------------------------------------
  2922. int __fastcall TSynchronizeChecklist::Compare(void * AItem1, void * AItem2)
  2923. {
  2924. return Compare(static_cast<TItem *>(AItem1), static_cast<TItem *>(AItem2));
  2925. }
  2926. //---------------------------------------------------------------------------
  2927. void TSynchronizeChecklist::Sort()
  2928. {
  2929. FList->Sort(Compare);
  2930. }
  2931. //---------------------------------------------------------------------------
  2932. int TSynchronizeChecklist::GetCount() const
  2933. {
  2934. return FList->Count;
  2935. }
  2936. //---------------------------------------------------------------------------
  2937. int TSynchronizeChecklist::GetCheckedCount() const
  2938. {
  2939. int Result = 0;
  2940. for (int Index = 0; (Index < Count); Index++)
  2941. {
  2942. if (Item[Index]->Checked)
  2943. {
  2944. Result++;
  2945. }
  2946. }
  2947. return Result;
  2948. }
  2949. //---------------------------------------------------------------------------
  2950. bool TSynchronizeChecklist::GetNextChecked(int & Index, const TItem *& AItem) const
  2951. {
  2952. while (Index < Count)
  2953. {
  2954. const TItem * TheItem = Item[Index];
  2955. Index++;
  2956. if (TheItem->Checked)
  2957. {
  2958. AItem = TheItem;
  2959. return true;
  2960. }
  2961. }
  2962. AItem = NULL;
  2963. return false;
  2964. }
  2965. //---------------------------------------------------------------------------
  2966. const TSynchronizeChecklist::TItem * TSynchronizeChecklist::GetItem(int Index) const
  2967. {
  2968. return static_cast<TItem *>(FList->Items[Index]);
  2969. }
  2970. //---------------------------------------------------------------------------
  2971. void __fastcall TSynchronizeChecklist::Update(const TItem * Item, bool Check, TAction Action)
  2972. {
  2973. // TSynchronizeChecklist owns non-const items so it can manipulate them freely,
  2974. // const_cast here is just an optimization
  2975. TItem * MutableItem = const_cast<TItem *>(Item);
  2976. DebugAssert(FList->IndexOf(MutableItem) >= 0);
  2977. MutableItem->Checked = Check;
  2978. MutableItem->Action = Action;
  2979. }
  2980. //---------------------------------------------------------------------------
  2981. void TSynchronizeChecklist::Delete(const TItem * Item)
  2982. {
  2983. // See comment in Update()
  2984. TItem * MutableItem = const_cast<TItem *>(Item);
  2985. FList->Extract(MutableItem);
  2986. delete Item;
  2987. }
  2988. //---------------------------------------------------------------------------
  2989. void __fastcall TSynchronizeChecklist::UpdateDirectorySize(const TItem * Item, __int64 Size)
  2990. {
  2991. // See comment in Update
  2992. TItem * MutableItem = const_cast<TItem *>(Item);
  2993. DebugAssert(FList->IndexOf(MutableItem) >= 0);
  2994. if (DebugAlwaysTrue(Item->IsDirectory))
  2995. {
  2996. MutableItem->FDirectoryHasSize = true;
  2997. if (Item->IsRemoteOnly())
  2998. {
  2999. MutableItem->Remote.Size = Size;
  3000. }
  3001. else if (Item->IsLocalOnly())
  3002. {
  3003. MutableItem->Local.Size = Size;
  3004. }
  3005. else
  3006. {
  3007. // "update" actions are not relevant for directories
  3008. DebugFail();
  3009. }
  3010. }
  3011. }
  3012. //---------------------------------------------------------------------------
  3013. TSynchronizeChecklist::TAction __fastcall TSynchronizeChecklist::Reverse(TSynchronizeChecklist::TAction Action)
  3014. {
  3015. switch (Action)
  3016. {
  3017. case saUploadNew:
  3018. return saDeleteLocal;
  3019. case saDownloadNew:
  3020. return saDeleteRemote;
  3021. case saUploadUpdate:
  3022. return saDownloadUpdate;
  3023. case saDownloadUpdate:
  3024. return saUploadUpdate;
  3025. case saDeleteRemote:
  3026. return saDownloadNew;
  3027. case saDeleteLocal:
  3028. return saUploadNew;
  3029. default:
  3030. case saNone:
  3031. DebugFail();
  3032. return saNone;
  3033. }
  3034. }
  3035. //---------------------------------------------------------------------------
  3036. bool __fastcall TSynchronizeChecklist::IsItemSizeIrrelevant(TAction Action)
  3037. {
  3038. switch (Action)
  3039. {
  3040. case saNone:
  3041. case saDeleteRemote:
  3042. case saDeleteLocal:
  3043. return true;
  3044. default:
  3045. return false;
  3046. }
  3047. }
  3048. //---------------------------------------------------------------------------
  3049. //---------------------------------------------------------------------------
  3050. TSynchronizeProgress::TSynchronizeProgress(const TSynchronizeChecklist * Checklist)
  3051. {
  3052. FTotalSize = -1;
  3053. FProcessedSize = 0;
  3054. FChecklist = Checklist;
  3055. }
  3056. //---------------------------------------------------------------------------
  3057. __int64 TSynchronizeProgress::ItemSize(const TSynchronizeChecklist::TItem * ChecklistItem) const
  3058. {
  3059. __int64 Result;
  3060. switch (ChecklistItem->Action)
  3061. {
  3062. case TSynchronizeChecklist::saDeleteRemote:
  3063. case TSynchronizeChecklist::saDeleteLocal:
  3064. Result = ChecklistItem->IsDirectory ? 1024*1024 : 100*1024;
  3065. break;
  3066. default:
  3067. if (ChecklistItem->HasSize())
  3068. {
  3069. Result = ChecklistItem->GetSize();
  3070. }
  3071. else
  3072. {
  3073. DebugAssert(ChecklistItem->IsDirectory);
  3074. Result = 1024*1024;
  3075. }
  3076. break;
  3077. }
  3078. return Result;
  3079. }
  3080. //---------------------------------------------------------------------------
  3081. void TSynchronizeProgress::ItemProcessed(const TSynchronizeChecklist::TItem * ChecklistItem)
  3082. {
  3083. FProcessedSize += ItemSize(ChecklistItem);
  3084. }
  3085. //---------------------------------------------------------------------------
  3086. __int64 TSynchronizeProgress::GetProcessed(const TFileOperationProgressType * CurrentItemOperationProgress) const
  3087. {
  3088. DebugAssert(!TFileOperationProgressType::IsIndeterminateOperation(CurrentItemOperationProgress->Operation));
  3089. // Need to calculate the total size on the first call only,
  3090. // as at the time the constructor it called, we usually do not have sizes of folders calculated yet.
  3091. if (FTotalSize < 0)
  3092. {
  3093. FTotalSize = 0;
  3094. int Index = 0;
  3095. const TSynchronizeChecklist::TItem * ChecklistItem;
  3096. while (FChecklist->GetNextChecked(Index, ChecklistItem))
  3097. {
  3098. FTotalSize += ItemSize(ChecklistItem);
  3099. }
  3100. }
  3101. // For (single-item-)delete operation, this should return 0
  3102. __int64 CurrentItemProcessedSize = CurrentItemOperationProgress->OperationTransferred;
  3103. return (FProcessedSize + CurrentItemProcessedSize);
  3104. }
  3105. //---------------------------------------------------------------------------
  3106. int TSynchronizeProgress::Progress(const TFileOperationProgressType * CurrentItemOperationProgress) const
  3107. {
  3108. int Result;
  3109. if (TFileOperationProgressType::IsIndeterminateOperation(CurrentItemOperationProgress->Operation))
  3110. {
  3111. Result = -1;
  3112. }
  3113. else
  3114. {
  3115. __int64 Processed = GetProcessed(CurrentItemOperationProgress);
  3116. if (FTotalSize > 0)
  3117. {
  3118. Result = static_cast<int>((Processed * 100) / FTotalSize);
  3119. }
  3120. else
  3121. {
  3122. Result = 0;
  3123. }
  3124. }
  3125. return Result;
  3126. }
  3127. //---------------------------------------------------------------------------
  3128. TDateTime TSynchronizeProgress::TimeLeft(const TFileOperationProgressType * CurrentItemOperationProgress) const
  3129. {
  3130. TDateTime Result;
  3131. __int64 Processed = GetProcessed(CurrentItemOperationProgress);
  3132. if (Processed > 0)
  3133. {
  3134. Result = TDateTime(double(Now() - CurrentItemOperationProgress->StartTime) / Processed * (FTotalSize - Processed));
  3135. }
  3136. return Result;
  3137. }