RemoteFiles.cpp 91 KB

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