RemoteFiles.cpp 76 KB

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