Skip to content

Commit

Permalink
Correcting Bug 99 implementation: Cannot read the timestamps while ap…
Browse files Browse the repository at this point in the history
…plication is busy

(reading timestamp after upload is done on the background connection, checking timestamp before upload is postponed until application is not busy)

This reverts all of 2983a6e and parts of 10b9d66

Source commit: ab78287d15ee431f42ceaf3c4399ade52cb0b90c
  • Loading branch information
martinprikryl committed Apr 14, 2023
1 parent 9bc4d58 commit 23df454
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 29 deletions.
82 changes: 62 additions & 20 deletions source/forms/CustomScpExplorer.cpp
Expand Up @@ -3882,6 +3882,55 @@ bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data
return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
}
//---------------------------------------------------------------------------
class TEditorUploadQueueItem : public TUploadQueueItem
{
public:
__fastcall TEditorUploadQueueItem(
TTerminal * Terminal, TStrings * FilesToCopy, const UnicodeString & TargetDir,
const TCopyParamType * CopyParam, int Params) :
TUploadQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, true, false)
{
}

protected:
virtual void __fastcall DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
{
TUploadQueueItem::DoTransferExecute(Terminal, ParallelOperation);
TTerminalManager::Instance()->ScpExplorer->EditedFileUploaded(Terminal, CompleteEvent);
}
};
//---------------------------------------------------------------------------
void TCustomScpExplorerForm::EditedFileUploaded(TTerminal * ATerminal, HANDLE UploadCompleteEvent)
{
if (WinConfiguration->EditorCheckNotModified) // optimization
{
UnicodeString RemoteFilePath;
{
TGuard Guard(FEditorManager->Section);
TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
if (DebugAlwaysTrue(Data != NULL) &&
EditorCheckNotModified(Data))
{
RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
}
}
if (!RemoteFilePath.IsEmpty())
{
std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(RemoteFilePath));
if (File.get() != NULL)
{
TGuard Guard(FEditorManager->Section);
TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
if (DebugAlwaysTrue(Data != NULL))
{
Data->SourceTimestamp = File->Modification;
AppLogFmt(L"Uploaded edited remote file timestamp: %s: ", (StandardTimestamp(Data->SourceTimestamp)));
}
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
{
Expand Down Expand Up @@ -3961,9 +4010,13 @@ void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
}
}

if (!Retry)
if (!Retry && EditorCheckNotModified(Data))
{
if (EditorCheckNotModified(Data))
if (NonVisualDataModule->Busy)
{
Retry = true;
}
else
{
UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
Expand All @@ -3981,7 +4034,10 @@ void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
}
}
}
}

if (!Retry)
{
TStrings * FileList = new TStringList();
try
{
Expand Down Expand Up @@ -4011,8 +4067,8 @@ void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
{
DebugAssert(Data->Queue != NULL);

TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
Data->RemoteDirectory, &CopyParam, Params, true, false);
TQueueItem * QueueItem =
new TEditorUploadQueueItem(Data->Terminal, FileList, Data->RemoteDirectory, &CopyParam, Params);
QueueItem->CompleteEvent = UploadCompleteEvent;
AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
}
Expand Down Expand Up @@ -4217,23 +4273,9 @@ void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
}
}
//---------------------------------------------------------------------------
void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TEditedFileData * Data, TObject * Sender, bool Failed)
void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
{
if (!Failed && EditorCheckNotModified(Data))
{
UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
if (File.get() != NULL)
{
Data->SourceTimestamp = File->Modification;
AppLogFmt(L"Uploaded edited remote file timestamp: %s: ", (StandardTimestamp(Data->SourceTimestamp)));
}
}

if (Sender != NULL)
{
EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
}
EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
}
//---------------------------------------------------------------------------
void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
Expand Down
4 changes: 3 additions & 1 deletion source/forms/CustomScpExplorer.h
Expand Up @@ -66,6 +66,7 @@ const TOperationSide osOther = osRemote;
class TCustomScpExplorerForm : public TForm
{
friend class TAutoBatch;
friend class TEditorUploadQueueItem;
__published:
TPanel *RemotePanel;
TTBXStatusBar *RemoteStatusBar;
Expand Down Expand Up @@ -459,12 +460,13 @@ friend class TAutoBatch;
void __fastcall DestroyProgressForm();
virtual void __fastcall FileOperationProgress(TFileOperationProgressType & ProgressData);
void __fastcall OperationComplete(const TDateTime & StartTime);
void EditedFileUploaded(TTerminal * ATerminal, HANDLE UploadCompleteEvent);
void __fastcall ExecutedFileChanged(
const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry);
void __fastcall ExecutedFileReload(const UnicodeString & FileName, TEditedFileData * Data);
void __fastcall ExecutedFileEarlyClosed(const TEditedFileData * Data,
bool & KeepOpen);
void __fastcall ExecutedFileUploadComplete(TEditedFileData * Data, TObject * Sender, bool Failed);
void __fastcall ExecutedFileUploadComplete(TObject * Sender);
bool EditorCheckNotModified(const TEditedFileData * Data);
void __fastcall CMDialogChar(TMessage & AMessage);
inline void __fastcall WMAppCommand(TMessage & Message);
Expand Down
39 changes: 33 additions & 6 deletions source/windows/EditorManager.cpp
Expand Up @@ -28,6 +28,7 @@ TEditedFileData::~TEditedFileData()
//---------------------------------------------------------------------------
__fastcall TEditorManager::TEditorManager()
{
FSection = new TCriticalSection();
FOnFileChange = NULL;
FOnFileReload = NULL;
FOnFileEarlyClosed = NULL;
Expand All @@ -52,10 +53,12 @@ __fastcall TEditorManager::~TEditorManager()
}
}
}
delete FSection;
}
//---------------------------------------------------------------------------
bool __fastcall TEditorManager::Empty(bool IgnoreClosed)
{
TGuard Guard(FSection);
bool Result;

if (!IgnoreClosed)
Expand Down Expand Up @@ -83,6 +86,7 @@ bool __fastcall TEditorManager::CanAddFile(const UnicodeString RemoteDirectory,
TObject *& Token, UnicodeString & ExistingLocalRootDirectory,
UnicodeString & ExistingLocalDirectory)
{
TGuard Guard(FSection);
bool Result = true;

Token = NULL;
Expand Down Expand Up @@ -148,8 +152,23 @@ bool __fastcall TEditorManager::CanAddFile(const UnicodeString RemoteDirectory,
return Result;
}
//---------------------------------------------------------------------------
TEditedFileData * TEditorManager::FindByUploadCompleteEvent(HANDLE UploadCompleteEvent)
{
TGuard Guard(FSection);
for (unsigned int i = 0; i < FFiles.size(); i++)
{
TFileData * FileData = &FFiles[i];
if (FileData->UploadCompleteEvent == UploadCompleteEvent)
{
return FileData->Data;
}
}
return NULL;
}
//---------------------------------------------------------------------------
void __fastcall TEditorManager::ProcessFiles(TEditedFileProcessEvent Callback, void * Arg)
{
TGuard Guard(FSection);
for (unsigned int i = 0; i < FFiles.size(); i++)
{
TFileData * FileData = &FFiles[i];
Expand All @@ -160,6 +179,7 @@ void __fastcall TEditorManager::ProcessFiles(TEditedFileProcessEvent Callback, v
//---------------------------------------------------------------------------
bool __fastcall TEditorManager::CloseInternalEditors(TNotifyEvent CloseCallback)
{
TGuard Guard(FSection);
// Traverse from end, as closing internal editor causes deletion of
// respective file vector element.
TObject * PrevToken = NULL;
Expand Down Expand Up @@ -197,6 +217,7 @@ bool __fastcall TEditorManager::CloseInternalEditors(TNotifyEvent CloseCallback)
//---------------------------------------------------------------------------
bool __fastcall TEditorManager::CloseExternalFilesWithoutProcess()
{
TGuard Guard(FSection);
for (unsigned int i = FFiles.size(); i > 0; i--)
{
TFileData * FileData = &FFiles[i - 1];
Expand All @@ -213,6 +234,7 @@ bool __fastcall TEditorManager::CloseExternalFilesWithoutProcess()
void __fastcall TEditorManager::AddFileInternal(const UnicodeString FileName,
TEditedFileData * AData, TObject * Token)
{
TGuard Guard(FSection);
std::unique_ptr<TEditedFileData> Data(AData);
TFileData FileData;
FileData.FileName = FileName;
Expand All @@ -226,6 +248,7 @@ void __fastcall TEditorManager::AddFileInternal(const UnicodeString FileName,
void __fastcall TEditorManager::AddFileExternal(const UnicodeString FileName,
TEditedFileData * AData, HANDLE Process)
{
TGuard Guard(FSection);
std::unique_ptr<TEditedFileData> Data(AData);
TFileData FileData;
FileData.FileName = FileName;
Expand All @@ -242,6 +265,7 @@ void __fastcall TEditorManager::AddFileExternal(const UnicodeString FileName,
//---------------------------------------------------------------------------
void __fastcall TEditorManager::Check()
{
TGuard Guard(FSection);
int Index;

for (Index = 0; Index < static_cast<int>(FFiles.size()); Index++)
Expand Down Expand Up @@ -297,7 +321,7 @@ void __fastcall TEditorManager::Check()

if (Index >= 0)
{
UploadComplete(Index, false); // To be improved: do not know if it really succeeded
UploadComplete(Index);
}
}
while ((Index >= 0) && (FUploadCompleteEvents.size() > 0));
Expand Down Expand Up @@ -329,6 +353,7 @@ bool __fastcall TEditorManager::EarlyClose(int Index)
//---------------------------------------------------------------------------
void __fastcall TEditorManager::FileChanged(TObject * Token)
{
TGuard Guard(FSection);
int Index = FindFile(Token);

DebugAssert(Index >= 0);
Expand All @@ -339,6 +364,7 @@ void __fastcall TEditorManager::FileChanged(TObject * Token)
//---------------------------------------------------------------------------
void __fastcall TEditorManager::FileReload(TObject * Token)
{
TGuard Guard(FSection);
int Index = FindFile(Token);

DebugAssert(Index >= 0);
Expand All @@ -352,6 +378,7 @@ void __fastcall TEditorManager::FileReload(TObject * Token)
//---------------------------------------------------------------------------
void __fastcall TEditorManager::FileClosed(TObject * Token, bool Forced)
{
TGuard Guard(FSection);
int Index = FindFile(Token);

DebugAssert(Index >= 0);
Expand Down Expand Up @@ -379,7 +406,7 @@ void __fastcall TEditorManager::AddFile(TFileData & FileData, TEditedFileData *
Data.release(); // ownership passed
}
//---------------------------------------------------------------------------
void __fastcall TEditorManager::UploadComplete(int Index, bool Failed)
void __fastcall TEditorManager::UploadComplete(int Index)
{
TFileData * FileData = &FFiles[Index];

Expand All @@ -401,9 +428,9 @@ void __fastcall TEditorManager::UploadComplete(int Index, bool Failed)
FileData->Reupload = false;
CheckFileChange(Index, true);
}
else if (FOnFileUploadComplete != NULL)
else if ((FileData->Token != NULL) && (FOnFileUploadComplete != NULL))
{
FOnFileUploadComplete(FileData->Data, FileData->Token, Failed);
FOnFileUploadComplete(FileData->Token);
}
}
}
Expand Down Expand Up @@ -549,7 +576,7 @@ void __fastcall TEditorManager::CheckFileChange(int Index, bool Force)
OnFileChange(FileData->FileName, FileData->Data, FileData->UploadCompleteEvent, Retry);
if (Retry)
{
UploadComplete(Index, true);
UploadComplete(Index);
FileData->Timestamp = PrevTimestamp;
}
}
Expand All @@ -558,7 +585,7 @@ void __fastcall TEditorManager::CheckFileChange(int Index, bool Force)
// upload failed (was not even started)
if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
{
UploadComplete(Index, true);
UploadComplete(Index);
}
throw;
}
Expand Down
7 changes: 5 additions & 2 deletions source/windows/EditorManager.h
Expand Up @@ -31,7 +31,7 @@ typedef void __fastcall (__closure * TEditedFileReloadEvent)
typedef void __fastcall (__closure * TEditedFileEarlyClosedEvent)
(const TEditedFileData * Data, bool & KeepOpen);
typedef void __fastcall (__closure * TEditedFileUploadComplete)
(TEditedFileData * Data, TObject * Token, bool Failed);
(TObject * Token);
//---------------------------------------------------------------------------
typedef void __fastcall (__closure * TEditedFileProcessEvent)
(const UnicodeString FileName, TEditedFileData * Data, TObject * Token, void * Arg);
Expand Down Expand Up @@ -62,11 +62,13 @@ class TEditorManager
void __fastcall FileClosed(TObject * Token, bool Forced);

void __fastcall ProcessFiles(TEditedFileProcessEvent Callback, void * Arg);
TEditedFileData * FindByUploadCompleteEvent(HANDLE UploadCompleteEvent);

__property TEditedFileChangedEvent OnFileChange = { read = FOnFileChange, write = FOnFileChange };
__property TEditedFileReloadEvent OnFileReload = { read = FOnFileReload, write = FOnFileReload };
__property TEditedFileEarlyClosedEvent OnFileEarlyClosed = { read = FOnFileEarlyClosed, write = FOnFileEarlyClosed };
__property TEditedFileUploadComplete OnFileUploadComplete = { read = FOnFileUploadComplete, write = FOnFileUploadComplete };
__property TCriticalSection * Section = { read = FSection };

private:
struct TFileData
Expand All @@ -92,9 +94,10 @@ class TEditorManager
TEditedFileReloadEvent FOnFileReload;
TEditedFileEarlyClosedEvent FOnFileEarlyClosed;
TEditedFileUploadComplete FOnFileUploadComplete;
TCriticalSection * FSection;

void __fastcall AddFile(TFileData & FileData, TEditedFileData * Data);
void __fastcall UploadComplete(int Index, bool Failed);
void __fastcall UploadComplete(int Index);
bool __fastcall CloseFile(int Index, bool IgnoreErrors, bool Delete);
void __fastcall CloseProcess(int Index);
bool __fastcall EarlyClose(int Index);
Expand Down

0 comments on commit 23df454

Please sign in to comment.