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
30 #define WIN32_NO_STATUS
32 #define COM_NO_WINDOWS_H
35 //#include "wine/port.h"
42 //#include <sys/types.h>
43 #ifdef HAVE_SYS_SOCKET_H
44 # include <sys/socket.h>
46 #ifdef HAVE_ARPA_INET_H
47 # include <arpa/inet.h>
52 #ifdef HAVE_SYS_IOCTL_H
53 # include <sys/ioctl.h>
61 //#include "winuser.h"
64 //#include "winerror.h"
66 //#include "winternl.h"
69 #if defined(__MINGW32__) || defined (_MSC_VER)
73 #include <wine/debug.h>
76 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
78 typedef struct _ftp_session_t ftp_session_t
;
83 ftp_session_t
*lpFtpSession
;
87 HANDLE cache_file_handle
;
96 int pasvSocket
; /* data socket connected by us in case of passive FTP */
97 ftp_file_t
*download_in_progress
;
98 struct sockaddr_in socketAddress
;
99 struct sockaddr_in lstnSocketAddress
;
101 INTERNET_PORT serverport
;
111 SYSTEMTIME tmLastModified
;
112 unsigned short permissions
;
113 } FILEPROPERTIESW
, *LPFILEPROPERTIESW
;
118 ftp_session_t
*lpFtpSession
;
121 LPFILEPROPERTIESW lpafp
;
122 } WININETFTPFINDNEXTW
, *LPWININETFTPFINDNEXTW
;
124 #define DATA_PACKET_SIZE 0x2000
125 #define szCRLF "\r\n"
126 #define MAX_BACKLOG 5
128 /* Testing shows that Windows only accepts dwFlags where the last
129 * 3 (yes 3) bits define FTP_TRANSFER_TYPE_UNKNOWN, FTP_TRANSFER_TYPE_ASCII or FTP_TRANSFER_TYPE_BINARY.
131 #define FTP_CONDITION_MASK 0x0007
134 /* FTP commands with arguments. */
150 /* FTP commands without arguments. */
159 static const CHAR
*const szFtpCommands
[] = {
182 static const CHAR szMonths
[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
183 static const WCHAR szNoAccount
[] = {'n','o','a','c','c','o','u','n','t','\0'};
185 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
186 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
);
187 static BOOL
FTP_SendStore(ftp_session_t
*, LPCWSTR lpszRemoteFile
, DWORD dwType
);
188 static BOOL
FTP_GetDataSocket(ftp_session_t
*, LPINT nDataSocket
);
189 static BOOL
FTP_SendData(ftp_session_t
*, INT nDataSocket
, HANDLE hFile
);
190 static INT
FTP_ReceiveResponse(ftp_session_t
*, DWORD_PTR dwContext
);
191 static BOOL
FTP_SendRetrieve(ftp_session_t
*, LPCWSTR lpszRemoteFile
, DWORD dwType
);
192 static BOOL
FTP_RetrieveFileData(ftp_session_t
*, INT nDataSocket
, HANDLE hFile
);
193 static BOOL
FTP_InitListenSocket(ftp_session_t
*);
194 static BOOL
FTP_ConnectToHost(ftp_session_t
*);
195 static BOOL
FTP_SendPassword(ftp_session_t
*);
196 static BOOL
FTP_SendAccount(ftp_session_t
*);
197 static BOOL
FTP_SendType(ftp_session_t
*, DWORD dwType
);
198 static BOOL
FTP_SendPort(ftp_session_t
*);
199 static BOOL
FTP_DoPassive(ftp_session_t
*);
200 static BOOL
FTP_SendPortOrPasv(ftp_session_t
*);
201 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
);
202 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW fileprop
);
203 static BOOL
FTP_ParseDirectory(ftp_session_t
*, INT nSocket
, LPCWSTR lpszSearchFile
,
204 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
);
205 static HINTERNET
FTP_ReceiveFileList(ftp_session_t
*, INT nSocket
, LPCWSTR lpszSearchFile
,
206 LPWIN32_FIND_DATAW lpFindFileData
, DWORD_PTR dwContext
);
207 static DWORD
FTP_SetResponseError(DWORD dwResponse
);
208 static BOOL
FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp
, LPWIN32_FIND_DATAW lpFindFileData
);
209 static BOOL
FTP_FtpPutFileW(ftp_session_t
*, LPCWSTR lpszLocalFile
,
210 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
);
211 static BOOL
FTP_FtpSetCurrentDirectoryW(ftp_session_t
*, LPCWSTR lpszDirectory
);
212 static BOOL
FTP_FtpCreateDirectoryW(ftp_session_t
*, LPCWSTR lpszDirectory
);
213 static HINTERNET
FTP_FtpFindFirstFileW(ftp_session_t
*,
214 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
);
215 static BOOL
FTP_FtpGetCurrentDirectoryW(ftp_session_t
*, LPWSTR lpszCurrentDirectory
,
216 LPDWORD lpdwCurrentDirectory
);
217 static BOOL
FTP_FtpRenameFileW(ftp_session_t
*, LPCWSTR lpszSrc
, LPCWSTR lpszDest
);
218 static BOOL
FTP_FtpRemoveDirectoryW(ftp_session_t
*, LPCWSTR lpszDirectory
);
219 static BOOL
FTP_FtpDeleteFileW(ftp_session_t
*, LPCWSTR lpszFileName
);
220 static BOOL
FTP_FtpGetFileW(ftp_session_t
*, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
221 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
222 DWORD_PTR dwContext
);
224 /* A temporary helper until we get rid of INTERNET_GetLastError calls */
225 static BOOL
res_to_le(DWORD res
)
227 if(res
!= ERROR_SUCCESS
)
228 INTERNET_SetLastError(res
);
229 return res
== ERROR_SUCCESS
;
232 /***********************************************************************
233 * FtpPutFileA (WININET.@)
235 * Uploads a file to the FTP server
242 BOOL WINAPI
FtpPutFileA(HINTERNET hConnect
, LPCSTR lpszLocalFile
,
243 LPCSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
245 LPWSTR lpwzLocalFile
;
246 LPWSTR lpwzNewRemoteFile
;
249 lpwzLocalFile
= heap_strdupAtoW(lpszLocalFile
);
250 lpwzNewRemoteFile
= heap_strdupAtoW(lpszNewRemoteFile
);
251 ret
= FtpPutFileW(hConnect
, lpwzLocalFile
, lpwzNewRemoteFile
,
253 heap_free(lpwzLocalFile
);
254 heap_free(lpwzNewRemoteFile
);
258 static void AsyncFtpPutFileProc(WORKREQUEST
*workRequest
)
260 struct WORKREQ_FTPPUTFILEW
const *req
= &workRequest
->u
.FtpPutFileW
;
261 ftp_session_t
*lpwfs
= (ftp_session_t
*) workRequest
->hdr
;
263 TRACE("%p\n", lpwfs
);
265 FTP_FtpPutFileW(lpwfs
, req
->lpszLocalFile
,
266 req
->lpszNewRemoteFile
, req
->dwFlags
, req
->dwContext
);
268 heap_free(req
->lpszLocalFile
);
269 heap_free(req
->lpszNewRemoteFile
);
272 /***********************************************************************
273 * FtpPutFileW (WININET.@)
275 * Uploads a file to the FTP server
282 BOOL WINAPI
FtpPutFileW(HINTERNET hConnect
, LPCWSTR lpszLocalFile
,
283 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
285 ftp_session_t
*lpwfs
;
286 appinfo_t
*hIC
= NULL
;
289 if (!lpszLocalFile
|| !lpszNewRemoteFile
)
291 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
295 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
298 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
302 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
304 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
308 if (lpwfs
->download_in_progress
!= NULL
)
310 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
314 if ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
316 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
320 hIC
= lpwfs
->lpAppInfo
;
321 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
323 WORKREQUEST workRequest
;
324 struct WORKREQ_FTPPUTFILEW
*req
= &workRequest
.u
.FtpPutFileW
;
326 workRequest
.asyncproc
= AsyncFtpPutFileProc
;
327 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
328 req
->lpszLocalFile
= heap_strdupW(lpszLocalFile
);
329 req
->lpszNewRemoteFile
= heap_strdupW(lpszNewRemoteFile
);
330 req
->dwFlags
= dwFlags
;
331 req
->dwContext
= dwContext
;
333 r
= res_to_le(INTERNET_AsyncCall(&workRequest
));
337 r
= FTP_FtpPutFileW(lpwfs
, lpszLocalFile
,
338 lpszNewRemoteFile
, dwFlags
, dwContext
);
342 WININET_Release( &lpwfs
->hdr
);
347 /***********************************************************************
348 * FTP_FtpPutFileW (Internal)
350 * Uploads a file to the FTP server
357 static BOOL
FTP_FtpPutFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszLocalFile
,
358 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
361 BOOL bSuccess
= FALSE
;
362 appinfo_t
*hIC
= NULL
;
365 TRACE(" lpszLocalFile(%s) lpszNewRemoteFile(%s)\n", debugstr_w(lpszLocalFile
), debugstr_w(lpszNewRemoteFile
));
367 /* Clear any error information */
368 INTERNET_SetLastError(0);
370 /* Open file to be uploaded */
371 if (INVALID_HANDLE_VALUE
==
372 (hFile
= CreateFileW(lpszLocalFile
, GENERIC_READ
, 0, 0, OPEN_EXISTING
, 0, 0)))
373 /* Let CreateFile set the appropriate error */
376 hIC
= lpwfs
->lpAppInfo
;
378 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
380 if (FTP_SendStore(lpwfs
, lpszNewRemoteFile
, dwFlags
))
384 /* Get data socket to server */
385 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
387 FTP_SendData(lpwfs
, nDataSocket
, hFile
);
388 closesocket(nDataSocket
);
389 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
395 FTP_SetResponseError(nResCode
);
400 if (lpwfs
->lstnSocket
!= -1)
402 closesocket(lpwfs
->lstnSocket
);
403 lpwfs
->lstnSocket
= -1;
406 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
408 INTERNET_ASYNC_RESULT iar
;
410 iar
.dwResult
= (DWORD
)bSuccess
;
411 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
412 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
413 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
422 /***********************************************************************
423 * FtpSetCurrentDirectoryA (WININET.@)
425 * Change the working directory on the FTP server
432 BOOL WINAPI
FtpSetCurrentDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
434 LPWSTR lpwzDirectory
;
437 lpwzDirectory
= heap_strdupAtoW(lpszDirectory
);
438 ret
= FtpSetCurrentDirectoryW(hConnect
, lpwzDirectory
);
439 heap_free(lpwzDirectory
);
444 static void AsyncFtpSetCurrentDirectoryProc(WORKREQUEST
*workRequest
)
446 struct WORKREQ_FTPSETCURRENTDIRECTORYW
const *req
= &workRequest
->u
.FtpSetCurrentDirectoryW
;
447 ftp_session_t
*lpwfs
= (ftp_session_t
*) workRequest
->hdr
;
449 TRACE("%p\n", lpwfs
);
451 FTP_FtpSetCurrentDirectoryW(lpwfs
, req
->lpszDirectory
);
452 heap_free(req
->lpszDirectory
);
455 /***********************************************************************
456 * FtpSetCurrentDirectoryW (WININET.@)
458 * Change the working directory on the FTP server
465 BOOL WINAPI
FtpSetCurrentDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
467 ftp_session_t
*lpwfs
= NULL
;
468 appinfo_t
*hIC
= NULL
;
473 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
477 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
478 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
480 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
484 if (lpwfs
->download_in_progress
!= NULL
)
486 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
490 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
492 hIC
= lpwfs
->lpAppInfo
;
493 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
495 WORKREQUEST workRequest
;
496 struct WORKREQ_FTPSETCURRENTDIRECTORYW
*req
;
498 workRequest
.asyncproc
= AsyncFtpSetCurrentDirectoryProc
;
499 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
500 req
= &workRequest
.u
.FtpSetCurrentDirectoryW
;
501 req
->lpszDirectory
= heap_strdupW(lpszDirectory
);
503 r
= res_to_le(INTERNET_AsyncCall(&workRequest
));
507 r
= FTP_FtpSetCurrentDirectoryW(lpwfs
, lpszDirectory
);
512 WININET_Release( &lpwfs
->hdr
);
518 /***********************************************************************
519 * FTP_FtpSetCurrentDirectoryW (Internal)
521 * Change the working directory on the FTP server
528 static BOOL
FTP_FtpSetCurrentDirectoryW(ftp_session_t
*lpwfs
, LPCWSTR lpszDirectory
)
531 appinfo_t
*hIC
= NULL
;
532 DWORD bSuccess
= FALSE
;
534 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
536 /* Clear any error information */
537 INTERNET_SetLastError(0);
539 hIC
= lpwfs
->lpAppInfo
;
540 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_CWD
, lpszDirectory
,
541 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
544 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
551 FTP_SetResponseError(nResCode
);
555 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
557 INTERNET_ASYNC_RESULT iar
;
559 iar
.dwResult
= bSuccess
;
560 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
561 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
562 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
568 /***********************************************************************
569 * FtpCreateDirectoryA (WININET.@)
571 * Create new directory on the FTP server
578 BOOL WINAPI
FtpCreateDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
580 LPWSTR lpwzDirectory
;
583 lpwzDirectory
= heap_strdupAtoW(lpszDirectory
);
584 ret
= FtpCreateDirectoryW(hConnect
, lpwzDirectory
);
585 heap_free(lpwzDirectory
);
590 static void AsyncFtpCreateDirectoryProc(WORKREQUEST
*workRequest
)
592 struct WORKREQ_FTPCREATEDIRECTORYW
const *req
= &workRequest
->u
.FtpCreateDirectoryW
;
593 ftp_session_t
*lpwfs
= (ftp_session_t
*) workRequest
->hdr
;
595 TRACE(" %p\n", lpwfs
);
597 FTP_FtpCreateDirectoryW(lpwfs
, req
->lpszDirectory
);
598 heap_free(req
->lpszDirectory
);
601 /***********************************************************************
602 * FtpCreateDirectoryW (WININET.@)
604 * Create new directory on the FTP server
611 BOOL WINAPI
FtpCreateDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
613 ftp_session_t
*lpwfs
;
614 appinfo_t
*hIC
= NULL
;
617 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
620 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
624 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
626 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
630 if (lpwfs
->download_in_progress
!= NULL
)
632 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
638 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
642 hIC
= lpwfs
->lpAppInfo
;
643 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
645 WORKREQUEST workRequest
;
646 struct WORKREQ_FTPCREATEDIRECTORYW
*req
;
648 workRequest
.asyncproc
= AsyncFtpCreateDirectoryProc
;
649 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
650 req
= &workRequest
.u
.FtpCreateDirectoryW
;
651 req
->lpszDirectory
= heap_strdupW(lpszDirectory
);
653 r
= res_to_le(INTERNET_AsyncCall(&workRequest
));
657 r
= FTP_FtpCreateDirectoryW(lpwfs
, lpszDirectory
);
660 WININET_Release( &lpwfs
->hdr
);
666 /***********************************************************************
667 * FTP_FtpCreateDirectoryW (Internal)
669 * Create new directory on the FTP server
676 static BOOL
FTP_FtpCreateDirectoryW(ftp_session_t
*lpwfs
, LPCWSTR lpszDirectory
)
679 BOOL bSuccess
= FALSE
;
680 appinfo_t
*hIC
= NULL
;
682 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
684 /* Clear any error information */
685 INTERNET_SetLastError(0);
687 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_MKD
, lpszDirectory
, 0, 0, 0))
690 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
696 FTP_SetResponseError(nResCode
);
700 hIC
= lpwfs
->lpAppInfo
;
701 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
703 INTERNET_ASYNC_RESULT iar
;
705 iar
.dwResult
= (DWORD
)bSuccess
;
706 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
707 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
708 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
714 /***********************************************************************
715 * FtpFindFirstFileA (WININET.@)
717 * Search the specified directory
720 * HINTERNET on success
724 HINTERNET WINAPI
FtpFindFirstFileA(HINTERNET hConnect
,
725 LPCSTR lpszSearchFile
, LPWIN32_FIND_DATAA lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
727 LPWSTR lpwzSearchFile
;
728 WIN32_FIND_DATAW wfd
;
729 LPWIN32_FIND_DATAW lpFindFileDataW
;
732 lpwzSearchFile
= heap_strdupAtoW(lpszSearchFile
);
733 lpFindFileDataW
= lpFindFileData
?&wfd
:NULL
;
734 ret
= FtpFindFirstFileW(hConnect
, lpwzSearchFile
, lpFindFileDataW
, dwFlags
, dwContext
);
735 heap_free(lpwzSearchFile
);
737 if (ret
&& lpFindFileData
)
738 WININET_find_data_WtoA(lpFindFileDataW
, lpFindFileData
);
744 static void AsyncFtpFindFirstFileProc(WORKREQUEST
*workRequest
)
746 struct WORKREQ_FTPFINDFIRSTFILEW
const *req
= &workRequest
->u
.FtpFindFirstFileW
;
747 ftp_session_t
*lpwfs
= (ftp_session_t
*) workRequest
->hdr
;
749 TRACE("%p\n", lpwfs
);
751 FTP_FtpFindFirstFileW(lpwfs
, req
->lpszSearchFile
,
752 req
->lpFindFileData
, req
->dwFlags
, req
->dwContext
);
753 heap_free(req
->lpszSearchFile
);
756 /***********************************************************************
757 * FtpFindFirstFileW (WININET.@)
759 * Search the specified directory
762 * HINTERNET on success
766 HINTERNET WINAPI
FtpFindFirstFileW(HINTERNET hConnect
,
767 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
769 ftp_session_t
*lpwfs
;
770 appinfo_t
*hIC
= NULL
;
773 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
774 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
776 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
780 if (lpwfs
->download_in_progress
!= NULL
)
782 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
786 hIC
= lpwfs
->lpAppInfo
;
787 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
789 WORKREQUEST workRequest
;
790 struct WORKREQ_FTPFINDFIRSTFILEW
*req
;
792 workRequest
.asyncproc
= AsyncFtpFindFirstFileProc
;
793 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
794 req
= &workRequest
.u
.FtpFindFirstFileW
;
795 req
->lpszSearchFile
= (lpszSearchFile
== NULL
) ? NULL
: heap_strdupW(lpszSearchFile
);
796 req
->lpFindFileData
= lpFindFileData
;
797 req
->dwFlags
= dwFlags
;
798 req
->dwContext
= dwContext
;
800 INTERNET_AsyncCall(&workRequest
);
805 r
= FTP_FtpFindFirstFileW(lpwfs
, lpszSearchFile
, lpFindFileData
,
810 WININET_Release( &lpwfs
->hdr
);
816 /***********************************************************************
817 * FTP_FtpFindFirstFileW (Internal)
819 * Search the specified directory
822 * HINTERNET on success
826 static HINTERNET
FTP_FtpFindFirstFileW(ftp_session_t
*lpwfs
,
827 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
830 appinfo_t
*hIC
= NULL
;
831 HINTERNET hFindNext
= NULL
;
835 /* Clear any error information */
836 INTERNET_SetLastError(0);
838 if (!FTP_InitListenSocket(lpwfs
))
841 if (!FTP_SendType(lpwfs
, INTERNET_FLAG_TRANSFER_ASCII
))
844 if (!FTP_SendPortOrPasv(lpwfs
))
847 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_LIST
, NULL
,
848 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
851 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
854 if (nResCode
== 125 || nResCode
== 150)
858 /* Get data socket to server */
859 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
861 hFindNext
= FTP_ReceiveFileList(lpwfs
, nDataSocket
, lpszSearchFile
, lpFindFileData
, dwContext
);
862 closesocket(nDataSocket
);
863 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
864 if (nResCode
!= 226 && nResCode
!= 250)
865 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
869 FTP_SetResponseError(nResCode
);
873 if (lpwfs
->lstnSocket
!= -1)
875 closesocket(lpwfs
->lstnSocket
);
876 lpwfs
->lstnSocket
= -1;
879 hIC
= lpwfs
->lpAppInfo
;
880 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
882 INTERNET_ASYNC_RESULT iar
;
886 iar
.dwResult
= (DWORD_PTR
)hFindNext
;
887 iar
.dwError
= ERROR_SUCCESS
;
888 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
889 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
892 iar
.dwResult
= (DWORD_PTR
)hFindNext
;
893 iar
.dwError
= hFindNext
? ERROR_SUCCESS
: INTERNET_GetLastError();
894 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
895 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
902 /***********************************************************************
903 * FtpGetCurrentDirectoryA (WININET.@)
905 * Retrieves the current directory
912 BOOL WINAPI
FtpGetCurrentDirectoryA(HINTERNET hFtpSession
, LPSTR lpszCurrentDirectory
,
913 LPDWORD lpdwCurrentDirectory
)
919 if(lpdwCurrentDirectory
) {
920 len
= *lpdwCurrentDirectory
;
921 if(lpszCurrentDirectory
)
923 dir
= heap_alloc(len
* sizeof(WCHAR
));
926 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
931 ret
= FtpGetCurrentDirectoryW(hFtpSession
, lpszCurrentDirectory
?dir
:NULL
, lpdwCurrentDirectory
?&len
:NULL
);
933 if (ret
&& lpszCurrentDirectory
)
934 WideCharToMultiByte(CP_ACP
, 0, dir
, -1, lpszCurrentDirectory
, len
, NULL
, NULL
);
936 if (lpdwCurrentDirectory
) *lpdwCurrentDirectory
= len
;
942 static void AsyncFtpGetCurrentDirectoryProc(WORKREQUEST
*workRequest
)
944 struct WORKREQ_FTPGETCURRENTDIRECTORYW
const *req
= &workRequest
->u
.FtpGetCurrentDirectoryW
;
945 ftp_session_t
*lpwfs
= (ftp_session_t
*) workRequest
->hdr
;
947 TRACE("%p\n", lpwfs
);
949 FTP_FtpGetCurrentDirectoryW(lpwfs
, req
->lpszDirectory
, req
->lpdwDirectory
);
952 /***********************************************************************
953 * FtpGetCurrentDirectoryW (WININET.@)
955 * Retrieves the current directory
962 BOOL WINAPI
FtpGetCurrentDirectoryW(HINTERNET hFtpSession
, LPWSTR lpszCurrentDirectory
,
963 LPDWORD lpdwCurrentDirectory
)
965 ftp_session_t
*lpwfs
;
966 appinfo_t
*hIC
= NULL
;
969 TRACE("%p %p %p\n", hFtpSession
, lpszCurrentDirectory
, lpdwCurrentDirectory
);
971 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
974 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
978 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
980 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
984 if (!lpdwCurrentDirectory
)
986 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
990 if (lpszCurrentDirectory
== NULL
)
992 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
996 if (lpwfs
->download_in_progress
!= NULL
)
998 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1002 hIC
= lpwfs
->lpAppInfo
;
1003 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1005 WORKREQUEST workRequest
;
1006 struct WORKREQ_FTPGETCURRENTDIRECTORYW
*req
;
1008 workRequest
.asyncproc
= AsyncFtpGetCurrentDirectoryProc
;
1009 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1010 req
= &workRequest
.u
.FtpGetCurrentDirectoryW
;
1011 req
->lpszDirectory
= lpszCurrentDirectory
;
1012 req
->lpdwDirectory
= lpdwCurrentDirectory
;
1014 r
= res_to_le(INTERNET_AsyncCall(&workRequest
));
1018 r
= FTP_FtpGetCurrentDirectoryW(lpwfs
, lpszCurrentDirectory
,
1019 lpdwCurrentDirectory
);
1024 WININET_Release( &lpwfs
->hdr
);
1030 /***********************************************************************
1031 * FTP_FtpGetCurrentDirectoryW (Internal)
1033 * Retrieves the current directory
1040 static BOOL
FTP_FtpGetCurrentDirectoryW(ftp_session_t
*lpwfs
, LPWSTR lpszCurrentDirectory
,
1041 LPDWORD lpdwCurrentDirectory
)
1044 appinfo_t
*hIC
= NULL
;
1045 DWORD bSuccess
= FALSE
;
1047 /* Clear any error information */
1048 INTERNET_SetLastError(0);
1050 hIC
= lpwfs
->lpAppInfo
;
1051 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PWD
, NULL
,
1052 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
1055 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1058 if (nResCode
== 257) /* Extract directory name */
1060 DWORD firstpos
, lastpos
, len
;
1061 LPWSTR lpszResponseBuffer
= heap_strdupAtoW(INTERNET_GetResponseBuffer());
1063 for (firstpos
= 0, lastpos
= 0; lpszResponseBuffer
[lastpos
]; lastpos
++)
1065 if ('"' == lpszResponseBuffer
[lastpos
])
1073 len
= lastpos
- firstpos
;
1074 if (*lpdwCurrentDirectory
>= len
)
1076 memcpy(lpszCurrentDirectory
, &lpszResponseBuffer
[firstpos
+ 1], len
* sizeof(WCHAR
));
1077 lpszCurrentDirectory
[len
- 1] = 0;
1078 *lpdwCurrentDirectory
= len
;
1081 else INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1083 heap_free(lpszResponseBuffer
);
1086 FTP_SetResponseError(nResCode
);
1090 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1092 INTERNET_ASYNC_RESULT iar
;
1094 iar
.dwResult
= bSuccess
;
1095 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
1096 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1097 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1104 /***********************************************************************
1105 * FTPFILE_Destroy(internal)
1107 * Closes the file transfer handle. This also 'cleans' the data queue of
1108 * the 'transfer complete' message (this is a bit of a hack though :-/ )
1111 static void FTPFILE_Destroy(object_header_t
*hdr
)
1113 ftp_file_t
*lpwh
= (ftp_file_t
*) hdr
;
1114 ftp_session_t
*lpwfs
= lpwh
->lpFtpSession
;
1119 if (lpwh
->cache_file_handle
!= INVALID_HANDLE_VALUE
)
1120 CloseHandle(lpwh
->cache_file_handle
);
1122 heap_free(lpwh
->cache_file
);
1124 if (!lpwh
->session_deleted
)
1125 lpwfs
->download_in_progress
= NULL
;
1127 if (lpwh
->nDataSocket
!= -1)
1128 closesocket(lpwh
->nDataSocket
);
1130 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1131 if (nResCode
> 0 && nResCode
!= 226) WARN("server reports failed transfer\n");
1133 WININET_Release(&lpwh
->lpFtpSession
->hdr
);
1136 static DWORD
FTPFILE_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
1139 case INTERNET_OPTION_HANDLE_TYPE
:
1140 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1142 if (*size
< sizeof(ULONG
))
1143 return ERROR_INSUFFICIENT_BUFFER
;
1145 *size
= sizeof(DWORD
);
1146 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_FTP_FILE
;
1147 return ERROR_SUCCESS
;
1148 case INTERNET_OPTION_DATAFILE_NAME
:
1151 ftp_file_t
*file
= (ftp_file_t
*)hdr
;
1153 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1155 if (!file
->cache_file
)
1158 return ERROR_INTERNET_ITEM_NOT_FOUND
;
1162 required
= (lstrlenW(file
->cache_file
) + 1) * sizeof(WCHAR
);
1163 if (*size
< required
)
1164 return ERROR_INSUFFICIENT_BUFFER
;
1167 memcpy(buffer
, file
->cache_file
, *size
);
1168 return ERROR_SUCCESS
;
1172 required
= WideCharToMultiByte(CP_ACP
, 0, file
->cache_file
, -1, NULL
, 0, NULL
, NULL
);
1173 if (required
> *size
)
1174 return ERROR_INSUFFICIENT_BUFFER
;
1176 *size
= WideCharToMultiByte(CP_ACP
, 0, file
->cache_file
, -1, buffer
, *size
, NULL
, NULL
);
1177 return ERROR_SUCCESS
;
1181 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
1184 static DWORD
FTPFILE_ReadFile(object_header_t
*hdr
, void *buffer
, DWORD size
, DWORD
*read
)
1186 ftp_file_t
*file
= (ftp_file_t
*)hdr
;
1190 if (file
->nDataSocket
== -1)
1191 return ERROR_INTERNET_DISCONNECTED
;
1193 /* FIXME: FTP should use NETCON_ stuff */
1194 res
= recv(file
->nDataSocket
, buffer
, size
, MSG_WAITALL
);
1195 *read
= res
>0 ? res
: 0;
1197 error
= res
>= 0 ? ERROR_SUCCESS
: INTERNET_ERROR_BASE
; /* FIXME */
1198 if (error
== ERROR_SUCCESS
&& file
->cache_file
)
1200 DWORD bytes_written
;
1202 if (!WriteFile(file
->cache_file_handle
, buffer
, *read
, &bytes_written
, NULL
))
1203 WARN("WriteFile failed: %u\n", GetLastError());
1208 static DWORD
FTPFILE_ReadFileExA(object_header_t
*hdr
, INTERNET_BUFFERSA
*buffers
,
1209 DWORD flags
, DWORD_PTR context
)
1211 return FTPFILE_ReadFile(hdr
, buffers
->lpvBuffer
, buffers
->dwBufferLength
, &buffers
->dwBufferLength
);
1214 static DWORD
FTPFILE_ReadFileExW(object_header_t
*hdr
, INTERNET_BUFFERSW
*buffers
,
1215 DWORD flags
, DWORD_PTR context
)
1217 return FTPFILE_ReadFile(hdr
, buffers
->lpvBuffer
, buffers
->dwBufferLength
, &buffers
->dwBufferLength
);
1220 static DWORD
FTPFILE_WriteFile(object_header_t
*hdr
, const void *buffer
, DWORD size
, DWORD
*written
)
1222 ftp_file_t
*lpwh
= (ftp_file_t
*) hdr
;
1225 res
= send(lpwh
->nDataSocket
, buffer
, size
, 0);
1227 *written
= res
>0 ? res
: 0;
1228 return res
>= 0 ? ERROR_SUCCESS
: sock_get_error(errno
);
1231 static void FTP_ReceiveRequestData(ftp_file_t
*file
, BOOL first_notif
)
1233 INTERNET_ASYNC_RESULT iar
;
1237 TRACE("%p\n", file
);
1239 available
= recv(file
->nDataSocket
, buffer
, sizeof(buffer
), MSG_PEEK
);
1241 if(available
!= -1) {
1242 iar
.dwResult
= (DWORD_PTR
)file
->hdr
.hInternet
;
1243 iar
.dwError
= first_notif
? 0 : available
;
1246 iar
.dwError
= INTERNET_GetLastError();
1249 INTERNET_SendCallback(&file
->hdr
, file
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
1250 sizeof(INTERNET_ASYNC_RESULT
));
1253 static void FTPFILE_AsyncQueryDataAvailableProc(WORKREQUEST
*workRequest
)
1255 ftp_file_t
*file
= (ftp_file_t
*)workRequest
->hdr
;
1257 FTP_ReceiveRequestData(file
, FALSE
);
1260 static DWORD
FTPFILE_QueryDataAvailable(object_header_t
*hdr
, DWORD
*available
, DWORD flags
, DWORD_PTR ctx
)
1262 ftp_file_t
*file
= (ftp_file_t
*) hdr
;
1263 int retval
, unread
= 0;
1265 TRACE("(%p %p %x %lx)\n", file
, available
, flags
, ctx
);
1268 retval
= ioctlsocket(file
->nDataSocket
, FIONREAD
, &unread
);
1270 TRACE("%d bytes of queued, but unread data\n", unread
);
1272 FIXME("FIONREAD not available\n");
1275 *available
= unread
;
1282 retval
= recv(file
->nDataSocket
, &byte
, 1, MSG_PEEK
);
1284 WORKREQUEST workRequest
;
1287 workRequest
.asyncproc
= FTPFILE_AsyncQueryDataAvailableProc
;
1288 workRequest
.hdr
= WININET_AddRef( &file
->hdr
);
1290 INTERNET_AsyncCall(&workRequest
);
1292 return ERROR_IO_PENDING
;
1296 return ERROR_SUCCESS
;
1300 static const object_vtbl_t FTPFILEVtbl
= {
1303 FTPFILE_QueryOption
,
1306 FTPFILE_ReadFileExA
,
1307 FTPFILE_ReadFileExW
,
1309 FTPFILE_QueryDataAvailable
,
1313 /***********************************************************************
1314 * FTP_FtpOpenFileW (Internal)
1316 * Open a remote file for writing or reading
1319 * HINTERNET handle on success
1323 static HINTERNET
FTP_FtpOpenFileW(ftp_session_t
*lpwfs
,
1324 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1325 DWORD_PTR dwContext
)
1328 BOOL bSuccess
= FALSE
;
1329 ftp_file_t
*lpwh
= NULL
;
1330 appinfo_t
*hIC
= NULL
;
1334 /* Clear any error information */
1335 INTERNET_SetLastError(0);
1337 if (GENERIC_READ
== fdwAccess
)
1339 /* Set up socket to retrieve data */
1340 bSuccess
= FTP_SendRetrieve(lpwfs
, lpszFileName
, dwFlags
);
1342 else if (GENERIC_WRITE
== fdwAccess
)
1344 /* Set up socket to send data */
1345 bSuccess
= FTP_SendStore(lpwfs
, lpszFileName
, dwFlags
);
1348 /* Get data socket to server */
1349 if (bSuccess
&& FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1351 lpwh
= alloc_object(&lpwfs
->hdr
, &FTPFILEVtbl
, sizeof(ftp_file_t
));
1352 lpwh
->hdr
.htype
= WH_HFILE
;
1353 lpwh
->hdr
.dwFlags
= dwFlags
;
1354 lpwh
->hdr
.dwContext
= dwContext
;
1355 lpwh
->nDataSocket
= nDataSocket
;
1356 lpwh
->cache_file
= NULL
;
1357 lpwh
->cache_file_handle
= INVALID_HANDLE_VALUE
;
1358 lpwh
->session_deleted
= FALSE
;
1360 WININET_AddRef( &lpwfs
->hdr
);
1361 lpwh
->lpFtpSession
= lpwfs
;
1362 list_add_head( &lpwfs
->hdr
.children
, &lpwh
->hdr
.entry
);
1364 /* Indicate that a download is currently in progress */
1365 lpwfs
->download_in_progress
= lpwh
;
1368 if (lpwfs
->lstnSocket
!= -1)
1370 closesocket(lpwfs
->lstnSocket
);
1371 lpwfs
->lstnSocket
= -1;
1374 if (bSuccess
&& fdwAccess
== GENERIC_READ
)
1376 WCHAR filename
[MAX_PATH
+ 1];
1380 memset(&uc
, 0, sizeof(uc
));
1381 uc
.dwStructSize
= sizeof(uc
);
1382 uc
.nScheme
= INTERNET_SCHEME_FTP
;
1383 uc
.lpszHostName
= lpwfs
->servername
;
1384 uc
.nPort
= lpwfs
->serverport
;
1385 uc
.lpszUserName
= lpwfs
->lpszUserName
;
1386 uc
.lpszUrlPath
= heap_strdupW(lpszFileName
);
1388 if (!InternetCreateUrlW(&uc
, 0, NULL
, &len
) && GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1390 WCHAR
*url
= heap_alloc(len
* sizeof(WCHAR
));
1392 if (url
&& InternetCreateUrlW(&uc
, 0, url
, &len
) && CreateUrlCacheEntryW(url
, 0, NULL
, filename
, 0))
1394 lpwh
->cache_file
= heap_strdupW(filename
);
1395 lpwh
->cache_file_handle
= CreateFileW(filename
, GENERIC_WRITE
, FILE_SHARE_READ
,
1396 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1397 if (lpwh
->cache_file_handle
== INVALID_HANDLE_VALUE
)
1399 WARN("Could not create cache file: %u\n", GetLastError());
1400 heap_free(lpwh
->cache_file
);
1401 lpwh
->cache_file
= NULL
;
1406 heap_free(uc
.lpszUrlPath
);
1409 hIC
= lpwfs
->lpAppInfo
;
1410 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1412 INTERNET_ASYNC_RESULT iar
;
1416 iar
.dwResult
= (DWORD_PTR
)lpwh
->hdr
.hInternet
;
1417 iar
.dwError
= ERROR_SUCCESS
;
1418 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
1419 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1423 FTP_ReceiveRequestData(lpwh
, TRUE
);
1426 iar
.dwError
= INTERNET_GetLastError();
1427 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1428 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1435 return lpwh
->hdr
.hInternet
;
1439 /***********************************************************************
1440 * FtpOpenFileA (WININET.@)
1442 * Open a remote file for writing or reading
1445 * HINTERNET handle on success
1449 HINTERNET WINAPI
FtpOpenFileA(HINTERNET hFtpSession
,
1450 LPCSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1451 DWORD_PTR dwContext
)
1453 LPWSTR lpwzFileName
;
1456 lpwzFileName
= heap_strdupAtoW(lpszFileName
);
1457 ret
= FtpOpenFileW(hFtpSession
, lpwzFileName
, fdwAccess
, dwFlags
, dwContext
);
1458 heap_free(lpwzFileName
);
1463 static void AsyncFtpOpenFileProc(WORKREQUEST
*workRequest
)
1465 struct WORKREQ_FTPOPENFILEW
const *req
= &workRequest
->u
.FtpOpenFileW
;
1466 ftp_session_t
*lpwfs
= (ftp_session_t
*) workRequest
->hdr
;
1468 TRACE("%p\n", lpwfs
);
1470 FTP_FtpOpenFileW(lpwfs
, req
->lpszFilename
,
1471 req
->dwAccess
, req
->dwFlags
, req
->dwContext
);
1472 heap_free(req
->lpszFilename
);
1475 /***********************************************************************
1476 * FtpOpenFileW (WININET.@)
1478 * Open a remote file for writing or reading
1481 * HINTERNET handle on success
1485 HINTERNET WINAPI
FtpOpenFileW(HINTERNET hFtpSession
,
1486 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1487 DWORD_PTR dwContext
)
1489 ftp_session_t
*lpwfs
;
1490 appinfo_t
*hIC
= NULL
;
1493 TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession
,
1494 debugstr_w(lpszFileName
), fdwAccess
, dwFlags
, dwContext
);
1496 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
1499 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1503 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1505 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1509 if ((!lpszFileName
) ||
1510 ((fdwAccess
!= GENERIC_READ
) && (fdwAccess
!= GENERIC_WRITE
)) ||
1511 ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
))
1513 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1517 if (lpwfs
->download_in_progress
!= NULL
)
1519 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1523 hIC
= lpwfs
->lpAppInfo
;
1524 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1526 WORKREQUEST workRequest
;
1527 struct WORKREQ_FTPOPENFILEW
*req
;
1529 workRequest
.asyncproc
= AsyncFtpOpenFileProc
;
1530 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1531 req
= &workRequest
.u
.FtpOpenFileW
;
1532 req
->lpszFilename
= heap_strdupW(lpszFileName
);
1533 req
->dwAccess
= fdwAccess
;
1534 req
->dwFlags
= dwFlags
;
1535 req
->dwContext
= dwContext
;
1537 INTERNET_AsyncCall(&workRequest
);
1542 r
= FTP_FtpOpenFileW(lpwfs
, lpszFileName
, fdwAccess
, dwFlags
, dwContext
);
1546 WININET_Release( &lpwfs
->hdr
);
1552 /***********************************************************************
1553 * FtpGetFileA (WININET.@)
1555 * Retrieve file from the FTP server
1562 BOOL WINAPI
FtpGetFileA(HINTERNET hInternet
, LPCSTR lpszRemoteFile
, LPCSTR lpszNewFile
,
1563 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1564 DWORD_PTR dwContext
)
1566 LPWSTR lpwzRemoteFile
;
1570 lpwzRemoteFile
= heap_strdupAtoW(lpszRemoteFile
);
1571 lpwzNewFile
= heap_strdupAtoW(lpszNewFile
);
1572 ret
= FtpGetFileW(hInternet
, lpwzRemoteFile
, lpwzNewFile
, fFailIfExists
,
1573 dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1574 heap_free(lpwzRemoteFile
);
1575 heap_free(lpwzNewFile
);
1580 static void AsyncFtpGetFileProc(WORKREQUEST
*workRequest
)
1582 struct WORKREQ_FTPGETFILEW
const *req
= &workRequest
->u
.FtpGetFileW
;
1583 ftp_session_t
*lpwfs
= (ftp_session_t
*) workRequest
->hdr
;
1585 TRACE("%p\n", lpwfs
);
1587 FTP_FtpGetFileW(lpwfs
, req
->lpszRemoteFile
,
1588 req
->lpszNewFile
, req
->fFailIfExists
,
1589 req
->dwLocalFlagsAttribute
, req
->dwFlags
, req
->dwContext
);
1590 heap_free(req
->lpszRemoteFile
);
1591 heap_free(req
->lpszNewFile
);
1595 /***********************************************************************
1596 * FtpGetFileW (WININET.@)
1598 * Retrieve file from the FTP server
1605 BOOL WINAPI
FtpGetFileW(HINTERNET hInternet
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1606 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1607 DWORD_PTR dwContext
)
1609 ftp_session_t
*lpwfs
;
1610 appinfo_t
*hIC
= NULL
;
1613 if (!lpszRemoteFile
|| !lpszNewFile
)
1615 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1619 lpwfs
= (ftp_session_t
*) get_handle_object( hInternet
);
1622 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1626 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1628 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1632 if ((dwInternetFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
1634 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1638 if (lpwfs
->download_in_progress
!= NULL
)
1640 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1644 hIC
= lpwfs
->lpAppInfo
;
1645 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1647 WORKREQUEST workRequest
;
1648 struct WORKREQ_FTPGETFILEW
*req
;
1650 workRequest
.asyncproc
= AsyncFtpGetFileProc
;
1651 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1652 req
= &workRequest
.u
.FtpGetFileW
;
1653 req
->lpszRemoteFile
= heap_strdupW(lpszRemoteFile
);
1654 req
->lpszNewFile
= heap_strdupW(lpszNewFile
);
1655 req
->dwLocalFlagsAttribute
= dwLocalFlagsAttribute
;
1656 req
->fFailIfExists
= fFailIfExists
;
1657 req
->dwFlags
= dwInternetFlags
;
1658 req
->dwContext
= dwContext
;
1660 r
= res_to_le(INTERNET_AsyncCall(&workRequest
));
1664 r
= FTP_FtpGetFileW(lpwfs
, lpszRemoteFile
, lpszNewFile
,
1665 fFailIfExists
, dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1669 WININET_Release( &lpwfs
->hdr
);
1675 /***********************************************************************
1676 * FTP_FtpGetFileW (Internal)
1678 * Retrieve file from the FTP server
1685 static BOOL
FTP_FtpGetFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1686 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1687 DWORD_PTR dwContext
)
1689 BOOL bSuccess
= FALSE
;
1691 appinfo_t
*hIC
= NULL
;
1693 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile
), debugstr_w(lpszNewFile
));
1695 /* Clear any error information */
1696 INTERNET_SetLastError(0);
1698 /* Ensure we can write to lpszNewfile by opening it */
1699 hFile
= CreateFileW(lpszNewFile
, GENERIC_WRITE
, 0, 0, fFailIfExists
?
1700 CREATE_NEW
: CREATE_ALWAYS
, dwLocalFlagsAttribute
, 0);
1701 if (INVALID_HANDLE_VALUE
== hFile
)
1704 /* Set up socket to retrieve data */
1705 if (FTP_SendRetrieve(lpwfs
, lpszRemoteFile
, dwInternetFlags
))
1709 /* Get data socket to server */
1710 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1715 FTP_RetrieveFileData(lpwfs
, nDataSocket
, hFile
);
1716 closesocket(nDataSocket
);
1718 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
1721 if (nResCode
== 226)
1724 FTP_SetResponseError(nResCode
);
1729 if (lpwfs
->lstnSocket
!= -1)
1731 closesocket(lpwfs
->lstnSocket
);
1732 lpwfs
->lstnSocket
= -1;
1737 hIC
= lpwfs
->lpAppInfo
;
1738 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1740 INTERNET_ASYNC_RESULT iar
;
1742 iar
.dwResult
= (DWORD
)bSuccess
;
1743 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1744 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1745 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1748 if (!bSuccess
) DeleteFileW(lpszNewFile
);
1752 /***********************************************************************
1753 * FtpGetFileSize (WININET.@)
1755 DWORD WINAPI
FtpGetFileSize( HINTERNET hFile
, LPDWORD lpdwFileSizeHigh
)
1757 FIXME("(%p, %p)\n", hFile
, lpdwFileSizeHigh
);
1759 if (lpdwFileSizeHigh
)
1760 *lpdwFileSizeHigh
= 0;
1765 /***********************************************************************
1766 * FtpDeleteFileA (WININET.@)
1768 * Delete a file on the ftp server
1775 BOOL WINAPI
FtpDeleteFileA(HINTERNET hFtpSession
, LPCSTR lpszFileName
)
1777 LPWSTR lpwzFileName
;
1780 lpwzFileName
= heap_strdupAtoW(lpszFileName
);
1781 ret
= FtpDeleteFileW(hFtpSession
, lpwzFileName
);
1782 heap_free(lpwzFileName
);
1786 static void AsyncFtpDeleteFileProc(WORKREQUEST
*workRequest
)
1788 struct WORKREQ_FTPDELETEFILEW
const *req
= &workRequest
->u
.FtpDeleteFileW
;
1789 ftp_session_t
*lpwfs
= (ftp_session_t
*) workRequest
->hdr
;
1791 TRACE("%p\n", lpwfs
);
1793 FTP_FtpDeleteFileW(lpwfs
, req
->lpszFilename
);
1794 heap_free(req
->lpszFilename
);
1797 /***********************************************************************
1798 * FtpDeleteFileW (WININET.@)
1800 * Delete a file on the ftp server
1807 BOOL WINAPI
FtpDeleteFileW(HINTERNET hFtpSession
, LPCWSTR lpszFileName
)
1809 ftp_session_t
*lpwfs
;
1810 appinfo_t
*hIC
= NULL
;
1813 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
1816 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1820 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1822 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1826 if (lpwfs
->download_in_progress
!= NULL
)
1828 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1834 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1838 hIC
= lpwfs
->lpAppInfo
;
1839 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1841 WORKREQUEST workRequest
;
1842 struct WORKREQ_FTPDELETEFILEW
*req
;
1844 workRequest
.asyncproc
= AsyncFtpDeleteFileProc
;
1845 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1846 req
= &workRequest
.u
.FtpDeleteFileW
;
1847 req
->lpszFilename
= heap_strdupW(lpszFileName
);
1849 r
= res_to_le(INTERNET_AsyncCall(&workRequest
));
1853 r
= FTP_FtpDeleteFileW(lpwfs
, lpszFileName
);
1857 WININET_Release( &lpwfs
->hdr
);
1862 /***********************************************************************
1863 * FTP_FtpDeleteFileW (Internal)
1865 * Delete a file on the ftp server
1872 BOOL
FTP_FtpDeleteFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszFileName
)
1875 BOOL bSuccess
= FALSE
;
1876 appinfo_t
*hIC
= NULL
;
1878 TRACE("%p\n", lpwfs
);
1880 /* Clear any error information */
1881 INTERNET_SetLastError(0);
1883 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_DELE
, lpszFileName
, 0, 0, 0))
1886 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1889 if (nResCode
== 250)
1892 FTP_SetResponseError(nResCode
);
1895 hIC
= lpwfs
->lpAppInfo
;
1896 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1898 INTERNET_ASYNC_RESULT iar
;
1900 iar
.dwResult
= (DWORD
)bSuccess
;
1901 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1902 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1903 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1910 /***********************************************************************
1911 * FtpRemoveDirectoryA (WININET.@)
1913 * Remove a directory on the ftp server
1920 BOOL WINAPI
FtpRemoveDirectoryA(HINTERNET hFtpSession
, LPCSTR lpszDirectory
)
1922 LPWSTR lpwzDirectory
;
1925 lpwzDirectory
= heap_strdupAtoW(lpszDirectory
);
1926 ret
= FtpRemoveDirectoryW(hFtpSession
, lpwzDirectory
);
1927 heap_free(lpwzDirectory
);
1931 static void AsyncFtpRemoveDirectoryProc(WORKREQUEST
*workRequest
)
1933 struct WORKREQ_FTPREMOVEDIRECTORYW
const *req
= &workRequest
->u
.FtpRemoveDirectoryW
;
1934 ftp_session_t
*lpwfs
= (ftp_session_t
*) workRequest
->hdr
;
1936 TRACE("%p\n", lpwfs
);
1938 FTP_FtpRemoveDirectoryW(lpwfs
, req
->lpszDirectory
);
1939 heap_free(req
->lpszDirectory
);
1942 /***********************************************************************
1943 * FtpRemoveDirectoryW (WININET.@)
1945 * Remove a directory on the ftp server
1952 BOOL WINAPI
FtpRemoveDirectoryW(HINTERNET hFtpSession
, LPCWSTR lpszDirectory
)
1954 ftp_session_t
*lpwfs
;
1955 appinfo_t
*hIC
= NULL
;
1958 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
1961 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1965 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1967 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1971 if (lpwfs
->download_in_progress
!= NULL
)
1973 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1979 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1983 hIC
= lpwfs
->lpAppInfo
;
1984 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1986 WORKREQUEST workRequest
;
1987 struct WORKREQ_FTPREMOVEDIRECTORYW
*req
;
1989 workRequest
.asyncproc
= AsyncFtpRemoveDirectoryProc
;
1990 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1991 req
= &workRequest
.u
.FtpRemoveDirectoryW
;
1992 req
->lpszDirectory
= heap_strdupW(lpszDirectory
);
1994 r
= res_to_le(INTERNET_AsyncCall(&workRequest
));
1998 r
= FTP_FtpRemoveDirectoryW(lpwfs
, lpszDirectory
);
2002 WININET_Release( &lpwfs
->hdr
);
2007 /***********************************************************************
2008 * FTP_FtpRemoveDirectoryW (Internal)
2010 * Remove a directory on the ftp server
2017 BOOL
FTP_FtpRemoveDirectoryW(ftp_session_t
*lpwfs
, LPCWSTR lpszDirectory
)
2020 BOOL bSuccess
= FALSE
;
2021 appinfo_t
*hIC
= NULL
;
2025 /* Clear any error information */
2026 INTERNET_SetLastError(0);
2028 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RMD
, lpszDirectory
, 0, 0, 0))
2031 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2034 if (nResCode
== 250)
2037 FTP_SetResponseError(nResCode
);
2041 hIC
= lpwfs
->lpAppInfo
;
2042 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2044 INTERNET_ASYNC_RESULT iar
;
2046 iar
.dwResult
= (DWORD
)bSuccess
;
2047 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
2048 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
2049 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
2056 /***********************************************************************
2057 * FtpRenameFileA (WININET.@)
2059 * Rename a file on the ftp server
2066 BOOL WINAPI
FtpRenameFileA(HINTERNET hFtpSession
, LPCSTR lpszSrc
, LPCSTR lpszDest
)
2072 lpwzSrc
= heap_strdupAtoW(lpszSrc
);
2073 lpwzDest
= heap_strdupAtoW(lpszDest
);
2074 ret
= FtpRenameFileW(hFtpSession
, lpwzSrc
, lpwzDest
);
2076 heap_free(lpwzDest
);
2080 static void AsyncFtpRenameFileProc(WORKREQUEST
*workRequest
)
2082 struct WORKREQ_FTPRENAMEFILEW
const *req
= &workRequest
->u
.FtpRenameFileW
;
2083 ftp_session_t
*lpwfs
= (ftp_session_t
*) workRequest
->hdr
;
2085 TRACE("%p\n", lpwfs
);
2087 FTP_FtpRenameFileW(lpwfs
, req
->lpszSrcFile
, req
->lpszDestFile
);
2088 heap_free(req
->lpszSrcFile
);
2089 heap_free(req
->lpszDestFile
);
2092 /***********************************************************************
2093 * FtpRenameFileW (WININET.@)
2095 * Rename a file on the ftp server
2102 BOOL WINAPI
FtpRenameFileW(HINTERNET hFtpSession
, LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
2104 ftp_session_t
*lpwfs
;
2105 appinfo_t
*hIC
= NULL
;
2108 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
2111 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
2115 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
2117 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2121 if (lpwfs
->download_in_progress
!= NULL
)
2123 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
2127 if (!lpszSrc
|| !lpszDest
)
2129 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2133 hIC
= lpwfs
->lpAppInfo
;
2134 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2136 WORKREQUEST workRequest
;
2137 struct WORKREQ_FTPRENAMEFILEW
*req
;
2139 workRequest
.asyncproc
= AsyncFtpRenameFileProc
;
2140 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
2141 req
= &workRequest
.u
.FtpRenameFileW
;
2142 req
->lpszSrcFile
= heap_strdupW(lpszSrc
);
2143 req
->lpszDestFile
= heap_strdupW(lpszDest
);
2145 r
= res_to_le(INTERNET_AsyncCall(&workRequest
));
2149 r
= FTP_FtpRenameFileW(lpwfs
, lpszSrc
, lpszDest
);
2153 WININET_Release( &lpwfs
->hdr
);
2158 /***********************************************************************
2159 * FTP_FtpRenameFileW (Internal)
2161 * Rename a file on the ftp server
2168 BOOL
FTP_FtpRenameFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
2171 BOOL bSuccess
= FALSE
;
2172 appinfo_t
*hIC
= NULL
;
2176 /* Clear any error information */
2177 INTERNET_SetLastError(0);
2179 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNFR
, lpszSrc
, 0, 0, 0))
2182 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2183 if (nResCode
== 350)
2185 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNTO
, lpszDest
, 0, 0, 0))
2188 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2191 if (nResCode
== 250)
2194 FTP_SetResponseError(nResCode
);
2197 hIC
= lpwfs
->lpAppInfo
;
2198 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2200 INTERNET_ASYNC_RESULT iar
;
2202 iar
.dwResult
= (DWORD
)bSuccess
;
2203 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
2204 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
2205 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
2211 /***********************************************************************
2212 * FtpCommandA (WININET.@)
2214 BOOL WINAPI
FtpCommandA( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
2215 LPCSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
2220 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
2221 debugstr_a(lpszCommand
), dwContext
, phFtpCommand
);
2223 if (fExpectResponse
)
2225 FIXME("data connection not supported\n");
2229 if (!lpszCommand
|| !lpszCommand
[0])
2231 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2235 if (!(cmdW
= heap_strdupAtoW(lpszCommand
)))
2237 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2241 r
= FtpCommandW(hConnect
, fExpectResponse
, dwFlags
, cmdW
, dwContext
, phFtpCommand
);
2247 /***********************************************************************
2248 * FtpCommandW (WININET.@)
2250 BOOL WINAPI
FtpCommandW( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
2251 LPCWSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
2254 ftp_session_t
*lpwfs
;
2256 DWORD len
, nBytesSent
= 0;
2257 INT nResCode
, nRC
= 0;
2259 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
2260 debugstr_w(lpszCommand
), dwContext
, phFtpCommand
);
2262 if (!lpszCommand
|| !lpszCommand
[0])
2264 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2268 if (fExpectResponse
)
2270 FIXME("data connection not supported\n");
2274 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
2277 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
2281 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
2283 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2287 if (lpwfs
->download_in_progress
!= NULL
)
2289 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
2293 len
= WideCharToMultiByte(CP_ACP
, 0, lpszCommand
, -1, NULL
, 0, NULL
, NULL
) + strlen(szCRLF
);
2294 if ((cmd
= heap_alloc(len
)))
2295 WideCharToMultiByte(CP_ACP
, 0, lpszCommand
, -1, cmd
, len
, NULL
, NULL
);
2298 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2302 strcat(cmd
, szCRLF
);
2305 TRACE("Sending (%s) len(%d)\n", cmd
, len
);
2306 while ((nBytesSent
< len
) && (nRC
!= -1))
2308 nRC
= send(lpwfs
->sndSocket
, cmd
+ nBytesSent
, len
- nBytesSent
, 0);
2312 TRACE("Sent %d bytes\n", nRC
);
2318 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2319 if (nResCode
> 0 && nResCode
< 400)
2322 FTP_SetResponseError(nResCode
);
2326 WININET_Release( &lpwfs
->hdr
);
2332 /***********************************************************************
2333 * FTPSESSION_Destroy (internal)
2335 * Deallocate session handle
2337 static void FTPSESSION_Destroy(object_header_t
*hdr
)
2339 ftp_session_t
*lpwfs
= (ftp_session_t
*) hdr
;
2343 WININET_Release(&lpwfs
->lpAppInfo
->hdr
);
2345 heap_free(lpwfs
->lpszPassword
);
2346 heap_free(lpwfs
->lpszUserName
);
2347 heap_free(lpwfs
->servername
);
2350 static void FTPSESSION_CloseConnection(object_header_t
*hdr
)
2352 ftp_session_t
*lpwfs
= (ftp_session_t
*) hdr
;
2356 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
2357 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
2359 if (lpwfs
->download_in_progress
!= NULL
)
2360 lpwfs
->download_in_progress
->session_deleted
= TRUE
;
2362 if (lpwfs
->sndSocket
!= -1)
2363 closesocket(lpwfs
->sndSocket
);
2365 if (lpwfs
->lstnSocket
!= -1)
2366 closesocket(lpwfs
->lstnSocket
);
2368 if (lpwfs
->pasvSocket
!= -1)
2369 closesocket(lpwfs
->pasvSocket
);
2371 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
2372 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
2375 static DWORD
FTPSESSION_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
2378 case INTERNET_OPTION_HANDLE_TYPE
:
2379 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2381 if (*size
< sizeof(ULONG
))
2382 return ERROR_INSUFFICIENT_BUFFER
;
2384 *size
= sizeof(DWORD
);
2385 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_CONNECT_FTP
;
2386 return ERROR_SUCCESS
;
2389 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
2392 static const object_vtbl_t FTPSESSIONVtbl
= {
2394 FTPSESSION_CloseConnection
,
2395 FTPSESSION_QueryOption
,
2405 /***********************************************************************
2406 * FTP_Connect (internal)
2408 * Connect to a ftp server
2411 * HINTERNET a session handle on success
2416 * Windows uses 'anonymous' as the username, when given a NULL username
2417 * and a NULL password. The password is first looked up in:
2419 * HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\EmailName
2421 * If this entry is not present it uses the current username as the password.
2425 HINTERNET
FTP_Connect(appinfo_t
*hIC
, LPCWSTR lpszServerName
,
2426 INTERNET_PORT nServerPort
, LPCWSTR lpszUserName
,
2427 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
2428 DWORD dwInternalFlags
)
2430 static const WCHAR szKey
[] = {'S','o','f','t','w','a','r','e','\\',
2431 'M','i','c','r','o','s','o','f','t','\\',
2432 'W','i','n','d','o','w','s','\\',
2433 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2434 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
2435 static const WCHAR szValue
[] = {'E','m','a','i','l','N','a','m','e',0};
2436 static const WCHAR szDefaultUsername
[] = {'a','n','o','n','y','m','o','u','s','\0'};
2437 static const WCHAR szEmpty
[] = {'\0'};
2438 struct sockaddr_in socketAddr
;
2440 socklen_t sock_namelen
;
2441 BOOL bSuccess
= FALSE
;
2442 ftp_session_t
*lpwfs
= NULL
;
2443 char szaddr
[INET_ADDRSTRLEN
];
2445 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
2446 hIC
, debugstr_w(lpszServerName
),
2447 nServerPort
, debugstr_w(lpszUserName
), debugstr_w(lpszPassword
));
2449 assert( hIC
->hdr
.htype
== WH_HINIT
);
2451 if ((!lpszUserName
|| !*lpszUserName
) && lpszPassword
&& *lpszPassword
)
2453 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2457 lpwfs
= alloc_object(&hIC
->hdr
, &FTPSESSIONVtbl
, sizeof(ftp_session_t
));
2460 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2464 if (nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
2465 lpwfs
->serverport
= INTERNET_DEFAULT_FTP_PORT
;
2467 lpwfs
->serverport
= nServerPort
;
2469 lpwfs
->hdr
.htype
= WH_HFTPSESSION
;
2470 lpwfs
->hdr
.dwFlags
= dwFlags
;
2471 lpwfs
->hdr
.dwContext
= dwContext
;
2472 lpwfs
->hdr
.dwInternalFlags
|= dwInternalFlags
;
2473 lpwfs
->download_in_progress
= NULL
;
2474 lpwfs
->sndSocket
= -1;
2475 lpwfs
->lstnSocket
= -1;
2476 lpwfs
->pasvSocket
= -1;
2478 WININET_AddRef( &hIC
->hdr
);
2479 lpwfs
->lpAppInfo
= hIC
;
2480 list_add_head( &hIC
->hdr
.children
, &lpwfs
->hdr
.entry
);
2482 if(hIC
->proxy
&& hIC
->accessType
== INTERNET_OPEN_TYPE_PROXY
) {
2483 if(strchrW(hIC
->proxy
, ' '))
2484 FIXME("Several proxies not implemented.\n");
2485 if(hIC
->proxyBypass
)
2486 FIXME("Proxy bypass is ignored.\n");
2488 if (!lpszUserName
|| !strlenW(lpszUserName
)) {
2490 WCHAR szPassword
[MAX_PATH
];
2491 DWORD len
= sizeof(szPassword
);
2493 lpwfs
->lpszUserName
= heap_strdupW(szDefaultUsername
);
2495 RegOpenKeyW(HKEY_CURRENT_USER
, szKey
, &key
);
2496 if (RegQueryValueExW(key
, szValue
, NULL
, NULL
, (LPBYTE
)szPassword
, &len
)) {
2497 /* Nothing in the registry, get the username and use that as the password */
2498 if (!GetUserNameW(szPassword
, &len
)) {
2499 /* Should never get here, but use an empty password as failsafe */
2500 strcpyW(szPassword
, szEmpty
);
2505 TRACE("Password used for anonymous ftp : (%s)\n", debugstr_w(szPassword
));
2506 lpwfs
->lpszPassword
= heap_strdupW(szPassword
);
2509 lpwfs
->lpszUserName
= heap_strdupW(lpszUserName
);
2510 lpwfs
->lpszPassword
= heap_strdupW(lpszPassword
? lpszPassword
: szEmpty
);
2512 lpwfs
->servername
= heap_strdupW(lpszServerName
);
2514 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2515 if (!(lpwfs
->hdr
.dwInternalFlags
& INET_OPENURL
))
2517 INTERNET_ASYNC_RESULT iar
;
2519 iar
.dwResult
= (DWORD_PTR
)lpwfs
->hdr
.hInternet
;
2520 iar
.dwError
= ERROR_SUCCESS
;
2522 SendAsyncCallback(&hIC
->hdr
, dwContext
,
2523 INTERNET_STATUS_HANDLE_CREATED
, &iar
,
2524 sizeof(INTERNET_ASYNC_RESULT
));
2527 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_RESOLVING_NAME
,
2528 (LPWSTR
) lpszServerName
, (strlenW(lpszServerName
)+1) * sizeof(WCHAR
));
2530 sock_namelen
= sizeof(socketAddr
);
2531 if (!GetAddress(lpszServerName
, lpwfs
->serverport
, (struct sockaddr
*)&socketAddr
, &sock_namelen
))
2533 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
2537 if (socketAddr
.sin_family
!= AF_INET
)
2539 WARN("unsupported address family %d\n", socketAddr
.sin_family
);
2540 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2544 inet_ntop(socketAddr
.sin_family
, &socketAddr
.sin_addr
, szaddr
, sizeof(szaddr
));
2545 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_NAME_RESOLVED
,
2546 szaddr
, strlen(szaddr
)+1);
2548 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
2551 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2555 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTING_TO_SERVER
,
2556 szaddr
, strlen(szaddr
)+1);
2558 if (connect(nsocket
, (struct sockaddr
*)&socketAddr
, sock_namelen
) < 0)
2560 ERR("Unable to connect (%s)\n", strerror(errno
));
2561 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2562 closesocket(nsocket
);
2566 TRACE("Connected to server\n");
2567 lpwfs
->sndSocket
= nsocket
;
2568 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTED_TO_SERVER
,
2569 szaddr
, strlen(szaddr
)+1);
2571 sock_namelen
= sizeof(lpwfs
->socketAddress
);
2572 getsockname(nsocket
, (struct sockaddr
*) &lpwfs
->socketAddress
, &sock_namelen
);
2574 if (FTP_ConnectToHost(lpwfs
))
2576 TRACE("Successfully logged into server\n");
2585 WININET_Release( &lpwfs
->hdr
);
2589 return lpwfs
->hdr
.hInternet
;
2593 /***********************************************************************
2594 * FTP_ConnectToHost (internal)
2596 * Connect to a ftp server
2603 static BOOL
FTP_ConnectToHost(ftp_session_t
*lpwfs
)
2606 BOOL bSuccess
= FALSE
;
2609 FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2611 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_USER
, lpwfs
->lpszUserName
, 0, 0, 0))
2614 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2617 /* Login successful... */
2618 if (nResCode
== 230)
2620 /* User name okay, need password... */
2621 else if (nResCode
== 331)
2622 bSuccess
= FTP_SendPassword(lpwfs
);
2623 /* Need account for login... */
2624 else if (nResCode
== 332)
2625 bSuccess
= FTP_SendAccount(lpwfs
);
2627 FTP_SetResponseError(nResCode
);
2630 TRACE("Returning %d\n", bSuccess
);
2636 /***********************************************************************
2637 * FTP_SendCommandA (internal)
2639 * Send command to server
2646 static BOOL
FTP_SendCommandA(INT nSocket
, FTP_COMMAND ftpCmd
, LPCSTR lpszParam
,
2647 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
)
2651 DWORD nBytesSent
= 0;
2655 TRACE("%d: (%s) %d\n", ftpCmd
, debugstr_a(lpszParam
), nSocket
);
2659 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
2662 dwParamLen
= lpszParam
?strlen(lpszParam
)+1:0;
2663 len
= dwParamLen
+ strlen(szFtpCommands
[ftpCmd
]) + strlen(szCRLF
);
2664 if (NULL
== (buf
= heap_alloc(len
+1)))
2666 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2669 sprintf(buf
, "%s%s%s%s", szFtpCommands
[ftpCmd
], dwParamLen
? " " : "",
2670 dwParamLen
? lpszParam
: "", szCRLF
);
2672 TRACE("Sending (%s) len(%d)\n", buf
, len
);
2673 while((nBytesSent
< len
) && (nRC
!= -1))
2675 nRC
= send(nSocket
, buf
+nBytesSent
, len
- nBytesSent
, 0);
2682 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_REQUEST_SENT
,
2683 &nBytesSent
, sizeof(DWORD
));
2686 TRACE("Sent %d bytes\n", nBytesSent
);
2690 /***********************************************************************
2691 * FTP_SendCommand (internal)
2693 * Send command to server
2700 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
2701 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
)
2704 LPSTR lpszParamA
= heap_strdupWtoA(lpszParam
);
2705 ret
= FTP_SendCommandA(nSocket
, ftpCmd
, lpszParamA
, lpfnStatusCB
, hdr
, dwContext
);
2706 heap_free(lpszParamA
);
2710 /***********************************************************************
2711 * FTP_ReceiveResponse (internal)
2713 * Receive response from server
2716 * Reply code on success
2720 INT
FTP_ReceiveResponse(ftp_session_t
*lpwfs
, DWORD_PTR dwContext
)
2722 LPSTR lpszResponse
= INTERNET_GetResponseBuffer();
2725 char firstprefix
[5];
2726 BOOL multiline
= FALSE
;
2728 TRACE("socket(%d)\n", lpwfs
->sndSocket
);
2730 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
2734 if (!INTERNET_GetNextLine(lpwfs
->sndSocket
, &nRecv
))
2741 if(lpszResponse
[3] != '-')
2744 { /* Start of multiline response. Loop until we get "nnn " */
2746 memcpy(firstprefix
, lpszResponse
, 3);
2747 firstprefix
[3] = ' ';
2748 firstprefix
[4] = '\0';
2753 if(!memcmp(firstprefix
, lpszResponse
, 4))
2761 rc
= atoi(lpszResponse
);
2763 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
2764 &nRecv
, sizeof(DWORD
));
2768 TRACE("return %d\n", rc
);
2773 /***********************************************************************
2774 * FTP_SendPassword (internal)
2776 * Send password to ftp server
2783 static BOOL
FTP_SendPassword(ftp_session_t
*lpwfs
)
2786 BOOL bSuccess
= FALSE
;
2789 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASS
, lpwfs
->lpszPassword
, 0, 0, 0))
2792 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2795 TRACE("Received reply code %d\n", nResCode
);
2796 /* Login successful... */
2797 if (nResCode
== 230)
2799 /* Command not implemented, superfluous at the server site... */
2800 /* Need account for login... */
2801 else if (nResCode
== 332)
2802 bSuccess
= FTP_SendAccount(lpwfs
);
2804 FTP_SetResponseError(nResCode
);
2808 TRACE("Returning %d\n", bSuccess
);
2813 /***********************************************************************
2814 * FTP_SendAccount (internal)
2823 static BOOL
FTP_SendAccount(ftp_session_t
*lpwfs
)
2826 BOOL bSuccess
= FALSE
;
2829 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_ACCT
, szNoAccount
, 0, 0, 0))
2832 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2836 FTP_SetResponseError(nResCode
);
2843 /***********************************************************************
2844 * FTP_SendStore (internal)
2846 * Send request to upload file to ftp server
2853 static BOOL
FTP_SendStore(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
2856 BOOL bSuccess
= FALSE
;
2859 if (!FTP_InitListenSocket(lpwfs
))
2862 if (!FTP_SendType(lpwfs
, dwType
))
2865 if (!FTP_SendPortOrPasv(lpwfs
))
2868 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_STOR
, lpszRemoteFile
, 0, 0, 0))
2870 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2873 if (nResCode
== 150 || nResCode
== 125)
2876 FTP_SetResponseError(nResCode
);
2880 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2882 closesocket(lpwfs
->lstnSocket
);
2883 lpwfs
->lstnSocket
= -1;
2890 /***********************************************************************
2891 * FTP_InitListenSocket (internal)
2893 * Create a socket to listen for server response
2900 static BOOL
FTP_InitListenSocket(ftp_session_t
*lpwfs
)
2902 BOOL bSuccess
= FALSE
;
2903 socklen_t namelen
= sizeof(lpwfs
->lstnSocketAddress
);
2907 lpwfs
->lstnSocket
= socket(PF_INET
, SOCK_STREAM
, 0);
2908 if (lpwfs
->lstnSocket
== -1)
2910 TRACE("Unable to create listening socket\n");
2914 /* We obtain our ip addr from the name of the command channel socket */
2915 lpwfs
->lstnSocketAddress
= lpwfs
->socketAddress
;
2917 /* and get the system to assign us a port */
2918 lpwfs
->lstnSocketAddress
.sin_port
= htons(0);
2920 if (bind(lpwfs
->lstnSocket
,(struct sockaddr
*) &lpwfs
->lstnSocketAddress
, sizeof(lpwfs
->lstnSocketAddress
)) == -1)
2922 TRACE("Unable to bind socket\n");
2926 if (listen(lpwfs
->lstnSocket
, MAX_BACKLOG
) == -1)
2928 TRACE("listen failed\n");
2932 if (getsockname(lpwfs
->lstnSocket
, (struct sockaddr
*) &lpwfs
->lstnSocketAddress
, &namelen
) != -1)
2936 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2938 closesocket(lpwfs
->lstnSocket
);
2939 lpwfs
->lstnSocket
= -1;
2946 /***********************************************************************
2947 * FTP_SendType (internal)
2949 * Tell server type of data being transferred
2955 * W98SE doesn't cache the type that's currently set
2956 * (i.e. it sends it always),
2957 * so we probably don't want to do that either.
2959 static BOOL
FTP_SendType(ftp_session_t
*lpwfs
, DWORD dwType
)
2962 WCHAR type
[] = { 'I','\0' };
2963 BOOL bSuccess
= FALSE
;
2966 if (dwType
& INTERNET_FLAG_TRANSFER_ASCII
)
2969 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_TYPE
, type
, 0, 0, 0))
2972 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
)/100;
2978 FTP_SetResponseError(nResCode
);
2986 #if 0 /* FIXME: should probably be used for FtpGetFileSize */
2987 /***********************************************************************
2988 * FTP_GetFileSize (internal)
2990 * Retrieves from the server the size of the given file
2997 static BOOL
FTP_GetFileSize(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, DWORD
*dwSize
)
3000 BOOL bSuccess
= FALSE
;
3004 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_SIZE
, lpszRemoteFile
, 0, 0, 0))
3007 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3010 if (nResCode
== 213) {
3011 /* Now parses the output to get the actual file size */
3013 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
3015 for (i
= 0; (lpszResponseBuffer
[i
] != ' ') && (lpszResponseBuffer
[i
] != '\0'); i
++) ;
3016 if (lpszResponseBuffer
[i
] == '\0') return FALSE
;
3017 *dwSize
= atol(&(lpszResponseBuffer
[i
+ 1]));
3021 FTP_SetResponseError(nResCode
);
3031 /***********************************************************************
3032 * FTP_SendPort (internal)
3034 * Tell server which port to use
3041 static BOOL
FTP_SendPort(ftp_session_t
*lpwfs
)
3043 static const WCHAR szIPFormat
[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
3045 WCHAR szIPAddress
[64];
3046 BOOL bSuccess
= FALSE
;
3049 sprintfW(szIPAddress
, szIPFormat
,
3050 lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x000000FF,
3051 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x0000FF00)>>8,
3052 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x00FF0000)>>16,
3053 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0xFF000000)>>24,
3054 lpwfs
->lstnSocketAddress
.sin_port
& 0xFF,
3055 (lpwfs
->lstnSocketAddress
.sin_port
& 0xFF00)>>8);
3057 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PORT
, szIPAddress
, 0, 0, 0))
3060 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3063 if (nResCode
== 200)
3066 FTP_SetResponseError(nResCode
);
3074 /***********************************************************************
3075 * FTP_DoPassive (internal)
3077 * Tell server that we want to do passive transfers
3078 * and connect data socket
3085 static BOOL
FTP_DoPassive(ftp_session_t
*lpwfs
)
3088 BOOL bSuccess
= FALSE
;
3091 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASV
, NULL
, 0, 0, 0))
3094 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3097 if (nResCode
== 227)
3099 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
3103 char *pAddr
, *pPort
;
3105 struct sockaddr_in dataSocketAddress
;
3107 p
= lpszResponseBuffer
+4; /* skip status code */
3108 while (*p
!= '\0' && (*p
< '0' || *p
> '9')) p
++;
3112 ERR("no address found in response, aborting\n");
3116 if (sscanf(p
, "%d,%d,%d,%d,%d,%d", &f
[0], &f
[1], &f
[2], &f
[3],
3119 ERR("unknown response address format '%s', aborting\n", p
);
3122 for (i
=0; i
< 6; i
++)
3125 dataSocketAddress
= lpwfs
->socketAddress
;
3126 pAddr
= (char *)&(dataSocketAddress
.sin_addr
.s_addr
);
3127 pPort
= (char *)&(dataSocketAddress
.sin_port
);
3135 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
3139 if (connect(nsocket
, (struct sockaddr
*)&dataSocketAddress
, sizeof(dataSocketAddress
)))
3141 ERR("can't connect passive FTP data port.\n");
3142 closesocket(nsocket
);
3145 lpwfs
->pasvSocket
= nsocket
;
3149 FTP_SetResponseError(nResCode
);
3157 static BOOL
FTP_SendPortOrPasv(ftp_session_t
*lpwfs
)
3159 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
3161 if (!FTP_DoPassive(lpwfs
))
3166 if (!FTP_SendPort(lpwfs
))
3173 /***********************************************************************
3174 * FTP_GetDataSocket (internal)
3176 * Either accepts an incoming data socket connection from the server
3177 * or just returns the already opened socket after a PASV command
3178 * in case of passive FTP.
3186 static BOOL
FTP_GetDataSocket(ftp_session_t
*lpwfs
, LPINT nDataSocket
)
3188 struct sockaddr_in saddr
;
3189 socklen_t addrlen
= sizeof(struct sockaddr
);
3192 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
3194 *nDataSocket
= lpwfs
->pasvSocket
;
3195 lpwfs
->pasvSocket
= -1;
3199 *nDataSocket
= accept(lpwfs
->lstnSocket
, (struct sockaddr
*) &saddr
, &addrlen
);
3200 closesocket(lpwfs
->lstnSocket
);
3201 lpwfs
->lstnSocket
= -1;
3203 return *nDataSocket
!= -1;
3207 /***********************************************************************
3208 * FTP_SendData (internal)
3210 * Send data to the server
3217 static BOOL
FTP_SendData(ftp_session_t
*lpwfs
, INT nDataSocket
, HANDLE hFile
)
3219 BY_HANDLE_FILE_INFORMATION fi
;
3220 DWORD nBytesRead
= 0;
3221 DWORD nBytesSent
= 0;
3222 DWORD nTotalSent
= 0;
3223 DWORD nBytesToSend
, nLen
;
3225 time_t s_long_time
, e_long_time
;
3230 lpszBuffer
= heap_alloc_zero(sizeof(CHAR
)*DATA_PACKET_SIZE
);
3232 /* Get the size of the file. */
3233 GetFileInformationByHandle(hFile
, &fi
);
3238 nBytesToSend
= nBytesRead
- nBytesSent
;
3240 if (nBytesToSend
<= 0)
3242 /* Read data from file. */
3244 if (!ReadFile(hFile
, lpszBuffer
, DATA_PACKET_SIZE
, &nBytesRead
, 0))
3245 ERR("Failed reading from file\n");
3248 nBytesToSend
= nBytesRead
;
3253 nLen
= DATA_PACKET_SIZE
< nBytesToSend
?
3254 DATA_PACKET_SIZE
: nBytesToSend
;
3255 nRC
= send(nDataSocket
, lpszBuffer
, nLen
, 0);
3263 /* Do some computation to display the status. */
3265 nSeconds
= e_long_time
- s_long_time
;
3266 if( nSeconds
/ 60 > 0 )
3268 TRACE( "%d bytes of %d bytes (%d%%) in %d min %d sec estimated remaining time %d sec\n",
3269 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
/ 60,
3270 nSeconds
% 60, (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
3274 TRACE( "%d bytes of %d bytes (%d%%) in %d sec estimated remaining time %d sec\n",
3275 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
,
3276 (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
3278 } while (nRC
!= -1);
3280 TRACE("file transfer complete!\n");
3282 heap_free(lpszBuffer
);
3287 /***********************************************************************
3288 * FTP_SendRetrieve (internal)
3290 * Send request to retrieve a file
3293 * Number of bytes to be received on success
3297 static BOOL
FTP_SendRetrieve(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
3303 if (!(ret
= FTP_InitListenSocket(lpwfs
)))
3306 if (!(ret
= FTP_SendType(lpwfs
, dwType
)))
3309 if (!(ret
= FTP_SendPortOrPasv(lpwfs
)))
3312 if (!(ret
= FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RETR
, lpszRemoteFile
, 0, 0, 0)))
3315 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3316 if ((nResCode
!= 125) && (nResCode
!= 150)) {
3317 /* That means that we got an error getting the file. */
3318 FTP_SetResponseError(nResCode
);
3323 if (!ret
&& lpwfs
->lstnSocket
!= -1)
3325 closesocket(lpwfs
->lstnSocket
);
3326 lpwfs
->lstnSocket
= -1;
3333 /***********************************************************************
3334 * FTP_RetrieveData (internal)
3336 * Retrieve data from server
3343 static BOOL
FTP_RetrieveFileData(ftp_session_t
*lpwfs
, INT nDataSocket
, HANDLE hFile
)
3345 DWORD nBytesWritten
;
3351 lpszBuffer
= heap_alloc_zero(sizeof(CHAR
)*DATA_PACKET_SIZE
);
3352 if (NULL
== lpszBuffer
)
3354 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
3360 nRC
= recv(nDataSocket
, lpszBuffer
, DATA_PACKET_SIZE
, 0);
3363 /* other side closed socket. */
3366 WriteFile(hFile
, lpszBuffer
, nRC
, &nBytesWritten
, NULL
);
3370 TRACE("Data transfer complete\n");
3373 heap_free(lpszBuffer
);
3377 /***********************************************************************
3378 * FTPFINDNEXT_Destroy (internal)
3380 * Deallocate session handle
3382 static void FTPFINDNEXT_Destroy(object_header_t
*hdr
)
3384 LPWININETFTPFINDNEXTW lpwfn
= (LPWININETFTPFINDNEXTW
) hdr
;
3389 WININET_Release(&lpwfn
->lpFtpSession
->hdr
);
3391 for (i
= 0; i
< lpwfn
->size
; i
++)
3393 heap_free(lpwfn
->lpafp
[i
].lpszName
);
3395 heap_free(lpwfn
->lpafp
);
3398 static DWORD
FTPFINDNEXT_FindNextFileProc(WININETFTPFINDNEXTW
*find
, LPVOID data
)
3400 WIN32_FIND_DATAW
*find_data
= data
;
3401 DWORD res
= ERROR_SUCCESS
;
3403 TRACE("index(%d) size(%d)\n", find
->index
, find
->size
);
3405 ZeroMemory(find_data
, sizeof(WIN32_FIND_DATAW
));
3407 if (find
->index
< find
->size
) {
3408 FTP_ConvertFileProp(&find
->lpafp
[find
->index
], find_data
);
3411 TRACE("Name: %s\nSize: %d\n", debugstr_w(find_data
->cFileName
), find_data
->nFileSizeLow
);
3413 res
= ERROR_NO_MORE_FILES
;
3416 if (find
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3418 INTERNET_ASYNC_RESULT iar
;
3420 iar
.dwResult
= (res
== ERROR_SUCCESS
);
3423 INTERNET_SendCallback(&find
->hdr
, find
->hdr
.dwContext
,
3424 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
3425 sizeof(INTERNET_ASYNC_RESULT
));
3431 static void FTPFINDNEXT_AsyncFindNextFileProc(WORKREQUEST
*workRequest
)
3433 struct WORKREQ_FTPFINDNEXTW
*req
= &workRequest
->u
.FtpFindNextW
;
3435 FTPFINDNEXT_FindNextFileProc((WININETFTPFINDNEXTW
*)workRequest
->hdr
, req
->lpFindFileData
);
3438 static DWORD
FTPFINDNEXT_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
3441 case INTERNET_OPTION_HANDLE_TYPE
:
3442 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3444 if (*size
< sizeof(ULONG
))
3445 return ERROR_INSUFFICIENT_BUFFER
;
3447 *size
= sizeof(DWORD
);
3448 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_FTP_FIND
;
3449 return ERROR_SUCCESS
;
3452 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
3455 static DWORD
FTPFINDNEXT_FindNextFileW(object_header_t
*hdr
, void *data
)
3457 WININETFTPFINDNEXTW
*find
= (WININETFTPFINDNEXTW
*)hdr
;
3459 if (find
->lpFtpSession
->lpAppInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3461 WORKREQUEST workRequest
;
3462 struct WORKREQ_FTPFINDNEXTW
*req
;
3464 workRequest
.asyncproc
= FTPFINDNEXT_AsyncFindNextFileProc
;
3465 workRequest
.hdr
= WININET_AddRef( &find
->hdr
);
3466 req
= &workRequest
.u
.FtpFindNextW
;
3467 req
->lpFindFileData
= data
;
3469 INTERNET_AsyncCall(&workRequest
);
3471 return ERROR_SUCCESS
;
3474 return FTPFINDNEXT_FindNextFileProc(find
, data
);
3477 static const object_vtbl_t FTPFINDNEXTVtbl
= {
3478 FTPFINDNEXT_Destroy
,
3480 FTPFINDNEXT_QueryOption
,
3487 FTPFINDNEXT_FindNextFileW
3490 /***********************************************************************
3491 * FTP_ReceiveFileList (internal)
3493 * Read file list from server
3496 * Handle to file list on success
3500 static HINTERNET
FTP_ReceiveFileList(ftp_session_t
*lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
3501 LPWIN32_FIND_DATAW lpFindFileData
, DWORD_PTR dwContext
)
3504 LPFILEPROPERTIESW lpafp
= NULL
;
3505 LPWININETFTPFINDNEXTW lpwfn
= NULL
;
3507 TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs
, nSocket
, debugstr_w(lpszSearchFile
), lpFindFileData
, dwContext
);
3509 if (FTP_ParseDirectory(lpwfs
, nSocket
, lpszSearchFile
, &lpafp
, &dwSize
))
3512 FTP_ConvertFileProp(lpafp
, lpFindFileData
);
3514 lpwfn
= alloc_object(&lpwfs
->hdr
, &FTPFINDNEXTVtbl
, sizeof(WININETFTPFINDNEXTW
));
3517 lpwfn
->hdr
.htype
= WH_HFTPFINDNEXT
;
3518 lpwfn
->hdr
.dwContext
= dwContext
;
3519 lpwfn
->index
= 1; /* Next index is 1 since we return index 0 */
3520 lpwfn
->size
= dwSize
;
3521 lpwfn
->lpafp
= lpafp
;
3523 WININET_AddRef( &lpwfs
->hdr
);
3524 lpwfn
->lpFtpSession
= lpwfs
;
3525 list_add_head( &lpwfs
->hdr
.children
, &lpwfn
->hdr
.entry
);
3529 TRACE("Matched %d files\n", dwSize
);
3530 return lpwfn
? lpwfn
->hdr
.hInternet
: NULL
;
3534 /***********************************************************************
3535 * FTP_ConvertFileProp (internal)
3537 * Converts FILEPROPERTIESW struct to WIN32_FIND_DATAA
3544 static BOOL
FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp
, LPWIN32_FIND_DATAW lpFindFileData
)
3546 BOOL bSuccess
= FALSE
;
3548 ZeroMemory(lpFindFileData
, sizeof(WIN32_FIND_DATAW
));
3552 SystemTimeToFileTime( &lpafp
->tmLastModified
, &lpFindFileData
->ftLastAccessTime
);
3553 lpFindFileData
->ftLastWriteTime
= lpFindFileData
->ftLastAccessTime
;
3554 lpFindFileData
->ftCreationTime
= lpFindFileData
->ftLastAccessTime
;
3556 /* Not all fields are filled in */
3557 lpFindFileData
->nFileSizeHigh
= 0; /* We do not handle files bigger than 0xFFFFFFFF bytes yet :-) */
3558 lpFindFileData
->nFileSizeLow
= lpafp
->nSize
;
3560 if (lpafp
->bIsDirectory
)
3561 lpFindFileData
->dwFileAttributes
|= FILE_ATTRIBUTE_DIRECTORY
;
3563 if (lpafp
->lpszName
)
3564 lstrcpynW(lpFindFileData
->cFileName
, lpafp
->lpszName
, MAX_PATH
);
3572 /***********************************************************************
3573 * FTP_ParseNextFile (internal)
3575 * Parse the next line in file listing
3581 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW lpfp
)
3583 static const char szSpace
[] = " \t";
3591 lpfp
->lpszName
= NULL
;
3593 if(!(pszLine
= INTERNET_GetNextLine(nSocket
, &nBufLen
)))
3596 pszToken
= strtok(pszLine
, szSpace
);
3598 * <Permissions> <NoLinks> <owner> <group> <size> <date> <time or year> <filename>
3601 * drwx--s--- 2 pcarrier ens 512 Sep 28 1995 pcarrier
3603 if(!isdigit(pszToken
[0]) && 10 == strlen(pszToken
)) {
3604 if(!FTP_ParsePermission(pszToken
, lpfp
))
3605 lpfp
->bIsDirectory
= FALSE
;
3606 for(i
=0; i
<=3; i
++) {
3607 if(!(pszToken
= strtok(NULL
, szSpace
)))
3610 if(!pszToken
) continue;
3611 if(lpfp
->bIsDirectory
) {
3612 TRACE("Is directory\n");
3616 TRACE("Size: %s\n", pszToken
);
3617 lpfp
->nSize
= atol(pszToken
);
3620 lpfp
->tmLastModified
.wSecond
= 0;
3621 lpfp
->tmLastModified
.wMinute
= 0;
3622 lpfp
->tmLastModified
.wHour
= 0;
3623 lpfp
->tmLastModified
.wDay
= 0;
3624 lpfp
->tmLastModified
.wMonth
= 0;
3625 lpfp
->tmLastModified
.wYear
= 0;
3627 /* Determine month */
3628 pszToken
= strtok(NULL
, szSpace
);
3629 if(!pszToken
) continue;
3630 if(strlen(pszToken
) >= 3) {
3632 if((pszTmp
= StrStrIA(szMonths
, pszToken
)))
3633 lpfp
->tmLastModified
.wMonth
= ((pszTmp
- szMonths
) / 3)+1;
3636 pszToken
= strtok(NULL
, szSpace
);
3637 if(!pszToken
) continue;
3638 lpfp
->tmLastModified
.wDay
= atoi(pszToken
);
3639 /* Determine time or year */
3640 pszToken
= strtok(NULL
, szSpace
);
3641 if(!pszToken
) continue;
3642 if((pszTmp
= strchr(pszToken
, ':'))) {
3643 SYSTEMTIME curr_time
;
3646 lpfp
->tmLastModified
.wMinute
= atoi(pszTmp
);
3647 lpfp
->tmLastModified
.wHour
= atoi(pszToken
);
3648 GetLocalTime( &curr_time
);
3649 lpfp
->tmLastModified
.wYear
= curr_time
.wYear
;
3652 lpfp
->tmLastModified
.wYear
= atoi(pszToken
);
3653 lpfp
->tmLastModified
.wHour
= 12;
3655 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3656 lpfp
->tmLastModified
.wHour
, lpfp
->tmLastModified
.wMinute
, lpfp
->tmLastModified
.wSecond
,
3657 lpfp
->tmLastModified
.wYear
, lpfp
->tmLastModified
.wMonth
, lpfp
->tmLastModified
.wDay
);
3659 pszToken
= strtok(NULL
, szSpace
);
3660 if(!pszToken
) continue;
3661 lpfp
->lpszName
= heap_strdupAtoW(pszToken
);
3662 TRACE("File: %s\n", debugstr_w(lpfp
->lpszName
));
3664 /* NT way of parsing ... :
3666 07-13-03 08:55PM <DIR> sakpatch
3667 05-09-03 06:02PM 12656686 2003-04-21bgm_cmd_e.rgz
3669 else if(isdigit(pszToken
[0]) && 8 == strlen(pszToken
)) {
3670 int mon
, mday
, year
, hour
, min
;
3671 lpfp
->permissions
= 0xFFFF; /* No idea, put full permission :-) */
3673 sscanf(pszToken
, "%d-%d-%d", &mon
, &mday
, &year
);
3674 lpfp
->tmLastModified
.wDay
= mday
;
3675 lpfp
->tmLastModified
.wMonth
= mon
;
3676 lpfp
->tmLastModified
.wYear
= year
;
3678 /* Hacky and bad Y2K protection :-) */
3679 if (lpfp
->tmLastModified
.wYear
< 70) lpfp
->tmLastModified
.wYear
+= 2000;
3681 pszToken
= strtok(NULL
, szSpace
);
3682 if(!pszToken
) continue;
3683 sscanf(pszToken
, "%d:%d", &hour
, &min
);
3684 lpfp
->tmLastModified
.wHour
= hour
;
3685 lpfp
->tmLastModified
.wMinute
= min
;
3686 if((pszToken
[5] == 'P') && (pszToken
[6] == 'M')) {
3687 lpfp
->tmLastModified
.wHour
+= 12;
3689 lpfp
->tmLastModified
.wSecond
= 0;
3691 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3692 lpfp
->tmLastModified
.wHour
, lpfp
->tmLastModified
.wMinute
, lpfp
->tmLastModified
.wSecond
,
3693 lpfp
->tmLastModified
.wYear
, lpfp
->tmLastModified
.wMonth
, lpfp
->tmLastModified
.wDay
);
3695 pszToken
= strtok(NULL
, szSpace
);
3696 if(!pszToken
) continue;
3697 if(!strcasecmp(pszToken
, "<DIR>")) {
3698 lpfp
->bIsDirectory
= TRUE
;
3700 TRACE("Is directory\n");
3703 lpfp
->bIsDirectory
= FALSE
;
3704 lpfp
->nSize
= atol(pszToken
);
3705 TRACE("Size: %d\n", lpfp
->nSize
);
3708 pszToken
= strtok(NULL
, szSpace
);
3709 if(!pszToken
) continue;
3710 lpfp
->lpszName
= heap_strdupAtoW(pszToken
);
3711 TRACE("Name: %s\n", debugstr_w(lpfp
->lpszName
));
3713 /* EPLF format - http://cr.yp.to/ftp/list/eplf.html */
3714 else if(pszToken
[0] == '+') {
3715 FIXME("EPLF Format not implemented\n");
3718 if(lpfp
->lpszName
) {
3719 if((lpszSearchFile
== NULL
) ||
3720 (PathMatchSpecW(lpfp
->lpszName
, lpszSearchFile
))) {
3722 TRACE("Matched: %s\n", debugstr_w(lpfp
->lpszName
));
3725 heap_free(lpfp
->lpszName
);
3726 lpfp
->lpszName
= NULL
;
3733 /***********************************************************************
3734 * FTP_ParseDirectory (internal)
3736 * Parse string of directory information
3742 static BOOL
FTP_ParseDirectory(ftp_session_t
*lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
3743 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
)
3745 BOOL bSuccess
= TRUE
;
3746 INT sizeFilePropArray
= 500;/*20; */
3747 INT indexFilePropArray
= -1;
3751 /* Allocate initial file properties array */
3752 *lpafp
= heap_alloc_zero(sizeof(FILEPROPERTIESW
)*(sizeFilePropArray
));
3757 if (indexFilePropArray
+1 >= sizeFilePropArray
)
3759 LPFILEPROPERTIESW tmpafp
;
3761 sizeFilePropArray
*= 2;
3762 tmpafp
= heap_realloc_zero(*lpafp
, sizeof(FILEPROPERTIESW
)*sizeFilePropArray
);
3771 indexFilePropArray
++;
3772 } while (FTP_ParseNextFile(nSocket
, lpszSearchFile
, &(*lpafp
)[indexFilePropArray
]));
3774 if (bSuccess
&& indexFilePropArray
)
3776 if (indexFilePropArray
< sizeFilePropArray
- 1)
3778 LPFILEPROPERTIESW tmpafp
;
3780 tmpafp
= heap_realloc(*lpafp
, sizeof(FILEPROPERTIESW
)*indexFilePropArray
);
3784 *dwfp
= indexFilePropArray
;
3789 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
3797 /***********************************************************************
3798 * FTP_ParsePermission (internal)
3800 * Parse permission string of directory information
3807 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
)
3809 BOOL bSuccess
= TRUE
;
3810 unsigned short nPermission
= 0;
3815 if ((*lpszPermission
!= 'd') && (*lpszPermission
!= '-') && (*lpszPermission
!= 'l'))
3821 lpfp
->bIsDirectory
= (*lpszPermission
== 'd');
3827 nPermission
|= (*(lpszPermission
+1) == 'r' ? 1 : 0) << 8;
3830 nPermission
|= (*(lpszPermission
+2) == 'w' ? 1 : 0) << 7;
3833 nPermission
|= (*(lpszPermission
+3) == 'x' ? 1 : 0) << 6;
3836 nPermission
|= (*(lpszPermission
+4) == 'r' ? 1 : 0) << 5;
3839 nPermission
|= (*(lpszPermission
+5) == 'w' ? 1 : 0) << 4;
3842 nPermission
|= (*(lpszPermission
+6) == 'x' ? 1 : 0) << 3;
3845 nPermission
|= (*(lpszPermission
+7) == 'r' ? 1 : 0) << 2;
3848 nPermission
|= (*(lpszPermission
+8) == 'w' ? 1 : 0) << 1;
3851 nPermission
|= (*(lpszPermission
+9) == 'x' ? 1 : 0);
3855 }while (nPos
<= nLast
);
3857 lpfp
->permissions
= nPermission
;
3862 /***********************************************************************
3863 * FTP_SetResponseError (internal)
3865 * Set the appropriate error code for a given response from the server
3870 static DWORD
FTP_SetResponseError(DWORD dwResponse
)
3876 case 425: /* Cannot open data connection. */
3877 dwCode
= ERROR_INTERNET_CANNOT_CONNECT
;
3880 case 426: /* Connection closed, transer aborted. */
3881 dwCode
= ERROR_INTERNET_CONNECTION_ABORTED
;
3884 case 530: /* Not logged in. Login incorrect. */
3885 dwCode
= ERROR_INTERNET_LOGIN_FAILURE
;
3888 case 421: /* Service not available - Server may be shutting down. */
3889 case 450: /* File action not taken. File may be busy. */
3890 case 451: /* Action aborted. Server error. */
3891 case 452: /* Action not taken. Insufficient storage space on server. */
3892 case 500: /* Syntax error. Command unrecognized. */
3893 case 501: /* Syntax error. Error in parameters or arguments. */
3894 case 502: /* Command not implemented. */
3895 case 503: /* Bad sequence of commands. */
3896 case 504: /* Command not implemented for that parameter. */
3897 case 532: /* Need account for storing files */
3898 case 550: /* File action not taken. File not found or no access. */
3899 case 551: /* Requested action aborted. Page type unknown */
3900 case 552: /* Action aborted. Exceeded storage allocation */
3901 case 553: /* Action not taken. File name not allowed. */
3904 dwCode
= ERROR_INTERNET_EXTENDED_ERROR
;
3908 INTERNET_SetLastError(dwCode
);