RemoteFiles.cpp 51 KB

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