RemoteFiles.cpp 89 KB

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