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 );
888 if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
890 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
894 if (lpwfs->download_in_progress != NULL)
896 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
900 hIC = lpwfs->lpAppInfo;
901 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
903 WORKREQUEST workRequest;
904 struct WORKREQ_FTPGETCURRENTDIRECTORYW *req;
906 workRequest.asyncproc = AsyncFtpGetCurrentDirectoryProc;
907 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
908 req = &workRequest.u.FtpGetCurrentDirectoryW;
909 req->lpszDirectory = lpszCurrentDirectory;
910 req->lpdwDirectory = lpdwCurrentDirectory;
912 r = INTERNET_AsyncCall(&workRequest);
916 r = FTP_FtpGetCurrentDirectoryW(lpwfs, lpszCurrentDirectory,
917 lpdwCurrentDirectory);
922 WININET_Release( &lpwfs->hdr );
928 /***********************************************************************
929 * FTP_FtpGetCurrentDirectoryW (Internal)
931 * Retrieves the current directory
938 BOOL WINAPI FTP_FtpGetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs, LPWSTR lpszCurrentDirectory,
939 LPDWORD lpdwCurrentDirectory)
942 LPWININETAPPINFOW hIC = NULL;
943 DWORD bSuccess = FALSE;
945 TRACE("len(%d)\n", *lpdwCurrentDirectory);
947 /* Clear any error information */
948 INTERNET_SetLastError(0);
950 ZeroMemory(lpszCurrentDirectory, *lpdwCurrentDirectory);
952 hIC = lpwfs->lpAppInfo;
953 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PWD, NULL,
954 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
957 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
960 if (nResCode == 257) /* Extract directory name */
962 DWORD firstpos, lastpos, len;
963 LPWSTR lpszResponseBuffer = WININET_strdup_AtoW(INTERNET_GetResponseBuffer());
965 for (firstpos = 0, lastpos = 0; lpszResponseBuffer[lastpos]; lastpos++)
967 if ('"' == lpszResponseBuffer[lastpos])
976 len = lastpos - firstpos - 1;
977 lstrcpynW(lpszCurrentDirectory, &lpszResponseBuffer[firstpos+1], *lpdwCurrentDirectory);
978 HeapFree(GetProcessHeap(), 0, lpszResponseBuffer);
979 *lpdwCurrentDirectory = len;
983 FTP_SetResponseError(nResCode);
987 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
989 INTERNET_ASYNC_RESULT iar;
991 iar.dwResult = (DWORD)bSuccess;
992 iar.dwError = bSuccess ? ERROR_SUCCESS : ERROR_INTERNET_EXTENDED_ERROR;
993 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
994 &iar, sizeof(INTERNET_ASYNC_RESULT));
997 return (DWORD) bSuccess;
1000 /***********************************************************************
1001 * FtpOpenFileA (WININET.@)
1003 * Open a remote file for writing or reading
1006 * HINTERNET handle on success
1010 HINTERNET WINAPI FtpOpenFileA(HINTERNET hFtpSession,
1011 LPCSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1012 DWORD_PTR dwContext)
1014 LPWSTR lpwzFileName;
1017 lpwzFileName = lpszFileName?WININET_strdup_AtoW(lpszFileName):NULL;
1018 ret = FtpOpenFileW(hFtpSession, lpwzFileName, fdwAccess, dwFlags, dwContext);
1019 HeapFree(GetProcessHeap(), 0, lpwzFileName);
1024 static void AsyncFtpOpenFileProc(WORKREQUEST *workRequest)
1026 struct WORKREQ_FTPOPENFILEW const *req = &workRequest->u.FtpOpenFileW;
1027 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1029 TRACE("%p\n", lpwfs);
1031 FTP_FtpOpenFileW(lpwfs, req->lpszFilename,
1032 req->dwAccess, req->dwFlags, req->dwContext);
1033 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
1036 /***********************************************************************
1037 * FtpOpenFileW (WININET.@)
1039 * Open a remote file for writing or reading
1042 * HINTERNET handle on success
1046 HINTERNET WINAPI FtpOpenFileW(HINTERNET hFtpSession,
1047 LPCWSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1048 DWORD_PTR dwContext)
1050 LPWININETFTPSESSIONW lpwfs;
1051 LPWININETAPPINFOW hIC = NULL;
1054 TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession,
1055 debugstr_w(lpszFileName), fdwAccess, dwFlags, dwContext);
1057 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1060 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1064 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1066 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1070 if ((!lpszFileName) ||
1071 ((fdwAccess != GENERIC_READ) && (fdwAccess != GENERIC_WRITE)) ||
1072 ((dwFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY))
1074 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1078 if (lpwfs->download_in_progress != NULL)
1080 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1084 hIC = lpwfs->lpAppInfo;
1085 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1087 WORKREQUEST workRequest;
1088 struct WORKREQ_FTPOPENFILEW *req;
1090 workRequest.asyncproc = AsyncFtpOpenFileProc;
1091 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1092 req = &workRequest.u.FtpOpenFileW;
1093 req->lpszFilename = WININET_strdupW(lpszFileName);
1094 req->dwAccess = fdwAccess;
1095 req->dwFlags = dwFlags;
1096 req->dwContext = dwContext;
1098 INTERNET_AsyncCall(&workRequest);
1103 r = FTP_FtpOpenFileW(lpwfs, lpszFileName, fdwAccess, dwFlags, dwContext);
1107 WININET_Release( &lpwfs->hdr );
1113 /***********************************************************************
1114 * FTP_FtpOpenFileW (Internal)
1116 * Open a remote file for writing or reading
1119 * HINTERNET handle on success
1123 HINTERNET FTP_FtpOpenFileW(LPWININETFTPSESSIONW lpwfs,
1124 LPCWSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1125 DWORD_PTR dwContext)
1128 BOOL bSuccess = FALSE;
1129 LPWININETFTPFILE lpwh = NULL;
1130 LPWININETAPPINFOW hIC = NULL;
1131 HINTERNET handle = NULL;
1135 /* Clear any error information */
1136 INTERNET_SetLastError(0);
1138 if (GENERIC_READ == fdwAccess)
1140 /* Set up socket to retrieve data */
1141 bSuccess = FTP_SendRetrieve(lpwfs, lpszFileName, dwFlags);
1143 else if (GENERIC_WRITE == fdwAccess)
1145 /* Set up socket to send data */
1146 bSuccess = FTP_SendStore(lpwfs, lpszFileName, dwFlags);
1149 /* Get data socket to server */
1150 if (bSuccess && FTP_GetDataSocket(lpwfs, &nDataSocket))
1152 lpwh = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPFILE));
1153 lpwh->hdr.htype = WH_HFILE;
1154 lpwh->hdr.dwFlags = dwFlags;
1155 lpwh->hdr.dwContext = dwContext;
1156 lpwh->hdr.dwRefCount = 1;
1157 lpwh->hdr.close_connection = NULL;
1158 lpwh->hdr.destroy = FTP_CloseFileTransferHandle;
1159 lpwh->hdr.lpfnStatusCB = lpwfs->hdr.lpfnStatusCB;
1160 lpwh->nDataSocket = nDataSocket;
1161 lpwh->session_deleted = FALSE;
1163 WININET_AddRef( &lpwfs->hdr );
1164 lpwh->lpFtpSession = lpwfs;
1165 list_add_head( &lpwfs->hdr.children, &lpwh->hdr.entry );
1167 handle = WININET_AllocHandle( &lpwh->hdr );
1171 /* Indicate that a download is currently in progress */
1172 lpwfs->download_in_progress = lpwh;
1175 if (lpwfs->lstnSocket != -1)
1176 closesocket(lpwfs->lstnSocket);
1178 hIC = lpwfs->lpAppInfo;
1179 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1181 INTERNET_ASYNC_RESULT iar;
1185 iar.dwResult = (DWORD)handle;
1186 iar.dwError = ERROR_SUCCESS;
1187 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_HANDLE_CREATED,
1188 &iar, sizeof(INTERNET_ASYNC_RESULT));
1191 iar.dwResult = (DWORD)bSuccess;
1192 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1193 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1194 &iar, sizeof(INTERNET_ASYNC_RESULT));
1199 WININET_Release( &lpwh->hdr );
1205 /***********************************************************************
1206 * FtpGetFileA (WININET.@)
1208 * Retrieve file from the FTP server
1215 BOOL WINAPI FtpGetFileA(HINTERNET hInternet, LPCSTR lpszRemoteFile, LPCSTR lpszNewFile,
1216 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1217 DWORD_PTR dwContext)
1219 LPWSTR lpwzRemoteFile;
1223 lpwzRemoteFile = lpszRemoteFile?WININET_strdup_AtoW(lpszRemoteFile):NULL;
1224 lpwzNewFile = lpszNewFile?WININET_strdup_AtoW(lpszNewFile):NULL;
1225 ret = FtpGetFileW(hInternet, lpwzRemoteFile, lpwzNewFile, fFailIfExists,
1226 dwLocalFlagsAttribute, dwInternetFlags, dwContext);
1227 HeapFree(GetProcessHeap(), 0, lpwzRemoteFile);
1228 HeapFree(GetProcessHeap(), 0, lpwzNewFile);
1233 static void AsyncFtpGetFileProc(WORKREQUEST *workRequest)
1235 struct WORKREQ_FTPGETFILEW const *req = &workRequest->u.FtpGetFileW;
1236 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1238 TRACE("%p\n", lpwfs);
1240 FTP_FtpGetFileW(lpwfs, req->lpszRemoteFile,
1241 req->lpszNewFile, req->fFailIfExists,
1242 req->dwLocalFlagsAttribute, req->dwFlags, req->dwContext);
1243 HeapFree(GetProcessHeap(), 0, req->lpszRemoteFile);
1244 HeapFree(GetProcessHeap(), 0, req->lpszNewFile);
1248 /***********************************************************************
1249 * FtpGetFileW (WININET.@)
1251 * Retrieve file from the FTP server
1258 BOOL WINAPI FtpGetFileW(HINTERNET hInternet, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
1259 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1260 DWORD_PTR dwContext)
1262 LPWININETFTPSESSIONW lpwfs;
1263 LPWININETAPPINFOW hIC = NULL;
1266 if (!lpszRemoteFile || !lpszNewFile)
1268 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1272 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hInternet );
1275 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1279 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1281 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1285 if ((dwInternetFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY)
1287 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1291 if (lpwfs->download_in_progress != NULL)
1293 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1297 hIC = lpwfs->lpAppInfo;
1298 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1300 WORKREQUEST workRequest;
1301 struct WORKREQ_FTPGETFILEW *req;
1303 workRequest.asyncproc = AsyncFtpGetFileProc;
1304 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1305 req = &workRequest.u.FtpGetFileW;
1306 req->lpszRemoteFile = WININET_strdupW(lpszRemoteFile);
1307 req->lpszNewFile = WININET_strdupW(lpszNewFile);
1308 req->dwLocalFlagsAttribute = dwLocalFlagsAttribute;
1309 req->fFailIfExists = fFailIfExists;
1310 req->dwFlags = dwInternetFlags;
1311 req->dwContext = dwContext;
1313 r = INTERNET_AsyncCall(&workRequest);
1317 r = FTP_FtpGetFileW(lpwfs, lpszRemoteFile, lpszNewFile,
1318 fFailIfExists, dwLocalFlagsAttribute, dwInternetFlags, dwContext);
1322 WININET_Release( &lpwfs->hdr );
1328 /***********************************************************************
1329 * FTP_FtpGetFileW (Internal)
1331 * Retrieve file from the FTP server
1338 BOOL WINAPI FTP_FtpGetFileW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
1339 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1340 DWORD_PTR dwContext)
1342 BOOL bSuccess = FALSE;
1344 LPWININETAPPINFOW hIC = NULL;
1346 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile), debugstr_w(lpszNewFile));
1348 /* Clear any error information */
1349 INTERNET_SetLastError(0);
1351 /* Ensure we can write to lpszNewfile by opening it */
1352 hFile = CreateFileW(lpszNewFile, GENERIC_WRITE, 0, 0, fFailIfExists ?
1353 CREATE_NEW : CREATE_ALWAYS, dwLocalFlagsAttribute, 0);
1354 if (INVALID_HANDLE_VALUE == hFile)
1357 /* Set up socket to retrieve data */
1358 if (FTP_SendRetrieve(lpwfs, lpszRemoteFile, dwInternetFlags))
1362 /* Get data socket to server */
1363 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
1368 FTP_RetrieveFileData(lpwfs, nDataSocket, hFile);
1369 closesocket(nDataSocket);
1371 nResCode = FTP_ReceiveResponse(lpwfs, dwContext);
1374 if (nResCode == 226)
1377 FTP_SetResponseError(nResCode);
1382 if (lpwfs->lstnSocket != -1)
1383 closesocket(lpwfs->lstnSocket);
1387 hIC = lpwfs->lpAppInfo;
1388 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1390 INTERNET_ASYNC_RESULT iar;
1392 iar.dwResult = (DWORD)bSuccess;
1393 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1394 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1395 &iar, sizeof(INTERNET_ASYNC_RESULT));
1401 /***********************************************************************
1402 * FtpGetFileSize (WININET.@)
1404 DWORD WINAPI FtpGetFileSize( HINTERNET hFile, LPDWORD lpdwFileSizeHigh )
1406 FIXME("(%p, %p)\n", hFile, lpdwFileSizeHigh);
1408 if (lpdwFileSizeHigh)
1409 *lpdwFileSizeHigh = 0;
1414 /***********************************************************************
1415 * FtpDeleteFileA (WININET.@)
1417 * Delete a file on the ftp server
1424 BOOL WINAPI FtpDeleteFileA(HINTERNET hFtpSession, LPCSTR lpszFileName)
1426 LPWSTR lpwzFileName;
1429 lpwzFileName = lpszFileName?WININET_strdup_AtoW(lpszFileName):NULL;
1430 ret = FtpDeleteFileW(hFtpSession, lpwzFileName);
1431 HeapFree(GetProcessHeap(), 0, lpwzFileName);
1435 static void AsyncFtpDeleteFileProc(WORKREQUEST *workRequest)
1437 struct WORKREQ_FTPDELETEFILEW const *req = &workRequest->u.FtpDeleteFileW;
1438 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1440 TRACE("%p\n", lpwfs);
1442 FTP_FtpDeleteFileW(lpwfs, req->lpszFilename);
1443 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
1446 /***********************************************************************
1447 * FtpDeleteFileW (WININET.@)
1449 * Delete a file on the ftp server
1456 BOOL WINAPI FtpDeleteFileW(HINTERNET hFtpSession, LPCWSTR lpszFileName)
1458 LPWININETFTPSESSIONW lpwfs;
1459 LPWININETAPPINFOW hIC = NULL;
1462 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1465 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1469 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1471 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1475 if (lpwfs->download_in_progress != NULL)
1477 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1483 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1487 hIC = lpwfs->lpAppInfo;
1488 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1490 WORKREQUEST workRequest;
1491 struct WORKREQ_FTPDELETEFILEW *req;
1493 workRequest.asyncproc = AsyncFtpDeleteFileProc;
1494 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1495 req = &workRequest.u.FtpDeleteFileW;
1496 req->lpszFilename = WININET_strdupW(lpszFileName);
1498 r = INTERNET_AsyncCall(&workRequest);
1502 r = FTP_FtpDeleteFileW(lpwfs, lpszFileName);
1506 WININET_Release( &lpwfs->hdr );
1511 /***********************************************************************
1512 * FTP_FtpDeleteFileW (Internal)
1514 * Delete a file on the ftp server
1521 BOOL FTP_FtpDeleteFileW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszFileName)
1524 BOOL bSuccess = FALSE;
1525 LPWININETAPPINFOW hIC = NULL;
1527 TRACE("%p\n", lpwfs);
1529 /* Clear any error information */
1530 INTERNET_SetLastError(0);
1532 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_DELE, lpszFileName, 0, 0, 0))
1535 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1538 if (nResCode == 250)
1541 FTP_SetResponseError(nResCode);
1544 hIC = lpwfs->lpAppInfo;
1545 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1547 INTERNET_ASYNC_RESULT iar;
1549 iar.dwResult = (DWORD)bSuccess;
1550 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1551 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1552 &iar, sizeof(INTERNET_ASYNC_RESULT));
1559 /***********************************************************************
1560 * FtpRemoveDirectoryA (WININET.@)
1562 * Remove a directory on the ftp server
1569 BOOL WINAPI FtpRemoveDirectoryA(HINTERNET hFtpSession, LPCSTR lpszDirectory)
1571 LPWSTR lpwzDirectory;
1574 lpwzDirectory = lpszDirectory?WININET_strdup_AtoW(lpszDirectory):NULL;
1575 ret = FtpRemoveDirectoryW(hFtpSession, lpwzDirectory);
1576 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
1580 static void AsyncFtpRemoveDirectoryProc(WORKREQUEST *workRequest)
1582 struct WORKREQ_FTPREMOVEDIRECTORYW const *req = &workRequest->u.FtpRemoveDirectoryW;
1583 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1585 TRACE("%p\n", lpwfs);
1587 FTP_FtpRemoveDirectoryW(lpwfs, req->lpszDirectory);
1588 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
1591 /***********************************************************************
1592 * FtpRemoveDirectoryW (WININET.@)
1594 * Remove a directory on the ftp server
1601 BOOL WINAPI FtpRemoveDirectoryW(HINTERNET hFtpSession, LPCWSTR lpszDirectory)
1603 LPWININETFTPSESSIONW lpwfs;
1604 LPWININETAPPINFOW hIC = NULL;
1607 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1610 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1614 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1616 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1620 if (lpwfs->download_in_progress != NULL)
1622 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1628 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1632 hIC = lpwfs->lpAppInfo;
1633 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1635 WORKREQUEST workRequest;
1636 struct WORKREQ_FTPREMOVEDIRECTORYW *req;
1638 workRequest.asyncproc = AsyncFtpRemoveDirectoryProc;
1639 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1640 req = &workRequest.u.FtpRemoveDirectoryW;
1641 req->lpszDirectory = WININET_strdupW(lpszDirectory);
1643 r = INTERNET_AsyncCall(&workRequest);
1647 r = FTP_FtpRemoveDirectoryW(lpwfs, lpszDirectory);
1651 WININET_Release( &lpwfs->hdr );
1656 /***********************************************************************
1657 * FTP_FtpRemoveDirectoryW (Internal)
1659 * Remove a directory on the ftp server
1666 BOOL FTP_FtpRemoveDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory)
1669 BOOL bSuccess = FALSE;
1670 LPWININETAPPINFOW hIC = NULL;
1674 /* Clear any error information */
1675 INTERNET_SetLastError(0);
1677 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RMD, lpszDirectory, 0, 0, 0))
1680 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1683 if (nResCode == 250)
1686 FTP_SetResponseError(nResCode);
1690 hIC = lpwfs->lpAppInfo;
1691 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1693 INTERNET_ASYNC_RESULT iar;
1695 iar.dwResult = (DWORD)bSuccess;
1696 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1697 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1698 &iar, sizeof(INTERNET_ASYNC_RESULT));
1705 /***********************************************************************
1706 * FtpRenameFileA (WININET.@)
1708 * Rename a file on the ftp server
1715 BOOL WINAPI FtpRenameFileA(HINTERNET hFtpSession, LPCSTR lpszSrc, LPCSTR lpszDest)
1721 lpwzSrc = lpszSrc?WININET_strdup_AtoW(lpszSrc):NULL;
1722 lpwzDest = lpszDest?WININET_strdup_AtoW(lpszDest):NULL;
1723 ret = FtpRenameFileW(hFtpSession, lpwzSrc, lpwzDest);
1724 HeapFree(GetProcessHeap(), 0, lpwzSrc);
1725 HeapFree(GetProcessHeap(), 0, lpwzDest);
1729 static void AsyncFtpRenameFileProc(WORKREQUEST *workRequest)
1731 struct WORKREQ_FTPRENAMEFILEW const *req = &workRequest->u.FtpRenameFileW;
1732 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1734 TRACE("%p\n", lpwfs);
1736 FTP_FtpRenameFileW(lpwfs, req->lpszSrcFile, req->lpszDestFile);
1737 HeapFree(GetProcessHeap(), 0, req->lpszSrcFile);
1738 HeapFree(GetProcessHeap(), 0, req->lpszDestFile);
1741 /***********************************************************************
1742 * FtpRenameFileW (WININET.@)
1744 * Rename a file on the ftp server
1751 BOOL WINAPI FtpRenameFileW(HINTERNET hFtpSession, LPCWSTR lpszSrc, LPCWSTR lpszDest)
1753 LPWININETFTPSESSIONW lpwfs;
1754 LPWININETAPPINFOW hIC = NULL;
1757 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1760 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1764 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1766 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1770 if (lpwfs->download_in_progress != NULL)
1772 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1776 if (!lpszSrc || !lpszDest)
1778 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1782 hIC = lpwfs->lpAppInfo;
1783 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1785 WORKREQUEST workRequest;
1786 struct WORKREQ_FTPRENAMEFILEW *req;
1788 workRequest.asyncproc = AsyncFtpRenameFileProc;
1789 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1790 req = &workRequest.u.FtpRenameFileW;
1791 req->lpszSrcFile = WININET_strdupW(lpszSrc);
1792 req->lpszDestFile = WININET_strdupW(lpszDest);
1794 r = INTERNET_AsyncCall(&workRequest);
1798 r = FTP_FtpRenameFileW(lpwfs, lpszSrc, lpszDest);
1802 WININET_Release( &lpwfs->hdr );
1807 /***********************************************************************
1808 * FTP_FtpRenameFileW (Internal)
1810 * Rename a file on the ftp server
1817 BOOL FTP_FtpRenameFileW( LPWININETFTPSESSIONW lpwfs,
1818 LPCWSTR lpszSrc, LPCWSTR lpszDest)
1821 BOOL bSuccess = FALSE;
1822 LPWININETAPPINFOW hIC = NULL;
1826 /* Clear any error information */
1827 INTERNET_SetLastError(0);
1829 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RNFR, lpszSrc, 0, 0, 0))
1832 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1833 if (nResCode == 350)
1835 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RNTO, lpszDest, 0, 0, 0))
1838 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1841 if (nResCode == 250)
1844 FTP_SetResponseError(nResCode);
1847 hIC = lpwfs->lpAppInfo;
1848 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1850 INTERNET_ASYNC_RESULT iar;
1852 iar.dwResult = (DWORD)bSuccess;
1853 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1854 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1855 &iar, sizeof(INTERNET_ASYNC_RESULT));
1861 /***********************************************************************
1862 * FtpCommandA (WININET.@)
1864 BOOL WINAPI FtpCommandA( HINTERNET hConnect, BOOL fExpectResponse, DWORD dwFlags,
1865 LPCSTR lpszCommand, DWORD_PTR dwContext, HINTERNET* phFtpCommand )
1870 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect, fExpectResponse, dwFlags,
1871 debugstr_a(lpszCommand), dwContext, phFtpCommand);
1873 if (fExpectResponse)
1875 FIXME("data connection not supported\n");
1879 if (!lpszCommand || !lpszCommand[0])
1881 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1885 if (!(cmdW = WININET_strdup_AtoW(lpszCommand)))
1887 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1891 r = FtpCommandW(hConnect, fExpectResponse, dwFlags, cmdW, dwContext, phFtpCommand);
1893 HeapFree(GetProcessHeap(), 0, cmdW);
1897 /***********************************************************************
1898 * FtpCommandW (WININET.@)
1900 BOOL WINAPI FtpCommandW( HINTERNET hConnect, BOOL fExpectResponse, DWORD dwFlags,
1901 LPCWSTR lpszCommand, DWORD_PTR dwContext, HINTERNET* phFtpCommand )
1904 LPWININETFTPSESSIONW lpwfs;
1906 DWORD len, nBytesSent= 0;
1907 INT nResCode, nRC = 0;
1909 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect, fExpectResponse, dwFlags,
1910 debugstr_w(lpszCommand), dwContext, phFtpCommand);
1912 if (!lpszCommand || !lpszCommand[0])
1914 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1918 if (fExpectResponse)
1920 FIXME("data connection not supported\n");
1924 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
1927 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1931 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1933 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1937 if (lpwfs->download_in_progress != NULL)
1939 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1943 len = WideCharToMultiByte(CP_ACP, 0, lpszCommand, -1, NULL, 0, NULL, NULL) + strlen(szCRLF);
1944 if ((cmd = HeapAlloc(GetProcessHeap(), 0, len )))
1945 WideCharToMultiByte(CP_ACP, 0, lpszCommand, -1, cmd, len, NULL, NULL);
1948 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1952 strcat(cmd, szCRLF);
1955 TRACE("Sending (%s) len(%d)\n", cmd, len);
1956 while ((nBytesSent < len) && (nRC != -1))
1958 nRC = send(lpwfs->sndSocket, cmd + nBytesSent, len - nBytesSent, 0);
1962 TRACE("Sent %d bytes\n", nRC);
1968 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1969 if (nResCode > 0 && nResCode < 400)
1972 FTP_SetResponseError(nResCode);
1976 WININET_Release( &lpwfs->hdr );
1977 HeapFree(GetProcessHeap(), 0, cmd);
1981 /***********************************************************************
1982 * FTP_Connect (internal)
1984 * Connect to a ftp server
1987 * HINTERNET a session handle on success
1992 * Windows uses 'anonymous' as the username, when given a NULL username
1993 * and a NULL password. The password is first looked up in:
1995 * HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\EmailName
1997 * If this entry is not present it uses the current username as the password.
2001 HINTERNET FTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2002 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2003 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
2004 DWORD dwInternalFlags)
2006 static const WCHAR szKey[] = {'S','o','f','t','w','a','r','e','\\',
2007 'M','i','c','r','o','s','o','f','t','\\',
2008 'W','i','n','d','o','w','s','\\',
2009 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2010 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
2011 static const WCHAR szValue[] = {'E','m','a','i','l','N','a','m','e',0};
2012 static const WCHAR szDefaultUsername[] = {'a','n','o','n','y','m','o','u','s','\0'};
2013 static const WCHAR szEmpty[] = {'\0'};
2014 struct sockaddr_in socketAddr;
2017 BOOL bSuccess = FALSE;
2018 LPWININETFTPSESSIONW lpwfs = NULL;
2019 HINTERNET handle = NULL;
2021 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
2022 hIC, debugstr_w(lpszServerName),
2023 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword));
2025 assert( hIC->hdr.htype == WH_HINIT );
2027 if (NULL == lpszUserName && NULL != lpszPassword)
2029 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2033 lpwfs = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPSESSIONW));
2036 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2040 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
2041 nServerPort = INTERNET_DEFAULT_FTP_PORT;
2043 lpwfs->hdr.htype = WH_HFTPSESSION;
2044 lpwfs->hdr.dwFlags = dwFlags;
2045 lpwfs->hdr.dwContext = dwContext;
2046 lpwfs->hdr.dwInternalFlags = dwInternalFlags;
2047 lpwfs->hdr.dwRefCount = 1;
2048 lpwfs->hdr.close_connection = FTP_CloseConnection;
2049 lpwfs->hdr.destroy = FTP_CloseSessionHandle;
2050 lpwfs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2051 lpwfs->download_in_progress = NULL;
2052 lpwfs->sndSocket = -1;
2053 lpwfs->lstnSocket = -1;
2054 lpwfs->pasvSocket = -1;
2056 WININET_AddRef( &hIC->hdr );
2057 lpwfs->lpAppInfo = hIC;
2058 list_add_head( &hIC->hdr.children, &lpwfs->hdr.entry );
2060 handle = WININET_AllocHandle( &lpwfs->hdr );
2063 ERR("Failed to alloc handle\n");
2064 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2068 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2069 if(strchrW(hIC->lpszProxy, ' '))
2070 FIXME("Several proxies not implemented.\n");
2071 if(hIC->lpszProxyBypass)
2072 FIXME("Proxy bypass is ignored.\n");
2074 if ( !lpszUserName) {
2076 WCHAR szPassword[MAX_PATH];
2077 DWORD len = sizeof(szPassword);
2079 lpwfs->lpszUserName = WININET_strdupW(szDefaultUsername);
2081 RegOpenKeyW(HKEY_CURRENT_USER, szKey, &key);
2082 if (RegQueryValueExW(key, szValue, NULL, NULL, (LPBYTE)szPassword, &len)) {
2083 /* Nothing in the registry, get the username and use that as the password */
2084 if (!GetUserNameW(szPassword, &len)) {
2085 /* Should never get here, but use an empty password as failsafe */
2086 strcpyW(szPassword, szEmpty);
2091 TRACE("Password used for anonymous ftp : (%s)\n", debugstr_w(szPassword));
2092 lpwfs->lpszPassword = WININET_strdupW(szPassword);
2095 lpwfs->lpszUserName = WININET_strdupW(lpszUserName);
2098 lpwfs->lpszPassword = WININET_strdupW(lpszPassword);
2100 lpwfs->lpszPassword = WININET_strdupW(szEmpty);
2103 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2104 if (!(lpwfs->hdr.dwInternalFlags & INET_OPENURL))
2106 INTERNET_ASYNC_RESULT iar;
2108 iar.dwResult = (DWORD)handle;
2109 iar.dwError = ERROR_SUCCESS;
2111 SendAsyncCallback(&hIC->hdr, dwContext,
2112 INTERNET_STATUS_HANDLE_CREATED, &iar,
2113 sizeof(INTERNET_ASYNC_RESULT));
2116 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_RESOLVING_NAME,
2117 (LPWSTR) lpszServerName, strlenW(lpszServerName));
2119 if (!GetAddress(lpszServerName, nServerPort, &socketAddr))
2121 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
2125 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_NAME_RESOLVED,
2126 (LPWSTR) lpszServerName, strlenW(lpszServerName));
2128 nsocket = socket(AF_INET,SOCK_STREAM,0);
2131 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2135 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_CONNECTING_TO_SERVER,
2136 &socketAddr, sizeof(struct sockaddr_in));
2138 if (connect(nsocket, (struct sockaddr *)&socketAddr, sizeof(socketAddr)) < 0)
2140 ERR("Unable to connect (%s)\n", strerror(errno));
2141 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2145 TRACE("Connected to server\n");
2146 lpwfs->sndSocket = nsocket;
2147 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_CONNECTED_TO_SERVER,
2148 &socketAddr, sizeof(struct sockaddr_in));
2150 sock_namelen = sizeof(lpwfs->socketAddress);
2151 getsockname(nsocket, (struct sockaddr *) &lpwfs->socketAddress, &sock_namelen);
2153 if (FTP_ConnectToHost(lpwfs))
2155 TRACE("Successfully logged into server\n");
2161 if (lpwfs) WININET_Release( &lpwfs->hdr );
2163 if (!bSuccess && handle)
2165 WININET_FreeHandle( handle );
2169 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2171 INTERNET_ASYNC_RESULT iar;
2173 iar.dwResult = bSuccess ? (DWORD_PTR)lpwfs : 0;
2174 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2175 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
2176 &iar, sizeof(INTERNET_ASYNC_RESULT));
2183 /***********************************************************************
2184 * FTP_ConnectToHost (internal)
2186 * Connect to a ftp server
2193 static BOOL FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs)
2196 BOOL bSuccess = FALSE;
2199 FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2201 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_USER, lpwfs->lpszUserName, 0, 0, 0))
2204 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2207 /* Login successful... */
2208 if (nResCode == 230)
2210 /* User name okay, need password... */
2211 else if (nResCode == 331)
2212 bSuccess = FTP_SendPassword(lpwfs);
2213 /* Need account for login... */
2214 else if (nResCode == 332)
2215 bSuccess = FTP_SendAccount(lpwfs);
2217 FTP_SetResponseError(nResCode);
2220 TRACE("Returning %d\n", bSuccess);
2226 /***********************************************************************
2227 * FTP_SendCommandA (internal)
2229 * Send command to server
2236 static BOOL FTP_SendCommandA(INT nSocket, FTP_COMMAND ftpCmd, LPCSTR lpszParam,
2237 INTERNET_STATUS_CALLBACK lpfnStatusCB, LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext)
2241 DWORD nBytesSent = 0;
2245 TRACE("%d: (%s) %d\n", ftpCmd, lpszParam, nSocket);
2249 lpfnStatusCB(hdr->hInternet, dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2252 dwParamLen = lpszParam?strlen(lpszParam)+1:0;
2253 len = dwParamLen + strlen(szFtpCommands[ftpCmd]) + strlen(szCRLF);
2254 if (NULL == (buf = HeapAlloc(GetProcessHeap(), 0, len+1)))
2256 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2259 sprintf(buf, "%s%s%s%s", szFtpCommands[ftpCmd], dwParamLen ? " " : "",
2260 dwParamLen ? lpszParam : "", szCRLF);
2262 TRACE("Sending (%s) len(%d)\n", buf, len);
2263 while((nBytesSent < len) && (nRC != -1))
2265 nRC = send(nSocket, buf+nBytesSent, len - nBytesSent, 0);
2269 HeapFree(GetProcessHeap(), 0, (LPVOID)buf);
2273 lpfnStatusCB(hdr->hInternet, dwContext, INTERNET_STATUS_REQUEST_SENT,
2274 &nBytesSent, sizeof(DWORD));
2277 TRACE("Sent %d bytes\n", nBytesSent);
2281 /***********************************************************************
2282 * FTP_SendCommand (internal)
2284 * Send command to server
2291 static BOOL FTP_SendCommand(INT nSocket, FTP_COMMAND ftpCmd, LPCWSTR lpszParam,
2292 INTERNET_STATUS_CALLBACK lpfnStatusCB, LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext)
2295 LPSTR lpszParamA = lpszParam?WININET_strdup_WtoA(lpszParam):NULL;
2296 ret = FTP_SendCommandA(nSocket, ftpCmd, lpszParamA, lpfnStatusCB, hdr, dwContext);
2297 HeapFree(GetProcessHeap(), 0, lpszParamA);
2301 /***********************************************************************
2302 * FTP_ReceiveResponse (internal)
2304 * Receive response from server
2307 * Reply code on success
2311 INT FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs, DWORD_PTR dwContext)
2313 LPSTR lpszResponse = INTERNET_GetResponseBuffer();
2316 char firstprefix[5];
2317 BOOL multiline = FALSE;
2318 LPWININETAPPINFOW hIC = NULL;
2320 TRACE("socket(%d)\n", lpwfs->sndSocket);
2322 hIC = lpwfs->lpAppInfo;
2323 SendAsyncCallback(&lpwfs->hdr, dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2327 if (!INTERNET_GetNextLine(lpwfs->sndSocket, &nRecv))
2334 if(lpszResponse[3] != '-')
2337 { /* Start of multiline repsonse. Loop until we get "nnn " */
2339 memcpy(firstprefix, lpszResponse, 3);
2340 firstprefix[3] = ' ';
2341 firstprefix[4] = '\0';
2346 if(!memcmp(firstprefix, lpszResponse, 4))
2354 rc = atoi(lpszResponse);
2356 SendAsyncCallback(&lpwfs->hdr, dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
2357 &nRecv, sizeof(DWORD));
2361 TRACE("return %d\n", rc);
2366 /***********************************************************************
2367 * FTP_SendPassword (internal)
2369 * Send password to ftp server
2376 static BOOL FTP_SendPassword(LPWININETFTPSESSIONW lpwfs)
2379 BOOL bSuccess = FALSE;
2382 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PASS, lpwfs->lpszPassword, 0, 0, 0))
2385 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2388 TRACE("Received reply code %d\n", nResCode);
2389 /* Login successful... */
2390 if (nResCode == 230)
2392 /* Command not implemented, superfluous at the server site... */
2393 /* Need account for login... */
2394 else if (nResCode == 332)
2395 bSuccess = FTP_SendAccount(lpwfs);
2397 FTP_SetResponseError(nResCode);
2401 TRACE("Returning %d\n", bSuccess);
2406 /***********************************************************************
2407 * FTP_SendAccount (internal)
2416 static BOOL FTP_SendAccount(LPWININETFTPSESSIONW lpwfs)
2419 BOOL bSuccess = FALSE;
2422 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_ACCT, szNoAccount, 0, 0, 0))
2425 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2429 FTP_SetResponseError(nResCode);
2436 /***********************************************************************
2437 * FTP_SendStore (internal)
2439 * Send request to upload file to ftp server
2446 static BOOL FTP_SendStore(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType)
2449 BOOL bSuccess = FALSE;
2452 if (!FTP_InitListenSocket(lpwfs))
2455 if (!FTP_SendType(lpwfs, dwType))
2458 if (!FTP_SendPortOrPasv(lpwfs))
2461 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_STOR, lpszRemoteFile, 0, 0, 0))
2463 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2466 if (nResCode == 150 || nResCode == 125)
2469 FTP_SetResponseError(nResCode);
2473 if (!bSuccess && lpwfs->lstnSocket != -1)
2475 closesocket(lpwfs->lstnSocket);
2476 lpwfs->lstnSocket = -1;
2483 /***********************************************************************
2484 * FTP_InitListenSocket (internal)
2486 * Create a socket to listen for server response
2493 static BOOL FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs)
2495 BOOL bSuccess = FALSE;
2496 socklen_t namelen = sizeof(struct sockaddr_in);
2500 lpwfs->lstnSocket = socket(PF_INET, SOCK_STREAM, 0);
2501 if (lpwfs->lstnSocket == -1)
2503 TRACE("Unable to create listening socket\n");
2507 /* We obtain our ip addr from the name of the command channel socket */
2508 lpwfs->lstnSocketAddress = lpwfs->socketAddress;
2510 /* and get the system to assign us a port */
2511 lpwfs->lstnSocketAddress.sin_port = htons((u_short) 0);
2513 if (bind(lpwfs->lstnSocket,(struct sockaddr *) &lpwfs->lstnSocketAddress, sizeof(struct sockaddr_in)) == -1)
2515 TRACE("Unable to bind socket\n");
2519 if (listen(lpwfs->lstnSocket, MAX_BACKLOG) == -1)
2521 TRACE("listen failed\n");
2525 if (getsockname(lpwfs->lstnSocket, (struct sockaddr *) &lpwfs->lstnSocketAddress, &namelen) != -1)
2529 if (!bSuccess && lpwfs->lstnSocket != -1)
2531 closesocket(lpwfs->lstnSocket);
2532 lpwfs->lstnSocket = -1;
2539 /***********************************************************************
2540 * FTP_SendType (internal)
2542 * Tell server type of data being transferred
2548 * W98SE doesn't cache the type that's currently set
2549 * (i.e. it sends it always),
2550 * so we probably don't want to do that either.
2552 static BOOL FTP_SendType(LPWININETFTPSESSIONW lpwfs, DWORD dwType)
2555 WCHAR type[] = { 'I','\0' };
2556 BOOL bSuccess = FALSE;
2559 if (dwType & INTERNET_FLAG_TRANSFER_ASCII)
2562 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_TYPE, type, 0, 0, 0))
2565 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext)/100;
2571 FTP_SetResponseError(nResCode);
2579 #if 0 /* FIXME: should probably be used for FtpGetFileSize */
2580 /***********************************************************************
2581 * FTP_GetFileSize (internal)
2583 * Retrieves from the server the size of the given file
2590 static BOOL FTP_GetFileSize(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD *dwSize)
2593 BOOL bSuccess = FALSE;
2597 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_SIZE, lpszRemoteFile, 0, 0, 0))
2600 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2603 if (nResCode == 213) {
2604 /* Now parses the output to get the actual file size */
2606 LPSTR lpszResponseBuffer = INTERNET_GetResponseBuffer();
2608 for (i = 0; (lpszResponseBuffer[i] != ' ') && (lpszResponseBuffer[i] != '\0'); i++) ;
2609 if (lpszResponseBuffer[i] == '\0') return FALSE;
2610 *dwSize = atol(&(lpszResponseBuffer[i + 1]));
2614 FTP_SetResponseError(nResCode);
2624 /***********************************************************************
2625 * FTP_SendPort (internal)
2627 * Tell server which port to use
2634 static BOOL FTP_SendPort(LPWININETFTPSESSIONW lpwfs)
2636 static const WCHAR szIPFormat[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
2638 WCHAR szIPAddress[64];
2639 BOOL bSuccess = FALSE;
2642 sprintfW(szIPAddress, szIPFormat,
2643 lpwfs->lstnSocketAddress.sin_addr.s_addr&0x000000FF,
2644 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0x0000FF00)>>8,
2645 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0x00FF0000)>>16,
2646 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0xFF000000)>>24,
2647 lpwfs->lstnSocketAddress.sin_port & 0xFF,
2648 (lpwfs->lstnSocketAddress.sin_port & 0xFF00)>>8);
2650 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PORT, szIPAddress, 0, 0, 0))
2653 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2656 if (nResCode == 200)
2659 FTP_SetResponseError(nResCode);
2667 /***********************************************************************
2668 * FTP_DoPassive (internal)
2670 * Tell server that we want to do passive transfers
2671 * and connect data socket
2678 static BOOL FTP_DoPassive(LPWININETFTPSESSIONW lpwfs)
2681 BOOL bSuccess = FALSE;
2684 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PASV, NULL, 0, 0, 0))
2687 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2690 if (nResCode == 227)
2692 LPSTR lpszResponseBuffer = INTERNET_GetResponseBuffer();
2696 char *pAddr, *pPort;
2698 struct sockaddr_in dataSocketAddress;
2700 p = lpszResponseBuffer+4; /* skip status code */
2701 while (*p != '\0' && (*p < '0' || *p > '9')) p++;
2705 ERR("no address found in response, aborting\n");
2709 if (sscanf(p, "%d,%d,%d,%d,%d,%d", &f[0], &f[1], &f[2], &f[3],
2712 ERR("unknown response address format '%s', aborting\n", p);
2715 for (i=0; i < 6; i++)
2718 dataSocketAddress = lpwfs->socketAddress;
2719 pAddr = (char *)&(dataSocketAddress.sin_addr.s_addr);
2720 pPort = (char *)&(dataSocketAddress.sin_port);
2728 nsocket = socket(AF_INET,SOCK_STREAM,0);
2732 if (connect(nsocket, (struct sockaddr *)&dataSocketAddress, sizeof(dataSocketAddress)))
2734 ERR("can't connect passive FTP data port.\n");
2735 closesocket(nsocket);
2738 lpwfs->pasvSocket = nsocket;
2742 FTP_SetResponseError(nResCode);
2750 static BOOL FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs)
2752 if (lpwfs->hdr.dwFlags & INTERNET_FLAG_PASSIVE)
2754 if (!FTP_DoPassive(lpwfs))
2759 if (!FTP_SendPort(lpwfs))
2766 /***********************************************************************
2767 * FTP_GetDataSocket (internal)
2769 * Either accepts an incoming data socket connection from the server
2770 * or just returns the already opened socket after a PASV command
2771 * in case of passive FTP.
2779 static BOOL FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs, LPINT nDataSocket)
2781 struct sockaddr_in saddr;
2782 socklen_t addrlen = sizeof(struct sockaddr);
2785 if (lpwfs->hdr.dwFlags & INTERNET_FLAG_PASSIVE)
2787 *nDataSocket = lpwfs->pasvSocket;
2791 *nDataSocket = accept(lpwfs->lstnSocket, (struct sockaddr *) &saddr, &addrlen);
2792 closesocket(lpwfs->lstnSocket);
2793 lpwfs->lstnSocket = -1;
2795 return *nDataSocket != -1;
2799 /***********************************************************************
2800 * FTP_SendData (internal)
2802 * Send data to the server
2809 static BOOL FTP_SendData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile)
2811 BY_HANDLE_FILE_INFORMATION fi;
2812 DWORD nBytesRead = 0;
2813 DWORD nBytesSent = 0;
2814 DWORD nTotalSent = 0;
2815 DWORD nBytesToSend, nLen;
2817 time_t s_long_time, e_long_time;
2822 lpszBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHAR)*DATA_PACKET_SIZE);
2824 /* Get the size of the file. */
2825 GetFileInformationByHandle(hFile, &fi);
2830 nBytesToSend = nBytesRead - nBytesSent;
2832 if (nBytesToSend <= 0)
2834 /* Read data from file. */
2836 if (!ReadFile(hFile, lpszBuffer, DATA_PACKET_SIZE, &nBytesRead, 0))
2837 ERR("Failed reading from file\n");
2840 nBytesToSend = nBytesRead;
2845 nLen = DATA_PACKET_SIZE < nBytesToSend ?
2846 DATA_PACKET_SIZE : nBytesToSend;
2847 nRC = send(nDataSocket, lpszBuffer, nLen, 0);
2855 /* Do some computation to display the status. */
2857 nSeconds = e_long_time - s_long_time;
2858 if( nSeconds / 60 > 0 )
2860 TRACE( "%d bytes of %d bytes (%d%%) in %d min %d sec estimated remaining time %d sec\n",
2861 nTotalSent, fi.nFileSizeLow, nTotalSent*100/fi.nFileSizeLow, nSeconds / 60,
2862 nSeconds % 60, (fi.nFileSizeLow - nTotalSent) * nSeconds / nTotalSent );
2866 TRACE( "%d bytes of %d bytes (%d%%) in %d sec estimated remaining time %d sec\n",
2867 nTotalSent, fi.nFileSizeLow, nTotalSent*100/fi.nFileSizeLow, nSeconds,
2868 (fi.nFileSizeLow - nTotalSent) * nSeconds / nTotalSent);
2870 } while (nRC != -1);
2872 TRACE("file transfer complete!\n");
2874 HeapFree(GetProcessHeap(), 0, lpszBuffer);
2880 /***********************************************************************
2881 * FTP_SendRetrieve (internal)
2883 * Send request to retrieve a file
2886 * Number of bytes to be received on success
2890 static BOOL FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType)
2896 if (!(ret = FTP_InitListenSocket(lpwfs)))
2899 if (!(ret = FTP_SendType(lpwfs, dwType)))
2902 if (!(ret = FTP_SendPortOrPasv(lpwfs)))
2905 if (!(ret = FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RETR, lpszRemoteFile, 0, 0, 0)))
2908 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2909 if ((nResCode != 125) && (nResCode != 150)) {
2910 /* That means that we got an error getting the file. */
2911 FTP_SetResponseError(nResCode);
2916 if (!ret && lpwfs->lstnSocket != -1)
2918 closesocket(lpwfs->lstnSocket);
2919 lpwfs->lstnSocket = -1;
2926 /***********************************************************************
2927 * FTP_RetrieveData (internal)
2929 * Retrieve data from server
2936 static BOOL FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile)
2938 DWORD nBytesWritten;
2939 DWORD nBytesReceived = 0;
2945 lpszBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHAR)*DATA_PACKET_SIZE);
2946 if (NULL == lpszBuffer)
2948 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2954 nRC = recv(nDataSocket, lpszBuffer, DATA_PACKET_SIZE, 0);
2957 /* other side closed socket. */
2960 WriteFile(hFile, lpszBuffer, nRC, &nBytesWritten, NULL);
2961 nBytesReceived += nRC;
2965 TRACE("Data transfer complete\n");
2968 HeapFree(GetProcessHeap(), 0, lpszBuffer);
2973 /***********************************************************************
2974 * FTP_CloseConnection (internal)
2978 static void FTP_CloseConnection(LPWININETHANDLEHEADER hdr)
2980 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) hdr;
2984 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext,
2985 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2987 if (lpwfs->download_in_progress != NULL)
2988 lpwfs->download_in_progress->session_deleted = TRUE;
2990 if (lpwfs->sndSocket != -1)
2991 closesocket(lpwfs->sndSocket);
2993 if (lpwfs->lstnSocket != -1)
2994 closesocket(lpwfs->lstnSocket);
2996 if (lpwfs->pasvSocket != -1)
2997 closesocket(lpwfs->pasvSocket);
2999 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext,
3000 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
3004 /***********************************************************************
3005 * FTP_CloseSessionHandle (internal)
3007 * Deallocate session handle
3009 static void FTP_CloseSessionHandle(LPWININETHANDLEHEADER hdr)
3011 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) hdr;
3015 WININET_Release(&lpwfs->lpAppInfo->hdr);
3017 HeapFree(GetProcessHeap(), 0, lpwfs->lpszPassword);
3018 HeapFree(GetProcessHeap(), 0, lpwfs->lpszUserName);
3019 HeapFree(GetProcessHeap(), 0, lpwfs);
3023 /***********************************************************************
3024 * FTP_FindNextFileW (Internal)
3026 * Continues a file search from a previous call to FindFirstFile
3033 BOOL WINAPI FTP_FindNextFileW(LPWININETFTPFINDNEXTW lpwh, LPVOID lpvFindData)
3035 BOOL bSuccess = TRUE;
3036 LPWIN32_FIND_DATAW lpFindFileData;
3038 TRACE("index(%d) size(%d)\n", lpwh->index, lpwh->size);
3040 assert (lpwh->hdr.htype == WH_HFTPFINDNEXT);
3042 /* Clear any error information */
3043 INTERNET_SetLastError(0);
3045 lpFindFileData = (LPWIN32_FIND_DATAW) lpvFindData;
3046 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
3048 if (lpwh->index >= lpwh->size)
3050 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
3055 FTP_ConvertFileProp(&lpwh->lpafp[lpwh->index], lpFindFileData);
3058 TRACE("\nName: %s\nSize: %d\n", debugstr_w(lpFindFileData->cFileName), lpFindFileData->nFileSizeLow);
3062 if (lpwh->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3064 INTERNET_ASYNC_RESULT iar;
3066 iar.dwResult = (DWORD)bSuccess;
3067 iar.dwError = iar.dwError = bSuccess ? ERROR_SUCCESS :
3068 INTERNET_GetLastError();
3070 INTERNET_SendCallback(&lpwh->hdr, lpwh->hdr.dwContext,
3071 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3072 sizeof(INTERNET_ASYNC_RESULT));
3079 /***********************************************************************
3080 * FTP_CloseFindNextHandle (internal)
3082 * Deallocate session handle
3089 static void FTP_CloseFindNextHandle(LPWININETHANDLEHEADER hdr)
3091 LPWININETFTPFINDNEXTW lpwfn = (LPWININETFTPFINDNEXTW) hdr;
3096 WININET_Release(&lpwfn->lpFtpSession->hdr);
3098 for (i = 0; i < lpwfn->size; i++)
3100 HeapFree(GetProcessHeap(), 0, lpwfn->lpafp[i].lpszName);
3103 HeapFree(GetProcessHeap(), 0, lpwfn->lpafp);
3104 HeapFree(GetProcessHeap(), 0, lpwfn);
3107 /***********************************************************************
3108 * FTP_CloseFileTransferHandle (internal)
3110 * Closes the file transfer handle. This also 'cleans' the data queue of
3111 * the 'transfer complete' message (this is a bit of a hack though :-/ )
3114 static void FTP_CloseFileTransferHandle(LPWININETHANDLEHEADER hdr)
3116 LPWININETFTPFILE lpwh = (LPWININETFTPFILE) hdr;
3117 LPWININETFTPSESSIONW lpwfs = lpwh->lpFtpSession;
3122 WININET_Release(&lpwh->lpFtpSession->hdr);
3124 if (!lpwh->session_deleted)
3125 lpwfs->download_in_progress = NULL;
3127 if (lpwh->nDataSocket != -1)
3128 closesocket(lpwh->nDataSocket);
3130 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
3131 if (nResCode > 0 && nResCode != 226) WARN("server reports failed transfer\n");
3133 HeapFree(GetProcessHeap(), 0, lpwh);
3136 /***********************************************************************
3137 * FTP_ReceiveFileList (internal)
3139 * Read file list from server
3142 * Handle to file list on success
3146 static HINTERNET FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
3147 LPWIN32_FIND_DATAW lpFindFileData, DWORD_PTR dwContext)
3150 LPFILEPROPERTIESW lpafp = NULL;
3151 LPWININETFTPFINDNEXTW lpwfn = NULL;
3152 HINTERNET handle = 0;
3154 TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs, nSocket, debugstr_w(lpszSearchFile), lpFindFileData, dwContext);
3156 if (FTP_ParseDirectory(lpwfs, nSocket, lpszSearchFile, &lpafp, &dwSize))
3159 FTP_ConvertFileProp(lpafp, lpFindFileData);
3161 lpwfn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETFTPFINDNEXTW));
3164 lpwfn->hdr.htype = WH_HFTPFINDNEXT;
3165 lpwfn->hdr.dwContext = dwContext;
3166 lpwfn->hdr.dwRefCount = 1;
3167 lpwfn->hdr.close_connection = NULL;
3168 lpwfn->hdr.destroy = FTP_CloseFindNextHandle;
3169 lpwfn->hdr.lpfnStatusCB = lpwfs->hdr.lpfnStatusCB;
3170 lpwfn->index = 1; /* Next index is 1 since we return index 0 */
3171 lpwfn->size = dwSize;
3172 lpwfn->lpafp = lpafp;
3174 WININET_AddRef( &lpwfs->hdr );
3175 lpwfn->lpFtpSession = lpwfs;
3176 list_add_head( &lpwfs->hdr.children, &lpwfn->hdr.entry );
3178 handle = WININET_AllocHandle( &lpwfn->hdr );
3183 WININET_Release( &lpwfn->hdr );
3185 TRACE("Matched %d files\n", dwSize);
3190 /***********************************************************************
3191 * FTP_ConvertFileProp (internal)
3193 * Converts FILEPROPERTIESW struct to WIN32_FIND_DATAA
3200 BOOL FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp, LPWIN32_FIND_DATAW lpFindFileData)
3202 BOOL bSuccess = FALSE;
3204 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAW));
3208 /* Convert 'Unix' time to Windows time */
3209 RtlSecondsSince1970ToTime(mktime(&lpafp->tmLastModified),
3210 (LARGE_INTEGER *) &(lpFindFileData->ftLastAccessTime));
3211 lpFindFileData->ftLastWriteTime = lpFindFileData->ftLastAccessTime;
3212 lpFindFileData->ftCreationTime = lpFindFileData->ftLastAccessTime;
3214 /* Not all fields are filled in */
3215 lpFindFileData->nFileSizeHigh = 0; /* We do not handle files bigger than 0xFFFFFFFF bytes yet :-) */
3216 lpFindFileData->nFileSizeLow = lpafp->nSize;
3218 if (lpafp->bIsDirectory)
3219 lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
3221 if (lpafp->lpszName)
3222 lstrcpynW(lpFindFileData->cFileName, lpafp->lpszName, MAX_PATH);
3230 /***********************************************************************
3231 * FTP_ParseNextFile (internal)
3233 * Parse the next line in file listing
3239 static BOOL FTP_ParseNextFile(INT nSocket, LPCWSTR lpszSearchFile, LPFILEPROPERTIESW lpfp)
3241 static const char szSpace[] = " \t";
3249 lpfp->lpszName = NULL;
3251 if(!(pszLine = INTERNET_GetNextLine(nSocket, &nBufLen)))
3254 pszToken = strtok(pszLine, szSpace);
3256 * <Permissions> <NoLinks> <owner> <group> <size> <date> <time or year> <filename>
3259 * drwx--s--- 2 pcarrier ens 512 Sep 28 1995 pcarrier
3261 if(!isdigit(pszToken[0]) && 10 == strlen(pszToken)) {
3262 if(!FTP_ParsePermission(pszToken, lpfp))
3263 lpfp->bIsDirectory = FALSE;
3264 for(i=0; i<=3; i++) {
3265 if(!(pszToken = strtok(NULL, szSpace)))
3268 if(!pszToken) continue;
3269 if(lpfp->bIsDirectory) {
3270 TRACE("Is directory\n");
3274 TRACE("Size: %s\n", pszToken);
3275 lpfp->nSize = atol(pszToken);
3278 lpfp->tmLastModified.tm_sec = 0;
3279 lpfp->tmLastModified.tm_min = 0;
3280 lpfp->tmLastModified.tm_hour = 0;
3281 lpfp->tmLastModified.tm_mday = 0;
3282 lpfp->tmLastModified.tm_mon = 0;
3283 lpfp->tmLastModified.tm_year = 0;
3285 /* Determine month */
3286 pszToken = strtok(NULL, szSpace);
3287 if(!pszToken) continue;
3288 if(strlen(pszToken) >= 3) {
3290 if((pszTmp = StrStrIA(szMonths, pszToken)))
3291 lpfp->tmLastModified.tm_mon = ((pszTmp - szMonths) / 3)+1;
3294 pszToken = strtok(NULL, szSpace);
3295 if(!pszToken) continue;
3296 lpfp->tmLastModified.tm_mday = atoi(pszToken);
3297 /* Determine time or year */
3298 pszToken = strtok(NULL, szSpace);
3299 if(!pszToken) continue;
3300 if((pszTmp = strchr(pszToken, ':'))) {
3305 lpfp->tmLastModified.tm_min = atoi(pszTmp);
3306 lpfp->tmLastModified.tm_hour = atoi(pszToken);
3308 apTM = localtime(&aTime);
3309 lpfp->tmLastModified.tm_year = apTM->tm_year;
3312 lpfp->tmLastModified.tm_year = atoi(pszToken) - 1900;
3313 lpfp->tmLastModified.tm_hour = 12;
3315 TRACE("Mod time: %02d:%02d:%02d %02d/%02d/%02d\n",
3316 lpfp->tmLastModified.tm_hour, lpfp->tmLastModified.tm_min, lpfp->tmLastModified.tm_sec,
3317 (lpfp->tmLastModified.tm_year >= 100) ? lpfp->tmLastModified.tm_year - 100 : lpfp->tmLastModified.tm_year,
3318 lpfp->tmLastModified.tm_mon, lpfp->tmLastModified.tm_mday);
3320 pszToken = strtok(NULL, szSpace);
3321 if(!pszToken) continue;
3322 lpfp->lpszName = WININET_strdup_AtoW(pszToken);
3323 TRACE("File: %s\n", debugstr_w(lpfp->lpszName));
3325 /* NT way of parsing ... :
3327 07-13-03 08:55PM <DIR> sakpatch
3328 05-09-03 06:02PM 12656686 2003-04-21bgm_cmd_e.rgz
3330 else if(isdigit(pszToken[0]) && 8 == strlen(pszToken)) {
3331 lpfp->permissions = 0xFFFF; /* No idea, put full permission :-) */
3333 sscanf(pszToken, "%d-%d-%d",
3334 &lpfp->tmLastModified.tm_mon,
3335 &lpfp->tmLastModified.tm_mday,
3336 &lpfp->tmLastModified.tm_year);
3338 /* Hacky and bad Y2K protection :-) */
3339 if (lpfp->tmLastModified.tm_year < 70)
3340 lpfp->tmLastModified.tm_year += 100;
3342 pszToken = strtok(NULL, szSpace);
3343 if(!pszToken) continue;
3344 sscanf(pszToken, "%d:%d",
3345 &lpfp->tmLastModified.tm_hour,
3346 &lpfp->tmLastModified.tm_min);
3347 if((pszToken[5] == 'P') && (pszToken[6] == 'M')) {
3348 lpfp->tmLastModified.tm_hour += 12;
3350 lpfp->tmLastModified.tm_sec = 0;
3352 TRACE("Mod time: %02d:%02d:%02d %02d/%02d/%02d\n",
3353 lpfp->tmLastModified.tm_hour, lpfp->tmLastModified.tm_min, lpfp->tmLastModified.tm_sec,
3354 (lpfp->tmLastModified.tm_year >= 100) ? lpfp->tmLastModified.tm_year - 100 : lpfp->tmLastModified.tm_year,
3355 lpfp->tmLastModified.tm_mon, lpfp->tmLastModified.tm_mday);
3357 pszToken = strtok(NULL, szSpace);
3358 if(!pszToken) continue;
3359 if(!strcasecmp(pszToken, "<DIR>")) {
3360 lpfp->bIsDirectory = TRUE;
3362 TRACE("Is directory\n");
3365 lpfp->bIsDirectory = FALSE;
3366 lpfp->nSize = atol(pszToken);
3367 TRACE("Size: %d\n", lpfp->nSize);
3370 pszToken = strtok(NULL, szSpace);
3371 if(!pszToken) continue;
3372 lpfp->lpszName = WININET_strdup_AtoW(pszToken);
3373 TRACE("Name: %s\n", debugstr_w(lpfp->lpszName));
3375 /* EPLF format - http://cr.yp.to/ftp/list/eplf.html */
3376 else if(pszToken[0] == '+') {
3377 FIXME("EPLF Format not implemented\n");
3380 if(lpfp->lpszName) {
3381 if((lpszSearchFile == NULL) ||
3382 (PathMatchSpecW(lpfp->lpszName, lpszSearchFile))) {
3384 TRACE("Matched: %s\n", debugstr_w(lpfp->lpszName));
3387 HeapFree(GetProcessHeap(), 0, lpfp->lpszName);
3388 lpfp->lpszName = NULL;
3395 /***********************************************************************
3396 * FTP_ParseDirectory (internal)
3398 * Parse string of directory information
3404 static BOOL FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
3405 LPFILEPROPERTIESW *lpafp, LPDWORD dwfp)
3407 BOOL bSuccess = TRUE;
3408 INT sizeFilePropArray = 500;/*20; */
3409 INT indexFilePropArray = -1;
3413 /* Allocate intial file properties array */
3414 *lpafp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FILEPROPERTIESW)*(sizeFilePropArray));
3419 if (indexFilePropArray+1 >= sizeFilePropArray)
3421 LPFILEPROPERTIESW tmpafp;
3423 sizeFilePropArray *= 2;
3424 tmpafp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *lpafp,
3425 sizeof(FILEPROPERTIESW)*sizeFilePropArray);
3434 indexFilePropArray++;
3435 } while (FTP_ParseNextFile(nSocket, lpszSearchFile, &(*lpafp)[indexFilePropArray]));
3437 if (bSuccess && indexFilePropArray)
3439 if (indexFilePropArray < sizeFilePropArray - 1)
3441 LPFILEPROPERTIESW tmpafp;
3443 tmpafp = HeapReAlloc(GetProcessHeap(), 0, *lpafp,
3444 sizeof(FILEPROPERTIESW)*indexFilePropArray);
3448 *dwfp = indexFilePropArray;
3452 HeapFree(GetProcessHeap(), 0, *lpafp);
3453 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
3461 /***********************************************************************
3462 * FTP_ParsePermission (internal)
3464 * Parse permission string of directory information
3471 static BOOL FTP_ParsePermission(LPCSTR lpszPermission, LPFILEPROPERTIESW lpfp)
3473 BOOL bSuccess = TRUE;
3474 unsigned short nPermission = 0;
3479 if ((*lpszPermission != 'd') && (*lpszPermission != '-') && (*lpszPermission != 'l'))
3485 lpfp->bIsDirectory = (*lpszPermission == 'd');
3491 nPermission |= (*(lpszPermission+1) == 'r' ? 1 : 0) << 8;
3494 nPermission |= (*(lpszPermission+2) == 'w' ? 1 : 0) << 7;
3497 nPermission |= (*(lpszPermission+3) == 'x' ? 1 : 0) << 6;
3500 nPermission |= (*(lpszPermission+4) == 'r' ? 1 : 0) << 5;
3503 nPermission |= (*(lpszPermission+5) == 'w' ? 1 : 0) << 4;
3506 nPermission |= (*(lpszPermission+6) == 'x' ? 1 : 0) << 3;
3509 nPermission |= (*(lpszPermission+7) == 'r' ? 1 : 0) << 2;
3512 nPermission |= (*(lpszPermission+8) == 'w' ? 1 : 0) << 1;
3515 nPermission |= (*(lpszPermission+9) == 'x' ? 1 : 0);
3519 }while (nPos <= nLast);
3521 lpfp->permissions = nPermission;
3526 /***********************************************************************
3527 * FTP_SetResponseError (internal)
3529 * Set the appropriate error code for a given response from the server
3534 static DWORD FTP_SetResponseError(DWORD dwResponse)
3540 case 425: /* Cannot open data connection. */
3541 dwCode = ERROR_INTERNET_CANNOT_CONNECT;
3544 case 426: /* Connection closed, transer aborted. */
3545 dwCode = ERROR_INTERNET_CONNECTION_ABORTED;
3548 case 530: /* Not logged in. Login incorrect. */
3549 dwCode = ERROR_INTERNET_LOGIN_FAILURE;
3552 case 421: /* Service not available - Server may be shutting down. */
3553 case 450: /* File action not taken. File may be busy. */
3554 case 451: /* Action aborted. Server error. */
3555 case 452: /* Action not taken. Insufficient storage space on server. */
3556 case 500: /* Syntax error. Command unrecognized. */
3557 case 501: /* Syntax error. Error in parameters or arguments. */
3558 case 502: /* Command not implemented. */
3559 case 503: /* Bad sequence of commands. */
3560 case 504: /* Command not implemented for that parameter. */
3561 case 532: /* Need account for storing files */
3562 case 550: /* File action not taken. File not found or no access. */
3563 case 551: /* Requested action aborted. Page type unknown */
3564 case 552: /* Action aborted. Exceeded storage allocation */
3565 case 553: /* Action not taken. File name not allowed. */
3568 dwCode = ERROR_INTERNET_EXTENDED_ERROR;
3572 INTERNET_SetLastError(dwCode);