wininet: Use a blank password if none is provided in FTP_Connect.
[wine] / dlls / shell32 / shlfileop.c
1 /*
2  * SHFileOperation
3  *
4  * Copyright 2000 Juergen Schmied
5  * Copyright 2002 Andriy Palamarchuk
6  * Copyright 2004 Dietrich Teickner (from Odin)
7  * Copyright 2004 Rolf Kalbermatter
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdarg.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winreg.h"
34 #include "shellapi.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37 #include "shlobj.h"
38 #include "shresdef.h"
39 #define NO_SHLWAPI_STREAM
40 #include "shlwapi.h"
41 #include "shell32_main.h"
42 #include "undocshell.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(shell);
47
48 #define IsAttrib(x, y)  ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
49 #define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
50 #define IsAttribDir(x)  IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
51 #define IsDotDir(x)     ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
52
53 #define FO_MASK         0xF
54
55 static const WCHAR wWildcardFile[] = {'*',0};
56 static const WCHAR wWildcardChars[] = {'*','?',0};
57
58 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec);
59 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec);
60 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path);
61 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path);
62 static DWORD SHNotifyDeleteFileA(LPCSTR path);
63 static DWORD SHNotifyDeleteFileW(LPCWSTR path);
64 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest);
65 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists);
66 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly);
67
68 typedef struct
69 {
70         HINSTANCE hIconInstance;
71         UINT icon_resource_id;
72         UINT caption_resource_id, text_resource_id;
73 } SHELL_ConfirmIDstruc;
74
75 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
76 {
77         ids->hIconInstance = shell32_hInstance;
78         switch (nKindOfDialog) {
79           case ASK_DELETE_FILE:
80             ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
81             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
82             ids->text_resource_id  = IDS_DELETEITEM_TEXT;
83             return TRUE;
84           case ASK_DELETE_FOLDER:
85             ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
86             ids->caption_resource_id  = IDS_DELETEFOLDER_CAPTION;
87             ids->text_resource_id  = IDS_DELETEITEM_TEXT;
88             return TRUE;
89           case ASK_DELETE_MULTIPLE_ITEM:
90             ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
91             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
92             ids->text_resource_id  = IDS_DELETEMULTIPLE_TEXT;
93             return TRUE;
94           case ASK_OVERWRITE_FILE:
95             ids->hIconInstance = NULL;
96             ids->icon_resource_id = IDI_WARNING;
97             ids->caption_resource_id  = IDS_OVERWRITEFILE_CAPTION;
98             ids->text_resource_id  = IDS_OVERWRITEFILE_TEXT;
99             return TRUE;
100           default:
101             FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
102         }
103         return FALSE;
104 }
105
106 BOOL SHELL_ConfirmDialogW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir)
107 {
108         WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
109         SHELL_ConfirmIDstruc ids;
110         MSGBOXPARAMSW params;
111
112         if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
113           return FALSE;
114
115         LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption)/sizeof(WCHAR));
116         LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText)/sizeof(WCHAR));
117
118         FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
119                        szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
120
121         ZeroMemory(&params, sizeof(params));
122         params.cbSize = sizeof(MSGBOXPARAMSW);
123         params.hwndOwner = hWnd;
124         params.hInstance = ids.hIconInstance;
125         params.lpszText = szBuffer;
126         params.lpszCaption = szCaption;
127         params.lpszIcon = (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id);
128         params.dwStyle = MB_OKCANCEL | MB_USERICON;
129
130         return (IDOK == MessageBoxIndirectW(&params));
131 }
132
133 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
134 {
135         DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
136
137         if (len < minChars)
138           len = minChars;
139
140         *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
141         if (*wPath)
142         {
143           MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
144           return NO_ERROR;
145         }
146         return E_OUTOFMEMORY;
147 }
148
149 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
150 {
151         HeapFree(GetProcessHeap(), 0, wPath);
152 }
153
154 /**************************************************************************
155  * SHELL_DeleteDirectory()  [internal]
156  *
157  * Asks for confirmation when bShowUI is true and deletes the directory and
158  * all its subdirectories and files if necessary.
159  */
160 BOOL SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
161 {
162         BOOL    ret = TRUE;
163         HANDLE  hFind;
164         WIN32_FIND_DATAW wfd;
165         WCHAR   szTemp[MAX_PATH];
166
167         /* Make sure the directory exists before eventually prompting the user */
168         PathCombineW(szTemp, pszDir, wWildcardFile);
169         hFind = FindFirstFileW(szTemp, &wfd);
170         if (hFind == INVALID_HANDLE_VALUE)
171           return FALSE;
172
173         if (!bShowUI || (ret = SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir)))
174         {
175           do
176           {
177             LPWSTR lp = wfd.cAlternateFileName;
178             if (!lp[0])
179               lp = wfd.cFileName;
180             if (IsDotDir(lp))
181               continue;
182             PathCombineW(szTemp, pszDir, lp);
183             if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
184               ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
185             else
186               ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
187           } while (ret && FindNextFileW(hFind, &wfd));
188         }
189         FindClose(hFind);
190         if (ret)
191           ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
192         return ret;
193 }
194
195 /**************************************************************************
196  * Win32CreateDirectory      [SHELL32.93]
197  *
198  * Creates a directory. Also triggers a change notify if one exists.
199  *
200  * PARAMS
201  *  path       [I]   path to directory to create
202  *
203  * RETURNS
204  *  TRUE if successful, FALSE otherwise
205  *
206  * NOTES
207  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
208  *  This is Unicode on NT/2000
209  */
210 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
211 {
212         LPWSTR wPath;
213         DWORD retCode;
214
215         TRACE("(%s, %p)\n", debugstr_a(path), sec);
216
217         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
218         if (!retCode)
219         {
220           retCode = SHNotifyCreateDirectoryW(wPath, sec);
221           SHELL32_FreeUnicodeBuf(wPath);
222         }
223         return retCode;
224 }
225
226 /**********************************************************************/
227
228 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
229 {
230         TRACE("(%s, %p)\n", debugstr_w(path), sec);
231
232         if (CreateDirectoryW(path, sec))
233         {
234           SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
235           return ERROR_SUCCESS;
236         }
237         return GetLastError();
238 }
239
240 /**********************************************************************/
241
242 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
243 {
244         if (SHELL_OsIsUnicode())
245           return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
246         return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
247 }
248
249 /************************************************************************
250  * Win32RemoveDirectory      [SHELL32.94]
251  *
252  * Deletes a directory. Also triggers a change notify if one exists.
253  *
254  * PARAMS
255  *  path       [I]   path to directory to delete
256  *
257  * RETURNS
258  *  TRUE if successful, FALSE otherwise
259  *
260  * NOTES
261  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
262  *  This is Unicode on NT/2000
263  */
264 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
265 {
266         LPWSTR wPath;
267         DWORD retCode;
268
269         TRACE("(%s)\n", debugstr_a(path));
270
271         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
272         if (!retCode)
273         {
274           retCode = SHNotifyRemoveDirectoryW(wPath);
275           SHELL32_FreeUnicodeBuf(wPath);
276         }
277         return retCode;
278 }
279
280 /***********************************************************************/
281
282 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
283 {
284         BOOL ret;
285         TRACE("(%s)\n", debugstr_w(path));
286
287         ret = RemoveDirectoryW(path);
288         if (!ret)
289         {
290           /* Directory may be write protected */
291           DWORD dwAttr = GetFileAttributesW(path);
292           if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
293             if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
294               ret = RemoveDirectoryW(path);
295         }
296         if (ret)
297         {
298           SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
299           return ERROR_SUCCESS;
300         }
301         return GetLastError();
302 }
303
304 /***********************************************************************/
305
306 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
307 {
308         if (SHELL_OsIsUnicode())
309           return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
310         return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
311 }
312
313 /************************************************************************
314  * Win32DeleteFile           [SHELL32.164]
315  *
316  * Deletes a file. Also triggers a change notify if one exists.
317  *
318  * PARAMS
319  *  path       [I]   path to file to delete
320  *
321  * RETURNS
322  *  TRUE if successful, FALSE otherwise
323  *
324  * NOTES
325  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
326  *  This is Unicode on NT/2000
327  */
328 static DWORD SHNotifyDeleteFileA(LPCSTR path)
329 {
330         LPWSTR wPath;
331         DWORD retCode;
332
333         TRACE("(%s)\n", debugstr_a(path));
334
335         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
336         if (!retCode)
337         {
338           retCode = SHNotifyDeleteFileW(wPath);
339           SHELL32_FreeUnicodeBuf(wPath);
340         }
341         return retCode;
342 }
343
344 /***********************************************************************/
345
346 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
347 {
348         BOOL ret;
349
350         TRACE("(%s)\n", debugstr_w(path));
351
352         ret = DeleteFileW(path);
353         if (!ret)
354         {
355           /* File may be write protected or a system file */
356           DWORD dwAttr = GetFileAttributesW(path);
357           if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
358             if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
359               ret = DeleteFileW(path);
360         }
361         if (ret)
362         {
363           SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
364           return ERROR_SUCCESS;
365         }
366         return GetLastError();
367 }
368
369 /***********************************************************************/
370
371 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
372 {
373         if (SHELL_OsIsUnicode())
374           return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
375         return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
376 }
377
378 /************************************************************************
379  * SHNotifyMoveFile          [internal]
380  *
381  * Moves a file. Also triggers a change notify if one exists.
382  *
383  * PARAMS
384  *  src        [I]   path to source file to move
385  *  dest       [I]   path to target file to move to
386  *
387  * RETURNS
388  *  ERORR_SUCCESS if successful
389  */
390 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
391 {
392         BOOL ret;
393
394         TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
395
396         ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
397
398         /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
399         if (!ret)
400             ret = MoveFileW(src, dest);
401
402         if (!ret)
403         {
404           DWORD dwAttr;
405
406           dwAttr = SHFindAttrW(dest, FALSE);
407           if (INVALID_FILE_ATTRIBUTES == dwAttr)
408           {
409             /* Source file may be write protected or a system file */
410             dwAttr = GetFileAttributesW(src);
411             if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
412               if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
413                 ret = MoveFileW(src, dest);
414           }
415         }
416         if (ret)
417         {
418           SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
419           return ERROR_SUCCESS;
420         }
421         return GetLastError();
422 }
423
424 /************************************************************************
425  * SHNotifyCopyFile          [internal]
426  *
427  * Copies a file. Also triggers a change notify if one exists.
428  *
429  * PARAMS
430  *  src           [I]   path to source file to move
431  *  dest          [I]   path to target file to move to
432  *  bFailIfExists [I]   if TRUE, the target file will not be overwritten if
433  *                      a file with this name already exists
434  *
435  * RETURNS
436  *  ERROR_SUCCESS if successful
437  */
438 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
439 {
440         BOOL ret;
441
442         TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
443
444         ret = CopyFileW(src, dest, bFailIfExists);
445         if (ret)
446         {
447           SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
448           return ERROR_SUCCESS;
449         }
450
451         return GetLastError();
452 }
453
454 /*************************************************************************
455  * SHCreateDirectory         [SHELL32.165]
456  *
457  * This function creates a file system folder whose fully qualified path is
458  * given by path. If one or more of the intermediate folders do not exist,
459  * they will be created as well.
460  *
461  * PARAMS
462  *  hWnd       [I]
463  *  path       [I]   path of directory to create
464  *
465  * RETURNS
466  *  ERRROR_SUCCESS or one of the following values:
467  *  ERROR_BAD_PATHNAME if the path is relative
468  *  ERROR_FILE_EXISTS when a file with that name exists
469  *  ERROR_PATH_NOT_FOUND can't find the path, probably invalid
470  *  ERROR_INVLID_NAME if the path contains invalid chars
471  *  ERROR_ALREADY_EXISTS when the directory already exists
472  *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
473  *
474  * NOTES
475  *  exported by ordinal
476  *  Win9x exports ANSI
477  *  WinNT/2000 exports Unicode
478  */
479 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
480 {
481         if (SHELL_OsIsUnicode())
482           return SHCreateDirectoryExW(hWnd, path, NULL);
483         return SHCreateDirectoryExA(hWnd, path, NULL);
484 }
485
486 /*************************************************************************
487  * SHCreateDirectoryExA      [SHELL32.@]
488  *
489  * This function creates a file system folder whose fully qualified path is
490  * given by path. If one or more of the intermediate folders do not exist,
491  * they will be created as well.
492  *
493  * PARAMS
494  *  hWnd       [I]
495  *  path       [I]   path of directory to create
496  *  sec        [I]   security attributes to use or NULL
497  *
498  * RETURNS
499  *  ERRROR_SUCCESS or one of the following values:
500  *  ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
501  *  ERROR_INVLID_NAME if the path contains invalid chars
502  *  ERROR_FILE_EXISTS when a file with that name exists
503  *  ERROR_ALREADY_EXISTS when the directory already exists
504  *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
505  *
506  *  FIXME: Not implemented yet;
507  *  SHCreateDirectoryEx also verifies that the files in the directory will be visible
508  *  if the path is a network path to deal with network drivers which might have a limited
509  *  but unknown maximum path length. If not:
510  *
511  *  If hWnd is set to a valid window handle, a message box is displayed warning
512  *  the user that the files may not be accessible. If the user chooses not to
513  *  proceed, the function returns ERROR_CANCELLED.
514  *
515  *  If hWnd is set to NULL, no user interface is displayed and the function
516  *  returns ERROR_CANCELLED.
517  */
518 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
519 {
520         LPWSTR wPath;
521         DWORD retCode;
522
523         TRACE("(%s, %p)\n", debugstr_a(path), sec);
524
525         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
526         if (!retCode)
527         {
528           retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
529           SHELL32_FreeUnicodeBuf(wPath);
530         }
531         return retCode;
532 }
533
534 /*************************************************************************
535  * SHCreateDirectoryExW      [SHELL32.@]
536  *
537  * See SHCreateDirectoryExA.
538  */
539 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
540 {
541         int ret = ERROR_BAD_PATHNAME;
542         TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
543
544         if (PathIsRelativeW(path))
545         {
546           SetLastError(ret);
547         }
548         else
549         {
550           ret = SHNotifyCreateDirectoryW(path, sec);
551           /* Refuse to work on certain error codes before trying to create directories recursively */
552           if (ret != ERROR_SUCCESS &&
553               ret != ERROR_FILE_EXISTS &&
554               ret != ERROR_ALREADY_EXISTS &&
555               ret != ERROR_FILENAME_EXCED_RANGE)
556           {
557             WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1];  /* extra for PathAddBackslash() */
558
559             lstrcpynW(szTemp, path, MAX_PATH);
560             pEnd = PathAddBackslashW(szTemp);
561             pSlash = szTemp + 3;
562
563             while (*pSlash)
564             {
565               while (*pSlash && *pSlash != '\\')
566                 pSlash = CharNextW(pSlash);
567
568               if (*pSlash)
569               {
570                 *pSlash = 0;    /* terminate path at separator */
571
572                 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
573               }
574               *pSlash++ = '\\'; /* put the separator back */
575             }
576           }
577
578           if (ret && hWnd && (ERROR_CANCELLED != ret))
579           {
580             /* We failed and should show a dialog box */
581             FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
582             ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
583           }
584         }
585         return ret;
586 }
587
588 /*************************************************************************
589  * SHFindAttrW      [internal]
590  *
591  * Get the Attributes for a file or directory. The difference to GetAttributes()
592  * is that this function will also work for paths containing wildcard characters
593  * in its filename.
594
595  * PARAMS
596  *  path       [I]   path of directory or file to check
597  *  fileOnly   [I]   TRUE if only files should be found
598  *
599  * RETURNS
600  *  INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
601  *  the first file or directory found otherwise
602  */
603 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
604 {
605         WIN32_FIND_DATAW wfd;
606         BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
607         DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
608         HANDLE hFind = FindFirstFileW(pName, &wfd);
609
610         TRACE("%s %d\n", debugstr_w(pName), fileOnly);
611         if (INVALID_HANDLE_VALUE != hFind)
612         {
613           do
614           {
615             if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
616                continue;
617             dwAttr = wfd.dwFileAttributes;
618             break;
619           }
620           while (FindNextFileW(hFind, &wfd));
621           FindClose(hFind);
622         }
623         return dwAttr;
624 }
625
626 /*************************************************************************
627  *
628  * SHNameTranslate HelperFunction for SHFileOperationA
629  *
630  * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
631  * is NULL, only the necessary size of the string is determined and returned,
632  * otherwise the ASCII strings are copied into it and the buffer is increased
633  * to point to the location after the final 0 termination char.
634  */
635 DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
636 {
637         DWORD size = 0, aSize = 0;
638         LPCSTR aString = (LPCSTR)*pWToFrom;
639
640         if (aString)
641         {
642           do
643           {
644             size = lstrlenA(aString) + 1;
645             aSize += size;
646             aString += size;
647           } while ((size != 1) && more);
648           /* The two sizes might be different in the case of multibyte chars */
649           size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
650           if (*wString) /* only in the second loop */
651           {
652             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
653             *pWToFrom = *wString;
654             *wString += size;
655           }
656         }
657         return size;
658 }
659 /*************************************************************************
660  * SHFileOperationA          [SHELL32.@]
661  *
662  * Function to copy, move, delete and create one or more files with optional
663  * user prompts.
664  *
665  * PARAMS
666  *  lpFileOp   [I/O] pointer to a structure containing all the necessary information
667  *
668  * RETURNS
669  *  Success: ERROR_SUCCESS.
670  *  Failure: ERROR_CANCELLED.
671  *
672  * NOTES
673  *  exported by name
674  */
675 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
676 {
677         SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
678         int retCode = 0;
679         DWORD size;
680         LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
681                wString = NULL; /* we change this in SHNameTranslate */
682
683         TRACE("\n");
684         if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
685           nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
686         if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
687           nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
688         while (1) /* every loop calculate size, second translate also, if we have storage for this */
689         {
690           size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
691           size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
692           size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
693
694           if (ForFree)
695           {
696             retCode = SHFileOperationW(&nFileOp);
697             HeapFree(GetProcessHeap(), 0, ForFree); /* we cannot use wString, it was changed */
698             break;
699           }
700           else
701           {
702             wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
703             if (ForFree) continue;
704             retCode = ERROR_OUTOFMEMORY;
705             nFileOp.fAnyOperationsAborted = TRUE;
706             SetLastError(retCode);
707             return retCode;
708           }
709         }
710
711         lpFileOp->hNameMappings = nFileOp.hNameMappings;
712         lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
713         return retCode;
714 }
715
716 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
717
718 typedef struct
719 {
720     DWORD attributes;
721     LPWSTR szDirectory;
722     LPWSTR szFilename;
723     LPWSTR szFullPath;
724     BOOL bFromWildcard;
725     BOOL bFromRelative;
726     BOOL bExists;
727 } FILE_ENTRY;
728
729 typedef struct
730 {
731     FILE_ENTRY *feFiles;
732     DWORD num_alloc;
733     DWORD dwNumFiles;
734     BOOL bAnyFromWildcard;
735     BOOL bAnyDirectories;
736     BOOL bAnyDontExist;
737 } FILE_LIST;
738
739
740 static inline void grow_list(FILE_LIST *list)
741 {
742     FILE_ENTRY *new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
743                                   list->num_alloc * 2 * sizeof(*new) );
744     list->feFiles = new;
745     list->num_alloc *= 2;
746 }
747
748 /* adds a file to the FILE_ENTRY struct
749  */
750 static void add_file_to_entry(FILE_ENTRY *feFile, LPWSTR szFile)
751 {
752     DWORD dwLen = strlenW(szFile) + 1;
753     LPWSTR ptr;
754
755     feFile->szFullPath = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
756     strcpyW(feFile->szFullPath, szFile);
757
758     ptr = StrRChrW(szFile, NULL, '\\');
759     if (ptr)
760     {
761         dwLen = ptr - szFile + 1;
762         feFile->szDirectory = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
763         lstrcpynW(feFile->szDirectory, szFile, dwLen);
764
765         dwLen = strlenW(feFile->szFullPath) - dwLen + 1;
766         feFile->szFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
767         strcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
768     }
769     feFile->bFromWildcard = FALSE;
770 }
771
772 static LPWSTR wildcard_to_file(LPWSTR szWildCard, LPWSTR szFileName)
773 {
774     LPWSTR szFullPath, ptr;
775     DWORD dwDirLen, dwFullLen;
776
777     ptr = StrRChrW(szWildCard, NULL, '\\');
778     dwDirLen = ptr - szWildCard + 1;
779
780     dwFullLen = dwDirLen + strlenW(szFileName) + 1;
781     szFullPath = HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
782
783     lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
784     strcatW(szFullPath, szFileName);
785
786     return szFullPath;
787 }
788
789 static void parse_wildcard_files(FILE_LIST *flList, LPWSTR szFile, LPDWORD pdwListIndex)
790 {
791     WIN32_FIND_DATAW wfd;
792     HANDLE hFile = FindFirstFileW(szFile, &wfd);
793     FILE_ENTRY *file;
794     LPWSTR szFullPath;
795     BOOL res;
796
797     for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
798     {
799         if (IsDotDir(wfd.cFileName)) continue;
800         if (*pdwListIndex >= flList->num_alloc) grow_list( flList );
801         szFullPath = wildcard_to_file(szFile, wfd.cFileName);
802         file = &flList->feFiles[(*pdwListIndex)++];
803         add_file_to_entry(file, szFullPath);
804         file->bFromWildcard = TRUE;
805         file->attributes = wfd.dwFileAttributes;
806         if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
807         HeapFree(GetProcessHeap(), 0, szFullPath);
808     }
809
810     FindClose(hFile);
811 }
812
813 /* takes the null-separated file list and fills out the FILE_LIST */
814 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
815 {
816     LPCWSTR ptr = szFiles;
817     WCHAR szCurFile[MAX_PATH];
818     DWORD i = 0, dwDirLen;
819
820     if (!szFiles)
821         return ERROR_INVALID_PARAMETER;
822
823     flList->bAnyFromWildcard = FALSE;
824     flList->bAnyDirectories = FALSE;
825     flList->bAnyDontExist = FALSE;
826     flList->num_alloc = 32;
827     flList->dwNumFiles = 0;
828
829     /* empty list */
830     if (!szFiles[0])
831         return ERROR_ACCESS_DENIED;
832         
833     flList->feFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
834                                 flList->num_alloc * sizeof(FILE_ENTRY));
835
836     while (*ptr)
837     {
838         if (i >= flList->num_alloc) grow_list( flList );
839
840         /* change relative to absolute path */
841         if (PathIsRelativeW(ptr))
842         {
843             dwDirLen = GetCurrentDirectoryW(MAX_PATH, szCurFile) + 1;
844             PathCombineW(szCurFile, szCurFile, ptr);
845             flList->feFiles[i].bFromRelative = TRUE;
846         }
847         else
848         {
849             strcpyW(szCurFile, ptr);
850             flList->feFiles[i].bFromRelative = FALSE;
851         }
852
853         /* parse wildcard files if they are in the filename */
854         if (StrPBrkW(szCurFile, wWildcardChars))
855         {
856             parse_wildcard_files(flList, szCurFile, &i);
857             flList->bAnyFromWildcard = TRUE;
858             i--;
859         }
860         else
861         {
862             FILE_ENTRY *file = &flList->feFiles[i];
863             add_file_to_entry(file, szCurFile);
864             file->attributes = GetFileAttributesW( file->szFullPath );
865             file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
866             if (!file->bExists) flList->bAnyDontExist = TRUE;
867             if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
868         }
869
870         /* advance to the next string */
871         ptr += strlenW(ptr) + 1;
872         i++;
873     }
874     flList->dwNumFiles = i;
875
876     return S_OK;
877 }
878
879 /* free the FILE_LIST */
880 static void destroy_file_list(FILE_LIST *flList)
881 {
882     DWORD i;
883
884     if (!flList || !flList->feFiles)
885         return;
886
887     for (i = 0; i < flList->dwNumFiles; i++)
888     {
889         HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
890         HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
891         HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
892     }
893
894     HeapFree(GetProcessHeap(), 0, flList->feFiles);
895 }
896
897 static void copy_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
898 {
899     WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
900     SHFILEOPSTRUCTW fileOp;
901
902     static const WCHAR wildCardFiles[] = {'*','.','*',0};
903
904     if (IsDotDir(feFrom->szFilename))
905         return;
906
907     if (PathFileExistsW(szDestPath))
908         PathCombineW(szTo, szDestPath, feFrom->szFilename);
909     else
910         strcpyW(szTo, szDestPath);
911
912     szTo[strlenW(szTo) + 1] = '\0';
913     SHNotifyCreateDirectoryW(szTo, NULL);
914
915     PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
916     szFrom[strlenW(szFrom) + 1] = '\0';
917
918     memcpy(&fileOp, lpFileOp, sizeof(fileOp));
919     fileOp.pFrom = szFrom;
920     fileOp.pTo = szTo;
921     fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
922
923     SHFileOperationW(&fileOp);
924 }
925
926 /* copy a file or directory to another directory */
927 static void copy_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
928 {
929     WCHAR szDestPath[MAX_PATH];
930
931     if (!PathFileExistsW(feTo->szFullPath))
932         SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
933
934     PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
935
936     if (IsAttribFile(feFrom->attributes))
937         SHNotifyCopyFileW(feFrom->szFullPath, szDestPath, FALSE);
938     else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
939         copy_dir_to_dir(lpFileOp, feFrom, szDestPath);
940 }
941
942 static void create_dest_dirs(LPWSTR szDestDir)
943 {
944     WCHAR dir[MAX_PATH];
945     LPWSTR ptr = StrChrW(szDestDir, '\\');
946
947     /* make sure all directories up to last one are created */
948     while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
949     {
950         lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
951
952         if (!PathFileExistsW(dir))
953             SHNotifyCreateDirectoryW(dir, NULL);
954     }
955
956     /* create last directory */
957     if (!PathFileExistsW(szDestDir))
958         SHNotifyCreateDirectoryW(szDestDir, NULL);
959 }
960
961 /* the FO_COPY operation */
962 static HRESULT copy_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
963 {
964     DWORD i;
965     FILE_ENTRY *entryToCopy;
966     FILE_ENTRY *fileDest = &flTo->feFiles[0];
967     BOOL bCancelIfAnyDirectories = FALSE;
968
969     if (flFrom->bAnyDontExist)
970         return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
971
972     if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->bAnyFromWildcard)
973         return ERROR_CANCELLED;
974
975     if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flTo->dwNumFiles != 1)
976         return ERROR_CANCELLED;
977
978     if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->dwNumFiles != 1 &&
979         flFrom->dwNumFiles != flTo->dwNumFiles)
980     {
981         return ERROR_CANCELLED;
982     }
983
984     if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 &&
985         !PathFileExistsW(flTo->feFiles[0].szFullPath) &&
986         IsAttribFile(fileDest->attributes))
987     {
988         bCancelIfAnyDirectories = TRUE;
989     }
990
991     if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
992         !PathFileExistsW(fileDest->szFullPath))
993     {
994         lpFileOp->fAnyOperationsAborted = TRUE;
995         return ERROR_CANCELLED;
996     }
997
998     if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flFrom->dwNumFiles != 1 &&
999         PathFileExistsW(fileDest->szFullPath) &&
1000         IsAttribFile(fileDest->attributes))
1001     {
1002         return ERROR_CANCELLED;
1003     }
1004
1005     for (i = 0; i < flFrom->dwNumFiles; i++)
1006     {
1007         entryToCopy = &flFrom->feFiles[i];
1008
1009         if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1010             fileDest = &flTo->feFiles[i];
1011
1012         if (IsAttribDir(entryToCopy->attributes) &&
1013             !lstrcmpW(entryToCopy->szFullPath, fileDest->szDirectory))
1014         {
1015             return ERROR_SUCCESS;
1016         }
1017
1018         if (IsAttribDir(entryToCopy->attributes) && bCancelIfAnyDirectories)
1019             return ERROR_CANCELLED;
1020
1021         create_dest_dirs(fileDest->szDirectory);
1022
1023         if (!strcmpW(entryToCopy->szFullPath, fileDest->szFullPath))
1024         {
1025             if (IsAttribFile(entryToCopy->attributes))
1026                 return ERROR_NO_MORE_SEARCH_HANDLES;
1027             else
1028                 return ERROR_SUCCESS;
1029         }
1030
1031         if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1032             (flFrom->dwNumFiles == 1 && IsAttribDir(fileDest->attributes)))
1033         {
1034             copy_to_dir(lpFileOp, entryToCopy, fileDest);
1035         }
1036         else if (IsAttribDir(entryToCopy->attributes))
1037         {
1038             copy_dir_to_dir(lpFileOp, entryToCopy, fileDest->szFullPath);
1039         }
1040         else
1041         {
1042             if (SHNotifyCopyFileW(entryToCopy->szFullPath, fileDest->szFullPath, TRUE))
1043             {
1044                 lpFileOp->fAnyOperationsAborted = TRUE;
1045                 return ERROR_CANCELLED;
1046             }
1047         }
1048     }
1049
1050     return ERROR_SUCCESS;
1051 }
1052
1053 static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, FILE_LIST *flFrom)
1054 {
1055     if (flFrom->dwNumFiles > 1)
1056     {
1057         WCHAR tmp[8];
1058         const WCHAR format[] = {'%','d',0};
1059
1060         wnsprintfW(tmp, sizeof(tmp)/sizeof(tmp[0]), format, flFrom->dwNumFiles);
1061         return SHELL_ConfirmDialogW(hWnd, ASK_DELETE_MULTIPLE_ITEM, tmp);
1062     }
1063     else
1064     {
1065         FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
1066
1067         if (IsAttribFile(fileEntry->attributes))
1068             return SHELL_ConfirmDialogW(hWnd, ASK_DELETE_FILE, fileEntry->szFullPath);
1069         else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1070             return SHELL_ConfirmDialogW(hWnd, ASK_DELETE_FOLDER, fileEntry->szFullPath);
1071     }
1072     return TRUE;
1073 }
1074
1075 /* the FO_DELETE operation */
1076 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom)
1077 {
1078     FILE_ENTRY *fileEntry;
1079     DWORD i;
1080     BOOL bPathExists;
1081
1082     if (!flFrom->dwNumFiles)
1083         return ERROR_SUCCESS;
1084
1085     if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1086         if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, flFrom))
1087         {
1088             lpFileOp->fAnyOperationsAborted = TRUE;
1089             return 0;
1090         }
1091
1092     for (i = 0; i < flFrom->dwNumFiles; i++)
1093     {
1094         bPathExists = TRUE;
1095         fileEntry = &flFrom->feFiles[i];
1096
1097         /* delete the file or directory */
1098         if (IsAttribFile(fileEntry->attributes))
1099             bPathExists = DeleteFileW(fileEntry->szFullPath);
1100         else if (!(lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1101             bPathExists = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1102
1103         if (!bPathExists)
1104             return ERROR_PATH_NOT_FOUND;
1105     }
1106
1107     return ERROR_SUCCESS;
1108 }
1109
1110 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
1111 {
1112     WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1113     SHFILEOPSTRUCTW fileOp;
1114
1115     static const WCHAR wildCardFiles[] = {'*','.','*',0};
1116
1117     if (IsDotDir(feFrom->szFilename))
1118         return;
1119
1120     SHNotifyCreateDirectoryW(szDestPath, NULL);
1121
1122     PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1123     szFrom[strlenW(szFrom) + 1] = '\0';
1124
1125     strcpyW(szTo, szDestPath);
1126     szTo[strlenW(szDestPath) + 1] = '\0';
1127
1128     memcpy(&fileOp, lpFileOp, sizeof(fileOp));
1129     fileOp.pFrom = szFrom;
1130     fileOp.pTo = szTo;
1131
1132     SHFileOperationW(&fileOp);
1133 }
1134
1135 /* moves a file or directory to another directory */
1136 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
1137 {
1138     WCHAR szDestPath[MAX_PATH];
1139
1140     PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1141
1142     if (IsAttribFile(feFrom->attributes))
1143         SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
1144     else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1145         move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1146 }
1147
1148 /* the FO_MOVE operation */
1149 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1150 {
1151     DWORD i;
1152     FILE_ENTRY *entryToMove;
1153     FILE_ENTRY *fileDest;
1154
1155     if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1156         return ERROR_CANCELLED;
1157
1158     if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1159         flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1160     {
1161         return ERROR_CANCELLED;
1162     }
1163
1164     if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1165         !flFrom->bAnyDirectories &&
1166         flFrom->dwNumFiles > flTo->dwNumFiles)
1167     {
1168         return ERROR_CANCELLED;
1169     }
1170
1171     if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1172         return ERROR_CANCELLED;
1173
1174     if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1175         flFrom->dwNumFiles != flTo->dwNumFiles)
1176     {
1177         return ERROR_CANCELLED;
1178     }
1179
1180     fileDest = &flTo->feFiles[0];
1181     for (i = 0; i < flFrom->dwNumFiles; i++)
1182     {
1183         entryToMove = &flFrom->feFiles[i];
1184
1185         if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1186             fileDest = &flTo->feFiles[i];
1187
1188         if (!PathFileExistsW(fileDest->szDirectory))
1189             return ERROR_CANCELLED;
1190
1191         if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1192             move_to_dir(lpFileOp, entryToMove, fileDest);
1193         else
1194             SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
1195     }
1196
1197     return ERROR_SUCCESS;
1198 }
1199
1200 /* the FO_RENAME files */
1201 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1202 {
1203     FILE_ENTRY *feFrom;
1204     FILE_ENTRY *feTo;
1205
1206     if (flFrom->dwNumFiles != 1)
1207         return ERROR_GEN_FAILURE;
1208
1209     if (flTo->dwNumFiles != 1)
1210         return ERROR_CANCELLED;
1211
1212     feFrom = &flFrom->feFiles[0];
1213     feTo= &flTo->feFiles[0];
1214
1215     /* fail if destination doesn't exist */
1216     if (!feFrom->bExists)
1217         return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1218
1219     /* fail if destination already exists */
1220     if (feTo->bExists)
1221         return ERROR_ALREADY_EXISTS;
1222
1223     return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
1224 }
1225
1226 /* alert the user if an unsupported flag is used */
1227 static void check_flags(FILEOP_FLAGS fFlags)
1228 {
1229     WORD wUnsupportedFlags = FOF_ALLOWUNDO | FOF_NO_CONNECTED_ELEMENTS |
1230         FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1231         FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1232
1233     if (fFlags & wUnsupportedFlags)
1234         FIXME("Unsupported flags: %04x\n", fFlags);
1235 }
1236
1237 /*************************************************************************
1238  * SHFileOperationW          [SHELL32.@]
1239  *
1240  * See SHFileOperationA
1241  */
1242 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1243 {
1244     FILE_LIST flFrom, flTo;
1245     int ret = 0;
1246
1247     if (!lpFileOp)
1248         return ERROR_INVALID_PARAMETER;
1249
1250     check_flags(lpFileOp->fFlags);
1251
1252     ZeroMemory(&flFrom, sizeof(FILE_LIST));
1253     ZeroMemory(&flTo, sizeof(FILE_LIST));
1254
1255     if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1256         return ret;
1257
1258     if (lpFileOp->wFunc != FO_DELETE)
1259         parse_file_list(&flTo, lpFileOp->pTo);
1260
1261     switch (lpFileOp->wFunc)
1262     {
1263         case FO_COPY:
1264             ret = copy_files(lpFileOp, &flFrom, &flTo);
1265             break;
1266         case FO_DELETE:
1267             ret = delete_files(lpFileOp, &flFrom);
1268             break;
1269         case FO_MOVE:
1270             ret = move_files(lpFileOp, &flFrom, &flTo);
1271             break;
1272         case FO_RENAME:
1273             ret = rename_files(lpFileOp, &flFrom, &flTo);
1274             break;
1275         default:
1276             ret = ERROR_INVALID_PARAMETER;
1277             break;
1278     }
1279
1280     destroy_file_list(&flFrom);
1281
1282     if (lpFileOp->wFunc != FO_DELETE)
1283         destroy_file_list(&flTo);
1284
1285     if (ret == ERROR_CANCELLED)
1286         lpFileOp->fAnyOperationsAborted = TRUE;
1287     
1288     return ret;
1289 }
1290
1291 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1292
1293 /*************************************************************************
1294  * SHFreeNameMappings      [shell32.246]
1295  *
1296  * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1297  * was specified.
1298  *
1299  * PARAMS
1300  *  hNameMapping [I] handle to the name mappings used during renaming of files
1301  *
1302  * RETURNS
1303  *  Nothing
1304  */
1305 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1306 {
1307         if (hNameMapping)
1308         {
1309           int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1310
1311           for (; i>= 0; i--)
1312           {
1313             LPSHNAMEMAPPINGW lp = DSA_GetItemPtr((HDSA)hNameMapping, i);
1314
1315             SHFree(lp->pszOldPath);
1316             SHFree(lp->pszNewPath);
1317           }
1318           DSA_Destroy((HDSA)hNameMapping);
1319         }
1320 }
1321
1322 /*************************************************************************
1323  * SheGetDirA [SHELL32.@]
1324  *
1325  */
1326 HRESULT WINAPI SheGetDirA(LPSTR u, LPSTR v)
1327 {   FIXME("%p %p stub\n",u,v);
1328     return 0;
1329 }
1330
1331 /*************************************************************************
1332  * SheGetDirW [SHELL32.@]
1333  *
1334  */
1335 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1336 {       FIXME("%p %p stub\n",u,v);
1337         return 0;
1338 }
1339
1340 /*************************************************************************
1341  * SheChangeDirA [SHELL32.@]
1342  *
1343  */
1344 HRESULT WINAPI SheChangeDirA(LPSTR u)
1345 {   FIXME("(%s),stub\n",debugstr_a(u));
1346     return 0;
1347 }
1348
1349 /*************************************************************************
1350  * SheChangeDirW [SHELL32.@]
1351  *
1352  */
1353 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1354 {       FIXME("(%s),stub\n",debugstr_w(u));
1355         return 0;
1356 }
1357
1358 /*************************************************************************
1359  * IsNetDrive                   [SHELL32.66]
1360  */
1361 BOOL WINAPI IsNetDrive(DWORD drive)
1362 {
1363         char root[4];
1364         strcpy(root, "A:\\");
1365         root[0] += (char)drive;
1366         return (GetDriveTypeA(root) == DRIVE_REMOTE);
1367 }
1368
1369
1370 /*************************************************************************
1371  * RealDriveType                [SHELL32.524]
1372  */
1373 INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1374 {
1375     char root[] = "A:\\";
1376     root[0] += (char)drive;
1377     return GetDriveTypeA(root);
1378 }