RemoteFiles.cpp 89 KB

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