RemoteFiles.cpp 71 KB

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