kernel32: Get rid of the last parameter to PROFILE_CopyEntry().
[wine.git] / dlls / shell32 / shlfileop.c
blobb941db39144554832122c759de69a5eae3d238a4
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 #define DE_SAMEFILE 0x71
57 #define DE_DESTSAMETREE 0x7D
59 static const WCHAR wWildcardFile[] = {'*',0};
60 static const WCHAR wWildcardChars[] = {'*','?',0};
62 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec);
63 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec);
64 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path);
65 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path);
66 static DWORD SHNotifyDeleteFileA(LPCSTR path);
67 static DWORD SHNotifyDeleteFileW(LPCWSTR path);
68 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest);
69 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists);
70 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly);
72 typedef struct
74 SHFILEOPSTRUCTW *req;
75 DWORD dwYesToAllMask;
76 BOOL bManyItems;
77 BOOL bCancelled;
78 } FILE_OPERATION;
80 /* Confirm dialogs with an optional "Yes To All" as used in file operations confirmations
82 static const WCHAR CONFIRM_MSG_PROP[] = {'W','I','N','E','_','C','O','N','F','I','R','M',0};
84 struct confirm_msg_info
86 LPWSTR lpszText;
87 LPWSTR lpszCaption;
88 HICON hIcon;
89 BOOL bYesToAll;
92 /* as some buttons may be hidden and the dialog height may change we may need
93 * to move the controls */
94 static void confirm_msg_move_button(HWND hDlg, INT iId, INT *xPos, INT yOffset, BOOL bShow)
96 HWND hButton = GetDlgItem(hDlg, iId);
97 RECT r;
99 if (bShow) {
100 int width;
102 GetWindowRect(hButton, &r);
103 MapWindowPoints( 0, hDlg, (POINT *)&r, 2 );
104 width = r.right - r.left;
105 SetWindowPos(hButton, 0, *xPos - width, r.top - yOffset, 0, 0,
106 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW );
107 *xPos -= width + 5;
109 else
110 ShowWindow(hButton, SW_HIDE);
113 /* Note: we paint the text manually and don't use the static control to make
114 * sure the text has the same height as the one computed in WM_INITDIALOG
116 static INT_PTR ConfirmMsgBox_Paint(HWND hDlg)
118 PAINTSTRUCT ps;
119 HFONT hOldFont;
120 RECT r;
121 HDC hdc;
123 BeginPaint(hDlg, &ps);
124 hdc = ps.hdc;
125 SetBkMode(hdc, TRANSPARENT);
127 GetClientRect(GetDlgItem(hDlg, IDD_MESSAGE), &r);
128 /* this will remap the rect to dialog coords */
129 MapWindowPoints(GetDlgItem(hDlg, IDD_MESSAGE), hDlg, (LPPOINT)&r, 2);
130 hOldFont = SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDD_MESSAGE, WM_GETFONT, 0, 0));
131 DrawTextW(hdc, GetPropW(hDlg, CONFIRM_MSG_PROP), -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK);
132 SelectObject(hdc, hOldFont);
133 EndPaint(hDlg, &ps);
134 return TRUE;
137 static INT_PTR ConfirmMsgBox_Init(HWND hDlg, LPARAM lParam)
139 struct confirm_msg_info *info = (struct confirm_msg_info *)lParam;
140 INT xPos, yOffset;
141 int width, height;
142 HFONT hOldFont;
143 HDC hdc;
144 RECT r;
146 SetWindowTextW(hDlg, info->lpszCaption);
147 ShowWindow(GetDlgItem(hDlg, IDD_MESSAGE), SW_HIDE);
148 SetPropW(hDlg, CONFIRM_MSG_PROP, info->lpszText);
149 SendDlgItemMessageW(hDlg, IDD_ICON, STM_SETICON, (WPARAM)info->hIcon, 0);
151 /* compute the text height and resize the dialog */
152 GetClientRect(GetDlgItem(hDlg, IDD_MESSAGE), &r);
153 hdc = GetDC(hDlg);
154 yOffset = r.bottom;
155 hOldFont = SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDD_MESSAGE, WM_GETFONT, 0, 0));
156 DrawTextW(hdc, info->lpszText, -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK | DT_CALCRECT);
157 SelectObject(hdc, hOldFont);
158 yOffset -= r.bottom;
159 yOffset = min(yOffset, 35); /* don't make the dialog too small */
160 ReleaseDC(hDlg, hdc);
162 GetClientRect(hDlg, &r);
163 xPos = r.right - 7;
164 GetWindowRect(hDlg, &r);
165 width = r.right - r.left;
166 height = r.bottom - r.top - yOffset;
167 MoveWindow(hDlg, (GetSystemMetrics(SM_CXSCREEN) - width)/2,
168 (GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
170 confirm_msg_move_button(hDlg, IDCANCEL, &xPos, yOffset, info->bYesToAll);
171 confirm_msg_move_button(hDlg, IDNO, &xPos, yOffset, TRUE);
172 confirm_msg_move_button(hDlg, IDD_YESTOALL, &xPos, yOffset, info->bYesToAll);
173 confirm_msg_move_button(hDlg, IDYES, &xPos, yOffset, TRUE);
174 return TRUE;
177 static INT_PTR CALLBACK ConfirmMsgBoxProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
179 switch (uMsg)
181 case WM_INITDIALOG:
182 return ConfirmMsgBox_Init(hDlg, lParam);
183 case WM_PAINT:
184 return ConfirmMsgBox_Paint(hDlg);
185 case WM_COMMAND:
186 EndDialog(hDlg, wParam);
187 break;
188 case WM_CLOSE:
189 EndDialog(hDlg, IDCANCEL);
190 break;
192 return FALSE;
195 static int SHELL_ConfirmMsgBox(HWND hWnd, LPWSTR lpszText, LPWSTR lpszCaption, HICON hIcon, BOOL bYesToAll)
197 static const WCHAR wszTemplate[] = {'S','H','E','L','L','_','Y','E','S','T','O','A','L','L','_','M','S','G','B','O','X',0};
198 struct confirm_msg_info info;
200 info.lpszText = lpszText;
201 info.lpszCaption = lpszCaption;
202 info.hIcon = hIcon;
203 info.bYesToAll = bYesToAll;
204 return DialogBoxParamW(shell32_hInstance, wszTemplate, hWnd, ConfirmMsgBoxProc, (LPARAM)&info);
207 /* confirmation dialogs content */
208 typedef struct
210 HINSTANCE hIconInstance;
211 UINT icon_resource_id;
212 UINT caption_resource_id, text_resource_id;
213 } SHELL_ConfirmIDstruc;
215 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
217 ids->hIconInstance = shell32_hInstance;
218 switch (nKindOfDialog) {
219 case ASK_DELETE_FILE:
220 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
221 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
222 ids->text_resource_id = IDS_DELETEITEM_TEXT;
223 return TRUE;
224 case ASK_DELETE_FOLDER:
225 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
226 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
227 ids->text_resource_id = IDS_DELETEITEM_TEXT;
228 return TRUE;
229 case ASK_DELETE_MULTIPLE_ITEM:
230 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
231 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
232 ids->text_resource_id = IDS_DELETEMULTIPLE_TEXT;
233 return TRUE;
234 case ASK_TRASH_FILE:
235 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
236 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
237 ids->text_resource_id = IDS_TRASHITEM_TEXT;
238 return TRUE;
239 case ASK_TRASH_FOLDER:
240 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
241 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
242 ids->text_resource_id = IDS_TRASHFOLDER_TEXT;
243 return TRUE;
244 case ASK_TRASH_MULTIPLE_ITEM:
245 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
246 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
247 ids->text_resource_id = IDS_TRASHMULTIPLE_TEXT;
248 return TRUE;
249 case ASK_CANT_TRASH_ITEM:
250 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
251 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
252 ids->text_resource_id = IDS_CANTTRASH_TEXT;
253 return TRUE;
254 case ASK_DELETE_SELECTED:
255 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
256 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
257 ids->text_resource_id = IDS_DELETESELECTED_TEXT;
258 return TRUE;
259 case ASK_OVERWRITE_FILE:
260 ids->hIconInstance = NULL;
261 ids->icon_resource_id = IDI_WARNING;
262 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
263 ids->text_resource_id = IDS_OVERWRITEFILE_TEXT;
264 return TRUE;
265 case ASK_OVERWRITE_FOLDER:
266 ids->hIconInstance = NULL;
267 ids->icon_resource_id = IDI_WARNING;
268 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
269 ids->text_resource_id = IDS_OVERWRITEFOLDER_TEXT;
270 return TRUE;
271 default:
272 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
274 return FALSE;
277 static BOOL SHELL_ConfirmDialogW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir, FILE_OPERATION *op)
279 WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
280 SHELL_ConfirmIDstruc ids;
281 DWORD_PTR args[1];
282 HICON hIcon;
283 int ret;
285 assert(nKindOfDialog >= 0 && nKindOfDialog < 32);
286 if (op && (op->dwYesToAllMask & (1 << nKindOfDialog)))
287 return TRUE;
289 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids)) return FALSE;
291 LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, ARRAY_SIZE(szCaption));
292 LoadStringW(shell32_hInstance, ids.text_resource_id, szText, ARRAY_SIZE(szText));
294 args[0] = (DWORD_PTR)szDir;
295 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
296 szText, 0, 0, szBuffer, ARRAY_SIZE(szBuffer), (__ms_va_list*)args);
298 hIcon = LoadIconW(ids.hIconInstance, (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id));
300 ret = SHELL_ConfirmMsgBox(hWnd, szBuffer, szCaption, hIcon, op && op->bManyItems);
301 if (op) {
302 if (ret == IDD_YESTOALL) {
303 op->dwYesToAllMask |= (1 << nKindOfDialog);
304 ret = IDYES;
306 if (ret == IDCANCEL)
307 op->bCancelled = TRUE;
308 if (ret != IDYES)
309 op->req->fAnyOperationsAborted = TRUE;
311 return ret == IDYES;
314 BOOL SHELL_ConfirmYesNoW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir)
316 return SHELL_ConfirmDialogW(hWnd, nKindOfDialog, szDir, NULL);
319 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
321 DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
323 if (len < minChars)
324 len = minChars;
326 *wPath = heap_alloc(len * sizeof(WCHAR));
327 if (*wPath)
329 MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
330 return NO_ERROR;
332 return E_OUTOFMEMORY;
335 HRESULT WINAPI SHIsFileAvailableOffline(LPCWSTR path, LPDWORD status)
337 FIXME("(%s, %p) stub\n", debugstr_w(path), status);
338 return E_FAIL;
341 /**************************************************************************
342 * SHELL_DeleteDirectory() [internal]
344 * Asks for confirmation when bShowUI is true and deletes the directory and
345 * all its subdirectories and files if necessary.
347 static DWORD SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
349 DWORD ret = 0;
350 HANDLE hFind;
351 WIN32_FIND_DATAW wfd;
352 WCHAR szTemp[MAX_PATH];
354 PathCombineW(szTemp, pszDir, wWildcardFile);
355 hFind = FindFirstFileW(szTemp, &wfd);
357 if (hFind != INVALID_HANDLE_VALUE) {
358 if (!bShowUI || SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir, NULL)) {
359 do {
360 if (IsDotDir(wfd.cFileName))
361 continue;
362 PathCombineW(szTemp, pszDir, wfd.cFileName);
363 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
364 ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
365 else
366 ret = SHNotifyDeleteFileW(szTemp);
367 } while (!ret && FindNextFileW(hFind, &wfd));
369 FindClose(hFind);
371 if (ret == ERROR_SUCCESS)
372 ret = SHNotifyRemoveDirectoryW(pszDir);
374 return ret == ERROR_PATH_NOT_FOUND ?
375 0x7C: /* DE_INVALIDFILES (legacy Windows error) */
376 ret;
379 /**************************************************************************
380 * Win32CreateDirectory [SHELL32.93]
382 * Creates a directory. Also triggers a change notify if one exists.
384 * PARAMS
385 * path [I] path to directory to create
387 * RETURNS
388 * TRUE if successful, FALSE otherwise
390 * NOTES
391 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
392 * This is Unicode on NT/2000
394 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
396 LPWSTR wPath;
397 DWORD retCode;
399 TRACE("(%s, %p)\n", debugstr_a(path), sec);
401 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
402 if (!retCode)
404 retCode = SHNotifyCreateDirectoryW(wPath, sec);
405 heap_free(wPath);
407 return retCode;
410 /**********************************************************************/
412 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
414 TRACE("(%s, %p)\n", debugstr_w(path), sec);
416 if (CreateDirectoryW(path, sec))
418 SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
419 return ERROR_SUCCESS;
421 return GetLastError();
424 /**********************************************************************/
426 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
428 if (SHELL_OsIsUnicode())
429 return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
430 return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
433 /************************************************************************
434 * Win32RemoveDirectory [SHELL32.94]
436 * Deletes a directory. Also triggers a change notify if one exists.
438 * PARAMS
439 * path [I] path to directory to delete
441 * RETURNS
442 * TRUE if successful, FALSE otherwise
444 * NOTES
445 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
446 * This is Unicode on NT/2000
448 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
450 LPWSTR wPath;
451 DWORD retCode;
453 TRACE("(%s)\n", debugstr_a(path));
455 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
456 if (!retCode)
458 retCode = SHNotifyRemoveDirectoryW(wPath);
459 heap_free(wPath);
461 return retCode;
464 /***********************************************************************/
466 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
468 BOOL ret;
469 TRACE("(%s)\n", debugstr_w(path));
471 ret = RemoveDirectoryW(path);
472 if (!ret)
474 /* Directory may be write protected */
475 DWORD dwAttr = GetFileAttributesW(path);
476 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
477 if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
478 ret = RemoveDirectoryW(path);
480 if (ret)
482 SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
483 return ERROR_SUCCESS;
485 return GetLastError();
488 /***********************************************************************/
490 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
492 if (SHELL_OsIsUnicode())
493 return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
494 return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
497 /************************************************************************
498 * Win32DeleteFile [SHELL32.164]
500 * Deletes a file. Also triggers a change notify if one exists.
502 * PARAMS
503 * path [I] path to file to delete
505 * RETURNS
506 * TRUE if successful, FALSE otherwise
508 * NOTES
509 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
510 * This is Unicode on NT/2000
512 static DWORD SHNotifyDeleteFileA(LPCSTR path)
514 LPWSTR wPath;
515 DWORD retCode;
517 TRACE("(%s)\n", debugstr_a(path));
519 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
520 if (!retCode)
522 retCode = SHNotifyDeleteFileW(wPath);
523 heap_free(wPath);
525 return retCode;
528 /***********************************************************************/
530 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
532 BOOL ret;
534 TRACE("(%s)\n", debugstr_w(path));
536 ret = DeleteFileW(path);
537 if (!ret)
539 /* File may be write protected or a system file */
540 DWORD dwAttr = GetFileAttributesW(path);
541 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
542 if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
543 ret = DeleteFileW(path);
545 if (ret)
547 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
548 return ERROR_SUCCESS;
550 return GetLastError();
553 /***********************************************************************/
555 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
557 if (SHELL_OsIsUnicode())
558 return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
559 return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
562 /************************************************************************
563 * SHNotifyMoveFile [internal]
565 * Moves a file. Also triggers a change notify if one exists.
567 * PARAMS
568 * src [I] path to source file to move
569 * dest [I] path to target file to move to
571 * RETURNS
572 * ERROR_SUCCESS if successful
574 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
576 BOOL ret;
578 TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
580 ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
582 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
583 if (!ret)
584 ret = MoveFileW(src, dest);
586 if (!ret)
588 DWORD dwAttr;
590 dwAttr = SHFindAttrW(dest, FALSE);
591 if (INVALID_FILE_ATTRIBUTES == dwAttr)
593 /* Source file may be write protected or a system file */
594 dwAttr = GetFileAttributesW(src);
595 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
596 if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
597 ret = MoveFileW(src, dest);
600 if (ret)
602 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
603 return ERROR_SUCCESS;
605 return GetLastError();
608 /************************************************************************
609 * SHNotifyCopyFile [internal]
611 * Copies a file. Also triggers a change notify if one exists.
613 * PARAMS
614 * src [I] path to source file to move
615 * dest [I] path to target file to move to
616 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
617 * a file with this name already exists
619 * RETURNS
620 * ERROR_SUCCESS if successful
622 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
624 BOOL ret;
625 DWORD attribs;
627 TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
629 /* Destination file may already exist with read only attribute */
630 attribs = GetFileAttributesW(dest);
631 if (IsAttrib(attribs, FILE_ATTRIBUTE_READONLY))
632 SetFileAttributesW(dest, attribs & ~FILE_ATTRIBUTE_READONLY);
634 ret = CopyFileW(src, dest, bFailIfExists);
635 if (ret)
637 SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
638 return ERROR_SUCCESS;
641 return GetLastError();
644 /*************************************************************************
645 * SHCreateDirectory [SHELL32.165]
647 * This function creates a file system folder whose fully qualified path is
648 * given by path. If one or more of the intermediate folders do not exist,
649 * they will be created as well.
651 * PARAMS
652 * hWnd [I]
653 * path [I] path of directory to create
655 * RETURNS
656 * ERROR_SUCCESS or one of the following values:
657 * ERROR_BAD_PATHNAME if the path is relative
658 * ERROR_FILE_EXISTS when a file with that name exists
659 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
660 * ERROR_INVALID_NAME if the path contains invalid chars
661 * ERROR_ALREADY_EXISTS when the directory already exists
662 * ERROR_FILENAME_EXCED_RANGE if the filename was too long to process
664 * NOTES
665 * exported by ordinal
666 * Win9x exports ANSI
667 * WinNT/2000 exports Unicode
669 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
671 if (SHELL_OsIsUnicode())
672 return SHCreateDirectoryExW(hWnd, path, NULL);
673 return SHCreateDirectoryExA(hWnd, path, NULL);
676 /*************************************************************************
677 * SHCreateDirectoryExA [SHELL32.@]
679 * This function creates a file system folder whose fully qualified path is
680 * given by path. If one or more of the intermediate folders do not exist,
681 * they will be created as well.
683 * PARAMS
684 * hWnd [I]
685 * path [I] path of directory to create
686 * sec [I] security attributes to use or NULL
688 * RETURNS
689 * ERROR_SUCCESS or one of the following values:
690 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
691 * ERROR_INVALID_NAME if the path contains invalid chars
692 * ERROR_FILE_EXISTS when a file with that name exists
693 * ERROR_ALREADY_EXISTS when the directory already exists
694 * ERROR_FILENAME_EXCED_RANGE if the filename was too long to process
696 * FIXME: Not implemented yet;
697 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
698 * if the path is a network path to deal with network drivers which might have a limited
699 * but unknown maximum path length. If not:
701 * If hWnd is set to a valid window handle, a message box is displayed warning
702 * the user that the files may not be accessible. If the user chooses not to
703 * proceed, the function returns ERROR_CANCELLED.
705 * If hWnd is set to NULL, no user interface is displayed and the function
706 * returns ERROR_CANCELLED.
708 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
710 LPWSTR wPath;
711 DWORD retCode;
713 TRACE("(%s, %p)\n", debugstr_a(path), sec);
715 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
716 if (!retCode)
718 retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
719 heap_free(wPath);
721 return retCode;
724 /*************************************************************************
725 * SHCreateDirectoryExW [SHELL32.@]
727 * See SHCreateDirectoryExA.
729 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
731 int ret = ERROR_BAD_PATHNAME;
732 TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
734 if (PathIsRelativeW(path))
736 SetLastError(ret);
738 else
740 ret = SHNotifyCreateDirectoryW(path, sec);
741 /* Refuse to work on certain error codes before trying to create directories recursively */
742 if (ret != ERROR_SUCCESS &&
743 ret != ERROR_FILE_EXISTS &&
744 ret != ERROR_ALREADY_EXISTS &&
745 ret != ERROR_FILENAME_EXCED_RANGE)
747 WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1]; /* extra for PathAddBackslash() */
749 lstrcpynW(szTemp, path, MAX_PATH);
750 pEnd = PathAddBackslashW(szTemp);
751 pSlash = szTemp + 3;
753 while (*pSlash)
755 while (*pSlash && *pSlash != '\\') pSlash++;
756 if (*pSlash)
758 *pSlash = 0; /* terminate path at separator */
760 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
762 *pSlash++ = '\\'; /* put the separator back */
766 if (ret && hWnd &&
767 ret != ERROR_CANCELLED &&
768 ret != ERROR_ALREADY_EXISTS)
770 /* We failed and should show a dialog box */
771 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
772 ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
775 return ret;
778 /*************************************************************************
779 * SHFindAttrW [internal]
781 * Get the Attributes for a file or directory. The difference to GetAttributes()
782 * is that this function will also work for paths containing wildcard characters
783 * in its filename.
785 * PARAMS
786 * path [I] path of directory or file to check
787 * fileOnly [I] TRUE if only files should be found
789 * RETURNS
790 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
791 * the first file or directory found otherwise
793 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
795 WIN32_FIND_DATAW wfd;
796 BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
797 DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
798 HANDLE hFind = FindFirstFileW(pName, &wfd);
800 TRACE("%s %d\n", debugstr_w(pName), fileOnly);
801 if (INVALID_HANDLE_VALUE != hFind)
805 if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
806 continue;
807 dwAttr = wfd.dwFileAttributes;
808 break;
810 while (FindNextFileW(hFind, &wfd));
811 FindClose(hFind);
813 return dwAttr;
816 /*************************************************************************
818 * SHNameTranslate HelperFunction for SHFileOperationA
820 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
821 * is NULL, only the necessary size of the string is determined and returned,
822 * otherwise the ASCII strings are copied into it and the buffer is increased
823 * to point to the location after the final 0 termination char.
825 static DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
827 DWORD size = 0, aSize = 0;
828 LPCSTR aString = (LPCSTR)*pWToFrom;
830 if (aString)
834 size = lstrlenA(aString) + 1;
835 aSize += size;
836 aString += size;
837 } while ((size != 1) && more);
838 /* The two sizes might be different in the case of multibyte chars */
839 size = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, 0);
840 if (*wString) /* only in the second loop */
842 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
843 *pWToFrom = *wString;
844 *wString += size;
847 return size;
849 /*************************************************************************
850 * SHFileOperationA [SHELL32.@]
852 * Function to copy, move, delete and create one or more files with optional
853 * user prompts.
855 * PARAMS
856 * lpFileOp [I/O] pointer to a structure containing all the necessary information
858 * RETURNS
859 * Success: ERROR_SUCCESS.
860 * Failure: ERROR_CANCELLED.
862 * NOTES
863 * exported by name
865 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
867 SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
868 int retCode = 0;
869 DWORD size;
870 LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
871 wString = NULL; /* we change this in SHNameTranslate */
873 TRACE("\n");
874 if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
875 nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
876 if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
877 nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
878 while (1) /* every loop calculate size, second translate also, if we have storage for this */
880 size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
881 size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
882 size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
884 if (ForFree)
886 retCode = SHFileOperationW(&nFileOp);
887 /* Windows 95/98 returns S_OK for this case. */
888 if (retCode == ERROR_ACCESS_DENIED && (GetVersion() & 0x80000000))
889 retCode = S_OK;
891 heap_free(ForFree); /* we cannot use wString, it was changed */
892 break;
894 else
896 wString = ForFree = heap_alloc(size * sizeof(WCHAR));
897 if (ForFree) continue;
898 retCode = ERROR_OUTOFMEMORY;
899 nFileOp.fAnyOperationsAborted = TRUE;
900 return retCode;
904 lpFileOp->hNameMappings = nFileOp.hNameMappings;
905 lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
906 return retCode;
909 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
911 typedef struct
913 DWORD attributes;
914 LPWSTR szDirectory;
915 LPWSTR szFilename;
916 LPWSTR szFullPath;
917 BOOL bFromWildcard;
918 BOOL bFromRelative;
919 BOOL bExists;
920 } FILE_ENTRY;
922 typedef struct
924 FILE_ENTRY *feFiles;
925 DWORD num_alloc;
926 DWORD dwNumFiles;
927 BOOL bAnyFromWildcard;
928 BOOL bAnyDirectories;
929 BOOL bAnyDontExist;
930 } FILE_LIST;
933 static inline void grow_list(FILE_LIST *list)
935 FILE_ENTRY *new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
936 list->num_alloc * 2 * sizeof(*new) );
937 list->feFiles = new;
938 list->num_alloc *= 2;
941 /* adds a file to the FILE_ENTRY struct
943 static void add_file_to_entry(FILE_ENTRY *feFile, LPCWSTR szFile)
945 DWORD dwLen = lstrlenW(szFile) + 1;
946 LPCWSTR ptr;
948 feFile->szFullPath = heap_alloc(dwLen * sizeof(WCHAR));
949 lstrcpyW(feFile->szFullPath, szFile);
951 ptr = StrRChrW(szFile, NULL, '\\');
952 if (ptr)
954 dwLen = ptr - szFile + 1;
955 feFile->szDirectory = heap_alloc(dwLen * sizeof(WCHAR));
956 lstrcpynW(feFile->szDirectory, szFile, dwLen);
958 dwLen = lstrlenW(feFile->szFullPath) - dwLen + 1;
959 feFile->szFilename = heap_alloc(dwLen * sizeof(WCHAR));
960 lstrcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
962 feFile->bFromWildcard = FALSE;
965 static LPWSTR wildcard_to_file(LPCWSTR szWildCard, LPCWSTR szFileName)
967 LPCWSTR ptr;
968 LPWSTR szFullPath;
969 DWORD dwDirLen, dwFullLen;
971 ptr = StrRChrW(szWildCard, NULL, '\\');
972 dwDirLen = ptr - szWildCard + 1;
974 dwFullLen = dwDirLen + lstrlenW(szFileName) + 1;
975 szFullPath = heap_alloc(dwFullLen * sizeof(WCHAR));
977 lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
978 lstrcatW(szFullPath, szFileName);
980 return szFullPath;
983 static void parse_wildcard_files(FILE_LIST *flList, LPCWSTR szFile, LPDWORD pdwListIndex)
985 WIN32_FIND_DATAW wfd;
986 HANDLE hFile = FindFirstFileW(szFile, &wfd);
987 FILE_ENTRY *file;
988 LPWSTR szFullPath;
989 BOOL res;
991 if (hFile == INVALID_HANDLE_VALUE) return;
993 for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
995 if (IsDotDir(wfd.cFileName)) continue;
996 if (*pdwListIndex >= flList->num_alloc) grow_list( flList );
997 szFullPath = wildcard_to_file(szFile, wfd.cFileName);
998 file = &flList->feFiles[(*pdwListIndex)++];
999 add_file_to_entry(file, szFullPath);
1000 file->bFromWildcard = TRUE;
1001 file->attributes = wfd.dwFileAttributes;
1002 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
1003 heap_free(szFullPath);
1006 FindClose(hFile);
1009 /* takes the null-separated file list and fills out the FILE_LIST */
1010 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
1012 LPCWSTR ptr = szFiles;
1013 WCHAR szCurFile[MAX_PATH];
1014 WCHAR *p;
1015 DWORD i = 0;
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 = heap_alloc_zero(flList->num_alloc * sizeof(FILE_ENTRY));
1032 while (*ptr)
1034 if (i >= flList->num_alloc) grow_list( flList );
1036 /* change relative to absolute path */
1037 if (PathIsRelativeW(ptr))
1039 GetCurrentDirectoryW(MAX_PATH, szCurFile);
1040 PathCombineW(szCurFile, szCurFile, ptr);
1041 flList->feFiles[i].bFromRelative = TRUE;
1043 else
1045 lstrcpyW(szCurFile, ptr);
1046 flList->feFiles[i].bFromRelative = FALSE;
1049 for (p = szCurFile; *p; p++) if (*p == '/') *p = '\\';
1051 /* parse wildcard files if they are in the filename */
1052 if (StrPBrkW(szCurFile, wWildcardChars))
1054 parse_wildcard_files(flList, szCurFile, &i);
1055 flList->bAnyFromWildcard = TRUE;
1056 i--;
1058 else
1060 FILE_ENTRY *file = &flList->feFiles[i];
1061 add_file_to_entry(file, szCurFile);
1062 file->attributes = GetFileAttributesW( file->szFullPath );
1063 file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
1064 if (!file->bExists) flList->bAnyDontExist = TRUE;
1065 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
1068 /* advance to the next string */
1069 ptr += lstrlenW(ptr) + 1;
1070 i++;
1072 flList->dwNumFiles = i;
1074 return S_OK;
1077 /* free the FILE_LIST */
1078 static void destroy_file_list(FILE_LIST *flList)
1080 DWORD i;
1082 if (!flList || !flList->feFiles)
1083 return;
1085 for (i = 0; i < flList->dwNumFiles; i++)
1087 heap_free(flList->feFiles[i].szDirectory);
1088 heap_free(flList->feFiles[i].szFilename);
1089 heap_free(flList->feFiles[i].szFullPath);
1092 heap_free(flList->feFiles);
1095 static void copy_dir_to_dir(FILE_OPERATION *op, const FILE_ENTRY *feFrom, LPCWSTR szDestPath)
1097 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1098 SHFILEOPSTRUCTW fileOp;
1100 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1102 if (IsDotDir(feFrom->szFilename))
1103 return;
1105 if (PathFileExistsW(szDestPath))
1106 PathCombineW(szTo, szDestPath, feFrom->szFilename);
1107 else
1108 lstrcpyW(szTo, szDestPath);
1110 if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo)) {
1111 if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FOLDER, feFrom->szFilename, op))
1113 /* Vista returns an ERROR_CANCELLED even if user pressed "No" */
1114 if (!op->bManyItems)
1115 op->bCancelled = TRUE;
1116 return;
1120 szTo[lstrlenW(szTo) + 1] = '\0';
1121 SHNotifyCreateDirectoryW(szTo, NULL);
1123 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1124 szFrom[lstrlenW(szFrom) + 1] = '\0';
1126 fileOp = *op->req;
1127 fileOp.pFrom = szFrom;
1128 fileOp.pTo = szTo;
1129 fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
1131 /* Don't ask the user about overwriting files when he accepted to overwrite the
1132 folder. FIXME: this is not exactly what Windows does - e.g. there would be
1133 an additional confirmation for a nested folder */
1134 fileOp.fFlags |= FOF_NOCONFIRMATION;
1136 SHFileOperationW(&fileOp);
1139 static BOOL copy_file_to_file(FILE_OPERATION *op, const WCHAR *szFrom, const WCHAR *szTo)
1141 if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo))
1143 if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FILE, PathFindFileNameW(szTo), op))
1144 return FALSE;
1147 return SHNotifyCopyFileW(szFrom, szTo, FALSE) == 0;
1150 /* copy a file or directory to another directory */
1151 static void copy_to_dir(FILE_OPERATION *op, const FILE_ENTRY *feFrom, const FILE_ENTRY *feTo)
1153 if (!PathFileExistsW(feTo->szFullPath))
1154 SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
1156 if (IsAttribFile(feFrom->attributes))
1158 WCHAR szDestPath[MAX_PATH];
1160 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1161 copy_file_to_file(op, feFrom->szFullPath, szDestPath);
1163 else if (!(op->req->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1164 copy_dir_to_dir(op, feFrom, feTo->szFullPath);
1167 static void create_dest_dirs(LPCWSTR szDestDir)
1169 WCHAR dir[MAX_PATH];
1170 LPCWSTR ptr = StrChrW(szDestDir, '\\');
1172 /* make sure all directories up to last one are created */
1173 while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
1175 lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
1177 if (!PathFileExistsW(dir))
1178 SHNotifyCreateDirectoryW(dir, NULL);
1181 /* create last directory */
1182 if (!PathFileExistsW(szDestDir))
1183 SHNotifyCreateDirectoryW(szDestDir, NULL);
1186 /* the FO_COPY operation */
1187 static int copy_files(FILE_OPERATION *op, const FILE_LIST *flFrom, FILE_LIST *flTo)
1189 DWORD i;
1190 const FILE_ENTRY *entryToCopy;
1191 const FILE_ENTRY *fileDest = &flTo->feFiles[0];
1193 if (flFrom->bAnyDontExist)
1194 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1196 if (flTo->dwNumFiles == 0)
1198 /* If the destination is empty, SHFileOperation should use the current directory */
1199 WCHAR curdir[MAX_PATH+1];
1201 GetCurrentDirectoryW(MAX_PATH, curdir);
1202 curdir[lstrlenW(curdir)+1] = 0;
1204 destroy_file_list(flTo);
1205 ZeroMemory(flTo, sizeof(FILE_LIST));
1206 parse_file_list(flTo, curdir);
1207 fileDest = &flTo->feFiles[0];
1210 if (op->req->fFlags & FOF_MULTIDESTFILES)
1212 if (flFrom->bAnyFromWildcard)
1213 return ERROR_CANCELLED;
1215 if (flFrom->dwNumFiles != flTo->dwNumFiles)
1217 if (flFrom->dwNumFiles != 1 && !IsAttribDir(fileDest->attributes))
1218 return ERROR_CANCELLED;
1220 /* Free all but the first entry. */
1221 for (i = 1; i < flTo->dwNumFiles; i++)
1223 heap_free(flTo->feFiles[i].szDirectory);
1224 heap_free(flTo->feFiles[i].szFilename);
1225 heap_free(flTo->feFiles[i].szFullPath);
1228 flTo->dwNumFiles = 1;
1230 else if (IsAttribDir(fileDest->attributes))
1232 for (i = 1; i < flTo->dwNumFiles; i++)
1233 if (!IsAttribDir(flTo->feFiles[i].attributes) ||
1234 !IsAttribDir(flFrom->feFiles[i].attributes))
1236 return ERROR_CANCELLED;
1240 else if (flFrom->dwNumFiles != 1)
1242 if (flTo->dwNumFiles != 1 && !IsAttribDir(fileDest->attributes))
1243 return ERROR_CANCELLED;
1245 if (PathFileExistsW(fileDest->szFullPath) &&
1246 IsAttribFile(fileDest->attributes))
1248 return ERROR_CANCELLED;
1251 if (flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
1252 !PathFileExistsW(fileDest->szFullPath))
1254 return ERROR_CANCELLED;
1258 for (i = 0; i < flFrom->dwNumFiles; i++)
1260 entryToCopy = &flFrom->feFiles[i];
1262 if ((op->req->fFlags & FOF_MULTIDESTFILES) &&
1263 flTo->dwNumFiles > 1)
1265 fileDest = &flTo->feFiles[i];
1268 if (IsAttribDir(entryToCopy->attributes) &&
1269 !lstrcmpiW(entryToCopy->szFullPath, fileDest->szDirectory))
1271 return ERROR_SUCCESS;
1274 create_dest_dirs(fileDest->szDirectory);
1276 if (!lstrcmpiW(entryToCopy->szFullPath, fileDest->szFullPath))
1278 if (IsAttribFile(entryToCopy->attributes))
1279 return ERROR_NO_MORE_SEARCH_HANDLES;
1280 else
1281 return ERROR_SUCCESS;
1284 if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1285 IsAttribDir(fileDest->attributes))
1287 copy_to_dir(op, entryToCopy, fileDest);
1289 else if (IsAttribDir(entryToCopy->attributes))
1291 copy_dir_to_dir(op, entryToCopy, fileDest->szFullPath);
1293 else
1295 if (!copy_file_to_file(op, entryToCopy->szFullPath, fileDest->szFullPath))
1297 op->req->fAnyOperationsAborted = TRUE;
1298 return ERROR_CANCELLED;
1302 /* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
1303 if (op->bCancelled)
1304 return ERROR_CANCELLED;
1307 /* Vista return code. On XP if the used pressed "No" for the last item,
1308 * ERROR_ARENA_TRASHED would be returned */
1309 return ERROR_SUCCESS;
1312 static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, BOOL fTrash, const FILE_LIST *flFrom)
1314 if (flFrom->dwNumFiles > 1)
1316 static const WCHAR format[] = {'%','d',0};
1317 WCHAR tmp[8];
1319 wnsprintfW(tmp, ARRAY_SIZE(tmp), format, flFrom->dwNumFiles);
1320 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_MULTIPLE_ITEM:ASK_DELETE_MULTIPLE_ITEM), tmp, NULL);
1322 else
1324 const FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
1326 if (IsAttribFile(fileEntry->attributes))
1327 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FILE:ASK_DELETE_FILE), fileEntry->szFullPath, NULL);
1328 else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1329 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FOLDER:ASK_DELETE_FOLDER), fileEntry->szFullPath, NULL);
1331 return TRUE;
1334 /* the FO_DELETE operation */
1335 static int delete_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom)
1337 const FILE_ENTRY *fileEntry;
1338 DWORD i;
1339 int ret;
1340 BOOL bTrash;
1342 if (!flFrom->dwNumFiles)
1343 return ERROR_SUCCESS;
1345 /* Windows also checks only the first item */
1346 bTrash = (lpFileOp->fFlags & FOF_ALLOWUNDO)
1347 && TRASH_CanTrashFile(flFrom->feFiles[0].szFullPath);
1349 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (!bTrash && lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1350 if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, bTrash, flFrom))
1352 lpFileOp->fAnyOperationsAborted = TRUE;
1353 return 0;
1356 for (i = 0; i < flFrom->dwNumFiles; i++)
1358 fileEntry = &flFrom->feFiles[i];
1360 if (!IsAttribFile(fileEntry->attributes) &&
1361 (lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1362 continue;
1364 if (bTrash)
1366 BOOL bDelete;
1367 if (TRASH_TrashFile(fileEntry->szFullPath))
1368 continue;
1370 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1371 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1372 bDelete = SHELL_ConfirmDialogW(lpFileOp->hwnd, ASK_CANT_TRASH_ITEM, fileEntry->szFullPath, NULL);
1373 else
1374 bDelete = TRUE;
1376 if (!bDelete)
1378 lpFileOp->fAnyOperationsAborted = TRUE;
1379 break;
1383 /* delete the file or directory */
1384 if (IsAttribFile(fileEntry->attributes))
1385 ret = DeleteFileW(fileEntry->szFullPath) ?
1386 ERROR_SUCCESS : GetLastError();
1387 else
1388 ret = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1390 if (ret)
1391 return ret;
1394 return ERROR_SUCCESS;
1397 /* moves a file or directory to another directory */
1398 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, const FILE_ENTRY *feFrom, const FILE_ENTRY *feTo)
1400 WCHAR szDestPath[MAX_PATH];
1402 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1403 SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
1406 /* the FO_MOVE operation */
1407 static int move_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
1409 DWORD i;
1410 INT mismatched = 0;
1411 const FILE_ENTRY *entryToMove;
1412 const FILE_ENTRY *fileDest;
1414 if (!flFrom->dwNumFiles)
1415 return ERROR_SUCCESS;
1417 if (!flTo->dwNumFiles)
1418 return ERROR_FILE_NOT_FOUND;
1420 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1421 flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1423 return ERROR_CANCELLED;
1426 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1427 !flFrom->bAnyDirectories &&
1428 flFrom->dwNumFiles > flTo->dwNumFiles)
1430 return ERROR_CANCELLED;
1433 if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1434 return ERROR_CANCELLED;
1436 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1437 mismatched = flFrom->dwNumFiles - flTo->dwNumFiles;
1439 fileDest = &flTo->feFiles[0];
1440 for (i = 0; i < flFrom->dwNumFiles; i++)
1442 entryToMove = &flFrom->feFiles[i];
1444 if (!PathFileExistsW(fileDest->szDirectory))
1445 return ERROR_CANCELLED;
1447 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1449 if (i >= flTo->dwNumFiles)
1450 break;
1451 fileDest = &flTo->feFiles[i];
1452 if (mismatched && !fileDest->bExists)
1454 create_dest_dirs(flTo->feFiles[i].szFullPath);
1455 flTo->feFiles[i].bExists = TRUE;
1456 flTo->feFiles[i].attributes = FILE_ATTRIBUTE_DIRECTORY;
1460 if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1461 move_to_dir(lpFileOp, entryToMove, fileDest);
1462 else
1463 SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
1466 if (mismatched > 0)
1468 if (flFrom->bAnyDirectories)
1469 return DE_DESTSAMETREE;
1470 else
1471 return DE_SAMEFILE;
1474 return ERROR_SUCCESS;
1477 /* the FO_RENAME files */
1478 static int rename_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
1480 const FILE_ENTRY *feFrom;
1481 const FILE_ENTRY *feTo;
1483 if (flFrom->dwNumFiles != 1)
1484 return ERROR_GEN_FAILURE;
1486 if (flTo->dwNumFiles != 1)
1487 return ERROR_CANCELLED;
1489 feFrom = &flFrom->feFiles[0];
1490 feTo= &flTo->feFiles[0];
1492 /* fail if destination doesn't exist */
1493 if (!feFrom->bExists)
1494 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1496 /* fail if destination already exists */
1497 if (feTo->bExists)
1498 return ERROR_ALREADY_EXISTS;
1500 return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
1503 /* alert the user if an unsupported flag is used */
1504 static void check_flags(FILEOP_FLAGS fFlags)
1506 WORD wUnsupportedFlags = FOF_NO_CONNECTED_ELEMENTS |
1507 FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1508 FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1510 if (fFlags & wUnsupportedFlags)
1511 FIXME("Unsupported flags: %04x\n", fFlags);
1514 /*************************************************************************
1515 * SHFileOperationW [SHELL32.@]
1517 * See SHFileOperationA
1519 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1521 FILE_OPERATION op;
1522 FILE_LIST flFrom, flTo;
1523 int ret = 0;
1525 if (!lpFileOp)
1526 return ERROR_INVALID_PARAMETER;
1528 check_flags(lpFileOp->fFlags);
1530 ZeroMemory(&flFrom, sizeof(FILE_LIST));
1531 ZeroMemory(&flTo, sizeof(FILE_LIST));
1533 if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1534 return ret;
1536 if (lpFileOp->wFunc != FO_DELETE)
1537 parse_file_list(&flTo, lpFileOp->pTo);
1539 ZeroMemory(&op, sizeof(op));
1540 op.req = lpFileOp;
1541 op.bManyItems = (flFrom.dwNumFiles > 1);
1542 lpFileOp->fAnyOperationsAborted = FALSE;
1544 switch (lpFileOp->wFunc)
1546 case FO_COPY:
1547 ret = copy_files(&op, &flFrom, &flTo);
1548 break;
1549 case FO_DELETE:
1550 ret = delete_files(lpFileOp, &flFrom);
1551 break;
1552 case FO_MOVE:
1553 ret = move_files(lpFileOp, &flFrom, &flTo);
1554 break;
1555 case FO_RENAME:
1556 ret = rename_files(lpFileOp, &flFrom, &flTo);
1557 break;
1558 default:
1559 ret = ERROR_INVALID_PARAMETER;
1560 break;
1563 destroy_file_list(&flFrom);
1565 if (lpFileOp->wFunc != FO_DELETE)
1566 destroy_file_list(&flTo);
1568 if (ret == ERROR_CANCELLED)
1569 lpFileOp->fAnyOperationsAborted = TRUE;
1571 SetLastError(ERROR_SUCCESS);
1572 return ret;
1575 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1577 /*************************************************************************
1578 * SHFreeNameMappings [shell32.246]
1580 * Free the mapping handle returned by SHFileOperation if FOF_WANTSMAPPINGHANDLE
1581 * was specified.
1583 * PARAMS
1584 * hNameMapping [I] handle to the name mappings used during renaming of files
1586 * RETURNS
1587 * Nothing
1589 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1591 if (hNameMapping)
1593 int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1595 for (; i>= 0; i--)
1597 LPSHNAMEMAPPINGW lp = DSA_GetItemPtr(hNameMapping, i);
1599 SHFree(lp->pszOldPath);
1600 SHFree(lp->pszNewPath);
1602 DSA_Destroy(hNameMapping);
1606 /*************************************************************************
1607 * SheGetDirA [SHELL32.@]
1609 * drive = 0: returns the current directory path
1610 * drive > 0: returns the current directory path of the specified drive
1611 * drive=1 -> A: drive=2 -> B: ...
1612 * returns 0 if successful
1614 DWORD WINAPI SheGetDirA(DWORD drive, LPSTR buffer)
1616 WCHAR org_path[MAX_PATH];
1617 DWORD ret;
1618 char drv_path[3];
1620 /* change current directory to the specified drive */
1621 if (drive) {
1622 strcpy(drv_path, "A:");
1623 drv_path[0] += (char)drive-1;
1625 GetCurrentDirectoryW(MAX_PATH, org_path);
1627 SetCurrentDirectoryA(drv_path);
1630 /* query current directory path of the specified drive */
1631 ret = GetCurrentDirectoryA(MAX_PATH, buffer);
1633 /* back to the original drive */
1634 if (drive)
1635 SetCurrentDirectoryW(org_path);
1637 if (!ret)
1638 return GetLastError();
1640 return 0;
1643 /*************************************************************************
1644 * SheGetDirW [SHELL32.@]
1646 * drive = 0: returns the current directory path
1647 * drive > 0: returns the current directory path of the specified drive
1648 * drive=1 -> A: drive=2 -> B: ...
1649 * returns 0 if successful
1651 DWORD WINAPI SheGetDirW(DWORD drive, LPWSTR buffer)
1653 WCHAR org_path[MAX_PATH];
1654 DWORD ret;
1655 char drv_path[3];
1657 /* change current directory to the specified drive */
1658 if (drive) {
1659 strcpy(drv_path, "A:");
1660 drv_path[0] += (char)drive-1;
1662 GetCurrentDirectoryW(MAX_PATH, org_path);
1664 SetCurrentDirectoryA(drv_path);
1667 /* query current directory path of the specified drive */
1668 ret = GetCurrentDirectoryW(MAX_PATH, buffer);
1670 /* back to the original drive */
1671 if (drive)
1672 SetCurrentDirectoryW(org_path);
1674 if (!ret)
1675 return GetLastError();
1677 return 0;
1680 /*************************************************************************
1681 * SheChangeDirA [SHELL32.@]
1683 * changes the current directory to the specified path
1684 * and returns 0 if successful
1686 DWORD WINAPI SheChangeDirA(LPSTR path)
1688 if (SetCurrentDirectoryA(path))
1689 return 0;
1690 else
1691 return GetLastError();
1694 /*************************************************************************
1695 * SheChangeDirW [SHELL32.@]
1697 * changes the current directory to the specified path
1698 * and returns 0 if successful
1700 DWORD WINAPI SheChangeDirW(LPWSTR path)
1702 if (SetCurrentDirectoryW(path))
1703 return 0;
1704 else
1705 return GetLastError();
1708 /*************************************************************************
1709 * IsNetDrive [SHELL32.66]
1711 int WINAPI IsNetDrive(int drive)
1713 char root[4];
1714 strcpy(root, "A:\\");
1715 root[0] += (char)drive;
1716 return (GetDriveTypeA(root) == DRIVE_REMOTE);
1720 /*************************************************************************
1721 * RealDriveType [SHELL32.524]
1723 int WINAPI RealDriveType(int drive, BOOL bQueryNet)
1725 char root[] = "A:\\";
1726 root[0] += (char)drive;
1727 return GetDriveTypeA(root);
1730 /***********************************************************************
1731 * SHPathPrepareForWriteA (SHELL32.@)
1733 HRESULT WINAPI SHPathPrepareForWriteA(HWND hwnd, IUnknown *modless, LPCSTR path, DWORD flags)
1735 WCHAR wpath[MAX_PATH];
1736 MultiByteToWideChar( CP_ACP, 0, path, -1, wpath, MAX_PATH);
1737 return SHPathPrepareForWriteW(hwnd, modless, wpath, flags);
1740 /***********************************************************************
1741 * SHPathPrepareForWriteW (SHELL32.@)
1743 HRESULT WINAPI SHPathPrepareForWriteW(HWND hwnd, IUnknown *modless, LPCWSTR path, DWORD flags)
1745 DWORD res;
1746 DWORD err;
1747 LPCWSTR realpath;
1748 int len;
1749 WCHAR* last_slash;
1750 WCHAR* temppath=NULL;
1752 TRACE("%p %p %s 0x%08x\n", hwnd, modless, debugstr_w(path), flags);
1754 if (flags & ~(SHPPFW_DIRCREATE|SHPPFW_ASKDIRCREATE|SHPPFW_IGNOREFILENAME))
1755 FIXME("unimplemented flags 0x%08x\n", flags);
1757 /* cut off filename if necessary */
1758 if (flags & SHPPFW_IGNOREFILENAME)
1760 last_slash = StrRChrW(path, NULL, '\\');
1761 if (last_slash == NULL)
1762 len = 1;
1763 else
1764 len = last_slash - path + 1;
1765 temppath = heap_alloc(len * sizeof(WCHAR));
1766 if (!temppath)
1767 return E_OUTOFMEMORY;
1768 StrCpyNW(temppath, path, len);
1769 realpath = temppath;
1771 else
1773 realpath = path;
1776 /* try to create the directory if asked to */
1777 if (flags & (SHPPFW_DIRCREATE|SHPPFW_ASKDIRCREATE))
1779 if (flags & SHPPFW_ASKDIRCREATE)
1780 FIXME("treating SHPPFW_ASKDIRCREATE as SHPPFW_DIRCREATE\n");
1782 SHCreateDirectoryExW(0, realpath, NULL);
1785 /* check if we can access the directory */
1786 res = GetFileAttributesW(realpath);
1788 heap_free(temppath);
1790 if (res == INVALID_FILE_ATTRIBUTES)
1792 err = GetLastError();
1793 if (err == ERROR_FILE_NOT_FOUND)
1794 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
1795 return HRESULT_FROM_WIN32(err);
1797 else if (res & FILE_ATTRIBUTE_DIRECTORY)
1798 return S_OK;
1799 else
1800 return HRESULT_FROM_WIN32(ERROR_DIRECTORY);
1803 /*************************************************************************
1804 * SHMultiFileProperties [SHELL32.@]
1807 HRESULT WINAPI SHMultiFileProperties(IDataObject *pdtobj, DWORD flags)
1809 FIXME("stub: %p %u\n", pdtobj, flags);
1810 return E_NOTIMPL;