RemoteFiles.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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. if (!Path.IsDelimiter("/", Path.Length())) return Path + "/";
  16. else return Path;
  17. }
  18. //---------------------------------------------------------------------------
  19. AnsiString __fastcall UnixExcludeTrailingBackslash(const AnsiString Path)
  20. {
  21. if ((Path.Length() > 1) && Path.IsDelimiter("/", Path.Length()))
  22. return Path.SubString(1, Path.Length() - 1);
  23. else return Path;
  24. }
  25. //---------------------------------------------------------------------------
  26. Boolean __fastcall UnixComparePaths(const AnsiString Path1, const AnsiString Path2)
  27. {
  28. return (UnixIncludeTrailingBackslash(Path1) == UnixIncludeTrailingBackslash(Path2));
  29. }
  30. //---------------------------------------------------------------------------
  31. AnsiString __fastcall UnixExtractFileDir(const AnsiString Path)
  32. {
  33. int Pos = Path.LastDelimiter('/');
  34. // it used to return Path when no slash was found
  35. if (Pos > 1)
  36. {
  37. return Path.SubString(1, Pos - 1);
  38. }
  39. else
  40. {
  41. return (Pos == 1) ? AnsiString("/") : AnsiString();
  42. }
  43. }
  44. //---------------------------------------------------------------------------
  45. // must return trailing backslash
  46. AnsiString __fastcall UnixExtractFilePath(const AnsiString Path)
  47. {
  48. int Pos = Path.LastDelimiter('/');
  49. // it used to return Path when no slash was found
  50. return (Pos > 0) ? Path.SubString(1, Pos) : AnsiString();
  51. }
  52. //---------------------------------------------------------------------------
  53. AnsiString __fastcall UnixExtractFileName(const AnsiString Path)
  54. {
  55. int Pos = Path.LastDelimiter('/');
  56. return (Pos > 0) ? Path.SubString(Pos + 1, Path.Length() - Pos) : Path;
  57. }
  58. //---------------------------------------------------------------------------
  59. AnsiString __fastcall UnixExtractFileExt(const AnsiString Path)
  60. {
  61. AnsiString FileName = UnixExtractFileName(Path);
  62. int Pos = FileName.LastDelimiter(".");
  63. return (Pos > 0) ? Path.SubString(Pos, Path.Length() - Pos + 1) : AnsiString();
  64. }
  65. //- TRemoteFiles ------------------------------------------------------------
  66. __fastcall TRemoteFile::TRemoteFile(TRemoteFile * ALinkedByFile):
  67. TPersistent()
  68. {
  69. FLinkedFile = NULL;
  70. FRights = new TRights();
  71. FIconIndex = -1;
  72. FCyclicLink = false;
  73. FModificationFmt = mfFull;
  74. FLinkedByFile = ALinkedByFile;
  75. FTerminal = NULL;
  76. FDirectory = NULL;
  77. }
  78. //---------------------------------------------------------------------------
  79. __fastcall TRemoteFile::~TRemoteFile()
  80. {
  81. delete FRights;
  82. delete FLinkedFile;
  83. }
  84. //---------------------------------------------------------------------------
  85. TRemoteFile * __fastcall TRemoteFile::Duplicate()
  86. {
  87. TRemoteFile * Result;
  88. Result = new TRemoteFile();
  89. try
  90. {
  91. if (FLinkedFile)
  92. {
  93. Result->FLinkedFile = FLinkedFile->Duplicate();
  94. Result->FLinkedFile->FLinkedByFile = Result;
  95. }
  96. *Result->Rights = *FRights;
  97. #define COPY_FP(PROP) Result->F ## PROP = F ## PROP;
  98. COPY_FP(Terminal);
  99. COPY_FP(Owner);
  100. COPY_FP(ModificationFmt);
  101. COPY_FP(Size);
  102. COPY_FP(FileName);
  103. COPY_FP(INodeBlocks);
  104. COPY_FP(Modification);
  105. COPY_FP(Group);
  106. COPY_FP(IconIndex);
  107. COPY_FP(IsSymLink);
  108. COPY_FP(LinkTo);
  109. COPY_FP(Type);
  110. COPY_FP(Selected);
  111. COPY_FP(CyclicLink);
  112. #undef COPY_FP
  113. }
  114. catch(...)
  115. {
  116. delete Result;
  117. throw;
  118. }
  119. return Result;
  120. }
  121. //---------------------------------------------------------------------------
  122. Integer __fastcall TRemoteFile::GetIconIndex()
  123. {
  124. assert(FIconIndex >= -1);
  125. if (FIconIndex < 0)
  126. {
  127. /* TODO : If file is link: Should be attributes taken from linked file? */
  128. unsigned long Attrs = FILE_ATTRIBUTE_NORMAL;
  129. if (IsDirectory) Attrs |= FILE_ATTRIBUTE_DIRECTORY;
  130. if (IsHidden) Attrs |= FILE_ATTRIBUTE_HIDDEN;
  131. TSHFileInfo SHFileInfo;
  132. AnsiString DumbFileName = (IsSymLink && !LinkTo.IsEmpty() ? LinkTo : FileName);
  133. // On Win2k we get icon of "ZIP drive" for ".." (parent directory)
  134. if (DumbFileName == "..") DumbFileName = "dumb";
  135. SHGetFileInfo(DumbFileName.c_str(),
  136. Attrs, &SHFileInfo, sizeof(SHFileInfo),
  137. SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES);
  138. FIconIndex = SHFileInfo.iIcon;
  139. }
  140. return FIconIndex;
  141. }
  142. //---------------------------------------------------------------------------
  143. Boolean __fastcall TRemoteFile::GetIsHidden()
  144. {
  145. return (!IsParentDirectory && !IsThisDirectory &&
  146. !FileName.IsEmpty() && (FileName[1] == '.'));
  147. }
  148. //---------------------------------------------------------------------------
  149. Boolean __fastcall TRemoteFile::GetIsDirectory() const
  150. {
  151. return (toupper(Type) == FILETYPE_DIRECTORY);
  152. }
  153. //---------------------------------------------------------------------------
  154. Boolean __fastcall TRemoteFile::GetIsParentDirectory()
  155. {
  156. return (FileName == PARENTDIRECTORY);
  157. }
  158. //---------------------------------------------------------------------------
  159. Boolean __fastcall TRemoteFile::GetIsThisDirectory()
  160. {
  161. return (FileName == THISDIRECTORY);
  162. }
  163. //---------------------------------------------------------------------------
  164. Boolean __fastcall TRemoteFile::GetIsInaccesibleDirectory()
  165. {
  166. Boolean Result;
  167. if (IsDirectory)
  168. {
  169. assert(Terminal);
  170. Result = !
  171. (((Rights->RightUndef[rfOtherExec] != rsNo)) ||
  172. ((Rights->Right[rfGroupExec] != rsNo) &&
  173. (Terminal->UserGroups->IndexOf(Group) >= 0)) ||
  174. ((Rights->Right[rfUserExec] != rsNo) &&
  175. (AnsiCompareText(Terminal->UserName, Owner) == 0)));
  176. }
  177. else Result = False;
  178. return Result;
  179. }
  180. //---------------------------------------------------------------------------
  181. char __fastcall TRemoteFile::GetType() const
  182. {
  183. if (IsSymLink && FLinkedFile) return FLinkedFile->Type;
  184. else return FType;
  185. }
  186. //---------------------------------------------------------------------------
  187. void __fastcall TRemoteFile::SetType(char AType)
  188. {
  189. FType = AType;
  190. // Allow even non-standard file types (e.g. 'S')
  191. // if (!AnsiString("-DL").Pos((Char)toupper(FType))) Abort();
  192. FIsSymLink = ((Char)toupper(FType) == FILETYPE_SYMLINK);
  193. }
  194. //---------------------------------------------------------------------------
  195. TRemoteFile * __fastcall TRemoteFile::GetLinkedFile()
  196. {
  197. // it would be called releatedly for broken symlinks
  198. //if (!FLinkedFile) FindLinkedFile();
  199. return FLinkedFile;
  200. }
  201. //---------------------------------------------------------------------------
  202. void __fastcall TRemoteFile::SetLinkedFile(TRemoteFile * value)
  203. {
  204. if (FLinkedFile != value)
  205. {
  206. if (FLinkedFile) delete FLinkedFile;
  207. FLinkedFile = value;
  208. }
  209. }
  210. //---------------------------------------------------------------------------
  211. bool __fastcall TRemoteFile::GetBrokenLink()
  212. {
  213. assert(Terminal);
  214. // If file is symlink but we couldn't find linked file we assume broken link
  215. return (IsSymLink && (FCyclicLink || !FLinkedFile) &&
  216. Terminal->SessionData->ResolveSymlinks && Terminal->IsCapable[fcResolveSymlink]);
  217. // "!FLinkTo.IsEmpty()" removed because it does not work with SFTP
  218. }
  219. //---------------------------------------------------------------------------
  220. void __fastcall TRemoteFile::ShiftTime(const TDateTime & Difference)
  221. {
  222. double D = double(Difference);
  223. if ((D != 0) && (FModificationFmt != mfMDY))
  224. {
  225. assert(int(FModification) != 0);
  226. FModification = double(FModification) + D;
  227. assert(int(FLastAccess) != 0);
  228. FLastAccess = double(FLastAccess) + D;
  229. }
  230. }
  231. //---------------------------------------------------------------------------
  232. void __fastcall TRemoteFile::SetModification(const TDateTime & value)
  233. {
  234. if (FModification != value)
  235. {
  236. FModificationFmt = mfFull;
  237. FModification = value;
  238. }
  239. }
  240. //---------------------------------------------------------------------------
  241. AnsiString __fastcall TRemoteFile::GetUserModificationStr()
  242. {
  243. if (FModificationFmt == mfFull)
  244. {
  245. return FormatDateTime("ddddd tt", Modification);
  246. }
  247. else
  248. {
  249. return FormatDateTime("ddddd t", Modification);
  250. }
  251. }
  252. //---------------------------------------------------------------------------
  253. AnsiString __fastcall TRemoteFile::GetModificationStr()
  254. {
  255. Word Year, Month, Day, Hour, Min, Sec, MSec;
  256. Modification.DecodeDate(&Year, &Month, &Day);
  257. Modification.DecodeTime(&Hour, &Min, &Sec, &MSec);
  258. if (FModificationFmt != mfMDY)
  259. return FORMAT("%3s %2d %2d:%2.2d",
  260. (EngShortMonthNames[Month-1], Day, Hour, Min));
  261. else
  262. return FORMAT("%3s %2d %2d",
  263. (EngShortMonthNames[Month-1], Day, Year));
  264. }
  265. //---------------------------------------------------------------------------
  266. AnsiString __fastcall TRemoteFile::GetExtension()
  267. {
  268. return UnixExtractFileExt(FFileName);
  269. }
  270. //---------------------------------------------------------------------------
  271. void __fastcall TRemoteFile::SetRights(TRights * value)
  272. {
  273. FRights->Assign(value);
  274. }
  275. //---------------------------------------------------------------------------
  276. AnsiString __fastcall TRemoteFile::GetRightsStr()
  277. {
  278. return FRights->Text;
  279. }
  280. //---------------------------------------------------------------------------
  281. void __fastcall TRemoteFile::SetListingStr(AnsiString value)
  282. {
  283. // Value stored in 'value' can be used for error message
  284. AnsiString Line = value;
  285. FIconIndex = -1;
  286. try
  287. {
  288. AnsiString Col;
  289. // Do we need to do this (is ever TAB is LS output)?
  290. Line = ReplaceChar(Line, '\t', ' ');
  291. Type = Line[1];
  292. Line.Delete(1, 1);
  293. #define GETNCOL \
  294. { if (Line.IsEmpty()) throw Exception(""); \
  295. Integer P = Line.Pos(' '); \
  296. if (P) { Col = Line.SubString(1, P-1); Line.Delete(1, P); } \
  297. else { Col = Line; Line = ""; } \
  298. }
  299. #define GETCOL { GETNCOL; Line = TrimLeft(Line); }
  300. // Rights string may contain special permission attributes (S,t, ...)
  301. Rights->AllowUndef = True;
  302. // On some system there is no space between permissions and node blocks count columns
  303. // so we get only first 9 characters and trim all following spaces (if any)
  304. Rights->Text = Line.SubString(1, 9);
  305. Line.Delete(1, 9);
  306. // Rights column maybe followed by '+' sign, we ignore it
  307. if (!Line.IsEmpty() && (Line[1] == '+')) Line.Delete(1, 1);
  308. Line = Line.TrimLeft();
  309. GETCOL;
  310. FINodeBlocks = StrToInt(Col);
  311. GETCOL;
  312. FOwner = Col;
  313. // #60 17.10.01: group name can contain space
  314. FGroup = "";
  315. GETCOL;
  316. __int64 ASize;
  317. do
  318. {
  319. FGroup += Col;
  320. GETCOL;
  321. assert(!Col.IsEmpty());
  322. // for devices etc.. there is additional column ending by comma, we ignore it
  323. if (Col[Col.Length()] == ',') GETCOL;
  324. ASize = StrToInt64Def(Col, -1);
  325. // if it's not a number (file size) we take it as part of group name
  326. // (at least on CygWin, there can be group with space in its name)
  327. if (ASize < 0) Col = " " + Col;
  328. }
  329. while (ASize < 0);
  330. // do not read modification time and filename if it is already set
  331. if (double(FModification) == 0 && FileName.IsEmpty())
  332. {
  333. FSize = ASize;
  334. Word Day = 0, Month, Year, Hour, Min, P;
  335. GETCOL;
  336. Day = (Word)StrToIntDef(Col, 0);
  337. if (Day > 0)
  338. {
  339. GETCOL;
  340. }
  341. Month = 0;
  342. for (Word IMonth = 0; IMonth < 12; IMonth++)
  343. if (!Col.AnsiCompareIC(EngShortMonthNames[IMonth])) { Month = IMonth; Month++; break; }
  344. if (!Month) Abort();
  345. if (Day == 0)
  346. {
  347. GETNCOL;
  348. Day = (Word)StrToInt(Col);
  349. }
  350. if ((Day < 1) || (Day > 31)) Abort();
  351. // Time/Year indicator is always 5 charactes long (???), on most
  352. // systems year is aligned to right (_YYYY), but on some to left (YYYY_),
  353. // we must ensure that trailing space is also deleted, so real
  354. // separator space is not treated as part of file name
  355. Col = Line.SubString(1, 6).Trim();
  356. Line.Delete(1, 6);
  357. // GETNCOL; // We don't want to trim input strings (name with space at beginning???)
  358. // Check if we got time (contains :) or year
  359. if ((P = (Word)Col.Pos(':')) > 0)
  360. {
  361. Word CurrMonth, CurrDay;
  362. Hour = (Word)StrToInt(Col.SubString(1, P-1));
  363. Min = (Word)StrToInt(Col.SubString(P+1, Col.Length() - P));
  364. if (Hour > 23 || Hour > 59) Abort();
  365. // When we don't got year, we assume current year
  366. // with exception that the date would be in future
  367. // in this case we assume last year.
  368. DecodeDate(Date(), Year, CurrMonth, CurrDay);
  369. if ((Month > CurrMonth) ||
  370. (Month == CurrMonth && Day > CurrDay)) Year--;
  371. FModificationFmt = mfMDHM;
  372. }
  373. else
  374. {
  375. Year = (Word)StrToInt(Col);
  376. if (Year > 10000) Abort();
  377. // When we don't got time we assume midnight
  378. Hour = 0; Min = 0;
  379. FModificationFmt = mfMDY;
  380. }
  381. FModification = AdjustDateTimeFromUnix(
  382. EncodeDate(Year, Month, Day) + EncodeTime(Hour, Min, 0, 0));
  383. if (double(FLastAccess) == 0)
  384. {
  385. FLastAccess = FModification;
  386. }
  387. // separating space is already deleted, other spaces are treated as part of name
  388. {
  389. int P;
  390. FLinkTo = "";
  391. if (IsSymLink)
  392. {
  393. P = Line.Pos(SYMLINKSTR);
  394. if (P)
  395. {
  396. FLinkTo = Line.SubString(
  397. P + strlen(SYMLINKSTR), Line.Length() - P + strlen(SYMLINKSTR) + 1);
  398. Line.SetLength(P - 1);
  399. }
  400. else
  401. {
  402. Abort();
  403. }
  404. }
  405. FFileName = UnixExtractFileName(Line);
  406. }
  407. }
  408. #undef GETNCOL
  409. #undef GETCOL
  410. }
  411. catch (Exception &E)
  412. {
  413. throw ETerminal(&E, FmtLoadStr(LIST_LINE_ERROR, ARRAYOFCONST((value))));
  414. }
  415. assert(Terminal);
  416. if (IsSymLink && Terminal->SessionData->ResolveSymlinks &&
  417. Terminal->IsCapable[fcResolveSymlink])
  418. {
  419. FindLinkedFile();
  420. }
  421. }
  422. //---------------------------------------------------------------------------
  423. void __fastcall TRemoteFile::FindLinkedFile()
  424. {
  425. assert(Terminal && IsSymLink);
  426. if (FLinkedFile) delete FLinkedFile;
  427. FLinkedFile = NULL;
  428. FCyclicLink = false;
  429. if (!LinkTo.IsEmpty())
  430. {
  431. // check for cyclic link
  432. TRemoteFile * LinkedBy = FLinkedByFile;
  433. while (LinkedBy)
  434. {
  435. if (LinkedBy->LinkTo == LinkTo)
  436. {
  437. // this is currenly redundant information, because it is used only to
  438. // detect broken symlink, which would be otherwise detected
  439. // by FLinkedFile == NULL
  440. FCyclicLink = true;
  441. break;
  442. }
  443. LinkedBy = LinkedBy->FLinkedByFile;
  444. }
  445. }
  446. if (FCyclicLink)
  447. {
  448. TRemoteFile * LinkedBy = FLinkedByFile;
  449. while (LinkedBy)
  450. {
  451. LinkedBy->FCyclicLink = true;
  452. LinkedBy = LinkedBy->FLinkedByFile;
  453. }
  454. }
  455. else
  456. {
  457. assert(Terminal->SessionData->ResolveSymlinks && Terminal->IsCapable[fcResolveSymlink]);
  458. Terminal->ExceptionOnFail = true;
  459. try
  460. {
  461. try
  462. {
  463. Terminal->ReadSymlink(this, FLinkedFile);
  464. }
  465. __finally
  466. {
  467. Terminal->ExceptionOnFail = false;
  468. }
  469. }
  470. catch (Exception &E)
  471. {
  472. if (E.InheritsFrom(__classid(EFatal))) throw;
  473. else HandleExtendedException(&E, this);
  474. }
  475. }
  476. }
  477. //---------------------------------------------------------------------------
  478. AnsiString __fastcall TRemoteFile::GetListingStr()
  479. {
  480. return Format("%s%s %3s %s-8s %-8s %9d %s %s%s", ARRAYOFCONST((
  481. Type, Rights->Text, IntToStr(INodeBlocks), Owner,
  482. Group, IntToStr(Size), ModificationStr, FileName,
  483. (FLinkedFile ? AnsiString(SYMLINKSTR) + FLinkedFile->FileName : AnsiString()))));
  484. }
  485. //---------------------------------------------------------------------------
  486. AnsiString __fastcall TRemoteFile::GetFullFileName()
  487. {
  488. assert(Terminal);
  489. AnsiString Path;
  490. if (IsParentDirectory) Path = Directory->ParentPath;
  491. else
  492. if (IsDirectory) Path = UnixIncludeTrailingBackslash(Directory->FullDirectory + FileName);
  493. else Path = Directory->FullDirectory + FileName;
  494. return Terminal->TranslateLockedPath(Path, true);
  495. }
  496. //---------------------------------------------------------------------------
  497. Integer __fastcall TRemoteFile::GetAttr()
  498. {
  499. Integer Result = 0;
  500. if (Rights->ReadOnly) Result |= faReadOnly;
  501. if (IsHidden) Result |= faHidden;
  502. return Result;
  503. }
  504. //---------------------------------------------------------------------------
  505. void __fastcall TRemoteFile::SetTerminal(TTerminal * value)
  506. {
  507. FTerminal = value;
  508. if (FLinkedFile)
  509. {
  510. FLinkedFile->Terminal = value;
  511. }
  512. }
  513. //---------------------------------------------------------------------------
  514. //---------------------------------------------------------------------------
  515. __fastcall TRemoteParentDirectory::TRemoteParentDirectory() : TRemoteFile()
  516. {
  517. FileName = "..";
  518. Modification = Now();
  519. LastAccess = Modification;
  520. Type = 'D';
  521. Size = 0;
  522. }
  523. //=== TRemoteFileList ------------------------------------------------------
  524. __fastcall TRemoteFileList::TRemoteFileList():
  525. TObjectList()
  526. {
  527. }
  528. //---------------------------------------------------------------------------
  529. void __fastcall TRemoteFileList::AddFile(TRemoteFile * File)
  530. {
  531. Add(File);
  532. File->Directory = this;
  533. }
  534. //---------------------------------------------------------------------------
  535. void __fastcall TRemoteFileList::DuplicateTo(TRemoteFileList * Copy)
  536. {
  537. Copy->Clear();
  538. for (int Index = 0; Index < Count; Index++)
  539. {
  540. TRemoteFile * File = Files[Index];
  541. Copy->AddFile(File->Duplicate());
  542. }
  543. Copy->FDirectory = Directory;
  544. }
  545. //---------------------------------------------------------------------------
  546. void __fastcall TRemoteFileList::Clear()
  547. {
  548. TObjectList::Clear();
  549. }
  550. //---------------------------------------------------------------------------
  551. void __fastcall TRemoteFileList::SetDirectory(AnsiString value)
  552. {
  553. FDirectory = UnixExcludeTrailingBackslash(value);
  554. }
  555. //---------------------------------------------------------------------------
  556. AnsiString __fastcall TRemoteFileList::GetFullDirectory()
  557. {
  558. return UnixIncludeTrailingBackslash(Directory);
  559. }
  560. //---------------------------------------------------------------------------
  561. TRemoteFile * __fastcall TRemoteFileList::GetFiles(Integer Index)
  562. {
  563. return (TRemoteFile *)Items[Index];
  564. }
  565. //---------------------------------------------------------------------------
  566. Boolean __fastcall TRemoteFileList::GetIsRoot()
  567. {
  568. return (Directory == ROOTDIRECTORY);
  569. }
  570. //---------------------------------------------------------------------------
  571. AnsiString __fastcall TRemoteFileList::GetParentPath()
  572. {
  573. return UnixExtractFilePath(Directory);
  574. }
  575. //---------------------------------------------------------------------------
  576. __int64 __fastcall TRemoteFileList::GetTotalSize()
  577. {
  578. __int64 Result = 0;
  579. for (Integer Index = 0; Index < Count; Index++)
  580. if (!Files[Index]->IsDirectory) Result += Files[Index]->Size;
  581. return Result;
  582. }
  583. //---------------------------------------------------------------------------
  584. TRemoteFile * __fastcall TRemoteFileList::FindFile(const AnsiString &FileName)
  585. {
  586. for (Integer Index = 0; Index < Count; Index++)
  587. if (Files[Index]->FileName == FileName) return Files[Index];
  588. return NULL;
  589. }
  590. //=== TRemoteDirectory ------------------------------------------------------
  591. __fastcall TRemoteDirectory::TRemoteDirectory(TTerminal * aTerminal):
  592. TRemoteFileList(), FTerminal(aTerminal)
  593. {
  594. FSelectedFiles = NULL;
  595. FThisDirectory = NULL;
  596. FParentDirectory = NULL;
  597. FIncludeThisDirectory = false;
  598. FIncludeParentDirectory = true;
  599. }
  600. //---------------------------------------------------------------------------
  601. void __fastcall TRemoteDirectory::Clear()
  602. {
  603. if (ThisDirectory && !IncludeThisDirectory)
  604. {
  605. delete FThisDirectory;
  606. FThisDirectory = NULL;
  607. }
  608. if (ParentDirectory && !IncludeParentDirectory)
  609. {
  610. delete FParentDirectory;
  611. FParentDirectory = NULL;
  612. }
  613. TRemoteFileList::Clear();
  614. }
  615. //---------------------------------------------------------------------------
  616. void __fastcall TRemoteDirectory::SetDirectory(AnsiString value)
  617. {
  618. TRemoteFileList::SetDirectory(value);
  619. //Load();
  620. }
  621. //---------------------------------------------------------------------------
  622. void __fastcall TRemoteDirectory::AddFile(TRemoteFile * File)
  623. {
  624. if (File->IsThisDirectory) FThisDirectory = File;
  625. if (File->IsParentDirectory) FParentDirectory = File;
  626. if ((!File->IsThisDirectory || IncludeThisDirectory) &&
  627. (!File->IsParentDirectory || IncludeParentDirectory))
  628. {
  629. TRemoteFileList::AddFile(File);
  630. }
  631. File->Terminal = Terminal;
  632. }
  633. //---------------------------------------------------------------------------
  634. void __fastcall TRemoteDirectory::DuplicateTo(TRemoteFileList * Copy)
  635. {
  636. TRemoteFileList::DuplicateTo(Copy);
  637. if (ThisDirectory && !IncludeThisDirectory)
  638. {
  639. Copy->AddFile(ThisDirectory->Duplicate());
  640. }
  641. if (ParentDirectory && !IncludeParentDirectory)
  642. {
  643. Copy->AddFile(ParentDirectory->Duplicate());
  644. }
  645. }
  646. //---------------------------------------------------------------------------
  647. bool __fastcall TRemoteDirectory::GetLoaded()
  648. {
  649. return ((Terminal != NULL) && Terminal->Active && !Directory.IsEmpty());
  650. }
  651. //---------------------------------------------------------------------------
  652. TStrings * __fastcall TRemoteDirectory::GetSelectedFiles()
  653. {
  654. if (!FSelectedFiles)
  655. {
  656. FSelectedFiles = new TStringList();
  657. }
  658. else
  659. {
  660. FSelectedFiles->Clear();
  661. }
  662. for (int Index = 0; Index < Count; Index ++)
  663. {
  664. if (Files[Index]->Selected)
  665. {
  666. FSelectedFiles->Add(Files[Index]->FullFileName);
  667. }
  668. }
  669. return FSelectedFiles;
  670. }
  671. //---------------------------------------------------------------------------
  672. void __fastcall TRemoteDirectory::SetIncludeParentDirectory(Boolean value)
  673. {
  674. if (IncludeParentDirectory != value)
  675. {
  676. FIncludeParentDirectory = value;
  677. if (value && ParentDirectory)
  678. {
  679. assert(IndexOf(ParentDirectory) < 0);
  680. Add(ParentDirectory);
  681. }
  682. else if (!value && ParentDirectory)
  683. {
  684. assert(IndexOf(ParentDirectory) >= 0);
  685. Extract(ParentDirectory);
  686. }
  687. }
  688. }
  689. //---------------------------------------------------------------------------
  690. void __fastcall TRemoteDirectory::SetIncludeThisDirectory(Boolean value)
  691. {
  692. if (IncludeThisDirectory != value)
  693. {
  694. FIncludeThisDirectory = value;
  695. if (value && ThisDirectory)
  696. {
  697. assert(IndexOf(ThisDirectory) < 0);
  698. Add(ThisDirectory);
  699. }
  700. else if (!value && ThisDirectory)
  701. {
  702. assert(IndexOf(ThisDirectory) >= 0);
  703. Extract(ThisDirectory);
  704. }
  705. }
  706. }
  707. //===========================================================================
  708. __fastcall TRemoteDirectoryCache::TRemoteDirectoryCache(): TStringList()
  709. {
  710. Sorted = true;
  711. Duplicates = dupError;
  712. CaseSensitive = true;
  713. }
  714. //---------------------------------------------------------------------------
  715. __fastcall TRemoteDirectoryCache::~TRemoteDirectoryCache()
  716. {
  717. Clear();
  718. }
  719. //---------------------------------------------------------------------------
  720. void __fastcall TRemoteDirectoryCache::Clear()
  721. {
  722. try
  723. {
  724. for (int Index = 0; Index < Count; Index++)
  725. {
  726. delete (TRemoteFileList *)Objects[Index];
  727. Objects[Index] = NULL;
  728. }
  729. }
  730. __finally
  731. {
  732. TStringList::Clear();
  733. }
  734. }
  735. //---------------------------------------------------------------------------
  736. bool __fastcall TRemoteDirectoryCache::GetIsEmpty() const
  737. {
  738. return (const_cast<TRemoteDirectoryCache*>(this)->Count == 0);
  739. }
  740. //---------------------------------------------------------------------------
  741. TRemoteFileList * __fastcall TRemoteDirectoryCache::GetFileList(const AnsiString Directory)
  742. {
  743. int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));
  744. return (Index >= 0 ? (TRemoteFileList *)Objects[Index] : NULL);
  745. }
  746. //---------------------------------------------------------------------------
  747. void __fastcall TRemoteDirectoryCache::AddFileList(TRemoteFileList * FileList)
  748. {
  749. assert(FileList);
  750. TRemoteFileList * Copy = new TRemoteFileList();
  751. FileList->DuplicateTo(Copy);
  752. AddObject(Copy->Directory, Copy);
  753. }
  754. //---------------------------------------------------------------------------
  755. void __fastcall TRemoteDirectoryCache::ClearFileList(AnsiString Directory, bool SubDirs)
  756. {
  757. Directory = UnixExcludeTrailingBackslash(Directory);
  758. int Index = IndexOf(Directory);
  759. if (Index >= 0)
  760. {
  761. Delete(Index);
  762. }
  763. if (SubDirs)
  764. {
  765. Directory = UnixIncludeTrailingBackslash(Directory);
  766. Index = Count-1;
  767. while (Index >= 0)
  768. {
  769. if (Strings[Index].SubString(1, Directory.Length()) == Directory)
  770. {
  771. Delete(Index);
  772. }
  773. Index--;
  774. }
  775. }
  776. }
  777. //---------------------------------------------------------------------------
  778. void __fastcall TRemoteDirectoryCache::Delete(int Index)
  779. {
  780. delete (TRemoteFileList *)Objects[Index];
  781. TStringList::Delete(Index);
  782. }
  783. //---------------------------------------------------------------------------
  784. //---------------------------------------------------------------------------
  785. __fastcall TRemoteDirectoryChangesCache::TRemoteDirectoryChangesCache() :
  786. TStringList()
  787. {
  788. }
  789. //---------------------------------------------------------------------------
  790. void __fastcall TRemoteDirectoryChangesCache::Clear()
  791. {
  792. TStringList::Clear();
  793. }
  794. //---------------------------------------------------------------------------
  795. bool __fastcall TRemoteDirectoryChangesCache::GetIsEmpty() const
  796. {
  797. return (const_cast<TRemoteDirectoryChangesCache*>(this)->Count == 0);
  798. }
  799. //---------------------------------------------------------------------------
  800. void __fastcall TRemoteDirectoryChangesCache::AddDirectoryChange(
  801. const AnsiString SourceDir, const AnsiString Change,
  802. const AnsiString TargetDir)
  803. {
  804. assert(!TargetDir.IsEmpty());
  805. Values[TargetDir] = "//";
  806. if (TTerminal::ExpandFileName(Change, SourceDir) != TargetDir)
  807. {
  808. AnsiString Key;
  809. if (DirectoryChangeKey(SourceDir, Change, Key))
  810. {
  811. Values[Key] = TargetDir;
  812. }
  813. }
  814. }
  815. //---------------------------------------------------------------------------
  816. void __fastcall TRemoteDirectoryChangesCache::ClearDirectoryChange(
  817. AnsiString SourceDir)
  818. {
  819. for (int Index = 0; Index < Count; Index++)
  820. {
  821. if (Names[Index].SubString(1, SourceDir.Length()) == SourceDir)
  822. {
  823. Delete(Index);
  824. Index--;
  825. }
  826. }
  827. }
  828. //---------------------------------------------------------------------------
  829. bool __fastcall TRemoteDirectoryChangesCache::GetDirectoryChange(
  830. const AnsiString SourceDir, const AnsiString Change, AnsiString & TargetDir)
  831. {
  832. AnsiString Key;
  833. bool Result;
  834. Key = TTerminal::ExpandFileName(Change, SourceDir);
  835. Result = (IndexOfName(Key) >= 0);
  836. if (Result)
  837. {
  838. TargetDir = Key;
  839. }
  840. else
  841. {
  842. Result = DirectoryChangeKey(SourceDir, Change, Key);
  843. if (Result)
  844. {
  845. AnsiString Directory = Values[Key];
  846. Result = !Directory.IsEmpty();
  847. if (Result)
  848. {
  849. TargetDir = Directory;
  850. }
  851. }
  852. }
  853. return Result;
  854. }
  855. //---------------------------------------------------------------------------
  856. void __fastcall TRemoteDirectoryChangesCache::Serialize(AnsiString & Data)
  857. {
  858. Data = "A" + Text;
  859. }
  860. //---------------------------------------------------------------------------
  861. void __fastcall TRemoteDirectoryChangesCache::Deserialize(const AnsiString Data)
  862. {
  863. if (Data.IsEmpty())
  864. {
  865. Text = "";
  866. }
  867. else
  868. {
  869. Text = Data.c_str() + 1;
  870. }
  871. }
  872. //---------------------------------------------------------------------------
  873. bool __fastcall TRemoteDirectoryChangesCache::DirectoryChangeKey(
  874. const AnsiString SourceDir, const AnsiString Change, AnsiString & Key)
  875. {
  876. bool Result = !Change.IsEmpty();
  877. if (Result)
  878. {
  879. bool Absolute = TTerminal::IsAbsolutePath(Change);
  880. Result = !SourceDir.IsEmpty() || Absolute;
  881. if (Result)
  882. {
  883. Key = Absolute ? Change : SourceDir + "," + Change;
  884. }
  885. }
  886. return Result;
  887. }
  888. //=== TRights ---------------------------------------------------------------
  889. __fastcall TRights::TRights()
  890. {
  891. FAllowUndef = False;
  892. FText.SetLength(RightsFlagCount);
  893. Number = 0;
  894. }
  895. //---------------------------------------------------------------------------
  896. __fastcall TRights::TRights(Word aNumber)
  897. {
  898. FAllowUndef = False;
  899. FText.SetLength(RightsFlagCount);
  900. Number = aNumber;
  901. }
  902. //---------------------------------------------------------------------------
  903. __fastcall TRights::TRights(const TRights & Source)
  904. {
  905. Assign(&Source);
  906. }
  907. //---------------------------------------------------------------------------
  908. void __fastcall TRights::Assign(const TRights * Source)
  909. {
  910. AllowUndef = Source->AllowUndef;
  911. Text = Source->Text;
  912. }
  913. //---------------------------------------------------------------------------
  914. void __fastcall TRights::SetText(AnsiString value)
  915. {
  916. if (value != FText)
  917. {
  918. if ((value.Length() != RightsFlagCount) ||
  919. (!AllowUndef && value.Pos(UNDEFRIGHT)) ||
  920. value.Pos(" "))
  921. throw Exception(FmtLoadStr(RIGHTS_ERROR, ARRAYOFCONST((value))));
  922. FText = value;
  923. }
  924. }
  925. //---------------------------------------------------------------------------
  926. void __fastcall TRights::SetOctal(AnsiString value)
  927. {
  928. bool Correct = (value.Length() == 3);
  929. if (Correct)
  930. {
  931. for (int i = 1; i <= value.Length() && Correct; i++)
  932. {
  933. Correct = value[i] >= '0' && value[i] <= '7';
  934. }
  935. }
  936. if (!Correct)
  937. {
  938. throw Exception(FMTLOAD(INVALID_OCTAL_PERMISSIONS, (value)));
  939. }
  940. #pragma option push -w-sig
  941. Number =
  942. ((value[1] - '0') << 6) +
  943. ((value[2] - '0') << 3) +
  944. ((value[3] - '0') << 0);
  945. #pragma option pop
  946. }
  947. //---------------------------------------------------------------------------
  948. void __fastcall TRights::SetNumber(Word value)
  949. {
  950. for (int Index = 0; Index < RightsFlagCount; Index++)
  951. {
  952. Right[(TRightsFlag)Index] = (Boolean)((value & (1 << (RightsFlagCount - 1 - Index))) != 0);
  953. }
  954. }
  955. //---------------------------------------------------------------------------
  956. AnsiString __fastcall TRights::GetOctal() const
  957. {
  958. AnsiString Result;
  959. Word N = NumberSet; // used to be "Number"
  960. Result += (char)('0' + ((N & 0700) >> 6));
  961. Result += (char)('0' + ((N & 0070) >> 3));
  962. Result += (char)('0' + ((N & 0007) >> 0));
  963. return Result;
  964. }
  965. //---------------------------------------------------------------------------
  966. Word __fastcall TRights::CalcNumber(TRightState State, Boolean
  967. #ifdef _DEBUG
  968. AllowUndef
  969. #endif
  970. ) const
  971. {
  972. Word Result = 0;
  973. for (int Index = 0; Index < RightsFlagCount; Index++)
  974. {
  975. TRightState AState = RightUndef[(TRightsFlag)Index];
  976. assert(AllowUndef || (AState != rsUndef));
  977. if (AState == State) Result |= (Word)(1 << (RightsFlagCount - 1 - Index));
  978. }
  979. return Result;
  980. }
  981. //---------------------------------------------------------------------------
  982. Word __fastcall TRights::GetNumber() const
  983. {
  984. return CalcNumber(rsYes, False);
  985. }
  986. //---------------------------------------------------------------------------
  987. Word __fastcall TRights::GetNumberSet() const
  988. {
  989. return CalcNumber(rsYes, True);
  990. }
  991. //---------------------------------------------------------------------------
  992. Word __fastcall TRights::GetNumberUnset() const
  993. {
  994. return CalcNumber(rsNo, True);
  995. }
  996. //---------------------------------------------------------------------------
  997. AnsiString __fastcall TRights::GetText() const
  998. {
  999. return FText;
  1000. }
  1001. //---------------------------------------------------------------------------
  1002. void __fastcall TRights::SetAllowUndef(Boolean value)
  1003. {
  1004. if (FAllowUndef != value)
  1005. {
  1006. assert(!value || !FText.Pos(UNDEFRIGHT));
  1007. FAllowUndef = value;
  1008. }
  1009. }
  1010. //---------------------------------------------------------------------------
  1011. AnsiString __fastcall TRights::GetFullRights() const
  1012. {
  1013. return FULLRIGHTS;
  1014. }
  1015. //---------------------------------------------------------------------------
  1016. void __fastcall TRights::SetRight(TRightsFlag Flag, Boolean value)
  1017. {
  1018. RightUndef[Flag] = (value ? rsYes : rsNo);
  1019. }
  1020. //---------------------------------------------------------------------------
  1021. Boolean __fastcall TRights::GetRight(TRightsFlag Flag) const
  1022. {
  1023. TRightState State = RightUndef[Flag];
  1024. assert(State != rsUndef);
  1025. return (State == rsYes);
  1026. }
  1027. //---------------------------------------------------------------------------
  1028. void __fastcall TRights::SetRightUndef(TRightsFlag Flag, TRightState value)
  1029. {
  1030. if (value != RightUndef[Flag])
  1031. {
  1032. assert((value != rsUndef) || AllowUndef);
  1033. switch (value) {
  1034. case rsUndef: FText[Flag+1] = UNDEFRIGHT; break;
  1035. case rsNo: FText[Flag+1] = NORIGHT; break;
  1036. default: FText[Flag+1] = FullRights[Flag+1];
  1037. }
  1038. }
  1039. }
  1040. //---------------------------------------------------------------------------
  1041. TRightState __fastcall TRights::GetRightUndef(TRightsFlag Flag) const
  1042. {
  1043. Char FlagChar = Text[Flag+1];
  1044. if (FlagChar == NORIGHT) return rsNo;
  1045. else
  1046. if (FlagChar == FULLRIGHTS[Flag]) return rsYes;
  1047. else return rsUndef;
  1048. }
  1049. //---------------------------------------------------------------------------
  1050. TRights __fastcall TRights::operator |(const TRights & rhr) const
  1051. {
  1052. TRights Result = *this;
  1053. Result |= rhr;
  1054. return Result;
  1055. }
  1056. //---------------------------------------------------------------------------
  1057. TRights __fastcall TRights::operator |(Integer rhr) const
  1058. {
  1059. TRights Result = *this;
  1060. Result |= rhr;
  1061. return Result;
  1062. }
  1063. //---------------------------------------------------------------------------
  1064. TRights & __fastcall TRights::operator =(const TRights & rhr)
  1065. {
  1066. Assign(&rhr);
  1067. return *this;
  1068. }
  1069. //---------------------------------------------------------------------------
  1070. TRights & __fastcall TRights::operator =(Integer rhr)
  1071. {
  1072. Number = (Word)rhr;
  1073. return *this;
  1074. }
  1075. //---------------------------------------------------------------------------
  1076. TRights & __fastcall TRights::operator |=(const TRights & rhr)
  1077. {
  1078. Number |= rhr.Number;
  1079. return *this;
  1080. }
  1081. //---------------------------------------------------------------------------
  1082. TRights & __fastcall TRights::operator |=(Integer rhr)
  1083. {
  1084. Number |= (Word)rhr;
  1085. return *this;
  1086. }
  1087. //---------------------------------------------------------------------------
  1088. TRights __fastcall TRights::operator &(Integer rhr) const
  1089. {
  1090. TRights Result = *this;
  1091. Result &= rhr;
  1092. return Result;
  1093. }
  1094. //---------------------------------------------------------------------------
  1095. TRights __fastcall TRights::operator &(const TRights & rhr) const
  1096. {
  1097. TRights Result = *this;
  1098. Result &= rhr;
  1099. return Result;
  1100. }
  1101. //---------------------------------------------------------------------------
  1102. TRights & __fastcall TRights::operator &=(Integer rhr)
  1103. {
  1104. Number &= (Word)rhr;
  1105. return *this;
  1106. }
  1107. //---------------------------------------------------------------------------
  1108. TRights & __fastcall TRights::operator &=(const TRights & rhr)
  1109. {
  1110. if (AllowUndef)
  1111. {
  1112. for (int Index = 0; Index < RightsFlagCount; Index++)
  1113. if (RightUndef[(TRightsFlag)Index] != rhr.RightUndef[(TRightsFlag)Index])
  1114. RightUndef[(TRightsFlag)Index] = rsUndef;
  1115. }
  1116. else Number &= rhr.Number;
  1117. return *this;
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. TRights __fastcall TRights::operator ~() const
  1121. {
  1122. TRights Result;
  1123. Result.Number = (Word)~Number;
  1124. return Result;
  1125. }
  1126. //---------------------------------------------------------------------------
  1127. bool __fastcall TRights::operator ==(const TRights & rhr) const
  1128. {
  1129. if (AllowUndef || rhr.AllowUndef)
  1130. {
  1131. for (int Index = 0; Index < RightsFlagCount; Index++)
  1132. if (RightUndef[(TRightsFlag)Index] != rhr.RightUndef[(TRightsFlag)Index])
  1133. return False;
  1134. return True;
  1135. }
  1136. else return (bool)(Number == rhr.Number);
  1137. }
  1138. //---------------------------------------------------------------------------
  1139. bool __fastcall TRights::operator ==(Integer rhr) const
  1140. {
  1141. return (bool)(Number == rhr);
  1142. }
  1143. //---------------------------------------------------------------------------
  1144. bool __fastcall TRights::operator !=(const TRights & rhr) const
  1145. {
  1146. return !(*this == rhr);
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TRights::SetReadOnly(Boolean value)
  1150. {
  1151. Right[rfUserWrite] = !value;
  1152. Right[rfGroupWrite] = !value;
  1153. Right[rfOtherWrite] = !value;
  1154. }
  1155. //---------------------------------------------------------------------------
  1156. Boolean __fastcall TRights::GetReadOnly()
  1157. {
  1158. return (Boolean)((NumberUnset & raWrite) == raWrite);
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. AnsiString __fastcall TRights::GetSimplestStr() const
  1162. {
  1163. return IsUndef ? ModeStr : Octal;
  1164. }
  1165. //---------------------------------------------------------------------------
  1166. AnsiString __fastcall TRights::GetModeStr() const
  1167. {
  1168. AnsiString Result = "";
  1169. AnsiString SetModeStr, UnsetModeStr;
  1170. TRightsFlag Flag;
  1171. for (Integer Group = 0; Group < 3; Group++)
  1172. {
  1173. SetModeStr = "";
  1174. UnsetModeStr = "";
  1175. for (Integer Mode = 0; Mode < 3; Mode++)
  1176. {
  1177. Flag = (TRightsFlag)((Group * 3) + Mode);
  1178. switch (RightUndef[Flag]) {
  1179. case rsYes: SetModeStr += FULLRIGHTS[(int)Flag]; break;
  1180. case rsNo: UnsetModeStr += FULLRIGHTS[(int)Flag]; break;
  1181. }
  1182. }
  1183. if (!SetModeStr.IsEmpty() || !UnsetModeStr.IsEmpty())
  1184. {
  1185. if (!Result.IsEmpty()) Result += ',';
  1186. Result += MODEGROUPS[Group];
  1187. if (!SetModeStr.IsEmpty()) Result += "+" + SetModeStr;
  1188. if (!UnsetModeStr.IsEmpty()) Result += "-" + UnsetModeStr;
  1189. }
  1190. }
  1191. return Result;
  1192. }
  1193. //---------------------------------------------------------------------------
  1194. void __fastcall TRights::AddExecute()
  1195. {
  1196. for (int Index = 0; Index < 3; Index++)
  1197. {
  1198. if (NumberSet & (0700 >> (Index * 3)))
  1199. {
  1200. Right[(TRightsFlag)(rfUserExec + (Index * 3))] = true;
  1201. }
  1202. }
  1203. }
  1204. //---------------------------------------------------------------------------
  1205. void __fastcall TRights::AllUndef()
  1206. {
  1207. for (int Index = 0; Index < RightsFlagCount; Index++)
  1208. {
  1209. RightUndef[(TRightsFlag)Index] = rsUndef;
  1210. }
  1211. }
  1212. //---------------------------------------------------------------------------
  1213. Boolean __fastcall TRights::GetIsUndef() const
  1214. {
  1215. for (int Index = 0; Index < RightsFlagCount; Index++)
  1216. if (RightUndef[(TRightsFlag)Index] == rsUndef)
  1217. return True;
  1218. return False;
  1219. }
  1220. //---------------------------------------------------------------------------
  1221. __fastcall TRights::operator unsigned short() const
  1222. {
  1223. return Number;
  1224. }
  1225. //---------------------------------------------------------------------------
  1226. __fastcall TRights::operator unsigned long() const
  1227. {
  1228. return Number;
  1229. }
  1230. //=== TRemoteProperties -------------------------------------------------------
  1231. __fastcall TRemoteProperties::TRemoteProperties()
  1232. {
  1233. Valid.Clear();
  1234. AddXToDirectories = false;
  1235. Rights.AllowUndef = false;
  1236. Rights.Number = 0;
  1237. Group = "";
  1238. Owner = "";
  1239. Recursive = false;
  1240. };
  1241. /*//---------------------------------------------------------------------------
  1242. __fastcall TRemoteProperties::TRemoteProperties(const TRemoteProperties & Source)
  1243. {
  1244. *this = Source;
  1245. }
  1246. //---------------------------------------------------------------------------
  1247. void __fastcall TRemoteProperties::Clear()
  1248. {
  1249. };
  1250. //---------------------------------------------------------------------------
  1251. void __fastcall TRemoteProperties::operator =(const TRemoteProperties &rhp)
  1252. {
  1253. Valid = rhp.Valid;
  1254. Recursive = rhp.Recursive;
  1255. Rights = rhp.Rights;
  1256. AddXToDirectories = rhp.AddXToDirectories;
  1257. Group = rhp.Group;
  1258. Owner = rhp.Owner;
  1259. } */
  1260. //---------------------------------------------------------------------------
  1261. bool __fastcall TRemoteProperties::operator ==(const TRemoteProperties & rhp) const
  1262. {
  1263. bool Result = (Valid == rhp.Valid && Recursive == rhp.Recursive);
  1264. if (Result)
  1265. {
  1266. if ((Valid.Contains(vpRights) &&
  1267. (Rights != rhp.Rights || AddXToDirectories != rhp.AddXToDirectories)) ||
  1268. (Valid.Contains(vpOwner) && Owner != rhp.Owner) ||
  1269. (Valid.Contains(vpGroup) && Group != rhp.Group))
  1270. {
  1271. Result = false;
  1272. }
  1273. }
  1274. return Result;
  1275. }
  1276. //---------------------------------------------------------------------------
  1277. bool __fastcall TRemoteProperties::operator !=(const TRemoteProperties & rhp) const
  1278. {
  1279. return !(*this == rhp);
  1280. }
  1281. //---------------------------------------------------------------------------
  1282. TRemoteProperties __fastcall TRemoteProperties::CommonProperties(TStrings * FileList)
  1283. {
  1284. TRemoteProperties CommonProperties;
  1285. for (int Index = 0; Index < FileList->Count; Index++)
  1286. {
  1287. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  1288. assert(File);
  1289. if (!Index)
  1290. {
  1291. CommonProperties.Rights = *(File->Rights);
  1292. CommonProperties.Rights.AllowUndef = File->IsDirectory || File->Rights->IsUndef;
  1293. CommonProperties.Valid << vpRights;
  1294. if (!File->Owner.IsEmpty())
  1295. {
  1296. CommonProperties.Owner = File->Owner;
  1297. CommonProperties.Valid << vpOwner;
  1298. }
  1299. if (!File->Group.IsEmpty())
  1300. {
  1301. CommonProperties.Group = File->Group;
  1302. CommonProperties.Valid << vpGroup;
  1303. }
  1304. }
  1305. else
  1306. {
  1307. CommonProperties.Rights.AllowUndef = True;
  1308. CommonProperties.Rights &= *File->Rights;
  1309. if (CommonProperties.Owner != File->Owner)
  1310. {
  1311. CommonProperties.Owner = "";
  1312. CommonProperties.Valid >> vpOwner;
  1313. };
  1314. if (CommonProperties.Group != File->Group)
  1315. {
  1316. CommonProperties.Group = "";
  1317. CommonProperties.Valid >> vpGroup;
  1318. };
  1319. }
  1320. }
  1321. return CommonProperties;
  1322. }
  1323. //---------------------------------------------------------------------------
  1324. TRemoteProperties __fastcall TRemoteProperties::ChangedProperties(
  1325. const TRemoteProperties & OriginalProperties, TRemoteProperties NewProperties)
  1326. {
  1327. if (!NewProperties.Recursive)
  1328. {
  1329. if (NewProperties.Rights == OriginalProperties.Rights &&
  1330. !NewProperties.AddXToDirectories)
  1331. {
  1332. NewProperties.Valid >> vpRights;
  1333. }
  1334. if (NewProperties.Group == OriginalProperties.Group)
  1335. {
  1336. NewProperties.Valid >> vpGroup;
  1337. }
  1338. if (NewProperties.Owner == OriginalProperties.Owner)
  1339. {
  1340. NewProperties.Valid >> vpOwner;
  1341. }
  1342. }
  1343. return NewProperties;
  1344. }