RemoteFiles.cpp 77 KB

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