RemoteFiles.cpp 94 KB

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