RemoteFiles.cpp 77 KB

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