| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include <stdio.h>
- #include "Common.h"
- #include "SessionInfo.h"
- #include "Exceptions.h"
- #include "TextsCore.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- AnsiString __fastcall XmlEscape(AnsiString Str)
- {
- for (int i = 1; i <= Str.Length(); i++)
- {
- const char * Repl = NULL;
- switch (Str[i])
- {
- case '&':
- Repl = "amp;";
- break;
- case '>':
- Repl = "gt;";
- break;
- case '<':
- Repl = "lt;";
- break;
- case '"':
- Repl = "quot;";
- break;
- case '\r':
- Str.Delete(i, 1);
- i--;
- break;
- }
- if (Repl != NULL)
- {
- Str[i] = '&';
- Str.Insert(Repl, i + 1);
- i += strlen(Repl);
- }
- }
- Str = AnsiToUtf8(Str);
- return Str;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall XmlTimestamp(const TDateTime & DateTime)
- {
- return FormatDateTime("yyyy'-'mm'-'dd'T'hh':'nn':'ss'.'zzz'Z'", ConvertTimestampToUTC(DateTime));
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall XmlTimestamp()
- {
- return XmlTimestamp(Now());
- }
- //---------------------------------------------------------------------------
- #pragma warn -inl
- class TSessionActionRecord
- {
- public:
- __fastcall TSessionActionRecord(TSessionLog * Log, TLogAction Action) :
- FLog(Log),
- FAction(Action),
- FState(Opened),
- FRecursive(false),
- FErrorMessages(NULL),
- FNames(new TStringList()),
- FValues(new TStringList()),
- FFileList(NULL)
- {
- FLog->AddPendingAction(this);
- }
- __fastcall ~TSessionActionRecord()
- {
- delete FErrorMessages;
- delete FNames;
- delete FValues;
- delete FFileList;
- }
- void __fastcall Restart()
- {
- FState = Opened;
- FRecursive = false;
- delete FErrorMessages;
- FErrorMessages = NULL;
- delete FFileList;
- FFileList = NULL;
- FNames->Clear();
- FValues->Clear();
- }
- bool __fastcall Record()
- {
- bool Result = (FState != Opened);
- if (Result)
- {
- if ((FLog->FLoggingActions) && (FState != Cancelled))
- {
- const char * Name = ActionName();
- AnsiString Attrs;
- if (FRecursive)
- {
- Attrs = " recursive=\"true\"";
- }
- FLog->Add(llAction, FORMAT(" <%s%s>", (Name, Attrs)));
- for (int Index = 0; Index < FNames->Count; Index++)
- {
- AnsiString Value = FValues->Strings[Index];
- if (Value.IsEmpty())
- {
- FLog->Add(llAction, FORMAT(" <%s />", (FNames->Strings[Index])));
- }
- else
- {
- FLog->Add(llAction, FORMAT(" <%s value=\"%s\" />",
- (FNames->Strings[Index], XmlEscape(Value))));
- }
- }
- if (FFileList != NULL)
- {
- FLog->Add(llAction, " <files>");
- for (int Index = 0; Index < FFileList->Count; Index++)
- {
- TRemoteFile * File = FFileList->Files[Index];
- FLog->Add(llAction, " <file>");
- FLog->Add(llAction, FORMAT(" <filename value=\"%s\" />", (XmlEscape(File->FileName))));
- FLog->Add(llAction, FORMAT(" <type value=\"%s\" />", (XmlEscape(File->Type))));
- if (!File->IsDirectory)
- {
- FLog->Add(llAction, FORMAT(" <size value=\"%s\" />", (IntToStr(File->Size))));
- }
- FLog->Add(llAction, FORMAT(" <modification value=\"%s\" />", (XmlTimestamp(File->Modification))));
- FLog->Add(llAction, FORMAT(" <permissions value=\"%s\" />", (XmlEscape(File->Rights->Text))));
- FLog->Add(llAction, " </file>");
- }
- FLog->Add(llAction, " </files>");
- }
- if (FState == RolledBack)
- {
- if (FErrorMessages != NULL)
- {
- FLog->Add(llAction, " <result success=\"false\">");
- for (int Index = 0; Index < FErrorMessages->Count; Index++)
- {
- FLog->Add(llAction,
- FORMAT(" <message>%s</message>", (XmlEscape(FErrorMessages->Strings[Index]))));
- }
- FLog->Add(llAction, " </result>");
- }
- else
- {
- FLog->Add(llAction, " <result success=\"false\" />");
- }
- }
- else
- {
- FLog->Add(llAction, " <result success=\"true\" />");
- }
- FLog->Add(llAction, FORMAT(" </%s>", (Name)));
- }
- delete this;
- }
- return Result;
- }
- void __fastcall Commit()
- {
- Close(Committed);
- }
- void __fastcall Rollback(Exception * E)
- {
- assert(FErrorMessages == NULL);
- FErrorMessages = new TStringList();
- if (!E->Message.IsEmpty())
- {
- FErrorMessages->Add(E->Message);
- }
- ExtException * EE = dynamic_cast<ExtException *>(E);
- if ((EE != NULL) && (EE->MoreMessages != NULL))
- {
- FErrorMessages->AddStrings(EE->MoreMessages);
- }
- if (FErrorMessages->Count == 0)
- {
- delete FErrorMessages;
- FErrorMessages = NULL;
- }
- Close(RolledBack);
- }
- void __fastcall Cancel()
- {
- Close(Cancelled);
- }
- void __fastcall FileName(const AnsiString & FileName)
- {
- Parameter("filename", FileName);
- }
- void __fastcall Destination(const AnsiString & Destination)
- {
- Parameter("destination", Destination);
- }
- void __fastcall Rights(const TRights & Rights)
- {
- Parameter("permissions", Rights.Text);
- }
- void __fastcall Modification(const TDateTime & DateTime)
- {
- Parameter("modification", XmlTimestamp(DateTime));
- }
- void __fastcall Recursive()
- {
- FRecursive = true;
- }
- void __fastcall Command(const AnsiString & Command)
- {
- Parameter("command", Command);
- }
- void __fastcall AddOutput(AnsiString Output, bool StdError)
- {
- const char * Name = (StdError ? "erroroutput" : "output");
- int Index = FNames->IndexOf(Name);
- if (Index >= 0)
- {
- FValues->Strings[Index] = FValues->Strings[Index] + "\r\n" + Output;
- }
- else
- {
- Parameter(Name, Output);
- }
- }
- void __fastcall FileList(TRemoteFileList * FileList)
- {
- if (FFileList == NULL)
- {
- FFileList = new TRemoteFileList();
- }
- FileList->DuplicateTo(FFileList);
- }
- protected:
- enum TState { Opened, Committed, RolledBack, Cancelled };
- inline void __fastcall Close(TState State)
- {
- assert(FState == Opened);
- FState = State;
- FLog->RecordPendingActions();
- }
- const char * __fastcall ActionName()
- {
- switch (FAction)
- {
- case laUpload: return "upload";
- case laDownload: return "download";
- case laTouch: return "touch";
- case laChmod: return "chmod";
- case laMkdir: return "mkdir";
- case laRm: return "rm";
- case laMv: return "mv";
- case laCall: return "call";
- case laLs: return "ls";
- default: assert(false); return "";
- }
- }
- void __fastcall Parameter(const AnsiString & Name, const AnsiString & Value = "")
- {
- FNames->Add(Name);
- FValues->Add(Value);
- }
- private:
- TSessionLog * FLog;
- TLogAction FAction;
- TState FState;
- bool FRecursive;
- TStrings * FErrorMessages;
- TStrings * FNames;
- TStrings * FValues;
- TRemoteFileList * FFileList;
- };
- #pragma warn .inl
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- __fastcall TSessionAction::TSessionAction(TSessionLog * Log, TLogAction Action)
- {
- if (Log->FLoggingActions)
- {
- FRecord = new TSessionActionRecord(Log, Action);
- }
- else
- {
- FRecord = NULL;
- }
- }
- //---------------------------------------------------------------------------
- __fastcall TSessionAction::~TSessionAction()
- {
- if (FRecord != NULL)
- {
- Commit();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionAction::Restart()
- {
- if (FRecord != NULL)
- {
- FRecord->Restart();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionAction::Commit()
- {
- if (FRecord != NULL)
- {
- TSessionActionRecord * Record = FRecord;
- FRecord = NULL;
- Record->Commit();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionAction::Rollback(Exception * E)
- {
- if (FRecord != NULL)
- {
- TSessionActionRecord * Record = FRecord;
- FRecord = NULL;
- Record->Rollback(E);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionAction::Cancel()
- {
- if (FRecord != NULL)
- {
- TSessionActionRecord * Record = FRecord;
- FRecord = NULL;
- Record->Cancel();
- }
- }
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- __fastcall TFileSessionAction::TFileSessionAction(TSessionLog * Log, TLogAction Action) :
- TSessionAction(Log, Action)
- {
- };
- //---------------------------------------------------------------------------
- __fastcall TFileSessionAction::TFileSessionAction(
- TSessionLog * Log, TLogAction Action, const AnsiString & AFileName) :
- TSessionAction(Log, Action)
- {
- FileName(AFileName);
- };
- //---------------------------------------------------------------------------
- void __fastcall TFileSessionAction::FileName(const AnsiString & FileName)
- {
- if (FRecord != NULL)
- {
- FRecord->FileName(FileName);
- }
- }
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- __fastcall TFileLocationSessionAction::TFileLocationSessionAction(
- TSessionLog * Log, TLogAction Action) :
- TFileSessionAction(Log, Action)
- {
- };
- //---------------------------------------------------------------------------
- __fastcall TFileLocationSessionAction::TFileLocationSessionAction(
- TSessionLog * Log, TLogAction Action, const AnsiString & FileName) :
- TFileSessionAction(Log, Action, FileName)
- {
- };
- //---------------------------------------------------------------------------
- void __fastcall TFileLocationSessionAction::Destination(const AnsiString & Destination)
- {
- if (FRecord != NULL)
- {
- FRecord->Destination(Destination);
- }
- }
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- __fastcall TUploadSessionAction::TUploadSessionAction(TSessionLog * Log) :
- TFileLocationSessionAction(Log, laUpload)
- {
- };
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- __fastcall TDownloadSessionAction::TDownloadSessionAction(TSessionLog * Log) :
- TFileLocationSessionAction(Log, laDownload)
- {
- };
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- __fastcall TChmodSessionAction::TChmodSessionAction(
- TSessionLog * Log, const AnsiString & FileName) :
- TFileSessionAction(Log, laChmod, FileName)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TChmodSessionAction::Recursive()
- {
- if (FRecord != NULL)
- {
- FRecord->Recursive();
- }
- }
- //---------------------------------------------------------------------------
- __fastcall TChmodSessionAction::TChmodSessionAction(
- TSessionLog * Log, const AnsiString & FileName, const TRights & ARights) :
- TFileSessionAction(Log, laChmod, FileName)
- {
- Rights(ARights);
- }
- //---------------------------------------------------------------------------
- void __fastcall TChmodSessionAction::Rights(const TRights & Rights)
- {
- if (FRecord != NULL)
- {
- FRecord->Rights(Rights);
- }
- }
- //---------------------------------------------------------------------------
- __fastcall TTouchSessionAction::TTouchSessionAction(
- TSessionLog * Log, const AnsiString & FileName, const TDateTime & Modification) :
- TFileSessionAction(Log, laTouch, FileName)
- {
- if (FRecord != NULL)
- {
- FRecord->Modification(Modification);
- }
- }
- //---------------------------------------------------------------------------
- __fastcall TMkdirSessionAction::TMkdirSessionAction(
- TSessionLog * Log, const AnsiString & FileName) :
- TFileSessionAction(Log, laMkdir, FileName)
- {
- }
- //---------------------------------------------------------------------------
- __fastcall TRmSessionAction::TRmSessionAction(
- TSessionLog * Log, const AnsiString & FileName) :
- TFileSessionAction(Log, laRm, FileName)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TRmSessionAction::Recursive()
- {
- if (FRecord != NULL)
- {
- FRecord->Recursive();
- }
- }
- //---------------------------------------------------------------------------
- __fastcall TMvSessionAction::TMvSessionAction(TSessionLog * Log,
- const AnsiString & FileName, const AnsiString & ADestination) :
- TFileLocationSessionAction(Log, laMv, FileName)
- {
- Destination(ADestination);
- }
- //---------------------------------------------------------------------------
- __fastcall TCallSessionAction::TCallSessionAction(TSessionLog * Log,
- const AnsiString & Command, const AnsiString & Destination) :
- TSessionAction(Log, laCall)
- {
- if (FRecord != NULL)
- {
- FRecord->Command(Command);
- FRecord->Destination(Destination);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TCallSessionAction::AddOutput(const AnsiString & Output, bool StdError)
- {
- if (FRecord != NULL)
- {
- FRecord->AddOutput(Output, StdError);
- }
- }
- //---------------------------------------------------------------------------
- __fastcall TLsSessionAction::TLsSessionAction(TSessionLog * Log,
- const AnsiString & Destination) :
- TSessionAction(Log, laLs)
- {
- if (FRecord != NULL)
- {
- FRecord->Destination(Destination);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TLsSessionAction::FileList(TRemoteFileList * FileList)
- {
- if (FRecord != NULL)
- {
- FRecord->FileList(FileList);
- }
- }
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- TSessionInfo::TSessionInfo()
- {
- LoginTime = Now();
- }
- //---------------------------------------------------------------------------
- TFileSystemInfo::TFileSystemInfo()
- {
- memset(&IsCapable, false, sizeof(IsCapable));
- }
- //---------------------------------------------------------------------------
- const char *LogLineMarks = "<>!.*";
- __fastcall TSessionLog::TSessionLog(TSessionUI* UI, TSessionData * SessionData,
- TConfiguration * Configuration):
- TStringList()
- {
- FCriticalSection = new TCriticalSection;
- FConfiguration = Configuration;
- FParent = NULL;
- FUI = UI;
- FSessionData = SessionData;
- FFile = NULL;
- FLoggedLines = 0;
- FTopIndex = -1;
- FCurrentLogFileName = "";
- FCurrentFileName = "";
- FLoggingActions = false;
- FClosed = false;
- FPendingActions = new TList();
- }
- //---------------------------------------------------------------------------
- __fastcall TSessionLog::~TSessionLog()
- {
- assert(FPendingActions->Count == 0);
- delete FPendingActions;
- FClosed = true;
- ReflectSettings();
- assert(FFile == NULL);
- delete FCriticalSection;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::Lock()
- {
- FCriticalSection->Enter();
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::Unlock()
- {
- FCriticalSection->Leave();
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TSessionLog::GetSessionName()
- {
- assert(FSessionData != NULL);
- return FSessionData->SessionName;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TSessionLog::GetLine(Integer Index)
- {
- return Strings[Index - FTopIndex];
- }
- //---------------------------------------------------------------------------
- TLogLineType __fastcall TSessionLog::GetType(int Index)
- {
- return (TLogLineType)Objects[Index - FTopIndex];
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::DoAddToParent(TLogLineType Type, const AnsiString & Line)
- {
- assert(FParent != NULL);
- FParent->Add(Type, Line);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::DoAddToSelf(TLogLineType Type, const AnsiString & Line)
- {
- if (FTopIndex < 0)
- {
- FTopIndex = 0;
- }
- TStringList::AddObject(Line, (TObject*)Type);
- FLoggedLines++;
- if (LogToFile())
- {
- if (FFile == NULL)
- {
- OpenLogFile();
- }
- if (FFile != NULL)
- {
- if (Type != llAction)
- {
- AnsiString Timestamp = FormatDateTime(" yyyy-mm-dd hh:nn:ss.zzz ", Now());
- fputc(LogLineMarks[Type], (FILE *)FFile);
- fwrite(Timestamp.c_str(), Timestamp.Length(), 1, (FILE *)FFile);
- }
- // use fwrite instead of fprintf to make sure that even
- // non-ascii data (unicode) gets in.
- fwrite(Line.c_str(), Line.Length(), 1, (FILE *)FFile);
- fputc('\n', (FILE *)FFile);
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::DoAdd(TLogLineType Type, AnsiString Line,
- void __fastcall (__closure *f)(TLogLineType Type, const AnsiString & Line))
- {
- AnsiString Prefix;
- if ((Type != llAction) && !Name.IsEmpty())
- {
- Prefix = "[" + Name + "] ";
- }
- while (!Line.IsEmpty())
- {
- f(Type, Prefix + CutToChar(Line, '\n', false));
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::Add(TLogLineType Type, const AnsiString & Line)
- {
- assert(FConfiguration);
- if (Logging && (FConfiguration->LogActions == (Type == llAction)))
- {
- try
- {
- if (FParent != NULL)
- {
- DoAdd(Type, Line, &DoAddToParent);
- }
- else
- {
- TGuard Guard(FCriticalSection);
- BeginUpdate();
- try
- {
- DoAdd(Type, Line, DoAddToSelf);
- }
- __finally
- {
- DeleteUnnecessary();
- EndUpdate();
- }
- }
- }
- catch (Exception &E)
- {
- // We failed logging, turn it off and notify user.
- FConfiguration->Logging = false;
- try
- {
- throw ExtException(&E, LOG_GEN_ERROR);
- }
- catch (Exception &E)
- {
- AddException(&E);
- FUI->HandleExtendedException(&E);
- }
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::AddException(Exception * E)
- {
- if (E != NULL)
- {
- Add(llException, ExceptionLogString(E));
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::ReflectSettings()
- {
- TGuard Guard(FCriticalSection);
- bool ALogging =
- !FClosed &&
- ((FParent != NULL) || FConfiguration->Logging) &&
- ((FParent == NULL) || !FConfiguration->LogActions);
- bool LoggingActions = ALogging && FConfiguration->LogActions;
- if (LoggingActions && !FLoggingActions)
- {
- FLoggingActions = true;
- FLogging = ALogging;
- Add(llAction, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
- Add(llAction, FORMAT("<session xmlns=\"http://winscp.net/schema/session/1.0\" name=\"%s\" start=\"%s\">",
- (XmlEscape(FSessionData->SessionName), XmlTimestamp())));
- StateChange();
- }
- else if (!LoggingActions && FLoggingActions)
- {
- FLoggingActions = false;
- Add(llAction, "</session>");
- FLogging = ALogging;
- StateChange();
- }
- else if (FLogging != ALogging)
- {
- FLogging = ALogging;
- StateChange();
- }
- // if logging to file was turned off or log file was change -> close current log file
- // but disallow changing logfilename for xml logging
- if ((FFile != NULL) &&
- ((!LogToFile() || (FCurrentLogFileName != FConfiguration->LogFileName)) && !FLoggingActions))
- {
- CloseLogFile();
- }
- DeleteUnnecessary();
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSessionLog::LogToFile()
- {
- return Logging && FConfiguration->LogToFile && (FParent == NULL);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::CloseLogFile()
- {
- if (FFile != NULL)
- {
- fclose((FILE *)FFile);
- FFile = NULL;
- }
- FCurrentLogFileName = "";
- FCurrentFileName = "";
- StateChange();
- }
- //---------------------------------------------------------------------------
- void TSessionLog::OpenLogFile()
- {
- try
- {
- assert(FFile == NULL);
- assert(FConfiguration != NULL);
- FCurrentLogFileName = FConfiguration->LogFileName;
- AnsiString NewFileName = StripPathQuotes(ExpandEnvironmentVariables(FCurrentLogFileName));
- TDateTime N = Now();
- for (int Index = 1; Index < NewFileName.Length(); Index++)
- {
- if (NewFileName[Index] == '!')
- {
- AnsiString Replacement;
- // keep consistent with TFileCustomCommand::PatternReplacement
- switch (tolower(NewFileName[Index + 1]))
- {
- case 'y':
- Replacement = FormatDateTime("yyyy", N);
- break;
- case 'm':
- Replacement = FormatDateTime("mm", N);
- break;
- case 'd':
- Replacement = FormatDateTime("dd", N);
- break;
- case 't':
- Replacement = FormatDateTime("hhnnss", N);
- break;
- case '@':
- Replacement = MakeValidFileName(FSessionData->HostName);
- break;
- case 's':
- Replacement = MakeValidFileName(FSessionData->SessionName);
- break;
- case '!':
- Replacement = "!";
- break;
- default:
- Replacement = AnsiString("!") + NewFileName[Index + 1];
- break;
- }
- NewFileName.Delete(Index, 2);
- NewFileName.Insert(Replacement, Index);
- Index += Replacement.Length() - 1;
- }
- }
- FFile = fopen(NewFileName.c_str(),
- (FConfiguration->LogFileAppend && !FLoggingActions ? "a" : "w"));
- if (FFile)
- {
- setvbuf((FILE *)FFile, NULL, _IONBF, BUFSIZ);
- FCurrentFileName = NewFileName;
- }
- else
- {
- throw Exception(FMTLOAD(LOG_OPENERROR, (NewFileName)));
- }
- }
- catch (Exception & E)
- {
- // We failed logging to file, turn it off and notify user.
- FCurrentLogFileName = "";
- FCurrentFileName = "";
- FConfiguration->LogToFile = false;
- try
- {
- throw ExtException(&E, LOG_GEN_ERROR);
- }
- catch (Exception & E)
- {
- AddException(&E);
- FUI->HandleExtendedException(&E);
- }
- }
- StateChange();
- }
- //---------------------------------------------------------------------------
- void TSessionLog::StateChange()
- {
- if (FOnStateChange != NULL)
- {
- FOnStateChange(this);
- }
- }
- //---------------------------------------------------------------------------
- void TSessionLog::DeleteUnnecessary()
- {
- BeginUpdate();
- try
- {
- if (!Logging || (FParent != NULL))
- {
- Clear();
- }
- else
- {
- while (!FConfiguration->LogWindowComplete && (Count > FConfiguration->LogWindowLines))
- {
- Delete(0);
- FTopIndex++;
- }
- }
- }
- __finally
- {
- EndUpdate();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::AddStartupInfo()
- {
- if (Logging && !FConfiguration->LogActions)
- {
- if (FParent != NULL)
- {
- // do not add session info for secondary session
- // (this should better be handled in the TSecondaryTerminal)
- }
- else
- {
- DoAddStartupInfo(FSessionData);
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::DoAddStartupInfo(TSessionData * Data)
- {
- TGuard Guard(FCriticalSection);
- BeginUpdate();
- try
- {
- #define ADF(S, F) DoAdd(llMessage, FORMAT(S, F), DoAddToSelf);
- AddSeparator();
- ADF("WinSCP %s (OS %s)", (FConfiguration->VersionStr, FConfiguration->OSVersionStr));
- THierarchicalStorage * Storage = FConfiguration->CreateScpStorage(false);
- try
- {
- ADF("Configuration: %s", (Storage->Source));
- }
- __finally
- {
- delete Storage;
- }
- ADF("Login time: %s", (FormatDateTime("dddddd tt", Now())));
- AddSeparator();
- ADF("Session name: %s (%s)", (Data->SessionName, Data->Source));
- ADF("Host name: %s (Port: %d)", (Data->HostName, Data->PortNumber));
- ADF("User name: %s (Password: %s, Key file: %s)",
- (Data->UserName, BooleanToEngStr(!Data->Password.IsEmpty()),
- BooleanToEngStr(!Data->PublicKeyFile.IsEmpty())))
- ADF("Tunnel: %s", (BooleanToEngStr(Data->Tunnel)));
- if (Data->Tunnel)
- {
- ADF("Tunnel: Host name: %s (Port: %d)", (Data->TunnelHostName, Data->TunnelPortNumber));
- ADF("Tunnel: User name: %s (Password: %s, Key file: %s)",
- (Data->TunnelUserName, BooleanToEngStr(!Data->TunnelPassword.IsEmpty()),
- BooleanToEngStr(!Data->TunnelPublicKeyFile.IsEmpty())))
- ADF("Tunnel: Local port number: %d", (Data->TunnelLocalPortNumber));
- }
- ADF("Transfer Protocol: %s", (Data->FSProtocolStr));
- char * PingTypes = "-NC";
- TPingType PingType;
- int PingInterval;
- if (Data->FSProtocol == fsFTP)
- {
- PingType = Data->FtpPingType;
- PingInterval = Data->FtpPingInterval;
- }
- else
- {
- PingType = Data->PingType;
- PingInterval = Data->PingInterval;
- }
- ADF("Ping type: %s, Ping interval: %d sec; Timeout: %d sec",
- (AnsiString(PingTypes[PingType]), PingInterval, Data->Timeout));
- ADF("Proxy: %s", (ProxyMethodList[Data->ProxyMethod]));
- if (Data->ProxyMethod != pmNone)
- {
- ADF("HostName: %s (Port: %d); Username: %s; Passwd: %s",
- (Data->ProxyHost, Data->ProxyPort,
- Data->ProxyUsername, BooleanToEngStr(!Data->ProxyPassword.IsEmpty())));
- if (Data->ProxyMethod == pmTelnet)
- {
- ADF("Telnet command: %s", (Data->ProxyTelnetCommand));
- }
- if (Data->ProxyMethod == pmCmd)
- {
- ADF("Local command: %s", (Data->ProxyLocalCommand));
- }
- }
- if (Data->UsesSsh)
- {
- ADF("SSH protocol version: %s; Compression: %s",
- (Data->SshProtStr, BooleanToEngStr(Data->Compression)));
- ADF("Bypass authentication: %s",
- (BooleanToEngStr(Data->SshNoUserAuth)));
- ADF("Try agent: %s; Agent forwarding: %s; TIS/CryptoCard: %s; KI: %s; GSSAPI: %s",
- (BooleanToEngStr(Data->TryAgent), BooleanToEngStr(Data->AgentFwd), BooleanToEngStr(Data->AuthTIS),
- BooleanToEngStr(Data->AuthKI), BooleanToEngStr(Data->AuthGSSAPI)));
- if (Data->AuthGSSAPI)
- {
- ADF("GSSAPI: Forwarding: %s; Server realm: %s",
- (BooleanToEngStr(Data->GSSAPIFwdTGT), Data->GSSAPIServerRealm));
- }
- ADF("Ciphers: %s; Ssh2DES: %s",
- (Data->CipherList, BooleanToEngStr(Data->Ssh2DES)));
- AnsiString Bugs;
- char const * BugFlags = "A+-";
- for (int Index = 0; Index < BUG_COUNT; Index++)
- {
- Bugs += AnsiString(BugFlags[Data->Bug[(TSshBug)Index]])+(Index<BUG_COUNT-1?",":"");
- }
- ADF("SSH Bugs: %s", (Bugs));
- Bugs = "";
- for (int Index = 0; Index < SFTP_BUG_COUNT; Index++)
- {
- Bugs += AnsiString(BugFlags[Data->SFTPBug[(TSftpBug)Index]])+(Index<SFTP_BUG_COUNT-1?",":"");
- }
- ADF("SFTP Bugs: %s", (Bugs));
- ADF("Return code variable: %s; Lookup user groups: %s",
- ((Data->DetectReturnVar ? AnsiString("Autodetect") : Data->ReturnVar),
- BooleanToEngStr(Data->LookupUserGroups)));
- ADF("Shell: %s", ((Data->Shell.IsEmpty()? AnsiString("default") : Data->Shell)));
- ADF("EOL: %d, UTF: %d", (Data->EOLType, Data->NotUtf));
- ADF("Clear aliases: %s, Unset nat.vars: %s, Resolve symlinks: %s",
- (BooleanToEngStr(Data->ClearAliases), BooleanToEngStr(Data->UnsetNationalVars),
- BooleanToEngStr(Data->ResolveSymlinks)));
- ADF("LS: %s, Ign LS warn: %s, Scp1 Comp: %s",
- (Data->ListingCommand,
- BooleanToEngStr(Data->IgnoreLsWarnings),
- BooleanToEngStr(Data->Scp1Compatibility)));
- }
- if (Data->FSProtocol == fsFTP)
- {
- AnsiString Ftps;
- switch (Data->Ftps)
- {
- case ftpsImplicit:
- Ftps = "Implicit SSL/TLS";
- break;
- case ftpsExplicitSsl:
- Ftps = "Explicit SSL";
- break;
- case ftpsExplicitTls:
- Ftps = "Explicit TLS";
- break;
- default:
- assert(Data->Ftps == ftpsNone);
- Ftps = "None";
- break;
- }
- ADF("FTP: FTPS: %s; Passive: %s [Force IP: %s]",
- (Ftps, BooleanToEngStr(Data->FtpPasvMode),
- BooleanToEngStr(Data->FtpForcePasvIp)));
- }
- ADF("Local directory: %s, Remote directory: %s, Update: %s, Cache: %s",
- ((Data->LocalDirectory.IsEmpty() ? AnsiString("default") : Data->LocalDirectory),
- (Data->RemoteDirectory.IsEmpty() ? AnsiString("home") : Data->RemoteDirectory),
- BooleanToEngStr(Data->UpdateDirectories),
- BooleanToEngStr(Data->CacheDirectories)));
- ADF("Cache directory changes: %s, Permanent: %s",
- (BooleanToEngStr(Data->CacheDirectoryChanges),
- BooleanToEngStr(Data->PreserveDirectoryChanges)));
- ADF("DST mode: %d", (int(Data->DSTMode)));
- AddSeparator();
- #undef ADF
- }
- __finally
- {
- DeleteUnnecessary();
- EndUpdate();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::AddSeparator()
- {
- Add(llMessage, "--------------------------------------------------------------------------");
- }
- //---------------------------------------------------------------------------
- int __fastcall TSessionLog::GetBottomIndex()
- {
- return (Count > 0 ? (TopIndex + Count - 1) : -1);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSessionLog::GetLoggingToFile()
- {
- assert((FFile == NULL) || LogToFile());
- return (FFile != NULL);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::Clear()
- {
- TGuard Guard(FCriticalSection);
- FTopIndex += Count;
- TStringList::Clear();
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::AddPendingAction(TSessionActionRecord * Action)
- {
- FPendingActions->Add(Action);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSessionLog::RecordPendingActions()
- {
- while ((FPendingActions->Count > 0) &&
- static_cast<TSessionActionRecord *>(FPendingActions->Items[0])->Record())
- {
- FPendingActions->Delete(0);
- }
- }
|