Removed a couple of no longer needed entries.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 static const WCHAR wBackslash[] = {'\\',0};
58
59 static BOOL SHELL_DeleteDirectoryW(LPCWSTR path, BOOL bShowUI);
60 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec);
61 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec);
62 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path);
63 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path);
64 static DWORD SHNotifyDeleteFileA(LPCSTR path);
65 static DWORD SHNotifyDeleteFileW(LPCWSTR path);
66 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest);
67 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists);
68 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly);
69
70 typedef struct
71 {
72         UINT caption_resource_id, text_resource_id;
73 } SHELL_ConfirmIDstruc;
74
75 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
76 {
77         switch (nKindOfDialog) {
78           case ASK_DELETE_FILE:
79             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
80             ids->text_resource_id  = IDS_DELETEITEM_TEXT;
81             return TRUE;
82           case ASK_DELETE_FOLDER:
83             ids->caption_resource_id  = IDS_DELETEFOLDER_CAPTION;
84             ids->text_resource_id  = IDS_DELETEITEM_TEXT;
85             return TRUE;
86           case ASK_DELETE_MULTIPLE_ITEM:
87             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
88             ids->text_resource_id  = IDS_DELETEMULTIPLE_TEXT;
89             return TRUE;
90           case ASK_OVERWRITE_FILE:
91             ids->caption_resource_id  = IDS_OVERWRITEFILE_CAPTION;
92             ids->text_resource_id  = IDS_OVERWRITEFILE_TEXT;
93             return TRUE;
94           default:
95             FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
96         }
97         return FALSE;
98 }
99
100 BOOL SHELL_ConfirmDialog(int nKindOfDialog, LPCSTR szDir)
101 {
102         CHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
103         SHELL_ConfirmIDstruc ids;
104
105         if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
106           return FALSE;
107
108         LoadStringA(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption));
109         LoadStringA(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText));
110
111         FormatMessageA(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
112                        szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
113
114         return (IDOK == MessageBoxA(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
115 }
116
117 BOOL SHELL_ConfirmDialogW(int nKindOfDialog, LPCWSTR szDir)
118 {
119         WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
120         SHELL_ConfirmIDstruc ids;
121
122         if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
123           return FALSE;
124
125         LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption));
126         LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText));
127
128         FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
129                        szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
130
131         return (IDOK == MessageBoxW(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
132 }
133
134 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
135 {
136         DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
137
138         if (len < minChars)
139           len = minChars;
140
141         *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
142         if (*wPath)
143         {
144           MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
145           return NO_ERROR;
146         }
147         return E_OUTOFMEMORY;
148 }
149
150 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
151 {
152         HeapFree(GetProcessHeap(), 0, wPath);
153 }
154
155 /**************************************************************************
156  * SHELL_DeleteDirectory()  [internal]
157  *
158  * Asks for confirmation when bShowUI is true and deletes the directory and
159  * all its subdirectories and files if necessary.
160  */
161 BOOL SHELL_DeleteDirectoryA(LPCSTR pszDir, BOOL bShowUI)
162 {
163         LPWSTR wPath;
164         BOOL ret = FALSE;
165
166         if (!SHELL32_AnsiToUnicodeBuf(pszDir, &wPath, 0))
167         {
168           ret = SHELL_DeleteDirectoryW(wPath, bShowUI);
169           SHELL32_FreeUnicodeBuf(wPath);
170         }
171         return ret;
172 }
173
174 static BOOL SHELL_DeleteDirectoryW(LPCWSTR pszDir, BOOL bShowUI)
175 {
176         BOOL    ret = TRUE;
177         HANDLE  hFind;
178         WIN32_FIND_DATAW wfd;
179         WCHAR   szTemp[MAX_PATH];
180
181         /* Make sure the directory exists before eventually prompting the user */
182         PathCombineW(szTemp, pszDir, wWildcardFile);
183         hFind = FindFirstFileW(szTemp, &wfd);
184         if (hFind == INVALID_HANDLE_VALUE)
185           return FALSE;
186
187         if (!bShowUI || (ret = SHELL_ConfirmDialogW(ASK_DELETE_FOLDER, pszDir)))
188         {
189           do
190           {
191             LPWSTR lp = wfd.cAlternateFileName;
192             if (!lp[0])
193               lp = wfd.cFileName;
194             if (IsDotDir(lp))
195               continue;
196             PathCombineW(szTemp, pszDir, lp);
197             if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
198               ret = SHELL_DeleteDirectoryW(szTemp, FALSE);
199             else
200               ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
201           } while (ret && FindNextFileW(hFind, &wfd));
202         }
203         FindClose(hFind);
204         if (ret)
205           ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
206         return ret;
207 }
208
209 /**************************************************************************
210  *  SHELL_DeleteFileA()      [internal]
211  */
212 BOOL SHELL_DeleteFileA(LPCSTR pszFile, BOOL bShowUI)
213 {
214         if (bShowUI && !SHELL_ConfirmDialog(ASK_DELETE_FILE, pszFile))
215           return FALSE;
216
217         return (SHNotifyDeleteFileA(pszFile) == ERROR_SUCCESS);
218 }
219
220 BOOL SHELL_DeleteFileW(LPCWSTR pszFile, BOOL bShowUI)
221 {
222         if (bShowUI && !SHELL_ConfirmDialogW(ASK_DELETE_FILE, pszFile))
223           return FALSE;
224
225         return (SHNotifyDeleteFileW(pszFile) == ERROR_SUCCESS);
226 }
227
228 /**************************************************************************
229  * Win32CreateDirectory      [SHELL32.93]
230  *
231  * Creates a directory. Also triggers a change notify if one exists.
232  *
233  * PARAMS
234  *  path       [I]   path to directory to create
235  *
236  * RETURNS
237  *  TRUE if successful, FALSE otherwise
238  *
239  * NOTES:
240  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
241  *  This is Unicode on NT/2000
242  */
243 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
244 {
245         LPWSTR wPath;
246         DWORD retCode;
247
248         TRACE("(%s, %p)\n", debugstr_a(path), sec);
249
250         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
251         if (!retCode)
252         {
253           retCode = SHNotifyCreateDirectoryW(wPath, sec);
254           SHELL32_FreeUnicodeBuf(wPath);
255         }
256         return retCode;
257 }
258
259 /**********************************************************************/
260
261 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
262 {
263         TRACE("(%s, %p)\n", debugstr_w(path), sec);
264
265         if (CreateDirectoryW(path, sec))
266         {
267           SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
268           return ERROR_SUCCESS;
269         }
270         return GetLastError();
271 }
272
273 /**********************************************************************/
274
275 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
276 {
277         if (SHELL_OsIsUnicode())
278           return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
279         return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
280 }
281
282 /************************************************************************
283  * Win32RemoveDirectory      [SHELL32.94]
284  *
285  * Deletes a directory. Also triggers a change notify if one exists.
286  *
287  * PARAMS
288  *  path       [I]   path to directory to delete
289  *
290  * RETURNS
291  *  TRUE if successful, FALSE otherwise
292  *
293  * NOTES:
294  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
295  *  This is Unicode on NT/2000
296  */
297 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
298 {
299         LPWSTR wPath;
300         DWORD retCode;
301
302         TRACE("(%s)\n", debugstr_a(path));
303
304         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
305         if (!retCode)
306         {
307           retCode = SHNotifyRemoveDirectoryW(wPath);
308           SHELL32_FreeUnicodeBuf(wPath);
309         }
310         return retCode;
311 }
312
313 /***********************************************************************/
314
315 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
316 {
317         BOOL ret;
318         TRACE("(%s)\n", debugstr_w(path));
319
320         ret = RemoveDirectoryW(path);
321         if (!ret)
322         {
323           /* Directory may be write protected */
324           DWORD dwAttr = GetFileAttributesW(path);
325           if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
326             if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
327               ret = RemoveDirectoryW(path);
328         }
329         if (ret)
330         {
331           SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
332           return ERROR_SUCCESS;
333         }
334         return GetLastError();
335 }
336
337 /***********************************************************************/
338
339 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
340 {
341         if (SHELL_OsIsUnicode())
342           return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
343         return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
344 }
345
346 /************************************************************************
347  * Win32DeleteFile           [SHELL32.164]
348  *
349  * Deletes a file. Also triggers a change notify if one exists.
350  *
351  * PARAMS
352  *  path       [I]   path to file to delete
353  *
354  * RETURNS
355  *  TRUE if successful, FALSE otherwise
356  *
357  * NOTES:
358  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
359  *  This is Unicode on NT/2000
360  */
361 static DWORD SHNotifyDeleteFileA(LPCSTR path)
362 {
363         LPWSTR wPath;
364         DWORD retCode;
365
366         TRACE("(%s)\n", debugstr_a(path));
367
368         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
369         if (!retCode)
370         {
371           retCode = SHNotifyDeleteFileW(wPath);
372           SHELL32_FreeUnicodeBuf(wPath);
373         }
374         return retCode;
375 }
376
377 /***********************************************************************/
378
379 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
380 {
381         BOOL ret;
382
383         TRACE("(%s)\n", debugstr_w(path));
384
385         ret = DeleteFileW(path);
386         if (!ret)
387         {
388           /* File may be write protected or a system file */
389           DWORD dwAttr = GetFileAttributesW(path);
390           if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
391             if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
392               ret = DeleteFileW(path);
393         }
394         if (ret)
395         {
396           SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
397           return ERROR_SUCCESS;
398         }
399         return GetLastError();
400 }
401
402 /***********************************************************************/
403
404 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
405 {
406         if (SHELL_OsIsUnicode())
407           return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
408         return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
409 }
410
411 /************************************************************************
412  * SHNotifyMoveFile          [internal]
413  *
414  * Moves a file. Also triggers a change notify if one exists.
415  *
416  * PARAMS
417  *  src        [I]   path to source file to move
418  *  dest       [I]   path to target file to move to
419  *
420  * RETURNS
421  *  ERORR_SUCCESS if successful
422  */
423 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
424 {
425         BOOL ret;
426
427         TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
428
429         ret = MoveFileW(src, dest);
430         if (!ret)
431         {
432           DWORD dwAttr;
433
434           dwAttr = SHFindAttrW(dest, FALSE);
435           if (INVALID_FILE_ATTRIBUTES == dwAttr)
436           {
437             /* Source file may be write protected or a system file */
438             dwAttr = GetFileAttributesW(src);
439             if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
440               if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
441                 ret = MoveFileW(src, dest);
442           }
443         }
444         if (ret)
445         {
446           SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
447           return ERROR_SUCCESS;
448         }
449         return GetLastError();
450 }
451
452 /************************************************************************
453  * SHNotifyCopyFile          [internal]
454  *
455  * Copies a file. Also triggers a change notify if one exists.
456  *
457  * PARAMS
458  *  src           [I]   path to source file to move
459  *  dest          [I]   path to target file to move to
460  *  bFailIfExists [I]   if TRUE, the target file will not be overwritten if
461  *                      a file with this name already exists
462  *
463  * RETURNS
464  *  ERROR_SUCCESS if successful
465  */
466 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
467 {
468         BOOL ret;
469
470         TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
471
472         ret = CopyFileW(src, dest, bFailIfExists);
473         if (ret)
474         {
475           SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
476           return ERROR_SUCCESS;
477         }
478         return GetLastError();
479 }
480
481 /*************************************************************************
482  * SHCreateDirectory         [SHELL32.165]
483  *
484  * This function creates a file system folder whose fully qualified path is
485  * given by path. If one or more of the intermediate folders do not exist,
486  * they will be created as well.
487  *
488  * PARAMS
489  *  hWnd       [I]
490  *  path       [I]   path of directory to create
491  *
492  * RETURNS
493  *  ERRROR_SUCCESS or one of the following values:
494  *  ERROR_BAD_PATHNAME if the path is relative
495  *  ERROR_FILE_EXISTS when a file with that name exists
496  *  ERROR_PATH_NOT_FOUND can't find the path, probably invalid
497  *  ERROR_INVLID_NAME if the path contains invalid chars
498  *  ERROR_ALREADY_EXISTS when the directory already exists
499  *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
500  *
501  * NOTES
502  *  exported by ordinal
503  *  Win9x exports ANSI
504  *  WinNT/2000 exports Unicode
505  */
506 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
507 {
508         if (SHELL_OsIsUnicode())
509           return SHCreateDirectoryExW(hWnd, path, NULL);
510         return SHCreateDirectoryExA(hWnd, path, NULL);
511 }
512
513 /*************************************************************************
514  * SHCreateDirectoryExA      [SHELL32.@]
515  *
516  * This function creates a file system folder whose fully qualified path is
517  * given by path. If one or more of the intermediate folders do not exist,
518  * they will be created as well.
519  *
520  * PARAMS
521  *  hWnd       [I]
522  *  path       [I]   path of directory to create
523  *  sec        [I]   security attributes to use or NULL
524  *
525  * RETURNS
526  *  ERRROR_SUCCESS or one of the following values:
527  *  ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
528  *  ERROR_INVLID_NAME if the path contains invalid chars
529  *  ERROR_FILE_EXISTS when a file with that name exists
530  *  ERROR_ALREADY_EXISTS when the directory already exists
531  *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
532  *
533  *  FIXME: Not implemented yet;
534  *  SHCreateDirectoryEx also verifies that the files in the directory will be visible
535  *  if the path is a network path to deal with network drivers which might have a limited
536  *  but unknown maximum path length. If not:
537  *
538  *  If hWnd is set to a valid window handle, a message box is displayed warning
539  *  the user that the files may not be accessible. If the user chooses not to
540  *  proceed, the function returns ERROR_CANCELLED.
541  *
542  *  If hWnd is set to NULL, no user interface is displayed and the function
543  *  returns ERROR_CANCELLED.
544  */
545 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
546 {
547         LPWSTR wPath;
548         DWORD retCode;
549
550         TRACE("(%s, %p)\n", debugstr_a(path), sec);
551
552         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
553         if (!retCode)
554         {
555           retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
556           SHELL32_FreeUnicodeBuf(wPath);
557         }
558         return retCode;
559 }
560
561 /*************************************************************************
562  * SHCreateDirectoryExW      [SHELL32.@]
563  */
564 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
565 {
566         int ret = ERROR_BAD_PATHNAME;
567         TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
568
569         if (PathIsRelativeW(path))
570         {
571           SetLastError(ret);
572         }
573         else
574         {
575           ret = SHNotifyCreateDirectoryW(path, sec);
576           /* Refuse to work on certain error codes before trying to create directories recursively */
577           if (ret != ERROR_FILE_EXISTS &&
578               ret != ERROR_ALREADY_EXISTS &&
579               ret != ERROR_FILENAME_EXCED_RANGE)
580           {
581             WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1];  /* extra for PathAddBackslash() */
582
583             lstrcpynW(szTemp, path, MAX_PATH);
584             pEnd = PathAddBackslashW(szTemp);
585             pSlash = szTemp + 3;
586
587             while (*pSlash)
588             {
589               while (*pSlash && *pSlash != '\\')
590                 pSlash = CharNextW(pSlash);
591
592               if (*pSlash)
593               {
594                 *pSlash = 0;    /* terminate path at separator */
595
596                 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
597               }
598               *pSlash++ = '\\'; /* put the separator back */
599             }
600           }
601
602           if (ret && hWnd && (ERROR_CANCELLED != ret))
603           {
604             /* We failed and should show a dialog box */
605             FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
606             ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
607           }
608         }
609         return ret;
610 }
611
612
613 /*************************************************************************
614  * SHFindAttrW      [internal]
615  *
616  * Get the Attributes for a file or directory. The difference to GetAttributes()
617  * is that this function will also work for paths containing wildcard characters
618  * in its filename.
619
620  * PARAMS
621  *  path       [I]   path of directory or file to check
622  *  fileOnly   [I]   TRUE if only files should be found
623  *
624  * RETURNS
625  *  INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
626  *  the first file or directory found otherwise
627  */
628 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
629 {
630         WIN32_FIND_DATAW wfd;
631         BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
632         DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
633         HANDLE hFind = FindFirstFileW(pName, &wfd);
634
635         TRACE("%s %d\n", debugstr_w(pName), fileOnly);
636         if (INVALID_HANDLE_VALUE != hFind)
637         {
638           do
639           {
640             if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
641                continue;
642             dwAttr = wfd.dwFileAttributes;
643             break;
644           }
645           while (FindNextFileW(hFind, &wfd));
646           FindClose(hFind);
647         }
648         return dwAttr;
649 }
650
651 /*************************************************************************
652  *
653  * SHFileStrICmp HelperFunction for SHFileOperationW
654  *
655  */
656 BOOL SHFileStrICmpW(LPWSTR p1, LPWSTR p2, LPWSTR p1End, LPWSTR p2End)
657 {
658         WCHAR C1 = '\0';
659         WCHAR C2 = '\0';
660         int i_Temp = -1;
661         int i_len1 = lstrlenW(p1);
662         int i_len2 = lstrlenW(p2);
663
664         if (p1End && (&p1[i_len1] >= p1End) && ('\\' == p1End[0]))
665         {
666           C1 = p1End[0];
667           p1End[0] = '\0';
668           i_len1 = lstrlenW(p1);
669         }
670         if (p2End)
671         {
672           if ((&p2[i_len2] >= p2End) && ('\\' == p2End[0]))
673           {
674             C2 = p2End[0];
675             if (C2)
676               p2End[0] = '\0';
677           }
678         }
679         else
680         {
681           if ((i_len1 <= i_len2) && ('\\' == p2[i_len1]))
682           {
683             C2 = p2[i_len1];
684             if (C2)
685               p2[i_len1] = '\0';
686           }
687         }
688         i_len2 = lstrlenW(p2);
689         if (i_len1 == i_len2)
690           i_Temp = lstrcmpiW(p1,p2);
691         if (C1)
692           p1[i_len1] = C1;
693         if (C2)
694           p2[i_len2] = C2;
695         return !(i_Temp);
696 }
697
698 /*************************************************************************
699  *
700  * SHFileStrCpyCat HelperFunction for SHFileOperationW
701  *
702  */
703 LPWSTR SHFileStrCpyCatW(LPWSTR pTo, LPCWSTR pFrom, LPCWSTR pCatStr)
704 {
705         LPWSTR pToFile = NULL;
706         int  i_len;
707         if (pTo)
708         {
709           if (pFrom)
710             lstrcpyW(pTo, pFrom);
711           if (pCatStr)
712           {
713             i_len = lstrlenW(pTo);
714             if ((i_len) && (pTo[--i_len] != '\\'))
715               i_len++;
716             pTo[i_len] = '\\';
717             if (pCatStr[0] == '\\')
718               pCatStr++; \
719             lstrcpyW(&pTo[i_len+1], pCatStr);
720           }
721           pToFile = StrRChrW(pTo,NULL,'\\');
722           /* termination of the new string-group */
723           pTo[(lstrlenW(pTo)) + 1] = '\0';
724         }
725         return pToFile;
726 }
727
728 /**************************************************************************
729  *      SHELL_FileNamesMatch()
730  *
731  * Accepts two \0 delimited lists of the file names. Checks whether number of
732  * files in both lists is the same, and checks also if source-name exists.
733  */
734 BOOL SHELL_FileNamesMatch(LPCWSTR pszFiles1, LPCWSTR pszFiles2, BOOL bOnlySrc)
735 {
736         while ((pszFiles1[0] != '\0') &&
737                (bOnlySrc || (pszFiles2[0] != '\0')))
738         {
739           if (NULL == StrPBrkW(pszFiles1, wWildcardChars))
740           {
741             if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(pszFiles1))
742               return FALSE;
743           }
744           pszFiles1 += lstrlenW(pszFiles1) + 1;
745           if (!bOnlySrc)
746             pszFiles2 += lstrlenW(pszFiles2) + 1;
747         }
748         return ((pszFiles1[0] == '\0') && (bOnlySrc || (pszFiles2[0] == '\0')));
749 }
750
751 /*************************************************************************
752  *
753  * SHNameTranslate HelperFunction for SHFileOperationA
754  *
755  * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
756  * is NULL, only the necessary size of the string is determined and returned,
757  * otherwise the ASCII strings are copied into it and the buffer is increased
758  * to point to the location after the final 0 termination char.
759  */
760 DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
761 {
762         DWORD size = 0, aSize = 0;
763         LPCSTR aString = (LPCSTR)*pWToFrom;
764
765         if (aString)
766         {
767           do
768           {
769             size = lstrlenA(aString) + 1;
770             aSize += size;
771             aString += size;
772           } while ((size != 1) && more);
773           /* The two sizes might be different in the case of multibyte chars */
774           size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
775           if (*wString) /* only in the second loop */
776           {
777             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
778             *pWToFrom = *wString;
779             *wString += size;
780           }
781         }
782         return size;
783 }
784 /*************************************************************************
785  * SHFileOperationA          [SHELL32.@]
786  *
787  * Function to copy, move, delete and create one or more files with optional
788  * user prompts.
789  *
790  * PARAMS
791  *  lpFileOp   [I/O] pointer to a structure containing all the necessary information
792  *
793  * NOTES
794  *  exported by name
795  */
796 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
797 {
798         SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
799         int retCode = 0;
800         DWORD size;
801         LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
802                wString = NULL; /* we change this in SHNameTranslate */
803
804         TRACE("\n");
805         if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
806           nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
807         if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
808           nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
809         while (1) /* every loop calculate size, second translate also, if we have storage for this */
810         {
811           size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
812           size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
813           size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
814
815           if (ForFree)
816           {
817             retCode = SHFileOperationW(&nFileOp);
818             HeapFree(GetProcessHeap(), 0, ForFree); /* we can not use wString, it was changed */
819             break;
820           }
821           else
822           {
823             wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
824             if (ForFree) continue;
825             retCode = ERROR_OUTOFMEMORY;
826             nFileOp.fAnyOperationsAborted = TRUE;
827             SetLastError(retCode);
828             return retCode;
829           }
830         }
831
832         lpFileOp->hNameMappings = nFileOp.hNameMappings;
833         lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
834         return retCode;
835 }
836
837 static const char * debug_shfileops_flags( DWORD fFlags )
838 {
839     return wine_dbg_sprintf( "%s%s%s%s%s%s%s%s%s%s%s%s%s",
840         fFlags & FOF_MULTIDESTFILES ? "FOF_MULTIDESTFILES " : "",
841         fFlags & FOF_CONFIRMMOUSE ? "FOF_CONFIRMMOUSE " : "",
842         fFlags & FOF_SILENT ? "FOF_SILENT " : "",
843         fFlags & FOF_RENAMEONCOLLISION ? "FOF_RENAMEONCOLLISION " : "",
844         fFlags & FOF_NOCONFIRMATION ? "FOF_NOCONFIRMATION " : "",
845         fFlags & FOF_WANTMAPPINGHANDLE ? "FOF_WANTMAPPINGHANDLE " : "",
846         fFlags & FOF_ALLOWUNDO ? "FOF_ALLOWUNDO " : "",
847         fFlags & FOF_FILESONLY ? "FOF_FILESONLY " : "",
848         fFlags & FOF_SIMPLEPROGRESS ? "FOF_SIMPLEPROGRESS " : "",
849         fFlags & FOF_NOCONFIRMMKDIR ? "FOF_NOCONFIRMMKDIR " : "",
850         fFlags & FOF_NOERRORUI ? "FOF_NOERRORUI " : "",
851         fFlags & FOF_NOCOPYSECURITYATTRIBS ? "FOF_NOCOPYSECURITYATTRIBS" : "",
852         fFlags & 0xf000 ? "MORE-UNKNOWN-Flags" : "");
853 }
854
855 static const char * debug_shfileops_action( DWORD op )
856 {
857     LPCSTR cFO_Name [] = {"FO_????","FO_MOVE","FO_COPY","FO_DELETE","FO_RENAME"};
858     return wine_dbg_sprintf("%s", cFO_Name[ op ]);
859 }
860
861 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
862 #define HIGH_ADR (LPWSTR)0xffffffff
863
864 /*************************************************************************
865  * SHFileOperationW          [SHELL32.@]
866  *
867  * See SHFileOperationA
868  */
869 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
870 {
871         SHFILEOPSTRUCTW nFileOp = *(lpFileOp);
872
873         LPCWSTR pNextFrom = nFileOp.pFrom;
874         LPCWSTR pNextTo = nFileOp.pTo;
875         LPCWSTR pFrom = pNextFrom;
876         LPCWSTR pTo = NULL;
877         HANDLE hFind = INVALID_HANDLE_VALUE;
878         WIN32_FIND_DATAW wfd;
879         LPWSTR pTempFrom = NULL;
880         LPWSTR pTempTo = NULL;
881         LPWSTR pFromFile;
882         LPWSTR pToFile = NULL;
883         LPWSTR lpFileName;
884         int retCode = 0;
885         DWORD ToAttr;
886         DWORD ToPathAttr;
887         DWORD FromPathAttr;
888         FILEOP_FLAGS OFl = ((FILEOP_FLAGS)lpFileOp->fFlags & 0xfff);
889
890         BOOL b_Multi = (nFileOp.fFlags & FOF_MULTIDESTFILES);
891
892         BOOL b_MultiTo = (FO_DELETE != (lpFileOp->wFunc & FO_MASK));
893         BOOL b_MultiPaired = (!b_MultiTo);
894         BOOL b_MultiFrom = FALSE;
895         BOOL not_overwrite;
896         BOOL ask_overwrite;
897         BOOL b_SameRoot;
898         BOOL b_SameTailName;
899         BOOL b_ToInvalidTail = FALSE;
900         BOOL b_ToValid; /* for W98-Bug for FO_MOVE with source and target in same rootdrive */
901         BOOL b_Mask;
902         BOOL b_ToTailSlash = FALSE;
903
904         long FuncSwitch = (nFileOp.wFunc & FO_MASK);
905         long level= nFileOp.wFunc>>4;
906
907         /*  default no error */
908         nFileOp.fAnyOperationsAborted = FALSE;
909
910         if ((FuncSwitch < FO_MOVE) || (FuncSwitch > FO_RENAME))
911             goto shfileop_end; /* no valid FunctionCode */
912
913         if (level == 0)
914             TRACE("%s: flags (0x%04x) : %s\n",
915                 debug_shfileops_action(FuncSwitch), nFileOp.fFlags,
916                 debug_shfileops_flags(nFileOp.fFlags) );
917
918         /* establish when pTo is interpreted as the name of the destination file
919          * or the directory where the Fromfile should be copied to.
920          * This depends on:
921          * (1) pTo points to the name of an existing directory;
922          * (2) the flag FOF_MULTIDESTFILES is present;
923          * (3) whether pFrom point to multiple filenames.
924          *
925          * Some experiments:
926          *
927          * destisdir               1 1 1 1 0 0 0 0
928          * FOF_MULTIDESTFILES      1 1 0 0 1 1 0 0
929          * multiple from filenames 1 0 1 0 1 0 1 0
930          *                         ---------------
931          * copy files to dir       1 0 1 1 0 0 1 0
932          * create dir              0 0 0 0 0 0 1 0
933          */
934
935         /*
936          * Summary of flags:
937          *
938          * implemented flags:
939          * FOF_MULTIDESTFILES, FOF_NOCONFIRMATION, FOF_FILESONLY
940          *
941          * unimplememented and ignored flags:
942          * FOF_CONFIRMMOUSE, FOF_SILENT, FOF_NOCONFIRMMKDIR,
943          *       FOF_SIMPLEPROGRESS, FOF_NOCOPYSECURITYATTRIBS
944          *
945          * partially implemented, breaks if file exists:
946          * FOF_RENAMEONCOLLISION
947          *
948          * unimplemented and break if any other flag set:
949          * FOF_ALLOWUNDO, FOF_WANTMAPPINGHANDLE
950          */
951
952         TRACE("%s level=%ld nFileOp.fFlags=0x%x\n", 
953                 debug_shfileops_action(FuncSwitch), level, lpFileOp->fFlags);
954
955         /*    OFl &= (-1 - (FOF_MULTIDESTFILES | FOF_FILESONLY)); */
956         /*    OFl ^= (FOF_SILENT | FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS | FOF_NOCONFIRMMKDIR); */
957         OFl &= (~(FOF_MULTIDESTFILES | FOF_NOCONFIRMATION | FOF_FILESONLY));  /* implemented */
958         OFl ^= (FOF_SILENT | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_NOCOPYSECURITYATTRIBS); /* ignored, if one */
959         OFl &= (~FOF_SIMPLEPROGRESS);                      /* ignored, only with FOF_SILENT */
960         if (OFl)
961         {
962             if (OFl & (~(FOF_CONFIRMMOUSE | FOF_SILENT | FOF_RENAMEONCOLLISION |
963                          FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_NOCOPYSECURITYATTRIBS)))
964             {
965                 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not implemented, Aborted=TRUE, stub\n",
966                       debug_shfileops_action(FuncSwitch), level, OFl);
967                 retCode = 0x403; /* 1027, we need an extension to shlfileop */
968                 goto shfileop_end;
969             }
970             else
971             {
972                 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not fully implemented, stub\n", 
973                       debug_shfileops_action(FuncSwitch), level, OFl);
974             } 
975         } 
976
977         if ((pNextFrom) && (!(b_MultiTo) || (pNextTo)))
978         {
979             nFileOp.pFrom = pTempFrom = HeapAlloc(GetProcessHeap(), 0, ((1 + 2 * (b_MultiTo)) * MAX_PATH + 6) * sizeof(WCHAR));
980             if (!pTempFrom)
981             {
982                 retCode = ERROR_OUTOFMEMORY;
983                 SetLastError(retCode);
984                 goto shfileop_end;
985             }
986             if (b_MultiTo)
987                 pTempTo = &pTempFrom[MAX_PATH + 4];
988             nFileOp.pTo = pTempTo;
989             ask_overwrite = (!(nFileOp.fFlags & FOF_NOCONFIRMATION) && !(nFileOp.fFlags & FOF_RENAMEONCOLLISION));
990             not_overwrite = (!(nFileOp.fFlags & FOF_NOCONFIRMATION) ||  (nFileOp.fFlags & FOF_RENAMEONCOLLISION));
991         }
992         else
993         {
994             retCode = ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
995             goto shfileop_end;
996         }
997         /* need break at error before change sourcepointer */
998         while(!nFileOp.fAnyOperationsAborted && (pNextFrom[0]))
999         {
1000             nFileOp.wFunc =  ((level + 1) << 4) + FuncSwitch;
1001             nFileOp.fFlags = lpFileOp->fFlags;
1002
1003             if (b_MultiTo)
1004             {
1005                 pTo = pNextTo;
1006                 pNextTo = &pNextTo[lstrlenW(pTo)+1];
1007                 b_MultiTo = (b_Multi && pNextTo[0]);
1008             }
1009
1010             pFrom = pNextFrom;
1011             pNextFrom = &pNextFrom[lstrlenW(pNextFrom)+1];
1012             if (!b_MultiFrom && !b_MultiTo)
1013                 b_MultiFrom = (pNextFrom[0]);
1014
1015             pFromFile = SHFileStrCpyCatW(pTempFrom, pFrom, NULL);
1016
1017             if (pTo)
1018             {
1019                 pToFile = SHFileStrCpyCatW(pTempTo, pTo, NULL);
1020             }
1021             if (!b_MultiPaired)
1022             {
1023                 b_MultiPaired =
1024                     SHELL_FileNamesMatch(lpFileOp->pFrom, lpFileOp->pTo, (!b_Multi || b_MultiFrom));
1025             }
1026             if (!(b_MultiPaired) || !(pFromFile) || !(pFromFile[1]) || ((pTo) && !(pToFile)))
1027             {
1028                 retCode = ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1029                 goto shfileop_end;
1030             }
1031             if (pTo)
1032             {
1033                 b_ToTailSlash = (!pToFile[1]);
1034                 if (b_ToTailSlash)
1035                 {
1036                     pToFile[0] = '\0';
1037                     if (StrChrW(pTempTo,'\\'))
1038                     {
1039                         pToFile = SHFileStrCpyCatW(pTempTo, NULL, NULL);
1040                     }
1041                 }
1042                 b_ToInvalidTail = (NULL != StrPBrkW(&pToFile[1], wWildcardChars));
1043             }
1044
1045             /* for all */
1046             b_Mask = (NULL != StrPBrkW(&pFromFile[1], wWildcardChars));
1047             if (FO_RENAME == FuncSwitch)
1048             {
1049                 /* temporary only for FO_RENAME */
1050                 if (b_MultiTo || b_MultiFrom || (b_Mask && !b_ToInvalidTail))
1051                 {
1052 #ifndef W98_FO_FUNCTION
1053                     retCode = ERROR_GEN_FAILURE;  /* W2K ERROR_GEN_FAILURE, W98 returns no error */
1054 #endif
1055                     goto shfileop_end;
1056                 }
1057             }
1058
1059             hFind = FindFirstFileW(pFrom, &wfd);
1060             if (INVALID_HANDLE_VALUE == hFind)
1061             {
1062                 if ((FO_DELETE == FuncSwitch) && (b_Mask))
1063                 {
1064                     pFromFile[0] = '\0';
1065                     FromPathAttr = GetFileAttributesW(pTempFrom);
1066                     pFromFile[0] = '\\';
1067                     if (IsAttribDir(FromPathAttr))
1068                     {
1069                         /* FO_DELETE with mask and without found is valid */
1070                         goto shfileop_end;
1071                     }
1072                 }
1073                 /* root (without mask) is also not allowed as source, tested in W98 */
1074                 retCode = ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1075                 goto shfileop_end;
1076             }
1077
1078             /* for all */
1079
1080             /* ??? b_Mask = (!SHFileStrICmpA(&pFromFile[1], &wfd.cFileName[0], HIGH_ADR, HIGH_ADR)); */
1081             if (!pTo) /* FO_DELETE */
1082             {
1083                 do
1084                 {
1085                     lpFileName = wfd.cAlternateFileName;
1086                     if (!lpFileName[0])
1087                         lpFileName = wfd.cFileName;
1088                     if (IsDotDir(lpFileName) ||
1089                         ((b_Mask) && IsAttribDir(wfd.dwFileAttributes) && (nFileOp.fFlags & FOF_FILESONLY)))
1090                         continue;
1091                     SHFileStrCpyCatW(&pFromFile[1], lpFileName, NULL);
1092                     /* TODO: Check the SHELL_DeleteFileOrDirectoryW() function in shell32.dll */
1093                     if (IsAttribFile(wfd.dwFileAttributes))
1094                     {
1095                         if(SHNotifyDeleteFileW(pTempFrom) != ERROR_SUCCESS)
1096                         {
1097                             nFileOp.fAnyOperationsAborted = TRUE;
1098                             retCode = 0x78; /* value unknown */
1099                         }
1100                     }
1101                     else
1102                     {
1103                         if(!SHELL_DeleteDirectoryW(pTempFrom, (!(nFileOp.fFlags & FOF_NOCONFIRMATION))))
1104                         {
1105                             nFileOp.fAnyOperationsAborted = TRUE;
1106                             retCode = 0x79; /* value unknown */
1107                         }
1108                     }
1109                 } while (!nFileOp.fAnyOperationsAborted && FindNextFileW(hFind, &wfd));
1110                 FindClose(hFind);
1111                 hFind = INVALID_HANDLE_VALUE;
1112                 if (nFileOp.fAnyOperationsAborted)
1113                     goto shfileop_end;
1114                 continue;
1115             } /* FO_DELETE ends, pTo must be always valid from here */
1116
1117             b_SameRoot = (toupperW(pTempFrom[0]) == toupperW(pTempTo[0]));
1118             b_SameTailName = SHFileStrICmpW(pToFile, pFromFile, NULL, NULL);
1119
1120             ToPathAttr = ToAttr = GetFileAttributesW(pTempTo);
1121             if (!b_Mask && (ToAttr == INVALID_FILE_ATTRIBUTES) && (pToFile))
1122             {
1123                 pToFile[0] = '\0';
1124                 ToPathAttr = GetFileAttributesW(pTempTo);
1125                 pToFile[0] = '\\';
1126             }
1127
1128             if (FO_RENAME == FuncSwitch)
1129             {
1130                 if (!b_SameRoot || b_Mask /* FO_RENAME works not with Mask */
1131                     || !SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, NULL)
1132                     || (SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, HIGH_ADR) && !b_ToTailSlash))
1133                 {
1134                     retCode = 0x73;
1135                     goto shfileop_end;
1136                 }
1137                 if (b_ToInvalidTail)
1138                 {
1139                     retCode=0x2;
1140                     goto shfileop_end;
1141                 }
1142                 if (INVALID_FILE_ATTRIBUTES == ToPathAttr)
1143                 {
1144                     retCode = 0x75;
1145                     goto shfileop_end;
1146                 }
1147                 if (IsAttribDir(wfd.dwFileAttributes) && IsAttribDir(ToAttr))
1148                 {
1149                     retCode = (b_ToTailSlash) ? 0xb7 : 0x7b;
1150                     goto shfileop_end;
1151                 }
1152                 /* we use SHNotifyMoveFile() instead MoveFileW */
1153                 if (SHNotifyMoveFileW(pTempFrom, pTempTo) != ERROR_SUCCESS)
1154                 {
1155                     /* we need still the value for the returncode, we use the mostly assumed */
1156                     retCode = 0xb7;
1157                     goto shfileop_end;
1158                 }
1159                 goto shfileop_end;
1160             }
1161
1162             /* W98 Bug with FO_MOVE different from FO_COPY, better the same as FO_COPY */
1163             b_ToValid = ((b_SameTailName &&  b_SameRoot && (FO_COPY == FuncSwitch)) ||
1164                          (b_SameTailName && !b_SameRoot) || (b_ToInvalidTail));
1165
1166             /* handle mask in source */
1167             if (b_Mask)
1168             {
1169                 if (!IsAttribDir(ToAttr))
1170                 {
1171                     retCode = (b_ToInvalidTail &&/* b_SameTailName &&*/ (FO_MOVE == FuncSwitch)) \
1172                         ? 0x2 : 0x75;
1173                     goto shfileop_end;
1174                 }
1175                 pToFile = SHFileStrCpyCatW(pTempTo, NULL, wBackslash);
1176                 nFileOp.fFlags = (nFileOp.fFlags | FOF_MULTIDESTFILES);
1177                 do
1178                 {
1179                     lpFileName = wfd.cAlternateFileName;
1180                     if (!lpFileName[0])
1181                         lpFileName = wfd.cFileName;
1182                     if (IsDotDir(lpFileName) ||
1183                         (IsAttribDir(wfd.dwFileAttributes) && (nFileOp.fFlags & FOF_FILESONLY)))
1184                         continue; /* next name in pTempFrom(dir) */
1185                     SHFileStrCpyCatW(&pToFile[1], lpFileName, NULL);
1186                     SHFileStrCpyCatW(&pFromFile[1], lpFileName, NULL);
1187                     retCode = SHFileOperationW (&nFileOp);
1188                 } while(!nFileOp.fAnyOperationsAborted && FindNextFileW(hFind, &wfd));
1189             }
1190             FindClose(hFind);
1191             hFind = INVALID_HANDLE_VALUE;
1192             /* FO_COPY/FO_MOVE with mask, FO_DELETE and FO_RENAME are solved */
1193             if (b_Mask)
1194                 continue;
1195
1196             /* only FO_COPY/FO_MOVE without mask, all others are (must be) solved */
1197             if (IsAttribDir(wfd.dwFileAttributes) && (ToAttr == INVALID_FILE_ATTRIBUTES))
1198             {
1199                 if (pToFile)
1200                 {
1201                     pToFile[0] = '\0';
1202                     ToPathAttr = GetFileAttributesW(pTempTo);
1203                     if ((ToPathAttr == INVALID_FILE_ATTRIBUTES) && b_ToValid)
1204                     {
1205                         /* create dir must be here, sample target D:\y\ *.* create with RC=10003 */
1206                         if (SHNotifyCreateDirectoryW(pTempTo, NULL))
1207                         {
1208                             retCode = 0x73;/* value unknown */
1209                             goto shfileop_end;
1210                         }
1211                         ToPathAttr = GetFileAttributesW(pTempTo);
1212                     }
1213                     pToFile[0] = '\\';
1214                     if (b_ToInvalidTail)
1215                     {
1216                         retCode = 0x10003;
1217                         goto shfileop_end;
1218                     }
1219                 }
1220             }
1221
1222             /* trailing BackSlash is ever removed and pToFile points to BackSlash before */
1223             if (!b_MultiTo && (b_MultiFrom || (!(b_Multi) && IsAttribDir(ToAttr))))
1224             {
1225                 if ((FO_MOVE == FuncSwitch) && IsAttribDir(ToAttr) && IsAttribDir(wfd.dwFileAttributes))
1226                 {
1227                     if (b_Multi)
1228                     {
1229                         retCode = 0x73; /* !b_Multi = 0x8 ?? */
1230                         goto shfileop_end;
1231                     }
1232                 }
1233                 pToFile = SHFileStrCpyCatW(pTempTo, NULL, wfd.cFileName);
1234                 ToAttr = GetFileAttributesW(pTempTo);
1235             }
1236
1237             if (IsAttribDir(ToAttr))
1238             {
1239                 if (IsAttribFile(wfd.dwFileAttributes))
1240                 {
1241                     retCode = (FO_COPY == FuncSwitch) ? 0x75 : 0xb7;
1242                     goto shfileop_end;
1243                 }
1244             }
1245             else
1246             {
1247                 pToFile[0] = '\0';
1248                 ToPathAttr = GetFileAttributesW(pTempTo);
1249                 pToFile[0] = '\\';
1250                 if (IsAttribFile(ToPathAttr))
1251                 {
1252                     /* error, is this tested ? */
1253                     retCode = 0x777402;
1254                     goto shfileop_end;
1255                 }
1256             }
1257
1258             /* singlesource + no mask */
1259             if (INVALID_FILE_ATTRIBUTES == (ToAttr & ToPathAttr))
1260             {
1261                 /* Target-dir does not exist, and cannot be created */
1262                 retCode=0x75;
1263                 goto shfileop_end;
1264             }
1265
1266             switch(FuncSwitch)
1267             {
1268             case FO_MOVE:
1269                 pToFile = NULL;
1270                 if ((ToAttr == INVALID_FILE_ATTRIBUTES) && SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, NULL))
1271                 {
1272                     nFileOp.wFunc =  ((level+1)<<4) + FO_RENAME;
1273                 }
1274                 else
1275                 {
1276                     if (b_SameRoot && IsAttribDir(ToAttr) && IsAttribDir(wfd.dwFileAttributes))
1277                     {
1278                         /* we need pToFile for FO_DELETE after FO_MOVE contence */
1279                         pToFile = SHFileStrCpyCatW(pTempFrom, NULL, wWildcardFile);
1280                     }
1281                     else
1282                     {
1283                         nFileOp.wFunc =  ((level+1)<<4) + FO_COPY;
1284                     }
1285                 }
1286                 retCode = SHFileOperationW(&nFileOp);
1287                 if (pToFile)
1288                     ((DWORD*)pToFile)[0] = '\0';
1289                 if (!nFileOp.fAnyOperationsAborted && (FO_RENAME != (nFileOp.wFunc & 0xf)))
1290                 {
1291                     nFileOp.wFunc =  ((level+1)<<4) + FO_DELETE;
1292                     retCode = SHFileOperationW(&nFileOp);
1293                 }
1294                 continue;
1295             case FO_COPY:
1296                 if (SHFileStrICmpW(pTempFrom, pTempTo, NULL, NULL))
1297                 { /* target is the same as source ? */
1298                     /* we still need the value for the returncode, we assume 0x71 */
1299                     retCode = 0x71;
1300                     goto shfileop_end;
1301                 }
1302                 if (IsAttribDir((ToAttr & wfd.dwFileAttributes)))
1303                 {
1304                     if (IsAttribDir(ToAttr) || !SHNotifyCreateDirectoryW(pTempTo, NULL))
1305                     {
1306                         /* ??? nFileOp.fFlags = (nFileOp.fFlags | FOF_MULTIDESTFILES); */
1307                         SHFileStrCpyCatW(pTempFrom, NULL, wWildcardFile);
1308                         retCode = SHFileOperationW(&nFileOp);
1309                     }
1310                     else
1311                     {
1312                         retCode = 0x750;/* value unknown */
1313                         goto shfileop_end;
1314                     }
1315                 }
1316                 else
1317                 {
1318                     if (!(ask_overwrite && SHELL_ConfirmDialogW(ASK_OVERWRITE_FILE, pTempTo))
1319                         && (not_overwrite))
1320                     {
1321                         /* we still need the value for the returncode, we use the mostly assumed */
1322                         retCode = 0x73;
1323                         goto shfileop_end;
1324                     }
1325                     if (SHNotifyCopyFileW(pTempFrom, pTempTo, TRUE) != ERROR_SUCCESS)
1326                     {
1327                         retCode = 0x77; /* value unknown */
1328                         goto shfileop_end;
1329                     }
1330                 }
1331             }
1332         }
1333
1334 shfileop_end:
1335         if (hFind != INVALID_HANDLE_VALUE)
1336             FindClose(hFind);
1337         hFind = INVALID_HANDLE_VALUE;
1338         if (pTempFrom)
1339             HeapFree(GetProcessHeap(), 0, pTempFrom);
1340         if (retCode)
1341             nFileOp.fAnyOperationsAborted = TRUE;
1342         TRACE("%s level=%ld AnyOpsAborted=%s ret=0x%x, with %s %s%s\n",
1343               debug_shfileops_action(FuncSwitch), level,
1344               nFileOp.fAnyOperationsAborted ? "TRUE":"FALSE",
1345               retCode, debugstr_w(pFrom), pTo ? "-> ":"", debugstr_w(pTo));
1346
1347         lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
1348         return retCode;
1349 }
1350
1351 /*************************************************************************
1352  * SHFileOperation        [SHELL32.@]
1353  *
1354  */
1355 DWORD WINAPI SHFileOperationAW(LPVOID lpFileOp)
1356 {
1357         if (SHELL_OsIsUnicode())
1358           return SHFileOperationW(lpFileOp);
1359         return SHFileOperationA(lpFileOp);
1360 }
1361
1362 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1363
1364 /*************************************************************************
1365  * SHFreeNameMappings      [shell32.246]
1366  *
1367  * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1368  * was specified.
1369  *
1370  * PARAMS
1371  *  hNameMapping [I] handle to the name mappings used during renaming of files
1372  *
1373  */
1374 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1375 {
1376         if (hNameMapping)
1377         {
1378           int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1379
1380           for (; i>= 0; i--)
1381           {
1382             LPSHNAMEMAPPINGW lp = DSA_GetItemPtr((HDSA)hNameMapping, i);
1383
1384             SHFree(lp->pszOldPath);
1385             SHFree(lp->pszNewPath);
1386           }
1387           DSA_Destroy((HDSA)hNameMapping);
1388         }
1389 }
1390
1391 /*************************************************************************
1392  * SheGetDirW [SHELL32.281]
1393  *
1394  */
1395 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1396 {       FIXME("%p %p stub\n",u,v);
1397         return 0;
1398 }
1399
1400 /*************************************************************************
1401  * SheChangeDirW [SHELL32.274]
1402  *
1403  */
1404 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1405 {       FIXME("(%s),stub\n",debugstr_w(u));
1406         return 0;
1407 }
1408
1409 /*************************************************************************
1410  * IsNetDrive                   [SHELL32.66]
1411  */
1412 BOOL WINAPI IsNetDrive(DWORD drive)
1413 {
1414         char root[4];
1415         strcpy(root, "A:\\");
1416         root[0] += (char)drive;
1417         return (GetDriveTypeA(root) == DRIVE_REMOTE);
1418 }
1419
1420
1421 /*************************************************************************
1422  * RealDriveType                [SHELL32.524]
1423  */
1424 INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1425 {
1426     char root[] = "A:\\";
1427     root[0] += (char)drive;
1428     return GetDriveTypeA(root);
1429 }