RemoteFiles.cpp 75 KB

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