RemoteFiles.cpp 62 KB

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