2 * WININET - Ftp implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2004 Mike McCormack for CodeWeavers
6 * Copyright 2004 Kevin Koltzau
7 * Copyright 2007 Hans Leidekker
12 * Copyright 2000 Andreas Mohr
13 * Copyright 2002 Jaco Greeff
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wine/port.h"
38 #include <sys/types.h>
39 #ifdef HAVE_SYS_SOCKET_H
40 # include <sys/socket.h>
59 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
64 #define DATA_PACKET_SIZE 0x2000
68 /* Testing shows that Windows only accepts dwFlags where the last
69 * 3 (yes 3) bits define FTP_TRANSFER_TYPE_UNKNOWN, FTP_TRANSFER_TYPE_ASCII or FTP_TRANSFER_TYPE_BINARY.
71 #define FTP_CONDITION_MASK 0x0007
74 /* FTP commands with arguments. */
90 /* FTP commands without arguments. */
99 static const CHAR *const szFtpCommands[] = {
122 static const CHAR szMonths[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
123 static const WCHAR szNoAccount[] = {'n','o','a','c','c','o','u','n','t','\0'};
125 static void FTP_CloseFileTransferHandle(LPWININETHANDLEHEADER hdr);
126 static void FTP_CloseSessionHandle(LPWININETHANDLEHEADER hdr);
127 static void FTP_CloseConnection(LPWININETHANDLEHEADER hdr);
128 static void FTP_CloseFindNextHandle(LPWININETHANDLEHEADER hdr);
129 static BOOL FTP_SendCommand(INT nSocket, FTP_COMMAND ftpCmd, LPCWSTR lpszParam,
130 INTERNET_STATUS_CALLBACK lpfnStatusCB, LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext);
131 static BOOL FTP_SendStore(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType);
132 static BOOL FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs, LPINT nDataSocket);
133 static BOOL FTP_SendData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile);
134 static INT FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs, DWORD_PTR dwContext);
135 static BOOL FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType);
136 static BOOL FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile);
137 static BOOL FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs);
138 static BOOL FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs);
139 static BOOL FTP_SendPassword(LPWININETFTPSESSIONW lpwfs);
140 static BOOL FTP_SendAccount(LPWININETFTPSESSIONW lpwfs);
141 static BOOL FTP_SendType(LPWININETFTPSESSIONW lpwfs, DWORD dwType);
142 static BOOL FTP_SendPort(LPWININETFTPSESSIONW lpwfs);
143 static BOOL FTP_DoPassive(LPWININETFTPSESSIONW lpwfs);
144 static BOOL FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs);
145 static BOOL FTP_ParsePermission(LPCSTR lpszPermission, LPFILEPROPERTIESW lpfp);
146 static BOOL FTP_ParseNextFile(INT nSocket, LPCWSTR lpszSearchFile, LPFILEPROPERTIESW fileprop);
147 static BOOL FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
148 LPFILEPROPERTIESW *lpafp, LPDWORD dwfp);
149 static HINTERNET FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
150 LPWIN32_FIND_DATAW lpFindFileData, DWORD_PTR dwContext);
151 static DWORD FTP_SetResponseError(DWORD dwResponse);
153 /***********************************************************************
154 * FtpPutFileA (WININET.@)
156 * Uploads a file to the FTP server
163 BOOL WINAPI FtpPutFileA(HINTERNET hConnect, LPCSTR lpszLocalFile,
164 LPCSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
166 LPWSTR lpwzLocalFile;
167 LPWSTR lpwzNewRemoteFile;
170 lpwzLocalFile = lpszLocalFile?WININET_strdup_AtoW(lpszLocalFile):NULL;
171 lpwzNewRemoteFile = lpszNewRemoteFile?WININET_strdup_AtoW(lpszNewRemoteFile):NULL;
172 ret = FtpPutFileW(hConnect, lpwzLocalFile, lpwzNewRemoteFile,
174 HeapFree(GetProcessHeap(), 0, lpwzLocalFile);
175 HeapFree(GetProcessHeap(), 0, lpwzNewRemoteFile);
179 static void AsyncFtpPutFileProc(WORKREQUEST *workRequest)
181 struct WORKREQ_FTPPUTFILEW const *req = &workRequest->u.FtpPutFileW;
182 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
184 TRACE("%p\n", lpwfs);
186 FTP_FtpPutFileW(lpwfs, req->lpszLocalFile,
187 req->lpszNewRemoteFile, req->dwFlags, req->dwContext);
189 HeapFree(GetProcessHeap(), 0, req->lpszLocalFile);
190 HeapFree(GetProcessHeap(), 0, req->lpszNewRemoteFile);
193 /***********************************************************************
194 * FtpPutFileW (WININET.@)
196 * Uploads a file to the FTP server
203 BOOL WINAPI FtpPutFileW(HINTERNET hConnect, LPCWSTR lpszLocalFile,
204 LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
206 LPWININETFTPSESSIONW lpwfs;
207 LPWININETAPPINFOW hIC = NULL;
210 if (!lpszLocalFile || !lpszNewRemoteFile)
212 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
216 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
219 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
223 if (WH_HFTPSESSION != lpwfs->hdr.htype)
225 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
229 if (lpwfs->download_in_progress != NULL)
231 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
235 if ((dwFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY)
237 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
241 hIC = lpwfs->lpAppInfo;
242 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
244 WORKREQUEST workRequest;
245 struct WORKREQ_FTPPUTFILEW *req = &workRequest.u.FtpPutFileW;
247 workRequest.asyncproc = AsyncFtpPutFileProc;
248 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
249 req->lpszLocalFile = WININET_strdupW(lpszLocalFile);
250 req->lpszNewRemoteFile = WININET_strdupW(lpszNewRemoteFile);
251 req->dwFlags = dwFlags;
252 req->dwContext = dwContext;
254 r = INTERNET_AsyncCall(&workRequest);
258 r = FTP_FtpPutFileW(lpwfs, lpszLocalFile,
259 lpszNewRemoteFile, dwFlags, dwContext);
263 WININET_Release( &lpwfs->hdr );
268 /***********************************************************************
269 * FTP_FtpPutFileW (Internal)
271 * Uploads a file to the FTP server
278 BOOL WINAPI FTP_FtpPutFileW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszLocalFile,
279 LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
282 BOOL bSuccess = FALSE;
283 LPWININETAPPINFOW hIC = NULL;
286 TRACE(" lpszLocalFile(%s) lpszNewRemoteFile(%s)\n", debugstr_w(lpszLocalFile), debugstr_w(lpszNewRemoteFile));
288 /* Clear any error information */
289 INTERNET_SetLastError(0);
291 /* Open file to be uploaded */
292 if (INVALID_HANDLE_VALUE ==
293 (hFile = CreateFileW(lpszLocalFile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0)))
294 /* Let CreateFile set the appropriate error */
297 hIC = lpwfs->lpAppInfo;
299 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
301 if (FTP_SendStore(lpwfs, lpszNewRemoteFile, dwFlags))
305 /* Get data socket to server */
306 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
308 FTP_SendData(lpwfs, nDataSocket, hFile);
309 closesocket(nDataSocket);
310 nResCode = FTP_ReceiveResponse(lpwfs, dwContext);
316 FTP_SetResponseError(nResCode);
321 if (lpwfs->lstnSocket != -1)
322 closesocket(lpwfs->lstnSocket);
324 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
326 INTERNET_ASYNC_RESULT iar;
328 iar.dwResult = (DWORD)bSuccess;
329 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
330 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
331 &iar, sizeof(INTERNET_ASYNC_RESULT));
340 /***********************************************************************
341 * FtpSetCurrentDirectoryA (WININET.@)
343 * Change the working directory on the FTP server
350 BOOL WINAPI FtpSetCurrentDirectoryA(HINTERNET hConnect, LPCSTR lpszDirectory)
352 LPWSTR lpwzDirectory;
355 lpwzDirectory = lpszDirectory?WININET_strdup_AtoW(lpszDirectory):NULL;
356 ret = FtpSetCurrentDirectoryW(hConnect, lpwzDirectory);
357 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
362 static void AsyncFtpSetCurrentDirectoryProc(WORKREQUEST *workRequest)
364 struct WORKREQ_FTPSETCURRENTDIRECTORYW const *req = &workRequest->u.FtpSetCurrentDirectoryW;
365 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
367 TRACE("%p\n", lpwfs);
369 FTP_FtpSetCurrentDirectoryW(lpwfs, req->lpszDirectory);
370 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
373 /***********************************************************************
374 * FtpSetCurrentDirectoryW (WININET.@)
376 * Change the working directory on the FTP server
383 BOOL WINAPI FtpSetCurrentDirectoryW(HINTERNET hConnect, LPCWSTR lpszDirectory)
385 LPWININETFTPSESSIONW lpwfs = NULL;
386 LPWININETAPPINFOW hIC = NULL;
391 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
395 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
396 if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
398 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
402 if (lpwfs->download_in_progress != NULL)
404 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
408 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
410 hIC = lpwfs->lpAppInfo;
411 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
413 WORKREQUEST workRequest;
414 struct WORKREQ_FTPSETCURRENTDIRECTORYW *req;
416 workRequest.asyncproc = AsyncFtpSetCurrentDirectoryProc;
417 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
418 req = &workRequest.u.FtpSetCurrentDirectoryW;
419 req->lpszDirectory = WININET_strdupW(lpszDirectory);
421 r = INTERNET_AsyncCall(&workRequest);
425 r = FTP_FtpSetCurrentDirectoryW(lpwfs, lpszDirectory);
430 WININET_Release( &lpwfs->hdr );
436 /***********************************************************************
437 * FTP_FtpSetCurrentDirectoryW (Internal)
439 * Change the working directory on the FTP server
446 BOOL WINAPI FTP_FtpSetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory)
449 LPWININETAPPINFOW hIC = NULL;
450 DWORD bSuccess = FALSE;
452 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
454 /* Clear any error information */
455 INTERNET_SetLastError(0);
457 hIC = lpwfs->lpAppInfo;
458 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_CWD, lpszDirectory,
459 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
462 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
469 FTP_SetResponseError(nResCode);
473 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
475 INTERNET_ASYNC_RESULT iar;
477 iar.dwResult = (DWORD)bSuccess;
478 iar.dwError = bSuccess ? ERROR_SUCCESS : ERROR_INTERNET_EXTENDED_ERROR;
479 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
480 &iar, sizeof(INTERNET_ASYNC_RESULT));
486 /***********************************************************************
487 * FtpCreateDirectoryA (WININET.@)
489 * Create new directory on the FTP server
496 BOOL WINAPI FtpCreateDirectoryA(HINTERNET hConnect, LPCSTR lpszDirectory)
498 LPWSTR lpwzDirectory;
501 lpwzDirectory = lpszDirectory?WININET_strdup_AtoW(lpszDirectory):NULL;
502 ret = FtpCreateDirectoryW(hConnect, lpwzDirectory);
503 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
508 static void AsyncFtpCreateDirectoryProc(WORKREQUEST *workRequest)
510 struct WORKREQ_FTPCREATEDIRECTORYW const *req = &workRequest->u.FtpCreateDirectoryW;
511 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
513 TRACE(" %p\n", lpwfs);
515 FTP_FtpCreateDirectoryW(lpwfs, req->lpszDirectory);
516 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
519 /***********************************************************************
520 * FtpCreateDirectoryW (WININET.@)
522 * Create new directory on the FTP server
529 BOOL WINAPI FtpCreateDirectoryW(HINTERNET hConnect, LPCWSTR lpszDirectory)
531 LPWININETFTPSESSIONW lpwfs;
532 LPWININETAPPINFOW hIC = NULL;
535 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
538 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
542 if (WH_HFTPSESSION != lpwfs->hdr.htype)
544 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
548 if (lpwfs->download_in_progress != NULL)
550 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
556 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
560 hIC = lpwfs->lpAppInfo;
561 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
563 WORKREQUEST workRequest;
564 struct WORKREQ_FTPCREATEDIRECTORYW *req;
566 workRequest.asyncproc = AsyncFtpCreateDirectoryProc;
567 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
568 req = &workRequest.u.FtpCreateDirectoryW;
569 req->lpszDirectory = WININET_strdupW(lpszDirectory);
571 r = INTERNET_AsyncCall(&workRequest);
575 r = FTP_FtpCreateDirectoryW(lpwfs, lpszDirectory);
578 WININET_Release( &lpwfs->hdr );
584 /***********************************************************************
585 * FTP_FtpCreateDirectoryW (Internal)
587 * Create new directory on the FTP server
594 BOOL WINAPI FTP_FtpCreateDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory)
597 BOOL bSuccess = FALSE;
598 LPWININETAPPINFOW hIC = NULL;
600 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
602 /* Clear any error information */
603 INTERNET_SetLastError(0);
605 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_MKD, lpszDirectory, 0, 0, 0))
608 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
614 FTP_SetResponseError(nResCode);
618 hIC = lpwfs->lpAppInfo;
619 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
621 INTERNET_ASYNC_RESULT iar;
623 iar.dwResult = (DWORD)bSuccess;
624 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
625 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
626 &iar, sizeof(INTERNET_ASYNC_RESULT));
632 /***********************************************************************
633 * FtpFindFirstFileA (WININET.@)
635 * Search the specified directory
638 * HINTERNET on success
642 HINTERNET WINAPI FtpFindFirstFileA(HINTERNET hConnect,
643 LPCSTR lpszSearchFile, LPWIN32_FIND_DATAA lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
645 LPWSTR lpwzSearchFile;
646 WIN32_FIND_DATAW wfd;
647 LPWIN32_FIND_DATAW lpFindFileDataW;
650 lpwzSearchFile = lpszSearchFile?WININET_strdup_AtoW(lpszSearchFile):NULL;
651 lpFindFileDataW = lpFindFileData?&wfd:NULL;
652 ret = FtpFindFirstFileW(hConnect, lpwzSearchFile, lpFindFileDataW, dwFlags, dwContext);
653 HeapFree(GetProcessHeap(), 0, lpwzSearchFile);
656 WININET_find_data_WtoA(lpFindFileDataW, lpFindFileData);
662 static void AsyncFtpFindFirstFileProc(WORKREQUEST *workRequest)
664 struct WORKREQ_FTPFINDFIRSTFILEW const *req = &workRequest->u.FtpFindFirstFileW;
665 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
667 TRACE("%p\n", lpwfs);
669 FTP_FtpFindFirstFileW(lpwfs, req->lpszSearchFile,
670 req->lpFindFileData, req->dwFlags, req->dwContext);
671 HeapFree(GetProcessHeap(), 0, req->lpszSearchFile);
674 /***********************************************************************
675 * FtpFindFirstFileW (WININET.@)
677 * Search the specified directory
680 * HINTERNET on success
684 HINTERNET WINAPI FtpFindFirstFileW(HINTERNET hConnect,
685 LPCWSTR lpszSearchFile, LPWIN32_FIND_DATAW lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
687 LPWININETFTPSESSIONW lpwfs;
688 LPWININETAPPINFOW hIC = NULL;
691 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
692 if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
694 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
698 if (lpwfs->download_in_progress != NULL)
700 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
704 hIC = lpwfs->lpAppInfo;
705 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
707 WORKREQUEST workRequest;
708 struct WORKREQ_FTPFINDFIRSTFILEW *req;
710 workRequest.asyncproc = AsyncFtpFindFirstFileProc;
711 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
712 req = &workRequest.u.FtpFindFirstFileW;
713 req->lpszSearchFile = (lpszSearchFile == NULL) ? NULL : WININET_strdupW(lpszSearchFile);
714 req->lpFindFileData = lpFindFileData;
715 req->dwFlags = dwFlags;
716 req->dwContext= dwContext;
718 INTERNET_AsyncCall(&workRequest);
723 r = FTP_FtpFindFirstFileW(lpwfs, lpszSearchFile, lpFindFileData,
728 WININET_Release( &lpwfs->hdr );
734 /***********************************************************************
735 * FTP_FtpFindFirstFileW (Internal)
737 * Search the specified directory
740 * HINTERNET on success
744 HINTERNET WINAPI FTP_FtpFindFirstFileW(LPWININETFTPSESSIONW lpwfs,
745 LPCWSTR lpszSearchFile, LPWIN32_FIND_DATAW lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
748 LPWININETAPPINFOW hIC = NULL;
749 HINTERNET hFindNext = NULL;
753 /* Clear any error information */
754 INTERNET_SetLastError(0);
756 if (!FTP_InitListenSocket(lpwfs))
759 if (!FTP_SendType(lpwfs, INTERNET_FLAG_TRANSFER_ASCII))
762 if (!FTP_SendPortOrPasv(lpwfs))
765 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_LIST, NULL,
766 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
769 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
772 if (nResCode == 125 || nResCode == 150)
776 /* Get data socket to server */
777 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
779 hFindNext = FTP_ReceiveFileList(lpwfs, nDataSocket, lpszSearchFile, lpFindFileData, dwContext);
780 closesocket(nDataSocket);
781 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
782 if (nResCode != 226 && nResCode != 250)
783 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
787 FTP_SetResponseError(nResCode);
791 if (lpwfs->lstnSocket != -1)
792 closesocket(lpwfs->lstnSocket);
794 hIC = lpwfs->lpAppInfo;
795 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
797 INTERNET_ASYNC_RESULT iar;
801 iar.dwResult = (DWORD)hFindNext;
802 iar.dwError = ERROR_SUCCESS;
803 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_HANDLE_CREATED,
804 &iar, sizeof(INTERNET_ASYNC_RESULT));
807 iar.dwResult = (DWORD)hFindNext;
808 iar.dwError = hFindNext ? ERROR_SUCCESS : INTERNET_GetLastError();
809 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
810 &iar, sizeof(INTERNET_ASYNC_RESULT));
817 /***********************************************************************
818 * FtpGetCurrentDirectoryA (WININET.@)
820 * Retrieves the current directory
827 BOOL WINAPI FtpGetCurrentDirectoryA(HINTERNET hFtpSession, LPSTR lpszCurrentDirectory,
828 LPDWORD lpdwCurrentDirectory)
834 if(lpdwCurrentDirectory) {
835 len = *lpdwCurrentDirectory;
836 if(lpszCurrentDirectory)
838 dir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
841 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
846 ret = FtpGetCurrentDirectoryW(hFtpSession, lpszCurrentDirectory?dir:NULL, lpdwCurrentDirectory?&len:NULL);
847 if(lpdwCurrentDirectory) {
848 *lpdwCurrentDirectory = len;
849 if(lpszCurrentDirectory) {
850 WideCharToMultiByte(CP_ACP, 0, dir, len, lpszCurrentDirectory, *lpdwCurrentDirectory, NULL, NULL);
851 HeapFree(GetProcessHeap(), 0, dir);
858 static void AsyncFtpGetCurrentDirectoryProc(WORKREQUEST *workRequest)
860 struct WORKREQ_FTPGETCURRENTDIRECTORYW const *req = &workRequest->u.FtpGetCurrentDirectoryW;
861 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
863 TRACE("%p\n", lpwfs);
865 FTP_FtpGetCurrentDirectoryW(lpwfs, req->lpszDirectory, req->lpdwDirectory);
868 /***********************************************************************
869 * FtpGetCurrentDirectoryW (WININET.@)
871 * Retrieves the current directory
878 BOOL WINAPI FtpGetCurrentDirectoryW(HINTERNET hFtpSession, LPWSTR lpszCurrentDirectory,
879 LPDWORD lpdwCurrentDirectory)
881 LPWININETFTPSESSIONW lpwfs;
882 LPWININETAPPINFOW hIC = NULL;
885 TRACE("len(%d)\n", *lpdwCurrentDirectory);
887 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
890 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
894 if (WH_HFTPSESSION != lpwfs->hdr.htype)
896 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
900 if (!lpdwCurrentDirectory)
902 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
906 if (lpszCurrentDirectory == NULL)
908 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
912 if (lpwfs->download_in_progress != NULL)
914 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
918 hIC = lpwfs->lpAppInfo;
919 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
921 WORKREQUEST workRequest;
922 struct WORKREQ_FTPGETCURRENTDIRECTORYW *req;
924 workRequest.asyncproc = AsyncFtpGetCurrentDirectoryProc;
925 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
926 req = &workRequest.u.FtpGetCurrentDirectoryW;
927 req->lpszDirectory = lpszCurrentDirectory;
928 req->lpdwDirectory = lpdwCurrentDirectory;
930 r = INTERNET_AsyncCall(&workRequest);
934 r = FTP_FtpGetCurrentDirectoryW(lpwfs, lpszCurrentDirectory,
935 lpdwCurrentDirectory);
940 WININET_Release( &lpwfs->hdr );
946 /***********************************************************************
947 * FTP_FtpGetCurrentDirectoryW (Internal)
949 * Retrieves the current directory
956 BOOL WINAPI FTP_FtpGetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs, LPWSTR lpszCurrentDirectory,
957 LPDWORD lpdwCurrentDirectory)
960 LPWININETAPPINFOW hIC = NULL;
961 DWORD bSuccess = FALSE;
963 TRACE("len(%d)\n", *lpdwCurrentDirectory);
965 /* Clear any error information */
966 INTERNET_SetLastError(0);
968 ZeroMemory(lpszCurrentDirectory, *lpdwCurrentDirectory);
970 hIC = lpwfs->lpAppInfo;
971 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PWD, NULL,
972 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
975 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
978 if (nResCode == 257) /* Extract directory name */
980 DWORD firstpos, lastpos, len;
981 LPWSTR lpszResponseBuffer = WININET_strdup_AtoW(INTERNET_GetResponseBuffer());
983 for (firstpos = 0, lastpos = 0; lpszResponseBuffer[lastpos]; lastpos++)
985 if ('"' == lpszResponseBuffer[lastpos])
994 len = lastpos - firstpos - 1;
995 lstrcpynW(lpszCurrentDirectory, &lpszResponseBuffer[firstpos+1], *lpdwCurrentDirectory);
996 HeapFree(GetProcessHeap(), 0, lpszResponseBuffer);
997 *lpdwCurrentDirectory = len;
1001 FTP_SetResponseError(nResCode);
1005 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1007 INTERNET_ASYNC_RESULT iar;
1009 iar.dwResult = (DWORD)bSuccess;
1010 iar.dwError = bSuccess ? ERROR_SUCCESS : ERROR_INTERNET_EXTENDED_ERROR;
1011 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1012 &iar, sizeof(INTERNET_ASYNC_RESULT));
1015 return (DWORD) bSuccess;
1018 /***********************************************************************
1019 * FtpOpenFileA (WININET.@)
1021 * Open a remote file for writing or reading
1024 * HINTERNET handle on success
1028 HINTERNET WINAPI FtpOpenFileA(HINTERNET hFtpSession,
1029 LPCSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1030 DWORD_PTR dwContext)
1032 LPWSTR lpwzFileName;
1035 lpwzFileName = lpszFileName?WININET_strdup_AtoW(lpszFileName):NULL;
1036 ret = FtpOpenFileW(hFtpSession, lpwzFileName, fdwAccess, dwFlags, dwContext);
1037 HeapFree(GetProcessHeap(), 0, lpwzFileName);
1042 static void AsyncFtpOpenFileProc(WORKREQUEST *workRequest)
1044 struct WORKREQ_FTPOPENFILEW const *req = &workRequest->u.FtpOpenFileW;
1045 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1047 TRACE("%p\n", lpwfs);
1049 FTP_FtpOpenFileW(lpwfs, req->lpszFilename,
1050 req->dwAccess, req->dwFlags, req->dwContext);
1051 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
1054 /***********************************************************************
1055 * FtpOpenFileW (WININET.@)
1057 * Open a remote file for writing or reading
1060 * HINTERNET handle on success
1064 HINTERNET WINAPI FtpOpenFileW(HINTERNET hFtpSession,
1065 LPCWSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1066 DWORD_PTR dwContext)
1068 LPWININETFTPSESSIONW lpwfs;
1069 LPWININETAPPINFOW hIC = NULL;
1072 TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession,
1073 debugstr_w(lpszFileName), fdwAccess, dwFlags, dwContext);
1075 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1078 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1082 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1084 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1088 if ((!lpszFileName) ||
1089 ((fdwAccess != GENERIC_READ) && (fdwAccess != GENERIC_WRITE)) ||
1090 ((dwFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY))
1092 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1096 if (lpwfs->download_in_progress != NULL)
1098 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1102 hIC = lpwfs->lpAppInfo;
1103 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1105 WORKREQUEST workRequest;
1106 struct WORKREQ_FTPOPENFILEW *req;
1108 workRequest.asyncproc = AsyncFtpOpenFileProc;
1109 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1110 req = &workRequest.u.FtpOpenFileW;
1111 req->lpszFilename = WININET_strdupW(lpszFileName);
1112 req->dwAccess = fdwAccess;
1113 req->dwFlags = dwFlags;
1114 req->dwContext = dwContext;
1116 INTERNET_AsyncCall(&workRequest);
1121 r = FTP_FtpOpenFileW(lpwfs, lpszFileName, fdwAccess, dwFlags, dwContext);
1125 WININET_Release( &lpwfs->hdr );
1131 /***********************************************************************
1132 * FTP_FtpOpenFileW (Internal)
1134 * Open a remote file for writing or reading
1137 * HINTERNET handle on success
1141 HINTERNET FTP_FtpOpenFileW(LPWININETFTPSESSIONW lpwfs,
1142 LPCWSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1143 DWORD_PTR dwContext)
1146 BOOL bSuccess = FALSE;
1147 LPWININETFTPFILE lpwh = NULL;
1148 LPWININETAPPINFOW hIC = NULL;
1149 HINTERNET handle = NULL;
1153 /* Clear any error information */
1154 INTERNET_SetLastError(0);
1156 if (GENERIC_READ == fdwAccess)
1158 /* Set up socket to retrieve data */
1159 bSuccess = FTP_SendRetrieve(lpwfs, lpszFileName, dwFlags);
1161 else if (GENERIC_WRITE == fdwAccess)
1163 /* Set up socket to send data */
1164 bSuccess = FTP_SendStore(lpwfs, lpszFileName, dwFlags);
1167 /* Get data socket to server */
1168 if (bSuccess && FTP_GetDataSocket(lpwfs, &nDataSocket))
1170 lpwh = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPFILE));
1171 lpwh->hdr.htype = WH_HFILE;
1172 lpwh->hdr.dwFlags = dwFlags;
1173 lpwh->hdr.dwContext = dwContext;
1174 lpwh->hdr.dwRefCount = 1;
1175 lpwh->hdr.close_connection = NULL;
1176 lpwh->hdr.destroy = FTP_CloseFileTransferHandle;
1177 lpwh->hdr.lpfnStatusCB = lpwfs->hdr.lpfnStatusCB;
1178 lpwh->nDataSocket = nDataSocket;
1179 lpwh->session_deleted = FALSE;
1181 WININET_AddRef( &lpwfs->hdr );
1182 lpwh->lpFtpSession = lpwfs;
1183 list_add_head( &lpwfs->hdr.children, &lpwh->hdr.entry );
1185 handle = WININET_AllocHandle( &lpwh->hdr );
1189 /* Indicate that a download is currently in progress */
1190 lpwfs->download_in_progress = lpwh;
1193 if (lpwfs->lstnSocket != -1)
1194 closesocket(lpwfs->lstnSocket);
1196 hIC = lpwfs->lpAppInfo;
1197 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1199 INTERNET_ASYNC_RESULT iar;
1203 iar.dwResult = (DWORD)handle;
1204 iar.dwError = ERROR_SUCCESS;
1205 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_HANDLE_CREATED,
1206 &iar, sizeof(INTERNET_ASYNC_RESULT));
1209 iar.dwResult = (DWORD)bSuccess;
1210 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1211 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1212 &iar, sizeof(INTERNET_ASYNC_RESULT));
1217 WININET_Release( &lpwh->hdr );
1223 /***********************************************************************
1224 * FtpGetFileA (WININET.@)
1226 * Retrieve file from the FTP server
1233 BOOL WINAPI FtpGetFileA(HINTERNET hInternet, LPCSTR lpszRemoteFile, LPCSTR lpszNewFile,
1234 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1235 DWORD_PTR dwContext)
1237 LPWSTR lpwzRemoteFile;
1241 lpwzRemoteFile = lpszRemoteFile?WININET_strdup_AtoW(lpszRemoteFile):NULL;
1242 lpwzNewFile = lpszNewFile?WININET_strdup_AtoW(lpszNewFile):NULL;
1243 ret = FtpGetFileW(hInternet, lpwzRemoteFile, lpwzNewFile, fFailIfExists,
1244 dwLocalFlagsAttribute, dwInternetFlags, dwContext);
1245 HeapFree(GetProcessHeap(), 0, lpwzRemoteFile);
1246 HeapFree(GetProcessHeap(), 0, lpwzNewFile);
1251 static void AsyncFtpGetFileProc(WORKREQUEST *workRequest)
1253 struct WORKREQ_FTPGETFILEW const *req = &workRequest->u.FtpGetFileW;
1254 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1256 TRACE("%p\n", lpwfs);
1258 FTP_FtpGetFileW(lpwfs, req->lpszRemoteFile,
1259 req->lpszNewFile, req->fFailIfExists,
1260 req->dwLocalFlagsAttribute, req->dwFlags, req->dwContext);
1261 HeapFree(GetProcessHeap(), 0, req->lpszRemoteFile);
1262 HeapFree(GetProcessHeap(), 0, req->lpszNewFile);
1266 /***********************************************************************
1267 * FtpGetFileW (WININET.@)
1269 * Retrieve file from the FTP server
1276 BOOL WINAPI FtpGetFileW(HINTERNET hInternet, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
1277 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1278 DWORD_PTR dwContext)
1280 LPWININETFTPSESSIONW lpwfs;
1281 LPWININETAPPINFOW hIC = NULL;
1284 if (!lpszRemoteFile || !lpszNewFile)
1286 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1290 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hInternet );
1293 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1297 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1299 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1303 if ((dwInternetFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY)
1305 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1309 if (lpwfs->download_in_progress != NULL)
1311 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1315 hIC = lpwfs->lpAppInfo;
1316 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1318 WORKREQUEST workRequest;
1319 struct WORKREQ_FTPGETFILEW *req;
1321 workRequest.asyncproc = AsyncFtpGetFileProc;
1322 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1323 req = &workRequest.u.FtpGetFileW;
1324 req->lpszRemoteFile = WININET_strdupW(lpszRemoteFile);
1325 req->lpszNewFile = WININET_strdupW(lpszNewFile);
1326 req->dwLocalFlagsAttribute = dwLocalFlagsAttribute;
1327 req->fFailIfExists = fFailIfExists;
1328 req->dwFlags = dwInternetFlags;
1329 req->dwContext = dwContext;
1331 r = INTERNET_AsyncCall(&workRequest);
1335 r = FTP_FtpGetFileW(lpwfs, lpszRemoteFile, lpszNewFile,
1336 fFailIfExists, dwLocalFlagsAttribute, dwInternetFlags, dwContext);
1340 WININET_Release( &lpwfs->hdr );
1346 /***********************************************************************
1347 * FTP_FtpGetFileW (Internal)
1349 * Retrieve file from the FTP server
1356 BOOL WINAPI FTP_FtpGetFileW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
1357 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1358 DWORD_PTR dwContext)
1360 BOOL bSuccess = FALSE;
1362 LPWININETAPPINFOW hIC = NULL;
1364 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile), debugstr_w(lpszNewFile));
1366 /* Clear any error information */
1367 INTERNET_SetLastError(0);
1369 /* Ensure we can write to lpszNewfile by opening it */
1370 hFile = CreateFileW(lpszNewFile, GENERIC_WRITE, 0, 0, fFailIfExists ?
1371 CREATE_NEW : CREATE_ALWAYS, dwLocalFlagsAttribute, 0);
1372 if (INVALID_HANDLE_VALUE == hFile)
1375 /* Set up socket to retrieve data */
1376 if (FTP_SendRetrieve(lpwfs, lpszRemoteFile, dwInternetFlags))
1380 /* Get data socket to server */
1381 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
1386 FTP_RetrieveFileData(lpwfs, nDataSocket, hFile);
1387 closesocket(nDataSocket);
1389 nResCode = FTP_ReceiveResponse(lpwfs, dwContext);
1392 if (nResCode == 226)
1395 FTP_SetResponseError(nResCode);
1400 if (lpwfs->lstnSocket != -1)
1401 closesocket(lpwfs->lstnSocket);
1405 hIC = lpwfs->lpAppInfo;
1406 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1408 INTERNET_ASYNC_RESULT iar;
1410 iar.dwResult = (DWORD)bSuccess;
1411 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1412 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1413 &iar, sizeof(INTERNET_ASYNC_RESULT));
1419 /***********************************************************************
1420 * FtpGetFileSize (WININET.@)
1422 DWORD WINAPI FtpGetFileSize( HINTERNET hFile, LPDWORD lpdwFileSizeHigh )
1424 FIXME("(%p, %p)\n", hFile, lpdwFileSizeHigh);
1426 if (lpdwFileSizeHigh)
1427 *lpdwFileSizeHigh = 0;
1432 /***********************************************************************
1433 * FtpDeleteFileA (WININET.@)
1435 * Delete a file on the ftp server
1442 BOOL WINAPI FtpDeleteFileA(HINTERNET hFtpSession, LPCSTR lpszFileName)
1444 LPWSTR lpwzFileName;
1447 lpwzFileName = lpszFileName?WININET_strdup_AtoW(lpszFileName):NULL;
1448 ret = FtpDeleteFileW(hFtpSession, lpwzFileName);
1449 HeapFree(GetProcessHeap(), 0, lpwzFileName);
1453 static void AsyncFtpDeleteFileProc(WORKREQUEST *workRequest)
1455 struct WORKREQ_FTPDELETEFILEW const *req = &workRequest->u.FtpDeleteFileW;
1456 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1458 TRACE("%p\n", lpwfs);
1460 FTP_FtpDeleteFileW(lpwfs, req->lpszFilename);
1461 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
1464 /***********************************************************************
1465 * FtpDeleteFileW (WININET.@)
1467 * Delete a file on the ftp server
1474 BOOL WINAPI FtpDeleteFileW(HINTERNET hFtpSession, LPCWSTR lpszFileName)
1476 LPWININETFTPSESSIONW lpwfs;
1477 LPWININETAPPINFOW hIC = NULL;
1480 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1483 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1487 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1489 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1493 if (lpwfs->download_in_progress != NULL)
1495 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1501 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1505 hIC = lpwfs->lpAppInfo;
1506 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1508 WORKREQUEST workRequest;
1509 struct WORKREQ_FTPDELETEFILEW *req;
1511 workRequest.asyncproc = AsyncFtpDeleteFileProc;
1512 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1513 req = &workRequest.u.FtpDeleteFileW;
1514 req->lpszFilename = WININET_strdupW(lpszFileName);
1516 r = INTERNET_AsyncCall(&workRequest);
1520 r = FTP_FtpDeleteFileW(lpwfs, lpszFileName);
1524 WININET_Release( &lpwfs->hdr );
1529 /***********************************************************************
1530 * FTP_FtpDeleteFileW (Internal)
1532 * Delete a file on the ftp server
1539 BOOL FTP_FtpDeleteFileW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszFileName)
1542 BOOL bSuccess = FALSE;
1543 LPWININETAPPINFOW hIC = NULL;
1545 TRACE("%p\n", lpwfs);
1547 /* Clear any error information */
1548 INTERNET_SetLastError(0);
1550 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_DELE, lpszFileName, 0, 0, 0))
1553 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1556 if (nResCode == 250)
1559 FTP_SetResponseError(nResCode);
1562 hIC = lpwfs->lpAppInfo;
1563 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1565 INTERNET_ASYNC_RESULT iar;
1567 iar.dwResult = (DWORD)bSuccess;
1568 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1569 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1570 &iar, sizeof(INTERNET_ASYNC_RESULT));
1577 /***********************************************************************
1578 * FtpRemoveDirectoryA (WININET.@)
1580 * Remove a directory on the ftp server
1587 BOOL WINAPI FtpRemoveDirectoryA(HINTERNET hFtpSession, LPCSTR lpszDirectory)
1589 LPWSTR lpwzDirectory;
1592 lpwzDirectory = lpszDirectory?WININET_strdup_AtoW(lpszDirectory):NULL;
1593 ret = FtpRemoveDirectoryW(hFtpSession, lpwzDirectory);
1594 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
1598 static void AsyncFtpRemoveDirectoryProc(WORKREQUEST *workRequest)
1600 struct WORKREQ_FTPREMOVEDIRECTORYW const *req = &workRequest->u.FtpRemoveDirectoryW;
1601 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1603 TRACE("%p\n", lpwfs);
1605 FTP_FtpRemoveDirectoryW(lpwfs, req->lpszDirectory);
1606 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
1609 /***********************************************************************
1610 * FtpRemoveDirectoryW (WININET.@)
1612 * Remove a directory on the ftp server
1619 BOOL WINAPI FtpRemoveDirectoryW(HINTERNET hFtpSession, LPCWSTR lpszDirectory)
1621 LPWININETFTPSESSIONW lpwfs;
1622 LPWININETAPPINFOW hIC = NULL;
1625 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1628 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1632 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1634 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1638 if (lpwfs->download_in_progress != NULL)
1640 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1646 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1650 hIC = lpwfs->lpAppInfo;
1651 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1653 WORKREQUEST workRequest;
1654 struct WORKREQ_FTPREMOVEDIRECTORYW *req;
1656 workRequest.asyncproc = AsyncFtpRemoveDirectoryProc;
1657 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1658 req = &workRequest.u.FtpRemoveDirectoryW;
1659 req->lpszDirectory = WININET_strdupW(lpszDirectory);
1661 r = INTERNET_AsyncCall(&workRequest);
1665 r = FTP_FtpRemoveDirectoryW(lpwfs, lpszDirectory);
1669 WININET_Release( &lpwfs->hdr );
1674 /***********************************************************************
1675 * FTP_FtpRemoveDirectoryW (Internal)
1677 * Remove a directory on the ftp server
1684 BOOL FTP_FtpRemoveDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory)
1687 BOOL bSuccess = FALSE;
1688 LPWININETAPPINFOW hIC = NULL;
1692 /* Clear any error information */
1693 INTERNET_SetLastError(0);
1695 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RMD, lpszDirectory, 0, 0, 0))
1698 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1701 if (nResCode == 250)
1704 FTP_SetResponseError(nResCode);
1708 hIC = lpwfs->lpAppInfo;
1709 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1711 INTERNET_ASYNC_RESULT iar;
1713 iar.dwResult = (DWORD)bSuccess;
1714 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1715 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1716 &iar, sizeof(INTERNET_ASYNC_RESULT));
1723 /***********************************************************************
1724 * FtpRenameFileA (WININET.@)
1726 * Rename a file on the ftp server
1733 BOOL WINAPI FtpRenameFileA(HINTERNET hFtpSession, LPCSTR lpszSrc, LPCSTR lpszDest)
1739 lpwzSrc = lpszSrc?WININET_strdup_AtoW(lpszSrc):NULL;
1740 lpwzDest = lpszDest?WININET_strdup_AtoW(lpszDest):NULL;
1741 ret = FtpRenameFileW(hFtpSession, lpwzSrc, lpwzDest);
1742 HeapFree(GetProcessHeap(), 0, lpwzSrc);
1743 HeapFree(GetProcessHeap(), 0, lpwzDest);
1747 static void AsyncFtpRenameFileProc(WORKREQUEST *workRequest)
1749 struct WORKREQ_FTPRENAMEFILEW const *req = &workRequest->u.FtpRenameFileW;
1750 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1752 TRACE("%p\n", lpwfs);
1754 FTP_FtpRenameFileW(lpwfs, req->lpszSrcFile, req->lpszDestFile);
1755 HeapFree(GetProcessHeap(), 0, req->lpszSrcFile);
1756 HeapFree(GetProcessHeap(), 0, req->lpszDestFile);
1759 /***********************************************************************
1760 * FtpRenameFileW (WININET.@)
1762 * Rename a file on the ftp server
1769 BOOL WINAPI FtpRenameFileW(HINTERNET hFtpSession, LPCWSTR lpszSrc, LPCWSTR lpszDest)
1771 LPWININETFTPSESSIONW lpwfs;
1772 LPWININETAPPINFOW hIC = NULL;
1775 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1778 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1782 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1784 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1788 if (lpwfs->download_in_progress != NULL)
1790 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1794 if (!lpszSrc || !lpszDest)
1796 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1800 hIC = lpwfs->lpAppInfo;
1801 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1803 WORKREQUEST workRequest;
1804 struct WORKREQ_FTPRENAMEFILEW *req;
1806 workRequest.asyncproc = AsyncFtpRenameFileProc;
1807 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1808 req = &workRequest.u.FtpRenameFileW;
1809 req->lpszSrcFile = WININET_strdupW(lpszSrc);
1810 req->lpszDestFile = WININET_strdupW(lpszDest);
1812 r = INTERNET_AsyncCall(&workRequest);
1816 r = FTP_FtpRenameFileW(lpwfs, lpszSrc, lpszDest);
1820 WININET_Release( &lpwfs->hdr );
1825 /***********************************************************************
1826 * FTP_FtpRenameFileW (Internal)
1828 * Rename a file on the ftp server
1835 BOOL FTP_FtpRenameFileW( LPWININETFTPSESSIONW lpwfs,
1836 LPCWSTR lpszSrc, LPCWSTR lpszDest)
1839 BOOL bSuccess = FALSE;
1840 LPWININETAPPINFOW hIC = NULL;
1844 /* Clear any error information */
1845 INTERNET_SetLastError(0);
1847 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RNFR, lpszSrc, 0, 0, 0))
1850 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1851 if (nResCode == 350)
1853 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RNTO, lpszDest, 0, 0, 0))
1856 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1859 if (nResCode == 250)
1862 FTP_SetResponseError(nResCode);
1865 hIC = lpwfs->lpAppInfo;
1866 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1868 INTERNET_ASYNC_RESULT iar;
1870 iar.dwResult = (DWORD)bSuccess;
1871 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1872 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1873 &iar, sizeof(INTERNET_ASYNC_RESULT));
1879 /***********************************************************************
1880 * FtpCommandA (WININET.@)
1882 BOOL WINAPI FtpCommandA( HINTERNET hConnect, BOOL fExpectResponse, DWORD dwFlags,
1883 LPCSTR lpszCommand, DWORD_PTR dwContext, HINTERNET* phFtpCommand )
1888 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect, fExpectResponse, dwFlags,
1889 debugstr_a(lpszCommand), dwContext, phFtpCommand);
1891 if (fExpectResponse)
1893 FIXME("data connection not supported\n");
1897 if (!lpszCommand || !lpszCommand[0])
1899 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1903 if (!(cmdW = WININET_strdup_AtoW(lpszCommand)))
1905 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1909 r = FtpCommandW(hConnect, fExpectResponse, dwFlags, cmdW, dwContext, phFtpCommand);
1911 HeapFree(GetProcessHeap(), 0, cmdW);
1915 /***********************************************************************
1916 * FtpCommandW (WININET.@)
1918 BOOL WINAPI FtpCommandW( HINTERNET hConnect, BOOL fExpectResponse, DWORD dwFlags,
1919 LPCWSTR lpszCommand, DWORD_PTR dwContext, HINTERNET* phFtpCommand )
1922 LPWININETFTPSESSIONW lpwfs;
1924 DWORD len, nBytesSent= 0;
1925 INT nResCode, nRC = 0;
1927 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect, fExpectResponse, dwFlags,
1928 debugstr_w(lpszCommand), dwContext, phFtpCommand);
1930 if (!lpszCommand || !lpszCommand[0])
1932 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1936 if (fExpectResponse)
1938 FIXME("data connection not supported\n");
1942 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
1945 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1949 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1951 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1955 if (lpwfs->download_in_progress != NULL)
1957 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1961 len = WideCharToMultiByte(CP_ACP, 0, lpszCommand, -1, NULL, 0, NULL, NULL) + strlen(szCRLF);
1962 if ((cmd = HeapAlloc(GetProcessHeap(), 0, len )))
1963 WideCharToMultiByte(CP_ACP, 0, lpszCommand, -1, cmd, len, NULL, NULL);
1966 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1970 strcat(cmd, szCRLF);
1973 TRACE("Sending (%s) len(%d)\n", cmd, len);
1974 while ((nBytesSent < len) && (nRC != -1))
1976 nRC = send(lpwfs->sndSocket, cmd + nBytesSent, len - nBytesSent, 0);
1980 TRACE("Sent %d bytes\n", nRC);
1986 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1987 if (nResCode > 0 && nResCode < 400)
1990 FTP_SetResponseError(nResCode);
1994 WININET_Release( &lpwfs->hdr );
1995 HeapFree(GetProcessHeap(), 0, cmd);
1999 /***********************************************************************
2000 * FTP_Connect (internal)
2002 * Connect to a ftp server
2005 * HINTERNET a session handle on success
2010 * Windows uses 'anonymous' as the username, when given a NULL username
2011 * and a NULL password. The password is first looked up in:
2013 * HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\EmailName
2015 * If this entry is not present it uses the current username as the password.
2019 HINTERNET FTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2020 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2021 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
2022 DWORD dwInternalFlags)
2024 static const WCHAR szKey[] = {'S','o','f','t','w','a','r','e','\\',
2025 'M','i','c','r','o','s','o','f','t','\\',
2026 'W','i','n','d','o','w','s','\\',
2027 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2028 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
2029 static const WCHAR szValue[] = {'E','m','a','i','l','N','a','m','e',0};
2030 static const WCHAR szDefaultUsername[] = {'a','n','o','n','y','m','o','u','s','\0'};
2031 static const WCHAR szEmpty[] = {'\0'};
2032 struct sockaddr_in socketAddr;
2035 BOOL bSuccess = FALSE;
2036 LPWININETFTPSESSIONW lpwfs = NULL;
2037 HINTERNET handle = NULL;
2039 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
2040 hIC, debugstr_w(lpszServerName),
2041 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword));
2043 assert( hIC->hdr.htype == WH_HINIT );
2045 if (NULL == lpszUserName && NULL != lpszPassword)
2047 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2051 lpwfs = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPSESSIONW));
2054 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2058 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
2059 nServerPort = INTERNET_DEFAULT_FTP_PORT;
2061 lpwfs->hdr.htype = WH_HFTPSESSION;
2062 lpwfs->hdr.dwFlags = dwFlags;
2063 lpwfs->hdr.dwContext = dwContext;
2064 lpwfs->hdr.dwInternalFlags = dwInternalFlags;
2065 lpwfs->hdr.dwRefCount = 1;
2066 lpwfs->hdr.close_connection = FTP_CloseConnection;
2067 lpwfs->hdr.destroy = FTP_CloseSessionHandle;
2068 lpwfs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2069 lpwfs->download_in_progress = NULL;
2070 lpwfs->sndSocket = -1;
2071 lpwfs->lstnSocket = -1;
2072 lpwfs->pasvSocket = -1;
2074 WININET_AddRef( &hIC->hdr );
2075 lpwfs->lpAppInfo = hIC;
2076 list_add_head( &hIC->hdr.children, &lpwfs->hdr.entry );
2078 handle = WININET_AllocHandle( &lpwfs->hdr );
2081 ERR("Failed to alloc handle\n");
2082 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2086 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2087 if(strchrW(hIC->lpszProxy, ' '))
2088 FIXME("Several proxies not implemented.\n");
2089 if(hIC->lpszProxyBypass)
2090 FIXME("Proxy bypass is ignored.\n");
2092 if ( !lpszUserName) {
2094 WCHAR szPassword[MAX_PATH];
2095 DWORD len = sizeof(szPassword);
2097 lpwfs->lpszUserName = WININET_strdupW(szDefaultUsername);
2099 RegOpenKeyW(HKEY_CURRENT_USER, szKey, &key);
2100 if (RegQueryValueExW(key, szValue, NULL, NULL, (LPBYTE)szPassword, &len)) {
2101 /* Nothing in the registry, get the username and use that as the password */
2102 if (!GetUserNameW(szPassword, &len)) {
2103 /* Should never get here, but use an empty password as failsafe */
2104 strcpyW(szPassword, szEmpty);
2109 TRACE("Password used for anonymous ftp : (%s)\n", debugstr_w(szPassword));
2110 lpwfs->lpszPassword = WININET_strdupW(szPassword);
2113 lpwfs->lpszUserName = WININET_strdupW(lpszUserName);
2116 lpwfs->lpszPassword = WININET_strdupW(lpszPassword);
2118 lpwfs->lpszPassword = WININET_strdupW(szEmpty);
2121 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2122 if (!(lpwfs->hdr.dwInternalFlags & INET_OPENURL))
2124 INTERNET_ASYNC_RESULT iar;
2126 iar.dwResult = (DWORD)handle;
2127 iar.dwError = ERROR_SUCCESS;
2129 SendAsyncCallback(&hIC->hdr, dwContext,
2130 INTERNET_STATUS_HANDLE_CREATED, &iar,
2131 sizeof(INTERNET_ASYNC_RESULT));
2134 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_RESOLVING_NAME,
2135 (LPWSTR) lpszServerName, strlenW(lpszServerName));
2137 if (!GetAddress(lpszServerName, nServerPort, &socketAddr))
2139 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
2143 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_NAME_RESOLVED,
2144 (LPWSTR) lpszServerName, strlenW(lpszServerName));
2146 nsocket = socket(AF_INET,SOCK_STREAM,0);
2149 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2153 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_CONNECTING_TO_SERVER,
2154 &socketAddr, sizeof(struct sockaddr_in));
2156 if (connect(nsocket, (struct sockaddr *)&socketAddr, sizeof(socketAddr)) < 0)
2158 ERR("Unable to connect (%s)\n", strerror(errno));
2159 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2163 TRACE("Connected to server\n");
2164 lpwfs->sndSocket = nsocket;
2165 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_CONNECTED_TO_SERVER,
2166 &socketAddr, sizeof(struct sockaddr_in));
2168 sock_namelen = sizeof(lpwfs->socketAddress);
2169 getsockname(nsocket, (struct sockaddr *) &lpwfs->socketAddress, &sock_namelen);
2171 if (FTP_ConnectToHost(lpwfs))
2173 TRACE("Successfully logged into server\n");
2179 if (lpwfs) WININET_Release( &lpwfs->hdr );
2181 if (!bSuccess && handle)
2183 WININET_FreeHandle( handle );
2187 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2189 INTERNET_ASYNC_RESULT iar;
2191 iar.dwResult = bSuccess ? (DWORD_PTR)lpwfs : 0;
2192 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2193 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
2194 &iar, sizeof(INTERNET_ASYNC_RESULT));
2201 /***********************************************************************
2202 * FTP_ConnectToHost (internal)
2204 * Connect to a ftp server
2211 static BOOL FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs)
2214 BOOL bSuccess = FALSE;
2217 FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2219 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_USER, lpwfs->lpszUserName, 0, 0, 0))
2222 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2225 /* Login successful... */
2226 if (nResCode == 230)
2228 /* User name okay, need password... */
2229 else if (nResCode == 331)
2230 bSuccess = FTP_SendPassword(lpwfs);
2231 /* Need account for login... */
2232 else if (nResCode == 332)
2233 bSuccess = FTP_SendAccount(lpwfs);
2235 FTP_SetResponseError(nResCode);
2238 TRACE("Returning %d\n", bSuccess);
2244 /***********************************************************************
2245 * FTP_SendCommandA (internal)
2247 * Send command to server
2254 static BOOL FTP_SendCommandA(INT nSocket, FTP_COMMAND ftpCmd, LPCSTR lpszParam,
2255 INTERNET_STATUS_CALLBACK lpfnStatusCB, LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext)
2259 DWORD nBytesSent = 0;
2263 TRACE("%d: (%s) %d\n", ftpCmd, lpszParam, nSocket);
2267 lpfnStatusCB(hdr->hInternet, dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2270 dwParamLen = lpszParam?strlen(lpszParam)+1:0;
2271 len = dwParamLen + strlen(szFtpCommands[ftpCmd]) + strlen(szCRLF);
2272 if (NULL == (buf = HeapAlloc(GetProcessHeap(), 0, len+1)))
2274 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2277 sprintf(buf, "%s%s%s%s", szFtpCommands[ftpCmd], dwParamLen ? " " : "",
2278 dwParamLen ? lpszParam : "", szCRLF);
2280 TRACE("Sending (%s) len(%d)\n", buf, len);
2281 while((nBytesSent < len) && (nRC != -1))
2283 nRC = send(nSocket, buf+nBytesSent, len - nBytesSent, 0);
2287 HeapFree(GetProcessHeap(), 0, (LPVOID)buf);
2291 lpfnStatusCB(hdr->hInternet, dwContext, INTERNET_STATUS_REQUEST_SENT,
2292 &nBytesSent, sizeof(DWORD));
2295 TRACE("Sent %d bytes\n", nBytesSent);
2299 /***********************************************************************
2300 * FTP_SendCommand (internal)
2302 * Send command to server
2309 static BOOL FTP_SendCommand(INT nSocket, FTP_COMMAND ftpCmd, LPCWSTR lpszParam,
2310 INTERNET_STATUS_CALLBACK lpfnStatusCB, LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext)
2313 LPSTR lpszParamA = lpszParam?WININET_strdup_WtoA(lpszParam):NULL;
2314 ret = FTP_SendCommandA(nSocket, ftpCmd, lpszParamA, lpfnStatusCB, hdr, dwContext);
2315 HeapFree(GetProcessHeap(), 0, lpszParamA);
2319 /***********************************************************************
2320 * FTP_ReceiveResponse (internal)
2322 * Receive response from server
2325 * Reply code on success
2329 INT FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs, DWORD_PTR dwContext)
2331 LPSTR lpszResponse = INTERNET_GetResponseBuffer();
2334 char firstprefix[5];
2335 BOOL multiline = FALSE;
2336 LPWININETAPPINFOW hIC = NULL;
2338 TRACE("socket(%d)\n", lpwfs->sndSocket);
2340 hIC = lpwfs->lpAppInfo;
2341 SendAsyncCallback(&lpwfs->hdr, dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2345 if (!INTERNET_GetNextLine(lpwfs->sndSocket, &nRecv))
2352 if(lpszResponse[3] != '-')
2355 { /* Start of multiline repsonse. Loop until we get "nnn " */
2357 memcpy(firstprefix, lpszResponse, 3);
2358 firstprefix[3] = ' ';
2359 firstprefix[4] = '\0';
2364 if(!memcmp(firstprefix, lpszResponse, 4))
2372 rc = atoi(lpszResponse);
2374 SendAsyncCallback(&lpwfs->hdr, dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
2375 &nRecv, sizeof(DWORD));
2379 TRACE("return %d\n", rc);
2384 /***********************************************************************
2385 * FTP_SendPassword (internal)
2387 * Send password to ftp server
2394 static BOOL FTP_SendPassword(LPWININETFTPSESSIONW lpwfs)
2397 BOOL bSuccess = FALSE;
2400 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PASS, lpwfs->lpszPassword, 0, 0, 0))
2403 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2406 TRACE("Received reply code %d\n", nResCode);
2407 /* Login successful... */
2408 if (nResCode == 230)
2410 /* Command not implemented, superfluous at the server site... */
2411 /* Need account for login... */
2412 else if (nResCode == 332)
2413 bSuccess = FTP_SendAccount(lpwfs);
2415 FTP_SetResponseError(nResCode);
2419 TRACE("Returning %d\n", bSuccess);
2424 /***********************************************************************
2425 * FTP_SendAccount (internal)
2434 static BOOL FTP_SendAccount(LPWININETFTPSESSIONW lpwfs)
2437 BOOL bSuccess = FALSE;
2440 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_ACCT, szNoAccount, 0, 0, 0))
2443 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2447 FTP_SetResponseError(nResCode);
2454 /***********************************************************************
2455 * FTP_SendStore (internal)
2457 * Send request to upload file to ftp server
2464 static BOOL FTP_SendStore(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType)
2467 BOOL bSuccess = FALSE;
2470 if (!FTP_InitListenSocket(lpwfs))
2473 if (!FTP_SendType(lpwfs, dwType))
2476 if (!FTP_SendPortOrPasv(lpwfs))
2479 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_STOR, lpszRemoteFile, 0, 0, 0))
2481 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2484 if (nResCode == 150 || nResCode == 125)
2487 FTP_SetResponseError(nResCode);
2491 if (!bSuccess && lpwfs->lstnSocket != -1)
2493 closesocket(lpwfs->lstnSocket);
2494 lpwfs->lstnSocket = -1;
2501 /***********************************************************************
2502 * FTP_InitListenSocket (internal)
2504 * Create a socket to listen for server response
2511 static BOOL FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs)
2513 BOOL bSuccess = FALSE;
2514 socklen_t namelen = sizeof(struct sockaddr_in);
2518 lpwfs->lstnSocket = socket(PF_INET, SOCK_STREAM, 0);
2519 if (lpwfs->lstnSocket == -1)
2521 TRACE("Unable to create listening socket\n");
2525 /* We obtain our ip addr from the name of the command channel socket */
2526 lpwfs->lstnSocketAddress = lpwfs->socketAddress;
2528 /* and get the system to assign us a port */
2529 lpwfs->lstnSocketAddress.sin_port = htons((u_short) 0);
2531 if (bind(lpwfs->lstnSocket,(struct sockaddr *) &lpwfs->lstnSocketAddress, sizeof(struct sockaddr_in)) == -1)
2533 TRACE("Unable to bind socket\n");
2537 if (listen(lpwfs->lstnSocket, MAX_BACKLOG) == -1)
2539 TRACE("listen failed\n");
2543 if (getsockname(lpwfs->lstnSocket, (struct sockaddr *) &lpwfs->lstnSocketAddress, &namelen) != -1)
2547 if (!bSuccess && lpwfs->lstnSocket != -1)
2549 closesocket(lpwfs->lstnSocket);
2550 lpwfs->lstnSocket = -1;
2557 /***********************************************************************
2558 * FTP_SendType (internal)
2560 * Tell server type of data being transferred
2566 * W98SE doesn't cache the type that's currently set
2567 * (i.e. it sends it always),
2568 * so we probably don't want to do that either.
2570 static BOOL FTP_SendType(LPWININETFTPSESSIONW lpwfs, DWORD dwType)
2573 WCHAR type[] = { 'I','\0' };
2574 BOOL bSuccess = FALSE;
2577 if (dwType & INTERNET_FLAG_TRANSFER_ASCII)
2580 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_TYPE, type, 0, 0, 0))
2583 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext)/100;
2589 FTP_SetResponseError(nResCode);
2597 #if 0 /* FIXME: should probably be used for FtpGetFileSize */
2598 /***********************************************************************
2599 * FTP_GetFileSize (internal)
2601 * Retrieves from the server the size of the given file
2608 static BOOL FTP_GetFileSize(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD *dwSize)
2611 BOOL bSuccess = FALSE;
2615 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_SIZE, lpszRemoteFile, 0, 0, 0))
2618 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2621 if (nResCode == 213) {
2622 /* Now parses the output to get the actual file size */
2624 LPSTR lpszResponseBuffer = INTERNET_GetResponseBuffer();
2626 for (i = 0; (lpszResponseBuffer[i] != ' ') && (lpszResponseBuffer[i] != '\0'); i++) ;
2627 if (lpszResponseBuffer[i] == '\0') return FALSE;
2628 *dwSize = atol(&(lpszResponseBuffer[i + 1]));
2632 FTP_SetResponseError(nResCode);
2642 /***********************************************************************
2643 * FTP_SendPort (internal)
2645 * Tell server which port to use
2652 static BOOL FTP_SendPort(LPWININETFTPSESSIONW lpwfs)
2654 static const WCHAR szIPFormat[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
2656 WCHAR szIPAddress[64];
2657 BOOL bSuccess = FALSE;
2660 sprintfW(szIPAddress, szIPFormat,
2661 lpwfs->lstnSocketAddress.sin_addr.s_addr&0x000000FF,
2662 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0x0000FF00)>>8,
2663 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0x00FF0000)>>16,
2664 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0xFF000000)>>24,
2665 lpwfs->lstnSocketAddress.sin_port & 0xFF,
2666 (lpwfs->lstnSocketAddress.sin_port & 0xFF00)>>8);
2668 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PORT, szIPAddress, 0, 0, 0))
2671 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2674 if (nResCode == 200)
2677 FTP_SetResponseError(nResCode);
2685 /***********************************************************************
2686 * FTP_DoPassive (internal)
2688 * Tell server that we want to do passive transfers
2689 * and connect data socket
2696 static BOOL FTP_DoPassive(LPWININETFTPSESSIONW lpwfs)
2699 BOOL bSuccess = FALSE;
2702 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PASV, NULL, 0, 0, 0))
2705 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2708 if (nResCode == 227)
2710 LPSTR lpszResponseBuffer = INTERNET_GetResponseBuffer();
2714 char *pAddr, *pPort;
2716 struct sockaddr_in dataSocketAddress;
2718 p = lpszResponseBuffer+4; /* skip status code */
2719 while (*p != '\0' && (*p < '0' || *p > '9')) p++;
2723 ERR("no address found in response, aborting\n");
2727 if (sscanf(p, "%d,%d,%d,%d,%d,%d", &f[0], &f[1], &f[2], &f[3],
2730 ERR("unknown response address format '%s', aborting\n", p);
2733 for (i=0; i < 6; i++)
2736 dataSocketAddress = lpwfs->socketAddress;
2737 pAddr = (char *)&(dataSocketAddress.sin_addr.s_addr);
2738 pPort = (char *)&(dataSocketAddress.sin_port);
2746 nsocket = socket(AF_INET,SOCK_STREAM,0);
2750 if (connect(nsocket, (struct sockaddr *)&dataSocketAddress, sizeof(dataSocketAddress)))
2752 ERR("can't connect passive FTP data port.\n");
2753 closesocket(nsocket);
2756 lpwfs->pasvSocket = nsocket;
2760 FTP_SetResponseError(nResCode);
2768 static BOOL FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs)
2770 if (lpwfs->hdr.dwFlags & INTERNET_FLAG_PASSIVE)
2772 if (!FTP_DoPassive(lpwfs))
2777 if (!FTP_SendPort(lpwfs))
2784 /***********************************************************************
2785 * FTP_GetDataSocket (internal)
2787 * Either accepts an incoming data socket connection from the server
2788 * or just returns the already opened socket after a PASV command
2789 * in case of passive FTP.
2797 static BOOL FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs, LPINT nDataSocket)
2799 struct sockaddr_in saddr;
2800 socklen_t addrlen = sizeof(struct sockaddr);
2803 if (lpwfs->hdr.dwFlags & INTERNET_FLAG_PASSIVE)
2805 *nDataSocket = lpwfs->pasvSocket;
2809 *nDataSocket = accept(lpwfs->lstnSocket, (struct sockaddr *) &saddr, &addrlen);
2810 closesocket(lpwfs->lstnSocket);
2811 lpwfs->lstnSocket = -1;
2813 return *nDataSocket != -1;
2817 /***********************************************************************
2818 * FTP_SendData (internal)
2820 * Send data to the server
2827 static BOOL FTP_SendData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile)
2829 BY_HANDLE_FILE_INFORMATION fi;
2830 DWORD nBytesRead = 0;
2831 DWORD nBytesSent = 0;
2832 DWORD nTotalSent = 0;
2833 DWORD nBytesToSend, nLen;
2835 time_t s_long_time, e_long_time;
2840 lpszBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHAR)*DATA_PACKET_SIZE);
2842 /* Get the size of the file. */
2843 GetFileInformationByHandle(hFile, &fi);
2848 nBytesToSend = nBytesRead - nBytesSent;
2850 if (nBytesToSend <= 0)
2852 /* Read data from file. */
2854 if (!ReadFile(hFile, lpszBuffer, DATA_PACKET_SIZE, &nBytesRead, 0))
2855 ERR("Failed reading from file\n");
2858 nBytesToSend = nBytesRead;
2863 nLen = DATA_PACKET_SIZE < nBytesToSend ?
2864 DATA_PACKET_SIZE : nBytesToSend;
2865 nRC = send(nDataSocket, lpszBuffer, nLen, 0);
2873 /* Do some computation to display the status. */
2875 nSeconds = e_long_time - s_long_time;
2876 if( nSeconds / 60 > 0 )
2878 TRACE( "%d bytes of %d bytes (%d%%) in %d min %d sec estimated remaining time %d sec\n",
2879 nTotalSent, fi.nFileSizeLow, nTotalSent*100/fi.nFileSizeLow, nSeconds / 60,
2880 nSeconds % 60, (fi.nFileSizeLow - nTotalSent) * nSeconds / nTotalSent );
2884 TRACE( "%d bytes of %d bytes (%d%%) in %d sec estimated remaining time %d sec\n",
2885 nTotalSent, fi.nFileSizeLow, nTotalSent*100/fi.nFileSizeLow, nSeconds,
2886 (fi.nFileSizeLow - nTotalSent) * nSeconds / nTotalSent);
2888 } while (nRC != -1);
2890 TRACE("file transfer complete!\n");
2892 HeapFree(GetProcessHeap(), 0, lpszBuffer);
2898 /***********************************************************************
2899 * FTP_SendRetrieve (internal)
2901 * Send request to retrieve a file
2904 * Number of bytes to be received on success
2908 static BOOL FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType)
2914 if (!(ret = FTP_InitListenSocket(lpwfs)))
2917 if (!(ret = FTP_SendType(lpwfs, dwType)))
2920 if (!(ret = FTP_SendPortOrPasv(lpwfs)))
2923 if (!(ret = FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RETR, lpszRemoteFile, 0, 0, 0)))
2926 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2927 if ((nResCode != 125) && (nResCode != 150)) {
2928 /* That means that we got an error getting the file. */
2929 FTP_SetResponseError(nResCode);
2934 if (!ret && lpwfs->lstnSocket != -1)
2936 closesocket(lpwfs->lstnSocket);
2937 lpwfs->lstnSocket = -1;
2944 /***********************************************************************
2945 * FTP_RetrieveData (internal)
2947 * Retrieve data from server
2954 static BOOL FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile)
2956 DWORD nBytesWritten;
2957 DWORD nBytesReceived = 0;
2963 lpszBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHAR)*DATA_PACKET_SIZE);
2964 if (NULL == lpszBuffer)
2966 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2972 nRC = recv(nDataSocket, lpszBuffer, DATA_PACKET_SIZE, 0);
2975 /* other side closed socket. */
2978 WriteFile(hFile, lpszBuffer, nRC, &nBytesWritten, NULL);
2979 nBytesReceived += nRC;
2983 TRACE("Data transfer complete\n");
2986 HeapFree(GetProcessHeap(), 0, lpszBuffer);
2991 /***********************************************************************
2992 * FTP_CloseConnection (internal)
2996 static void FTP_CloseConnection(LPWININETHANDLEHEADER hdr)
2998 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) hdr;
3002 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext,
3003 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
3005 if (lpwfs->download_in_progress != NULL)
3006 lpwfs->download_in_progress->session_deleted = TRUE;
3008 if (lpwfs->sndSocket != -1)
3009 closesocket(lpwfs->sndSocket);
3011 if (lpwfs->lstnSocket != -1)
3012 closesocket(lpwfs->lstnSocket);
3014 if (lpwfs->pasvSocket != -1)
3015 closesocket(lpwfs->pasvSocket);
3017 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext,
3018 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
3022 /***********************************************************************
3023 * FTP_CloseSessionHandle (internal)
3025 * Deallocate session handle
3027 static void FTP_CloseSessionHandle(LPWININETHANDLEHEADER hdr)
3029 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) hdr;
3033 WININET_Release(&lpwfs->lpAppInfo->hdr);
3035 HeapFree(GetProcessHeap(), 0, lpwfs->lpszPassword);
3036 HeapFree(GetProcessHeap(), 0, lpwfs->lpszUserName);
3037 HeapFree(GetProcessHeap(), 0, lpwfs);
3041 /***********************************************************************
3042 * FTP_FindNextFileW (Internal)
3044 * Continues a file search from a previous call to FindFirstFile
3051 BOOL WINAPI FTP_FindNextFileW(LPWININETFTPFINDNEXTW lpwh, LPVOID lpvFindData)
3053 BOOL bSuccess = TRUE;
3054 LPWIN32_FIND_DATAW lpFindFileData;
3056 TRACE("index(%d) size(%d)\n", lpwh->index, lpwh->size);
3058 assert (lpwh->hdr.htype == WH_HFTPFINDNEXT);
3060 /* Clear any error information */
3061 INTERNET_SetLastError(0);
3063 lpFindFileData = (LPWIN32_FIND_DATAW) lpvFindData;
3064 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
3066 if (lpwh->index >= lpwh->size)
3068 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
3073 FTP_ConvertFileProp(&lpwh->lpafp[lpwh->index], lpFindFileData);
3076 TRACE("\nName: %s\nSize: %d\n", debugstr_w(lpFindFileData->cFileName), lpFindFileData->nFileSizeLow);
3080 if (lpwh->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3082 INTERNET_ASYNC_RESULT iar;
3084 iar.dwResult = (DWORD)bSuccess;
3085 iar.dwError = iar.dwError = bSuccess ? ERROR_SUCCESS :
3086 INTERNET_GetLastError();
3088 INTERNET_SendCallback(&lpwh->hdr, lpwh->hdr.dwContext,
3089 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3090 sizeof(INTERNET_ASYNC_RESULT));
3097 /***********************************************************************
3098 * FTP_CloseFindNextHandle (internal)
3100 * Deallocate session handle
3107 static void FTP_CloseFindNextHandle(LPWININETHANDLEHEADER hdr)
3109 LPWININETFTPFINDNEXTW lpwfn = (LPWININETFTPFINDNEXTW) hdr;
3114 WININET_Release(&lpwfn->lpFtpSession->hdr);
3116 for (i = 0; i < lpwfn->size; i++)
3118 HeapFree(GetProcessHeap(), 0, lpwfn->lpafp[i].lpszName);
3121 HeapFree(GetProcessHeap(), 0, lpwfn->lpafp);
3122 HeapFree(GetProcessHeap(), 0, lpwfn);
3125 /***********************************************************************
3126 * FTP_CloseFileTransferHandle (internal)
3128 * Closes the file transfer handle. This also 'cleans' the data queue of
3129 * the 'transfer complete' message (this is a bit of a hack though :-/ )
3132 static void FTP_CloseFileTransferHandle(LPWININETHANDLEHEADER hdr)
3134 LPWININETFTPFILE lpwh = (LPWININETFTPFILE) hdr;
3135 LPWININETFTPSESSIONW lpwfs = lpwh->lpFtpSession;
3140 WININET_Release(&lpwh->lpFtpSession->hdr);
3142 if (!lpwh->session_deleted)
3143 lpwfs->download_in_progress = NULL;
3145 if (lpwh->nDataSocket != -1)
3146 closesocket(lpwh->nDataSocket);
3148 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
3149 if (nResCode > 0 && nResCode != 226) WARN("server reports failed transfer\n");
3151 HeapFree(GetProcessHeap(), 0, lpwh);
3154 /***********************************************************************
3155 * FTP_ReceiveFileList (internal)
3157 * Read file list from server
3160 * Handle to file list on success
3164 static HINTERNET FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
3165 LPWIN32_FIND_DATAW lpFindFileData, DWORD_PTR dwContext)
3168 LPFILEPROPERTIESW lpafp = NULL;
3169 LPWININETFTPFINDNEXTW lpwfn = NULL;
3170 HINTERNET handle = 0;
3172 TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs, nSocket, debugstr_w(lpszSearchFile), lpFindFileData, dwContext);
3174 if (FTP_ParseDirectory(lpwfs, nSocket, lpszSearchFile, &lpafp, &dwSize))
3177 FTP_ConvertFileProp(lpafp, lpFindFileData);
3179 lpwfn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETFTPFINDNEXTW));
3182 lpwfn->hdr.htype = WH_HFTPFINDNEXT;
3183 lpwfn->hdr.dwContext = dwContext;
3184 lpwfn->hdr.dwRefCount = 1;
3185 lpwfn->hdr.close_connection = NULL;
3186 lpwfn->hdr.destroy = FTP_CloseFindNextHandle;
3187 lpwfn->hdr.lpfnStatusCB = lpwfs->hdr.lpfnStatusCB;
3188 lpwfn->index = 1; /* Next index is 1 since we return index 0 */
3189 lpwfn->size = dwSize;
3190 lpwfn->lpafp = lpafp;
3192 WININET_AddRef( &lpwfs->hdr );
3193 lpwfn->lpFtpSession = lpwfs;
3194 list_add_head( &lpwfs->hdr.children, &lpwfn->hdr.entry );
3196 handle = WININET_AllocHandle( &lpwfn->hdr );
3201 WININET_Release( &lpwfn->hdr );
3203 TRACE("Matched %d files\n", dwSize);
3208 /***********************************************************************
3209 * FTP_ConvertFileProp (internal)
3211 * Converts FILEPROPERTIESW struct to WIN32_FIND_DATAA
3218 BOOL FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp, LPWIN32_FIND_DATAW lpFindFileData)
3220 BOOL bSuccess = FALSE;
3222 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAW));
3226 /* Convert 'Unix' time to Windows time */
3227 RtlSecondsSince1970ToTime(mktime(&lpafp->tmLastModified),
3228 (LARGE_INTEGER *) &(lpFindFileData->ftLastAccessTime));
3229 lpFindFileData->ftLastWriteTime = lpFindFileData->ftLastAccessTime;
3230 lpFindFileData->ftCreationTime = lpFindFileData->ftLastAccessTime;
3232 /* Not all fields are filled in */
3233 lpFindFileData->nFileSizeHigh = 0; /* We do not handle files bigger than 0xFFFFFFFF bytes yet :-) */
3234 lpFindFileData->nFileSizeLow = lpafp->nSize;
3236 if (lpafp->bIsDirectory)
3237 lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
3239 if (lpafp->lpszName)
3240 lstrcpynW(lpFindFileData->cFileName, lpafp->lpszName, MAX_PATH);
3248 /***********************************************************************
3249 * FTP_ParseNextFile (internal)
3251 * Parse the next line in file listing
3257 static BOOL FTP_ParseNextFile(INT nSocket, LPCWSTR lpszSearchFile, LPFILEPROPERTIESW lpfp)
3259 static const char szSpace[] = " \t";
3267 lpfp->lpszName = NULL;
3269 if(!(pszLine = INTERNET_GetNextLine(nSocket, &nBufLen)))
3272 pszToken = strtok(pszLine, szSpace);
3274 * <Permissions> <NoLinks> <owner> <group> <size> <date> <time or year> <filename>
3277 * drwx--s--- 2 pcarrier ens 512 Sep 28 1995 pcarrier
3279 if(!isdigit(pszToken[0]) && 10 == strlen(pszToken)) {
3280 if(!FTP_ParsePermission(pszToken, lpfp))
3281 lpfp->bIsDirectory = FALSE;
3282 for(i=0; i<=3; i++) {
3283 if(!(pszToken = strtok(NULL, szSpace)))
3286 if(!pszToken) continue;
3287 if(lpfp->bIsDirectory) {
3288 TRACE("Is directory\n");
3292 TRACE("Size: %s\n", pszToken);
3293 lpfp->nSize = atol(pszToken);
3296 lpfp->tmLastModified.tm_sec = 0;
3297 lpfp->tmLastModified.tm_min = 0;
3298 lpfp->tmLastModified.tm_hour = 0;
3299 lpfp->tmLastModified.tm_mday = 0;
3300 lpfp->tmLastModified.tm_mon = 0;
3301 lpfp->tmLastModified.tm_year = 0;
3303 /* Determine month */
3304 pszToken = strtok(NULL, szSpace);
3305 if(!pszToken) continue;
3306 if(strlen(pszToken) >= 3) {
3308 if((pszTmp = StrStrIA(szMonths, pszToken)))
3309 lpfp->tmLastModified.tm_mon = ((pszTmp - szMonths) / 3)+1;
3312 pszToken = strtok(NULL, szSpace);
3313 if(!pszToken) continue;
3314 lpfp->tmLastModified.tm_mday = atoi(pszToken);
3315 /* Determine time or year */
3316 pszToken = strtok(NULL, szSpace);
3317 if(!pszToken) continue;
3318 if((pszTmp = strchr(pszToken, ':'))) {
3323 lpfp->tmLastModified.tm_min = atoi(pszTmp);
3324 lpfp->tmLastModified.tm_hour = atoi(pszToken);
3326 apTM = localtime(&aTime);
3327 lpfp->tmLastModified.tm_year = apTM->tm_year;
3330 lpfp->tmLastModified.tm_year = atoi(pszToken) - 1900;
3331 lpfp->tmLastModified.tm_hour = 12;
3333 TRACE("Mod time: %02d:%02d:%02d %02d/%02d/%02d\n",
3334 lpfp->tmLastModified.tm_hour, lpfp->tmLastModified.tm_min, lpfp->tmLastModified.tm_sec,
3335 (lpfp->tmLastModified.tm_year >= 100) ? lpfp->tmLastModified.tm_year - 100 : lpfp->tmLastModified.tm_year,
3336 lpfp->tmLastModified.tm_mon, lpfp->tmLastModified.tm_mday);
3338 pszToken = strtok(NULL, szSpace);
3339 if(!pszToken) continue;
3340 lpfp->lpszName = WININET_strdup_AtoW(pszToken);
3341 TRACE("File: %s\n", debugstr_w(lpfp->lpszName));
3343 /* NT way of parsing ... :
3345 07-13-03 08:55PM <DIR> sakpatch
3346 05-09-03 06:02PM 12656686 2003-04-21bgm_cmd_e.rgz
3348 else if(isdigit(pszToken[0]) && 8 == strlen(pszToken)) {
3349 lpfp->permissions = 0xFFFF; /* No idea, put full permission :-) */
3351 sscanf(pszToken, "%d-%d-%d",
3352 &lpfp->tmLastModified.tm_mon,
3353 &lpfp->tmLastModified.tm_mday,
3354 &lpfp->tmLastModified.tm_year);
3356 /* Hacky and bad Y2K protection :-) */
3357 if (lpfp->tmLastModified.tm_year < 70)
3358 lpfp->tmLastModified.tm_year += 100;
3360 pszToken = strtok(NULL, szSpace);
3361 if(!pszToken) continue;
3362 sscanf(pszToken, "%d:%d",
3363 &lpfp->tmLastModified.tm_hour,
3364 &lpfp->tmLastModified.tm_min);
3365 if((pszToken[5] == 'P') && (pszToken[6] == 'M')) {
3366 lpfp->tmLastModified.tm_hour += 12;
3368 lpfp->tmLastModified.tm_sec = 0;
3370 TRACE("Mod time: %02d:%02d:%02d %02d/%02d/%02d\n",
3371 lpfp->tmLastModified.tm_hour, lpfp->tmLastModified.tm_min, lpfp->tmLastModified.tm_sec,
3372 (lpfp->tmLastModified.tm_year >= 100) ? lpfp->tmLastModified.tm_year - 100 : lpfp->tmLastModified.tm_year,
3373 lpfp->tmLastModified.tm_mon, lpfp->tmLastModified.tm_mday);
3375 pszToken = strtok(NULL, szSpace);
3376 if(!pszToken) continue;
3377 if(!strcasecmp(pszToken, "<DIR>")) {
3378 lpfp->bIsDirectory = TRUE;
3380 TRACE("Is directory\n");
3383 lpfp->bIsDirectory = FALSE;
3384 lpfp->nSize = atol(pszToken);
3385 TRACE("Size: %d\n", lpfp->nSize);
3388 pszToken = strtok(NULL, szSpace);
3389 if(!pszToken) continue;
3390 lpfp->lpszName = WININET_strdup_AtoW(pszToken);
3391 TRACE("Name: %s\n", debugstr_w(lpfp->lpszName));
3393 /* EPLF format - http://cr.yp.to/ftp/list/eplf.html */
3394 else if(pszToken[0] == '+') {
3395 FIXME("EPLF Format not implemented\n");
3398 if(lpfp->lpszName) {
3399 if((lpszSearchFile == NULL) ||
3400 (PathMatchSpecW(lpfp->lpszName, lpszSearchFile))) {
3402 TRACE("Matched: %s\n", debugstr_w(lpfp->lpszName));
3405 HeapFree(GetProcessHeap(), 0, lpfp->lpszName);
3406 lpfp->lpszName = NULL;
3413 /***********************************************************************
3414 * FTP_ParseDirectory (internal)
3416 * Parse string of directory information
3422 static BOOL FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
3423 LPFILEPROPERTIESW *lpafp, LPDWORD dwfp)
3425 BOOL bSuccess = TRUE;
3426 INT sizeFilePropArray = 500;/*20; */
3427 INT indexFilePropArray = -1;
3431 /* Allocate intial file properties array */
3432 *lpafp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FILEPROPERTIESW)*(sizeFilePropArray));
3437 if (indexFilePropArray+1 >= sizeFilePropArray)
3439 LPFILEPROPERTIESW tmpafp;
3441 sizeFilePropArray *= 2;
3442 tmpafp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *lpafp,
3443 sizeof(FILEPROPERTIESW)*sizeFilePropArray);
3452 indexFilePropArray++;
3453 } while (FTP_ParseNextFile(nSocket, lpszSearchFile, &(*lpafp)[indexFilePropArray]));
3455 if (bSuccess && indexFilePropArray)
3457 if (indexFilePropArray < sizeFilePropArray - 1)
3459 LPFILEPROPERTIESW tmpafp;
3461 tmpafp = HeapReAlloc(GetProcessHeap(), 0, *lpafp,
3462 sizeof(FILEPROPERTIESW)*indexFilePropArray);
3466 *dwfp = indexFilePropArray;
3470 HeapFree(GetProcessHeap(), 0, *lpafp);
3471 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
3479 /***********************************************************************
3480 * FTP_ParsePermission (internal)
3482 * Parse permission string of directory information
3489 static BOOL FTP_ParsePermission(LPCSTR lpszPermission, LPFILEPROPERTIESW lpfp)
3491 BOOL bSuccess = TRUE;
3492 unsigned short nPermission = 0;
3497 if ((*lpszPermission != 'd') && (*lpszPermission != '-') && (*lpszPermission != 'l'))
3503 lpfp->bIsDirectory = (*lpszPermission == 'd');
3509 nPermission |= (*(lpszPermission+1) == 'r' ? 1 : 0) << 8;
3512 nPermission |= (*(lpszPermission+2) == 'w' ? 1 : 0) << 7;
3515 nPermission |= (*(lpszPermission+3) == 'x' ? 1 : 0) << 6;
3518 nPermission |= (*(lpszPermission+4) == 'r' ? 1 : 0) << 5;
3521 nPermission |= (*(lpszPermission+5) == 'w' ? 1 : 0) << 4;
3524 nPermission |= (*(lpszPermission+6) == 'x' ? 1 : 0) << 3;
3527 nPermission |= (*(lpszPermission+7) == 'r' ? 1 : 0) << 2;
3530 nPermission |= (*(lpszPermission+8) == 'w' ? 1 : 0) << 1;
3533 nPermission |= (*(lpszPermission+9) == 'x' ? 1 : 0);
3537 }while (nPos <= nLast);
3539 lpfp->permissions = nPermission;
3544 /***********************************************************************
3545 * FTP_SetResponseError (internal)
3547 * Set the appropriate error code for a given response from the server
3552 static DWORD FTP_SetResponseError(DWORD dwResponse)
3558 case 425: /* Cannot open data connection. */
3559 dwCode = ERROR_INTERNET_CANNOT_CONNECT;
3562 case 426: /* Connection closed, transer aborted. */
3563 dwCode = ERROR_INTERNET_CONNECTION_ABORTED;
3566 case 530: /* Not logged in. Login incorrect. */
3567 dwCode = ERROR_INTERNET_LOGIN_FAILURE;
3570 case 421: /* Service not available - Server may be shutting down. */
3571 case 450: /* File action not taken. File may be busy. */
3572 case 451: /* Action aborted. Server error. */
3573 case 452: /* Action not taken. Insufficient storage space on server. */
3574 case 500: /* Syntax error. Command unrecognized. */
3575 case 501: /* Syntax error. Error in parameters or arguments. */
3576 case 502: /* Command not implemented. */
3577 case 503: /* Bad sequence of commands. */
3578 case 504: /* Command not implemented for that parameter. */
3579 case 532: /* Need account for storing files */
3580 case 550: /* File action not taken. File not found or no access. */
3581 case 551: /* Requested action aborted. Page type unknown */
3582 case 552: /* Action aborted. Exceeded storage allocation */
3583 case 553: /* Action not taken. File name not allowed. */
3586 dwCode = ERROR_INTERNET_EXTENDED_ERROR;
3590 INTERNET_SetLastError(dwCode);