RemoteFiles.cpp 75 KB

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