| 1 |
{ |
| 2 |
Double commander |
| 3 |
------------------------------------------------------------------------- |
| 4 |
WFX plugin for working with File Transfer Protocol |
| 5 |
|
| 6 |
Copyright (C) 2009-2021 Alexander Koblov ([email protected]) |
| 7 |
|
| 8 |
This library is free software; you can redistribute it and/or |
| 9 |
modify it under the terms of the GNU Lesser General Public |
| 10 |
License as published by the Free Software Foundation; either |
| 11 |
version 2.1 of the License, or (at your option) any later version. |
| 12 |
|
| 13 |
This library is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
Lesser General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU Lesser General Public |
| 19 |
License along with this library; if not, write to the Free Software |
| 20 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
} |
| 22 |
|
| 23 |
unit FtpAdv; |
| 24 |
|
| 25 |
{$mode delphi} |
| 26 |
{$macro on} |
| 27 |
|
| 28 |
interface |
| 29 |
|
| 30 |
uses |
| 31 |
Classes, SysUtils, WfxPlugin, FtpSend, DCClassesUtf8, LConvEncoding, |
| 32 |
DCConvertEncoding, DCFileAttributes, DCBasicTypes, blcksock, LazVersion; |
| 33 |
|
| 34 |
type |
| 35 |
TConvertUTF8ToEncodingFunc = function(const S: String {$IFDEF FPC_HAS_CPSTRING}; SetTargetCodePage: Boolean = False{$ENDIF}): RawByteString; |
| 36 |
|
| 37 |
type |
| 38 |
|
| 39 |
{ EUserAbort } |
| 40 |
|
| 41 |
EUserAbort = class(Exception); |
| 42 |
|
| 43 |
{ TFTPListRecEx } |
| 44 |
|
| 45 |
TFTPListRecEx = class(TFTPListRec) |
| 46 |
private |
| 47 |
FMode: TFileAttrs; |
| 48 |
public |
| 49 |
procedure Assign(Value: TFTPListRec); override; |
| 50 |
property Mode: TFileAttrs read FMode write FMode; |
| 51 |
end; |
| 52 |
|
| 53 |
{ TFTPListEx } |
| 54 |
|
| 55 |
TFTPListEx = class(TFTPList) |
| 56 |
private |
| 57 |
FIndex: Integer; |
| 58 |
protected |
| 59 |
procedure FillRecord(const Value: TFTPListRec); override; |
| 60 |
public |
| 61 |
procedure Clear; override; |
| 62 |
procedure ParseLines; override; |
| 63 |
procedure Assign(Value: TFTPList); override; |
| 64 |
end; |
| 65 |
|
| 66 |
{ TProgressStream } |
| 67 |
|
| 68 |
TProgressStream = class(TFileStreamEx) |
| 69 |
public |
| 70 |
DoneSize: Int64; |
| 71 |
FileSize: Int64; |
| 72 |
PluginNumber: Integer; |
| 73 |
ProgressProc: TProgressProcW; |
| 74 |
SourceName, TargetName: PWideChar; |
| 75 |
private |
| 76 |
FTime: QWord; |
| 77 |
FtpSend: TFTPSend; |
| 78 |
procedure DoProgress(Result: Integer); |
| 79 |
public |
| 80 |
function Read(var Buffer; Count: Longint): Longint; override; |
| 81 |
function Write(const Buffer; Count: Longint): Longint; override; |
| 82 |
end; |
| 83 |
|
| 84 |
{ TFTPSendEx } |
| 85 |
|
| 86 |
TFTPSendEx = class(TFTPSend) |
| 87 |
private |
| 88 |
FUnicode: Boolean; |
| 89 |
FSetTime: Boolean; |
| 90 |
FMachine: Boolean; |
| 91 |
FShowHidden: Boolean; |
| 92 |
FShowHiddenText: String; |
| 93 |
FUseAllocate: Boolean; |
| 94 |
FTcpKeepAlive: Boolean; |
| 95 |
FKeepAliveTransfer: Boolean; |
| 96 |
procedure SetEncoding(AValue: String); |
| 97 |
protected |
| 98 |
ConvertToUtf8: TConvertEncodingFunction; |
| 99 |
ConvertFromUtf8: TConvertUTF8ToEncodingFunc; |
| 100 |
protected |
| 101 |
FAuto: Boolean; |
| 102 |
FEncoding: String; |
| 103 |
FPublicKey, FPrivateKey: String; |
| 104 |
function Connect: Boolean; override; |
| 105 |
function DataSocket: Boolean; override; |
| 106 |
function ListMachine(Directory: String): Boolean; |
| 107 |
procedure DoStatus(Response: Boolean; const Value: string); override; |
| 108 |
procedure OnSocketStatus(Sender: TObject; Reason: THookSocketReason; const Value: String); |
| 109 |
public |
| 110 |
function ServerToClient(const Value: AnsiString): UnicodeString; |
| 111 |
function ClientToServer(const Value: AnsiString): AnsiString; overload; |
| 112 |
function ClientToServer(const Value: UnicodeString): AnsiString; overload; |
| 113 |
public |
| 114 |
function FsFindFirstW(const Path: String; var FindData: TWin32FindDataW): Pointer; virtual; |
| 115 |
function FsFindNextW(Handle: Pointer; var FindData: TWin32FindDataW): BOOL; virtual; |
| 116 |
function FsFindClose(Handle: Pointer): Integer; virtual; |
| 117 |
function FsSetTime(const FileName: String; LastAccessTime, LastWriteTime: PWfxFileTime): BOOL; virtual; |
| 118 |
public |
| 119 |
constructor Create(const Encoding: String); virtual; reintroduce; |
| 120 |
function Login: Boolean; override; |
| 121 |
function Clone: TFTPSendEx; virtual; |
| 122 |
procedure CloneTo(AValue: TFTPSendEx); virtual; |
| 123 |
procedure ParseRemote(Value: string); override; |
| 124 |
function FileExists(const FileName: String): Boolean; virtual; |
| 125 |
function CreateDir(const Directory: string): Boolean; override; |
| 126 |
function FileProperties(const FileName: String): Boolean; virtual; |
| 127 |
function CopyFile(const OldName, NewName: String): Boolean; virtual; |
| 128 |
function ChangeMode(const FileName, Mode: String): Boolean; virtual; |
| 129 |
function List(Directory: String; NameList: Boolean): Boolean; override; |
| 130 |
function StoreFile(const FileName: string; Restore: Boolean): Boolean; override; |
| 131 |
function ExecuteCommand(const Command: String; const Directory: String = ''): Boolean; virtual; |
| 132 |
function RetrieveFile(const FileName: string; FileSize: Int64; Restore: Boolean): Boolean; virtual; overload; |
| 133 |
function NetworkError(): Boolean; virtual; |
| 134 |
public |
| 135 |
property Encoding: String write SetEncoding; |
| 136 |
property UseAllocate: Boolean write FUseAllocate; |
| 137 |
property TcpKeepAlive: Boolean write FTcpKeepAlive; |
| 138 |
property PublicKey: String read FPublicKey write FPublicKey; |
| 139 |
property PrivateKey: String read FPrivateKey write FPrivateKey; |
| 140 |
property ShowHidden: Boolean read FShowHidden write FShowHidden; |
| 141 |
property KeepAliveTransfer: Boolean read FKeepAliveTransfer write FKeepAliveTransfer; |
| 142 |
end; |
| 143 |
|
| 144 |
{ TFTPSendExClass } |
| 145 |
|
| 146 |
TFTPSendExClass = class of TFTPSendEx; |
| 147 |
|
| 148 |
implementation |
| 149 |
|
| 150 |
uses |
| 151 |
LazUTF8, DCOSUtils, FtpFunc, FtpUtils, synautil, synsock, DCStrUtils, |
| 152 |
DCDateTimeUtils |
| 153 |
{$IF (FPC_FULLVERSION < 30000)} |
| 154 |
, LazUTF8SysUtils |
| 155 |
{$ENDIF} |
| 156 |
; |
| 157 |
|
| 158 |
{$IF NOT DECLARED(EncodingCP1250)} |
| 159 |
const |
| 160 |
EncodingCP1250 = 'cp1250'; |
| 161 |
EncodingCP1251 = 'cp1251'; |
| 162 |
EncodingCP1252 = 'cp1252'; |
| 163 |
EncodingCP1253 = 'cp1253'; |
| 164 |
EncodingCP1254 = 'cp1254'; |
| 165 |
EncodingCP1255 = 'cp1255'; |
| 166 |
EncodingCP1256 = 'cp1256'; |
| 167 |
EncodingCP1257 = 'cp1257'; |
| 168 |
EncodingCP1258 = 'cp1258'; |
| 169 |
EncodingCP437 = 'cp437'; |
| 170 |
EncodingCP850 = 'cp850'; |
| 171 |
EncodingCP852 = 'cp852'; |
| 172 |
EncodingCP866 = 'cp866'; |
| 173 |
EncodingCP874 = 'cp874'; |
| 174 |
EncodingCP932 = 'cp932'; |
| 175 |
EncodingCP936 = 'cp936'; |
| 176 |
EncodingCP949 = 'cp949'; |
| 177 |
EncodingCP950 = 'cp950'; |
| 178 |
EncodingCPKOI8 = 'koi8'; |
| 179 |
EncodingCPIso1 = 'iso88591'; |
| 180 |
EncodingCPIso2 = 'iso88592'; |
| 181 |
EncodingCPIso15 = 'iso885915'; |
| 182 |
{$ENDIF} |
| 183 |
|
| 184 |
{$IF NOT DECLARED(EncodingCPKOI8R)} |
| 185 |
const |
| 186 |
EncodingCPKOI8R = 'koi8r'; |
| 187 |
{$define KOI8RToUTF8:= KOI8ToUTF8} |
| 188 |
{$define UTF8ToKOI8R:= UTF8ToKOI8} |
| 189 |
{$ENDIF} |
| 190 |
|
| 191 |
function Dummy(const S: String): String; |
| 192 |
begin |
| 193 |
Result:= S; |
| 194 |
end; |
| 195 |
|
| 196 |
function Ymmud(const S: String {$IFDEF FPC_HAS_CPSTRING}; SetTargetCodePage: Boolean = False{$ENDIF}): RawByteString; |
| 197 |
begin |
| 198 |
Result:= S; |
| 199 |
end; |
| 200 |
|
| 201 |
function Utf8ToSys(const S: String {$IFDEF FPC_HAS_CPSTRING}; SetTargetCodePage: Boolean = False{$ENDIF}): RawByteString; |
| 202 |
begin |
| 203 |
Result:= CeUtf8ToSys(S); |
| 204 |
end; |
| 205 |
|
| 206 |
{ TFTPListRecEx } |
| 207 |
|
| 208 |
procedure TFTPListRecEx.Assign(Value: TFTPListRec); |
| 209 |
begin |
| 210 |
inherited Assign(Value); |
| 211 |
Permission:= Value.Permission; |
| 212 |
if Value is TFTPListRecEx then |
| 213 |
Mode:= TFTPListRecEx(Value).Mode |
| 214 |
else |
| 215 |
Mode:= UnixStrToFileAttr(Permission); |
| 216 |
end; |
| 217 |
|
| 218 |
{ TFTPListEx } |
| 219 |
|
| 220 |
procedure TFTPListEx.Clear; |
| 221 |
begin |
| 222 |
FIndex := 0; |
| 223 |
inherited Clear; |
| 224 |
end; |
| 225 |
|
| 226 |
procedure TFTPListEx.ParseLines; |
| 227 |
var |
| 228 |
ATotal: Int64; |
| 229 |
begin |
| 230 |
if FLines.Count > 0 then |
| 231 |
begin |
| 232 |
if Pos('total', FLines[0]) = 1 then |
| 233 |
begin |
| 234 |
if TryStrToInt64(Trim(Copy(FLines[0], 6, MaxInt)), ATotal) then |
| 235 |
FLines.Delete(0); |
| 236 |
end; |
| 237 |
end; |
| 238 |
inherited ParseLines; |
| 239 |
end; |
| 240 |
|
| 241 |
procedure TFTPListEx.FillRecord(const Value: TFTPListRec); |
| 242 |
var |
| 243 |
flr: TFTPListRecEx; |
| 244 |
begin |
| 245 |
inherited FillRecord(Value); |
| 246 |
if Value.Directory and (Value.FileName = '..') then |
| 247 |
begin |
| 248 |
flr := TFTPListRecEx.Create; |
| 249 |
flr.Assign(Value); |
| 250 |
FList.Add(flr); |
| 251 |
end; |
| 252 |
end; |
| 253 |
|
| 254 |
procedure TFTPListEx.Assign(Value: TFTPList); |
| 255 |
var |
| 256 |
flr: TFTPListRecEx; |
| 257 |
n: integer; |
| 258 |
begin |
| 259 |
Clear; |
| 260 |
for n := 0 to Value.Count - 1 do |
| 261 |
begin |
| 262 |
flr := TFTPListRecEx.Create; |
| 263 |
flr.Assign(Value[n]); |
| 264 |
Flist.Add(flr); |
| 265 |
end; |
| 266 |
Lines.Assign(Value.Lines); |
| 267 |
Masks.Assign(Value.Masks); |
| 268 |
UnparsedLines.Assign(Value.UnparsedLines); |
| 269 |
end; |
| 270 |
|
| 271 |
{ TProgressStream } |
| 272 |
|
| 273 |
procedure TProgressStream.DoProgress(Result: Integer); |
| 274 |
var |
| 275 |
Percent: Int64; |
| 276 |
begin |
| 277 |
DoneSize += Result; |
| 278 |
Percent:= DoneSize * 100 div FileSize; |
| 279 |
if ProgressProc(PluginNumber, SourceName, TargetName, Percent) = 1 then |
| 280 |
raise EUserAbort.Create(EmptyStr); |
| 281 |
// Send keepalive also during a transfer |
| 282 |
if TFTPSendEx(FtpSend).KeepAliveTransfer then |
| 283 |
begin |
| 284 |
Percent:= GetTickCount64; |
| 285 |
if (Percent - FTime) > 30000 then |
| 286 |
begin |
| 287 |
FTime:= Percent; |
| 288 |
FtpSend.Sock.SendString(CRLF); |
| 289 |
end; |
| 290 |
end; |
| 291 |
end; |
| 292 |
|
| 293 |
function TProgressStream.Read(var Buffer; Count: Longint): Longint; |
| 294 |
begin |
| 295 |
Result:= inherited Read(Buffer, Count); |
| 296 |
if FileSize > 0 then DoProgress(Result); |
| 297 |
end; |
| 298 |
|
| 299 |
function TProgressStream.Write(const Buffer; Count: Longint): Longint; |
| 300 |
begin |
| 301 |
Result:= inherited Write(Buffer, Count); |
| 302 |
if FileSize > 0 then DoProgress(Result); |
| 303 |
end; |
| 304 |
|
| 305 |
{ TFTPSendEx } |
| 306 |
|
| 307 |
procedure TFTPSendEx.SetEncoding(AValue: String); |
| 308 |
begin |
| 309 |
FEncoding:= AValue; |
| 310 |
|
| 311 |
if FEncoding = EncodingUTF8 then |
| 312 |
begin |
| 313 |
ConvertToUtf8:= @Dummy; |
| 314 |
ConvertFromUtf8:= @Ymmud; |
| 315 |
end |
| 316 |
else if FEncoding = EncodingCPIso1 then |
| 317 |
begin |
| 318 |
ConvertToUtf8:= @ISO_8859_1ToUTF8; |
| 319 |
ConvertFromUtf8:= @UTF8ToISO_8859_1; |
| 320 |
end |
| 321 |
else if FEncoding = EncodingCPIso2 then |
| 322 |
begin |
| 323 |
ConvertToUtf8:= @ISO_8859_2ToUTF8; |
| 324 |
ConvertFromUtf8:= @UTF8ToISO_8859_2; |
| 325 |
end |
| 326 |
else if FEncoding = EncodingCPIso15 then |
| 327 |
begin |
| 328 |
ConvertToUtf8:= @ISO_8859_15ToUTF8; |
| 329 |
ConvertFromUtf8:= @UTF8ToISO_8859_15; |
| 330 |
end |
| 331 |
else if FEncoding = EncodingCP1250 then |
| 332 |
begin |
| 333 |
ConvertToUtf8:= @CP1250ToUTF8; |
| 334 |
ConvertFromUtf8:= @UTF8ToCP1250; |
| 335 |
end |
| 336 |
else if FEncoding = EncodingCP1251 then |
| 337 |
begin |
| 338 |
ConvertToUtf8:= @CP1251ToUTF8; |
| 339 |
ConvertFromUtf8:= @UTF8ToCP1251; |
| 340 |
end |
| 341 |
else if FEncoding = EncodingCP1252 then |
| 342 |
begin |
| 343 |
ConvertToUtf8:= @CP1252ToUTF8; |
| 344 |
ConvertFromUtf8:= @UTF8ToCP1252; |
| 345 |
end |
| 346 |
else if FEncoding = EncodingCP1253 then |
| 347 |
begin |
| 348 |
ConvertToUtf8:= @CP1253ToUTF8; |
| 349 |
ConvertFromUtf8:= @UTF8ToCP1253; |
| 350 |
end |
| 351 |
else if FEncoding = EncodingCP1254 then |
| 352 |
begin |
| 353 |
ConvertToUtf8:= @CP1254ToUTF8; |
| 354 |
ConvertFromUtf8:= @UTF8ToCP1254; |
| 355 |
end |
| 356 |
else if FEncoding = EncodingCP1255 then |
| 357 |
begin |
| 358 |
ConvertToUtf8:= @CP1255ToUTF8; |
| 359 |
ConvertFromUtf8:= @UTF8ToCP1255; |
| 360 |
end |
| 361 |
else if FEncoding = EncodingCP1256 then |
| 362 |
begin |
| 363 |
ConvertToUtf8:= @CP1256ToUTF8; |
| 364 |
ConvertFromUtf8:= @UTF8ToCP1256; |
| 365 |
end |
| 366 |
else if FEncoding = EncodingCP1257 then |
| 367 |
begin |
| 368 |
ConvertToUtf8:= @CP1257ToUTF8; |
| 369 |
ConvertFromUtf8:= @UTF8ToCP1257; |
| 370 |
end |
| 371 |
else if FEncoding = EncodingCP1258 then |
| 372 |
begin |
| 373 |
ConvertToUtf8:= @CP1258ToUTF8; |
| 374 |
ConvertFromUtf8:= @UTF8ToCP1258; |
| 375 |
end |
| 376 |
else if FEncoding = EncodingCP437 then |
| 377 |
begin |
| 378 |
ConvertToUtf8:= @CP437ToUTF8; |
| 379 |
ConvertFromUtf8:= @UTF8ToCP437; |
| 380 |
end |
| 381 |
else if FEncoding = EncodingCP850 then |
| 382 |
begin |
| 383 |
ConvertToUtf8:= @CP850ToUTF8; |
| 384 |
ConvertFromUtf8:= @UTF8ToCP850; |
| 385 |
end |
| 386 |
else if FEncoding = EncodingCP852 then |
| 387 |
begin |
| 388 |
ConvertToUtf8:= @CP852ToUTF8; |
| 389 |
ConvertFromUtf8:= @UTF8ToCP852; |
| 390 |
end |
| 391 |
else if FEncoding = EncodingCP866 then |
| 392 |
begin |
| 393 |
ConvertToUtf8:= @CP866ToUTF8; |
| 394 |
ConvertFromUtf8:= @UTF8ToCP866; |
| 395 |
end |
| 396 |
else if FEncoding = EncodingCP874 then |
| 397 |
begin |
| 398 |
ConvertToUtf8:= @CP874ToUTF8; |
| 399 |
ConvertFromUtf8:= @UTF8ToCP874; |
| 400 |
end |
| 401 |
else if FEncoding = EncodingCP932 then |
| 402 |
begin |
| 403 |
ConvertToUtf8:= @CP932ToUTF8; |
| 404 |
ConvertFromUtf8:= @UTF8ToCP932; |
| 405 |
end |
| 406 |
else if FEncoding = EncodingCP936 then |
| 407 |
begin |
| 408 |
ConvertToUtf8:= @CP936ToUTF8; |
| 409 |
ConvertFromUtf8:= @UTF8ToCP936; |
| 410 |
end |
| 411 |
else if FEncoding = EncodingCP949 then |
| 412 |
begin |
| 413 |
ConvertToUtf8:= @CP949ToUTF8; |
| 414 |
ConvertFromUtf8:= @UTF8ToCP949; |
| 415 |
end |
| 416 |
else if FEncoding = EncodingCP950 then |
| 417 |
begin |
| 418 |
ConvertToUtf8:= @CP950ToUTF8; |
| 419 |
ConvertFromUtf8:= @UTF8ToCP950; |
| 420 |
end |
| 421 |
else if FEncoding = EncodingCPKOI8R then |
| 422 |
begin |
| 423 |
ConvertToUtf8:= @KOI8RToUTF8; |
| 424 |
ConvertFromUtf8:= @UTF8ToKOI8R; |
| 425 |
end; |
| 426 |
end; |
| 427 |
|
| 428 |
function TFTPSendEx.Connect: Boolean; |
| 429 |
var |
| 430 |
Option: Cardinal = 1; |
| 431 |
Message: UnicodeString; |
| 432 |
begin |
| 433 |
Result:= inherited Connect; |
| 434 |
if Result then LogProc(PluginNumber, MSGTYPE_CONNECT, nil); |
| 435 |
// Apply TcpKeepAlive option |
| 436 |
if FTcpKeepAlive and Result then |
| 437 |
begin |
| 438 |
if SetSockOpt(FSock.Socket, SOL_SOCKET, SO_KEEPALIVE, @Option, SizeOf(Option)) <> 0 then |
| 439 |
begin |
| 440 |
Message := UTF8ToUTF16(FSock.GetErrorDesc(synsock.WSAGetLastError)); |
| 441 |
LogProc(PluginNumber, msgtype_importanterror, PWideChar('CSOCK ERROR ' + Message)); |
| 442 |
end; |
| 443 |
end; |
| 444 |
end; |
| 445 |
|
| 446 |
function TFTPSendEx.DataSocket: Boolean; |
| 447 |
var |
| 448 |
Message: UnicodeString; |
| 449 |
begin |
| 450 |
Result:= inherited DataSocket; |
| 451 |
if FDSock.LastError <> 0 then begin |
| 452 |
Message:= UTF8ToUTF16(CeSysToUtf8(FDSock.LastErrorDesc)); |
| 453 |
LogProc(PluginNumber, msgtype_importanterror, PWideChar('DSOCK ERROR ' + Message)); |
| 454 |
end; |
| 455 |
end; |
| 456 |
|
| 457 |
function TFTPSendEx.ListMachine(Directory: String): Boolean; |
| 458 |
var |
| 459 |
v: String; |
| 460 |
start: Boolean; |
| 461 |
s, x, y: Integer; |
| 462 |
flr: TFTPListRecEx; |
| 463 |
pdir, pcdir: Boolean; |
| 464 |
option, value: String; |
| 465 |
begin |
| 466 |
FFTPList.Clear; |
| 467 |
Result := False; |
| 468 |
FDataStream.Clear; |
| 469 |
if Directory <> '' then |
| 470 |
Directory := ' ' + Directory; |
| 471 |
FTPCommand('TYPE A'); |
| 472 |
if not DataSocket then Exit; |
| 473 |
x := FTPCommand('MLSD' + Directory); |
| 474 |
if (x div 100) <> 1 then Exit; |
| 475 |
Result := DataRead(FDataStream); |
| 476 |
if Result then |
| 477 |
begin |
| 478 |
pdir := False; |
| 479 |
pcdir := False; |
| 480 |
option := EmptyStr; |
| 481 |
FDataStream.Position := 0; |
| 482 |
FFTPList.Lines.LoadFromStream(FDataStream); |
| 483 |
for x:= 0 to FFTPList.Lines.Count - 1 do |
| 484 |
begin |
| 485 |
s:= 1; |
| 486 |
start := False; |
| 487 |
flr := TFTPListRecEx.Create; |
| 488 |
v:= FFTPList.Lines[x]; |
| 489 |
flr.OriginalLine:= v; |
| 490 |
// DoStatus(True, v); |
| 491 |
for y:= 1 to Length(v) do |
| 492 |
begin |
| 493 |
if (not start) and (v[y] = '=') then |
| 494 |
begin |
| 495 |
option:= LowerCase(Copy(v, s, y - s)); |
| 496 |
start := True; |
| 497 |
s:= y + 1; |
| 498 |
end |
| 499 |
else if v[y] = ';' then |
| 500 |
begin |
| 501 |
value:= LowerCase(Copy(v, s, y - s)); |
| 502 |
if (option = 'type') then |
| 503 |
begin |
| 504 |
// Skip 'cdir' entry |
| 505 |
if (value = 'cdir') then |
| 506 |
begin |
| 507 |
FreeAndNil(flr); |
| 508 |
Break; |
| 509 |
end; |
| 510 |
// Parent directory entry |
| 511 |
pcdir := (value = 'pdir'); |
| 512 |
if pcdir then |
| 513 |
begin |
| 514 |
// Skip duplicate 'pdir' entry |
| 515 |
if pdir then |
| 516 |
begin |
| 517 |
FreeAndNil(flr); |
| 518 |
Break; |
| 519 |
end; |
| 520 |
pdir := True; |
| 521 |
end; |
| 522 |
flr.Directory:= (value = 'os.unix=symlink'); |
| 523 |
if flr.Directory then |
| 524 |
flr.Mode := flr.Mode or S_IFLNK |
| 525 |
else begin |
| 526 |
flr.Directory := pcdir or (value = 'dir'); |
| 527 |
end; |
| 528 |
end |
| 529 |
else if (option = 'modify') then |
| 530 |
begin |
| 531 |
flr.FileTime:= DecodeMachineTime(value); |
| 532 |
end |
| 533 |
else if (option = 'size') then |
| 534 |
begin |
| 535 |
flr.FileSize:= StrToInt64Def(value, 0); |
| 536 |
end |
| 537 |
else if (option = 'unix.mode') then |
| 538 |
begin |
| 539 |
flr.Mode:= flr.Mode or OctToDec(value); |
| 540 |
end; |
| 541 |
if (y < Length(v)) and (v[y + 1] = ' ') then |
| 542 |
begin |
| 543 |
if (flr.Directory and pcdir) then |
| 544 |
flr.FileName:= '..' |
| 545 |
else |
| 546 |
flr.FileName:= SeparateLeft(Copy(v, y + 2, MaxInt), ' -> '); |
| 547 |
Break; |
| 548 |
end; |
| 549 |
start := False; |
| 550 |
s:= y + 1; |
| 551 |
end; |
| 552 |
end; |
| 553 |
if Assigned(flr) then FFTPList.List.Add(flr); |
| 554 |
end; |
| 555 |
end; |
| 556 |
FDataStream.Position := 0; |
| 557 |
end; |
| 558 |
|
| 559 |
procedure TFTPSendEx.DoStatus(Response: Boolean; const Value: string); |
| 560 |
var |
| 561 |
Index: Integer; |
| 562 |
Message: UnicodeString; |
| 563 |
begin |
| 564 |
Index:= Pos('PASS ', Value); |
| 565 |
if Index = 0 then |
| 566 |
Message:= ServerToClient(Value) |
| 567 |
else begin |
| 568 |
Message:= ServerToClient(Copy(Value, 1, Index + 4)) + '********'; |
| 569 |
end; |
| 570 |
LogProc(PluginNumber, msgtype_details, PWideChar(Message)); |
| 571 |
if FSock.LastError <> 0 then begin |
| 572 |
Message:= UTF8ToUTF16(CeSysToUtf8(FSock.LastErrorDesc)); |
| 573 |
LogProc(PluginNumber, msgtype_importanterror, PWideChar('CSOCK ERROR ' + Message)); |
| 574 |
end; |
| 575 |
end; |
| 576 |
|
| 577 |
procedure TFTPSendEx.OnSocketStatus(Sender: TObject; Reason: THookSocketReason; const Value: String); |
| 578 |
var |
| 579 |
MsgType: Integer; |
| 580 |
begin |
| 581 |
if (Reason in [HR_ResolvingBegin, HR_ResolvingEnd, HR_Error]) then |
| 582 |
begin |
| 583 |
if (Length(Value) > 0) then |
| 584 |
begin |
| 585 |
if Reason = HR_Error then |
| 586 |
MsgType:= msgtype_importanterror |
| 587 |
else begin |
| 588 |
MsgType:= msgtype_details; |
| 589 |
end; |
| 590 |
LogProc(PluginNumber, MsgType, PWideChar(ServerToClient(Value))); |
| 591 |
end; |
| 592 |
end; |
| 593 |
end; |
| 594 |
|
| 595 |
function TFTPSendEx.ClientToServer(const Value: AnsiString): AnsiString; |
| 596 |
begin |
| 597 |
Result:= ConvertFromUtf8(Value); |
| 598 |
end; |
| 599 |
|
| 600 |
function TFTPSendEx.ClientToServer(const Value: UnicodeString): AnsiString; |
| 601 |
begin |
| 602 |
Result:= ConvertFromUtf8(UTF16ToUTF8(Value)); |
| 603 |
end; |
| 604 |
|
| 605 |
function TFTPSendEx.ServerToClient(const Value: AnsiString): UnicodeString; |
| 606 |
begin |
| 607 |
Result:= UTF8ToUTF16(ConvertToUtf8(Value)); |
| 608 |
end; |
| 609 |
|
| 610 |
function TFTPSendEx.FsFindFirstW(const Path: String; var FindData: TWin32FindDataW): Pointer; |
| 611 |
begin |
| 612 |
Result:= nil; |
| 613 |
// Get directory listing |
| 614 |
if List(Path, False) then |
| 615 |
begin |
| 616 |
if FtpList.Count > 0 then |
| 617 |
begin |
| 618 |
// Save file list |
| 619 |
Result:= TFTPListEx.Create; |
| 620 |
TFTPListEx(Result).Assign(FtpList); |
| 621 |
FsFindNextW(Result, FindData); |
| 622 |
end; |
| 623 |
end; |
| 624 |
end; |
| 625 |
|
| 626 |
function TFTPSendEx.FsFindNextW(Handle: Pointer; var FindData: TWin32FindDataW): BOOL; |
| 627 |
var |
| 628 |
I: Integer; |
| 629 |
FtpList: TFTPListEx absolute Handle; |
| 630 |
begin |
| 631 |
Result := False; |
| 632 |
if Assigned(FtpList) then |
| 633 |
begin |
| 634 |
I := FtpList.FIndex; |
| 635 |
if I < FtpList.Count then |
| 636 |
begin |
| 637 |
FillChar(FindData, SizeOf(FindData), 0); |
| 638 |
StrPCopy(FindData.cFileName, ServerToClient(FtpList.Items[I].FileName)); |
| 639 |
FindData.dwFileAttributes := FindData.dwFileAttributes or FILE_ATTRIBUTE_UNIX_MODE; |
| 640 |
if TFTPListEx(FtpList).Items[I].Directory then |
| 641 |
FindData.dwFileAttributes := FindData.dwFileAttributes or FILE_ATTRIBUTE_DIRECTORY |
| 642 |
else begin |
| 643 |
FindData.nFileSizeLow := (FtpList.Items[I].FileSize and MAXDWORD); |
| 644 |
FindData.nFileSizeHigh := (FtpList.Items[I].FileSize shr $20); |
| 645 |
end; |
| 646 |
// set Unix permissions |
| 647 |
FindData.dwReserved0 := TFTPListRecEx(FtpList.Items[I]).Mode; |
| 648 |
FindData.ftLastWriteTime := TWfxFileTime(DateTimeToWinFileTime(FtpList.Items[I].FileTime)); |
| 649 |
Inc(FtpList.FIndex); |
| 650 |
Result := True; |
| 651 |
end; |
| 652 |
end; |
| 653 |
end; |
| 654 |
|
| 655 |
function TFTPSendEx.FsFindClose(Handle: Pointer): Integer; |
| 656 |
begin |
| 657 |
Result:= 0; |
| 658 |
FreeAndNil(TFTPListEx(Handle)); |
| 659 |
end; |
| 660 |
|
| 661 |
constructor TFTPSendEx.Create(const Encoding: String); |
| 662 |
begin |
| 663 |
inherited Create; |
| 664 |
FTimeout:= 15000; |
| 665 |
FDirectFile:= True; |
| 666 |
|
| 667 |
ConvertToUtf8:= @CeSysToUtf8; |
| 668 |
ConvertFromUtf8:= @Utf8ToSys; |
| 669 |
|
| 670 |
Sock.OnStatus:= OnSocketStatus; |
| 671 |
|
| 672 |
FEncoding:= NormalizeEncoding(Encoding); |
| 673 |
FAuto:= (FEncoding = '') or (FEncoding = 'auto'); |
| 674 |
|
| 675 |
if not FAuto then SetEncoding(FEncoding); |
| 676 |
|
| 677 |
FFtpList.Free; |
| 678 |
FFtpList:= TFTPListEx.Create; |
| 679 |
|
| 680 |
// Move mostly used UNIX format to first |
| 681 |
FFtpList.Masks.Exchange(0, 2); |
| 682 |
// Windows CE 5.1 (insert before BullGCOS7) |
| 683 |
FFtpList.Masks.Insert(35, 'MM DD YY hh mm !S* n*'); |
| 684 |
FFtpList.Masks.Insert(36, 'MM DD YY hh mm $ d!n*'); |
| 685 |
end; |
| 686 |
|
| 687 |
function TFTPSendEx.Login: Boolean; |
| 688 |
var |
| 689 |
Index: Integer; |
| 690 |
Client: Boolean = False; |
| 691 |
begin |
| 692 |
Result:= inherited Login; |
| 693 |
if Result then |
| 694 |
begin |
| 695 |
if IsTLS and Sock.SSL.SSLEnabled then |
| 696 |
begin |
| 697 |
LogProc(PluginNumber, msgtype_details, PWideChar('TLS Library ' + UTF8ToUTF16(FSock.SSL.LibVersion))); |
| 698 |
LogProc(PluginNumber, msgtype_details, PWideChar('TLS Protocol ' + UTF8ToUTF16(FSock.SSL.GetSSLVersion))); |
| 699 |
LogProc(PluginNumber, msgtype_details, PWideChar('TLS Cipher ' + UTF8ToUTF16(FSock.SSL.GetCipherName))); |
| 700 |
end; |
| 701 |
if (FTPCommand('FEAT') div 100) = 2 then |
| 702 |
begin |
| 703 |
for Index:= 0 to FFullResult.Count - 1 do |
| 704 |
begin |
| 705 |
if not Client then Client := Pos('CLNT', FFullResult[Index]) > 0; |
| 706 |
if not FMachine then FMachine:= Pos('MLST', FFullResult[Index]) > 0; |
| 707 |
if not FMachine then FMachine:= Pos('MLSD', FFullResult[Index]) > 0; |
| 708 |
if not FUnicode then FUnicode:= Pos('UTF8', FFullResult[Index]) > 0; |
| 709 |
if not FSetTime then FSetTime:= Pos('MFMT', FFullResult[Index]) > 0; |
| 710 |
end; |
| 711 |
// Some servers refuse to enable UTF-8 if client does not send CLNT command |
| 712 |
if Client then |
| 713 |
begin |
| 714 |
FTPCommand('CLNT Double Commander (UTF-8)'); |
| 715 |
end; |
| 716 |
if FUnicode and FAuto then |
| 717 |
begin |
| 718 |
ConvertToUtf8:= @Dummy; |
| 719 |
ConvertFromUtf8:= @Ymmud; |
| 720 |
FTPCommand('OPTS UTF8 ON'); |
| 721 |
end; |
| 722 |
end; |
| 723 |
if (not FMachine) and FShowHidden then |
| 724 |
begin |
| 725 |
if inherited List('-la', False) then |
| 726 |
FShowHiddenText:= '-la' |
| 727 |
else begin |
| 728 |
DoStatus(False, 'Server does not seem to support LIST -a'); |
| 729 |
end; |
| 730 |
end; |
| 731 |
end; |
| 732 |
end; |
| 733 |
|
| 734 |
function TFTPSendEx.Clone: TFTPSendEx; |
| 735 |
begin |
| 736 |
Result:= TFTPSendExClass(ClassType).Create(FEncoding); |
| 737 |
CloneTo(Result); |
| 738 |
end; |
| 739 |
|
| 740 |
procedure TFTPSendEx.CloneTo(AValue: TFTPSendEx); |
| 741 |
begin |
| 742 |
AValue.TargetHost := TargetHost; |
| 743 |
AValue.TargetPort:= TargetPort; |
| 744 |
AValue.PassiveMode:= PassiveMode; |
| 745 |
AValue.AutoTLS:= AutoTLS; |
| 746 |
AValue.FullSSL:= FullSSL; |
| 747 |
AValue.UseAllocate:= FUseAllocate; |
| 748 |
AValue.UserName:= UserName; |
| 749 |
AValue.Password:= Password; |
| 750 |
AValue.KeepAliveTransfer:= KeepAliveTransfer; |
| 751 |
AValue.PublicKey:= FPublicKey; |
| 752 |
AValue.PrivateKey:= FPrivateKey; |
| 753 |
AValue.ShowHidden:= FShowHidden; |
| 754 |
AValue.TcpKeepAlive:= FTcpKeepAlive; |
| 755 |
|
| 756 |
AValue.Sock.HTTPTunnelIP:= Sock.HTTPTunnelIP; |
| 757 |
AValue.Sock.HTTPTunnelPort:= Sock.HTTPTunnelPort; |
| 758 |
AValue.Sock.HTTPTunnelUser:= Sock.HTTPTunnelUser; |
| 759 |
AValue.Sock.HTTPTunnelPass:= Sock.HTTPTunnelPass; |
| 760 |
|
| 761 |
AValue.Sock.SocksIP:= Sock.SocksIP; |
| 762 |
AValue.Sock.SocksType:= Sock.SocksType; |
| 763 |
AValue.Sock.SocksPort:= Sock.SocksPort; |
| 764 |
AValue.Sock.SocksUsername:= Sock.SocksUsername; |
| 765 |
AValue.Sock.SocksPassword:= Sock.SocksPassword; |
| 766 |
|
| 767 |
AValue.DSock.HTTPTunnelIP:= DSock.HTTPTunnelIP; |
| 768 |
AValue.DSock.HTTPTunnelPort:= DSock.HTTPTunnelPort; |
| 769 |
AValue.DSock.HTTPTunnelUser:= DSock.HTTPTunnelUser; |
| 770 |
AValue.DSock.HTTPTunnelPass:= DSock.HTTPTunnelPass; |
| 771 |
|
| 772 |
AValue.DSock.SocksIP:= DSock.SocksIP; |
| 773 |
AValue.DSock.SocksType:= DSock.SocksType; |
| 774 |
AValue.DSock.SocksPort:= DSock.SocksPort; |
| 775 |
AValue.DSock.SocksUsername:= DSock.SocksUsername; |
| 776 |
AValue.DSock.SocksPassword:= DSock.SocksPassword; |
| 777 |
end; |
| 778 |
|
| 779 |
procedure TFTPSendEx.ParseRemote(Value: string); |
| 780 |
var |
| 781 |
RemoteIP: String; |
| 782 |
begin |
| 783 |
inherited ParseRemote(Value); |
| 784 |
RemoteIP:= FSock.GetRemoteSinIP; |
| 785 |
if FDataIP = '0.0.0.0' then FDataIP:= RemoteIP |
| 786 |
else if IsIpPrivate(FDataIP) and (IsIpPrivate(RemoteIP) = False) then |
| 787 |
begin |
| 788 |
FDataIP:= RemoteIP; |
| 789 |
DoStatus(False, 'Server reports local IP -> Redirect to: ' + FDataIP); |
| 790 |
end; |
| 791 |
end; |
| 792 |
|
| 793 |
function TFTPSendEx.FileExists(const FileName: String): Boolean; |
| 794 |
begin |
| 795 |
Result:= (Self.FileSize(FileName) >= 0); |
| 796 |
end; |
| 797 |
|
| 798 |
function TFTPSendEx.CreateDir(const Directory: string): Boolean; |
| 799 |
var |
| 800 |
sOldPath: AnsiString; |
| 801 |
begin |
| 802 |
sOldPath := GetCurrentDir; |
| 803 |
if ChangeWorkingDir(Directory) then |
| 804 |
Result := ChangeWorkingDir(sOldPath) |
| 805 |
else begin |
| 806 |
Result := inherited CreateDir(Directory); |
| 807 |
end; |
| 808 |
end; |
| 809 |
|
| 810 |
function TFTPSendEx.ExecuteCommand(const Command, Directory: String): Boolean; |
| 811 |
begin |
| 812 |
Result:= (FTPCommand(Command) div 100) = 2; |
| 813 |
end; |
| 814 |
|
| 815 |
function TFTPSendEx.FileProperties(const FileName: String): Boolean; |
| 816 |
begin |
| 817 |
Result:= (FTPCommand('STAT' + #32 + FileName) div 100) = 2; |
| 818 |
end; |
| 819 |
|
| 820 |
function TFTPSendEx.CopyFile(const OldName, NewName: String): Boolean; |
| 821 |
begin |
| 822 |
Result:= False; |
| 823 |
end; |
| 824 |
|
| 825 |
function TFTPSendEx.ChangeMode(const FileName, Mode: String): Boolean; |
| 826 |
begin |
| 827 |
Result:= (FTPCommand('SITE CHMOD' + #32 + Mode + #32 + FileName) div 100) = 2; |
| 828 |
end; |
| 829 |
|
| 830 |
function TFTPSendEx.List(Directory: String; NameList: Boolean): Boolean; |
| 831 |
var |
| 832 |
Message: UnicodeString; |
| 833 |
begin |
| 834 |
Result:= ChangeWorkingDir(Directory); |
| 835 |
if Result then |
| 836 |
begin |
| 837 |
if FMachine then |
| 838 |
Result:= ListMachine(EmptyStr) |
| 839 |
else begin |
| 840 |
Result:= inherited List(FShowHiddenText, NameList); |
| 841 |
end; |
| 842 |
if (Result = False) and (FSock.WaitingData > 0) then |
| 843 |
begin |
| 844 |
Message:= UnicodeString(FSock.RecvPacket(1000)); |
| 845 |
LogProc(PluginNumber, msgtype_importanterror, PWideChar(Message)); |
| 846 |
end; |
| 847 |
FTPCommand('TYPE I'); |
| 848 |
end; |
| 849 |
end; |
| 850 |
|
| 851 |
function TFTPSendEx.FsSetTime(const FileName: String; LastAccessTime, LastWriteTime: PWfxFileTime): BOOL; |
| 852 |
var |
| 853 |
Time: String; |
| 854 |
begin |
| 855 |
if not FSetTime then Exit(False); |
| 856 |
if (LastWriteTime = nil) then Exit(False); |
| 857 |
Time:= FormatMachineTime(LastWriteTime^); |
| 858 |
Result:= FTPCommand('MFMT ' + Time + ' ' + FileName) = 213; |
| 859 |
end; |
| 860 |
|
| 861 |
function TFTPSendEx.StoreFile(const FileName: string; Restore: Boolean): Boolean; |
| 862 |
var |
| 863 |
StorSize: Int64; |
| 864 |
RestoreAt: Int64 = 0; |
| 865 |
SendStream: TProgressStream; |
| 866 |
begin |
| 867 |
Result := False; |
| 868 |
Restore := Restore and FCanResume; |
| 869 |
if Restore then |
| 870 |
begin |
| 871 |
RestoreAt := Self.FileSize(FileName); |
| 872 |
if RestoreAt < 0 then RestoreAt := 0; |
| 873 |
end; |
| 874 |
|
| 875 |
SendStream := TProgressStream.Create(FDirectFileName, fmOpenRead or fmShareDenyWrite); |
| 876 |
|
| 877 |
SendStream.FtpSend:= Self; |
| 878 |
SendStream.PluginNumber:= PluginNumber; |
| 879 |
SendStream.ProgressProc:= ProgressProc; |
| 880 |
SendStream.TargetName:= PWideChar(ServerToClient(FileName)); |
| 881 |
SendStream.SourceName:= PWideChar(CeUtf8ToUtf16(FDirectFileName)); |
| 882 |
|
| 883 |
try |
| 884 |
if not DataSocket then Exit; |
| 885 |
FTPCommand('TYPE I'); |
| 886 |
StorSize := SendStream.Size; |
| 887 |
if not FCanResume then RestoreAt := 0; |
| 888 |
if RestoreAt > StorSize then RestoreAt := 0; |
| 889 |
if (StorSize > 0) and (RestoreAt = StorSize) then |
| 890 |
begin |
| 891 |
Result := True; |
| 892 |
Exit; |
| 893 |
end; |
| 894 |
SendStream.FileSize := StorSize; |
| 895 |
SendStream.DoneSize := RestoreAt; |
| 896 |
if FUseAllocate then FTPCommand('ALLO ' + IntToStr(StorSize - RestoreAt)); |
| 897 |
if FCanResume then |
| 898 |
begin |
| 899 |
if (FTPCommand('REST ' + IntToStr(RestoreAt)) div 100) <> 3 then |
| 900 |
Exit; |
| 901 |
end; |
| 902 |
SendStream.Position := RestoreAt; |
| 903 |
if (FTPCommand('STOR ' + FileName) div 100) <> 1 then |
| 904 |
Exit; |
| 905 |
Result := DataWrite(SendStream); |
| 906 |
finally |
| 907 |
SendStream.Free; |
| 908 |
end; |
| 909 |
end; |
| 910 |
|
| 911 |
function TFTPSendEx.RetrieveFile(const FileName: string; FileSize: Int64; Restore: Boolean): Boolean; |
| 912 |
var |
| 913 |
RetrStream: TProgressStream; |
| 914 |
begin |
| 915 |
Result := False; |
| 916 |
if not DataSocket then Exit; |
| 917 |
Restore := Restore and FCanResume; |
| 918 |
|
| 919 |
if Restore and mbFileExists(FDirectFileName) then |
| 920 |
RetrStream := TProgressStream.Create(FDirectFileName, fmOpenWrite or fmShareExclusive) |
| 921 |
else begin |
| 922 |
RetrStream := TProgressStream.Create(FDirectFileName, fmCreate or fmShareDenyWrite) |
| 923 |
end; |
| 924 |
|
| 925 |
RetrStream.FtpSend := Self; |
| 926 |
RetrStream.FileSize := FileSize; |
| 927 |
RetrStream.PluginNumber := PluginNumber; |
| 928 |
RetrStream.ProgressProc := ProgressProc; |
| 929 |
RetrStream.SourceName := PWideChar(ServerToClient(FileName)); |
| 930 |
RetrStream.TargetName := PWideChar(CeUtf8ToUtf16(FDirectFileName)); |
| 931 |
|
| 932 |
try |
| 933 |
FTPCommand('TYPE I'); |
| 934 |
if Restore then |
| 935 |
begin |
| 936 |
RetrStream.DoneSize := RetrStream.Size; |
| 937 |
RetrStream.Position := RetrStream.DoneSize; |
| 938 |
if (FTPCommand('REST ' + IntToStr(RetrStream.DoneSize)) div 100) <> 3 then |
| 939 |
Exit; |
| 940 |
end; |
| 941 |
if (FTPCommand('RETR ' + FileName) div 100) <> 1 then |
| 942 |
Exit; |
| 943 |
Result := DataRead(RetrStream); |
| 944 |
finally |
| 945 |
RetrStream.Free; |
| 946 |
end; |
| 947 |
end; |
| 948 |
|
| 949 |
function TFTPSendEx.NetworkError(): Boolean; |
| 950 |
begin |
| 951 |
Result := FSock.CanRead(0); |
| 952 |
end; |
| 953 |
|
| 954 |
end. |
| 955 |
|