RemoteFiles.cpp 87 KB

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