123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "ScpFileSystem.h"
- #include "Terminal.h"
- #include "Common.h"
- #include "Exceptions.h"
- #include "Interface.h"
- #include "TextsCore.h"
- #include "HelpCore.h"
- #include "SecureShell.h"
- #include <stdio.h>
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- #define FILE_OPERATION_LOOP_EX(ALLOW_SKIP, MESSAGE, OPERATION) \
- FILE_OPERATION_LOOP_CUSTOM(FTerminal, ALLOW_SKIP, MESSAGE, OPERATION, L"")
- //---------------------------------------------------------------------------
- const int coRaiseExcept = 1;
- const int coExpectNoOutput = 2;
- const int coWaitForLastLine = 4;
- const int coOnlyReturnCode = 8;
- const int coIgnoreWarnings = 16;
- const int coReadProgress = 32;
- const int ecRaiseExcept = 1;
- const int ecIgnoreWarnings = 2;
- const int ecReadProgress = 4;
- const int ecDefault = ecRaiseExcept;
- //---------------------------------------------------------------------------
- #define THROW_FILE_SKIPPED(EXCEPTION, MESSAGE) \
- throw EScpFileSkipped(EXCEPTION, MESSAGE)
- #define THROW_SCP_ERROR(EXCEPTION, MESSAGE) \
- throw EScp(EXCEPTION, MESSAGE)
- //===========================================================================
- #define MaxShellCommand fsAnyCommand
- #define ShellCommandCount MaxShellCommand + 1
- #define MaxCommandLen 40
- struct TCommandType
- {
- int MinLines;
- int MaxLines;
- bool ModifiesFiles;
- bool ChangesDirectory;
- bool InteractiveCommand;
- wchar_t Command[MaxCommandLen];
- };
- // Only one character! See TSCPFileSystem::ReadCommandOutput()
- #define LastLineSeparator L":"
- #define LAST_LINE L"WinSCP: this is end-of-file"
- #define FIRST_LINE L"WinSCP: this is begin-of-file"
- extern const TCommandType DefaultCommandSet[];
- #define NationalVarCount 10
- extern const wchar_t NationalVars[NationalVarCount][15];
- #define CHECK_CMD assert((Cmd >=0) && (Cmd <= MaxShellCommand))
- class TSessionData;
- //---------------------------------------------------------------------------
- class TCommandSet
- {
- private:
- TCommandType CommandSet[ShellCommandCount];
- TSessionData * FSessionData;
- UnicodeString FReturnVar;
- int __fastcall GetMaxLines(TFSCommand Cmd);
- int __fastcall GetMinLines(TFSCommand Cmd);
- bool __fastcall GetModifiesFiles(TFSCommand Cmd);
- bool __fastcall GetChangesDirectory(TFSCommand Cmd);
- bool __fastcall GetOneLineCommand(TFSCommand Cmd);
- void __fastcall SetCommands(TFSCommand Cmd, UnicodeString value);
- UnicodeString __fastcall GetCommands(TFSCommand Cmd);
- UnicodeString __fastcall GetFirstLine();
- bool __fastcall GetInteractiveCommand(TFSCommand Cmd);
- UnicodeString __fastcall GetLastLine();
- UnicodeString __fastcall GetReturnVar();
- public:
- __fastcall TCommandSet(TSessionData *aSessionData);
- void __fastcall Default();
- void __fastcall CopyFrom(TCommandSet * Source);
- UnicodeString __fastcall Command(TFSCommand Cmd, const TVarRec * args, int size);
- TStrings * __fastcall CreateCommandList();
- UnicodeString __fastcall FullCommand(TFSCommand Cmd, const TVarRec * args, int size);
- static UnicodeString __fastcall ExtractCommand(UnicodeString Command);
- __property int MaxLines[TFSCommand Cmd] = { read=GetMaxLines};
- __property int MinLines[TFSCommand Cmd] = { read=GetMinLines };
- __property bool ModifiesFiles[TFSCommand Cmd] = { read=GetModifiesFiles };
- __property bool ChangesDirectory[TFSCommand Cmd] = { read=GetChangesDirectory };
- __property bool OneLineCommand[TFSCommand Cmd] = { read=GetOneLineCommand };
- __property UnicodeString Commands[TFSCommand Cmd] = { read=GetCommands, write=SetCommands };
- __property UnicodeString FirstLine = { read = GetFirstLine };
- __property bool InteractiveCommand[TFSCommand Cmd] = { read = GetInteractiveCommand };
- __property UnicodeString LastLine = { read=GetLastLine };
- __property TSessionData * SessionData = { read=FSessionData, write=FSessionData };
- __property UnicodeString ReturnVar = { read=GetReturnVar, write=FReturnVar };
- };
- //===========================================================================
- const wchar_t NationalVars[NationalVarCount][15] =
- {L"LANG", L"LANGUAGE", L"LC_CTYPE", L"LC_COLLATE", L"LC_MONETARY", L"LC_NUMERIC",
- L"LC_TIME", L"LC_MESSAGES", L"LC_ALL", L"HUMAN_BLOCKS" };
- const wchar_t FullTimeOption[] = L"--full-time";
- //---------------------------------------------------------------------------
- #define F false
- #define T true
- // TODO: remove "mf" and "cd", it is implemented in TTerminal already
- const TCommandType DefaultCommandSet[ShellCommandCount] = {
- // min max mf cd ia command
- /*Null*/ { -1, -1, F, F, F, L"" },
- /*VarValue*/ { -1, -1, F, F, F, L"echo \"$%s\"" /* variable */ },
- /*LastLine*/ { -1, -1, F, F, F, L"echo \"%s" LastLineSeparator "%s\"" /* last line, return var */ },
- /*FirstLine*/ { -1, -1, F, F, F, L"echo \"%s\"" /* first line */ },
- /*CurrentDirectory*/ { 1, 1, F, F, F, L"pwd" },
- /*ChangeDirectory*/ { 0, 0, F, T, F, L"cd %s" /* directory */ },
- // list directory can be empty on permission denied, this is handled in ReadDirectory
- /*ListDirectory*/ { -1, -1, F, F, F, L"%s %s \"%s\"" /* listing command, options, directory */ },
- /*ListCurrentDirectory*/{ -1, -1, F, F, F, L"%s %s" /* listing command, options */ },
- /*ListFile*/ { 1, 1, F, F, F, L"%s -d %s \"%s\"" /* listing command, options, file/directory */ },
- /*LookupUserGroups*/ { 0, 1, F, F, F, L"groups" },
- /*CopyToRemote*/ { -1, -1, T, F, T, L"scp -r %s -d -t \"%s\"" /* options, directory */ },
- /*CopyToLocal*/ { -1, -1, F, F, T, L"scp -r %s -d -f \"%s\"" /* options, file */ },
- /*DeleteFile*/ { 0, 0, T, F, F, L"rm -f -r \"%s\"" /* file/directory */},
- /*RenameFile*/ { 0, 0, T, F, F, L"mv -f \"%s\" \"%s\"" /* file/directory, new name*/},
- /*CreateDirectory*/ { 0, 0, T, F, F, L"mkdir \"%s\"" /* new directory */},
- /*ChangeMode*/ { 0, 0, T, F, F, L"chmod %s %s \"%s\"" /* options, mode, filename */},
- /*ChangeGroup*/ { 0, 0, T, F, F, L"chgrp %s \"%s\" \"%s\"" /* options, group, filename */},
- /*ChangeOwner*/ { 0, 0, T, F, F, L"chown %s \"%s\" \"%s\"" /* options, owner, filename */},
- /*HomeDirectory*/ { 0, 0, F, T, F, L"cd" },
- /*Unset*/ { 0, 0, F, F, F, L"unset \"%s\"" /* variable */ },
- /*Unalias*/ { 0, 0, F, F, F, L"unalias \"%s\"" /* alias */ },
- /*CreateLink*/ { 0, 0, T, F, F, L"ln %s \"%s\" \"%s\"" /*symbolic (-s), filename, point to*/},
- /*CopyFile*/ { 0, 0, T, F, F, L"cp -p -r -f \"%s\" \"%s\"" /* file/directory, target name*/},
- /*AnyCommand*/ { 0, -1, T, T, F, L"%s" }
- };
- #undef F
- #undef T
- //---------------------------------------------------------------------------
- __fastcall TCommandSet::TCommandSet(TSessionData *aSessionData):
- FSessionData(aSessionData), FReturnVar(L"")
- {
- assert(FSessionData);
- Default();
- }
- //---------------------------------------------------------------------------
- void __fastcall TCommandSet::CopyFrom(TCommandSet * Source)
- {
- memmove(&CommandSet, Source->CommandSet, sizeof(CommandSet));
- }
- //---------------------------------------------------------------------------
- void __fastcall TCommandSet::Default()
- {
- assert(sizeof(CommandSet) == sizeof(DefaultCommandSet));
- memmove(&CommandSet, &DefaultCommandSet, sizeof(CommandSet));
- }
- //---------------------------------------------------------------------------
- int __fastcall TCommandSet::GetMaxLines(TFSCommand Cmd)
- {
- CHECK_CMD;
- return CommandSet[Cmd].MaxLines;
- }
- //---------------------------------------------------------------------------
- int __fastcall TCommandSet::GetMinLines(TFSCommand Cmd)
- {
- CHECK_CMD;
- return CommandSet[Cmd].MinLines;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TCommandSet::GetModifiesFiles(TFSCommand Cmd)
- {
- CHECK_CMD;
- return CommandSet[Cmd].ModifiesFiles;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TCommandSet::GetChangesDirectory(TFSCommand Cmd)
- {
- CHECK_CMD;
- return CommandSet[Cmd].ChangesDirectory;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TCommandSet::GetInteractiveCommand(TFSCommand Cmd)
- {
- CHECK_CMD;
- return CommandSet[Cmd].InteractiveCommand;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TCommandSet::GetOneLineCommand(TFSCommand /*Cmd*/)
- {
- //CHECK_CMD;
- // #56: we send "echo last line" from all commands on same line
- // just as it was in 1.0
- return True; //CommandSet[Cmd].OneLineCommand;
- }
- //---------------------------------------------------------------------------
- void __fastcall TCommandSet::SetCommands(TFSCommand Cmd, UnicodeString value)
- {
- CHECK_CMD;
- wcscpy(CommandSet[Cmd].Command, value.SubString(1, MaxCommandLen - 1).c_str());
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TCommandSet::GetCommands(TFSCommand Cmd)
- {
- CHECK_CMD;
- return CommandSet[Cmd].Command;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TCommandSet::Command(TFSCommand Cmd, const TVarRec * args, int size)
- {
- if (args) return Format(Commands[Cmd], args, size);
- else return Commands[Cmd];
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TCommandSet::FullCommand(TFSCommand Cmd, const TVarRec * args, int size)
- {
- UnicodeString Separator;
- if (OneLineCommand[Cmd]) Separator = L" ; ";
- else Separator = L"\n";
- UnicodeString Line = Command(Cmd, args, size);
- UnicodeString LastLineCmd =
- Command(fsLastLine, ARRAYOFCONST((LastLine, ReturnVar)));
- UnicodeString FirstLineCmd;
- if (InteractiveCommand[Cmd])
- FirstLineCmd = Command(fsFirstLine, ARRAYOFCONST((FirstLine))) + Separator;
- UnicodeString Result;
- if (!Line.IsEmpty())
- Result = FORMAT(L"%s%s%s%s", (FirstLineCmd, Line, Separator, LastLineCmd));
- else
- Result = FORMAT(L"%s%s", (FirstLineCmd, LastLineCmd));
- return Result;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TCommandSet::GetFirstLine()
- {
- return FIRST_LINE;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TCommandSet::GetLastLine()
- {
- return LAST_LINE;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TCommandSet::GetReturnVar()
- {
- assert(SessionData);
- if (!FReturnVar.IsEmpty())
- {
- return UnicodeString(L'$') + FReturnVar;
- }
- else if (SessionData->DetectReturnVar)
- {
- return L'0';
- }
- else
- {
- return UnicodeString(L'$') + SessionData->ReturnVar;
- }
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TCommandSet::ExtractCommand(UnicodeString Command)
- {
- int P = Command.Pos(L" ");
- if (P > 0)
- {
- Command.SetLength(P-1);
- }
- return Command;
- }
- //---------------------------------------------------------------------------
- TStrings * __fastcall TCommandSet::CreateCommandList()
- {
- TStrings * CommandList = new TStringList();
- for (Integer Index = 0; Index < ShellCommandCount; Index++)
- {
- UnicodeString Cmd = Commands[(TFSCommand)Index];
- if (!Cmd.IsEmpty())
- {
- Cmd = ExtractCommand(Cmd);
- if ((Cmd != L"%s") && (CommandList->IndexOf(Cmd) < 0))
- CommandList->Add(Cmd);
- }
- }
- return CommandList;
- }
- //===========================================================================
- __fastcall TSCPFileSystem::TSCPFileSystem(TTerminal * ATerminal, TSecureShell * SecureShell):
- TCustomFileSystem(ATerminal)
- {
- FSecureShell = SecureShell;
- FCommandSet = new TCommandSet(FTerminal->SessionData);
- FLsFullTime = FTerminal->SessionData->SCPLsFullTime;
- FOutput = new TStringList();
- FProcessingCommand = false;
- FOnCaptureOutput = NULL;
- FFileSystemInfo.ProtocolBaseName = L"SCP";
- FFileSystemInfo.ProtocolName = FFileSystemInfo.ProtocolBaseName;
- // capabilities of SCP protocol are fixed
- for (int Index = 0; Index < fcCount; Index++)
- {
- FFileSystemInfo.IsCapable[Index] = IsCapable((TFSCapability)Index);
- }
- }
- //---------------------------------------------------------------------------
- __fastcall TSCPFileSystem::~TSCPFileSystem()
- {
- delete FCommandSet;
- delete FOutput;
- delete FSecureShell;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::Open()
- {
- // this is used for reconnects only
- FSecureShell->Open();
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::Close()
- {
- FSecureShell->Close();
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSCPFileSystem::GetActive()
- {
- return FSecureShell->Active;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::CollectUsage()
- {
- FSecureShell->CollectUsage();
- }
- //---------------------------------------------------------------------------
- const TSessionInfo & __fastcall TSCPFileSystem::GetSessionInfo()
- {
- return FSecureShell->GetSessionInfo();
- }
- //---------------------------------------------------------------------------
- const TFileSystemInfo & __fastcall TSCPFileSystem::GetFileSystemInfo(bool Retrieve)
- {
- if (FFileSystemInfo.AdditionalInfo.IsEmpty() && Retrieve)
- {
- UnicodeString UName;
- FTerminal->ExceptionOnFail = true;
- try
- {
- try
- {
- AnyCommand(L"uname -a", NULL);
- for (int Index = 0; Index < Output->Count; Index++)
- {
- if (Index > 0)
- {
- UName += L"; ";
- }
- UName += Output->Strings[Index];
- }
- }
- catch(...)
- {
- if (!FTerminal->Active)
- {
- throw;
- }
- }
- }
- __finally
- {
- FTerminal->ExceptionOnFail = false;
- }
- FFileSystemInfo.RemoteSystem = UName;
- }
- return FFileSystemInfo;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSCPFileSystem::TemporaryTransferFile(const UnicodeString & /*FileName*/)
- {
- return false;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSCPFileSystem::GetStoredCredentialsTried()
- {
- return FSecureShell->GetStoredCredentialsTried();
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TSCPFileSystem::GetUserName()
- {
- return FSecureShell->UserName;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::Idle()
- {
- // Keep session alive
- if ((FTerminal->SessionData->PingType != ptOff) &&
- (Now() - FSecureShell->LastDataSent > FTerminal->SessionData->PingIntervalDT))
- {
- if ((FTerminal->SessionData->PingType == ptDummyCommand) &&
- FSecureShell->Ready)
- {
- if (!FProcessingCommand)
- {
- ExecCommand(fsNull, NULL, 0, 0);
- }
- else
- {
- FTerminal->LogEvent(L"Cannot send keepalive, command is being executed");
- // send at least SSH-level keepalive, if nothing else, it at least updates
- // LastDataSent, no the next keepalive attempt is postponed
- FSecureShell->KeepAlive();
- }
- }
- else
- {
- FSecureShell->KeepAlive();
- }
- }
- FSecureShell->Idle();
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TSCPFileSystem::AbsolutePath(UnicodeString Path, bool /*Local*/)
- {
- return ::AbsolutePath(CurrentDirectory, Path);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSCPFileSystem::IsCapable(int Capability) const
- {
- assert(FTerminal);
- switch (Capability) {
- case fcUserGroupListing:
- case fcModeChanging:
- case fcModeChangingUpload:
- case fcPreservingTimestampUpload:
- case fcGroupChanging:
- case fcOwnerChanging:
- case fcAnyCommand:
- case fcShellAnyCommand:
- case fcHardLink:
- case fcSymbolicLink:
- case fcResolveSymlink:
- case fcRename:
- case fcRemoteMove:
- case fcRemoteCopy:
- case fcRemoveCtrlZUpload:
- case fcRemoveBOMUpload:
- return true;
- case fcTextMode:
- return FTerminal->SessionData->EOLType != FTerminal->Configuration->LocalEOLType;
- case fcNativeTextMode:
- case fcNewerOnlyUpload:
- case fcTimestampChanging:
- case fcLoadingAdditionalProperties:
- case fcCheckingSpaceAvailable:
- case fcIgnorePermErrors:
- case fcCalculatingChecksum:
- case fcSecondaryShell: // has fcShellAnyCommand
- case fcGroupOwnerChangingByID: // by name
- return false;
- default:
- assert(false);
- return false;
- }
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TSCPFileSystem::DelimitStr(UnicodeString Str)
- {
- if (!Str.IsEmpty())
- {
- Str = ::DelimitStr(Str, L"\\`$\"");
- if (Str[1] == L'-') Str = L"./"+Str;
- }
- return Str;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::EnsureLocation()
- {
- if (!FCachedDirectoryChange.IsEmpty())
- {
- FTerminal->LogEvent(FORMAT(L"Locating to cached directory \"%s\".",
- (FCachedDirectoryChange)));
- UnicodeString Directory = FCachedDirectoryChange;
- FCachedDirectoryChange = L"";
- try
- {
- ChangeDirectory(Directory);
- }
- catch(...)
- {
- // when location to cached directory fails, pretend again
- // location in cached directory
- // here used to be check (CurrentDirectory != Directory), but it is
- // false always (current directory is already set to cached directory),
- // making the condition below useless. check removed.
- if (FTerminal->Active)
- {
- FCachedDirectoryChange = Directory;
- }
- throw;
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::SendCommand(const UnicodeString Cmd)
- {
- EnsureLocation();
- UnicodeString Line;
- FSecureShell->ClearStdError();
- FReturnCode = 0;
- FOutput->Clear();
- // We suppose, that 'Cmd' already contains command that ensures,
- // that 'LastLine' will be printed
- FSecureShell->SendLine(Cmd);
- FProcessingCommand = true;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSCPFileSystem::IsTotalListingLine(const UnicodeString Line)
- {
- // On some hosts there is not "total" but "totalt". What's the reason??
- // see mail from "Jan Wiklund (SysOp)" <[email protected]>
- return !Line.SubString(1, 5).CompareIC(L"total");
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSCPFileSystem::RemoveLastLine(UnicodeString & Line,
- int & ReturnCode, UnicodeString LastLine)
- {
- bool IsLastLine = false;
- if (LastLine.IsEmpty()) LastLine = LAST_LINE;
- // #55: fixed so, even when last line of command output does not
- // contain CR/LF, we can recognize last line
- int Pos = Line.Pos(LastLine);
- if (Pos)
- {
- // 2003-07-14: There must be nothing after return code number to
- // consider string as last line. This fixes bug with 'set' command
- // in console window
- UnicodeString ReturnCodeStr = Line.SubString(Pos + LastLine.Length() + 1,
- Line.Length() - Pos + LastLine.Length());
- if (TryStrToInt(ReturnCodeStr, ReturnCode))
- {
- IsLastLine = true;
- Line.SetLength(Pos - 1);
- }
- }
- return IsLastLine;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSCPFileSystem::IsLastLine(UnicodeString & Line)
- {
- bool Result = false;
- try
- {
- Result = RemoveLastLine(Line, FReturnCode, FCommandSet->LastLine);
- }
- catch (Exception &E)
- {
- FTerminal->TerminalError(&E, LoadStr(CANT_DETECT_RETURN_CODE));
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::SkipFirstLine()
- {
- UnicodeString Line = FSecureShell->ReceiveLine();
- if (Line != FCommandSet->FirstLine)
- {
- FTerminal->TerminalError(NULL, FMTLOAD(FIRST_LINE_EXPECTED, (Line)));
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::ReadCommandOutput(int Params, const UnicodeString * Cmd)
- {
- try
- {
- if (Params & coWaitForLastLine)
- {
- UnicodeString Line;
- bool IsLast;
- unsigned int Total = 0;
- // #55: fixed so, even when last line of command output does not
- // contain CR/LF, we can recognize last line
- do
- {
- Line = FSecureShell->ReceiveLine();
- IsLast = IsLastLine(Line);
- if (!IsLast || !Line.IsEmpty())
- {
- FOutput->Add(Line);
- if (FLAGSET(Params, coReadProgress))
- {
- Total++;
- if (Total % 10 == 0)
- {
- bool Cancel; //dummy
- FTerminal->DoReadDirectoryProgress(Total, Cancel);
- }
- }
- }
- }
- while (!IsLast);
- }
- if (Params & coRaiseExcept)
- {
- UnicodeString Message = FSecureShell->GetStdError();
- if ((Params & coExpectNoOutput) && FOutput->Count)
- {
- if (!Message.IsEmpty()) Message += L"\n";
- Message += FOutput->Text;
- }
- while (!Message.IsEmpty() && (Message.LastDelimiter(L"\n\r") == Message.Length()))
- {
- Message.SetLength(Message.Length() - 1);
- }
- bool WrongReturnCode =
- (ReturnCode > 1) || (ReturnCode == 1 && !(Params & coIgnoreWarnings));
- if (Params & coOnlyReturnCode && WrongReturnCode)
- {
- FTerminal->TerminalError(FMTLOAD(COMMAND_FAILED_CODEONLY, (ReturnCode)));
- }
- else if (!(Params & coOnlyReturnCode) &&
- ((!Message.IsEmpty() && ((FOutput->Count == 0) || !(Params & coIgnoreWarnings))) ||
- WrongReturnCode))
- {
- assert(Cmd != NULL);
- FTerminal->TerminalError(FMTLOAD(COMMAND_FAILED, (*Cmd, ReturnCode, Message)));
- }
- }
- }
- __finally
- {
- FProcessingCommand = false;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::ExecCommand(const UnicodeString & Cmd, int Params,
- const UnicodeString & CmdString)
- {
- if (Params < 0) Params = ecDefault;
- if (FTerminal->UseBusyCursor)
- {
- Busy(true);
- }
- try
- {
- SendCommand(Cmd);
- int COParams = coWaitForLastLine;
- if (Params & ecRaiseExcept) COParams |= coRaiseExcept;
- if (Params & ecIgnoreWarnings) COParams |= coIgnoreWarnings;
- if (Params & ecReadProgress) COParams |= coReadProgress;
- ReadCommandOutput(COParams, &CmdString);
- }
- __finally
- {
- if (FTerminal->UseBusyCursor)
- {
- Busy(false);
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::ExecCommand(TFSCommand Cmd, const TVarRec * args,
- int size, int Params)
- {
- if (Params < 0) Params = ecDefault;
- UnicodeString FullCommand = FCommandSet->FullCommand(Cmd, args, size);
- UnicodeString Command = FCommandSet->Command(Cmd, args, size);
- ExecCommand(FullCommand, Params, Command);
- if (Params & ecRaiseExcept)
- {
- Integer MinL = FCommandSet->MinLines[Cmd];
- Integer MaxL = FCommandSet->MaxLines[Cmd];
- if (((MinL >= 0) && (MinL > FOutput->Count)) ||
- ((MaxL >= 0) && (MaxL > FOutput->Count)))
- {
- FTerminal->TerminalError(FmtLoadStr(INVALID_OUTPUT_ERROR,
- ARRAYOFCONST((FullCommand, Output->Text))));
- }
- }
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TSCPFileSystem::GetCurrentDirectory()
- {
- return FCurrentDirectory;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::DoStartup()
- {
- // SkipStartupMessage and DetectReturnVar must succeed,
- // otherwise session is to be closed.
- try
- {
- FTerminal->ExceptionOnFail = true;
- SkipStartupMessage();
- if (FTerminal->SessionData->DetectReturnVar) DetectReturnVar();
- FTerminal->ExceptionOnFail = false;
- }
- catch (Exception & E)
- {
- FTerminal->FatalError(&E, L"");
- }
- #define COND_OPER(OPER) if (FTerminal->SessionData->OPER) OPER()
- COND_OPER(ClearAliases);
- COND_OPER(UnsetNationalVars);
- #undef COND_OPER
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::SkipStartupMessage()
- {
- try
- {
- FTerminal->LogEvent(L"Skipping host startup message (if any).");
- ExecCommand(fsNull, NULL, 0, 0);
- }
- catch (Exception & E)
- {
- FTerminal->CommandError(&E, LoadStr(SKIP_STARTUP_MESSAGE_ERROR), 0, HELP_SKIP_STARTUP_MESSAGE_ERROR);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::LookupUsersGroups()
- {
- ExecCommand(fsLookupUsersGroups);
- FTerminal->FUsers.Clear();
- FTerminal->FGroups.Clear();
- if (FOutput->Count > 0)
- {
- UnicodeString Groups = FOutput->Strings[0];
- while (!Groups.IsEmpty())
- {
- UnicodeString NewGroup = CutToChar(Groups, L' ', false);
- FTerminal->FGroups.Add(TRemoteToken(NewGroup));
- FTerminal->FMembership.Add(TRemoteToken(NewGroup));
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::DetectReturnVar()
- {
- // This suppose that something was already executed (probably SkipStartupMessage())
- // or return code variable is already set on start up.
- try
- {
- // #60 17.10.01: "status" and "?" switched
- UnicodeString ReturnVars[2] = { L"status", L"?" };
- UnicodeString NewReturnVar = L"";
- FTerminal->LogEvent(L"Detecting variable containing return code of last command.");
- for (int Index = 0; Index < 2; Index++)
- {
- bool Success = true;
- try
- {
- FTerminal->LogEvent(FORMAT(L"Trying \"$%s\".", (ReturnVars[Index])));
- ExecCommand(fsVarValue, ARRAYOFCONST((ReturnVars[Index])));
- if ((Output->Count != 1) || (StrToIntDef(Output->Strings[0], 256) > 255))
- {
- FTerminal->LogEvent(L"The response is not numerical exit code");
- EXCEPTION;
- }
- }
- catch (EFatal &E)
- {
- // if fatal error occurs, we need to exit ...
- throw;
- }
- catch (Exception &E)
- {
- // ...otherwise, we will try next variable (if any)
- Success = false;
- }
- if (Success)
- {
- NewReturnVar = ReturnVars[Index];
- break;
- }
- }
- if (NewReturnVar.IsEmpty())
- {
- EXCEPTION;
- }
- else
- {
- FCommandSet->ReturnVar = NewReturnVar;
- FTerminal->LogEvent(FORMAT(L"Return code variable \"%s\" selected.",
- (FCommandSet->ReturnVar)));
- }
- }
- catch (Exception &E)
- {
- FTerminal->CommandError(&E, LoadStr(DETECT_RETURNVAR_ERROR));
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::ClearAlias(UnicodeString Alias)
- {
- if (!Alias.IsEmpty())
- {
- // this command usually fails, because there will never be
- // aliases on all commands -> see last False parameter
- ExecCommand(fsUnalias, ARRAYOFCONST((Alias)), false);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::ClearAliases()
- {
- try
- {
- FTerminal->LogEvent(L"Clearing all aliases.");
- ClearAlias(TCommandSet::ExtractCommand(FTerminal->SessionData->ListingCommand));
- TStrings * CommandList = FCommandSet->CreateCommandList();
- try
- {
- for (int Index = 0; Index < CommandList->Count; Index++)
- {
- ClearAlias(CommandList->Strings[Index]);
- }
- }
- __finally
- {
- delete CommandList;
- }
- }
- catch (Exception &E)
- {
- FTerminal->CommandError(&E, LoadStr(UNALIAS_ALL_ERROR));
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::UnsetNationalVars()
- {
- try
- {
- FTerminal->LogEvent(L"Clearing national user variables.");
- for (int Index = 0; Index < NationalVarCount; Index++)
- {
- ExecCommand(fsUnset, ARRAYOFCONST((NationalVars[Index])), false);
- }
- }
- catch (Exception &E)
- {
- FTerminal->CommandError(&E, LoadStr(UNSET_NATIONAL_ERROR));
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::ReadCurrentDirectory()
- {
- if (FCachedDirectoryChange.IsEmpty())
- {
- ExecCommand(fsCurrentDirectory);
- FCurrentDirectory = UnixExcludeTrailingBackslash(FOutput->Strings[0]);
- }
- else
- {
- FCurrentDirectory = FCachedDirectoryChange;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::HomeDirectory()
- {
- ExecCommand(fsHomeDirectory);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::AnnounceFileListOperation()
- {
- // noop
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::ChangeDirectory(const UnicodeString Directory)
- {
- UnicodeString ToDir;
- if (!Directory.IsEmpty() &&
- ((Directory[1] != L'~') || (Directory.SubString(1, 2) == L"~ ")))
- {
- ToDir = L"\"" + DelimitStr(Directory) + L"\"";
- }
- else
- {
- ToDir = DelimitStr(Directory);
- }
- ExecCommand(fsChangeDirectory, ARRAYOFCONST((ToDir)));
- FCachedDirectoryChange = L"";
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::CachedChangeDirectory(const UnicodeString Directory)
- {
- FCachedDirectoryChange = UnixExcludeTrailingBackslash(Directory);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::ReadDirectory(TRemoteFileList * FileList)
- {
- assert(FileList);
- // emptying file list moved before command execution
- FileList->Reset();
- bool Again;
- do
- {
- Again = false;
- try
- {
- int Params = ecDefault | ecReadProgress |
- FLAGMASK(FTerminal->SessionData->IgnoreLsWarnings, ecIgnoreWarnings);
- const wchar_t * Options =
- ((FLsFullTime == asAuto) || (FLsFullTime == asOn)) ? FullTimeOption : L"";
- bool ListCurrentDirectory = (FileList->Directory == FTerminal->CurrentDirectory);
- if (ListCurrentDirectory)
- {
- FTerminal->LogEvent(L"Listing current directory.");
- ExecCommand(fsListCurrentDirectory,
- ARRAYOFCONST((FTerminal->SessionData->ListingCommand, Options)), Params);
- }
- else
- {
- FTerminal->LogEvent(FORMAT(L"Listing directory \"%s\".",
- (FileList->Directory)));
- ExecCommand(fsListDirectory,
- ARRAYOFCONST((FTerminal->SessionData->ListingCommand, Options,
- DelimitStr(FileList->Directory))),
- Params);
- }
- TRemoteFile * File;
- // If output is not empty, we have successfully got file listing,
- // otherwise there was an error, in case it was "permission denied"
- // we try to get at least parent directory (see "else" statement below)
- if (FOutput->Count > 0)
- {
- // Copy LS command output, because eventual symlink analysis would
- // modify FTerminal->Output
- TStringList * OutputCopy = new TStringList();
- try
- {
- OutputCopy->Assign(FOutput);
- // delete leading "total xxx" line
- // On some hosts there is not "total" but "totalt". What's the reason??
- // see mail from "Jan Wiklund (SysOp)" <[email protected]>
- if (IsTotalListingLine(OutputCopy->Strings[0]))
- {
- OutputCopy->Delete(0);
- }
- for (int Index = 0; Index < OutputCopy->Count; Index++)
- {
- File = CreateRemoteFile(OutputCopy->Strings[Index]);
- FileList->AddFile(File);
- }
- }
- __finally
- {
- delete OutputCopy;
- }
- }
- else
- {
- bool Empty;
- if (ListCurrentDirectory)
- {
- // Empty file list -> probably "permission denied", we
- // at least get link to parent directory ("..")
- FTerminal->ReadFile(
- UnixIncludeTrailingBackslash(FTerminal->FFiles->Directory) +
- PARENTDIRECTORY, File);
- Empty = (File == NULL);
- if (!Empty)
- {
- assert(File->IsParentDirectory);
- FileList->AddFile(File);
- }
- }
- else
- {
- Empty = true;
- }
- if (Empty)
- {
- throw ExtException(
- NULL, FMTLOAD(EMPTY_DIRECTORY, (FileList->Directory)),
- HELP_EMPTY_DIRECTORY);
- }
- }
- if (FLsFullTime == asAuto)
- {
- FTerminal->LogEvent(
- FORMAT(L"Directory listing with %s succeed, next time all errors during "
- "directory listing will be displayed immediately.",
- (FullTimeOption)));
- FLsFullTime = asOn;
- }
- }
- catch(Exception & E)
- {
- if (FTerminal->Active)
- {
- if (FLsFullTime == asAuto)
- {
- FTerminal->Log->AddException(&E);
- FLsFullTime = asOff;
- Again = true;
- FTerminal->LogEvent(
- FORMAT(L"Directory listing with %s failed, try again regular listing.",
- (FullTimeOption)));
- }
- else
- {
- throw;
- }
- }
- else
- {
- throw;
- }
- }
- }
- while (Again);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::ReadSymlink(TRemoteFile * SymlinkFile,
- TRemoteFile *& File)
- {
- CustomReadFile(SymlinkFile->LinkTo, File, SymlinkFile);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::ReadFile(const UnicodeString FileName,
- TRemoteFile *& File)
- {
- CustomReadFile(FileName, File, NULL);
- }
- //---------------------------------------------------------------------------
- TRemoteFile * __fastcall TSCPFileSystem::CreateRemoteFile(
- const UnicodeString & ListingStr, TRemoteFile * LinkedByFile)
- {
- TRemoteFile * File = new TRemoteFile(LinkedByFile);
- try
- {
- File->Terminal = FTerminal;
- File->ListingStr = ListingStr;
- File->ShiftTime(FTerminal->SessionData->TimeDifference);
- File->Complete();
- }
- catch(...)
- {
- delete File;
- throw;
- }
- return File;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::CustomReadFile(const UnicodeString FileName,
- TRemoteFile *& File, TRemoteFile * ALinkedByFile)
- {
- File = NULL;
- int Params = ecDefault |
- FLAGMASK(FTerminal->SessionData->IgnoreLsWarnings, ecIgnoreWarnings);
- // the auto-detection of --full-time support is not implemented for fsListFile,
- // so we use it only if we already know that it is supported (asOn).
- const wchar_t * Options = (FLsFullTime == asOn) ? FullTimeOption : L"";
- ExecCommand(fsListFile,
- ARRAYOFCONST((FTerminal->SessionData->ListingCommand, Options, DelimitStr(FileName))),
- Params);
- if (FOutput->Count)
- {
- int LineIndex = 0;
- if (IsTotalListingLine(FOutput->Strings[LineIndex]) && FOutput->Count > 1)
- {
- LineIndex++;
- }
- File = CreateRemoteFile(FOutput->Strings[LineIndex], ALinkedByFile);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::DeleteFile(const UnicodeString FileName,
- const TRemoteFile * File, int Params, TRmSessionAction & Action)
- {
- USEDPARAM(File);
- USEDPARAM(Params);
- Action.Recursive();
- assert(FLAGCLEAR(Params, dfNoRecursive) || (File && File->IsSymLink));
- ExecCommand(fsDeleteFile, ARRAYOFCONST((DelimitStr(FileName))));
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::RenameFile(const UnicodeString FileName,
- const UnicodeString NewName)
- {
- ExecCommand(fsRenameFile, ARRAYOFCONST((DelimitStr(FileName), DelimitStr(NewName))));
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::CopyFile(const UnicodeString FileName,
- const UnicodeString NewName)
- {
- ExecCommand(fsCopyFile, ARRAYOFCONST((DelimitStr(FileName), DelimitStr(NewName))));
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::CreateDirectory(const UnicodeString DirName)
- {
- ExecCommand(fsCreateDirectory, ARRAYOFCONST((DelimitStr(DirName))));
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::CreateLink(const UnicodeString FileName,
- const UnicodeString PointTo, bool Symbolic)
- {
- ExecCommand(fsCreateLink,
- ARRAYOFCONST((Symbolic ? L"-s" : L"", DelimitStr(PointTo), DelimitStr(FileName))));
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::ChangeFileToken(const UnicodeString & DelimitedName,
- const TRemoteToken & Token, TFSCommand Cmd, const UnicodeString & RecursiveStr)
- {
- UnicodeString Str;
- if (Token.IDValid)
- {
- Str = IntToStr(int(Token.ID));
- }
- else if (Token.NameValid)
- {
- Str = Token.Name;
- }
- if (!Str.IsEmpty())
- {
- ExecCommand(Cmd, ARRAYOFCONST((RecursiveStr, Str, DelimitedName)));
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::ChangeFileProperties(const UnicodeString FileName,
- const TRemoteFile * File, const TRemoteProperties * Properties,
- TChmodSessionAction & Action)
- {
- assert(Properties);
- bool IsDirectory = File && File->IsDirectory;
- bool Recursive = Properties->Recursive && IsDirectory;
- UnicodeString RecursiveStr = Recursive ? L"-R" : L"";
- UnicodeString DelimitedName = DelimitStr(FileName);
- // change group before permissions as chgrp change permissions
- if (Properties->Valid.Contains(vpGroup))
- {
- ChangeFileToken(DelimitedName, Properties->Group, fsChangeGroup, RecursiveStr);
- }
- if (Properties->Valid.Contains(vpOwner))
- {
- ChangeFileToken(DelimitedName, Properties->Owner, fsChangeOwner, RecursiveStr);
- }
- if (Properties->Valid.Contains(vpRights))
- {
- TRights Rights = Properties->Rights;
- // if we don't set modes recursively, we may add X at once with other
- // options. Otherwise we have to add X after recursive command
- if (!Recursive && IsDirectory && Properties->AddXToDirectories)
- Rights.AddExecute();
- Action.Rights(Rights);
- if (Recursive)
- {
- Action.Recursive();
- }
- if ((Rights.NumberSet | Rights.NumberUnset) != TRights::rfNo)
- {
- ExecCommand(fsChangeMode,
- ARRAYOFCONST((RecursiveStr, Rights.SimplestStr, DelimitedName)));
- }
- // if file is directory and we do recursive mode settings with
- // add-x-to-directories option on, add those X
- if (Recursive && IsDirectory && Properties->AddXToDirectories)
- {
- Rights.AddExecute();
- ExecCommand(fsChangeMode,
- ARRAYOFCONST((L"", Rights.SimplestStr, DelimitedName)));
- }
- }
- else
- {
- Action.Cancel();
- }
- assert(!Properties->Valid.Contains(vpLastAccess));
- assert(!Properties->Valid.Contains(vpModification));
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSCPFileSystem::LoadFilesProperties(TStrings * /*FileList*/ )
- {
- assert(false);
- return false;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::CalculateFilesChecksum(const UnicodeString & /*Alg*/,
- TStrings * /*FileList*/, TStrings * /*Checksums*/,
- TCalculatedChecksumEvent /*OnCalculatedChecksum*/)
- {
- assert(false);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::CustomCommandOnFile(const UnicodeString FileName,
- const TRemoteFile * File, UnicodeString Command, int Params,
- TCaptureOutputEvent OutputEvent)
- {
- assert(File);
- bool Dir = File->IsDirectory && !File->IsSymLink;
- if (Dir && (Params & ccRecursive))
- {
- TCustomCommandParams AParams;
- AParams.Command = Command;
- AParams.Params = Params;
- AParams.OutputEvent = OutputEvent;
- FTerminal->ProcessDirectory(FileName, FTerminal->CustomCommandOnFile,
- &AParams);
- }
- if (!Dir || (Params & ccApplyToDirectories))
- {
- TCustomCommandData Data(FTerminal);
- UnicodeString Cmd = TRemoteCustomCommand(
- Data, FTerminal->CurrentDirectory, FileName, L"").
- Complete(Command, true);
- AnyCommand(Cmd, OutputEvent);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::CaptureOutput(const UnicodeString & AddedLine, bool StdError)
- {
- int ReturnCode;
- UnicodeString Line = AddedLine;
- if (StdError ||
- !RemoveLastLine(Line, ReturnCode) ||
- !Line.IsEmpty())
- {
- assert(FOnCaptureOutput != NULL);
- FOnCaptureOutput(Line, StdError);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::AnyCommand(const UnicodeString Command,
- TCaptureOutputEvent OutputEvent)
- {
- assert(FSecureShell->OnCaptureOutput == NULL);
- if (OutputEvent != NULL)
- {
- FSecureShell->OnCaptureOutput = CaptureOutput;
- FOnCaptureOutput = OutputEvent;
- }
- try
- {
- ExecCommand(fsAnyCommand, ARRAYOFCONST((Command)),
- ecDefault | ecIgnoreWarnings);
- }
- __finally
- {
- FOnCaptureOutput = NULL;
- FSecureShell->OnCaptureOutput = NULL;
- }
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TSCPFileSystem::FileUrl(const UnicodeString FileName)
- {
- return FTerminal->FileUrl(ScpProtocol, FileName);
- }
- //---------------------------------------------------------------------------
- TStrings * __fastcall TSCPFileSystem::GetFixedPaths()
- {
- return NULL;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::SpaceAvailable(const UnicodeString Path,
- TSpaceAvailable & /*ASpaceAvailable*/)
- {
- assert(false);
- }
- //---------------------------------------------------------------------------
- // transfer protocol
- //---------------------------------------------------------------------------
- unsigned int __fastcall TSCPFileSystem::ConfirmOverwrite(
- UnicodeString & FileName, TOperationSide Side,
- const TOverwriteFileParams * FileParams, const TCopyParamType * CopyParam,
- int Params, TFileOperationProgressType * OperationProgress)
- {
- TQueryButtonAlias Aliases[3];
- Aliases[0].Button = qaAll;
- Aliases[0].Alias = LoadStr(YES_TO_NEWER_BUTTON);
- Aliases[0].GroupWith = qaYes;
- Aliases[0].GrouppedShiftState = TShiftState() << ssCtrl;
- Aliases[1].Button = qaYesToAll;
- Aliases[1].GroupWith = qaYes;
- Aliases[1].GrouppedShiftState = TShiftState() << ssShift;
- Aliases[2].Button = qaNoToAll;
- Aliases[2].GroupWith = qaNo;
- Aliases[2].GrouppedShiftState = TShiftState() << ssShift;
- TQueryParams QueryParams(qpNeverAskAgainCheck);
- QueryParams.Aliases = Aliases;
- QueryParams.AliasesCount = LENOF(Aliases);
- unsigned int Answer;
- SUSPEND_OPERATION
- (
- Answer = FTerminal->ConfirmFileOverwrite(
- FileName, FileParams,
- qaYes | qaNo | qaCancel | qaYesToAll | qaNoToAll | qaAll,
- &QueryParams, Side, CopyParam, Params, OperationProgress);
- );
- return Answer;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::SCPResponse(bool * GotLastLine)
- {
- // Taken from scp.c response() and modified
- unsigned char Resp;
- FSecureShell->Receive(&Resp, 1);
- switch (Resp)
- {
- case 0: /* ok */
- FTerminal->LogEvent(L"SCP remote side confirmation (0)");
- return;
- default:
- case 1: /* error */
- case 2: /* fatal error */
- // pscp adds 'Resp' to 'Msg', why?
- UnicodeString Msg = FSecureShell->ReceiveLine();
- UnicodeString Line = UnicodeString(static_cast<char>(Resp)) + Msg;
- if (IsLastLine(Line))
- {
- if (GotLastLine != NULL)
- {
- *GotLastLine = true;
- }
- /* TODO 1 : Show stderror to user? */
- FSecureShell->ClearStdError();
- try
- {
- ReadCommandOutput(coExpectNoOutput | coRaiseExcept | coOnlyReturnCode);
- }
- catch(...)
- {
- // when ReadCommandOutput() fails than remote SCP is terminated already
- if (GotLastLine != NULL)
- {
- *GotLastLine = true;
- }
- throw;
- }
- }
- else
- if (Resp == 1)
- {
- FTerminal->LogEvent(L"SCP remote side error (1):");
- }
- else
- {
- FTerminal->LogEvent(L"SCP remote side fatal error (2):");
- }
- if (Resp == 1)
- {
- THROW_FILE_SKIPPED(NULL, Msg);
- }
- else
- {
- THROW_SCP_ERROR(NULL, Msg);
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::CopyToRemote(TStrings * FilesToCopy,
- const UnicodeString TargetDir, const TCopyParamType * CopyParam,
- int Params, TFileOperationProgressType * OperationProgress,
- TOnceDoneOperation & OnceDoneOperation)
- {
- // scp.c: source(), toremote()
- assert(FilesToCopy && OperationProgress);
- Params &= ~(cpAppend | cpResume);
- UnicodeString Options = L"";
- bool CheckExistence = UnixComparePaths(TargetDir, FTerminal->CurrentDirectory) &&
- (FTerminal->FFiles != NULL) && FTerminal->FFiles->Loaded;
- bool CopyBatchStarted = false;
- bool Failed = true;
- bool GotLastLine = false;
- UnicodeString TargetDirFull = UnixIncludeTrailingBackslash(TargetDir);
- if (CopyParam->PreserveRights) Options = L"-p";
- if (FTerminal->SessionData->Scp1Compatibility) Options += L" -1";
- SendCommand(FCommandSet->FullCommand(fsCopyToRemote,
- ARRAYOFCONST((Options, DelimitStr(UnixExcludeTrailingBackslash(TargetDir))))));
- SkipFirstLine();
- try
- {
- try
- {
- SCPResponse(&GotLastLine);
- // This can happen only if SCP command is not executed and return code is 0
- // It has never happened to me (return code is usually 127)
- if (GotLastLine)
- {
- throw Exception(L"");
- }
- }
- catch(Exception & E)
- {
- if (GotLastLine && FTerminal->Active)
- {
- FTerminal->TerminalError(&E, LoadStr(SCP_INIT_ERROR));
- }
- else
- {
- throw;
- }
- }
- CopyBatchStarted = true;
- for (int IFile = 0; (IFile < FilesToCopy->Count) &&
- !OperationProgress->Cancel; IFile++)
- {
- UnicodeString FileName = FilesToCopy->Strings[IFile];
- bool CanProceed;
- UnicodeString FileNameOnly =
- CopyParam->ChangeFileName(ExtractFileName(FileName), osLocal, true);
- if (CheckExistence)
- {
- // previously there was assertion on FTerminal->FFiles->Loaded, but it
- // fails for scripting, if 'ls' is not issued before.
- // formally we should call CheckRemoteFile here but as checking is for
- // free here (almost) ...
- TRemoteFile * File = FTerminal->FFiles->FindFile(FileNameOnly);
- if (File != NULL)
- {
- unsigned int Answer;
- if (File->IsDirectory)
- {
- UnicodeString Message = FMTLOAD(DIRECTORY_OVERWRITE, (FileNameOnly));
- TQueryParams QueryParams(qpNeverAskAgainCheck);
- SUSPEND_OPERATION
- (
- Answer = FTerminal->ConfirmFileOverwrite(
- FileNameOnly /*not used*/, NULL,
- qaYes | qaNo | qaCancel | qaYesToAll | qaNoToAll,
- &QueryParams, osRemote, CopyParam, Params, OperationProgress, Message);
- );
- }
- else
- {
- __int64 MTime;
- TOverwriteFileParams FileParams;
- FTerminal->OpenLocalFile(FileName, GENERIC_READ,
- NULL, NULL, NULL, &MTime, NULL,
- &FileParams.SourceSize);
- FileParams.SourceTimestamp = UnixToDateTime(MTime,
- FTerminal->SessionData->DSTMode);
- FileParams.DestSize = File->Size;
- FileParams.DestTimestamp = File->Modification;
- Answer = ConfirmOverwrite(FileNameOnly, osRemote,
- &FileParams, CopyParam, Params, OperationProgress);
- }
- switch (Answer)
- {
- case qaYes:
- CanProceed = true;
- break;
- case qaCancel:
- if (!OperationProgress->Cancel) OperationProgress->Cancel = csCancel;
- case qaNo:
- CanProceed = false;
- break;
- default:
- assert(false);
- break;
- }
- }
- else
- {
- CanProceed = true;
- }
- }
- else
- {
- CanProceed = true;
- }
- if (CanProceed)
- {
- if (FTerminal->SessionData->CacheDirectories)
- {
- FTerminal->DirectoryModified(TargetDir, false);
- if (DirectoryExists(FileName))
- {
- FTerminal->DirectoryModified(UnixIncludeTrailingBackslash(TargetDir)+
- FileNameOnly, true);
- }
- }
- try
- {
- SCPSource(FileName, TargetDirFull,
- CopyParam, Params, OperationProgress, 0);
- OperationProgress->Finish(FileName, true, OnceDoneOperation);
- }
- catch (EScpFileSkipped &E)
- {
- TQueryParams Params(qpAllowContinueOnError);
- SUSPEND_OPERATION (
- if (FTerminal->QueryUserException(FMTLOAD(COPY_ERROR, (FileName)), &E,
- qaOK | qaAbort, &Params, qtError) == qaAbort)
- {
- OperationProgress->Cancel = csCancel;
- }
- OperationProgress->Finish(FileName, false, OnceDoneOperation);
- if (!FTerminal->HandleException(&E)) throw;
- );
- }
- catch (EScpSkipFile &E)
- {
- OperationProgress->Finish(FileName, false, OnceDoneOperation);
- // If ESkipFile occurs, just log it and continue with next file
- SUSPEND_OPERATION (
- if (!FTerminal->HandleException(&E)) throw;
- );
- }
- catch (...)
- {
- OperationProgress->Finish(FileName, false, OnceDoneOperation);
- throw;
- }
- }
- }
- Failed = false;
- }
- __finally
- {
- // Tell remote side, that we're done.
- if (FTerminal->Active)
- {
- try
- {
- if (!GotLastLine)
- {
- if (CopyBatchStarted)
- {
- // What about case, remote side sends fatal error ???
- // (Not sure, if it causes remote side to terminate scp)
- FSecureShell->SendLine(L"E");
- SCPResponse();
- }
- /* TODO 1 : Show stderror to user? */
- FSecureShell->ClearStdError();
- ReadCommandOutput(coExpectNoOutput | coWaitForLastLine | coOnlyReturnCode |
- (Failed ? 0 : coRaiseExcept));
- }
- }
- catch (Exception &E)
- {
- // Only log error message (it should always succeed, but
- // some pending error maybe in queue) }
- FTerminal->Log->AddException(&E);
- }
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::SCPSource(const UnicodeString FileName,
- const UnicodeString TargetDir, const TCopyParamType * CopyParam, int Params,
- TFileOperationProgressType * OperationProgress, int Level)
- {
- UnicodeString DestFileName = CopyParam->ChangeFileName(
- ExtractFileName(FileName), osLocal, Level == 0);
- FTerminal->LogEvent(FORMAT(L"File: \"%s\"", (FileName)));
- OperationProgress->SetFile(FileName, false);
- if (!FTerminal->AllowLocalFileTransfer(FileName, CopyParam))
- {
- FTerminal->LogEvent(FORMAT(L"File \"%s\" excluded from transfer", (FileName)));
- THROW_SKIP_FILE_NULL;
- }
- HANDLE File;
- int Attrs;
- __int64 MTime, ATime;
- __int64 Size;
- FTerminal->OpenLocalFile(FileName, GENERIC_READ,
- &Attrs, &File, NULL, &MTime, &ATime, &Size);
- bool Dir = FLAGSET(Attrs, faDirectory);
- TStream * Stream = new TSafeHandleStream((THandle)File);
- try
- {
- OperationProgress->SetFileInProgress();
- if (Dir)
- {
- SCPDirectorySource(FileName, TargetDir, CopyParam, Params, OperationProgress, Level);
- }
- else
- {
- UnicodeString AbsoluteFileName = FTerminal->AbsolutePath(TargetDir + DestFileName, false);
- assert(File);
- // File is regular file (not directory)
- FTerminal->LogEvent(FORMAT(L"Copying \"%s\" to remote directory started.", (FileName)));
- OperationProgress->SetLocalSize(Size);
- // Suppose same data size to transfer as to read
- // (not true with ASCII transfer)
- OperationProgress->SetTransferSize(OperationProgress->LocalSize);
- OperationProgress->TransferingFile = false;
- TDateTime Modification = UnixToDateTime(MTime, FTerminal->SessionData->DSTMode);
- // Will we use ASCII of BINARY file transfer?
- TFileMasks::TParams MaskParams;
- MaskParams.Size = Size;
- MaskParams.Modification = Modification;
- OperationProgress->SetAsciiTransfer(
- CopyParam->UseAsciiTransfer(FileName, osLocal, MaskParams));
- FTerminal->LogEvent(
- UnicodeString((OperationProgress->AsciiTransfer ? L"Ascii" : L"Binary")) +
- L" transfer mode selected.");
- TUploadSessionAction Action(FTerminal->ActionLog);
- Action.FileName(ExpandUNCFileName(FileName));
- Action.Destination(AbsoluteFileName);
- TRights Rights = CopyParam->RemoteFileRights(Attrs);
- try
- {
- // During ASCII transfer we will load whole file to this buffer
- // than convert EOL and send it at once, because before converting EOL
- // we can't know its size
- TFileBuffer AsciiBuf;
- bool ConvertToken = false;
- do
- {
- // Buffer for one block of data
- TFileBuffer BlockBuf;
- // This is crucial, if it fails during file transfer, it's fatal error
- FILE_OPERATION_LOOP_EX (!OperationProgress->TransferingFile,
- FMTLOAD(READ_ERROR, (FileName)),
- BlockBuf.LoadStream(Stream, OperationProgress->LocalBlockSize(), true);
- );
- OperationProgress->AddLocallyUsed(BlockBuf.Size);
- // We do ASCII transfer: convert EOL of current block
- // (we don't convert whole buffer, cause it would produce
- // huge memory-transfers while inserting/deleting EOL characters)
- // Than we add current block to file buffer
- if (OperationProgress->AsciiTransfer)
- {
- int ConvertParams =
- FLAGMASK(CopyParam->RemoveCtrlZ, cpRemoveCtrlZ) |
- FLAGMASK(CopyParam->RemoveBOM, cpRemoveBOM);
- BlockBuf.Convert(FTerminal->Configuration->LocalEOLType,
- FTerminal->SessionData->EOLType,
- ConvertParams, ConvertToken);
- BlockBuf.Memory->Seek(0, soFromBeginning);
- AsciiBuf.ReadStream(BlockBuf.Memory, BlockBuf.Size, true);
- // We don't need it any more
- BlockBuf.Memory->Clear();
- // Calculate total size to sent (assume that ratio between
- // size of source and size of EOL-transformed data would remain same)
- // First check if file contains anything (div by zero!)
- if (OperationProgress->LocallyUsed)
- {
- __int64 X = OperationProgress->LocalSize;
- X *= AsciiBuf.Size;
- X /= OperationProgress->LocallyUsed;
- OperationProgress->ChangeTransferSize(X);
- }
- else
- {
- OperationProgress->ChangeTransferSize(0);
- }
- }
- // We send file information on first pass during BINARY transfer
- // and on last pass during ASCII transfer
- // BINARY: We succeeded reading first buffer from file, hopefully
- // we will be able to read whole, so we send file info to remote side
- // This is done, because when reading fails we can't interrupt sending
- // (don't know how to tell other side that it failed)
- if (!OperationProgress->TransferingFile &&
- (!OperationProgress->AsciiTransfer || OperationProgress->IsLocallyDone()))
- {
- UnicodeString Buf;
- if (CopyParam->PreserveTime)
- {
- // Send last file access and modification time
- // TVarRec don't understand 'unsigned int' -> we use sprintf()
- Buf.sprintf(L"T%lu 0 %lu 0", static_cast<unsigned long>(MTime),
- static_cast<unsigned long>(ATime));
- FSecureShell->SendLine(Buf);
- SCPResponse();
- }
- // Send file modes (rights), filesize and file name
- // TVarRec don't understand 'unsigned int' -> we use sprintf()
- Buf.sprintf(L"C%s %Ld %s",
- Rights.Octal.data(),
- (OperationProgress->AsciiTransfer ? (__int64)AsciiBuf.Size :
- OperationProgress->LocalSize),
- DestFileName.data());
- FSecureShell->SendLine(Buf);
- SCPResponse();
- // Indicate we started transferring file, we need to finish it
- // If not, it's fatal error
- OperationProgress->TransferingFile = true;
- // If we're doing ASCII transfer, this is last pass
- // so we send whole file
- /* TODO : We can't send file above 32bit size in ASCII mode! */
- if (OperationProgress->AsciiTransfer)
- {
- FTerminal->LogEvent(FORMAT(L"Sending ASCII data (%u bytes)",
- (AsciiBuf.Size)));
- // Should be equal, just in case it's rounded (see above)
- OperationProgress->ChangeTransferSize(AsciiBuf.Size);
- while (!OperationProgress->IsTransferDone())
- {
- unsigned long BlockSize = OperationProgress->TransferBlockSize();
- FSecureShell->Send(
- reinterpret_cast<unsigned char *>(AsciiBuf.Data + (unsigned int)OperationProgress->TransferedSize),
- BlockSize);
- OperationProgress->AddTransfered(BlockSize);
- if (OperationProgress->Cancel == csCancelTransfer)
- {
- throw Exception(USER_TERMINATED);
- }
- }
- }
- }
- // At end of BINARY transfer pass, send current block
- if (!OperationProgress->AsciiTransfer)
- {
- if (!OperationProgress->TransferedSize)
- {
- FTerminal->LogEvent(FORMAT(L"Sending BINARY data (first block, %u bytes)",
- (BlockBuf.Size)));
- }
- else if (FTerminal->Configuration->ActualLogProtocol >= 1)
- {
- FTerminal->LogEvent(FORMAT(L"Sending BINARY data (%u bytes)",
- (BlockBuf.Size)));
- }
- FSecureShell->Send(reinterpret_cast<const unsigned char *>(BlockBuf.Data), BlockBuf.Size);
- OperationProgress->AddTransfered(BlockBuf.Size);
- }
- if ((OperationProgress->Cancel == csCancelTransfer) ||
- (OperationProgress->Cancel == csCancel && !OperationProgress->TransferingFile))
- {
- throw Exception(USER_TERMINATED);
- }
- }
- while (!OperationProgress->IsLocallyDone() || !OperationProgress->IsTransferDone());
- FSecureShell->SendNull();
- try
- {
- SCPResponse();
- // If one of two following exceptions occurs, it means, that remote
- // side already know, that file transfer finished, even if it failed
- // so we don't have to throw EFatal
- }
- catch (EScp &E)
- {
- // SCP protocol fatal error
- OperationProgress->TransferingFile = false;
- throw;
- }
- catch (EScpFileSkipped &E)
- {
- // SCP protocol non-fatal error
- OperationProgress->TransferingFile = false;
- throw;
- }
- // We succeeded transferring file, from now we can handle exceptions
- // normally -> no fatal error
- OperationProgress->TransferingFile = false;
- }
- catch (Exception &E)
- {
- // EScpFileSkipped is derived from EScpSkipFile,
- // but is does not indicate file skipped by user here
- if (dynamic_cast<EScpFileSkipped *>(&E) != NULL)
- {
- Action.Rollback(&E);
- }
- else
- {
- FTerminal->RollbackAction(Action, OperationProgress, &E);
- }
- // Every exception during file transfer is fatal
- if (OperationProgress->TransferingFile)
- {
- FTerminal->FatalError(&E, FMTLOAD(COPY_FATAL, (FileName)));
- }
- else
- {
- throw;
- }
- }
- // With SCP we are not able to distinguish reason for failure
- // (upload itself, touch or chmod).
- // So we always report error with upload action and
- // log touch and chmod actions only if upload succeeds.
- if (CopyParam->PreserveTime)
- {
- TTouchSessionAction(FTerminal->ActionLog, AbsoluteFileName, Modification);
- }
- if (CopyParam->PreserveRights)
- {
- TChmodSessionAction(FTerminal->ActionLog, AbsoluteFileName,
- Rights);
- }
- }
- }
- __finally
- {
- if (File != NULL)
- {
- CloseHandle(File);
- }
- delete Stream;
- }
- /* TODO : Delete also read-only files. */
- if (FLAGSET(Params, cpDelete))
- {
- if (!Dir)
- {
- FILE_OPERATION_LOOP (FMTLOAD(DELETE_LOCAL_FILE_ERROR, (FileName)),
- THROWOSIFFALSE(Sysutils::DeleteFile(FileName));
- )
- }
- }
- else if (CopyParam->ClearArchive && FLAGSET(Attrs, faArchive))
- {
- FILE_OPERATION_LOOP (FMTLOAD(CANT_SET_ATTRS, (FileName)),
- THROWOSIFFALSE(FileSetAttr(FileName, Attrs & ~faArchive) == 0);
- )
- }
- FTerminal->LogEvent(FORMAT(L"Copying \"%s\" to remote directory finished.", (FileName)));
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::SCPDirectorySource(const UnicodeString DirectoryName,
- const UnicodeString TargetDir, const TCopyParamType * CopyParam, int Params,
- TFileOperationProgressType * OperationProgress, int Level)
- {
- int Attrs;
- FTerminal->LogEvent(FORMAT(L"Entering directory \"%s\".", (DirectoryName)));
- OperationProgress->SetFile(DirectoryName);
- UnicodeString DestFileName = CopyParam->ChangeFileName(
- ExtractFileName(DirectoryName), osLocal, Level == 0);
- // Get directory attributes
- FILE_OPERATION_LOOP (FMTLOAD(CANT_GET_ATTRS, (DirectoryName)),
- Attrs = FileGetAttr(DirectoryName);
- if (Attrs == -1) RaiseLastOSError();
- )
- UnicodeString TargetDirFull = UnixIncludeTrailingBackslash(TargetDir + DestFileName);
- UnicodeString Buf;
- /* TODO 1: maybe send filetime */
- // Send directory modes (rights), filesize and file name
- Buf = FORMAT(L"D%s 0 %s",
- (CopyParam->RemoteFileRights(Attrs).Octal, DestFileName));
- FSecureShell->SendLine(Buf);
- SCPResponse();
- try
- {
- int FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
- TSearchRec SearchRec;
- bool FindOK;
- FILE_OPERATION_LOOP (FMTLOAD(LIST_DIR_ERROR, (DirectoryName)),
- FindOK = (bool)(FindFirstChecked(IncludeTrailingBackslash(DirectoryName) + L"*.*",
- FindAttrs, SearchRec) == 0);
- );
- try
- {
- while (FindOK && !OperationProgress->Cancel)
- {
- UnicodeString FileName = IncludeTrailingBackslash(DirectoryName) + SearchRec.Name;
- try
- {
- if ((SearchRec.Name != L".") && (SearchRec.Name != L".."))
- {
- SCPSource(FileName, TargetDirFull, CopyParam, Params, OperationProgress, Level + 1);
- }
- }
- // Previously we caught EScpSkipFile, making error being displayed
- // even when file was excluded by mask. Now the EScpSkipFile is special
- // case without error message.
- catch (EScpFileSkipped &E)
- {
- TQueryParams Params(qpAllowContinueOnError);
- SUSPEND_OPERATION (
- if (FTerminal->QueryUserException(FMTLOAD(COPY_ERROR, (FileName)), &E,
- qaOK | qaAbort, &Params, qtError) == qaAbort)
- {
- OperationProgress->Cancel = csCancel;
- }
- if (!FTerminal->HandleException(&E)) throw;
- );
- }
- catch (EScpSkipFile &E)
- {
- // If ESkipFile occurs, just log it and continue with next file
- SUSPEND_OPERATION (
- if (!FTerminal->HandleException(&E)) throw;
- );
- }
- FILE_OPERATION_LOOP (FMTLOAD(LIST_DIR_ERROR, (DirectoryName)),
- FindOK = (FindNextChecked(SearchRec) == 0);
- );
- }
- }
- __finally
- {
- FindClose(SearchRec);
- }
- /* TODO : Delete also read-only directories. */
- /* TODO : Show error message on failure. */
- if (!OperationProgress->Cancel)
- {
- if (FLAGSET(Params, cpDelete))
- {
- RemoveDir(DirectoryName);
- }
- else if (CopyParam->ClearArchive && FLAGSET(Attrs, faArchive))
- {
- FILE_OPERATION_LOOP (FMTLOAD(CANT_SET_ATTRS, (DirectoryName)),
- THROWOSIFFALSE(FileSetAttr(DirectoryName, Attrs & ~faArchive) == 0);
- )
- }
- }
- }
- __finally
- {
- if (FTerminal->Active)
- {
- // Tell remote side, that we're done.
- FTerminal->LogEvent(FORMAT(L"Leaving directory \"%s\".", (DirectoryName)));
- FSecureShell->SendLine(L"E");
- SCPResponse();
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::CopyToLocal(TStrings * FilesToCopy,
- const UnicodeString TargetDir, const TCopyParamType * CopyParam,
- int Params, TFileOperationProgressType * OperationProgress,
- TOnceDoneOperation & OnceDoneOperation)
- {
- bool CloseSCP = False;
- Params &= ~(cpAppend | cpResume);
- UnicodeString Options = L"";
- if (CopyParam->PreserveRights || CopyParam->PreserveTime) Options = L"-p";
- if (FTerminal->SessionData->Scp1Compatibility) Options += L" -1";
- FTerminal->LogEvent(FORMAT(L"Copying %d files/directories to local directory "
- "\"%s\"", (FilesToCopy->Count, TargetDir)));
- FTerminal->LogEvent(CopyParam->LogStr);
- try
- {
- for (int IFile = 0; (IFile < FilesToCopy->Count) &&
- !OperationProgress->Cancel; IFile++)
- {
- UnicodeString FileName = FilesToCopy->Strings[IFile];
- TRemoteFile * File = (TRemoteFile *)FilesToCopy->Objects[IFile];
- assert(File);
- try
- {
- bool Success = true; // Have to be set to True (see ::SCPSink)
- SendCommand(FCommandSet->FullCommand(fsCopyToLocal,
- ARRAYOFCONST((Options, DelimitStr(FileName)))));
- SkipFirstLine();
- // Filename is used for error messaging and excluding files only
- // Send in full path to allow path-based excluding
- UnicodeString FullFileName = UnixExcludeTrailingBackslash(File->FullFileName);
- SCPSink(TargetDir, FullFileName, UnixExtractFilePath(FullFileName),
- CopyParam, Success, OperationProgress, Params, 0);
- // operation succeeded (no exception), so it's ok that
- // remote side closed SCP, but we continue with next file
- if (OperationProgress->Cancel == csRemoteAbort)
- {
- OperationProgress->Cancel = csContinue;
- }
- // Move operation -> delete file/directory afterwards
- // but only if copying succeeded
- if ((Params & cpDelete) && Success && !OperationProgress->Cancel)
- {
- try
- {
- FTerminal->ExceptionOnFail = true;
- try
- {
- FILE_OPERATION_LOOP(FMTLOAD(DELETE_FILE_ERROR, (FileName)),
- // pass full file name in FileName, in case we are not moving
- // from current directory
- FTerminal->DeleteFile(FileName, File)
- );
- }
- __finally
- {
- FTerminal->ExceptionOnFail = false;
- }
- }
- catch (EFatal &E)
- {
- throw;
- }
- catch (...)
- {
- // If user selects skip (or abort), nothing special actually occurs
- // we just run DoFinished with Success = False, so file won't
- // be deselected in panel (depends on assigned event handler)
- // On csCancel we would later try to close remote SCP, but it
- // is closed already
- if (OperationProgress->Cancel == csCancel)
- {
- OperationProgress->Cancel = csRemoteAbort;
- }
- Success = false;
- }
- }
- OperationProgress->Finish(FileName,
- (!OperationProgress->Cancel && Success), OnceDoneOperation);
- }
- catch (...)
- {
- OperationProgress->Finish(FileName, false, OnceDoneOperation);
- CloseSCP = (OperationProgress->Cancel != csRemoteAbort);
- throw;
- }
- }
- }
- __finally
- {
- // In case that copying doesn't cause fatal error (ie. connection is
- // still active) but wasn't successful (exception or user termination)
- // we need to ensure, that SCP on remote side is closed
- if (FTerminal->Active && (CloseSCP ||
- (OperationProgress->Cancel == csCancel) ||
- (OperationProgress->Cancel == csCancelTransfer)))
- {
- bool LastLineRead;
- // If we get LastLine, it means that remote side 'scp' is already
- // terminated, so we need not to terminate it. There is also
- // possibility that remote side waits for confirmation, so it will hang.
- // This should not happen (hope)
- UnicodeString Line = FSecureShell->ReceiveLine();
- LastLineRead = IsLastLine(Line);
- if (!LastLineRead)
- {
- SCPSendError((OperationProgress->Cancel ? L"Terminated by user." : L"Exception"), true);
- }
- // Just in case, remote side already sent some more data (it's probable)
- // but we don't want to raise exception (user asked to terminate, it's not error)
- int ECParams = coOnlyReturnCode;
- if (!LastLineRead) ECParams |= coWaitForLastLine;
- ReadCommandOutput(ECParams);
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::SCPError(const UnicodeString Message, bool Fatal)
- {
- SCPSendError(Message, Fatal);
- THROW_FILE_SKIPPED(NULL, Message);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::SCPSendError(const UnicodeString Message, bool Fatal)
- {
- unsigned char ErrorLevel = (char)(Fatal ? 2 : 1);
- FTerminal->LogEvent(FORMAT(L"Sending SCP error (%d) to remote side:",
- ((int)ErrorLevel)));
- FSecureShell->Send(&ErrorLevel, 1);
- // We don't send exact error message, because some unspecified
- // characters can terminate remote scp
- FSecureShell->SendLine(L"scp: error");
- }
- //---------------------------------------------------------------------------
- void __fastcall TSCPFileSystem::SCPSink(const UnicodeString TargetDir,
- const UnicodeString FileName, const UnicodeString SourceDir,
- const TCopyParamType * CopyParam, bool & Success,
- TFileOperationProgressType * OperationProgress, int Params,
- int Level)
- {
- struct
- {
- int SetTime;
- FILETIME AcTime;
- FILETIME WrTime;
- TRights RemoteRights;
- int Attrs;
- bool Exists;
- } FileData;
- TDateTime SourceTimestamp;
- bool SkipConfirmed = false;
- bool Initialized = (Level > 0);
- FileData.SetTime = 0;
- FSecureShell->SendNull();
- while (!OperationProgress->Cancel)
- {
- // See (switch ... case 'T':)
- if (FileData.SetTime) FileData.SetTime--;
- // In case of error occurred before control record arrived.
- // We can finally use full path here, as we get current path in FileName param
- // (we used to set the file into OperationProgress->FileName, but it collided
- // with progress outputting, particularly for scripting)
- UnicodeString AbsoluteFileName = FileName;
- try
- {
- // Receive control record
- UnicodeString Line = FSecureShell->ReceiveLine();
- if (Line.Length() == 0) FTerminal->FatalError(NULL, LoadStr(SCP_EMPTY_LINE));
- if (IsLastLine(Line))
- {
- // Remote side finished copying, so remote SCP was closed
- // and we don't need to terminate it manually, see CopyToLocal()
- OperationProgress->Cancel = csRemoteAbort;
- /* TODO 1 : Show stderror to user? */
- FSecureShell->ClearStdError();
- try
- {
- // coIgnoreWarnings should allow batch transfer to continue when
- // download of one the files fails (user denies overwriting
- // of target local file, no read permissions...)
- ReadCommandOutput(coExpectNoOutput | coRaiseExcept |
- coOnlyReturnCode | coIgnoreWarnings);
- if (!Initialized)
- {
- throw Exception(L"");
- }
- }
- catch(Exception & E)
- {
- if (!Initialized && FTerminal->Active)
- {
- FTerminal->TerminalError(&E, LoadStr(SCP_INIT_ERROR));
- }
- else
- {
- throw;
- }
- }
- return;
- }
- else
- {
- Initialized = true;
- // First character distinguish type of control record
- wchar_t Ctrl = Line[1];
- Line.Delete(1, 1);
- switch (Ctrl) {
- case 1:
- // Error (already logged by ReceiveLine())
- THROW_FILE_SKIPPED(NULL, FMTLOAD(REMOTE_ERROR, (Line)));
- case 2:
- // Fatal error, terminate copying
- FTerminal->TerminalError(Line);
- return; // Unreachable
- case L'E': // Exit
- FSecureShell->SendNull();
- return;
- case L'T':
- unsigned long MTime, ATime;
- if (swscanf(Line.c_str(), L"%ld %*d %ld %*d", &MTime, &ATime) == 2)
- {
- FileData.AcTime = DateTimeToFileTime(UnixToDateTime(ATime,
- FTerminal->SessionData->DSTMode), FTerminal->SessionData->DSTMode);
- FileData.WrTime = DateTimeToFileTime(UnixToDateTime(MTime,
- FTerminal->SessionData->DSTMode), FTerminal->SessionData->DSTMode);
- SourceTimestamp = UnixToDateTime(MTime,
- FTerminal->SessionData->DSTMode);
- FSecureShell->SendNull();
- // File time is only valid until next pass
- FileData.SetTime = 2;
- continue;
- }
- else
- {
- SCPError(LoadStr(SCP_ILLEGAL_TIME_FORMAT), False);
- }
- case L'C':
- case L'D':
- break; // continue pass switch{}
- default:
- FTerminal->FatalError(NULL, FMTLOAD(SCP_INVALID_CONTROL_RECORD, (Ctrl, Line)));
- }
- TFileMasks::TParams MaskParams;
- MaskParams.Modification = SourceTimestamp;
- // We reach this point only if control record was 'C' or 'D'
- try
- {
- FileData.RemoteRights.Octal = CutToChar(Line, L' ', True);
- // do not trim leading spaces of the filename
- __int64 TSize = StrToInt64(CutToChar(Line, L' ', False).TrimRight());
- MaskParams.Size = TSize;
- // Security fix: ensure the file ends up where we asked for it.
- // (accept only filename, not path)
- UnicodeString OnlyFileName = UnixExtractFileName(Line);
- if (Line != OnlyFileName)
- {
- FTerminal->LogEvent(FORMAT(L"Warning: Remote host set a compound pathname '%s'", (Line)));
- }
- OperationProgress->SetFile(OnlyFileName);
- AbsoluteFileName = SourceDir + OnlyFileName;
- OperationProgress->SetTransferSize(TSize);
- }
- catch (Exception &E)
- {
- SUSPEND_OPERATION (
- FTerminal->Log->AddException(&E);
- );
- SCPError(LoadStr(SCP_ILLEGAL_FILE_DESCRIPTOR), false);
- }
- // last possibility to cancel transfer before it starts
- if (OperationProgress->Cancel)
- {
- THROW_SKIP_FILE(NULL, LoadStr(USER_TERMINATED));
- }
- bool Dir = (Ctrl == L'D');
- UnicodeString SourceFullName = SourceDir + OperationProgress->FileName;
- if (!CopyParam->AllowTransfer(SourceFullName, osRemote, Dir, MaskParams))
- {
- FTerminal->LogEvent(FORMAT(L"File \"%s\" excluded from transfer",
- (AbsoluteFileName)));
- SkipConfirmed = true;
- SCPError(L"", false);
- }
- FTerminal->LogFileDetails(FileName, SourceTimestamp, MaskParams.Size);
- UnicodeString DestFileName =
- IncludeTrailingBackslash(TargetDir) +
- CopyParam->ChangeFileName(OperationProgress->FileName, osRemote,
- Level == 0);
- FileData.Attrs = FileGetAttr(DestFileName);
- // If getting attrs fails, we suppose, that file/folder doesn't exists
- FileData.Exists = (FileData.Attrs != -1);
- if (Dir)
- {
- if (FileData.Exists && !(FileData.Attrs & faDirectory))
- {
- SCPError(FMTLOAD(NOT_DIRECTORY_ERROR, (DestFileName)), false);
- }
- if (!FileData.Exists)
- {
- FILE_OPERATION_LOOP (FMTLOAD(CREATE_DIR_ERROR, (DestFileName)),
- THROWOSIFFALSE(ForceDirectories(DestFileName));
- );
- /* SCP: can we set the timestamp for directories ? */
- }
- UnicodeString FullFileName = SourceDir + OperationProgress->FileName;
- SCPSink(DestFileName, FullFileName, UnixIncludeTrailingBackslash(FullFileName),
- CopyParam, Success, OperationProgress, Params, Level + 1);
- continue;
- }
- else if (Ctrl == L'C')
- {
- TDownloadSessionAction Action(FTerminal->ActionLog);
- Action.FileName(AbsoluteFileName);
- try
- {
- HANDLE File = NULL;
- TStream * FileStream = NULL;
- /* TODO 1 : Turn off read-only attr */
- try
- {
- try
- {
- if (FileExists(DestFileName))
- {
- __int64 MTime;
- TOverwriteFileParams FileParams;
- FileParams.SourceSize = OperationProgress->TransferSize;
- FileParams.SourceTimestamp = SourceTimestamp;
- FTerminal->OpenLocalFile(DestFileName, GENERIC_READ,
- NULL, NULL, NULL, &MTime, NULL,
- &FileParams.DestSize);
- FileParams.DestTimestamp = UnixToDateTime(MTime,
- FTerminal->SessionData->DSTMode);
- unsigned int Answer =
- ConfirmOverwrite(OperationProgress->FileName, osLocal,
- &FileParams, CopyParam, Params, OperationProgress);
- switch (Answer)
- {
- case qaCancel:
- OperationProgress->Cancel = csCancel; // continue on next case
- case qaNo:
- SkipConfirmed = true;
- EXCEPTION;
- }
- }
- Action.Destination(DestFileName);
- if (!FTerminal->CreateLocalFile(DestFileName, OperationProgress,
- &File, FLAGSET(Params, cpNoConfirmation)))
- {
- SkipConfirmed = true;
- EXCEPTION;
- }
- FileStream = new TSafeHandleStream((THandle)File);
- }
- catch (Exception &E)
- {
- // In this step we can still cancel transfer, so we do it
- SCPError(E.Message, false);
- throw;
- }
- // We succeeded, so we confirm transfer to remote side
- FSecureShell->SendNull();
- // From now we need to finish file transfer, if not it's fatal error
- OperationProgress->TransferingFile = true;
- // Suppose same data size to transfer as to write
- // (not true with ASCII transfer)
- OperationProgress->SetLocalSize(OperationProgress->TransferSize);
- // Will we use ASCII of BINARY file transfer?
- OperationProgress->SetAsciiTransfer(
- CopyParam->UseAsciiTransfer(SourceFullName, osRemote, MaskParams));
- FTerminal->LogEvent(UnicodeString((OperationProgress->AsciiTransfer ? L"Ascii" : L"Binary")) +
- L" transfer mode selected.");
- try
- {
- // Buffer for one block of data
- TFileBuffer BlockBuf;
- bool ConvertToken = false;
- do
- {
- BlockBuf.Size = OperationProgress->TransferBlockSize();
- BlockBuf.Position = 0;
- FSecureShell->Receive(reinterpret_cast<unsigned char *>(BlockBuf.Data), BlockBuf.Size);
- OperationProgress->AddTransfered(BlockBuf.Size);
- if (OperationProgress->AsciiTransfer)
- {
- unsigned int PrevBlockSize = BlockBuf.Size;
- BlockBuf.Convert(FTerminal->SessionData->EOLType,
- FTerminal->Configuration->LocalEOLType, 0, ConvertToken);
- OperationProgress->SetLocalSize(
- OperationProgress->LocalSize - PrevBlockSize + BlockBuf.Size);
- }
- // This is crucial, if it fails during file transfer, it's fatal error
- FILE_OPERATION_LOOP_EX (false, FMTLOAD(WRITE_ERROR, (DestFileName)),
- BlockBuf.WriteToStream(FileStream, BlockBuf.Size);
- );
- OperationProgress->AddLocallyUsed(BlockBuf.Size);
- if (OperationProgress->Cancel == csCancelTransfer)
- {
- throw Exception(USER_TERMINATED);
- }
- }
- while (!OperationProgress->IsLocallyDone() || !
- OperationProgress->IsTransferDone());
- }
- catch (Exception &E)
- {
- // Every exception during file transfer is fatal
- FTerminal->FatalError(&E,
- FMTLOAD(COPY_FATAL, (OperationProgress->FileName)));
- }
- OperationProgress->TransferingFile = false;
- try
- {
- SCPResponse();
- // If one of following exception occurs, we still need
- // to send confirmation to other side
- }
- catch (EScp &E)
- {
- FSecureShell->SendNull();
- throw;
- }
- catch (EScpFileSkipped &E)
- {
- FSecureShell->SendNull();
- throw;
- }
- FSecureShell->SendNull();
- if (FileData.SetTime && CopyParam->PreserveTime)
- {
- SetFileTime(File, NULL, &FileData.AcTime, &FileData.WrTime);
- }
- }
- __finally
- {
- if (File) CloseHandle(File);
- if (FileStream) delete FileStream;
- }
- }
- catch(Exception & E)
- {
- if (SkipConfirmed)
- {
- Action.Cancel();
- }
- else
- {
- FTerminal->RollbackAction(Action, OperationProgress, &E);
- }
- throw;
- }
- if (FileData.Attrs == -1) FileData.Attrs = faArchive;
- int NewAttrs = CopyParam->LocalFileAttrs(FileData.RemoteRights);
- if ((NewAttrs & FileData.Attrs) != NewAttrs)
- {
- FILE_OPERATION_LOOP (FMTLOAD(CANT_SET_ATTRS, (DestFileName)),
- THROWOSIFFALSE(FileSetAttr(DestFileName, FileData.Attrs | NewAttrs) == 0);
- );
- }
- }
- }
- }
- catch (EScpFileSkipped &E)
- {
- if (!SkipConfirmed)
- {
- SUSPEND_OPERATION (
- TQueryParams Params(qpAllowContinueOnError);
- if (FTerminal->QueryUserException(FMTLOAD(COPY_ERROR, (AbsoluteFileName)),
- &E, qaOK | qaAbort, &Params, qtError) == qaAbort)
- {
- OperationProgress->Cancel = csCancel;
- }
- FTerminal->Log->AddException(&E);
- );
- }
- // this was inside above condition, but then transfer was considered
- // successful, even when for example user refused to overwrite file
- Success = false;
- }
- catch (EScpSkipFile &E)
- {
- SCPSendError(E.Message, false);
- Success = false;
- if (!FTerminal->HandleException(&E)) throw;
- }
- }
- }
|