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
68 /* FTP commands with arguments. */
84 /* FTP commands without arguments. */
93 static const CHAR
*szFtpCommands
[] = {
116 static const CHAR szMonths
[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
117 static const WCHAR szNoAccount
[] = {'n','o','a','c','c','o','u','n','t','\0'};
119 static void FTP_CloseFileTransferHandle(LPWININETHANDLEHEADER hdr
);
120 static void FTP_CloseSessionHandle(LPWININETHANDLEHEADER hdr
);
121 static void FTP_CloseFindNextHandle(LPWININETHANDLEHEADER hdr
);
122 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
123 INTERNET_STATUS_CALLBACK lpfnStatusCB
, LPWININETHANDLEHEADER hdr
, DWORD dwContext
);
124 static BOOL
FTP_SendStore(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
);
125 static BOOL
FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs
, LPINT nDataSocket
);
126 static BOOL
FTP_SendData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
);
127 static INT
FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs
, DWORD dwContext
);
128 static DWORD
FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
);
129 static BOOL
FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, DWORD nBytes
, HANDLE hFile
);
130 static BOOL
FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs
);
131 static BOOL
FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs
);
132 static BOOL
FTP_SendPassword(LPWININETFTPSESSIONW lpwfs
);
133 static BOOL
FTP_SendAccount(LPWININETFTPSESSIONW lpwfs
);
134 static BOOL
FTP_SendType(LPWININETFTPSESSIONW lpwfs
, DWORD dwType
);
135 static BOOL
FTP_GetFileSize(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD
*dwSize
);
136 static BOOL
FTP_SendPort(LPWININETFTPSESSIONW lpwfs
);
137 static BOOL
FTP_DoPassive(LPWININETFTPSESSIONW lpwfs
);
138 static BOOL
FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs
);
139 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
);
140 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW fileprop
);
141 static BOOL
FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
142 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
);
143 static HINTERNET
FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
144 LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwContext
);
145 static DWORD
FTP_SetResponseError(DWORD dwResponse
);
147 /***********************************************************************
148 * FtpPutFileA (WININET.@)
150 * Uploads a file to the FTP server
157 BOOL WINAPI
FtpPutFileA(HINTERNET hConnect
, LPCSTR lpszLocalFile
,
158 LPCSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD dwContext
)
160 LPWSTR lpwzLocalFile
;
161 LPWSTR lpwzNewRemoteFile
;
164 lpwzLocalFile
= lpszLocalFile
?WININET_strdup_AtoW(lpszLocalFile
):NULL
;
165 lpwzNewRemoteFile
= lpszNewRemoteFile
?WININET_strdup_AtoW(lpszNewRemoteFile
):NULL
;
166 ret
= FtpPutFileW(hConnect
, lpwzLocalFile
, lpwzNewRemoteFile
,
168 HeapFree(GetProcessHeap(), 0, lpwzLocalFile
);
169 HeapFree(GetProcessHeap(), 0, lpwzNewRemoteFile
);
173 /***********************************************************************
174 * FtpPutFileW (WININET.@)
176 * Uploads a file to the FTP server
183 BOOL WINAPI
FtpPutFileW(HINTERNET hConnect
, LPCWSTR lpszLocalFile
,
184 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD dwContext
)
186 LPWININETFTPSESSIONW lpwfs
;
187 LPWININETAPPINFOW hIC
= NULL
;
190 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
191 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
193 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
197 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
198 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
200 WORKREQUEST workRequest
;
201 struct WORKREQ_FTPPUTFILEW
*req
= &workRequest
.u
.FtpPutFileW
;
203 workRequest
.asyncall
= FTPPUTFILEW
;
204 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
205 req
->lpszLocalFile
= WININET_strdupW(lpszLocalFile
);
206 req
->lpszNewRemoteFile
= WININET_strdupW(lpszNewRemoteFile
);
207 req
->dwFlags
= dwFlags
;
208 req
->dwContext
= dwContext
;
210 r
= INTERNET_AsyncCall(&workRequest
);
214 r
= FTP_FtpPutFileW(lpwfs
, lpszLocalFile
,
215 lpszNewRemoteFile
, dwFlags
, dwContext
);
220 WININET_Release( &lpwfs
->hdr
);
225 /***********************************************************************
226 * FTP_FtpPutFileW (Internal)
228 * Uploads a file to the FTP server
235 BOOL WINAPI
FTP_FtpPutFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszLocalFile
,
236 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD dwContext
)
239 BOOL bSuccess
= FALSE
;
240 LPWININETAPPINFOW hIC
= NULL
;
243 TRACE(" lpszLocalFile(%s) lpszNewRemoteFile(%s)\n", debugstr_w(lpszLocalFile
), debugstr_w(lpszNewRemoteFile
));
245 if (!lpszLocalFile
|| !lpszNewRemoteFile
)
247 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
251 assert( WH_HFTPSESSION
== lpwfs
->hdr
.htype
);
253 /* Clear any error information */
254 INTERNET_SetLastError(0);
255 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
257 /* Open file to be uploaded */
258 if (INVALID_HANDLE_VALUE
==
259 (hFile
= CreateFileW(lpszLocalFile
, GENERIC_READ
, 0, 0, OPEN_EXISTING
, 0, 0)))
261 INTERNET_SetLastError(ERROR_FILE_NOT_FOUND
);
265 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
267 if (FTP_SendStore(lpwfs
, lpszNewRemoteFile
, dwFlags
))
271 /* Get data socket to server */
272 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
274 FTP_SendData(lpwfs
, nDataSocket
, hFile
);
275 closesocket(nDataSocket
);
276 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
282 FTP_SetResponseError(nResCode
);
288 if (lpwfs
->lstnSocket
!= -1)
289 closesocket(lpwfs
->lstnSocket
);
291 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
293 INTERNET_ASYNC_RESULT iar
;
295 iar
.dwResult
= (DWORD
)bSuccess
;
296 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
297 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
298 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
308 /***********************************************************************
309 * FtpSetCurrentDirectoryA (WININET.@)
311 * Change the working directory on the FTP server
318 BOOL WINAPI
FtpSetCurrentDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
320 LPWSTR lpwzDirectory
;
323 lpwzDirectory
= lpszDirectory
?WININET_strdup_AtoW(lpszDirectory
):NULL
;
324 ret
= FtpSetCurrentDirectoryW(hConnect
, lpwzDirectory
);
325 HeapFree(GetProcessHeap(), 0, lpwzDirectory
);
330 /***********************************************************************
331 * FtpSetCurrentDirectoryW (WININET.@)
333 * Change the working directory on the FTP server
340 BOOL WINAPI
FtpSetCurrentDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
342 LPWININETFTPSESSIONW lpwfs
= NULL
;
343 LPWININETAPPINFOW hIC
= NULL
;
348 SetLastError(ERROR_INVALID_PARAMETER
);
352 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
353 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
355 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
359 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
361 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
362 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
364 WORKREQUEST workRequest
;
365 struct WORKREQ_FTPSETCURRENTDIRECTORYW
*req
;
367 workRequest
.asyncall
= FTPSETCURRENTDIRECTORYW
;
368 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
369 req
= &workRequest
.u
.FtpSetCurrentDirectoryW
;
370 req
->lpszDirectory
= WININET_strdupW(lpszDirectory
);
372 r
= INTERNET_AsyncCall(&workRequest
);
376 r
= FTP_FtpSetCurrentDirectoryW(lpwfs
, lpszDirectory
);
381 WININET_Release( &lpwfs
->hdr
);
387 /***********************************************************************
388 * FTP_FtpSetCurrentDirectoryW (Internal)
390 * Change the working directory on the FTP server
397 BOOL WINAPI
FTP_FtpSetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
)
400 LPWININETAPPINFOW hIC
= NULL
;
401 DWORD bSuccess
= FALSE
;
403 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
405 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
407 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
411 /* Clear any error information */
412 INTERNET_SetLastError(0);
414 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
415 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_CWD
, lpszDirectory
,
416 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
419 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
426 FTP_SetResponseError(nResCode
);
430 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
432 INTERNET_ASYNC_RESULT iar
;
434 iar
.dwResult
= (DWORD
)bSuccess
;
435 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
436 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
437 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
443 /***********************************************************************
444 * FtpCreateDirectoryA (WININET.@)
446 * Create new directory on the FTP server
453 BOOL WINAPI
FtpCreateDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
455 LPWSTR lpwzDirectory
;
458 lpwzDirectory
= lpszDirectory
?WININET_strdup_AtoW(lpszDirectory
):NULL
;
459 ret
= FtpCreateDirectoryW(hConnect
, lpwzDirectory
);
460 HeapFree(GetProcessHeap(), 0, lpwzDirectory
);
465 /***********************************************************************
466 * FtpCreateDirectoryW (WININET.@)
468 * Create new directory on the FTP server
475 BOOL WINAPI
FtpCreateDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
477 LPWININETFTPSESSIONW lpwfs
;
478 LPWININETAPPINFOW hIC
= NULL
;
481 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
482 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
484 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
488 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
489 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
491 WORKREQUEST workRequest
;
492 struct WORKREQ_FTPCREATEDIRECTORYW
*req
;
494 workRequest
.asyncall
= FTPCREATEDIRECTORYW
;
495 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
496 req
= &workRequest
.u
.FtpCreateDirectoryW
;
497 req
->lpszDirectory
= WININET_strdupW(lpszDirectory
);
499 r
= INTERNET_AsyncCall(&workRequest
);
503 r
= FTP_FtpCreateDirectoryW(lpwfs
, lpszDirectory
);
507 WININET_Release( &lpwfs
->hdr
);
513 /***********************************************************************
514 * FTP_FtpCreateDirectoryW (Internal)
516 * Create new directory on the FTP server
523 BOOL WINAPI
FTP_FtpCreateDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
)
526 BOOL bSuccess
= FALSE
;
527 LPWININETAPPINFOW hIC
= NULL
;
529 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
531 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
533 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
537 /* Clear any error information */
538 INTERNET_SetLastError(0);
540 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_MKD
, lpszDirectory
, 0, 0, 0))
543 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
549 FTP_SetResponseError(nResCode
);
553 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
554 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
556 INTERNET_ASYNC_RESULT iar
;
558 iar
.dwResult
= (DWORD
)bSuccess
;
559 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
560 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
561 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
567 /***********************************************************************
568 * FtpFindFirstFileA (WININET.@)
570 * Search the specified directory
573 * HINTERNET on success
577 HINTERNET WINAPI
FtpFindFirstFileA(HINTERNET hConnect
,
578 LPCSTR lpszSearchFile
, LPWIN32_FIND_DATAA lpFindFileData
, DWORD dwFlags
, DWORD dwContext
)
580 LPWSTR lpwzSearchFile
;
581 WIN32_FIND_DATAW wfd
;
582 LPWIN32_FIND_DATAW lpFindFileDataW
;
585 lpwzSearchFile
= lpszSearchFile
?WININET_strdup_AtoW(lpszSearchFile
):NULL
;
586 lpFindFileDataW
= lpFindFileData
?&wfd
:NULL
;
587 ret
= FtpFindFirstFileW(hConnect
, lpwzSearchFile
, lpFindFileDataW
, dwFlags
, dwContext
);
588 HeapFree(GetProcessHeap(), 0, lpwzSearchFile
);
591 WININET_find_data_WtoA(lpFindFileDataW
, lpFindFileData
);
597 /***********************************************************************
598 * FtpFindFirstFileW (WININET.@)
600 * Search the specified directory
603 * HINTERNET on success
607 HINTERNET WINAPI
FtpFindFirstFileW(HINTERNET hConnect
,
608 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD dwContext
)
610 LPWININETFTPSESSIONW lpwfs
;
611 LPWININETAPPINFOW hIC
= NULL
;
614 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
615 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
617 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
621 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
622 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
624 WORKREQUEST workRequest
;
625 struct WORKREQ_FTPFINDFIRSTFILEW
*req
;
627 workRequest
.asyncall
= FTPFINDFIRSTFILEW
;
628 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
629 req
= &workRequest
.u
.FtpFindFirstFileW
;
630 req
->lpszSearchFile
= (lpszSearchFile
== NULL
) ? NULL
: WININET_strdupW(lpszSearchFile
);
631 req
->lpFindFileData
= lpFindFileData
;
632 req
->dwFlags
= dwFlags
;
633 req
->dwContext
= dwContext
;
635 INTERNET_AsyncCall(&workRequest
);
640 r
= FTP_FtpFindFirstFileW(lpwfs
, lpszSearchFile
, lpFindFileData
,
645 WININET_Release( &lpwfs
->hdr
);
651 /***********************************************************************
652 * FTP_FtpFindFirstFileW (Internal)
654 * Search the specified directory
657 * HINTERNET on success
661 HINTERNET WINAPI
FTP_FtpFindFirstFileW(LPWININETFTPSESSIONW lpwfs
,
662 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD dwContext
)
665 LPWININETAPPINFOW hIC
= NULL
;
666 HINTERNET hFindNext
= NULL
;
670 assert(WH_HFTPSESSION
== lpwfs
->hdr
.htype
);
672 /* Clear any error information */
673 INTERNET_SetLastError(0);
675 if (!FTP_InitListenSocket(lpwfs
))
678 if (!FTP_SendType(lpwfs
, INTERNET_FLAG_TRANSFER_ASCII
))
681 if (!FTP_SendPortOrPasv(lpwfs
))
684 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
685 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_LIST
, NULL
,
686 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
689 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
692 if (nResCode
== 125 || nResCode
== 150)
696 /* Get data socket to server */
697 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
699 hFindNext
= FTP_ReceiveFileList(lpwfs
, nDataSocket
, lpszSearchFile
, lpFindFileData
, dwContext
);
700 closesocket(nDataSocket
);
701 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
702 if (nResCode
!= 226 && nResCode
!= 250)
703 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
707 FTP_SetResponseError(nResCode
);
711 if (lpwfs
->lstnSocket
!= -1)
712 closesocket(lpwfs
->lstnSocket
);
714 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
716 INTERNET_ASYNC_RESULT iar
;
720 iar
.dwResult
= (DWORD
)hFindNext
;
721 iar
.dwError
= ERROR_SUCCESS
;
722 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
723 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
726 iar
.dwResult
= (DWORD
)hFindNext
;
727 iar
.dwError
= hFindNext
? ERROR_SUCCESS
: INTERNET_GetLastError();
728 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
729 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
736 /***********************************************************************
737 * FtpGetCurrentDirectoryA (WININET.@)
739 * Retrieves the current directory
746 BOOL WINAPI
FtpGetCurrentDirectoryA(HINTERNET hFtpSession
, LPSTR lpszCurrentDirectory
,
747 LPDWORD lpdwCurrentDirectory
)
753 if(lpdwCurrentDirectory
) {
754 len
= *lpdwCurrentDirectory
;
755 if(lpszCurrentDirectory
)
757 dir
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
760 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
765 ret
= FtpGetCurrentDirectoryW(hFtpSession
, lpszCurrentDirectory
?dir
:NULL
, lpdwCurrentDirectory
?&len
:NULL
);
766 if(lpdwCurrentDirectory
) {
767 *lpdwCurrentDirectory
= len
;
768 if(lpszCurrentDirectory
) {
769 WideCharToMultiByte(CP_ACP
, 0, dir
, len
, lpszCurrentDirectory
, *lpdwCurrentDirectory
, NULL
, NULL
);
770 HeapFree(GetProcessHeap(), 0, dir
);
777 /***********************************************************************
778 * FtpGetCurrentDirectoryW (WININET.@)
780 * Retrieves the current directory
787 BOOL WINAPI
FtpGetCurrentDirectoryW(HINTERNET hFtpSession
, LPWSTR lpszCurrentDirectory
,
788 LPDWORD lpdwCurrentDirectory
)
790 LPWININETFTPSESSIONW lpwfs
;
791 LPWININETAPPINFOW hIC
= NULL
;
794 TRACE("len(%ld)\n", *lpdwCurrentDirectory
);
796 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
797 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
799 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
803 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
804 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
806 WORKREQUEST workRequest
;
807 struct WORKREQ_FTPGETCURRENTDIRECTORYW
*req
;
809 workRequest
.asyncall
= FTPGETCURRENTDIRECTORYW
;
810 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
811 req
= &workRequest
.u
.FtpGetCurrentDirectoryW
;
812 req
->lpszDirectory
= lpszCurrentDirectory
;
813 req
->lpdwDirectory
= lpdwCurrentDirectory
;
815 r
= INTERNET_AsyncCall(&workRequest
);
819 r
= FTP_FtpGetCurrentDirectoryW(lpwfs
, lpszCurrentDirectory
,
820 lpdwCurrentDirectory
);
825 WININET_Release( &lpwfs
->hdr
);
831 /***********************************************************************
832 * FTP_FtpGetCurrentDirectoryA (Internal)
834 * Retrieves the current directory
841 BOOL WINAPI
FTP_FtpGetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPWSTR lpszCurrentDirectory
,
842 LPDWORD lpdwCurrentDirectory
)
845 LPWININETAPPINFOW hIC
= NULL
;
846 DWORD bSuccess
= FALSE
;
848 TRACE("len(%ld)\n", *lpdwCurrentDirectory
);
850 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
852 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
856 /* Clear any error information */
857 INTERNET_SetLastError(0);
859 ZeroMemory(lpszCurrentDirectory
, *lpdwCurrentDirectory
);
861 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
862 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PWD
, NULL
,
863 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
866 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
869 if (nResCode
== 257) /* Extract directory name */
871 DWORD firstpos
, lastpos
, len
;
872 LPWSTR lpszResponseBuffer
= WININET_strdup_AtoW(INTERNET_GetResponseBuffer());
874 for (firstpos
= 0, lastpos
= 0; lpszResponseBuffer
[lastpos
]; lastpos
++)
876 if ('"' == lpszResponseBuffer
[lastpos
])
885 len
= lastpos
- firstpos
- 1;
886 lstrcpynW(lpszCurrentDirectory
, &lpszResponseBuffer
[firstpos
+1], *lpdwCurrentDirectory
);
887 HeapFree(GetProcessHeap(), 0, lpszResponseBuffer
);
888 *lpdwCurrentDirectory
= len
;
892 FTP_SetResponseError(nResCode
);
896 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
898 INTERNET_ASYNC_RESULT iar
;
900 iar
.dwResult
= (DWORD
)bSuccess
;
901 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
902 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
903 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
906 return (DWORD
) bSuccess
;
909 /***********************************************************************
910 * FtpOpenFileA (WININET.@)
912 * Open a remote file for writing or reading
915 * HINTERNET handle on success
919 HINTERNET WINAPI
FtpOpenFileA(HINTERNET hFtpSession
,
920 LPCSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
926 lpwzFileName
= lpszFileName
?WININET_strdup_AtoW(lpszFileName
):NULL
;
927 ret
= FtpOpenFileW(hFtpSession
, lpwzFileName
, fdwAccess
, dwFlags
, dwContext
);
928 HeapFree(GetProcessHeap(), 0, lpwzFileName
);
933 /***********************************************************************
934 * FtpOpenFileW (WININET.@)
936 * Open a remote file for writing or reading
939 * HINTERNET handle on success
943 HINTERNET WINAPI
FtpOpenFileW(HINTERNET hFtpSession
,
944 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
947 LPWININETFTPSESSIONW lpwfs
;
948 LPWININETAPPINFOW hIC
= NULL
;
951 TRACE("(%p,%s,0x%08lx,0x%08lx,0x%08lx)\n", hFtpSession
,
952 debugstr_w(lpszFileName
), fdwAccess
, dwFlags
, dwContext
);
954 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
955 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
957 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
961 if (lpwfs
->download_in_progress
!= NULL
) {
962 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
965 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
966 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
968 WORKREQUEST workRequest
;
969 struct WORKREQ_FTPOPENFILEW
*req
;
971 workRequest
.asyncall
= FTPOPENFILEW
;
972 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
973 req
= &workRequest
.u
.FtpOpenFileW
;
974 req
->lpszFilename
= WININET_strdupW(lpszFileName
);
975 req
->dwAccess
= fdwAccess
;
976 req
->dwFlags
= dwFlags
;
977 req
->dwContext
= dwContext
;
979 INTERNET_AsyncCall(&workRequest
);
984 r
= FTP_FtpOpenFileW(lpwfs
, lpszFileName
, fdwAccess
, dwFlags
, dwContext
);
989 WININET_Release( &lpwfs
->hdr
);
995 /***********************************************************************
996 * FTP_FtpOpenFileW (Internal)
998 * Open a remote file for writing or reading
1001 * HINTERNET handle on success
1005 HINTERNET
FTP_FtpOpenFileW(LPWININETFTPSESSIONW lpwfs
,
1006 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1010 BOOL bSuccess
= FALSE
;
1011 LPWININETFILE lpwh
= NULL
;
1012 LPWININETAPPINFOW hIC
= NULL
;
1013 HINTERNET handle
= NULL
;
1017 assert (WH_HFTPSESSION
== lpwfs
->hdr
.htype
);
1019 /* Clear any error information */
1020 INTERNET_SetLastError(0);
1022 if (GENERIC_READ
== fdwAccess
)
1024 /* Set up socket to retrieve data */
1025 bSuccess
= FTP_SendRetrieve(lpwfs
, lpszFileName
, dwFlags
);
1027 else if (GENERIC_WRITE
== fdwAccess
)
1029 /* Set up socket to send data */
1030 bSuccess
= FTP_SendStore(lpwfs
, lpszFileName
, dwFlags
);
1033 /* Get data socket to server */
1034 if (bSuccess
&& FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1036 lpwh
= HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFILE
));
1037 lpwh
->hdr
.htype
= WH_HFILE
;
1038 lpwh
->hdr
.dwFlags
= dwFlags
;
1039 lpwh
->hdr
.dwContext
= dwContext
;
1040 lpwh
->hdr
.lpwhparent
= WININET_AddRef( &lpwfs
->hdr
);
1041 lpwh
->hdr
.dwRefCount
= 1;
1042 lpwh
->hdr
.destroy
= FTP_CloseFileTransferHandle
;
1043 lpwh
->hdr
.lpfnStatusCB
= lpwfs
->hdr
.lpfnStatusCB
;
1044 lpwh
->nDataSocket
= nDataSocket
;
1045 lpwh
->session_deleted
= FALSE
;
1047 handle
= WININET_AllocHandle( &lpwh
->hdr
);
1051 /* Indicate that a download is currently in progress */
1052 lpwfs
->download_in_progress
= lpwh
;
1055 if (lpwfs
->lstnSocket
!= -1)
1056 closesocket(lpwfs
->lstnSocket
);
1058 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
1059 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1061 INTERNET_ASYNC_RESULT iar
;
1065 iar
.dwResult
= (DWORD
)handle
;
1066 iar
.dwError
= ERROR_SUCCESS
;
1067 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
1068 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1071 iar
.dwResult
= (DWORD
)bSuccess
;
1072 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1073 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1074 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1079 WININET_Release( &lpwh
->hdr
);
1085 /***********************************************************************
1086 * FtpGetFileA (WININET.@)
1088 * Retrieve file from the FTP server
1095 BOOL WINAPI
FtpGetFileA(HINTERNET hInternet
, LPCSTR lpszRemoteFile
, LPCSTR lpszNewFile
,
1096 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1099 LPWSTR lpwzRemoteFile
;
1103 lpwzRemoteFile
= lpszRemoteFile
?WININET_strdup_AtoW(lpszRemoteFile
):NULL
;
1104 lpwzNewFile
= lpszNewFile
?WININET_strdup_AtoW(lpszNewFile
):NULL
;
1105 ret
= FtpGetFileW(hInternet
, lpwzRemoteFile
, lpwzNewFile
, fFailIfExists
,
1106 dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1107 HeapFree(GetProcessHeap(), 0, lpwzRemoteFile
);
1108 HeapFree(GetProcessHeap(), 0, lpwzNewFile
);
1113 /***********************************************************************
1114 * FtpGetFileW (WININET.@)
1116 * Retrieve file from the FTP server
1123 BOOL WINAPI
FtpGetFileW(HINTERNET hInternet
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1124 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1127 LPWININETFTPSESSIONW lpwfs
;
1128 LPWININETAPPINFOW hIC
= NULL
;
1131 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hInternet
);
1132 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1134 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1138 if (lpwfs
->download_in_progress
!= NULL
) {
1139 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1143 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
1144 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1146 WORKREQUEST workRequest
;
1147 struct WORKREQ_FTPGETFILEW
*req
;
1149 workRequest
.asyncall
= FTPGETFILEW
;
1150 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1151 req
= &workRequest
.u
.FtpGetFileW
;
1152 req
->lpszRemoteFile
= WININET_strdupW(lpszRemoteFile
);
1153 req
->lpszNewFile
= WININET_strdupW(lpszNewFile
);
1154 req
->dwLocalFlagsAttribute
= dwLocalFlagsAttribute
;
1155 req
->fFailIfExists
= fFailIfExists
;
1156 req
->dwFlags
= dwInternetFlags
;
1157 req
->dwContext
= dwContext
;
1159 r
= INTERNET_AsyncCall(&workRequest
);
1163 r
= FTP_FtpGetFileW(lpwfs
, lpszRemoteFile
, lpszNewFile
,
1164 fFailIfExists
, dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1169 WININET_Release( &lpwfs
->hdr
);
1175 /***********************************************************************
1176 * FTP_FtpGetFileW (Internal)
1178 * Retrieve file from the FTP server
1185 BOOL WINAPI
FTP_FtpGetFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1186 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1190 BOOL bSuccess
= FALSE
;
1192 LPWININETAPPINFOW hIC
= NULL
;
1194 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile
), debugstr_w(lpszNewFile
));
1196 assert (WH_HFTPSESSION
== lpwfs
->hdr
.htype
);
1198 /* Clear any error information */
1199 INTERNET_SetLastError(0);
1201 /* Ensure we can write to lpszNewfile by opening it */
1202 hFile
= CreateFileW(lpszNewFile
, GENERIC_WRITE
, 0, 0, fFailIfExists
?
1203 CREATE_NEW
: CREATE_ALWAYS
, dwLocalFlagsAttribute
, 0);
1204 if (INVALID_HANDLE_VALUE
== hFile
)
1207 /* Set up socket to retrieve data */
1208 nBytes
= FTP_SendRetrieve(lpwfs
, lpszRemoteFile
, dwInternetFlags
);
1214 /* Get data socket to server */
1215 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1220 FTP_RetrieveFileData(lpwfs
, nDataSocket
, nBytes
, hFile
);
1221 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
1224 if (nResCode
== 226)
1227 FTP_SetResponseError(nResCode
);
1229 closesocket(nDataSocket
);
1234 if (lpwfs
->lstnSocket
!= -1)
1235 closesocket(lpwfs
->lstnSocket
);
1240 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
1241 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1243 INTERNET_ASYNC_RESULT iar
;
1245 iar
.dwResult
= (DWORD
)bSuccess
;
1246 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1247 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1248 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1254 /***********************************************************************
1255 * FtpGetFileSize (WININET.@)
1257 DWORD WINAPI
FtpGetFileSize( HINTERNET hFile
, LPDWORD lpdwFileSizeHigh
)
1259 FIXME("(%p, %p)\n", hFile
, lpdwFileSizeHigh
);
1261 if (lpdwFileSizeHigh
)
1262 *lpdwFileSizeHigh
= 0;
1267 /***********************************************************************
1268 * FtpDeleteFileA (WININET.@)
1270 * Delete a file on the ftp server
1277 BOOL WINAPI
FtpDeleteFileA(HINTERNET hFtpSession
, LPCSTR lpszFileName
)
1279 LPWSTR lpwzFileName
;
1282 lpwzFileName
= lpszFileName
?WININET_strdup_AtoW(lpszFileName
):NULL
;
1283 ret
= FtpDeleteFileW(hFtpSession
, lpwzFileName
);
1284 HeapFree(GetProcessHeap(), 0, lpwzFileName
);
1288 /***********************************************************************
1289 * FtpDeleteFileW (WININET.@)
1291 * Delete a file on the ftp server
1298 BOOL WINAPI
FtpDeleteFileW(HINTERNET hFtpSession
, LPCWSTR lpszFileName
)
1300 LPWININETFTPSESSIONW lpwfs
;
1301 LPWININETAPPINFOW hIC
= NULL
;
1304 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1305 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1307 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1311 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
1312 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1314 WORKREQUEST workRequest
;
1315 struct WORKREQ_FTPDELETEFILEW
*req
;
1317 workRequest
.asyncall
= FTPDELETEFILEW
;
1318 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1319 req
= &workRequest
.u
.FtpDeleteFileW
;
1320 req
->lpszFilename
= WININET_strdupW(lpszFileName
);
1322 r
= INTERNET_AsyncCall(&workRequest
);
1326 r
= FTP_FtpDeleteFileW(hFtpSession
, lpszFileName
);
1331 WININET_Release( &lpwfs
->hdr
);
1336 /***********************************************************************
1337 * FTP_FtpDeleteFileW (Internal)
1339 * Delete a file on the ftp server
1346 BOOL
FTP_FtpDeleteFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszFileName
)
1349 BOOL bSuccess
= FALSE
;
1350 LPWININETAPPINFOW hIC
= NULL
;
1352 TRACE("%p\n", lpwfs
);
1354 assert (WH_HFTPSESSION
== lpwfs
->hdr
.htype
);
1356 /* Clear any error information */
1357 INTERNET_SetLastError(0);
1359 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_DELE
, lpszFileName
, 0, 0, 0))
1362 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1365 if (nResCode
== 250)
1368 FTP_SetResponseError(nResCode
);
1371 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
1372 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1374 INTERNET_ASYNC_RESULT iar
;
1376 iar
.dwResult
= (DWORD
)bSuccess
;
1377 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1378 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1379 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1386 /***********************************************************************
1387 * FtpRemoveDirectoryA (WININET.@)
1389 * Remove a directory on the ftp server
1396 BOOL WINAPI
FtpRemoveDirectoryA(HINTERNET hFtpSession
, LPCSTR lpszDirectory
)
1398 LPWSTR lpwzDirectory
;
1401 lpwzDirectory
= lpszDirectory
?WININET_strdup_AtoW(lpszDirectory
):NULL
;
1402 ret
= FtpRemoveDirectoryW(hFtpSession
, lpwzDirectory
);
1403 HeapFree(GetProcessHeap(), 0, lpwzDirectory
);
1407 /***********************************************************************
1408 * FtpRemoveDirectoryW (WININET.@)
1410 * Remove a directory on the ftp server
1417 BOOL WINAPI
FtpRemoveDirectoryW(HINTERNET hFtpSession
, LPCWSTR lpszDirectory
)
1419 LPWININETFTPSESSIONW lpwfs
;
1420 LPWININETAPPINFOW hIC
= NULL
;
1423 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1424 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1426 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1430 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
1431 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1433 WORKREQUEST workRequest
;
1434 struct WORKREQ_FTPREMOVEDIRECTORYW
*req
;
1436 workRequest
.asyncall
= FTPREMOVEDIRECTORYW
;
1437 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1438 req
= &workRequest
.u
.FtpRemoveDirectoryW
;
1439 req
->lpszDirectory
= WININET_strdupW(lpszDirectory
);
1441 r
= INTERNET_AsyncCall(&workRequest
);
1445 r
= FTP_FtpRemoveDirectoryW(lpwfs
, lpszDirectory
);
1450 WININET_Release( &lpwfs
->hdr
);
1455 /***********************************************************************
1456 * FTP_FtpRemoveDirectoryW (Internal)
1458 * Remove a directory on the ftp server
1465 BOOL
FTP_FtpRemoveDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
)
1468 BOOL bSuccess
= FALSE
;
1469 LPWININETAPPINFOW hIC
= NULL
;
1473 assert (WH_HFTPSESSION
== lpwfs
->hdr
.htype
);
1475 /* Clear any error information */
1476 INTERNET_SetLastError(0);
1478 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RMD
, lpszDirectory
, 0, 0, 0))
1481 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1484 if (nResCode
== 250)
1487 FTP_SetResponseError(nResCode
);
1491 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
1492 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1494 INTERNET_ASYNC_RESULT iar
;
1496 iar
.dwResult
= (DWORD
)bSuccess
;
1497 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1498 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1499 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1506 /***********************************************************************
1507 * FtpRenameFileA (WININET.@)
1509 * Rename a file on the ftp server
1516 BOOL WINAPI
FtpRenameFileA(HINTERNET hFtpSession
, LPCSTR lpszSrc
, LPCSTR lpszDest
)
1522 lpwzSrc
= lpszSrc
?WININET_strdup_AtoW(lpszSrc
):NULL
;
1523 lpwzDest
= lpszDest
?WININET_strdup_AtoW(lpszDest
):NULL
;
1524 ret
= FtpRenameFileW(hFtpSession
, lpwzSrc
, lpwzDest
);
1525 HeapFree(GetProcessHeap(), 0, lpwzSrc
);
1526 HeapFree(GetProcessHeap(), 0, lpwzDest
);
1530 /***********************************************************************
1531 * FtpRenameFileW (WININET.@)
1533 * Rename a file on the ftp server
1540 BOOL WINAPI
FtpRenameFileW(HINTERNET hFtpSession
, LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
1542 LPWININETFTPSESSIONW lpwfs
;
1543 LPWININETAPPINFOW hIC
= NULL
;
1546 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1547 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1549 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1553 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
1554 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1556 WORKREQUEST workRequest
;
1557 struct WORKREQ_FTPRENAMEFILEW
*req
;
1559 workRequest
.asyncall
= FTPRENAMEFILEW
;
1560 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1561 req
= &workRequest
.u
.FtpRenameFileW
;
1562 req
->lpszSrcFile
= WININET_strdupW(lpszSrc
);
1563 req
->lpszDestFile
= WININET_strdupW(lpszDest
);
1565 r
= INTERNET_AsyncCall(&workRequest
);
1569 r
= FTP_FtpRenameFileW(hFtpSession
, lpszSrc
, lpszDest
);
1574 WININET_Release( &lpwfs
->hdr
);
1579 /***********************************************************************
1580 * FTP_FtpRenameFileW (Internal)
1582 * Rename a file on the ftp server
1589 BOOL
FTP_FtpRenameFileW( LPWININETFTPSESSIONW lpwfs
,
1590 LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
1593 BOOL bSuccess
= FALSE
;
1594 LPWININETAPPINFOW hIC
= NULL
;
1598 assert (WH_HFTPSESSION
== lpwfs
->hdr
.htype
);
1600 /* Clear any error information */
1601 INTERNET_SetLastError(0);
1603 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNFR
, lpszSrc
, 0, 0, 0))
1606 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1607 if (nResCode
== 350)
1609 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNTO
, lpszDest
, 0, 0, 0))
1612 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1615 if (nResCode
== 250)
1618 FTP_SetResponseError(nResCode
);
1621 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
1622 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1624 INTERNET_ASYNC_RESULT iar
;
1626 iar
.dwResult
= (DWORD
)bSuccess
;
1627 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1628 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1629 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1635 /***********************************************************************
1636 * FtpCommandA (WININET.@)
1638 BOOL WINAPI
FtpCommandA( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
1639 LPCSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
1641 FIXME("%p %d 0x%08lx %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
1642 debugstr_a(lpszCommand
), dwContext
, phFtpCommand
);
1647 /***********************************************************************
1648 * FtpCommandW (WININET.@)
1650 BOOL WINAPI
FtpCommandW( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
1651 LPCWSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
1653 FIXME("%p %d 0x%08lx %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
1654 debugstr_w(lpszCommand
), dwContext
, phFtpCommand
);
1659 /***********************************************************************
1660 * FTP_Connect (internal)
1662 * Connect to a ftp server
1665 * HINTERNET a session handle on success
1670 HINTERNET
FTP_Connect(LPWININETAPPINFOW hIC
, LPCWSTR lpszServerName
,
1671 INTERNET_PORT nServerPort
, LPCWSTR lpszUserName
,
1672 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD dwContext
,
1673 DWORD dwInternalFlags
)
1675 static const WCHAR szDefaultUsername
[] = {'a','n','o','n','y','m','o','u','s','\0'};
1676 static const WCHAR szDefaultPassword
[] = {'u','s','e','r','@','s','e','r','v','e','r','\0'};
1677 static const WCHAR szEmpty
[] = {'\0'};
1678 struct sockaddr_in socketAddr
;
1681 BOOL bSuccess
= FALSE
;
1682 LPWININETFTPSESSIONW lpwfs
= NULL
;
1683 HINTERNET handle
= NULL
;
1685 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
1686 hIC
, debugstr_w(lpszServerName
),
1687 nServerPort
, debugstr_w(lpszUserName
), debugstr_w(lpszPassword
));
1689 assert( hIC
->hdr
.htype
== WH_HINIT
);
1691 if (NULL
== lpszUserName
&& NULL
!= lpszPassword
)
1693 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1697 lpwfs
= HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPSESSIONW
));
1700 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1704 if (nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
1705 nServerPort
= INTERNET_DEFAULT_FTP_PORT
;
1707 lpwfs
->hdr
.htype
= WH_HFTPSESSION
;
1708 lpwfs
->hdr
.lpwhparent
= WININET_AddRef( &hIC
->hdr
);
1709 lpwfs
->hdr
.dwFlags
= dwFlags
;
1710 lpwfs
->hdr
.dwContext
= dwContext
;
1711 lpwfs
->hdr
.dwInternalFlags
= dwInternalFlags
;
1712 lpwfs
->hdr
.dwRefCount
= 1;
1713 lpwfs
->hdr
.destroy
= FTP_CloseSessionHandle
;
1714 lpwfs
->hdr
.lpfnStatusCB
= hIC
->hdr
.lpfnStatusCB
;
1715 lpwfs
->download_in_progress
= NULL
;
1717 handle
= WININET_AllocHandle( &lpwfs
->hdr
);
1720 ERR("Failed to alloc handle\n");
1721 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1725 if(hIC
->lpszProxy
&& hIC
->dwAccessType
== INTERNET_OPEN_TYPE_PROXY
) {
1726 if(strchrW(hIC
->lpszProxy
, ' '))
1727 FIXME("Several proxies not implemented.\n");
1728 if(hIC
->lpszProxyBypass
)
1729 FIXME("Proxy bypass is ignored.\n");
1731 if ( !lpszUserName
) {
1732 lpwfs
->lpszUserName
= WININET_strdupW(szDefaultUsername
);
1733 lpwfs
->lpszPassword
= WININET_strdupW(szDefaultPassword
);
1736 lpwfs
->lpszUserName
= WININET_strdupW(lpszUserName
);
1739 lpwfs
->lpszPassword
= WININET_strdupW(lpszPassword
);
1741 lpwfs
->lpszPassword
= WININET_strdupW(szEmpty
);
1744 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
1745 if (!(lpwfs
->hdr
.dwInternalFlags
& INET_OPENURL
))
1747 INTERNET_ASYNC_RESULT iar
;
1749 iar
.dwResult
= (DWORD
)handle
;
1750 iar
.dwError
= ERROR_SUCCESS
;
1752 SendAsyncCallback(&hIC
->hdr
, dwContext
,
1753 INTERNET_STATUS_HANDLE_CREATED
, &iar
,
1754 sizeof(INTERNET_ASYNC_RESULT
));
1757 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_RESOLVING_NAME
,
1758 (LPWSTR
) lpszServerName
, strlenW(lpszServerName
));
1760 if (!GetAddress(lpszServerName
, nServerPort
, &socketAddr
))
1762 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
1766 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_NAME_RESOLVED
,
1767 (LPWSTR
) lpszServerName
, strlenW(lpszServerName
));
1769 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
1772 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
1776 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTING_TO_SERVER
,
1777 &socketAddr
, sizeof(struct sockaddr_in
));
1779 if (connect(nsocket
, (struct sockaddr
*)&socketAddr
, sizeof(socketAddr
)) < 0)
1781 ERR("Unable to connect (%s)\n", strerror(errno
));
1782 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
1786 TRACE("Connected to server\n");
1787 lpwfs
->sndSocket
= nsocket
;
1788 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTED_TO_SERVER
,
1789 &socketAddr
, sizeof(struct sockaddr_in
));
1791 sock_namelen
= sizeof(lpwfs
->socketAddress
);
1792 getsockname(nsocket
, (struct sockaddr
*) &lpwfs
->socketAddress
, &sock_namelen
);
1794 if (FTP_ConnectToHost(lpwfs
))
1796 TRACE("Successfully logged into server\n");
1802 if (!bSuccess
&& nsocket
== -1)
1803 closesocket(nsocket
);
1805 if (!bSuccess
&& lpwfs
)
1807 HeapFree(GetProcessHeap(), 0, lpwfs
);
1808 WININET_FreeHandle( handle
);
1813 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1815 INTERNET_ASYNC_RESULT iar
;
1817 iar
.dwResult
= (DWORD
)lpwfs
;
1818 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1819 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1820 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1827 /***********************************************************************
1828 * FTP_ConnectToHost (internal)
1830 * Connect to a ftp server
1837 static BOOL
FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs
)
1840 BOOL bSuccess
= FALSE
;
1843 FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1845 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_USER
, lpwfs
->lpszUserName
, 0, 0, 0))
1848 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1851 /* Login successful... */
1852 if (nResCode
== 230)
1854 /* User name okay, need password... */
1855 else if (nResCode
== 331)
1856 bSuccess
= FTP_SendPassword(lpwfs
);
1857 /* Need account for login... */
1858 else if (nResCode
== 332)
1859 bSuccess
= FTP_SendAccount(lpwfs
);
1861 FTP_SetResponseError(nResCode
);
1864 TRACE("Returning %d\n", bSuccess
);
1870 /***********************************************************************
1871 * FTP_SendCommandA (internal)
1873 * Send command to server
1880 static BOOL
FTP_SendCommandA(INT nSocket
, FTP_COMMAND ftpCmd
, LPCSTR lpszParam
,
1881 INTERNET_STATUS_CALLBACK lpfnStatusCB
, LPWININETHANDLEHEADER hdr
, DWORD dwContext
)
1885 DWORD nBytesSent
= 0;
1889 TRACE("%d: (%s) %d\n", ftpCmd
, lpszParam
, nSocket
);
1893 HINTERNET hHandle
= WININET_FindHandle( hdr
);
1896 lpfnStatusCB(hHandle
, dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
1897 WININET_Release( hdr
);
1901 dwParamLen
= lpszParam
?strlen(lpszParam
)+1:0;
1902 len
= dwParamLen
+ strlen(szFtpCommands
[ftpCmd
]) + strlen(szCRLF
);
1903 if (NULL
== (buf
= HeapAlloc(GetProcessHeap(), 0, len
+1)))
1905 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1908 sprintf(buf
, "%s%s%s%s", szFtpCommands
[ftpCmd
], dwParamLen
? " " : "",
1909 dwParamLen
? lpszParam
: "", szCRLF
);
1911 TRACE("Sending (%s) len(%ld)\n", buf
, len
);
1912 while((nBytesSent
< len
) && (nRC
!= -1))
1914 nRC
= send(nSocket
, buf
+nBytesSent
, len
- nBytesSent
, 0);
1918 HeapFree(GetProcessHeap(), 0, (LPVOID
)buf
);
1922 HINTERNET hHandle
= WININET_FindHandle( hdr
);
1925 lpfnStatusCB(hHandle
, dwContext
, INTERNET_STATUS_REQUEST_SENT
,
1926 &nBytesSent
, sizeof(DWORD
));
1927 WININET_Release( hdr
);
1931 TRACE("Sent %ld bytes\n", nBytesSent
);
1935 /***********************************************************************
1936 * FTP_SendCommand (internal)
1938 * Send command to server
1945 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
1946 INTERNET_STATUS_CALLBACK lpfnStatusCB
, LPWININETHANDLEHEADER hdr
, DWORD dwContext
)
1949 LPSTR lpszParamA
= lpszParam
?WININET_strdup_WtoA(lpszParam
):NULL
;
1950 ret
= FTP_SendCommandA(nSocket
, ftpCmd
, lpszParamA
, lpfnStatusCB
, hdr
, dwContext
);
1951 HeapFree(GetProcessHeap(), 0, lpszParamA
);
1955 /***********************************************************************
1956 * FTP_ReceiveResponse (internal)
1958 * Receive response from server
1961 * Reply code on success
1965 INT
FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs
, DWORD dwContext
)
1967 LPSTR lpszResponse
= INTERNET_GetResponseBuffer();
1970 char firstprefix
[5];
1971 BOOL multiline
= FALSE
;
1972 LPWININETAPPINFOW hIC
= NULL
;
1974 TRACE("socket(%d)\n", lpwfs
->sndSocket
);
1976 hIC
= (LPWININETAPPINFOW
) lpwfs
->hdr
.lpwhparent
;
1977 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
1981 if (!INTERNET_GetNextLine(lpwfs
->sndSocket
, &nRecv
))
1988 if(lpszResponse
[3] != '-')
1991 { /* Start of multiline repsonse. Loop until we get "nnn " */
1993 memcpy(firstprefix
, lpszResponse
, 3);
1994 firstprefix
[3] = ' ';
1995 firstprefix
[4] = '\0';
2000 if(!memcmp(firstprefix
, lpszResponse
, 4))
2008 rc
= atoi(lpszResponse
);
2010 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
2011 &nRecv
, sizeof(DWORD
));
2015 TRACE("return %d\n", rc
);
2020 /***********************************************************************
2021 * FTP_SendPassword (internal)
2023 * Send password to ftp server
2030 static BOOL
FTP_SendPassword(LPWININETFTPSESSIONW lpwfs
)
2033 BOOL bSuccess
= FALSE
;
2036 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASS
, lpwfs
->lpszPassword
, 0, 0, 0))
2039 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2042 TRACE("Received reply code %d\n", nResCode
);
2043 /* Login successful... */
2044 if (nResCode
== 230)
2046 /* Command not implemented, superfluous at the server site... */
2047 /* Need account for login... */
2048 else if (nResCode
== 332)
2049 bSuccess
= FTP_SendAccount(lpwfs
);
2051 FTP_SetResponseError(nResCode
);
2055 TRACE("Returning %d\n", bSuccess
);
2060 /***********************************************************************
2061 * FTP_SendAccount (internal)
2070 static BOOL
FTP_SendAccount(LPWININETFTPSESSIONW lpwfs
)
2073 BOOL bSuccess
= FALSE
;
2076 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_ACCT
, szNoAccount
, 0, 0, 0))
2079 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2083 FTP_SetResponseError(nResCode
);
2090 /***********************************************************************
2091 * FTP_SendStore (internal)
2093 * Send request to upload file to ftp server
2100 static BOOL
FTP_SendStore(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
2103 BOOL bSuccess
= FALSE
;
2106 if (!FTP_InitListenSocket(lpwfs
))
2109 if (!FTP_SendType(lpwfs
, dwType
))
2112 if (!FTP_SendPortOrPasv(lpwfs
))
2115 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_STOR
, lpszRemoteFile
, 0, 0, 0))
2117 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2120 if (nResCode
== 150 || nResCode
== 125)
2123 FTP_SetResponseError(nResCode
);
2127 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2129 closesocket(lpwfs
->lstnSocket
);
2130 lpwfs
->lstnSocket
= -1;
2137 /***********************************************************************
2138 * FTP_InitListenSocket (internal)
2140 * Create a socket to listen for server response
2147 static BOOL
FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs
)
2149 BOOL bSuccess
= FALSE
;
2150 size_t namelen
= sizeof(struct sockaddr_in
);
2154 lpwfs
->lstnSocket
= socket(PF_INET
, SOCK_STREAM
, 0);
2155 if (lpwfs
->lstnSocket
== -1)
2157 TRACE("Unable to create listening socket\n");
2161 /* We obtain our ip addr from the name of the command channel socket */
2162 lpwfs
->lstnSocketAddress
= lpwfs
->socketAddress
;
2164 /* and get the system to assign us a port */
2165 lpwfs
->lstnSocketAddress
.sin_port
= htons((u_short
) 0);
2167 if (bind(lpwfs
->lstnSocket
,(struct sockaddr
*) &lpwfs
->lstnSocketAddress
, sizeof(struct sockaddr_in
)) == -1)
2169 TRACE("Unable to bind socket\n");
2173 if (listen(lpwfs
->lstnSocket
, MAX_BACKLOG
) == -1)
2175 TRACE("listen failed\n");
2179 if (getsockname(lpwfs
->lstnSocket
, (struct sockaddr
*) &lpwfs
->lstnSocketAddress
, &namelen
) != -1)
2183 if (!bSuccess
&& lpwfs
->lstnSocket
== -1)
2185 closesocket(lpwfs
->lstnSocket
);
2186 lpwfs
->lstnSocket
= -1;
2193 /***********************************************************************
2194 * FTP_SendType (internal)
2196 * Tell server type of data being transferred
2202 * W98SE doesn't cache the type that's currently set
2203 * (i.e. it sends it always),
2204 * so we probably don't want to do that either.
2206 static BOOL
FTP_SendType(LPWININETFTPSESSIONW lpwfs
, DWORD dwType
)
2209 WCHAR type
[] = { 'I','\0' };
2210 BOOL bSuccess
= FALSE
;
2213 if (dwType
& INTERNET_FLAG_TRANSFER_ASCII
)
2216 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_TYPE
, type
, 0, 0, 0))
2219 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
)/100;
2225 FTP_SetResponseError(nResCode
);
2232 /***********************************************************************
2233 * FTP_GetFileSize (internal)
2235 * Retrieves from the server the size of the given file
2242 static BOOL
FTP_GetFileSize(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD
*dwSize
)
2245 BOOL bSuccess
= FALSE
;
2249 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_SIZE
, lpszRemoteFile
, 0, 0, 0))
2252 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2255 if (nResCode
== 213) {
2256 /* Now parses the output to get the actual file size */
2258 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
2260 for (i
= 0; (lpszResponseBuffer
[i
] != ' ') && (lpszResponseBuffer
[i
] != '\0'); i
++) ;
2261 if (lpszResponseBuffer
[i
] == '\0') return FALSE
;
2262 *dwSize
= atol(&(lpszResponseBuffer
[i
+ 1]));
2266 FTP_SetResponseError(nResCode
);
2275 /***********************************************************************
2276 * FTP_SendPort (internal)
2278 * Tell server which port to use
2285 static BOOL
FTP_SendPort(LPWININETFTPSESSIONW lpwfs
)
2287 static const WCHAR szIPFormat
[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
2289 WCHAR szIPAddress
[64];
2290 BOOL bSuccess
= FALSE
;
2293 sprintfW(szIPAddress
, szIPFormat
,
2294 lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x000000FF,
2295 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x0000FF00)>>8,
2296 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x00FF0000)>>16,
2297 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0xFF000000)>>24,
2298 lpwfs
->lstnSocketAddress
.sin_port
& 0xFF,
2299 (lpwfs
->lstnSocketAddress
.sin_port
& 0xFF00)>>8);
2301 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PORT
, szIPAddress
, 0, 0, 0))
2304 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2307 if (nResCode
== 200)
2310 FTP_SetResponseError(nResCode
);
2318 /***********************************************************************
2319 * FTP_DoPassive (internal)
2321 * Tell server that we want to do passive transfers
2322 * and connect data socket
2329 static BOOL
FTP_DoPassive(LPWININETFTPSESSIONW lpwfs
)
2332 BOOL bSuccess
= FALSE
;
2335 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASV
, NULL
, 0, 0, 0))
2338 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2341 if (nResCode
== 227)
2343 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
2347 char *pAddr
, *pPort
;
2349 struct sockaddr_in dataSocketAddress
;
2351 p
= lpszResponseBuffer
+4; /* skip status code */
2353 /* do a very strict check; we can improve that later. */
2355 if (strncmp(p
, "Entering Passive Mode", 21))
2357 ERR("unknown response '%.*s', aborting\n", 21, p
);
2360 p
+= 21; /* skip string */
2361 if ((*p
++ != ' ') || (*p
++ != '('))
2363 ERR("unknown response format, aborting\n");
2367 if (sscanf(p
, "%d,%d,%d,%d,%d,%d", &f
[0], &f
[1], &f
[2], &f
[3],
2370 ERR("unknown response address format '%s', aborting\n", p
);
2373 for (i
=0; i
< 6; i
++)
2376 dataSocketAddress
= lpwfs
->socketAddress
;
2377 pAddr
= (char *)&(dataSocketAddress
.sin_addr
.s_addr
);
2378 pPort
= (char *)&(dataSocketAddress
.sin_port
);
2386 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
2390 if (connect(nsocket
, (struct sockaddr
*)&dataSocketAddress
, sizeof(dataSocketAddress
)))
2392 ERR("can't connect passive FTP data port.\n");
2395 lpwfs
->pasvSocket
= nsocket
;
2399 FTP_SetResponseError(nResCode
);
2407 static BOOL
FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs
)
2409 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
2411 if (!FTP_DoPassive(lpwfs
))
2416 if (!FTP_SendPort(lpwfs
))
2423 /***********************************************************************
2424 * FTP_GetDataSocket (internal)
2426 * Either accepts an incoming data socket connection from the server
2427 * or just returns the already opened socket after a PASV command
2428 * in case of passive FTP.
2436 static BOOL
FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs
, LPINT nDataSocket
)
2438 struct sockaddr_in saddr
;
2439 size_t addrlen
= sizeof(struct sockaddr
);
2442 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
2444 *nDataSocket
= lpwfs
->pasvSocket
;
2448 *nDataSocket
= accept(lpwfs
->lstnSocket
, (struct sockaddr
*) &saddr
, &addrlen
);
2449 closesocket(lpwfs
->lstnSocket
);
2450 lpwfs
->lstnSocket
= -1;
2452 return *nDataSocket
!= -1;
2456 /***********************************************************************
2457 * FTP_SendData (internal)
2459 * Send data to the server
2466 static BOOL
FTP_SendData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
)
2468 BY_HANDLE_FILE_INFORMATION fi
;
2469 DWORD nBytesRead
= 0;
2470 DWORD nBytesSent
= 0;
2471 DWORD nTotalSent
= 0;
2472 DWORD nBytesToSend
, nLen
;
2474 time_t s_long_time
, e_long_time
;
2479 lpszBuffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(CHAR
)*DATA_PACKET_SIZE
);
2480 memset(lpszBuffer
, 0, sizeof(CHAR
)*DATA_PACKET_SIZE
);
2482 /* Get the size of the file. */
2483 GetFileInformationByHandle(hFile
, &fi
);
2488 nBytesToSend
= nBytesRead
- nBytesSent
;
2490 if (nBytesToSend
<= 0)
2492 /* Read data from file. */
2494 if (!ReadFile(hFile
, lpszBuffer
, DATA_PACKET_SIZE
, &nBytesRead
, 0))
2495 ERR("Failed reading from file\n");
2498 nBytesToSend
= nBytesRead
;
2503 nLen
= DATA_PACKET_SIZE
< nBytesToSend
?
2504 DATA_PACKET_SIZE
: nBytesToSend
;
2505 nRC
= send(nDataSocket
, lpszBuffer
, nLen
, 0);
2513 /* Do some computation to display the status. */
2515 nSeconds
= e_long_time
- s_long_time
;
2516 if( nSeconds
/ 60 > 0 )
2518 TRACE( "%ld bytes of %ld bytes (%ld%%) in %ld min %ld sec estimated remaining time %ld sec\n",
2519 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
/ 60,
2520 nSeconds
% 60, (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
2524 TRACE( "%ld bytes of %ld bytes (%ld%%) in %ld sec estimated remaining time %ld sec\n",
2525 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
,
2526 (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
2528 } while (nRC
!= -1);
2530 TRACE("file transfer complete!\n");
2532 HeapFree(GetProcessHeap(), 0, lpszBuffer
);
2538 /***********************************************************************
2539 * FTP_SendRetrieve (internal)
2541 * Send request to retrieve a file
2544 * Number of bytes to be received on success
2548 static DWORD
FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
2554 if (!FTP_InitListenSocket(lpwfs
))
2557 if (!FTP_SendType(lpwfs
, dwType
))
2560 if (!FTP_SendPortOrPasv(lpwfs
))
2563 if (!FTP_GetFileSize(lpwfs
, lpszRemoteFile
, &nResult
))
2566 TRACE("Waiting to receive %ld bytes\n", nResult
);
2568 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RETR
, lpszRemoteFile
, 0, 0, 0))
2571 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2572 if ((nResCode
!= 125) && (nResCode
!= 150)) {
2573 /* That means that we got an error getting the file. */
2578 if (0 == nResult
&& lpwfs
->lstnSocket
!= -1)
2580 closesocket(lpwfs
->lstnSocket
);
2581 lpwfs
->lstnSocket
= -1;
2588 /***********************************************************************
2589 * FTP_RetrieveData (internal)
2591 * Retrieve data from server
2598 static BOOL
FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, DWORD nBytes
, HANDLE hFile
)
2600 DWORD nBytesWritten
;
2601 DWORD nBytesReceived
= 0;
2607 if (INVALID_HANDLE_VALUE
== hFile
)
2610 lpszBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(CHAR
)*DATA_PACKET_SIZE
);
2611 if (NULL
== lpszBuffer
)
2613 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2617 while (nBytesReceived
< nBytes
&& nRC
!= -1)
2619 nRC
= recv(nDataSocket
, lpszBuffer
, DATA_PACKET_SIZE
, 0);
2622 /* other side closed socket. */
2625 WriteFile(hFile
, lpszBuffer
, nRC
, &nBytesWritten
, NULL
);
2626 nBytesReceived
+= nRC
;
2629 TRACE("%ld bytes of %ld (%ld%%)\r", nBytesReceived
, nBytes
,
2630 nBytesReceived
* 100 / nBytes
);
2633 TRACE("Data transfer complete\n");
2634 HeapFree(GetProcessHeap(), 0, lpszBuffer
);
2641 /***********************************************************************
2642 * FTP_CloseSessionHandle (internal)
2644 * Deallocate session handle
2651 static void FTP_CloseSessionHandle(LPWININETHANDLEHEADER hdr
)
2653 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) hdr
;
2657 if (lpwfs
->download_in_progress
!= NULL
)
2658 lpwfs
->download_in_progress
->session_deleted
= TRUE
;
2660 if (lpwfs
->sndSocket
!= -1)
2661 closesocket(lpwfs
->sndSocket
);
2663 if (lpwfs
->lstnSocket
!= -1)
2664 closesocket(lpwfs
->lstnSocket
);
2666 HeapFree(GetProcessHeap(), 0, lpwfs
->lpszPassword
);
2667 HeapFree(GetProcessHeap(), 0, lpwfs
->lpszUserName
);
2668 HeapFree(GetProcessHeap(), 0, lpwfs
);
2672 /***********************************************************************
2673 * FTP_CloseFindNextHandle (internal)
2675 * Deallocate session handle
2682 static void FTP_CloseFindNextHandle(LPWININETHANDLEHEADER hdr
)
2684 LPWININETFINDNEXTW lpwfn
= (LPWININETFINDNEXTW
) hdr
;
2689 for (i
= 0; i
< lpwfn
->size
; i
++)
2691 HeapFree(GetProcessHeap(), 0, lpwfn
->lpafp
[i
].lpszName
);
2694 HeapFree(GetProcessHeap(), 0, lpwfn
->lpafp
);
2695 HeapFree(GetProcessHeap(), 0, lpwfn
);
2698 /***********************************************************************
2699 * FTP_CloseFileTransferHandle (internal)
2701 * Closes the file transfer handle. This also 'cleans' the data queue of
2702 * the 'transfer conplete' message (this is a bit of a hack though :-/ )
2705 static void FTP_CloseFileTransferHandle(LPWININETHANDLEHEADER hdr
)
2707 LPWININETFILE lpwh
= (LPWININETFILE
) hdr
;
2708 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) lpwh
->hdr
.lpwhparent
;
2713 if (!lpwh
->session_deleted
)
2714 lpwfs
->download_in_progress
= NULL
;
2716 /* This just serves to flush the control socket of any spurrious lines written
2717 to it (like '226 Transfer complete.').
2719 Wonder what to do if the server sends us an error code though...
2721 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2723 if (lpwh
->nDataSocket
!= -1)
2724 closesocket(lpwh
->nDataSocket
);
2726 HeapFree(GetProcessHeap(), 0, lpwh
);
2729 /***********************************************************************
2730 * FTP_ReceiveFileList (internal)
2732 * Read file list from server
2735 * Handle to file list on success
2739 static HINTERNET
FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
2740 LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwContext
)
2743 LPFILEPROPERTIESW lpafp
= NULL
;
2744 LPWININETFINDNEXTW lpwfn
= NULL
;
2745 HINTERNET handle
= 0;
2747 TRACE("(%p,%d,%s,%p,%ld)\n", lpwfs
, nSocket
, debugstr_w(lpszSearchFile
), lpFindFileData
, dwContext
);
2749 if (FTP_ParseDirectory(lpwfs
, nSocket
, lpszSearchFile
, &lpafp
, &dwSize
))
2752 FTP_ConvertFileProp(lpafp
, lpFindFileData
);
2754 lpwfn
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETFINDNEXTW
));
2757 lpwfn
->hdr
.htype
= WH_HFINDNEXT
;
2758 lpwfn
->hdr
.lpwhparent
= WININET_AddRef( &lpwfs
->hdr
);
2759 lpwfn
->hdr
.dwContext
= dwContext
;
2760 lpwfn
->hdr
.dwRefCount
= 1;
2761 lpwfn
->hdr
.destroy
= FTP_CloseFindNextHandle
;
2762 lpwfn
->hdr
.lpfnStatusCB
= lpwfs
->hdr
.lpfnStatusCB
;
2763 lpwfn
->index
= 1; /* Next index is 1 since we return index 0 */
2764 lpwfn
->size
= dwSize
;
2765 lpwfn
->lpafp
= lpafp
;
2767 handle
= WININET_AllocHandle( &lpwfn
->hdr
);
2772 WININET_Release( &lpwfn
->hdr
);
2774 TRACE("Matched %ld files\n", dwSize
);
2779 /***********************************************************************
2780 * FTP_ConvertFileProp (internal)
2782 * Converts FILEPROPERTIESW struct to WIN32_FIND_DATAA
2789 BOOL
FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp
, LPWIN32_FIND_DATAW lpFindFileData
)
2791 BOOL bSuccess
= FALSE
;
2793 ZeroMemory(lpFindFileData
, sizeof(WIN32_FIND_DATAW
));
2797 /* Convert 'Unix' time to Windows time */
2798 RtlSecondsSince1970ToTime(mktime(&lpafp
->tmLastModified
),
2799 (LARGE_INTEGER
*) &(lpFindFileData
->ftLastAccessTime
));
2800 lpFindFileData
->ftLastWriteTime
= lpFindFileData
->ftLastAccessTime
;
2801 lpFindFileData
->ftCreationTime
= lpFindFileData
->ftLastAccessTime
;
2803 /* Not all fields are filled in */
2804 lpFindFileData
->nFileSizeHigh
= 0; /* We do not handle files bigger than 0xFFFFFFFF bytes yet :-) */
2805 lpFindFileData
->nFileSizeLow
= lpafp
->nSize
;
2807 if (lpafp
->bIsDirectory
)
2808 lpFindFileData
->dwFileAttributes
|= FILE_ATTRIBUTE_DIRECTORY
;
2810 if (lpafp
->lpszName
)
2811 lstrcpynW(lpFindFileData
->cFileName
, lpafp
->lpszName
, MAX_PATH
);
2819 /***********************************************************************
2820 * FTP_ParseNextFile (internal)
2822 * Parse the next line in file listing
2828 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW lpfp
)
2830 static const char szSpace
[] = " \t";
2838 lpfp
->lpszName
= NULL
;
2840 if(!(pszLine
= INTERNET_GetNextLine(nSocket
, &nBufLen
)))
2843 pszToken
= strtok(pszLine
, szSpace
);
2845 * <Permissions> <NoLinks> <owner> <group> <size> <date> <time or year> <filename>
2848 * drwx--s--- 2 pcarrier ens 512 Sep 28 1995 pcarrier
2850 if(!isdigit(pszToken
[0]) && 10 == strlen(pszToken
)) {
2851 if(!FTP_ParsePermission(pszToken
, lpfp
))
2852 lpfp
->bIsDirectory
= FALSE
;
2853 for(i
=0; i
<=3; i
++) {
2854 if(!(pszToken
= strtok(NULL
, szSpace
)))
2857 if(!pszToken
) continue;
2858 if(lpfp
->bIsDirectory
) {
2859 TRACE("Is directory\n");
2863 TRACE("Size: %s\n", pszToken
);
2864 lpfp
->nSize
= atol(pszToken
);
2867 lpfp
->tmLastModified
.tm_sec
= 0;
2868 lpfp
->tmLastModified
.tm_min
= 0;
2869 lpfp
->tmLastModified
.tm_hour
= 0;
2870 lpfp
->tmLastModified
.tm_mday
= 0;
2871 lpfp
->tmLastModified
.tm_mon
= 0;
2872 lpfp
->tmLastModified
.tm_year
= 0;
2874 /* Determine month */
2875 pszToken
= strtok(NULL
, szSpace
);
2876 if(!pszToken
) continue;
2877 if(strlen(pszToken
) >= 3) {
2879 if((pszTmp
= StrStrIA(szMonths
, pszToken
)))
2880 lpfp
->tmLastModified
.tm_mon
= ((pszTmp
- szMonths
) / 3)+1;
2883 pszToken
= strtok(NULL
, szSpace
);
2884 if(!pszToken
) continue;
2885 lpfp
->tmLastModified
.tm_mday
= atoi(pszToken
);
2886 /* Determine time or year */
2887 pszToken
= strtok(NULL
, szSpace
);
2888 if(!pszToken
) continue;
2889 if((pszTmp
= strchr(pszToken
, ':'))) {
2894 lpfp
->tmLastModified
.tm_min
= atoi(pszTmp
);
2895 lpfp
->tmLastModified
.tm_hour
= atoi(pszToken
);
2897 apTM
= localtime(&aTime
);
2898 lpfp
->tmLastModified
.tm_year
= apTM
->tm_year
;
2901 lpfp
->tmLastModified
.tm_year
= atoi(pszToken
) - 1900;
2902 lpfp
->tmLastModified
.tm_hour
= 12;
2904 TRACE("Mod time: %02d:%02d:%02d %02d/%02d/%02d\n",
2905 lpfp
->tmLastModified
.tm_hour
, lpfp
->tmLastModified
.tm_min
, lpfp
->tmLastModified
.tm_sec
,
2906 (lpfp
->tmLastModified
.tm_year
>= 100) ? lpfp
->tmLastModified
.tm_year
- 100 : lpfp
->tmLastModified
.tm_year
,
2907 lpfp
->tmLastModified
.tm_mon
, lpfp
->tmLastModified
.tm_mday
);
2909 pszToken
= strtok(NULL
, szSpace
);
2910 if(!pszToken
) continue;
2911 lpfp
->lpszName
= WININET_strdup_AtoW(pszToken
);
2912 TRACE("File: %s\n", debugstr_w(lpfp
->lpszName
));
2914 /* NT way of parsing ... :
2916 07-13-03 08:55PM <DIR> sakpatch
2917 05-09-03 06:02PM 12656686 2003-04-21bgm_cmd_e.rgz
2919 else if(isdigit(pszToken
[0]) && 8 == strlen(pszToken
)) {
2920 lpfp
->permissions
= 0xFFFF; /* No idea, put full permission :-) */
2922 sscanf(pszToken
, "%d-%d-%d",
2923 &lpfp
->tmLastModified
.tm_mon
,
2924 &lpfp
->tmLastModified
.tm_mday
,
2925 &lpfp
->tmLastModified
.tm_year
);
2927 /* Hacky and bad Y2K protection :-) */
2928 if (lpfp
->tmLastModified
.tm_year
< 70)
2929 lpfp
->tmLastModified
.tm_year
+= 100;
2931 pszToken
= strtok(NULL
, szSpace
);
2932 if(!pszToken
) continue;
2933 sscanf(pszToken
, "%d:%d",
2934 &lpfp
->tmLastModified
.tm_hour
,
2935 &lpfp
->tmLastModified
.tm_min
);
2936 if((pszToken
[5] == 'P') && (pszToken
[6] == 'M')) {
2937 lpfp
->tmLastModified
.tm_hour
+= 12;
2939 lpfp
->tmLastModified
.tm_sec
= 0;
2941 TRACE("Mod time: %02d:%02d:%02d %02d/%02d/%02d\n",
2942 lpfp
->tmLastModified
.tm_hour
, lpfp
->tmLastModified
.tm_min
, lpfp
->tmLastModified
.tm_sec
,
2943 (lpfp
->tmLastModified
.tm_year
>= 100) ? lpfp
->tmLastModified
.tm_year
- 100 : lpfp
->tmLastModified
.tm_year
,
2944 lpfp
->tmLastModified
.tm_mon
, lpfp
->tmLastModified
.tm_mday
);
2946 pszToken
= strtok(NULL
, szSpace
);
2947 if(!pszToken
) continue;
2948 if(!strcasecmp(pszToken
, "<DIR>")) {
2949 lpfp
->bIsDirectory
= TRUE
;
2951 TRACE("Is directory\n");
2954 lpfp
->bIsDirectory
= FALSE
;
2955 lpfp
->nSize
= atol(pszToken
);
2956 TRACE("Size: %ld\n", lpfp
->nSize
);
2959 pszToken
= strtok(NULL
, szSpace
);
2960 if(!pszToken
) continue;
2961 lpfp
->lpszName
= WININET_strdup_AtoW(pszToken
);
2962 TRACE("Name: %s\n", debugstr_w(lpfp
->lpszName
));
2964 /* EPLF format - http://cr.yp.to/ftp/list/eplf.html */
2965 else if(pszToken
[0] == '+') {
2966 FIXME("EPLF Format not implemented\n");
2969 if(lpfp
->lpszName
) {
2970 if((lpszSearchFile
== NULL
) ||
2971 (PathMatchSpecW(lpfp
->lpszName
, lpszSearchFile
))) {
2973 TRACE("Matched: %s\n", debugstr_w(lpfp
->lpszName
));
2976 HeapFree(GetProcessHeap(), 0, lpfp
->lpszName
);
2977 lpfp
->lpszName
= NULL
;
2984 /***********************************************************************
2985 * FTP_ParseDirectory (internal)
2987 * Parse string of directory information
2993 static BOOL
FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
2994 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
)
2996 BOOL bSuccess
= TRUE
;
2997 INT sizeFilePropArray
= 500;/*20; */
2998 INT indexFilePropArray
= -1;
3002 /* Allocate intial file properties array */
3003 *lpafp
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(FILEPROPERTIESW
)*(sizeFilePropArray
));
3008 if (indexFilePropArray
+1 >= sizeFilePropArray
)
3010 LPFILEPROPERTIESW tmpafp
;
3012 sizeFilePropArray
*= 2;
3013 tmpafp
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *lpafp
,
3014 sizeof(FILEPROPERTIESW
)*sizeFilePropArray
);
3023 indexFilePropArray
++;
3024 } while (FTP_ParseNextFile(nSocket
, lpszSearchFile
, &(*lpafp
)[indexFilePropArray
]));
3026 if (bSuccess
&& indexFilePropArray
)
3028 if (indexFilePropArray
< sizeFilePropArray
- 1)
3030 LPFILEPROPERTIESW tmpafp
;
3032 tmpafp
= HeapReAlloc(GetProcessHeap(), 0, *lpafp
,
3033 sizeof(FILEPROPERTIESW
)*indexFilePropArray
);
3037 *dwfp
= indexFilePropArray
;
3041 HeapFree(GetProcessHeap(), 0, *lpafp
);
3042 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
3050 /***********************************************************************
3051 * FTP_ParsePermission (internal)
3053 * Parse permission string of directory information
3060 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
)
3062 BOOL bSuccess
= TRUE
;
3063 unsigned short nPermission
= 0;
3068 if ((*lpszPermission
!= 'd') && (*lpszPermission
!= '-') && (*lpszPermission
!= 'l'))
3074 lpfp
->bIsDirectory
= (*lpszPermission
== 'd');
3080 nPermission
|= (*(lpszPermission
+1) == 'r' ? 1 : 0) << 8;
3083 nPermission
|= (*(lpszPermission
+2) == 'w' ? 1 : 0) << 7;
3086 nPermission
|= (*(lpszPermission
+3) == 'x' ? 1 : 0) << 6;
3089 nPermission
|= (*(lpszPermission
+4) == 'r' ? 1 : 0) << 5;
3092 nPermission
|= (*(lpszPermission
+5) == 'w' ? 1 : 0) << 4;
3095 nPermission
|= (*(lpszPermission
+6) == 'x' ? 1 : 0) << 3;
3098 nPermission
|= (*(lpszPermission
+7) == 'r' ? 1 : 0) << 2;
3101 nPermission
|= (*(lpszPermission
+8) == 'w' ? 1 : 0) << 1;
3104 nPermission
|= (*(lpszPermission
+9) == 'x' ? 1 : 0);
3108 }while (nPos
<= nLast
);
3110 lpfp
->permissions
= nPermission
;
3115 /***********************************************************************
3116 * FTP_SetResponseError (internal)
3118 * Set the appropriate error code for a given response from the server
3123 static DWORD
FTP_SetResponseError(DWORD dwResponse
)
3129 case 421: /* Service not available - Server may be shutting down. */
3130 dwCode
= ERROR_INTERNET_TIMEOUT
;
3133 case 425: /* Cannot open data connection. */
3134 dwCode
= ERROR_INTERNET_CANNOT_CONNECT
;
3137 case 426: /* Connection closed, transer aborted. */
3138 dwCode
= ERROR_INTERNET_CONNECTION_ABORTED
;
3141 case 500: /* Syntax error. Command unrecognized. */
3142 case 501: /* Syntax error. Error in parameters or arguments. */
3143 dwCode
= ERROR_INTERNET_INCORRECT_FORMAT
;
3146 case 530: /* Not logged in. Login incorrect. */
3147 dwCode
= ERROR_INTERNET_LOGIN_FAILURE
;
3150 case 550: /* File action not taken. File not found or no access. */
3151 dwCode
= ERROR_INTERNET_ITEM_NOT_FOUND
;
3154 case 450: /* File action not taken. File may be busy. */
3155 case 451: /* Action aborted. Server error. */
3156 case 452: /* Action not taken. Insufficient storage space on server. */
3157 case 502: /* Command not implemented. */
3158 case 503: /* Bad sequence of command. */
3159 case 504: /* Command not implemented for that parameter. */
3160 case 532: /* Need account for storing files */
3161 case 551: /* Requested action aborted. Page type unknown */
3162 case 552: /* Action aborted. Exceeded storage allocation */
3163 case 553: /* Action not taken. File name not allowed. */
3166 dwCode
= ERROR_INTERNET_INTERNAL_ERROR
;
3170 INTERNET_SetLastError(dwCode
);