RemoteFiles.cpp 74 KB

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