Page 1 of 1

[SFTP client] Local file in use after upload complete

Posted: Thu 21 Jul 2016 05:29
by pcz
Hello

When does TscSFTPClient release local file after upload?
(I am using NonBlocking mode)

I just want to rename local file after successful upload
So I tried to use OnSuccess event handler

Code: Select all

procedure TForm1.SFTPClientSuccess(Sender: TObject; Operation: TScSFTPOperation; const FileName: string;
  const Handle: TArray<System.Byte>; const Message: string);
begin
  if Operation = opClosingHandle then
    RenameFile(NameOfUploadedFile, NameOfUploadedFile + '.sent');   // problem here
end;
but it looks like file is still opened and cannot be renamed in this particular moment...

Any idea? :)

Regards
P.C.

Re: [SFTP client] Local file in use after upload complete

Posted: Thu 21 Jul 2016 11:49
by ViktorV
Yes, you are right. On Operation = opClosingHandle, the OnSuccess event is raised when the local file is still open. Therefore calling the RenameFile method finishes with an error.
We will consider the possibility to change this behavior of SecureBridge in one of the next releases.

Re: [SFTP client] Local file in use after upload complete

Posted: Fri 22 Jul 2016 11:39
by pcz
ViktorV wrote:We will consider the possibility to change this behavior of SecureBridge in one of the next releases.
It will be nice :)

In case of anyone looking for workaround of this problem (in console app):

Code: Select all

...
private
  FWorking, FOK: Boolean;
...

function ProcessMessage(var Msg: TMsg): Boolean;   // message pump analogue to Vcl.Forms.TApplication
var
  Unicode: Boolean;
  MsgExists: Boolean;
begin
  Result := False;
  if PeekMessage(Msg, 0, 0, 0, PM_NOREMOVE) then
  begin
    Unicode := (Msg.hwnd = 0) or IsWindowUnicode(Msg.hwnd);
    if Unicode then
      MsgExists := PeekMessageW(Msg, 0, 0, 0, PM_REMOVE)
    else
      MsgExists := PeekMessageA(Msg, 0, 0, 0, PM_REMOVE);
    if MsgExists then
    begin
      Result := True;
      if Msg.Message <> WM_QUIT then
      begin
        TranslateMessage(Msg);
        if Unicode then
          DispatchMessageW(Msg)
        else
          DispatchMessageA(Msg);
      end;
    end;
  end;
end;

procedure ProcessMessages;
var
  Msg: TMsg;
begin
  while ProcessMessage(Msg) do {loop};
end;

procedure TDataModule1.SFTPClientSuccess(Sender: TObject; Operation: TScSFTPOperation; const FileName: string; const Handle: TArray<System.Byte>; const Message: string);
begin
  if Operation = TScSFTPOperation.opClosingHandle then
  begin
    FWorking := False;
    FOK := True;
  end;
end;

procedure TDataModule1.SFTPClientError(Sender: TObject; Operation: TScSFTPOperation; const FileName: string; const Handle: TArray<System.Byte>; ErrorCode: Integer; const ErrorMessage: string; var Fail: Boolean);
begin
  FWorking := False;
end;

procedure TDataModule1.Upload;
var
  WaitResult: DWORD;
  Dummy: THandle;
begin
  FWorking := True;
  FOK := False;
  SFTPClient.UploadFile(LocalFilePath, RemoteFilePath, True);
  while FWorking do
  begin
    WaitResult := MsgWaitForMultipleObjects(0, Dummy, False, 1000, QS_ALLINPUT);
    if WaitResult = WAIT_OBJECT_0 then
      ProcessMessages;
  end;

  if FOK then
    // ...and now file can be renamed
end;

Re: [SFTP client] Local file in use after upload complete

Posted: Fri 22 Jul 2016 12:08
by ViktorV
We are glad to know you have found a solution for the issue. Please contact us if you have any questions concerning SecureBridge.

Re: [SFTP client] Local file in use after upload complete

Posted: Fri 25 Nov 2016 08:15
by ViktorV
We changed SecureBridge behavior. Now the OnSuccess event when Operation = opClosingHandle occurs when a local file is already closed. The new build of SecureBridge 7.2.3 including this change is already available for download.