RemoteFiles.cpp 77 KB

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