2 * WININET - Ftp implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2004 Mike McCormack for CodeWeavers
6 * Copyright 2004 Kevin Koltzau
11 * Copyright 2000 Andreas Mohr
12 * Copyright 2002 Jaco Greeff
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/port.h"
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
58 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
63 #define DATA_PACKET_SIZE 0x2000
67 /* Testing shows that Windows only accepts dwFlags where the last
68 * 3 (yes 3) bits define FTP_TRANSFER_TYPE_UNKNOWN, FTP_TRANSFER_TYPE_ASCII or FTP_TRANSFER_TYPE_BINARY.
70 #define FTP_CONDITION_MASK 0x0007
73 /* FTP commands with arguments. */
89 /* FTP commands without arguments. */
98 static const CHAR
*const szFtpCommands
[] = {
121 static const CHAR szMonths
[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
122 static const WCHAR szNoAccount
[] = {'n','o','a','c','c','o','u','n','t','\0'};
124 static void FTP_CloseFileTransferHandle(LPWININETHANDLEHEADER hdr
);
125 static void FTP_CloseSessionHandle(LPWININETHANDLEHEADER hdr
);
126 static void FTP_CloseConnection(LPWININETHANDLEHEADER hdr
);
127 static void FTP_CloseFindNextHandle(LPWININETHANDLEHEADER hdr
);
128 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
129 INTERNET_STATUS_CALLBACK lpfnStatusCB
, LPWININETHANDLEHEADER hdr
, DWORD_PTR dwContext
);
130 static BOOL
FTP_SendStore(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
);
131 static BOOL
FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs
, LPINT nDataSocket
);
132 static BOOL
FTP_SendData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
);
133 static INT
FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs
, DWORD_PTR dwContext
);
134 static BOOL
FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
);
135 static BOOL
FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
);
136 static BOOL
FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs
);
137 static BOOL
FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs
);
138 static BOOL
FTP_SendPassword(LPWININETFTPSESSIONW lpwfs
);
139 static BOOL
FTP_SendAccount(LPWININETFTPSESSIONW lpwfs
);
140 static BOOL
FTP_SendType(LPWININETFTPSESSIONW lpwfs
, DWORD dwType
);
141 static BOOL
FTP_SendPort(LPWININETFTPSESSIONW lpwfs
);
142 static BOOL
FTP_DoPassive(LPWININETFTPSESSIONW lpwfs
);
143 static BOOL
FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs
);
144 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
);
145 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW fileprop
);
146 static BOOL
FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
147 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
);
148 static HINTERNET
FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
149 LPWIN32_FIND_DATAW lpFindFileData
, DWORD_PTR dwContext
);
150 static DWORD
FTP_SetResponseError(DWORD dwResponse
);
152 /***********************************************************************
153 * FtpPutFileA (WININET.@)
155 * Uploads a file to the FTP server
162 BOOL WINAPI
FtpPutFileA(HINTERNET hConnect
, LPCSTR lpszLocalFile
,
163 LPCSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
165 LPWSTR lpwzLocalFile
;
166 LPWSTR lpwzNewRemoteFile
;
169 lpwzLocalFile
= lpszLocalFile
?WININET_strdup_AtoW(lpszLocalFile
):NULL
;
170 lpwzNewRemoteFile
= lpszNewRemoteFile
?WININET_strdup_AtoW(lpszNewRemoteFile
):NULL
;
171 ret
= FtpPutFileW(hConnect
, lpwzLocalFile
, lpwzNewRemoteFile
,
173 HeapFree(GetProcessHeap(), 0, lpwzLocalFile
);
174 HeapFree(GetProcessHeap(), 0, lpwzNewRemoteFile
);
178 static void AsyncFtpPutFileProc(WORKREQUEST
*workRequest
)
180 struct WORKREQ_FTPPUTFILEW
const *req
= &workRequest
->u
.FtpPutFileW
;
181 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
183 TRACE("%p\n", lpwfs
);
185 FTP_FtpPutFileW(lpwfs
, req
->lpszLocalFile
,
186 req
->lpszNewRemoteFile
, req
->dwFlags
, req
->dwContext
);
188 HeapFree(GetProcessHeap(), 0, req
->lpszLocalFile
);
189 HeapFree(GetProcessHeap(), 0, req
->lpszNewRemoteFile
);
192 /***********************************************************************
193 * FtpPutFileW (WININET.@)
195 * Uploads a file to the FTP server
202 BOOL WINAPI
FtpPutFileW(HINTERNET hConnect
, LPCWSTR lpszLocalFile
,
203 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
205 LPWININETFTPSESSIONW lpwfs
;
206 LPWININETAPPINFOW hIC
= NULL
;
209 if (!lpszLocalFile
|| !lpszNewRemoteFile
)
211 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
215 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
218 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
222 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
224 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
228 if (lpwfs
->download_in_progress
!= NULL
)
230 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
234 if ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
236 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
240 hIC
= lpwfs
->lpAppInfo
;
241 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
243 WORKREQUEST workRequest
;
244 struct WORKREQ_FTPPUTFILEW
*req
= &workRequest
.u
.FtpPutFileW
;
246 workRequest
.asyncproc
= AsyncFtpPutFileProc
;
247 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
248 req
->lpszLocalFile
= WININET_strdupW(lpszLocalFile
);
249 req
->lpszNewRemoteFile
= WININET_strdupW(lpszNewRemoteFile
);
250 req
->dwFlags
= dwFlags
;
251 req
->dwContext
= dwContext
;
253 r
= INTERNET_AsyncCall(&workRequest
);
257 r
= FTP_FtpPutFileW(lpwfs
, lpszLocalFile
,
258 lpszNewRemoteFile
, dwFlags
, dwContext
);
262 WININET_Release( &lpwfs
->hdr
);
267 /***********************************************************************
268 * FTP_FtpPutFileW (Internal)
270 * Uploads a file to the FTP server
277 BOOL WINAPI
FTP_FtpPutFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszLocalFile
,
278 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
281 BOOL bSuccess
= FALSE
;
282 LPWININETAPPINFOW hIC
= NULL
;
285 TRACE(" lpszLocalFile(%s) lpszNewRemoteFile(%s)\n", debugstr_w(lpszLocalFile
), debugstr_w(lpszNewRemoteFile
));
287 /* Clear any error information */
288 INTERNET_SetLastError(0);
290 /* Open file to be uploaded */
291 if (INVALID_HANDLE_VALUE
==
292 (hFile
= CreateFileW(lpszLocalFile
, GENERIC_READ
, 0, 0, OPEN_EXISTING
, 0, 0)))
293 /* Let CreateFile set the appropriate error */
296 hIC
= lpwfs
->lpAppInfo
;
298 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
300 if (FTP_SendStore(lpwfs
, lpszNewRemoteFile
, dwFlags
))
304 /* Get data socket to server */
305 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
307 FTP_SendData(lpwfs
, nDataSocket
, hFile
);
308 closesocket(nDataSocket
);
309 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
315 FTP_SetResponseError(nResCode
);
320 if (lpwfs
->lstnSocket
!= -1)
321 closesocket(lpwfs
->lstnSocket
);
323 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
325 INTERNET_ASYNC_RESULT iar
;
327 iar
.dwResult
= (DWORD
)bSuccess
;
328 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
329 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
330 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
339 /***********************************************************************
340 * FtpSetCurrentDirectoryA (WININET.@)
342 * Change the working directory on the FTP server
349 BOOL WINAPI
FtpSetCurrentDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
351 LPWSTR lpwzDirectory
;
354 lpwzDirectory
= lpszDirectory
?WININET_strdup_AtoW(lpszDirectory
):NULL
;
355 ret
= FtpSetCurrentDirectoryW(hConnect
, lpwzDirectory
);
356 HeapFree(GetProcessHeap(), 0, lpwzDirectory
);
361 static void AsyncFtpSetCurrentDirectoryProc(WORKREQUEST
*workRequest
)
363 struct WORKREQ_FTPSETCURRENTDIRECTORYW
const *req
= &workRequest
->u
.FtpSetCurrentDirectoryW
;
364 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
366 TRACE("%p\n", lpwfs
);
368 FTP_FtpSetCurrentDirectoryW(lpwfs
, req
->lpszDirectory
);
369 HeapFree(GetProcessHeap(), 0, req
->lpszDirectory
);
372 /***********************************************************************
373 * FtpSetCurrentDirectoryW (WININET.@)
375 * Change the working directory on the FTP server
382 BOOL WINAPI
FtpSetCurrentDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
384 LPWININETFTPSESSIONW lpwfs
= NULL
;
385 LPWININETAPPINFOW hIC
= NULL
;
390 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
394 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
395 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
397 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
401 if (lpwfs
->download_in_progress
!= NULL
)
403 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
407 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
409 hIC
= lpwfs
->lpAppInfo
;
410 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
412 WORKREQUEST workRequest
;
413 struct WORKREQ_FTPSETCURRENTDIRECTORYW
*req
;
415 workRequest
.asyncproc
= AsyncFtpSetCurrentDirectoryProc
;
416 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
417 req
= &workRequest
.u
.FtpSetCurrentDirectoryW
;
418 req
->lpszDirectory
= WININET_strdupW(lpszDirectory
);
420 r
= INTERNET_AsyncCall(&workRequest
);
424 r
= FTP_FtpSetCurrentDirectoryW(lpwfs
, lpszDirectory
);
429 WININET_Release( &lpwfs
->hdr
);
435 /***********************************************************************
436 * FTP_FtpSetCurrentDirectoryW (Internal)
438 * Change the working directory on the FTP server
445 BOOL WINAPI
FTP_FtpSetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
)
448 LPWININETAPPINFOW hIC
= NULL
;
449 DWORD bSuccess
= FALSE
;
451 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
453 /* Clear any error information */
454 INTERNET_SetLastError(0);
456 hIC
= lpwfs
->lpAppInfo
;
457 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_CWD
, lpszDirectory
,
458 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
461 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
468 FTP_SetResponseError(nResCode
);
472 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
474 INTERNET_ASYNC_RESULT iar
;
476 iar
.dwResult
= (DWORD
)bSuccess
;
477 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
478 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
479 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
485 /***********************************************************************
486 * FtpCreateDirectoryA (WININET.@)
488 * Create new directory on the FTP server
495 BOOL WINAPI
FtpCreateDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
497 LPWSTR lpwzDirectory
;
500 lpwzDirectory
= lpszDirectory
?WININET_strdup_AtoW(lpszDirectory
):NULL
;
501 ret
= FtpCreateDirectoryW(hConnect
, lpwzDirectory
);
502 HeapFree(GetProcessHeap(), 0, lpwzDirectory
);
507 static void AsyncFtpCreateDirectoryProc(WORKREQUEST
*workRequest
)
509 struct WORKREQ_FTPCREATEDIRECTORYW
const *req
= &workRequest
->u
.FtpCreateDirectoryW
;
510 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
512 TRACE(" %p\n", lpwfs
);
514 FTP_FtpCreateDirectoryW(lpwfs
, req
->lpszDirectory
);
515 HeapFree(GetProcessHeap(), 0, req
->lpszDirectory
);
518 /***********************************************************************
519 * FtpCreateDirectoryW (WININET.@)
521 * Create new directory on the FTP server
528 BOOL WINAPI
FtpCreateDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
530 LPWININETFTPSESSIONW lpwfs
;
531 LPWININETAPPINFOW hIC
= NULL
;
534 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
537 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
541 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
543 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
547 if (lpwfs
->download_in_progress
!= NULL
)
549 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
555 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
559 hIC
= lpwfs
->lpAppInfo
;
560 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
562 WORKREQUEST workRequest
;
563 struct WORKREQ_FTPCREATEDIRECTORYW
*req
;
565 workRequest
.asyncproc
= AsyncFtpCreateDirectoryProc
;
566 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
567 req
= &workRequest
.u
.FtpCreateDirectoryW
;
568 req
->lpszDirectory
= WININET_strdupW(lpszDirectory
);
570 r
= INTERNET_AsyncCall(&workRequest
);
574 r
= FTP_FtpCreateDirectoryW(lpwfs
, lpszDirectory
);
577 WININET_Release( &lpwfs
->hdr
);
583 /***********************************************************************
584 * FTP_FtpCreateDirectoryW (Internal)
586 * Create new directory on the FTP server
593 BOOL WINAPI
FTP_FtpCreateDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
)
596 BOOL bSuccess
= FALSE
;
597 LPWININETAPPINFOW hIC
= NULL
;
599 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
601 /* Clear any error information */
602 INTERNET_SetLastError(0);
604 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_MKD
, lpszDirectory
, 0, 0, 0))
607 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
613 FTP_SetResponseError(nResCode
);
617 hIC
= lpwfs
->lpAppInfo
;
618 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
620 INTERNET_ASYNC_RESULT iar
;
622 iar
.dwResult
= (DWORD
)bSuccess
;
623 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
624 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
625 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
631 /***********************************************************************
632 * FtpFindFirstFileA (WININET.@)
634 * Search the specified directory
637 * HINTERNET on success
641 HINTERNET WINAPI
FtpFindFirstFileA(HINTERNET hConnect
,
642 LPCSTR lpszSearchFile
, LPWIN32_FIND_DATAA lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
644 LPWSTR lpwzSearchFile
;
645 WIN32_FIND_DATAW wfd
;
646 LPWIN32_FIND_DATAW lpFindFileDataW
;
649 lpwzSearchFile
= lpszSearchFile
?WININET_strdup_AtoW(lpszSearchFile
):NULL
;
650 lpFindFileDataW
= lpFindFileData
?&wfd
:NULL
;
651 ret
= FtpFindFirstFileW(hConnect
, lpwzSearchFile
, lpFindFileDataW
, dwFlags
, dwContext
);
652 HeapFree(GetProcessHeap(), 0, lpwzSearchFile
);
655 WININET_find_data_WtoA(lpFindFileDataW
, lpFindFileData
);
661 static void AsyncFtpFindFirstFileProc(WORKREQUEST
*workRequest
)
663 struct WORKREQ_FTPFINDFIRSTFILEW
const *req
= &workRequest
->u
.FtpFindFirstFileW
;
664 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
666 TRACE("%p\n", lpwfs
);
668 FTP_FtpFindFirstFileW(lpwfs
, req
->lpszSearchFile
,
669 req
->lpFindFileData
, req
->dwFlags
, req
->dwContext
);
670 HeapFree(GetProcessHeap(), 0, req
->lpszSearchFile
);
673 /***********************************************************************
674 * FtpFindFirstFileW (WININET.@)
676 * Search the specified directory
679 * HINTERNET on success
683 HINTERNET WINAPI
FtpFindFirstFileW(HINTERNET hConnect
,
684 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
686 LPWININETFTPSESSIONW lpwfs
;
687 LPWININETAPPINFOW hIC
= NULL
;
690 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
691 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
693 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
697 if (lpwfs
->download_in_progress
!= NULL
)
699 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
703 hIC
= lpwfs
->lpAppInfo
;
704 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
706 WORKREQUEST workRequest
;
707 struct WORKREQ_FTPFINDFIRSTFILEW
*req
;
709 workRequest
.asyncproc
= AsyncFtpFindFirstFileProc
;
710 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
711 req
= &workRequest
.u
.FtpFindFirstFileW
;
712 req
->lpszSearchFile
= (lpszSearchFile
== NULL
) ? NULL
: WININET_strdupW(lpszSearchFile
);
713 req
->lpFindFileData
= lpFindFileData
;
714 req
->dwFlags
= dwFlags
;
715 req
->dwContext
= dwContext
;
717 INTERNET_AsyncCall(&workRequest
);
722 r
= FTP_FtpFindFirstFileW(lpwfs
, lpszSearchFile
, lpFindFileData
,
727 WININET_Release( &lpwfs
->hdr
);
733 /***********************************************************************
734 * FTP_FtpFindFirstFileW (Internal)
736 * Search the specified directory
739 * HINTERNET on success
743 HINTERNET WINAPI
FTP_FtpFindFirstFileW(LPWININETFTPSESSIONW lpwfs
,
744 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
747 LPWININETAPPINFOW hIC
= NULL
;
748 HINTERNET hFindNext
= NULL
;
752 /* Clear any error information */
753 INTERNET_SetLastError(0);
755 if (!FTP_InitListenSocket(lpwfs
))
758 if (!FTP_SendType(lpwfs
, INTERNET_FLAG_TRANSFER_ASCII
))
761 if (!FTP_SendPortOrPasv(lpwfs
))
764 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_LIST
, NULL
,
765 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
768 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
771 if (nResCode
== 125 || nResCode
== 150)
775 /* Get data socket to server */
776 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
778 hFindNext
= FTP_ReceiveFileList(lpwfs
, nDataSocket
, lpszSearchFile
, lpFindFileData
, dwContext
);
779 closesocket(nDataSocket
);
780 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
781 if (nResCode
!= 226 && nResCode
!= 250)
782 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
786 FTP_SetResponseError(nResCode
);
790 if (lpwfs
->lstnSocket
!= -1)
791 closesocket(lpwfs
->lstnSocket
);
793 hIC
= lpwfs
->lpAppInfo
;
794 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
796 INTERNET_ASYNC_RESULT iar
;
800 iar
.dwResult
= (DWORD
)hFindNext
;
801 iar
.dwError
= ERROR_SUCCESS
;
802 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
803 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
806 iar
.dwResult
= (DWORD
)hFindNext
;
807 iar
.dwError
= hFindNext
? ERROR_SUCCESS
: INTERNET_GetLastError();
808 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
809 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
816 /***********************************************************************
817 * FtpGetCurrentDirectoryA (WININET.@)
819 * Retrieves the current directory
826 BOOL WINAPI
FtpGetCurrentDirectoryA(HINTERNET hFtpSession
, LPSTR lpszCurrentDirectory
,
827 LPDWORD lpdwCurrentDirectory
)
833 if(lpdwCurrentDirectory
) {
834 len
= *lpdwCurrentDirectory
;
835 if(lpszCurrentDirectory
)
837 dir
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
840 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
845 ret
= FtpGetCurrentDirectoryW(hFtpSession
, lpszCurrentDirectory
?dir
:NULL
, lpdwCurrentDirectory
?&len
:NULL
);
846 if(lpdwCurrentDirectory
) {
847 *lpdwCurrentDirectory
= len
;
848 if(lpszCurrentDirectory
) {
849 WideCharToMultiByte(CP_ACP
, 0, dir
, len
, lpszCurrentDirectory
, *lpdwCurrentDirectory
, NULL
, NULL
);
850 HeapFree(GetProcessHeap(), 0, dir
);
857 static void AsyncFtpGetCurrentDirectoryProc(WORKREQUEST
*workRequest
)
859 struct WORKREQ_FTPGETCURRENTDIRECTORYW
const *req
= &workRequest
->u
.FtpGetCurrentDirectoryW
;
860 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
862 TRACE("%p\n", lpwfs
);
864 FTP_FtpGetCurrentDirectoryW(lpwfs
, req
->lpszDirectory
, req
->lpdwDirectory
);
867 /***********************************************************************
868 * FtpGetCurrentDirectoryW (WININET.@)
870 * Retrieves the current directory
877 BOOL WINAPI
FtpGetCurrentDirectoryW(HINTERNET hFtpSession
, LPWSTR lpszCurrentDirectory
,
878 LPDWORD lpdwCurrentDirectory
)
880 LPWININETFTPSESSIONW lpwfs
;
881 LPWININETAPPINFOW hIC
= NULL
;
884 TRACE("len(%d)\n", *lpdwCurrentDirectory
);
886 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
887 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
889 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
893 if (lpwfs
->download_in_progress
!= NULL
)
895 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
899 hIC
= lpwfs
->lpAppInfo
;
900 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
902 WORKREQUEST workRequest
;
903 struct WORKREQ_FTPGETCURRENTDIRECTORYW
*req
;
905 workRequest
.asyncproc
= AsyncFtpGetCurrentDirectoryProc
;
906 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
907 req
= &workRequest
.u
.FtpGetCurrentDirectoryW
;
908 req
->lpszDirectory
= lpszCurrentDirectory
;
909 req
->lpdwDirectory
= lpdwCurrentDirectory
;
911 r
= INTERNET_AsyncCall(&workRequest
);
915 r
= FTP_FtpGetCurrentDirectoryW(lpwfs
, lpszCurrentDirectory
,
916 lpdwCurrentDirectory
);
921 WININET_Release( &lpwfs
->hdr
);
927 /***********************************************************************
928 * FTP_FtpGetCurrentDirectoryW (Internal)
930 * Retrieves the current directory
937 BOOL WINAPI
FTP_FtpGetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPWSTR lpszCurrentDirectory
,
938 LPDWORD lpdwCurrentDirectory
)
941 LPWININETAPPINFOW hIC
= NULL
;
942 DWORD bSuccess
= FALSE
;
944 TRACE("len(%d)\n", *lpdwCurrentDirectory
);
946 /* Clear any error information */
947 INTERNET_SetLastError(0);
949 ZeroMemory(lpszCurrentDirectory
, *lpdwCurrentDirectory
);
951 hIC
= lpwfs
->lpAppInfo
;
952 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PWD
, NULL
,
953 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
956 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
959 if (nResCode
== 257) /* Extract directory name */
961 DWORD firstpos
, lastpos
, len
;
962 LPWSTR lpszResponseBuffer
= WININET_strdup_AtoW(INTERNET_GetResponseBuffer());
964 for (firstpos
= 0, lastpos
= 0; lpszResponseBuffer
[lastpos
]; lastpos
++)
966 if ('"' == lpszResponseBuffer
[lastpos
])
975 len
= lastpos
- firstpos
- 1;
976 lstrcpynW(lpszCurrentDirectory
, &lpszResponseBuffer
[firstpos
+1], *lpdwCurrentDirectory
);
977 HeapFree(GetProcessHeap(), 0, lpszResponseBuffer
);
978 *lpdwCurrentDirectory
= len
;
982 FTP_SetResponseError(nResCode
);
986 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
988 INTERNET_ASYNC_RESULT iar
;
990 iar
.dwResult
= (DWORD
)bSuccess
;
991 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
992 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
993 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
996 return (DWORD
) bSuccess
;
999 /***********************************************************************
1000 * FtpOpenFileA (WININET.@)
1002 * Open a remote file for writing or reading
1005 * HINTERNET handle on success
1009 HINTERNET WINAPI
FtpOpenFileA(HINTERNET hFtpSession
,
1010 LPCSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1011 DWORD_PTR dwContext
)
1013 LPWSTR lpwzFileName
;
1016 lpwzFileName
= lpszFileName
?WININET_strdup_AtoW(lpszFileName
):NULL
;
1017 ret
= FtpOpenFileW(hFtpSession
, lpwzFileName
, fdwAccess
, dwFlags
, dwContext
);
1018 HeapFree(GetProcessHeap(), 0, lpwzFileName
);
1023 static void AsyncFtpOpenFileProc(WORKREQUEST
*workRequest
)
1025 struct WORKREQ_FTPOPENFILEW
const *req
= &workRequest
->u
.FtpOpenFileW
;
1026 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1028 TRACE("%p\n", lpwfs
);
1030 FTP_FtpOpenFileW(lpwfs
, req
->lpszFilename
,
1031 req
->dwAccess
, req
->dwFlags
, req
->dwContext
);
1032 HeapFree(GetProcessHeap(), 0, req
->lpszFilename
);
1035 /***********************************************************************
1036 * FtpOpenFileW (WININET.@)
1038 * Open a remote file for writing or reading
1041 * HINTERNET handle on success
1045 HINTERNET WINAPI
FtpOpenFileW(HINTERNET hFtpSession
,
1046 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1047 DWORD_PTR dwContext
)
1049 LPWININETFTPSESSIONW lpwfs
;
1050 LPWININETAPPINFOW hIC
= NULL
;
1053 TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession
,
1054 debugstr_w(lpszFileName
), fdwAccess
, dwFlags
, dwContext
);
1056 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1059 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1063 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1065 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1069 if ((!lpszFileName
) ||
1070 ((fdwAccess
!= GENERIC_READ
) && (fdwAccess
!= GENERIC_WRITE
)) ||
1071 ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
))
1073 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1077 if (lpwfs
->download_in_progress
!= NULL
)
1079 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1083 hIC
= lpwfs
->lpAppInfo
;
1084 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1086 WORKREQUEST workRequest
;
1087 struct WORKREQ_FTPOPENFILEW
*req
;
1089 workRequest
.asyncproc
= AsyncFtpOpenFileProc
;
1090 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1091 req
= &workRequest
.u
.FtpOpenFileW
;
1092 req
->lpszFilename
= WININET_strdupW(lpszFileName
);
1093 req
->dwAccess
= fdwAccess
;
1094 req
->dwFlags
= dwFlags
;
1095 req
->dwContext
= dwContext
;
1097 INTERNET_AsyncCall(&workRequest
);
1102 r
= FTP_FtpOpenFileW(lpwfs
, lpszFileName
, fdwAccess
, dwFlags
, dwContext
);
1106 WININET_Release( &lpwfs
->hdr
);
1112 /***********************************************************************
1113 * FTP_FtpOpenFileW (Internal)
1115 * Open a remote file for writing or reading
1118 * HINTERNET handle on success
1122 HINTERNET
FTP_FtpOpenFileW(LPWININETFTPSESSIONW lpwfs
,
1123 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1124 DWORD_PTR dwContext
)
1127 BOOL bSuccess
= FALSE
;
1128 LPWININETFTPFILE lpwh
= NULL
;
1129 LPWININETAPPINFOW hIC
= NULL
;
1130 HINTERNET handle
= NULL
;
1134 /* Clear any error information */
1135 INTERNET_SetLastError(0);
1137 if (GENERIC_READ
== fdwAccess
)
1139 /* Set up socket to retrieve data */
1140 bSuccess
= FTP_SendRetrieve(lpwfs
, lpszFileName
, dwFlags
);
1142 else if (GENERIC_WRITE
== fdwAccess
)
1144 /* Set up socket to send data */
1145 bSuccess
= FTP_SendStore(lpwfs
, lpszFileName
, dwFlags
);
1148 /* Get data socket to server */
1149 if (bSuccess
&& FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1151 lpwh
= HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPFILE
));
1152 lpwh
->hdr
.htype
= WH_HFILE
;
1153 lpwh
->hdr
.dwFlags
= dwFlags
;
1154 lpwh
->hdr
.dwContext
= dwContext
;
1155 lpwh
->hdr
.dwRefCount
= 1;
1156 lpwh
->hdr
.close_connection
= NULL
;
1157 lpwh
->hdr
.destroy
= FTP_CloseFileTransferHandle
;
1158 lpwh
->hdr
.lpfnStatusCB
= lpwfs
->hdr
.lpfnStatusCB
;
1159 lpwh
->nDataSocket
= nDataSocket
;
1160 lpwh
->session_deleted
= FALSE
;
1162 WININET_AddRef( &lpwfs
->hdr
);
1163 lpwh
->lpFtpSession
= lpwfs
;
1164 list_add_head( &lpwfs
->hdr
.children
, &lpwh
->hdr
.entry
);
1166 handle
= WININET_AllocHandle( &lpwh
->hdr
);
1170 /* Indicate that a download is currently in progress */
1171 lpwfs
->download_in_progress
= lpwh
;
1174 if (lpwfs
->lstnSocket
!= -1)
1175 closesocket(lpwfs
->lstnSocket
);
1177 hIC
= lpwfs
->lpAppInfo
;
1178 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1180 INTERNET_ASYNC_RESULT iar
;
1184 iar
.dwResult
= (DWORD
)handle
;
1185 iar
.dwError
= ERROR_SUCCESS
;
1186 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
1187 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1190 iar
.dwResult
= (DWORD
)bSuccess
;
1191 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1192 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1193 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1198 WININET_Release( &lpwh
->hdr
);
1204 /***********************************************************************
1205 * FtpGetFileA (WININET.@)
1207 * Retrieve file from the FTP server
1214 BOOL WINAPI
FtpGetFileA(HINTERNET hInternet
, LPCSTR lpszRemoteFile
, LPCSTR lpszNewFile
,
1215 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1216 DWORD_PTR dwContext
)
1218 LPWSTR lpwzRemoteFile
;
1222 lpwzRemoteFile
= lpszRemoteFile
?WININET_strdup_AtoW(lpszRemoteFile
):NULL
;
1223 lpwzNewFile
= lpszNewFile
?WININET_strdup_AtoW(lpszNewFile
):NULL
;
1224 ret
= FtpGetFileW(hInternet
, lpwzRemoteFile
, lpwzNewFile
, fFailIfExists
,
1225 dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1226 HeapFree(GetProcessHeap(), 0, lpwzRemoteFile
);
1227 HeapFree(GetProcessHeap(), 0, lpwzNewFile
);
1232 static void AsyncFtpGetFileProc(WORKREQUEST
*workRequest
)
1234 struct WORKREQ_FTPGETFILEW
const *req
= &workRequest
->u
.FtpGetFileW
;
1235 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1237 TRACE("%p\n", lpwfs
);
1239 FTP_FtpGetFileW(lpwfs
, req
->lpszRemoteFile
,
1240 req
->lpszNewFile
, req
->fFailIfExists
,
1241 req
->dwLocalFlagsAttribute
, req
->dwFlags
, req
->dwContext
);
1242 HeapFree(GetProcessHeap(), 0, req
->lpszRemoteFile
);
1243 HeapFree(GetProcessHeap(), 0, req
->lpszNewFile
);
1247 /***********************************************************************
1248 * FtpGetFileW (WININET.@)
1250 * Retrieve file from the FTP server
1257 BOOL WINAPI
FtpGetFileW(HINTERNET hInternet
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1258 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1259 DWORD_PTR dwContext
)
1261 LPWININETFTPSESSIONW lpwfs
;
1262 LPWININETAPPINFOW hIC
= NULL
;
1265 if (!lpszRemoteFile
|| !lpszNewFile
)
1267 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1271 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hInternet
);
1274 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1278 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1280 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1284 if ((dwInternetFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
1286 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1290 if (lpwfs
->download_in_progress
!= NULL
)
1292 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1296 hIC
= lpwfs
->lpAppInfo
;
1297 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1299 WORKREQUEST workRequest
;
1300 struct WORKREQ_FTPGETFILEW
*req
;
1302 workRequest
.asyncproc
= AsyncFtpGetFileProc
;
1303 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1304 req
= &workRequest
.u
.FtpGetFileW
;
1305 req
->lpszRemoteFile
= WININET_strdupW(lpszRemoteFile
);
1306 req
->lpszNewFile
= WININET_strdupW(lpszNewFile
);
1307 req
->dwLocalFlagsAttribute
= dwLocalFlagsAttribute
;
1308 req
->fFailIfExists
= fFailIfExists
;
1309 req
->dwFlags
= dwInternetFlags
;
1310 req
->dwContext
= dwContext
;
1312 r
= INTERNET_AsyncCall(&workRequest
);
1316 r
= FTP_FtpGetFileW(lpwfs
, lpszRemoteFile
, lpszNewFile
,
1317 fFailIfExists
, dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1321 WININET_Release( &lpwfs
->hdr
);
1327 /***********************************************************************
1328 * FTP_FtpGetFileW (Internal)
1330 * Retrieve file from the FTP server
1337 BOOL WINAPI
FTP_FtpGetFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1338 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1339 DWORD_PTR dwContext
)
1341 BOOL bSuccess
= FALSE
;
1343 LPWININETAPPINFOW hIC
= NULL
;
1345 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile
), debugstr_w(lpszNewFile
));
1347 /* Clear any error information */
1348 INTERNET_SetLastError(0);
1350 /* Ensure we can write to lpszNewfile by opening it */
1351 hFile
= CreateFileW(lpszNewFile
, GENERIC_WRITE
, 0, 0, fFailIfExists
?
1352 CREATE_NEW
: CREATE_ALWAYS
, dwLocalFlagsAttribute
, 0);
1353 if (INVALID_HANDLE_VALUE
== hFile
)
1356 /* Set up socket to retrieve data */
1357 if (FTP_SendRetrieve(lpwfs
, lpszRemoteFile
, dwInternetFlags
))
1361 /* Get data socket to server */
1362 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1367 FTP_RetrieveFileData(lpwfs
, nDataSocket
, hFile
);
1368 closesocket(nDataSocket
);
1370 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
1373 if (nResCode
== 226)
1376 FTP_SetResponseError(nResCode
);
1381 if (lpwfs
->lstnSocket
!= -1)
1382 closesocket(lpwfs
->lstnSocket
);
1386 hIC
= lpwfs
->lpAppInfo
;
1387 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1389 INTERNET_ASYNC_RESULT iar
;
1391 iar
.dwResult
= (DWORD
)bSuccess
;
1392 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1393 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1394 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1400 /***********************************************************************
1401 * FtpGetFileSize (WININET.@)
1403 DWORD WINAPI
FtpGetFileSize( HINTERNET hFile
, LPDWORD lpdwFileSizeHigh
)
1405 FIXME("(%p, %p)\n", hFile
, lpdwFileSizeHigh
);
1407 if (lpdwFileSizeHigh
)
1408 *lpdwFileSizeHigh
= 0;
1413 /***********************************************************************
1414 * FtpDeleteFileA (WININET.@)
1416 * Delete a file on the ftp server
1423 BOOL WINAPI
FtpDeleteFileA(HINTERNET hFtpSession
, LPCSTR lpszFileName
)
1425 LPWSTR lpwzFileName
;
1428 lpwzFileName
= lpszFileName
?WININET_strdup_AtoW(lpszFileName
):NULL
;
1429 ret
= FtpDeleteFileW(hFtpSession
, lpwzFileName
);
1430 HeapFree(GetProcessHeap(), 0, lpwzFileName
);
1434 static void AsyncFtpDeleteFileProc(WORKREQUEST
*workRequest
)
1436 struct WORKREQ_FTPDELETEFILEW
const *req
= &workRequest
->u
.FtpDeleteFileW
;
1437 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1439 TRACE("%p\n", lpwfs
);
1441 FTP_FtpDeleteFileW(lpwfs
, req
->lpszFilename
);
1442 HeapFree(GetProcessHeap(), 0, req
->lpszFilename
);
1445 /***********************************************************************
1446 * FtpDeleteFileW (WININET.@)
1448 * Delete a file on the ftp server
1455 BOOL WINAPI
FtpDeleteFileW(HINTERNET hFtpSession
, LPCWSTR lpszFileName
)
1457 LPWININETFTPSESSIONW lpwfs
;
1458 LPWININETAPPINFOW hIC
= NULL
;
1461 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1464 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1468 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1470 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1474 if (lpwfs
->download_in_progress
!= NULL
)
1476 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1482 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1486 hIC
= lpwfs
->lpAppInfo
;
1487 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1489 WORKREQUEST workRequest
;
1490 struct WORKREQ_FTPDELETEFILEW
*req
;
1492 workRequest
.asyncproc
= AsyncFtpDeleteFileProc
;
1493 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1494 req
= &workRequest
.u
.FtpDeleteFileW
;
1495 req
->lpszFilename
= WININET_strdupW(lpszFileName
);
1497 r
= INTERNET_AsyncCall(&workRequest
);
1501 r
= FTP_FtpDeleteFileW(lpwfs
, lpszFileName
);
1505 WININET_Release( &lpwfs
->hdr
);
1510 /***********************************************************************
1511 * FTP_FtpDeleteFileW (Internal)
1513 * Delete a file on the ftp server
1520 BOOL
FTP_FtpDeleteFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszFileName
)
1523 BOOL bSuccess
= FALSE
;
1524 LPWININETAPPINFOW hIC
= NULL
;
1526 TRACE("%p\n", lpwfs
);
1528 /* Clear any error information */
1529 INTERNET_SetLastError(0);
1531 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_DELE
, lpszFileName
, 0, 0, 0))
1534 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1537 if (nResCode
== 250)
1540 FTP_SetResponseError(nResCode
);
1543 hIC
= lpwfs
->lpAppInfo
;
1544 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1546 INTERNET_ASYNC_RESULT iar
;
1548 iar
.dwResult
= (DWORD
)bSuccess
;
1549 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1550 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1551 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1558 /***********************************************************************
1559 * FtpRemoveDirectoryA (WININET.@)
1561 * Remove a directory on the ftp server
1568 BOOL WINAPI
FtpRemoveDirectoryA(HINTERNET hFtpSession
, LPCSTR lpszDirectory
)
1570 LPWSTR lpwzDirectory
;
1573 lpwzDirectory
= lpszDirectory
?WININET_strdup_AtoW(lpszDirectory
):NULL
;
1574 ret
= FtpRemoveDirectoryW(hFtpSession
, lpwzDirectory
);
1575 HeapFree(GetProcessHeap(), 0, lpwzDirectory
);
1579 static void AsyncFtpRemoveDirectoryProc(WORKREQUEST
*workRequest
)
1581 struct WORKREQ_FTPREMOVEDIRECTORYW
const *req
= &workRequest
->u
.FtpRemoveDirectoryW
;
1582 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1584 TRACE("%p\n", lpwfs
);
1586 FTP_FtpRemoveDirectoryW(lpwfs
, req
->lpszDirectory
);
1587 HeapFree(GetProcessHeap(), 0, req
->lpszDirectory
);
1590 /***********************************************************************
1591 * FtpRemoveDirectoryW (WININET.@)
1593 * Remove a directory on the ftp server
1600 BOOL WINAPI
FtpRemoveDirectoryW(HINTERNET hFtpSession
, LPCWSTR lpszDirectory
)
1602 LPWININETFTPSESSIONW lpwfs
;
1603 LPWININETAPPINFOW hIC
= NULL
;
1606 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1609 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1613 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1615 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1619 if (lpwfs
->download_in_progress
!= NULL
)
1621 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1627 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1631 hIC
= lpwfs
->lpAppInfo
;
1632 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1634 WORKREQUEST workRequest
;
1635 struct WORKREQ_FTPREMOVEDIRECTORYW
*req
;
1637 workRequest
.asyncproc
= AsyncFtpRemoveDirectoryProc
;
1638 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1639 req
= &workRequest
.u
.FtpRemoveDirectoryW
;
1640 req
->lpszDirectory
= WININET_strdupW(lpszDirectory
);
1642 r
= INTERNET_AsyncCall(&workRequest
);
1646 r
= FTP_FtpRemoveDirectoryW(lpwfs
, lpszDirectory
);
1650 WININET_Release( &lpwfs
->hdr
);
1655 /***********************************************************************
1656 * FTP_FtpRemoveDirectoryW (Internal)
1658 * Remove a directory on the ftp server
1665 BOOL
FTP_FtpRemoveDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
)
1668 BOOL bSuccess
= FALSE
;
1669 LPWININETAPPINFOW hIC
= NULL
;
1673 /* Clear any error information */
1674 INTERNET_SetLastError(0);
1676 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RMD
, lpszDirectory
, 0, 0, 0))
1679 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1682 if (nResCode
== 250)
1685 FTP_SetResponseError(nResCode
);
1689 hIC
= lpwfs
->lpAppInfo
;
1690 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1692 INTERNET_ASYNC_RESULT iar
;
1694 iar
.dwResult
= (DWORD
)bSuccess
;
1695 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1696 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1697 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1704 /***********************************************************************
1705 * FtpRenameFileA (WININET.@)
1707 * Rename a file on the ftp server
1714 BOOL WINAPI
FtpRenameFileA(HINTERNET hFtpSession
, LPCSTR lpszSrc
, LPCSTR lpszDest
)
1720 lpwzSrc
= lpszSrc
?WININET_strdup_AtoW(lpszSrc
):NULL
;
1721 lpwzDest
= lpszDest
?WININET_strdup_AtoW(lpszDest
):NULL
;
1722 ret
= FtpRenameFileW(hFtpSession
, lpwzSrc
, lpwzDest
);
1723 HeapFree(GetProcessHeap(), 0, lpwzSrc
);
1724 HeapFree(GetProcessHeap(), 0, lpwzDest
);
1728 static void AsyncFtpRenameFileProc(WORKREQUEST
*workRequest
)
1730 struct WORKREQ_FTPRENAMEFILEW
const *req
= &workRequest
->u
.FtpRenameFileW
;
1731 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1733 TRACE("%p\n", lpwfs
);
1735 FTP_FtpRenameFileW(lpwfs
, req
->lpszSrcFile
, req
->lpszDestFile
);
1736 HeapFree(GetProcessHeap(), 0, req
->lpszSrcFile
);
1737 HeapFree(GetProcessHeap(), 0, req
->lpszDestFile
);
1740 /***********************************************************************
1741 * FtpRenameFileW (WININET.@)
1743 * Rename a file on the ftp server
1750 BOOL WINAPI
FtpRenameFileW(HINTERNET hFtpSession
, LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
1752 LPWININETFTPSESSIONW lpwfs
;
1753 LPWININETAPPINFOW hIC
= NULL
;
1756 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1759 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1763 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1765 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1769 if (lpwfs
->download_in_progress
!= NULL
)
1771 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1775 if (!lpszSrc
|| !lpszDest
)
1777 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1781 hIC
= lpwfs
->lpAppInfo
;
1782 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1784 WORKREQUEST workRequest
;
1785 struct WORKREQ_FTPRENAMEFILEW
*req
;
1787 workRequest
.asyncproc
= AsyncFtpRenameFileProc
;
1788 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1789 req
= &workRequest
.u
.FtpRenameFileW
;
1790 req
->lpszSrcFile
= WININET_strdupW(lpszSrc
);
1791 req
->lpszDestFile
= WININET_strdupW(lpszDest
);
1793 r
= INTERNET_AsyncCall(&workRequest
);
1797 r
= FTP_FtpRenameFileW(lpwfs
, lpszSrc
, lpszDest
);
1801 WININET_Release( &lpwfs
->hdr
);
1806 /***********************************************************************
1807 * FTP_FtpRenameFileW (Internal)
1809 * Rename a file on the ftp server
1816 BOOL
FTP_FtpRenameFileW( LPWININETFTPSESSIONW lpwfs
,
1817 LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
1820 BOOL bSuccess
= FALSE
;
1821 LPWININETAPPINFOW hIC
= NULL
;
1825 /* Clear any error information */
1826 INTERNET_SetLastError(0);
1828 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNFR
, lpszSrc
, 0, 0, 0))
1831 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1832 if (nResCode
== 350)
1834 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNTO
, lpszDest
, 0, 0, 0))
1837 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1840 if (nResCode
== 250)
1843 FTP_SetResponseError(nResCode
);
1846 hIC
= lpwfs
->lpAppInfo
;
1847 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1849 INTERNET_ASYNC_RESULT iar
;
1851 iar
.dwResult
= (DWORD
)bSuccess
;
1852 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1853 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1854 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1860 /***********************************************************************
1861 * FtpCommandA (WININET.@)
1863 BOOL WINAPI
FtpCommandA( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
1864 LPCSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
1866 FIXME("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
1867 debugstr_a(lpszCommand
), dwContext
, phFtpCommand
);
1872 /***********************************************************************
1873 * FtpCommandW (WININET.@)
1875 BOOL WINAPI
FtpCommandW( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
1876 LPCWSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
1878 FIXME("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
1879 debugstr_w(lpszCommand
), dwContext
, phFtpCommand
);
1884 /***********************************************************************
1885 * FTP_Connect (internal)
1887 * Connect to a ftp server
1890 * HINTERNET a session handle on success
1895 * Windows uses 'anonymous' as the username, when given a NULL username
1896 * and a NULL password. The password is first looked up in:
1898 * HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\EmailName
1900 * If this entry is not present it uses the current username as the password.
1904 HINTERNET
FTP_Connect(LPWININETAPPINFOW hIC
, LPCWSTR lpszServerName
,
1905 INTERNET_PORT nServerPort
, LPCWSTR lpszUserName
,
1906 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
1907 DWORD dwInternalFlags
)
1909 static const WCHAR szKey
[] = {'S','o','f','t','w','a','r','e','\\',
1910 'M','i','c','r','o','s','o','f','t','\\',
1911 'W','i','n','d','o','w','s','\\',
1912 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1913 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
1914 static const WCHAR szValue
[] = {'E','m','a','i','l','N','a','m','e',0};
1915 static const WCHAR szDefaultUsername
[] = {'a','n','o','n','y','m','o','u','s','\0'};
1916 static const WCHAR szEmpty
[] = {'\0'};
1917 struct sockaddr_in socketAddr
;
1920 BOOL bSuccess
= FALSE
;
1921 LPWININETFTPSESSIONW lpwfs
= NULL
;
1922 HINTERNET handle
= NULL
;
1924 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
1925 hIC
, debugstr_w(lpszServerName
),
1926 nServerPort
, debugstr_w(lpszUserName
), debugstr_w(lpszPassword
));
1928 assert( hIC
->hdr
.htype
== WH_HINIT
);
1930 if (NULL
== lpszUserName
&& NULL
!= lpszPassword
)
1932 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1936 lpwfs
= HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPSESSIONW
));
1939 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1943 if (nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
1944 nServerPort
= INTERNET_DEFAULT_FTP_PORT
;
1946 lpwfs
->hdr
.htype
= WH_HFTPSESSION
;
1947 lpwfs
->hdr
.dwFlags
= dwFlags
;
1948 lpwfs
->hdr
.dwContext
= dwContext
;
1949 lpwfs
->hdr
.dwInternalFlags
= dwInternalFlags
;
1950 lpwfs
->hdr
.dwRefCount
= 1;
1951 lpwfs
->hdr
.close_connection
= FTP_CloseConnection
;
1952 lpwfs
->hdr
.destroy
= FTP_CloseSessionHandle
;
1953 lpwfs
->hdr
.lpfnStatusCB
= hIC
->hdr
.lpfnStatusCB
;
1954 lpwfs
->download_in_progress
= NULL
;
1956 WININET_AddRef( &hIC
->hdr
);
1957 lpwfs
->lpAppInfo
= hIC
;
1958 list_add_head( &hIC
->hdr
.children
, &lpwfs
->hdr
.entry
);
1960 handle
= WININET_AllocHandle( &lpwfs
->hdr
);
1963 ERR("Failed to alloc handle\n");
1964 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1968 if(hIC
->lpszProxy
&& hIC
->dwAccessType
== INTERNET_OPEN_TYPE_PROXY
) {
1969 if(strchrW(hIC
->lpszProxy
, ' '))
1970 FIXME("Several proxies not implemented.\n");
1971 if(hIC
->lpszProxyBypass
)
1972 FIXME("Proxy bypass is ignored.\n");
1974 if ( !lpszUserName
) {
1976 WCHAR szPassword
[MAX_PATH
];
1977 DWORD len
= sizeof(szPassword
);
1979 lpwfs
->lpszUserName
= WININET_strdupW(szDefaultUsername
);
1981 RegOpenKeyW(HKEY_CURRENT_USER
, szKey
, &key
);
1982 if (RegQueryValueExW(key
, szValue
, NULL
, NULL
, (LPBYTE
)szPassword
, &len
)) {
1983 /* Nothing in the registry, get the username and use that as the password */
1984 if (!GetUserNameW(szPassword
, &len
)) {
1985 /* Should never get here, but use an empty password as failsafe */
1986 strcpyW(szPassword
, szEmpty
);
1991 TRACE("Password used for anonymous ftp : (%s)\n", debugstr_w(szPassword
));
1992 lpwfs
->lpszPassword
= WININET_strdupW(szPassword
);
1995 lpwfs
->lpszUserName
= WININET_strdupW(lpszUserName
);
1998 lpwfs
->lpszPassword
= WININET_strdupW(lpszPassword
);
2000 lpwfs
->lpszPassword
= WININET_strdupW(szEmpty
);
2003 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2004 if (!(lpwfs
->hdr
.dwInternalFlags
& INET_OPENURL
))
2006 INTERNET_ASYNC_RESULT iar
;
2008 iar
.dwResult
= (DWORD
)handle
;
2009 iar
.dwError
= ERROR_SUCCESS
;
2011 SendAsyncCallback(&hIC
->hdr
, dwContext
,
2012 INTERNET_STATUS_HANDLE_CREATED
, &iar
,
2013 sizeof(INTERNET_ASYNC_RESULT
));
2016 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_RESOLVING_NAME
,
2017 (LPWSTR
) lpszServerName
, strlenW(lpszServerName
));
2019 if (!GetAddress(lpszServerName
, nServerPort
, &socketAddr
))
2021 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
2025 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_NAME_RESOLVED
,
2026 (LPWSTR
) lpszServerName
, strlenW(lpszServerName
));
2028 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
2031 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2035 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTING_TO_SERVER
,
2036 &socketAddr
, sizeof(struct sockaddr_in
));
2038 if (connect(nsocket
, (struct sockaddr
*)&socketAddr
, sizeof(socketAddr
)) < 0)
2040 ERR("Unable to connect (%s)\n", strerror(errno
));
2041 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2045 TRACE("Connected to server\n");
2046 lpwfs
->sndSocket
= nsocket
;
2047 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTED_TO_SERVER
,
2048 &socketAddr
, sizeof(struct sockaddr_in
));
2050 sock_namelen
= sizeof(lpwfs
->socketAddress
);
2051 getsockname(nsocket
, (struct sockaddr
*) &lpwfs
->socketAddress
, &sock_namelen
);
2053 if (FTP_ConnectToHost(lpwfs
))
2055 TRACE("Successfully logged into server\n");
2061 if (lpwfs
) WININET_Release( &lpwfs
->hdr
);
2063 if (!bSuccess
&& handle
)
2065 WININET_FreeHandle( handle
);
2069 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2071 INTERNET_ASYNC_RESULT iar
;
2073 iar
.dwResult
= bSuccess
? (DWORD_PTR
)lpwfs
: 0;
2074 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
2075 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
2076 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
2083 /***********************************************************************
2084 * FTP_ConnectToHost (internal)
2086 * Connect to a ftp server
2093 static BOOL
FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs
)
2096 BOOL bSuccess
= FALSE
;
2099 FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2101 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_USER
, lpwfs
->lpszUserName
, 0, 0, 0))
2104 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2107 /* Login successful... */
2108 if (nResCode
== 230)
2110 /* User name okay, need password... */
2111 else if (nResCode
== 331)
2112 bSuccess
= FTP_SendPassword(lpwfs
);
2113 /* Need account for login... */
2114 else if (nResCode
== 332)
2115 bSuccess
= FTP_SendAccount(lpwfs
);
2117 FTP_SetResponseError(nResCode
);
2120 TRACE("Returning %d\n", bSuccess
);
2126 /***********************************************************************
2127 * FTP_SendCommandA (internal)
2129 * Send command to server
2136 static BOOL
FTP_SendCommandA(INT nSocket
, FTP_COMMAND ftpCmd
, LPCSTR lpszParam
,
2137 INTERNET_STATUS_CALLBACK lpfnStatusCB
, LPWININETHANDLEHEADER hdr
, DWORD_PTR dwContext
)
2141 DWORD nBytesSent
= 0;
2145 TRACE("%d: (%s) %d\n", ftpCmd
, lpszParam
, nSocket
);
2149 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
2152 dwParamLen
= lpszParam
?strlen(lpszParam
)+1:0;
2153 len
= dwParamLen
+ strlen(szFtpCommands
[ftpCmd
]) + strlen(szCRLF
);
2154 if (NULL
== (buf
= HeapAlloc(GetProcessHeap(), 0, len
+1)))
2156 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2159 sprintf(buf
, "%s%s%s%s", szFtpCommands
[ftpCmd
], dwParamLen
? " " : "",
2160 dwParamLen
? lpszParam
: "", szCRLF
);
2162 TRACE("Sending (%s) len(%d)\n", buf
, len
);
2163 while((nBytesSent
< len
) && (nRC
!= -1))
2165 nRC
= send(nSocket
, buf
+nBytesSent
, len
- nBytesSent
, 0);
2169 HeapFree(GetProcessHeap(), 0, (LPVOID
)buf
);
2173 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_REQUEST_SENT
,
2174 &nBytesSent
, sizeof(DWORD
));
2177 TRACE("Sent %d bytes\n", nBytesSent
);
2181 /***********************************************************************
2182 * FTP_SendCommand (internal)
2184 * Send command to server
2191 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
2192 INTERNET_STATUS_CALLBACK lpfnStatusCB
, LPWININETHANDLEHEADER hdr
, DWORD_PTR dwContext
)
2195 LPSTR lpszParamA
= lpszParam
?WININET_strdup_WtoA(lpszParam
):NULL
;
2196 ret
= FTP_SendCommandA(nSocket
, ftpCmd
, lpszParamA
, lpfnStatusCB
, hdr
, dwContext
);
2197 HeapFree(GetProcessHeap(), 0, lpszParamA
);
2201 /***********************************************************************
2202 * FTP_ReceiveResponse (internal)
2204 * Receive response from server
2207 * Reply code on success
2211 INT
FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs
, DWORD_PTR dwContext
)
2213 LPSTR lpszResponse
= INTERNET_GetResponseBuffer();
2216 char firstprefix
[5];
2217 BOOL multiline
= FALSE
;
2218 LPWININETAPPINFOW hIC
= NULL
;
2220 TRACE("socket(%d)\n", lpwfs
->sndSocket
);
2222 hIC
= lpwfs
->lpAppInfo
;
2223 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
2227 if (!INTERNET_GetNextLine(lpwfs
->sndSocket
, &nRecv
))
2234 if(lpszResponse
[3] != '-')
2237 { /* Start of multiline repsonse. Loop until we get "nnn " */
2239 memcpy(firstprefix
, lpszResponse
, 3);
2240 firstprefix
[3] = ' ';
2241 firstprefix
[4] = '\0';
2246 if(!memcmp(firstprefix
, lpszResponse
, 4))
2254 rc
= atoi(lpszResponse
);
2256 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
2257 &nRecv
, sizeof(DWORD
));
2261 TRACE("return %d\n", rc
);
2266 /***********************************************************************
2267 * FTP_SendPassword (internal)
2269 * Send password to ftp server
2276 static BOOL
FTP_SendPassword(LPWININETFTPSESSIONW lpwfs
)
2279 BOOL bSuccess
= FALSE
;
2282 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASS
, lpwfs
->lpszPassword
, 0, 0, 0))
2285 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2288 TRACE("Received reply code %d\n", nResCode
);
2289 /* Login successful... */
2290 if (nResCode
== 230)
2292 /* Command not implemented, superfluous at the server site... */
2293 /* Need account for login... */
2294 else if (nResCode
== 332)
2295 bSuccess
= FTP_SendAccount(lpwfs
);
2297 FTP_SetResponseError(nResCode
);
2301 TRACE("Returning %d\n", bSuccess
);
2306 /***********************************************************************
2307 * FTP_SendAccount (internal)
2316 static BOOL
FTP_SendAccount(LPWININETFTPSESSIONW lpwfs
)
2319 BOOL bSuccess
= FALSE
;
2322 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_ACCT
, szNoAccount
, 0, 0, 0))
2325 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2329 FTP_SetResponseError(nResCode
);
2336 /***********************************************************************
2337 * FTP_SendStore (internal)
2339 * Send request to upload file to ftp server
2346 static BOOL
FTP_SendStore(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
2349 BOOL bSuccess
= FALSE
;
2352 if (!FTP_InitListenSocket(lpwfs
))
2355 if (!FTP_SendType(lpwfs
, dwType
))
2358 if (!FTP_SendPortOrPasv(lpwfs
))
2361 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_STOR
, lpszRemoteFile
, 0, 0, 0))
2363 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2366 if (nResCode
== 150 || nResCode
== 125)
2369 FTP_SetResponseError(nResCode
);
2373 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2375 closesocket(lpwfs
->lstnSocket
);
2376 lpwfs
->lstnSocket
= -1;
2383 /***********************************************************************
2384 * FTP_InitListenSocket (internal)
2386 * Create a socket to listen for server response
2393 static BOOL
FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs
)
2395 BOOL bSuccess
= FALSE
;
2396 socklen_t namelen
= sizeof(struct sockaddr_in
);
2400 lpwfs
->lstnSocket
= socket(PF_INET
, SOCK_STREAM
, 0);
2401 if (lpwfs
->lstnSocket
== -1)
2403 TRACE("Unable to create listening socket\n");
2407 /* We obtain our ip addr from the name of the command channel socket */
2408 lpwfs
->lstnSocketAddress
= lpwfs
->socketAddress
;
2410 /* and get the system to assign us a port */
2411 lpwfs
->lstnSocketAddress
.sin_port
= htons((u_short
) 0);
2413 if (bind(lpwfs
->lstnSocket
,(struct sockaddr
*) &lpwfs
->lstnSocketAddress
, sizeof(struct sockaddr_in
)) == -1)
2415 TRACE("Unable to bind socket\n");
2419 if (listen(lpwfs
->lstnSocket
, MAX_BACKLOG
) == -1)
2421 TRACE("listen failed\n");
2425 if (getsockname(lpwfs
->lstnSocket
, (struct sockaddr
*) &lpwfs
->lstnSocketAddress
, &namelen
) != -1)
2429 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2431 closesocket(lpwfs
->lstnSocket
);
2432 lpwfs
->lstnSocket
= -1;
2439 /***********************************************************************
2440 * FTP_SendType (internal)
2442 * Tell server type of data being transferred
2448 * W98SE doesn't cache the type that's currently set
2449 * (i.e. it sends it always),
2450 * so we probably don't want to do that either.
2452 static BOOL
FTP_SendType(LPWININETFTPSESSIONW lpwfs
, DWORD dwType
)
2455 WCHAR type
[] = { 'I','\0' };
2456 BOOL bSuccess
= FALSE
;
2459 if (dwType
& INTERNET_FLAG_TRANSFER_ASCII
)
2462 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_TYPE
, type
, 0, 0, 0))
2465 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
)/100;
2471 FTP_SetResponseError(nResCode
);
2479 #if 0 /* FIXME: should probably be used for FtpGetFileSize */
2480 /***********************************************************************
2481 * FTP_GetFileSize (internal)
2483 * Retrieves from the server the size of the given file
2490 static BOOL
FTP_GetFileSize(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD
*dwSize
)
2493 BOOL bSuccess
= FALSE
;
2497 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_SIZE
, lpszRemoteFile
, 0, 0, 0))
2500 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2503 if (nResCode
== 213) {
2504 /* Now parses the output to get the actual file size */
2506 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
2508 for (i
= 0; (lpszResponseBuffer
[i
] != ' ') && (lpszResponseBuffer
[i
] != '\0'); i
++) ;
2509 if (lpszResponseBuffer
[i
] == '\0') return FALSE
;
2510 *dwSize
= atol(&(lpszResponseBuffer
[i
+ 1]));
2514 FTP_SetResponseError(nResCode
);
2524 /***********************************************************************
2525 * FTP_SendPort (internal)
2527 * Tell server which port to use
2534 static BOOL
FTP_SendPort(LPWININETFTPSESSIONW lpwfs
)
2536 static const WCHAR szIPFormat
[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
2538 WCHAR szIPAddress
[64];
2539 BOOL bSuccess
= FALSE
;
2542 sprintfW(szIPAddress
, szIPFormat
,
2543 lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x000000FF,
2544 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x0000FF00)>>8,
2545 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x00FF0000)>>16,
2546 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0xFF000000)>>24,
2547 lpwfs
->lstnSocketAddress
.sin_port
& 0xFF,
2548 (lpwfs
->lstnSocketAddress
.sin_port
& 0xFF00)>>8);
2550 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PORT
, szIPAddress
, 0, 0, 0))
2553 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2556 if (nResCode
== 200)
2559 FTP_SetResponseError(nResCode
);
2567 /***********************************************************************
2568 * FTP_DoPassive (internal)
2570 * Tell server that we want to do passive transfers
2571 * and connect data socket
2578 static BOOL
FTP_DoPassive(LPWININETFTPSESSIONW lpwfs
)
2581 BOOL bSuccess
= FALSE
;
2584 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASV
, NULL
, 0, 0, 0))
2587 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2590 if (nResCode
== 227)
2592 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
2596 char *pAddr
, *pPort
;
2598 struct sockaddr_in dataSocketAddress
;
2600 p
= lpszResponseBuffer
+4; /* skip status code */
2601 while (*p
!= '\0' && (*p
< '0' || *p
> '9')) p
++;
2605 ERR("no address found in response, aborting\n");
2609 if (sscanf(p
, "%d,%d,%d,%d,%d,%d", &f
[0], &f
[1], &f
[2], &f
[3],
2612 ERR("unknown response address format '%s', aborting\n", p
);
2615 for (i
=0; i
< 6; i
++)
2618 dataSocketAddress
= lpwfs
->socketAddress
;
2619 pAddr
= (char *)&(dataSocketAddress
.sin_addr
.s_addr
);
2620 pPort
= (char *)&(dataSocketAddress
.sin_port
);
2628 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
2632 if (connect(nsocket
, (struct sockaddr
*)&dataSocketAddress
, sizeof(dataSocketAddress
)))
2634 ERR("can't connect passive FTP data port.\n");
2635 closesocket(nsocket
);
2638 lpwfs
->pasvSocket
= nsocket
;
2642 FTP_SetResponseError(nResCode
);
2650 static BOOL
FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs
)
2652 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
2654 if (!FTP_DoPassive(lpwfs
))
2659 if (!FTP_SendPort(lpwfs
))
2666 /***********************************************************************
2667 * FTP_GetDataSocket (internal)
2669 * Either accepts an incoming data socket connection from the server
2670 * or just returns the already opened socket after a PASV command
2671 * in case of passive FTP.
2679 static BOOL
FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs
, LPINT nDataSocket
)
2681 struct sockaddr_in saddr
;
2682 socklen_t addrlen
= sizeof(struct sockaddr
);
2685 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
2687 *nDataSocket
= lpwfs
->pasvSocket
;
2691 *nDataSocket
= accept(lpwfs
->lstnSocket
, (struct sockaddr
*) &saddr
, &addrlen
);
2692 closesocket(lpwfs
->lstnSocket
);
2693 lpwfs
->lstnSocket
= -1;
2695 return *nDataSocket
!= -1;
2699 /***********************************************************************
2700 * FTP_SendData (internal)
2702 * Send data to the server
2709 static BOOL
FTP_SendData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
)
2711 BY_HANDLE_FILE_INFORMATION fi
;
2712 DWORD nBytesRead
= 0;
2713 DWORD nBytesSent
= 0;
2714 DWORD nTotalSent
= 0;
2715 DWORD nBytesToSend
, nLen
;
2717 time_t s_long_time
, e_long_time
;
2722 lpszBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(CHAR
)*DATA_PACKET_SIZE
);
2724 /* Get the size of the file. */
2725 GetFileInformationByHandle(hFile
, &fi
);
2730 nBytesToSend
= nBytesRead
- nBytesSent
;
2732 if (nBytesToSend
<= 0)
2734 /* Read data from file. */
2736 if (!ReadFile(hFile
, lpszBuffer
, DATA_PACKET_SIZE
, &nBytesRead
, 0))
2737 ERR("Failed reading from file\n");
2740 nBytesToSend
= nBytesRead
;
2745 nLen
= DATA_PACKET_SIZE
< nBytesToSend
?
2746 DATA_PACKET_SIZE
: nBytesToSend
;
2747 nRC
= send(nDataSocket
, lpszBuffer
, nLen
, 0);
2755 /* Do some computation to display the status. */
2757 nSeconds
= e_long_time
- s_long_time
;
2758 if( nSeconds
/ 60 > 0 )
2760 TRACE( "%d bytes of %d bytes (%d%%) in %d min %d sec estimated remaining time %d sec\n",
2761 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
/ 60,
2762 nSeconds
% 60, (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
2766 TRACE( "%d bytes of %d bytes (%d%%) in %d sec estimated remaining time %d sec\n",
2767 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
,
2768 (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
2770 } while (nRC
!= -1);
2772 TRACE("file transfer complete!\n");
2774 HeapFree(GetProcessHeap(), 0, lpszBuffer
);
2780 /***********************************************************************
2781 * FTP_SendRetrieve (internal)
2783 * Send request to retrieve a file
2786 * Number of bytes to be received on success
2790 static BOOL
FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
2796 if (!(ret
= FTP_InitListenSocket(lpwfs
)))
2799 if (!(ret
= FTP_SendType(lpwfs
, dwType
)))
2802 if (!(ret
= FTP_SendPortOrPasv(lpwfs
)))
2805 if (!(ret
= FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RETR
, lpszRemoteFile
, 0, 0, 0)))
2808 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2809 if ((nResCode
!= 125) && (nResCode
!= 150)) {
2810 /* That means that we got an error getting the file. */
2811 FTP_SetResponseError(nResCode
);
2816 if (!ret
&& lpwfs
->lstnSocket
!= -1)
2818 closesocket(lpwfs
->lstnSocket
);
2819 lpwfs
->lstnSocket
= -1;
2826 /***********************************************************************
2827 * FTP_RetrieveData (internal)
2829 * Retrieve data from server
2836 static BOOL
FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
)
2838 DWORD nBytesWritten
;
2839 DWORD nBytesReceived
= 0;
2845 lpszBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(CHAR
)*DATA_PACKET_SIZE
);
2846 if (NULL
== lpszBuffer
)
2848 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2854 nRC
= recv(nDataSocket
, lpszBuffer
, DATA_PACKET_SIZE
, 0);
2857 /* other side closed socket. */
2860 WriteFile(hFile
, lpszBuffer
, nRC
, &nBytesWritten
, NULL
);
2861 nBytesReceived
+= nRC
;
2865 TRACE("Data transfer complete\n");
2868 HeapFree(GetProcessHeap(), 0, lpszBuffer
);
2873 /***********************************************************************
2874 * FTP_CloseConnection (internal)
2878 static void FTP_CloseConnection(LPWININETHANDLEHEADER hdr
)
2880 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) hdr
;
2884 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
2885 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
2887 if (lpwfs
->download_in_progress
!= NULL
)
2888 lpwfs
->download_in_progress
->session_deleted
= TRUE
;
2890 if (lpwfs
->sndSocket
!= -1)
2891 closesocket(lpwfs
->sndSocket
);
2893 if (lpwfs
->lstnSocket
!= -1)
2894 closesocket(lpwfs
->lstnSocket
);
2896 if (lpwfs
->pasvSocket
!= -1)
2897 closesocket(lpwfs
->pasvSocket
);
2899 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
2900 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
2904 /***********************************************************************
2905 * FTP_CloseSessionHandle (internal)
2907 * Deallocate session handle
2909 static void FTP_CloseSessionHandle(LPWININETHANDLEHEADER hdr
)
2911 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) hdr
;
2915 WININET_Release(&lpwfs
->lpAppInfo
->hdr
);
2917 HeapFree(GetProcessHeap(), 0, lpwfs
->lpszPassword
);
2918 HeapFree(GetProcessHeap(), 0, lpwfs
->lpszUserName
);
2919 HeapFree(GetProcessHeap(), 0, lpwfs
);
2923 /***********************************************************************
2924 * FTP_FindNextFileW (Internal)
2926 * Continues a file search from a previous call to FindFirstFile
2933 BOOL WINAPI
FTP_FindNextFileW(LPWININETFTPFINDNEXTW lpwh
, LPVOID lpvFindData
)
2935 BOOL bSuccess
= TRUE
;
2936 LPWIN32_FIND_DATAW lpFindFileData
;
2938 TRACE("index(%d) size(%d)\n", lpwh
->index
, lpwh
->size
);
2940 assert (lpwh
->hdr
.htype
== WH_HFTPFINDNEXT
);
2942 /* Clear any error information */
2943 INTERNET_SetLastError(0);
2945 lpFindFileData
= (LPWIN32_FIND_DATAW
) lpvFindData
;
2946 ZeroMemory(lpFindFileData
, sizeof(WIN32_FIND_DATAA
));
2948 if (lpwh
->index
>= lpwh
->size
)
2950 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
2955 FTP_ConvertFileProp(&lpwh
->lpafp
[lpwh
->index
], lpFindFileData
);
2958 TRACE("\nName: %s\nSize: %d\n", debugstr_w(lpFindFileData
->cFileName
), lpFindFileData
->nFileSizeLow
);
2962 if (lpwh
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2964 INTERNET_ASYNC_RESULT iar
;
2966 iar
.dwResult
= (DWORD
)bSuccess
;
2967 iar
.dwError
= iar
.dwError
= bSuccess
? ERROR_SUCCESS
:
2968 INTERNET_GetLastError();
2970 INTERNET_SendCallback(&lpwh
->hdr
, lpwh
->hdr
.dwContext
,
2971 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
2972 sizeof(INTERNET_ASYNC_RESULT
));
2979 /***********************************************************************
2980 * FTP_CloseFindNextHandle (internal)
2982 * Deallocate session handle
2989 static void FTP_CloseFindNextHandle(LPWININETHANDLEHEADER hdr
)
2991 LPWININETFTPFINDNEXTW lpwfn
= (LPWININETFTPFINDNEXTW
) hdr
;
2996 WININET_Release(&lpwfn
->lpFtpSession
->hdr
);
2998 for (i
= 0; i
< lpwfn
->size
; i
++)
3000 HeapFree(GetProcessHeap(), 0, lpwfn
->lpafp
[i
].lpszName
);
3003 HeapFree(GetProcessHeap(), 0, lpwfn
->lpafp
);
3004 HeapFree(GetProcessHeap(), 0, lpwfn
);
3007 /***********************************************************************
3008 * FTP_CloseFileTransferHandle (internal)
3010 * Closes the file transfer handle. This also 'cleans' the data queue of
3011 * the 'transfer complete' message (this is a bit of a hack though :-/ )
3014 static void FTP_CloseFileTransferHandle(LPWININETHANDLEHEADER hdr
)
3016 LPWININETFTPFILE lpwh
= (LPWININETFTPFILE
) hdr
;
3017 LPWININETFTPSESSIONW lpwfs
= lpwh
->lpFtpSession
;
3022 WININET_Release(&lpwh
->lpFtpSession
->hdr
);
3024 if (!lpwh
->session_deleted
)
3025 lpwfs
->download_in_progress
= NULL
;
3027 if (lpwh
->nDataSocket
!= -1)
3028 closesocket(lpwh
->nDataSocket
);
3030 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3031 if (nResCode
> 0 && nResCode
!= 226) WARN("server reports failed transfer\n");
3033 HeapFree(GetProcessHeap(), 0, lpwh
);
3036 /***********************************************************************
3037 * FTP_ReceiveFileList (internal)
3039 * Read file list from server
3042 * Handle to file list on success
3046 static HINTERNET
FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
3047 LPWIN32_FIND_DATAW lpFindFileData
, DWORD_PTR dwContext
)
3050 LPFILEPROPERTIESW lpafp
= NULL
;
3051 LPWININETFTPFINDNEXTW lpwfn
= NULL
;
3052 HINTERNET handle
= 0;
3054 TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs
, nSocket
, debugstr_w(lpszSearchFile
), lpFindFileData
, dwContext
);
3056 if (FTP_ParseDirectory(lpwfs
, nSocket
, lpszSearchFile
, &lpafp
, &dwSize
))
3059 FTP_ConvertFileProp(lpafp
, lpFindFileData
);
3061 lpwfn
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETFTPFINDNEXTW
));
3064 lpwfn
->hdr
.htype
= WH_HFTPFINDNEXT
;
3065 lpwfn
->hdr
.dwContext
= dwContext
;
3066 lpwfn
->hdr
.dwRefCount
= 1;
3067 lpwfn
->hdr
.close_connection
= NULL
;
3068 lpwfn
->hdr
.destroy
= FTP_CloseFindNextHandle
;
3069 lpwfn
->hdr
.lpfnStatusCB
= lpwfs
->hdr
.lpfnStatusCB
;
3070 lpwfn
->index
= 1; /* Next index is 1 since we return index 0 */
3071 lpwfn
->size
= dwSize
;
3072 lpwfn
->lpafp
= lpafp
;
3074 WININET_AddRef( &lpwfs
->hdr
);
3075 lpwfn
->lpFtpSession
= lpwfs
;
3076 list_add_head( &lpwfs
->hdr
.children
, &lpwfn
->hdr
.entry
);
3078 handle
= WININET_AllocHandle( &lpwfn
->hdr
);
3083 WININET_Release( &lpwfn
->hdr
);
3085 TRACE("Matched %d files\n", dwSize
);
3090 /***********************************************************************
3091 * FTP_ConvertFileProp (internal)
3093 * Converts FILEPROPERTIESW struct to WIN32_FIND_DATAA
3100 BOOL
FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp
, LPWIN32_FIND_DATAW lpFindFileData
)
3102 BOOL bSuccess
= FALSE
;
3104 ZeroMemory(lpFindFileData
, sizeof(WIN32_FIND_DATAW
));
3108 /* Convert 'Unix' time to Windows time */
3109 RtlSecondsSince1970ToTime(mktime(&lpafp
->tmLastModified
),
3110 (LARGE_INTEGER
*) &(lpFindFileData
->ftLastAccessTime
));
3111 lpFindFileData
->ftLastWriteTime
= lpFindFileData
->ftLastAccessTime
;
3112 lpFindFileData
->ftCreationTime
= lpFindFileData
->ftLastAccessTime
;
3114 /* Not all fields are filled in */
3115 lpFindFileData
->nFileSizeHigh
= 0; /* We do not handle files bigger than 0xFFFFFFFF bytes yet :-) */
3116 lpFindFileData
->nFileSizeLow
= lpafp
->nSize
;
3118 if (lpafp
->bIsDirectory
)
3119 lpFindFileData
->dwFileAttributes
|= FILE_ATTRIBUTE_DIRECTORY
;
3121 if (lpafp
->lpszName
)
3122 lstrcpynW(lpFindFileData
->cFileName
, lpafp
->lpszName
, MAX_PATH
);
3130 /***********************************************************************
3131 * FTP_ParseNextFile (internal)
3133 * Parse the next line in file listing
3139 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW lpfp
)
3141 static const char szSpace
[] = " \t";
3149 lpfp
->lpszName
= NULL
;
3151 if(!(pszLine
= INTERNET_GetNextLine(nSocket
, &nBufLen
)))
3154 pszToken
= strtok(pszLine
, szSpace
);
3156 * <Permissions> <NoLinks> <owner> <group> <size> <date> <time or year> <filename>
3159 * drwx--s--- 2 pcarrier ens 512 Sep 28 1995 pcarrier
3161 if(!isdigit(pszToken
[0]) && 10 == strlen(pszToken
)) {
3162 if(!FTP_ParsePermission(pszToken
, lpfp
))
3163 lpfp
->bIsDirectory
= FALSE
;
3164 for(i
=0; i
<=3; i
++) {
3165 if(!(pszToken
= strtok(NULL
, szSpace
)))
3168 if(!pszToken
) continue;
3169 if(lpfp
->bIsDirectory
) {
3170 TRACE("Is directory\n");
3174 TRACE("Size: %s\n", pszToken
);
3175 lpfp
->nSize
= atol(pszToken
);
3178 lpfp
->tmLastModified
.tm_sec
= 0;
3179 lpfp
->tmLastModified
.tm_min
= 0;
3180 lpfp
->tmLastModified
.tm_hour
= 0;
3181 lpfp
->tmLastModified
.tm_mday
= 0;
3182 lpfp
->tmLastModified
.tm_mon
= 0;
3183 lpfp
->tmLastModified
.tm_year
= 0;
3185 /* Determine month */
3186 pszToken
= strtok(NULL
, szSpace
);
3187 if(!pszToken
) continue;
3188 if(strlen(pszToken
) >= 3) {
3190 if((pszTmp
= StrStrIA(szMonths
, pszToken
)))
3191 lpfp
->tmLastModified
.tm_mon
= ((pszTmp
- szMonths
) / 3)+1;
3194 pszToken
= strtok(NULL
, szSpace
);
3195 if(!pszToken
) continue;
3196 lpfp
->tmLastModified
.tm_mday
= atoi(pszToken
);
3197 /* Determine time or year */
3198 pszToken
= strtok(NULL
, szSpace
);
3199 if(!pszToken
) continue;
3200 if((pszTmp
= strchr(pszToken
, ':'))) {
3205 lpfp
->tmLastModified
.tm_min
= atoi(pszTmp
);
3206 lpfp
->tmLastModified
.tm_hour
= atoi(pszToken
);
3208 apTM
= localtime(&aTime
);
3209 lpfp
->tmLastModified
.tm_year
= apTM
->tm_year
;
3212 lpfp
->tmLastModified
.tm_year
= atoi(pszToken
) - 1900;
3213 lpfp
->tmLastModified
.tm_hour
= 12;
3215 TRACE("Mod time: %02d:%02d:%02d %02d/%02d/%02d\n",
3216 lpfp
->tmLastModified
.tm_hour
, lpfp
->tmLastModified
.tm_min
, lpfp
->tmLastModified
.tm_sec
,
3217 (lpfp
->tmLastModified
.tm_year
>= 100) ? lpfp
->tmLastModified
.tm_year
- 100 : lpfp
->tmLastModified
.tm_year
,
3218 lpfp
->tmLastModified
.tm_mon
, lpfp
->tmLastModified
.tm_mday
);
3220 pszToken
= strtok(NULL
, szSpace
);
3221 if(!pszToken
) continue;
3222 lpfp
->lpszName
= WININET_strdup_AtoW(pszToken
);
3223 TRACE("File: %s\n", debugstr_w(lpfp
->lpszName
));
3225 /* NT way of parsing ... :
3227 07-13-03 08:55PM <DIR> sakpatch
3228 05-09-03 06:02PM 12656686 2003-04-21bgm_cmd_e.rgz
3230 else if(isdigit(pszToken
[0]) && 8 == strlen(pszToken
)) {
3231 lpfp
->permissions
= 0xFFFF; /* No idea, put full permission :-) */
3233 sscanf(pszToken
, "%d-%d-%d",
3234 &lpfp
->tmLastModified
.tm_mon
,
3235 &lpfp
->tmLastModified
.tm_mday
,
3236 &lpfp
->tmLastModified
.tm_year
);
3238 /* Hacky and bad Y2K protection :-) */
3239 if (lpfp
->tmLastModified
.tm_year
< 70)
3240 lpfp
->tmLastModified
.tm_year
+= 100;
3242 pszToken
= strtok(NULL
, szSpace
);
3243 if(!pszToken
) continue;
3244 sscanf(pszToken
, "%d:%d",
3245 &lpfp
->tmLastModified
.tm_hour
,
3246 &lpfp
->tmLastModified
.tm_min
);
3247 if((pszToken
[5] == 'P') && (pszToken
[6] == 'M')) {
3248 lpfp
->tmLastModified
.tm_hour
+= 12;
3250 lpfp
->tmLastModified
.tm_sec
= 0;
3252 TRACE("Mod time: %02d:%02d:%02d %02d/%02d/%02d\n",
3253 lpfp
->tmLastModified
.tm_hour
, lpfp
->tmLastModified
.tm_min
, lpfp
->tmLastModified
.tm_sec
,
3254 (lpfp
->tmLastModified
.tm_year
>= 100) ? lpfp
->tmLastModified
.tm_year
- 100 : lpfp
->tmLastModified
.tm_year
,
3255 lpfp
->tmLastModified
.tm_mon
, lpfp
->tmLastModified
.tm_mday
);
3257 pszToken
= strtok(NULL
, szSpace
);
3258 if(!pszToken
) continue;
3259 if(!strcasecmp(pszToken
, "<DIR>")) {
3260 lpfp
->bIsDirectory
= TRUE
;
3262 TRACE("Is directory\n");
3265 lpfp
->bIsDirectory
= FALSE
;
3266 lpfp
->nSize
= atol(pszToken
);
3267 TRACE("Size: %d\n", lpfp
->nSize
);
3270 pszToken
= strtok(NULL
, szSpace
);
3271 if(!pszToken
) continue;
3272 lpfp
->lpszName
= WININET_strdup_AtoW(pszToken
);
3273 TRACE("Name: %s\n", debugstr_w(lpfp
->lpszName
));
3275 /* EPLF format - http://cr.yp.to/ftp/list/eplf.html */
3276 else if(pszToken
[0] == '+') {
3277 FIXME("EPLF Format not implemented\n");
3280 if(lpfp
->lpszName
) {
3281 if((lpszSearchFile
== NULL
) ||
3282 (PathMatchSpecW(lpfp
->lpszName
, lpszSearchFile
))) {
3284 TRACE("Matched: %s\n", debugstr_w(lpfp
->lpszName
));
3287 HeapFree(GetProcessHeap(), 0, lpfp
->lpszName
);
3288 lpfp
->lpszName
= NULL
;
3295 /***********************************************************************
3296 * FTP_ParseDirectory (internal)
3298 * Parse string of directory information
3304 static BOOL
FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
3305 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
)
3307 BOOL bSuccess
= TRUE
;
3308 INT sizeFilePropArray
= 500;/*20; */
3309 INT indexFilePropArray
= -1;
3313 /* Allocate intial file properties array */
3314 *lpafp
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(FILEPROPERTIESW
)*(sizeFilePropArray
));
3319 if (indexFilePropArray
+1 >= sizeFilePropArray
)
3321 LPFILEPROPERTIESW tmpafp
;
3323 sizeFilePropArray
*= 2;
3324 tmpafp
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *lpafp
,
3325 sizeof(FILEPROPERTIESW
)*sizeFilePropArray
);
3334 indexFilePropArray
++;
3335 } while (FTP_ParseNextFile(nSocket
, lpszSearchFile
, &(*lpafp
)[indexFilePropArray
]));
3337 if (bSuccess
&& indexFilePropArray
)
3339 if (indexFilePropArray
< sizeFilePropArray
- 1)
3341 LPFILEPROPERTIESW tmpafp
;
3343 tmpafp
= HeapReAlloc(GetProcessHeap(), 0, *lpafp
,
3344 sizeof(FILEPROPERTIESW
)*indexFilePropArray
);
3348 *dwfp
= indexFilePropArray
;
3352 HeapFree(GetProcessHeap(), 0, *lpafp
);
3353 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
3361 /***********************************************************************
3362 * FTP_ParsePermission (internal)
3364 * Parse permission string of directory information
3371 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
)
3373 BOOL bSuccess
= TRUE
;
3374 unsigned short nPermission
= 0;
3379 if ((*lpszPermission
!= 'd') && (*lpszPermission
!= '-') && (*lpszPermission
!= 'l'))
3385 lpfp
->bIsDirectory
= (*lpszPermission
== 'd');
3391 nPermission
|= (*(lpszPermission
+1) == 'r' ? 1 : 0) << 8;
3394 nPermission
|= (*(lpszPermission
+2) == 'w' ? 1 : 0) << 7;
3397 nPermission
|= (*(lpszPermission
+3) == 'x' ? 1 : 0) << 6;
3400 nPermission
|= (*(lpszPermission
+4) == 'r' ? 1 : 0) << 5;
3403 nPermission
|= (*(lpszPermission
+5) == 'w' ? 1 : 0) << 4;
3406 nPermission
|= (*(lpszPermission
+6) == 'x' ? 1 : 0) << 3;
3409 nPermission
|= (*(lpszPermission
+7) == 'r' ? 1 : 0) << 2;
3412 nPermission
|= (*(lpszPermission
+8) == 'w' ? 1 : 0) << 1;
3415 nPermission
|= (*(lpszPermission
+9) == 'x' ? 1 : 0);
3419 }while (nPos
<= nLast
);
3421 lpfp
->permissions
= nPermission
;
3426 /***********************************************************************
3427 * FTP_SetResponseError (internal)
3429 * Set the appropriate error code for a given response from the server
3434 static DWORD
FTP_SetResponseError(DWORD dwResponse
)
3440 case 425: /* Cannot open data connection. */
3441 dwCode
= ERROR_INTERNET_CANNOT_CONNECT
;
3444 case 426: /* Connection closed, transer aborted. */
3445 dwCode
= ERROR_INTERNET_CONNECTION_ABORTED
;
3448 case 530: /* Not logged in. Login incorrect. */
3449 dwCode
= ERROR_INTERNET_LOGIN_FAILURE
;
3452 case 421: /* Service not available - Server may be shutting down. */
3453 case 450: /* File action not taken. File may be busy. */
3454 case 451: /* Action aborted. Server error. */
3455 case 452: /* Action not taken. Insufficient storage space on server. */
3456 case 500: /* Syntax error. Command unrecognized. */
3457 case 501: /* Syntax error. Error in parameters or arguments. */
3458 case 502: /* Command not implemented. */
3459 case 503: /* Bad sequence of commands. */
3460 case 504: /* Command not implemented for that parameter. */
3461 case 532: /* Need account for storing files */
3462 case 550: /* File action not taken. File not found or no access. */
3463 case 551: /* Requested action aborted. Page type unknown */
3464 case 552: /* Action aborted. Exceeded storage allocation */
3465 case 553: /* Action not taken. File name not allowed. */
3468 dwCode
= ERROR_INTERNET_EXTENDED_ERROR
;
3472 INTERNET_SetLastError(dwCode
);