comctl32/listview: Don't change Z-order of edit window while resizing edit box.
[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 #include <assert.h>
31
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "shellapi.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "shlobj.h"
39 #include "shresdef.h"
40 #define NO_SHLWAPI_STREAM
41 #include "shlwapi.h"
42 #include "shell32_main.h"
43 #include "undocshell.h"
44 #include "wine/debug.h"
45 #include "xdg.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(shell);
48
49 #define IsAttrib(x, y)  ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
50 #define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
51 #define IsAttribDir(x)  IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
52 #define IsDotDir(x)     ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
53
54 #define FO_MASK         0xF
55
56 static const WCHAR wWildcardFile[] = {'*',0};
57 static const WCHAR wWildcardChars[] = {'*','?',0};
58
59 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec);
60 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec);
61 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path);
62 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path);
63 static DWORD SHNotifyDeleteFileA(LPCSTR path);
64 static DWORD SHNotifyDeleteFileW(LPCWSTR path);
65 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest);
66 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists);
67 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly);
68
69 typedef struct
70 {
71     SHFILEOPSTRUCTW *req;
72     DWORD dwYesToAllMask;
73     BOOL bManyItems;
74     BOOL bCancelled;
75 } FILE_OPERATION;
76
77 /* Confirm dialogs with an optional "Yes To All" as used in file operations confirmations
78  */
79 static const WCHAR CONFIRM_MSG_PROP[] = {'W','I','N','E','_','C','O','N','F','I','R','M',0};
80
81 struct confirm_msg_info
82 {
83     LPWSTR lpszText;
84     LPWSTR lpszCaption;
85     HICON hIcon;
86     BOOL bYesToAll;
87 };
88
89 /* as some buttons may be hidden and the dialog height may change we may need
90  * to move the controls */
91 static void confirm_msg_move_button(HWND hDlg, INT iId, INT *xPos, INT yOffset, BOOL bShow)
92 {
93     HWND hButton = GetDlgItem(hDlg, iId);
94     RECT r;
95
96     if (bShow) {
97         POINT pt;
98         int width;
99
100         GetWindowRect(hButton, &r);
101         width = r.right - r.left;
102         pt.x = r.left;
103         pt.y = r.top;
104         ScreenToClient(hDlg, &pt);
105         MoveWindow(hButton, *xPos - width, pt.y - yOffset, width, r.bottom - r.top, FALSE);
106         *xPos -= width + 5;
107     }
108     else
109         ShowWindow(hButton, SW_HIDE);
110 }
111
112 /* Note: we paint the text manually and don't use the static control to make
113  * sure the text has the same height as the one computed in WM_INITDIALOG
114  */
115 static INT_PTR ConfirmMsgBox_Paint(HWND hDlg)
116 {
117     PAINTSTRUCT ps;
118     HFONT hOldFont;
119     RECT r;
120     HDC hdc;
121
122     BeginPaint(hDlg, &ps);
123     hdc = ps.hdc;
124
125     GetClientRect(GetDlgItem(hDlg, IDD_MESSAGE), &r);
126     /* this will remap the rect to dialog coords */
127     MapWindowPoints(GetDlgItem(hDlg, IDD_MESSAGE), hDlg, (LPPOINT)&r, 2);
128     hOldFont = SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDD_MESSAGE, WM_GETFONT, 0, 0));
129     DrawTextW(hdc, GetPropW(hDlg, CONFIRM_MSG_PROP), -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK);
130     SelectObject(hdc, hOldFont);
131     EndPaint(hDlg, &ps);
132     return TRUE;
133 }
134
135 static INT_PTR ConfirmMsgBox_Init(HWND hDlg, LPARAM lParam)
136 {
137     struct confirm_msg_info *info = (struct confirm_msg_info *)lParam;
138     INT xPos, yOffset;
139     int width, height;
140     HFONT hOldFont;
141     HDC hdc;
142     RECT r;
143
144     SetWindowTextW(hDlg, info->lpszCaption);
145     ShowWindow(GetDlgItem(hDlg, IDD_MESSAGE), SW_HIDE);
146     SetPropW(hDlg, CONFIRM_MSG_PROP, info->lpszText);
147     SendDlgItemMessageW(hDlg, IDD_ICON, STM_SETICON, (WPARAM)info->hIcon, 0);
148
149     /* compute the text height and resize the dialog */
150     GetClientRect(GetDlgItem(hDlg, IDD_MESSAGE), &r);
151     hdc = GetDC(hDlg);
152     yOffset = r.bottom;
153     hOldFont = SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDD_MESSAGE, WM_GETFONT, 0, 0));
154     DrawTextW(hdc, info->lpszText, -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK | DT_CALCRECT);
155     SelectObject(hdc, hOldFont);
156     yOffset -= r.bottom;
157     yOffset = min(yOffset, 35);  /* don't make the dialog too small */
158     ReleaseDC(hDlg, hdc);
159
160     GetClientRect(hDlg, &r);
161     xPos = r.right - 7;
162     GetWindowRect(hDlg, &r);
163     width = r.right - r.left;
164     height = r.bottom - r.top - yOffset;
165     MoveWindow(hDlg, (GetSystemMetrics(SM_CXSCREEN) - width)/2,
166         (GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
167
168     confirm_msg_move_button(hDlg, IDCANCEL,     &xPos, yOffset, info->bYesToAll);
169     confirm_msg_move_button(hDlg, IDNO,         &xPos, yOffset, TRUE);
170     confirm_msg_move_button(hDlg, IDD_YESTOALL, &xPos, yOffset, info->bYesToAll);
171     confirm_msg_move_button(hDlg, IDYES,        &xPos, yOffset, TRUE);
172     return TRUE;
173 }
174
175 static INT_PTR CALLBACK ConfirmMsgBoxProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
176 {
177     switch (uMsg)
178     {
179         case WM_INITDIALOG:
180             return ConfirmMsgBox_Init(hDlg, lParam);
181         case WM_PAINT:
182             return ConfirmMsgBox_Paint(hDlg);
183         case WM_COMMAND:
184             EndDialog(hDlg, wParam);
185             break;
186         case WM_CLOSE:
187             EndDialog(hDlg, IDCANCEL);
188             break;
189     }
190     return FALSE;
191 }
192
193 static int SHELL_ConfirmMsgBox(HWND hWnd, LPWSTR lpszText, LPWSTR lpszCaption, HICON hIcon, BOOL bYesToAll)
194 {
195     static const WCHAR wszTemplate[] = {'S','H','E','L','L','_','Y','E','S','T','O','A','L','L','_','M','S','G','B','O','X',0};
196     struct confirm_msg_info info;
197
198     info.lpszText = lpszText;
199     info.lpszCaption = lpszCaption;
200     info.hIcon = hIcon;
201     info.bYesToAll = bYesToAll;
202     return DialogBoxParamW(shell32_hInstance, wszTemplate, hWnd, ConfirmMsgBoxProc, (LPARAM)&info);
203 }
204
205 /* confirmation dialogs content */
206 typedef struct
207 {
208         HINSTANCE hIconInstance;
209         UINT icon_resource_id;
210         UINT caption_resource_id, text_resource_id;
211 } SHELL_ConfirmIDstruc;
212
213 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
214 {
215         ids->hIconInstance = shell32_hInstance;
216         switch (nKindOfDialog) {
217           case ASK_DELETE_FILE:
218             ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
219             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
220             ids->text_resource_id  = IDS_DELETEITEM_TEXT;
221             return TRUE;
222           case ASK_DELETE_FOLDER:
223             ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
224             ids->caption_resource_id  = IDS_DELETEFOLDER_CAPTION;
225             ids->text_resource_id  = IDS_DELETEITEM_TEXT;
226             return TRUE;
227           case ASK_DELETE_MULTIPLE_ITEM:
228             ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
229             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
230             ids->text_resource_id  = IDS_DELETEMULTIPLE_TEXT;
231             return TRUE;
232           case ASK_TRASH_FILE:
233             ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
234             ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
235             ids->text_resource_id = IDS_TRASHITEM_TEXT;
236             return TRUE;
237           case ASK_TRASH_FOLDER:
238             ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
239             ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
240             ids->text_resource_id = IDS_TRASHFOLDER_TEXT;
241             return TRUE;
242           case ASK_TRASH_MULTIPLE_ITEM:
243             ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
244             ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
245             ids->text_resource_id = IDS_TRASHMULTIPLE_TEXT;
246             return TRUE;
247           case ASK_CANT_TRASH_ITEM:
248             ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
249             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
250             ids->text_resource_id  = IDS_CANTTRASH_TEXT;
251             return TRUE;
252           case ASK_DELETE_SELECTED:
253             ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
254             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
255             ids->text_resource_id  = IDS_DELETESELECTED_TEXT;
256             return TRUE;
257           case ASK_OVERWRITE_FILE:
258             ids->hIconInstance = NULL;
259             ids->icon_resource_id = IDI_WARNING;
260             ids->caption_resource_id  = IDS_OVERWRITEFILE_CAPTION;
261             ids->text_resource_id  = IDS_OVERWRITEFILE_TEXT;
262             return TRUE;
263           case ASK_OVERWRITE_FOLDER:
264             ids->hIconInstance = NULL;
265             ids->icon_resource_id = IDI_WARNING;
266             ids->caption_resource_id  = IDS_OVERWRITEFILE_CAPTION;
267             ids->text_resource_id  = IDS_OVERWRITEFOLDER_TEXT;
268             return TRUE;
269           default:
270             FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
271         }
272         return FALSE;
273 }
274
275 static BOOL SHELL_ConfirmDialogW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir, FILE_OPERATION *op)
276 {
277         WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
278         SHELL_ConfirmIDstruc ids;
279         DWORD_PTR args[1];
280         HICON hIcon;
281         int ret;
282
283         assert(nKindOfDialog >= 0 && nKindOfDialog < 32);
284         if (op && (op->dwYesToAllMask & (1 << nKindOfDialog)))
285             return TRUE;
286
287         if (!SHELL_ConfirmIDs(nKindOfDialog, &ids)) return FALSE;
288
289         LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption)/sizeof(WCHAR));
290         LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText)/sizeof(WCHAR));
291
292         args[0] = (DWORD_PTR)szDir;
293         FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
294                        szText, 0, 0, szBuffer, sizeof(szBuffer), (__ms_va_list*)args);
295         hIcon = LoadIconW(ids.hIconInstance, (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id));
296
297         ret = SHELL_ConfirmMsgBox(hWnd, szBuffer, szCaption, hIcon, op && op->bManyItems);
298         if (op) {
299             if (ret == IDD_YESTOALL) {
300                 op->dwYesToAllMask |= (1 << nKindOfDialog);
301                 ret = IDYES;
302             }
303             if (ret == IDCANCEL)
304                 op->bCancelled = TRUE;
305             if (ret != IDYES)
306                 op->req->fAnyOperationsAborted = TRUE;
307         }
308         return ret == IDYES;
309 }
310
311 BOOL SHELL_ConfirmYesNoW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir)
312 {
313     return SHELL_ConfirmDialogW(hWnd, nKindOfDialog, szDir, NULL);
314 }
315
316 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
317 {
318         DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
319
320         if (len < minChars)
321           len = minChars;
322
323         *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
324         if (*wPath)
325         {
326           MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
327           return NO_ERROR;
328         }
329         return E_OUTOFMEMORY;
330 }
331
332 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
333 {
334         HeapFree(GetProcessHeap(), 0, wPath);
335 }
336
337 HRESULT WINAPI SHIsFileAvailableOffline(LPCWSTR path, LPDWORD status)
338 {
339     FIXME("(%s, %p) stub\n", debugstr_w(path), status);
340     return E_FAIL;
341 }
342
343 /**************************************************************************
344  * SHELL_DeleteDirectory()  [internal]
345  *
346  * Asks for confirmation when bShowUI is true and deletes the directory and
347  * all its subdirectories and files if necessary.
348  */
349 static BOOL SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
350 {
351         BOOL    ret = TRUE;
352         HANDLE  hFind;
353         WIN32_FIND_DATAW wfd;
354         WCHAR   szTemp[MAX_PATH];
355
356         /* Make sure the directory exists before eventually prompting the user */
357         PathCombineW(szTemp, pszDir, wWildcardFile);
358         hFind = FindFirstFileW(szTemp, &wfd);
359         if (hFind == INVALID_HANDLE_VALUE)
360           return FALSE;
361
362         if (!bShowUI || (ret = SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir, NULL)))
363         {
364           do
365           {
366             if (IsDotDir(wfd.cFileName))
367               continue;
368             PathCombineW(szTemp, pszDir, wfd.cFileName);
369             if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
370               ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
371             else
372               ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
373           } while (ret && FindNextFileW(hFind, &wfd));
374         }
375         FindClose(hFind);
376         if (ret)
377           ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
378         return ret;
379 }
380
381 /**************************************************************************
382  * Win32CreateDirectory      [SHELL32.93]
383  *
384  * Creates a directory. Also triggers a change notify if one exists.
385  *
386  * PARAMS
387  *  path       [I]   path to directory to create
388  *
389  * RETURNS
390  *  TRUE if successful, FALSE otherwise
391  *
392  * NOTES
393  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
394  *  This is Unicode on NT/2000
395  */
396 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
397 {
398         LPWSTR wPath;
399         DWORD retCode;
400
401         TRACE("(%s, %p)\n", debugstr_a(path), sec);
402
403         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
404         if (!retCode)
405         {
406           retCode = SHNotifyCreateDirectoryW(wPath, sec);
407           SHELL32_FreeUnicodeBuf(wPath);
408         }
409         return retCode;
410 }
411
412 /**********************************************************************/
413
414 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
415 {
416         TRACE("(%s, %p)\n", debugstr_w(path), sec);
417
418         if (CreateDirectoryW(path, sec))
419         {
420           SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
421           return ERROR_SUCCESS;
422         }
423         return GetLastError();
424 }
425
426 /**********************************************************************/
427
428 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
429 {
430         if (SHELL_OsIsUnicode())
431           return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
432         return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
433 }
434
435 /************************************************************************
436  * Win32RemoveDirectory      [SHELL32.94]
437  *
438  * Deletes a directory. Also triggers a change notify if one exists.
439  *
440  * PARAMS
441  *  path       [I]   path to directory to delete
442  *
443  * RETURNS
444  *  TRUE if successful, FALSE otherwise
445  *
446  * NOTES
447  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
448  *  This is Unicode on NT/2000
449  */
450 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
451 {
452         LPWSTR wPath;
453         DWORD retCode;
454
455         TRACE("(%s)\n", debugstr_a(path));
456
457         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
458         if (!retCode)
459         {
460           retCode = SHNotifyRemoveDirectoryW(wPath);
461           SHELL32_FreeUnicodeBuf(wPath);
462         }
463         return retCode;
464 }
465
466 /***********************************************************************/
467
468 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
469 {
470         BOOL ret;
471         TRACE("(%s)\n", debugstr_w(path));
472
473         ret = RemoveDirectoryW(path);
474         if (!ret)
475         {
476           /* Directory may be write protected */
477           DWORD dwAttr = GetFileAttributesW(path);
478           if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
479             if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
480               ret = RemoveDirectoryW(path);
481         }
482         if (ret)
483         {
484           SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
485           return ERROR_SUCCESS;
486         }
487         return GetLastError();
488 }
489
490 /***********************************************************************/
491
492 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
493 {
494         if (SHELL_OsIsUnicode())
495           return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
496         return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
497 }
498
499 /************************************************************************
500  * Win32DeleteFile           [SHELL32.164]
501  *
502  * Deletes a file. Also triggers a change notify if one exists.
503  *
504  * PARAMS
505  *  path       [I]   path to file to delete
506  *
507  * RETURNS
508  *  TRUE if successful, FALSE otherwise
509  *
510  * NOTES
511  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
512  *  This is Unicode on NT/2000
513  */
514 static DWORD SHNotifyDeleteFileA(LPCSTR path)
515 {
516         LPWSTR wPath;
517         DWORD retCode;
518
519         TRACE("(%s)\n", debugstr_a(path));
520
521         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
522         if (!retCode)
523         {
524           retCode = SHNotifyDeleteFileW(wPath);
525           SHELL32_FreeUnicodeBuf(wPath);
526         }
527         return retCode;
528 }
529
530 /***********************************************************************/
531
532 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
533 {
534         BOOL ret;
535
536         TRACE("(%s)\n", debugstr_w(path));
537
538         ret = DeleteFileW(path);
539         if (!ret)
540         {
541           /* File may be write protected or a system file */
542           DWORD dwAttr = GetFileAttributesW(path);
543           if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
544             if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
545               ret = DeleteFileW(path);
546         }
547         if (ret)
548         {
549           SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
550           return ERROR_SUCCESS;
551         }
552         return GetLastError();
553 }
554
555 /***********************************************************************/
556
557 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
558 {
559         if (SHELL_OsIsUnicode())
560           return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
561         return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
562 }
563
564 /************************************************************************
565  * SHNotifyMoveFile          [internal]
566  *
567  * Moves a file. Also triggers a change notify if one exists.
568  *
569  * PARAMS
570  *  src        [I]   path to source file to move
571  *  dest       [I]   path to target file to move to
572  *
573  * RETURNS
574  *  ERORR_SUCCESS if successful
575  */
576 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
577 {
578         BOOL ret;
579
580         TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
581
582         ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
583
584         /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
585         if (!ret)
586             ret = MoveFileW(src, dest);
587
588         if (!ret)
589         {
590           DWORD dwAttr;
591
592           dwAttr = SHFindAttrW(dest, FALSE);
593           if (INVALID_FILE_ATTRIBUTES == dwAttr)
594           {
595             /* Source file may be write protected or a system file */
596             dwAttr = GetFileAttributesW(src);
597             if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
598               if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
599                 ret = MoveFileW(src, dest);
600           }
601         }
602         if (ret)
603         {
604           SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
605           return ERROR_SUCCESS;
606         }
607         return GetLastError();
608 }
609
610 /************************************************************************
611  * SHNotifyCopyFile          [internal]
612  *
613  * Copies a file. Also triggers a change notify if one exists.
614  *
615  * PARAMS
616  *  src           [I]   path to source file to move
617  *  dest          [I]   path to target file to move to
618  *  bFailIfExists [I]   if TRUE, the target file will not be overwritten if
619  *                      a file with this name already exists
620  *
621  * RETURNS
622  *  ERROR_SUCCESS if successful
623  */
624 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
625 {
626         BOOL ret;
627         DWORD attribs;
628
629         TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
630
631         /* Destination file may already exist with read only attribute */
632         attribs = GetFileAttributesW(dest);
633         if (IsAttrib(attribs, FILE_ATTRIBUTE_READONLY))
634           SetFileAttributesW(dest, attribs & ~FILE_ATTRIBUTE_READONLY);
635
636         ret = CopyFileW(src, dest, bFailIfExists);
637         if (ret)
638         {
639           SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
640           return ERROR_SUCCESS;
641         }
642
643         return GetLastError();
644 }
645
646 /*************************************************************************
647  * SHCreateDirectory         [SHELL32.165]
648  *
649  * This function creates a file system folder whose fully qualified path is
650  * given by path. If one or more of the intermediate folders do not exist,
651  * they will be created as well.
652  *
653  * PARAMS
654  *  hWnd       [I]
655  *  path       [I]   path of directory to create
656  *
657  * RETURNS
658  *  ERROR_SUCCESS or one of the following values:
659  *  ERROR_BAD_PATHNAME if the path is relative
660  *  ERROR_FILE_EXISTS when a file with that name exists
661  *  ERROR_PATH_NOT_FOUND can't find the path, probably invalid
662  *  ERROR_INVALID_NAME if the path contains invalid chars
663  *  ERROR_ALREADY_EXISTS when the directory already exists
664  *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
665  *
666  * NOTES
667  *  exported by ordinal
668  *  Win9x exports ANSI
669  *  WinNT/2000 exports Unicode
670  */
671 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
672 {
673         if (SHELL_OsIsUnicode())
674           return SHCreateDirectoryExW(hWnd, path, NULL);
675         return SHCreateDirectoryExA(hWnd, path, NULL);
676 }
677
678 /*************************************************************************
679  * SHCreateDirectoryExA      [SHELL32.@]
680  *
681  * This function creates a file system folder whose fully qualified path is
682  * given by path. If one or more of the intermediate folders do not exist,
683  * they will be created as well.
684  *
685  * PARAMS
686  *  hWnd       [I]
687  *  path       [I]   path of directory to create
688  *  sec        [I]   security attributes to use or NULL
689  *
690  * RETURNS
691  *  ERROR_SUCCESS or one of the following values:
692  *  ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
693  *  ERROR_INVALID_NAME if the path contains invalid chars
694  *  ERROR_FILE_EXISTS when a file with that name exists
695  *  ERROR_ALREADY_EXISTS when the directory already exists
696  *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
697  *
698  *  FIXME: Not implemented yet;
699  *  SHCreateDirectoryEx also verifies that the files in the directory will be visible
700  *  if the path is a network path to deal with network drivers which might have a limited
701  *  but unknown maximum path length. If not:
702  *
703  *  If hWnd is set to a valid window handle, a message box is displayed warning
704  *  the user that the files may not be accessible. If the user chooses not to
705  *  proceed, the function returns ERROR_CANCELLED.
706  *
707  *  If hWnd is set to NULL, no user interface is displayed and the function
708  *  returns ERROR_CANCELLED.
709  */
710 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
711 {
712         LPWSTR wPath;
713         DWORD retCode;
714
715         TRACE("(%s, %p)\n", debugstr_a(path), sec);
716
717         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
718         if (!retCode)
719         {
720           retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
721           SHELL32_FreeUnicodeBuf(wPath);
722         }
723         return retCode;
724 }
725
726 /*************************************************************************
727  * SHCreateDirectoryExW      [SHELL32.@]
728  *
729  * See SHCreateDirectoryExA.
730  */
731 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
732 {
733         int ret = ERROR_BAD_PATHNAME;
734         TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
735
736         if (PathIsRelativeW(path))
737         {
738           SetLastError(ret);
739         }
740         else
741         {
742           ret = SHNotifyCreateDirectoryW(path, sec);
743           /* Refuse to work on certain error codes before trying to create directories recursively */
744           if (ret != ERROR_SUCCESS &&
745               ret != ERROR_FILE_EXISTS &&
746               ret != ERROR_ALREADY_EXISTS &&
747               ret != ERROR_FILENAME_EXCED_RANGE)
748           {
749             WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1];  /* extra for PathAddBackslash() */
750
751             lstrcpynW(szTemp, path, MAX_PATH);
752             pEnd = PathAddBackslashW(szTemp);
753             pSlash = szTemp + 3;
754
755             while (*pSlash)
756             {
757               while (*pSlash && *pSlash != '\\') pSlash++;
758               if (*pSlash)
759               {
760                 *pSlash = 0;    /* terminate path at separator */
761
762                 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
763               }
764               *pSlash++ = '\\'; /* put the separator back */
765             }
766           }
767
768           if (ret && hWnd && (ERROR_CANCELLED != ret))
769           {
770             /* We failed and should show a dialog box */
771             FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
772             ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
773           }
774         }
775         return ret;
776 }
777
778 /*************************************************************************
779  * SHFindAttrW      [internal]
780  *
781  * Get the Attributes for a file or directory. The difference to GetAttributes()
782  * is that this function will also work for paths containing wildcard characters
783  * in its filename.
784
785  * PARAMS
786  *  path       [I]   path of directory or file to check
787  *  fileOnly   [I]   TRUE if only files should be found
788  *
789  * RETURNS
790  *  INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
791  *  the first file or directory found otherwise
792  */
793 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
794 {
795         WIN32_FIND_DATAW wfd;
796         BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
797         DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
798         HANDLE hFind = FindFirstFileW(pName, &wfd);
799
800         TRACE("%s %d\n", debugstr_w(pName), fileOnly);
801         if (INVALID_HANDLE_VALUE != hFind)
802         {
803           do
804           {
805             if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
806                continue;
807             dwAttr = wfd.dwFileAttributes;
808             break;
809           }
810           while (FindNextFileW(hFind, &wfd));
811           FindClose(hFind);
812         }
813         return dwAttr;
814 }
815
816 /*************************************************************************
817  *
818  * SHNameTranslate HelperFunction for SHFileOperationA
819  *
820  * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
821  * is NULL, only the necessary size of the string is determined and returned,
822  * otherwise the ASCII strings are copied into it and the buffer is increased
823  * to point to the location after the final 0 termination char.
824  */
825 static DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
826 {
827         DWORD size = 0, aSize = 0;
828         LPCSTR aString = (LPCSTR)*pWToFrom;
829
830         if (aString)
831         {
832           do
833           {
834             size = lstrlenA(aString) + 1;
835             aSize += size;
836             aString += size;
837           } while ((size != 1) && more);
838           /* The two sizes might be different in the case of multibyte chars */
839           size = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, 0);
840           if (*wString) /* only in the second loop */
841           {
842             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
843             *pWToFrom = *wString;
844             *wString += size;
845           }
846         }
847         return size;
848 }
849 /*************************************************************************
850  * SHFileOperationA          [SHELL32.@]
851  *
852  * Function to copy, move, delete and create one or more files with optional
853  * user prompts.
854  *
855  * PARAMS
856  *  lpFileOp   [I/O] pointer to a structure containing all the necessary information
857  *
858  * RETURNS
859  *  Success: ERROR_SUCCESS.
860  *  Failure: ERROR_CANCELLED.
861  *
862  * NOTES
863  *  exported by name
864  */
865 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
866 {
867         SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
868         int retCode = 0;
869         DWORD size;
870         LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
871                wString = NULL; /* we change this in SHNameTranslate */
872
873         TRACE("\n");
874         if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
875           nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
876         if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
877           nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
878         while (1) /* every loop calculate size, second translate also, if we have storage for this */
879         {
880           size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
881           size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
882           size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
883
884           if (ForFree)
885           {
886             retCode = SHFileOperationW(&nFileOp);
887             HeapFree(GetProcessHeap(), 0, ForFree); /* we cannot use wString, it was changed */
888             break;
889           }
890           else
891           {
892             wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
893             if (ForFree) continue;
894             retCode = ERROR_OUTOFMEMORY;
895             nFileOp.fAnyOperationsAborted = TRUE;
896             SetLastError(retCode);
897             return retCode;
898           }
899         }
900
901         lpFileOp->hNameMappings = nFileOp.hNameMappings;
902         lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
903         return retCode;
904 }
905
906 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
907
908 typedef struct
909 {
910     DWORD attributes;
911     LPWSTR szDirectory;
912     LPWSTR szFilename;
913     LPWSTR szFullPath;
914     BOOL bFromWildcard;
915     BOOL bFromRelative;
916     BOOL bExists;
917 } FILE_ENTRY;
918
919 typedef struct
920 {
921     FILE_ENTRY *feFiles;
922     DWORD num_alloc;
923     DWORD dwNumFiles;
924     BOOL bAnyFromWildcard;
925     BOOL bAnyDirectories;
926     BOOL bAnyDontExist;
927 } FILE_LIST;
928
929
930 static inline void grow_list(FILE_LIST *list)
931 {
932     FILE_ENTRY *new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
933                                   list->num_alloc * 2 * sizeof(*new) );
934     list->feFiles = new;
935     list->num_alloc *= 2;
936 }
937
938 /* adds a file to the FILE_ENTRY struct
939  */
940 static void add_file_to_entry(FILE_ENTRY *feFile, LPCWSTR szFile)
941 {
942     DWORD dwLen = lstrlenW(szFile) + 1;
943     LPCWSTR ptr;
944
945     feFile->szFullPath = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
946     lstrcpyW(feFile->szFullPath, szFile);
947
948     ptr = StrRChrW(szFile, NULL, '\\');
949     if (ptr)
950     {
951         dwLen = ptr - szFile + 1;
952         feFile->szDirectory = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
953         lstrcpynW(feFile->szDirectory, szFile, dwLen);
954
955         dwLen = lstrlenW(feFile->szFullPath) - dwLen + 1;
956         feFile->szFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
957         lstrcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
958     }
959     feFile->bFromWildcard = FALSE;
960 }
961
962 static LPWSTR wildcard_to_file(LPCWSTR szWildCard, LPCWSTR szFileName)
963 {
964     LPCWSTR ptr;
965     LPWSTR szFullPath;
966     DWORD dwDirLen, dwFullLen;
967
968     ptr = StrRChrW(szWildCard, NULL, '\\');
969     dwDirLen = ptr - szWildCard + 1;
970
971     dwFullLen = dwDirLen + lstrlenW(szFileName) + 1;
972     szFullPath = HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
973
974     lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
975     lstrcatW(szFullPath, szFileName);
976
977     return szFullPath;
978 }
979
980 static void parse_wildcard_files(FILE_LIST *flList, LPCWSTR szFile, LPDWORD pdwListIndex)
981 {
982     WIN32_FIND_DATAW wfd;
983     HANDLE hFile = FindFirstFileW(szFile, &wfd);
984     FILE_ENTRY *file;
985     LPWSTR szFullPath;
986     BOOL res;
987
988     if (hFile == INVALID_HANDLE_VALUE) return;
989
990     for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
991     {
992         if (IsDotDir(wfd.cFileName)) continue;
993         if (*pdwListIndex >= flList->num_alloc) grow_list( flList );
994         szFullPath = wildcard_to_file(szFile, wfd.cFileName);
995         file = &flList->feFiles[(*pdwListIndex)++];
996         add_file_to_entry(file, szFullPath);
997         file->bFromWildcard = TRUE;
998         file->attributes = wfd.dwFileAttributes;
999         if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
1000         HeapFree(GetProcessHeap(), 0, szFullPath);
1001     }
1002
1003     FindClose(hFile);
1004 }
1005
1006 /* takes the null-separated file list and fills out the FILE_LIST */
1007 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
1008 {
1009     LPCWSTR ptr = szFiles;
1010     WCHAR szCurFile[MAX_PATH];
1011     DWORD i = 0;
1012
1013     if (!szFiles)
1014         return ERROR_INVALID_PARAMETER;
1015
1016     flList->bAnyFromWildcard = FALSE;
1017     flList->bAnyDirectories = FALSE;
1018     flList->bAnyDontExist = FALSE;
1019     flList->num_alloc = 32;
1020     flList->dwNumFiles = 0;
1021
1022     /* empty list */
1023     if (!szFiles[0])
1024         return ERROR_ACCESS_DENIED;
1025         
1026     flList->feFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1027                                 flList->num_alloc * sizeof(FILE_ENTRY));
1028
1029     while (*ptr)
1030     {
1031         if (i >= flList->num_alloc) grow_list( flList );
1032
1033         /* change relative to absolute path */
1034         if (PathIsRelativeW(ptr))
1035         {
1036             GetCurrentDirectoryW(MAX_PATH, szCurFile);
1037             PathCombineW(szCurFile, szCurFile, ptr);
1038             flList->feFiles[i].bFromRelative = TRUE;
1039         }
1040         else
1041         {
1042             lstrcpyW(szCurFile, ptr);
1043             flList->feFiles[i].bFromRelative = FALSE;
1044         }
1045
1046         /* parse wildcard files if they are in the filename */
1047         if (StrPBrkW(szCurFile, wWildcardChars))
1048         {
1049             parse_wildcard_files(flList, szCurFile, &i);
1050             flList->bAnyFromWildcard = TRUE;
1051             i--;
1052         }
1053         else
1054         {
1055             FILE_ENTRY *file = &flList->feFiles[i];
1056             add_file_to_entry(file, szCurFile);
1057             file->attributes = GetFileAttributesW( file->szFullPath );
1058             file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
1059             if (!file->bExists) flList->bAnyDontExist = TRUE;
1060             if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
1061         }
1062
1063         /* advance to the next string */
1064         ptr += lstrlenW(ptr) + 1;
1065         i++;
1066     }
1067     flList->dwNumFiles = i;
1068
1069     return S_OK;
1070 }
1071
1072 /* free the FILE_LIST */
1073 static void destroy_file_list(FILE_LIST *flList)
1074 {
1075     DWORD i;
1076
1077     if (!flList || !flList->feFiles)
1078         return;
1079
1080     for (i = 0; i < flList->dwNumFiles; i++)
1081     {
1082         HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
1083         HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
1084         HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
1085     }
1086
1087     HeapFree(GetProcessHeap(), 0, flList->feFiles);
1088 }
1089
1090 static void copy_dir_to_dir(FILE_OPERATION *op, const FILE_ENTRY *feFrom, LPCWSTR szDestPath)
1091 {
1092     WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1093     SHFILEOPSTRUCTW fileOp;
1094
1095     static const WCHAR wildCardFiles[] = {'*','.','*',0};
1096
1097     if (IsDotDir(feFrom->szFilename))
1098         return;
1099
1100     if (PathFileExistsW(szDestPath))
1101         PathCombineW(szTo, szDestPath, feFrom->szFilename);
1102     else
1103         lstrcpyW(szTo, szDestPath);
1104
1105     if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo)) {
1106         if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FOLDER, feFrom->szFilename, op))
1107         {
1108             /* Vista returns an ERROR_CANCELLED even if user pressed "No" */
1109             if (!op->bManyItems)
1110                 op->bCancelled = TRUE;
1111             return;
1112         }
1113     }
1114
1115     szTo[lstrlenW(szTo) + 1] = '\0';
1116     SHNotifyCreateDirectoryW(szTo, NULL);
1117
1118     PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1119     szFrom[lstrlenW(szFrom) + 1] = '\0';
1120
1121     fileOp = *op->req;
1122     fileOp.pFrom = szFrom;
1123     fileOp.pTo = szTo;
1124     fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
1125
1126     /* Don't ask the user about overwriting files when he accepted to overwrite the
1127        folder. FIXME: this is not exactly what Windows does - e.g. there would be
1128        an additional confirmation for a nested folder */
1129     fileOp.fFlags |= FOF_NOCONFIRMATION;  
1130
1131     SHFileOperationW(&fileOp);
1132 }
1133
1134 static BOOL copy_file_to_file(FILE_OPERATION *op, const WCHAR *szFrom, const WCHAR *szTo)
1135 {
1136     if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo))
1137     {
1138         if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FILE, PathFindFileNameW(szTo), op))
1139             return 0;
1140     }
1141
1142     return SHNotifyCopyFileW(szFrom, szTo, FALSE) == 0;
1143 }
1144
1145 /* copy a file or directory to another directory */
1146 static void copy_to_dir(FILE_OPERATION *op, const FILE_ENTRY *feFrom, const FILE_ENTRY *feTo)
1147 {
1148     if (!PathFileExistsW(feTo->szFullPath))
1149         SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
1150
1151     if (IsAttribFile(feFrom->attributes))
1152     {
1153         WCHAR szDestPath[MAX_PATH];
1154
1155         PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1156         copy_file_to_file(op, feFrom->szFullPath, szDestPath);
1157     }
1158     else if (!(op->req->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1159         copy_dir_to_dir(op, feFrom, feTo->szFullPath);
1160 }
1161
1162 static void create_dest_dirs(LPCWSTR szDestDir)
1163 {
1164     WCHAR dir[MAX_PATH];
1165     LPCWSTR ptr = StrChrW(szDestDir, '\\');
1166
1167     /* make sure all directories up to last one are created */
1168     while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
1169     {
1170         lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
1171
1172         if (!PathFileExistsW(dir))
1173             SHNotifyCreateDirectoryW(dir, NULL);
1174     }
1175
1176     /* create last directory */
1177     if (!PathFileExistsW(szDestDir))
1178         SHNotifyCreateDirectoryW(szDestDir, NULL);
1179 }
1180
1181 /* the FO_COPY operation */
1182 static HRESULT copy_files(FILE_OPERATION *op, const FILE_LIST *flFrom, FILE_LIST *flTo)
1183 {
1184     DWORD i;
1185     const FILE_ENTRY *entryToCopy;
1186     const FILE_ENTRY *fileDest = &flTo->feFiles[0];
1187
1188     if (flFrom->bAnyDontExist)
1189         return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1190
1191     if (flTo->dwNumFiles == 0)
1192     {
1193         /* If the destination is empty, SHFileOperation should use the current directory */
1194         WCHAR curdir[MAX_PATH+1];
1195
1196         GetCurrentDirectoryW(MAX_PATH, curdir);
1197         curdir[lstrlenW(curdir)+1] = 0;
1198
1199         destroy_file_list(flTo);
1200         ZeroMemory(flTo, sizeof(FILE_LIST));
1201         parse_file_list(flTo, curdir);
1202         fileDest = &flTo->feFiles[0];
1203     }
1204
1205     if (op->req->fFlags & FOF_MULTIDESTFILES)
1206     {
1207         if (flFrom->bAnyFromWildcard)
1208             return ERROR_CANCELLED;
1209
1210         if (flFrom->dwNumFiles != flTo->dwNumFiles)
1211         {
1212             if (flFrom->dwNumFiles != 1 && !IsAttribDir(fileDest->attributes))
1213                 return ERROR_CANCELLED;
1214
1215             /* Free all but the first entry. */
1216             for (i = 1; i < flTo->dwNumFiles; i++)
1217             {
1218                 HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szDirectory);
1219                 HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szFilename);
1220                 HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szFullPath);
1221             }
1222
1223             flTo->dwNumFiles = 1;
1224         }
1225         else if (IsAttribDir(fileDest->attributes))
1226         {
1227             for (i = 1; i < flTo->dwNumFiles; i++)
1228                 if (!IsAttribDir(flTo->feFiles[i].attributes) ||
1229                     !IsAttribDir(flFrom->feFiles[i].attributes))
1230                 {
1231                     return ERROR_CANCELLED;
1232                 }
1233         }
1234     }
1235     else if (flFrom->dwNumFiles != 1)
1236     {
1237         if (flTo->dwNumFiles != 1 && !IsAttribDir(fileDest->attributes))
1238             return ERROR_CANCELLED;
1239
1240         if (PathFileExistsW(fileDest->szFullPath) &&
1241             IsAttribFile(fileDest->attributes))
1242         {
1243             return ERROR_CANCELLED;
1244         }
1245
1246         if (flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
1247             !PathFileExistsW(fileDest->szFullPath))
1248         {
1249             return ERROR_CANCELLED;
1250         }
1251     }
1252
1253     for (i = 0; i < flFrom->dwNumFiles; i++)
1254     {
1255         entryToCopy = &flFrom->feFiles[i];
1256
1257         if ((op->req->fFlags & FOF_MULTIDESTFILES) &&
1258             flTo->dwNumFiles > 1)
1259         {
1260             fileDest = &flTo->feFiles[i];
1261         }
1262
1263         if (IsAttribDir(entryToCopy->attributes) &&
1264             !lstrcmpiW(entryToCopy->szFullPath, fileDest->szDirectory))
1265         {
1266             return ERROR_SUCCESS;
1267         }
1268
1269         create_dest_dirs(fileDest->szDirectory);
1270
1271         if (!lstrcmpiW(entryToCopy->szFullPath, fileDest->szFullPath))
1272         {
1273             if (IsAttribFile(entryToCopy->attributes))
1274                 return ERROR_NO_MORE_SEARCH_HANDLES;
1275             else
1276                 return ERROR_SUCCESS;
1277         }
1278
1279         if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1280             IsAttribDir(fileDest->attributes))
1281         {
1282             copy_to_dir(op, entryToCopy, fileDest);
1283         }
1284         else if (IsAttribDir(entryToCopy->attributes))
1285         {
1286             copy_dir_to_dir(op, entryToCopy, fileDest->szFullPath);
1287         }
1288         else
1289         {
1290             if (!copy_file_to_file(op, entryToCopy->szFullPath, fileDest->szFullPath))
1291             {
1292                 op->req->fAnyOperationsAborted = TRUE;
1293                 return ERROR_CANCELLED;
1294             }
1295         }
1296
1297         /* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
1298         if (op->bCancelled)
1299             return ERROR_CANCELLED;
1300     }
1301
1302     /* Vista return code. On XP if the used pressed "No" for the last item,
1303      * ERROR_ARENA_TRASHED would be returned */
1304     return ERROR_SUCCESS;
1305 }
1306
1307 static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, BOOL fTrash, const FILE_LIST *flFrom)
1308 {
1309     if (flFrom->dwNumFiles > 1)
1310     {
1311         WCHAR tmp[8];
1312         const WCHAR format[] = {'%','d',0};
1313
1314         wnsprintfW(tmp, sizeof(tmp)/sizeof(tmp[0]), format, flFrom->dwNumFiles);
1315         return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_MULTIPLE_ITEM:ASK_DELETE_MULTIPLE_ITEM), tmp, NULL);
1316     }
1317     else
1318     {
1319         const FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
1320
1321         if (IsAttribFile(fileEntry->attributes))
1322             return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FILE:ASK_DELETE_FILE), fileEntry->szFullPath, NULL);
1323         else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1324             return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FOLDER:ASK_DELETE_FOLDER), fileEntry->szFullPath, NULL);
1325     }
1326     return TRUE;
1327 }
1328
1329 /* the FO_DELETE operation */
1330 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom)
1331 {
1332     const FILE_ENTRY *fileEntry;
1333     DWORD i;
1334     BOOL bPathExists;
1335     BOOL bTrash;
1336
1337     if (!flFrom->dwNumFiles)
1338         return ERROR_SUCCESS;
1339
1340     /* Windows also checks only the first item */
1341     bTrash = (lpFileOp->fFlags & FOF_ALLOWUNDO)
1342         && TRASH_CanTrashFile(flFrom->feFiles[0].szFullPath);
1343
1344     if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (!bTrash && lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1345         if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, bTrash, flFrom))
1346         {
1347             lpFileOp->fAnyOperationsAborted = TRUE;
1348             return 0;
1349         }
1350
1351     for (i = 0; i < flFrom->dwNumFiles; i++)
1352     {
1353         bPathExists = TRUE;
1354         fileEntry = &flFrom->feFiles[i];
1355
1356         if (!IsAttribFile(fileEntry->attributes) &&
1357             (lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1358             continue;
1359
1360         if (bTrash)
1361         {
1362             BOOL bDelete;
1363             if (TRASH_TrashFile(fileEntry->szFullPath))
1364                 continue;
1365
1366             /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1367             if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1368                 bDelete = SHELL_ConfirmDialogW(lpFileOp->hwnd, ASK_CANT_TRASH_ITEM, fileEntry->szFullPath, NULL);
1369             else
1370                 bDelete = TRUE;
1371
1372             if (!bDelete)
1373             {
1374                 lpFileOp->fAnyOperationsAborted = TRUE;
1375                 break;
1376             }
1377         }
1378         
1379         /* delete the file or directory */
1380         if (IsAttribFile(fileEntry->attributes))
1381             bPathExists = DeleteFileW(fileEntry->szFullPath);
1382         else
1383             bPathExists = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1384
1385         if (!bPathExists)
1386             return ERROR_PATH_NOT_FOUND;
1387     }
1388
1389     return ERROR_SUCCESS;
1390 }
1391
1392 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, const FILE_ENTRY *feFrom, LPCWSTR szDestPath)
1393 {
1394     WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1395     SHFILEOPSTRUCTW fileOp;
1396
1397     static const WCHAR wildCardFiles[] = {'*','.','*',0};
1398
1399     if (IsDotDir(feFrom->szFilename))
1400         return;
1401
1402     SHNotifyCreateDirectoryW(szDestPath, NULL);
1403
1404     PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1405     szFrom[lstrlenW(szFrom) + 1] = '\0';
1406
1407     lstrcpyW(szTo, szDestPath);
1408     szTo[lstrlenW(szDestPath) + 1] = '\0';
1409
1410     fileOp = *lpFileOp;
1411     fileOp.pFrom = szFrom;
1412     fileOp.pTo = szTo;
1413
1414     SHFileOperationW(&fileOp);
1415 }
1416
1417 /* moves a file or directory to another directory */
1418 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, const FILE_ENTRY *feFrom, const FILE_ENTRY *feTo)
1419 {
1420     WCHAR szDestPath[MAX_PATH];
1421
1422     PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1423
1424     if (IsAttribFile(feFrom->attributes))
1425         SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
1426     else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1427         move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1428 }
1429
1430 /* the FO_MOVE operation */
1431 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
1432 {
1433     DWORD i;
1434     const FILE_ENTRY *entryToMove;
1435     const FILE_ENTRY *fileDest;
1436
1437     if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1438         return ERROR_CANCELLED;
1439
1440     if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1441         flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1442     {
1443         return ERROR_CANCELLED;
1444     }
1445
1446     if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1447         !flFrom->bAnyDirectories &&
1448         flFrom->dwNumFiles > flTo->dwNumFiles)
1449     {
1450         return ERROR_CANCELLED;
1451     }
1452
1453     if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1454         return ERROR_CANCELLED;
1455
1456     if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1457         flFrom->dwNumFiles != flTo->dwNumFiles)
1458     {
1459         return ERROR_CANCELLED;
1460     }
1461
1462     fileDest = &flTo->feFiles[0];
1463     for (i = 0; i < flFrom->dwNumFiles; i++)
1464     {
1465         entryToMove = &flFrom->feFiles[i];
1466
1467         if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1468             fileDest = &flTo->feFiles[i];
1469
1470         if (!PathFileExistsW(fileDest->szDirectory))
1471             return ERROR_CANCELLED;
1472
1473         if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1474             move_to_dir(lpFileOp, entryToMove, fileDest);
1475         else
1476             SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
1477     }
1478
1479     return ERROR_SUCCESS;
1480 }
1481
1482 /* the FO_RENAME files */
1483 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
1484 {
1485     const FILE_ENTRY *feFrom;
1486     const FILE_ENTRY *feTo;
1487
1488     if (flFrom->dwNumFiles != 1)
1489         return ERROR_GEN_FAILURE;
1490
1491     if (flTo->dwNumFiles != 1)
1492         return ERROR_CANCELLED;
1493
1494     feFrom = &flFrom->feFiles[0];
1495     feTo= &flTo->feFiles[0];
1496
1497     /* fail if destination doesn't exist */
1498     if (!feFrom->bExists)
1499         return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1500
1501     /* fail if destination already exists */
1502     if (feTo->bExists)
1503         return ERROR_ALREADY_EXISTS;
1504
1505     return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
1506 }
1507
1508 /* alert the user if an unsupported flag is used */
1509 static void check_flags(FILEOP_FLAGS fFlags)
1510 {
1511     WORD wUnsupportedFlags = FOF_NO_CONNECTED_ELEMENTS |
1512         FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1513         FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1514
1515     if (fFlags & wUnsupportedFlags)
1516         FIXME("Unsupported flags: %04x\n", fFlags);
1517 }
1518
1519 /*************************************************************************
1520  * SHFileOperationW          [SHELL32.@]
1521  *
1522  * See SHFileOperationA
1523  */
1524 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1525 {
1526     FILE_OPERATION op;
1527     FILE_LIST flFrom, flTo;
1528     int ret = 0;
1529
1530     if (!lpFileOp)
1531         return ERROR_INVALID_PARAMETER;
1532
1533     check_flags(lpFileOp->fFlags);
1534
1535     ZeroMemory(&flFrom, sizeof(FILE_LIST));
1536     ZeroMemory(&flTo, sizeof(FILE_LIST));
1537
1538     if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1539         return ret;
1540
1541     if (lpFileOp->wFunc != FO_DELETE)
1542         parse_file_list(&flTo, lpFileOp->pTo);
1543
1544     ZeroMemory(&op, sizeof(op));
1545     op.req = lpFileOp;
1546     op.bManyItems = (flFrom.dwNumFiles > 1);
1547
1548     switch (lpFileOp->wFunc)
1549     {
1550         case FO_COPY:
1551             ret = copy_files(&op, &flFrom, &flTo);
1552             break;
1553         case FO_DELETE:
1554             ret = delete_files(lpFileOp, &flFrom);
1555             break;
1556         case FO_MOVE:
1557             ret = move_files(lpFileOp, &flFrom, &flTo);
1558             break;
1559         case FO_RENAME:
1560             ret = rename_files(lpFileOp, &flFrom, &flTo);
1561             break;
1562         default:
1563             ret = ERROR_INVALID_PARAMETER;
1564             break;
1565     }
1566
1567     destroy_file_list(&flFrom);
1568
1569     if (lpFileOp->wFunc != FO_DELETE)
1570         destroy_file_list(&flTo);
1571
1572     if (ret == ERROR_CANCELLED)
1573         lpFileOp->fAnyOperationsAborted = TRUE;
1574
1575     return ret;
1576 }
1577
1578 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1579
1580 /*************************************************************************
1581  * SHFreeNameMappings      [shell32.246]
1582  *
1583  * Free the mapping handle returned by SHFileOperation if FOF_WANTSMAPPINGHANDLE
1584  * was specified.
1585  *
1586  * PARAMS
1587  *  hNameMapping [I] handle to the name mappings used during renaming of files
1588  *
1589  * RETURNS
1590  *  Nothing
1591  */
1592 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1593 {
1594         if (hNameMapping)
1595         {
1596           int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1597
1598           for (; i>= 0; i--)
1599           {
1600             LPSHNAMEMAPPINGW lp = DSA_GetItemPtr(hNameMapping, i);
1601
1602             SHFree(lp->pszOldPath);
1603             SHFree(lp->pszNewPath);
1604           }
1605           DSA_Destroy(hNameMapping);
1606         }
1607 }
1608
1609 /*************************************************************************
1610  * SheGetDirA [SHELL32.@]
1611  *
1612  * drive = 0: returns the current directory path
1613  * drive > 0: returns the current directory path of the specified drive
1614  *            drive=1 -> A:  drive=2 -> B:  ...
1615  * returns 0 if successful
1616 */
1617 DWORD WINAPI SheGetDirA(DWORD drive, LPSTR buffer)
1618 {
1619     WCHAR org_path[MAX_PATH];
1620     DWORD ret;
1621     char drv_path[3];
1622
1623     /* change current directory to the specified drive */
1624     if (drive) {
1625         strcpy(drv_path, "A:");
1626         drv_path[0] += (char)drive-1;
1627
1628         GetCurrentDirectoryW(MAX_PATH, org_path);
1629
1630         SetCurrentDirectoryA(drv_path);
1631     }
1632
1633     /* query current directory path of the specified drive */
1634     ret = GetCurrentDirectoryA(MAX_PATH, buffer);
1635
1636     /* back to the original drive */
1637     if (drive)
1638         SetCurrentDirectoryW(org_path);
1639
1640     if (!ret)
1641         return GetLastError();
1642
1643     return 0;
1644 }
1645
1646 /*************************************************************************
1647  * SheGetDirW [SHELL32.@]
1648  *
1649  * drive = 0: returns the current directory path
1650  * drive > 0: returns the current directory path of the specified drive
1651  *            drive=1 -> A:  drive=2 -> B:  ...
1652  * returns 0 if successful
1653  */
1654 DWORD WINAPI SheGetDirW(DWORD drive, LPWSTR buffer)
1655 {
1656     WCHAR org_path[MAX_PATH];
1657     DWORD ret;
1658     char drv_path[3];
1659
1660     /* change current directory to the specified drive */
1661     if (drive) {
1662         strcpy(drv_path, "A:");
1663         drv_path[0] += (char)drive-1;
1664
1665         GetCurrentDirectoryW(MAX_PATH, org_path);
1666
1667         SetCurrentDirectoryA(drv_path);
1668     }
1669
1670     /* query current directory path of the specified drive */
1671     ret = GetCurrentDirectoryW(MAX_PATH, buffer);
1672
1673     /* back to the original drive */
1674     if (drive)
1675         SetCurrentDirectoryW(org_path);
1676
1677     if (!ret)
1678         return GetLastError();
1679
1680     return 0;
1681 }
1682
1683 /*************************************************************************
1684  * SheChangeDirA [SHELL32.@]
1685  *
1686  * changes the current directory to the specified path
1687  * and returns 0 if successful
1688  */
1689 DWORD WINAPI SheChangeDirA(LPSTR path)
1690 {
1691     if (SetCurrentDirectoryA(path))
1692         return 0;
1693     else
1694         return GetLastError();
1695 }
1696
1697 /*************************************************************************
1698  * SheChangeDirW [SHELL32.@]
1699  *
1700  * changes the current directory to the specified path
1701  * and returns 0 if successful
1702  */
1703 DWORD WINAPI SheChangeDirW(LPWSTR path)
1704 {
1705     if (SetCurrentDirectoryW(path))
1706         return 0;
1707     else
1708         return GetLastError();
1709 }
1710
1711 /*************************************************************************
1712  * IsNetDrive                   [SHELL32.66]
1713  */
1714 BOOL WINAPI IsNetDrive(DWORD drive)
1715 {
1716         char root[4];
1717         strcpy(root, "A:\\");
1718         root[0] += (char)drive;
1719         return (GetDriveTypeA(root) == DRIVE_REMOTE);
1720 }
1721
1722
1723 /*************************************************************************
1724  * RealDriveType                [SHELL32.524]
1725  */
1726 INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1727 {
1728     char root[] = "A:\\";
1729     root[0] += (char)drive;
1730     return GetDriveTypeA(root);
1731 }
1732
1733 /***********************************************************************
1734  *              SHPathPrepareForWriteA (SHELL32.@)
1735  */
1736 HRESULT WINAPI SHPathPrepareForWriteA(HWND hwnd, IUnknown *modless, LPCSTR path, DWORD flags)
1737 {
1738     WCHAR wpath[MAX_PATH];
1739     MultiByteToWideChar( CP_ACP, 0, path, -1, wpath, MAX_PATH);
1740     return SHPathPrepareForWriteW(hwnd, modless, wpath, flags);
1741 }
1742
1743 /***********************************************************************
1744  *              SHPathPrepareForWriteW (SHELL32.@)
1745  */
1746 HRESULT WINAPI SHPathPrepareForWriteW(HWND hwnd, IUnknown *modless, LPCWSTR path, DWORD flags)
1747 {
1748     DWORD res;
1749     DWORD err;
1750     LPCWSTR realpath;
1751     int len;
1752     WCHAR* last_slash;
1753     WCHAR* temppath=NULL;
1754
1755     TRACE("%p %p %s 0x%80x\n", hwnd, modless, debugstr_w(path), flags);
1756
1757     if (flags & ~(SHPPFW_DIRCREATE|SHPPFW_ASKDIRCREATE|SHPPFW_IGNOREFILENAME))
1758         FIXME("unimplemented flags 0x%08x\n", flags);
1759
1760     /* cut off filename if necessary */
1761     if (flags & SHPPFW_IGNOREFILENAME)
1762     {
1763         last_slash = StrRChrW(path, NULL, '\\');
1764         if (last_slash == NULL)
1765             len = 1;
1766         else
1767             len = last_slash - path + 1;
1768         temppath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1769         if (!temppath)
1770             return E_OUTOFMEMORY;
1771         StrCpyNW(temppath, path, len);
1772         realpath = temppath;
1773     }
1774     else
1775     {
1776         realpath = path;
1777     }
1778
1779     /* try to create the directory if asked to */
1780     if (flags & (SHPPFW_DIRCREATE|SHPPFW_ASKDIRCREATE))
1781     {
1782         if (flags & SHPPFW_ASKDIRCREATE)
1783             FIXME("treating SHPPFW_ASKDIRCREATE as SHPPFW_DIRCREATE\n");
1784
1785         SHCreateDirectoryExW(0, realpath, NULL);
1786     }
1787
1788     /* check if we can access the directory */
1789     res = GetFileAttributesW(realpath);
1790
1791     HeapFree(GetProcessHeap(), 0, temppath);
1792
1793     if (res == INVALID_FILE_ATTRIBUTES)
1794     {
1795         err = GetLastError();
1796         if (err == ERROR_FILE_NOT_FOUND)
1797             return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
1798         return HRESULT_FROM_WIN32(err);
1799     }
1800     else if (res & FILE_ATTRIBUTE_DIRECTORY)
1801         return S_OK;
1802     else
1803         return HRESULT_FROM_WIN32(ERROR_DIRECTORY);
1804 }