RemoteFiles.cpp 76 KB

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