RemoteFiles.cpp 94 KB

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