| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437 | //---------------------------------------------------------------------------#include <CorePCH.h>#pragma hdrstop#include "RemoteFiles.h"#include "Terminal.h"#include "Cryptography.h"#include <System.Win.ComObj.hpp>/* TODO 1 : Path class instead of UnicodeString (handle relativity...) *///---------------------------------------------------------------------------const UnicodeString PartialExt(L".filepart");//---------------------------------------------------------------------------bool __fastcall IsUnixStyleWindowsPath(const UnicodeString & Path){  return (Path.Length() >= 3) && IsLetter(Path[1]) && (Path[2] == L':') && (Path[3] == L'/');}//---------------------------------------------------------------------------bool __fastcall UnixIsAbsolutePath(const UnicodeString & Path){  return    ((Path.Length() >= 1) && (Path[1] == L'/')) ||    // we need this for FTP only, but this is unfortunately used in a static context    IsUnixStyleWindowsPath(Path);}//---------------------------------------------------------------------------UnicodeString __fastcall UnixIncludeTrailingBackslash(const UnicodeString & Path){  // it used to return "/" when input path was empty  if (!Path.IsEmpty() && !Path.IsDelimiter(L"/", Path.Length()))  {    return Path + L"/";  }  else  {    return Path;  }}//---------------------------------------------------------------------------UnicodeString UniversalIncludeTrailingBackslash(bool Unix, const UnicodeString & Path){  return Unix ? UnixIncludeTrailingBackslash(Path) : IncludeTrailingBackslash(Path);}//---------------------------------------------------------------------------// Keeps "/" for root pathUnicodeString __fastcall UnixExcludeTrailingBackslash(const UnicodeString & Path, bool Simple){  if (Path.IsEmpty() ||      (Path == L"/") ||      !Path.IsDelimiter(L"/", Path.Length()) ||      (!Simple && ((Path.Length() == 3) && IsUnixStyleWindowsPath(Path))))  {    return Path;  }  else  {    return Path.SubString(1, Path.Length() - 1);  }}//---------------------------------------------------------------------------UnicodeString UniversalExcludeTrailingBackslash(bool Unix, const UnicodeString & Path){  return Unix ? UnixExcludeTrailingBackslash(Path) : ExcludeTrailingBackslash(Path);}//---------------------------------------------------------------------------UnicodeString __fastcall SimpleUnixExcludeTrailingBackslash(const UnicodeString & Path){  return UnixExcludeTrailingBackslash(Path, true);}//---------------------------------------------------------------------------UnicodeString UnixCombinePathsForce(const UnicodeString & Path1, const UnicodeString & Path2){  return UnixIncludeTrailingBackslash(Path1) + Path2;}//---------------------------------------------------------------------------UnicodeString __fastcall UnixCombinePaths(const UnicodeString & Path1, const UnicodeString & Path2){  DebugAssert(!Path2.IsEmpty());  return UnixCombinePathsForce(Path1, Path2);}//---------------------------------------------------------------------------UnicodeString UniversalCombinePaths(bool Unix, const UnicodeString & Path1, const UnicodeString & Path2){  return Unix ? UnixCombinePaths(Path1, Path2) : CombinePaths(Path1, Path2);}//---------------------------------------------------------------------------// Eventually make UnixCombinePaths do this,// once we verify that no use of UnixCombinePaths relies on it adding backslash even for empty second argUnicodeString UnixCombinePathsSmart(const UnicodeString & Path1, const UnicodeString & Path2){  if (Path2.IsEmpty())  {    return Path1;  }  else  {    return UnixCombinePaths(Path1, Path2);  }}//---------------------------------------------------------------------------Boolean __fastcall UnixSamePath(const UnicodeString & Path1, const UnicodeString & Path2){  return (UnixIncludeTrailingBackslash(Path1) == UnixIncludeTrailingBackslash(Path2));}//---------------------------------------------------------------------------bool __fastcall UnixIsChildPath(const UnicodeString & AParent, const UnicodeString & AChild){  UnicodeString Parent = UnixIncludeTrailingBackslash(AParent);  UnicodeString Child = UnixIncludeTrailingBackslash(AChild);  return (Child.SubString(1, Parent.Length()) == Parent);}//---------------------------------------------------------------------------UnicodeString __fastcall UnixExtractFileDir(const UnicodeString & Path){  int Pos = Path.LastDelimiter(L'/');  // it used to return Path when no slash was found  if (Pos > 1)  {    return Path.SubString(1, Pos - 1);  }  else  {    return (Pos == 1) ? UnicodeString(ROOTDIRECTORY) : UnicodeString();  }}//---------------------------------------------------------------------------// must return trailing backslashUnicodeString __fastcall UnixExtractFilePath(const UnicodeString & Path){  int Pos = Path.LastDelimiter(L'/');  // it used to return Path when no slash was found  if (Pos > 0)  {    return Path.SubString(1, Pos);  }  else  {    return UnicodeString();  }}//---------------------------------------------------------------------------UnicodeString __fastcall UnixExtractFileName(const UnicodeString & Path){  int Pos = Path.LastDelimiter(L'/');  UnicodeString Result;  if (Pos > 0)  {    Result = Path.SubString(Pos + 1, Path.Length() - Pos);  }  else  {    Result = Path;  }  return Result;}//---------------------------------------------------------------------------UnicodeString __fastcall UnixExtractFileExt(const UnicodeString & Path){  UnicodeString FileName = UnixExtractFileName(Path);  int Pos = FileName.LastDelimiter(L".");  return (Pos > 0) ? Path.SubString(Pos, Path.Length() - Pos + 1) : UnicodeString();}//---------------------------------------------------------------------------UnicodeString __fastcall ExtractFileName(const UnicodeString & Path, bool Unix){  if (Unix)  {    return UnixExtractFileName(Path);  }  else  {    return ExtractFileName(Path);  }}//---------------------------------------------------------------------------UnicodeString ExtractShortName(const UnicodeString & Path, bool Unix){  UnicodeString Result = ExtractFileName(Path, Unix);  if (Result.IsEmpty())  {    Result = Path;  }  return Result;}//---------------------------------------------------------------------------bool __fastcall ExtractCommonPath(TStrings * Files, UnicodeString & Path){  DebugAssert(Files->Count > 0);  Path = ExtractFilePath(Files->Strings[0]);  bool Result = !Path.IsEmpty();  if (Result)  {    for (int Index = 1; Index < Files->Count; Index++)    {      while (!Path.IsEmpty() &&        (Files->Strings[Index].SubString(1, Path.Length()) != Path))      {        int PrevLen = Path.Length();        Path = ExtractFilePath(ExcludeTrailingBackslash(Path));        if (Path.Length() == PrevLen)        {          Path = L"";          Result = false;        }      }    }  }  return Result;}//---------------------------------------------------------------------------static UnicodeString GetFileListItemPath(TStrings * Files, int Index){  UnicodeString Result;  if (Files->Objects[Index] != NULL)  {    Result = DebugNotNull(dynamic_cast<TRemoteFile *>(Files->Objects[Index]))->FullFileName;  }  else  {    Result = Files->Strings[Index];  }  return Result;}//---------------------------------------------------------------------------bool __fastcall UnixExtractCommonPath(TStrings * Files, UnicodeString & Path){  DebugAssert(Files->Count > 0);  Path = UnixExtractFilePath(GetFileListItemPath(Files, 0));  bool Result = !Path.IsEmpty();  if (Result)  {    for (int Index = 1; Index < Files->Count; Index++)    {      while (!Path.IsEmpty() &&        (GetFileListItemPath(Files, Index).SubString(1, Path.Length()) != Path))      {        int PrevLen = Path.Length();        Path = UnixExtractFilePath(UnixExcludeTrailingBackslash(Path));        if (Path.Length() == PrevLen)        {          Path = L"";          Result = false;        }      }    }  }  return Result;}//---------------------------------------------------------------------------bool __fastcall IsUnixRootPath(const UnicodeString & Path){  return Path.IsEmpty() || (Path == ROOTDIRECTORY);}//---------------------------------------------------------------------------bool __fastcall IsUnixHiddenFile(const UnicodeString & FileName){  return IsRealFile(FileName) && !FileName.IsEmpty() && (FileName[1] == L'.');}//---------------------------------------------------------------------------UnicodeString __fastcall AbsolutePath(const UnicodeString & Base, const UnicodeString & Path){  // There's a duplicate implementation in TTerminal::ExpandFileName()  UnicodeString Result;  if (Path.IsEmpty())  {    Result = Base;  }  else if (Path[1] == L'/')  {    Result = UnixExcludeTrailingBackslash(Path);  }  else  {    Result = UnixIncludeTrailingBackslash(      UnixIncludeTrailingBackslash(Base) + Path);    int P;    while ((P = Result.Pos(L"/../")) > 0)    {      // special case, "/../" => "/"      if (P == 1)      {        Result = L"/";      }      else      {        int P2 = Result.SubString(1, P-1).LastDelimiter(L"/");        DebugAssert(P2 > 0);        Result.Delete(P2, P - P2 + 3);      }    }    while ((P = Result.Pos(L"/./")) > 0)    {      Result.Delete(P, 2);    }    Result = UnixExcludeTrailingBackslash(Result);  }  return Result;}//---------------------------------------------------------------------------UnicodeString __fastcall FromUnixPath(const UnicodeString & Path){  return ReplaceStr(Path, L"/", L"\\");}//---------------------------------------------------------------------------UnicodeString __fastcall ToUnixPath(const UnicodeString & Path){  return ReplaceStr(Path, L"\\", L"/");}//---------------------------------------------------------------------------static void __fastcall CutFirstDirectory(UnicodeString & S, bool Unix){  bool Root;  int P;  UnicodeString Sep = Unix ? L"/" : L"\\";  if (S == Sep)  {    S = L"";  }  else  {    if (S[1] == Sep[1])    {      Root = true;      S.Delete(1, 1);    }    else    {      Root = false;    }    if (S[1] == L'.')    {      S.Delete(1, 4);    }    P = S.Pos(Sep[1]);    if (P)    {      S.Delete(1, P);      S = Ellipsis + Sep + S;    }    else    {      S = L"";    }    if (Root)    {      S = Sep + S;    }  }}//---------------------------------------------------------------------------UnicodeString __fastcall MinimizeName(const UnicodeString & FileName, int MaxLen, bool Unix){  UnicodeString Drive, Dir, Name, Result;  UnicodeString Sep = Unix ? L"/" : L"\\";  Result = FileName;  if (Unix)  {    int P = Result.LastDelimiter(L"/");    if (P)    {      Dir = Result.SubString(1, P);      Name = Result.SubString(P + 1, Result.Length() - P);    }    else    {      Dir = L"";      Name = Result;    }  }  else  {    Dir = ExtractFilePath(Result);    Name = ExtractFileName(Result);    if (Dir.Length() >= 2 && Dir[2] == L':')    {      Drive = Dir.SubString(1, 2);      Dir.Delete(1, 2);    }  }  while ((!Dir.IsEmpty() || !Drive.IsEmpty()) && (Result.Length() > MaxLen))  {    if (Dir == Sep + Ellipsis + Sep)    {      Dir = Ellipsis + Sep;    }    else if (Dir == L"")    {      Drive = L"";    }    else    {      CutFirstDirectory(Dir, Unix);    }    Result = Drive + Dir + Name;  }  if (Result.Length() > MaxLen)  {    Result = Result.SubString(1, MaxLen);  }  return Result;}//---------------------------------------------------------------------------UnicodeString __fastcall MakeFileList(TStrings * FileList){  UnicodeString Result;  for (int Index = 0; Index < FileList->Count; Index++)  {    UnicodeString FileName = FileList->Strings[Index];    // currently this is used for local file only, so no delimiting is done    AddToList(Result, AddQuotes(FileName), L" ");  }  return Result;}//---------------------------------------------------------------------------// copy from BaseUtils.pasTDateTime __fastcall ReduceDateTimePrecision(TDateTime DateTime,  TModificationFmt Precision){  if (Precision == mfNone)  {    DateTime = double(0);  }  else if (Precision != mfFull)  {    unsigned short Y, M, D, H, N, S, MS;    DecodeDate(DateTime, Y, M, D);    DecodeTime(DateTime, H, N, S, MS);    switch (Precision)    {      case mfYMDHM:      case mfMDHM:        S = 0;        MS = 0;        break;      case mfMDY:        H = 0;        N = 0;        S = 0;        MS = 0;        break;      default:        DebugFail();    }    DateTime = EncodeDateVerbose(Y, M, D) + EncodeTimeVerbose(H, N, S, MS);  }  return DateTime;}//---------------------------------------------------------------------------TModificationFmt __fastcall LessDateTimePrecision(  TModificationFmt Precision1, TModificationFmt Precision2){  return (Precision1 < Precision2) ? Precision1 : Precision2;}//---------------------------------------------------------------------------UnicodeString __fastcall UserModificationStr(TDateTime DateTime,  TModificationFmt Precision){  switch (Precision)  {    case mfNone:      return L"";    case mfMDY:      return FormatDateTime(L"ddddd", DateTime);    case mfYMDHM:    case mfMDHM:      return FormatDateTime(L"ddddd t", DateTime);    case mfFull:    default:      // Keep consistent with TDirView.GetDisplayInfo      return FormatDateTime(L"ddddd tt", DateTime);  }}//---------------------------------------------------------------------------UnicodeString __fastcall ModificationStr(TDateTime DateTime,  TModificationFmt Precision){  Word Year, Month, Day, Hour, Min, Sec, MSec;  DateTime.DecodeDate(&Year, &Month, &Day);  DateTime.DecodeTime(&Hour, &Min, &Sec, &MSec);  switch (Precision)  {    case mfNone:      return L"";    case mfMDY:      return FORMAT(L"%3s %2d %2d", (EngShortMonthNames[Month-1], Day, Year));    case mfMDHM:      return FORMAT(L"%3s %2d %2d:%2.2d",        (EngShortMonthNames[Month-1], Day, Hour, Min));    case mfYMDHM:      return FORMAT(L"%3s %2d %2d:%2.2d %4d",        (EngShortMonthNames[Month-1], Day, Hour, Min, Year));    default:      DebugFail();      // fall thru    case mfFull:      return FORMAT(L"%3s %2d %2d:%2.2d:%2.2d %4d",        (EngShortMonthNames[Month-1], Day, Hour, Min, Sec, Year));  }}//---------------------------------------------------------------------------int GetPartialFileExtLen(const UnicodeString & FileName){  int Result = 0;  if (EndsText(PartialExt, FileName))  {    Result = PartialExt.Length();  }  else  {    int P = FileName.LastDelimiter(L".");    if ((P > 0) && (P < FileName.Length()))    {      if (IsNumber(MidStr(FileName, P + 1)) &&          EndsText(PartialExt, FileName.SubString(1, P - 1)))      {        Result = PartialExt.Length() + (FileName.Length() - P + 1);      }    }  }  return Result;}//---------------------------------------------------------------------------int __fastcall FakeFileImageIndex(UnicodeString FileName, unsigned long Attrs,  UnicodeString * TypeName){  Attrs |= FILE_ATTRIBUTE_NORMAL;  TSHFileInfoW SHFileInfo;  // On Win2k we get icon of "ZIP drive" for ".." (parent directory)  if ((FileName == PARENTDIRECTORY) ||      ((FileName.Length() == 2) && (FileName[2] == L':') && IsLetter(FileName[1])) ||      IsReservedName(FileName))  {    FileName = L"dumb";  }  // this should be somewhere else, probably in TUnixDirView,  // as the "partial" overlay is added there too  int PartialFileExtLen = GetPartialFileExtLen(FileName);  if (GetPartialFileExtLen(FileName) > 0)  {    FileName.SetLength(FileName.Length() - PartialFileExtLen);  }  int Icon;  if (SHGetFileInfo(FileName.c_str(),        Attrs, &SHFileInfo, sizeof(SHFileInfo),        SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES | SHGFI_TYPENAME) != 0)  {    if (TypeName != NULL)    {      *TypeName = SHFileInfo.szTypeName;    }    Icon = SHFileInfo.iIcon;  }  else  {    if (TypeName != NULL)    {      *TypeName = L"";    }    Icon = -1;  }  return Icon;}//---------------------------------------------------------------------------bool __fastcall SameUserName(const UnicodeString & UserName1, const UnicodeString & UserName2){  // Bitvise reports file owner as "user@host", but we login with "user" only.  UnicodeString AUserName1 = CopyToChar(UserName1, L'@', true);  UnicodeString AUserName2 = CopyToChar(UserName2, L'@', true);  return SameText(AUserName1, AUserName2);}//---------------------------------------------------------------------------UnicodeString __fastcall FormatMultiFilesToOneConfirmation(const UnicodeString & Target, bool Unix){  UnicodeString Dir;  UnicodeString Name;  UnicodeString Path;  if (Unix)  {    Dir = UnixExtractFileDir(Target);    Name = UnixExtractFileName(Target);    Path = UnixIncludeTrailingBackslash(Target);  }  else  {    Dir = ExtractFilePath(Target);    Name = ExtractFileName(Target);    Path = IncludeTrailingBackslash(Target);  }  return FMTLOAD(MULTI_FILES_TO_ONE, (Name, Dir, Path));}//---------------------------------------------------------------------------//---------------------------------------------------------------------------__fastcall TRemoteToken::TRemoteToken() :  FID(0),  FIDValid(false){}//---------------------------------------------------------------------------__fastcall TRemoteToken::TRemoteToken(const UnicodeString & Name) :  FName(Name),  FID(0),  FIDValid(false){}//---------------------------------------------------------------------------void __fastcall TRemoteToken::Clear(){  FID = 0;  FIDValid = false;}//---------------------------------------------------------------------------bool __fastcall TRemoteToken::operator ==(const TRemoteToken & rht) const{  return    (FName == rht.FName) &&    (FIDValid == rht.FIDValid) &&    (!FIDValid || (FID == rht.FID));}//---------------------------------------------------------------------------bool __fastcall TRemoteToken::operator !=(const TRemoteToken & rht) const{  return !(*this == rht);}//---------------------------------------------------------------------------TRemoteToken & __fastcall TRemoteToken::operator =(const TRemoteToken & rht){  if (this != &rht)  {    FName = rht.FName;    FIDValid = rht.FIDValid;    FID = rht.FID;  }  return *this;}//---------------------------------------------------------------------------int __fastcall TRemoteToken::Compare(const TRemoteToken & rht) const{  int Result;  if (!FName.IsEmpty())  {    if (!rht.FName.IsEmpty())    {      Result = AnsiCompareText(FName, rht.FName);    }    else    {      Result = -1;    }  }  else  {    if (!rht.FName.IsEmpty())    {      Result = 1;    }    else    {      if (FIDValid)      {        if (rht.FIDValid)        {          Result = (FID < rht.FID) ? -1 : ((FID > rht.FID) ? 1 : 0);        }        else        {          Result = -1;        }      }      else      {        if (rht.FIDValid)        {          Result = 1;        }        else        {          Result = 0;        }      }    }  }  return Result;}//---------------------------------------------------------------------------void __fastcall TRemoteToken::SetID(unsigned int value){  FID = value;  FIDValid = true;}//---------------------------------------------------------------------------bool __fastcall TRemoteToken::GetNameValid() const{  return !FName.IsEmpty();}//---------------------------------------------------------------------------bool __fastcall TRemoteToken::GetIsSet() const{  return !FName.IsEmpty() || FIDValid;}//---------------------------------------------------------------------------UnicodeString __fastcall TRemoteToken::GetDisplayText() const{  if (!FName.IsEmpty())  {    return FName;  }  else if (FIDValid)  {    return IntToStr(int(FID));  }  else  {    return UnicodeString();  }}//---------------------------------------------------------------------------UnicodeString __fastcall TRemoteToken::GetLogText() const{  return FORMAT(L"\"%s\" [%d]", (FName, int(FID)));}//---------------------------------------------------------------------------//---------------------------------------------------------------------------TRemoteTokenList * __fastcall TRemoteTokenList::Duplicate() const{  TRemoteTokenList * Result = new TRemoteTokenList();  try  {    TTokens::const_iterator I = FTokens.begin();    while (I != FTokens.end())    {      Result->Add(*I);      ++I;    }  }  catch(...)  {    delete Result;    throw;  }  return Result;}//---------------------------------------------------------------------------void __fastcall TRemoteTokenList::Clear(){  FTokens.clear();  FNameMap.clear();  FIDMap.clear();}//---------------------------------------------------------------------------void __fastcall TRemoteTokenList::Add(const TRemoteToken & Token){  FTokens.push_back(Token);  if (Token.IDValid)  {    FIDMap.insert(TIDMap::value_type(Token.ID, FTokens.size() - 1));  }  if (Token.NameValid)  {    FNameMap.insert(TNameMap::value_type(Token.Name, FTokens.size() - 1));  }}//---------------------------------------------------------------------------void __fastcall TRemoteTokenList::AddUnique(const TRemoteToken & Token){  if (Token.IDValid)  {    TIDMap::const_iterator I = FIDMap.find(Token.ID);    if (I != FIDMap.end())    {      // is present already.      // may have different name (should not),      // but what can we do about it anyway?    }    else    {      Add(Token);    }  }  else if (Token.NameValid)  {    TNameMap::const_iterator I = FNameMap.find(Token.Name);    if (I != FNameMap.end())    {      // is present already.    }    else    {      Add(Token);    }  }  else  {    // can happen, e.g. with winsshd/SFTP  }}//---------------------------------------------------------------------------bool __fastcall TRemoteTokenList::Exists(const UnicodeString & Name) const{  // We should make use of SameUserName  return (FNameMap.find(Name) != FNameMap.end());}//---------------------------------------------------------------------------const TRemoteToken * TRemoteTokenList::Find(unsigned int ID) const{  TIDMap::const_iterator I = FIDMap.find(ID);  const TRemoteToken * Result;  if (I != FIDMap.end())  {    Result = &FTokens[(*I).second];  }  else  {    Result = NULL;  }  return Result;}//---------------------------------------------------------------------------const TRemoteToken * TRemoteTokenList::Find(const UnicodeString & Name) const{  TNameMap::const_iterator I = FNameMap.find(Name);  const TRemoteToken * Result;  if (I != FNameMap.end())  {    Result = &FTokens[(*I).second];  }  else  {    Result = NULL;  }  return Result;}//---------------------------------------------------------------------------void __fastcall TRemoteTokenList::Log(TTerminal * Terminal, const wchar_t * Title){  if (!FTokens.empty())  {    Terminal->LogEvent(FORMAT(L"Following %s found:", (Title)));    for (size_t Index = 0; Index < FTokens.size(); Index++)    {      Terminal->LogEvent(UnicodeString(L"  ") + FTokens[Index].LogText);    }  }  else  {    Terminal->LogEvent(FORMAT(L"No %s found.", (Title)));  }}//---------------------------------------------------------------------------int __fastcall TRemoteTokenList::Count() const{  return (int)FTokens.size();}//---------------------------------------------------------------------------const TRemoteToken * __fastcall TRemoteTokenList::Token(int Index) const{  return &FTokens[Index];}//---------------------------------------------------------------------------//---------------------------------------------------------------------------__fastcall TRemoteFile::TRemoteFile(TRemoteFile * ALinkedByFile):  TPersistent(){  FLinkedFile = NULL;  FRights = new TRights();  FIconIndex = -1;  FCyclicLink = false;  FModificationFmt = mfFull;  FLinkedByFile = ALinkedByFile;  FTerminal = NULL;  FDirectory = NULL;  FIsHidden = -1;  FIsEncrypted = false;  FCalculatedSize = -1;}//---------------------------------------------------------------------------__fastcall TRemoteFile::~TRemoteFile(){  delete FRights;  delete FLinkedFile;}//---------------------------------------------------------------------------TRemoteFile * __fastcall TRemoteFile::Duplicate(bool Standalone) const{  TRemoteFile * Result;  Result = new TRemoteFile();  try  {    if (FLinkedFile)    {      Result->FLinkedFile = FLinkedFile->Duplicate(true);      Result->FLinkedFile->FLinkedByFile = Result;    }    *Result->Rights = *FRights;    #define COPY_FP(PROP) Result->F ## PROP = F ## PROP;    COPY_FP(Terminal);    COPY_FP(Owner);    COPY_FP(ModificationFmt);    COPY_FP(Size);    COPY_FP(CalculatedSize);    COPY_FP(FileName);    COPY_FP(DisplayName);    COPY_FP(INodeBlocks);    COPY_FP(Modification);    COPY_FP(LastAccess);    COPY_FP(Group);    COPY_FP(IconIndex);    COPY_FP(TypeName);    COPY_FP(IsSymLink);    COPY_FP(LinkTo);    COPY_FP(Type);    COPY_FP(Tags);    COPY_FP(CyclicLink);    COPY_FP(HumanRights);    COPY_FP(IsEncrypted);    #undef COPY_FP    if (Standalone && (!FFullFileName.IsEmpty() || (Directory != NULL)))    {      Result->FFullFileName = FullFileName;    }  }  catch(...)  {    delete Result;    throw;  }  return Result;}//---------------------------------------------------------------------------void __fastcall TRemoteFile::LoadTypeInfo(){  /* TODO : If file is link: Should be attributes taken from linked file? */  unsigned long Attrs = 0;  if (IsDirectory) Attrs |= FILE_ATTRIBUTE_DIRECTORY;  if (IsHidden) Attrs |= FILE_ATTRIBUTE_HIDDEN;  UnicodeString DumbFileName = (IsSymLink && !LinkTo.IsEmpty() ? LinkTo : FileName);  FIconIndex = FakeFileImageIndex(DumbFileName, Attrs, &FTypeName);}//---------------------------------------------------------------------------__int64 __fastcall TRemoteFile::GetSize() const{  return IsDirectory ? 0 : FSize;}//---------------------------------------------------------------------------Integer __fastcall TRemoteFile::GetIconIndex() const{  if (FIconIndex == -1)  {    const_cast<TRemoteFile *>(this)->LoadTypeInfo();  }  return FIconIndex;}//---------------------------------------------------------------------------UnicodeString __fastcall TRemoteFile::GetTypeName(){  // check availability of type info by icon index, because type name can be empty  if (FIconIndex < 0)  {    LoadTypeInfo();  }  return FTypeName;}//---------------------------------------------------------------------------Boolean __fastcall TRemoteFile::GetIsHidden() const{  bool Result;  switch (FIsHidden)  {    case 0:      Result = false;      break;    case 1:      Result = true;      break;    default:      Result = IsUnixHiddenFile(FileName);      break;  }  return Result;}//---------------------------------------------------------------------------void __fastcall TRemoteFile::SetIsHidden(bool value){  FIsHidden = value ? 1 : 0;}//---------------------------------------------------------------------------Boolean __fastcall TRemoteFile::GetIsDirectory() const{  if (IsSymLink && (FLinkedFile != NULL))  {    return FLinkedFile->IsDirectory;  }  else  {    return (towupper(Type) == FILETYPE_DIRECTORY);  }}//---------------------------------------------------------------------------Boolean __fastcall TRemoteFile::GetIsParentDirectory() const{  return (FileName == PARENTDIRECTORY);}//---------------------------------------------------------------------------Boolean __fastcall TRemoteFile::GetIsThisDirectory() const{  return (FileName == THISDIRECTORY);}//---------------------------------------------------------------------------Boolean __fastcall TRemoteFile::GetIsInaccessibleDirectory() const{  Boolean Result;  if (IsDirectory)  {    DebugAssert(Terminal);    Result = !       (SameUserName(Terminal->UserName, L"root") ||        ((Rights->RightUndef[TRights::rrOtherExec] != TRights::rsNo)) ||        ((Rights->Right[TRights::rrGroupExec] != TRights::rsNo) &&         Terminal->Membership->Exists(Group.Name)) ||        ((Rights->Right[TRights::rrUserExec] != TRights::rsNo) &&         SameUserName(Terminal->UserName, Owner.Name)));  }    else Result = False;  return Result;}//---------------------------------------------------------------------------wchar_t __fastcall TRemoteFile::GetType() const{  return FType;}//---------------------------------------------------------------------------void __fastcall TRemoteFile::SetType(wchar_t AType){  FType = AType;  FIsSymLink = ((wchar_t)towupper(FType) == FILETYPE_SYMLINK);}//---------------------------------------------------------------------------const TRemoteFile * __fastcall TRemoteFile::GetLinkedFile() const{  // do not call FindLinkedFile as it would be called repeatedly for broken symlinks  return FLinkedFile;}//---------------------------------------------------------------------------bool __fastcall TRemoteFile::GetBrokenLink(){  DebugAssert(Terminal);  // If file is symlink but we couldn't find linked file we assume broken link  return (IsSymLink && (FCyclicLink || !FLinkedFile) &&    Terminal->ResolvingSymlinks);  // "!FLinkTo.IsEmpty()" removed because it does not work with SFTP}//---------------------------------------------------------------------------bool __fastcall TRemoteFile::IsTimeShiftingApplicable(){  return IsTimeShiftingApplicable(ModificationFmt);}//---------------------------------------------------------------------------bool __fastcall TRemoteFile::IsTimeShiftingApplicable(TModificationFmt ModificationFmt){  return (ModificationFmt == mfMDHM) || (ModificationFmt == mfYMDHM) || (ModificationFmt == mfFull);}//---------------------------------------------------------------------------void __fastcall TRemoteFile::ShiftTimeInSeconds(__int64 Seconds){  ShiftTimeInSeconds(FModification, ModificationFmt, Seconds);  ShiftTimeInSeconds(FLastAccess, ModificationFmt, Seconds);}//---------------------------------------------------------------------------void __fastcall TRemoteFile::ShiftTimeInSeconds(TDateTime & DateTime, TModificationFmt ModificationFmt, __int64 Seconds){  if ((Seconds != 0) && IsTimeShiftingApplicable(ModificationFmt))  {    DebugAssert(int(DateTime) != 0);    DateTime = IncSecond(DateTime, Seconds);  }}//---------------------------------------------------------------------------void __fastcall TRemoteFile::SetModification(const TDateTime & value){  if (FModification != value)  {    FModificationFmt = mfFull;    FModification = value;  }}//---------------------------------------------------------------------------UnicodeString __fastcall TRemoteFile::GetUserModificationStr(){  return ::UserModificationStr(Modification, FModificationFmt);}//---------------------------------------------------------------------------UnicodeString __fastcall TRemoteFile::GetModificationStr(){  return ::ModificationStr(Modification, FModificationFmt);}//---------------------------------------------------------------------------UnicodeString __fastcall TRemoteFile::GetExtension(){  return UnixExtractFileExt(FFileName);}//---------------------------------------------------------------------------void __fastcall TRemoteFile::SetRights(TRights * value){  FRights->Assign(value);}//---------------------------------------------------------------------------UnicodeString __fastcall TRemoteFile::GetRightsStr(){  // note that HumanRights is typically an empty string  // (with an exception of Perm-fact-only MLSD FTP listing)  return FRights->Unknown ? HumanRights : FRights->Text;}//---------------------------------------------------------------------------void __fastcall TRemoteFile::SetListingStr(UnicodeString value){  // Value stored in 'value' can be used for error message  UnicodeString Line = value;  FIconIndex = -1;  try  {    UnicodeString Col;    // Do we need to do this (is ever TAB is LS output)?    Line = ReplaceChar(Line, L'\t', L' ');    Type = Line[1];    Line.Delete(1, 1);    #define GETNCOL  \      { if (Line.IsEmpty()) throw Exception(L""); \        Integer P = Line.Pos(L' '); \        if (P) { Col = Line.SubString(1, P-1); Line.Delete(1, P); } \          else { Col = Line; Line = L""; } \      }    #define GETCOL { GETNCOL; Line = TrimLeft(Line); }    // Rights string may contain special permission attributes (S,t, ...)    // (TODO: maybe no longer necessary, once we can handle the special permissions)    Rights->AllowUndef = True;    // On some system there is no space between permissions and node blocks count columns    // so we get only first 9 characters and trim all following spaces (if any)    Rights->Text = Line.SubString(1, 9);    Line.Delete(1, 9);    // Rights column maybe followed by '+', '@' or '.' signs, we ignore them    // (On MacOS, there may be a space in between)    if (!Line.IsEmpty() && ((Line[1] == L'+') || (Line[1] == L'@') || (Line[1] == L'.')))    {      Line.Delete(1, 1);    }    else if ((Line.Length() >= 2) && (Line[1] == L' ') &&             ((Line[2] == L'+') || (Line[2] == L'@') || (Line[2] == L'.')))    {      Line.Delete(1, 2);    }    Line = Line.TrimLeft();    GETCOL;    if (!TryStrToInt(Col, FINodeBlocks))    {      // if the column is not an integer, suppose it's owner      // (Android BusyBox)      FINodeBlocks = 0;    }    else    {      GETCOL;    }    FOwner.Name = Col;    // #60 17.10.01: group name can contain space    FGroup.Name = L"";    GETCOL;    __int64 ASize;    do    {      FGroup.Name = FGroup.Name + Col;      GETCOL;      // SSH FS link like      // d????????? ? ? ? ? ? name      if ((FGroup.Name == L"?") && (Col == L"?"))      {        ASize = 0;      }      else      {        DebugAssert(!Col.IsEmpty());        // for devices etc.. there is additional column ending by comma, we ignore it        if (Col[Col.Length()] == L',') GETCOL;        ASize = StrToInt64Def(Col, -1);        // if it's not a number (file size) we take it as part of group name        // (at least on CygWin, there can be group with space in its name)        if (ASize < 0) Col = L" " + Col;      }    }    while (ASize < 0);    // Do not read modification time and filename (test close to the end of this block) if it is already set.    if (double(FModification) == 0)    {      bool FullTime = false;      bool DayMonthFormat = false;      Word Day, Month, Year, Hour, Min, Sec, P;      GETCOL;      // SSH FS link, see above      if (Col == L"?")      {        GETCOL;        FModificationFmt = mfNone;        FModification = 0;        FLastAccess = 0;      }      else      {        // format dd mmm or mmm dd ?        Day = (Word)StrToIntDef(Col, 0);        if (Day > 0)        {          DayMonthFormat = true;          GETCOL;        }        Month = 0;        #define COL2MONTH \          for (Word IMonth = 0; IMonth < 12; IMonth++) \            if (!Col.CompareIC(EngShortMonthNames[IMonth])) { Month = IMonth; Month++; break; }        COL2MONTH;        // if the column is not known month name, it may have been "yyyy-mm-dd"        // for --full-time format        if ((Month == 0) && (Col.Length() == 10) && (Col[5] == L'-') && (Col[8] == L'-'))        {          Year = (Word)Col.SubString(1, 4).ToInt();          Month = (Word)Col.SubString(6, 2).ToInt();          Day = (Word)Col.SubString(9, 2).ToInt();          GETCOL;          Hour = (Word)Col.SubString(1, 2).ToInt();          Min = (Word)Col.SubString(4, 2).ToInt();          if (Col.Length() >= 8)          {            Sec = (Word)StrToInt(Col.SubString(7, 2));          }          else          {            Sec = 0;          }          FModificationFmt = mfFull;          // skip TZ (TODO)          // do not trim leading space of filename          GETNCOL;        }        else        {          // or it may have been day name for another format of --full-time          if (Month == 0)          {            GETCOL;            COL2MONTH;            // neither standard, not --full-time format            if (Month == 0)            {              Abort();            }            else            {              FullTime = true;            }          }          #undef COL2MONTH          if (Day == 0)          {            GETNCOL;            Day = (Word)StrToInt(Col);          }          if ((Day < 1) || (Day > 31)) Abort();          // second full-time format          // ddd mmm dd hh:nn:ss yyyy          if (FullTime)          {            GETCOL;            if (Col.Length() != 8)            {              Abort();            }            Hour = (Word)StrToInt(Col.SubString(1, 2));            Min = (Word)StrToInt(Col.SubString(4, 2));            Sec = (Word)StrToInt(Col.SubString(7, 2));            FModificationFmt = mfFull;            // do not trim leading space of filename            GETNCOL;            Year = (Word)StrToInt(Col);          }          else          {            // for format dd mmm the below description seems not to be true,            // the year is not aligned to 5 characters            if (DayMonthFormat)            {              GETCOL;            }            else            {              // Time/Year indicator is always 5 characters long (???), on most              // systems year is aligned to right (_YYYY), but on some to left (YYYY_),              // we must ensure that trailing space is also deleted, so real              // separator space is not treated as part of file name              Col = Line.SubString(1, 6).Trim();              Line.Delete(1, 6);            }            // GETNCOL; // We don't want to trim input strings (name with space at beginning???)            // Check if we got time (contains :) or year            if ((P = (Word)Col.Pos(L':')) > 0)            {              Word CurrMonth, CurrDay;              Hour = (Word)StrToInt(Col.SubString(1, P-1));              Min = (Word)StrToInt(Col.SubString(P+1, Col.Length() - P));              if (Hour > 23 || Min > 59) Abort();              // When we don't got year, we assume current year              // with exception that the date would be in future              // in this case we assume last year.              DecodeDate(Date(), Year, CurrMonth, CurrDay);              if ((Month > CurrMonth) ||                  (Month == CurrMonth && Day > CurrDay)) Year--;              Sec = 0;              FModificationFmt = mfMDHM;            }              else            {              Year = (Word)StrToInt(Col);              if (Year > 10000) Abort();              // When we didn't get time we assume midnight              Hour = 0; Min = 0; Sec = 0;              FModificationFmt = mfMDY;            }          }        }        FModification = EncodeDateVerbose(Year, Month, Day) + EncodeTimeVerbose(Hour, Min, Sec, 0);        // adjust only when time is known,        // adjusting default "midnight" time makes no sense        if ((FModificationFmt == mfMDHM) ||             DebugAlwaysFalse(FModificationFmt == mfYMDHM) ||             (FModificationFmt == mfFull))        {          DebugAssert(Terminal != NULL);          FModification = AdjustDateTimeFromUnix(FModification,            Terminal->SessionData->DSTMode);        }        if (double(FLastAccess) == 0)        {          FLastAccess = FModification;        }      }      // separating space is already deleted, other spaces are treated as part of name      // see comment at the beginning of the block      if (FileName.IsEmpty())      {        FSize = ASize;        int P;        FLinkTo = L"";        if (IsSymLink)        {          P = Line.Pos(SYMLINKSTR);          if (P)          {            FLinkTo = Line.SubString(              P + wcslen(SYMLINKSTR), Line.Length() - P + wcslen(SYMLINKSTR) + 1);            Line.SetLength(P - 1);          }          else          {            Abort();          }        }        FFileName = UnixExtractFileName(Line);      }    }    #undef GETNCOL    #undef GETCOL  }  catch (Exception &E)  {    throw ETerminal(&E, FmtLoadStr(LIST_LINE_ERROR, ARRAYOFCONST((value))), HELP_LIST_LINE_ERROR);  }}//---------------------------------------------------------------------------void __fastcall TRemoteFile::Complete(){  DebugAssert(Terminal != NULL);  if (IsSymLink && Terminal->ResolvingSymlinks)  {    FindLinkedFile();  }}//---------------------------------------------------------------------------void __fastcall TRemoteFile::SetEncrypted(){  FIsEncrypted = true;  if (Size > TEncryption::GetOverhead())  {    Size -= TEncryption::GetOverhead();  }}//---------------------------------------------------------------------------void __fastcall TRemoteFile::FindLinkedFile(){  DebugAssert(Terminal && IsSymLink);  if (FLinkedFile) delete FLinkedFile;  FLinkedFile = NULL;  FCyclicLink = false;  if (!LinkTo.IsEmpty())  {    // check for cyclic link    TRemoteFile * LinkedBy = FLinkedByFile;    while (LinkedBy)    {      if (LinkedBy->LinkTo == LinkTo)      {        // this is currently redundant information, because it is used only to        // detect broken symlink, which would be otherwise detected        // by FLinkedFile == NULL        FCyclicLink = true;        break;      }      LinkedBy = LinkedBy->FLinkedByFile;    }  }  if (FCyclicLink)  {    TRemoteFile * LinkedBy = FLinkedByFile;    while (LinkedBy)    {      LinkedBy->FCyclicLink = true;      LinkedBy = LinkedBy->FLinkedByFile;    }  }  else  {    DebugAssert(Terminal->ResolvingSymlinks);    Terminal->ExceptionOnFail = true;    try    {      try      {        Terminal->ReadSymlink(this, FLinkedFile);      }      __finally      {        Terminal->ExceptionOnFail = false;      }    }    catch (Exception &E)    {      if (E.InheritsFrom(__classid(EFatal))) throw;      else      {        Terminal->Log->AddException(&E);      }    }  }}//---------------------------------------------------------------------------const TRemoteFile * __fastcall TRemoteFile::Resolve() const{  const TRemoteFile * Result = this;  while (Result->LinkedFile != NULL)  {    Result = Result->LinkedFile;  }  return Result;}//---------------------------------------------------------------------------UnicodeString __fastcall TRemoteFile::GetListingStr(){  // note that ModificationStr is longer than 12 for mfFull  UnicodeString LinkPart;  // expanded from ?: to avoid memory leaks  if (IsSymLink)  {    LinkPart = UnicodeString(SYMLINKSTR) + LinkTo;  }  return Format(L"%s%s %3s %-8s %-8s %9s %-12s %s%s", ARRAYOFCONST((    Type, Rights->Text, IntToStr(INodeBlocks), Owner.Name, Group.Name,    IntToStr(FSize), // explicitly using size even for directories    ModificationStr, FileName,    LinkPart)));}//---------------------------------------------------------------------------UnicodeString __fastcall TRemoteFile::GetFullFileName() const{  UnicodeString Result;  if (FFullFileName.IsEmpty())  {    DebugAssert(Terminal);    DebugAssert(Directory != NULL);    if (IsParentDirectory)    {      Result = Directory->ParentPath;    }    else if (IsDirectory)    {      Result = UnixIncludeTrailingBackslash(Directory->FullDirectory + FileName);    }    else    {      Result = Directory->FullDirectory + FileName;    }  }  else  {    Result = FFullFileName;  }  return Result;}//---------------------------------------------------------------------------bool __fastcall TRemoteFile::GetHaveFullFileName() const{  return !FFullFileName.IsEmpty() || (Directory != NULL);}//---------------------------------------------------------------------------Integer __fastcall TRemoteFile::GetAttr(){  Integer Result = 0;  if (Rights->ReadOnly) Result |= faReadOnly;  if (IsHidden) Result |= faHidden;  return Result;}//---------------------------------------------------------------------------void __fastcall TRemoteFile::SetTerminal(TTerminal * value){  FTerminal = value;  if (FLinkedFile)  {    FLinkedFile->Terminal = value;  }}//---------------------------------------------------------------------------//---------------------------------------------------------------------------__fastcall TRemoteDirectoryFile::TRemoteDirectoryFile() : TRemoteFile(){  Modification = double(0);  ModificationFmt = mfNone;  LastAccess = Modification;  Type = L'D';}//---------------------------------------------------------------------------//---------------------------------------------------------------------------__fastcall TRemoteParentDirectory::TRemoteParentDirectory(TTerminal * ATerminal)  : TRemoteDirectoryFile(){  FileName = PARENTDIRECTORY;  Terminal = ATerminal;}//=== TRemoteFileList ------------------------------------------------------__fastcall TRemoteFileList::TRemoteFileList():  TObjectList(){  FTimestamp = Now();}//---------------------------------------------------------------------------bool TRemoteFileList::AddFile(TRemoteFile * File){  Add(File);  File->Directory = this;  return true;}//---------------------------------------------------------------------------void TRemoteFileList::ExtractFile(TRemoteFile * File){  Extract(File);  File->Directory = NULL;}//---------------------------------------------------------------------------TStrings * __fastcall TRemoteFileList::CloneStrings(TStrings * List){  std::unique_ptr<TStringList> Result(new TStringList());  Result->OwnsObjects = true;  for (int Index = 0; Index < List->Count; Index++)  {    TRemoteFile * File = static_cast<TRemoteFile *>(List->Objects[Index]);    Result->AddObject(List->Strings[Index], File->Duplicate(true));  }  return Result.release();}//---------------------------------------------------------------------------bool TRemoteFileList::AnyDirectory(TStrings * List){  bool Result = false;  for (int Index = 0; !Result && (Index < List->Count); Index++)  {    Result = static_cast<TRemoteFile *>(List->Objects[Index])->IsDirectory;  }  return Result;}//---------------------------------------------------------------------------void __fastcall TRemoteFileList::DuplicateTo(TRemoteFileList * Copy){  Copy->Reset();  for (int Index = 0; Index < Count; Index++)  {    TRemoteFile * File = Files[Index];    Copy->AddFile(File->Duplicate(false));  }  Copy->FDirectory = Directory;  Copy->FTimestamp = FTimestamp;}//---------------------------------------------------------------------------void __fastcall TRemoteFileList::Reset(){  FTimestamp = Now();  Clear();}//---------------------------------------------------------------------------void TRemoteFileList::SetDirectory(const UnicodeString & value){  FDirectory = UnixExcludeTrailingBackslash(value);}//---------------------------------------------------------------------------UnicodeString __fastcall TRemoteFileList::GetFullDirectory(){  return UnixIncludeTrailingBackslash(Directory);}//---------------------------------------------------------------------------TRemoteFile * __fastcall TRemoteFileList::GetFiles(Integer Index){  return (TRemoteFile *)Items[Index];}//---------------------------------------------------------------------------Boolean __fastcall TRemoteFileList::GetIsRoot(){  return (Directory == ROOTDIRECTORY);}//---------------------------------------------------------------------------UnicodeString __fastcall TRemoteFileList::GetParentPath(){  return UnixExtractFilePath(Directory);}//---------------------------------------------------------------------------__int64 __fastcall TRemoteFileList::GetTotalSize(){  __int64 Result = 0;  for (Integer Index = 0; Index < Count; Index++)    Result += Files[Index]->Size;  return Result;}//---------------------------------------------------------------------------TRemoteFile * __fastcall TRemoteFileList::FindFile(const UnicodeString &FileName){  for (Integer Index = 0; Index < Count; Index++)    if (Files[Index]->FileName == FileName) return Files[Index];  return NULL;}//=== TRemoteDirectory ------------------------------------------------------__fastcall TRemoteDirectory::TRemoteDirectory(TTerminal * aTerminal, TRemoteDirectory * Template) :  TRemoteFileList(), FTerminal(aTerminal){  FParentDirectory = NULL;  if (Template == NULL)  {    FIncludeParentDirectory = true;  }  else  {    FIncludeParentDirectory = Template->FIncludeParentDirectory;  }}//---------------------------------------------------------------------------__fastcall TRemoteDirectory::~TRemoteDirectory(){  ReleaseRelativeDirectories();}//---------------------------------------------------------------------------void __fastcall TRemoteDirectory::ReleaseRelativeDirectories(){  if ((FParentDirectory != NULL) && !IncludeParentDirectory)  {    delete FParentDirectory;    FParentDirectory = NULL;  }}//---------------------------------------------------------------------------void __fastcall TRemoteDirectory::Reset(){  ReleaseRelativeDirectories();  TRemoteFileList::Reset();}//---------------------------------------------------------------------------bool TRemoteDirectory::AddFile(TRemoteFile * File){  bool Result = !File->IsThisDirectory;  if (!Result)  {    delete File;  }  else  {    if (File->IsParentDirectory)    {      FParentDirectory = File;    }    if (!File->IsParentDirectory || IncludeParentDirectory)    {      DebugCheck(TRemoteFileList::AddFile(File));    }    File->Terminal = Terminal;  }  return Result;}//---------------------------------------------------------------------------void __fastcall TRemoteDirectory::DuplicateTo(TRemoteFileList * Copy){  TRemoteFileList::DuplicateTo(Copy);  if ((FParentDirectory != NULL) && !IncludeParentDirectory)  {    Copy->AddFile(FParentDirectory->Duplicate(false));  }}//---------------------------------------------------------------------------bool __fastcall TRemoteDirectory::GetLoaded(){  return ((Terminal != NULL) && Terminal->Active && !Directory.IsEmpty());}//---------------------------------------------------------------------------void __fastcall TRemoteDirectory::SetIncludeParentDirectory(Boolean value){  if (IncludeParentDirectory != value)  {    FIncludeParentDirectory = value;    if (value && (FParentDirectory != NULL))    {      DebugAssert(IndexOf(FParentDirectory) < 0);      AddFile(FParentDirectory);    }    else if (!value && (FParentDirectory != NULL))    {      DebugAssert(IndexOf(FParentDirectory) >= 0);      ExtractFile(FParentDirectory);    }  }}//===========================================================================__fastcall TRemoteDirectoryCache::TRemoteDirectoryCache(): TStringList(){  FSection = new TCriticalSection();  Sorted = true;  Duplicates = Types::dupError;  CaseSensitive = true;}//---------------------------------------------------------------------------__fastcall TRemoteDirectoryCache::~TRemoteDirectoryCache(){  Clear();  delete FSection;}//---------------------------------------------------------------------------void __fastcall TRemoteDirectoryCache::Clear(){  TGuard Guard(FSection);  try  {    for (int Index = 0; Index < Count; Index++)    {      delete (TRemoteFileList *)Objects[Index];      Objects[Index] = NULL;    }  }  __finally  {    TStringList::Clear();  }}//---------------------------------------------------------------------------bool __fastcall TRemoteDirectoryCache::GetIsEmpty() const{  TGuard Guard(FSection);  return (const_cast<TRemoteDirectoryCache*>(this)->Count == 0);}//---------------------------------------------------------------------------bool __fastcall TRemoteDirectoryCache::HasFileList(const UnicodeString Directory){  TGuard Guard(FSection);  int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));  return (Index >= 0);}//---------------------------------------------------------------------------bool __fastcall TRemoteDirectoryCache::HasNewerFileList(const UnicodeString Directory,  TDateTime Timestamp){  TGuard Guard(FSection);  int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));  if (Index >= 0)  {    TRemoteFileList * FileList = dynamic_cast<TRemoteFileList *>(Objects[Index]);    if (FileList->Timestamp <= Timestamp)    {      Index = -1;    }  }  return (Index >= 0);}//---------------------------------------------------------------------------bool __fastcall TRemoteDirectoryCache::GetFileList(const UnicodeString Directory,  TRemoteFileList * FileList){  TGuard Guard(FSection);  int Index = IndexOf(UnixExcludeTrailingBackslash(Directory));  bool Result = (Index >= 0);  if (Result)  {    DebugAssert(Objects[Index] != NULL);    dynamic_cast<TRemoteFileList *>(Objects[Index])->DuplicateTo(FileList);  }  return Result;}//---------------------------------------------------------------------------void __fastcall TRemoteDirectoryCache::AddFileList(TRemoteFileList * FileList){  DebugAssert(FileList);  TRemoteFileList * Copy = new TRemoteFileList();  FileList->DuplicateTo(Copy);  {    TGuard Guard(FSection);    // file list cannot be cached already with only one thread, but it can be    // when directory is loaded by secondary terminal    DoClearFileList(FileList->Directory, false);    AddObject(Copy->Directory, Copy);  }}//---------------------------------------------------------------------------void __fastcall TRemoteDirectoryCache::ClearFileList(UnicodeString Directory, bool SubDirs){  TGuard Guard(FSection);  DoClearFileList(Directory, SubDirs);}//---------------------------------------------------------------------------void __fastcall TRemoteDirectoryCache::DoClearFileList(UnicodeString Directory, bool SubDirs){  Directory = UnixExcludeTrailingBackslash(Directory);  int Index = IndexOf(Directory);  if (Index >= 0)  {    Delete(Index);  }  if (SubDirs)  {    UnicodeString DirectoryWithSlash = UnixIncludeTrailingBackslash(Directory); // optimization    Index = Count-1;    while (Index >= 0)    {      if (UnixIsChildPath(DirectoryWithSlash, Strings[Index]))      {        Delete(Index);      }      Index--;    }  }}//---------------------------------------------------------------------------void __fastcall TRemoteDirectoryCache::Delete(int Index){  delete dynamic_cast<TRemoteFileList *>(Objects[Index]);  TStringList::Delete(Index);}//---------------------------------------------------------------------------//---------------------------------------------------------------------------__fastcall TRemoteDirectoryChangesCache::TRemoteDirectoryChangesCache(int MaxSize) :  TStringList(),  FMaxSize(MaxSize){  CaseSensitive = true;}//---------------------------------------------------------------------------void __fastcall TRemoteDirectoryChangesCache::Clear(){  TStringList::Clear();}//---------------------------------------------------------------------------bool __fastcall TRemoteDirectoryChangesCache::GetIsEmpty() const{  return (const_cast<TRemoteDirectoryChangesCache*>(this)->Count == 0);}//---------------------------------------------------------------------------void __fastcall TRemoteDirectoryChangesCache::SetValue(const UnicodeString & Name,  const UnicodeString & Value){  int Index = IndexOfName(Name);  if (Index > 0)  {    Delete(Index);  }  Values[Name] = Value;}//---------------------------------------------------------------------------UnicodeString __fastcall TRemoteDirectoryChangesCache::GetValue(const UnicodeString & Name){  UnicodeString Value = Values[Name];  SetValue(Name, Value);  return Value;}//---------------------------------------------------------------------------void __fastcall TRemoteDirectoryChangesCache::AddDirectoryChange(  const UnicodeString SourceDir, const UnicodeString Change,  const UnicodeString TargetDir){  DebugAssert(!TargetDir.IsEmpty());  SetValue(TargetDir, L"//");  if (TTerminal::ExpandFileName(Change, SourceDir) != TargetDir)  {    UnicodeString Key;    if (DirectoryChangeKey(SourceDir, Change, Key))    {      SetValue(Key, TargetDir);    }  }}//---------------------------------------------------------------------------void __fastcall TRemoteDirectoryChangesCache::ClearDirectoryChange(  UnicodeString SourceDir){  for (int Index = 0; Index < Count; Index++)  {    if (Names[Index].SubString(1, SourceDir.Length()) == SourceDir)    {      Delete(Index);      Index--;    }  }}//---------------------------------------------------------------------------void __fastcall TRemoteDirectoryChangesCache::ClearDirectoryChangeTarget(  UnicodeString TargetDir){  UnicodeString Key;  // hack to clear at least local sym-link change in case symlink is deleted  DirectoryChangeKey(UnixExcludeTrailingBackslash(UnixExtractFilePath(TargetDir)),    UnixExtractFileName(TargetDir), Key);  for (int Index = 0; Index < Count; Index++)  {    UnicodeString Name = Names[Index];    if ((Name.SubString(1, TargetDir.Length()) == TargetDir) ||        (Values[Name].SubString(1, TargetDir.Length()) == TargetDir) ||        (!Key.IsEmpty() && (Name == Key)))    {      Delete(Index);      Index--;    }  }}//---------------------------------------------------------------------------bool __fastcall TRemoteDirectoryChangesCache::GetDirectoryChange(  const UnicodeString SourceDir, const UnicodeString Change, UnicodeString & TargetDir){  UnicodeString Key;  bool Result;  Key = TTerminal::ExpandFileName(Change, SourceDir);  Result = (IndexOfName(Key) >= 0);  if (Result)  {    TargetDir = GetValue(Key);    // TargetDir is not "//" here only when Change is full path to symbolic link    if (TargetDir == L"//")    {      TargetDir = Key;    }  }  else  {    Result = DirectoryChangeKey(SourceDir, Change, Key);    if (Result)    {      UnicodeString Directory = GetValue(Key);      Result = !Directory.IsEmpty();      if (Result)      {        TargetDir = Directory;      }    }  }  return Result;}//---------------------------------------------------------------------------void __fastcall TRemoteDirectoryChangesCache::Serialize(UnicodeString & Data){  Data = L"A";  int ACount = Count;  if (ACount > FMaxSize)  {    TStrings * Limited = new TStringList();    try    {      int Index = ACount - FMaxSize;      while (Index < ACount)      {        Limited->Add(Strings[Index]);        Index++;      }      Data += Limited->Text;    }    __finally    {      delete Limited;    }  }  else  {    Data += Text;  }}//---------------------------------------------------------------------------void __fastcall TRemoteDirectoryChangesCache::Deserialize(const UnicodeString Data){  if (Data.IsEmpty())  {    Text = L"";  }  else  {    Text = Data.c_str() + 1;  }}//---------------------------------------------------------------------------bool __fastcall TRemoteDirectoryChangesCache::DirectoryChangeKey(  const UnicodeString SourceDir, const UnicodeString Change, UnicodeString & Key){  bool Result = !Change.IsEmpty();  if (Result)  {    bool Absolute = UnixIsAbsolutePath(Change);    Result = !SourceDir.IsEmpty() || Absolute;    if (Result)    {      // expanded from ?: to avoid memory leaks      if (Absolute)      {        Key = Change;      }      else      {        Key = SourceDir + L"," + Change;      }    }  }  return Result;}//=== TRights ---------------------------------------------------------------const wchar_t TRights::BasicSymbols[] = L"rwxrwxrwx";const wchar_t TRights::CombinedSymbols[] = L"--s--s--t";const wchar_t TRights::ExtendedSymbols[] = L"--S--S--T";const wchar_t TRights::ModeGroups[] = L"ugo";//---------------------------------------------------------------------------__fastcall TRights::TRights(){  FAllowUndef = false;  FSet = 0;  FUnset = 0;  Number = 0;  FUnknown = true;}//---------------------------------------------------------------------------__fastcall TRights::TRights(unsigned short ANumber){  FAllowUndef = false;  FSet = 0;  FUnset = 0;  Number = ANumber;}//---------------------------------------------------------------------------__fastcall TRights::TRights(const TRights & Source){  Assign(&Source);}//---------------------------------------------------------------------------//---------------------------------------------------------------------------void __fastcall TRights::Assign(const TRights * Source){  FAllowUndef = Source->AllowUndef;  FSet = Source->FSet;  FUnset = Source->FUnset;  FText = Source->FText;  FUnknown = Source->FUnknown;}//---------------------------------------------------------------------------TRights::TRight TRights::CalculateRight(TRightGroup Group, TRightLevel Level){  int Result;  if (Level == rlSpecial)  {    Result = rrUserIDExec + Group;  }  else  {    DebugAssert(rlRead == 0);    Result = rrUserRead + Level + (Group * 3);  }  return static_cast<TRight>(Result);}//---------------------------------------------------------------------------TRights::TFlag __fastcall TRights::RightToFlag(TRights::TRight Right){  return static_cast<TFlag>(1 << (rrLast - Right));}//---------------------------------------------------------------------------TRights::TFlag TRights::CalculateFlag(TRightGroup Group, TRightLevel Level){  return RightToFlag(CalculateRight(Group, Level));}//---------------------------------------------------------------------------unsigned short TRights::CalculatePermissions(TRightGroup Group, TRightLevel Level, TRightLevel Level2, TRightLevel Level3){  unsigned int Permissions = CalculateFlag(Group, Level);  if (Level2 != rlNone)  {    Permissions |= CalculateFlag(Group, Level2);  }  if (Level3 != rlNone)  {    Permissions |= CalculateFlag(Group, Level3);  }  unsigned short Result = static_cast<unsigned short>(Permissions);  DebugAssert((Permissions - Result) == 0);  return Result;}//---------------------------------------------------------------------------bool __fastcall TRights::operator ==(const TRights & rhr) const{  if (AllowUndef || rhr.AllowUndef)  {    for (int Right = rrFirst; Right <= rrLast; Right++)    {      if (RightUndef[static_cast<TRight>(Right)] !=            rhr.RightUndef[static_cast<TRight>(Right)])      {        return false;      }    }    return true;  }  else  {    return (Number == rhr.Number);  }}//---------------------------------------------------------------------------bool __fastcall TRights::operator ==(unsigned short rhr) const{  return (Number == rhr);}//---------------------------------------------------------------------------bool __fastcall TRights::operator !=(const TRights & rhr) const{  return !(*this == rhr);}//---------------------------------------------------------------------------TRights & __fastcall TRights::operator =(unsigned short rhr){  Number = rhr;  return *this;}//---------------------------------------------------------------------------TRights & __fastcall TRights::operator =(const TRights & rhr){  Assign(&rhr);  return *this;}//---------------------------------------------------------------------------TRights __fastcall TRights::operator ~() const{  TRights Result(static_cast<unsigned short>(~Number));  return Result;}//---------------------------------------------------------------------------TRights __fastcall TRights::operator &(const TRights & rhr) const{  TRights Result(*this);  Result &= rhr;  return Result;}//---------------------------------------------------------------------------TRights __fastcall TRights::operator &(unsigned short rhr) const{  TRights Result(*this);  Result &= rhr;  return Result;}//---------------------------------------------------------------------------TRights & __fastcall TRights::operator &=(const TRights & rhr){  if (AllowUndef || rhr.AllowUndef)  {    for (int Right = rrFirst; Right <= rrLast; Right++)    {      if (RightUndef[static_cast<TRight>(Right)] !=            rhr.RightUndef[static_cast<TRight>(Right)])      {        RightUndef[static_cast<TRight>(Right)] = rsUndef;      }    }  }  else  {    Number &= rhr.Number;  }  return *this;}//---------------------------------------------------------------------------TRights & __fastcall TRights::operator &=(unsigned short rhr){  Number &= rhr;  return *this;}//---------------------------------------------------------------------------TRights __fastcall TRights::operator |(const TRights & rhr) const{  TRights Result(*this);  Result |= rhr;  return Result;}//---------------------------------------------------------------------------TRights __fastcall TRights::operator |(unsigned short rhr) const{  TRights Result(*this);  Result |= rhr;  return Result;}//---------------------------------------------------------------------------TRights & __fastcall TRights::operator |=(const TRights & rhr){  Number |= rhr.Number;  return *this;}//---------------------------------------------------------------------------TRights & __fastcall TRights::operator |=(unsigned short rhr){  Number |= rhr;  return *this;}//---------------------------------------------------------------------------void __fastcall TRights::SetAllowUndef(bool value){  if (FAllowUndef != value)  {    DebugAssert(!value || ((FSet | FUnset) == rfAllSpecials));    FAllowUndef = value;  }}//---------------------------------------------------------------------------void __fastcall TRights::SetText(const UnicodeString & value){  if (value != Text)  {    if ((value.Length() != TextLen) ||        (!AllowUndef && (value.Pos(UndefSymbol) > 0)) ||        (value.Pos(L" ") > 0))    {      throw Exception(FMTLOAD(RIGHTS_ERROR, (value)));    }    FSet = 0;    FUnset = 0;    int Flag = 00001;    int ExtendedFlag = 01000;    bool KeepText = false;    for (int i = TextLen; i >= 1; i--)    {      if ((value[i] == UnsetSymbol) || (value[i] == UnsetSymbolWin))      {        FUnset |= static_cast<unsigned short>(Flag | ExtendedFlag);      }      else if (value[i] == UndefSymbol)      {        // do nothing      }      else if (value[i] == CombinedSymbols[i - 1])      {        FSet |= static_cast<unsigned short>(Flag | ExtendedFlag);      }      else if (value[i] == ExtendedSymbols[i - 1])      {        FSet |= static_cast<unsigned short>(ExtendedFlag);        FUnset |= static_cast<unsigned short>(Flag);      }      else      {        if (value[i] != BasicSymbols[i - 1])        {          KeepText = true;        }        FSet |= static_cast<unsigned short>(Flag);        if (i % 3 == 0)        {          FUnset |= static_cast<unsigned short>(ExtendedFlag);        }      }      Flag <<= 1;      if (i % 3 == 1)      {        ExtendedFlag <<= 1;      }    }    FText = KeepText ? value : UnicodeString();  }  FUnknown = false;}//---------------------------------------------------------------------------void TRights::SetTextOverride(const UnicodeString & value){  if (FText != value)  {    FText = value;    FUnknown = false;  }}//---------------------------------------------------------------------------UnicodeString __fastcall TRights::GetText() const{  if (!FText.IsEmpty())  {    return FText;  }  else  {    UnicodeString Result;    Result.SetLength(TextLen);    int Flag = 00001;    int ExtendedFlag = 01000;    bool ExtendedPos = true;    wchar_t Symbol;    int i = TextLen;    while (i >= 1)    {      if (ExtendedPos &&          ((FSet & (Flag | ExtendedFlag)) == (Flag | ExtendedFlag)))      {        Symbol = CombinedSymbols[i - 1];      }      else if ((FSet & Flag) != 0)      {        Symbol = BasicSymbols[i - 1];      }      else if (ExtendedPos && ((FSet & ExtendedFlag) != 0))      {        Symbol = ExtendedSymbols[i - 1];      }      else if ((!ExtendedPos && ((FUnset & Flag) == Flag)) ||        (ExtendedPos && ((FUnset & (Flag | ExtendedFlag)) == (Flag | ExtendedFlag))))      {        Symbol = UnsetSymbol;      }      else      {        Symbol = UndefSymbol;      }      Result[i] = Symbol;      Flag <<= 1;      i--;      ExtendedPos = ((i % 3) == 0);      if (ExtendedPos)      {        ExtendedFlag <<= 1;      }    }    return Result;  }}//---------------------------------------------------------------------------void __fastcall TRights::SetOctal(UnicodeString value){  UnicodeString AValue(value);  if (AValue.Length() == 3)  {    AValue = L"0" + AValue;  }  if (Octal != AValue)  {    bool Correct = (AValue.Length() == 4);    if (Correct)    {      for (int i = 1; (i <= AValue.Length()) && Correct; i++)      {        Correct = (AValue[i] >= L'0') && (AValue[i] <= L'7');      }    }    if (!Correct)    {      throw Exception(FMTLOAD(INVALID_OCTAL_PERMISSIONS, (value)));    }    Number = static_cast<unsigned short>(      ((AValue[1] - L'0') << 9) +      ((AValue[2] - L'0') << 6) +      ((AValue[3] - L'0') << 3) +      ((AValue[4] - L'0') << 0));    FText = L"";  }  FUnknown = false;}//---------------------------------------------------------------------------unsigned long __fastcall TRights::GetNumberDecadic() const{  unsigned long N = NumberSet; // used to be "Number"  unsigned long Result =      ((N & 07000) / 01000 * 1000) +      ((N & 00700) /  0100 *  100) +      ((N & 00070) /   010 *   10) +      ((N & 00007) /    01 *    1);  return Result;}//---------------------------------------------------------------------------UnicodeString __fastcall TRights::GetOctal() const{  UnicodeString Result;  unsigned short N = NumberSet; // used to be "Number"  Result.SetLength(4);  Result[1] = static_cast<wchar_t>(L'0' + ((N & 07000) >> 9));  Result[2] = static_cast<wchar_t>(L'0' + ((N & 00700) >> 6));  Result[3] = static_cast<wchar_t>(L'0' + ((N & 00070) >> 3));  Result[4] = static_cast<wchar_t>(L'0' + ((N & 00007) >> 0));  return Result;}//---------------------------------------------------------------------------void __fastcall TRights::SetNumber(unsigned short value){  if ((FSet != value) || ((FSet | FUnset) != rfAllSpecials))  {    FSet = value;    FUnset = static_cast<unsigned short>(rfAllSpecials & ~FSet);    FText = L"";  }  FUnknown = false;}//---------------------------------------------------------------------------unsigned short __fastcall TRights::GetNumber() const{  DebugAssert(!IsUndef);  return FSet;}//---------------------------------------------------------------------------void __fastcall TRights::SetRight(TRight Right, bool value){  RightUndef[Right] = (value ? rsYes : rsNo);}//---------------------------------------------------------------------------bool __fastcall TRights::GetRight(TRight Right) const{  TState State = RightUndef[Right];  DebugAssert(State != rsUndef);  return (State == rsYes);}//---------------------------------------------------------------------------void __fastcall TRights::SetRightUndef(TRight Right, TState value){  if (value != RightUndef[Right])  {    DebugAssert((value != rsUndef) || AllowUndef);    TFlag Flag = RightToFlag(Right);    switch (value)    {      case rsYes:        FSet |= static_cast<unsigned short>(Flag);        FUnset &= static_cast<unsigned short>(~Flag);        break;      case rsNo:        FSet &= static_cast<unsigned short>(~Flag);        FUnset |= static_cast<unsigned short>(Flag);        break;      case rsUndef:      default:        FSet &= static_cast<unsigned short>(~Flag);        FUnset &= static_cast<unsigned short>(~Flag);        break;    }    FText = L"";  }  FUnknown = false;}//---------------------------------------------------------------------------TRights::TState __fastcall TRights::GetRightUndef(TRight Right) const{  TFlag Flag = RightToFlag(Right);  TState Result;  if ((FSet & Flag) != 0)  {    Result = rsYes;  }  else if ((FUnset & Flag) != 0)  {    Result = rsNo;  }  else  {    Result = rsUndef;  }  return Result;}//---------------------------------------------------------------------------void __fastcall TRights::SetReadOnly(bool value){  Right[rrUserWrite] = !value;  Right[rrGroupWrite] = !value;  Right[rrOtherWrite] = !value;}//---------------------------------------------------------------------------bool  __fastcall TRights::GetReadOnly(){  return Right[rrUserWrite] && Right[rrGroupWrite] && Right[rrOtherWrite];}//---------------------------------------------------------------------------UnicodeString __fastcall TRights::GetChmodStr(int Directory) const{  UnicodeString Result;  if (IsUndef)  {    Result = ModeStr;  }  else  {    Result = Octal;    if (Directory != 0) // unknown or folder    {      // New versions of coreutils need leading 5th zero to unset UID/GID for directories      Result = UnicodeString(L"0") + Result;    }  }  return Result;}//---------------------------------------------------------------------------UnicodeString __fastcall TRights::GetModeStr() const{  UnicodeString Result;  UnicodeString SetModeStr, UnsetModeStr;  TRight Right;  int Index;  for (int Group = 0; Group < 3; Group++)  {    SetModeStr = L"";    UnsetModeStr = L"";    for (int Mode = 0; Mode < 3; Mode++)    {      Index = (Group * 3) + Mode;      Right = static_cast<TRight>(rrUserRead + Index);      switch (RightUndef[Right])      {        case rsYes:          SetModeStr += BasicSymbols[Index];          break;        case rsNo:          UnsetModeStr += BasicSymbols[Index];          break;      }    }    Right = static_cast<TRight>(rrUserIDExec + Group);    Index = (Group * 3) + 2;    switch (RightUndef[Right])    {      case rsYes:        SetModeStr += CombinedSymbols[Index];        break;      case rsNo:        UnsetModeStr += CombinedSymbols[Index];        break;    }    if (!SetModeStr.IsEmpty() || !UnsetModeStr.IsEmpty())    {      if (!Result.IsEmpty())      {        Result += L',';      }      Result += ModeGroups[Group];      if (!SetModeStr.IsEmpty())      {        Result += L"+" + SetModeStr;      }      if (!UnsetModeStr.IsEmpty())      {        Result += L"-" + UnsetModeStr;      }    }  }  return Result;}//---------------------------------------------------------------------------void __fastcall TRights::AddExecute(){  for (int Group = 0; Group < 3; Group++)  {    if ((RightUndef[static_cast<TRight>(rrUserRead + (Group * 3))] == rsYes) ||        (RightUndef[static_cast<TRight>(rrUserWrite + (Group * 3))] == rsYes))    {      Right[static_cast<TRight>(rrUserExec + (Group * 3))] = true;      FText = L"";    }  }  FUnknown = false;}//---------------------------------------------------------------------------void __fastcall TRights::AllUndef(){  if ((FSet != 0) || (FUnset != 0))  {    FSet = 0;    FUnset = 0;    FText = L"";  }  FUnknown = false;}//---------------------------------------------------------------------------bool __fastcall TRights::GetIsUndef() const{  return ((FSet | FUnset) != rfAllSpecials);}//---------------------------------------------------------------------------__fastcall TRights::operator unsigned short() const{  return Number;}//---------------------------------------------------------------------------__fastcall TRights::operator unsigned long() const{  return Number;}//---------------------------------------------------------------------------TRights TRights::Combine(const TRights & Other) const{  TRights Result = (*this);  Result |= Other.NumberSet;  Result &= (unsigned short)~Other.NumberUnset;  return Result;}//=== TRemoteProperties -------------------------------------------------------__fastcall TRemoteProperties::TRemoteProperties(){  Default();}//---------------------------------------------------------------------------__fastcall TRemoteProperties::TRemoteProperties(const TRemoteProperties & rhp) :  Valid(rhp.Valid),  Recursive(rhp.Recursive),  Rights(rhp.Rights),  AddXToDirectories(rhp.AddXToDirectories),  Group(rhp.Group),  Owner(rhp.Owner),  Modification(rhp.Modification),  LastAccess(rhp.Modification),  Encrypt(rhp.Encrypt),  Tags(rhp.Tags){}//---------------------------------------------------------------------------void __fastcall TRemoteProperties::Default(){  Valid.Clear();  AddXToDirectories = false;  Rights.AllowUndef = false;  Rights.Number = 0;  Group.Clear();  Owner.Clear();  Recursive = false;  Encrypt = false;  Tags = EmptyStr;}//---------------------------------------------------------------------------bool __fastcall TRemoteProperties::operator ==(const TRemoteProperties & rhp) const{  bool Result = (Valid == rhp.Valid && Recursive == rhp.Recursive);  if (Result)  {    if ((Valid.Contains(vpRights) &&          (Rights != rhp.Rights || AddXToDirectories != rhp.AddXToDirectories)) ||        (Valid.Contains(vpOwner) && (Owner != rhp.Owner)) ||        (Valid.Contains(vpGroup) && (Group != rhp.Group)) ||        (Valid.Contains(vpModification) && (Modification != rhp.Modification)) ||        (Valid.Contains(vpLastAccess) && (LastAccess != rhp.LastAccess)) ||        (Valid.Contains(vpEncrypt) && (Encrypt != rhp.Encrypt)) ||        (Valid.Contains(vpTags) && (Tags != rhp.Tags)))    {      Result = false;    }  }  return Result;}//---------------------------------------------------------------------------bool __fastcall TRemoteProperties::operator !=(const TRemoteProperties & rhp) const{  return !(*this == rhp);}//---------------------------------------------------------------------------TRemoteProperties __fastcall TRemoteProperties::CommonProperties(TStrings * FileList){  // TODO: Modification and LastAccess  TRemoteProperties CommonProperties;  for (int Index = 0; Index < FileList->Count; Index++)  {    TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);    DebugAssert(File);    if (!Index)    {      if (!File->Rights->Unknown)      {        CommonProperties.Rights = *(File->Rights);        // previously we allowed undef implicitly for directories,        // now we do it explicitly in properties dialog and only in combination        // with "recursive" option        CommonProperties.Rights.AllowUndef = File->Rights->IsUndef;        CommonProperties.Valid << vpRights;      }      if (File->Owner.IsSet)      {        CommonProperties.Owner = File->Owner;        CommonProperties.Valid << vpOwner;      }      if (File->Group.IsSet)      {        CommonProperties.Group = File->Group;        CommonProperties.Valid << vpGroup;      }      CommonProperties.Tags = File->Tags;      CommonProperties.Valid << vpTags;    }    else    {      CommonProperties.Rights.AllowUndef = True;      CommonProperties.Rights &= *File->Rights;      if (CommonProperties.Owner != File->Owner)      {        CommonProperties.Owner.Clear();        CommonProperties.Valid >> vpOwner;      }      if (CommonProperties.Group != File->Group)      {        CommonProperties.Group.Clear();        CommonProperties.Valid >> vpGroup;      }      if (CommonProperties.Tags != File->Tags)      {        CommonProperties.Tags = EmptyStr;        CommonProperties.Valid >> vpTags;      }    }  }  return CommonProperties;}//---------------------------------------------------------------------------TRemoteProperties __fastcall TRemoteProperties::ChangedProperties(  const TRemoteProperties & OriginalProperties, TRemoteProperties NewProperties){  DebugAssert(!OriginalProperties.Valid.Contains(vpEncrypt));  // TODO: Modification and LastAccess  if (!NewProperties.Recursive)  {    if (NewProperties.Rights == OriginalProperties.Rights &&        !NewProperties.AddXToDirectories)    {      NewProperties.Valid >> vpRights;    }    if (NewProperties.Group == OriginalProperties.Group)    {      NewProperties.Valid >> vpGroup;    }    if (NewProperties.Owner == OriginalProperties.Owner)    {      NewProperties.Valid >> vpOwner;    }    if (NewProperties.Tags == OriginalProperties.Tags)    {      NewProperties.Valid >> vpTags;    }  }  return NewProperties;}//---------------------------------------------------------------------------void __fastcall TRemoteProperties::Load(THierarchicalStorage * Storage){  unsigned char Buf[sizeof(Valid)];  if (static_cast<size_t>(Storage->ReadBinaryData(L"Valid", &Buf, sizeof(Buf))) == sizeof(Buf))  {    memmove(&Valid, Buf, sizeof(Valid));  }  if (Valid.Contains(vpRights))  {    Rights.Text = Storage->ReadString(L"Rights", Rights.Text);  }  // TODO}//---------------------------------------------------------------------------void __fastcall TRemoteProperties::Save(THierarchicalStorage * Storage) const{  Storage->WriteBinaryData(UnicodeString(L"Valid"),    static_cast<const void *>(&Valid), sizeof(Valid));  if (Valid.Contains(vpRights))  {    Storage->WriteString(L"Rights", Rights.Text);  }  // TODO}//---------------------------------------------------------------------------//---------------------------------------------------------------------------TSynchronizeChecklist::TItem::TItem() :  Action(saNone), IsDirectory(false), ImageIndex(-1), Checked(true), RemoteFile(NULL), FDirectoryHasSize(false){  Info1.ModificationFmt = mfFull;  Info1.Modification = 0;  Info1.Size = 0;  Info2.ModificationFmt = mfFull;  Info2.Modification = 0;  Info2.Size = 0;}//---------------------------------------------------------------------------TSynchronizeChecklist::TItem::~TItem(){  delete RemoteFile;}//---------------------------------------------------------------------------const UnicodeString& TSynchronizeChecklist::TItem::GetFileName() const{  if (!Info2.FileName.IsEmpty())  {    return Info2.FileName;  }  else  {    DebugAssert(!Info1.FileName.IsEmpty());    return Info1.FileName;  }}//---------------------------------------------------------------------------__int64 __fastcall TSynchronizeChecklist::TItem::GetSize() const{  return GetSize(Action);}//---------------------------------------------------------------------------__int64 __fastcall TSynchronizeChecklist::TItem::GetSize(TAction AAction) const{  if (IsItemSizeIrrelevant(AAction))  {    return 0;  }  else  {    return GetBaseSize(AAction);  }}//---------------------------------------------------------------------------__int64 __fastcall TSynchronizeChecklist::TItem::GetBaseSize() const{  return GetBaseSize(Action);}//---------------------------------------------------------------------------__int64 __fastcall TSynchronizeChecklist::TItem::GetBaseSize(TAction AAction) const{  switch (AAction)  {    case saUploadNew:    case saUploadUpdate:    case saDeleteLocal:      return Info1.Size;    case saDownloadNew:    case saDownloadUpdate:    case saDeleteRemote:      return Info2.Size;    default:      DebugFail();      return 0;  }}//---------------------------------------------------------------------------UnicodeString TSynchronizeChecklist::TItem::GetLocalPath() const{  return CombinePaths(Info1.Directory, Info1.FileName);}//---------------------------------------------------------------------------UnicodeString TSynchronizeChecklist::TItem::GetLocalPath2() const{  return CombinePaths(Info2.Directory, Info2.FileName);}//---------------------------------------------------------------------------UnicodeString TSynchronizeChecklist::TItem::ForceGetLocalPath() const{  return CombinePaths(Info1.Directory, DefaultStr(Info1.FileName, Info2.FileName));}//---------------------------------------------------------------------------UnicodeString TSynchronizeChecklist::TItem::ForceGetLocalPath2() const{  return CombinePaths(Info2.Directory, DefaultStr(Info2.FileName, Info1.FileName));}//---------------------------------------------------------------------------UnicodeString TSynchronizeChecklist::TItem::GetRemotePath() const{  return UnixCombinePaths(Info2.Directory, Info2.FileName);}//---------------------------------------------------------------------------UnicodeString TSynchronizeChecklist::TItem::ForceGetRemotePath() const{  return UnixCombinePaths(Info2.Directory, GetFileName());}//---------------------------------------------------------------------------UnicodeString TSynchronizeChecklist::TItem::GetLocalTarget() const{  return IncludeTrailingBackslash(Info1.Directory);}//---------------------------------------------------------------------------UnicodeString TSynchronizeChecklist::TItem::GetLocalTarget2() const{  return IncludeTrailingBackslash(Info2.Directory);}//---------------------------------------------------------------------------UnicodeString TSynchronizeChecklist::TItem::GetRemoteTarget() const{  return UnixIncludeTrailingBackslash(Info2.Directory);};//---------------------------------------------------------------------------TStrings * TSynchronizeChecklist::TItem::GetFileList() const{  std::unique_ptr<TStrings> FileList(new TStringList());  switch (Action)  {    case TSynchronizeChecklist::saDownloadNew:    case TSynchronizeChecklist::saDownloadUpdate:    case TSynchronizeChecklist::saDeleteRemote:      FileList->AddObject(GetRemotePath(), RemoteFile);      break;    case TSynchronizeChecklist::saUploadNew:    case TSynchronizeChecklist::saUploadUpdate:    case TSynchronizeChecklist::saDeleteLocal:      FileList->Add(GetLocalPath());      break;    default:      DebugFail();      NotImplemented();      UNREACHABLE_AFTER_NORETURN(break);  }  return FileList.release();}//---------------------------------------------------------------------------//---------------------------------------------------------------------------TSynchronizeChecklist::TSynchronizeChecklist() :  FList(new TList()){}//---------------------------------------------------------------------------TSynchronizeChecklist::~TSynchronizeChecklist(){  for (int Index = 0; Index < FList->Count; Index++)  {    delete static_cast<TItem *>(FList->Items[Index]);  }  delete FList;}//---------------------------------------------------------------------------void TSynchronizeChecklist::Add(TItem * Item){  FList->Add(Item);}//---------------------------------------------------------------------------int TSynchronizeChecklist::Compare(const TItem * Item1, const TItem * Item2){  int Result;  if (!Item1->Info1.Directory.IsEmpty())  {    Result = AnsiCompareText(Item1->Info1.Directory, Item2->Info1.Directory);  }  else  {    DebugAssert(!Item1->Info2.Directory.IsEmpty());    Result = AnsiCompareText(Item1->Info2.Directory, Item2->Info2.Directory);  }  if (Result == 0)  {    Result = AnsiCompareText(Item1->GetFileName(), Item2->GetFileName());  }  return Result;}//---------------------------------------------------------------------------int __fastcall TSynchronizeChecklist::Compare(void * AItem1, void * AItem2){  return Compare(static_cast<TItem *>(AItem1), static_cast<TItem *>(AItem2));}//---------------------------------------------------------------------------void TSynchronizeChecklist::Sort(){  FList->Sort(Compare);}//---------------------------------------------------------------------------int TSynchronizeChecklist::GetCount() const{  return FList->Count;}//---------------------------------------------------------------------------int TSynchronizeChecklist::GetCheckedCount() const{  int Result = 0;  for (int Index = 0; (Index < Count); Index++)  {    if (Item[Index]->Checked)    {      Result++;    }  }  return Result;}//---------------------------------------------------------------------------bool TSynchronizeChecklist::GetNextChecked(int & Index, const TItem *& AItem) const{  while (Index < Count)  {    const TItem * TheItem = Item[Index];    Index++;    if (TheItem->Checked)    {      AItem = TheItem;      return true;    }  }  AItem = NULL;  return false;}//---------------------------------------------------------------------------const TSynchronizeChecklist::TItem * TSynchronizeChecklist::GetItem(int Index) const{  return static_cast<TItem *>(FList->Items[Index]);}//---------------------------------------------------------------------------void __fastcall TSynchronizeChecklist::Update(const TItem * Item, bool Check, TAction Action){  // TSynchronizeChecklist owns non-const items so it can manipulate them freely,  // const_cast here is just an optimization  TItem * MutableItem = const_cast<TItem *>(Item);  DebugAssert(FList->IndexOf(MutableItem) >= 0);  MutableItem->Checked = Check;  MutableItem->Action = Action;}//---------------------------------------------------------------------------void TSynchronizeChecklist::Delete(const TItem * Item){  // See comment in Update()  TItem * MutableItem = const_cast<TItem *>(Item);  FList->Extract(MutableItem);  delete Item;}//---------------------------------------------------------------------------void __fastcall TSynchronizeChecklist::UpdateDirectorySize(const TItem * Item, __int64 Size){  // See comment in Update  TItem * MutableItem = const_cast<TItem *>(Item);  DebugAssert(FList->IndexOf(MutableItem) >= 0);  if (DebugAlwaysTrue(Item->IsDirectory))  {    MutableItem->FDirectoryHasSize = true;    if (Item->IsRemoteOnly())    {      MutableItem->Info2.Size = Size;    }    else if (Item->IsLocalOnly())    {      MutableItem->Info1.Size = Size;    }    else    {      // "update" actions are not relevant for directories      DebugFail();    }  }}//---------------------------------------------------------------------------TSynchronizeChecklist::TAction __fastcall TSynchronizeChecklist::Reverse(TSynchronizeChecklist::TAction Action){  switch (Action)  {    case saUploadNew:      return saDeleteLocal;    case saDownloadNew:      return saDeleteRemote;    case saUploadUpdate:      return saDownloadUpdate;    case saDownloadUpdate:      return saUploadUpdate;    case saDeleteRemote:      return saDownloadNew;    case saDeleteLocal:      return saUploadNew;    default:    case saNone:      DebugFail();      return saNone;  }}//---------------------------------------------------------------------------bool __fastcall TSynchronizeChecklist::IsItemSizeIrrelevant(TAction Action){  switch (Action)  {    case saNone:    case saDeleteRemote:    case saDeleteLocal:      return true;    default:      return false;  }}//---------------------------------------------------------------------------//---------------------------------------------------------------------------class TFileOperationProgressSink : public TCppInterfacedObject<IFileOperationProgressSink>{public:  TFileOperationProgressSink(TSynchronizeChecklistFileOperation * FileOperation) : FFileOperation(FileOperation)  {  }  virtual HRESULT STDMETHODCALLTYPE StartOperations() { return S_OK; }  virtual HRESULT STDMETHODCALLTYPE FinishOperations(HRESULT) { return S_OK; }  virtual HRESULT STDMETHODCALLTYPE PreRenameItem(DWORD, IShellItem *, LPCWSTR) { return S_OK; }  virtual HRESULT STDMETHODCALLTYPE PostRenameItem(DWORD, IShellItem * Item, LPCWSTR, HRESULT, IShellItem *)  {    DebugFail();    FFileOperation->ProcessedItem(Item);    return S_OK;  }  virtual HRESULT STDMETHODCALLTYPE PreMoveItem(DWORD, IShellItem *, IShellItem *, LPCWSTR) { return S_OK; }  virtual HRESULT STDMETHODCALLTYPE PostMoveItem(DWORD, IShellItem * Item, IShellItem *, LPCWSTR, HRESULT, IShellItem *)  {    DebugFail();    FFileOperation->ProcessedItem(Item);    return S_OK;  }  virtual HRESULT STDMETHODCALLTYPE PreCopyItem(DWORD, IShellItem *, IShellItem *, LPCWSTR) { return S_OK; }  virtual HRESULT STDMETHODCALLTYPE PostCopyItem(DWORD, IShellItem * Item, IShellItem *, LPCWSTR, HRESULT, IShellItem *)  {    FFileOperation->ProcessedItem(Item);    return S_OK;  }  virtual HRESULT STDMETHODCALLTYPE PreDeleteItem(DWORD, IShellItem *) { return S_OK; }  virtual HRESULT STDMETHODCALLTYPE PostDeleteItem(DWORD, IShellItem * Item, HRESULT, IShellItem *)  {    FFileOperation->ProcessedItem(Item);    return S_OK;  }  virtual HRESULT STDMETHODCALLTYPE PreNewItem(DWORD, IShellItem *, LPCWSTR) { return S_OK; }  virtual HRESULT STDMETHODCALLTYPE PostNewItem(DWORD, IShellItem *, LPCWSTR, LPCWSTR, DWORD, HRESULT, IShellItem *)  {    DebugFail();    return S_OK;  }  virtual HRESULT STDMETHODCALLTYPE UpdateProgress(UINT, UINT) { return S_OK; }  virtual HRESULT STDMETHODCALLTYPE ResetTimer() { return S_OK; }  virtual HRESULT STDMETHODCALLTYPE PauseTimer() { return S_OK; }  virtual HRESULT STDMETHODCALLTYPE ResumeTimer() { return S_OK; }private:  TSynchronizeChecklistFileOperation * FFileOperation;};//---------------------------------------------------------------------------IShellItem * CreateShellItemFromParsingName(const UnicodeString & Path){  IShellItem * Result;  OleCheck(SHCreateItemFromParsingName(Path.c_str(), nullptr, IID_PPV_ARGS(&Result)));  return Result;}//---------------------------------------------------------------------------TSynchronizeChecklistFileOperation::TSynchronizeChecklistFileOperation(    const TSynchronizeChecklist * Checklist, TProcessedSynchronizationChecklistItem OnProcessedItem, void * Token) :  FOnProcessedItem(OnProcessedItem),  FToken(Token){  FFileOperation.Create(CLSID_FileOperation, CLSCTX_ALL);  FProgressSink = new TFileOperationProgressSink(this);  DWORD UnusedCookie;  OleCheck(FFileOperation->Advise(FProgressSink, &UnusedCookie));  int Index = 0;  const TSynchronizeChecklist::TItem * ChecklistItem;  while (Checklist->GetNextChecked(Index, ChecklistItem))  {    UnicodeString ItemPath;    switch (ChecklistItem->Action)    {      case TSynchronizeChecklist::saDownloadNew:      case TSynchronizeChecklist::saDownloadUpdate:      case TSynchronizeChecklist::saUploadNew:      case TSynchronizeChecklist::saUploadUpdate:        {          UnicodeString DestinationFolderPath;          if ((ChecklistItem->Action == TSynchronizeChecklist::saDownloadNew) ||              (ChecklistItem->Action == TSynchronizeChecklist::saDownloadUpdate))          {            ItemPath = ChecklistItem->GetLocalPath2();            DestinationFolderPath = ChecklistItem->GetLocalTarget();          }          else          {            ItemPath = ChecklistItem->GetLocalPath();            DestinationFolderPath = ChecklistItem->GetLocalTarget2();          }          TComPtr<IShellItem> Item = CreateShellItemFromParsingName(ItemPath);          TComPtr<IShellItem> DestinationFolder = CreateShellItemFromParsingName(DestinationFolderPath);          OleCheck(FFileOperation->CopyItem(Item.Get(), DestinationFolder.Get(), nullptr, nullptr));        }        break;      case TSynchronizeChecklist::saDeleteRemote:      case TSynchronizeChecklist::saDeleteLocal:        {          bool Left = (ChecklistItem->Action == TSynchronizeChecklist::saDeleteLocal);          ItemPath = Left ? ChecklistItem->GetLocalPath() : ChecklistItem->GetLocalPath2();          TComPtr<IShellItem> Item = CreateShellItemFromParsingName(ItemPath);          OleCheck(FFileOperation->DeleteItem(Item.Get(), nullptr));        }        break;      default:        DebugFail();        break;    }    if (!ItemPath.IsEmpty())    {      FShellItems.insert(std::make_pair(ItemPath.LowerCase(), ChecklistItem));    }  }}//---------------------------------------------------------------------------TSynchronizeChecklistFileOperation::~TSynchronizeChecklistFileOperation(){  FProgressSink->Release();}//---------------------------------------------------------------------------void TSynchronizeChecklistFileOperation::ProcessedItem(IShellItem * ShellItem){  wchar_t * NamePtr;  // It's different IShellItem than what we pass to IFileOperation above, so we have to use underlying path for lookup  if (DebugAlwaysTrue(SUCCEEDED(ShellItem->GetDisplayName(SIGDN_FILESYSPATH, &NamePtr))))  {    UnicodeString Name = NamePtr;    CoTaskMemFree(NamePtr);    auto I = FShellItems.find(Name.LowerCase());    // The callback are triggered even for files in folders, so we do not get here only for top level checklist items.    // The folder is triggered before the files, so we unfortunatelly mark the folder as processed before its files are processed.    // Solution would be to wait until we get (Pre?) trigger for another checklist item (or until the end of whole operation).    if (I != FShellItems.end())    {      auto ChecklistItem = I->second;      FOnProcessedItem(FToken, ChecklistItem);    }  }}//---------------------------------------------------------------------------//---------------------------------------------------------------------------TSynchronizeProgress::TSynchronizeProgress(const TSynchronizeChecklist * Checklist){  FTotalSize = -1;  FProcessedSize = 0;  FChecklist = Checklist;}//---------------------------------------------------------------------------__int64 TSynchronizeProgress::ItemSize(const TSynchronizeChecklist::TItem * ChecklistItem) const{  __int64 Result;  switch (ChecklistItem->Action)  {    case TSynchronizeChecklist::saDeleteRemote:    case TSynchronizeChecklist::saDeleteLocal:      Result = ChecklistItem->IsDirectory ? 1024*1024 : 100*1024;      break;    default:      if (ChecklistItem->HasSize())      {        Result = ChecklistItem->GetSize();      }      else      {        DebugAssert(ChecklistItem->IsDirectory);        Result = 1024*1024;      }      break;  }  return Result;}//---------------------------------------------------------------------------void TSynchronizeProgress::ItemProcessed(const TSynchronizeChecklist::TItem * ChecklistItem){  FProcessedSize += ItemSize(ChecklistItem);}//---------------------------------------------------------------------------__int64 TSynchronizeProgress::GetProcessed(const TFileOperationProgressType * CurrentItemOperationProgress) const{  DebugAssert(!TFileOperationProgressType::IsIndeterminateOperation(CurrentItemOperationProgress->Operation));  // Need to calculate the total size on the first call only,  // as at the time the constructor it called, we usually do not have sizes of folders calculated yet.  if (FTotalSize < 0)  {    FTotalSize = 0;    int Index = 0;    const TSynchronizeChecklist::TItem * ChecklistItem;    while (FChecklist->GetNextChecked(Index, ChecklistItem))    {      FTotalSize += ItemSize(ChecklistItem);    }  }  // For (single-item-)delete operation, this should return 0  __int64 CurrentItemProcessedSize = CurrentItemOperationProgress->OperationTransferred;  return (FProcessedSize + CurrentItemProcessedSize);}//---------------------------------------------------------------------------int TSynchronizeProgress::Progress(const TFileOperationProgressType * CurrentItemOperationProgress) const{  int Result;  if (TFileOperationProgressType::IsIndeterminateOperation(CurrentItemOperationProgress->Operation))  {    Result = -1;  }  else  {    __int64 Processed = GetProcessed(CurrentItemOperationProgress);    if (FTotalSize > 0)    {      Result = static_cast<int>((Processed * 100) / FTotalSize);    }    else    {      Result = 0;    }  }  return Result;}//---------------------------------------------------------------------------TDateTime TSynchronizeProgress::TimeLeft(const TFileOperationProgressType * CurrentItemOperationProgress) const{  TDateTime Result;  __int64 Processed = GetProcessed(CurrentItemOperationProgress);  if (Processed > 0)  {    Result = TDateTime(double(Now() - CurrentItemOperationProgress->StartTime) / Processed * (FTotalSize - Processed));  }  return Result;}
 |