RemoteFiles.cpp 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249
  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. void TRemoteFileList::ExtractFile(TRemoteFile * File)
  1526. {
  1527. Extract(File);
  1528. File->Directory = NULL;
  1529. }
  1530. //---------------------------------------------------------------------------
  1531. TStrings * __fastcall TRemoteFileList::CloneStrings(TStrings * List)
  1532. {
  1533. std::unique_ptr<TStringList> Result(new TStringList());
  1534. Result->OwnsObjects = true;
  1535. for (int Index = 0; Index < List->Count; Index++)
  1536. {
  1537. TRemoteFile * File = static_cast<TRemoteFile *>(List->Objects[Index]);
  1538. Result->AddObject(List->Strings[Index], File->Duplicate(true));
  1539. }
  1540. return Result.release();
  1541. }
  1542. //---------------------------------------------------------------------------
  1543. bool TRemoteFileList::AnyDirectory(TStrings * List)
  1544. {
  1545. bool Result = false;
  1546. for (int Index = 0; !Result && (Index < List->Count); Index++)
  1547. {
  1548. Result = static_cast<TRemoteFile *>(List->Objects[Index])->IsDirectory;
  1549. }
  1550. return Result;
  1551. }
  1552. //---------------------------------------------------------------------------
  1553. void __fastcall TRemoteFileList::DuplicateTo(TRemoteFileList * Copy)
  1554. {
  1555. Copy->Reset();
  1556. for (int Index = 0; Index < Count; Index++)
  1557. {
  1558. TRemoteFile * File = Files[Index];
  1559. Copy->AddFile(File->Duplicate(false));
  1560. }
  1561. Copy->FDirectory = Directory;
  1562. Copy->FTimestamp = FTimestamp;
  1563. }
  1564. //---------------------------------------------------------------------------
  1565. void __fastcall TRemoteFileList::Reset()
  1566. {
  1567. FTimestamp = Now();
  1568. Clear();
  1569. }
  1570. //---------------------------------------------------------------------------
  1571. void __fastcall TRemoteFileList::SetDirectory(UnicodeString value)
  1572. {
  1573. FDirectory = UnixExcludeTrailingBackslash(value);
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. UnicodeString __fastcall TRemoteFileList::GetFullDirectory()
  1577. {
  1578. return UnixIncludeTrailingBackslash(Directory);
  1579. }
  1580. //---------------------------------------------------------------------------
  1581. TRemoteFile * __fastcall TRemoteFileList::GetFiles(Integer Index)
  1582. {
  1583. return (TRemoteFile *)Items[Index];
  1584. }
  1585. //---------------------------------------------------------------------------
  1586. Boolean __fastcall TRemoteFileList::GetIsRoot()
  1587. {
  1588. return (Directory == ROOTDIRECTORY);
  1589. }
  1590. //---------------------------------------------------------------------------
  1591. UnicodeString __fastcall TRemoteFileList::GetParentPath()
  1592. {
  1593. return UnixExtractFilePath(Directory);
  1594. }
  1595. //---------------------------------------------------------------------------
  1596. __int64 __fastcall TRemoteFileList::GetTotalSize()
  1597. {
  1598. __int64 Result = 0;
  1599. for (Integer Index = 0; Index < Count; Index++)
  1600. Result += Files[Index]->Size;
  1601. return Result;
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. TRemoteFile * __fastcall TRemoteFileList::FindFile(const UnicodeString &FileName)
  1605. {
  1606. for (Integer Index = 0; Index < Count; Index++)
  1607. if (Files[Index]->FileName == FileName) return Files[Index];
  1608. return NULL;
  1609. }
  1610. //=== TRemoteDirectory ------------------------------------------------------
  1611. __fastcall TRemoteDirectory::TRemoteDirectory(TTerminal * aTerminal, TRemoteDirectory * Template) :
  1612. TRemoteFileList(), FTerminal(aTerminal)
  1613. {
  1614. FThisDirectory = NULL;
  1615. FParentDirectory = NULL;
  1616. if (Template == NULL)
  1617. {
  1618. FIncludeThisDirectory = false;
  1619. FIncludeParentDirectory = true;
  1620. }
  1621. else
  1622. {
  1623. FIncludeThisDirectory = Template->FIncludeThisDirectory;
  1624. FIncludeParentDirectory = Template->FIncludeParentDirectory;
  1625. }
  1626. }
  1627. //---------------------------------------------------------------------------
  1628. __fastcall TRemoteDirectory::~TRemoteDirectory()
  1629. {
  1630. ReleaseRelativeDirectories();
  1631. }
  1632. //---------------------------------------------------------------------------
  1633. void __fastcall TRemoteDirectory::ReleaseRelativeDirectories()
  1634. {
  1635. if ((ThisDirectory != NULL) && !IncludeThisDirectory)
  1636. {
  1637. delete FThisDirectory;
  1638. FThisDirectory = NULL;
  1639. }
  1640. if ((ParentDirectory != NULL) && !IncludeParentDirectory)
  1641. {
  1642. delete FParentDirectory;
  1643. FParentDirectory = NULL;
  1644. }
  1645. }
  1646. //---------------------------------------------------------------------------
  1647. void __fastcall TRemoteDirectory::Reset()
  1648. {
  1649. ReleaseRelativeDirectories();
  1650. TRemoteFileList::Reset();
  1651. }
  1652. //---------------------------------------------------------------------------
  1653. void __fastcall TRemoteDirectory::SetDirectory(UnicodeString value)
  1654. {
  1655. TRemoteFileList::SetDirectory(value);
  1656. }
  1657. //---------------------------------------------------------------------------
  1658. void __fastcall TRemoteDirectory::AddFile(TRemoteFile * File)
  1659. {
  1660. if (File->IsThisDirectory) FThisDirectory = File;
  1661. if (File->IsParentDirectory) FParentDirectory = File;
  1662. if ((!File->IsThisDirectory || IncludeThisDirectory) &&
  1663. (!File->IsParentDirectory || IncludeParentDirectory))
  1664. {
  1665. TRemoteFileList::AddFile(File);
  1666. }
  1667. File->Terminal = Terminal;
  1668. }
  1669. //---------------------------------------------------------------------------
  1670. void __fastcall TRemoteDirectory::DuplicateTo(TRemoteFileList * Copy)
  1671. {
  1672. TRemoteFileList::DuplicateTo(Copy);
  1673. if (ThisDirectory && !IncludeThisDirectory)
  1674. {
  1675. Copy->AddFile(ThisDirectory->Duplicate(false));
  1676. }
  1677. if (ParentDirectory && !IncludeParentDirectory)
  1678. {
  1679. Copy->AddFile(ParentDirectory->Duplicate(false));
  1680. }
  1681. }
  1682. //---------------------------------------------------------------------------
  1683. bool __fastcall TRemoteDirectory::GetLoaded()
  1684. {
  1685. return ((Terminal != NULL) && Terminal->Active && !Directory.IsEmpty());
  1686. }
  1687. //---------------------------------------------------------------------------
  1688. void __fastcall TRemoteDirectory::SetIncludeParentDirectory(Boolean value)
  1689. {
  1690. if (IncludeParentDirectory != value)
  1691. {
  1692. FIncludeParentDirectory = value;
  1693. if (value && ParentDirectory)
  1694. {
  1695. DebugAssert(IndexOf(ParentDirectory) < 0);
  1696. AddFile(ParentDirectory);
  1697. }
  1698. else if (!value && ParentDirectory)
  1699. {
  1700. DebugAssert(IndexOf(ParentDirectory) >= 0);
  1701. ExtractFile(ParentDirectory);
  1702. }
  1703. }
  1704. }
  1705. //---------------------------------------------------------------------------
  1706. void __fastcall TRemoteDirectory::SetIncludeThisDirectory(Boolean value)
  1707. {
  1708. if (IncludeThisDirectory != value)
  1709. {
  1710. FIncludeThisDirectory = value;
  1711. if (value && ThisDirectory)
  1712. {
  1713. DebugAssert(IndexOf(ThisDirectory) < 0);
  1714. Add(ThisDirectory);
  1715. }
  1716. else if (!value && ThisDirectory)
  1717. {
  1718. DebugAssert(IndexOf(ThisDirectory) >= 0);
  1719. Extract(ThisDirectory);
  1720. }
  1721. }
  1722. }
  1723. //===========================================================================
  1724. __fastcall TRemoteDirectoryCache::TRemoteDirectoryCache(): TStringList()
  1725. {
  1726. FSection = new TCriticalSection();
  1727. Sorted = true;
  1728. Duplicates = Types::dupError;
  1729. CaseSensitive = true;
  1730. }
  1731. //---------------------------------------------------------------------------
  1732. __fastcall TRemoteDirectoryCache::~TRemoteDirectoryCache()
  1733. {
  1734. Clear();
  1735. delete FSection;
  1736. }
  1737. //---------------------------------------------------------------------------
  1738. void __fastcall TRemoteDirectoryCache::Clear()
  1739. {
  1740. TGuard Guard(FSection);
  1741. try
  1742. {
  1743. for (int Index = 0; Index < Count; Index++)
  1744. {
  1745. delete (TRemoteFileList *)Objects[Index];
  1746. Objects[Index] = NULL;
  1747. }
  1748. }
  1749. __finally
  1750. {
  1751. TStringList::Clear();
  1752. }
  1753. }
  1754. //---------------------------------------------------------------------------
  1755. bool __fastcall TRemoteDirectoryCache::GetIsEmpty() const
  1756. {
  1757. TGuard Guard(FSection);
  1758. return (const_cast<TRemoteDirectoryCache*>(this)->Count == 0);
  1759. }
  1760. //---------------------------------------------------------------------------
  1761. bool __fastcall TRemoteDirectoryCache::HasFileList(const UnicodeString Directory)
  1762. {
  1763. TGuard Guard(FSection);
  1764. int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));
  1765. return (Index >= 0);
  1766. }
  1767. //---------------------------------------------------------------------------
  1768. bool __fastcall TRemoteDirectoryCache::HasNewerFileList(const UnicodeString Directory,
  1769. TDateTime Timestamp)
  1770. {
  1771. TGuard Guard(FSection);
  1772. int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));
  1773. if (Index >= 0)
  1774. {
  1775. TRemoteFileList * FileList = dynamic_cast<TRemoteFileList *>(Objects[Index]);
  1776. if (FileList->Timestamp <= Timestamp)
  1777. {
  1778. Index = -1;
  1779. }
  1780. }
  1781. return (Index >= 0);
  1782. }
  1783. //---------------------------------------------------------------------------
  1784. bool __fastcall TRemoteDirectoryCache::GetFileList(const UnicodeString Directory,
  1785. TRemoteFileList * FileList)
  1786. {
  1787. TGuard Guard(FSection);
  1788. int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));
  1789. bool Result = (Index >= 0);
  1790. if (Result)
  1791. {
  1792. DebugAssert(Objects[Index] != NULL);
  1793. dynamic_cast<TRemoteFileList *>(Objects[Index])->DuplicateTo(FileList);
  1794. }
  1795. return Result;
  1796. }
  1797. //---------------------------------------------------------------------------
  1798. void __fastcall TRemoteDirectoryCache::AddFileList(TRemoteFileList * FileList)
  1799. {
  1800. DebugAssert(FileList);
  1801. TRemoteFileList * Copy = new TRemoteFileList();
  1802. FileList->DuplicateTo(Copy);
  1803. {
  1804. TGuard Guard(FSection);
  1805. // file list cannot be cached already with only one thread, but it can be
  1806. // when directory is loaded by secondary terminal
  1807. DoClearFileList(FileList->Directory, false);
  1808. AddObject(Copy->Directory, Copy);
  1809. }
  1810. }
  1811. //---------------------------------------------------------------------------
  1812. void __fastcall TRemoteDirectoryCache::ClearFileList(UnicodeString Directory, bool SubDirs)
  1813. {
  1814. TGuard Guard(FSection);
  1815. DoClearFileList(Directory, SubDirs);
  1816. }
  1817. //---------------------------------------------------------------------------
  1818. void __fastcall TRemoteDirectoryCache::DoClearFileList(UnicodeString Directory, bool SubDirs)
  1819. {
  1820. Directory = UnixExcludeTrailingBackslash(Directory);
  1821. int Index = IndexOf(Directory);
  1822. if (Index >= 0)
  1823. {
  1824. Delete(Index);
  1825. }
  1826. if (SubDirs)
  1827. {
  1828. UnicodeString DirectoryWithSlash = UnixIncludeTrailingBackslash(Directory); // optimization
  1829. Index = Count-1;
  1830. while (Index >= 0)
  1831. {
  1832. if (UnixIsChildPath(DirectoryWithSlash, Strings[Index]))
  1833. {
  1834. Delete(Index);
  1835. }
  1836. Index--;
  1837. }
  1838. }
  1839. }
  1840. //---------------------------------------------------------------------------
  1841. void __fastcall TRemoteDirectoryCache::Delete(int Index)
  1842. {
  1843. delete dynamic_cast<TRemoteFileList *>(Objects[Index]);
  1844. TStringList::Delete(Index);
  1845. }
  1846. //---------------------------------------------------------------------------
  1847. //---------------------------------------------------------------------------
  1848. __fastcall TRemoteDirectoryChangesCache::TRemoteDirectoryChangesCache(int MaxSize) :
  1849. TStringList(),
  1850. FMaxSize(MaxSize)
  1851. {
  1852. CaseSensitive = true;
  1853. }
  1854. //---------------------------------------------------------------------------
  1855. void __fastcall TRemoteDirectoryChangesCache::Clear()
  1856. {
  1857. TStringList::Clear();
  1858. }
  1859. //---------------------------------------------------------------------------
  1860. bool __fastcall TRemoteDirectoryChangesCache::GetIsEmpty() const
  1861. {
  1862. return (const_cast<TRemoteDirectoryChangesCache*>(this)->Count == 0);
  1863. }
  1864. //---------------------------------------------------------------------------
  1865. void __fastcall TRemoteDirectoryChangesCache::SetValue(const UnicodeString & Name,
  1866. const UnicodeString & Value)
  1867. {
  1868. int Index = IndexOfName(Name);
  1869. if (Index > 0)
  1870. {
  1871. Delete(Index);
  1872. }
  1873. Values[Name] = Value;
  1874. }
  1875. //---------------------------------------------------------------------------
  1876. UnicodeString __fastcall TRemoteDirectoryChangesCache::GetValue(const UnicodeString & Name)
  1877. {
  1878. UnicodeString Value = Values[Name];
  1879. SetValue(Name, Value);
  1880. return Value;
  1881. }
  1882. //---------------------------------------------------------------------------
  1883. void __fastcall TRemoteDirectoryChangesCache::AddDirectoryChange(
  1884. const UnicodeString SourceDir, const UnicodeString Change,
  1885. const UnicodeString TargetDir)
  1886. {
  1887. DebugAssert(!TargetDir.IsEmpty());
  1888. SetValue(TargetDir, L"//");
  1889. if (TTerminal::ExpandFileName(Change, SourceDir) != TargetDir)
  1890. {
  1891. UnicodeString Key;
  1892. if (DirectoryChangeKey(SourceDir, Change, Key))
  1893. {
  1894. SetValue(Key, TargetDir);
  1895. }
  1896. }
  1897. }
  1898. //---------------------------------------------------------------------------
  1899. void __fastcall TRemoteDirectoryChangesCache::ClearDirectoryChange(
  1900. UnicodeString SourceDir)
  1901. {
  1902. for (int Index = 0; Index < Count; Index++)
  1903. {
  1904. if (Names[Index].SubString(1, SourceDir.Length()) == SourceDir)
  1905. {
  1906. Delete(Index);
  1907. Index--;
  1908. }
  1909. }
  1910. }
  1911. //---------------------------------------------------------------------------
  1912. void __fastcall TRemoteDirectoryChangesCache::ClearDirectoryChangeTarget(
  1913. UnicodeString TargetDir)
  1914. {
  1915. UnicodeString Key;
  1916. // hack to clear at least local sym-link change in case symlink is deleted
  1917. DirectoryChangeKey(UnixExcludeTrailingBackslash(UnixExtractFilePath(TargetDir)),
  1918. UnixExtractFileName(TargetDir), Key);
  1919. for (int Index = 0; Index < Count; Index++)
  1920. {
  1921. UnicodeString Name = Names[Index];
  1922. if ((Name.SubString(1, TargetDir.Length()) == TargetDir) ||
  1923. (Values[Name].SubString(1, TargetDir.Length()) == TargetDir) ||
  1924. (!Key.IsEmpty() && (Name == Key)))
  1925. {
  1926. Delete(Index);
  1927. Index--;
  1928. }
  1929. }
  1930. }
  1931. //---------------------------------------------------------------------------
  1932. bool __fastcall TRemoteDirectoryChangesCache::GetDirectoryChange(
  1933. const UnicodeString SourceDir, const UnicodeString Change, UnicodeString & TargetDir)
  1934. {
  1935. UnicodeString Key;
  1936. bool Result;
  1937. Key = TTerminal::ExpandFileName(Change, SourceDir);
  1938. Result = (IndexOfName(Key) >= 0);
  1939. if (Result)
  1940. {
  1941. TargetDir = GetValue(Key);
  1942. // TargetDir is not "//" here only when Change is full path to symbolic link
  1943. if (TargetDir == L"//")
  1944. {
  1945. TargetDir = Key;
  1946. }
  1947. }
  1948. else
  1949. {
  1950. Result = DirectoryChangeKey(SourceDir, Change, Key);
  1951. if (Result)
  1952. {
  1953. UnicodeString Directory = GetValue(Key);
  1954. Result = !Directory.IsEmpty();
  1955. if (Result)
  1956. {
  1957. TargetDir = Directory;
  1958. }
  1959. }
  1960. }
  1961. return Result;
  1962. }
  1963. //---------------------------------------------------------------------------
  1964. void __fastcall TRemoteDirectoryChangesCache::Serialize(UnicodeString & Data)
  1965. {
  1966. Data = L"A";
  1967. int ACount = Count;
  1968. if (ACount > FMaxSize)
  1969. {
  1970. TStrings * Limited = new TStringList();
  1971. try
  1972. {
  1973. int Index = ACount - FMaxSize;
  1974. while (Index < ACount)
  1975. {
  1976. Limited->Add(Strings[Index]);
  1977. Index++;
  1978. }
  1979. Data += Limited->Text;
  1980. }
  1981. __finally
  1982. {
  1983. delete Limited;
  1984. }
  1985. }
  1986. else
  1987. {
  1988. Data += Text;
  1989. }
  1990. }
  1991. //---------------------------------------------------------------------------
  1992. void __fastcall TRemoteDirectoryChangesCache::Deserialize(const UnicodeString Data)
  1993. {
  1994. if (Data.IsEmpty())
  1995. {
  1996. Text = L"";
  1997. }
  1998. else
  1999. {
  2000. Text = Data.c_str() + 1;
  2001. }
  2002. }
  2003. //---------------------------------------------------------------------------
  2004. bool __fastcall TRemoteDirectoryChangesCache::DirectoryChangeKey(
  2005. const UnicodeString SourceDir, const UnicodeString Change, UnicodeString & Key)
  2006. {
  2007. bool Result = !Change.IsEmpty();
  2008. if (Result)
  2009. {
  2010. bool Absolute = UnixIsAbsolutePath(Change);
  2011. Result = !SourceDir.IsEmpty() || Absolute;
  2012. if (Result)
  2013. {
  2014. // expanded from ?: to avoid memory leaks
  2015. if (Absolute)
  2016. {
  2017. Key = Change;
  2018. }
  2019. else
  2020. {
  2021. Key = SourceDir + L"," + Change;
  2022. }
  2023. }
  2024. }
  2025. return Result;
  2026. }
  2027. //=== TRights ---------------------------------------------------------------
  2028. const wchar_t TRights::BasicSymbols[] = L"rwxrwxrwx";
  2029. const wchar_t TRights::CombinedSymbols[] = L"--s--s--t";
  2030. const wchar_t TRights::ExtendedSymbols[] = L"--S--S--T";
  2031. const wchar_t TRights::ModeGroups[] = L"ugo";
  2032. //---------------------------------------------------------------------------
  2033. __fastcall TRights::TRights()
  2034. {
  2035. FAllowUndef = false;
  2036. FSet = 0;
  2037. FUnset = 0;
  2038. Number = 0;
  2039. FUnknown = true;
  2040. }
  2041. //---------------------------------------------------------------------------
  2042. __fastcall TRights::TRights(unsigned short ANumber)
  2043. {
  2044. FAllowUndef = false;
  2045. FSet = 0;
  2046. FUnset = 0;
  2047. Number = ANumber;
  2048. }
  2049. //---------------------------------------------------------------------------
  2050. __fastcall TRights::TRights(const TRights & Source)
  2051. {
  2052. Assign(&Source);
  2053. }
  2054. //---------------------------------------------------------------------------
  2055. //---------------------------------------------------------------------------
  2056. void __fastcall TRights::Assign(const TRights * Source)
  2057. {
  2058. FAllowUndef = Source->AllowUndef;
  2059. FSet = Source->FSet;
  2060. FUnset = Source->FUnset;
  2061. FText = Source->FText;
  2062. FUnknown = Source->FUnknown;
  2063. }
  2064. //---------------------------------------------------------------------------
  2065. TRights::TRight TRights::CalculateRight(TRightGroup Group, TRightLevel Level)
  2066. {
  2067. int Result;
  2068. if (Level == rlSpecial)
  2069. {
  2070. Result = rrUserIDExec + Group;
  2071. }
  2072. else
  2073. {
  2074. DebugAssert(rlRead == 0);
  2075. Result = rrUserRead + Level + (Group * 3);
  2076. }
  2077. return static_cast<TRight>(Result);
  2078. }
  2079. //---------------------------------------------------------------------------
  2080. TRights::TFlag __fastcall TRights::RightToFlag(TRights::TRight Right)
  2081. {
  2082. return static_cast<TFlag>(1 << (rrLast - Right));
  2083. }
  2084. //---------------------------------------------------------------------------
  2085. TRights::TFlag TRights::CalculateFlag(TRightGroup Group, TRightLevel Level)
  2086. {
  2087. return RightToFlag(CalculateRight(Group, Level));
  2088. }
  2089. //---------------------------------------------------------------------------
  2090. unsigned short TRights::CalculatePermissions(TRightGroup Group, TRightLevel Level, TRightLevel Level2, TRightLevel Level3)
  2091. {
  2092. unsigned int Permissions = CalculateFlag(Group, Level);
  2093. if (Level2 != rlNone)
  2094. {
  2095. Permissions |= CalculateFlag(Group, Level2);
  2096. }
  2097. if (Level3 != rlNone)
  2098. {
  2099. Permissions |= CalculateFlag(Group, Level3);
  2100. }
  2101. unsigned short Result = static_cast<unsigned short>(Permissions);
  2102. DebugAssert((Permissions - Result) == 0);
  2103. return Result;
  2104. }
  2105. //---------------------------------------------------------------------------
  2106. bool __fastcall TRights::operator ==(const TRights & rhr) const
  2107. {
  2108. if (AllowUndef || rhr.AllowUndef)
  2109. {
  2110. for (int Right = rrFirst; Right <= rrLast; Right++)
  2111. {
  2112. if (RightUndef[static_cast<TRight>(Right)] !=
  2113. rhr.RightUndef[static_cast<TRight>(Right)])
  2114. {
  2115. return false;
  2116. }
  2117. }
  2118. return true;
  2119. }
  2120. else
  2121. {
  2122. return (Number == rhr.Number);
  2123. }
  2124. }
  2125. //---------------------------------------------------------------------------
  2126. bool __fastcall TRights::operator ==(unsigned short rhr) const
  2127. {
  2128. return (Number == rhr);
  2129. }
  2130. //---------------------------------------------------------------------------
  2131. bool __fastcall TRights::operator !=(const TRights & rhr) const
  2132. {
  2133. return !(*this == rhr);
  2134. }
  2135. //---------------------------------------------------------------------------
  2136. TRights & __fastcall TRights::operator =(unsigned short rhr)
  2137. {
  2138. Number = rhr;
  2139. return *this;
  2140. }
  2141. //---------------------------------------------------------------------------
  2142. TRights & __fastcall TRights::operator =(const TRights & rhr)
  2143. {
  2144. Assign(&rhr);
  2145. return *this;
  2146. }
  2147. //---------------------------------------------------------------------------
  2148. TRights __fastcall TRights::operator ~() const
  2149. {
  2150. TRights Result(static_cast<unsigned short>(~Number));
  2151. return Result;
  2152. }
  2153. //---------------------------------------------------------------------------
  2154. TRights __fastcall TRights::operator &(const TRights & rhr) const
  2155. {
  2156. TRights Result(*this);
  2157. Result &= rhr;
  2158. return Result;
  2159. }
  2160. //---------------------------------------------------------------------------
  2161. TRights __fastcall TRights::operator &(unsigned short rhr) const
  2162. {
  2163. TRights Result(*this);
  2164. Result &= rhr;
  2165. return Result;
  2166. }
  2167. //---------------------------------------------------------------------------
  2168. TRights & __fastcall TRights::operator &=(const TRights & rhr)
  2169. {
  2170. if (AllowUndef || rhr.AllowUndef)
  2171. {
  2172. for (int Right = rrFirst; Right <= rrLast; Right++)
  2173. {
  2174. if (RightUndef[static_cast<TRight>(Right)] !=
  2175. rhr.RightUndef[static_cast<TRight>(Right)])
  2176. {
  2177. RightUndef[static_cast<TRight>(Right)] = rsUndef;
  2178. }
  2179. }
  2180. }
  2181. else
  2182. {
  2183. Number &= rhr.Number;
  2184. }
  2185. return *this;
  2186. }
  2187. //---------------------------------------------------------------------------
  2188. TRights & __fastcall TRights::operator &=(unsigned short rhr)
  2189. {
  2190. Number &= rhr;
  2191. return *this;
  2192. }
  2193. //---------------------------------------------------------------------------
  2194. TRights __fastcall TRights::operator |(const TRights & rhr) const
  2195. {
  2196. TRights Result(*this);
  2197. Result |= rhr;
  2198. return Result;
  2199. }
  2200. //---------------------------------------------------------------------------
  2201. TRights __fastcall TRights::operator |(unsigned short rhr) const
  2202. {
  2203. TRights Result(*this);
  2204. Result |= rhr;
  2205. return Result;
  2206. }
  2207. //---------------------------------------------------------------------------
  2208. TRights & __fastcall TRights::operator |=(const TRights & rhr)
  2209. {
  2210. Number |= rhr.Number;
  2211. return *this;
  2212. }
  2213. //---------------------------------------------------------------------------
  2214. TRights & __fastcall TRights::operator |=(unsigned short rhr)
  2215. {
  2216. Number |= rhr;
  2217. return *this;
  2218. }
  2219. //---------------------------------------------------------------------------
  2220. void __fastcall TRights::SetAllowUndef(bool value)
  2221. {
  2222. if (FAllowUndef != value)
  2223. {
  2224. DebugAssert(!value || ((FSet | FUnset) == rfAllSpecials));
  2225. FAllowUndef = value;
  2226. }
  2227. }
  2228. //---------------------------------------------------------------------------
  2229. void __fastcall TRights::SetText(const UnicodeString & value)
  2230. {
  2231. if (value != Text)
  2232. {
  2233. if ((value.Length() != TextLen) ||
  2234. (!AllowUndef && (value.Pos(UndefSymbol) > 0)) ||
  2235. (value.Pos(L" ") > 0))
  2236. {
  2237. throw Exception(FMTLOAD(RIGHTS_ERROR, (value)));
  2238. }
  2239. FSet = 0;
  2240. FUnset = 0;
  2241. int Flag = 00001;
  2242. int ExtendedFlag = 01000;
  2243. bool KeepText = false;
  2244. for (int i = TextLen; i >= 1; i--)
  2245. {
  2246. if ((value[i] == UnsetSymbol) || (value[i] == UnsetSymbolWin))
  2247. {
  2248. FUnset |= static_cast<unsigned short>(Flag | ExtendedFlag);
  2249. }
  2250. else if (value[i] == UndefSymbol)
  2251. {
  2252. // do nothing
  2253. }
  2254. else if (value[i] == CombinedSymbols[i - 1])
  2255. {
  2256. FSet |= static_cast<unsigned short>(Flag | ExtendedFlag);
  2257. }
  2258. else if (value[i] == ExtendedSymbols[i - 1])
  2259. {
  2260. FSet |= static_cast<unsigned short>(ExtendedFlag);
  2261. FUnset |= static_cast<unsigned short>(Flag);
  2262. }
  2263. else
  2264. {
  2265. if (value[i] != BasicSymbols[i - 1])
  2266. {
  2267. KeepText = true;
  2268. }
  2269. FSet |= static_cast<unsigned short>(Flag);
  2270. if (i % 3 == 0)
  2271. {
  2272. FUnset |= static_cast<unsigned short>(ExtendedFlag);
  2273. }
  2274. }
  2275. Flag <<= 1;
  2276. if (i % 3 == 1)
  2277. {
  2278. ExtendedFlag <<= 1;
  2279. }
  2280. }
  2281. FText = KeepText ? value : UnicodeString();
  2282. }
  2283. FUnknown = false;
  2284. }
  2285. //---------------------------------------------------------------------------
  2286. void TRights::SetTextOverride(const UnicodeString & value)
  2287. {
  2288. if (FText != value)
  2289. {
  2290. FText = value;
  2291. FUnknown = false;
  2292. }
  2293. }
  2294. //---------------------------------------------------------------------------
  2295. UnicodeString __fastcall TRights::GetText() const
  2296. {
  2297. if (!FText.IsEmpty())
  2298. {
  2299. return FText;
  2300. }
  2301. else
  2302. {
  2303. UnicodeString Result;
  2304. Result.SetLength(TextLen);
  2305. int Flag = 00001;
  2306. int ExtendedFlag = 01000;
  2307. bool ExtendedPos = true;
  2308. wchar_t Symbol;
  2309. int i = TextLen;
  2310. while (i >= 1)
  2311. {
  2312. if (ExtendedPos &&
  2313. ((FSet & (Flag | ExtendedFlag)) == (Flag | ExtendedFlag)))
  2314. {
  2315. Symbol = CombinedSymbols[i - 1];
  2316. }
  2317. else if ((FSet & Flag) != 0)
  2318. {
  2319. Symbol = BasicSymbols[i - 1];
  2320. }
  2321. else if (ExtendedPos && ((FSet & ExtendedFlag) != 0))
  2322. {
  2323. Symbol = ExtendedSymbols[i - 1];
  2324. }
  2325. else if ((!ExtendedPos && ((FUnset & Flag) == Flag)) ||
  2326. (ExtendedPos && ((FUnset & (Flag | ExtendedFlag)) == (Flag | ExtendedFlag))))
  2327. {
  2328. Symbol = UnsetSymbol;
  2329. }
  2330. else
  2331. {
  2332. Symbol = UndefSymbol;
  2333. }
  2334. Result[i] = Symbol;
  2335. Flag <<= 1;
  2336. i--;
  2337. ExtendedPos = ((i % 3) == 0);
  2338. if (ExtendedPos)
  2339. {
  2340. ExtendedFlag <<= 1;
  2341. }
  2342. }
  2343. return Result;
  2344. }
  2345. }
  2346. //---------------------------------------------------------------------------
  2347. void __fastcall TRights::SetOctal(UnicodeString value)
  2348. {
  2349. UnicodeString AValue(value);
  2350. if (AValue.Length() == 3)
  2351. {
  2352. AValue = L"0" + AValue;
  2353. }
  2354. if (Octal != AValue)
  2355. {
  2356. bool Correct = (AValue.Length() == 4);
  2357. if (Correct)
  2358. {
  2359. for (int i = 1; (i <= AValue.Length()) && Correct; i++)
  2360. {
  2361. Correct = (AValue[i] >= L'0') && (AValue[i] <= L'7');
  2362. }
  2363. }
  2364. if (!Correct)
  2365. {
  2366. throw Exception(FMTLOAD(INVALID_OCTAL_PERMISSIONS, (value)));
  2367. }
  2368. Number = static_cast<unsigned short>(
  2369. ((AValue[1] - L'0') << 9) +
  2370. ((AValue[2] - L'0') << 6) +
  2371. ((AValue[3] - L'0') << 3) +
  2372. ((AValue[4] - L'0') << 0));
  2373. FText = L"";
  2374. }
  2375. FUnknown = false;
  2376. }
  2377. //---------------------------------------------------------------------------
  2378. unsigned long __fastcall TRights::GetNumberDecadic() const
  2379. {
  2380. unsigned long N = NumberSet; // used to be "Number"
  2381. unsigned long Result =
  2382. ((N & 07000) / 01000 * 1000) +
  2383. ((N & 00700) / 0100 * 100) +
  2384. ((N & 00070) / 010 * 10) +
  2385. ((N & 00007) / 01 * 1);
  2386. return Result;
  2387. }
  2388. //---------------------------------------------------------------------------
  2389. UnicodeString __fastcall TRights::GetOctal() const
  2390. {
  2391. UnicodeString Result;
  2392. unsigned short N = NumberSet; // used to be "Number"
  2393. Result.SetLength(4);
  2394. Result[1] = static_cast<wchar_t>(L'0' + ((N & 07000) >> 9));
  2395. Result[2] = static_cast<wchar_t>(L'0' + ((N & 00700) >> 6));
  2396. Result[3] = static_cast<wchar_t>(L'0' + ((N & 00070) >> 3));
  2397. Result[4] = static_cast<wchar_t>(L'0' + ((N & 00007) >> 0));
  2398. return Result;
  2399. }
  2400. //---------------------------------------------------------------------------
  2401. void __fastcall TRights::SetNumber(unsigned short value)
  2402. {
  2403. if ((FSet != value) || ((FSet | FUnset) != rfAllSpecials))
  2404. {
  2405. FSet = value;
  2406. FUnset = static_cast<unsigned short>(rfAllSpecials & ~FSet);
  2407. FText = L"";
  2408. }
  2409. FUnknown = false;
  2410. }
  2411. //---------------------------------------------------------------------------
  2412. unsigned short __fastcall TRights::GetNumber() const
  2413. {
  2414. DebugAssert(!IsUndef);
  2415. return FSet;
  2416. }
  2417. //---------------------------------------------------------------------------
  2418. void __fastcall TRights::SetRight(TRight Right, bool value)
  2419. {
  2420. RightUndef[Right] = (value ? rsYes : rsNo);
  2421. }
  2422. //---------------------------------------------------------------------------
  2423. bool __fastcall TRights::GetRight(TRight Right) const
  2424. {
  2425. TState State = RightUndef[Right];
  2426. DebugAssert(State != rsUndef);
  2427. return (State == rsYes);
  2428. }
  2429. //---------------------------------------------------------------------------
  2430. void __fastcall TRights::SetRightUndef(TRight Right, TState value)
  2431. {
  2432. if (value != RightUndef[Right])
  2433. {
  2434. DebugAssert((value != rsUndef) || AllowUndef);
  2435. TFlag Flag = RightToFlag(Right);
  2436. switch (value)
  2437. {
  2438. case rsYes:
  2439. FSet |= static_cast<unsigned short>(Flag);
  2440. FUnset &= static_cast<unsigned short>(~Flag);
  2441. break;
  2442. case rsNo:
  2443. FSet &= static_cast<unsigned short>(~Flag);
  2444. FUnset |= static_cast<unsigned short>(Flag);
  2445. break;
  2446. case rsUndef:
  2447. default:
  2448. FSet &= static_cast<unsigned short>(~Flag);
  2449. FUnset &= static_cast<unsigned short>(~Flag);
  2450. break;
  2451. }
  2452. FText = L"";
  2453. }
  2454. FUnknown = false;
  2455. }
  2456. //---------------------------------------------------------------------------
  2457. TRights::TState __fastcall TRights::GetRightUndef(TRight Right) const
  2458. {
  2459. TFlag Flag = RightToFlag(Right);
  2460. TState Result;
  2461. if ((FSet & Flag) != 0)
  2462. {
  2463. Result = rsYes;
  2464. }
  2465. else if ((FUnset & Flag) != 0)
  2466. {
  2467. Result = rsNo;
  2468. }
  2469. else
  2470. {
  2471. Result = rsUndef;
  2472. }
  2473. return Result;
  2474. }
  2475. //---------------------------------------------------------------------------
  2476. void __fastcall TRights::SetReadOnly(bool value)
  2477. {
  2478. Right[rrUserWrite] = !value;
  2479. Right[rrGroupWrite] = !value;
  2480. Right[rrOtherWrite] = !value;
  2481. }
  2482. //---------------------------------------------------------------------------
  2483. bool __fastcall TRights::GetReadOnly()
  2484. {
  2485. return Right[rrUserWrite] && Right[rrGroupWrite] && Right[rrOtherWrite];
  2486. }
  2487. //---------------------------------------------------------------------------
  2488. UnicodeString __fastcall TRights::GetChmodStr(int Directory) const
  2489. {
  2490. UnicodeString Result;
  2491. if (IsUndef)
  2492. {
  2493. Result = ModeStr;
  2494. }
  2495. else
  2496. {
  2497. Result = Octal;
  2498. if (Directory != 0) // unknown or folder
  2499. {
  2500. // New versions of coreutils need leading 5th zero to unset UID/GID for directories
  2501. Result = UnicodeString(L"0") + Result;
  2502. }
  2503. }
  2504. return Result;
  2505. }
  2506. //---------------------------------------------------------------------------
  2507. UnicodeString __fastcall TRights::GetModeStr() const
  2508. {
  2509. UnicodeString Result;
  2510. UnicodeString SetModeStr, UnsetModeStr;
  2511. TRight Right;
  2512. int Index;
  2513. for (int Group = 0; Group < 3; Group++)
  2514. {
  2515. SetModeStr = L"";
  2516. UnsetModeStr = L"";
  2517. for (int Mode = 0; Mode < 3; Mode++)
  2518. {
  2519. Index = (Group * 3) + Mode;
  2520. Right = static_cast<TRight>(rrUserRead + Index);
  2521. switch (RightUndef[Right])
  2522. {
  2523. case rsYes:
  2524. SetModeStr += BasicSymbols[Index];
  2525. break;
  2526. case rsNo:
  2527. UnsetModeStr += BasicSymbols[Index];
  2528. break;
  2529. }
  2530. }
  2531. Right = static_cast<TRight>(rrUserIDExec + Group);
  2532. Index = (Group * 3) + 2;
  2533. switch (RightUndef[Right])
  2534. {
  2535. case rsYes:
  2536. SetModeStr += CombinedSymbols[Index];
  2537. break;
  2538. case rsNo:
  2539. UnsetModeStr += CombinedSymbols[Index];
  2540. break;
  2541. }
  2542. if (!SetModeStr.IsEmpty() || !UnsetModeStr.IsEmpty())
  2543. {
  2544. if (!Result.IsEmpty())
  2545. {
  2546. Result += L',';
  2547. }
  2548. Result += ModeGroups[Group];
  2549. if (!SetModeStr.IsEmpty())
  2550. {
  2551. Result += L"+" + SetModeStr;
  2552. }
  2553. if (!UnsetModeStr.IsEmpty())
  2554. {
  2555. Result += L"-" + UnsetModeStr;
  2556. }
  2557. }
  2558. }
  2559. return Result;
  2560. }
  2561. //---------------------------------------------------------------------------
  2562. void __fastcall TRights::AddExecute()
  2563. {
  2564. for (int Group = 0; Group < 3; Group++)
  2565. {
  2566. if ((RightUndef[static_cast<TRight>(rrUserRead + (Group * 3))] == rsYes) ||
  2567. (RightUndef[static_cast<TRight>(rrUserWrite + (Group * 3))] == rsYes))
  2568. {
  2569. Right[static_cast<TRight>(rrUserExec + (Group * 3))] = true;
  2570. FText = L"";
  2571. }
  2572. }
  2573. FUnknown = false;
  2574. }
  2575. //---------------------------------------------------------------------------
  2576. void __fastcall TRights::AllUndef()
  2577. {
  2578. if ((FSet != 0) || (FUnset != 0))
  2579. {
  2580. FSet = 0;
  2581. FUnset = 0;
  2582. FText = L"";
  2583. }
  2584. FUnknown = false;
  2585. }
  2586. //---------------------------------------------------------------------------
  2587. bool __fastcall TRights::GetIsUndef() const
  2588. {
  2589. return ((FSet | FUnset) != rfAllSpecials);
  2590. }
  2591. //---------------------------------------------------------------------------
  2592. __fastcall TRights::operator unsigned short() const
  2593. {
  2594. return Number;
  2595. }
  2596. //---------------------------------------------------------------------------
  2597. __fastcall TRights::operator unsigned long() const
  2598. {
  2599. return Number;
  2600. }
  2601. //---------------------------------------------------------------------------
  2602. TRights TRights::Combine(const TRights & Other) const
  2603. {
  2604. TRights Result = (*this);
  2605. Result |= Other.NumberSet;
  2606. Result &= (unsigned short)~Other.NumberUnset;
  2607. return Result;
  2608. }
  2609. //=== TRemoteProperties -------------------------------------------------------
  2610. __fastcall TRemoteProperties::TRemoteProperties()
  2611. {
  2612. Default();
  2613. }
  2614. //---------------------------------------------------------------------------
  2615. __fastcall TRemoteProperties::TRemoteProperties(const TRemoteProperties & rhp) :
  2616. Valid(rhp.Valid),
  2617. Recursive(rhp.Recursive),
  2618. Rights(rhp.Rights),
  2619. AddXToDirectories(rhp.AddXToDirectories),
  2620. Group(rhp.Group),
  2621. Owner(rhp.Owner),
  2622. Modification(rhp.Modification),
  2623. LastAccess(rhp.Modification),
  2624. Encrypt(rhp.Encrypt),
  2625. Tags(rhp.Tags)
  2626. {
  2627. }
  2628. //---------------------------------------------------------------------------
  2629. void __fastcall TRemoteProperties::Default()
  2630. {
  2631. Valid.Clear();
  2632. AddXToDirectories = false;
  2633. Rights.AllowUndef = false;
  2634. Rights.Number = 0;
  2635. Group.Clear();
  2636. Owner.Clear();
  2637. Recursive = false;
  2638. Encrypt = false;
  2639. Tags = EmptyStr;
  2640. }
  2641. //---------------------------------------------------------------------------
  2642. bool __fastcall TRemoteProperties::operator ==(const TRemoteProperties & rhp) const
  2643. {
  2644. bool Result = (Valid == rhp.Valid && Recursive == rhp.Recursive);
  2645. if (Result)
  2646. {
  2647. if ((Valid.Contains(vpRights) &&
  2648. (Rights != rhp.Rights || AddXToDirectories != rhp.AddXToDirectories)) ||
  2649. (Valid.Contains(vpOwner) && (Owner != rhp.Owner)) ||
  2650. (Valid.Contains(vpGroup) && (Group != rhp.Group)) ||
  2651. (Valid.Contains(vpModification) && (Modification != rhp.Modification)) ||
  2652. (Valid.Contains(vpLastAccess) && (LastAccess != rhp.LastAccess)) ||
  2653. (Valid.Contains(vpEncrypt) && (Encrypt != rhp.Encrypt)) ||
  2654. (Valid.Contains(vpTags) && (Tags != rhp.Tags)))
  2655. {
  2656. Result = false;
  2657. }
  2658. }
  2659. return Result;
  2660. }
  2661. //---------------------------------------------------------------------------
  2662. bool __fastcall TRemoteProperties::operator !=(const TRemoteProperties & rhp) const
  2663. {
  2664. return !(*this == rhp);
  2665. }
  2666. //---------------------------------------------------------------------------
  2667. TRemoteProperties __fastcall TRemoteProperties::CommonProperties(TStrings * FileList)
  2668. {
  2669. // TODO: Modification and LastAccess
  2670. TRemoteProperties CommonProperties;
  2671. for (int Index = 0; Index < FileList->Count; Index++)
  2672. {
  2673. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  2674. DebugAssert(File);
  2675. if (!Index)
  2676. {
  2677. if (!File->Rights->Unknown)
  2678. {
  2679. CommonProperties.Rights = *(File->Rights);
  2680. // previously we allowed undef implicitly for directories,
  2681. // now we do it explicitly in properties dialog and only in combination
  2682. // with "recursive" option
  2683. CommonProperties.Rights.AllowUndef = File->Rights->IsUndef;
  2684. CommonProperties.Valid << vpRights;
  2685. }
  2686. if (File->Owner.IsSet)
  2687. {
  2688. CommonProperties.Owner = File->Owner;
  2689. CommonProperties.Valid << vpOwner;
  2690. }
  2691. if (File->Group.IsSet)
  2692. {
  2693. CommonProperties.Group = File->Group;
  2694. CommonProperties.Valid << vpGroup;
  2695. }
  2696. CommonProperties.Tags = File->Tags;
  2697. CommonProperties.Valid << vpTags;
  2698. }
  2699. else
  2700. {
  2701. CommonProperties.Rights.AllowUndef = True;
  2702. CommonProperties.Rights &= *File->Rights;
  2703. if (CommonProperties.Owner != File->Owner)
  2704. {
  2705. CommonProperties.Owner.Clear();
  2706. CommonProperties.Valid >> vpOwner;
  2707. }
  2708. if (CommonProperties.Group != File->Group)
  2709. {
  2710. CommonProperties.Group.Clear();
  2711. CommonProperties.Valid >> vpGroup;
  2712. }
  2713. if (CommonProperties.Tags != File->Tags)
  2714. {
  2715. CommonProperties.Tags = EmptyStr;
  2716. CommonProperties.Valid >> vpTags;
  2717. }
  2718. }
  2719. }
  2720. return CommonProperties;
  2721. }
  2722. //---------------------------------------------------------------------------
  2723. TRemoteProperties __fastcall TRemoteProperties::ChangedProperties(
  2724. const TRemoteProperties & OriginalProperties, TRemoteProperties NewProperties)
  2725. {
  2726. DebugAssert(!OriginalProperties.Valid.Contains(vpEncrypt));
  2727. // TODO: Modification and LastAccess
  2728. if (!NewProperties.Recursive)
  2729. {
  2730. if (NewProperties.Rights == OriginalProperties.Rights &&
  2731. !NewProperties.AddXToDirectories)
  2732. {
  2733. NewProperties.Valid >> vpRights;
  2734. }
  2735. if (NewProperties.Group == OriginalProperties.Group)
  2736. {
  2737. NewProperties.Valid >> vpGroup;
  2738. }
  2739. if (NewProperties.Owner == OriginalProperties.Owner)
  2740. {
  2741. NewProperties.Valid >> vpOwner;
  2742. }
  2743. if (NewProperties.Tags == OriginalProperties.Tags)
  2744. {
  2745. NewProperties.Valid >> vpTags;
  2746. }
  2747. }
  2748. return NewProperties;
  2749. }
  2750. //---------------------------------------------------------------------------
  2751. void __fastcall TRemoteProperties::Load(THierarchicalStorage * Storage)
  2752. {
  2753. unsigned char Buf[sizeof(Valid)];
  2754. if (static_cast<size_t>(Storage->ReadBinaryData(L"Valid", &Buf, sizeof(Buf))) == sizeof(Buf))
  2755. {
  2756. memmove(&Valid, Buf, sizeof(Valid));
  2757. }
  2758. if (Valid.Contains(vpRights))
  2759. {
  2760. Rights.Text = Storage->ReadString(L"Rights", Rights.Text);
  2761. }
  2762. // TODO
  2763. }
  2764. //---------------------------------------------------------------------------
  2765. void __fastcall TRemoteProperties::Save(THierarchicalStorage * Storage) const
  2766. {
  2767. Storage->WriteBinaryData(UnicodeString(L"Valid"),
  2768. static_cast<const void *>(&Valid), sizeof(Valid));
  2769. if (Valid.Contains(vpRights))
  2770. {
  2771. Storage->WriteString(L"Rights", Rights.Text);
  2772. }
  2773. // TODO
  2774. }
  2775. //---------------------------------------------------------------------------
  2776. //---------------------------------------------------------------------------
  2777. TSynchronizeChecklist::TItem::TItem() :
  2778. Action(saNone), IsDirectory(false), ImageIndex(-1), Checked(true), RemoteFile(NULL), FDirectoryHasSize(false)
  2779. {
  2780. Local.ModificationFmt = mfFull;
  2781. Local.Modification = 0;
  2782. Local.Size = 0;
  2783. Remote.ModificationFmt = mfFull;
  2784. Remote.Modification = 0;
  2785. Remote.Size = 0;
  2786. }
  2787. //---------------------------------------------------------------------------
  2788. TSynchronizeChecklist::TItem::~TItem()
  2789. {
  2790. delete RemoteFile;
  2791. }
  2792. //---------------------------------------------------------------------------
  2793. const UnicodeString& TSynchronizeChecklist::TItem::GetFileName() const
  2794. {
  2795. if (!Remote.FileName.IsEmpty())
  2796. {
  2797. return Remote.FileName;
  2798. }
  2799. else
  2800. {
  2801. DebugAssert(!Local.FileName.IsEmpty());
  2802. return Local.FileName;
  2803. }
  2804. }
  2805. //---------------------------------------------------------------------------
  2806. __int64 __fastcall TSynchronizeChecklist::TItem::GetSize() const
  2807. {
  2808. return GetSize(Action);
  2809. }
  2810. //---------------------------------------------------------------------------
  2811. __int64 __fastcall TSynchronizeChecklist::TItem::GetSize(TAction AAction) const
  2812. {
  2813. if (IsItemSizeIrrelevant(AAction))
  2814. {
  2815. return 0;
  2816. }
  2817. else
  2818. {
  2819. return GetBaseSize(AAction);
  2820. }
  2821. }
  2822. //---------------------------------------------------------------------------
  2823. __int64 __fastcall TSynchronizeChecklist::TItem::GetBaseSize() const
  2824. {
  2825. return GetBaseSize(Action);
  2826. }
  2827. //---------------------------------------------------------------------------
  2828. __int64 __fastcall TSynchronizeChecklist::TItem::GetBaseSize(TAction AAction) const
  2829. {
  2830. switch (AAction)
  2831. {
  2832. case saUploadNew:
  2833. case saUploadUpdate:
  2834. case saDeleteLocal:
  2835. return Local.Size;
  2836. case saDownloadNew:
  2837. case saDownloadUpdate:
  2838. case saDeleteRemote:
  2839. return Remote.Size;
  2840. default:
  2841. DebugFail();
  2842. return 0;
  2843. }
  2844. }
  2845. //---------------------------------------------------------------------------
  2846. UnicodeString TSynchronizeChecklist::TItem::GetLocalPath() const
  2847. {
  2848. return CombinePaths(Local.Directory, Local.FileName);
  2849. }
  2850. //---------------------------------------------------------------------------
  2851. UnicodeString TSynchronizeChecklist::TItem::GetRemotePath() const
  2852. {
  2853. return UnixCombinePaths(Remote.Directory, Remote.FileName);
  2854. }
  2855. //---------------------------------------------------------------------------
  2856. UnicodeString TSynchronizeChecklist::TItem::GetLocalTarget() const
  2857. {
  2858. return IncludeTrailingBackslash(Local.Directory);
  2859. }
  2860. //---------------------------------------------------------------------------
  2861. UnicodeString TSynchronizeChecklist::TItem::GetRemoteTarget() const
  2862. {
  2863. return UnixIncludeTrailingBackslash(Remote.Directory);
  2864. };
  2865. //---------------------------------------------------------------------------
  2866. TStrings * TSynchronizeChecklist::TItem::GetFileList() const
  2867. {
  2868. std::unique_ptr<TStrings> FileList(new TStringList());
  2869. switch (Action)
  2870. {
  2871. case TSynchronizeChecklist::saDownloadNew:
  2872. case TSynchronizeChecklist::saDownloadUpdate:
  2873. case TSynchronizeChecklist::saDeleteRemote:
  2874. FileList->AddObject(GetRemotePath(), RemoteFile);
  2875. break;
  2876. case TSynchronizeChecklist::saUploadNew:
  2877. case TSynchronizeChecklist::saUploadUpdate:
  2878. case TSynchronizeChecklist::saDeleteLocal:
  2879. FileList->Add(GetLocalPath());
  2880. break;
  2881. default:
  2882. DebugFail();
  2883. NotImplemented();
  2884. UNREACHABLE_AFTER_NORETURN(break);
  2885. }
  2886. return FileList.release();
  2887. }
  2888. //---------------------------------------------------------------------------
  2889. //---------------------------------------------------------------------------
  2890. TSynchronizeChecklist::TSynchronizeChecklist() :
  2891. FList(new TList())
  2892. {
  2893. }
  2894. //---------------------------------------------------------------------------
  2895. TSynchronizeChecklist::~TSynchronizeChecklist()
  2896. {
  2897. for (int Index = 0; Index < FList->Count; Index++)
  2898. {
  2899. delete static_cast<TItem *>(FList->Items[Index]);
  2900. }
  2901. delete FList;
  2902. }
  2903. //---------------------------------------------------------------------------
  2904. void TSynchronizeChecklist::Add(TItem * Item)
  2905. {
  2906. FList->Add(Item);
  2907. }
  2908. //---------------------------------------------------------------------------
  2909. int TSynchronizeChecklist::Compare(const TItem * Item1, const TItem * Item2)
  2910. {
  2911. int Result;
  2912. if (!Item1->Local.Directory.IsEmpty())
  2913. {
  2914. Result = AnsiCompareText(Item1->Local.Directory, Item2->Local.Directory);
  2915. }
  2916. else
  2917. {
  2918. DebugAssert(!Item1->Remote.Directory.IsEmpty());
  2919. Result = AnsiCompareText(Item1->Remote.Directory, Item2->Remote.Directory);
  2920. }
  2921. if (Result == 0)
  2922. {
  2923. Result = AnsiCompareText(Item1->GetFileName(), Item2->GetFileName());
  2924. }
  2925. return Result;
  2926. }
  2927. //---------------------------------------------------------------------------
  2928. int __fastcall TSynchronizeChecklist::Compare(void * AItem1, void * AItem2)
  2929. {
  2930. return Compare(static_cast<TItem *>(AItem1), static_cast<TItem *>(AItem2));
  2931. }
  2932. //---------------------------------------------------------------------------
  2933. void TSynchronizeChecklist::Sort()
  2934. {
  2935. FList->Sort(Compare);
  2936. }
  2937. //---------------------------------------------------------------------------
  2938. int TSynchronizeChecklist::GetCount() const
  2939. {
  2940. return FList->Count;
  2941. }
  2942. //---------------------------------------------------------------------------
  2943. int TSynchronizeChecklist::GetCheckedCount() const
  2944. {
  2945. int Result = 0;
  2946. for (int Index = 0; (Index < Count); Index++)
  2947. {
  2948. if (Item[Index]->Checked)
  2949. {
  2950. Result++;
  2951. }
  2952. }
  2953. return Result;
  2954. }
  2955. //---------------------------------------------------------------------------
  2956. bool TSynchronizeChecklist::GetNextChecked(int & Index, const TItem *& AItem) const
  2957. {
  2958. while (Index < Count)
  2959. {
  2960. const TItem * TheItem = Item[Index];
  2961. Index++;
  2962. if (TheItem->Checked)
  2963. {
  2964. AItem = TheItem;
  2965. return true;
  2966. }
  2967. }
  2968. AItem = NULL;
  2969. return false;
  2970. }
  2971. //---------------------------------------------------------------------------
  2972. const TSynchronizeChecklist::TItem * TSynchronizeChecklist::GetItem(int Index) const
  2973. {
  2974. return static_cast<TItem *>(FList->Items[Index]);
  2975. }
  2976. //---------------------------------------------------------------------------
  2977. void __fastcall TSynchronizeChecklist::Update(const TItem * Item, bool Check, TAction Action)
  2978. {
  2979. // TSynchronizeChecklist owns non-const items so it can manipulate them freely,
  2980. // const_cast here is just an optimization
  2981. TItem * MutableItem = const_cast<TItem *>(Item);
  2982. DebugAssert(FList->IndexOf(MutableItem) >= 0);
  2983. MutableItem->Checked = Check;
  2984. MutableItem->Action = Action;
  2985. }
  2986. //---------------------------------------------------------------------------
  2987. void TSynchronizeChecklist::Delete(const TItem * Item)
  2988. {
  2989. // See comment in Update()
  2990. TItem * MutableItem = const_cast<TItem *>(Item);
  2991. FList->Extract(MutableItem);
  2992. delete Item;
  2993. }
  2994. //---------------------------------------------------------------------------
  2995. void __fastcall TSynchronizeChecklist::UpdateDirectorySize(const TItem * Item, __int64 Size)
  2996. {
  2997. // See comment in Update
  2998. TItem * MutableItem = const_cast<TItem *>(Item);
  2999. DebugAssert(FList->IndexOf(MutableItem) >= 0);
  3000. if (DebugAlwaysTrue(Item->IsDirectory))
  3001. {
  3002. MutableItem->FDirectoryHasSize = true;
  3003. if (Item->IsRemoteOnly())
  3004. {
  3005. MutableItem->Remote.Size = Size;
  3006. }
  3007. else if (Item->IsLocalOnly())
  3008. {
  3009. MutableItem->Local.Size = Size;
  3010. }
  3011. else
  3012. {
  3013. // "update" actions are not relevant for directories
  3014. DebugFail();
  3015. }
  3016. }
  3017. }
  3018. //---------------------------------------------------------------------------
  3019. TSynchronizeChecklist::TAction __fastcall TSynchronizeChecklist::Reverse(TSynchronizeChecklist::TAction Action)
  3020. {
  3021. switch (Action)
  3022. {
  3023. case saUploadNew:
  3024. return saDeleteLocal;
  3025. case saDownloadNew:
  3026. return saDeleteRemote;
  3027. case saUploadUpdate:
  3028. return saDownloadUpdate;
  3029. case saDownloadUpdate:
  3030. return saUploadUpdate;
  3031. case saDeleteRemote:
  3032. return saDownloadNew;
  3033. case saDeleteLocal:
  3034. return saUploadNew;
  3035. default:
  3036. case saNone:
  3037. DebugFail();
  3038. return saNone;
  3039. }
  3040. }
  3041. //---------------------------------------------------------------------------
  3042. bool __fastcall TSynchronizeChecklist::IsItemSizeIrrelevant(TAction Action)
  3043. {
  3044. switch (Action)
  3045. {
  3046. case saNone:
  3047. case saDeleteRemote:
  3048. case saDeleteLocal:
  3049. return true;
  3050. default:
  3051. return false;
  3052. }
  3053. }
  3054. //---------------------------------------------------------------------------
  3055. //---------------------------------------------------------------------------
  3056. TSynchronizeProgress::TSynchronizeProgress(const TSynchronizeChecklist * Checklist)
  3057. {
  3058. FTotalSize = -1;
  3059. FProcessedSize = 0;
  3060. FChecklist = Checklist;
  3061. }
  3062. //---------------------------------------------------------------------------
  3063. __int64 TSynchronizeProgress::ItemSize(const TSynchronizeChecklist::TItem * ChecklistItem) const
  3064. {
  3065. __int64 Result;
  3066. switch (ChecklistItem->Action)
  3067. {
  3068. case TSynchronizeChecklist::saDeleteRemote:
  3069. case TSynchronizeChecklist::saDeleteLocal:
  3070. Result = ChecklistItem->IsDirectory ? 1024*1024 : 100*1024;
  3071. break;
  3072. default:
  3073. if (ChecklistItem->HasSize())
  3074. {
  3075. Result = ChecklistItem->GetSize();
  3076. }
  3077. else
  3078. {
  3079. DebugAssert(ChecklistItem->IsDirectory);
  3080. Result = 1024*1024;
  3081. }
  3082. break;
  3083. }
  3084. return Result;
  3085. }
  3086. //---------------------------------------------------------------------------
  3087. void TSynchronizeProgress::ItemProcessed(const TSynchronizeChecklist::TItem * ChecklistItem)
  3088. {
  3089. FProcessedSize += ItemSize(ChecklistItem);
  3090. }
  3091. //---------------------------------------------------------------------------
  3092. __int64 TSynchronizeProgress::GetProcessed(const TFileOperationProgressType * CurrentItemOperationProgress) const
  3093. {
  3094. DebugAssert(!TFileOperationProgressType::IsIndeterminateOperation(CurrentItemOperationProgress->Operation));
  3095. // Need to calculate the total size on the first call only,
  3096. // as at the time the constructor it called, we usually do not have sizes of folders calculated yet.
  3097. if (FTotalSize < 0)
  3098. {
  3099. FTotalSize = 0;
  3100. int Index = 0;
  3101. const TSynchronizeChecklist::TItem * ChecklistItem;
  3102. while (FChecklist->GetNextChecked(Index, ChecklistItem))
  3103. {
  3104. FTotalSize += ItemSize(ChecklistItem);
  3105. }
  3106. }
  3107. // For (single-item-)delete operation, this should return 0
  3108. __int64 CurrentItemProcessedSize = CurrentItemOperationProgress->OperationTransferred;
  3109. return (FProcessedSize + CurrentItemProcessedSize);
  3110. }
  3111. //---------------------------------------------------------------------------
  3112. int TSynchronizeProgress::Progress(const TFileOperationProgressType * CurrentItemOperationProgress) const
  3113. {
  3114. int Result;
  3115. if (TFileOperationProgressType::IsIndeterminateOperation(CurrentItemOperationProgress->Operation))
  3116. {
  3117. Result = -1;
  3118. }
  3119. else
  3120. {
  3121. __int64 Processed = GetProcessed(CurrentItemOperationProgress);
  3122. if (FTotalSize > 0)
  3123. {
  3124. Result = static_cast<int>((Processed * 100) / FTotalSize);
  3125. }
  3126. else
  3127. {
  3128. Result = 0;
  3129. }
  3130. }
  3131. return Result;
  3132. }
  3133. //---------------------------------------------------------------------------
  3134. TDateTime TSynchronizeProgress::TimeLeft(const TFileOperationProgressType * CurrentItemOperationProgress) const
  3135. {
  3136. TDateTime Result;
  3137. __int64 Processed = GetProcessed(CurrentItemOperationProgress);
  3138. if (Processed > 0)
  3139. {
  3140. Result = TDateTime(double(Now() - CurrentItemOperationProgress->StartTime) / Processed * (FTotalSize - Processed));
  3141. }
  3142. return Result;
  3143. }