shell32: Add a confirmation dialog with a "Yes to All" option.
[wine/winequartzdrv.git] / dlls / shell32 / shlfileop.c
blob0a64531b7d98734140ec1fc48b19b78c24c9b10e
1 /*
2 * SHFileOperation
4 * Copyright 2000 Juergen Schmied
5 * Copyright 2002 Andriy Palamarchuk
6 * Copyright 2004 Dietrich Teickner (from Odin)
7 * Copyright 2004 Rolf Kalbermatter
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.
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.
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
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
28 #include <string.h>
29 #include <ctype.h>
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/debug.h"
44 #include "xdg.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(shell);
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))))
53 #define FO_MASK 0xF
55 static const WCHAR wWildcardFile[] = {'*',0};
56 static const WCHAR wWildcardChars[] = {'*','?',0};
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);
68 /* Confirm dialogs with an optional "Yes To All" as used in file operations confirmations
70 static const WCHAR CONFIRM_MSG_PROP[] = {'W','I','N','E','_','C','O','N','F','I','R','M',0};
72 struct confirm_msg_info
74 LPWSTR lpszText;
75 LPWSTR lpszCaption;
76 HICON hIcon;
77 BOOL bYesToAll;
80 /* as some buttons may be hidden and the dialog height may change we may need
81 * to move the controls */
82 static void confirm_msg_move_button(HWND hDlg, INT iId, INT *xPos, INT yOffset, BOOL bShow)
84 HWND hButton = GetDlgItem(hDlg, iId);
85 RECT r;
87 if (bShow) {
88 POINT pt;
89 int width;
91 GetWindowRect(hButton, &r);
92 width = r.right - r.left;
93 pt.x = r.left;
94 pt.y = r.top;
95 ScreenToClient(hDlg, &pt);
96 MoveWindow(hButton, *xPos - width, pt.y - yOffset, width, r.bottom - r.top, FALSE);
97 *xPos -= width + 5;
99 else
100 ShowWindow(hButton, SW_HIDE);
103 /* Note: we paint the text manually and don't use the static control to make
104 * sure the text has the same height as the one computed in WM_INITDIALOG
106 static INT_PTR CALLBACK ConfirmMsgBox_Paint(HWND hDlg)
108 PAINTSTRUCT ps;
109 HFONT hOldFont;
110 RECT r;
111 HDC hdc;
113 BeginPaint(hDlg, &ps);
114 hdc = ps.hdc;
116 GetClientRect(GetDlgItem(hDlg, IDD_MESSAGE), &r);
117 /* this will remap the rect to dialog coords */
118 MapWindowPoints(GetDlgItem(hDlg, IDD_MESSAGE), hDlg, (LPPOINT)&r, 2);
119 hOldFont = SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDD_MESSAGE, WM_GETFONT, 0, 0));
120 DrawTextW(hdc, (LPWSTR)GetPropW(hDlg, CONFIRM_MSG_PROP), -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK);
121 SelectObject(hdc, hOldFont);
122 EndPaint(hDlg, &ps);
123 return TRUE;
126 static INT_PTR CALLBACK ConfirmMsgBox_Init(HWND hDlg, LPARAM lParam)
128 struct confirm_msg_info *info = (struct confirm_msg_info *)lParam;
129 INT xPos, yOffset;
130 int width, height;
131 HFONT hOldFont;
132 HDC hdc;
133 RECT r;
135 SetWindowTextW(hDlg, info->lpszCaption);
136 ShowWindow(GetDlgItem(hDlg, IDD_MESSAGE), SW_HIDE);
137 SetPropW(hDlg, CONFIRM_MSG_PROP, (HANDLE)info->lpszText);
138 SendDlgItemMessageW(hDlg, IDD_ICON, STM_SETICON, (WPARAM)info->hIcon, 0);
140 /* compute the text height and resize the dialog */
141 GetClientRect(GetDlgItem(hDlg, IDD_MESSAGE), &r);
142 hdc = GetDC(hDlg);
143 yOffset = r.bottom;
144 hOldFont = SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDD_MESSAGE, WM_GETFONT, 0, 0));
145 DrawTextW(hdc, info->lpszText, -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK | DT_CALCRECT);
146 SelectObject(hdc, hOldFont);
147 yOffset -= r.bottom;
148 yOffset = min(yOffset, 35); /* don't make the dialog too small */
149 ReleaseDC(hDlg, hdc);
151 GetClientRect(hDlg, &r);
152 xPos = r.right - 7;
153 GetWindowRect(hDlg, &r);
154 width = r.right - r.left;
155 height = r.bottom - r.top - yOffset;
156 MoveWindow(hDlg, (GetSystemMetrics(SM_CXSCREEN) - width)/2,
157 (GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
159 confirm_msg_move_button(hDlg, IDCANCEL, &xPos, yOffset, info->bYesToAll);
160 confirm_msg_move_button(hDlg, IDNO, &xPos, yOffset, TRUE);
161 confirm_msg_move_button(hDlg, IDD_YESTOALL, &xPos, yOffset, info->bYesToAll);
162 confirm_msg_move_button(hDlg, IDYES, &xPos, yOffset, TRUE);
163 return TRUE;
166 static INT_PTR CALLBACK ConfirmMsgBoxProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
168 switch (uMsg)
170 case WM_INITDIALOG:
171 return ConfirmMsgBox_Init(hDlg, lParam);
172 case WM_PAINT:
173 return ConfirmMsgBox_Paint(hDlg);
174 case WM_COMMAND:
175 EndDialog(hDlg, wParam);
176 break;
177 case WM_CLOSE:
178 EndDialog(hDlg, IDCANCEL);
179 break;
181 return FALSE;
184 static int SHELL_ConfirmMsgBox(HWND hWnd, LPWSTR lpszText, LPWSTR lpszCaption, HICON hIcon, BOOL bYesToAll)
186 static const WCHAR wszTemplate[] = {'S','H','E','L','L','_','Y','E','S','T','O','A','L','L','_','M','S','G','B','O','X',0};
187 struct confirm_msg_info info;
189 info.lpszText = lpszText;
190 info.lpszCaption = lpszCaption;
191 info.hIcon = hIcon;
192 info.bYesToAll = bYesToAll;
193 return DialogBoxParamW(shell32_hInstance, wszTemplate, hWnd, ConfirmMsgBoxProc, (LPARAM)&info);
196 /* confirmation dialogs content */
197 typedef struct
199 HINSTANCE hIconInstance;
200 UINT icon_resource_id;
201 UINT caption_resource_id, text_resource_id;
202 } SHELL_ConfirmIDstruc;
204 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
206 ids->hIconInstance = shell32_hInstance;
207 switch (nKindOfDialog) {
208 case ASK_DELETE_FILE:
209 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
210 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
211 ids->text_resource_id = IDS_DELETEITEM_TEXT;
212 return TRUE;
213 case ASK_DELETE_FOLDER:
214 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
215 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
216 ids->text_resource_id = IDS_DELETEITEM_TEXT;
217 return TRUE;
218 case ASK_DELETE_MULTIPLE_ITEM:
219 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
220 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
221 ids->text_resource_id = IDS_DELETEMULTIPLE_TEXT;
222 return TRUE;
223 case ASK_TRASH_FILE:
224 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
225 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
226 ids->text_resource_id = IDS_TRASHITEM_TEXT;
227 return TRUE;
228 case ASK_TRASH_FOLDER:
229 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
230 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
231 ids->text_resource_id = IDS_TRASHFOLDER_TEXT;
232 return TRUE;
233 case ASK_TRASH_MULTIPLE_ITEM:
234 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
235 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
236 ids->text_resource_id = IDS_TRASHMULTIPLE_TEXT;
237 return TRUE;
238 case ASK_CANT_TRASH_ITEM:
239 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
240 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
241 ids->text_resource_id = IDS_CANTTRASH_TEXT;
242 return TRUE;
243 case ASK_DELETE_SELECTED:
244 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
245 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
246 ids->text_resource_id = IDS_DELETESELECTED_TEXT;
247 return TRUE;
248 case ASK_OVERWRITE_FILE:
249 ids->hIconInstance = NULL;
250 ids->icon_resource_id = IDI_WARNING;
251 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
252 ids->text_resource_id = IDS_OVERWRITEFILE_TEXT;
253 return TRUE;
254 default:
255 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
257 return FALSE;
260 BOOL SHELL_ConfirmDialogW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir)
262 WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
263 SHELL_ConfirmIDstruc ids;
264 DWORD_PTR args[1];
265 HICON hIcon;
267 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
268 return FALSE;
270 LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption)/sizeof(WCHAR));
271 LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText)/sizeof(WCHAR));
273 args[0] = (DWORD_PTR)szDir;
274 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
275 szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)args);
276 hIcon = LoadIconW(ids.hIconInstance, (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id));
278 return (IDYES == SHELL_ConfirmMsgBox(hWnd, szBuffer, szCaption, hIcon, FALSE));
281 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
283 DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
285 if (len < minChars)
286 len = minChars;
288 *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
289 if (*wPath)
291 MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
292 return NO_ERROR;
294 return E_OUTOFMEMORY;
297 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
299 HeapFree(GetProcessHeap(), 0, wPath);
302 HRESULT WINAPI SHIsFileAvailableOffline(LPCWSTR path, LPDWORD status)
304 FIXME("(%s, %p) stub\n", debugstr_w(path), status);
305 return E_FAIL;
308 /**************************************************************************
309 * SHELL_DeleteDirectory() [internal]
311 * Asks for confirmation when bShowUI is true and deletes the directory and
312 * all its subdirectories and files if necessary.
314 BOOL SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
316 BOOL ret = TRUE;
317 HANDLE hFind;
318 WIN32_FIND_DATAW wfd;
319 WCHAR szTemp[MAX_PATH];
321 /* Make sure the directory exists before eventually prompting the user */
322 PathCombineW(szTemp, pszDir, wWildcardFile);
323 hFind = FindFirstFileW(szTemp, &wfd);
324 if (hFind == INVALID_HANDLE_VALUE)
325 return FALSE;
327 if (!bShowUI || (ret = SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir)))
331 if (IsDotDir(wfd.cFileName))
332 continue;
333 PathCombineW(szTemp, pszDir, wfd.cFileName);
334 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
335 ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
336 else
337 ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
338 } while (ret && FindNextFileW(hFind, &wfd));
340 FindClose(hFind);
341 if (ret)
342 ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
343 return ret;
346 /**************************************************************************
347 * Win32CreateDirectory [SHELL32.93]
349 * Creates a directory. Also triggers a change notify if one exists.
351 * PARAMS
352 * path [I] path to directory to create
354 * RETURNS
355 * TRUE if successful, FALSE otherwise
357 * NOTES
358 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
359 * This is Unicode on NT/2000
361 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
363 LPWSTR wPath;
364 DWORD retCode;
366 TRACE("(%s, %p)\n", debugstr_a(path), sec);
368 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
369 if (!retCode)
371 retCode = SHNotifyCreateDirectoryW(wPath, sec);
372 SHELL32_FreeUnicodeBuf(wPath);
374 return retCode;
377 /**********************************************************************/
379 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
381 TRACE("(%s, %p)\n", debugstr_w(path), sec);
383 if (CreateDirectoryW(path, sec))
385 SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
386 return ERROR_SUCCESS;
388 return GetLastError();
391 /**********************************************************************/
393 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
395 if (SHELL_OsIsUnicode())
396 return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
397 return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
400 /************************************************************************
401 * Win32RemoveDirectory [SHELL32.94]
403 * Deletes a directory. Also triggers a change notify if one exists.
405 * PARAMS
406 * path [I] path to directory to delete
408 * RETURNS
409 * TRUE if successful, FALSE otherwise
411 * NOTES
412 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
413 * This is Unicode on NT/2000
415 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
417 LPWSTR wPath;
418 DWORD retCode;
420 TRACE("(%s)\n", debugstr_a(path));
422 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
423 if (!retCode)
425 retCode = SHNotifyRemoveDirectoryW(wPath);
426 SHELL32_FreeUnicodeBuf(wPath);
428 return retCode;
431 /***********************************************************************/
433 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
435 BOOL ret;
436 TRACE("(%s)\n", debugstr_w(path));
438 ret = RemoveDirectoryW(path);
439 if (!ret)
441 /* Directory may be write protected */
442 DWORD dwAttr = GetFileAttributesW(path);
443 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
444 if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
445 ret = RemoveDirectoryW(path);
447 if (ret)
449 SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
450 return ERROR_SUCCESS;
452 return GetLastError();
455 /***********************************************************************/
457 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
459 if (SHELL_OsIsUnicode())
460 return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
461 return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
464 /************************************************************************
465 * Win32DeleteFile [SHELL32.164]
467 * Deletes a file. Also triggers a change notify if one exists.
469 * PARAMS
470 * path [I] path to file to delete
472 * RETURNS
473 * TRUE if successful, FALSE otherwise
475 * NOTES
476 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
477 * This is Unicode on NT/2000
479 static DWORD SHNotifyDeleteFileA(LPCSTR path)
481 LPWSTR wPath;
482 DWORD retCode;
484 TRACE("(%s)\n", debugstr_a(path));
486 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
487 if (!retCode)
489 retCode = SHNotifyDeleteFileW(wPath);
490 SHELL32_FreeUnicodeBuf(wPath);
492 return retCode;
495 /***********************************************************************/
497 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
499 BOOL ret;
501 TRACE("(%s)\n", debugstr_w(path));
503 ret = DeleteFileW(path);
504 if (!ret)
506 /* File may be write protected or a system file */
507 DWORD dwAttr = GetFileAttributesW(path);
508 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
509 if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
510 ret = DeleteFileW(path);
512 if (ret)
514 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
515 return ERROR_SUCCESS;
517 return GetLastError();
520 /***********************************************************************/
522 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
524 if (SHELL_OsIsUnicode())
525 return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
526 return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
529 /************************************************************************
530 * SHNotifyMoveFile [internal]
532 * Moves a file. Also triggers a change notify if one exists.
534 * PARAMS
535 * src [I] path to source file to move
536 * dest [I] path to target file to move to
538 * RETURNS
539 * ERORR_SUCCESS if successful
541 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
543 BOOL ret;
545 TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
547 ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
549 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
550 if (!ret)
551 ret = MoveFileW(src, dest);
553 if (!ret)
555 DWORD dwAttr;
557 dwAttr = SHFindAttrW(dest, FALSE);
558 if (INVALID_FILE_ATTRIBUTES == dwAttr)
560 /* Source file may be write protected or a system file */
561 dwAttr = GetFileAttributesW(src);
562 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
563 if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
564 ret = MoveFileW(src, dest);
567 if (ret)
569 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
570 return ERROR_SUCCESS;
572 return GetLastError();
575 /************************************************************************
576 * SHNotifyCopyFile [internal]
578 * Copies a file. Also triggers a change notify if one exists.
580 * PARAMS
581 * src [I] path to source file to move
582 * dest [I] path to target file to move to
583 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
584 * a file with this name already exists
586 * RETURNS
587 * ERROR_SUCCESS if successful
589 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
591 BOOL ret;
593 TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
595 ret = CopyFileW(src, dest, bFailIfExists);
596 if (ret)
598 SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
599 return ERROR_SUCCESS;
602 return GetLastError();
605 /*************************************************************************
606 * SHCreateDirectory [SHELL32.165]
608 * This function creates a file system folder whose fully qualified path is
609 * given by path. If one or more of the intermediate folders do not exist,
610 * they will be created as well.
612 * PARAMS
613 * hWnd [I]
614 * path [I] path of directory to create
616 * RETURNS
617 * ERRROR_SUCCESS or one of the following values:
618 * ERROR_BAD_PATHNAME if the path is relative
619 * ERROR_FILE_EXISTS when a file with that name exists
620 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
621 * ERROR_INVLID_NAME if the path contains invalid chars
622 * ERROR_ALREADY_EXISTS when the directory already exists
623 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
625 * NOTES
626 * exported by ordinal
627 * Win9x exports ANSI
628 * WinNT/2000 exports Unicode
630 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
632 if (SHELL_OsIsUnicode())
633 return SHCreateDirectoryExW(hWnd, path, NULL);
634 return SHCreateDirectoryExA(hWnd, path, NULL);
637 /*************************************************************************
638 * SHCreateDirectoryExA [SHELL32.@]
640 * This function creates a file system folder whose fully qualified path is
641 * given by path. If one or more of the intermediate folders do not exist,
642 * they will be created as well.
644 * PARAMS
645 * hWnd [I]
646 * path [I] path of directory to create
647 * sec [I] security attributes to use or NULL
649 * RETURNS
650 * ERRROR_SUCCESS or one of the following values:
651 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
652 * ERROR_INVLID_NAME if the path contains invalid chars
653 * ERROR_FILE_EXISTS when a file with that name exists
654 * ERROR_ALREADY_EXISTS when the directory already exists
655 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
657 * FIXME: Not implemented yet;
658 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
659 * if the path is a network path to deal with network drivers which might have a limited
660 * but unknown maximum path length. If not:
662 * If hWnd is set to a valid window handle, a message box is displayed warning
663 * the user that the files may not be accessible. If the user chooses not to
664 * proceed, the function returns ERROR_CANCELLED.
666 * If hWnd is set to NULL, no user interface is displayed and the function
667 * returns ERROR_CANCELLED.
669 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
671 LPWSTR wPath;
672 DWORD retCode;
674 TRACE("(%s, %p)\n", debugstr_a(path), sec);
676 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
677 if (!retCode)
679 retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
680 SHELL32_FreeUnicodeBuf(wPath);
682 return retCode;
685 /*************************************************************************
686 * SHCreateDirectoryExW [SHELL32.@]
688 * See SHCreateDirectoryExA.
690 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
692 int ret = ERROR_BAD_PATHNAME;
693 TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
695 if (PathIsRelativeW(path))
697 SetLastError(ret);
699 else
701 ret = SHNotifyCreateDirectoryW(path, sec);
702 /* Refuse to work on certain error codes before trying to create directories recursively */
703 if (ret != ERROR_SUCCESS &&
704 ret != ERROR_FILE_EXISTS &&
705 ret != ERROR_ALREADY_EXISTS &&
706 ret != ERROR_FILENAME_EXCED_RANGE)
708 WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1]; /* extra for PathAddBackslash() */
710 lstrcpynW(szTemp, path, MAX_PATH);
711 pEnd = PathAddBackslashW(szTemp);
712 pSlash = szTemp + 3;
714 while (*pSlash)
716 while (*pSlash && *pSlash != '\\')
717 pSlash = CharNextW(pSlash);
719 if (*pSlash)
721 *pSlash = 0; /* terminate path at separator */
723 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
725 *pSlash++ = '\\'; /* put the separator back */
729 if (ret && hWnd && (ERROR_CANCELLED != ret))
731 /* We failed and should show a dialog box */
732 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
733 ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
736 return ret;
739 /*************************************************************************
740 * SHFindAttrW [internal]
742 * Get the Attributes for a file or directory. The difference to GetAttributes()
743 * is that this function will also work for paths containing wildcard characters
744 * in its filename.
746 * PARAMS
747 * path [I] path of directory or file to check
748 * fileOnly [I] TRUE if only files should be found
750 * RETURNS
751 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
752 * the first file or directory found otherwise
754 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
756 WIN32_FIND_DATAW wfd;
757 BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
758 DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
759 HANDLE hFind = FindFirstFileW(pName, &wfd);
761 TRACE("%s %d\n", debugstr_w(pName), fileOnly);
762 if (INVALID_HANDLE_VALUE != hFind)
766 if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
767 continue;
768 dwAttr = wfd.dwFileAttributes;
769 break;
771 while (FindNextFileW(hFind, &wfd));
772 FindClose(hFind);
774 return dwAttr;
777 /*************************************************************************
779 * SHNameTranslate HelperFunction for SHFileOperationA
781 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
782 * is NULL, only the necessary size of the string is determined and returned,
783 * otherwise the ASCII strings are copied into it and the buffer is increased
784 * to point to the location after the final 0 termination char.
786 static DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
788 DWORD size = 0, aSize = 0;
789 LPCSTR aString = (LPCSTR)*pWToFrom;
791 if (aString)
795 size = lstrlenA(aString) + 1;
796 aSize += size;
797 aString += size;
798 } while ((size != 1) && more);
799 /* The two sizes might be different in the case of multibyte chars */
800 size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
801 if (*wString) /* only in the second loop */
803 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
804 *pWToFrom = *wString;
805 *wString += size;
808 return size;
810 /*************************************************************************
811 * SHFileOperationA [SHELL32.@]
813 * Function to copy, move, delete and create one or more files with optional
814 * user prompts.
816 * PARAMS
817 * lpFileOp [I/O] pointer to a structure containing all the necessary information
819 * RETURNS
820 * Success: ERROR_SUCCESS.
821 * Failure: ERROR_CANCELLED.
823 * NOTES
824 * exported by name
826 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
828 SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
829 int retCode = 0;
830 DWORD size;
831 LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
832 wString = NULL; /* we change this in SHNameTranslate */
834 TRACE("\n");
835 if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
836 nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
837 if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
838 nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
839 while (1) /* every loop calculate size, second translate also, if we have storage for this */
841 size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
842 size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
843 size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
845 if (ForFree)
847 retCode = SHFileOperationW(&nFileOp);
848 HeapFree(GetProcessHeap(), 0, ForFree); /* we cannot use wString, it was changed */
849 break;
851 else
853 wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
854 if (ForFree) continue;
855 retCode = ERROR_OUTOFMEMORY;
856 nFileOp.fAnyOperationsAborted = TRUE;
857 SetLastError(retCode);
858 return retCode;
862 lpFileOp->hNameMappings = nFileOp.hNameMappings;
863 lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
864 return retCode;
867 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
869 typedef struct
871 DWORD attributes;
872 LPWSTR szDirectory;
873 LPWSTR szFilename;
874 LPWSTR szFullPath;
875 BOOL bFromWildcard;
876 BOOL bFromRelative;
877 BOOL bExists;
878 } FILE_ENTRY;
880 typedef struct
882 FILE_ENTRY *feFiles;
883 DWORD num_alloc;
884 DWORD dwNumFiles;
885 BOOL bAnyFromWildcard;
886 BOOL bAnyDirectories;
887 BOOL bAnyDontExist;
888 } FILE_LIST;
891 static inline void grow_list(FILE_LIST *list)
893 FILE_ENTRY *new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
894 list->num_alloc * 2 * sizeof(*new) );
895 list->feFiles = new;
896 list->num_alloc *= 2;
899 /* adds a file to the FILE_ENTRY struct
901 static void add_file_to_entry(FILE_ENTRY *feFile, LPWSTR szFile)
903 DWORD dwLen = lstrlenW(szFile) + 1;
904 LPWSTR ptr;
906 feFile->szFullPath = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
907 lstrcpyW(feFile->szFullPath, szFile);
909 ptr = StrRChrW(szFile, NULL, '\\');
910 if (ptr)
912 dwLen = ptr - szFile + 1;
913 feFile->szDirectory = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
914 lstrcpynW(feFile->szDirectory, szFile, dwLen);
916 dwLen = lstrlenW(feFile->szFullPath) - dwLen + 1;
917 feFile->szFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
918 lstrcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
920 feFile->bFromWildcard = FALSE;
923 static LPWSTR wildcard_to_file(LPWSTR szWildCard, LPWSTR szFileName)
925 LPWSTR szFullPath, ptr;
926 DWORD dwDirLen, dwFullLen;
928 ptr = StrRChrW(szWildCard, NULL, '\\');
929 dwDirLen = ptr - szWildCard + 1;
931 dwFullLen = dwDirLen + lstrlenW(szFileName) + 1;
932 szFullPath = HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
934 lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
935 lstrcatW(szFullPath, szFileName);
937 return szFullPath;
940 static void parse_wildcard_files(FILE_LIST *flList, LPWSTR szFile, LPDWORD pdwListIndex)
942 WIN32_FIND_DATAW wfd;
943 HANDLE hFile = FindFirstFileW(szFile, &wfd);
944 FILE_ENTRY *file;
945 LPWSTR szFullPath;
946 BOOL res;
948 if (hFile == INVALID_HANDLE_VALUE) return;
950 for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
952 if (IsDotDir(wfd.cFileName)) continue;
953 if (*pdwListIndex >= flList->num_alloc) grow_list( flList );
954 szFullPath = wildcard_to_file(szFile, wfd.cFileName);
955 file = &flList->feFiles[(*pdwListIndex)++];
956 add_file_to_entry(file, szFullPath);
957 file->bFromWildcard = TRUE;
958 file->attributes = wfd.dwFileAttributes;
959 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
960 HeapFree(GetProcessHeap(), 0, szFullPath);
963 FindClose(hFile);
966 /* takes the null-separated file list and fills out the FILE_LIST */
967 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
969 LPCWSTR ptr = szFiles;
970 WCHAR szCurFile[MAX_PATH];
971 DWORD i = 0, dwDirLen;
973 if (!szFiles)
974 return ERROR_INVALID_PARAMETER;
976 flList->bAnyFromWildcard = FALSE;
977 flList->bAnyDirectories = FALSE;
978 flList->bAnyDontExist = FALSE;
979 flList->num_alloc = 32;
980 flList->dwNumFiles = 0;
982 /* empty list */
983 if (!szFiles[0])
984 return ERROR_ACCESS_DENIED;
986 flList->feFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
987 flList->num_alloc * sizeof(FILE_ENTRY));
989 while (*ptr)
991 if (i >= flList->num_alloc) grow_list( flList );
993 /* change relative to absolute path */
994 if (PathIsRelativeW(ptr))
996 dwDirLen = GetCurrentDirectoryW(MAX_PATH, szCurFile) + 1;
997 PathCombineW(szCurFile, szCurFile, ptr);
998 flList->feFiles[i].bFromRelative = TRUE;
1000 else
1002 lstrcpyW(szCurFile, ptr);
1003 flList->feFiles[i].bFromRelative = FALSE;
1006 /* parse wildcard files if they are in the filename */
1007 if (StrPBrkW(szCurFile, wWildcardChars))
1009 parse_wildcard_files(flList, szCurFile, &i);
1010 flList->bAnyFromWildcard = TRUE;
1011 i--;
1013 else
1015 FILE_ENTRY *file = &flList->feFiles[i];
1016 add_file_to_entry(file, szCurFile);
1017 file->attributes = GetFileAttributesW( file->szFullPath );
1018 file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
1019 if (!file->bExists) flList->bAnyDontExist = TRUE;
1020 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
1023 /* advance to the next string */
1024 ptr += lstrlenW(ptr) + 1;
1025 i++;
1027 flList->dwNumFiles = i;
1029 return S_OK;
1032 /* free the FILE_LIST */
1033 static void destroy_file_list(FILE_LIST *flList)
1035 DWORD i;
1037 if (!flList || !flList->feFiles)
1038 return;
1040 for (i = 0; i < flList->dwNumFiles; i++)
1042 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
1043 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
1044 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
1047 HeapFree(GetProcessHeap(), 0, flList->feFiles);
1050 static void copy_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
1052 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1053 SHFILEOPSTRUCTW fileOp;
1055 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1057 if (IsDotDir(feFrom->szFilename))
1058 return;
1060 if (PathFileExistsW(szDestPath))
1061 PathCombineW(szTo, szDestPath, feFrom->szFilename);
1062 else
1063 lstrcpyW(szTo, szDestPath);
1065 szTo[lstrlenW(szTo) + 1] = '\0';
1066 SHNotifyCreateDirectoryW(szTo, NULL);
1068 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1069 szFrom[lstrlenW(szFrom) + 1] = '\0';
1071 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
1072 fileOp.pFrom = szFrom;
1073 fileOp.pTo = szTo;
1074 fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
1076 SHFileOperationW(&fileOp);
1079 /* copy a file or directory to another directory */
1080 static void copy_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
1082 if (!PathFileExistsW(feTo->szFullPath))
1083 SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
1085 if (IsAttribFile(feFrom->attributes))
1087 WCHAR szDestPath[MAX_PATH];
1089 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1090 SHNotifyCopyFileW(feFrom->szFullPath, szDestPath, FALSE);
1092 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1093 copy_dir_to_dir(lpFileOp, feFrom, feTo->szFullPath);
1096 static void create_dest_dirs(LPWSTR szDestDir)
1098 WCHAR dir[MAX_PATH];
1099 LPWSTR ptr = StrChrW(szDestDir, '\\');
1101 /* make sure all directories up to last one are created */
1102 while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
1104 lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
1106 if (!PathFileExistsW(dir))
1107 SHNotifyCreateDirectoryW(dir, NULL);
1110 /* create last directory */
1111 if (!PathFileExistsW(szDestDir))
1112 SHNotifyCreateDirectoryW(szDestDir, NULL);
1115 /* the FO_COPY operation */
1116 static HRESULT copy_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1118 DWORD i;
1119 FILE_ENTRY *entryToCopy;
1120 FILE_ENTRY *fileDest = &flTo->feFiles[0];
1121 BOOL bCancelIfAnyDirectories = FALSE;
1123 if (flFrom->bAnyDontExist)
1124 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1126 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->bAnyFromWildcard)
1127 return ERROR_CANCELLED;
1129 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flTo->dwNumFiles != 1)
1130 return ERROR_CANCELLED;
1132 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->dwNumFiles != 1 &&
1133 flFrom->dwNumFiles != flTo->dwNumFiles)
1135 return ERROR_CANCELLED;
1138 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 &&
1139 !PathFileExistsW(flTo->feFiles[0].szFullPath) &&
1140 IsAttribFile(fileDest->attributes))
1142 bCancelIfAnyDirectories = TRUE;
1145 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
1146 !PathFileExistsW(fileDest->szFullPath))
1148 lpFileOp->fAnyOperationsAborted = TRUE;
1149 return ERROR_CANCELLED;
1152 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flFrom->dwNumFiles != 1 &&
1153 PathFileExistsW(fileDest->szFullPath) &&
1154 IsAttribFile(fileDest->attributes))
1156 return ERROR_CANCELLED;
1159 for (i = 0; i < flFrom->dwNumFiles; i++)
1161 entryToCopy = &flFrom->feFiles[i];
1163 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1164 fileDest = &flTo->feFiles[i];
1166 if (IsAttribDir(entryToCopy->attributes) &&
1167 !lstrcmpiW(entryToCopy->szFullPath, fileDest->szDirectory))
1169 return ERROR_SUCCESS;
1172 if (IsAttribDir(entryToCopy->attributes) && bCancelIfAnyDirectories)
1173 return ERROR_CANCELLED;
1175 create_dest_dirs(fileDest->szDirectory);
1177 if (!lstrcmpiW(entryToCopy->szFullPath, fileDest->szFullPath))
1179 if (IsAttribFile(entryToCopy->attributes))
1180 return ERROR_NO_MORE_SEARCH_HANDLES;
1181 else
1182 return ERROR_SUCCESS;
1185 if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1186 (flFrom->dwNumFiles == 1 && IsAttribDir(fileDest->attributes)))
1188 copy_to_dir(lpFileOp, entryToCopy, fileDest);
1190 else if (IsAttribDir(entryToCopy->attributes))
1192 copy_dir_to_dir(lpFileOp, entryToCopy, fileDest->szFullPath);
1194 else
1196 if (SHNotifyCopyFileW(entryToCopy->szFullPath, fileDest->szFullPath, TRUE))
1198 lpFileOp->fAnyOperationsAborted = TRUE;
1199 return ERROR_CANCELLED;
1204 return ERROR_SUCCESS;
1207 static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, BOOL fTrash, FILE_LIST *flFrom)
1209 if (flFrom->dwNumFiles > 1)
1211 WCHAR tmp[8];
1212 const WCHAR format[] = {'%','d',0};
1214 wnsprintfW(tmp, sizeof(tmp)/sizeof(tmp[0]), format, flFrom->dwNumFiles);
1215 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_MULTIPLE_ITEM:ASK_DELETE_MULTIPLE_ITEM), tmp);
1217 else
1219 FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
1221 if (IsAttribFile(fileEntry->attributes))
1222 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FILE:ASK_DELETE_FILE), fileEntry->szFullPath);
1223 else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1224 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FOLDER:ASK_DELETE_FOLDER), fileEntry->szFullPath);
1226 return TRUE;
1229 /* the FO_DELETE operation */
1230 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom)
1232 FILE_ENTRY *fileEntry;
1233 DWORD i;
1234 BOOL bPathExists;
1235 BOOL bTrash;
1237 if (!flFrom->dwNumFiles)
1238 return ERROR_SUCCESS;
1240 /* Windows also checks only the first item */
1241 bTrash = (lpFileOp->fFlags & FOF_ALLOWUNDO)
1242 && TRASH_CanTrashFile(flFrom->feFiles[0].szFullPath);
1244 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (!bTrash && lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1245 if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, bTrash, flFrom))
1247 lpFileOp->fAnyOperationsAborted = TRUE;
1248 return 0;
1251 for (i = 0; i < flFrom->dwNumFiles; i++)
1253 bPathExists = TRUE;
1254 fileEntry = &flFrom->feFiles[i];
1256 if (!IsAttribFile(fileEntry->attributes) &&
1257 (lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1258 continue;
1260 if (bTrash)
1262 BOOL bDelete;
1263 if (TRASH_TrashFile(fileEntry->szFullPath))
1264 continue;
1266 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1267 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1268 bDelete = SHELL_ConfirmDialogW(lpFileOp->hwnd, ASK_CANT_TRASH_ITEM, fileEntry->szFullPath);
1269 else
1270 bDelete = TRUE;
1272 if (!bDelete)
1274 lpFileOp->fAnyOperationsAborted = TRUE;
1275 break;
1279 /* delete the file or directory */
1280 if (IsAttribFile(fileEntry->attributes))
1281 bPathExists = DeleteFileW(fileEntry->szFullPath);
1282 else
1283 bPathExists = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1285 if (!bPathExists)
1286 return ERROR_PATH_NOT_FOUND;
1289 return ERROR_SUCCESS;
1292 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
1294 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1295 SHFILEOPSTRUCTW fileOp;
1297 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1299 if (IsDotDir(feFrom->szFilename))
1300 return;
1302 SHNotifyCreateDirectoryW(szDestPath, NULL);
1304 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1305 szFrom[lstrlenW(szFrom) + 1] = '\0';
1307 lstrcpyW(szTo, szDestPath);
1308 szTo[lstrlenW(szDestPath) + 1] = '\0';
1310 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
1311 fileOp.pFrom = szFrom;
1312 fileOp.pTo = szTo;
1314 SHFileOperationW(&fileOp);
1317 /* moves a file or directory to another directory */
1318 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
1320 WCHAR szDestPath[MAX_PATH];
1322 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1324 if (IsAttribFile(feFrom->attributes))
1325 SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
1326 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1327 move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1330 /* the FO_MOVE operation */
1331 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1333 DWORD i;
1334 FILE_ENTRY *entryToMove;
1335 FILE_ENTRY *fileDest;
1337 if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1338 return ERROR_CANCELLED;
1340 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1341 flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1343 return ERROR_CANCELLED;
1346 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1347 !flFrom->bAnyDirectories &&
1348 flFrom->dwNumFiles > flTo->dwNumFiles)
1350 return ERROR_CANCELLED;
1353 if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1354 return ERROR_CANCELLED;
1356 if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1357 flFrom->dwNumFiles != flTo->dwNumFiles)
1359 return ERROR_CANCELLED;
1362 fileDest = &flTo->feFiles[0];
1363 for (i = 0; i < flFrom->dwNumFiles; i++)
1365 entryToMove = &flFrom->feFiles[i];
1367 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1368 fileDest = &flTo->feFiles[i];
1370 if (!PathFileExistsW(fileDest->szDirectory))
1371 return ERROR_CANCELLED;
1373 if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1374 move_to_dir(lpFileOp, entryToMove, fileDest);
1375 else
1376 SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
1379 return ERROR_SUCCESS;
1382 /* the FO_RENAME files */
1383 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1385 FILE_ENTRY *feFrom;
1386 FILE_ENTRY *feTo;
1388 if (flFrom->dwNumFiles != 1)
1389 return ERROR_GEN_FAILURE;
1391 if (flTo->dwNumFiles != 1)
1392 return ERROR_CANCELLED;
1394 feFrom = &flFrom->feFiles[0];
1395 feTo= &flTo->feFiles[0];
1397 /* fail if destination doesn't exist */
1398 if (!feFrom->bExists)
1399 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1401 /* fail if destination already exists */
1402 if (feTo->bExists)
1403 return ERROR_ALREADY_EXISTS;
1405 return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
1408 /* alert the user if an unsupported flag is used */
1409 static void check_flags(FILEOP_FLAGS fFlags)
1411 WORD wUnsupportedFlags = FOF_NO_CONNECTED_ELEMENTS |
1412 FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1413 FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1415 if (fFlags & wUnsupportedFlags)
1416 FIXME("Unsupported flags: %04x\n", fFlags);
1419 /*************************************************************************
1420 * SHFileOperationW [SHELL32.@]
1422 * See SHFileOperationA
1424 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1426 FILE_LIST flFrom, flTo;
1427 int ret = 0;
1429 if (!lpFileOp)
1430 return ERROR_INVALID_PARAMETER;
1432 check_flags(lpFileOp->fFlags);
1434 ZeroMemory(&flFrom, sizeof(FILE_LIST));
1435 ZeroMemory(&flTo, sizeof(FILE_LIST));
1437 if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1438 return ret;
1440 if (lpFileOp->wFunc != FO_DELETE)
1441 parse_file_list(&flTo, lpFileOp->pTo);
1443 switch (lpFileOp->wFunc)
1445 case FO_COPY:
1446 ret = copy_files(lpFileOp, &flFrom, &flTo);
1447 break;
1448 case FO_DELETE:
1449 ret = delete_files(lpFileOp, &flFrom);
1450 break;
1451 case FO_MOVE:
1452 ret = move_files(lpFileOp, &flFrom, &flTo);
1453 break;
1454 case FO_RENAME:
1455 ret = rename_files(lpFileOp, &flFrom, &flTo);
1456 break;
1457 default:
1458 ret = ERROR_INVALID_PARAMETER;
1459 break;
1462 destroy_file_list(&flFrom);
1464 if (lpFileOp->wFunc != FO_DELETE)
1465 destroy_file_list(&flTo);
1467 if (ret == ERROR_CANCELLED)
1468 lpFileOp->fAnyOperationsAborted = TRUE;
1470 return ret;
1473 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1475 /*************************************************************************
1476 * SHFreeNameMappings [shell32.246]
1478 * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1479 * was specified.
1481 * PARAMS
1482 * hNameMapping [I] handle to the name mappings used during renaming of files
1484 * RETURNS
1485 * Nothing
1487 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1489 if (hNameMapping)
1491 int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1493 for (; i>= 0; i--)
1495 LPSHNAMEMAPPINGW lp = DSA_GetItemPtr((HDSA)hNameMapping, i);
1497 SHFree(lp->pszOldPath);
1498 SHFree(lp->pszNewPath);
1500 DSA_Destroy((HDSA)hNameMapping);
1504 /*************************************************************************
1505 * SheGetDirA [SHELL32.@]
1508 HRESULT WINAPI SheGetDirA(LPSTR u, LPSTR v)
1509 { FIXME("%p %p stub\n",u,v);
1510 return 0;
1513 /*************************************************************************
1514 * SheGetDirW [SHELL32.@]
1517 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1518 { FIXME("%p %p stub\n",u,v);
1519 return 0;
1522 /*************************************************************************
1523 * SheChangeDirA [SHELL32.@]
1526 HRESULT WINAPI SheChangeDirA(LPSTR u)
1527 { FIXME("(%s),stub\n",debugstr_a(u));
1528 return 0;
1531 /*************************************************************************
1532 * SheChangeDirW [SHELL32.@]
1535 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1536 { FIXME("(%s),stub\n",debugstr_w(u));
1537 return 0;
1540 /*************************************************************************
1541 * IsNetDrive [SHELL32.66]
1543 BOOL WINAPI IsNetDrive(DWORD drive)
1545 char root[4];
1546 strcpy(root, "A:\\");
1547 root[0] += (char)drive;
1548 return (GetDriveTypeA(root) == DRIVE_REMOTE);
1552 /*************************************************************************
1553 * RealDriveType [SHELL32.524]
1555 INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1557 char root[] = "A:\\";
1558 root[0] += (char)drive;
1559 return GetDriveTypeA(root);