RemoteFiles.cpp 94 KB

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