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"
42 #define NO_SHLWAPI_STREAM
44 #include "shell32_main.h"
45 #include "undocshell.h"
46 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
51 #define IsAttrib(x, y) ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
52 #define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
53 #define IsAttribDir(x) IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
54 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
58 #define DE_SAMEFILE 0x71
59 #define DE_DESTSAMETREE 0x7D
61 static const WCHAR wWildcardFile
[] = {'*',0};
62 static const WCHAR wWildcardChars
[] = {'*','?',0};
64 static DWORD
SHNotifyCreateDirectoryA(LPCSTR path
, LPSECURITY_ATTRIBUTES sec
);
65 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
);
66 static DWORD
SHNotifyRemoveDirectoryA(LPCSTR path
);
67 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
);
68 static DWORD
SHNotifyDeleteFileA(LPCSTR path
);
69 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
);
70 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
);
71 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bFailIfExists
);
72 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
);
82 /* Confirm dialogs with an optional "Yes To All" as used in file operations confirmations
84 static const WCHAR CONFIRM_MSG_PROP
[] = {'W','I','N','E','_','C','O','N','F','I','R','M',0};
86 struct confirm_msg_info
94 /* as some buttons may be hidden and the dialog height may change we may need
95 * to move the controls */
96 static void confirm_msg_move_button(HWND hDlg
, INT iId
, INT
*xPos
, INT yOffset
, BOOL bShow
)
98 HWND hButton
= GetDlgItem(hDlg
, iId
);
104 GetWindowRect(hButton
, &r
);
105 MapWindowPoints( 0, hDlg
, (POINT
*)&r
, 2 );
106 width
= r
.right
- r
.left
;
107 SetWindowPos(hButton
, 0, *xPos
- width
, r
.top
- yOffset
, 0, 0,
108 SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
| SWP_NOREDRAW
);
112 ShowWindow(hButton
, SW_HIDE
);
115 /* Note: we paint the text manually and don't use the static control to make
116 * sure the text has the same height as the one computed in WM_INITDIALOG
118 static INT_PTR
ConfirmMsgBox_Paint(HWND hDlg
)
125 BeginPaint(hDlg
, &ps
);
127 SetBkMode(hdc
, TRANSPARENT
);
129 GetClientRect(GetDlgItem(hDlg
, IDD_MESSAGE
), &r
);
130 /* this will remap the rect to dialog coords */
131 MapWindowPoints(GetDlgItem(hDlg
, IDD_MESSAGE
), hDlg
, (LPPOINT
)&r
, 2);
132 hOldFont
= SelectObject(hdc
, (HFONT
)SendDlgItemMessageW(hDlg
, IDD_MESSAGE
, WM_GETFONT
, 0, 0));
133 DrawTextW(hdc
, GetPropW(hDlg
, CONFIRM_MSG_PROP
), -1, &r
, DT_NOPREFIX
| DT_PATH_ELLIPSIS
| DT_WORDBREAK
);
134 SelectObject(hdc
, hOldFont
);
139 static INT_PTR
ConfirmMsgBox_Init(HWND hDlg
, LPARAM lParam
)
141 struct confirm_msg_info
*info
= (struct confirm_msg_info
*)lParam
;
148 SetWindowTextW(hDlg
, info
->lpszCaption
);
149 ShowWindow(GetDlgItem(hDlg
, IDD_MESSAGE
), SW_HIDE
);
150 SetPropW(hDlg
, CONFIRM_MSG_PROP
, info
->lpszText
);
151 SendDlgItemMessageW(hDlg
, IDD_ICON
, STM_SETICON
, (WPARAM
)info
->hIcon
, 0);
153 /* compute the text height and resize the dialog */
154 GetClientRect(GetDlgItem(hDlg
, IDD_MESSAGE
), &r
);
157 hOldFont
= SelectObject(hdc
, (HFONT
)SendDlgItemMessageW(hDlg
, IDD_MESSAGE
, WM_GETFONT
, 0, 0));
158 DrawTextW(hdc
, info
->lpszText
, -1, &r
, DT_NOPREFIX
| DT_PATH_ELLIPSIS
| DT_WORDBREAK
| DT_CALCRECT
);
159 SelectObject(hdc
, hOldFont
);
161 yOffset
= min(yOffset
, 35); /* don't make the dialog too small */
162 ReleaseDC(hDlg
, hdc
);
164 GetClientRect(hDlg
, &r
);
166 GetWindowRect(hDlg
, &r
);
167 width
= r
.right
- r
.left
;
168 height
= r
.bottom
- r
.top
- yOffset
;
169 MoveWindow(hDlg
, (GetSystemMetrics(SM_CXSCREEN
) - width
)/2,
170 (GetSystemMetrics(SM_CYSCREEN
) - height
)/2, width
, height
, FALSE
);
172 confirm_msg_move_button(hDlg
, IDCANCEL
, &xPos
, yOffset
, info
->bYesToAll
);
173 confirm_msg_move_button(hDlg
, IDNO
, &xPos
, yOffset
, TRUE
);
174 confirm_msg_move_button(hDlg
, IDD_YESTOALL
, &xPos
, yOffset
, info
->bYesToAll
);
175 confirm_msg_move_button(hDlg
, IDYES
, &xPos
, yOffset
, TRUE
);
179 static INT_PTR CALLBACK
ConfirmMsgBoxProc(HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
184 return ConfirmMsgBox_Init(hDlg
, lParam
);
186 return ConfirmMsgBox_Paint(hDlg
);
188 EndDialog(hDlg
, wParam
);
191 EndDialog(hDlg
, IDCANCEL
);
197 static int SHELL_ConfirmMsgBox(HWND hWnd
, LPWSTR lpszText
, LPWSTR lpszCaption
, HICON hIcon
, BOOL bYesToAll
)
199 static const WCHAR wszTemplate
[] = {'S','H','E','L','L','_','Y','E','S','T','O','A','L','L','_','M','S','G','B','O','X',0};
200 struct confirm_msg_info info
;
202 info
.lpszText
= lpszText
;
203 info
.lpszCaption
= lpszCaption
;
205 info
.bYesToAll
= bYesToAll
;
206 return DialogBoxParamW(shell32_hInstance
, wszTemplate
, hWnd
, ConfirmMsgBoxProc
, (LPARAM
)&info
);
209 /* confirmation dialogs content */
212 HINSTANCE hIconInstance
;
213 UINT icon_resource_id
;
214 UINT caption_resource_id
, text_resource_id
;
215 } SHELL_ConfirmIDstruc
;
217 static BOOL
SHELL_ConfirmIDs(int nKindOfDialog
, SHELL_ConfirmIDstruc
*ids
)
219 ids
->hIconInstance
= shell32_hInstance
;
220 switch (nKindOfDialog
) {
221 case ASK_DELETE_FILE
:
222 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
223 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
224 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
226 case ASK_DELETE_FOLDER
:
227 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
228 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
229 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
231 case ASK_DELETE_MULTIPLE_ITEM
:
232 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
233 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
234 ids
->text_resource_id
= IDS_DELETEMULTIPLE_TEXT
;
237 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
238 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
239 ids
->text_resource_id
= IDS_TRASHITEM_TEXT
;
241 case ASK_TRASH_FOLDER
:
242 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
243 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
244 ids
->text_resource_id
= IDS_TRASHFOLDER_TEXT
;
246 case ASK_TRASH_MULTIPLE_ITEM
:
247 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
248 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
249 ids
->text_resource_id
= IDS_TRASHMULTIPLE_TEXT
;
251 case ASK_CANT_TRASH_ITEM
:
252 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
253 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
254 ids
->text_resource_id
= IDS_CANTTRASH_TEXT
;
256 case ASK_DELETE_SELECTED
:
257 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
258 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
259 ids
->text_resource_id
= IDS_DELETESELECTED_TEXT
;
261 case ASK_OVERWRITE_FILE
:
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_OVERWRITEFILE_TEXT
;
267 case ASK_OVERWRITE_FOLDER
:
268 ids
->hIconInstance
= NULL
;
269 ids
->icon_resource_id
= IDI_WARNING
;
270 ids
->caption_resource_id
= IDS_OVERWRITEFILE_CAPTION
;
271 ids
->text_resource_id
= IDS_OVERWRITEFOLDER_TEXT
;
274 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog
);
279 static BOOL
SHELL_ConfirmDialogW(HWND hWnd
, int nKindOfDialog
, LPCWSTR szDir
, FILE_OPERATION
*op
)
281 WCHAR szCaption
[255], szText
[255], szBuffer
[MAX_PATH
+ 256];
282 SHELL_ConfirmIDstruc ids
;
287 assert(nKindOfDialog
>= 0 && nKindOfDialog
< 32);
288 if (op
&& (op
->dwYesToAllMask
& (1 << nKindOfDialog
)))
291 if (!SHELL_ConfirmIDs(nKindOfDialog
, &ids
)) return FALSE
;
293 LoadStringW(shell32_hInstance
, ids
.caption_resource_id
, szCaption
, ARRAY_SIZE(szCaption
));
294 LoadStringW(shell32_hInstance
, ids
.text_resource_id
, szText
, ARRAY_SIZE(szText
));
296 args
[0] = (DWORD_PTR
)szDir
;
297 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
298 szText
, 0, 0, szBuffer
, ARRAY_SIZE(szBuffer
), (__ms_va_list
*)args
);
300 hIcon
= LoadIconW(ids
.hIconInstance
, (LPWSTR
)MAKEINTRESOURCE(ids
.icon_resource_id
));
302 ret
= SHELL_ConfirmMsgBox(hWnd
, szBuffer
, szCaption
, hIcon
, op
&& op
->bManyItems
);
304 if (ret
== IDD_YESTOALL
) {
305 op
->dwYesToAllMask
|= (1 << nKindOfDialog
);
309 op
->bCancelled
= TRUE
;
311 op
->req
->fAnyOperationsAborted
= TRUE
;
316 BOOL
SHELL_ConfirmYesNoW(HWND hWnd
, int nKindOfDialog
, LPCWSTR szDir
)
318 return SHELL_ConfirmDialogW(hWnd
, nKindOfDialog
, szDir
, NULL
);
321 static DWORD
SHELL32_AnsiToUnicodeBuf(LPCSTR aPath
, LPWSTR
*wPath
, DWORD minChars
)
323 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, NULL
, 0);
328 *wPath
= heap_alloc(len
* sizeof(WCHAR
));
331 MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, *wPath
, len
);
334 return E_OUTOFMEMORY
;
337 HRESULT WINAPI
SHIsFileAvailableOffline(LPCWSTR path
, LPDWORD status
)
339 FIXME("(%s, %p) stub\n", debugstr_w(path
), status
);
343 /**************************************************************************
344 * SHELL_DeleteDirectory() [internal]
346 * Asks for confirmation when bShowUI is true and deletes the directory and
347 * all its subdirectories and files if necessary.
349 static DWORD
SHELL_DeleteDirectoryW(HWND hwnd
, LPCWSTR pszDir
, BOOL bShowUI
)
353 WIN32_FIND_DATAW wfd
;
354 WCHAR szTemp
[MAX_PATH
];
356 PathCombineW(szTemp
, pszDir
, wWildcardFile
);
357 hFind
= FindFirstFileW(szTemp
, &wfd
);
359 if (hFind
!= INVALID_HANDLE_VALUE
) {
360 if (!bShowUI
|| SHELL_ConfirmDialogW(hwnd
, ASK_DELETE_FOLDER
, pszDir
, NULL
)) {
362 if (IsDotDir(wfd
.cFileName
))
364 PathCombineW(szTemp
, pszDir
, wfd
.cFileName
);
365 if (FILE_ATTRIBUTE_DIRECTORY
& wfd
.dwFileAttributes
)
366 ret
= SHELL_DeleteDirectoryW(hwnd
, szTemp
, FALSE
);
368 ret
= SHNotifyDeleteFileW(szTemp
);
369 } while (!ret
&& FindNextFileW(hFind
, &wfd
));
373 if (ret
== ERROR_SUCCESS
)
374 ret
= SHNotifyRemoveDirectoryW(pszDir
);
376 return ret
== ERROR_PATH_NOT_FOUND
?
377 0x7C: /* DE_INVALIDFILES (legacy Windows error) */
381 /**************************************************************************
382 * Win32CreateDirectory [SHELL32.93]
384 * Creates a directory. Also triggers a change notify if one exists.
387 * path [I] path to directory to create
390 * TRUE if successful, FALSE otherwise
393 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
394 * This is Unicode on NT/2000
396 static DWORD
SHNotifyCreateDirectoryA(LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
401 TRACE("(%s, %p)\n", debugstr_a(path
), sec
);
403 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
406 retCode
= SHNotifyCreateDirectoryW(wPath
, sec
);
412 /**********************************************************************/
414 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
416 TRACE("(%s, %p)\n", debugstr_w(path
), sec
);
418 if (CreateDirectoryW(path
, sec
))
420 SHChangeNotify(SHCNE_MKDIR
, SHCNF_PATHW
, path
, NULL
);
421 return ERROR_SUCCESS
;
423 return GetLastError();
426 /**********************************************************************/
428 BOOL WINAPI
Win32CreateDirectoryAW(LPCVOID path
, LPSECURITY_ATTRIBUTES sec
)
430 if (SHELL_OsIsUnicode())
431 return (SHNotifyCreateDirectoryW(path
, sec
) == ERROR_SUCCESS
);
432 return (SHNotifyCreateDirectoryA(path
, sec
) == ERROR_SUCCESS
);
435 /************************************************************************
436 * Win32RemoveDirectory [SHELL32.94]
438 * Deletes a directory. Also triggers a change notify if one exists.
441 * path [I] path to directory to delete
444 * TRUE if successful, FALSE otherwise
447 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
448 * This is Unicode on NT/2000
450 static DWORD
SHNotifyRemoveDirectoryA(LPCSTR path
)
455 TRACE("(%s)\n", debugstr_a(path
));
457 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
460 retCode
= SHNotifyRemoveDirectoryW(wPath
);
466 /***********************************************************************/
468 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
)
471 TRACE("(%s)\n", debugstr_w(path
));
473 ret
= RemoveDirectoryW(path
);
476 /* Directory may be write protected */
477 DWORD dwAttr
= GetFileAttributesW(path
);
478 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
))
479 if (SetFileAttributesW(path
, dwAttr
& ~FILE_ATTRIBUTE_READONLY
))
480 ret
= RemoveDirectoryW(path
);
484 SHChangeNotify(SHCNE_RMDIR
, SHCNF_PATHW
, path
, NULL
);
485 return ERROR_SUCCESS
;
487 return GetLastError();
490 /***********************************************************************/
492 BOOL WINAPI
Win32RemoveDirectoryAW(LPCVOID path
)
494 if (SHELL_OsIsUnicode())
495 return (SHNotifyRemoveDirectoryW(path
) == ERROR_SUCCESS
);
496 return (SHNotifyRemoveDirectoryA(path
) == ERROR_SUCCESS
);
499 /************************************************************************
500 * Win32DeleteFile [SHELL32.164]
502 * Deletes a file. Also triggers a change notify if one exists.
505 * path [I] path to file to delete
508 * TRUE if successful, FALSE otherwise
511 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
512 * This is Unicode on NT/2000
514 static DWORD
SHNotifyDeleteFileA(LPCSTR path
)
519 TRACE("(%s)\n", debugstr_a(path
));
521 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
524 retCode
= SHNotifyDeleteFileW(wPath
);
530 /***********************************************************************/
532 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
)
536 TRACE("(%s)\n", debugstr_w(path
));
538 ret
= DeleteFileW(path
);
541 /* File may be write protected or a system file */
542 DWORD dwAttr
= GetFileAttributesW(path
);
543 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
544 if (SetFileAttributesW(path
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
545 ret
= DeleteFileW(path
);
549 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, path
, NULL
);
550 return ERROR_SUCCESS
;
552 return GetLastError();
555 /***********************************************************************/
557 DWORD WINAPI
Win32DeleteFileAW(LPCVOID path
)
559 if (SHELL_OsIsUnicode())
560 return (SHNotifyDeleteFileW(path
) == ERROR_SUCCESS
);
561 return (SHNotifyDeleteFileA(path
) == ERROR_SUCCESS
);
564 /************************************************************************
565 * SHNotifyMoveFile [internal]
567 * Moves a file. Also triggers a change notify if one exists.
570 * src [I] path to source file to move
571 * dest [I] path to target file to move to
574 * ERROR_SUCCESS if successful
576 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
)
580 TRACE("(%s %s)\n", debugstr_w(src
), debugstr_w(dest
));
582 ret
= MoveFileExW(src
, dest
, MOVEFILE_REPLACE_EXISTING
);
584 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
586 ret
= MoveFileW(src
, dest
);
592 dwAttr
= SHFindAttrW(dest
, FALSE
);
593 if (INVALID_FILE_ATTRIBUTES
== dwAttr
)
595 /* Source file may be write protected or a system file */
596 dwAttr
= GetFileAttributesW(src
);
597 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
598 if (SetFileAttributesW(src
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
599 ret
= MoveFileW(src
, dest
);
604 SHChangeNotify(SHCNE_RENAMEITEM
, SHCNF_PATHW
, src
, dest
);
605 return ERROR_SUCCESS
;
607 return GetLastError();
610 /************************************************************************
611 * SHNotifyCopyFile [internal]
613 * Copies a file. Also triggers a change notify if one exists.
616 * src [I] path to source file to move
617 * dest [I] path to target file to move to
618 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
619 * a file with this name already exists
622 * ERROR_SUCCESS if successful
624 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bFailIfExists
)
629 TRACE("(%s %s %s)\n", debugstr_w(src
), debugstr_w(dest
), bFailIfExists
? "failIfExists" : "");
631 /* Destination file may already exist with read only attribute */
632 attribs
= GetFileAttributesW(dest
);
633 if (IsAttrib(attribs
, FILE_ATTRIBUTE_READONLY
))
634 SetFileAttributesW(dest
, attribs
& ~FILE_ATTRIBUTE_READONLY
);
636 ret
= CopyFileW(src
, dest
, bFailIfExists
);
639 SHChangeNotify(SHCNE_CREATE
, SHCNF_PATHW
, dest
, NULL
);
640 return ERROR_SUCCESS
;
643 return GetLastError();
646 /*************************************************************************
647 * SHCreateDirectory [SHELL32.165]
649 * This function creates a file system folder whose fully qualified path is
650 * given by path. If one or more of the intermediate folders do not exist,
651 * they will be created as well.
655 * path [I] path of directory to create
658 * ERROR_SUCCESS or one of the following values:
659 * ERROR_BAD_PATHNAME if the path is relative
660 * ERROR_FILE_EXISTS when a file with that name exists
661 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
662 * ERROR_INVALID_NAME if the path contains invalid chars
663 * ERROR_ALREADY_EXISTS when the directory already exists
664 * ERROR_FILENAME_EXCED_RANGE if the filename was too long to process
667 * exported by ordinal
669 * WinNT/2000 exports Unicode
671 DWORD WINAPI
SHCreateDirectory(HWND hWnd
, LPCVOID path
)
673 if (SHELL_OsIsUnicode())
674 return SHCreateDirectoryExW(hWnd
, path
, NULL
);
675 return SHCreateDirectoryExA(hWnd
, path
, NULL
);
678 /*************************************************************************
679 * SHCreateDirectoryExA [SHELL32.@]
681 * This function creates a file system folder whose fully qualified path is
682 * given by path. If one or more of the intermediate folders do not exist,
683 * they will be created as well.
687 * path [I] path of directory to create
688 * sec [I] security attributes to use or NULL
691 * ERROR_SUCCESS or one of the following values:
692 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
693 * ERROR_INVALID_NAME if the path contains invalid chars
694 * ERROR_FILE_EXISTS when a file with that name exists
695 * ERROR_ALREADY_EXISTS when the directory already exists
696 * ERROR_FILENAME_EXCED_RANGE if the filename was too long to process
698 * FIXME: Not implemented yet;
699 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
700 * if the path is a network path to deal with network drivers which might have a limited
701 * but unknown maximum path length. If not:
703 * If hWnd is set to a valid window handle, a message box is displayed warning
704 * the user that the files may not be accessible. If the user chooses not to
705 * proceed, the function returns ERROR_CANCELLED.
707 * If hWnd is set to NULL, no user interface is displayed and the function
708 * returns ERROR_CANCELLED.
710 int WINAPI
SHCreateDirectoryExA(HWND hWnd
, LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
715 TRACE("(%s, %p)\n", debugstr_a(path
), sec
);
717 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
720 retCode
= SHCreateDirectoryExW(hWnd
, wPath
, sec
);
726 /*************************************************************************
727 * SHCreateDirectoryExW [SHELL32.@]
729 * See SHCreateDirectoryExA.
731 int WINAPI
SHCreateDirectoryExW(HWND hWnd
, LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
733 int ret
= ERROR_BAD_PATHNAME
;
734 TRACE("(%p, %s, %p)\n", hWnd
, debugstr_w(path
), sec
);
736 if (PathIsRelativeW(path
))
742 ret
= SHNotifyCreateDirectoryW(path
, sec
);
743 /* Refuse to work on certain error codes before trying to create directories recursively */
744 if (ret
!= ERROR_SUCCESS
&&
745 ret
!= ERROR_FILE_EXISTS
&&
746 ret
!= ERROR_ALREADY_EXISTS
&&
747 ret
!= ERROR_FILENAME_EXCED_RANGE
)
749 WCHAR
*pEnd
, *pSlash
, szTemp
[MAX_PATH
+ 1]; /* extra for PathAddBackslash() */
751 lstrcpynW(szTemp
, path
, MAX_PATH
);
752 pEnd
= PathAddBackslashW(szTemp
);
757 while (*pSlash
&& *pSlash
!= '\\') pSlash
++;
760 *pSlash
= 0; /* terminate path at separator */
762 ret
= SHNotifyCreateDirectoryW(szTemp
, pSlash
+ 1 == pEnd
? sec
: NULL
);
764 *pSlash
++ = '\\'; /* put the separator back */
769 ret
!= ERROR_CANCELLED
&&
770 ret
!= ERROR_ALREADY_EXISTS
)
772 /* We failed and should show a dialog box */
773 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path
), ret
);
774 ret
= ERROR_CANCELLED
; /* Error has been already presented to user (not really yet!) */
780 /*************************************************************************
781 * SHFindAttrW [internal]
783 * Get the Attributes for a file or directory. The difference to GetAttributes()
784 * is that this function will also work for paths containing wildcard characters
788 * path [I] path of directory or file to check
789 * fileOnly [I] TRUE if only files should be found
792 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
793 * the first file or directory found otherwise
795 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
)
797 WIN32_FIND_DATAW wfd
;
798 BOOL b_FileMask
= fileOnly
&& (NULL
!= StrPBrkW(pName
, wWildcardChars
));
799 DWORD dwAttr
= INVALID_FILE_ATTRIBUTES
;
800 HANDLE hFind
= FindFirstFileW(pName
, &wfd
);
802 TRACE("%s %d\n", debugstr_w(pName
), fileOnly
);
803 if (INVALID_HANDLE_VALUE
!= hFind
)
807 if (b_FileMask
&& IsAttribDir(wfd
.dwFileAttributes
))
809 dwAttr
= wfd
.dwFileAttributes
;
812 while (FindNextFileW(hFind
, &wfd
));
818 /*************************************************************************
820 * SHNameTranslate HelperFunction for SHFileOperationA
822 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
823 * is NULL, only the necessary size of the string is determined and returned,
824 * otherwise the ASCII strings are copied into it and the buffer is increased
825 * to point to the location after the final 0 termination char.
827 static DWORD
SHNameTranslate(LPWSTR
* wString
, LPCWSTR
* pWToFrom
, BOOL more
)
829 DWORD size
= 0, aSize
= 0;
830 LPCSTR aString
= (LPCSTR
)*pWToFrom
;
836 size
= lstrlenA(aString
) + 1;
839 } while ((size
!= 1) && more
);
840 /* The two sizes might be different in the case of multibyte chars */
841 size
= MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, 0);
842 if (*wString
) /* only in the second loop */
844 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, size
);
845 *pWToFrom
= *wString
;
851 /*************************************************************************
852 * SHFileOperationA [SHELL32.@]
854 * Function to copy, move, delete and create one or more files with optional
858 * lpFileOp [I/O] pointer to a structure containing all the necessary information
861 * Success: ERROR_SUCCESS.
862 * Failure: ERROR_CANCELLED.
867 int WINAPI
SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp
)
869 SHFILEOPSTRUCTW nFileOp
= *((LPSHFILEOPSTRUCTW
)lpFileOp
);
872 LPWSTR ForFree
= NULL
, /* we change wString in SHNameTranslate and can't use it for freeing */
873 wString
= NULL
; /* we change this in SHNameTranslate */
876 if (FO_DELETE
== (nFileOp
.wFunc
& FO_MASK
))
877 nFileOp
.pTo
= NULL
; /* we need a NULL or a valid pointer for translation */
878 if (!(nFileOp
.fFlags
& FOF_SIMPLEPROGRESS
))
879 nFileOp
.lpszProgressTitle
= NULL
; /* we need a NULL or a valid pointer for translation */
880 while (1) /* every loop calculate size, second translate also, if we have storage for this */
882 size
= SHNameTranslate(&wString
, &nFileOp
.lpszProgressTitle
, FALSE
); /* no loop */
883 size
+= SHNameTranslate(&wString
, &nFileOp
.pFrom
, TRUE
); /* internal loop */
884 size
+= SHNameTranslate(&wString
, &nFileOp
.pTo
, TRUE
); /* internal loop */
888 retCode
= SHFileOperationW(&nFileOp
);
889 /* Windows 95/98 returns S_OK for this case. */
890 if (retCode
== ERROR_ACCESS_DENIED
&& (GetVersion() & 0x80000000))
893 heap_free(ForFree
); /* we cannot use wString, it was changed */
898 wString
= ForFree
= heap_alloc(size
* sizeof(WCHAR
));
899 if (ForFree
) continue;
900 retCode
= ERROR_OUTOFMEMORY
;
901 nFileOp
.fAnyOperationsAborted
= TRUE
;
906 lpFileOp
->hNameMappings
= nFileOp
.hNameMappings
;
907 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
911 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
929 BOOL bAnyFromWildcard
;
930 BOOL bAnyDirectories
;
935 static inline void grow_list(FILE_LIST
*list
)
937 FILE_ENTRY
*new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, list
->feFiles
,
938 list
->num_alloc
* 2 * sizeof(*new) );
940 list
->num_alloc
*= 2;
943 /* adds a file to the FILE_ENTRY struct
945 static void add_file_to_entry(FILE_ENTRY
*feFile
, LPCWSTR szFile
)
947 DWORD dwLen
= lstrlenW(szFile
) + 1;
950 feFile
->szFullPath
= heap_alloc(dwLen
* sizeof(WCHAR
));
951 lstrcpyW(feFile
->szFullPath
, szFile
);
953 ptr
= StrRChrW(szFile
, NULL
, '\\');
956 dwLen
= ptr
- szFile
+ 1;
957 feFile
->szDirectory
= heap_alloc(dwLen
* sizeof(WCHAR
));
958 lstrcpynW(feFile
->szDirectory
, szFile
, dwLen
);
960 dwLen
= lstrlenW(feFile
->szFullPath
) - dwLen
+ 1;
961 feFile
->szFilename
= heap_alloc(dwLen
* sizeof(WCHAR
));
962 lstrcpyW(feFile
->szFilename
, ptr
+ 1); /* skip over backslash */
964 feFile
->bFromWildcard
= FALSE
;
967 static LPWSTR
wildcard_to_file(LPCWSTR szWildCard
, LPCWSTR szFileName
)
971 DWORD dwDirLen
, dwFullLen
;
973 ptr
= StrRChrW(szWildCard
, NULL
, '\\');
974 dwDirLen
= ptr
- szWildCard
+ 1;
976 dwFullLen
= dwDirLen
+ lstrlenW(szFileName
) + 1;
977 szFullPath
= heap_alloc(dwFullLen
* sizeof(WCHAR
));
979 lstrcpynW(szFullPath
, szWildCard
, dwDirLen
+ 1);
980 lstrcatW(szFullPath
, szFileName
);
985 static void parse_wildcard_files(FILE_LIST
*flList
, LPCWSTR szFile
, LPDWORD pdwListIndex
)
987 WIN32_FIND_DATAW wfd
;
988 HANDLE hFile
= FindFirstFileW(szFile
, &wfd
);
993 if (hFile
== INVALID_HANDLE_VALUE
) return;
995 for (res
= TRUE
; res
; res
= FindNextFileW(hFile
, &wfd
))
997 if (IsDotDir(wfd
.cFileName
)) continue;
998 if (*pdwListIndex
>= flList
->num_alloc
) grow_list( flList
);
999 szFullPath
= wildcard_to_file(szFile
, wfd
.cFileName
);
1000 file
= &flList
->feFiles
[(*pdwListIndex
)++];
1001 add_file_to_entry(file
, szFullPath
);
1002 file
->bFromWildcard
= TRUE
;
1003 file
->attributes
= wfd
.dwFileAttributes
;
1004 if (IsAttribDir(file
->attributes
)) flList
->bAnyDirectories
= TRUE
;
1005 heap_free(szFullPath
);
1011 /* takes the null-separated file list and fills out the FILE_LIST */
1012 static HRESULT
parse_file_list(FILE_LIST
*flList
, LPCWSTR szFiles
)
1014 LPCWSTR ptr
= szFiles
;
1015 WCHAR szCurFile
[MAX_PATH
];
1020 return ERROR_INVALID_PARAMETER
;
1022 flList
->bAnyFromWildcard
= FALSE
;
1023 flList
->bAnyDirectories
= FALSE
;
1024 flList
->bAnyDontExist
= FALSE
;
1025 flList
->num_alloc
= 32;
1026 flList
->dwNumFiles
= 0;
1030 return ERROR_ACCESS_DENIED
;
1032 flList
->feFiles
= heap_alloc_zero(flList
->num_alloc
* sizeof(FILE_ENTRY
));
1036 if (i
>= flList
->num_alloc
) grow_list( flList
);
1038 /* change relative to absolute path */
1039 if (PathIsRelativeW(ptr
))
1041 GetCurrentDirectoryW(MAX_PATH
, szCurFile
);
1042 PathCombineW(szCurFile
, szCurFile
, ptr
);
1043 flList
->feFiles
[i
].bFromRelative
= TRUE
;
1047 lstrcpyW(szCurFile
, ptr
);
1048 flList
->feFiles
[i
].bFromRelative
= FALSE
;
1051 for (p
= szCurFile
; *p
; p
++) if (*p
== '/') *p
= '\\';
1053 /* parse wildcard files if they are in the filename */
1054 if (StrPBrkW(szCurFile
, wWildcardChars
))
1056 parse_wildcard_files(flList
, szCurFile
, &i
);
1057 flList
->bAnyFromWildcard
= TRUE
;
1062 FILE_ENTRY
*file
= &flList
->feFiles
[i
];
1063 add_file_to_entry(file
, szCurFile
);
1064 file
->attributes
= GetFileAttributesW( file
->szFullPath
);
1065 file
->bExists
= (file
->attributes
!= INVALID_FILE_ATTRIBUTES
);
1066 if (!file
->bExists
) flList
->bAnyDontExist
= TRUE
;
1067 if (IsAttribDir(file
->attributes
)) flList
->bAnyDirectories
= TRUE
;
1070 /* advance to the next string */
1071 ptr
+= lstrlenW(ptr
) + 1;
1074 flList
->dwNumFiles
= i
;
1079 /* free the FILE_LIST */
1080 static void destroy_file_list(FILE_LIST
*flList
)
1084 if (!flList
|| !flList
->feFiles
)
1087 for (i
= 0; i
< flList
->dwNumFiles
; i
++)
1089 heap_free(flList
->feFiles
[i
].szDirectory
);
1090 heap_free(flList
->feFiles
[i
].szFilename
);
1091 heap_free(flList
->feFiles
[i
].szFullPath
);
1094 heap_free(flList
->feFiles
);
1097 static void copy_dir_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, LPCWSTR szDestPath
)
1099 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
1100 SHFILEOPSTRUCTW fileOp
;
1102 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1104 if (IsDotDir(feFrom
->szFilename
))
1107 if (PathFileExistsW(szDestPath
))
1108 PathCombineW(szTo
, szDestPath
, feFrom
->szFilename
);
1110 lstrcpyW(szTo
, szDestPath
);
1112 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
)) {
1113 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FOLDER
, feFrom
->szFilename
, op
))
1115 /* Vista returns an ERROR_CANCELLED even if user pressed "No" */
1116 if (!op
->bManyItems
)
1117 op
->bCancelled
= TRUE
;
1122 szTo
[lstrlenW(szTo
) + 1] = '\0';
1123 SHNotifyCreateDirectoryW(szTo
, NULL
);
1125 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1126 szFrom
[lstrlenW(szFrom
) + 1] = '\0';
1129 fileOp
.pFrom
= szFrom
;
1131 fileOp
.fFlags
&= ~FOF_MULTIDESTFILES
; /* we know we're copying to one dir */
1133 /* Don't ask the user about overwriting files when he accepted to overwrite the
1134 folder. FIXME: this is not exactly what Windows does - e.g. there would be
1135 an additional confirmation for a nested folder */
1136 fileOp
.fFlags
|= FOF_NOCONFIRMATION
;
1138 SHFileOperationW(&fileOp
);
1141 static BOOL
copy_file_to_file(FILE_OPERATION
*op
, const WCHAR
*szFrom
, const WCHAR
*szTo
)
1143 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
))
1145 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FILE
, PathFindFileNameW(szTo
), op
))
1149 return SHNotifyCopyFileW(szFrom
, szTo
, FALSE
) == 0;
1152 /* copy a file or directory to another directory */
1153 static void copy_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1155 if (!PathFileExistsW(feTo
->szFullPath
))
1156 SHNotifyCreateDirectoryW(feTo
->szFullPath
, NULL
);
1158 if (IsAttribFile(feFrom
->attributes
))
1160 WCHAR szDestPath
[MAX_PATH
];
1162 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1163 copy_file_to_file(op
, feFrom
->szFullPath
, szDestPath
);
1165 else if (!(op
->req
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1166 copy_dir_to_dir(op
, feFrom
, feTo
->szFullPath
);
1169 static void create_dest_dirs(LPCWSTR szDestDir
)
1171 WCHAR dir
[MAX_PATH
];
1172 LPCWSTR ptr
= StrChrW(szDestDir
, '\\');
1174 /* make sure all directories up to last one are created */
1175 while (ptr
&& (ptr
= StrChrW(ptr
+ 1, '\\')))
1177 lstrcpynW(dir
, szDestDir
, ptr
- szDestDir
+ 1);
1179 if (!PathFileExistsW(dir
))
1180 SHNotifyCreateDirectoryW(dir
, NULL
);
1183 /* create last directory */
1184 if (!PathFileExistsW(szDestDir
))
1185 SHNotifyCreateDirectoryW(szDestDir
, NULL
);
1188 /* the FO_COPY operation */
1189 static int copy_files(FILE_OPERATION
*op
, const FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
1192 const FILE_ENTRY
*entryToCopy
;
1193 const FILE_ENTRY
*fileDest
= &flTo
->feFiles
[0];
1195 if (flFrom
->bAnyDontExist
)
1196 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1198 if (flTo
->dwNumFiles
== 0)
1200 /* If the destination is empty, SHFileOperation should use the current directory */
1201 WCHAR curdir
[MAX_PATH
+1];
1203 GetCurrentDirectoryW(MAX_PATH
, curdir
);
1204 curdir
[lstrlenW(curdir
)+1] = 0;
1206 destroy_file_list(flTo
);
1207 ZeroMemory(flTo
, sizeof(FILE_LIST
));
1208 parse_file_list(flTo
, curdir
);
1209 fileDest
= &flTo
->feFiles
[0];
1212 if (op
->req
->fFlags
& FOF_MULTIDESTFILES
&& flTo
->dwNumFiles
> 1)
1214 if (flFrom
->bAnyFromWildcard
)
1215 return ERROR_CANCELLED
;
1217 if (flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1219 if (flFrom
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1220 return ERROR_CANCELLED
;
1222 /* Free all but the first entry. */
1223 for (i
= 1; i
< flTo
->dwNumFiles
; i
++)
1225 heap_free(flTo
->feFiles
[i
].szDirectory
);
1226 heap_free(flTo
->feFiles
[i
].szFilename
);
1227 heap_free(flTo
->feFiles
[i
].szFullPath
);
1230 flTo
->dwNumFiles
= 1;
1232 else if (IsAttribDir(fileDest
->attributes
))
1234 for (i
= 1; i
< flTo
->dwNumFiles
; i
++)
1235 if (!IsAttribDir(flTo
->feFiles
[i
].attributes
) ||
1236 !IsAttribDir(flFrom
->feFiles
[i
].attributes
))
1238 return ERROR_CANCELLED
;
1242 else if (flFrom
->dwNumFiles
!= 1)
1244 if (flTo
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1245 return ERROR_CANCELLED
;
1247 if (PathFileExistsW(fileDest
->szFullPath
) &&
1248 IsAttribFile(fileDest
->attributes
))
1250 return ERROR_CANCELLED
;
1253 if (flTo
->dwNumFiles
== 1 && fileDest
->bFromRelative
&&
1254 !PathFileExistsW(fileDest
->szFullPath
))
1256 return ERROR_CANCELLED
;
1260 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1262 entryToCopy
= &flFrom
->feFiles
[i
];
1264 if ((op
->req
->fFlags
& FOF_MULTIDESTFILES
) &&
1265 flTo
->dwNumFiles
> 1)
1267 fileDest
= &flTo
->feFiles
[i
];
1270 if (IsAttribDir(entryToCopy
->attributes
) &&
1271 !lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szDirectory
))
1273 return ERROR_SUCCESS
;
1276 create_dest_dirs(fileDest
->szDirectory
);
1278 if (!lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1280 if (IsAttribFile(entryToCopy
->attributes
))
1281 return ERROR_NO_MORE_SEARCH_HANDLES
;
1283 return ERROR_SUCCESS
;
1286 if ((flFrom
->dwNumFiles
> 1 && flTo
->dwNumFiles
== 1) ||
1287 IsAttribDir(fileDest
->attributes
))
1289 copy_to_dir(op
, entryToCopy
, fileDest
);
1291 else if (IsAttribDir(entryToCopy
->attributes
))
1293 copy_dir_to_dir(op
, entryToCopy
, fileDest
->szFullPath
);
1297 if (!copy_file_to_file(op
, entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1299 op
->req
->fAnyOperationsAborted
= TRUE
;
1300 return ERROR_CANCELLED
;
1304 /* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
1306 return ERROR_CANCELLED
;
1309 /* Vista return code. On XP if the used pressed "No" for the last item,
1310 * ERROR_ARENA_TRASHED would be returned */
1311 return ERROR_SUCCESS
;
1314 static BOOL
confirm_delete_list(HWND hWnd
, DWORD fFlags
, BOOL fTrash
, const FILE_LIST
*flFrom
)
1316 if (flFrom
->dwNumFiles
> 1)
1318 static const WCHAR format
[] = {'%','d',0};
1321 wnsprintfW(tmp
, ARRAY_SIZE(tmp
), format
, flFrom
->dwNumFiles
);
1322 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_MULTIPLE_ITEM
:ASK_DELETE_MULTIPLE_ITEM
), tmp
, NULL
);
1326 const FILE_ENTRY
*fileEntry
= &flFrom
->feFiles
[0];
1328 if (IsAttribFile(fileEntry
->attributes
))
1329 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FILE
:ASK_DELETE_FILE
), fileEntry
->szFullPath
, NULL
);
1330 else if (!(fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1331 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FOLDER
:ASK_DELETE_FOLDER
), fileEntry
->szFullPath
, NULL
);
1336 /* the FO_DELETE operation */
1337 static int delete_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
)
1339 const FILE_ENTRY
*fileEntry
;
1344 if (!flFrom
->dwNumFiles
)
1345 return ERROR_SUCCESS
;
1347 /* Windows also checks only the first item */
1348 bTrash
= (lpFileOp
->fFlags
& FOF_ALLOWUNDO
)
1349 && TRASH_CanTrashFile(flFrom
->feFiles
[0].szFullPath
);
1351 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (!bTrash
&& lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1352 if (!confirm_delete_list(lpFileOp
->hwnd
, lpFileOp
->fFlags
, bTrash
, flFrom
))
1354 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1358 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1360 fileEntry
= &flFrom
->feFiles
[i
];
1362 if (!IsAttribFile(fileEntry
->attributes
) &&
1363 (lpFileOp
->fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1369 if (TRASH_TrashFile(fileEntry
->szFullPath
))
1372 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1373 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1374 bDelete
= SHELL_ConfirmDialogW(lpFileOp
->hwnd
, ASK_CANT_TRASH_ITEM
, fileEntry
->szFullPath
, NULL
);
1380 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1385 /* delete the file or directory */
1386 if (IsAttribFile(fileEntry
->attributes
))
1387 ret
= DeleteFileW(fileEntry
->szFullPath
) ?
1388 ERROR_SUCCESS
: GetLastError();
1390 ret
= SHELL_DeleteDirectoryW(lpFileOp
->hwnd
, fileEntry
->szFullPath
, FALSE
);
1396 return ERROR_SUCCESS
;
1399 /* moves a file or directory to another directory */
1400 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1402 WCHAR szDestPath
[MAX_PATH
];
1404 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1405 SHNotifyMoveFileW(feFrom
->szFullPath
, szDestPath
);
1408 /* the FO_MOVE operation */
1409 static int move_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
, const FILE_LIST
*flTo
)
1413 const FILE_ENTRY
*entryToMove
;
1414 const FILE_ENTRY
*fileDest
;
1416 if (!flFrom
->dwNumFiles
)
1417 return ERROR_SUCCESS
;
1419 if (!flTo
->dwNumFiles
)
1420 return ERROR_FILE_NOT_FOUND
;
1422 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1423 flTo
->dwNumFiles
> 1 && flFrom
->dwNumFiles
> 1)
1425 return ERROR_CANCELLED
;
1428 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1429 !flFrom
->bAnyDirectories
&&
1430 flFrom
->dwNumFiles
> flTo
->dwNumFiles
)
1432 return ERROR_CANCELLED
;
1435 if (!PathFileExistsW(flTo
->feFiles
[0].szDirectory
))
1436 return ERROR_CANCELLED
;
1438 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
)
1439 mismatched
= flFrom
->dwNumFiles
- flTo
->dwNumFiles
;
1441 fileDest
= &flTo
->feFiles
[0];
1442 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1444 entryToMove
= &flFrom
->feFiles
[i
];
1446 if (!PathFileExistsW(fileDest
->szDirectory
))
1447 return ERROR_CANCELLED
;
1449 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
)
1451 if (i
>= flTo
->dwNumFiles
)
1453 fileDest
= &flTo
->feFiles
[i
];
1454 if (mismatched
&& !fileDest
->bExists
)
1456 create_dest_dirs(flTo
->feFiles
[i
].szFullPath
);
1457 flTo
->feFiles
[i
].bExists
= TRUE
;
1458 flTo
->feFiles
[i
].attributes
= FILE_ATTRIBUTE_DIRECTORY
;
1462 if (fileDest
->bExists
&& IsAttribDir(fileDest
->attributes
))
1463 move_to_dir(lpFileOp
, entryToMove
, fileDest
);
1465 SHNotifyMoveFileW(entryToMove
->szFullPath
, fileDest
->szFullPath
);
1470 if (flFrom
->bAnyDirectories
)
1471 return DE_DESTSAMETREE
;
1476 return ERROR_SUCCESS
;
1479 /* the FO_RENAME files */
1480 static int 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);
1544 lpFileOp
->fAnyOperationsAborted
= FALSE
;
1546 switch (lpFileOp
->wFunc
)
1549 ret
= copy_files(&op
, &flFrom
, &flTo
);
1552 ret
= delete_files(lpFileOp
, &flFrom
);
1555 ret
= move_files(lpFileOp
, &flFrom
, &flTo
);
1558 ret
= rename_files(lpFileOp
, &flFrom
, &flTo
);
1561 ret
= ERROR_INVALID_PARAMETER
;
1565 destroy_file_list(&flFrom
);
1567 if (lpFileOp
->wFunc
!= FO_DELETE
)
1568 destroy_file_list(&flTo
);
1570 if (ret
== ERROR_CANCELLED
)
1571 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1573 SetLastError(ERROR_SUCCESS
);
1577 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1579 /*************************************************************************
1580 * SHFreeNameMappings [shell32.246]
1582 * Free the mapping handle returned by SHFileOperation if FOF_WANTSMAPPINGHANDLE
1586 * hNameMapping [I] handle to the name mappings used during renaming of files
1591 void WINAPI
SHFreeNameMappings(HANDLE hNameMapping
)
1595 int i
= SHDSA_GetItemCount((HDSA
)hNameMapping
) - 1;
1599 LPSHNAMEMAPPINGW lp
= DSA_GetItemPtr(hNameMapping
, i
);
1601 SHFree(lp
->pszOldPath
);
1602 SHFree(lp
->pszNewPath
);
1604 DSA_Destroy(hNameMapping
);
1608 /*************************************************************************
1609 * SheGetDirA [SHELL32.@]
1611 * drive = 0: returns the current directory path
1612 * drive > 0: returns the current directory path of the specified drive
1613 * drive=1 -> A: drive=2 -> B: ...
1614 * returns 0 if successful
1616 DWORD WINAPI
SheGetDirA(DWORD drive
, LPSTR buffer
)
1618 WCHAR org_path
[MAX_PATH
];
1622 /* change current directory to the specified drive */
1624 strcpy(drv_path
, "A:");
1625 drv_path
[0] += (char)drive
-1;
1627 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1629 SetCurrentDirectoryA(drv_path
);
1632 /* query current directory path of the specified drive */
1633 ret
= GetCurrentDirectoryA(MAX_PATH
, buffer
);
1635 /* back to the original drive */
1637 SetCurrentDirectoryW(org_path
);
1640 return GetLastError();
1645 /*************************************************************************
1646 * SheGetDirW [SHELL32.@]
1648 * drive = 0: returns the current directory path
1649 * drive > 0: returns the current directory path of the specified drive
1650 * drive=1 -> A: drive=2 -> B: ...
1651 * returns 0 if successful
1653 DWORD WINAPI
SheGetDirW(DWORD drive
, LPWSTR buffer
)
1655 WCHAR org_path
[MAX_PATH
];
1659 /* change current directory to the specified drive */
1661 strcpy(drv_path
, "A:");
1662 drv_path
[0] += (char)drive
-1;
1664 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1666 SetCurrentDirectoryA(drv_path
);
1669 /* query current directory path of the specified drive */
1670 ret
= GetCurrentDirectoryW(MAX_PATH
, buffer
);
1672 /* back to the original drive */
1674 SetCurrentDirectoryW(org_path
);
1677 return GetLastError();
1682 /*************************************************************************
1683 * SheChangeDirA [SHELL32.@]
1685 * changes the current directory to the specified path
1686 * and returns 0 if successful
1688 DWORD WINAPI
SheChangeDirA(LPSTR path
)
1690 if (SetCurrentDirectoryA(path
))
1693 return GetLastError();
1696 /*************************************************************************
1697 * SheChangeDirW [SHELL32.@]
1699 * changes the current directory to the specified path
1700 * and returns 0 if successful
1702 DWORD WINAPI
SheChangeDirW(LPWSTR path
)
1704 if (SetCurrentDirectoryW(path
))
1707 return GetLastError();
1710 /*************************************************************************
1711 * IsNetDrive [SHELL32.66]
1713 int WINAPI
IsNetDrive(int drive
)
1716 strcpy(root
, "A:\\");
1717 root
[0] += (char)drive
;
1718 return (GetDriveTypeA(root
) == DRIVE_REMOTE
);
1722 /*************************************************************************
1723 * RealDriveType [SHELL32.524]
1725 int WINAPI
RealDriveType(int drive
, BOOL bQueryNet
)
1727 char root
[] = "A:\\";
1728 root
[0] += (char)drive
;
1729 return GetDriveTypeA(root
);
1732 /***********************************************************************
1733 * SHPathPrepareForWriteA (SHELL32.@)
1735 HRESULT WINAPI
SHPathPrepareForWriteA(HWND hwnd
, IUnknown
*modless
, LPCSTR path
, DWORD flags
)
1737 WCHAR wpath
[MAX_PATH
];
1738 MultiByteToWideChar( CP_ACP
, 0, path
, -1, wpath
, MAX_PATH
);
1739 return SHPathPrepareForWriteW(hwnd
, modless
, wpath
, flags
);
1742 /***********************************************************************
1743 * SHPathPrepareForWriteW (SHELL32.@)
1745 HRESULT WINAPI
SHPathPrepareForWriteW(HWND hwnd
, IUnknown
*modless
, LPCWSTR path
, DWORD flags
)
1752 WCHAR
* temppath
=NULL
;
1754 TRACE("%p %p %s 0x%08x\n", hwnd
, modless
, debugstr_w(path
), flags
);
1756 if (flags
& ~(SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
|SHPPFW_IGNOREFILENAME
))
1757 FIXME("unimplemented flags 0x%08x\n", flags
);
1759 /* cut off filename if necessary */
1760 if (flags
& SHPPFW_IGNOREFILENAME
)
1762 last_slash
= StrRChrW(path
, NULL
, '\\');
1763 if (last_slash
== NULL
)
1766 len
= last_slash
- path
+ 1;
1767 temppath
= heap_alloc(len
* sizeof(WCHAR
));
1769 return E_OUTOFMEMORY
;
1770 StrCpyNW(temppath
, path
, len
);
1771 realpath
= temppath
;
1778 /* try to create the directory if asked to */
1779 if (flags
& (SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
))
1781 if (flags
& SHPPFW_ASKDIRCREATE
)
1782 FIXME("treating SHPPFW_ASKDIRCREATE as SHPPFW_DIRCREATE\n");
1784 SHCreateDirectoryExW(0, realpath
, NULL
);
1787 /* check if we can access the directory */
1788 res
= GetFileAttributesW(realpath
);
1790 heap_free(temppath
);
1792 if (res
== INVALID_FILE_ATTRIBUTES
)
1794 err
= GetLastError();
1795 if (err
== ERROR_FILE_NOT_FOUND
)
1796 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
1797 return HRESULT_FROM_WIN32(err
);
1799 else if (res
& FILE_ATTRIBUTE_DIRECTORY
)
1802 return HRESULT_FROM_WIN32(ERROR_DIRECTORY
);
1805 /*************************************************************************
1806 * SHMultiFileProperties [SHELL32.@]
1809 HRESULT WINAPI
SHMultiFileProperties(IDataObject
*pdtobj
, DWORD flags
)
1811 FIXME("stub: %p %u\n", pdtobj
, flags
);
1815 struct file_operation
1817 IFileOperation IFileOperation_iface
;
1821 static inline struct file_operation
*impl_from_IFileOperation(IFileOperation
*iface
)
1823 return CONTAINING_RECORD(iface
, struct file_operation
, IFileOperation_iface
);
1826 static HRESULT WINAPI
file_operation_QueryInterface(IFileOperation
*iface
, REFIID riid
, void **out
)
1828 struct file_operation
*operation
= impl_from_IFileOperation(iface
);
1830 TRACE("(%p, %s, %p).\n", iface
, debugstr_guid(riid
), out
);
1832 if (IsEqualIID(&IID_IFileOperation
, riid
) ||
1833 IsEqualIID(&IID_IUnknown
, riid
))
1834 *out
= &operation
->IFileOperation_iface
;
1837 FIXME("not implemented for %s.\n", debugstr_guid(riid
));
1839 return E_NOINTERFACE
;
1842 IUnknown_AddRef((IUnknown
*)*out
);
1846 static ULONG WINAPI
file_operation_AddRef(IFileOperation
*iface
)
1848 struct file_operation
*operation
= impl_from_IFileOperation(iface
);
1849 ULONG ref
= InterlockedIncrement(&operation
->ref
);
1851 TRACE("(%p): ref=%u.\n", iface
, ref
);
1856 static ULONG WINAPI
file_operation_Release(IFileOperation
*iface
)
1858 struct file_operation
*operation
= impl_from_IFileOperation(iface
);
1859 ULONG ref
= InterlockedDecrement(&operation
->ref
);
1861 TRACE("(%p): ref=%u.\n", iface
, ref
);
1865 HeapFree(GetProcessHeap(), 0, operation
);
1871 static HRESULT WINAPI
file_operation_Advise(IFileOperation
*iface
, IFileOperationProgressSink
*sink
, DWORD
*cookie
)
1873 FIXME("(%p, %p, %p): stub.\n", iface
, sink
, cookie
);
1878 static HRESULT WINAPI
file_operation_Unadvise(IFileOperation
*iface
, DWORD cookie
)
1880 FIXME("(%p, %x): stub.\n", iface
, cookie
);
1885 static HRESULT WINAPI
file_operation_SetOperationFlags(IFileOperation
*iface
, DWORD flags
)
1887 FIXME("(%p, %x): stub.\n", iface
, flags
);
1892 static HRESULT WINAPI
file_operation_SetProgressMessage(IFileOperation
*iface
, LPCWSTR message
)
1894 FIXME("(%p, %s): stub.\n", iface
, debugstr_w(message
));
1899 static HRESULT WINAPI
file_operation_SetProgressDialog(IFileOperation
*iface
, IOperationsProgressDialog
*dialog
)
1901 FIXME("(%p, %p): stub.\n", iface
, dialog
);
1906 static HRESULT WINAPI
file_operation_SetProperties(IFileOperation
*iface
, IPropertyChangeArray
*array
)
1908 FIXME("(%p, %p): stub.\n", iface
, array
);
1913 static HRESULT WINAPI
file_operation_SetOwnerWindow(IFileOperation
*iface
, HWND owner
)
1915 FIXME("(%p, %p): stub.\n", iface
, owner
);
1920 static HRESULT WINAPI
file_operation_ApplyPropertiesToItem(IFileOperation
*iface
, IShellItem
*item
)
1922 FIXME("(%p, %p): stub.\n", iface
, item
);
1927 static HRESULT WINAPI
file_operation_ApplyPropertiesToItems(IFileOperation
*iface
, IUnknown
*items
)
1929 FIXME("(%p, %p): stub.\n", iface
, items
);
1934 static HRESULT WINAPI
file_operation_RenameItem(IFileOperation
*iface
, IShellItem
*item
, LPCWSTR name
,
1935 IFileOperationProgressSink
*sink
)
1937 FIXME("(%p, %p, %s, %p): stub.\n", iface
, item
, debugstr_w(name
), sink
);
1942 static HRESULT WINAPI
file_operation_RenameItems(IFileOperation
*iface
, IUnknown
*items
, LPCWSTR name
)
1944 FIXME("(%p, %p, %s): stub.\n", iface
, items
, debugstr_w(name
));
1949 static HRESULT WINAPI
file_operation_MoveItem(IFileOperation
*iface
, IShellItem
*item
, IShellItem
*folder
,
1950 LPCWSTR name
, IFileOperationProgressSink
*sink
)
1952 FIXME("(%p, %p, %p, %s, %p): stub.\n", iface
, item
, folder
, debugstr_w(name
), sink
);
1957 static HRESULT WINAPI
file_operation_MoveItems(IFileOperation
*iface
, IUnknown
*items
, IShellItem
*folder
)
1959 FIXME("(%p, %p, %p): stub.\n", iface
, items
, folder
);
1964 static HRESULT WINAPI
file_operation_CopyItem(IFileOperation
*iface
, IShellItem
*item
, IShellItem
*folder
,
1965 LPCWSTR name
, IFileOperationProgressSink
*sink
)
1967 FIXME("(%p, %p, %p, %s, %p): stub.\n", iface
, item
, folder
, debugstr_w(name
), sink
);
1972 static HRESULT WINAPI
file_operation_CopyItems(IFileOperation
*iface
, IUnknown
*items
, IShellItem
*folder
)
1974 FIXME("(%p, %p, %p): stub.\n", iface
, items
, folder
);
1979 static HRESULT WINAPI
file_operation_DeleteItem(IFileOperation
*iface
, IShellItem
*item
,
1980 IFileOperationProgressSink
*sink
)
1982 FIXME("(%p, %p, %p): stub.\n", iface
, item
, sink
);
1987 static HRESULT WINAPI
file_operation_DeleteItems(IFileOperation
*iface
, IUnknown
*items
)
1989 FIXME("(%p, %p): stub.\n", iface
, items
);
1994 static HRESULT WINAPI
file_operation_NewItem(IFileOperation
*iface
, IShellItem
*folder
, DWORD attributes
,
1995 LPCWSTR name
, LPCWSTR
template, IFileOperationProgressSink
*sink
)
1997 FIXME("(%p, %p, %x, %s, %s, %p): stub.\n", iface
, folder
, attributes
,
1998 debugstr_w(name
), debugstr_w(template), sink
);
2003 static HRESULT WINAPI
file_operation_PerformOperations(IFileOperation
*iface
)
2005 FIXME("(%p): stub.\n", iface
);
2010 static HRESULT WINAPI
file_operation_GetAnyOperationsAborted(IFileOperation
*iface
, BOOL
*aborted
)
2012 FIXME("(%p, %p): stub.\n", iface
, aborted
);
2017 static const IFileOperationVtbl file_operation_vtbl
=
2019 file_operation_QueryInterface
,
2020 file_operation_AddRef
,
2021 file_operation_Release
,
2022 file_operation_Advise
,
2023 file_operation_Unadvise
,
2024 file_operation_SetOperationFlags
,
2025 file_operation_SetProgressMessage
,
2026 file_operation_SetProgressDialog
,
2027 file_operation_SetProperties
,
2028 file_operation_SetOwnerWindow
,
2029 file_operation_ApplyPropertiesToItem
,
2030 file_operation_ApplyPropertiesToItems
,
2031 file_operation_RenameItem
,
2032 file_operation_RenameItems
,
2033 file_operation_MoveItem
,
2034 file_operation_MoveItems
,
2035 file_operation_CopyItem
,
2036 file_operation_CopyItems
,
2037 file_operation_DeleteItem
,
2038 file_operation_DeleteItems
,
2039 file_operation_NewItem
,
2040 file_operation_PerformOperations
,
2041 file_operation_GetAnyOperationsAborted
2044 HRESULT WINAPI
IFileOperation_Constructor(IUnknown
*outer
, REFIID riid
, void **out
)
2046 struct file_operation
*object
;
2049 object
= heap_alloc_zero(sizeof(*object
));
2051 return E_OUTOFMEMORY
;
2053 object
->IFileOperation_iface
.lpVtbl
= &file_operation_vtbl
;
2056 hr
= IFileOperation_QueryInterface(&object
->IFileOperation_iface
, riid
, out
);
2057 IFileOperation_Release(&object
->IFileOperation_iface
);