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
25 #include "wine/port.h"
40 #define NO_SHLWAPI_STREAM
42 #include "shell32_main.h"
43 #include "undocshell.h"
44 #include "wine/debug.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))))
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);
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
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);
99 GetWindowRect(hButton, &r);
100 MapWindowPoints( 0, hDlg, (POINT *)&r, 2 );
101 width = r.right - r.left;
102 SetWindowPos(hButton, 0, *xPos - width, r.top - yOffset, 0, 0,
103 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW );
107 ShowWindow(hButton, SW_HIDE);
110 /* Note: we paint the text manually and don't use the static control to make
111 * sure the text has the same height as the one computed in WM_INITDIALOG
113 static INT_PTR ConfirmMsgBox_Paint(HWND hDlg)
120 BeginPaint(hDlg, &ps);
123 GetClientRect(GetDlgItem(hDlg, IDD_MESSAGE), &r);
124 /* this will remap the rect to dialog coords */
125 MapWindowPoints(GetDlgItem(hDlg, IDD_MESSAGE), hDlg, (LPPOINT)&r, 2);
126 hOldFont = SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDD_MESSAGE, WM_GETFONT, 0, 0));
127 DrawTextW(hdc, GetPropW(hDlg, CONFIRM_MSG_PROP), -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK);
128 SelectObject(hdc, hOldFont);
133 static INT_PTR ConfirmMsgBox_Init(HWND hDlg, LPARAM lParam)
135 struct confirm_msg_info *info = (struct confirm_msg_info *)lParam;
142 SetWindowTextW(hDlg, info->lpszCaption);
143 ShowWindow(GetDlgItem(hDlg, IDD_MESSAGE), SW_HIDE);
144 SetPropW(hDlg, CONFIRM_MSG_PROP, info->lpszText);
145 SendDlgItemMessageW(hDlg, IDD_ICON, STM_SETICON, (WPARAM)info->hIcon, 0);
147 /* compute the text height and resize the dialog */
148 GetClientRect(GetDlgItem(hDlg, IDD_MESSAGE), &r);
151 hOldFont = SelectObject(hdc, (HFONT)SendDlgItemMessageW(hDlg, IDD_MESSAGE, WM_GETFONT, 0, 0));
152 DrawTextW(hdc, info->lpszText, -1, &r, DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_WORDBREAK | DT_CALCRECT);
153 SelectObject(hdc, hOldFont);
155 yOffset = min(yOffset, 35); /* don't make the dialog too small */
156 ReleaseDC(hDlg, hdc);
158 GetClientRect(hDlg, &r);
160 GetWindowRect(hDlg, &r);
161 width = r.right - r.left;
162 height = r.bottom - r.top - yOffset;
163 MoveWindow(hDlg, (GetSystemMetrics(SM_CXSCREEN) - width)/2,
164 (GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
166 confirm_msg_move_button(hDlg, IDCANCEL, &xPos, yOffset, info->bYesToAll);
167 confirm_msg_move_button(hDlg, IDNO, &xPos, yOffset, TRUE);
168 confirm_msg_move_button(hDlg, IDD_YESTOALL, &xPos, yOffset, info->bYesToAll);
169 confirm_msg_move_button(hDlg, IDYES, &xPos, yOffset, TRUE);
173 static INT_PTR CALLBACK ConfirmMsgBoxProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
178 return ConfirmMsgBox_Init(hDlg, lParam);
180 return ConfirmMsgBox_Paint(hDlg);
182 EndDialog(hDlg, wParam);
185 EndDialog(hDlg, IDCANCEL);
191 static int SHELL_ConfirmMsgBox(HWND hWnd, LPWSTR lpszText, LPWSTR lpszCaption, HICON hIcon, BOOL bYesToAll)
193 static const WCHAR wszTemplate[] = {'S','H','E','L','L','_','Y','E','S','T','O','A','L','L','_','M','S','G','B','O','X',0};
194 struct confirm_msg_info info;
196 info.lpszText = lpszText;
197 info.lpszCaption = lpszCaption;
199 info.bYesToAll = bYesToAll;
200 return DialogBoxParamW(shell32_hInstance, wszTemplate, hWnd, ConfirmMsgBoxProc, (LPARAM)&info);
203 /* confirmation dialogs content */
206 HINSTANCE hIconInstance;
207 UINT icon_resource_id;
208 UINT caption_resource_id, text_resource_id;
209 } SHELL_ConfirmIDstruc;
211 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
213 ids->hIconInstance = shell32_hInstance;
214 switch (nKindOfDialog) {
215 case ASK_DELETE_FILE:
216 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
217 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
218 ids->text_resource_id = IDS_DELETEITEM_TEXT;
220 case ASK_DELETE_FOLDER:
221 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
222 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
223 ids->text_resource_id = IDS_DELETEITEM_TEXT;
225 case ASK_DELETE_MULTIPLE_ITEM:
226 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
227 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
228 ids->text_resource_id = IDS_DELETEMULTIPLE_TEXT;
231 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
232 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
233 ids->text_resource_id = IDS_TRASHITEM_TEXT;
235 case ASK_TRASH_FOLDER:
236 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
237 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
238 ids->text_resource_id = IDS_TRASHFOLDER_TEXT;
240 case ASK_TRASH_MULTIPLE_ITEM:
241 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
242 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
243 ids->text_resource_id = IDS_TRASHMULTIPLE_TEXT;
245 case ASK_CANT_TRASH_ITEM:
246 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
247 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
248 ids->text_resource_id = IDS_CANTTRASH_TEXT;
250 case ASK_DELETE_SELECTED:
251 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
252 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
253 ids->text_resource_id = IDS_DELETESELECTED_TEXT;
255 case ASK_OVERWRITE_FILE:
256 ids->hIconInstance = NULL;
257 ids->icon_resource_id = IDI_WARNING;
258 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
259 ids->text_resource_id = IDS_OVERWRITEFILE_TEXT;
261 case ASK_OVERWRITE_FOLDER:
262 ids->hIconInstance = NULL;
263 ids->icon_resource_id = IDI_WARNING;
264 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
265 ids->text_resource_id = IDS_OVERWRITEFOLDER_TEXT;
268 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
273 static BOOL SHELL_ConfirmDialogW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir, FILE_OPERATION *op)
275 WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
276 SHELL_ConfirmIDstruc ids;
281 assert(nKindOfDialog >= 0 && nKindOfDialog < 32);
282 if (op && (op->dwYesToAllMask & (1 << nKindOfDialog)))
285 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids)) return FALSE;
287 LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption)/sizeof(WCHAR));
288 LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText)/sizeof(WCHAR));
290 args[0] = (DWORD_PTR)szDir;
291 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
292 szText, 0, 0, szBuffer, sizeof(szBuffer), (__ms_va_list*)args);
293 hIcon = LoadIconW(ids.hIconInstance, (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id));
295 ret = SHELL_ConfirmMsgBox(hWnd, szBuffer, szCaption, hIcon, op && op->bManyItems);
297 if (ret == IDD_YESTOALL) {
298 op->dwYesToAllMask |= (1 << nKindOfDialog);
302 op->bCancelled = TRUE;
304 op->req->fAnyOperationsAborted = TRUE;
309 BOOL SHELL_ConfirmYesNoW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir)
311 return SHELL_ConfirmDialogW(hWnd, nKindOfDialog, szDir, NULL);
314 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
316 DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
321 *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
324 MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
327 return E_OUTOFMEMORY;
330 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
332 HeapFree(GetProcessHeap(), 0, wPath);
335 HRESULT WINAPI SHIsFileAvailableOffline(LPCWSTR path, LPDWORD status)
337 FIXME("(%s, %p) stub\n", debugstr_w(path), status);
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 BOOL SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
351 WIN32_FIND_DATAW wfd;
352 WCHAR szTemp[MAX_PATH];
354 /* Make sure the directory exists before eventually prompting the user */
355 PathCombineW(szTemp, pszDir, wWildcardFile);
356 hFind = FindFirstFileW(szTemp, &wfd);
357 if (hFind == INVALID_HANDLE_VALUE)
360 if (!bShowUI || (ret = SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir, NULL)))
364 if (IsDotDir(wfd.cFileName))
366 PathCombineW(szTemp, pszDir, wfd.cFileName);
367 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
368 ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
370 ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
371 } while (ret && FindNextFileW(hFind, &wfd));
375 ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
379 /**************************************************************************
380 * Win32CreateDirectory [SHELL32.93]
382 * Creates a directory. Also triggers a change notify if one exists.
385 * path [I] path to directory to create
388 * TRUE if successful, FALSE otherwise
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)
399 TRACE("(%s, %p)\n", debugstr_a(path), sec);
401 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
404 retCode = SHNotifyCreateDirectoryW(wPath, sec);
405 SHELL32_FreeUnicodeBuf(wPath);
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.
439 * path [I] path to directory to delete
442 * TRUE if successful, FALSE otherwise
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)
453 TRACE("(%s)\n", debugstr_a(path));
455 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
458 retCode = SHNotifyRemoveDirectoryW(wPath);
459 SHELL32_FreeUnicodeBuf(wPath);
464 /***********************************************************************/
466 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
469 TRACE("(%s)\n", debugstr_w(path));
471 ret = RemoveDirectoryW(path);
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);
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.
503 * path [I] path to file to delete
506 * TRUE if successful, FALSE otherwise
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)
517 TRACE("(%s)\n", debugstr_a(path));
519 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
522 retCode = SHNotifyDeleteFileW(wPath);
523 SHELL32_FreeUnicodeBuf(wPath);
528 /***********************************************************************/
530 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
534 TRACE("(%s)\n", debugstr_w(path));
536 ret = DeleteFileW(path);
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);
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.
568 * src [I] path to source file to move
569 * dest [I] path to target file to move to
572 * ERORR_SUCCESS if successful
574 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
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 */
584 ret = MoveFileW(src, dest);
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);
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.
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
620 * ERROR_SUCCESS if successful
622 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
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);
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.
653 * path [I] path of directory to create
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 to long to process
665 * exported by ordinal
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.
685 * path [I] path of directory to create
686 * sec [I] security attributes to use or NULL
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 to 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)
713 TRACE("(%s, %p)\n", debugstr_a(path), sec);
715 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
718 retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
719 SHELL32_FreeUnicodeBuf(wPath);
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))
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);
755 while (*pSlash && *pSlash != '\\') 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 && (ERROR_CANCELLED != ret))
768 /* We failed and should show a dialog box */
769 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
770 ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
776 /*************************************************************************
777 * SHFindAttrW [internal]
779 * Get the Attributes for a file or directory. The difference to GetAttributes()
780 * is that this function will also work for paths containing wildcard characters
784 * path [I] path of directory or file to check
785 * fileOnly [I] TRUE if only files should be found
788 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
789 * the first file or directory found otherwise
791 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
793 WIN32_FIND_DATAW wfd;
794 BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
795 DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
796 HANDLE hFind = FindFirstFileW(pName, &wfd);
798 TRACE("%s %d\n", debugstr_w(pName), fileOnly);
799 if (INVALID_HANDLE_VALUE != hFind)
803 if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
805 dwAttr = wfd.dwFileAttributes;
808 while (FindNextFileW(hFind, &wfd));
814 /*************************************************************************
816 * SHNameTranslate HelperFunction for SHFileOperationA
818 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
819 * is NULL, only the necessary size of the string is determined and returned,
820 * otherwise the ASCII strings are copied into it and the buffer is increased
821 * to point to the location after the final 0 termination char.
823 static DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
825 DWORD size = 0, aSize = 0;
826 LPCSTR aString = (LPCSTR)*pWToFrom;
832 size = lstrlenA(aString) + 1;
835 } while ((size != 1) && more);
836 /* The two sizes might be different in the case of multibyte chars */
837 size = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, 0);
838 if (*wString) /* only in the second loop */
840 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
841 *pWToFrom = *wString;
847 /*************************************************************************
848 * SHFileOperationA [SHELL32.@]
850 * Function to copy, move, delete and create one or more files with optional
854 * lpFileOp [I/O] pointer to a structure containing all the necessary information
857 * Success: ERROR_SUCCESS.
858 * Failure: ERROR_CANCELLED.
863 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
865 SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
868 LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
869 wString = NULL; /* we change this in SHNameTranslate */
872 if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
873 nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
874 if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
875 nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
876 while (1) /* every loop calculate size, second translate also, if we have storage for this */
878 size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
879 size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
880 size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
884 retCode = SHFileOperationW(&nFileOp);
885 HeapFree(GetProcessHeap(), 0, ForFree); /* we cannot use wString, it was changed */
890 wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
891 if (ForFree) continue;
892 retCode = ERROR_OUTOFMEMORY;
893 nFileOp.fAnyOperationsAborted = TRUE;
894 SetLastError(retCode);
899 lpFileOp->hNameMappings = nFileOp.hNameMappings;
900 lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
904 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
922 BOOL bAnyFromWildcard;
923 BOOL bAnyDirectories;
928 static inline void grow_list(FILE_LIST *list)
930 FILE_ENTRY *new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
931 list->num_alloc * 2 * sizeof(*new) );
933 list->num_alloc *= 2;
936 /* adds a file to the FILE_ENTRY struct
938 static void add_file_to_entry(FILE_ENTRY *feFile, LPCWSTR szFile)
940 DWORD dwLen = lstrlenW(szFile) + 1;
943 feFile->szFullPath = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
944 lstrcpyW(feFile->szFullPath, szFile);
946 ptr = StrRChrW(szFile, NULL, '\\');
949 dwLen = ptr - szFile + 1;
950 feFile->szDirectory = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
951 lstrcpynW(feFile->szDirectory, szFile, dwLen);
953 dwLen = lstrlenW(feFile->szFullPath) - dwLen + 1;
954 feFile->szFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
955 lstrcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
957 feFile->bFromWildcard = FALSE;
960 static LPWSTR wildcard_to_file(LPCWSTR szWildCard, LPCWSTR szFileName)
964 DWORD dwDirLen, dwFullLen;
966 ptr = StrRChrW(szWildCard, NULL, '\\');
967 dwDirLen = ptr - szWildCard + 1;
969 dwFullLen = dwDirLen + lstrlenW(szFileName) + 1;
970 szFullPath = HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
972 lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
973 lstrcatW(szFullPath, szFileName);
978 static void parse_wildcard_files(FILE_LIST *flList, LPCWSTR szFile, LPDWORD pdwListIndex)
980 WIN32_FIND_DATAW wfd;
981 HANDLE hFile = FindFirstFileW(szFile, &wfd);
986 if (hFile == INVALID_HANDLE_VALUE) return;
988 for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
990 if (IsDotDir(wfd.cFileName)) continue;
991 if (*pdwListIndex >= flList->num_alloc) grow_list( flList );
992 szFullPath = wildcard_to_file(szFile, wfd.cFileName);
993 file = &flList->feFiles[(*pdwListIndex)++];
994 add_file_to_entry(file, szFullPath);
995 file->bFromWildcard = TRUE;
996 file->attributes = wfd.dwFileAttributes;
997 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
998 HeapFree(GetProcessHeap(), 0, szFullPath);
1004 /* takes the null-separated file list and fills out the FILE_LIST */
1005 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
1007 LPCWSTR ptr = szFiles;
1008 WCHAR szCurFile[MAX_PATH];
1012 return ERROR_INVALID_PARAMETER;
1014 flList->bAnyFromWildcard = FALSE;
1015 flList->bAnyDirectories = FALSE;
1016 flList->bAnyDontExist = FALSE;
1017 flList->num_alloc = 32;
1018 flList->dwNumFiles = 0;
1022 return ERROR_ACCESS_DENIED;
1024 flList->feFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1025 flList->num_alloc * sizeof(FILE_ENTRY));
1029 if (i >= flList->num_alloc) grow_list( flList );
1031 /* change relative to absolute path */
1032 if (PathIsRelativeW(ptr))
1034 GetCurrentDirectoryW(MAX_PATH, szCurFile);
1035 PathCombineW(szCurFile, szCurFile, ptr);
1036 flList->feFiles[i].bFromRelative = TRUE;
1040 lstrcpyW(szCurFile, ptr);
1041 flList->feFiles[i].bFromRelative = FALSE;
1044 /* parse wildcard files if they are in the filename */
1045 if (StrPBrkW(szCurFile, wWildcardChars))
1047 parse_wildcard_files(flList, szCurFile, &i);
1048 flList->bAnyFromWildcard = TRUE;
1053 FILE_ENTRY *file = &flList->feFiles[i];
1054 add_file_to_entry(file, szCurFile);
1055 file->attributes = GetFileAttributesW( file->szFullPath );
1056 file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
1057 if (!file->bExists) flList->bAnyDontExist = TRUE;
1058 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
1061 /* advance to the next string */
1062 ptr += lstrlenW(ptr) + 1;
1065 flList->dwNumFiles = i;
1070 /* free the FILE_LIST */
1071 static void destroy_file_list(FILE_LIST *flList)
1075 if (!flList || !flList->feFiles)
1078 for (i = 0; i < flList->dwNumFiles; i++)
1080 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
1081 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
1082 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
1085 HeapFree(GetProcessHeap(), 0, flList->feFiles);
1088 static void copy_dir_to_dir(FILE_OPERATION *op, const FILE_ENTRY *feFrom, LPCWSTR szDestPath)
1090 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1091 SHFILEOPSTRUCTW fileOp;
1093 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1095 if (IsDotDir(feFrom->szFilename))
1098 if (PathFileExistsW(szDestPath))
1099 PathCombineW(szTo, szDestPath, feFrom->szFilename);
1101 lstrcpyW(szTo, szDestPath);
1103 if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo)) {
1104 if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FOLDER, feFrom->szFilename, op))
1106 /* Vista returns an ERROR_CANCELLED even if user pressed "No" */
1107 if (!op->bManyItems)
1108 op->bCancelled = TRUE;
1113 szTo[lstrlenW(szTo) + 1] = '\0';
1114 SHNotifyCreateDirectoryW(szTo, NULL);
1116 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1117 szFrom[lstrlenW(szFrom) + 1] = '\0';
1120 fileOp.pFrom = szFrom;
1122 fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
1124 /* Don't ask the user about overwriting files when he accepted to overwrite the
1125 folder. FIXME: this is not exactly what Windows does - e.g. there would be
1126 an additional confirmation for a nested folder */
1127 fileOp.fFlags |= FOF_NOCONFIRMATION;
1129 SHFileOperationW(&fileOp);
1132 static BOOL copy_file_to_file(FILE_OPERATION *op, const WCHAR *szFrom, const WCHAR *szTo)
1134 if (!(op->req->fFlags & FOF_NOCONFIRMATION) && PathFileExistsW(szTo))
1136 if (!SHELL_ConfirmDialogW(op->req->hwnd, ASK_OVERWRITE_FILE, PathFindFileNameW(szTo), op))
1140 return SHNotifyCopyFileW(szFrom, szTo, FALSE) == 0;
1143 /* copy a file or directory to another directory */
1144 static void copy_to_dir(FILE_OPERATION *op, const FILE_ENTRY *feFrom, const FILE_ENTRY *feTo)
1146 if (!PathFileExistsW(feTo->szFullPath))
1147 SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
1149 if (IsAttribFile(feFrom->attributes))
1151 WCHAR szDestPath[MAX_PATH];
1153 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1154 copy_file_to_file(op, feFrom->szFullPath, szDestPath);
1156 else if (!(op->req->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1157 copy_dir_to_dir(op, feFrom, feTo->szFullPath);
1160 static void create_dest_dirs(LPCWSTR szDestDir)
1162 WCHAR dir[MAX_PATH];
1163 LPCWSTR ptr = StrChrW(szDestDir, '\\');
1165 /* make sure all directories up to last one are created */
1166 while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
1168 lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
1170 if (!PathFileExistsW(dir))
1171 SHNotifyCreateDirectoryW(dir, NULL);
1174 /* create last directory */
1175 if (!PathFileExistsW(szDestDir))
1176 SHNotifyCreateDirectoryW(szDestDir, NULL);
1179 /* the FO_COPY operation */
1180 static HRESULT copy_files(FILE_OPERATION *op, const FILE_LIST *flFrom, FILE_LIST *flTo)
1183 const FILE_ENTRY *entryToCopy;
1184 const FILE_ENTRY *fileDest = &flTo->feFiles[0];
1186 if (flFrom->bAnyDontExist)
1187 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1189 if (flTo->dwNumFiles == 0)
1191 /* If the destination is empty, SHFileOperation should use the current directory */
1192 WCHAR curdir[MAX_PATH+1];
1194 GetCurrentDirectoryW(MAX_PATH, curdir);
1195 curdir[lstrlenW(curdir)+1] = 0;
1197 destroy_file_list(flTo);
1198 ZeroMemory(flTo, sizeof(FILE_LIST));
1199 parse_file_list(flTo, curdir);
1200 fileDest = &flTo->feFiles[0];
1203 if (op->req->fFlags & FOF_MULTIDESTFILES)
1205 if (flFrom->bAnyFromWildcard)
1206 return ERROR_CANCELLED;
1208 if (flFrom->dwNumFiles != flTo->dwNumFiles)
1210 if (flFrom->dwNumFiles != 1 && !IsAttribDir(fileDest->attributes))
1211 return ERROR_CANCELLED;
1213 /* Free all but the first entry. */
1214 for (i = 1; i < flTo->dwNumFiles; i++)
1216 HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szDirectory);
1217 HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szFilename);
1218 HeapFree(GetProcessHeap(), 0, flTo->feFiles[i].szFullPath);
1221 flTo->dwNumFiles = 1;
1223 else if (IsAttribDir(fileDest->attributes))
1225 for (i = 1; i < flTo->dwNumFiles; i++)
1226 if (!IsAttribDir(flTo->feFiles[i].attributes) ||
1227 !IsAttribDir(flFrom->feFiles[i].attributes))
1229 return ERROR_CANCELLED;
1233 else if (flFrom->dwNumFiles != 1)
1235 if (flTo->dwNumFiles != 1 && !IsAttribDir(fileDest->attributes))
1236 return ERROR_CANCELLED;
1238 if (PathFileExistsW(fileDest->szFullPath) &&
1239 IsAttribFile(fileDest->attributes))
1241 return ERROR_CANCELLED;
1244 if (flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
1245 !PathFileExistsW(fileDest->szFullPath))
1247 return ERROR_CANCELLED;
1251 for (i = 0; i < flFrom->dwNumFiles; i++)
1253 entryToCopy = &flFrom->feFiles[i];
1255 if ((op->req->fFlags & FOF_MULTIDESTFILES) &&
1256 flTo->dwNumFiles > 1)
1258 fileDest = &flTo->feFiles[i];
1261 if (IsAttribDir(entryToCopy->attributes) &&
1262 !lstrcmpiW(entryToCopy->szFullPath, fileDest->szDirectory))
1264 return ERROR_SUCCESS;
1267 create_dest_dirs(fileDest->szDirectory);
1269 if (!lstrcmpiW(entryToCopy->szFullPath, fileDest->szFullPath))
1271 if (IsAttribFile(entryToCopy->attributes))
1272 return ERROR_NO_MORE_SEARCH_HANDLES;
1274 return ERROR_SUCCESS;
1277 if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1278 IsAttribDir(fileDest->attributes))
1280 copy_to_dir(op, entryToCopy, fileDest);
1282 else if (IsAttribDir(entryToCopy->attributes))
1284 copy_dir_to_dir(op, entryToCopy, fileDest->szFullPath);
1288 if (!copy_file_to_file(op, entryToCopy->szFullPath, fileDest->szFullPath))
1290 op->req->fAnyOperationsAborted = TRUE;
1291 return ERROR_CANCELLED;
1295 /* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
1297 return ERROR_CANCELLED;
1300 /* Vista return code. On XP if the used pressed "No" for the last item,
1301 * ERROR_ARENA_TRASHED would be returned */
1302 return ERROR_SUCCESS;
1305 static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, BOOL fTrash, const FILE_LIST *flFrom)
1307 if (flFrom->dwNumFiles > 1)
1310 const WCHAR format[] = {'%','d',0};
1312 wnsprintfW(tmp, sizeof(tmp)/sizeof(tmp[0]), format, flFrom->dwNumFiles);
1313 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_MULTIPLE_ITEM:ASK_DELETE_MULTIPLE_ITEM), tmp, NULL);
1317 const FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
1319 if (IsAttribFile(fileEntry->attributes))
1320 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FILE:ASK_DELETE_FILE), fileEntry->szFullPath, NULL);
1321 else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1322 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FOLDER:ASK_DELETE_FOLDER), fileEntry->szFullPath, NULL);
1327 /* the FO_DELETE operation */
1328 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom)
1330 const FILE_ENTRY *fileEntry;
1335 if (!flFrom->dwNumFiles)
1336 return ERROR_SUCCESS;
1338 /* Windows also checks only the first item */
1339 bTrash = (lpFileOp->fFlags & FOF_ALLOWUNDO)
1340 && TRASH_CanTrashFile(flFrom->feFiles[0].szFullPath);
1342 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (!bTrash && lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1343 if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, bTrash, flFrom))
1345 lpFileOp->fAnyOperationsAborted = TRUE;
1349 for (i = 0; i < flFrom->dwNumFiles; i++)
1351 fileEntry = &flFrom->feFiles[i];
1353 if (!IsAttribFile(fileEntry->attributes) &&
1354 (lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1360 if (TRASH_TrashFile(fileEntry->szFullPath))
1363 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1364 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1365 bDelete = SHELL_ConfirmDialogW(lpFileOp->hwnd, ASK_CANT_TRASH_ITEM, fileEntry->szFullPath, NULL);
1371 lpFileOp->fAnyOperationsAborted = TRUE;
1376 /* delete the file or directory */
1377 if (IsAttribFile(fileEntry->attributes))
1378 bPathExists = DeleteFileW(fileEntry->szFullPath);
1380 bPathExists = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1383 return ERROR_PATH_NOT_FOUND;
1386 return ERROR_SUCCESS;
1389 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, const FILE_ENTRY *feFrom, LPCWSTR szDestPath)
1391 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1392 SHFILEOPSTRUCTW fileOp;
1394 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1396 if (IsDotDir(feFrom->szFilename))
1399 SHNotifyCreateDirectoryW(szDestPath, NULL);
1401 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1402 szFrom[lstrlenW(szFrom) + 1] = '\0';
1404 lstrcpyW(szTo, szDestPath);
1405 szTo[lstrlenW(szDestPath) + 1] = '\0';
1408 fileOp.pFrom = szFrom;
1411 SHFileOperationW(&fileOp);
1414 /* moves a file or directory to another directory */
1415 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, const FILE_ENTRY *feFrom, const FILE_ENTRY *feTo)
1417 WCHAR szDestPath[MAX_PATH];
1419 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1421 if (IsAttribFile(feFrom->attributes))
1422 SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
1423 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1424 move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1427 /* the FO_MOVE operation */
1428 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
1431 const FILE_ENTRY *entryToMove;
1432 const FILE_ENTRY *fileDest;
1434 if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1435 return ERROR_CANCELLED;
1437 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1438 flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1440 return ERROR_CANCELLED;
1443 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1444 !flFrom->bAnyDirectories &&
1445 flFrom->dwNumFiles > flTo->dwNumFiles)
1447 return ERROR_CANCELLED;
1450 if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1451 return ERROR_CANCELLED;
1453 if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1454 flFrom->dwNumFiles != flTo->dwNumFiles)
1456 return ERROR_CANCELLED;
1459 fileDest = &flTo->feFiles[0];
1460 for (i = 0; i < flFrom->dwNumFiles; i++)
1462 entryToMove = &flFrom->feFiles[i];
1464 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1465 fileDest = &flTo->feFiles[i];
1467 if (!PathFileExistsW(fileDest->szDirectory))
1468 return ERROR_CANCELLED;
1470 if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1471 move_to_dir(lpFileOp, entryToMove, fileDest);
1473 SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
1476 return ERROR_SUCCESS;
1479 /* the FO_RENAME files */
1480 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, const FILE_LIST *flFrom, const FILE_LIST *flTo)
1482 const FILE_ENTRY *feFrom;
1483 const FILE_ENTRY *feTo;
1485 if (flFrom->dwNumFiles != 1)
1486 return ERROR_GEN_FAILURE;
1488 if (flTo->dwNumFiles != 1)
1489 return ERROR_CANCELLED;
1491 feFrom = &flFrom->feFiles[0];
1492 feTo= &flTo->feFiles[0];
1494 /* fail if destination doesn't exist */
1495 if (!feFrom->bExists)
1496 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1498 /* fail if destination already exists */
1500 return ERROR_ALREADY_EXISTS;
1502 return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
1505 /* alert the user if an unsupported flag is used */
1506 static void check_flags(FILEOP_FLAGS fFlags)
1508 WORD wUnsupportedFlags = FOF_NO_CONNECTED_ELEMENTS |
1509 FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1510 FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1512 if (fFlags & wUnsupportedFlags)
1513 FIXME("Unsupported flags: %04x\n", fFlags);
1516 /*************************************************************************
1517 * SHFileOperationW [SHELL32.@]
1519 * See SHFileOperationA
1521 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1524 FILE_LIST flFrom, flTo;
1528 return ERROR_INVALID_PARAMETER;
1530 check_flags(lpFileOp->fFlags);
1532 ZeroMemory(&flFrom, sizeof(FILE_LIST));
1533 ZeroMemory(&flTo, sizeof(FILE_LIST));
1535 if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1538 if (lpFileOp->wFunc != FO_DELETE)
1539 parse_file_list(&flTo, lpFileOp->pTo);
1541 ZeroMemory(&op, sizeof(op));
1543 op.bManyItems = (flFrom.dwNumFiles > 1);
1545 switch (lpFileOp->wFunc)
1548 ret = copy_files(&op, &flFrom, &flTo);
1551 ret = delete_files(lpFileOp, &flFrom);
1554 ret = move_files(lpFileOp, &flFrom, &flTo);
1557 ret = rename_files(lpFileOp, &flFrom, &flTo);
1560 ret = ERROR_INVALID_PARAMETER;
1564 destroy_file_list(&flFrom);
1566 if (lpFileOp->wFunc != FO_DELETE)
1567 destroy_file_list(&flTo);
1569 if (ret == ERROR_CANCELLED)
1570 lpFileOp->fAnyOperationsAborted = TRUE;
1575 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1577 /*************************************************************************
1578 * SHFreeNameMappings [shell32.246]
1580 * Free the mapping handle returned by SHFileOperation if FOF_WANTSMAPPINGHANDLE
1584 * hNameMapping [I] handle to the name mappings used during renaming of files
1589 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1593 int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
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];
1620 /* change current directory to the specified 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 */
1635 SetCurrentDirectoryW(org_path);
1638 return GetLastError();
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];
1657 /* change current directory to the specified 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 */
1672 SetCurrentDirectoryW(org_path);
1675 return GetLastError();
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))
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))
1705 return GetLastError();
1708 /*************************************************************************
1709 * IsNetDrive [SHELL32.66]
1711 BOOL WINAPI IsNetDrive(DWORD drive)
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)
1750 WCHAR* temppath=NULL;
1752 TRACE("%p %p %s 0x%80x\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)
1764 len = last_slash - path + 1;
1765 temppath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1767 return E_OUTOFMEMORY;
1768 StrCpyNW(temppath, path, len);
1769 realpath = temppath;
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 HeapFree(GetProcessHeap(), 0, 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)
1800 return HRESULT_FROM_WIN32(ERROR_DIRECTORY);