Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Check Whether the Last SFTP Read Failed
See more SFTP Examples
Demonstrates the Chilkat SFtp.LastReadFailed method, which returns whether the most recent read for a handle failed. The only argument is the handle. A normal end-of-file is not a failure.
Background: A chunked read loop needs to tell three outcomes apart: more data, a clean end-of-file, and an actual error such as a dropped connection or a revoked handle.
LastReadFailed isolates the error case — it is false on a normal EOF read (which succeeds with zero bytes) and true for an empty, malformed, or already-closed handle — so the loop can stop cleanly on EOF but bail out with an error message on a genuine failure.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.SFtp,
Chilkat.BinData;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
sftp: TSFtp;
port: Integer;
password: string;
handle: string;
reading: Boolean;
chunkSize: Integer;
bd: TBinData;
begin
success := False;
// Demonstrates the SFtp.LastReadFailed method, which returns whether the most recent read for a
// handle failed. The only argument is the handle. A normal end-of-file is NOT a failure.
sftp := TSFtp.Create;
// Connect, authenticate, and initialize the SFTP subsystem.
port := 22;
success := sftp.Connect('sftp.example.com',port);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
password := 'mySshPassword';
success := sftp.AuthenticatePw('mySshLogin',password);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
success := sftp.InitializeSftp();
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
handle := sftp.OpenFile('subdir/data.txt','readOnly','openExisting');
if (sftp.LastMethodSuccess = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
reading := True;
chunkSize := 4096;
bd := TBinData.Create;
while reading do
begin
success := sftp.ReadFileBd(handle,chunkSize,bd);
// Distinguish an actual read failure from a normal EOF. On EOF the read succeeds, returns
// zero bytes, and LastReadFailed is False.
if (sftp.LastReadFailed(handle)) then
begin
WriteLn('Read failed: ' + sftp.LastErrorText);
Exit;
end;
reading := not sftp.Eof(handle);
end;
success := sftp.CloseHandle(handle);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
WriteLn('Read ' + bd.NumBytes + ' bytes with no read failures.');
sftp.Disconnect();
sftp.Free;
bd.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.