RemoteFiles.cpp 94 KB

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