RemoteFiles.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673
  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. /* TODO 1 : Path class instead of UnicodeString (handle relativity...) */
  15. //---------------------------------------------------------------------------
  16. bool __fastcall IsUnixStyleWindowsPath(const UnicodeString & Path)
  17. {
  18. return (Path.Length() >= 3) && IsLetter(Path[1]) && (Path[2] == L':') && (Path[3] == L'/');
  19. }
  20. //---------------------------------------------------------------------------
  21. bool __fastcall UnixIsAbsolutePath(const UnicodeString & Path)
  22. {
  23. return
  24. ((Path.Length() >= 1) && (Path[1] == L'/')) ||
  25. // we need this for FTP only, but this is unfortunately used in a static context
  26. IsUnixStyleWindowsPath(Path);
  27. }
  28. //---------------------------------------------------------------------------
  29. UnicodeString __fastcall UnixIncludeTrailingBackslash(const UnicodeString Path)
  30. {
  31. // it used to return "/" when input path was empty
  32. if (!Path.IsEmpty() && !Path.IsDelimiter(L"/", Path.Length()))
  33. {
  34. return Path + L"/";
  35. }
  36. else
  37. {
  38. return Path;
  39. }
  40. }
  41. //---------------------------------------------------------------------------
  42. // Keeps "/" for root path
  43. UnicodeString __fastcall UnixExcludeTrailingBackslash(const UnicodeString Path, bool Simple)
  44. {
  45. if (Path.IsEmpty() ||
  46. (Path == L"/") ||
  47. !Path.IsDelimiter(L"/", Path.Length()) ||
  48. (!Simple && ((Path.Length() == 3) && IsUnixStyleWindowsPath(Path))))
  49. {
  50. return Path;
  51. }
  52. else
  53. {
  54. return Path.SubString(1, Path.Length() - 1);
  55. }
  56. }
  57. //---------------------------------------------------------------------------
  58. UnicodeString __fastcall SimpleUnixExcludeTrailingBackslash(const UnicodeString Path)
  59. {
  60. return UnixExcludeTrailingBackslash(Path, true);
  61. }
  62. //---------------------------------------------------------------------------
  63. UnicodeString __fastcall UnixCombinePaths(const UnicodeString & Path1, const UnicodeString & Path2)
  64. {
  65. return UnixIncludeTrailingBackslash(Path1) + Path2;
  66. }
  67. //---------------------------------------------------------------------------
  68. Boolean __fastcall UnixSamePath(const UnicodeString Path1, const UnicodeString Path2)
  69. {
  70. return (UnixIncludeTrailingBackslash(Path1) == UnixIncludeTrailingBackslash(Path2));
  71. }
  72. //---------------------------------------------------------------------------
  73. bool __fastcall UnixIsChildPath(UnicodeString Parent, UnicodeString Child)
  74. {
  75. Parent = UnixIncludeTrailingBackslash(Parent);
  76. Child = UnixIncludeTrailingBackslash(Child);
  77. return (Child.SubString(1, Parent.Length()) == Parent);
  78. }
  79. //---------------------------------------------------------------------------
  80. UnicodeString __fastcall UnixExtractFileDir(const UnicodeString Path)
  81. {
  82. int Pos = Path.LastDelimiter(L'/');
  83. // it used to return Path when no slash was found
  84. if (Pos > 1)
  85. {
  86. return Path.SubString(1, Pos - 1);
  87. }
  88. else
  89. {
  90. return (Pos == 1) ? UnicodeString(L"/") : UnicodeString();
  91. }
  92. }
  93. //---------------------------------------------------------------------------
  94. // must return trailing backslash
  95. UnicodeString __fastcall UnixExtractFilePath(const UnicodeString Path)
  96. {
  97. int Pos = Path.LastDelimiter(L'/');
  98. // it used to return Path when no slash was found
  99. if (Pos > 0)
  100. {
  101. return Path.SubString(1, Pos);
  102. }
  103. else
  104. {
  105. return UnicodeString();
  106. }
  107. }
  108. //---------------------------------------------------------------------------
  109. UnicodeString __fastcall UnixExtractFileName(const UnicodeString Path)
  110. {
  111. int Pos = Path.LastDelimiter(L'/');
  112. UnicodeString Result;
  113. if (Pos > 0)
  114. {
  115. Result = Path.SubString(Pos + 1, Path.Length() - Pos);
  116. }
  117. else
  118. {
  119. Result = Path;
  120. }
  121. return Result;
  122. }
  123. //---------------------------------------------------------------------------
  124. UnicodeString __fastcall UnixExtractFileExt(const UnicodeString Path)
  125. {
  126. UnicodeString FileName = UnixExtractFileName(Path);
  127. int Pos = FileName.LastDelimiter(L".");
  128. return (Pos > 0) ? Path.SubString(Pos, Path.Length() - Pos + 1) : UnicodeString();
  129. }
  130. //---------------------------------------------------------------------------
  131. UnicodeString __fastcall ExtractFileName(const UnicodeString & Path, bool Unix)
  132. {
  133. if (Unix)
  134. {
  135. return UnixExtractFileName(Path);
  136. }
  137. else
  138. {
  139. return ExtractFileName(Path);
  140. }
  141. }
  142. //---------------------------------------------------------------------------
  143. bool __fastcall ExtractCommonPath(TStrings * Files, UnicodeString & Path)
  144. {
  145. DebugAssert(Files->Count > 0);
  146. Path = ExtractFilePath(Files->Strings[0]);
  147. bool Result = !Path.IsEmpty();
  148. if (Result)
  149. {
  150. for (int Index = 1; Index < Files->Count; Index++)
  151. {
  152. while (!Path.IsEmpty() &&
  153. (Files->Strings[Index].SubString(1, Path.Length()) != Path))
  154. {
  155. int PrevLen = Path.Length();
  156. Path = ExtractFilePath(ExcludeTrailingBackslash(Path));
  157. if (Path.Length() == PrevLen)
  158. {
  159. Path = L"";
  160. Result = false;
  161. }
  162. }
  163. }
  164. }
  165. return Result;
  166. }
  167. //---------------------------------------------------------------------------
  168. bool __fastcall UnixExtractCommonPath(TStrings * Files, UnicodeString & Path)
  169. {
  170. DebugAssert(Files->Count > 0);
  171. Path = UnixExtractFilePath(Files->Strings[0]);
  172. bool Result = !Path.IsEmpty();
  173. if (Result)
  174. {
  175. for (int Index = 1; Index < Files->Count; Index++)
  176. {
  177. while (!Path.IsEmpty() &&
  178. (Files->Strings[Index].SubString(1, Path.Length()) != Path))
  179. {
  180. int PrevLen = Path.Length();
  181. Path = UnixExtractFilePath(UnixExcludeTrailingBackslash(Path));
  182. if (Path.Length() == PrevLen)
  183. {
  184. Path = L"";
  185. Result = false;
  186. }
  187. }
  188. }
  189. }
  190. return Result;
  191. }
  192. //---------------------------------------------------------------------------
  193. bool __fastcall IsUnixRootPath(const UnicodeString Path)
  194. {
  195. return Path.IsEmpty() || (Path == ROOTDIRECTORY);
  196. }
  197. //---------------------------------------------------------------------------
  198. bool __fastcall IsUnixHiddenFile(const UnicodeString FileName)
  199. {
  200. return (FileName != THISDIRECTORY) && (FileName != PARENTDIRECTORY) &&
  201. !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 == L"..") ||
  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. }
  791. //---------------------------------------------------------------------------
  792. __fastcall TRemoteFile::~TRemoteFile()
  793. {
  794. delete FRights;
  795. delete FLinkedFile;
  796. }
  797. //---------------------------------------------------------------------------
  798. TRemoteFile * __fastcall TRemoteFile::Duplicate(bool Standalone) const
  799. {
  800. TRemoteFile * Result;
  801. Result = new TRemoteFile();
  802. try
  803. {
  804. if (FLinkedFile)
  805. {
  806. Result->FLinkedFile = FLinkedFile->Duplicate(true);
  807. Result->FLinkedFile->FLinkedByFile = Result;
  808. }
  809. *Result->Rights = *FRights;
  810. #define COPY_FP(PROP) Result->F ## PROP = F ## PROP;
  811. COPY_FP(Terminal);
  812. COPY_FP(Owner);
  813. COPY_FP(ModificationFmt);
  814. COPY_FP(Size);
  815. COPY_FP(FileName);
  816. COPY_FP(DisplayName);
  817. COPY_FP(INodeBlocks);
  818. COPY_FP(Modification);
  819. COPY_FP(LastAccess);
  820. COPY_FP(Group);
  821. COPY_FP(IconIndex);
  822. COPY_FP(TypeName);
  823. COPY_FP(IsSymLink);
  824. COPY_FP(LinkTo);
  825. COPY_FP(Type);
  826. COPY_FP(CyclicLink);
  827. COPY_FP(HumanRights);
  828. #undef COPY_FP
  829. if (Standalone && (!FFullFileName.IsEmpty() || (Directory != NULL)))
  830. {
  831. Result->FFullFileName = FullFileName;
  832. }
  833. }
  834. catch(...)
  835. {
  836. delete Result;
  837. throw;
  838. }
  839. return Result;
  840. }
  841. //---------------------------------------------------------------------------
  842. void __fastcall TRemoteFile::LoadTypeInfo()
  843. {
  844. /* TODO : If file is link: Should be attributes taken from linked file? */
  845. unsigned long Attrs = 0;
  846. if (IsDirectory) Attrs |= FILE_ATTRIBUTE_DIRECTORY;
  847. if (IsHidden) Attrs |= FILE_ATTRIBUTE_HIDDEN;
  848. UnicodeString DumbFileName = (IsSymLink && !LinkTo.IsEmpty() ? LinkTo : FileName);
  849. FIconIndex = FakeFileImageIndex(DumbFileName, Attrs, &FTypeName);
  850. }
  851. //---------------------------------------------------------------------------
  852. __int64 __fastcall TRemoteFile::GetSize() const
  853. {
  854. return IsDirectory ? 0 : FSize;
  855. }
  856. //---------------------------------------------------------------------------
  857. Integer __fastcall TRemoteFile::GetIconIndex() const
  858. {
  859. if (FIconIndex == -1)
  860. {
  861. const_cast<TRemoteFile *>(this)->LoadTypeInfo();
  862. }
  863. return FIconIndex;
  864. }
  865. //---------------------------------------------------------------------------
  866. UnicodeString __fastcall TRemoteFile::GetTypeName()
  867. {
  868. // check availability of type info by icon index, because type name can be empty
  869. if (FIconIndex < 0)
  870. {
  871. LoadTypeInfo();
  872. }
  873. return FTypeName;
  874. }
  875. //---------------------------------------------------------------------------
  876. Boolean __fastcall TRemoteFile::GetIsHidden()
  877. {
  878. bool Result;
  879. switch (FIsHidden)
  880. {
  881. case 0:
  882. Result = false;
  883. break;
  884. case 1:
  885. Result = true;
  886. break;
  887. default:
  888. Result = IsUnixHiddenFile(FileName);
  889. break;
  890. }
  891. return Result;
  892. }
  893. //---------------------------------------------------------------------------
  894. void __fastcall TRemoteFile::SetIsHidden(bool value)
  895. {
  896. FIsHidden = value ? 1 : 0;
  897. }
  898. //---------------------------------------------------------------------------
  899. Boolean __fastcall TRemoteFile::GetIsDirectory() const
  900. {
  901. return (toupper(Type) == FILETYPE_DIRECTORY);
  902. }
  903. //---------------------------------------------------------------------------
  904. Boolean __fastcall TRemoteFile::GetIsParentDirectory() const
  905. {
  906. return (FileName == PARENTDIRECTORY);
  907. }
  908. //---------------------------------------------------------------------------
  909. Boolean __fastcall TRemoteFile::GetIsThisDirectory() const
  910. {
  911. return (FileName == THISDIRECTORY);
  912. }
  913. //---------------------------------------------------------------------------
  914. Boolean __fastcall TRemoteFile::GetIsInaccesibleDirectory() const
  915. {
  916. Boolean Result;
  917. if (IsDirectory)
  918. {
  919. DebugAssert(Terminal);
  920. Result = !
  921. (SameUserName(Terminal->UserName, L"root") ||
  922. ((Rights->RightUndef[TRights::rrOtherExec] != TRights::rsNo)) ||
  923. ((Rights->Right[TRights::rrGroupExec] != TRights::rsNo) &&
  924. Terminal->Membership->Exists(Group.Name)) ||
  925. ((Rights->Right[TRights::rrUserExec] != TRights::rsNo) &&
  926. SameUserName(Terminal->UserName, Owner.Name)));
  927. }
  928. else Result = False;
  929. return Result;
  930. }
  931. //---------------------------------------------------------------------------
  932. wchar_t __fastcall TRemoteFile::GetType() const
  933. {
  934. if (IsSymLink && FLinkedFile) return FLinkedFile->Type;
  935. else return FType;
  936. }
  937. //---------------------------------------------------------------------------
  938. void __fastcall TRemoteFile::SetType(wchar_t AType)
  939. {
  940. FType = AType;
  941. FIsSymLink = ((wchar_t)towupper(FType) == FILETYPE_SYMLINK);
  942. }
  943. //---------------------------------------------------------------------------
  944. TRemoteFile * __fastcall TRemoteFile::GetLinkedFile()
  945. {
  946. // do not call FindLinkedFile as it would be called repeatedly for broken symlinks
  947. return FLinkedFile;
  948. }
  949. //---------------------------------------------------------------------------
  950. void __fastcall TRemoteFile::SetLinkedFile(TRemoteFile * value)
  951. {
  952. if (FLinkedFile != value)
  953. {
  954. if (FLinkedFile) delete FLinkedFile;
  955. FLinkedFile = value;
  956. }
  957. }
  958. //---------------------------------------------------------------------------
  959. bool __fastcall TRemoteFile::GetBrokenLink()
  960. {
  961. DebugAssert(Terminal);
  962. // If file is symlink but we couldn't find linked file we assume broken link
  963. return (IsSymLink && (FCyclicLink || !FLinkedFile) &&
  964. Terminal->ResolvingSymlinks);
  965. // "!FLinkTo.IsEmpty()" removed because it does not work with SFTP
  966. }
  967. //---------------------------------------------------------------------------
  968. bool __fastcall TRemoteFile::IsTimeShiftingApplicable()
  969. {
  970. return IsTimeShiftingApplicable(ModificationFmt);
  971. }
  972. //---------------------------------------------------------------------------
  973. bool __fastcall TRemoteFile::IsTimeShiftingApplicable(TModificationFmt ModificationFmt)
  974. {
  975. return (ModificationFmt == mfMDHM) || (ModificationFmt == mfFull);
  976. }
  977. //---------------------------------------------------------------------------
  978. void __fastcall TRemoteFile::ShiftTimeInSeconds(__int64 Seconds)
  979. {
  980. ShiftTimeInSeconds(FModification, ModificationFmt, Seconds);
  981. ShiftTimeInSeconds(FLastAccess, ModificationFmt, Seconds);
  982. }
  983. //---------------------------------------------------------------------------
  984. void __fastcall TRemoteFile::ShiftTimeInSeconds(TDateTime & DateTime, TModificationFmt ModificationFmt, __int64 Seconds)
  985. {
  986. if ((Seconds != 0) && IsTimeShiftingApplicable(ModificationFmt))
  987. {
  988. DebugAssert(int(DateTime) != 0);
  989. DateTime = IncSecond(DateTime, Seconds);
  990. }
  991. }
  992. //---------------------------------------------------------------------------
  993. void __fastcall TRemoteFile::SetModification(const TDateTime & value)
  994. {
  995. if (FModification != value)
  996. {
  997. FModificationFmt = mfFull;
  998. FModification = value;
  999. }
  1000. }
  1001. //---------------------------------------------------------------------------
  1002. UnicodeString __fastcall TRemoteFile::GetUserModificationStr()
  1003. {
  1004. return ::UserModificationStr(Modification, FModificationFmt);
  1005. }
  1006. //---------------------------------------------------------------------------
  1007. UnicodeString __fastcall TRemoteFile::GetModificationStr()
  1008. {
  1009. return ::ModificationStr(Modification, FModificationFmt);
  1010. }
  1011. //---------------------------------------------------------------------------
  1012. UnicodeString __fastcall TRemoteFile::GetExtension()
  1013. {
  1014. return UnixExtractFileExt(FFileName);
  1015. }
  1016. //---------------------------------------------------------------------------
  1017. void __fastcall TRemoteFile::SetRights(TRights * value)
  1018. {
  1019. FRights->Assign(value);
  1020. }
  1021. //---------------------------------------------------------------------------
  1022. UnicodeString __fastcall TRemoteFile::GetRightsStr()
  1023. {
  1024. // note that HumanRights is typically an empty string
  1025. // (with an exception of Perm-fact-only MLSD FTP listing)
  1026. return FRights->Unknown ? HumanRights : FRights->Text;
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. void __fastcall TRemoteFile::SetListingStr(UnicodeString value)
  1030. {
  1031. // Value stored in 'value' can be used for error message
  1032. UnicodeString Line = value;
  1033. FIconIndex = -1;
  1034. try
  1035. {
  1036. UnicodeString Col;
  1037. // Do we need to do this (is ever TAB is LS output)?
  1038. Line = ReplaceChar(Line, L'\t', L' ');
  1039. Type = Line[1];
  1040. Line.Delete(1, 1);
  1041. #define GETNCOL \
  1042. { if (Line.IsEmpty()) throw Exception(L""); \
  1043. Integer P = Line.Pos(L' '); \
  1044. if (P) { Col = Line.SubString(1, P-1); Line.Delete(1, P); } \
  1045. else { Col = Line; Line = L""; } \
  1046. }
  1047. #define GETCOL { GETNCOL; Line = TrimLeft(Line); }
  1048. // Rights string may contain special permission attributes (S,t, ...)
  1049. // (TODO: maybe no longer necessary, once we can handle the special permissions)
  1050. Rights->AllowUndef = True;
  1051. // On some system there is no space between permissions and node blocks count columns
  1052. // so we get only first 9 characters and trim all following spaces (if any)
  1053. Rights->Text = Line.SubString(1, 9);
  1054. Line.Delete(1, 9);
  1055. // Rights column maybe followed by '+', '@' or '.' signs, we ignore them
  1056. // (On MacOS, there may be a space in between)
  1057. if (!Line.IsEmpty() && ((Line[1] == L'+') || (Line[1] == L'@') || (Line[1] == L'.')))
  1058. {
  1059. Line.Delete(1, 1);
  1060. }
  1061. else if ((Line.Length() >= 2) && (Line[1] == L' ') &&
  1062. ((Line[2] == L'+') || (Line[2] == L'@') || (Line[2] == L'.')))
  1063. {
  1064. Line.Delete(1, 2);
  1065. }
  1066. Line = Line.TrimLeft();
  1067. GETCOL;
  1068. if (!TryStrToInt(Col, FINodeBlocks))
  1069. {
  1070. // if the column is not an integer, suppose it's owner
  1071. // (Android BusyBox)
  1072. FINodeBlocks = 0;
  1073. }
  1074. else
  1075. {
  1076. GETCOL;
  1077. }
  1078. FOwner.Name = Col;
  1079. // #60 17.10.01: group name can contain space
  1080. FGroup.Name = L"";
  1081. GETCOL;
  1082. __int64 ASize;
  1083. do
  1084. {
  1085. FGroup.Name = FGroup.Name + Col;
  1086. GETCOL;
  1087. // SSH FS link like
  1088. // d????????? ? ? ? ? ? name
  1089. if ((FGroup.Name == L"?") && (Col == L"?"))
  1090. {
  1091. ASize = 0;
  1092. }
  1093. else
  1094. {
  1095. DebugAssert(!Col.IsEmpty());
  1096. // for devices etc.. there is additional column ending by comma, we ignore it
  1097. if (Col[Col.Length()] == L',') GETCOL;
  1098. ASize = StrToInt64Def(Col, -1);
  1099. // if it's not a number (file size) we take it as part of group name
  1100. // (at least on CygWin, there can be group with space in its name)
  1101. if (ASize < 0) Col = L" " + Col;
  1102. }
  1103. }
  1104. while (ASize < 0);
  1105. // do not read modification time and filename if it is already set
  1106. if (double(FModification) == 0 && FileName.IsEmpty())
  1107. {
  1108. FSize = ASize;
  1109. bool FullTime = false;
  1110. bool DayMonthFormat = false;
  1111. Word Day, Month, Year, Hour, Min, Sec, P;
  1112. GETCOL;
  1113. // SSH FS link, see above
  1114. if (Col == L"?")
  1115. {
  1116. GETCOL;
  1117. FModificationFmt = mfNone;
  1118. FModification = 0;
  1119. FLastAccess = 0;
  1120. }
  1121. else
  1122. {
  1123. // format dd mmm or mmm dd ?
  1124. Day = (Word)StrToIntDef(Col, 0);
  1125. if (Day > 0)
  1126. {
  1127. DayMonthFormat = true;
  1128. GETCOL;
  1129. }
  1130. Month = 0;
  1131. #define COL2MONTH \
  1132. for (Word IMonth = 0; IMonth < 12; IMonth++) \
  1133. if (!Col.CompareIC(EngShortMonthNames[IMonth])) { Month = IMonth; Month++; break; }
  1134. COL2MONTH;
  1135. // if the column is not known month name, it may have been "yyyy-mm-dd"
  1136. // for --full-time format
  1137. if ((Month == 0) && (Col.Length() == 10) && (Col[5] == L'-') && (Col[8] == L'-'))
  1138. {
  1139. Year = (Word)Col.SubString(1, 4).ToInt();
  1140. Month = (Word)Col.SubString(6, 2).ToInt();
  1141. Day = (Word)Col.SubString(9, 2).ToInt();
  1142. GETCOL;
  1143. Hour = (Word)Col.SubString(1, 2).ToInt();
  1144. Min = (Word)Col.SubString(4, 2).ToInt();
  1145. if (Col.Length() >= 8)
  1146. {
  1147. Sec = (Word)StrToInt(Col.SubString(7, 2));
  1148. }
  1149. else
  1150. {
  1151. Sec = 0;
  1152. }
  1153. FModificationFmt = mfFull;
  1154. // skip TZ (TODO)
  1155. // do not trim leading space of filename
  1156. GETNCOL;
  1157. }
  1158. else
  1159. {
  1160. // or it may have been day name for another format of --full-time
  1161. if (Month == 0)
  1162. {
  1163. GETCOL;
  1164. COL2MONTH;
  1165. // neither standard, not --full-time format
  1166. if (Month == 0)
  1167. {
  1168. Abort();
  1169. }
  1170. else
  1171. {
  1172. FullTime = true;
  1173. }
  1174. }
  1175. #undef COL2MONTH
  1176. if (Day == 0)
  1177. {
  1178. GETNCOL;
  1179. Day = (Word)StrToInt(Col);
  1180. }
  1181. if ((Day < 1) || (Day > 31)) Abort();
  1182. // second full-time format
  1183. // ddd mmm dd hh:nn:ss yyyy
  1184. if (FullTime)
  1185. {
  1186. GETCOL;
  1187. if (Col.Length() != 8)
  1188. {
  1189. Abort();
  1190. }
  1191. Hour = (Word)StrToInt(Col.SubString(1, 2));
  1192. Min = (Word)StrToInt(Col.SubString(4, 2));
  1193. Sec = (Word)StrToInt(Col.SubString(7, 2));
  1194. FModificationFmt = mfFull;
  1195. // do not trim leading space of filename
  1196. GETNCOL;
  1197. Year = (Word)StrToInt(Col);
  1198. }
  1199. else
  1200. {
  1201. // for format dd mmm the below description seems not to be true,
  1202. // the year is not aligned to 5 characters
  1203. if (DayMonthFormat)
  1204. {
  1205. GETCOL;
  1206. }
  1207. else
  1208. {
  1209. // Time/Year indicator is always 5 characters long (???), on most
  1210. // systems year is aligned to right (_YYYY), but on some to left (YYYY_),
  1211. // we must ensure that trailing space is also deleted, so real
  1212. // separator space is not treated as part of file name
  1213. Col = Line.SubString(1, 6).Trim();
  1214. Line.Delete(1, 6);
  1215. }
  1216. // GETNCOL; // We don't want to trim input strings (name with space at beginning???)
  1217. // Check if we got time (contains :) or year
  1218. if ((P = (Word)Col.Pos(L':')) > 0)
  1219. {
  1220. Word CurrMonth, CurrDay;
  1221. Hour = (Word)StrToInt(Col.SubString(1, P-1));
  1222. Min = (Word)StrToInt(Col.SubString(P+1, Col.Length() - P));
  1223. if (Hour > 23 || Min > 59) Abort();
  1224. // When we don't got year, we assume current year
  1225. // with exception that the date would be in future
  1226. // in this case we assume last year.
  1227. DecodeDate(Date(), Year, CurrMonth, CurrDay);
  1228. if ((Month > CurrMonth) ||
  1229. (Month == CurrMonth && Day > CurrDay)) Year--;
  1230. Sec = 0;
  1231. FModificationFmt = mfMDHM;
  1232. }
  1233. else
  1234. {
  1235. Year = (Word)StrToInt(Col);
  1236. if (Year > 10000) Abort();
  1237. // When we don't got time we assume midnight
  1238. Hour = 0; Min = 0; Sec = 0;
  1239. FModificationFmt = mfMDY;
  1240. }
  1241. }
  1242. }
  1243. FModification = EncodeDateVerbose(Year, Month, Day) + EncodeTimeVerbose(Hour, Min, Sec, 0);
  1244. // adjust only when time is known,
  1245. // adjusting default "midnight" time makes no sense
  1246. if ((FModificationFmt == mfMDHM) || (FModificationFmt == mfFull))
  1247. {
  1248. DebugAssert(Terminal != NULL);
  1249. FModification = AdjustDateTimeFromUnix(FModification,
  1250. Terminal->SessionData->DSTMode);
  1251. }
  1252. if (double(FLastAccess) == 0)
  1253. {
  1254. FLastAccess = FModification;
  1255. }
  1256. }
  1257. // separating space is already deleted, other spaces are treated as part of name
  1258. {
  1259. int P;
  1260. FLinkTo = L"";
  1261. if (IsSymLink)
  1262. {
  1263. P = Line.Pos(SYMLINKSTR);
  1264. if (P)
  1265. {
  1266. FLinkTo = Line.SubString(
  1267. P + wcslen(SYMLINKSTR), Line.Length() - P + wcslen(SYMLINKSTR) + 1);
  1268. Line.SetLength(P - 1);
  1269. }
  1270. else
  1271. {
  1272. Abort();
  1273. }
  1274. }
  1275. FFileName = UnixExtractFileName(Line);
  1276. }
  1277. }
  1278. #undef GETNCOL
  1279. #undef GETCOL
  1280. }
  1281. catch (Exception &E)
  1282. {
  1283. throw ETerminal(&E, FmtLoadStr(LIST_LINE_ERROR, ARRAYOFCONST((value))), HELP_LIST_LINE_ERROR);
  1284. }
  1285. }
  1286. //---------------------------------------------------------------------------
  1287. void __fastcall TRemoteFile::Complete()
  1288. {
  1289. DebugAssert(Terminal != NULL);
  1290. if (IsSymLink && Terminal->ResolvingSymlinks)
  1291. {
  1292. FindLinkedFile();
  1293. }
  1294. }
  1295. //---------------------------------------------------------------------------
  1296. void __fastcall TRemoteFile::FindLinkedFile()
  1297. {
  1298. DebugAssert(Terminal && IsSymLink);
  1299. if (FLinkedFile) delete FLinkedFile;
  1300. FLinkedFile = NULL;
  1301. FCyclicLink = false;
  1302. if (!LinkTo.IsEmpty())
  1303. {
  1304. // check for cyclic link
  1305. TRemoteFile * LinkedBy = FLinkedByFile;
  1306. while (LinkedBy)
  1307. {
  1308. if (LinkedBy->LinkTo == LinkTo)
  1309. {
  1310. // this is currenly redundant information, because it is used only to
  1311. // detect broken symlink, which would be otherwise detected
  1312. // by FLinkedFile == NULL
  1313. FCyclicLink = true;
  1314. break;
  1315. }
  1316. LinkedBy = LinkedBy->FLinkedByFile;
  1317. }
  1318. }
  1319. if (FCyclicLink)
  1320. {
  1321. TRemoteFile * LinkedBy = FLinkedByFile;
  1322. while (LinkedBy)
  1323. {
  1324. LinkedBy->FCyclicLink = true;
  1325. LinkedBy = LinkedBy->FLinkedByFile;
  1326. }
  1327. }
  1328. else
  1329. {
  1330. DebugAssert(Terminal->ResolvingSymlinks);
  1331. Terminal->ExceptionOnFail = true;
  1332. try
  1333. {
  1334. try
  1335. {
  1336. Terminal->ReadSymlink(this, FLinkedFile);
  1337. }
  1338. __finally
  1339. {
  1340. Terminal->ExceptionOnFail = false;
  1341. }
  1342. }
  1343. catch (Exception &E)
  1344. {
  1345. if (E.InheritsFrom(__classid(EFatal))) throw;
  1346. else
  1347. {
  1348. Terminal->Log->AddException(&E);
  1349. }
  1350. }
  1351. }
  1352. }
  1353. //---------------------------------------------------------------------------
  1354. UnicodeString __fastcall TRemoteFile::GetListingStr()
  1355. {
  1356. // note that ModificationStr is longer than 12 for mfFull
  1357. UnicodeString LinkPart;
  1358. // expanded from ?: to avoid memory leaks
  1359. if (IsSymLink)
  1360. {
  1361. LinkPart = UnicodeString(SYMLINKSTR) + LinkTo;
  1362. }
  1363. return Format(L"%s%s %3s %-8s %-8s %9s %-12s %s%s", ARRAYOFCONST((
  1364. Type, Rights->Text, IntToStr(INodeBlocks), Owner.Name, Group.Name,
  1365. IntToStr(FSize), // explicitly using size even for directories
  1366. ModificationStr, FileName,
  1367. LinkPart)));
  1368. }
  1369. //---------------------------------------------------------------------------
  1370. UnicodeString __fastcall TRemoteFile::GetFullFileName() const
  1371. {
  1372. if (FFullFileName.IsEmpty())
  1373. {
  1374. DebugAssert(Terminal);
  1375. DebugAssert(Directory != NULL);
  1376. UnicodeString Path;
  1377. if (IsParentDirectory) Path = Directory->ParentPath;
  1378. else if (IsDirectory) Path = UnixIncludeTrailingBackslash(Directory->FullDirectory + FileName);
  1379. else Path = Directory->FullDirectory + FileName;
  1380. return Terminal->TranslateLockedPath(Path, true);
  1381. }
  1382. else
  1383. {
  1384. return FFullFileName;
  1385. }
  1386. }
  1387. //---------------------------------------------------------------------------
  1388. bool __fastcall TRemoteFile::GetHaveFullFileName() const
  1389. {
  1390. return !FFullFileName.IsEmpty() || (Directory != NULL);
  1391. }
  1392. //---------------------------------------------------------------------------
  1393. Integer __fastcall TRemoteFile::GetAttr()
  1394. {
  1395. Integer Result = 0;
  1396. if (Rights->ReadOnly) Result |= faReadOnly;
  1397. if (IsHidden) Result |= faHidden;
  1398. return Result;
  1399. }
  1400. //---------------------------------------------------------------------------
  1401. void __fastcall TRemoteFile::SetTerminal(TTerminal * value)
  1402. {
  1403. FTerminal = value;
  1404. if (FLinkedFile)
  1405. {
  1406. FLinkedFile->Terminal = value;
  1407. }
  1408. }
  1409. //---------------------------------------------------------------------------
  1410. //---------------------------------------------------------------------------
  1411. __fastcall TRemoteDirectoryFile::TRemoteDirectoryFile() : TRemoteFile()
  1412. {
  1413. Modification = double(0);
  1414. ModificationFmt = mfNone;
  1415. LastAccess = Modification;
  1416. Type = L'D';
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. //---------------------------------------------------------------------------
  1420. __fastcall TRemoteParentDirectory::TRemoteParentDirectory(TTerminal * ATerminal)
  1421. : TRemoteDirectoryFile()
  1422. {
  1423. FileName = PARENTDIRECTORY;
  1424. Terminal = ATerminal;
  1425. }
  1426. //=== TRemoteFileList ------------------------------------------------------
  1427. __fastcall TRemoteFileList::TRemoteFileList():
  1428. TObjectList()
  1429. {
  1430. FTimestamp = Now();
  1431. }
  1432. //---------------------------------------------------------------------------
  1433. void __fastcall TRemoteFileList::AddFile(TRemoteFile * File)
  1434. {
  1435. Add(File);
  1436. File->Directory = this;
  1437. }
  1438. //---------------------------------------------------------------------------
  1439. TStrings * __fastcall TRemoteFileList::CloneStrings(TStrings * List)
  1440. {
  1441. std::unique_ptr<TStringList> Result(new TStringList());
  1442. for (int Index = 0; Index < List->Count; Index++)
  1443. {
  1444. TRemoteFile * File = static_cast<TRemoteFile *>(List->Objects[Index]);
  1445. Result->AddObject(List->Strings[Index], File);
  1446. }
  1447. return Result.release();
  1448. }
  1449. //---------------------------------------------------------------------------
  1450. void __fastcall TRemoteFileList::DuplicateTo(TRemoteFileList * Copy)
  1451. {
  1452. Copy->Reset();
  1453. for (int Index = 0; Index < Count; Index++)
  1454. {
  1455. TRemoteFile * File = Files[Index];
  1456. Copy->AddFile(File->Duplicate(false));
  1457. }
  1458. Copy->FDirectory = Directory;
  1459. Copy->FTimestamp = FTimestamp;
  1460. }
  1461. //---------------------------------------------------------------------------
  1462. void __fastcall TRemoteFileList::Reset()
  1463. {
  1464. FTimestamp = Now();
  1465. Clear();
  1466. }
  1467. //---------------------------------------------------------------------------
  1468. void __fastcall TRemoteFileList::SetDirectory(UnicodeString value)
  1469. {
  1470. FDirectory = UnixExcludeTrailingBackslash(value);
  1471. }
  1472. //---------------------------------------------------------------------------
  1473. UnicodeString __fastcall TRemoteFileList::GetFullDirectory()
  1474. {
  1475. return UnixIncludeTrailingBackslash(Directory);
  1476. }
  1477. //---------------------------------------------------------------------------
  1478. TRemoteFile * __fastcall TRemoteFileList::GetFiles(Integer Index)
  1479. {
  1480. return (TRemoteFile *)Items[Index];
  1481. }
  1482. //---------------------------------------------------------------------------
  1483. Boolean __fastcall TRemoteFileList::GetIsRoot()
  1484. {
  1485. return (Directory == ROOTDIRECTORY);
  1486. }
  1487. //---------------------------------------------------------------------------
  1488. UnicodeString __fastcall TRemoteFileList::GetParentPath()
  1489. {
  1490. return UnixExtractFilePath(Directory);
  1491. }
  1492. //---------------------------------------------------------------------------
  1493. __int64 __fastcall TRemoteFileList::GetTotalSize()
  1494. {
  1495. __int64 Result = 0;
  1496. for (Integer Index = 0; Index < Count; Index++)
  1497. Result += Files[Index]->Size;
  1498. return Result;
  1499. }
  1500. //---------------------------------------------------------------------------
  1501. TRemoteFile * __fastcall TRemoteFileList::FindFile(const UnicodeString &FileName)
  1502. {
  1503. for (Integer Index = 0; Index < Count; Index++)
  1504. if (Files[Index]->FileName == FileName) return Files[Index];
  1505. return NULL;
  1506. }
  1507. //=== TRemoteDirectory ------------------------------------------------------
  1508. __fastcall TRemoteDirectory::TRemoteDirectory(TTerminal * aTerminal, TRemoteDirectory * Template) :
  1509. TRemoteFileList(), FTerminal(aTerminal)
  1510. {
  1511. FThisDirectory = NULL;
  1512. FParentDirectory = NULL;
  1513. if (Template == NULL)
  1514. {
  1515. FIncludeThisDirectory = false;
  1516. FIncludeParentDirectory = true;
  1517. }
  1518. else
  1519. {
  1520. FIncludeThisDirectory = Template->FIncludeThisDirectory;
  1521. FIncludeParentDirectory = Template->FIncludeParentDirectory;
  1522. }
  1523. }
  1524. //---------------------------------------------------------------------------
  1525. __fastcall TRemoteDirectory::~TRemoteDirectory()
  1526. {
  1527. ReleaseRelativeDirectories();
  1528. }
  1529. //---------------------------------------------------------------------------
  1530. void __fastcall TRemoteDirectory::ReleaseRelativeDirectories()
  1531. {
  1532. if ((ThisDirectory != NULL) && !IncludeThisDirectory)
  1533. {
  1534. delete FThisDirectory;
  1535. FThisDirectory = NULL;
  1536. }
  1537. if ((ParentDirectory != NULL) && !IncludeParentDirectory)
  1538. {
  1539. delete FParentDirectory;
  1540. FParentDirectory = NULL;
  1541. }
  1542. }
  1543. //---------------------------------------------------------------------------
  1544. void __fastcall TRemoteDirectory::Reset()
  1545. {
  1546. ReleaseRelativeDirectories();
  1547. TRemoteFileList::Reset();
  1548. }
  1549. //---------------------------------------------------------------------------
  1550. void __fastcall TRemoteDirectory::SetDirectory(UnicodeString value)
  1551. {
  1552. TRemoteFileList::SetDirectory(value);
  1553. }
  1554. //---------------------------------------------------------------------------
  1555. void __fastcall TRemoteDirectory::AddFile(TRemoteFile * File)
  1556. {
  1557. if (File->IsThisDirectory) FThisDirectory = File;
  1558. if (File->IsParentDirectory) FParentDirectory = File;
  1559. if ((!File->IsThisDirectory || IncludeThisDirectory) &&
  1560. (!File->IsParentDirectory || IncludeParentDirectory))
  1561. {
  1562. TRemoteFileList::AddFile(File);
  1563. }
  1564. File->Terminal = Terminal;
  1565. }
  1566. //---------------------------------------------------------------------------
  1567. void __fastcall TRemoteDirectory::DuplicateTo(TRemoteFileList * Copy)
  1568. {
  1569. TRemoteFileList::DuplicateTo(Copy);
  1570. if (ThisDirectory && !IncludeThisDirectory)
  1571. {
  1572. Copy->AddFile(ThisDirectory->Duplicate(false));
  1573. }
  1574. if (ParentDirectory && !IncludeParentDirectory)
  1575. {
  1576. Copy->AddFile(ParentDirectory->Duplicate(false));
  1577. }
  1578. }
  1579. //---------------------------------------------------------------------------
  1580. bool __fastcall TRemoteDirectory::GetLoaded()
  1581. {
  1582. return ((Terminal != NULL) && Terminal->Active && !Directory.IsEmpty());
  1583. }
  1584. //---------------------------------------------------------------------------
  1585. void __fastcall TRemoteDirectory::SetIncludeParentDirectory(Boolean value)
  1586. {
  1587. if (IncludeParentDirectory != value)
  1588. {
  1589. FIncludeParentDirectory = value;
  1590. if (value && ParentDirectory)
  1591. {
  1592. DebugAssert(IndexOf(ParentDirectory) < 0);
  1593. Add(ParentDirectory);
  1594. }
  1595. else if (!value && ParentDirectory)
  1596. {
  1597. DebugAssert(IndexOf(ParentDirectory) >= 0);
  1598. Extract(ParentDirectory);
  1599. }
  1600. }
  1601. }
  1602. //---------------------------------------------------------------------------
  1603. void __fastcall TRemoteDirectory::SetIncludeThisDirectory(Boolean value)
  1604. {
  1605. if (IncludeThisDirectory != value)
  1606. {
  1607. FIncludeThisDirectory = value;
  1608. if (value && ThisDirectory)
  1609. {
  1610. DebugAssert(IndexOf(ThisDirectory) < 0);
  1611. Add(ThisDirectory);
  1612. }
  1613. else if (!value && ThisDirectory)
  1614. {
  1615. DebugAssert(IndexOf(ThisDirectory) >= 0);
  1616. Extract(ThisDirectory);
  1617. }
  1618. }
  1619. }
  1620. //===========================================================================
  1621. __fastcall TRemoteDirectoryCache::TRemoteDirectoryCache(): TStringList()
  1622. {
  1623. FSection = new TCriticalSection();
  1624. Sorted = true;
  1625. Duplicates = Types::dupError;
  1626. CaseSensitive = true;
  1627. }
  1628. //---------------------------------------------------------------------------
  1629. __fastcall TRemoteDirectoryCache::~TRemoteDirectoryCache()
  1630. {
  1631. Clear();
  1632. delete FSection;
  1633. }
  1634. //---------------------------------------------------------------------------
  1635. void __fastcall TRemoteDirectoryCache::Clear()
  1636. {
  1637. TGuard Guard(FSection);
  1638. try
  1639. {
  1640. for (int Index = 0; Index < Count; Index++)
  1641. {
  1642. delete (TRemoteFileList *)Objects[Index];
  1643. Objects[Index] = NULL;
  1644. }
  1645. }
  1646. __finally
  1647. {
  1648. TStringList::Clear();
  1649. }
  1650. }
  1651. //---------------------------------------------------------------------------
  1652. bool __fastcall TRemoteDirectoryCache::GetIsEmpty() const
  1653. {
  1654. TGuard Guard(FSection);
  1655. return (const_cast<TRemoteDirectoryCache*>(this)->Count == 0);
  1656. }
  1657. //---------------------------------------------------------------------------
  1658. bool __fastcall TRemoteDirectoryCache::HasFileList(const UnicodeString Directory)
  1659. {
  1660. TGuard Guard(FSection);
  1661. int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));
  1662. return (Index >= 0);
  1663. }
  1664. //---------------------------------------------------------------------------
  1665. bool __fastcall TRemoteDirectoryCache::HasNewerFileList(const UnicodeString Directory,
  1666. TDateTime Timestamp)
  1667. {
  1668. TGuard Guard(FSection);
  1669. int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));
  1670. if (Index >= 0)
  1671. {
  1672. TRemoteFileList * FileList = dynamic_cast<TRemoteFileList *>(Objects[Index]);
  1673. if (FileList->Timestamp <= Timestamp)
  1674. {
  1675. Index = -1;
  1676. }
  1677. }
  1678. return (Index >= 0);
  1679. }
  1680. //---------------------------------------------------------------------------
  1681. bool __fastcall TRemoteDirectoryCache::GetFileList(const UnicodeString Directory,
  1682. TRemoteFileList * FileList)
  1683. {
  1684. TGuard Guard(FSection);
  1685. int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));
  1686. bool Result = (Index >= 0);
  1687. if (Result)
  1688. {
  1689. DebugAssert(Objects[Index] != NULL);
  1690. dynamic_cast<TRemoteFileList *>(Objects[Index])->DuplicateTo(FileList);
  1691. }
  1692. return Result;
  1693. }
  1694. //---------------------------------------------------------------------------
  1695. void __fastcall TRemoteDirectoryCache::AddFileList(TRemoteFileList * FileList)
  1696. {
  1697. DebugAssert(FileList);
  1698. TRemoteFileList * Copy = new TRemoteFileList();
  1699. FileList->DuplicateTo(Copy);
  1700. {
  1701. TGuard Guard(FSection);
  1702. // file list cannot be cached already with only one thread, but it can be
  1703. // when directory is loaded by secondary terminal
  1704. DoClearFileList(FileList->Directory, false);
  1705. AddObject(Copy->Directory, Copy);
  1706. }
  1707. }
  1708. //---------------------------------------------------------------------------
  1709. void __fastcall TRemoteDirectoryCache::ClearFileList(UnicodeString Directory, bool SubDirs)
  1710. {
  1711. TGuard Guard(FSection);
  1712. DoClearFileList(Directory, SubDirs);
  1713. }
  1714. //---------------------------------------------------------------------------
  1715. void __fastcall TRemoteDirectoryCache::DoClearFileList(UnicodeString Directory, bool SubDirs)
  1716. {
  1717. Directory = UnixExcludeTrailingBackslash(Directory);
  1718. int Index = IndexOf(Directory);
  1719. if (Index >= 0)
  1720. {
  1721. Delete(Index);
  1722. }
  1723. if (SubDirs)
  1724. {
  1725. Directory = UnixIncludeTrailingBackslash(Directory);
  1726. Index = Count-1;
  1727. while (Index >= 0)
  1728. {
  1729. if (Strings[Index].SubString(1, Directory.Length()) == Directory)
  1730. {
  1731. Delete(Index);
  1732. }
  1733. Index--;
  1734. }
  1735. }
  1736. }
  1737. //---------------------------------------------------------------------------
  1738. void __fastcall TRemoteDirectoryCache::Delete(int Index)
  1739. {
  1740. delete dynamic_cast<TRemoteFileList *>(Objects[Index]);
  1741. TStringList::Delete(Index);
  1742. }
  1743. //---------------------------------------------------------------------------
  1744. //---------------------------------------------------------------------------
  1745. __fastcall TRemoteDirectoryChangesCache::TRemoteDirectoryChangesCache(int MaxSize) :
  1746. TStringList(),
  1747. FMaxSize(MaxSize)
  1748. {
  1749. }
  1750. //---------------------------------------------------------------------------
  1751. void __fastcall TRemoteDirectoryChangesCache::Clear()
  1752. {
  1753. TStringList::Clear();
  1754. }
  1755. //---------------------------------------------------------------------------
  1756. bool __fastcall TRemoteDirectoryChangesCache::GetIsEmpty() const
  1757. {
  1758. return (const_cast<TRemoteDirectoryChangesCache*>(this)->Count == 0);
  1759. }
  1760. //---------------------------------------------------------------------------
  1761. void __fastcall TRemoteDirectoryChangesCache::SetValue(const UnicodeString & Name,
  1762. const UnicodeString & Value)
  1763. {
  1764. int Index = IndexOfName(Name);
  1765. if (Index > 0)
  1766. {
  1767. Delete(Index);
  1768. }
  1769. Values[Name] = Value;
  1770. }
  1771. //---------------------------------------------------------------------------
  1772. UnicodeString __fastcall TRemoteDirectoryChangesCache::GetValue(const UnicodeString & Name)
  1773. {
  1774. UnicodeString Value = Values[Name];
  1775. SetValue(Name, Value);
  1776. return Value;
  1777. }
  1778. //---------------------------------------------------------------------------
  1779. void __fastcall TRemoteDirectoryChangesCache::AddDirectoryChange(
  1780. const UnicodeString SourceDir, const UnicodeString Change,
  1781. const UnicodeString TargetDir)
  1782. {
  1783. DebugAssert(!TargetDir.IsEmpty());
  1784. SetValue(TargetDir, L"//");
  1785. if (TTerminal::ExpandFileName(Change, SourceDir) != TargetDir)
  1786. {
  1787. UnicodeString Key;
  1788. if (DirectoryChangeKey(SourceDir, Change, Key))
  1789. {
  1790. SetValue(Key, TargetDir);
  1791. }
  1792. }
  1793. }
  1794. //---------------------------------------------------------------------------
  1795. void __fastcall TRemoteDirectoryChangesCache::ClearDirectoryChange(
  1796. UnicodeString SourceDir)
  1797. {
  1798. for (int Index = 0; Index < Count; Index++)
  1799. {
  1800. if (Names[Index].SubString(1, SourceDir.Length()) == SourceDir)
  1801. {
  1802. Delete(Index);
  1803. Index--;
  1804. }
  1805. }
  1806. }
  1807. //---------------------------------------------------------------------------
  1808. void __fastcall TRemoteDirectoryChangesCache::ClearDirectoryChangeTarget(
  1809. UnicodeString TargetDir)
  1810. {
  1811. UnicodeString Key;
  1812. // hack to clear at least local sym-link change in case symlink is deleted
  1813. DirectoryChangeKey(UnixExcludeTrailingBackslash(UnixExtractFilePath(TargetDir)),
  1814. UnixExtractFileName(TargetDir), Key);
  1815. for (int Index = 0; Index < Count; Index++)
  1816. {
  1817. UnicodeString Name = Names[Index];
  1818. if ((Name.SubString(1, TargetDir.Length()) == TargetDir) ||
  1819. (Values[Name].SubString(1, TargetDir.Length()) == TargetDir) ||
  1820. (!Key.IsEmpty() && (Name == Key)))
  1821. {
  1822. Delete(Index);
  1823. Index--;
  1824. }
  1825. }
  1826. }
  1827. //---------------------------------------------------------------------------
  1828. bool __fastcall TRemoteDirectoryChangesCache::GetDirectoryChange(
  1829. const UnicodeString SourceDir, const UnicodeString Change, UnicodeString & TargetDir)
  1830. {
  1831. UnicodeString Key;
  1832. bool Result;
  1833. Key = TTerminal::ExpandFileName(Change, SourceDir);
  1834. Result = (IndexOfName(Key) >= 0);
  1835. if (Result)
  1836. {
  1837. TargetDir = GetValue(Key);
  1838. // TargetDir is not "//" here only when Change is full path to symbolic link
  1839. if (TargetDir == L"//")
  1840. {
  1841. TargetDir = Key;
  1842. }
  1843. }
  1844. else
  1845. {
  1846. Result = DirectoryChangeKey(SourceDir, Change, Key);
  1847. if (Result)
  1848. {
  1849. UnicodeString Directory = GetValue(Key);
  1850. Result = !Directory.IsEmpty();
  1851. if (Result)
  1852. {
  1853. TargetDir = Directory;
  1854. }
  1855. }
  1856. }
  1857. return Result;
  1858. }
  1859. //---------------------------------------------------------------------------
  1860. void __fastcall TRemoteDirectoryChangesCache::Serialize(UnicodeString & Data)
  1861. {
  1862. Data = L"A";
  1863. int ACount = Count;
  1864. if (ACount > FMaxSize)
  1865. {
  1866. TStrings * Limited = new TStringList();
  1867. try
  1868. {
  1869. int Index = ACount - FMaxSize;
  1870. while (Index < ACount)
  1871. {
  1872. Limited->Add(Strings[Index]);
  1873. Index++;
  1874. }
  1875. Data += Limited->Text;
  1876. }
  1877. __finally
  1878. {
  1879. delete Limited;
  1880. }
  1881. }
  1882. else
  1883. {
  1884. Data += Text;
  1885. }
  1886. }
  1887. //---------------------------------------------------------------------------
  1888. void __fastcall TRemoteDirectoryChangesCache::Deserialize(const UnicodeString Data)
  1889. {
  1890. if (Data.IsEmpty())
  1891. {
  1892. Text = L"";
  1893. }
  1894. else
  1895. {
  1896. Text = Data.c_str() + 1;
  1897. }
  1898. }
  1899. //---------------------------------------------------------------------------
  1900. bool __fastcall TRemoteDirectoryChangesCache::DirectoryChangeKey(
  1901. const UnicodeString SourceDir, const UnicodeString Change, UnicodeString & Key)
  1902. {
  1903. bool Result = !Change.IsEmpty();
  1904. if (Result)
  1905. {
  1906. bool Absolute = UnixIsAbsolutePath(Change);
  1907. Result = !SourceDir.IsEmpty() || Absolute;
  1908. if (Result)
  1909. {
  1910. // expanded from ?: to avoid memory leaks
  1911. if (Absolute)
  1912. {
  1913. Key = Change;
  1914. }
  1915. else
  1916. {
  1917. Key = SourceDir + L"," + Change;
  1918. }
  1919. }
  1920. }
  1921. return Result;
  1922. }
  1923. //=== TRights ---------------------------------------------------------------
  1924. const wchar_t TRights::BasicSymbols[] = L"rwxrwxrwx";
  1925. const wchar_t TRights::CombinedSymbols[] = L"--s--s--t";
  1926. const wchar_t TRights::ExtendedSymbols[] = L"--S--S--T";
  1927. const wchar_t TRights::ModeGroups[] = L"ugo";
  1928. //---------------------------------------------------------------------------
  1929. __fastcall TRights::TRights()
  1930. {
  1931. FAllowUndef = false;
  1932. FSet = 0;
  1933. FUnset = 0;
  1934. Number = 0;
  1935. FUnknown = true;
  1936. }
  1937. //---------------------------------------------------------------------------
  1938. __fastcall TRights::TRights(unsigned short ANumber)
  1939. {
  1940. FAllowUndef = false;
  1941. FSet = 0;
  1942. FUnset = 0;
  1943. Number = ANumber;
  1944. }
  1945. //---------------------------------------------------------------------------
  1946. __fastcall TRights::TRights(const TRights & Source)
  1947. {
  1948. Assign(&Source);
  1949. }
  1950. //---------------------------------------------------------------------------
  1951. //---------------------------------------------------------------------------
  1952. void __fastcall TRights::Assign(const TRights * Source)
  1953. {
  1954. FAllowUndef = Source->AllowUndef;
  1955. FSet = Source->FSet;
  1956. FUnset = Source->FUnset;
  1957. FText = Source->FText;
  1958. FUnknown = Source->FUnknown;
  1959. }
  1960. //---------------------------------------------------------------------------
  1961. TRights::TFlag __fastcall TRights::RightToFlag(TRights::TRight Right)
  1962. {
  1963. return static_cast<TFlag>(1 << (rrLast - Right));
  1964. }
  1965. //---------------------------------------------------------------------------
  1966. bool __fastcall TRights::operator ==(const TRights & rhr) const
  1967. {
  1968. if (AllowUndef || rhr.AllowUndef)
  1969. {
  1970. for (int Right = rrFirst; Right <= rrLast; Right++)
  1971. {
  1972. if (RightUndef[static_cast<TRight>(Right)] !=
  1973. rhr.RightUndef[static_cast<TRight>(Right)])
  1974. {
  1975. return false;
  1976. }
  1977. }
  1978. return true;
  1979. }
  1980. else
  1981. {
  1982. return (Number == rhr.Number);
  1983. }
  1984. }
  1985. //---------------------------------------------------------------------------
  1986. bool __fastcall TRights::operator ==(unsigned short rhr) const
  1987. {
  1988. return (Number == rhr);
  1989. }
  1990. //---------------------------------------------------------------------------
  1991. bool __fastcall TRights::operator !=(const TRights & rhr) const
  1992. {
  1993. return !(*this == rhr);
  1994. }
  1995. //---------------------------------------------------------------------------
  1996. TRights & __fastcall TRights::operator =(unsigned short rhr)
  1997. {
  1998. Number = rhr;
  1999. return *this;
  2000. }
  2001. //---------------------------------------------------------------------------
  2002. TRights & __fastcall TRights::operator =(const TRights & rhr)
  2003. {
  2004. Assign(&rhr);
  2005. return *this;
  2006. }
  2007. //---------------------------------------------------------------------------
  2008. TRights __fastcall TRights::operator ~() const
  2009. {
  2010. TRights Result(static_cast<unsigned short>(~Number));
  2011. return Result;
  2012. }
  2013. //---------------------------------------------------------------------------
  2014. TRights __fastcall TRights::operator &(const TRights & rhr) const
  2015. {
  2016. TRights Result(*this);
  2017. Result &= rhr;
  2018. return Result;
  2019. }
  2020. //---------------------------------------------------------------------------
  2021. TRights __fastcall TRights::operator &(unsigned short rhr) const
  2022. {
  2023. TRights Result(*this);
  2024. Result &= rhr;
  2025. return Result;
  2026. }
  2027. //---------------------------------------------------------------------------
  2028. TRights & __fastcall TRights::operator &=(const TRights & rhr)
  2029. {
  2030. if (AllowUndef || rhr.AllowUndef)
  2031. {
  2032. for (int Right = rrFirst; Right <= rrLast; Right++)
  2033. {
  2034. if (RightUndef[static_cast<TRight>(Right)] !=
  2035. rhr.RightUndef[static_cast<TRight>(Right)])
  2036. {
  2037. RightUndef[static_cast<TRight>(Right)] = rsUndef;
  2038. }
  2039. }
  2040. }
  2041. else
  2042. {
  2043. Number &= rhr.Number;
  2044. }
  2045. return *this;
  2046. }
  2047. //---------------------------------------------------------------------------
  2048. TRights & __fastcall TRights::operator &=(unsigned short rhr)
  2049. {
  2050. Number &= rhr;
  2051. return *this;
  2052. }
  2053. //---------------------------------------------------------------------------
  2054. TRights __fastcall TRights::operator |(const TRights & rhr) const
  2055. {
  2056. TRights Result(*this);
  2057. Result |= rhr;
  2058. return Result;
  2059. }
  2060. //---------------------------------------------------------------------------
  2061. TRights __fastcall TRights::operator |(unsigned short rhr) const
  2062. {
  2063. TRights Result(*this);
  2064. Result |= rhr;
  2065. return Result;
  2066. }
  2067. //---------------------------------------------------------------------------
  2068. TRights & __fastcall TRights::operator |=(const TRights & rhr)
  2069. {
  2070. Number |= rhr.Number;
  2071. return *this;
  2072. }
  2073. //---------------------------------------------------------------------------
  2074. TRights & __fastcall TRights::operator |=(unsigned short rhr)
  2075. {
  2076. Number |= rhr;
  2077. return *this;
  2078. }
  2079. //---------------------------------------------------------------------------
  2080. void __fastcall TRights::SetAllowUndef(bool value)
  2081. {
  2082. if (FAllowUndef != value)
  2083. {
  2084. DebugAssert(!value || ((FSet | FUnset) == rfAllSpecials));
  2085. FAllowUndef = value;
  2086. }
  2087. }
  2088. //---------------------------------------------------------------------------
  2089. void __fastcall TRights::SetText(const UnicodeString & value)
  2090. {
  2091. if (value != Text)
  2092. {
  2093. if ((value.Length() != TextLen) ||
  2094. (!AllowUndef && (value.Pos(UndefSymbol) > 0)) ||
  2095. (value.Pos(L" ") > 0))
  2096. {
  2097. throw Exception(FMTLOAD(RIGHTS_ERROR, (value)));
  2098. }
  2099. FSet = 0;
  2100. FUnset = 0;
  2101. int Flag = 00001;
  2102. int ExtendedFlag = 01000;
  2103. bool KeepText = false;
  2104. for (int i = TextLen; i >= 1; i--)
  2105. {
  2106. if (value[i] == UnsetSymbol)
  2107. {
  2108. FUnset |= static_cast<unsigned short>(Flag | ExtendedFlag);
  2109. }
  2110. else if (value[i] == UndefSymbol)
  2111. {
  2112. // do nothing
  2113. }
  2114. else if (value[i] == CombinedSymbols[i - 1])
  2115. {
  2116. FSet |= static_cast<unsigned short>(Flag | ExtendedFlag);
  2117. }
  2118. else if (value[i] == ExtendedSymbols[i - 1])
  2119. {
  2120. FSet |= static_cast<unsigned short>(ExtendedFlag);
  2121. FUnset |= static_cast<unsigned short>(Flag);
  2122. }
  2123. else
  2124. {
  2125. if (value[i] != BasicSymbols[i - 1])
  2126. {
  2127. KeepText = true;
  2128. }
  2129. FSet |= static_cast<unsigned short>(Flag);
  2130. if (i % 3 == 0)
  2131. {
  2132. FUnset |= static_cast<unsigned short>(ExtendedFlag);
  2133. }
  2134. }
  2135. Flag <<= 1;
  2136. if (i % 3 == 1)
  2137. {
  2138. ExtendedFlag <<= 1;
  2139. }
  2140. }
  2141. FText = KeepText ? value : UnicodeString();
  2142. }
  2143. FUnknown = false;
  2144. }
  2145. //---------------------------------------------------------------------------
  2146. UnicodeString __fastcall TRights::GetText() const
  2147. {
  2148. if (!FText.IsEmpty())
  2149. {
  2150. return FText;
  2151. }
  2152. else
  2153. {
  2154. UnicodeString Result;
  2155. Result.SetLength(TextLen);
  2156. int Flag = 00001;
  2157. int ExtendedFlag = 01000;
  2158. bool ExtendedPos = true;
  2159. wchar_t Symbol;
  2160. int i = TextLen;
  2161. while (i >= 1)
  2162. {
  2163. if (ExtendedPos &&
  2164. ((FSet & (Flag | ExtendedFlag)) == (Flag | ExtendedFlag)))
  2165. {
  2166. Symbol = CombinedSymbols[i - 1];
  2167. }
  2168. else if ((FSet & Flag) != 0)
  2169. {
  2170. Symbol = BasicSymbols[i - 1];
  2171. }
  2172. else if (ExtendedPos && ((FSet & ExtendedFlag) != 0))
  2173. {
  2174. Symbol = ExtendedSymbols[i - 1];
  2175. }
  2176. else if ((!ExtendedPos && ((FUnset & Flag) == Flag)) ||
  2177. (ExtendedPos && ((FUnset & (Flag | ExtendedFlag)) == (Flag | ExtendedFlag))))
  2178. {
  2179. Symbol = UnsetSymbol;
  2180. }
  2181. else
  2182. {
  2183. Symbol = UndefSymbol;
  2184. }
  2185. Result[i] = Symbol;
  2186. Flag <<= 1;
  2187. i--;
  2188. ExtendedPos = ((i % 3) == 0);
  2189. if (ExtendedPos)
  2190. {
  2191. ExtendedFlag <<= 1;
  2192. }
  2193. }
  2194. return Result;
  2195. }
  2196. }
  2197. //---------------------------------------------------------------------------
  2198. void __fastcall TRights::SetOctal(UnicodeString value)
  2199. {
  2200. UnicodeString AValue(value);
  2201. if (AValue.Length() == 3)
  2202. {
  2203. AValue = L"0" + AValue;
  2204. }
  2205. if (Octal != AValue)
  2206. {
  2207. bool Correct = (AValue.Length() == 4);
  2208. if (Correct)
  2209. {
  2210. for (int i = 1; (i <= AValue.Length()) && Correct; i++)
  2211. {
  2212. Correct = (AValue[i] >= L'0') && (AValue[i] <= L'7');
  2213. }
  2214. }
  2215. if (!Correct)
  2216. {
  2217. throw Exception(FMTLOAD(INVALID_OCTAL_PERMISSIONS, (value)));
  2218. }
  2219. Number = static_cast<unsigned short>(
  2220. ((AValue[1] - L'0') << 9) +
  2221. ((AValue[2] - L'0') << 6) +
  2222. ((AValue[3] - L'0') << 3) +
  2223. ((AValue[4] - L'0') << 0));
  2224. }
  2225. FUnknown = false;
  2226. }
  2227. //---------------------------------------------------------------------------
  2228. unsigned long __fastcall TRights::GetNumberDecadic() const
  2229. {
  2230. unsigned long N = NumberSet; // used to be "Number"
  2231. unsigned long Result =
  2232. ((N & 07000) / 01000 * 1000) +
  2233. ((N & 00700) / 0100 * 100) +
  2234. ((N & 00070) / 010 * 10) +
  2235. ((N & 00007) / 01 * 1);
  2236. return Result;
  2237. }
  2238. //---------------------------------------------------------------------------
  2239. UnicodeString __fastcall TRights::GetOctal() const
  2240. {
  2241. UnicodeString Result;
  2242. unsigned short N = NumberSet; // used to be "Number"
  2243. Result.SetLength(4);
  2244. Result[1] = static_cast<wchar_t>(L'0' + ((N & 07000) >> 9));
  2245. Result[2] = static_cast<wchar_t>(L'0' + ((N & 00700) >> 6));
  2246. Result[3] = static_cast<wchar_t>(L'0' + ((N & 00070) >> 3));
  2247. Result[4] = static_cast<wchar_t>(L'0' + ((N & 00007) >> 0));
  2248. return Result;
  2249. }
  2250. //---------------------------------------------------------------------------
  2251. void __fastcall TRights::SetNumber(unsigned short value)
  2252. {
  2253. if ((FSet != value) || ((FSet | FUnset) != rfAllSpecials))
  2254. {
  2255. FSet = value;
  2256. FUnset = static_cast<unsigned short>(rfAllSpecials & ~FSet);
  2257. FText = L"";
  2258. }
  2259. FUnknown = false;
  2260. }
  2261. //---------------------------------------------------------------------------
  2262. unsigned short __fastcall TRights::GetNumber() const
  2263. {
  2264. DebugAssert(!IsUndef);
  2265. return FSet;
  2266. }
  2267. //---------------------------------------------------------------------------
  2268. void __fastcall TRights::SetRight(TRight Right, bool value)
  2269. {
  2270. RightUndef[Right] = (value ? rsYes : rsNo);
  2271. }
  2272. //---------------------------------------------------------------------------
  2273. bool __fastcall TRights::GetRight(TRight Right) const
  2274. {
  2275. TState State = RightUndef[Right];
  2276. DebugAssert(State != rsUndef);
  2277. return (State == rsYes);
  2278. }
  2279. //---------------------------------------------------------------------------
  2280. void __fastcall TRights::SetRightUndef(TRight Right, TState value)
  2281. {
  2282. if (value != RightUndef[Right])
  2283. {
  2284. DebugAssert((value != rsUndef) || AllowUndef);
  2285. TFlag Flag = RightToFlag(Right);
  2286. switch (value)
  2287. {
  2288. case rsYes:
  2289. FSet |= static_cast<unsigned short>(Flag);
  2290. FUnset &= static_cast<unsigned short>(~Flag);
  2291. break;
  2292. case rsNo:
  2293. FSet &= static_cast<unsigned short>(~Flag);
  2294. FUnset |= static_cast<unsigned short>(Flag);
  2295. break;
  2296. case rsUndef:
  2297. default:
  2298. FSet &= static_cast<unsigned short>(~Flag);
  2299. FUnset &= static_cast<unsigned short>(~Flag);
  2300. break;
  2301. }
  2302. FText = L"";
  2303. }
  2304. FUnknown = false;
  2305. }
  2306. //---------------------------------------------------------------------------
  2307. TRights::TState __fastcall TRights::GetRightUndef(TRight Right) const
  2308. {
  2309. TFlag Flag = RightToFlag(Right);
  2310. TState Result;
  2311. if ((FSet & Flag) != 0)
  2312. {
  2313. Result = rsYes;
  2314. }
  2315. else if ((FUnset & Flag) != 0)
  2316. {
  2317. Result = rsNo;
  2318. }
  2319. else
  2320. {
  2321. Result = rsUndef;
  2322. }
  2323. return Result;
  2324. }
  2325. //---------------------------------------------------------------------------
  2326. void __fastcall TRights::SetReadOnly(bool value)
  2327. {
  2328. Right[rrUserWrite] = !value;
  2329. Right[rrGroupWrite] = !value;
  2330. Right[rrOtherWrite] = !value;
  2331. }
  2332. //---------------------------------------------------------------------------
  2333. bool __fastcall TRights::GetReadOnly()
  2334. {
  2335. return Right[rrUserWrite] && Right[rrGroupWrite] && Right[rrOtherWrite];
  2336. }
  2337. //---------------------------------------------------------------------------
  2338. UnicodeString __fastcall TRights::GetSimplestStr() const
  2339. {
  2340. if (IsUndef)
  2341. {
  2342. return ModeStr;
  2343. }
  2344. else
  2345. {
  2346. return Octal;
  2347. }
  2348. }
  2349. //---------------------------------------------------------------------------
  2350. UnicodeString __fastcall TRights::GetModeStr() const
  2351. {
  2352. UnicodeString Result;
  2353. UnicodeString SetModeStr, UnsetModeStr;
  2354. TRight Right;
  2355. int Index;
  2356. for (int Group = 0; Group < 3; Group++)
  2357. {
  2358. SetModeStr = L"";
  2359. UnsetModeStr = L"";
  2360. for (int Mode = 0; Mode < 3; Mode++)
  2361. {
  2362. Index = (Group * 3) + Mode;
  2363. Right = static_cast<TRight>(rrUserRead + Index);
  2364. switch (RightUndef[Right])
  2365. {
  2366. case rsYes:
  2367. SetModeStr += BasicSymbols[Index];
  2368. break;
  2369. case rsNo:
  2370. UnsetModeStr += BasicSymbols[Index];
  2371. break;
  2372. }
  2373. }
  2374. Right = static_cast<TRight>(rrUserIDExec + Group);
  2375. Index = (Group * 3) + 2;
  2376. switch (RightUndef[Right])
  2377. {
  2378. case rsYes:
  2379. SetModeStr += CombinedSymbols[Index];
  2380. break;
  2381. case rsNo:
  2382. UnsetModeStr += CombinedSymbols[Index];
  2383. break;
  2384. }
  2385. if (!SetModeStr.IsEmpty() || !UnsetModeStr.IsEmpty())
  2386. {
  2387. if (!Result.IsEmpty())
  2388. {
  2389. Result += L',';
  2390. }
  2391. Result += ModeGroups[Group];
  2392. if (!SetModeStr.IsEmpty())
  2393. {
  2394. Result += L"+" + SetModeStr;
  2395. }
  2396. if (!UnsetModeStr.IsEmpty())
  2397. {
  2398. Result += L"-" + UnsetModeStr;
  2399. }
  2400. }
  2401. }
  2402. return Result;
  2403. }
  2404. //---------------------------------------------------------------------------
  2405. void __fastcall TRights::AddExecute()
  2406. {
  2407. for (int Group = 0; Group < 3; Group++)
  2408. {
  2409. if ((RightUndef[static_cast<TRight>(rrUserRead + (Group * 3))] == rsYes) ||
  2410. (RightUndef[static_cast<TRight>(rrUserWrite + (Group * 3))] == rsYes))
  2411. {
  2412. Right[static_cast<TRight>(rrUserExec + (Group * 3))] = true;
  2413. }
  2414. }
  2415. FUnknown = false;
  2416. }
  2417. //---------------------------------------------------------------------------
  2418. void __fastcall TRights::AllUndef()
  2419. {
  2420. if ((FSet != 0) || (FUnset != 0))
  2421. {
  2422. FSet = 0;
  2423. FUnset = 0;
  2424. FText = L"";
  2425. }
  2426. FUnknown = false;
  2427. }
  2428. //---------------------------------------------------------------------------
  2429. bool __fastcall TRights::GetIsUndef() const
  2430. {
  2431. return ((FSet | FUnset) != rfAllSpecials);
  2432. }
  2433. //---------------------------------------------------------------------------
  2434. __fastcall TRights::operator unsigned short() const
  2435. {
  2436. return Number;
  2437. }
  2438. //---------------------------------------------------------------------------
  2439. __fastcall TRights::operator unsigned long() const
  2440. {
  2441. return Number;
  2442. }
  2443. //=== TRemoteProperties -------------------------------------------------------
  2444. __fastcall TRemoteProperties::TRemoteProperties()
  2445. {
  2446. Default();
  2447. }
  2448. //---------------------------------------------------------------------------
  2449. __fastcall TRemoteProperties::TRemoteProperties(const TRemoteProperties & rhp) :
  2450. Valid(rhp.Valid),
  2451. Recursive(rhp.Recursive),
  2452. Rights(rhp.Rights),
  2453. AddXToDirectories(rhp.AddXToDirectories),
  2454. Group(rhp.Group),
  2455. Owner(rhp.Owner),
  2456. Modification(rhp.Modification),
  2457. LastAccess(rhp.Modification)
  2458. {
  2459. }
  2460. //---------------------------------------------------------------------------
  2461. void __fastcall TRemoteProperties::Default()
  2462. {
  2463. Valid.Clear();
  2464. AddXToDirectories = false;
  2465. Rights.AllowUndef = false;
  2466. Rights.Number = 0;
  2467. Group.Clear();
  2468. Owner.Clear();
  2469. Recursive = false;
  2470. }
  2471. //---------------------------------------------------------------------------
  2472. bool __fastcall TRemoteProperties::operator ==(const TRemoteProperties & rhp) const
  2473. {
  2474. bool Result = (Valid == rhp.Valid && Recursive == rhp.Recursive);
  2475. if (Result)
  2476. {
  2477. if ((Valid.Contains(vpRights) &&
  2478. (Rights != rhp.Rights || AddXToDirectories != rhp.AddXToDirectories)) ||
  2479. (Valid.Contains(vpOwner) && (Owner != rhp.Owner)) ||
  2480. (Valid.Contains(vpGroup) && (Group != rhp.Group)) ||
  2481. (Valid.Contains(vpModification) && (Modification != rhp.Modification)) ||
  2482. (Valid.Contains(vpLastAccess) && (LastAccess != rhp.LastAccess)))
  2483. {
  2484. Result = false;
  2485. }
  2486. }
  2487. return Result;
  2488. }
  2489. //---------------------------------------------------------------------------
  2490. bool __fastcall TRemoteProperties::operator !=(const TRemoteProperties & rhp) const
  2491. {
  2492. return !(*this == rhp);
  2493. }
  2494. //---------------------------------------------------------------------------
  2495. TRemoteProperties __fastcall TRemoteProperties::CommonProperties(TStrings * FileList)
  2496. {
  2497. // TODO: Modification and LastAccess
  2498. TRemoteProperties CommonProperties;
  2499. for (int Index = 0; Index < FileList->Count; Index++)
  2500. {
  2501. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  2502. DebugAssert(File);
  2503. if (!Index)
  2504. {
  2505. CommonProperties.Rights = *(File->Rights);
  2506. // previously we allowed undef implicitly for directories,
  2507. // now we do it explicitly in properties dialog and only in combination
  2508. // with "recursive" option
  2509. CommonProperties.Rights.AllowUndef = File->Rights->IsUndef;
  2510. CommonProperties.Valid << vpRights;
  2511. if (File->Owner.IsSet)
  2512. {
  2513. CommonProperties.Owner = File->Owner;
  2514. CommonProperties.Valid << vpOwner;
  2515. }
  2516. if (File->Group.IsSet)
  2517. {
  2518. CommonProperties.Group = File->Group;
  2519. CommonProperties.Valid << vpGroup;
  2520. }
  2521. }
  2522. else
  2523. {
  2524. CommonProperties.Rights.AllowUndef = True;
  2525. CommonProperties.Rights &= *File->Rights;
  2526. if (CommonProperties.Owner != File->Owner)
  2527. {
  2528. CommonProperties.Owner.Clear();
  2529. CommonProperties.Valid >> vpOwner;
  2530. }
  2531. if (CommonProperties.Group != File->Group)
  2532. {
  2533. CommonProperties.Group.Clear();
  2534. CommonProperties.Valid >> vpGroup;
  2535. }
  2536. }
  2537. }
  2538. return CommonProperties;
  2539. }
  2540. //---------------------------------------------------------------------------
  2541. TRemoteProperties __fastcall TRemoteProperties::ChangedProperties(
  2542. const TRemoteProperties & OriginalProperties, TRemoteProperties NewProperties)
  2543. {
  2544. // TODO: Modification and LastAccess
  2545. if (!NewProperties.Recursive)
  2546. {
  2547. if (NewProperties.Rights == OriginalProperties.Rights &&
  2548. !NewProperties.AddXToDirectories)
  2549. {
  2550. NewProperties.Valid >> vpRights;
  2551. }
  2552. if (NewProperties.Group == OriginalProperties.Group)
  2553. {
  2554. NewProperties.Valid >> vpGroup;
  2555. }
  2556. if (NewProperties.Owner == OriginalProperties.Owner)
  2557. {
  2558. NewProperties.Valid >> vpOwner;
  2559. }
  2560. }
  2561. return NewProperties;
  2562. }
  2563. //---------------------------------------------------------------------------
  2564. void __fastcall TRemoteProperties::Load(THierarchicalStorage * Storage)
  2565. {
  2566. unsigned char Buf[sizeof(Valid)];
  2567. if (static_cast<size_t>(Storage->ReadBinaryData(L"Valid", &Buf, sizeof(Buf))) == sizeof(Buf))
  2568. {
  2569. memmove(&Valid, Buf, sizeof(Valid));
  2570. }
  2571. if (Valid.Contains(vpRights))
  2572. {
  2573. Rights.Text = Storage->ReadString(L"Rights", Rights.Text);
  2574. }
  2575. // TODO
  2576. }
  2577. //---------------------------------------------------------------------------
  2578. void __fastcall TRemoteProperties::Save(THierarchicalStorage * Storage) const
  2579. {
  2580. Storage->WriteBinaryData(UnicodeString(L"Valid"),
  2581. static_cast<const void *>(&Valid), sizeof(Valid));
  2582. if (Valid.Contains(vpRights))
  2583. {
  2584. Storage->WriteString(L"Rights", Rights.Text);
  2585. }
  2586. // TODO
  2587. }