RemoteFiles.cpp 88 KB

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