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