RemoteFiles.cpp 43 KB

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