Debian Codegraph @

plugins/wfx/ftp/src/sftp/sftpsend.pas

raw
1 {
2 Double commander
3 -------------------------------------------------------------------------
4 Wfx plugin for working with File Transfer Protocol
5
6 Copyright (C) 2013-2024 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 SftpSend;
24
25 {$mode delphi}
26 {$pointermath on}
27
28 interface
29
30 uses
31 Classes, SysUtils, WfxPlugin, ftpsend, ScpSend, libssh, FtpAdv;
32
33 type
34
35 { TSftpSend }
36
37 TSftpSend = class(TScpSend)
38 private
39 function FileClose(Handle: Pointer): Boolean;
40 protected
41 FCopySCP: Boolean;
42 FSFTPSession: PLIBSSH2_SFTP;
43 protected
44 function Connect: Boolean; override;
45 public
46 constructor Create(const Encoding: String); override;
47 function Login: Boolean; override;
48 function Logout: Boolean; override;
49 function GetCurrentDir: String; override;
50 function FileSize(const FileName: String): Int64; override;
51 function CreateDir(const Directory: string): Boolean; override;
52 function DeleteDir(const Directory: string): Boolean; override;
53 function DeleteFile(const FileName: string): Boolean; override;
54 function ChangeWorkingDir(const Directory: string): Boolean; override;
55 function RenameFile(const OldName, NewName: string): Boolean; override;
56 function ChangeMode(const FileName, Mode: String): Boolean; override;
57 function StoreFile(const FileName: string; Restore: Boolean): Boolean; override;
58 function RetrieveFile(const FileName: string; FileSize: Int64; Restore: Boolean): Boolean; override;
59 public
60 function FsFindFirstW(const Path: String; var FindData: TWin32FindDataW): Pointer; override;
61 function FsFindNextW(Handle: Pointer; var FindData: TWin32FindDataW): BOOL; override;
62 function FsFindClose(Handle: Pointer): Integer; override;
63 function FsSetTime(const FileName: String; LastAccessTime, LastWriteTime: PFileTime): BOOL; override;
64 public
65 property CopySCP: Boolean read FCopySCP write FCopySCP;
66 end;
67
68 implementation
69
70 uses
71 LazUTF8, DCBasicTypes, DCDateTimeUtils, DCStrUtils, DCOSUtils, FtpFunc, CTypes,
72 DCClassesUtf8, DCFileAttributes, DCConvertEncoding;
73
74 const
75 SMB_BUFFER_SIZE = 131072;
76
77 type
78 PFindRec = ^TFindRec;
79 TFindRec = record
80 Path: String;
81 Handle: PLIBSSH2_SFTP_HANDLE;
82 end;
83
84 { TSftpSend }
85
86 function TSftpSend.FileClose(Handle: Pointer): Boolean;
87 begin
88 FLastError:= 0;
89 if Assigned(Handle) then
90 repeat
91 FLastError:= libssh2_sftp_close(Handle);
92 DoProgress(100);
93 FSock.CanRead(10);
94 until FLastError <> LIBSSH2_ERROR_EAGAIN;
95 Result:= (FLastError = 0);
96 end;
97
98 function TSftpSend.Connect: Boolean;
99 begin
100 Result:= inherited Connect;
101
102 if Result then
103 begin
104 FSFTPSession := libssh2_sftp_init(FSession);
105
106 Result:= Assigned(FSFTPSession);
107
108 if not Result then begin
109 libssh2_session_free(FSession);
110 FSock.CloseSocket;
111 end;
112 end;
113 end;
114
115 constructor TSftpSend.Create(const Encoding: String);
116 begin
117 inherited Create(Encoding);
118 FCanResume := True;
119 end;
120
121 function TSftpSend.Login: Boolean;
122 var
123 Return: Integer;
124 begin
125 Result:= Connect;
126 if Result then
127 begin
128 if FAuto then DetectEncoding;
129
130 if (Length(FCurrentDir) = 0) then
131 begin
132 SetLength(FCurrentDir, MAX_PATH + 1);
133 Return:= libssh2_sftp_realpath(FSFTPSession, '.', PAnsiChar(FCurrentDir), MAX_PATH);
134 if Return < 1 then
135 FCurrentDir:= '/'
136 else begin
137 SetLength(FCurrentDir, Return);
138 FCurrentDir:= CeUtf16ToUtf8(ServerToClient(FCurrentDir));
139 end;
140 DoStatus(False, 'Remote directory: ' + FCurrentDir);
141 end;
142 end;
143 end;
144
145 function TSftpSend.Logout: Boolean;
146 begin
147 Result:= libssh2_sftp_shutdown(FSFTPSession) = 0;
148 Result:= Result and inherited Logout;
149 end;
150
151 function TSftpSend.GetCurrentDir: String;
152 begin
153 Result:= FCurrentDir;
154 end;
155
156 function TSftpSend.FileSize(const FileName: String): Int64;
157 var
158 Attributes: LIBSSH2_SFTP_ATTRIBUTES;
159 begin
160 repeat
161 FLastError:= libssh2_sftp_stat(FSFTPSession, PAnsiChar(FileName), @Attributes);
162 if (FLastError = 0) then Exit(Attributes.filesize);
163 FSock.CanRead(10);
164 DoProgress(0);
165 until FLastError <> LIBSSH2_ERROR_EAGAIN;
166 Result:= -1;
167 end;
168
169 function TSftpSend.CreateDir(const Directory: string): Boolean;
170 var
171 Return: Integer;
172 Attributes: LIBSSH2_SFTP_ATTRIBUTES;
173 begin
174 Return:= libssh2_sftp_mkdir(FSFTPSession,
175 PAnsiChar(Directory),
176 LIBSSH2_SFTP_S_IRWXU or
177 LIBSSH2_SFTP_S_IRGRP or LIBSSH2_SFTP_S_IXGRP or
178 LIBSSH2_SFTP_S_IROTH or LIBSSH2_SFTP_S_IXOTH);
179 if (Return <> 0) then begin
180 Return:= libssh2_sftp_stat(FSFTPSession, PAnsiChar(Directory), @Attributes);
181 end;
182 Result:= (Return = 0);
183 end;
184
185 function TSftpSend.DeleteDir(const Directory: string): Boolean;
186 begin
187 Result:= libssh2_sftp_rmdir(FSFTPSession, PAnsiChar(Directory)) = 0;
188 end;
189
190 function TSftpSend.DeleteFile(const FileName: string): Boolean;
191 begin
192 Result:= libssh2_sftp_unlink(FSFTPSession, PAnsiChar(FileName)) = 0;
193 end;
194
195 function TSftpSend.ChangeWorkingDir(const Directory: string): Boolean;
196 var
197 Attributes: LIBSSH2_SFTP_ATTRIBUTES;
198 begin
199 Result:= libssh2_sftp_stat(FSFTPSession, PAnsiChar(Directory), @Attributes) = 0;
200 if Result then FCurrentDir:= Directory;
201 end;
202
203 function TSftpSend.RenameFile(const OldName, NewName: string): Boolean;
204 begin
205 Result:= libssh2_sftp_rename(FSFTPSession, PAnsiChar(OldName), PAnsiChar(NewName)) = 0;
206 end;
207
208 function TSftpSend.ChangeMode(const FileName, Mode: String): Boolean;
209 var
210 Attributes: LIBSSH2_SFTP_ATTRIBUTES;
211 begin
212 Attributes.permissions:= OctToDec(Mode);
213 Attributes.flags:= LIBSSH2_SFTP_ATTR_PERMISSIONS;
214 Result:= libssh2_sftp_setstat(FSFTPSession, PAnsiChar(FileName), @Attributes) = 0;
215 end;
216
217 function TSftpSend.StoreFile(const FileName: string; Restore: Boolean): Boolean;
218 var
219 Index: PtrInt;
220 FBuffer: PByte;
221 FileSize: Int64;
222 BytesRead: Integer;
223 BytesToRead: Integer;
224 BytesWritten: PtrInt;
225 BytesToWrite: Integer;
226 SendStream: TFileStreamEx;
227 TotalBytesToWrite: Int64 = 0;
228 TargetHandle: PLIBSSH2_SFTP_HANDLE = nil;
229 Flags: cint = LIBSSH2_FXF_CREAT or LIBSSH2_FXF_WRITE;
230 begin
231 if FCopySCP then begin
232 Result:= inherited StoreFile(FileName, Restore);
233 Exit;
234 end;
235
236 SendStream := TFileStreamEx.Create(FDirectFileName, fmOpenRead or fmShareDenyWrite);
237
238 TargetName:= PWideChar(ServerToClient(FileName));
239 SourceName:= PWideChar(CeUtf8ToUtf16(FDirectFileName));
240
241 FileSize:= SendStream.Size;
242 FBuffer:= GetMem(SMB_BUFFER_SIZE);
243 libssh2_session_set_blocking(FSession, 0);
244 try
245 if not Restore then
246 begin
247 TotalBytesToWrite:= FileSize;
248 Flags:= Flags or LIBSSH2_FXF_TRUNC
249 end
250 else begin
251 TotalBytesToWrite:= Self.FileSize(FileName);
252 if (FileSize = TotalBytesToWrite) then Exit(True);
253 if TotalBytesToWrite < 0 then TotalBytesToWrite:= 0;
254 SendStream.Seek(TotalBytesToWrite, soBeginning);
255 TotalBytesToWrite := FileSize - TotalBytesToWrite;
256 Flags:= Flags or LIBSSH2_FXF_APPEND;
257 end;
258
259 // Open remote file
260 repeat
261 TargetHandle:= libssh2_sftp_open(FSFTPSession,
262 PAnsiChar(FileName),
263 Flags, $1A0);
264 if (TargetHandle = nil) then
265 begin
266 FLastError:= libssh2_session_last_errno(FSession);
267 if (FLastError <> LIBSSH2_ERROR_EAGAIN) then Exit(False);
268 if (FileSize > 0) then DoProgress((FileSize - TotalBytesToWrite) * 100 div FileSize);
269 FSock.CanRead(10);
270 end;
271 until not ((TargetHandle = nil) and (FLastError = LIBSSH2_ERROR_EAGAIN));
272
273 BytesToRead:= SMB_BUFFER_SIZE;
274 while (TotalBytesToWrite > 0) do
275 begin
276 if (BytesToRead > TotalBytesToWrite) then begin
277 BytesToRead:= TotalBytesToWrite;
278 end;
279 BytesRead:= SendStream.Read(FBuffer^, BytesToRead);
280 if (BytesRead = 0) then Exit(False);
281 // Start write operation
282 Index:= 0;
283 BytesToWrite:= BytesRead;
284 while (BytesToWrite > 0) do
285 begin
286 repeat
287 BytesWritten:= libssh2_sftp_write(TargetHandle, FBuffer + Index, BytesToWrite);
288 if BytesWritten = LIBSSH2_ERROR_EAGAIN then begin
289 DoProgress((FileSize - TotalBytesToWrite) * 100 div FileSize);
290 FSock.CanRead(10);
291 end;
292 until BytesWritten <> LIBSSH2_ERROR_EAGAIN;
293 if (BytesWritten < 0) then Exit(False);
294 Dec(TotalBytesToWrite, BytesWritten);
295 Dec(BytesToWrite, BytesWritten);
296 Inc(Index, BytesWritten);
297 end;
298 DoProgress((FileSize - TotalBytesToWrite) * 100 div FileSize);
299 end;
300 Result:= True;
301 finally
302 SendStream.Free;
303 FreeMem(FBuffer);
304 Result:= FileClose(TargetHandle) and Result;
305 libssh2_session_set_blocking(FSession, 1);
306 end;
307 end;
308
309 function TSftpSend.RetrieveFile(const FileName: string; FileSize: Int64;
310 Restore: Boolean): Boolean;
311 var
312 FBuffer: PByte;
313 BytesRead: PtrInt;
314 RetrStream: TFileStreamEx;
315 TotalBytesToRead: Int64 = 0;
316 SourceHandle: PLIBSSH2_SFTP_HANDLE;
317 begin
318 if FCopySCP then begin
319 Result:= inherited RetrieveFile(FileName, FileSize, Restore);
320 Exit;
321 end;
322
323 if Restore and mbFileExists(FDirectFileName) then
324 RetrStream := TFileStreamEx.Create(FDirectFileName, fmOpenWrite or fmShareExclusive)
325 else begin
326 RetrStream := TFileStreamEx.Create(FDirectFileName, fmCreate or fmShareDenyWrite)
327 end;
328
329 SourceName := PWideChar(ServerToClient(FileName));
330 TargetName := PWideChar(CeUtf8ToUtf16(FDirectFileName));
331
332 if Restore then TotalBytesToRead:= RetrStream.Seek(0, soEnd);
333
334 libssh2_session_set_blocking(FSession, 0);
335 try
336 repeat
337 SourceHandle:= libssh2_sftp_open(FSFTPSession,
338 PAnsiChar(FileName),
339 LIBSSH2_FXF_READ, 0);
340 if (SourceHandle = nil) then
341 begin
342 FLastError:= libssh2_session_last_errno(FSession);
343 if (FLastError <> LIBSSH2_ERROR_EAGAIN) then Exit(False);
344 if (FileSize > 0) then DoProgress(TotalBytesToRead * 100 div FileSize);
345 FSock.CanRead(10);
346 end;
347 until not ((SourceHandle = nil) and (FLastError = LIBSSH2_ERROR_EAGAIN));
348
349 if Restore then begin
350 libssh2_sftp_seek64(SourceHandle, TotalBytesToRead);
351 end;
352
353 FBuffer:= GetMem(SMB_BUFFER_SIZE);
354 TotalBytesToRead:= FileSize - TotalBytesToRead;
355 try
356 while TotalBytesToRead > 0 do
357 begin
358 repeat
359 BytesRead := libssh2_sftp_read(SourceHandle, PAnsiChar(FBuffer), SMB_BUFFER_SIZE);
360 if BytesRead = LIBSSH2_ERROR_EAGAIN then begin
361 DoProgress((FileSize - TotalBytesToRead) * 100 div FileSize);
362 FSock.CanRead(10);
363 end;
364 until BytesRead <> LIBSSH2_ERROR_EAGAIN;
365
366 if (BytesRead < 0) then Exit(False);
367
368 if RetrStream.Write(FBuffer^, BytesRead) <> BytesRead then
369 Exit(False);
370
371 Dec(TotalBytesToRead, BytesRead);
372 DoProgress((FileSize - TotalBytesToRead) * 100 div FileSize);
373 end;
374 Result:= True;
375 finally
376 FreeMem(FBuffer);
377 Result:= FileClose(SourceHandle) and Result;
378 end;
379 finally
380 RetrStream.Free;
381 libssh2_session_set_blocking(FSession, 1);
382 end;
383 end;
384
385 function TSftpSend.FsFindFirstW(const Path: String; var FindData: TWin32FindDataW): Pointer;
386 var
387 FindRec: PFindRec;
388 begin
389 Result := libssh2_sftp_opendir(FSFTPSession, PAnsiChar(Path));
390 if (Result = nil) then
391 PrintLastError
392 else begin
393 New(FindRec);
394 FindRec.Path:= Path;
395 FindRec.Handle:= Result;
396 FsFindNextW(FindRec, FindData);
397 Result:= FindRec;
398 end;
399 end;
400
401 function TSftpSend.FsFindNextW(Handle: Pointer; var FindData: TWin32FindDataW): BOOL;
402 var
403 Return: Integer;
404 FindRec: PFindRec absolute Handle;
405 Attributes: LIBSSH2_SFTP_ATTRIBUTES;
406 AFileName: array[0..1023] of AnsiChar;
407 AFullData: array[0..2047] of AnsiChar;
408 begin
409 Return:= libssh2_sftp_readdir_ex(FindRec.Handle, AFileName, SizeOf(AFileName),
410 AFullData, SizeOf(AFullData), @Attributes);
411 Result:= (Return > 0);
412 if Result then
413 begin
414 FillChar(FindData, SizeOf(FindData), 0);
415 FindData.dwReserved0:= Attributes.permissions;
416 FindData.dwFileAttributes:= FILE_ATTRIBUTE_UNIX_MODE;
417 if (Attributes.permissions and S_IFMT) <> S_IFDIR then
418 begin
419 FindData.nFileSizeLow:= Int64Rec(Attributes.filesize).Lo;
420 FindData.nFileSizeHigh:= Int64Rec(Attributes.filesize).Hi;
421 end;
422 StrPLCopy(FindData.cFileName, ServerToClient(AFileName), MAX_PATH - 1);
423 FindData.ftLastWriteTime:= TWfxFileTime(UnixFileTimeToWinTime(Attributes.mtime));
424 FindData.ftLastAccessTime:= TWfxFileTime(UnixFileTimeToWinTime(Attributes.atime));
425 if (Attributes.permissions and S_IFMT) = S_IFLNK then
426 begin
427 if libssh2_sftp_stat(FSFTPSession, PAnsiChar(FindRec.Path + AFileName), @Attributes) = 0 then
428 begin
429 if (Attributes.permissions and S_IFMT) = S_IFDIR then
430 begin
431 FindData.nFileSizeLow:= 0;
432 FindData.nFileSizeHigh:= 0;
433 FindData.dwFileAttributes:= FindData.dwFileAttributes or FILE_ATTRIBUTE_REPARSE_POINT;
434 end;
435 end;
436 end;
437 end;
438 end;
439
440 function TSftpSend.FsFindClose(Handle: Pointer): Integer;
441 var
442 FindRec: PFindRec absolute Handle;
443 begin
444 Result:= libssh2_sftp_closedir(FindRec.Handle);
445 Dispose(FindRec);
446 end;
447
448 function TSftpSend.FsSetTime(const FileName: String; LastAccessTime,
449 LastWriteTime: WfxPlugin.PFileTime): BOOL;
450 var
451 Attributes: LIBSSH2_SFTP_ATTRIBUTES;
452 begin
453 if (LastAccessTime = nil) or (LastWriteTime = nil) then
454 begin
455 if libssh2_sftp_stat(FSFTPSession, PAnsiChar(FileName), @Attributes) <> 0 then
456 Exit(False);
457 end;
458 if Assigned(LastAccessTime) then begin
459 Attributes.atime:= WinFileTimeToUnixTime(TWinFileTime(LastAccessTime^));
460 end;
461 if Assigned(LastWriteTime) then begin
462 Attributes.mtime:= WinFileTimeToUnixTime(TWinFileTime(LastWriteTime^));
463 end;
464 Attributes.flags:= LIBSSH2_SFTP_ATTR_ACMODTIME;
465 Result:= libssh2_sftp_setstat(FSFTPSession, PAnsiChar(FileName), @Attributes) = 0;
466 end;
467
468 end.
469