RemoteFiles.cpp 87 KB

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