RemoteFiles.cpp 62 KB

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