RemoteFiles.cpp 87 KB

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