1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
unit ftplister;
{$mode objfpc}{$H+}
interface
uses
classes, sysutils, dateutils,
lnet, lftp;
type
{ TFTPLister }
TFTPLister = class
private
FFtp: TLFTPClient;
FInternalStatus: TLFTPStatus;
FDirList: string;
procedure FtpError(const msg: string; aSocket: TLSocket);
procedure FtpConnect(Sender: TLSocket);
procedure FtpControl(Sender: TLSocket);
procedure FtpReceive(Sender: TLSocket);
procedure FtpSuccess(Sender: TLSocket; const aStatus: TLFTPStatus);
procedure FtpFailure(Sender: TLSocket; const aStatus: TLFTPStatus);
public
constructor Create;
destructor Destroy; override;
function GetList(const server, dir: string): TStrings;
end;
{ TFtpFile }
TFtpFile = class
private
FFileDate: TDateTime;
FFileName: string;
public
constructor Create(AListLine: string);
property FileName : string read FFileName;
property FileDate : TDateTime read FFileDate;
end;
implementation
const
MonthNames : array[1..12] of string[3] =
('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN',
'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC');
constructor TFtpFile.Create(AListLine: string);
{ example line
lrwxrwxrwx 1 0 0 7 Jun 07 19:12 fpc -> pub/fpc
-rw-r--r-- 1 1003 100 52533164 Jul 07 2006 lazarus-arm-wince-20060707.7z
}
var
datestr: string;
month:integer;
year: string;
timestr: string;
startpos: integer;
procedure ParseMonth(Name: string);
begin
Name := uppercase(Name);
//writeln('Month: ', Name);
month := 1;
while (Name<>MonthNames[month]) and (month<high(MonthNames)) do
inc(month);
end;
begin
startpos := 55;
while (AListLine[startpos]<>' ') do
inc(startpos);
FFileName := copy(AListLine, startpos+1, length(AListLine)-startpos);
writeln(FFileName);
ParseMonth(copy(AListLine, 44, 3));
TimeStr := copy(AListLine, 51, 5);
if pos(':', timestr)=0 then begin
Year := Trim(timestr);
timestr := '';
end
else
Year := IntToStr(YearOf(Now));;
datestr := copy(AListLine, 48, 2) + '-' + IntToStr(month) + '-'+ Year;
ShortDateFormat := 'DD-MM-YYYY';
DateSeparator:= '-';
if length(timestr)>0 then
datestr := datestr + ' ' + timestr;
FFileDate := StrToDateTime(datestr);
if FFileDate > IncDay(Now) then
begin
// datum ligt in de toekomst, dan een jaar terug
Year := IntToStr(YearOf(Now)-1);
datestr := copy(AListLine, 48, 2) + '-' + IntToStr(month) + '-'+ Year + ' ' + timestr;
FFileDate := StrToDateTime(datestr);
end;
end;
{ TFPTLister }
procedure TFTPLister.FtpReceive(Sender: TLSocket);
var
s: string;
begin
//Write('D:');
s := FFtp.GetDataMessage;
if FInternalStatus = fsCWD then
FDirList := FDirList + s;
//Writeln(s);
end;
procedure TFTPLister.FtpSuccess(Sender: TLSocket; const aStatus: TLFTPStatus);
begin
FInternalStatus := aStatus;
//Writeln('Success on status: ' + FTPStatusToStr(aStatus));
end;
procedure TFTPLister.FtpFailure(Sender: TLSocket; const aStatus: TLFTPStatus);
begin
//Writeln('Failure on status: ' + FTPStatusToStr(aStatus));
end;
procedure TFTPLister.FtpError(const msg: string; aSocket: TLSocket);
begin
Writeln('Error: ', msg);
RunError(99);
end;
procedure TFTPLister.FtpConnect(Sender: TLSocket);
begin
//writeln('Connected');
end;
procedure TFTPLister.FtpControl(Sender: TLSocket);
var
s: string;
begin
if FFtp.GetMessage(s) > 0 then begin
//writeln('C: ', s);
end;
end;
constructor TFTPLister.Create;
begin
FInternalStatus := fsNone;
FFtp := TLFTPClient.Create(nil);
FFtp.StatusSet := [fsType, fsCon, fsCWD, fsList]; // let's watch for these only, these will call OnSuccess or OnFailure
FFtp.PipeLine := False; // this means CLIENT pipeline emulation
FFtp.Timeout := 100;
FFtp.OnConnect := @FtpConnect;
FFtp.OnError := @FtpError;
FFtp.OnReceive := @FtpReceive;
FFtp.OnControl := @FtpControl;
FFtp.OnSuccess := @FtpSuccess; // reports when "watched" status succesfuly finishes
FFtp.OnFailure := @FtpFailure; // --||-- fails
end;
destructor TFTPLister.Destroy;
begin
FFtp.Free;
inherited Destroy;
end;
function TFTPLister.GetList(const server, dir: string): TStrings;
begin
Result := TStringList.Create;
FFtp.Connect(server);
repeat
FFtp.CallAction;
until FFtp.Connected;
{ until here it's same }
{ Let's send all the commands at once (beware shitty servers, set pipeline to false for them) }
FFtp.Authenticate('anonymous', '');
FFtp.Binary:=True;
FFtp.ChangeDirectory(dir);
FFtp.List();
repeat
FFtp.CallAction;
until FInternalStatus = fsList; // we increate the status per successful operation
Result.Text := FDirList;
FFtp.Disconnect;
end;
end.
|