RemoteFiles.cpp 50 KB

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