RemoteFiles.cpp 78 KB

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