RemoteFiles.cpp 94 KB

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