RemoteFiles.cpp 90 KB

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