2 * WININET - Ftp implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2004 Mike McCormack for CodeWeavers
6 * Copyright 2004 Kevin Koltzau
7 * Copyright 2007 Hans Leidekker
12 * Copyright 2000 Andreas Mohr
13 * Copyright 2002 Jaco Greeff
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wine/port.h"
33 #if defined(__MINGW32__) || defined (_MSC_VER)
42 #include <sys/types.h>
43 #ifdef HAVE_SYS_SOCKET_H
44 # include <sys/socket.h>
49 #ifdef HAVE_SYS_IOCTL_H
50 # include <sys/ioctl.h>
66 #include "wine/debug.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
71 typedef struct _WININETFTPSESSIONW WININETFTPSESSIONW
;
76 WININETFTPSESSIONW
*lpFtpSession
;
79 } WININETFTPFILE
, *LPWININETFTPFILE
;
81 typedef struct _WININETFTPSESSIONW
84 WININETAPPINFOW
*lpAppInfo
;
87 int pasvSocket
; /* data socket connected by us in case of passive FTP */
88 LPWININETFTPFILE download_in_progress
;
89 struct sockaddr_in socketAddress
;
90 struct sockaddr_in lstnSocketAddress
;
93 } *LPWININETFTPSESSIONW
;
100 SYSTEMTIME tmLastModified
;
101 unsigned short permissions
;
102 } FILEPROPERTIESW
, *LPFILEPROPERTIESW
;
107 WININETFTPSESSIONW
*lpFtpSession
;
110 LPFILEPROPERTIESW lpafp
;
111 } WININETFTPFINDNEXTW
, *LPWININETFTPFINDNEXTW
;
113 #define DATA_PACKET_SIZE 0x2000
114 #define szCRLF "\r\n"
115 #define MAX_BACKLOG 5
117 /* Testing shows that Windows only accepts dwFlags where the last
118 * 3 (yes 3) bits define FTP_TRANSFER_TYPE_UNKNOWN, FTP_TRANSFER_TYPE_ASCII or FTP_TRANSFER_TYPE_BINARY.
120 #define FTP_CONDITION_MASK 0x0007
123 /* FTP commands with arguments. */
139 /* FTP commands without arguments. */
148 static const CHAR
*const szFtpCommands
[] = {
171 static const CHAR szMonths
[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
172 static const WCHAR szNoAccount
[] = {'n','o','a','c','c','o','u','n','t','\0'};
174 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
175 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
);
176 static BOOL
FTP_SendStore(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
);
177 static BOOL
FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs
, LPINT nDataSocket
);
178 static BOOL
FTP_SendData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
);
179 static INT
FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs
, DWORD_PTR dwContext
);
180 static BOOL
FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
);
181 static BOOL
FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
);
182 static BOOL
FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs
);
183 static BOOL
FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs
);
184 static BOOL
FTP_SendPassword(LPWININETFTPSESSIONW lpwfs
);
185 static BOOL
FTP_SendAccount(LPWININETFTPSESSIONW lpwfs
);
186 static BOOL
FTP_SendType(LPWININETFTPSESSIONW lpwfs
, DWORD dwType
);
187 static BOOL
FTP_SendPort(LPWININETFTPSESSIONW lpwfs
);
188 static BOOL
FTP_DoPassive(LPWININETFTPSESSIONW lpwfs
);
189 static BOOL
FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs
);
190 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
);
191 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW fileprop
);
192 static BOOL
FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
193 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
);
194 static HINTERNET
FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
195 LPWIN32_FIND_DATAW lpFindFileData
, DWORD_PTR dwContext
);
196 static DWORD
FTP_SetResponseError(DWORD dwResponse
);
197 static BOOL
FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp
, LPWIN32_FIND_DATAW lpFindFileData
);
198 static BOOL
FTP_FtpPutFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszLocalFile
,
199 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
);
200 static BOOL
FTP_FtpSetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
);
201 static BOOL
FTP_FtpCreateDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
);
202 static HINTERNET
FTP_FtpFindFirstFileW(LPWININETFTPSESSIONW lpwfs
,
203 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
);
204 static BOOL
FTP_FtpGetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPWSTR lpszCurrentDirectory
,
205 LPDWORD lpdwCurrentDirectory
);
206 static BOOL
FTP_FtpRenameFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszSrc
, LPCWSTR lpszDest
);
207 static BOOL
FTP_FtpRemoveDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
);
208 static BOOL
FTP_FtpDeleteFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszFileName
);
209 static BOOL
FTP_FtpGetFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
210 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
211 DWORD_PTR dwContext
);
214 /***********************************************************************
215 * FtpPutFileA (WININET.@)
217 * Uploads a file to the FTP server
224 BOOL WINAPI
FtpPutFileA(HINTERNET hConnect
, LPCSTR lpszLocalFile
,
225 LPCSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
227 LPWSTR lpwzLocalFile
;
228 LPWSTR lpwzNewRemoteFile
;
231 lpwzLocalFile
= lpszLocalFile
?WININET_strdup_AtoW(lpszLocalFile
):NULL
;
232 lpwzNewRemoteFile
= lpszNewRemoteFile
?WININET_strdup_AtoW(lpszNewRemoteFile
):NULL
;
233 ret
= FtpPutFileW(hConnect
, lpwzLocalFile
, lpwzNewRemoteFile
,
235 HeapFree(GetProcessHeap(), 0, lpwzLocalFile
);
236 HeapFree(GetProcessHeap(), 0, lpwzNewRemoteFile
);
240 static void AsyncFtpPutFileProc(WORKREQUEST
*workRequest
)
242 struct WORKREQ_FTPPUTFILEW
const *req
= &workRequest
->u
.FtpPutFileW
;
243 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
245 TRACE("%p\n", lpwfs
);
247 FTP_FtpPutFileW(lpwfs
, req
->lpszLocalFile
,
248 req
->lpszNewRemoteFile
, req
->dwFlags
, req
->dwContext
);
250 HeapFree(GetProcessHeap(), 0, req
->lpszLocalFile
);
251 HeapFree(GetProcessHeap(), 0, req
->lpszNewRemoteFile
);
254 /***********************************************************************
255 * FtpPutFileW (WININET.@)
257 * Uploads a file to the FTP server
264 BOOL WINAPI
FtpPutFileW(HINTERNET hConnect
, LPCWSTR lpszLocalFile
,
265 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
267 LPWININETFTPSESSIONW lpwfs
;
268 LPWININETAPPINFOW hIC
= NULL
;
271 if (!lpszLocalFile
|| !lpszNewRemoteFile
)
273 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
277 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
280 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
284 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
286 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
290 if (lpwfs
->download_in_progress
!= NULL
)
292 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
296 if ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
298 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
302 hIC
= lpwfs
->lpAppInfo
;
303 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
305 WORKREQUEST workRequest
;
306 struct WORKREQ_FTPPUTFILEW
*req
= &workRequest
.u
.FtpPutFileW
;
308 workRequest
.asyncproc
= AsyncFtpPutFileProc
;
309 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
310 req
->lpszLocalFile
= WININET_strdupW(lpszLocalFile
);
311 req
->lpszNewRemoteFile
= WININET_strdupW(lpszNewRemoteFile
);
312 req
->dwFlags
= dwFlags
;
313 req
->dwContext
= dwContext
;
315 r
= INTERNET_AsyncCall(&workRequest
);
319 r
= FTP_FtpPutFileW(lpwfs
, lpszLocalFile
,
320 lpszNewRemoteFile
, dwFlags
, dwContext
);
324 WININET_Release( &lpwfs
->hdr
);
329 /***********************************************************************
330 * FTP_FtpPutFileW (Internal)
332 * Uploads a file to the FTP server
339 static BOOL
FTP_FtpPutFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszLocalFile
,
340 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
343 BOOL bSuccess
= FALSE
;
344 LPWININETAPPINFOW hIC
= NULL
;
347 TRACE(" lpszLocalFile(%s) lpszNewRemoteFile(%s)\n", debugstr_w(lpszLocalFile
), debugstr_w(lpszNewRemoteFile
));
349 /* Clear any error information */
350 INTERNET_SetLastError(0);
352 /* Open file to be uploaded */
353 if (INVALID_HANDLE_VALUE
==
354 (hFile
= CreateFileW(lpszLocalFile
, GENERIC_READ
, 0, 0, OPEN_EXISTING
, 0, 0)))
355 /* Let CreateFile set the appropriate error */
358 hIC
= lpwfs
->lpAppInfo
;
360 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
362 if (FTP_SendStore(lpwfs
, lpszNewRemoteFile
, dwFlags
))
366 /* Get data socket to server */
367 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
369 FTP_SendData(lpwfs
, nDataSocket
, hFile
);
370 closesocket(nDataSocket
);
371 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
377 FTP_SetResponseError(nResCode
);
382 if (lpwfs
->lstnSocket
!= -1)
383 closesocket(lpwfs
->lstnSocket
);
385 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
387 INTERNET_ASYNC_RESULT iar
;
389 iar
.dwResult
= (DWORD
)bSuccess
;
390 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
391 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
392 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
401 /***********************************************************************
402 * FtpSetCurrentDirectoryA (WININET.@)
404 * Change the working directory on the FTP server
411 BOOL WINAPI
FtpSetCurrentDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
413 LPWSTR lpwzDirectory
;
416 lpwzDirectory
= lpszDirectory
?WININET_strdup_AtoW(lpszDirectory
):NULL
;
417 ret
= FtpSetCurrentDirectoryW(hConnect
, lpwzDirectory
);
418 HeapFree(GetProcessHeap(), 0, lpwzDirectory
);
423 static void AsyncFtpSetCurrentDirectoryProc(WORKREQUEST
*workRequest
)
425 struct WORKREQ_FTPSETCURRENTDIRECTORYW
const *req
= &workRequest
->u
.FtpSetCurrentDirectoryW
;
426 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
428 TRACE("%p\n", lpwfs
);
430 FTP_FtpSetCurrentDirectoryW(lpwfs
, req
->lpszDirectory
);
431 HeapFree(GetProcessHeap(), 0, req
->lpszDirectory
);
434 /***********************************************************************
435 * FtpSetCurrentDirectoryW (WININET.@)
437 * Change the working directory on the FTP server
444 BOOL WINAPI
FtpSetCurrentDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
446 LPWININETFTPSESSIONW lpwfs
= NULL
;
447 LPWININETAPPINFOW hIC
= NULL
;
452 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
456 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
457 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
459 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
463 if (lpwfs
->download_in_progress
!= NULL
)
465 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
469 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
471 hIC
= lpwfs
->lpAppInfo
;
472 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
474 WORKREQUEST workRequest
;
475 struct WORKREQ_FTPSETCURRENTDIRECTORYW
*req
;
477 workRequest
.asyncproc
= AsyncFtpSetCurrentDirectoryProc
;
478 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
479 req
= &workRequest
.u
.FtpSetCurrentDirectoryW
;
480 req
->lpszDirectory
= WININET_strdupW(lpszDirectory
);
482 r
= INTERNET_AsyncCall(&workRequest
);
486 r
= FTP_FtpSetCurrentDirectoryW(lpwfs
, lpszDirectory
);
491 WININET_Release( &lpwfs
->hdr
);
497 /***********************************************************************
498 * FTP_FtpSetCurrentDirectoryW (Internal)
500 * Change the working directory on the FTP server
507 static BOOL
FTP_FtpSetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
)
510 LPWININETAPPINFOW hIC
= NULL
;
511 DWORD bSuccess
= FALSE
;
513 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
515 /* Clear any error information */
516 INTERNET_SetLastError(0);
518 hIC
= lpwfs
->lpAppInfo
;
519 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_CWD
, lpszDirectory
,
520 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
523 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
530 FTP_SetResponseError(nResCode
);
534 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
536 INTERNET_ASYNC_RESULT iar
;
538 iar
.dwResult
= bSuccess
;
539 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
540 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
541 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
547 /***********************************************************************
548 * FtpCreateDirectoryA (WININET.@)
550 * Create new directory on the FTP server
557 BOOL WINAPI
FtpCreateDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
559 LPWSTR lpwzDirectory
;
562 lpwzDirectory
= lpszDirectory
?WININET_strdup_AtoW(lpszDirectory
):NULL
;
563 ret
= FtpCreateDirectoryW(hConnect
, lpwzDirectory
);
564 HeapFree(GetProcessHeap(), 0, lpwzDirectory
);
569 static void AsyncFtpCreateDirectoryProc(WORKREQUEST
*workRequest
)
571 struct WORKREQ_FTPCREATEDIRECTORYW
const *req
= &workRequest
->u
.FtpCreateDirectoryW
;
572 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
574 TRACE(" %p\n", lpwfs
);
576 FTP_FtpCreateDirectoryW(lpwfs
, req
->lpszDirectory
);
577 HeapFree(GetProcessHeap(), 0, req
->lpszDirectory
);
580 /***********************************************************************
581 * FtpCreateDirectoryW (WININET.@)
583 * Create new directory on the FTP server
590 BOOL WINAPI
FtpCreateDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
592 LPWININETFTPSESSIONW lpwfs
;
593 LPWININETAPPINFOW hIC
= NULL
;
596 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
599 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
603 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
605 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
609 if (lpwfs
->download_in_progress
!= NULL
)
611 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
617 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
621 hIC
= lpwfs
->lpAppInfo
;
622 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
624 WORKREQUEST workRequest
;
625 struct WORKREQ_FTPCREATEDIRECTORYW
*req
;
627 workRequest
.asyncproc
= AsyncFtpCreateDirectoryProc
;
628 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
629 req
= &workRequest
.u
.FtpCreateDirectoryW
;
630 req
->lpszDirectory
= WININET_strdupW(lpszDirectory
);
632 r
= INTERNET_AsyncCall(&workRequest
);
636 r
= FTP_FtpCreateDirectoryW(lpwfs
, lpszDirectory
);
639 WININET_Release( &lpwfs
->hdr
);
645 /***********************************************************************
646 * FTP_FtpCreateDirectoryW (Internal)
648 * Create new directory on the FTP server
655 static BOOL
FTP_FtpCreateDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
)
658 BOOL bSuccess
= FALSE
;
659 LPWININETAPPINFOW hIC
= NULL
;
661 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
663 /* Clear any error information */
664 INTERNET_SetLastError(0);
666 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_MKD
, lpszDirectory
, 0, 0, 0))
669 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
675 FTP_SetResponseError(nResCode
);
679 hIC
= lpwfs
->lpAppInfo
;
680 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
682 INTERNET_ASYNC_RESULT iar
;
684 iar
.dwResult
= (DWORD
)bSuccess
;
685 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
686 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
687 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
693 /***********************************************************************
694 * FtpFindFirstFileA (WININET.@)
696 * Search the specified directory
699 * HINTERNET on success
703 HINTERNET WINAPI
FtpFindFirstFileA(HINTERNET hConnect
,
704 LPCSTR lpszSearchFile
, LPWIN32_FIND_DATAA lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
706 LPWSTR lpwzSearchFile
;
707 WIN32_FIND_DATAW wfd
;
708 LPWIN32_FIND_DATAW lpFindFileDataW
;
711 lpwzSearchFile
= lpszSearchFile
?WININET_strdup_AtoW(lpszSearchFile
):NULL
;
712 lpFindFileDataW
= lpFindFileData
?&wfd
:NULL
;
713 ret
= FtpFindFirstFileW(hConnect
, lpwzSearchFile
, lpFindFileDataW
, dwFlags
, dwContext
);
714 HeapFree(GetProcessHeap(), 0, lpwzSearchFile
);
717 WININET_find_data_WtoA(lpFindFileDataW
, lpFindFileData
);
723 static void AsyncFtpFindFirstFileProc(WORKREQUEST
*workRequest
)
725 struct WORKREQ_FTPFINDFIRSTFILEW
const *req
= &workRequest
->u
.FtpFindFirstFileW
;
726 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
728 TRACE("%p\n", lpwfs
);
730 FTP_FtpFindFirstFileW(lpwfs
, req
->lpszSearchFile
,
731 req
->lpFindFileData
, req
->dwFlags
, req
->dwContext
);
732 HeapFree(GetProcessHeap(), 0, req
->lpszSearchFile
);
735 /***********************************************************************
736 * FtpFindFirstFileW (WININET.@)
738 * Search the specified directory
741 * HINTERNET on success
745 HINTERNET WINAPI
FtpFindFirstFileW(HINTERNET hConnect
,
746 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
748 LPWININETFTPSESSIONW lpwfs
;
749 LPWININETAPPINFOW hIC
= NULL
;
752 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
753 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
755 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
759 if (lpwfs
->download_in_progress
!= NULL
)
761 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
765 hIC
= lpwfs
->lpAppInfo
;
766 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
768 WORKREQUEST workRequest
;
769 struct WORKREQ_FTPFINDFIRSTFILEW
*req
;
771 workRequest
.asyncproc
= AsyncFtpFindFirstFileProc
;
772 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
773 req
= &workRequest
.u
.FtpFindFirstFileW
;
774 req
->lpszSearchFile
= (lpszSearchFile
== NULL
) ? NULL
: WININET_strdupW(lpszSearchFile
);
775 req
->lpFindFileData
= lpFindFileData
;
776 req
->dwFlags
= dwFlags
;
777 req
->dwContext
= dwContext
;
779 INTERNET_AsyncCall(&workRequest
);
784 r
= FTP_FtpFindFirstFileW(lpwfs
, lpszSearchFile
, lpFindFileData
,
789 WININET_Release( &lpwfs
->hdr
);
795 /***********************************************************************
796 * FTP_FtpFindFirstFileW (Internal)
798 * Search the specified directory
801 * HINTERNET on success
805 static HINTERNET
FTP_FtpFindFirstFileW(LPWININETFTPSESSIONW lpwfs
,
806 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
809 LPWININETAPPINFOW hIC
= NULL
;
810 HINTERNET hFindNext
= NULL
;
814 /* Clear any error information */
815 INTERNET_SetLastError(0);
817 if (!FTP_InitListenSocket(lpwfs
))
820 if (!FTP_SendType(lpwfs
, INTERNET_FLAG_TRANSFER_ASCII
))
823 if (!FTP_SendPortOrPasv(lpwfs
))
826 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_LIST
, NULL
,
827 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
830 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
833 if (nResCode
== 125 || nResCode
== 150)
837 /* Get data socket to server */
838 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
840 hFindNext
= FTP_ReceiveFileList(lpwfs
, nDataSocket
, lpszSearchFile
, lpFindFileData
, dwContext
);
841 closesocket(nDataSocket
);
842 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
843 if (nResCode
!= 226 && nResCode
!= 250)
844 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
848 FTP_SetResponseError(nResCode
);
852 if (lpwfs
->lstnSocket
!= -1)
853 closesocket(lpwfs
->lstnSocket
);
855 hIC
= lpwfs
->lpAppInfo
;
856 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
858 INTERNET_ASYNC_RESULT iar
;
862 iar
.dwResult
= (DWORD_PTR
)hFindNext
;
863 iar
.dwError
= ERROR_SUCCESS
;
864 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
865 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
868 iar
.dwResult
= (DWORD_PTR
)hFindNext
;
869 iar
.dwError
= hFindNext
? ERROR_SUCCESS
: INTERNET_GetLastError();
870 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
871 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
878 /***********************************************************************
879 * FtpGetCurrentDirectoryA (WININET.@)
881 * Retrieves the current directory
888 BOOL WINAPI
FtpGetCurrentDirectoryA(HINTERNET hFtpSession
, LPSTR lpszCurrentDirectory
,
889 LPDWORD lpdwCurrentDirectory
)
895 if(lpdwCurrentDirectory
) {
896 len
= *lpdwCurrentDirectory
;
897 if(lpszCurrentDirectory
)
899 dir
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
902 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
907 ret
= FtpGetCurrentDirectoryW(hFtpSession
, lpszCurrentDirectory
?dir
:NULL
, lpdwCurrentDirectory
?&len
:NULL
);
909 if (ret
&& lpszCurrentDirectory
)
910 WideCharToMultiByte(CP_ACP
, 0, dir
, -1, lpszCurrentDirectory
, len
, NULL
, NULL
);
912 if (lpdwCurrentDirectory
) *lpdwCurrentDirectory
= len
;
913 HeapFree(GetProcessHeap(), 0, dir
);
918 static void AsyncFtpGetCurrentDirectoryProc(WORKREQUEST
*workRequest
)
920 struct WORKREQ_FTPGETCURRENTDIRECTORYW
const *req
= &workRequest
->u
.FtpGetCurrentDirectoryW
;
921 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
923 TRACE("%p\n", lpwfs
);
925 FTP_FtpGetCurrentDirectoryW(lpwfs
, req
->lpszDirectory
, req
->lpdwDirectory
);
928 /***********************************************************************
929 * FtpGetCurrentDirectoryW (WININET.@)
931 * Retrieves the current directory
938 BOOL WINAPI
FtpGetCurrentDirectoryW(HINTERNET hFtpSession
, LPWSTR lpszCurrentDirectory
,
939 LPDWORD lpdwCurrentDirectory
)
941 LPWININETFTPSESSIONW lpwfs
;
942 LPWININETAPPINFOW hIC
= NULL
;
945 TRACE("%p %p %p\n", hFtpSession
, lpszCurrentDirectory
, lpdwCurrentDirectory
);
947 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
950 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
954 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
956 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
960 if (!lpdwCurrentDirectory
)
962 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
966 if (lpszCurrentDirectory
== NULL
)
968 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
972 if (lpwfs
->download_in_progress
!= NULL
)
974 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
978 hIC
= lpwfs
->lpAppInfo
;
979 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
981 WORKREQUEST workRequest
;
982 struct WORKREQ_FTPGETCURRENTDIRECTORYW
*req
;
984 workRequest
.asyncproc
= AsyncFtpGetCurrentDirectoryProc
;
985 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
986 req
= &workRequest
.u
.FtpGetCurrentDirectoryW
;
987 req
->lpszDirectory
= lpszCurrentDirectory
;
988 req
->lpdwDirectory
= lpdwCurrentDirectory
;
990 r
= INTERNET_AsyncCall(&workRequest
);
994 r
= FTP_FtpGetCurrentDirectoryW(lpwfs
, lpszCurrentDirectory
,
995 lpdwCurrentDirectory
);
1000 WININET_Release( &lpwfs
->hdr
);
1006 /***********************************************************************
1007 * FTP_FtpGetCurrentDirectoryW (Internal)
1009 * Retrieves the current directory
1016 static BOOL
FTP_FtpGetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPWSTR lpszCurrentDirectory
,
1017 LPDWORD lpdwCurrentDirectory
)
1020 LPWININETAPPINFOW hIC
= NULL
;
1021 DWORD bSuccess
= FALSE
;
1023 /* Clear any error information */
1024 INTERNET_SetLastError(0);
1026 hIC
= lpwfs
->lpAppInfo
;
1027 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PWD
, NULL
,
1028 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
1031 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1034 if (nResCode
== 257) /* Extract directory name */
1036 DWORD firstpos
, lastpos
, len
;
1037 LPWSTR lpszResponseBuffer
= WININET_strdup_AtoW(INTERNET_GetResponseBuffer());
1039 for (firstpos
= 0, lastpos
= 0; lpszResponseBuffer
[lastpos
]; lastpos
++)
1041 if ('"' == lpszResponseBuffer
[lastpos
])
1049 len
= lastpos
- firstpos
;
1050 if (*lpdwCurrentDirectory
>= len
)
1052 memcpy(lpszCurrentDirectory
, &lpszResponseBuffer
[firstpos
+ 1], len
* sizeof(WCHAR
));
1053 lpszCurrentDirectory
[len
- 1] = 0;
1054 *lpdwCurrentDirectory
= len
;
1057 else INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1059 HeapFree(GetProcessHeap(), 0, lpszResponseBuffer
);
1062 FTP_SetResponseError(nResCode
);
1066 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1068 INTERNET_ASYNC_RESULT iar
;
1070 iar
.dwResult
= bSuccess
;
1071 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
1072 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1073 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1080 /***********************************************************************
1081 * FTPFILE_Destroy(internal)
1083 * Closes the file transfer handle. This also 'cleans' the data queue of
1084 * the 'transfer complete' message (this is a bit of a hack though :-/ )
1087 static void FTPFILE_Destroy(object_header_t
*hdr
)
1089 LPWININETFTPFILE lpwh
= (LPWININETFTPFILE
) hdr
;
1090 LPWININETFTPSESSIONW lpwfs
= lpwh
->lpFtpSession
;
1095 if (!lpwh
->session_deleted
)
1096 lpwfs
->download_in_progress
= NULL
;
1098 if (lpwh
->nDataSocket
!= -1)
1099 closesocket(lpwh
->nDataSocket
);
1101 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1102 if (nResCode
> 0 && nResCode
!= 226) WARN("server reports failed transfer\n");
1104 WININET_Release(&lpwh
->lpFtpSession
->hdr
);
1106 HeapFree(GetProcessHeap(), 0, lpwh
);
1109 static DWORD
FTPFILE_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
1112 case INTERNET_OPTION_HANDLE_TYPE
:
1113 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1115 if (*size
< sizeof(ULONG
))
1116 return ERROR_INSUFFICIENT_BUFFER
;
1118 *size
= sizeof(DWORD
);
1119 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_FTP_FILE
;
1120 return ERROR_SUCCESS
;
1123 return INET_QueryOption(option
, buffer
, size
, unicode
);
1126 static DWORD
FTPFILE_ReadFile(object_header_t
*hdr
, void *buffer
, DWORD size
, DWORD
*read
)
1128 WININETFTPFILE
*file
= (WININETFTPFILE
*)hdr
;
1131 if (file
->nDataSocket
== -1)
1132 return ERROR_INTERNET_DISCONNECTED
;
1134 /* FIXME: FTP should use NETCON_ stuff */
1135 res
= recv(file
->nDataSocket
, buffer
, size
, MSG_WAITALL
);
1136 *read
= res
>0 ? res
: 0;
1138 return res
>=0 ? ERROR_SUCCESS
: INTERNET_ERROR_BASE
; /* FIXME*/
1141 static DWORD
FTPFILE_ReadFileExA(object_header_t
*hdr
, INTERNET_BUFFERSA
*buffers
,
1142 DWORD flags
, DWORD_PTR context
)
1144 return FTPFILE_ReadFile(hdr
, buffers
->lpvBuffer
, buffers
->dwBufferLength
, &buffers
->dwBufferLength
);
1147 static DWORD
FTPFILE_ReadFileExW(object_header_t
*hdr
, INTERNET_BUFFERSW
*buffers
,
1148 DWORD flags
, DWORD_PTR context
)
1150 return FTPFILE_ReadFile(hdr
, buffers
->lpvBuffer
, buffers
->dwBufferLength
, &buffers
->dwBufferLength
);
1153 static BOOL
FTPFILE_WriteFile(object_header_t
*hdr
, const void *buffer
, DWORD size
, DWORD
*written
)
1155 LPWININETFTPFILE lpwh
= (LPWININETFTPFILE
) hdr
;
1158 res
= send(lpwh
->nDataSocket
, buffer
, size
, 0);
1160 *written
= res
>0 ? res
: 0;
1164 static void FTP_ReceiveRequestData(WININETFTPFILE
*file
, BOOL first_notif
)
1166 INTERNET_ASYNC_RESULT iar
;
1170 TRACE("%p\n", file
);
1172 available
= recv(file
->nDataSocket
, buffer
, sizeof(buffer
), MSG_PEEK
);
1174 if(available
!= -1) {
1175 iar
.dwResult
= (DWORD_PTR
)file
->hdr
.hInternet
;
1176 iar
.dwError
= first_notif
? 0 : available
;
1179 iar
.dwError
= INTERNET_GetLastError();
1182 INTERNET_SendCallback(&file
->hdr
, file
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
1183 sizeof(INTERNET_ASYNC_RESULT
));
1186 static void FTPFILE_AsyncQueryDataAvailableProc(WORKREQUEST
*workRequest
)
1188 WININETFTPFILE
*file
= (WININETFTPFILE
*)workRequest
->hdr
;
1190 FTP_ReceiveRequestData(file
, FALSE
);
1193 static DWORD
FTPFILE_QueryDataAvailable(object_header_t
*hdr
, DWORD
*available
, DWORD flags
, DWORD_PTR ctx
)
1195 LPWININETFTPFILE file
= (LPWININETFTPFILE
) hdr
;
1196 int retval
, unread
= 0;
1198 TRACE("(%p %p %x %lx)\n", file
, available
, flags
, ctx
);
1201 retval
= ioctlsocket(file
->nDataSocket
, FIONREAD
, &unread
);
1203 TRACE("%d bytes of queued, but unread data\n", unread
);
1205 FIXME("FIONREAD not available\n");
1208 *available
= unread
;
1215 retval
= recv(file
->nDataSocket
, &byte
, 1, MSG_PEEK
);
1217 WORKREQUEST workRequest
;
1220 workRequest
.asyncproc
= FTPFILE_AsyncQueryDataAvailableProc
;
1221 workRequest
.hdr
= WININET_AddRef( &file
->hdr
);
1223 INTERNET_AsyncCall(&workRequest
);
1225 return ERROR_IO_PENDING
;
1229 return ERROR_SUCCESS
;
1233 static const object_vtbl_t FTPFILEVtbl
= {
1236 FTPFILE_QueryOption
,
1239 FTPFILE_ReadFileExA
,
1240 FTPFILE_ReadFileExW
,
1242 FTPFILE_QueryDataAvailable
,
1246 /***********************************************************************
1247 * FTP_FtpOpenFileW (Internal)
1249 * Open a remote file for writing or reading
1252 * HINTERNET handle on success
1256 HINTERNET
FTP_FtpOpenFileW(LPWININETFTPSESSIONW lpwfs
,
1257 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1258 DWORD_PTR dwContext
)
1261 BOOL bSuccess
= FALSE
;
1262 LPWININETFTPFILE lpwh
= NULL
;
1263 LPWININETAPPINFOW hIC
= NULL
;
1264 HINTERNET handle
= NULL
;
1268 /* Clear any error information */
1269 INTERNET_SetLastError(0);
1271 if (GENERIC_READ
== fdwAccess
)
1273 /* Set up socket to retrieve data */
1274 bSuccess
= FTP_SendRetrieve(lpwfs
, lpszFileName
, dwFlags
);
1276 else if (GENERIC_WRITE
== fdwAccess
)
1278 /* Set up socket to send data */
1279 bSuccess
= FTP_SendStore(lpwfs
, lpszFileName
, dwFlags
);
1282 /* Get data socket to server */
1283 if (bSuccess
&& FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1285 lpwh
= HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPFILE
));
1286 lpwh
->hdr
.htype
= WH_HFILE
;
1287 lpwh
->hdr
.vtbl
= &FTPFILEVtbl
;
1288 lpwh
->hdr
.dwFlags
= dwFlags
;
1289 lpwh
->hdr
.dwContext
= dwContext
;
1291 lpwh
->hdr
.lpfnStatusCB
= lpwfs
->hdr
.lpfnStatusCB
;
1292 lpwh
->nDataSocket
= nDataSocket
;
1293 lpwh
->session_deleted
= FALSE
;
1295 WININET_AddRef( &lpwfs
->hdr
);
1296 lpwh
->lpFtpSession
= lpwfs
;
1297 list_add_head( &lpwfs
->hdr
.children
, &lpwh
->hdr
.entry
);
1299 handle
= WININET_AllocHandle( &lpwh
->hdr
);
1303 /* Indicate that a download is currently in progress */
1304 lpwfs
->download_in_progress
= lpwh
;
1307 if (lpwfs
->lstnSocket
!= -1)
1308 closesocket(lpwfs
->lstnSocket
);
1310 hIC
= lpwfs
->lpAppInfo
;
1311 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1313 INTERNET_ASYNC_RESULT iar
;
1317 iar
.dwResult
= (DWORD_PTR
)handle
;
1318 iar
.dwError
= ERROR_SUCCESS
;
1319 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
1320 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1324 FTP_ReceiveRequestData(lpwh
, TRUE
);
1327 iar
.dwError
= INTERNET_GetLastError();
1328 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1329 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1335 WININET_Release( &lpwh
->hdr
);
1341 /***********************************************************************
1342 * FtpOpenFileA (WININET.@)
1344 * Open a remote file for writing or reading
1347 * HINTERNET handle on success
1351 HINTERNET WINAPI
FtpOpenFileA(HINTERNET hFtpSession
,
1352 LPCSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1353 DWORD_PTR dwContext
)
1355 LPWSTR lpwzFileName
;
1358 lpwzFileName
= lpszFileName
?WININET_strdup_AtoW(lpszFileName
):NULL
;
1359 ret
= FtpOpenFileW(hFtpSession
, lpwzFileName
, fdwAccess
, dwFlags
, dwContext
);
1360 HeapFree(GetProcessHeap(), 0, lpwzFileName
);
1365 static void AsyncFtpOpenFileProc(WORKREQUEST
*workRequest
)
1367 struct WORKREQ_FTPOPENFILEW
const *req
= &workRequest
->u
.FtpOpenFileW
;
1368 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1370 TRACE("%p\n", lpwfs
);
1372 FTP_FtpOpenFileW(lpwfs
, req
->lpszFilename
,
1373 req
->dwAccess
, req
->dwFlags
, req
->dwContext
);
1374 HeapFree(GetProcessHeap(), 0, req
->lpszFilename
);
1377 /***********************************************************************
1378 * FtpOpenFileW (WININET.@)
1380 * Open a remote file for writing or reading
1383 * HINTERNET handle on success
1387 HINTERNET WINAPI
FtpOpenFileW(HINTERNET hFtpSession
,
1388 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1389 DWORD_PTR dwContext
)
1391 LPWININETFTPSESSIONW lpwfs
;
1392 LPWININETAPPINFOW hIC
= NULL
;
1395 TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession
,
1396 debugstr_w(lpszFileName
), fdwAccess
, dwFlags
, dwContext
);
1398 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1401 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1405 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1407 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1411 if ((!lpszFileName
) ||
1412 ((fdwAccess
!= GENERIC_READ
) && (fdwAccess
!= GENERIC_WRITE
)) ||
1413 ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
))
1415 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1419 if (lpwfs
->download_in_progress
!= NULL
)
1421 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1425 hIC
= lpwfs
->lpAppInfo
;
1426 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1428 WORKREQUEST workRequest
;
1429 struct WORKREQ_FTPOPENFILEW
*req
;
1431 workRequest
.asyncproc
= AsyncFtpOpenFileProc
;
1432 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1433 req
= &workRequest
.u
.FtpOpenFileW
;
1434 req
->lpszFilename
= WININET_strdupW(lpszFileName
);
1435 req
->dwAccess
= fdwAccess
;
1436 req
->dwFlags
= dwFlags
;
1437 req
->dwContext
= dwContext
;
1439 INTERNET_AsyncCall(&workRequest
);
1444 r
= FTP_FtpOpenFileW(lpwfs
, lpszFileName
, fdwAccess
, dwFlags
, dwContext
);
1448 WININET_Release( &lpwfs
->hdr
);
1454 /***********************************************************************
1455 * FtpGetFileA (WININET.@)
1457 * Retrieve file from the FTP server
1464 BOOL WINAPI
FtpGetFileA(HINTERNET hInternet
, LPCSTR lpszRemoteFile
, LPCSTR lpszNewFile
,
1465 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1466 DWORD_PTR dwContext
)
1468 LPWSTR lpwzRemoteFile
;
1472 lpwzRemoteFile
= lpszRemoteFile
?WININET_strdup_AtoW(lpszRemoteFile
):NULL
;
1473 lpwzNewFile
= lpszNewFile
?WININET_strdup_AtoW(lpszNewFile
):NULL
;
1474 ret
= FtpGetFileW(hInternet
, lpwzRemoteFile
, lpwzNewFile
, fFailIfExists
,
1475 dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1476 HeapFree(GetProcessHeap(), 0, lpwzRemoteFile
);
1477 HeapFree(GetProcessHeap(), 0, lpwzNewFile
);
1482 static void AsyncFtpGetFileProc(WORKREQUEST
*workRequest
)
1484 struct WORKREQ_FTPGETFILEW
const *req
= &workRequest
->u
.FtpGetFileW
;
1485 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1487 TRACE("%p\n", lpwfs
);
1489 FTP_FtpGetFileW(lpwfs
, req
->lpszRemoteFile
,
1490 req
->lpszNewFile
, req
->fFailIfExists
,
1491 req
->dwLocalFlagsAttribute
, req
->dwFlags
, req
->dwContext
);
1492 HeapFree(GetProcessHeap(), 0, req
->lpszRemoteFile
);
1493 HeapFree(GetProcessHeap(), 0, req
->lpszNewFile
);
1497 /***********************************************************************
1498 * FtpGetFileW (WININET.@)
1500 * Retrieve file from the FTP server
1507 BOOL WINAPI
FtpGetFileW(HINTERNET hInternet
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1508 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1509 DWORD_PTR dwContext
)
1511 LPWININETFTPSESSIONW lpwfs
;
1512 LPWININETAPPINFOW hIC
= NULL
;
1515 if (!lpszRemoteFile
|| !lpszNewFile
)
1517 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1521 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hInternet
);
1524 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1528 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1530 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1534 if ((dwInternetFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
1536 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1540 if (lpwfs
->download_in_progress
!= NULL
)
1542 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1546 hIC
= lpwfs
->lpAppInfo
;
1547 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1549 WORKREQUEST workRequest
;
1550 struct WORKREQ_FTPGETFILEW
*req
;
1552 workRequest
.asyncproc
= AsyncFtpGetFileProc
;
1553 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1554 req
= &workRequest
.u
.FtpGetFileW
;
1555 req
->lpszRemoteFile
= WININET_strdupW(lpszRemoteFile
);
1556 req
->lpszNewFile
= WININET_strdupW(lpszNewFile
);
1557 req
->dwLocalFlagsAttribute
= dwLocalFlagsAttribute
;
1558 req
->fFailIfExists
= fFailIfExists
;
1559 req
->dwFlags
= dwInternetFlags
;
1560 req
->dwContext
= dwContext
;
1562 r
= INTERNET_AsyncCall(&workRequest
);
1566 r
= FTP_FtpGetFileW(lpwfs
, lpszRemoteFile
, lpszNewFile
,
1567 fFailIfExists
, dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1571 WININET_Release( &lpwfs
->hdr
);
1577 /***********************************************************************
1578 * FTP_FtpGetFileW (Internal)
1580 * Retrieve file from the FTP server
1587 static BOOL
FTP_FtpGetFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1588 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1589 DWORD_PTR dwContext
)
1591 BOOL bSuccess
= FALSE
;
1593 LPWININETAPPINFOW hIC
= NULL
;
1595 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile
), debugstr_w(lpszNewFile
));
1597 /* Clear any error information */
1598 INTERNET_SetLastError(0);
1600 /* Ensure we can write to lpszNewfile by opening it */
1601 hFile
= CreateFileW(lpszNewFile
, GENERIC_WRITE
, 0, 0, fFailIfExists
?
1602 CREATE_NEW
: CREATE_ALWAYS
, dwLocalFlagsAttribute
, 0);
1603 if (INVALID_HANDLE_VALUE
== hFile
)
1606 /* Set up socket to retrieve data */
1607 if (FTP_SendRetrieve(lpwfs
, lpszRemoteFile
, dwInternetFlags
))
1611 /* Get data socket to server */
1612 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1617 FTP_RetrieveFileData(lpwfs
, nDataSocket
, hFile
);
1618 closesocket(nDataSocket
);
1620 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
1623 if (nResCode
== 226)
1626 FTP_SetResponseError(nResCode
);
1631 if (lpwfs
->lstnSocket
!= -1)
1632 closesocket(lpwfs
->lstnSocket
);
1636 hIC
= lpwfs
->lpAppInfo
;
1637 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1639 INTERNET_ASYNC_RESULT iar
;
1641 iar
.dwResult
= (DWORD
)bSuccess
;
1642 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1643 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1644 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1647 if (!bSuccess
) DeleteFileW(lpszNewFile
);
1651 /***********************************************************************
1652 * FtpGetFileSize (WININET.@)
1654 DWORD WINAPI
FtpGetFileSize( HINTERNET hFile
, LPDWORD lpdwFileSizeHigh
)
1656 FIXME("(%p, %p)\n", hFile
, lpdwFileSizeHigh
);
1658 if (lpdwFileSizeHigh
)
1659 *lpdwFileSizeHigh
= 0;
1664 /***********************************************************************
1665 * FtpDeleteFileA (WININET.@)
1667 * Delete a file on the ftp server
1674 BOOL WINAPI
FtpDeleteFileA(HINTERNET hFtpSession
, LPCSTR lpszFileName
)
1676 LPWSTR lpwzFileName
;
1679 lpwzFileName
= lpszFileName
?WININET_strdup_AtoW(lpszFileName
):NULL
;
1680 ret
= FtpDeleteFileW(hFtpSession
, lpwzFileName
);
1681 HeapFree(GetProcessHeap(), 0, lpwzFileName
);
1685 static void AsyncFtpDeleteFileProc(WORKREQUEST
*workRequest
)
1687 struct WORKREQ_FTPDELETEFILEW
const *req
= &workRequest
->u
.FtpDeleteFileW
;
1688 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1690 TRACE("%p\n", lpwfs
);
1692 FTP_FtpDeleteFileW(lpwfs
, req
->lpszFilename
);
1693 HeapFree(GetProcessHeap(), 0, req
->lpszFilename
);
1696 /***********************************************************************
1697 * FtpDeleteFileW (WININET.@)
1699 * Delete a file on the ftp server
1706 BOOL WINAPI
FtpDeleteFileW(HINTERNET hFtpSession
, LPCWSTR lpszFileName
)
1708 LPWININETFTPSESSIONW lpwfs
;
1709 LPWININETAPPINFOW hIC
= NULL
;
1712 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1715 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1719 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1721 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1725 if (lpwfs
->download_in_progress
!= NULL
)
1727 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1733 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1737 hIC
= lpwfs
->lpAppInfo
;
1738 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1740 WORKREQUEST workRequest
;
1741 struct WORKREQ_FTPDELETEFILEW
*req
;
1743 workRequest
.asyncproc
= AsyncFtpDeleteFileProc
;
1744 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1745 req
= &workRequest
.u
.FtpDeleteFileW
;
1746 req
->lpszFilename
= WININET_strdupW(lpszFileName
);
1748 r
= INTERNET_AsyncCall(&workRequest
);
1752 r
= FTP_FtpDeleteFileW(lpwfs
, lpszFileName
);
1756 WININET_Release( &lpwfs
->hdr
);
1761 /***********************************************************************
1762 * FTP_FtpDeleteFileW (Internal)
1764 * Delete a file on the ftp server
1771 BOOL
FTP_FtpDeleteFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszFileName
)
1774 BOOL bSuccess
= FALSE
;
1775 LPWININETAPPINFOW hIC
= NULL
;
1777 TRACE("%p\n", lpwfs
);
1779 /* Clear any error information */
1780 INTERNET_SetLastError(0);
1782 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_DELE
, lpszFileName
, 0, 0, 0))
1785 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1788 if (nResCode
== 250)
1791 FTP_SetResponseError(nResCode
);
1794 hIC
= lpwfs
->lpAppInfo
;
1795 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1797 INTERNET_ASYNC_RESULT iar
;
1799 iar
.dwResult
= (DWORD
)bSuccess
;
1800 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1801 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1802 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1809 /***********************************************************************
1810 * FtpRemoveDirectoryA (WININET.@)
1812 * Remove a directory on the ftp server
1819 BOOL WINAPI
FtpRemoveDirectoryA(HINTERNET hFtpSession
, LPCSTR lpszDirectory
)
1821 LPWSTR lpwzDirectory
;
1824 lpwzDirectory
= lpszDirectory
?WININET_strdup_AtoW(lpszDirectory
):NULL
;
1825 ret
= FtpRemoveDirectoryW(hFtpSession
, lpwzDirectory
);
1826 HeapFree(GetProcessHeap(), 0, lpwzDirectory
);
1830 static void AsyncFtpRemoveDirectoryProc(WORKREQUEST
*workRequest
)
1832 struct WORKREQ_FTPREMOVEDIRECTORYW
const *req
= &workRequest
->u
.FtpRemoveDirectoryW
;
1833 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1835 TRACE("%p\n", lpwfs
);
1837 FTP_FtpRemoveDirectoryW(lpwfs
, req
->lpszDirectory
);
1838 HeapFree(GetProcessHeap(), 0, req
->lpszDirectory
);
1841 /***********************************************************************
1842 * FtpRemoveDirectoryW (WININET.@)
1844 * Remove a directory on the ftp server
1851 BOOL WINAPI
FtpRemoveDirectoryW(HINTERNET hFtpSession
, LPCWSTR lpszDirectory
)
1853 LPWININETFTPSESSIONW lpwfs
;
1854 LPWININETAPPINFOW hIC
= NULL
;
1857 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1860 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1864 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1866 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1870 if (lpwfs
->download_in_progress
!= NULL
)
1872 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1878 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1882 hIC
= lpwfs
->lpAppInfo
;
1883 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1885 WORKREQUEST workRequest
;
1886 struct WORKREQ_FTPREMOVEDIRECTORYW
*req
;
1888 workRequest
.asyncproc
= AsyncFtpRemoveDirectoryProc
;
1889 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1890 req
= &workRequest
.u
.FtpRemoveDirectoryW
;
1891 req
->lpszDirectory
= WININET_strdupW(lpszDirectory
);
1893 r
= INTERNET_AsyncCall(&workRequest
);
1897 r
= FTP_FtpRemoveDirectoryW(lpwfs
, lpszDirectory
);
1901 WININET_Release( &lpwfs
->hdr
);
1906 /***********************************************************************
1907 * FTP_FtpRemoveDirectoryW (Internal)
1909 * Remove a directory on the ftp server
1916 BOOL
FTP_FtpRemoveDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
)
1919 BOOL bSuccess
= FALSE
;
1920 LPWININETAPPINFOW hIC
= NULL
;
1924 /* Clear any error information */
1925 INTERNET_SetLastError(0);
1927 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RMD
, lpszDirectory
, 0, 0, 0))
1930 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1933 if (nResCode
== 250)
1936 FTP_SetResponseError(nResCode
);
1940 hIC
= lpwfs
->lpAppInfo
;
1941 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1943 INTERNET_ASYNC_RESULT iar
;
1945 iar
.dwResult
= (DWORD
)bSuccess
;
1946 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1947 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1948 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1955 /***********************************************************************
1956 * FtpRenameFileA (WININET.@)
1958 * Rename a file on the ftp server
1965 BOOL WINAPI
FtpRenameFileA(HINTERNET hFtpSession
, LPCSTR lpszSrc
, LPCSTR lpszDest
)
1971 lpwzSrc
= lpszSrc
?WININET_strdup_AtoW(lpszSrc
):NULL
;
1972 lpwzDest
= lpszDest
?WININET_strdup_AtoW(lpszDest
):NULL
;
1973 ret
= FtpRenameFileW(hFtpSession
, lpwzSrc
, lpwzDest
);
1974 HeapFree(GetProcessHeap(), 0, lpwzSrc
);
1975 HeapFree(GetProcessHeap(), 0, lpwzDest
);
1979 static void AsyncFtpRenameFileProc(WORKREQUEST
*workRequest
)
1981 struct WORKREQ_FTPRENAMEFILEW
const *req
= &workRequest
->u
.FtpRenameFileW
;
1982 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1984 TRACE("%p\n", lpwfs
);
1986 FTP_FtpRenameFileW(lpwfs
, req
->lpszSrcFile
, req
->lpszDestFile
);
1987 HeapFree(GetProcessHeap(), 0, req
->lpszSrcFile
);
1988 HeapFree(GetProcessHeap(), 0, req
->lpszDestFile
);
1991 /***********************************************************************
1992 * FtpRenameFileW (WININET.@)
1994 * Rename a file on the ftp server
2001 BOOL WINAPI
FtpRenameFileW(HINTERNET hFtpSession
, LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
2003 LPWININETFTPSESSIONW lpwfs
;
2004 LPWININETAPPINFOW hIC
= NULL
;
2007 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
2010 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
2014 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
2016 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2020 if (lpwfs
->download_in_progress
!= NULL
)
2022 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
2026 if (!lpszSrc
|| !lpszDest
)
2028 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2032 hIC
= lpwfs
->lpAppInfo
;
2033 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2035 WORKREQUEST workRequest
;
2036 struct WORKREQ_FTPRENAMEFILEW
*req
;
2038 workRequest
.asyncproc
= AsyncFtpRenameFileProc
;
2039 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
2040 req
= &workRequest
.u
.FtpRenameFileW
;
2041 req
->lpszSrcFile
= WININET_strdupW(lpszSrc
);
2042 req
->lpszDestFile
= WININET_strdupW(lpszDest
);
2044 r
= INTERNET_AsyncCall(&workRequest
);
2048 r
= FTP_FtpRenameFileW(lpwfs
, lpszSrc
, lpszDest
);
2052 WININET_Release( &lpwfs
->hdr
);
2057 /***********************************************************************
2058 * FTP_FtpRenameFileW (Internal)
2060 * Rename a file on the ftp server
2067 BOOL
FTP_FtpRenameFileW( LPWININETFTPSESSIONW lpwfs
,
2068 LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
2071 BOOL bSuccess
= FALSE
;
2072 LPWININETAPPINFOW hIC
= NULL
;
2076 /* Clear any error information */
2077 INTERNET_SetLastError(0);
2079 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNFR
, lpszSrc
, 0, 0, 0))
2082 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2083 if (nResCode
== 350)
2085 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNTO
, lpszDest
, 0, 0, 0))
2088 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2091 if (nResCode
== 250)
2094 FTP_SetResponseError(nResCode
);
2097 hIC
= lpwfs
->lpAppInfo
;
2098 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2100 INTERNET_ASYNC_RESULT iar
;
2102 iar
.dwResult
= (DWORD
)bSuccess
;
2103 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
2104 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
2105 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
2111 /***********************************************************************
2112 * FtpCommandA (WININET.@)
2114 BOOL WINAPI
FtpCommandA( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
2115 LPCSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
2120 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
2121 debugstr_a(lpszCommand
), dwContext
, phFtpCommand
);
2123 if (fExpectResponse
)
2125 FIXME("data connection not supported\n");
2129 if (!lpszCommand
|| !lpszCommand
[0])
2131 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2135 if (!(cmdW
= WININET_strdup_AtoW(lpszCommand
)))
2137 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2141 r
= FtpCommandW(hConnect
, fExpectResponse
, dwFlags
, cmdW
, dwContext
, phFtpCommand
);
2143 HeapFree(GetProcessHeap(), 0, cmdW
);
2147 /***********************************************************************
2148 * FtpCommandW (WININET.@)
2150 BOOL WINAPI
FtpCommandW( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
2151 LPCWSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
2154 LPWININETFTPSESSIONW lpwfs
;
2156 DWORD len
, nBytesSent
= 0;
2157 INT nResCode
, nRC
= 0;
2159 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
2160 debugstr_w(lpszCommand
), dwContext
, phFtpCommand
);
2162 if (!lpszCommand
|| !lpszCommand
[0])
2164 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2168 if (fExpectResponse
)
2170 FIXME("data connection not supported\n");
2174 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
2177 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
2181 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
2183 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2187 if (lpwfs
->download_in_progress
!= NULL
)
2189 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
2193 len
= WideCharToMultiByte(CP_ACP
, 0, lpszCommand
, -1, NULL
, 0, NULL
, NULL
) + strlen(szCRLF
);
2194 if ((cmd
= HeapAlloc(GetProcessHeap(), 0, len
)))
2195 WideCharToMultiByte(CP_ACP
, 0, lpszCommand
, -1, cmd
, len
, NULL
, NULL
);
2198 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2202 strcat(cmd
, szCRLF
);
2205 TRACE("Sending (%s) len(%d)\n", cmd
, len
);
2206 while ((nBytesSent
< len
) && (nRC
!= -1))
2208 nRC
= send(lpwfs
->sndSocket
, cmd
+ nBytesSent
, len
- nBytesSent
, 0);
2212 TRACE("Sent %d bytes\n", nRC
);
2218 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2219 if (nResCode
> 0 && nResCode
< 400)
2222 FTP_SetResponseError(nResCode
);
2226 WININET_Release( &lpwfs
->hdr
);
2227 HeapFree(GetProcessHeap(), 0, cmd
);
2232 /***********************************************************************
2233 * FTPSESSION_Destroy (internal)
2235 * Deallocate session handle
2237 static void FTPSESSION_Destroy(object_header_t
*hdr
)
2239 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) hdr
;
2243 WININET_Release(&lpwfs
->lpAppInfo
->hdr
);
2245 HeapFree(GetProcessHeap(), 0, lpwfs
->lpszPassword
);
2246 HeapFree(GetProcessHeap(), 0, lpwfs
->lpszUserName
);
2247 HeapFree(GetProcessHeap(), 0, lpwfs
);
2250 static void FTPSESSION_CloseConnection(object_header_t
*hdr
)
2252 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) hdr
;
2256 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
2257 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
2259 if (lpwfs
->download_in_progress
!= NULL
)
2260 lpwfs
->download_in_progress
->session_deleted
= TRUE
;
2262 if (lpwfs
->sndSocket
!= -1)
2263 closesocket(lpwfs
->sndSocket
);
2265 if (lpwfs
->lstnSocket
!= -1)
2266 closesocket(lpwfs
->lstnSocket
);
2268 if (lpwfs
->pasvSocket
!= -1)
2269 closesocket(lpwfs
->pasvSocket
);
2271 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
2272 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
2275 static DWORD
FTPSESSION_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
2278 case INTERNET_OPTION_HANDLE_TYPE
:
2279 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2281 if (*size
< sizeof(ULONG
))
2282 return ERROR_INSUFFICIENT_BUFFER
;
2284 *size
= sizeof(DWORD
);
2285 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_CONNECT_FTP
;
2286 return ERROR_SUCCESS
;
2289 return INET_QueryOption(option
, buffer
, size
, unicode
);
2292 static const object_vtbl_t FTPSESSIONVtbl
= {
2294 FTPSESSION_CloseConnection
,
2295 FTPSESSION_QueryOption
,
2305 /***********************************************************************
2306 * FTP_Connect (internal)
2308 * Connect to a ftp server
2311 * HINTERNET a session handle on success
2316 * Windows uses 'anonymous' as the username, when given a NULL username
2317 * and a NULL password. The password is first looked up in:
2319 * HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\EmailName
2321 * If this entry is not present it uses the current username as the password.
2325 HINTERNET
FTP_Connect(LPWININETAPPINFOW hIC
, LPCWSTR lpszServerName
,
2326 INTERNET_PORT nServerPort
, LPCWSTR lpszUserName
,
2327 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
2328 DWORD dwInternalFlags
)
2330 static const WCHAR szKey
[] = {'S','o','f','t','w','a','r','e','\\',
2331 'M','i','c','r','o','s','o','f','t','\\',
2332 'W','i','n','d','o','w','s','\\',
2333 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2334 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
2335 static const WCHAR szValue
[] = {'E','m','a','i','l','N','a','m','e',0};
2336 static const WCHAR szDefaultUsername
[] = {'a','n','o','n','y','m','o','u','s','\0'};
2337 static const WCHAR szEmpty
[] = {'\0'};
2338 struct sockaddr_in socketAddr
;
2341 BOOL bSuccess
= FALSE
;
2342 LPWININETFTPSESSIONW lpwfs
= NULL
;
2343 HINTERNET handle
= NULL
;
2345 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
2346 hIC
, debugstr_w(lpszServerName
),
2347 nServerPort
, debugstr_w(lpszUserName
), debugstr_w(lpszPassword
));
2349 assert( hIC
->hdr
.htype
== WH_HINIT
);
2351 if ((!lpszUserName
|| !*lpszUserName
) && lpszPassword
&& *lpszPassword
)
2353 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2357 lpwfs
= HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPSESSIONW
));
2360 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2364 if (nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
2365 nServerPort
= INTERNET_DEFAULT_FTP_PORT
;
2367 lpwfs
->hdr
.htype
= WH_HFTPSESSION
;
2368 lpwfs
->hdr
.vtbl
= &FTPSESSIONVtbl
;
2369 lpwfs
->hdr
.dwFlags
= dwFlags
;
2370 lpwfs
->hdr
.dwContext
= dwContext
;
2371 lpwfs
->hdr
.dwInternalFlags
= dwInternalFlags
;
2372 lpwfs
->hdr
.refs
= 1;
2373 lpwfs
->hdr
.lpfnStatusCB
= hIC
->hdr
.lpfnStatusCB
;
2374 lpwfs
->download_in_progress
= NULL
;
2375 lpwfs
->sndSocket
= -1;
2376 lpwfs
->lstnSocket
= -1;
2377 lpwfs
->pasvSocket
= -1;
2379 WININET_AddRef( &hIC
->hdr
);
2380 lpwfs
->lpAppInfo
= hIC
;
2381 list_add_head( &hIC
->hdr
.children
, &lpwfs
->hdr
.entry
);
2383 handle
= WININET_AllocHandle( &lpwfs
->hdr
);
2386 ERR("Failed to alloc handle\n");
2387 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2391 if(hIC
->lpszProxy
&& hIC
->dwAccessType
== INTERNET_OPEN_TYPE_PROXY
) {
2392 if(strchrW(hIC
->lpszProxy
, ' '))
2393 FIXME("Several proxies not implemented.\n");
2394 if(hIC
->lpszProxyBypass
)
2395 FIXME("Proxy bypass is ignored.\n");
2397 if (!lpszUserName
|| !strlenW(lpszUserName
)) {
2399 WCHAR szPassword
[MAX_PATH
];
2400 DWORD len
= sizeof(szPassword
);
2402 lpwfs
->lpszUserName
= WININET_strdupW(szDefaultUsername
);
2404 RegOpenKeyW(HKEY_CURRENT_USER
, szKey
, &key
);
2405 if (RegQueryValueExW(key
, szValue
, NULL
, NULL
, (LPBYTE
)szPassword
, &len
)) {
2406 /* Nothing in the registry, get the username and use that as the password */
2407 if (!GetUserNameW(szPassword
, &len
)) {
2408 /* Should never get here, but use an empty password as failsafe */
2409 strcpyW(szPassword
, szEmpty
);
2414 TRACE("Password used for anonymous ftp : (%s)\n", debugstr_w(szPassword
));
2415 lpwfs
->lpszPassword
= WININET_strdupW(szPassword
);
2418 lpwfs
->lpszUserName
= WININET_strdupW(lpszUserName
);
2421 lpwfs
->lpszPassword
= WININET_strdupW(lpszPassword
);
2423 lpwfs
->lpszPassword
= WININET_strdupW(szEmpty
);
2426 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2427 if (!(lpwfs
->hdr
.dwInternalFlags
& INET_OPENURL
))
2429 INTERNET_ASYNC_RESULT iar
;
2431 iar
.dwResult
= (DWORD_PTR
)handle
;
2432 iar
.dwError
= ERROR_SUCCESS
;
2434 SendAsyncCallback(&hIC
->hdr
, dwContext
,
2435 INTERNET_STATUS_HANDLE_CREATED
, &iar
,
2436 sizeof(INTERNET_ASYNC_RESULT
));
2439 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_RESOLVING_NAME
,
2440 (LPWSTR
) lpszServerName
, strlenW(lpszServerName
));
2442 sock_namelen
= sizeof(socketAddr
);
2443 if (!GetAddress(lpszServerName
, nServerPort
,
2444 (struct sockaddr
*)&socketAddr
, &sock_namelen
))
2446 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
2450 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_NAME_RESOLVED
,
2451 (LPWSTR
) lpszServerName
, strlenW(lpszServerName
));
2453 if (socketAddr
.sin_family
!= AF_INET
)
2455 WARN("unsupported address family %d\n", socketAddr
.sin_family
);
2456 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2459 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
2462 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2466 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTING_TO_SERVER
,
2467 &socketAddr
, sock_namelen
);
2469 if (connect(nsocket
, (struct sockaddr
*)&socketAddr
, sock_namelen
) < 0)
2471 ERR("Unable to connect (%s)\n", strerror(errno
));
2472 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2473 closesocket(nsocket
);
2477 TRACE("Connected to server\n");
2478 lpwfs
->sndSocket
= nsocket
;
2479 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTED_TO_SERVER
,
2480 &socketAddr
, sock_namelen
);
2482 sock_namelen
= sizeof(lpwfs
->socketAddress
);
2483 getsockname(nsocket
, (struct sockaddr
*) &lpwfs
->socketAddress
, &sock_namelen
);
2485 if (FTP_ConnectToHost(lpwfs
))
2487 TRACE("Successfully logged into server\n");
2493 if (lpwfs
) WININET_Release( &lpwfs
->hdr
);
2495 if (!bSuccess
&& handle
)
2497 WININET_FreeHandle( handle
);
2505 /***********************************************************************
2506 * FTP_ConnectToHost (internal)
2508 * Connect to a ftp server
2515 static BOOL
FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs
)
2518 BOOL bSuccess
= FALSE
;
2521 FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2523 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_USER
, lpwfs
->lpszUserName
, 0, 0, 0))
2526 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2529 /* Login successful... */
2530 if (nResCode
== 230)
2532 /* User name okay, need password... */
2533 else if (nResCode
== 331)
2534 bSuccess
= FTP_SendPassword(lpwfs
);
2535 /* Need account for login... */
2536 else if (nResCode
== 332)
2537 bSuccess
= FTP_SendAccount(lpwfs
);
2539 FTP_SetResponseError(nResCode
);
2542 TRACE("Returning %d\n", bSuccess
);
2548 /***********************************************************************
2549 * FTP_SendCommandA (internal)
2551 * Send command to server
2558 static BOOL
FTP_SendCommandA(INT nSocket
, FTP_COMMAND ftpCmd
, LPCSTR lpszParam
,
2559 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
)
2563 DWORD nBytesSent
= 0;
2567 TRACE("%d: (%s) %d\n", ftpCmd
, lpszParam
, nSocket
);
2571 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
2574 dwParamLen
= lpszParam
?strlen(lpszParam
)+1:0;
2575 len
= dwParamLen
+ strlen(szFtpCommands
[ftpCmd
]) + strlen(szCRLF
);
2576 if (NULL
== (buf
= HeapAlloc(GetProcessHeap(), 0, len
+1)))
2578 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2581 sprintf(buf
, "%s%s%s%s", szFtpCommands
[ftpCmd
], dwParamLen
? " " : "",
2582 dwParamLen
? lpszParam
: "", szCRLF
);
2584 TRACE("Sending (%s) len(%d)\n", buf
, len
);
2585 while((nBytesSent
< len
) && (nRC
!= -1))
2587 nRC
= send(nSocket
, buf
+nBytesSent
, len
- nBytesSent
, 0);
2591 HeapFree(GetProcessHeap(), 0, (LPVOID
)buf
);
2595 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_REQUEST_SENT
,
2596 &nBytesSent
, sizeof(DWORD
));
2599 TRACE("Sent %d bytes\n", nBytesSent
);
2603 /***********************************************************************
2604 * FTP_SendCommand (internal)
2606 * Send command to server
2613 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
2614 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
)
2617 LPSTR lpszParamA
= lpszParam
?WININET_strdup_WtoA(lpszParam
):NULL
;
2618 ret
= FTP_SendCommandA(nSocket
, ftpCmd
, lpszParamA
, lpfnStatusCB
, hdr
, dwContext
);
2619 HeapFree(GetProcessHeap(), 0, lpszParamA
);
2623 /***********************************************************************
2624 * FTP_ReceiveResponse (internal)
2626 * Receive response from server
2629 * Reply code on success
2633 INT
FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs
, DWORD_PTR dwContext
)
2635 LPSTR lpszResponse
= INTERNET_GetResponseBuffer();
2638 char firstprefix
[5];
2639 BOOL multiline
= FALSE
;
2641 TRACE("socket(%d)\n", lpwfs
->sndSocket
);
2643 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
2647 if (!INTERNET_GetNextLine(lpwfs
->sndSocket
, &nRecv
))
2654 if(lpszResponse
[3] != '-')
2657 { /* Start of multiline response. Loop until we get "nnn " */
2659 memcpy(firstprefix
, lpszResponse
, 3);
2660 firstprefix
[3] = ' ';
2661 firstprefix
[4] = '\0';
2666 if(!memcmp(firstprefix
, lpszResponse
, 4))
2674 rc
= atoi(lpszResponse
);
2676 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
2677 &nRecv
, sizeof(DWORD
));
2681 TRACE("return %d\n", rc
);
2686 /***********************************************************************
2687 * FTP_SendPassword (internal)
2689 * Send password to ftp server
2696 static BOOL
FTP_SendPassword(LPWININETFTPSESSIONW lpwfs
)
2699 BOOL bSuccess
= FALSE
;
2702 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASS
, lpwfs
->lpszPassword
, 0, 0, 0))
2705 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2708 TRACE("Received reply code %d\n", nResCode
);
2709 /* Login successful... */
2710 if (nResCode
== 230)
2712 /* Command not implemented, superfluous at the server site... */
2713 /* Need account for login... */
2714 else if (nResCode
== 332)
2715 bSuccess
= FTP_SendAccount(lpwfs
);
2717 FTP_SetResponseError(nResCode
);
2721 TRACE("Returning %d\n", bSuccess
);
2726 /***********************************************************************
2727 * FTP_SendAccount (internal)
2736 static BOOL
FTP_SendAccount(LPWININETFTPSESSIONW lpwfs
)
2739 BOOL bSuccess
= FALSE
;
2742 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_ACCT
, szNoAccount
, 0, 0, 0))
2745 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2749 FTP_SetResponseError(nResCode
);
2756 /***********************************************************************
2757 * FTP_SendStore (internal)
2759 * Send request to upload file to ftp server
2766 static BOOL
FTP_SendStore(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
2769 BOOL bSuccess
= FALSE
;
2772 if (!FTP_InitListenSocket(lpwfs
))
2775 if (!FTP_SendType(lpwfs
, dwType
))
2778 if (!FTP_SendPortOrPasv(lpwfs
))
2781 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_STOR
, lpszRemoteFile
, 0, 0, 0))
2783 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2786 if (nResCode
== 150 || nResCode
== 125)
2789 FTP_SetResponseError(nResCode
);
2793 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2795 closesocket(lpwfs
->lstnSocket
);
2796 lpwfs
->lstnSocket
= -1;
2803 /***********************************************************************
2804 * FTP_InitListenSocket (internal)
2806 * Create a socket to listen for server response
2813 static BOOL
FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs
)
2815 BOOL bSuccess
= FALSE
;
2816 socklen_t namelen
= sizeof(lpwfs
->lstnSocketAddress
);
2820 lpwfs
->lstnSocket
= socket(PF_INET
, SOCK_STREAM
, 0);
2821 if (lpwfs
->lstnSocket
== -1)
2823 TRACE("Unable to create listening socket\n");
2827 /* We obtain our ip addr from the name of the command channel socket */
2828 lpwfs
->lstnSocketAddress
= lpwfs
->socketAddress
;
2830 /* and get the system to assign us a port */
2831 lpwfs
->lstnSocketAddress
.sin_port
= htons(0);
2833 if (bind(lpwfs
->lstnSocket
,(struct sockaddr
*) &lpwfs
->lstnSocketAddress
, sizeof(lpwfs
->lstnSocketAddress
)) == -1)
2835 TRACE("Unable to bind socket\n");
2839 if (listen(lpwfs
->lstnSocket
, MAX_BACKLOG
) == -1)
2841 TRACE("listen failed\n");
2845 if (getsockname(lpwfs
->lstnSocket
, (struct sockaddr
*) &lpwfs
->lstnSocketAddress
, &namelen
) != -1)
2849 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2851 closesocket(lpwfs
->lstnSocket
);
2852 lpwfs
->lstnSocket
= -1;
2859 /***********************************************************************
2860 * FTP_SendType (internal)
2862 * Tell server type of data being transferred
2868 * W98SE doesn't cache the type that's currently set
2869 * (i.e. it sends it always),
2870 * so we probably don't want to do that either.
2872 static BOOL
FTP_SendType(LPWININETFTPSESSIONW lpwfs
, DWORD dwType
)
2875 WCHAR type
[] = { 'I','\0' };
2876 BOOL bSuccess
= FALSE
;
2879 if (dwType
& INTERNET_FLAG_TRANSFER_ASCII
)
2882 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_TYPE
, type
, 0, 0, 0))
2885 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
)/100;
2891 FTP_SetResponseError(nResCode
);
2899 #if 0 /* FIXME: should probably be used for FtpGetFileSize */
2900 /***********************************************************************
2901 * FTP_GetFileSize (internal)
2903 * Retrieves from the server the size of the given file
2910 static BOOL
FTP_GetFileSize(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD
*dwSize
)
2913 BOOL bSuccess
= FALSE
;
2917 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_SIZE
, lpszRemoteFile
, 0, 0, 0))
2920 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2923 if (nResCode
== 213) {
2924 /* Now parses the output to get the actual file size */
2926 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
2928 for (i
= 0; (lpszResponseBuffer
[i
] != ' ') && (lpszResponseBuffer
[i
] != '\0'); i
++) ;
2929 if (lpszResponseBuffer
[i
] == '\0') return FALSE
;
2930 *dwSize
= atol(&(lpszResponseBuffer
[i
+ 1]));
2934 FTP_SetResponseError(nResCode
);
2944 /***********************************************************************
2945 * FTP_SendPort (internal)
2947 * Tell server which port to use
2954 static BOOL
FTP_SendPort(LPWININETFTPSESSIONW lpwfs
)
2956 static const WCHAR szIPFormat
[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
2958 WCHAR szIPAddress
[64];
2959 BOOL bSuccess
= FALSE
;
2962 sprintfW(szIPAddress
, szIPFormat
,
2963 lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x000000FF,
2964 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x0000FF00)>>8,
2965 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x00FF0000)>>16,
2966 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0xFF000000)>>24,
2967 lpwfs
->lstnSocketAddress
.sin_port
& 0xFF,
2968 (lpwfs
->lstnSocketAddress
.sin_port
& 0xFF00)>>8);
2970 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PORT
, szIPAddress
, 0, 0, 0))
2973 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2976 if (nResCode
== 200)
2979 FTP_SetResponseError(nResCode
);
2987 /***********************************************************************
2988 * FTP_DoPassive (internal)
2990 * Tell server that we want to do passive transfers
2991 * and connect data socket
2998 static BOOL
FTP_DoPassive(LPWININETFTPSESSIONW lpwfs
)
3001 BOOL bSuccess
= FALSE
;
3004 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASV
, NULL
, 0, 0, 0))
3007 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3010 if (nResCode
== 227)
3012 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
3016 char *pAddr
, *pPort
;
3018 struct sockaddr_in dataSocketAddress
;
3020 p
= lpszResponseBuffer
+4; /* skip status code */
3021 while (*p
!= '\0' && (*p
< '0' || *p
> '9')) p
++;
3025 ERR("no address found in response, aborting\n");
3029 if (sscanf(p
, "%d,%d,%d,%d,%d,%d", &f
[0], &f
[1], &f
[2], &f
[3],
3032 ERR("unknown response address format '%s', aborting\n", p
);
3035 for (i
=0; i
< 6; i
++)
3038 dataSocketAddress
= lpwfs
->socketAddress
;
3039 pAddr
= (char *)&(dataSocketAddress
.sin_addr
.s_addr
);
3040 pPort
= (char *)&(dataSocketAddress
.sin_port
);
3048 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
3052 if (connect(nsocket
, (struct sockaddr
*)&dataSocketAddress
, sizeof(dataSocketAddress
)))
3054 ERR("can't connect passive FTP data port.\n");
3055 closesocket(nsocket
);
3058 lpwfs
->pasvSocket
= nsocket
;
3062 FTP_SetResponseError(nResCode
);
3070 static BOOL
FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs
)
3072 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
3074 if (!FTP_DoPassive(lpwfs
))
3079 if (!FTP_SendPort(lpwfs
))
3086 /***********************************************************************
3087 * FTP_GetDataSocket (internal)
3089 * Either accepts an incoming data socket connection from the server
3090 * or just returns the already opened socket after a PASV command
3091 * in case of passive FTP.
3099 static BOOL
FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs
, LPINT nDataSocket
)
3101 struct sockaddr_in saddr
;
3102 socklen_t addrlen
= sizeof(struct sockaddr
);
3105 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
3107 *nDataSocket
= lpwfs
->pasvSocket
;
3111 *nDataSocket
= accept(lpwfs
->lstnSocket
, (struct sockaddr
*) &saddr
, &addrlen
);
3112 closesocket(lpwfs
->lstnSocket
);
3113 lpwfs
->lstnSocket
= -1;
3115 return *nDataSocket
!= -1;
3119 /***********************************************************************
3120 * FTP_SendData (internal)
3122 * Send data to the server
3129 static BOOL
FTP_SendData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
)
3131 BY_HANDLE_FILE_INFORMATION fi
;
3132 DWORD nBytesRead
= 0;
3133 DWORD nBytesSent
= 0;
3134 DWORD nTotalSent
= 0;
3135 DWORD nBytesToSend
, nLen
;
3137 time_t s_long_time
, e_long_time
;
3142 lpszBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(CHAR
)*DATA_PACKET_SIZE
);
3144 /* Get the size of the file. */
3145 GetFileInformationByHandle(hFile
, &fi
);
3150 nBytesToSend
= nBytesRead
- nBytesSent
;
3152 if (nBytesToSend
<= 0)
3154 /* Read data from file. */
3156 if (!ReadFile(hFile
, lpszBuffer
, DATA_PACKET_SIZE
, &nBytesRead
, 0))
3157 ERR("Failed reading from file\n");
3160 nBytesToSend
= nBytesRead
;
3165 nLen
= DATA_PACKET_SIZE
< nBytesToSend
?
3166 DATA_PACKET_SIZE
: nBytesToSend
;
3167 nRC
= send(nDataSocket
, lpszBuffer
, nLen
, 0);
3175 /* Do some computation to display the status. */
3177 nSeconds
= e_long_time
- s_long_time
;
3178 if( nSeconds
/ 60 > 0 )
3180 TRACE( "%d bytes of %d bytes (%d%%) in %d min %d sec estimated remaining time %d sec\n",
3181 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
/ 60,
3182 nSeconds
% 60, (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
3186 TRACE( "%d bytes of %d bytes (%d%%) in %d sec estimated remaining time %d sec\n",
3187 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
,
3188 (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
3190 } while (nRC
!= -1);
3192 TRACE("file transfer complete!\n");
3194 HeapFree(GetProcessHeap(), 0, lpszBuffer
);
3200 /***********************************************************************
3201 * FTP_SendRetrieve (internal)
3203 * Send request to retrieve a file
3206 * Number of bytes to be received on success
3210 static BOOL
FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
3216 if (!(ret
= FTP_InitListenSocket(lpwfs
)))
3219 if (!(ret
= FTP_SendType(lpwfs
, dwType
)))
3222 if (!(ret
= FTP_SendPortOrPasv(lpwfs
)))
3225 if (!(ret
= FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RETR
, lpszRemoteFile
, 0, 0, 0)))
3228 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3229 if ((nResCode
!= 125) && (nResCode
!= 150)) {
3230 /* That means that we got an error getting the file. */
3231 FTP_SetResponseError(nResCode
);
3236 if (!ret
&& lpwfs
->lstnSocket
!= -1)
3238 closesocket(lpwfs
->lstnSocket
);
3239 lpwfs
->lstnSocket
= -1;
3246 /***********************************************************************
3247 * FTP_RetrieveData (internal)
3249 * Retrieve data from server
3256 static BOOL
FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
)
3258 DWORD nBytesWritten
;
3264 lpszBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(CHAR
)*DATA_PACKET_SIZE
);
3265 if (NULL
== lpszBuffer
)
3267 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
3273 nRC
= recv(nDataSocket
, lpszBuffer
, DATA_PACKET_SIZE
, 0);
3276 /* other side closed socket. */
3279 WriteFile(hFile
, lpszBuffer
, nRC
, &nBytesWritten
, NULL
);
3283 TRACE("Data transfer complete\n");
3286 HeapFree(GetProcessHeap(), 0, lpszBuffer
);
3291 /***********************************************************************
3292 * FTPFINDNEXT_Destroy (internal)
3294 * Deallocate session handle
3296 static void FTPFINDNEXT_Destroy(object_header_t
*hdr
)
3298 LPWININETFTPFINDNEXTW lpwfn
= (LPWININETFTPFINDNEXTW
) hdr
;
3303 WININET_Release(&lpwfn
->lpFtpSession
->hdr
);
3305 for (i
= 0; i
< lpwfn
->size
; i
++)
3307 HeapFree(GetProcessHeap(), 0, lpwfn
->lpafp
[i
].lpszName
);
3310 HeapFree(GetProcessHeap(), 0, lpwfn
->lpafp
);
3311 HeapFree(GetProcessHeap(), 0, lpwfn
);
3314 static DWORD
FTPFINDNEXT_FindNextFileProc(WININETFTPFINDNEXTW
*find
, LPVOID data
)
3316 WIN32_FIND_DATAW
*find_data
= data
;
3317 DWORD res
= ERROR_SUCCESS
;
3319 TRACE("index(%d) size(%d)\n", find
->index
, find
->size
);
3321 ZeroMemory(find_data
, sizeof(WIN32_FIND_DATAW
));
3323 if (find
->index
< find
->size
) {
3324 FTP_ConvertFileProp(&find
->lpafp
[find
->index
], find_data
);
3327 TRACE("Name: %s\nSize: %d\n", debugstr_w(find_data
->cFileName
), find_data
->nFileSizeLow
);
3329 res
= ERROR_NO_MORE_FILES
;
3332 if (find
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3334 INTERNET_ASYNC_RESULT iar
;
3336 iar
.dwResult
= (res
== ERROR_SUCCESS
);
3339 INTERNET_SendCallback(&find
->hdr
, find
->hdr
.dwContext
,
3340 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
3341 sizeof(INTERNET_ASYNC_RESULT
));
3347 static void FTPFINDNEXT_AsyncFindNextFileProc(WORKREQUEST
*workRequest
)
3349 struct WORKREQ_FTPFINDNEXTW
*req
= &workRequest
->u
.FtpFindNextW
;
3351 FTPFINDNEXT_FindNextFileProc((WININETFTPFINDNEXTW
*)workRequest
->hdr
, req
->lpFindFileData
);
3354 static DWORD
FTPFINDNEXT_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
3357 case INTERNET_OPTION_HANDLE_TYPE
:
3358 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3360 if (*size
< sizeof(ULONG
))
3361 return ERROR_INSUFFICIENT_BUFFER
;
3363 *size
= sizeof(DWORD
);
3364 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_FTP_FIND
;
3365 return ERROR_SUCCESS
;
3368 return INET_QueryOption(option
, buffer
, size
, unicode
);
3371 static DWORD
FTPFINDNEXT_FindNextFileW(object_header_t
*hdr
, void *data
)
3373 WININETFTPFINDNEXTW
*find
= (WININETFTPFINDNEXTW
*)hdr
;
3375 if (find
->lpFtpSession
->lpAppInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3377 WORKREQUEST workRequest
;
3378 struct WORKREQ_FTPFINDNEXTW
*req
;
3380 workRequest
.asyncproc
= FTPFINDNEXT_AsyncFindNextFileProc
;
3381 workRequest
.hdr
= WININET_AddRef( &find
->hdr
);
3382 req
= &workRequest
.u
.FtpFindNextW
;
3383 req
->lpFindFileData
= data
;
3385 INTERNET_AsyncCall(&workRequest
);
3387 return ERROR_SUCCESS
;
3390 return FTPFINDNEXT_FindNextFileProc(find
, data
);
3393 static const object_vtbl_t FTPFINDNEXTVtbl
= {
3394 FTPFINDNEXT_Destroy
,
3396 FTPFINDNEXT_QueryOption
,
3403 FTPFINDNEXT_FindNextFileW
3406 /***********************************************************************
3407 * FTP_ReceiveFileList (internal)
3409 * Read file list from server
3412 * Handle to file list on success
3416 static HINTERNET
FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
3417 LPWIN32_FIND_DATAW lpFindFileData
, DWORD_PTR dwContext
)
3420 LPFILEPROPERTIESW lpafp
= NULL
;
3421 LPWININETFTPFINDNEXTW lpwfn
= NULL
;
3422 HINTERNET handle
= 0;
3424 TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs
, nSocket
, debugstr_w(lpszSearchFile
), lpFindFileData
, dwContext
);
3426 if (FTP_ParseDirectory(lpwfs
, nSocket
, lpszSearchFile
, &lpafp
, &dwSize
))
3429 FTP_ConvertFileProp(lpafp
, lpFindFileData
);
3431 lpwfn
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETFTPFINDNEXTW
));
3434 lpwfn
->hdr
.htype
= WH_HFTPFINDNEXT
;
3435 lpwfn
->hdr
.vtbl
= &FTPFINDNEXTVtbl
;
3436 lpwfn
->hdr
.dwContext
= dwContext
;
3437 lpwfn
->hdr
.refs
= 1;
3438 lpwfn
->hdr
.lpfnStatusCB
= lpwfs
->hdr
.lpfnStatusCB
;
3439 lpwfn
->index
= 1; /* Next index is 1 since we return index 0 */
3440 lpwfn
->size
= dwSize
;
3441 lpwfn
->lpafp
= lpafp
;
3443 WININET_AddRef( &lpwfs
->hdr
);
3444 lpwfn
->lpFtpSession
= lpwfs
;
3445 list_add_head( &lpwfs
->hdr
.children
, &lpwfn
->hdr
.entry
);
3447 handle
= WININET_AllocHandle( &lpwfn
->hdr
);
3452 WININET_Release( &lpwfn
->hdr
);
3454 TRACE("Matched %d files\n", dwSize
);
3459 /***********************************************************************
3460 * FTP_ConvertFileProp (internal)
3462 * Converts FILEPROPERTIESW struct to WIN32_FIND_DATAA
3469 static BOOL
FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp
, LPWIN32_FIND_DATAW lpFindFileData
)
3471 BOOL bSuccess
= FALSE
;
3473 ZeroMemory(lpFindFileData
, sizeof(WIN32_FIND_DATAW
));
3477 SystemTimeToFileTime( &lpafp
->tmLastModified
, &lpFindFileData
->ftLastAccessTime
);
3478 lpFindFileData
->ftLastWriteTime
= lpFindFileData
->ftLastAccessTime
;
3479 lpFindFileData
->ftCreationTime
= lpFindFileData
->ftLastAccessTime
;
3481 /* Not all fields are filled in */
3482 lpFindFileData
->nFileSizeHigh
= 0; /* We do not handle files bigger than 0xFFFFFFFF bytes yet :-) */
3483 lpFindFileData
->nFileSizeLow
= lpafp
->nSize
;
3485 if (lpafp
->bIsDirectory
)
3486 lpFindFileData
->dwFileAttributes
|= FILE_ATTRIBUTE_DIRECTORY
;
3488 if (lpafp
->lpszName
)
3489 lstrcpynW(lpFindFileData
->cFileName
, lpafp
->lpszName
, MAX_PATH
);
3497 /***********************************************************************
3498 * FTP_ParseNextFile (internal)
3500 * Parse the next line in file listing
3506 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW lpfp
)
3508 static const char szSpace
[] = " \t";
3516 lpfp
->lpszName
= NULL
;
3518 if(!(pszLine
= INTERNET_GetNextLine(nSocket
, &nBufLen
)))
3521 pszToken
= strtok(pszLine
, szSpace
);
3523 * <Permissions> <NoLinks> <owner> <group> <size> <date> <time or year> <filename>
3526 * drwx--s--- 2 pcarrier ens 512 Sep 28 1995 pcarrier
3528 if(!isdigit(pszToken
[0]) && 10 == strlen(pszToken
)) {
3529 if(!FTP_ParsePermission(pszToken
, lpfp
))
3530 lpfp
->bIsDirectory
= FALSE
;
3531 for(i
=0; i
<=3; i
++) {
3532 if(!(pszToken
= strtok(NULL
, szSpace
)))
3535 if(!pszToken
) continue;
3536 if(lpfp
->bIsDirectory
) {
3537 TRACE("Is directory\n");
3541 TRACE("Size: %s\n", pszToken
);
3542 lpfp
->nSize
= atol(pszToken
);
3545 lpfp
->tmLastModified
.wSecond
= 0;
3546 lpfp
->tmLastModified
.wMinute
= 0;
3547 lpfp
->tmLastModified
.wHour
= 0;
3548 lpfp
->tmLastModified
.wDay
= 0;
3549 lpfp
->tmLastModified
.wMonth
= 0;
3550 lpfp
->tmLastModified
.wYear
= 0;
3552 /* Determine month */
3553 pszToken
= strtok(NULL
, szSpace
);
3554 if(!pszToken
) continue;
3555 if(strlen(pszToken
) >= 3) {
3557 if((pszTmp
= StrStrIA(szMonths
, pszToken
)))
3558 lpfp
->tmLastModified
.wMonth
= ((pszTmp
- szMonths
) / 3)+1;
3561 pszToken
= strtok(NULL
, szSpace
);
3562 if(!pszToken
) continue;
3563 lpfp
->tmLastModified
.wDay
= atoi(pszToken
);
3564 /* Determine time or year */
3565 pszToken
= strtok(NULL
, szSpace
);
3566 if(!pszToken
) continue;
3567 if((pszTmp
= strchr(pszToken
, ':'))) {
3568 SYSTEMTIME curr_time
;
3571 lpfp
->tmLastModified
.wMinute
= atoi(pszTmp
);
3572 lpfp
->tmLastModified
.wHour
= atoi(pszToken
);
3573 GetLocalTime( &curr_time
);
3574 lpfp
->tmLastModified
.wYear
= curr_time
.wYear
;
3577 lpfp
->tmLastModified
.wYear
= atoi(pszToken
);
3578 lpfp
->tmLastModified
.wHour
= 12;
3580 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3581 lpfp
->tmLastModified
.wHour
, lpfp
->tmLastModified
.wMinute
, lpfp
->tmLastModified
.wSecond
,
3582 lpfp
->tmLastModified
.wYear
, lpfp
->tmLastModified
.wMonth
, lpfp
->tmLastModified
.wDay
);
3584 pszToken
= strtok(NULL
, szSpace
);
3585 if(!pszToken
) continue;
3586 lpfp
->lpszName
= WININET_strdup_AtoW(pszToken
);
3587 TRACE("File: %s\n", debugstr_w(lpfp
->lpszName
));
3589 /* NT way of parsing ... :
3591 07-13-03 08:55PM <DIR> sakpatch
3592 05-09-03 06:02PM 12656686 2003-04-21bgm_cmd_e.rgz
3594 else if(isdigit(pszToken
[0]) && 8 == strlen(pszToken
)) {
3595 int mon
, mday
, year
, hour
, min
;
3596 lpfp
->permissions
= 0xFFFF; /* No idea, put full permission :-) */
3598 sscanf(pszToken
, "%d-%d-%d", &mon
, &mday
, &year
);
3599 lpfp
->tmLastModified
.wDay
= mday
;
3600 lpfp
->tmLastModified
.wMonth
= mon
;
3601 lpfp
->tmLastModified
.wYear
= year
;
3603 /* Hacky and bad Y2K protection :-) */
3604 if (lpfp
->tmLastModified
.wYear
< 70) lpfp
->tmLastModified
.wYear
+= 2000;
3606 pszToken
= strtok(NULL
, szSpace
);
3607 if(!pszToken
) continue;
3608 sscanf(pszToken
, "%d:%d", &hour
, &min
);
3609 lpfp
->tmLastModified
.wHour
= hour
;
3610 lpfp
->tmLastModified
.wMinute
= min
;
3611 if((pszToken
[5] == 'P') && (pszToken
[6] == 'M')) {
3612 lpfp
->tmLastModified
.wHour
+= 12;
3614 lpfp
->tmLastModified
.wSecond
= 0;
3616 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3617 lpfp
->tmLastModified
.wHour
, lpfp
->tmLastModified
.wMinute
, lpfp
->tmLastModified
.wSecond
,
3618 lpfp
->tmLastModified
.wYear
, lpfp
->tmLastModified
.wMonth
, lpfp
->tmLastModified
.wDay
);
3620 pszToken
= strtok(NULL
, szSpace
);
3621 if(!pszToken
) continue;
3622 if(!strcasecmp(pszToken
, "<DIR>")) {
3623 lpfp
->bIsDirectory
= TRUE
;
3625 TRACE("Is directory\n");
3628 lpfp
->bIsDirectory
= FALSE
;
3629 lpfp
->nSize
= atol(pszToken
);
3630 TRACE("Size: %d\n", lpfp
->nSize
);
3633 pszToken
= strtok(NULL
, szSpace
);
3634 if(!pszToken
) continue;
3635 lpfp
->lpszName
= WININET_strdup_AtoW(pszToken
);
3636 TRACE("Name: %s\n", debugstr_w(lpfp
->lpszName
));
3638 /* EPLF format - http://cr.yp.to/ftp/list/eplf.html */
3639 else if(pszToken
[0] == '+') {
3640 FIXME("EPLF Format not implemented\n");
3643 if(lpfp
->lpszName
) {
3644 if((lpszSearchFile
== NULL
) ||
3645 (PathMatchSpecW(lpfp
->lpszName
, lpszSearchFile
))) {
3647 TRACE("Matched: %s\n", debugstr_w(lpfp
->lpszName
));
3650 HeapFree(GetProcessHeap(), 0, lpfp
->lpszName
);
3651 lpfp
->lpszName
= NULL
;
3658 /***********************************************************************
3659 * FTP_ParseDirectory (internal)
3661 * Parse string of directory information
3667 static BOOL
FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
3668 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
)
3670 BOOL bSuccess
= TRUE
;
3671 INT sizeFilePropArray
= 500;/*20; */
3672 INT indexFilePropArray
= -1;
3676 /* Allocate initial file properties array */
3677 *lpafp
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(FILEPROPERTIESW
)*(sizeFilePropArray
));
3682 if (indexFilePropArray
+1 >= sizeFilePropArray
)
3684 LPFILEPROPERTIESW tmpafp
;
3686 sizeFilePropArray
*= 2;
3687 tmpafp
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *lpafp
,
3688 sizeof(FILEPROPERTIESW
)*sizeFilePropArray
);
3697 indexFilePropArray
++;
3698 } while (FTP_ParseNextFile(nSocket
, lpszSearchFile
, &(*lpafp
)[indexFilePropArray
]));
3700 if (bSuccess
&& indexFilePropArray
)
3702 if (indexFilePropArray
< sizeFilePropArray
- 1)
3704 LPFILEPROPERTIESW tmpafp
;
3706 tmpafp
= HeapReAlloc(GetProcessHeap(), 0, *lpafp
,
3707 sizeof(FILEPROPERTIESW
)*indexFilePropArray
);
3711 *dwfp
= indexFilePropArray
;
3715 HeapFree(GetProcessHeap(), 0, *lpafp
);
3716 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
3724 /***********************************************************************
3725 * FTP_ParsePermission (internal)
3727 * Parse permission string of directory information
3734 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
)
3736 BOOL bSuccess
= TRUE
;
3737 unsigned short nPermission
= 0;
3742 if ((*lpszPermission
!= 'd') && (*lpszPermission
!= '-') && (*lpszPermission
!= 'l'))
3748 lpfp
->bIsDirectory
= (*lpszPermission
== 'd');
3754 nPermission
|= (*(lpszPermission
+1) == 'r' ? 1 : 0) << 8;
3757 nPermission
|= (*(lpszPermission
+2) == 'w' ? 1 : 0) << 7;
3760 nPermission
|= (*(lpszPermission
+3) == 'x' ? 1 : 0) << 6;
3763 nPermission
|= (*(lpszPermission
+4) == 'r' ? 1 : 0) << 5;
3766 nPermission
|= (*(lpszPermission
+5) == 'w' ? 1 : 0) << 4;
3769 nPermission
|= (*(lpszPermission
+6) == 'x' ? 1 : 0) << 3;
3772 nPermission
|= (*(lpszPermission
+7) == 'r' ? 1 : 0) << 2;
3775 nPermission
|= (*(lpszPermission
+8) == 'w' ? 1 : 0) << 1;
3778 nPermission
|= (*(lpszPermission
+9) == 'x' ? 1 : 0);
3782 }while (nPos
<= nLast
);
3784 lpfp
->permissions
= nPermission
;
3789 /***********************************************************************
3790 * FTP_SetResponseError (internal)
3792 * Set the appropriate error code for a given response from the server
3797 static DWORD
FTP_SetResponseError(DWORD dwResponse
)
3803 case 425: /* Cannot open data connection. */
3804 dwCode
= ERROR_INTERNET_CANNOT_CONNECT
;
3807 case 426: /* Connection closed, transer aborted. */
3808 dwCode
= ERROR_INTERNET_CONNECTION_ABORTED
;
3811 case 530: /* Not logged in. Login incorrect. */
3812 dwCode
= ERROR_INTERNET_LOGIN_FAILURE
;
3815 case 421: /* Service not available - Server may be shutting down. */
3816 case 450: /* File action not taken. File may be busy. */
3817 case 451: /* Action aborted. Server error. */
3818 case 452: /* Action not taken. Insufficient storage space on server. */
3819 case 500: /* Syntax error. Command unrecognized. */
3820 case 501: /* Syntax error. Error in parameters or arguments. */
3821 case 502: /* Command not implemented. */
3822 case 503: /* Bad sequence of commands. */
3823 case 504: /* Command not implemented for that parameter. */
3824 case 532: /* Need account for storing files */
3825 case 550: /* File action not taken. File not found or no access. */
3826 case 551: /* Requested action aborted. Page type unknown */
3827 case 552: /* Action aborted. Exceeded storage allocation */
3828 case 553: /* Action not taken. File name not allowed. */
3831 dwCode
= ERROR_INTERNET_EXTENDED_ERROR
;
3835 INTERNET_SetLastError(dwCode
);