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