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