RemoteFiles.cpp 94 KB

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