2 * WININET - Ftp implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2004 Mike McCormack for CodeWeavers
6 * Copyright 2004 Kevin Koltzau
11 * Copyright 2000 Andreas Mohr
12 * Copyright 2002 Jaco Greeff
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/port.h"
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
58 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
63 #define DATA_PACKET_SIZE 0x2000
67 /* Testing shows that Windows only accepts dwFlags where the last
68 * 3 (yes 3) bits define FTP_TRANSFER_TYPE_UNKNOWN, FTP_TRANSFER_TYPE_ASCII or FTP_TRANSFER_TYPE_BINARY.
70 #define FTP_CONDITION_MASK 0x0007
73 /* FTP commands with arguments. */
89 /* FTP commands without arguments. */
98 static const CHAR *const szFtpCommands[] = {
121 static const CHAR szMonths[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
122 static const WCHAR szNoAccount[] = {'n','o','a','c','c','o','u','n','t','\0'};
124 static void FTP_CloseFileTransferHandle(LPWININETHANDLEHEADER hdr);
125 static void FTP_CloseSessionHandle(LPWININETHANDLEHEADER hdr);
126 static void FTP_CloseConnection(LPWININETHANDLEHEADER hdr);
127 static void FTP_CloseFindNextHandle(LPWININETHANDLEHEADER hdr);
128 static BOOL FTP_SendCommand(INT nSocket, FTP_COMMAND ftpCmd, LPCWSTR lpszParam,
129 INTERNET_STATUS_CALLBACK lpfnStatusCB, LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext);
130 static BOOL FTP_SendStore(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType);
131 static BOOL FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs, LPINT nDataSocket);
132 static BOOL FTP_SendData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile);
133 static INT FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs, DWORD_PTR dwContext);
134 static BOOL FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType);
135 static BOOL FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile);
136 static BOOL FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs);
137 static BOOL FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs);
138 static BOOL FTP_SendPassword(LPWININETFTPSESSIONW lpwfs);
139 static BOOL FTP_SendAccount(LPWININETFTPSESSIONW lpwfs);
140 static BOOL FTP_SendType(LPWININETFTPSESSIONW lpwfs, DWORD dwType);
141 static BOOL FTP_SendPort(LPWININETFTPSESSIONW lpwfs);
142 static BOOL FTP_DoPassive(LPWININETFTPSESSIONW lpwfs);
143 static BOOL FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs);
144 static BOOL FTP_ParsePermission(LPCSTR lpszPermission, LPFILEPROPERTIESW lpfp);
145 static BOOL FTP_ParseNextFile(INT nSocket, LPCWSTR lpszSearchFile, LPFILEPROPERTIESW fileprop);
146 static BOOL FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
147 LPFILEPROPERTIESW *lpafp, LPDWORD dwfp);
148 static HINTERNET FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
149 LPWIN32_FIND_DATAW lpFindFileData, DWORD_PTR dwContext);
150 static DWORD FTP_SetResponseError(DWORD dwResponse);
152 /***********************************************************************
153 * FtpPutFileA (WININET.@)
155 * Uploads a file to the FTP server
162 BOOL WINAPI FtpPutFileA(HINTERNET hConnect, LPCSTR lpszLocalFile,
163 LPCSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
165 LPWSTR lpwzLocalFile;
166 LPWSTR lpwzNewRemoteFile;
169 lpwzLocalFile = lpszLocalFile?WININET_strdup_AtoW(lpszLocalFile):NULL;
170 lpwzNewRemoteFile = lpszNewRemoteFile?WININET_strdup_AtoW(lpszNewRemoteFile):NULL;
171 ret = FtpPutFileW(hConnect, lpwzLocalFile, lpwzNewRemoteFile,
173 HeapFree(GetProcessHeap(), 0, lpwzLocalFile);
174 HeapFree(GetProcessHeap(), 0, lpwzNewRemoteFile);
178 static void AsyncFtpPutFileProc(WORKREQUEST *workRequest)
180 struct WORKREQ_FTPPUTFILEW const *req = &workRequest->u.FtpPutFileW;
181 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
183 TRACE("%p\n", lpwfs);
185 FTP_FtpPutFileW(lpwfs, req->lpszLocalFile,
186 req->lpszNewRemoteFile, req->dwFlags, req->dwContext);
188 HeapFree(GetProcessHeap(), 0, req->lpszLocalFile);
189 HeapFree(GetProcessHeap(), 0, req->lpszNewRemoteFile);
192 /***********************************************************************
193 * FtpPutFileW (WININET.@)
195 * Uploads a file to the FTP server
202 BOOL WINAPI FtpPutFileW(HINTERNET hConnect, LPCWSTR lpszLocalFile,
203 LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
205 LPWININETFTPSESSIONW lpwfs;
206 LPWININETAPPINFOW hIC = NULL;
209 if (!lpszLocalFile || !lpszNewRemoteFile)
211 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
215 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
218 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
222 if (WH_HFTPSESSION != lpwfs->hdr.htype)
224 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
228 if (lpwfs->download_in_progress != NULL)
230 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
234 if ((dwFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY)
236 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
240 hIC = lpwfs->lpAppInfo;
241 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
243 WORKREQUEST workRequest;
244 struct WORKREQ_FTPPUTFILEW *req = &workRequest.u.FtpPutFileW;
246 workRequest.asyncproc = AsyncFtpPutFileProc;
247 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
248 req->lpszLocalFile = WININET_strdupW(lpszLocalFile);
249 req->lpszNewRemoteFile = WININET_strdupW(lpszNewRemoteFile);
250 req->dwFlags = dwFlags;
251 req->dwContext = dwContext;
253 r = INTERNET_AsyncCall(&workRequest);
257 r = FTP_FtpPutFileW(lpwfs, lpszLocalFile,
258 lpszNewRemoteFile, dwFlags, dwContext);
262 WININET_Release( &lpwfs->hdr );
267 /***********************************************************************
268 * FTP_FtpPutFileW (Internal)
270 * Uploads a file to the FTP server
277 BOOL WINAPI FTP_FtpPutFileW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszLocalFile,
278 LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
281 BOOL bSuccess = FALSE;
282 LPWININETAPPINFOW hIC = NULL;
285 TRACE(" lpszLocalFile(%s) lpszNewRemoteFile(%s)\n", debugstr_w(lpszLocalFile), debugstr_w(lpszNewRemoteFile));
287 /* Clear any error information */
288 INTERNET_SetLastError(0);
290 /* Open file to be uploaded */
291 if (INVALID_HANDLE_VALUE ==
292 (hFile = CreateFileW(lpszLocalFile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0)))
293 /* Let CreateFile set the appropriate error */
296 hIC = lpwfs->lpAppInfo;
298 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
300 if (FTP_SendStore(lpwfs, lpszNewRemoteFile, dwFlags))
304 /* Get data socket to server */
305 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
307 FTP_SendData(lpwfs, nDataSocket, hFile);
308 closesocket(nDataSocket);
309 nResCode = FTP_ReceiveResponse(lpwfs, dwContext);
315 FTP_SetResponseError(nResCode);
320 if (lpwfs->lstnSocket != -1)
321 closesocket(lpwfs->lstnSocket);
323 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
325 INTERNET_ASYNC_RESULT iar;
327 iar.dwResult = (DWORD)bSuccess;
328 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
329 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
330 &iar, sizeof(INTERNET_ASYNC_RESULT));
339 /***********************************************************************
340 * FtpSetCurrentDirectoryA (WININET.@)
342 * Change the working directory on the FTP server
349 BOOL WINAPI FtpSetCurrentDirectoryA(HINTERNET hConnect, LPCSTR lpszDirectory)
351 LPWSTR lpwzDirectory;
354 lpwzDirectory = lpszDirectory?WININET_strdup_AtoW(lpszDirectory):NULL;
355 ret = FtpSetCurrentDirectoryW(hConnect, lpwzDirectory);
356 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
361 static void AsyncFtpSetCurrentDirectoryProc(WORKREQUEST *workRequest)
363 struct WORKREQ_FTPSETCURRENTDIRECTORYW const *req = &workRequest->u.FtpSetCurrentDirectoryW;
364 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
366 TRACE("%p\n", lpwfs);
368 FTP_FtpSetCurrentDirectoryW(lpwfs, req->lpszDirectory);
369 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
372 /***********************************************************************
373 * FtpSetCurrentDirectoryW (WININET.@)
375 * Change the working directory on the FTP server
382 BOOL WINAPI FtpSetCurrentDirectoryW(HINTERNET hConnect, LPCWSTR lpszDirectory)
384 LPWININETFTPSESSIONW lpwfs = NULL;
385 LPWININETAPPINFOW hIC = NULL;
390 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
394 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
395 if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
397 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
401 if (lpwfs->download_in_progress != NULL)
403 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
407 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
409 hIC = lpwfs->lpAppInfo;
410 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
412 WORKREQUEST workRequest;
413 struct WORKREQ_FTPSETCURRENTDIRECTORYW *req;
415 workRequest.asyncproc = AsyncFtpSetCurrentDirectoryProc;
416 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
417 req = &workRequest.u.FtpSetCurrentDirectoryW;
418 req->lpszDirectory = WININET_strdupW(lpszDirectory);
420 r = INTERNET_AsyncCall(&workRequest);
424 r = FTP_FtpSetCurrentDirectoryW(lpwfs, lpszDirectory);
429 WININET_Release( &lpwfs->hdr );
435 /***********************************************************************
436 * FTP_FtpSetCurrentDirectoryW (Internal)
438 * Change the working directory on the FTP server
445 BOOL WINAPI FTP_FtpSetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory)
448 LPWININETAPPINFOW hIC = NULL;
449 DWORD bSuccess = FALSE;
451 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
453 /* Clear any error information */
454 INTERNET_SetLastError(0);
456 hIC = lpwfs->lpAppInfo;
457 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_CWD, lpszDirectory,
458 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
461 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
468 FTP_SetResponseError(nResCode);
472 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
474 INTERNET_ASYNC_RESULT iar;
476 iar.dwResult = (DWORD)bSuccess;
477 iar.dwError = bSuccess ? ERROR_SUCCESS : ERROR_INTERNET_EXTENDED_ERROR;
478 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
479 &iar, sizeof(INTERNET_ASYNC_RESULT));
485 /***********************************************************************
486 * FtpCreateDirectoryA (WININET.@)
488 * Create new directory on the FTP server
495 BOOL WINAPI FtpCreateDirectoryA(HINTERNET hConnect, LPCSTR lpszDirectory)
497 LPWSTR lpwzDirectory;
500 lpwzDirectory = lpszDirectory?WININET_strdup_AtoW(lpszDirectory):NULL;
501 ret = FtpCreateDirectoryW(hConnect, lpwzDirectory);
502 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
507 static void AsyncFtpCreateDirectoryProc(WORKREQUEST *workRequest)
509 struct WORKREQ_FTPCREATEDIRECTORYW const *req = &workRequest->u.FtpCreateDirectoryW;
510 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
512 TRACE(" %p\n", lpwfs);
514 FTP_FtpCreateDirectoryW(lpwfs, req->lpszDirectory);
515 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
518 /***********************************************************************
519 * FtpCreateDirectoryW (WININET.@)
521 * Create new directory on the FTP server
528 BOOL WINAPI FtpCreateDirectoryW(HINTERNET hConnect, LPCWSTR lpszDirectory)
530 LPWININETFTPSESSIONW lpwfs;
531 LPWININETAPPINFOW hIC = NULL;
534 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
537 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
541 if (WH_HFTPSESSION != lpwfs->hdr.htype)
543 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
547 if (lpwfs->download_in_progress != NULL)
549 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
555 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
559 hIC = lpwfs->lpAppInfo;
560 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
562 WORKREQUEST workRequest;
563 struct WORKREQ_FTPCREATEDIRECTORYW *req;
565 workRequest.asyncproc = AsyncFtpCreateDirectoryProc;
566 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
567 req = &workRequest.u.FtpCreateDirectoryW;
568 req->lpszDirectory = WININET_strdupW(lpszDirectory);
570 r = INTERNET_AsyncCall(&workRequest);
574 r = FTP_FtpCreateDirectoryW(lpwfs, lpszDirectory);
577 WININET_Release( &lpwfs->hdr );
583 /***********************************************************************
584 * FTP_FtpCreateDirectoryW (Internal)
586 * Create new directory on the FTP server
593 BOOL WINAPI FTP_FtpCreateDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory)
596 BOOL bSuccess = FALSE;
597 LPWININETAPPINFOW hIC = NULL;
599 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
601 /* Clear any error information */
602 INTERNET_SetLastError(0);
604 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_MKD, lpszDirectory, 0, 0, 0))
607 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
613 FTP_SetResponseError(nResCode);
617 hIC = lpwfs->lpAppInfo;
618 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
620 INTERNET_ASYNC_RESULT iar;
622 iar.dwResult = (DWORD)bSuccess;
623 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
624 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
625 &iar, sizeof(INTERNET_ASYNC_RESULT));
631 /***********************************************************************
632 * FtpFindFirstFileA (WININET.@)
634 * Search the specified directory
637 * HINTERNET on success
641 HINTERNET WINAPI FtpFindFirstFileA(HINTERNET hConnect,
642 LPCSTR lpszSearchFile, LPWIN32_FIND_DATAA lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
644 LPWSTR lpwzSearchFile;
645 WIN32_FIND_DATAW wfd;
646 LPWIN32_FIND_DATAW lpFindFileDataW;
649 lpwzSearchFile = lpszSearchFile?WININET_strdup_AtoW(lpszSearchFile):NULL;
650 lpFindFileDataW = lpFindFileData?&wfd:NULL;
651 ret = FtpFindFirstFileW(hConnect, lpwzSearchFile, lpFindFileDataW, dwFlags, dwContext);
652 HeapFree(GetProcessHeap(), 0, lpwzSearchFile);
655 WININET_find_data_WtoA(lpFindFileDataW, lpFindFileData);
661 static void AsyncFtpFindFirstFileProc(WORKREQUEST *workRequest)
663 struct WORKREQ_FTPFINDFIRSTFILEW const *req = &workRequest->u.FtpFindFirstFileW;
664 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
666 TRACE("%p\n", lpwfs);
668 FTP_FtpFindFirstFileW(lpwfs, req->lpszSearchFile,
669 req->lpFindFileData, req->dwFlags, req->dwContext);
670 HeapFree(GetProcessHeap(), 0, req->lpszSearchFile);
673 /***********************************************************************
674 * FtpFindFirstFileW (WININET.@)
676 * Search the specified directory
679 * HINTERNET on success
683 HINTERNET WINAPI FtpFindFirstFileW(HINTERNET hConnect,
684 LPCWSTR lpszSearchFile, LPWIN32_FIND_DATAW lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
686 LPWININETFTPSESSIONW lpwfs;
687 LPWININETAPPINFOW hIC = NULL;
690 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
691 if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
693 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
697 if (lpwfs->download_in_progress != NULL)
699 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
703 hIC = lpwfs->lpAppInfo;
704 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
706 WORKREQUEST workRequest;
707 struct WORKREQ_FTPFINDFIRSTFILEW *req;
709 workRequest.asyncproc = AsyncFtpFindFirstFileProc;
710 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
711 req = &workRequest.u.FtpFindFirstFileW;
712 req->lpszSearchFile = (lpszSearchFile == NULL) ? NULL : WININET_strdupW(lpszSearchFile);
713 req->lpFindFileData = lpFindFileData;
714 req->dwFlags = dwFlags;
715 req->dwContext= dwContext;
717 INTERNET_AsyncCall(&workRequest);
722 r = FTP_FtpFindFirstFileW(lpwfs, lpszSearchFile, lpFindFileData,
727 WININET_Release( &lpwfs->hdr );
733 /***********************************************************************
734 * FTP_FtpFindFirstFileW (Internal)
736 * Search the specified directory
739 * HINTERNET on success
743 HINTERNET WINAPI FTP_FtpFindFirstFileW(LPWININETFTPSESSIONW lpwfs,
744 LPCWSTR lpszSearchFile, LPWIN32_FIND_DATAW lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
747 LPWININETAPPINFOW hIC = NULL;
748 HINTERNET hFindNext = NULL;
752 /* Clear any error information */
753 INTERNET_SetLastError(0);
755 if (!FTP_InitListenSocket(lpwfs))
758 if (!FTP_SendType(lpwfs, INTERNET_FLAG_TRANSFER_ASCII))
761 if (!FTP_SendPortOrPasv(lpwfs))
764 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_LIST, NULL,
765 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
768 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
771 if (nResCode == 125 || nResCode == 150)
775 /* Get data socket to server */
776 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
778 hFindNext = FTP_ReceiveFileList(lpwfs, nDataSocket, lpszSearchFile, lpFindFileData, dwContext);
779 closesocket(nDataSocket);
780 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
781 if (nResCode != 226 && nResCode != 250)
782 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
786 FTP_SetResponseError(nResCode);
790 if (lpwfs->lstnSocket != -1)
791 closesocket(lpwfs->lstnSocket);
793 hIC = lpwfs->lpAppInfo;
794 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
796 INTERNET_ASYNC_RESULT iar;
800 iar.dwResult = (DWORD)hFindNext;
801 iar.dwError = ERROR_SUCCESS;
802 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_HANDLE_CREATED,
803 &iar, sizeof(INTERNET_ASYNC_RESULT));
806 iar.dwResult = (DWORD)hFindNext;
807 iar.dwError = hFindNext ? ERROR_SUCCESS : INTERNET_GetLastError();
808 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
809 &iar, sizeof(INTERNET_ASYNC_RESULT));
816 /***********************************************************************
817 * FtpGetCurrentDirectoryA (WININET.@)
819 * Retrieves the current directory
826 BOOL WINAPI FtpGetCurrentDirectoryA(HINTERNET hFtpSession, LPSTR lpszCurrentDirectory,
827 LPDWORD lpdwCurrentDirectory)
833 if(lpdwCurrentDirectory) {
834 len = *lpdwCurrentDirectory;
835 if(lpszCurrentDirectory)
837 dir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
840 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
845 ret = FtpGetCurrentDirectoryW(hFtpSession, lpszCurrentDirectory?dir:NULL, lpdwCurrentDirectory?&len:NULL);
846 if(lpdwCurrentDirectory) {
847 *lpdwCurrentDirectory = len;
848 if(lpszCurrentDirectory) {
849 WideCharToMultiByte(CP_ACP, 0, dir, len, lpszCurrentDirectory, *lpdwCurrentDirectory, NULL, NULL);
850 HeapFree(GetProcessHeap(), 0, dir);
857 static void AsyncFtpGetCurrentDirectoryProc(WORKREQUEST *workRequest)
859 struct WORKREQ_FTPGETCURRENTDIRECTORYW const *req = &workRequest->u.FtpGetCurrentDirectoryW;
860 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
862 TRACE("%p\n", lpwfs);
864 FTP_FtpGetCurrentDirectoryW(lpwfs, req->lpszDirectory, req->lpdwDirectory);
867 /***********************************************************************
868 * FtpGetCurrentDirectoryW (WININET.@)
870 * Retrieves the current directory
877 BOOL WINAPI FtpGetCurrentDirectoryW(HINTERNET hFtpSession, LPWSTR lpszCurrentDirectory,
878 LPDWORD lpdwCurrentDirectory)
880 LPWININETFTPSESSIONW lpwfs;
881 LPWININETAPPINFOW hIC = NULL;
884 TRACE("len(%d)\n", *lpdwCurrentDirectory);
886 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
887 if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
889 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
893 if (lpwfs->download_in_progress != NULL)
895 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
899 hIC = lpwfs->lpAppInfo;
900 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
902 WORKREQUEST workRequest;
903 struct WORKREQ_FTPGETCURRENTDIRECTORYW *req;
905 workRequest.asyncproc = AsyncFtpGetCurrentDirectoryProc;
906 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
907 req = &workRequest.u.FtpGetCurrentDirectoryW;
908 req->lpszDirectory = lpszCurrentDirectory;
909 req->lpdwDirectory = lpdwCurrentDirectory;
911 r = INTERNET_AsyncCall(&workRequest);
915 r = FTP_FtpGetCurrentDirectoryW(lpwfs, lpszCurrentDirectory,
916 lpdwCurrentDirectory);
921 WININET_Release( &lpwfs->hdr );
927 /***********************************************************************
928 * FTP_FtpGetCurrentDirectoryW (Internal)
930 * Retrieves the current directory
937 BOOL WINAPI FTP_FtpGetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs, LPWSTR lpszCurrentDirectory,
938 LPDWORD lpdwCurrentDirectory)
941 LPWININETAPPINFOW hIC = NULL;
942 DWORD bSuccess = FALSE;
944 TRACE("len(%d)\n", *lpdwCurrentDirectory);
946 /* Clear any error information */
947 INTERNET_SetLastError(0);
949 ZeroMemory(lpszCurrentDirectory, *lpdwCurrentDirectory);
951 hIC = lpwfs->lpAppInfo;
952 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PWD, NULL,
953 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
956 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
959 if (nResCode == 257) /* Extract directory name */
961 DWORD firstpos, lastpos, len;
962 LPWSTR lpszResponseBuffer = WININET_strdup_AtoW(INTERNET_GetResponseBuffer());
964 for (firstpos = 0, lastpos = 0; lpszResponseBuffer[lastpos]; lastpos++)
966 if ('"' == lpszResponseBuffer[lastpos])
975 len = lastpos - firstpos - 1;
976 lstrcpynW(lpszCurrentDirectory, &lpszResponseBuffer[firstpos+1], *lpdwCurrentDirectory);
977 HeapFree(GetProcessHeap(), 0, lpszResponseBuffer);
978 *lpdwCurrentDirectory = len;
982 FTP_SetResponseError(nResCode);
986 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
988 INTERNET_ASYNC_RESULT iar;
990 iar.dwResult = (DWORD)bSuccess;
991 iar.dwError = bSuccess ? ERROR_SUCCESS : ERROR_INTERNET_EXTENDED_ERROR;
992 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
993 &iar, sizeof(INTERNET_ASYNC_RESULT));
996 return (DWORD) bSuccess;
999 /***********************************************************************
1000 * FtpOpenFileA (WININET.@)
1002 * Open a remote file for writing or reading
1005 * HINTERNET handle on success
1009 HINTERNET WINAPI FtpOpenFileA(HINTERNET hFtpSession,
1010 LPCSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1011 DWORD_PTR dwContext)
1013 LPWSTR lpwzFileName;
1016 lpwzFileName = lpszFileName?WININET_strdup_AtoW(lpszFileName):NULL;
1017 ret = FtpOpenFileW(hFtpSession, lpwzFileName, fdwAccess, dwFlags, dwContext);
1018 HeapFree(GetProcessHeap(), 0, lpwzFileName);
1023 static void AsyncFtpOpenFileProc(WORKREQUEST *workRequest)
1025 struct WORKREQ_FTPOPENFILEW const *req = &workRequest->u.FtpOpenFileW;
1026 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1028 TRACE("%p\n", lpwfs);
1030 FTP_FtpOpenFileW(lpwfs, req->lpszFilename,
1031 req->dwAccess, req->dwFlags, req->dwContext);
1032 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
1035 /***********************************************************************
1036 * FtpOpenFileW (WININET.@)
1038 * Open a remote file for writing or reading
1041 * HINTERNET handle on success
1045 HINTERNET WINAPI FtpOpenFileW(HINTERNET hFtpSession,
1046 LPCWSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1047 DWORD_PTR dwContext)
1049 LPWININETFTPSESSIONW lpwfs;
1050 LPWININETAPPINFOW hIC = NULL;
1053 TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession,
1054 debugstr_w(lpszFileName), fdwAccess, dwFlags, dwContext);
1056 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1059 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1063 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1065 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1069 if ((!lpszFileName) ||
1070 ((fdwAccess != GENERIC_READ) && (fdwAccess != GENERIC_WRITE)) ||
1071 ((dwFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY))
1073 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1077 if (lpwfs->download_in_progress != NULL)
1079 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1083 hIC = lpwfs->lpAppInfo;
1084 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1086 WORKREQUEST workRequest;
1087 struct WORKREQ_FTPOPENFILEW *req;
1089 workRequest.asyncproc = AsyncFtpOpenFileProc;
1090 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1091 req = &workRequest.u.FtpOpenFileW;
1092 req->lpszFilename = WININET_strdupW(lpszFileName);
1093 req->dwAccess = fdwAccess;
1094 req->dwFlags = dwFlags;
1095 req->dwContext = dwContext;
1097 INTERNET_AsyncCall(&workRequest);
1102 r = FTP_FtpOpenFileW(lpwfs, lpszFileName, fdwAccess, dwFlags, dwContext);
1106 WININET_Release( &lpwfs->hdr );
1112 /***********************************************************************
1113 * FTP_FtpOpenFileW (Internal)
1115 * Open a remote file for writing or reading
1118 * HINTERNET handle on success
1122 HINTERNET FTP_FtpOpenFileW(LPWININETFTPSESSIONW lpwfs,
1123 LPCWSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1124 DWORD_PTR dwContext)
1127 BOOL bSuccess = FALSE;
1128 LPWININETFTPFILE lpwh = NULL;
1129 LPWININETAPPINFOW hIC = NULL;
1130 HINTERNET handle = NULL;
1134 /* Clear any error information */
1135 INTERNET_SetLastError(0);
1137 if (GENERIC_READ == fdwAccess)
1139 /* Set up socket to retrieve data */
1140 bSuccess = FTP_SendRetrieve(lpwfs, lpszFileName, dwFlags);
1142 else if (GENERIC_WRITE == fdwAccess)
1144 /* Set up socket to send data */
1145 bSuccess = FTP_SendStore(lpwfs, lpszFileName, dwFlags);
1148 /* Get data socket to server */
1149 if (bSuccess && FTP_GetDataSocket(lpwfs, &nDataSocket))
1151 lpwh = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPFILE));
1152 lpwh->hdr.htype = WH_HFILE;
1153 lpwh->hdr.dwFlags = dwFlags;
1154 lpwh->hdr.dwContext = dwContext;
1155 lpwh->hdr.dwRefCount = 1;
1156 lpwh->hdr.close_connection = NULL;
1157 lpwh->hdr.destroy = FTP_CloseFileTransferHandle;
1158 lpwh->hdr.lpfnStatusCB = lpwfs->hdr.lpfnStatusCB;
1159 lpwh->nDataSocket = nDataSocket;
1160 lpwh->session_deleted = FALSE;
1162 WININET_AddRef( &lpwfs->hdr );
1163 lpwh->lpFtpSession = lpwfs;
1164 list_add_head( &lpwfs->hdr.children, &lpwh->hdr.entry );
1166 handle = WININET_AllocHandle( &lpwh->hdr );
1170 /* Indicate that a download is currently in progress */
1171 lpwfs->download_in_progress = lpwh;
1174 if (lpwfs->lstnSocket != -1)
1175 closesocket(lpwfs->lstnSocket);
1177 hIC = lpwfs->lpAppInfo;
1178 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1180 INTERNET_ASYNC_RESULT iar;
1184 iar.dwResult = (DWORD)handle;
1185 iar.dwError = ERROR_SUCCESS;
1186 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_HANDLE_CREATED,
1187 &iar, sizeof(INTERNET_ASYNC_RESULT));
1190 iar.dwResult = (DWORD)bSuccess;
1191 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1192 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1193 &iar, sizeof(INTERNET_ASYNC_RESULT));
1198 WININET_Release( &lpwh->hdr );
1204 /***********************************************************************
1205 * FtpGetFileA (WININET.@)
1207 * Retrieve file from the FTP server
1214 BOOL WINAPI FtpGetFileA(HINTERNET hInternet, LPCSTR lpszRemoteFile, LPCSTR lpszNewFile,
1215 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1216 DWORD_PTR dwContext)
1218 LPWSTR lpwzRemoteFile;
1222 lpwzRemoteFile = lpszRemoteFile?WININET_strdup_AtoW(lpszRemoteFile):NULL;
1223 lpwzNewFile = lpszNewFile?WININET_strdup_AtoW(lpszNewFile):NULL;
1224 ret = FtpGetFileW(hInternet, lpwzRemoteFile, lpwzNewFile, fFailIfExists,
1225 dwLocalFlagsAttribute, dwInternetFlags, dwContext);
1226 HeapFree(GetProcessHeap(), 0, lpwzRemoteFile);
1227 HeapFree(GetProcessHeap(), 0, lpwzNewFile);
1232 static void AsyncFtpGetFileProc(WORKREQUEST *workRequest)
1234 struct WORKREQ_FTPGETFILEW const *req = &workRequest->u.FtpGetFileW;
1235 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1237 TRACE("%p\n", lpwfs);
1239 FTP_FtpGetFileW(lpwfs, req->lpszRemoteFile,
1240 req->lpszNewFile, req->fFailIfExists,
1241 req->dwLocalFlagsAttribute, req->dwFlags, req->dwContext);
1242 HeapFree(GetProcessHeap(), 0, req->lpszRemoteFile);
1243 HeapFree(GetProcessHeap(), 0, req->lpszNewFile);
1247 /***********************************************************************
1248 * FtpGetFileW (WININET.@)
1250 * Retrieve file from the FTP server
1257 BOOL WINAPI FtpGetFileW(HINTERNET hInternet, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
1258 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1259 DWORD_PTR dwContext)
1261 LPWININETFTPSESSIONW lpwfs;
1262 LPWININETAPPINFOW hIC = NULL;
1265 if (!lpszRemoteFile || !lpszNewFile)
1267 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1271 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hInternet );
1274 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1278 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1280 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1284 if ((dwInternetFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY)
1286 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1290 if (lpwfs->download_in_progress != NULL)
1292 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1296 hIC = lpwfs->lpAppInfo;
1297 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1299 WORKREQUEST workRequest;
1300 struct WORKREQ_FTPGETFILEW *req;
1302 workRequest.asyncproc = AsyncFtpGetFileProc;
1303 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1304 req = &workRequest.u.FtpGetFileW;
1305 req->lpszRemoteFile = WININET_strdupW(lpszRemoteFile);
1306 req->lpszNewFile = WININET_strdupW(lpszNewFile);
1307 req->dwLocalFlagsAttribute = dwLocalFlagsAttribute;
1308 req->fFailIfExists = fFailIfExists;
1309 req->dwFlags = dwInternetFlags;
1310 req->dwContext = dwContext;
1312 r = INTERNET_AsyncCall(&workRequest);
1316 r = FTP_FtpGetFileW(lpwfs, lpszRemoteFile, lpszNewFile,
1317 fFailIfExists, dwLocalFlagsAttribute, dwInternetFlags, dwContext);
1321 WININET_Release( &lpwfs->hdr );
1327 /***********************************************************************
1328 * FTP_FtpGetFileW (Internal)
1330 * Retrieve file from the FTP server
1337 BOOL WINAPI FTP_FtpGetFileW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
1338 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1339 DWORD_PTR dwContext)
1341 BOOL bSuccess = FALSE;
1343 LPWININETAPPINFOW hIC = NULL;
1345 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile), debugstr_w(lpszNewFile));
1347 /* Clear any error information */
1348 INTERNET_SetLastError(0);
1350 /* Ensure we can write to lpszNewfile by opening it */
1351 hFile = CreateFileW(lpszNewFile, GENERIC_WRITE, 0, 0, fFailIfExists ?
1352 CREATE_NEW : CREATE_ALWAYS, dwLocalFlagsAttribute, 0);
1353 if (INVALID_HANDLE_VALUE == hFile)
1356 /* Set up socket to retrieve data */
1357 if (FTP_SendRetrieve(lpwfs, lpszRemoteFile, dwInternetFlags))
1361 /* Get data socket to server */
1362 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
1367 FTP_RetrieveFileData(lpwfs, nDataSocket, hFile);
1368 closesocket(nDataSocket);
1370 nResCode = FTP_ReceiveResponse(lpwfs, dwContext);
1373 if (nResCode == 226)
1376 FTP_SetResponseError(nResCode);
1381 if (lpwfs->lstnSocket != -1)
1382 closesocket(lpwfs->lstnSocket);
1386 hIC = lpwfs->lpAppInfo;
1387 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1389 INTERNET_ASYNC_RESULT iar;
1391 iar.dwResult = (DWORD)bSuccess;
1392 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1393 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1394 &iar, sizeof(INTERNET_ASYNC_RESULT));
1400 /***********************************************************************
1401 * FtpGetFileSize (WININET.@)
1403 DWORD WINAPI FtpGetFileSize( HINTERNET hFile, LPDWORD lpdwFileSizeHigh )
1405 FIXME("(%p, %p)\n", hFile, lpdwFileSizeHigh);
1407 if (lpdwFileSizeHigh)
1408 *lpdwFileSizeHigh = 0;
1413 /***********************************************************************
1414 * FtpDeleteFileA (WININET.@)
1416 * Delete a file on the ftp server
1423 BOOL WINAPI FtpDeleteFileA(HINTERNET hFtpSession, LPCSTR lpszFileName)
1425 LPWSTR lpwzFileName;
1428 lpwzFileName = lpszFileName?WININET_strdup_AtoW(lpszFileName):NULL;
1429 ret = FtpDeleteFileW(hFtpSession, lpwzFileName);
1430 HeapFree(GetProcessHeap(), 0, lpwzFileName);
1434 static void AsyncFtpDeleteFileProc(WORKREQUEST *workRequest)
1436 struct WORKREQ_FTPDELETEFILEW const *req = &workRequest->u.FtpDeleteFileW;
1437 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1439 TRACE("%p\n", lpwfs);
1441 FTP_FtpDeleteFileW(lpwfs, req->lpszFilename);
1442 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
1445 /***********************************************************************
1446 * FtpDeleteFileW (WININET.@)
1448 * Delete a file on the ftp server
1455 BOOL WINAPI FtpDeleteFileW(HINTERNET hFtpSession, LPCWSTR lpszFileName)
1457 LPWININETFTPSESSIONW lpwfs;
1458 LPWININETAPPINFOW hIC = NULL;
1461 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1464 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1468 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1470 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1474 if (lpwfs->download_in_progress != NULL)
1476 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1482 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1486 hIC = lpwfs->lpAppInfo;
1487 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1489 WORKREQUEST workRequest;
1490 struct WORKREQ_FTPDELETEFILEW *req;
1492 workRequest.asyncproc = AsyncFtpDeleteFileProc;
1493 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1494 req = &workRequest.u.FtpDeleteFileW;
1495 req->lpszFilename = WININET_strdupW(lpszFileName);
1497 r = INTERNET_AsyncCall(&workRequest);
1501 r = FTP_FtpDeleteFileW(lpwfs, lpszFileName);
1505 WININET_Release( &lpwfs->hdr );
1510 /***********************************************************************
1511 * FTP_FtpDeleteFileW (Internal)
1513 * Delete a file on the ftp server
1520 BOOL FTP_FtpDeleteFileW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszFileName)
1523 BOOL bSuccess = FALSE;
1524 LPWININETAPPINFOW hIC = NULL;
1526 TRACE("%p\n", lpwfs);
1528 /* Clear any error information */
1529 INTERNET_SetLastError(0);
1531 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_DELE, lpszFileName, 0, 0, 0))
1534 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1537 if (nResCode == 250)
1540 FTP_SetResponseError(nResCode);
1543 hIC = lpwfs->lpAppInfo;
1544 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1546 INTERNET_ASYNC_RESULT iar;
1548 iar.dwResult = (DWORD)bSuccess;
1549 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1550 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1551 &iar, sizeof(INTERNET_ASYNC_RESULT));
1558 /***********************************************************************
1559 * FtpRemoveDirectoryA (WININET.@)
1561 * Remove a directory on the ftp server
1568 BOOL WINAPI FtpRemoveDirectoryA(HINTERNET hFtpSession, LPCSTR lpszDirectory)
1570 LPWSTR lpwzDirectory;
1573 lpwzDirectory = lpszDirectory?WININET_strdup_AtoW(lpszDirectory):NULL;
1574 ret = FtpRemoveDirectoryW(hFtpSession, lpwzDirectory);
1575 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
1579 static void AsyncFtpRemoveDirectoryProc(WORKREQUEST *workRequest)
1581 struct WORKREQ_FTPREMOVEDIRECTORYW const *req = &workRequest->u.FtpRemoveDirectoryW;
1582 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1584 TRACE("%p\n", lpwfs);
1586 FTP_FtpRemoveDirectoryW(lpwfs, req->lpszDirectory);
1587 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
1590 /***********************************************************************
1591 * FtpRemoveDirectoryW (WININET.@)
1593 * Remove a directory on the ftp server
1600 BOOL WINAPI FtpRemoveDirectoryW(HINTERNET hFtpSession, LPCWSTR lpszDirectory)
1602 LPWININETFTPSESSIONW lpwfs;
1603 LPWININETAPPINFOW hIC = NULL;
1606 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1609 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1613 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1615 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1619 if (lpwfs->download_in_progress != NULL)
1621 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1627 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1631 hIC = lpwfs->lpAppInfo;
1632 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1634 WORKREQUEST workRequest;
1635 struct WORKREQ_FTPREMOVEDIRECTORYW *req;
1637 workRequest.asyncproc = AsyncFtpRemoveDirectoryProc;
1638 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1639 req = &workRequest.u.FtpRemoveDirectoryW;
1640 req->lpszDirectory = WININET_strdupW(lpszDirectory);
1642 r = INTERNET_AsyncCall(&workRequest);
1646 r = FTP_FtpRemoveDirectoryW(lpwfs, lpszDirectory);
1650 WININET_Release( &lpwfs->hdr );
1655 /***********************************************************************
1656 * FTP_FtpRemoveDirectoryW (Internal)
1658 * Remove a directory on the ftp server
1665 BOOL FTP_FtpRemoveDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory)
1668 BOOL bSuccess = FALSE;
1669 LPWININETAPPINFOW hIC = NULL;
1673 /* Clear any error information */
1674 INTERNET_SetLastError(0);
1676 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RMD, lpszDirectory, 0, 0, 0))
1679 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1682 if (nResCode == 250)
1685 FTP_SetResponseError(nResCode);
1689 hIC = lpwfs->lpAppInfo;
1690 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1692 INTERNET_ASYNC_RESULT iar;
1694 iar.dwResult = (DWORD)bSuccess;
1695 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1696 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1697 &iar, sizeof(INTERNET_ASYNC_RESULT));
1704 /***********************************************************************
1705 * FtpRenameFileA (WININET.@)
1707 * Rename a file on the ftp server
1714 BOOL WINAPI FtpRenameFileA(HINTERNET hFtpSession, LPCSTR lpszSrc, LPCSTR lpszDest)
1720 lpwzSrc = lpszSrc?WININET_strdup_AtoW(lpszSrc):NULL;
1721 lpwzDest = lpszDest?WININET_strdup_AtoW(lpszDest):NULL;
1722 ret = FtpRenameFileW(hFtpSession, lpwzSrc, lpwzDest);
1723 HeapFree(GetProcessHeap(), 0, lpwzSrc);
1724 HeapFree(GetProcessHeap(), 0, lpwzDest);
1728 static void AsyncFtpRenameFileProc(WORKREQUEST *workRequest)
1730 struct WORKREQ_FTPRENAMEFILEW const *req = &workRequest->u.FtpRenameFileW;
1731 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1733 TRACE("%p\n", lpwfs);
1735 FTP_FtpRenameFileW(lpwfs, req->lpszSrcFile, req->lpszDestFile);
1736 HeapFree(GetProcessHeap(), 0, req->lpszSrcFile);
1737 HeapFree(GetProcessHeap(), 0, req->lpszDestFile);
1740 /***********************************************************************
1741 * FtpRenameFileW (WININET.@)
1743 * Rename a file on the ftp server
1750 BOOL WINAPI FtpRenameFileW(HINTERNET hFtpSession, LPCWSTR lpszSrc, LPCWSTR lpszDest)
1752 LPWININETFTPSESSIONW lpwfs;
1753 LPWININETAPPINFOW hIC = NULL;
1756 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1759 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1763 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1765 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1769 if (lpwfs->download_in_progress != NULL)
1771 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1775 if (!lpszSrc || !lpszDest)
1777 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1781 hIC = lpwfs->lpAppInfo;
1782 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1784 WORKREQUEST workRequest;
1785 struct WORKREQ_FTPRENAMEFILEW *req;
1787 workRequest.asyncproc = AsyncFtpRenameFileProc;
1788 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1789 req = &workRequest.u.FtpRenameFileW;
1790 req->lpszSrcFile = WININET_strdupW(lpszSrc);
1791 req->lpszDestFile = WININET_strdupW(lpszDest);
1793 r = INTERNET_AsyncCall(&workRequest);
1797 r = FTP_FtpRenameFileW(lpwfs, lpszSrc, lpszDest);
1801 WININET_Release( &lpwfs->hdr );
1806 /***********************************************************************
1807 * FTP_FtpRenameFileW (Internal)
1809 * Rename a file on the ftp server
1816 BOOL FTP_FtpRenameFileW( LPWININETFTPSESSIONW lpwfs,
1817 LPCWSTR lpszSrc, LPCWSTR lpszDest)
1820 BOOL bSuccess = FALSE;
1821 LPWININETAPPINFOW hIC = NULL;
1825 /* Clear any error information */
1826 INTERNET_SetLastError(0);
1828 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RNFR, lpszSrc, 0, 0, 0))
1831 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1832 if (nResCode == 350)
1834 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RNTO, lpszDest, 0, 0, 0))
1837 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1840 if (nResCode == 250)
1843 FTP_SetResponseError(nResCode);
1846 hIC = lpwfs->lpAppInfo;
1847 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1849 INTERNET_ASYNC_RESULT iar;
1851 iar.dwResult = (DWORD)bSuccess;
1852 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1853 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1854 &iar, sizeof(INTERNET_ASYNC_RESULT));
1860 /***********************************************************************
1861 * FtpCommandA (WININET.@)
1863 BOOL WINAPI FtpCommandA( HINTERNET hConnect, BOOL fExpectResponse, DWORD dwFlags,
1864 LPCSTR lpszCommand, DWORD_PTR dwContext, HINTERNET* phFtpCommand )
1866 FIXME("%p %d 0x%08x %s 0x%08lx %p\n", hConnect, fExpectResponse, dwFlags,
1867 debugstr_a(lpszCommand), dwContext, phFtpCommand);
1872 /***********************************************************************
1873 * FtpCommandW (WININET.@)
1875 BOOL WINAPI FtpCommandW( HINTERNET hConnect, BOOL fExpectResponse, DWORD dwFlags,
1876 LPCWSTR lpszCommand, DWORD_PTR dwContext, HINTERNET* phFtpCommand )
1878 FIXME("%p %d 0x%08x %s 0x%08lx %p\n", hConnect, fExpectResponse, dwFlags,
1879 debugstr_w(lpszCommand), dwContext, phFtpCommand);
1884 /***********************************************************************
1885 * FTP_Connect (internal)
1887 * Connect to a ftp server
1890 * HINTERNET a session handle on success
1895 * Windows uses 'anonymous' as the username, when given a NULL username
1896 * and a NULL password. The password is first looked up in:
1898 * HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\EmailName
1900 * If this entry is not present it uses the current username as the password.
1904 HINTERNET FTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
1905 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
1906 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
1907 DWORD dwInternalFlags)
1909 static const WCHAR szKey[] = {'S','o','f','t','w','a','r','e','\\',
1910 'M','i','c','r','o','s','o','f','t','\\',
1911 'W','i','n','d','o','w','s','\\',
1912 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1913 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
1914 static const WCHAR szValue[] = {'E','m','a','i','l','N','a','m','e',0};
1915 static const WCHAR szDefaultUsername[] = {'a','n','o','n','y','m','o','u','s','\0'};
1916 static const WCHAR szEmpty[] = {'\0'};
1917 struct sockaddr_in socketAddr;
1920 BOOL bSuccess = FALSE;
1921 LPWININETFTPSESSIONW lpwfs = NULL;
1922 HINTERNET handle = NULL;
1924 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
1925 hIC, debugstr_w(lpszServerName),
1926 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword));
1928 assert( hIC->hdr.htype == WH_HINIT );
1930 if (NULL == lpszUserName && NULL != lpszPassword)
1932 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1936 lpwfs = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPSESSIONW));
1939 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1943 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
1944 nServerPort = INTERNET_DEFAULT_FTP_PORT;
1946 lpwfs->hdr.htype = WH_HFTPSESSION;
1947 lpwfs->hdr.dwFlags = dwFlags;
1948 lpwfs->hdr.dwContext = dwContext;
1949 lpwfs->hdr.dwInternalFlags = dwInternalFlags;
1950 lpwfs->hdr.dwRefCount = 1;
1951 lpwfs->hdr.close_connection = FTP_CloseConnection;
1952 lpwfs->hdr.destroy = FTP_CloseSessionHandle;
1953 lpwfs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
1954 lpwfs->download_in_progress = NULL;
1956 WININET_AddRef( &hIC->hdr );
1957 lpwfs->lpAppInfo = hIC;
1958 list_add_head( &hIC->hdr.children, &lpwfs->hdr.entry );
1960 handle = WININET_AllocHandle( &lpwfs->hdr );
1963 ERR("Failed to alloc handle\n");
1964 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1968 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1969 if(strchrW(hIC->lpszProxy, ' '))
1970 FIXME("Several proxies not implemented.\n");
1971 if(hIC->lpszProxyBypass)
1972 FIXME("Proxy bypass is ignored.\n");
1974 if ( !lpszUserName) {
1976 WCHAR szPassword[MAX_PATH];
1977 DWORD len = sizeof(szPassword);
1979 lpwfs->lpszUserName = WININET_strdupW(szDefaultUsername);
1981 RegOpenKeyW(HKEY_CURRENT_USER, szKey, &key);
1982 if (RegQueryValueExW(key, szValue, NULL, NULL, (LPBYTE)szPassword, &len)) {
1983 /* Nothing in the registry, get the username and use that as the password */
1984 if (!GetUserNameW(szPassword, &len)) {
1985 /* Should never get here, but use an empty password as failsafe */
1986 strcpyW(szPassword, szEmpty);
1991 TRACE("Password used for anonymous ftp : (%s)\n", debugstr_w(szPassword));
1992 lpwfs->lpszPassword = WININET_strdupW(szPassword);
1995 lpwfs->lpszUserName = WININET_strdupW(lpszUserName);
1998 lpwfs->lpszPassword = WININET_strdupW(lpszPassword);
2000 lpwfs->lpszPassword = WININET_strdupW(szEmpty);
2003 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2004 if (!(lpwfs->hdr.dwInternalFlags & INET_OPENURL))
2006 INTERNET_ASYNC_RESULT iar;
2008 iar.dwResult = (DWORD)handle;
2009 iar.dwError = ERROR_SUCCESS;
2011 SendAsyncCallback(&hIC->hdr, dwContext,
2012 INTERNET_STATUS_HANDLE_CREATED, &iar,
2013 sizeof(INTERNET_ASYNC_RESULT));
2016 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_RESOLVING_NAME,
2017 (LPWSTR) lpszServerName, strlenW(lpszServerName));
2019 if (!GetAddress(lpszServerName, nServerPort, &socketAddr))
2021 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
2025 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_NAME_RESOLVED,
2026 (LPWSTR) lpszServerName, strlenW(lpszServerName));
2028 nsocket = socket(AF_INET,SOCK_STREAM,0);
2031 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2035 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_CONNECTING_TO_SERVER,
2036 &socketAddr, sizeof(struct sockaddr_in));
2038 if (connect(nsocket, (struct sockaddr *)&socketAddr, sizeof(socketAddr)) < 0)
2040 ERR("Unable to connect (%s)\n", strerror(errno));
2041 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2045 TRACE("Connected to server\n");
2046 lpwfs->sndSocket = nsocket;
2047 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_CONNECTED_TO_SERVER,
2048 &socketAddr, sizeof(struct sockaddr_in));
2050 sock_namelen = sizeof(lpwfs->socketAddress);
2051 getsockname(nsocket, (struct sockaddr *) &lpwfs->socketAddress, &sock_namelen);
2053 if (FTP_ConnectToHost(lpwfs))
2055 TRACE("Successfully logged into server\n");
2061 if (lpwfs) WININET_Release( &lpwfs->hdr );
2063 if (!bSuccess && handle)
2065 WININET_Release( &hIC->hdr );
2066 WININET_FreeHandle( handle );
2070 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2072 INTERNET_ASYNC_RESULT iar;
2074 iar.dwResult = bSuccess ? (DWORD_PTR)lpwfs : 0;
2075 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2076 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
2077 &iar, sizeof(INTERNET_ASYNC_RESULT));
2084 /***********************************************************************
2085 * FTP_ConnectToHost (internal)
2087 * Connect to a ftp server
2094 static BOOL FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs)
2097 BOOL bSuccess = FALSE;
2100 FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2102 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_USER, lpwfs->lpszUserName, 0, 0, 0))
2105 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2108 /* Login successful... */
2109 if (nResCode == 230)
2111 /* User name okay, need password... */
2112 else if (nResCode == 331)
2113 bSuccess = FTP_SendPassword(lpwfs);
2114 /* Need account for login... */
2115 else if (nResCode == 332)
2116 bSuccess = FTP_SendAccount(lpwfs);
2118 FTP_SetResponseError(nResCode);
2121 TRACE("Returning %d\n", bSuccess);
2127 /***********************************************************************
2128 * FTP_SendCommandA (internal)
2130 * Send command to server
2137 static BOOL FTP_SendCommandA(INT nSocket, FTP_COMMAND ftpCmd, LPCSTR lpszParam,
2138 INTERNET_STATUS_CALLBACK lpfnStatusCB, LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext)
2142 DWORD nBytesSent = 0;
2146 TRACE("%d: (%s) %d\n", ftpCmd, lpszParam, nSocket);
2150 lpfnStatusCB(hdr->hInternet, dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2153 dwParamLen = lpszParam?strlen(lpszParam)+1:0;
2154 len = dwParamLen + strlen(szFtpCommands[ftpCmd]) + strlen(szCRLF);
2155 if (NULL == (buf = HeapAlloc(GetProcessHeap(), 0, len+1)))
2157 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2160 sprintf(buf, "%s%s%s%s", szFtpCommands[ftpCmd], dwParamLen ? " " : "",
2161 dwParamLen ? lpszParam : "", szCRLF);
2163 TRACE("Sending (%s) len(%d)\n", buf, len);
2164 while((nBytesSent < len) && (nRC != -1))
2166 nRC = send(nSocket, buf+nBytesSent, len - nBytesSent, 0);
2170 HeapFree(GetProcessHeap(), 0, (LPVOID)buf);
2174 lpfnStatusCB(hdr->hInternet, dwContext, INTERNET_STATUS_REQUEST_SENT,
2175 &nBytesSent, sizeof(DWORD));
2178 TRACE("Sent %d bytes\n", nBytesSent);
2182 /***********************************************************************
2183 * FTP_SendCommand (internal)
2185 * Send command to server
2192 static BOOL FTP_SendCommand(INT nSocket, FTP_COMMAND ftpCmd, LPCWSTR lpszParam,
2193 INTERNET_STATUS_CALLBACK lpfnStatusCB, LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext)
2196 LPSTR lpszParamA = lpszParam?WININET_strdup_WtoA(lpszParam):NULL;
2197 ret = FTP_SendCommandA(nSocket, ftpCmd, lpszParamA, lpfnStatusCB, hdr, dwContext);
2198 HeapFree(GetProcessHeap(), 0, lpszParamA);
2202 /***********************************************************************
2203 * FTP_ReceiveResponse (internal)
2205 * Receive response from server
2208 * Reply code on success
2212 INT FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs, DWORD_PTR dwContext)
2214 LPSTR lpszResponse = INTERNET_GetResponseBuffer();
2217 char firstprefix[5];
2218 BOOL multiline = FALSE;
2219 LPWININETAPPINFOW hIC = NULL;
2221 TRACE("socket(%d)\n", lpwfs->sndSocket);
2223 hIC = lpwfs->lpAppInfo;
2224 SendAsyncCallback(&lpwfs->hdr, dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2228 if (!INTERNET_GetNextLine(lpwfs->sndSocket, &nRecv))
2235 if(lpszResponse[3] != '-')
2238 { /* Start of multiline repsonse. Loop until we get "nnn " */
2240 memcpy(firstprefix, lpszResponse, 3);
2241 firstprefix[3] = ' ';
2242 firstprefix[4] = '\0';
2247 if(!memcmp(firstprefix, lpszResponse, 4))
2255 rc = atoi(lpszResponse);
2257 SendAsyncCallback(&lpwfs->hdr, dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
2258 &nRecv, sizeof(DWORD));
2262 TRACE("return %d\n", rc);
2267 /***********************************************************************
2268 * FTP_SendPassword (internal)
2270 * Send password to ftp server
2277 static BOOL FTP_SendPassword(LPWININETFTPSESSIONW lpwfs)
2280 BOOL bSuccess = FALSE;
2283 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PASS, lpwfs->lpszPassword, 0, 0, 0))
2286 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2289 TRACE("Received reply code %d\n", nResCode);
2290 /* Login successful... */
2291 if (nResCode == 230)
2293 /* Command not implemented, superfluous at the server site... */
2294 /* Need account for login... */
2295 else if (nResCode == 332)
2296 bSuccess = FTP_SendAccount(lpwfs);
2298 FTP_SetResponseError(nResCode);
2302 TRACE("Returning %d\n", bSuccess);
2307 /***********************************************************************
2308 * FTP_SendAccount (internal)
2317 static BOOL FTP_SendAccount(LPWININETFTPSESSIONW lpwfs)
2320 BOOL bSuccess = FALSE;
2323 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_ACCT, szNoAccount, 0, 0, 0))
2326 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2330 FTP_SetResponseError(nResCode);
2337 /***********************************************************************
2338 * FTP_SendStore (internal)
2340 * Send request to upload file to ftp server
2347 static BOOL FTP_SendStore(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType)
2350 BOOL bSuccess = FALSE;
2353 if (!FTP_InitListenSocket(lpwfs))
2356 if (!FTP_SendType(lpwfs, dwType))
2359 if (!FTP_SendPortOrPasv(lpwfs))
2362 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_STOR, lpszRemoteFile, 0, 0, 0))
2364 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2367 if (nResCode == 150 || nResCode == 125)
2370 FTP_SetResponseError(nResCode);
2374 if (!bSuccess && lpwfs->lstnSocket != -1)
2376 closesocket(lpwfs->lstnSocket);
2377 lpwfs->lstnSocket = -1;
2384 /***********************************************************************
2385 * FTP_InitListenSocket (internal)
2387 * Create a socket to listen for server response
2394 static BOOL FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs)
2396 BOOL bSuccess = FALSE;
2397 socklen_t namelen = sizeof(struct sockaddr_in);
2401 lpwfs->lstnSocket = socket(PF_INET, SOCK_STREAM, 0);
2402 if (lpwfs->lstnSocket == -1)
2404 TRACE("Unable to create listening socket\n");
2408 /* We obtain our ip addr from the name of the command channel socket */
2409 lpwfs->lstnSocketAddress = lpwfs->socketAddress;
2411 /* and get the system to assign us a port */
2412 lpwfs->lstnSocketAddress.sin_port = htons((u_short) 0);
2414 if (bind(lpwfs->lstnSocket,(struct sockaddr *) &lpwfs->lstnSocketAddress, sizeof(struct sockaddr_in)) == -1)
2416 TRACE("Unable to bind socket\n");
2420 if (listen(lpwfs->lstnSocket, MAX_BACKLOG) == -1)
2422 TRACE("listen failed\n");
2426 if (getsockname(lpwfs->lstnSocket, (struct sockaddr *) &lpwfs->lstnSocketAddress, &namelen) != -1)
2430 if (!bSuccess && lpwfs->lstnSocket != -1)
2432 closesocket(lpwfs->lstnSocket);
2433 lpwfs->lstnSocket = -1;
2440 /***********************************************************************
2441 * FTP_SendType (internal)
2443 * Tell server type of data being transferred
2449 * W98SE doesn't cache the type that's currently set
2450 * (i.e. it sends it always),
2451 * so we probably don't want to do that either.
2453 static BOOL FTP_SendType(LPWININETFTPSESSIONW lpwfs, DWORD dwType)
2456 WCHAR type[] = { 'I','\0' };
2457 BOOL bSuccess = FALSE;
2460 if (dwType & INTERNET_FLAG_TRANSFER_ASCII)
2463 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_TYPE, type, 0, 0, 0))
2466 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext)/100;
2472 FTP_SetResponseError(nResCode);
2480 #if 0 /* FIXME: should probably be used for FtpGetFileSize */
2481 /***********************************************************************
2482 * FTP_GetFileSize (internal)
2484 * Retrieves from the server the size of the given file
2491 static BOOL FTP_GetFileSize(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD *dwSize)
2494 BOOL bSuccess = FALSE;
2498 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_SIZE, lpszRemoteFile, 0, 0, 0))
2501 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2504 if (nResCode == 213) {
2505 /* Now parses the output to get the actual file size */
2507 LPSTR lpszResponseBuffer = INTERNET_GetResponseBuffer();
2509 for (i = 0; (lpszResponseBuffer[i] != ' ') && (lpszResponseBuffer[i] != '\0'); i++) ;
2510 if (lpszResponseBuffer[i] == '\0') return FALSE;
2511 *dwSize = atol(&(lpszResponseBuffer[i + 1]));
2515 FTP_SetResponseError(nResCode);
2525 /***********************************************************************
2526 * FTP_SendPort (internal)
2528 * Tell server which port to use
2535 static BOOL FTP_SendPort(LPWININETFTPSESSIONW lpwfs)
2537 static const WCHAR szIPFormat[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
2539 WCHAR szIPAddress[64];
2540 BOOL bSuccess = FALSE;
2543 sprintfW(szIPAddress, szIPFormat,
2544 lpwfs->lstnSocketAddress.sin_addr.s_addr&0x000000FF,
2545 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0x0000FF00)>>8,
2546 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0x00FF0000)>>16,
2547 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0xFF000000)>>24,
2548 lpwfs->lstnSocketAddress.sin_port & 0xFF,
2549 (lpwfs->lstnSocketAddress.sin_port & 0xFF00)>>8);
2551 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PORT, szIPAddress, 0, 0, 0))
2554 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2557 if (nResCode == 200)
2560 FTP_SetResponseError(nResCode);
2568 /***********************************************************************
2569 * FTP_DoPassive (internal)
2571 * Tell server that we want to do passive transfers
2572 * and connect data socket
2579 static BOOL FTP_DoPassive(LPWININETFTPSESSIONW lpwfs)
2582 BOOL bSuccess = FALSE;
2585 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PASV, NULL, 0, 0, 0))
2588 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2591 if (nResCode == 227)
2593 LPSTR lpszResponseBuffer = INTERNET_GetResponseBuffer();
2597 char *pAddr, *pPort;
2599 struct sockaddr_in dataSocketAddress;
2601 p = lpszResponseBuffer+4; /* skip status code */
2602 while (*p != '\0' && (*p < '0' || *p > '9')) p++;
2606 ERR("no address found in response, aborting\n");
2610 if (sscanf(p, "%d,%d,%d,%d,%d,%d", &f[0], &f[1], &f[2], &f[3],
2613 ERR("unknown response address format '%s', aborting\n", p);
2616 for (i=0; i < 6; i++)
2619 dataSocketAddress = lpwfs->socketAddress;
2620 pAddr = (char *)&(dataSocketAddress.sin_addr.s_addr);
2621 pPort = (char *)&(dataSocketAddress.sin_port);
2629 nsocket = socket(AF_INET,SOCK_STREAM,0);
2633 if (connect(nsocket, (struct sockaddr *)&dataSocketAddress, sizeof(dataSocketAddress)))
2635 ERR("can't connect passive FTP data port.\n");
2636 closesocket(nsocket);
2639 lpwfs->pasvSocket = nsocket;
2643 FTP_SetResponseError(nResCode);
2651 static BOOL FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs)
2653 if (lpwfs->hdr.dwFlags & INTERNET_FLAG_PASSIVE)
2655 if (!FTP_DoPassive(lpwfs))
2660 if (!FTP_SendPort(lpwfs))
2667 /***********************************************************************
2668 * FTP_GetDataSocket (internal)
2670 * Either accepts an incoming data socket connection from the server
2671 * or just returns the already opened socket after a PASV command
2672 * in case of passive FTP.
2680 static BOOL FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs, LPINT nDataSocket)
2682 struct sockaddr_in saddr;
2683 socklen_t addrlen = sizeof(struct sockaddr);
2686 if (lpwfs->hdr.dwFlags & INTERNET_FLAG_PASSIVE)
2688 *nDataSocket = lpwfs->pasvSocket;
2692 *nDataSocket = accept(lpwfs->lstnSocket, (struct sockaddr *) &saddr, &addrlen);
2693 closesocket(lpwfs->lstnSocket);
2694 lpwfs->lstnSocket = -1;
2696 return *nDataSocket != -1;
2700 /***********************************************************************
2701 * FTP_SendData (internal)
2703 * Send data to the server
2710 static BOOL FTP_SendData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile)
2712 BY_HANDLE_FILE_INFORMATION fi;
2713 DWORD nBytesRead = 0;
2714 DWORD nBytesSent = 0;
2715 DWORD nTotalSent = 0;
2716 DWORD nBytesToSend, nLen;
2718 time_t s_long_time, e_long_time;
2723 lpszBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHAR)*DATA_PACKET_SIZE);
2725 /* Get the size of the file. */
2726 GetFileInformationByHandle(hFile, &fi);
2731 nBytesToSend = nBytesRead - nBytesSent;
2733 if (nBytesToSend <= 0)
2735 /* Read data from file. */
2737 if (!ReadFile(hFile, lpszBuffer, DATA_PACKET_SIZE, &nBytesRead, 0))
2738 ERR("Failed reading from file\n");
2741 nBytesToSend = nBytesRead;
2746 nLen = DATA_PACKET_SIZE < nBytesToSend ?
2747 DATA_PACKET_SIZE : nBytesToSend;
2748 nRC = send(nDataSocket, lpszBuffer, nLen, 0);
2756 /* Do some computation to display the status. */
2758 nSeconds = e_long_time - s_long_time;
2759 if( nSeconds / 60 > 0 )
2761 TRACE( "%d bytes of %d bytes (%d%%) in %d min %d sec estimated remaining time %d sec\n",
2762 nTotalSent, fi.nFileSizeLow, nTotalSent*100/fi.nFileSizeLow, nSeconds / 60,
2763 nSeconds % 60, (fi.nFileSizeLow - nTotalSent) * nSeconds / nTotalSent );
2767 TRACE( "%d bytes of %d bytes (%d%%) in %d sec estimated remaining time %d sec\n",
2768 nTotalSent, fi.nFileSizeLow, nTotalSent*100/fi.nFileSizeLow, nSeconds,
2769 (fi.nFileSizeLow - nTotalSent) * nSeconds / nTotalSent);
2771 } while (nRC != -1);
2773 TRACE("file transfer complete!\n");
2775 HeapFree(GetProcessHeap(), 0, lpszBuffer);
2781 /***********************************************************************
2782 * FTP_SendRetrieve (internal)
2784 * Send request to retrieve a file
2787 * Number of bytes to be received on success
2791 static BOOL FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType)
2797 if (!(ret = FTP_InitListenSocket(lpwfs)))
2800 if (!(ret = FTP_SendType(lpwfs, dwType)))
2803 if (!(ret = FTP_SendPortOrPasv(lpwfs)))
2806 if (!(ret = FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RETR, lpszRemoteFile, 0, 0, 0)))
2809 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2810 if ((nResCode != 125) && (nResCode != 150)) {
2811 /* That means that we got an error getting the file. */
2812 FTP_SetResponseError(nResCode);
2817 if (!ret && lpwfs->lstnSocket != -1)
2819 closesocket(lpwfs->lstnSocket);
2820 lpwfs->lstnSocket = -1;
2827 /***********************************************************************
2828 * FTP_RetrieveData (internal)
2830 * Retrieve data from server
2837 static BOOL FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile)
2839 DWORD nBytesWritten;
2840 DWORD nBytesReceived = 0;
2846 lpszBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHAR)*DATA_PACKET_SIZE);
2847 if (NULL == lpszBuffer)
2849 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2855 nRC = recv(nDataSocket, lpszBuffer, DATA_PACKET_SIZE, 0);
2858 /* other side closed socket. */
2861 WriteFile(hFile, lpszBuffer, nRC, &nBytesWritten, NULL);
2862 nBytesReceived += nRC;
2866 TRACE("Data transfer complete\n");
2869 HeapFree(GetProcessHeap(), 0, lpszBuffer);
2874 /***********************************************************************
2875 * FTP_CloseConnection (internal)
2879 static void FTP_CloseConnection(LPWININETHANDLEHEADER hdr)
2881 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) hdr;
2885 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext,
2886 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2888 if (lpwfs->download_in_progress != NULL)
2889 lpwfs->download_in_progress->session_deleted = TRUE;
2891 if (lpwfs->sndSocket != -1)
2892 closesocket(lpwfs->sndSocket);
2894 if (lpwfs->lstnSocket != -1)
2895 closesocket(lpwfs->lstnSocket);
2897 if (lpwfs->pasvSocket != -1)
2898 closesocket(lpwfs->pasvSocket);
2900 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext,
2901 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2905 /***********************************************************************
2906 * FTP_CloseSessionHandle (internal)
2908 * Deallocate session handle
2910 static void FTP_CloseSessionHandle(LPWININETHANDLEHEADER hdr)
2912 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) hdr;
2916 WININET_Release(&lpwfs->lpAppInfo->hdr);
2918 HeapFree(GetProcessHeap(), 0, lpwfs->lpszPassword);
2919 HeapFree(GetProcessHeap(), 0, lpwfs->lpszUserName);
2920 HeapFree(GetProcessHeap(), 0, lpwfs);
2924 /***********************************************************************
2925 * FTP_FindNextFileW (Internal)
2927 * Continues a file search from a previous call to FindFirstFile
2934 BOOL WINAPI FTP_FindNextFileW(LPWININETFTPFINDNEXTW lpwh, LPVOID lpvFindData)
2936 BOOL bSuccess = TRUE;
2937 LPWIN32_FIND_DATAW lpFindFileData;
2939 TRACE("index(%d) size(%d)\n", lpwh->index, lpwh->size);
2941 assert (lpwh->hdr.htype == WH_HFTPFINDNEXT);
2943 /* Clear any error information */
2944 INTERNET_SetLastError(0);
2946 lpFindFileData = (LPWIN32_FIND_DATAW) lpvFindData;
2947 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
2949 if (lpwh->index >= lpwh->size)
2951 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
2956 FTP_ConvertFileProp(&lpwh->lpafp[lpwh->index], lpFindFileData);
2959 TRACE("\nName: %s\nSize: %d\n", debugstr_w(lpFindFileData->cFileName), lpFindFileData->nFileSizeLow);
2963 if (lpwh->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2965 INTERNET_ASYNC_RESULT iar;
2967 iar.dwResult = (DWORD)bSuccess;
2968 iar.dwError = iar.dwError = bSuccess ? ERROR_SUCCESS :
2969 INTERNET_GetLastError();
2971 INTERNET_SendCallback(&lpwh->hdr, lpwh->hdr.dwContext,
2972 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2973 sizeof(INTERNET_ASYNC_RESULT));
2980 /***********************************************************************
2981 * FTP_CloseFindNextHandle (internal)
2983 * Deallocate session handle
2990 static void FTP_CloseFindNextHandle(LPWININETHANDLEHEADER hdr)
2992 LPWININETFTPFINDNEXTW lpwfn = (LPWININETFTPFINDNEXTW) hdr;
2997 WININET_Release(&lpwfn->lpFtpSession->hdr);
2999 for (i = 0; i < lpwfn->size; i++)
3001 HeapFree(GetProcessHeap(), 0, lpwfn->lpafp[i].lpszName);
3004 HeapFree(GetProcessHeap(), 0, lpwfn->lpafp);
3005 HeapFree(GetProcessHeap(), 0, lpwfn);
3008 /***********************************************************************
3009 * FTP_CloseFileTransferHandle (internal)
3011 * Closes the file transfer handle. This also 'cleans' the data queue of
3012 * the 'transfer complete' message (this is a bit of a hack though :-/ )
3015 static void FTP_CloseFileTransferHandle(LPWININETHANDLEHEADER hdr)
3017 LPWININETFTPFILE lpwh = (LPWININETFTPFILE) hdr;
3018 LPWININETFTPSESSIONW lpwfs = lpwh->lpFtpSession;
3023 WININET_Release(&lpwh->lpFtpSession->hdr);
3025 if (!lpwh->session_deleted)
3026 lpwfs->download_in_progress = NULL;
3028 if (lpwh->nDataSocket != -1)
3029 closesocket(lpwh->nDataSocket);
3031 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
3032 if (nResCode > 0 && nResCode != 226) WARN("server reports failed transfer\n");
3034 HeapFree(GetProcessHeap(), 0, lpwh);
3037 /***********************************************************************
3038 * FTP_ReceiveFileList (internal)
3040 * Read file list from server
3043 * Handle to file list on success
3047 static HINTERNET FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
3048 LPWIN32_FIND_DATAW lpFindFileData, DWORD_PTR dwContext)
3051 LPFILEPROPERTIESW lpafp = NULL;
3052 LPWININETFTPFINDNEXTW lpwfn = NULL;
3053 HINTERNET handle = 0;
3055 TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs, nSocket, debugstr_w(lpszSearchFile), lpFindFileData, dwContext);
3057 if (FTP_ParseDirectory(lpwfs, nSocket, lpszSearchFile, &lpafp, &dwSize))
3060 FTP_ConvertFileProp(lpafp, lpFindFileData);
3062 lpwfn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETFTPFINDNEXTW));
3065 lpwfn->hdr.htype = WH_HFTPFINDNEXT;
3066 lpwfn->hdr.dwContext = dwContext;
3067 lpwfn->hdr.dwRefCount = 1;
3068 lpwfn->hdr.close_connection = NULL;
3069 lpwfn->hdr.destroy = FTP_CloseFindNextHandle;
3070 lpwfn->hdr.lpfnStatusCB = lpwfs->hdr.lpfnStatusCB;
3071 lpwfn->index = 1; /* Next index is 1 since we return index 0 */
3072 lpwfn->size = dwSize;
3073 lpwfn->lpafp = lpafp;
3075 WININET_AddRef( &lpwfs->hdr );
3076 lpwfn->lpFtpSession = lpwfs;
3077 list_add_head( &lpwfs->hdr.children, &lpwfn->hdr.entry );
3079 handle = WININET_AllocHandle( &lpwfn->hdr );
3084 WININET_Release( &lpwfn->hdr );
3086 TRACE("Matched %d files\n", dwSize);
3091 /***********************************************************************
3092 * FTP_ConvertFileProp (internal)
3094 * Converts FILEPROPERTIESW struct to WIN32_FIND_DATAA
3101 BOOL FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp, LPWIN32_FIND_DATAW lpFindFileData)
3103 BOOL bSuccess = FALSE;
3105 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAW));
3109 /* Convert 'Unix' time to Windows time */
3110 RtlSecondsSince1970ToTime(mktime(&lpafp->tmLastModified),
3111 (LARGE_INTEGER *) &(lpFindFileData->ftLastAccessTime));
3112 lpFindFileData->ftLastWriteTime = lpFindFileData->ftLastAccessTime;
3113 lpFindFileData->ftCreationTime = lpFindFileData->ftLastAccessTime;
3115 /* Not all fields are filled in */
3116 lpFindFileData->nFileSizeHigh = 0; /* We do not handle files bigger than 0xFFFFFFFF bytes yet :-) */
3117 lpFindFileData->nFileSizeLow = lpafp->nSize;
3119 if (lpafp->bIsDirectory)
3120 lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
3122 if (lpafp->lpszName)
3123 lstrcpynW(lpFindFileData->cFileName, lpafp->lpszName, MAX_PATH);
3131 /***********************************************************************
3132 * FTP_ParseNextFile (internal)
3134 * Parse the next line in file listing
3140 static BOOL FTP_ParseNextFile(INT nSocket, LPCWSTR lpszSearchFile, LPFILEPROPERTIESW lpfp)
3142 static const char szSpace[] = " \t";
3150 lpfp->lpszName = NULL;
3152 if(!(pszLine = INTERNET_GetNextLine(nSocket, &nBufLen)))
3155 pszToken = strtok(pszLine, szSpace);
3157 * <Permissions> <NoLinks> <owner> <group> <size> <date> <time or year> <filename>
3160 * drwx--s--- 2 pcarrier ens 512 Sep 28 1995 pcarrier
3162 if(!isdigit(pszToken[0]) && 10 == strlen(pszToken)) {
3163 if(!FTP_ParsePermission(pszToken, lpfp))
3164 lpfp->bIsDirectory = FALSE;
3165 for(i=0; i<=3; i++) {
3166 if(!(pszToken = strtok(NULL, szSpace)))
3169 if(!pszToken) continue;
3170 if(lpfp->bIsDirectory) {
3171 TRACE("Is directory\n");
3175 TRACE("Size: %s\n", pszToken);
3176 lpfp->nSize = atol(pszToken);
3179 lpfp->tmLastModified.tm_sec = 0;
3180 lpfp->tmLastModified.tm_min = 0;
3181 lpfp->tmLastModified.tm_hour = 0;
3182 lpfp->tmLastModified.tm_mday = 0;
3183 lpfp->tmLastModified.tm_mon = 0;
3184 lpfp->tmLastModified.tm_year = 0;
3186 /* Determine month */
3187 pszToken = strtok(NULL, szSpace);
3188 if(!pszToken) continue;
3189 if(strlen(pszToken) >= 3) {
3191 if((pszTmp = StrStrIA(szMonths, pszToken)))
3192 lpfp->tmLastModified.tm_mon = ((pszTmp - szMonths) / 3)+1;
3195 pszToken = strtok(NULL, szSpace);
3196 if(!pszToken) continue;
3197 lpfp->tmLastModified.tm_mday = atoi(pszToken);
3198 /* Determine time or year */
3199 pszToken = strtok(NULL, szSpace);
3200 if(!pszToken) continue;
3201 if((pszTmp = strchr(pszToken, ':'))) {
3206 lpfp->tmLastModified.tm_min = atoi(pszTmp);
3207 lpfp->tmLastModified.tm_hour = atoi(pszToken);
3209 apTM = localtime(&aTime);
3210 lpfp->tmLastModified.tm_year = apTM->tm_year;
3213 lpfp->tmLastModified.tm_year = atoi(pszToken) - 1900;
3214 lpfp->tmLastModified.tm_hour = 12;
3216 TRACE("Mod time: %02d:%02d:%02d %02d/%02d/%02d\n",
3217 lpfp->tmLastModified.tm_hour, lpfp->tmLastModified.tm_min, lpfp->tmLastModified.tm_sec,
3218 (lpfp->tmLastModified.tm_year >= 100) ? lpfp->tmLastModified.tm_year - 100 : lpfp->tmLastModified.tm_year,
3219 lpfp->tmLastModified.tm_mon, lpfp->tmLastModified.tm_mday);
3221 pszToken = strtok(NULL, szSpace);
3222 if(!pszToken) continue;
3223 lpfp->lpszName = WININET_strdup_AtoW(pszToken);
3224 TRACE("File: %s\n", debugstr_w(lpfp->lpszName));
3226 /* NT way of parsing ... :
3228 07-13-03 08:55PM <DIR> sakpatch
3229 05-09-03 06:02PM 12656686 2003-04-21bgm_cmd_e.rgz
3231 else if(isdigit(pszToken[0]) && 8 == strlen(pszToken)) {
3232 lpfp->permissions = 0xFFFF; /* No idea, put full permission :-) */
3234 sscanf(pszToken, "%d-%d-%d",
3235 &lpfp->tmLastModified.tm_mon,
3236 &lpfp->tmLastModified.tm_mday,
3237 &lpfp->tmLastModified.tm_year);
3239 /* Hacky and bad Y2K protection :-) */
3240 if (lpfp->tmLastModified.tm_year < 70)
3241 lpfp->tmLastModified.tm_year += 100;
3243 pszToken = strtok(NULL, szSpace);
3244 if(!pszToken) continue;
3245 sscanf(pszToken, "%d:%d",
3246 &lpfp->tmLastModified.tm_hour,
3247 &lpfp->tmLastModified.tm_min);
3248 if((pszToken[5] == 'P') && (pszToken[6] == 'M')) {
3249 lpfp->tmLastModified.tm_hour += 12;
3251 lpfp->tmLastModified.tm_sec = 0;
3253 TRACE("Mod time: %02d:%02d:%02d %02d/%02d/%02d\n",
3254 lpfp->tmLastModified.tm_hour, lpfp->tmLastModified.tm_min, lpfp->tmLastModified.tm_sec,
3255 (lpfp->tmLastModified.tm_year >= 100) ? lpfp->tmLastModified.tm_year - 100 : lpfp->tmLastModified.tm_year,
3256 lpfp->tmLastModified.tm_mon, lpfp->tmLastModified.tm_mday);
3258 pszToken = strtok(NULL, szSpace);
3259 if(!pszToken) continue;
3260 if(!strcasecmp(pszToken, "<DIR>")) {
3261 lpfp->bIsDirectory = TRUE;
3263 TRACE("Is directory\n");
3266 lpfp->bIsDirectory = FALSE;
3267 lpfp->nSize = atol(pszToken);
3268 TRACE("Size: %d\n", lpfp->nSize);
3271 pszToken = strtok(NULL, szSpace);
3272 if(!pszToken) continue;
3273 lpfp->lpszName = WININET_strdup_AtoW(pszToken);
3274 TRACE("Name: %s\n", debugstr_w(lpfp->lpszName));
3276 /* EPLF format - http://cr.yp.to/ftp/list/eplf.html */
3277 else if(pszToken[0] == '+') {
3278 FIXME("EPLF Format not implemented\n");
3281 if(lpfp->lpszName) {
3282 if((lpszSearchFile == NULL) ||
3283 (PathMatchSpecW(lpfp->lpszName, lpszSearchFile))) {
3285 TRACE("Matched: %s\n", debugstr_w(lpfp->lpszName));
3288 HeapFree(GetProcessHeap(), 0, lpfp->lpszName);
3289 lpfp->lpszName = NULL;
3296 /***********************************************************************
3297 * FTP_ParseDirectory (internal)
3299 * Parse string of directory information
3305 static BOOL FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
3306 LPFILEPROPERTIESW *lpafp, LPDWORD dwfp)
3308 BOOL bSuccess = TRUE;
3309 INT sizeFilePropArray = 500;/*20; */
3310 INT indexFilePropArray = -1;
3314 /* Allocate intial file properties array */
3315 *lpafp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FILEPROPERTIESW)*(sizeFilePropArray));
3320 if (indexFilePropArray+1 >= sizeFilePropArray)
3322 LPFILEPROPERTIESW tmpafp;
3324 sizeFilePropArray *= 2;
3325 tmpafp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *lpafp,
3326 sizeof(FILEPROPERTIESW)*sizeFilePropArray);
3335 indexFilePropArray++;
3336 } while (FTP_ParseNextFile(nSocket, lpszSearchFile, &(*lpafp)[indexFilePropArray]));
3338 if (bSuccess && indexFilePropArray)
3340 if (indexFilePropArray < sizeFilePropArray - 1)
3342 LPFILEPROPERTIESW tmpafp;
3344 tmpafp = HeapReAlloc(GetProcessHeap(), 0, *lpafp,
3345 sizeof(FILEPROPERTIESW)*indexFilePropArray);
3349 *dwfp = indexFilePropArray;
3353 HeapFree(GetProcessHeap(), 0, *lpafp);
3354 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
3362 /***********************************************************************
3363 * FTP_ParsePermission (internal)
3365 * Parse permission string of directory information
3372 static BOOL FTP_ParsePermission(LPCSTR lpszPermission, LPFILEPROPERTIESW lpfp)
3374 BOOL bSuccess = TRUE;
3375 unsigned short nPermission = 0;
3380 if ((*lpszPermission != 'd') && (*lpszPermission != '-') && (*lpszPermission != 'l'))
3386 lpfp->bIsDirectory = (*lpszPermission == 'd');
3392 nPermission |= (*(lpszPermission+1) == 'r' ? 1 : 0) << 8;
3395 nPermission |= (*(lpszPermission+2) == 'w' ? 1 : 0) << 7;
3398 nPermission |= (*(lpszPermission+3) == 'x' ? 1 : 0) << 6;
3401 nPermission |= (*(lpszPermission+4) == 'r' ? 1 : 0) << 5;
3404 nPermission |= (*(lpszPermission+5) == 'w' ? 1 : 0) << 4;
3407 nPermission |= (*(lpszPermission+6) == 'x' ? 1 : 0) << 3;
3410 nPermission |= (*(lpszPermission+7) == 'r' ? 1 : 0) << 2;
3413 nPermission |= (*(lpszPermission+8) == 'w' ? 1 : 0) << 1;
3416 nPermission |= (*(lpszPermission+9) == 'x' ? 1 : 0);
3420 }while (nPos <= nLast);
3422 lpfp->permissions = nPermission;
3427 /***********************************************************************
3428 * FTP_SetResponseError (internal)
3430 * Set the appropriate error code for a given response from the server
3435 static DWORD FTP_SetResponseError(DWORD dwResponse)
3441 case 421: /* Service not available - Server may be shutting down. */
3442 dwCode = ERROR_INTERNET_EXTENDED_ERROR;
3445 case 425: /* Cannot open data connection. */
3446 dwCode = ERROR_INTERNET_CANNOT_CONNECT;
3449 case 426: /* Connection closed, transer aborted. */
3450 dwCode = ERROR_INTERNET_CONNECTION_ABORTED;
3453 case 500: /* Syntax error. Command unrecognized. */
3454 case 501: /* Syntax error. Error in parameters or arguments. */
3455 dwCode = ERROR_INTERNET_INCORRECT_FORMAT;
3458 case 530: /* Not logged in. Login incorrect. */
3459 dwCode = ERROR_INTERNET_LOGIN_FAILURE;
3462 case 550: /* File action not taken. File not found or no access. */
3463 dwCode = ERROR_INTERNET_ITEM_NOT_FOUND;
3466 case 450: /* File action not taken. File may be busy. */
3467 case 451: /* Action aborted. Server error. */
3468 case 452: /* Action not taken. Insufficient storage space on server. */
3469 case 502: /* Command not implemented. */
3470 case 503: /* Bad sequence of commands. */
3471 case 504: /* Command not implemented for that parameter. */
3472 case 532: /* Need account for storing files */
3473 case 551: /* Requested action aborted. Page type unknown */
3474 case 552: /* Action aborted. Exceeded storage allocation */
3475 case 553: /* Action not taken. File name not allowed. */
3478 dwCode = ERROR_INTERNET_EXTENDED_ERROR;
3482 INTERNET_SetLastError(dwCode);