RemoteFiles.cpp 90 KB

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