RemoteFiles.cpp 71 KB

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