RemoteFiles.cpp 90 KB

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