RemoteFiles.cpp 102 KB

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