RemoteFiles.cpp 74 KB

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