Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / shell32 / shlfileop.c
blobc624cb8a5cdf6b441f728b6c8e3298c1c3843fda
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>
30 #include <assert.h>
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"
47 WINE_DEFAULT_DEBUG_CHANNEL(shell);
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))))
54 #define FO_MASK 0xF
56 static const WCHAR wWildcardFile[] = {'*',0};
57 static const WCHAR wWildcardChars[] = {'*','?',0};
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);
69 typedef struct
71 SHFILEOPSTRUCTW *req;
72 DWORD dwYesToAllMask;
73 BOOL bManyItems;
74 BOOL bCancelled;
75 } FILE_OPERATION;
77 /* Confirm dialogs with an optional "Yes To All" as used in file operations confirmations
79 static const WCHAR CONFIRM_MSG_PROP[] = {'W','I','N','E','_','C','O','N','F','I','R','M',0};
81 struct confirm_msg_info
83 LPWSTR lpszText;
84 LPWSTR lpszCaption;
85 HICON hIcon;
86 BOOL bYesToAll;
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)
93 HWND hButton = GetDlgItem(hDlg, iId);
94 RECT r;
96 if (bShow) {
97 POINT pt;
98 int width;
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;
108 else
109 ShowWindow(hButton, SW_HIDE);
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
115 static INT_PTR CALLBACK ConfirmMsgBox_Paint(HWND hDlg)
117 PAINTSTRUCT ps;
118 HFONT hOldFont;
119 RECT r;
120 HDC hdc;
122 BeginPaint(hDlg, &ps);
123 hdc = ps.hdc;
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, (LPWSTR)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;
135 static INT_PTR CALLBACK ConfirmMsgBox_Init(HWND hDlg, LPARAM lParam)
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;
144 SetWindowTextW(hDlg, info->lpszCaption);
145 ShowWindow(GetDlgItem(hDlg, IDD_MESSAGE), SW_HIDE);
146 SetPropW(hDlg, CONFIRM_MSG_PROP, (HANDLE)info->lpszText);
147 SendDlgItemMessageW(hDlg, IDD_ICON, STM_SETICON, (WPARAM)info->hIcon, 0);
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);
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);
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;
175 static INT_PTR CALLBACK ConfirmMsgBoxProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
177 switch (uMsg)
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;
190 return FALSE;
193 static int SHELL_ConfirmMsgBox(HWND hWnd, LPWSTR lpszText, LPWSTR lpszCaption, HICON hIcon, BOOL bYesToAll)
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;
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);
205 /* confirmation dialogs content */
206 typedef struct
208 HINSTANCE hIconInstance;
209 UINT icon_resource_id;
210 UINT caption_resource_id, text_resource_id;
211 } SHELL_ConfirmIDstruc;
213 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
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);
272 return FALSE;
275 static BOOL SHELL_ConfirmDialogW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir, FILE_OPERATION *op)
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;
283 assert(nKindOfDialog >= 0 && nKindOfDialog < 32);
284 if (op && (op->dwYesToAllMask & (1 << nKindOfDialog)))
285 return TRUE;
287 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids)) return FALSE;
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));
292 args[0] = (DWORD_PTR)szDir;
293 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
294 szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)args);
295 hIcon = LoadIconW(ids.hIconInstance, (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id));
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;
303 if (ret == IDCANCEL)
304 op->bCancelled = TRUE;
305 if (ret != IDYES)
306 op->req->fAnyOperationsAborted = TRUE;
308 return ret == IDYES;
311 BOOL SHELL_ConfirmYesNoW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir)
313 return SHELL_ConfirmDialogW(hWnd, nKindOfDialog, szDir, NULL);
316 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
318 DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
320 if (len < minChars)
321 len = minChars;
323 *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
324 if (*wPath)
326 MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
327 return NO_ERROR;
329 return E_OUTOFMEMORY;
332 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
334 HeapFree(GetProcessHeap(), 0, wPath);
337 HRESULT WINAPI SHIsFileAvailableOffline(LPCWSTR path, LPDWORD status)
339 FIXME("(%s, %p) stub\n", debugstr_w(path), status);
340 return E_FAIL;
343 /**************************************************************************
344 * SHELL_DeleteDirectory() [internal]
346 * Asks for confirmation when bShowUI is true and deletes the directory and
347 * all its subdirectories and files if necessary.
349 BOOL SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
351 BOOL ret = TRUE;
352 HANDLE hFind;
353 WIN32_FIND_DATAW wfd;
354 WCHAR szTemp[MAX_PATH];
355 WCHAR szPath[MAX_PATH];
356 BOOL foundOne;
358 /* Make sure the directory exists before eventually prompting the user */
359 PathCombineW(szPath, pszDir, wWildcardFile);
361 if (!bShowUI || (ret = SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir, NULL)))
365 foundOne = FALSE;
366 hFind = FindFirstFileW(szPath, &wfd);
367 if (hFind == INVALID_HANDLE_VALUE)
368 return FALSE;
372 if (IsDotDir(wfd.cFileName))
373 continue;
374 foundOne = TRUE;
375 PathCombineW(szTemp, pszDir, wfd.cFileName);
376 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
377 ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
378 else
379 ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
380 } while (ret && FindNextFileW(hFind, &wfd));
382 FindClose(hFind);
383 }while (ret && foundOne);
385 if (ret)
386 ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
387 return ret;
390 /**************************************************************************
391 * Win32CreateDirectory [SHELL32.93]
393 * Creates a directory. Also triggers a change notify if one exists.
395 * PARAMS
396 * path [I] path to directory to create
398 * RETURNS
399 * TRUE if successful, FALSE otherwise
401 * NOTES
402 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
403 * This is Unicode on NT/2000
405 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
407 LPWSTR wPath;
408 DWORD retCode;
410 TRACE("(%s, %p)\n", debugstr_a(path), sec);
412 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
413 if (!retCode)
415 retCode = SHNotifyCreateDirectoryW(wPath, sec);
416 SHELL32_FreeUnicodeBuf(wPath);
418 return retCode;
421 /**********************************************************************/
423 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
425 TRACE("(%s, %p)\n", debugstr_w(path), sec);
427 if (CreateDirectoryW(path, sec))
429 SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
430 return ERROR_SUCCESS;
432 return GetLastError();
435 /**********************************************************************/
437 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
439 if (SHELL_OsIsUnicode())
440 return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
441 return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
444 /************************************************************************
445 * Win32RemoveDirectory [SHELL32.94]
447 * Deletes a directory. Also triggers a change notify if one exists.
449 * PARAMS
450 * path [I] path to directory to delete
452 * RETURNS
453 * TRUE if successful, FALSE otherwise
455 * NOTES
456 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
457 * This is Unicode on NT/2000
459 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
461 LPWSTR wPath;
462 DWORD retCode;
464 TRACE("(%s)\n", debugstr_a(path));
466 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
467 if (!retCode)
469 retCode = SHNotifyRemoveDirectoryW(wPath);
470 SHELL32_FreeUnicodeBuf(wPath);
472 return retCode;
475 /***********************************************************************/
477 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
479 BOOL ret;
480 TRACE("(%s)\n", debugstr_w(path));
482 ret = RemoveDirectoryW(path);
483 if (!ret)
485 /* Directory may be write protected */
486 DWORD dwAttr = GetFileAttributesW(path);
487 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
488 if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
489 ret = RemoveDirectoryW(path);
491 if (ret)
493 SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
494 return ERROR_SUCCESS;
496 return GetLastError();
499 /***********************************************************************/
501 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
503 if (SHELL_OsIsUnicode())
504 return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
505 return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
508 /************************************************************************
509 * Win32DeleteFile [SHELL32.164]
511 * Deletes a file. Also triggers a change notify if one exists.
513 * PARAMS
514 * path [I] path to file to delete
516 * RETURNS
517 * TRUE if successful, FALSE otherwise
519 * NOTES
520 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
521 * This is Unicode on NT/2000
523 static DWORD SHNotifyDeleteFileA(LPCSTR path)
525 LPWSTR wPath;
526 DWORD retCode;
528 TRACE("(%s)\n", debugstr_a(path));
530 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
531 if (!retCode)
533 retCode = SHNotifyDeleteFileW(wPath);
534 SHELL32_FreeUnicodeBuf(wPath);
536 return retCode;
539 /***********************************************************************/
541 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
543 BOOL ret;
545 TRACE("(%s)\n", debugstr_w(path));
547 ret = DeleteFileW(path);
548 if (!ret)
550 /* File may be write protected or a system file */
551 DWORD dwAttr = GetFileAttributesW(path);
552 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
553 if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
554 ret = DeleteFileW(path);
556 if (ret)
558 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
559 return ERROR_SUCCESS;
561 return GetLastError();
564 /***********************************************************************/
566 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
568 if (SHELL_OsIsUnicode())
569 return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
570 return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
573 /************************************************************************
574 * SHNotifyMoveFile [internal]
576 * Moves a file. Also triggers a change notify if one exists.
578 * PARAMS
579 * src [I] path to source file to move
580 * dest [I] path to target file to move to
582 * RETURNS
583 * ERORR_SUCCESS if successful
585 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
587 BOOL ret;
589 TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
591 ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
593 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
594 if (!ret)
595 ret = MoveFileW(src, dest);
597 if (!ret)
599 DWORD dwAttr;
601 dwAttr = SHFindAttrW(dest, FALSE);
602 if (INVALID_FILE_ATTRIBUTES == dwAttr)
604 /* Source file may be write protected or a system file */
605 dwAttr = GetFileAttributesW(src);
606 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
607 if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
608 ret = MoveFileW(src, dest);
611 if (ret)
613 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
614 return ERROR_SUCCESS;
616 return GetLastError();
619 /************************************************************************
620 * SHNotifyCopyFile [internal]
622 * Copies a file. Also triggers a change notify if one exists.
624 * PARAMS
625 * src [I] path to source file to move
626 * dest [I] path to target file to move to
627 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
628 * a file with this name already exists
630 * RETURNS
631 * ERROR_SUCCESS if successful
633 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
635 BOOL ret;
637 TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
639 ret = CopyFileW(src, dest, bFailIfExists);
640 if (ret)
642 SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
643 return ERROR_SUCCESS;
646 return GetLastError();
649 /*************************************************************************
650 * SHCreateDirectory [SHELL32.165]
652 * This function creates a file system folder whose fully qualified path is
653 * given by path. If one or more of the intermediate folders do not exist,
654 * they will be created as well.
656 * PARAMS
657 * hWnd [I]
658 * path [I] path of directory to create
660 * RETURNS
661 * ERRROR_SUCCESS or one of the following values:
662 * ERROR_BAD_PATHNAME if the path is relative
663 * ERROR_FILE_EXISTS when a file with that name exists
664 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
665 * ERROR_INVLID_NAME if the path contains invalid chars
666 * ERROR_ALREADY_EXISTS when the directory already exists
667 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
669 * NOTES
670 * exported by ordinal
671 * Win9x exports ANSI
672 * WinNT/2000 exports Unicode
674 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
676 if (SHELL_OsIsUnicode())
677 return SHCreateDirectoryExW(hWnd, path, NULL);
678 return SHCreateDirectoryExA(hWnd, path, NULL);
681 /*************************************************************************
682 * SHCreateDirectoryExA [SHELL32.@]
684 * This function creates a file system folder whose fully qualified path is
685 * given by path. If one or more of the intermediate folders do not exist,
686 * they will be created as well.
688 * PARAMS
689 * hWnd [I]
690 * path [I] path of directory to create
691 * sec [I] security attributes to use or NULL
693 * RETURNS
694 * ERRROR_SUCCESS or one of the following values:
695 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
696 * ERROR_INVLID_NAME if the path contains invalid chars
697 * ERROR_FILE_EXISTS when a file with that name exists
698 * ERROR_ALREADY_EXISTS when the directory already exists
699 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
701 * FIXME: Not implemented yet;
702 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
703 * if the path is a network path to deal with network drivers which might have a limited
704 * but unknown maximum path length. If not:
706 * If hWnd is set to a valid window handle, a message box is displayed warning
707 * the user that the files may not be accessible. If the user chooses not to
708 * proceed, the function returns ERROR_CANCELLED.
710 * If hWnd is set to NULL, no user interface is displayed and the function
711 * returns ERROR_CANCELLED.
713 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
715 LPWSTR wPath;
716 DWORD retCode;
718 TRACE("(%s, %p)\n", debugstr_a(path), sec);
720 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
721 if (!retCode)
723 retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
724 SHELL32_FreeUnicodeBuf(wPath);
726 return retCode;
729 /*************************************************************************
730 * SHCreateDirectoryExW [SHELL32.@]
732 * See SHCreateDirectoryExA.
734 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
736 int ret = ERROR_BAD_PATHNAME;
737 TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
739 if (PathIsRelativeW(path))
741 SetLastError(ret);
743 else
745 ret = SHNotifyCreateDirectoryW(path, sec);
746 /* Refuse to work on certain error codes before trying to create directories recursively */
747 if (ret != ERROR_SUCCESS &&
748 ret != ERROR_FILE_EXISTS &&
749 ret != ERROR_ALREADY_EXISTS &&
750 ret != ERROR_FILENAME_EXCED_RANGE)
752 WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1]; /* extra for PathAddBackslash() */
754 lstrcpynW(szTemp, path, MAX_PATH);
755 pEnd = PathAddBackslashW(szTemp);
756 pSlash = szTemp + 3;
758 while (*pSlash)
760 while (*pSlash && *pSlash != '\\')
761 pSlash = CharNextW(pSlash);
763 if (*pSlash)
765 *pSlash = 0; /* terminate path at separator */
767 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
769 *pSlash++ = '\\'; /* put the separator back */
773 if (ret && hWnd && (ERROR_CANCELLED != ret))
775 /* We failed and should show a dialog box */
776 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
777 ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
780 return ret;
783 /*************************************************************************
784 * SHFindAttrW [internal]
786 * Get the Attributes for a file or directory. The difference to GetAttributes()
787 * is that this function will also work for paths containing wildcard characters
788 * in its filename.
790 * PARAMS
791 * path [I] path of directory or file to check
792 * fileOnly [I] TRUE if only files should be found
794 * RETURNS
795 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
796 * the first file or directory found otherwise
798 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
800 WIN32_FIND_DATAW wfd;
801 BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
802 DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
803 HANDLE hFind = FindFirstFileW(pName, &wfd);
805 TRACE("%s %d\n", debugstr_w(pName), fileOnly);
806 if (INVALID_HANDLE_VALUE != hFind)
810 if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
811 continue;
812 dwAttr = wfd.dwFileAttributes;
813 break;
815 while (FindNextFileW(hFind, &wfd));
816 FindClose(hFind);
818 return dwAttr;
821 /*************************************************************************
823 * SHNameTranslate HelperFunction for SHFileOperationA
825 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
826 * is NULL, only the necessary size of the string is determined and returned,
827 * otherwise the ASCII strings are copied into it and the buffer is increased
828 * to point to the location after the final 0 termination char.
830 static DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
832 DWORD size = 0, aSize = 0;
833 LPCSTR aString = (LPCSTR)*pWToFrom;
835 if (aString)
839 size = lstrlenA(aString) + 1;
840 aSize += size;
841 aString += size;
842 } while ((size != 1) && more);
843 /* The two sizes might be different in the case of multibyte chars */
844 size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
845 if (*wString) /* only in the second loop */
847 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
848 *pWToFrom = *wString;
849 *wString += size;
852 return size;
854 /*************************************************************************
855 * SHFileOperationA [SHELL32.@]
857 * Function to copy, move, delete and create one or more files with optional
858 * user prompts.
860 * PARAMS
861 * lpFileOp [I/O] pointer to a structure containing all the necessary information
863 * RETURNS
864 * Success: ERROR_SUCCESS.
865 * Failure: ERROR_CANCELLED.
867 * NOTES
868 * exported by name
870 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
872 SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
873 int retCode = 0;
874 DWORD size;
875 LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
876 wString = NULL; /* we change this in SHNameTranslate */
878 TRACE("\n");
879 if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
880 nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
881 if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
882 nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
883 while (1) /* every loop calculate size, second translate also, if we have storage for this */
885 size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
886 size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
887 size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
889 if (ForFree)
891 retCode = SHFileOperationW(&nFileOp);
892 HeapFree(GetProcessHeap(), 0, ForFree); /* we cannot use wString, it was changed */
893 break;
895 else
897 wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
898 if (ForFree) continue;
899 retCode = ERROR_OUTOFMEMORY;
900 nFileOp.fAnyOperationsAborted = TRUE;
901 SetLastError(retCode);
902 return retCode;
906 lpFileOp->hNameMappings = nFileOp.hNameMappings;
907 lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
908 return retCode;
911 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
913 typedef struct
915 DWORD attributes;
916 LPWSTR szDirectory;
917 LPWSTR szFilename;
918 LPWSTR szFullPath;
919 BOOL bFromWildcard;
920 BOOL bFromRelative;
921 BOOL bExists;
922 } FILE_ENTRY;
924 typedef struct
926 FILE_ENTRY *feFiles;
927 DWORD num_alloc;
928 DWORD dwNumFiles;
929 BOOL bAnyFromWildcard;
930 BOOL bAnyDirectories;
931 BOOL bAnyDontExist;
932 } FILE_LIST;
935 static inline void grow_list(FILE_LIST *list)
937 FILE_ENTRY *new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
938 list->num_alloc * 2 * sizeof(*new) );
939 list->feFiles = new;
940 list->num_alloc *= 2;
943 /* adds a file to the FILE_ENTRY struct
945 static void add_file_to_entry(FILE_ENTRY *feFile, LPWSTR szFile)
947 DWORD dwLen = lstrlenW(szFile) + 1;
948 LPWSTR ptr;
950 feFile->szFullPath = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
951 lstrcpyW(feFile->szFullPath, szFile);
953 ptr = StrRChrW(szFile, NULL, '\\');
954 if (ptr)
956 dwLen = ptr - szFile + 1;
957 feFile->szDirectory = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
958 lstrcpynW(feFile->szDirectory, szFile, dwLen);
960 dwLen = lstrlenW(feFile->szFullPath) - dwLen + 1;
961 feFile->szFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
962 lstrcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
964 feFile->bFromWildcard = FALSE;
967 static LPWSTR wildcard_to_file(LPWSTR szWildCard, LPWSTR szFileName)
969 LPWSTR szFullPath, ptr;
970 DWORD dwDirLen, dwFullLen;
972 ptr = StrRChrW(szWildCard, NULL, '\\');
973 dwDirLen = ptr - szWildCard + 1;
975 dwFullLen = dwDirLen + lstrlenW(szFileName) + 1;
976 szFullPath = HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
978 lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
979 lstrcatW(szFullPath, szFileName);
981 return szFullPath;
984 static void parse_wildcard_files(FILE_LIST *flList, LPWSTR szFile, LPDWORD pdwListIndex)
986 WIN32_FIND_DATAW wfd;
987 HANDLE hFile = FindFirstFileW(szFile, &wfd);
988 FILE_ENTRY *file;
989 LPWSTR szFullPath;
990 BOOL res;
992 if (hFile == INVALID_HANDLE_VALUE) return;
994 for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
996 if (IsDotDir(wfd.cFileName)) continue;
997 if (*pdwListIndex >= flList->num_alloc) grow_list( flList );
998 szFullPath = wildcard_to_file(szFile, wfd.cFileName);
999 file = &flList->feFiles[(*pdwListIndex)++];
1000 add_file_to_entry(file, szFullPath);
1001 file->bFromWildcard = TRUE;
1002 file->attributes = wfd.dwFileAttributes;
1003 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
1004 HeapFree(GetProcessHeap(), 0, szFullPath);
1007 FindClose(hFile);
1010 /* takes the null-separated file list and fills out the FILE_LIST */
1011 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
1013 LPCWSTR ptr = szFiles;
1014 WCHAR szCurFile[MAX_PATH];
1015 DWORD i = 0, dwDirLen;
1017 if (!szFiles)
1018 return ERROR_INVALID_PARAMETER;
1020 flList->bAnyFromWildcard = FALSE;
1021 flList->bAnyDirectories = FALSE;
1022 flList->bAnyDontExist = FALSE;
1023 flList->num_alloc = 32;
1024 flList->dwNumFiles = 0;
1026 /* empty list */
1027 if (!szFiles[0])
1028 return ERROR_ACCESS_DENIED;
1030 flList->feFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1031 flList->num_alloc * sizeof(FILE_ENTRY));
1033 while (*ptr)
1035 if (i >= flList->num_alloc) grow_list( flList );
1037 /* change relative to absolute path */
1038 if (PathIsRelativeW(ptr))
1040 dwDirLen = GetCurrentDirectoryW(MAX_PATH, szCurFile) + 1;
1041 PathCombineW(szCurFile, szCurFile, ptr);
1042 flList->feFiles[i].bFromRelative = TRUE;
1044 else
1046 lstrcpyW(szCurFile, ptr);
1047 flList->feFiles[i].bFromRelative = FALSE;
1050 /* parse wildcard files if they are in the filename */
1051 if (StrPBrkW(szCurFile, wWildcardChars))
1053 parse_wildcard_files(flList, szCurFile, &i);
1054 flList->bAnyFromWildcard = TRUE;
1055 i--;
1057 else
1059 FILE_ENTRY *file = &flList->feFiles[i];
1060 add_file_to_entry(file, szCurFile);
1061 file->attributes = GetFileAttributesW( file->szFullPath );
1062 file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
1063 if (!file->bExists) flList->bAnyDontExist = TRUE;
1064 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
1067 /* advance to the next string */
1068 ptr += lstrlenW(ptr) + 1;
1069 i++;
1071 flList->dwNumFiles = i;
1073 return S_OK;
1076 /* free the FILE_LIST */
1077 static void destroy_file_list(FILE_LIST *flList)
1079 DWORD i;
1081 if (!flList || !flList->feFiles)
1082 return;
1084 for (i = 0; i < flList->dwNumFiles; i++)
1086 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
1087 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
1088 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
1091 HeapFree(GetProcessHeap(), 0, flList->feFiles);
1094 static void copy_dir_to_dir(FILE_OPERATION *op, FILE_ENTRY *feFrom, LPWSTR szDestPath)
1096 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1097 SHFILEOPSTRUCTW fileOp;
1099 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1101 if (IsDotDir(feFrom->szFilename))
1102 return;
1104 if (PathFileExistsW(szDestPath))
1105 PathCombineW(szTo, szDestPath, feFrom->szFilename);
1106 else
1107 lstrcpyW(szTo, szDestPath);
1109 if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo)) {
1110 if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FOLDER, feFrom->szFilename, op))
1112 /* Vista returns an ERROR_CANCELLED even if user pressed "No" */
1113 if (!op->bManyItems)
1114 op->bCancelled = TRUE;
1115 return;
1119 szTo[lstrlenW(szTo) + 1] = '\0';
1120 SHNotifyCreateDirectoryW(szTo, NULL);
1122 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1123 szFrom[lstrlenW(szFrom) + 1] = '\0';
1125 memcpy(&fileOp, op->req, sizeof(fileOp));
1126 fileOp.pFrom = szFrom;
1127 fileOp.pTo = szTo;
1128 fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
1130 /* Don't ask the user about overwriting files when he accepted to overwrite the
1131 folder. FIXME: this is not exactly what Windows does - e.g. there would be
1132 an additional confirmation for a nested folder */
1133 fileOp.fFlags |= FOF_NOCONFIRMATION;
1135 SHFileOperationW(&fileOp);
1138 static BOOL copy_file_to_file(FILE_OPERATION *op, WCHAR *szFrom, WCHAR *szTo)
1140 if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo))
1142 if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FILE, PathFindFileNameW(szTo), op))
1143 return 0;
1146 return SHNotifyCopyFileW(szFrom, szTo, FALSE) == 0;
1149 /* copy a file or directory to another directory */
1150 static void copy_to_dir(FILE_OPERATION *op, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
1152 if (!PathFileExistsW(feTo->szFullPath))
1153 SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
1155 if (IsAttribFile(feFrom->attributes))
1157 WCHAR szDestPath[MAX_PATH];
1159 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1160 copy_file_to_file(op, feFrom->szFullPath, szDestPath);
1162 else if (!(op->req->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1163 copy_dir_to_dir(op, feFrom, feTo->szFullPath);
1166 static void create_dest_dirs(LPWSTR szDestDir)
1168 WCHAR dir[MAX_PATH];
1169 LPWSTR ptr = StrChrW(szDestDir, '\\');
1171 /* make sure all directories up to last one are created */
1172 while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
1174 lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
1176 if (!PathFileExistsW(dir))
1177 SHNotifyCreateDirectoryW(dir, NULL);
1180 /* create last directory */
1181 if (!PathFileExistsW(szDestDir))
1182 SHNotifyCreateDirectoryW(szDestDir, NULL);
1185 /* the FO_COPY operation */
1186 static HRESULT copy_files(FILE_OPERATION *op, FILE_LIST *flFrom, FILE_LIST *flTo)
1188 DWORD i;
1189 FILE_ENTRY *entryToCopy;
1190 FILE_ENTRY *fileDest = &flTo->feFiles[0];
1191 BOOL bCancelIfAnyDirectories = FALSE;
1193 if (flFrom->bAnyDontExist)
1194 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1196 if (op->req->fFlags & FOF_MULTIDESTFILES && flFrom->bAnyFromWildcard)
1197 return ERROR_CANCELLED;
1199 if (!(op->req->fFlags & FOF_MULTIDESTFILES) && flTo->dwNumFiles != 1)
1200 return ERROR_CANCELLED;
1202 if (op->req->fFlags & FOF_MULTIDESTFILES && flFrom->dwNumFiles != 1 &&
1203 flFrom->dwNumFiles != flTo->dwNumFiles)
1205 return ERROR_CANCELLED;
1208 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 &&
1209 !PathFileExistsW(flTo->feFiles[0].szFullPath) &&
1210 IsAttribFile(fileDest->attributes))
1212 bCancelIfAnyDirectories = TRUE;
1215 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
1216 !PathFileExistsW(fileDest->szFullPath))
1218 op->req->fAnyOperationsAborted = TRUE;
1219 return ERROR_CANCELLED;
1222 if (!(op->req->fFlags & FOF_MULTIDESTFILES) && flFrom->dwNumFiles != 1 &&
1223 PathFileExistsW(fileDest->szFullPath) &&
1224 IsAttribFile(fileDest->attributes))
1226 return ERROR_CANCELLED;
1229 for (i = 0; i < flFrom->dwNumFiles; i++)
1231 entryToCopy = &flFrom->feFiles[i];
1233 if (op->req->fFlags & FOF_MULTIDESTFILES)
1234 fileDest = &flTo->feFiles[i];
1236 if (IsAttribDir(entryToCopy->attributes) &&
1237 !lstrcmpiW(entryToCopy->szFullPath, fileDest->szDirectory))
1239 return ERROR_SUCCESS;
1242 if (IsAttribDir(entryToCopy->attributes) && bCancelIfAnyDirectories)
1243 return ERROR_CANCELLED;
1245 create_dest_dirs(fileDest->szDirectory);
1247 if (!lstrcmpiW(entryToCopy->szFullPath, fileDest->szFullPath))
1249 if (IsAttribFile(entryToCopy->attributes))
1250 return ERROR_NO_MORE_SEARCH_HANDLES;
1251 else
1252 return ERROR_SUCCESS;
1255 if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1256 (flFrom->dwNumFiles == 1 && IsAttribDir(fileDest->attributes)))
1258 copy_to_dir(op, entryToCopy, fileDest);
1260 else if (IsAttribDir(entryToCopy->attributes))
1262 copy_dir_to_dir(op, entryToCopy, fileDest->szFullPath);
1264 else
1266 if (!copy_file_to_file(op, entryToCopy->szFullPath, fileDest->szFullPath))
1268 op->req->fAnyOperationsAborted = TRUE;
1269 return ERROR_CANCELLED;
1273 /* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
1274 if (op->bCancelled)
1275 return ERROR_CANCELLED;
1278 /* Vista return code. On XP if the used pressed "No" for the last item,
1279 * ERROR_ARENA_TRASHED would be returned */
1280 return ERROR_SUCCESS;
1283 static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, BOOL fTrash, FILE_LIST *flFrom)
1285 if (flFrom->dwNumFiles > 1)
1287 WCHAR tmp[8];
1288 const WCHAR format[] = {'%','d',0};
1290 wnsprintfW(tmp, sizeof(tmp)/sizeof(tmp[0]), format, flFrom->dwNumFiles);
1291 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_MULTIPLE_ITEM:ASK_DELETE_MULTIPLE_ITEM), tmp, NULL);
1293 else
1295 FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
1297 if (IsAttribFile(fileEntry->attributes))
1298 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FILE:ASK_DELETE_FILE), fileEntry->szFullPath, NULL);
1299 else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1300 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FOLDER:ASK_DELETE_FOLDER), fileEntry->szFullPath, NULL);
1302 return TRUE;
1305 /* the FO_DELETE operation */
1306 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom)
1308 FILE_ENTRY *fileEntry;
1309 DWORD i;
1310 BOOL bPathExists;
1311 BOOL bTrash;
1313 if (!flFrom->dwNumFiles)
1314 return ERROR_SUCCESS;
1316 /* Windows also checks only the first item */
1317 bTrash = (lpFileOp->fFlags & FOF_ALLOWUNDO)
1318 && TRASH_CanTrashFile(flFrom->feFiles[0].szFullPath);
1320 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (!bTrash && lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1321 if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, bTrash, flFrom))
1323 lpFileOp->fAnyOperationsAborted = TRUE;
1324 return 0;
1327 for (i = 0; i < flFrom->dwNumFiles; i++)
1329 bPathExists = TRUE;
1330 fileEntry = &flFrom->feFiles[i];
1332 if (!IsAttribFile(fileEntry->attributes) &&
1333 (lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1334 continue;
1336 if (bTrash)
1338 BOOL bDelete;
1339 if (TRASH_TrashFile(fileEntry->szFullPath))
1340 continue;
1342 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1343 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1344 bDelete = SHELL_ConfirmDialogW(lpFileOp->hwnd, ASK_CANT_TRASH_ITEM, fileEntry->szFullPath, NULL);
1345 else
1346 bDelete = TRUE;
1348 if (!bDelete)
1350 lpFileOp->fAnyOperationsAborted = TRUE;
1351 break;
1355 /* delete the file or directory */
1356 if (IsAttribFile(fileEntry->attributes))
1357 bPathExists = DeleteFileW(fileEntry->szFullPath);
1358 else
1359 bPathExists = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1361 if (!bPathExists)
1362 return ERROR_PATH_NOT_FOUND;
1365 return ERROR_SUCCESS;
1368 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
1370 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1371 SHFILEOPSTRUCTW fileOp;
1373 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1375 if (IsDotDir(feFrom->szFilename))
1376 return;
1378 SHNotifyCreateDirectoryW(szDestPath, NULL);
1380 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1381 szFrom[lstrlenW(szFrom) + 1] = '\0';
1383 lstrcpyW(szTo, szDestPath);
1384 szTo[lstrlenW(szDestPath) + 1] = '\0';
1386 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
1387 fileOp.pFrom = szFrom;
1388 fileOp.pTo = szTo;
1390 SHFileOperationW(&fileOp);
1393 /* moves a file or directory to another directory */
1394 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
1396 WCHAR szDestPath[MAX_PATH];
1398 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1400 if (IsAttribFile(feFrom->attributes))
1401 SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
1402 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1403 move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1406 /* the FO_MOVE operation */
1407 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1409 DWORD i;
1410 FILE_ENTRY *entryToMove;
1411 FILE_ENTRY *fileDest;
1413 if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1414 return ERROR_CANCELLED;
1416 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1417 flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1419 return ERROR_CANCELLED;
1422 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1423 !flFrom->bAnyDirectories &&
1424 flFrom->dwNumFiles > flTo->dwNumFiles)
1426 return ERROR_CANCELLED;
1429 if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1430 return ERROR_CANCELLED;
1432 if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1433 flFrom->dwNumFiles != flTo->dwNumFiles)
1435 return ERROR_CANCELLED;
1438 fileDest = &flTo->feFiles[0];
1439 for (i = 0; i < flFrom->dwNumFiles; i++)
1441 entryToMove = &flFrom->feFiles[i];
1443 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1444 fileDest = &flTo->feFiles[i];
1446 if (!PathFileExistsW(fileDest->szDirectory))
1447 return ERROR_CANCELLED;
1449 if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1450 move_to_dir(lpFileOp, entryToMove, fileDest);
1451 else
1452 SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
1455 return ERROR_SUCCESS;
1458 /* the FO_RENAME files */
1459 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1461 FILE_ENTRY *feFrom;
1462 FILE_ENTRY *feTo;
1464 if (flFrom->dwNumFiles != 1)
1465 return ERROR_GEN_FAILURE;
1467 if (flTo->dwNumFiles != 1)
1468 return ERROR_CANCELLED;
1470 feFrom = &flFrom->feFiles[0];
1471 feTo= &flTo->feFiles[0];
1473 /* fail if destination doesn't exist */
1474 if (!feFrom->bExists)
1475 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1477 /* fail if destination already exists */
1478 if (feTo->bExists)
1479 return ERROR_ALREADY_EXISTS;
1481 return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
1484 /* alert the user if an unsupported flag is used */
1485 static void check_flags(FILEOP_FLAGS fFlags)
1487 WORD wUnsupportedFlags = FOF_NO_CONNECTED_ELEMENTS |
1488 FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1489 FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1491 if (fFlags & wUnsupportedFlags)
1492 FIXME("Unsupported flags: %04x\n", fFlags);
1495 /*************************************************************************
1496 * SHFileOperationW [SHELL32.@]
1498 * See SHFileOperationA
1500 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1502 FILE_OPERATION op;
1503 FILE_LIST flFrom, flTo;
1504 int ret = 0;
1506 if (!lpFileOp)
1507 return ERROR_INVALID_PARAMETER;
1509 check_flags(lpFileOp->fFlags);
1511 ZeroMemory(&flFrom, sizeof(FILE_LIST));
1512 ZeroMemory(&flTo, sizeof(FILE_LIST));
1514 if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1515 return ret;
1517 if (lpFileOp->wFunc != FO_DELETE)
1518 parse_file_list(&flTo, lpFileOp->pTo);
1520 ZeroMemory(&op, sizeof(op));
1521 op.req = lpFileOp;
1522 op.bManyItems = (flFrom.dwNumFiles > 1);
1524 switch (lpFileOp->wFunc)
1526 case FO_COPY:
1527 ret = copy_files(&op, &flFrom, &flTo);
1528 break;
1529 case FO_DELETE:
1530 ret = delete_files(lpFileOp, &flFrom);
1531 break;
1532 case FO_MOVE:
1533 ret = move_files(lpFileOp, &flFrom, &flTo);
1534 break;
1535 case FO_RENAME:
1536 ret = rename_files(lpFileOp, &flFrom, &flTo);
1537 break;
1538 default:
1539 ret = ERROR_INVALID_PARAMETER;
1540 break;
1543 destroy_file_list(&flFrom);
1545 if (lpFileOp->wFunc != FO_DELETE)
1546 destroy_file_list(&flTo);
1548 if (ret == ERROR_CANCELLED)
1549 lpFileOp->fAnyOperationsAborted = TRUE;
1551 return ret;
1554 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1556 /*************************************************************************
1557 * SHFreeNameMappings [shell32.246]
1559 * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1560 * was specified.
1562 * PARAMS
1563 * hNameMapping [I] handle to the name mappings used during renaming of files
1565 * RETURNS
1566 * Nothing
1568 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1570 if (hNameMapping)
1572 int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1574 for (; i>= 0; i--)
1576 LPSHNAMEMAPPINGW lp = DSA_GetItemPtr((HDSA)hNameMapping, i);
1578 SHFree(lp->pszOldPath);
1579 SHFree(lp->pszNewPath);
1581 DSA_Destroy((HDSA)hNameMapping);
1585 /*************************************************************************
1586 * SheGetDirA [SHELL32.@]
1589 HRESULT WINAPI SheGetDirA(LPSTR u, LPSTR v)
1590 { FIXME("%p %p stub\n",u,v);
1591 return 0;
1594 /*************************************************************************
1595 * SheGetDirW [SHELL32.@]
1598 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1599 { FIXME("%p %p stub\n",u,v);
1600 return 0;
1603 /*************************************************************************
1604 * SheChangeDirA [SHELL32.@]
1607 HRESULT WINAPI SheChangeDirA(LPSTR u)
1608 { FIXME("(%s),stub\n",debugstr_a(u));
1609 return 0;
1612 /*************************************************************************
1613 * SheChangeDirW [SHELL32.@]
1616 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1617 { FIXME("(%s),stub\n",debugstr_w(u));
1618 return 0;
1621 /*************************************************************************
1622 * IsNetDrive [SHELL32.66]
1624 BOOL WINAPI IsNetDrive(DWORD drive)
1626 char root[4];
1627 strcpy(root, "A:\\");
1628 root[0] += (char)drive;
1629 return (GetDriveTypeA(root) == DRIVE_REMOTE);
1633 /*************************************************************************
1634 * RealDriveType [SHELL32.524]
1636 INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1638 char root[] = "A:\\";
1639 root[0] += (char)drive;
1640 return GetDriveTypeA(root);