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
28 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
30 #define IsAttrib(x, y) ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
31 #define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
32 #define IsAttribDir(x) IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
33 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
36 #define WM_FILE (WM_USER + 1)
37 #define TIMER_ID (100)
39 static const WCHAR wWildcardFile
[] = {'*',0};
40 static const WCHAR wWildcardChars
[] = {'*','?',0};
42 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
);
43 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
);
44 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
);
45 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
, BOOL isdir
);
46 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bFailIfExists
);
47 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
);
57 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
75 BOOL bAnyFromWildcard
;
88 }FILE_OPERATION_CONTEXT
;
91 /* Confirm dialogs with an optional "Yes To All" as used in file operations confirmations
93 static const WCHAR CONFIRM_MSG_PROP
[] = {'W','I','N','E','_','C','O','N','F','I','R','M',0};
95 struct confirm_msg_info
103 /* as some buttons may be hidden and the dialog height may change we may need
104 * to move the controls */
105 static void confirm_msg_move_button(HWND hDlg
, INT iId
, INT
*xPos
, INT yOffset
, BOOL bShow
)
107 HWND hButton
= GetDlgItem(hDlg
, iId
);
115 GetWindowRect(hButton
, &r
);
116 width
= r
.right
- r
.left
;
119 ScreenToClient(hDlg
, &pt
);
120 MoveWindow(hButton
, *xPos
- width
, pt
.y
- yOffset
, width
, r
.bottom
- r
.top
, FALSE
);
124 ShowWindow(hButton
, SW_HIDE
);
127 /* Note: we paint the text manually and don't use the static control to make
128 * sure the text has the same height as the one computed in WM_INITDIALOG
130 static INT_PTR
ConfirmMsgBox_Paint(HWND hDlg
)
137 BeginPaint(hDlg
, &ps
);
140 GetClientRect(GetDlgItem(hDlg
, IDC_YESTOALL_MESSAGE
), &r
);
141 /* this will remap the rect to dialog coords */
142 MapWindowPoints(GetDlgItem(hDlg
, IDC_YESTOALL_MESSAGE
), hDlg
, (LPPOINT
)&r
, 2);
143 hOldFont
= (HFONT
)SelectObject(hdc
, (HFONT
)SendDlgItemMessageW(hDlg
, IDC_YESTOALL_MESSAGE
, WM_GETFONT
, 0, 0));
144 DrawTextW(hdc
, (LPWSTR
)GetPropW(hDlg
, CONFIRM_MSG_PROP
), -1, &r
, DT_NOPREFIX
| DT_PATH_ELLIPSIS
| DT_WORDBREAK
);
145 SelectObject(hdc
, hOldFont
);
151 static INT_PTR
ConfirmMsgBox_Init(HWND hDlg
, LPARAM lParam
)
153 struct confirm_msg_info
*info
= (struct confirm_msg_info
*)lParam
;
160 SetWindowTextW(hDlg
, info
->lpszCaption
);
161 ShowWindow(GetDlgItem(hDlg
, IDC_YESTOALL_MESSAGE
), SW_HIDE
);
162 SetPropW(hDlg
, CONFIRM_MSG_PROP
, info
->lpszText
);
163 SendDlgItemMessageW(hDlg
, IDC_YESTOALL_ICON
, STM_SETICON
, (WPARAM
)info
->hIcon
, 0);
165 /* compute the text height and resize the dialog */
166 GetClientRect(GetDlgItem(hDlg
, IDC_YESTOALL_MESSAGE
), &r
);
169 hOldFont
= (HFONT
)SelectObject(hdc
, (HFONT
)SendDlgItemMessageW(hDlg
, IDC_YESTOALL_MESSAGE
, WM_GETFONT
, 0, 0));
170 DrawTextW(hdc
, info
->lpszText
, -1, &r
, DT_NOPREFIX
| DT_PATH_ELLIPSIS
| DT_WORDBREAK
| DT_CALCRECT
);
171 SelectObject(hdc
, hOldFont
);
173 yOffset
= min(yOffset
, 35); /* don't make the dialog too small */
174 ReleaseDC(hDlg
, hdc
);
176 GetClientRect(hDlg
, &r
);
178 GetWindowRect(hDlg
, &r
);
179 width
= r
.right
- r
.left
;
180 height
= r
.bottom
- r
.top
- yOffset
;
181 MoveWindow(hDlg
, (GetSystemMetrics(SM_CXSCREEN
) - width
)/2,
182 (GetSystemMetrics(SM_CYSCREEN
) - height
)/2, width
, height
, FALSE
);
184 confirm_msg_move_button(hDlg
, IDCANCEL
, &xPos
, yOffset
, info
->bYesToAll
);
185 confirm_msg_move_button(hDlg
, IDNO
, &xPos
, yOffset
, TRUE
);
186 confirm_msg_move_button(hDlg
, IDC_YESTOALL
, &xPos
, yOffset
, info
->bYesToAll
);
187 confirm_msg_move_button(hDlg
, IDYES
, &xPos
, yOffset
, TRUE
);
192 static INT_PTR CALLBACK
ConfirmMsgBoxProc(HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
197 return ConfirmMsgBox_Init(hDlg
, lParam
);
199 return ConfirmMsgBox_Paint(hDlg
);
201 EndDialog(hDlg
, wParam
);
204 EndDialog(hDlg
, IDCANCEL
);
210 int SHELL_ConfirmMsgBox(HWND hWnd
, LPWSTR lpszText
, LPWSTR lpszCaption
, HICON hIcon
, BOOL bYesToAll
)
212 struct confirm_msg_info info
;
214 info
.lpszText
= lpszText
;
215 info
.lpszCaption
= lpszCaption
;
217 info
.bYesToAll
= bYesToAll
;
218 return DialogBoxParamW(shell32_hInstance
, MAKEINTRESOURCEW(IDD_YESTOALL_MSGBOX
), hWnd
, ConfirmMsgBoxProc
, (LPARAM
)&info
);
221 /* confirmation dialogs content */
224 HINSTANCE hIconInstance
;
225 UINT icon_resource_id
;
226 UINT caption_resource_id
, text_resource_id
;
227 } SHELL_ConfirmIDstruc
;
229 static BOOL
SHELL_ConfirmIDs(int nKindOfDialog
, SHELL_ConfirmIDstruc
*ids
)
231 ids
->hIconInstance
= shell32_hInstance
;
232 switch (nKindOfDialog
)
234 case ASK_DELETE_FILE
:
235 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
236 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
237 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
240 case ASK_DELETE_FOLDER
:
241 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
242 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
243 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
246 case ASK_DELETE_MULTIPLE_ITEM
:
247 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
248 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
249 ids
->text_resource_id
= IDS_DELETEMULTIPLE_TEXT
;
253 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
254 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
255 ids
->text_resource_id
= IDS_TRASHITEM_TEXT
;
258 case ASK_TRASH_FOLDER
:
259 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
260 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
261 ids
->text_resource_id
= IDS_TRASHFOLDER_TEXT
;
264 case ASK_TRASH_MULTIPLE_ITEM
:
265 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
266 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
267 ids
->text_resource_id
= IDS_TRASHMULTIPLE_TEXT
;
270 case ASK_CANT_TRASH_ITEM
:
271 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
272 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
273 ids
->text_resource_id
= IDS_CANTTRASH_TEXT
;
276 case ASK_DELETE_SELECTED
:
277 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
278 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
279 ids
->text_resource_id
= IDS_DELETESELECTED_TEXT
;
282 case ASK_OVERWRITE_FILE
:
283 ids
->hIconInstance
= NULL
;
284 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
285 ids
->caption_resource_id
= IDS_OVERWRITEFILE_CAPTION
;
286 ids
->text_resource_id
= IDS_OVERWRITEFILE_TEXT
;
289 case ASK_OVERWRITE_FOLDER
:
290 ids
->hIconInstance
= NULL
;
291 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
292 ids
->caption_resource_id
= IDS_OVERWRITEFILE_CAPTION
;
293 ids
->text_resource_id
= IDS_OVERWRITEFOLDER_TEXT
;
297 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog
);
302 static BOOL
SHELL_ConfirmDialogW(HWND hWnd
, int nKindOfDialog
, LPCWSTR szDir
, FILE_OPERATION
*op
)
304 WCHAR szCaption
[255], szText
[255], szBuffer
[MAX_PATH
+ 256];
305 SHELL_ConfirmIDstruc ids
;
310 assert(nKindOfDialog
>= 0 && nKindOfDialog
< 32);
311 if (op
&& (op
->dwYesToAllMask
& (1 << nKindOfDialog
)))
314 if (!SHELL_ConfirmIDs(nKindOfDialog
, &ids
)) return FALSE
;
316 LoadStringW(shell32_hInstance
, ids
.caption_resource_id
, szCaption
, sizeof(szCaption
)/sizeof(WCHAR
));
317 LoadStringW(shell32_hInstance
, ids
.text_resource_id
, szText
, sizeof(szText
)/sizeof(WCHAR
));
319 args
[0] = (DWORD_PTR
)szDir
;
320 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
321 szText
, 0, 0, szBuffer
, sizeof(szBuffer
), (va_list*)args
);
322 hIcon
= LoadIconW(ids
.hIconInstance
, (LPWSTR
)MAKEINTRESOURCE(ids
.icon_resource_id
));
324 ret
= SHELL_ConfirmMsgBox(hWnd
, szBuffer
, szCaption
, hIcon
, op
&& op
->bManyItems
);
327 if (ret
== IDC_YESTOALL
)
329 op
->dwYesToAllMask
|= (1 << nKindOfDialog
);
333 op
->bCancelled
= TRUE
;
335 op
->req
->fAnyOperationsAborted
= TRUE
;
340 BOOL
SHELL_ConfirmYesNoW(HWND hWnd
, int nKindOfDialog
, LPCWSTR szDir
)
342 return SHELL_ConfirmDialogW(hWnd
, nKindOfDialog
, szDir
, NULL
);
345 static DWORD
SHELL32_AnsiToUnicodeBuf(LPCSTR aPath
, LPWSTR
*wPath
, DWORD minChars
)
347 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, NULL
, 0);
352 *wPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
355 MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, *wPath
, len
);
358 return E_OUTOFMEMORY
;
361 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath
)
363 HeapFree(GetProcessHeap(), 0, wPath
);
366 EXTERN_C HRESULT WINAPI
SHIsFileAvailableOffline(LPCWSTR path
, LPDWORD status
)
368 FIXME("(%s, %p) stub\n", debugstr_w(path
), status
);
372 /**************************************************************************
373 * SHELL_DeleteDirectory() [internal]
375 * Asks for confirmation when bShowUI is true and deletes the directory and
376 * all its subdirectories and files if necessary.
378 BOOL
SHELL_DeleteDirectoryW(HWND hwnd
, LPCWSTR pszDir
, BOOL bShowUI
)
382 WIN32_FIND_DATAW wfd
;
383 WCHAR szTemp
[MAX_PATH
];
385 /* Make sure the directory exists before eventually prompting the user */
386 PathCombineW(szTemp
, pszDir
, wWildcardFile
);
387 hFind
= FindFirstFileW(szTemp
, &wfd
);
388 if (hFind
== INVALID_HANDLE_VALUE
)
391 if (!bShowUI
|| (ret
= SHELL_ConfirmDialogW(hwnd
, ASK_DELETE_FOLDER
, pszDir
, NULL
)))
395 if (IsDotDir(wfd
.cFileName
))
397 PathCombineW(szTemp
, pszDir
, wfd
.cFileName
);
398 if (FILE_ATTRIBUTE_DIRECTORY
& wfd
.dwFileAttributes
)
399 ret
= SHELL_DeleteDirectoryW(hwnd
, szTemp
, FALSE
);
401 ret
= (SHNotifyDeleteFileW(szTemp
) == ERROR_SUCCESS
);
402 } while (ret
&& FindNextFileW(hFind
, &wfd
));
406 ret
= (SHNotifyRemoveDirectoryW(pszDir
) == ERROR_SUCCESS
);
410 /**************************************************************************
411 * Win32CreateDirectory [SHELL32.93]
413 * Creates a directory. Also triggers a change notify if one exists.
416 * path [I] path to directory to create
419 * TRUE if successful, FALSE otherwise
422 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
424 TRACE("(%s, %p)\n", debugstr_w(path
), sec
);
426 if (CreateDirectoryW(path
, sec
))
428 SHChangeNotify(SHCNE_MKDIR
, SHCNF_PATHW
, path
, NULL
);
429 return ERROR_SUCCESS
;
431 return GetLastError();
434 /**********************************************************************/
436 EXTERN_C BOOL WINAPI
Win32CreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
438 return (SHNotifyCreateDirectoryW(path
, sec
) == ERROR_SUCCESS
);
441 /************************************************************************
442 * Win32RemoveDirectory [SHELL32.94]
444 * Deletes a directory. Also triggers a change notify if one exists.
447 * path [I] path to directory to delete
450 * TRUE if successful, FALSE otherwise
452 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
)
455 TRACE("(%s)\n", debugstr_w(path
));
457 ret
= RemoveDirectoryW(path
);
460 /* Directory may be write protected */
461 DWORD dwAttr
= GetFileAttributesW(path
);
462 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
))
463 if (SetFileAttributesW(path
, dwAttr
& ~FILE_ATTRIBUTE_READONLY
))
464 ret
= RemoveDirectoryW(path
);
468 SHChangeNotify(SHCNE_RMDIR
, SHCNF_PATHW
, path
, NULL
);
469 return ERROR_SUCCESS
;
471 return GetLastError();
474 /***********************************************************************/
476 EXTERN_C BOOL WINAPI
Win32RemoveDirectoryW(LPCWSTR path
)
478 return (SHNotifyRemoveDirectoryW(path
) == ERROR_SUCCESS
);
481 /************************************************************************
482 * Win32DeleteFile [SHELL32.164]
484 * Deletes a file. Also triggers a change notify if one exists.
487 * path [I] path to file to delete
490 * TRUE if successful, FALSE otherwise
492 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
)
496 TRACE("(%s)\n", debugstr_w(path
));
498 ret
= DeleteFileW(path
);
501 /* File may be write protected or a system file */
502 DWORD dwAttr
= GetFileAttributesW(path
);
503 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
504 if (SetFileAttributesW(path
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
505 ret
= DeleteFileW(path
);
509 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, path
, NULL
);
510 return ERROR_SUCCESS
;
512 return GetLastError();
515 /***********************************************************************/
517 EXTERN_C DWORD WINAPI
Win32DeleteFileW(LPCWSTR path
)
519 return (SHNotifyDeleteFileW(path
) == ERROR_SUCCESS
);
522 /************************************************************************
523 * SHNotifyMoveFile [internal]
525 * Moves a file. Also triggers a change notify if one exists.
528 * src [I] path to source file to move
529 * dest [I] path to target file to move to
532 * ERORR_SUCCESS if successful
534 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
, BOOL isdir
)
538 TRACE("(%s %s)\n", debugstr_w(src
), debugstr_w(dest
));
540 ret
= MoveFileExW(src
, dest
, MOVEFILE_REPLACE_EXISTING
);
542 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
544 ret
= MoveFileW(src
, dest
);
550 dwAttr
= SHFindAttrW(dest
, FALSE
);
551 if (INVALID_FILE_ATTRIBUTES
== dwAttr
)
553 /* Source file may be write protected or a system file */
554 dwAttr
= GetFileAttributesW(src
);
555 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
556 if (SetFileAttributesW(src
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
557 ret
= MoveFileW(src
, dest
);
562 SHChangeNotify(isdir
? SHCNE_MKDIR
: SHCNE_CREATE
, SHCNF_PATHW
, dest
, NULL
);
563 SHChangeNotify(isdir
? SHCNE_RMDIR
: SHCNE_DELETE
, SHCNF_PATHW
, src
, NULL
);
564 return ERROR_SUCCESS
;
566 return GetLastError();
569 static DWORD WINAPI
SHOperationProgressRoutine(LARGE_INTEGER TotalFileSize
, LARGE_INTEGER TotalBytesTransferred
, LARGE_INTEGER StreamSize
, LARGE_INTEGER StreamBytesTransferred
, DWORD dwStreamNumber
, DWORD dwCallbackReason
, HANDLE hSourceFile
, HANDLE hDestinationFile
, LPVOID lpData
)
571 FILE_OPERATION_CONTEXT
* Context
;
572 LARGE_INTEGER Progress
;
575 Context
= (FILE_OPERATION_CONTEXT
*)lpData
;
577 if (TotalBytesTransferred
.QuadPart
)
579 Progress
.QuadPart
= (TotalBytesTransferred
.QuadPart
* 100) / TotalFileSize
.QuadPart
;
583 Progress
.QuadPart
= 1;
586 /* update progress bar */
587 SendMessageW(Context
->hDlgCtrl
, PBM_SETPOS
, (WPARAM
)Progress
.u
.LowPart
, 0);
589 if (TotalBytesTransferred
.QuadPart
== TotalFileSize
.QuadPart
)
591 /* file was copied */
593 PostMessageW(Context
->hwndDlg
, WM_FILE
, 0, 0);
596 return PROGRESS_CONTINUE
;
601 FILE_OPERATION_CONTEXT
* Context
)
603 FILE_ENTRY
* from
, *to
= NULL
;
606 if (Context
->Index
>= Context
->from
->dwNumFiles
)
609 /* get current file */
610 from
= &Context
->from
->feFiles
[Context
->Index
];
612 if (Context
->op
->req
->wFunc
!= FO_DELETE
)
613 to
= &Context
->to
->feFiles
[Context
->Index
];
616 SetDlgItemTextW(Context
->hwndDlg
, 14001, from
->szFullPath
);
618 if (Context
->op
->req
->wFunc
== FO_COPY
)
620 bRet
= CopyFileExW(from
->szFullPath
, to
->szFullPath
, SHOperationProgressRoutine
, (LPVOID
)Context
, &Context
->op
->bCancelled
, 0);
622 else if (Context
->op
->req
->wFunc
== FO_MOVE
)
624 //bRet = MoveFileWithProgressW(from->szFullPath, to->szFullPath, SHOperationProgressRoutine, (LPVOID)Context, MOVEFILE_COPY_ALLOWED);
626 else if (Context
->op
->req
->wFunc
== FO_DELETE
)
628 bRet
= DeleteFile(from
->szFullPath
);
634 static INT_PTR CALLBACK
SHOperationDialog(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
636 FILE_OPERATION_CONTEXT
* Context
;
638 Context
= (FILE_OPERATION_CONTEXT
*) GetWindowLongPtr(hwndDlg
, DWLP_USER
);
643 SetWindowLongPtr(hwndDlg
, DWLP_USER
, (LONG
)lParam
);
646 Context
= (FILE_OPERATION_CONTEXT
*)lParam
;
648 /* store progress bar handle */
649 Context
->hDlgCtrl
= GetDlgItem(hwndDlg
, 14000);
651 /* store window handle */
652 Context
->hwndDlg
= hwndDlg
;
654 /* set progress bar range */
655 (void)SendMessageW(Context
->hDlgCtrl
, (UINT
) PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
657 /* start file queueing */
658 SetTimer(hwndDlg
, TIMER_ID
, 1000, NULL
);
659 //QueueFile(Context);
664 Context
->op
->bCancelled
= TRUE
;
665 EndDialog(hwndDlg
, Context
->op
->bCancelled
);
669 if (LOWORD(wParam
) == 14002)
671 Context
->op
->bCancelled
= TRUE
;
672 EndDialog(hwndDlg
, Context
->op
->bCancelled
);
677 if (wParam
== TIMER_ID
)
680 KillTimer(hwndDlg
, TIMER_ID
);
684 if (!QueueFile(Context
))
685 EndDialog(hwndDlg
, Context
->op
->bCancelled
);
693 SHShowFileOperationDialog(FILE_OPERATION
*op
, FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
698 FILE_OPERATION_CONTEXT Context
;
700 Context
.from
= flFrom
;
704 Context
.op
->bCancelled
= FALSE
;
706 hwnd
= CreateDialogParam(shell32_hInstance
, MAKEINTRESOURCE(IDD_FILE_COPY
), NULL
, SHOperationDialog
, (LPARAM
)&Context
);
709 ERR("Failed to create dialog\n");
712 ShowWindow(hwnd
, SW_SHOWNORMAL
);
714 while ((bRet
= GetMessage(&msg
, NULL
, 0, 0)) != 0)
716 if (!IsWindow(hwnd
) || !IsDialogMessage(hwnd
, &msg
))
718 TranslateMessage(&msg
);
719 DispatchMessage(&msg
);
727 /************************************************************************
728 * SHNotifyCopyFile [internal]
730 * Copies a file. Also triggers a change notify if one exists.
733 * src [I] path to source file to move
734 * dest [I] path to target file to move to
735 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
736 * a file with this name already exists
739 * ERROR_SUCCESS if successful
741 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bFailIfExists
)
746 TRACE("(%s %s %s)\n", debugstr_w(src
), debugstr_w(dest
), bFailIfExists
? "failIfExists" : "");
748 /* Destination file may already exist with read only attribute */
749 attribs
= GetFileAttributesW(dest
);
750 if (IsAttrib(attribs
, FILE_ATTRIBUTE_READONLY
))
751 SetFileAttributesW(dest
, attribs
& ~FILE_ATTRIBUTE_READONLY
);
753 if (GetFileAttributesW(dest
) & FILE_ATTRIBUTE_READONLY
)
755 SetFileAttributesW(dest
, attribs
& ~FILE_ATTRIBUTE_READONLY
);
756 if (GetFileAttributesW(dest
) & FILE_ATTRIBUTE_READONLY
)
758 TRACE("[shell32, SHNotifyCopyFileW] STILL SHIT\n");
762 ret
= CopyFileW(src
, dest
, bFailIfExists
);
765 SHChangeNotify(SHCNE_CREATE
, SHCNF_PATHW
, dest
, NULL
);
766 return ERROR_SUCCESS
;
769 return GetLastError();
772 /*************************************************************************
773 * SHCreateDirectory [SHELL32.165]
775 * This function creates a file system folder whose fully qualified path is
776 * given by path. If one or more of the intermediate folders do not exist,
777 * they will be created as well.
781 * path [I] path of directory to create
784 * ERROR_SUCCESS or one of the following values:
785 * ERROR_BAD_PATHNAME if the path is relative
786 * ERROR_FILE_EXISTS when a file with that name exists
787 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
788 * ERROR_INVALID_NAME if the path contains invalid chars
789 * ERROR_ALREADY_EXISTS when the directory already exists
790 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
793 * exported by ordinal
795 * WinNT/2000 exports Unicode
797 int WINAPI
SHCreateDirectory(HWND hWnd
, LPCWSTR path
)
799 return SHCreateDirectoryExW(hWnd
, path
, NULL
);
802 /*************************************************************************
803 * SHCreateDirectoryExA [SHELL32.@]
805 * This function creates a file system folder whose fully qualified path is
806 * given by path. If one or more of the intermediate folders do not exist,
807 * they will be created as well.
811 * path [I] path of directory to create
812 * sec [I] security attributes to use or NULL
815 * ERROR_SUCCESS or one of the following values:
816 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
817 * ERROR_INVALID_NAME if the path contains invalid chars
818 * ERROR_FILE_EXISTS when a file with that name exists
819 * ERROR_ALREADY_EXISTS when the directory already exists
820 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
822 * FIXME: Not implemented yet;
823 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
824 * if the path is a network path to deal with network drivers which might have a limited
825 * but unknown maximum path length. If not:
827 * If hWnd is set to a valid window handle, a message box is displayed warning
828 * the user that the files may not be accessible. If the user chooses not to
829 * proceed, the function returns ERROR_CANCELLED.
831 * If hWnd is set to NULL, no user interface is displayed and the function
832 * returns ERROR_CANCELLED.
834 int WINAPI
SHCreateDirectoryExA(HWND hWnd
, LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
839 TRACE("(%s, %p)\n", debugstr_a(path
), sec
);
841 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
844 retCode
= SHCreateDirectoryExW(hWnd
, wPath
, sec
);
845 SHELL32_FreeUnicodeBuf(wPath
);
850 /*************************************************************************
851 * SHCreateDirectoryExW [SHELL32.@]
853 * See SHCreateDirectoryExA.
855 int WINAPI
SHCreateDirectoryExW(HWND hWnd
, LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
857 int ret
= ERROR_BAD_PATHNAME
;
858 TRACE("(%p, %s, %p)\n", hWnd
, debugstr_w(path
), sec
);
860 if (PathIsRelativeW(path
))
866 ret
= SHNotifyCreateDirectoryW(path
, sec
);
867 /* Refuse to work on certain error codes before trying to create directories recursively */
868 if (ret
!= ERROR_SUCCESS
&&
869 ret
!= ERROR_FILE_EXISTS
&&
870 ret
!= ERROR_ALREADY_EXISTS
&&
871 ret
!= ERROR_FILENAME_EXCED_RANGE
)
873 WCHAR
*pEnd
, *pSlash
, szTemp
[MAX_PATH
+ 1]; /* extra for PathAddBackslash() */
875 lstrcpynW(szTemp
, path
, MAX_PATH
);
876 pEnd
= PathAddBackslashW(szTemp
);
881 while (*pSlash
&& *pSlash
!= '\\') pSlash
++;
884 *pSlash
= 0; /* terminate path at separator */
886 ret
= SHNotifyCreateDirectoryW(szTemp
, pSlash
+ 1 == pEnd
? sec
: NULL
);
888 *pSlash
++ = '\\'; /* put the separator back */
892 if (ret
&& hWnd
&& (ERROR_CANCELLED
!= ret
))
894 /* We failed and should show a dialog box */
895 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path
), ret
);
896 ret
= ERROR_CANCELLED
; /* Error has been already presented to user (not really yet!) */
903 /*************************************************************************
904 * SHFindAttrW [internal]
906 * Get the Attributes for a file or directory. The difference to GetAttributes()
907 * is that this function will also work for paths containing wildcard characters
911 * path [I] path of directory or file to check
912 * fileOnly [I] TRUE if only files should be found
915 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
916 * the first file or directory found otherwise
918 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
)
920 WIN32_FIND_DATAW wfd
;
921 BOOL b_FileMask
= fileOnly
&& (NULL
!= StrPBrkW(pName
, wWildcardChars
));
922 DWORD dwAttr
= INVALID_FILE_ATTRIBUTES
;
923 HANDLE hFind
= FindFirstFileW(pName
, &wfd
);
925 TRACE("%s %d\n", debugstr_w(pName
), fileOnly
);
926 if (INVALID_HANDLE_VALUE
!= hFind
)
930 if (b_FileMask
&& IsAttribDir(wfd
.dwFileAttributes
))
932 dwAttr
= wfd
.dwFileAttributes
;
934 } while (FindNextFileW(hFind
, &wfd
));
941 /*************************************************************************
943 * _ConvertAtoW helper function for SHFileOperationA
945 * Converts a string or string-list to unicode.
947 static DWORD
_ConvertAtoW(PCSTR strSrc
, PCWSTR
* pStrDest
, BOOL isList
)
951 // If the input is null, nothing to convert.
955 // Measure the total size, depending on if it's a zero-terminated list.
959 PCSTR tmpSrc
= strSrc
;
963 size
= lstrlenA(tmpSrc
) + 1;
970 sizeA
= lstrlenA(strSrc
) + 1;
973 // Measure the needed allocation size.
974 int sizeW
= MultiByteToWideChar(CP_ACP
, 0, strSrc
, sizeA
, NULL
, 0);
976 return GetLastError();
978 PWSTR strDest
= (PWSTR
) HeapAlloc(GetProcessHeap(), 0, sizeW
* sizeof(WCHAR
));
980 return ERROR_OUTOFMEMORY
;
982 int err
= MultiByteToWideChar(CP_ACP
, 0, strSrc
, sizeA
, strDest
, sizeW
);
985 HeapFree(GetProcessHeap(), 0, strDest
);
986 return GetLastError();
993 /*************************************************************************
994 * SHFileOperationA [SHELL32.@]
996 * Function to copy, move, delete and create one or more files with optional
1000 * lpFileOp [I/O] pointer to a structure containing all the necessary information
1003 * Success: ERROR_SUCCESS.
1004 * Failure: ERROR_CANCELLED.
1009 int WINAPI
SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp
)
1011 int errCode
, retCode
;
1012 SHFILEOPSTRUCTW nFileOp
= { 0 };
1014 // Convert A information to W
1015 nFileOp
.hwnd
= lpFileOp
->hwnd
;
1016 nFileOp
.wFunc
= lpFileOp
->wFunc
;
1017 nFileOp
.fFlags
= lpFileOp
->fFlags
;
1019 errCode
= _ConvertAtoW(lpFileOp
->pFrom
, &nFileOp
.pFrom
, TRUE
);
1023 if (FO_DELETE
!= (nFileOp
.wFunc
& FO_MASK
))
1025 errCode
= _ConvertAtoW(lpFileOp
->pTo
, &nFileOp
.pTo
, TRUE
);
1030 if (nFileOp
.fFlags
& FOF_SIMPLEPROGRESS
)
1032 errCode
= _ConvertAtoW(lpFileOp
->lpszProgressTitle
, &nFileOp
.lpszProgressTitle
, FALSE
);
1037 // Call the actual function
1038 retCode
= SHFileOperationW(&nFileOp
);
1043 HeapFree(GetProcessHeap(), 0, (PVOID
) nFileOp
.pFrom
);
1045 HeapFree(GetProcessHeap(), 0, (PVOID
) nFileOp
.pTo
);
1046 if (nFileOp
.lpszProgressTitle
)
1047 HeapFree(GetProcessHeap(), 0, (PVOID
) nFileOp
.lpszProgressTitle
);
1051 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1052 SetLastError(errCode
);
1057 // Thankfully, starting with NT4 the name mappings are always unicode, so no need to convert.
1058 lpFileOp
->hNameMappings
= nFileOp
.hNameMappings
;
1059 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
1063 static void __inline
grow_list(FILE_LIST
*list
)
1065 FILE_ENTRY
*newx
= (FILE_ENTRY
*)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, list
->feFiles
,
1066 list
->num_alloc
* 2 * sizeof(*newx
) );
1067 list
->feFiles
= newx
;
1068 list
->num_alloc
*= 2;
1071 /* adds a file to the FILE_ENTRY struct
1073 static void add_file_to_entry(FILE_ENTRY
*feFile
, LPCWSTR szFile
)
1075 DWORD dwLen
= lstrlenW(szFile
) + 1;
1078 feFile
->szFullPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
1079 lstrcpyW(feFile
->szFullPath
, szFile
);
1081 ptr
= StrRChrW(szFile
, NULL
, '\\');
1084 dwLen
= ptr
- szFile
+ 1;
1085 feFile
->szDirectory
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
1086 lstrcpynW(feFile
->szDirectory
, szFile
, dwLen
);
1088 dwLen
= lstrlenW(feFile
->szFullPath
) - dwLen
+ 1;
1089 feFile
->szFilename
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
1090 lstrcpyW(feFile
->szFilename
, ptr
+ 1); /* skip over backslash */
1092 feFile
->bFromWildcard
= FALSE
;
1095 static LPWSTR
wildcard_to_file(LPCWSTR szWildCard
, LPCWSTR szFileName
)
1099 DWORD dwDirLen
, dwFullLen
;
1101 ptr
= StrRChrW(szWildCard
, NULL
, '\\');
1102 dwDirLen
= ptr
- szWildCard
+ 1;
1104 dwFullLen
= dwDirLen
+ lstrlenW(szFileName
) + 1;
1105 szFullPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwFullLen
* sizeof(WCHAR
));
1107 lstrcpynW(szFullPath
, szWildCard
, dwDirLen
+ 1);
1108 lstrcatW(szFullPath
, szFileName
);
1113 static void parse_wildcard_files(FILE_LIST
*flList
, LPCWSTR szFile
, LPDWORD pdwListIndex
)
1115 WIN32_FIND_DATAW wfd
;
1116 HANDLE hFile
= FindFirstFileW(szFile
, &wfd
);
1121 if (hFile
== INVALID_HANDLE_VALUE
) return;
1123 for (res
= TRUE
; res
; res
= FindNextFileW(hFile
, &wfd
))
1125 if (IsDotDir(wfd
.cFileName
))
1128 if (*pdwListIndex
>= flList
->num_alloc
)
1129 grow_list( flList
);
1131 szFullPath
= wildcard_to_file(szFile
, wfd
.cFileName
);
1132 file
= &flList
->feFiles
[(*pdwListIndex
)++];
1133 add_file_to_entry(file
, szFullPath
);
1134 file
->bFromWildcard
= TRUE
;
1135 file
->attributes
= wfd
.dwFileAttributes
;
1137 if (IsAttribDir(file
->attributes
))
1138 flList
->bAnyDirectories
= TRUE
;
1140 HeapFree(GetProcessHeap(), 0, szFullPath
);
1146 /* takes the null-separated file list and fills out the FILE_LIST */
1147 static HRESULT
parse_file_list(FILE_LIST
*flList
, LPCWSTR szFiles
)
1149 LPCWSTR ptr
= szFiles
;
1150 WCHAR szCurFile
[MAX_PATH
];
1154 return ERROR_INVALID_PARAMETER
;
1156 flList
->bAnyFromWildcard
= FALSE
;
1157 flList
->bAnyDirectories
= FALSE
;
1158 flList
->bAnyDontExist
= FALSE
;
1159 flList
->num_alloc
= 32;
1160 flList
->dwNumFiles
= 0;
1164 return ERROR_ACCESS_DENIED
;
1166 flList
->feFiles
= (FILE_ENTRY
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1167 flList
->num_alloc
* sizeof(FILE_ENTRY
));
1171 if (i
>= flList
->num_alloc
) grow_list( flList
);
1173 /* change relative to absolute path */
1174 if (PathIsRelativeW(ptr
))
1176 GetCurrentDirectoryW(MAX_PATH
, szCurFile
);
1177 PathCombineW(szCurFile
, szCurFile
, ptr
);
1178 flList
->feFiles
[i
].bFromRelative
= TRUE
;
1182 lstrcpyW(szCurFile
, ptr
);
1183 flList
->feFiles
[i
].bFromRelative
= FALSE
;
1186 /* parse wildcard files if they are in the filename */
1187 if (StrPBrkW(szCurFile
, wWildcardChars
))
1189 parse_wildcard_files(flList
, szCurFile
, &i
);
1190 flList
->bAnyFromWildcard
= TRUE
;
1195 FILE_ENTRY
*file
= &flList
->feFiles
[i
];
1196 add_file_to_entry(file
, szCurFile
);
1197 file
->attributes
= GetFileAttributesW( file
->szFullPath
);
1198 file
->bExists
= (file
->attributes
!= INVALID_FILE_ATTRIBUTES
);
1201 flList
->bAnyDontExist
= TRUE
;
1203 if (IsAttribDir(file
->attributes
))
1204 flList
->bAnyDirectories
= TRUE
;
1207 /* advance to the next string */
1208 ptr
+= lstrlenW(ptr
) + 1;
1211 flList
->dwNumFiles
= i
;
1216 /* free the FILE_LIST */
1217 static void destroy_file_list(FILE_LIST
*flList
)
1221 if (!flList
|| !flList
->feFiles
)
1224 for (i
= 0; i
< flList
->dwNumFiles
; i
++)
1226 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szDirectory
);
1227 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFilename
);
1228 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFullPath
);
1231 HeapFree(GetProcessHeap(), 0, flList
->feFiles
);
1234 static void copy_dir_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, LPCWSTR szDestPath
)
1236 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
1237 SHFILEOPSTRUCTW fileOp
;
1239 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1241 if (IsDotDir(feFrom
->szFilename
))
1244 if (PathFileExistsW(szDestPath
))
1245 PathCombineW(szTo
, szDestPath
, feFrom
->szFilename
);
1247 lstrcpyW(szTo
, szDestPath
);
1249 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
))
1251 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FOLDER
, feFrom
->szFilename
, op
))
1253 /* Vista returns an ERROR_CANCELLED even if user pressed "No" */
1254 if (!op
->bManyItems
)
1255 op
->bCancelled
= TRUE
;
1260 szTo
[lstrlenW(szTo
) + 1] = '\0';
1261 SHNotifyCreateDirectoryW(szTo
, NULL
);
1263 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1264 szFrom
[lstrlenW(szFrom
) + 1] = '\0';
1267 fileOp
.pFrom
= szFrom
;
1269 fileOp
.fFlags
&= ~FOF_MULTIDESTFILES
; /* we know we're copying to one dir */
1271 /* Don't ask the user about overwriting files when he accepted to overwrite the
1272 folder. FIXME: this is not exactly what Windows does - e.g. there would be
1273 an additional confirmation for a nested folder */
1274 fileOp
.fFlags
|= FOF_NOCONFIRMATION
;
1276 SHFileOperationW(&fileOp
);
1279 static BOOL
copy_file_to_file(FILE_OPERATION
*op
, const WCHAR
*szFrom
, const WCHAR
*szTo
)
1281 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
))
1283 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FILE
, PathFindFileNameW(szTo
), op
))
1287 return SHNotifyCopyFileW(szFrom
, szTo
, FALSE
) == 0;
1290 /* copy a file or directory to another directory */
1291 static void copy_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1293 if (!PathFileExistsW(feTo
->szFullPath
))
1294 SHNotifyCreateDirectoryW(feTo
->szFullPath
, NULL
);
1296 if (IsAttribFile(feFrom
->attributes
))
1298 WCHAR szDestPath
[MAX_PATH
];
1300 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1301 copy_file_to_file(op
, feFrom
->szFullPath
, szDestPath
);
1303 else if (!(op
->req
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1304 copy_dir_to_dir(op
, feFrom
, feTo
->szFullPath
);
1307 static void create_dest_dirs(LPCWSTR szDestDir
)
1309 WCHAR dir
[MAX_PATH
];
1310 LPCWSTR ptr
= StrChrW(szDestDir
, '\\');
1312 /* make sure all directories up to last one are created */
1313 while (ptr
&& (ptr
= StrChrW(ptr
+ 1, '\\')))
1315 lstrcpynW(dir
, szDestDir
, ptr
- szDestDir
+ 1);
1317 if (!PathFileExistsW(dir
))
1318 SHNotifyCreateDirectoryW(dir
, NULL
);
1321 /* create last directory */
1322 if (!PathFileExistsW(szDestDir
))
1323 SHNotifyCreateDirectoryW(szDestDir
, NULL
);
1326 /* the FO_COPY operation */
1327 static HRESULT
copy_files(FILE_OPERATION
*op
, const FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
1330 const FILE_ENTRY
*entryToCopy
;
1331 const FILE_ENTRY
*fileDest
= &flTo
->feFiles
[0];
1333 if (flFrom
->bAnyDontExist
)
1334 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1336 if (flTo
->dwNumFiles
== 0)
1338 /* If the destination is empty, SHFileOperation should use the current directory */
1339 WCHAR curdir
[MAX_PATH
+1];
1341 GetCurrentDirectoryW(MAX_PATH
, curdir
);
1342 curdir
[lstrlenW(curdir
)+1] = 0;
1344 destroy_file_list(flTo
);
1345 ZeroMemory(flTo
, sizeof(FILE_LIST
));
1346 parse_file_list(flTo
, curdir
);
1347 fileDest
= &flTo
->feFiles
[0];
1350 if (op
->req
->fFlags
& FOF_MULTIDESTFILES
)
1352 if (flFrom
->bAnyFromWildcard
)
1353 return ERROR_CANCELLED
;
1355 if (flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1357 if (flFrom
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1358 return ERROR_CANCELLED
;
1360 /* Free all but the first entry. */
1361 for (i
= 1; i
< flTo
->dwNumFiles
; i
++)
1363 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szDirectory
);
1364 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szFilename
);
1365 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szFullPath
);
1368 flTo
->dwNumFiles
= 1;
1370 else if (IsAttribDir(fileDest
->attributes
))
1372 for (i
= 1; i
< flTo
->dwNumFiles
; i
++)
1373 if (!IsAttribDir(flTo
->feFiles
[i
].attributes
) ||
1374 !IsAttribDir(flFrom
->feFiles
[i
].attributes
))
1376 return ERROR_CANCELLED
;
1380 else if (flFrom
->dwNumFiles
!= 1)
1382 if (flTo
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1383 return ERROR_CANCELLED
;
1385 if (PathFileExistsW(fileDest
->szFullPath
) &&
1386 IsAttribFile(fileDest
->attributes
))
1388 return ERROR_CANCELLED
;
1391 if (flTo
->dwNumFiles
== 1 && fileDest
->bFromRelative
&&
1392 !PathFileExistsW(fileDest
->szFullPath
))
1394 return ERROR_CANCELLED
;
1398 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1400 entryToCopy
= &flFrom
->feFiles
[i
];
1402 if ((op
->req
->fFlags
& FOF_MULTIDESTFILES
) &&
1403 flTo
->dwNumFiles
> 1)
1405 fileDest
= &flTo
->feFiles
[i
];
1408 if (IsAttribDir(entryToCopy
->attributes
) &&
1409 !lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szDirectory
))
1411 return ERROR_SUCCESS
;
1414 create_dest_dirs(fileDest
->szDirectory
);
1416 if (!lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1418 if (IsAttribFile(entryToCopy
->attributes
))
1419 return ERROR_NO_MORE_SEARCH_HANDLES
;
1421 return ERROR_SUCCESS
;
1424 if ((flFrom
->dwNumFiles
> 1 && flTo
->dwNumFiles
== 1) ||
1425 IsAttribDir(fileDest
->attributes
))
1427 copy_to_dir(op
, entryToCopy
, fileDest
);
1429 else if (IsAttribDir(entryToCopy
->attributes
))
1431 copy_dir_to_dir(op
, entryToCopy
, fileDest
->szFullPath
);
1435 if (!copy_file_to_file(op
, entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1437 op
->req
->fAnyOperationsAborted
= TRUE
;
1438 return ERROR_CANCELLED
;
1442 /* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
1444 return ERROR_CANCELLED
;
1447 /* Vista return code. On XP if the used pressed "No" for the last item,
1448 * ERROR_ARENA_TRASHED would be returned */
1449 return ERROR_SUCCESS
;
1452 static BOOL
confirm_delete_list(HWND hWnd
, DWORD fFlags
, BOOL fTrash
, const FILE_LIST
*flFrom
)
1454 if (flFrom
->dwNumFiles
> 1)
1457 const WCHAR format
[] = {'%','d',0};
1459 wnsprintfW(tmp
, sizeof(tmp
)/sizeof(tmp
[0]), format
, flFrom
->dwNumFiles
);
1460 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_MULTIPLE_ITEM
:ASK_DELETE_MULTIPLE_ITEM
), tmp
, NULL
);
1464 const FILE_ENTRY
*fileEntry
= &flFrom
->feFiles
[0];
1466 if (IsAttribFile(fileEntry
->attributes
))
1467 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FILE
:ASK_DELETE_FILE
), fileEntry
->szFullPath
, NULL
);
1468 else if (!(fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1469 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FOLDER
:ASK_DELETE_FOLDER
), fileEntry
->szFullPath
, NULL
);
1474 /* the FO_DELETE operation */
1475 static HRESULT
delete_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
)
1477 const FILE_ENTRY
*fileEntry
;
1482 if (!flFrom
->dwNumFiles
)
1483 return ERROR_SUCCESS
;
1485 /* Windows also checks only the first item */
1486 bTrash
= (lpFileOp
->fFlags
& FOF_ALLOWUNDO
)
1487 && TRASH_CanTrashFile(flFrom
->feFiles
[0].szFullPath
);
1489 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (!bTrash
&& lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1490 if (!confirm_delete_list(lpFileOp
->hwnd
, lpFileOp
->fFlags
, bTrash
, flFrom
))
1492 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1496 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1499 fileEntry
= &flFrom
->feFiles
[i
];
1501 if (!IsAttribFile(fileEntry
->attributes
) &&
1502 (lpFileOp
->fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1508 if (TRASH_TrashFile(fileEntry
->szFullPath
))
1510 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, fileEntry
->szFullPath
, NULL
);
1514 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1515 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1516 bDelete
= SHELL_ConfirmDialogW(lpFileOp
->hwnd
, ASK_CANT_TRASH_ITEM
, fileEntry
->szFullPath
, NULL
);
1522 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1527 /* delete the file or directory */
1528 if (IsAttribFile(fileEntry
->attributes
))
1530 bPathExists
= DeleteFileW(fileEntry
->szFullPath
);
1531 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, fileEntry
->szFullPath
, NULL
);
1534 bPathExists
= SHELL_DeleteDirectoryW(lpFileOp
->hwnd
, fileEntry
->szFullPath
, FALSE
);
1538 DWORD err
= GetLastError();
1540 if (ERROR_FILE_NOT_FOUND
== err
)
1542 // This is a windows 2003 server specific value which ahs been removed.
1543 // Later versions of windows return ERROR_FILE_NOT_FOUND.
1553 return ERROR_SUCCESS
;
1556 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_ENTRY
*feFrom
, LPCWSTR szDestPath
)
1558 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
1559 SHFILEOPSTRUCTW fileOp
;
1561 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1563 if (IsDotDir(feFrom
->szFilename
))
1566 SHNotifyCreateDirectoryW(szDestPath
, NULL
);
1568 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1569 szFrom
[lstrlenW(szFrom
) + 1] = '\0';
1571 lstrcpyW(szTo
, szDestPath
);
1572 szTo
[lstrlenW(szDestPath
) + 1] = '\0';
1575 fileOp
.pFrom
= szFrom
;
1578 SHFileOperationW(&fileOp
);
1581 /* moves a file or directory to another directory */
1582 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1584 WCHAR szDestPath
[MAX_PATH
];
1586 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1588 if (IsAttribFile(feFrom
->attributes
))
1589 SHNotifyMoveFileW(feFrom
->szFullPath
, szDestPath
, FALSE
);
1590 else if (!(lpFileOp
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1591 move_dir_to_dir(lpFileOp
, feFrom
, szDestPath
);
1594 /* the FO_MOVE operation */
1595 static HRESULT
move_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
, const FILE_LIST
*flTo
)
1598 const FILE_ENTRY
*entryToMove
;
1599 const FILE_ENTRY
*fileDest
;
1601 if (!flFrom
->dwNumFiles
|| !flTo
->dwNumFiles
)
1602 return ERROR_CANCELLED
;
1604 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1605 flTo
->dwNumFiles
> 1 && flFrom
->dwNumFiles
> 1)
1607 return ERROR_CANCELLED
;
1610 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1611 !flFrom
->bAnyDirectories
&&
1612 flFrom
->dwNumFiles
> flTo
->dwNumFiles
)
1614 return ERROR_CANCELLED
;
1617 if (!PathFileExistsW(flTo
->feFiles
[0].szDirectory
))
1618 return ERROR_CANCELLED
;
1620 if ((lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1621 flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1623 return ERROR_CANCELLED
;
1626 fileDest
= &flTo
->feFiles
[0];
1627 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1629 entryToMove
= &flFrom
->feFiles
[i
];
1631 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
)
1632 fileDest
= &flTo
->feFiles
[i
];
1634 if (!PathFileExistsW(fileDest
->szDirectory
))
1635 return ERROR_CANCELLED
;
1637 if (fileDest
->bExists
&& IsAttribDir(fileDest
->attributes
))
1638 move_to_dir(lpFileOp
, entryToMove
, fileDest
);
1640 SHNotifyMoveFileW(entryToMove
->szFullPath
, fileDest
->szFullPath
, IsAttribDir(entryToMove
->attributes
));
1643 return ERROR_SUCCESS
;
1646 /* the FO_RENAME files */
1647 static HRESULT
rename_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
, const FILE_LIST
*flTo
)
1649 const FILE_ENTRY
*feFrom
;
1650 const FILE_ENTRY
*feTo
;
1652 if (flFrom
->dwNumFiles
!= 1)
1653 return ERROR_GEN_FAILURE
;
1655 if (flTo
->dwNumFiles
!= 1)
1656 return ERROR_CANCELLED
;
1658 feFrom
= &flFrom
->feFiles
[0];
1659 feTo
= &flTo
->feFiles
[0];
1661 /* fail if destination doesn't exist */
1662 if (!feFrom
->bExists
)
1663 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1665 /* fail if destination already exists */
1667 return ERROR_ALREADY_EXISTS
;
1669 return SHNotifyMoveFileW(feFrom
->szFullPath
, feTo
->szFullPath
, IsAttribDir(feFrom
->attributes
));
1672 /* alert the user if an unsupported flag is used */
1673 static void check_flags(FILEOP_FLAGS fFlags
)
1675 WORD wUnsupportedFlags
= FOF_NO_CONNECTED_ELEMENTS
|
1676 FOF_NOCOPYSECURITYATTRIBS
| FOF_NORECURSEREPARSE
|
1677 FOF_RENAMEONCOLLISION
| FOF_WANTMAPPINGHANDLE
;
1679 if (fFlags
& wUnsupportedFlags
)
1680 FIXME("Unsupported flags: %04x\n", fFlags
);
1683 /*************************************************************************
1684 * SHFileOperationW [SHELL32.@]
1686 * See SHFileOperationA
1688 int WINAPI
SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp
)
1691 FILE_LIST flFrom
, flTo
;
1695 return ERROR_INVALID_PARAMETER
;
1697 check_flags(lpFileOp
->fFlags
);
1699 ZeroMemory(&flFrom
, sizeof(FILE_LIST
));
1700 ZeroMemory(&flTo
, sizeof(FILE_LIST
));
1702 if ((ret
= parse_file_list(&flFrom
, lpFileOp
->pFrom
)))
1705 if (lpFileOp
->wFunc
!= FO_DELETE
)
1706 parse_file_list(&flTo
, lpFileOp
->pTo
);
1708 ZeroMemory(&op
, sizeof(op
));
1710 op
.bManyItems
= (flFrom
.dwNumFiles
> 1);
1712 switch (lpFileOp
->wFunc
)
1715 ret
= copy_files(&op
, &flFrom
, &flTo
);
1718 ret
= delete_files(lpFileOp
, &flFrom
);
1721 ret
= move_files(lpFileOp
, &flFrom
, &flTo
);
1724 ret
= rename_files(lpFileOp
, &flFrom
, &flTo
);
1727 ret
= ERROR_INVALID_PARAMETER
;
1731 destroy_file_list(&flFrom
);
1733 if (lpFileOp
->wFunc
!= FO_DELETE
)
1734 destroy_file_list(&flTo
);
1736 if (ret
== ERROR_CANCELLED
)
1737 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1742 // Used by SHFreeNameMappings
1743 static int CALLBACK
_DestroyCallback(void *p
, void *pData
)
1745 LPSHNAMEMAPPINGW lp
= (SHNAMEMAPPINGW
*)p
;
1747 SHFree(lp
->pszOldPath
);
1748 SHFree(lp
->pszNewPath
);
1753 /*************************************************************************
1754 * SHFreeNameMappings [shell32.246]
1756 * Free the mapping handle returned by SHFileOperation if FOF_WANTSMAPPINGHANDLE
1760 * hNameMapping [I] handle to the name mappings used during renaming of files
1765 void WINAPI
SHFreeNameMappings(HANDLE hNameMapping
)
1769 DSA_DestroyCallback((HDSA
) hNameMapping
, _DestroyCallback
, NULL
);
1773 /*************************************************************************
1774 * SheGetDirA [SHELL32.@]
1776 * drive = 0: returns the current directory path
1777 * drive > 0: returns the current directory path of the specified drive
1778 * drive=1 -> A: drive=2 -> B: ...
1779 * returns 0 if successful
1781 EXTERN_C DWORD WINAPI
SheGetDirA(DWORD drive
, LPSTR buffer
)
1783 WCHAR org_path
[MAX_PATH
];
1787 /* change current directory to the specified drive */
1789 strcpy(drv_path
, "A:");
1790 drv_path
[0] += (char)drive
-1;
1792 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1794 SetCurrentDirectoryA(drv_path
);
1797 /* query current directory path of the specified drive */
1798 ret
= GetCurrentDirectoryA(MAX_PATH
, buffer
);
1800 /* back to the original drive */
1802 SetCurrentDirectoryW(org_path
);
1805 return GetLastError();
1810 /*************************************************************************
1811 * SheGetDirW [SHELL32.@]
1813 * drive = 0: returns the current directory path
1814 * drive > 0: returns the current directory path of the specified drive
1815 * drive=1 -> A: drive=2 -> B: ...
1816 * returns 0 if successful
1818 EXTERN_C DWORD WINAPI
SheGetDirW(DWORD drive
, LPWSTR buffer
)
1820 WCHAR org_path
[MAX_PATH
];
1824 /* change current directory to the specified drive */
1827 strcpy(drv_path
, "A:");
1828 drv_path
[0] += (char)drive
-1;
1830 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1832 SetCurrentDirectoryA(drv_path
);
1835 /* query current directory path of the specified drive */
1836 ret
= GetCurrentDirectoryW(MAX_PATH
, buffer
);
1838 /* back to the original drive */
1840 SetCurrentDirectoryW(org_path
);
1843 return GetLastError();
1848 /*************************************************************************
1849 * SheChangeDirA [SHELL32.@]
1851 * changes the current directory to the specified path
1852 * and returns 0 if successful
1854 EXTERN_C DWORD WINAPI
SheChangeDirA(LPSTR path
)
1856 if (SetCurrentDirectoryA(path
))
1859 return GetLastError();
1862 /*************************************************************************
1863 * SheChangeDirW [SHELL32.@]
1865 * changes the current directory to the specified path
1866 * and returns 0 if successful
1868 EXTERN_C DWORD WINAPI
SheChangeDirW(LPWSTR path
)
1870 if (SetCurrentDirectoryW(path
))
1873 return GetLastError();
1876 /*************************************************************************
1877 * IsNetDrive [SHELL32.66]
1879 EXTERN_C
int WINAPI
IsNetDrive(int drive
)
1882 strcpy(root
, "A:\\");
1883 root
[0] += (char)drive
;
1884 return (GetDriveTypeA(root
) == DRIVE_REMOTE
);
1888 /*************************************************************************
1889 * RealDriveType [SHELL32.524]
1891 EXTERN_C INT WINAPI
RealDriveType(INT drive
, BOOL bQueryNet
)
1893 char root
[] = "A:\\";
1894 root
[0] += (char)drive
;
1895 return GetDriveTypeA(root
);
1898 /***********************************************************************
1899 * SHPathPrepareForWriteW (SHELL32.@)
1901 EXTERN_C HRESULT WINAPI
SHPathPrepareForWriteW(HWND hwnd
, IUnknown
*modless
, LPCWSTR path
, DWORD flags
)
1908 WCHAR
* temppath
=NULL
;
1910 TRACE("%p %p %s 0x%80x\n", hwnd
, modless
, debugstr_w(path
), flags
);
1912 if (flags
& ~(SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
|SHPPFW_IGNOREFILENAME
))
1913 FIXME("unimplemented flags 0x%08x\n", flags
);
1915 /* cut off filename if necessary */
1916 if (flags
& SHPPFW_IGNOREFILENAME
)
1918 last_slash
= StrRChrW(path
, NULL
, '\\');
1919 if (last_slash
== NULL
)
1922 len
= last_slash
- path
+ 1;
1923 temppath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1925 return E_OUTOFMEMORY
;
1926 StrCpyNW(temppath
, path
, len
);
1927 realpath
= temppath
;
1934 /* try to create the directory if asked to */
1935 if (flags
& (SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
))
1937 if (flags
& SHPPFW_ASKDIRCREATE
)
1938 FIXME("treating SHPPFW_ASKDIRCREATE as SHPPFW_DIRCREATE\n");
1940 SHCreateDirectoryExW(0, realpath
, NULL
);
1943 /* check if we can access the directory */
1944 res
= GetFileAttributesW(realpath
);
1946 HeapFree(GetProcessHeap(), 0, temppath
);
1948 if (res
== INVALID_FILE_ATTRIBUTES
)
1950 err
= GetLastError();
1951 if (err
== ERROR_FILE_NOT_FOUND
)
1952 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
1953 return HRESULT_FROM_WIN32(err
);
1955 else if (res
& FILE_ATTRIBUTE_DIRECTORY
)
1958 return HRESULT_FROM_WIN32(ERROR_DIRECTORY
);
1961 /***********************************************************************
1962 * SHPathPrepareForWriteA (SHELL32.@)
1964 EXTERN_C HRESULT WINAPI
SHPathPrepareForWriteA(HWND hwnd
, IUnknown
*modless
, LPCSTR path
, DWORD flags
)
1966 WCHAR wpath
[MAX_PATH
];
1967 MultiByteToWideChar( CP_ACP
, 0, path
, -1, wpath
, MAX_PATH
);
1968 return SHPathPrepareForWriteW(hwnd
, modless
, wpath
, flags
);