RemoteFiles.cpp 74 KB

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