RemoteFiles.cpp 95 KB

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