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 * SHNameTranslate HelperFunction for SHFileOperationA
945 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
946 * is NULL, only the necessary size of the string is determined and returned,
947 * otherwise the ASCII strings are copied into it and the buffer is increased
948 * to point to the location after the final 0 termination char.
950 static DWORD
SHNameTranslate(LPWSTR
* wString
, LPCWSTR
* pWToFrom
, BOOL more
)
952 DWORD size
= 0, aSize
= 0;
953 LPCSTR aString
= (LPCSTR
)*pWToFrom
;
959 size
= lstrlenA(aString
) + 1;
962 } while ((size
!= 1) && more
);
964 /* The two sizes might be different in the case of multibyte chars */
965 size
= MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, 0);
966 if (*wString
) /* only in the second loop */
968 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, size
);
969 *pWToFrom
= *wString
;
975 /*************************************************************************
976 * SHFileOperationA [SHELL32.@]
978 * Function to copy, move, delete and create one or more files with optional
982 * lpFileOp [I/O] pointer to a structure containing all the necessary information
985 * Success: ERROR_SUCCESS.
986 * Failure: ERROR_CANCELLED.
991 int WINAPI
SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp
)
993 SHFILEOPSTRUCTW nFileOp
= *((LPSHFILEOPSTRUCTW
)lpFileOp
);
996 LPWSTR ForFree
= NULL
, /* we change wString in SHNameTranslate and can't use it for freeing */
997 wString
= NULL
; /* we change this in SHNameTranslate */
1000 if (FO_DELETE
== (nFileOp
.wFunc
& FO_MASK
))
1001 nFileOp
.pTo
= NULL
; /* we need a NULL or a valid pointer for translation */
1002 if (!(nFileOp
.fFlags
& FOF_SIMPLEPROGRESS
))
1003 nFileOp
.lpszProgressTitle
= NULL
; /* we need a NULL or a valid pointer for translation */
1004 while (1) /* every loop calculate size, second translate also, if we have storage for this */
1006 size
= SHNameTranslate(&wString
, &nFileOp
.lpszProgressTitle
, FALSE
); /* no loop */
1007 size
+= SHNameTranslate(&wString
, &nFileOp
.pFrom
, TRUE
); /* internal loop */
1008 size
+= SHNameTranslate(&wString
, &nFileOp
.pTo
, TRUE
); /* internal loop */
1012 retCode
= SHFileOperationW(&nFileOp
);
1013 HeapFree(GetProcessHeap(), 0, ForFree
); /* we cannot use wString, it was changed */
1018 wString
= ForFree
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
1019 if (ForFree
) continue;
1020 retCode
= ERROR_OUTOFMEMORY
;
1021 nFileOp
.fAnyOperationsAborted
= TRUE
;
1022 SetLastError(retCode
);
1027 lpFileOp
->hNameMappings
= nFileOp
.hNameMappings
;
1028 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
1032 static void __inline
grow_list(FILE_LIST
*list
)
1034 FILE_ENTRY
*newx
= (FILE_ENTRY
*)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, list
->feFiles
,
1035 list
->num_alloc
* 2 * sizeof(*newx
) );
1036 list
->feFiles
= newx
;
1037 list
->num_alloc
*= 2;
1040 /* adds a file to the FILE_ENTRY struct
1042 static void add_file_to_entry(FILE_ENTRY
*feFile
, LPCWSTR szFile
)
1044 DWORD dwLen
= lstrlenW(szFile
) + 1;
1047 feFile
->szFullPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
1048 lstrcpyW(feFile
->szFullPath
, szFile
);
1050 ptr
= StrRChrW(szFile
, NULL
, '\\');
1053 dwLen
= ptr
- szFile
+ 1;
1054 feFile
->szDirectory
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
1055 lstrcpynW(feFile
->szDirectory
, szFile
, dwLen
);
1057 dwLen
= lstrlenW(feFile
->szFullPath
) - dwLen
+ 1;
1058 feFile
->szFilename
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
1059 lstrcpyW(feFile
->szFilename
, ptr
+ 1); /* skip over backslash */
1061 feFile
->bFromWildcard
= FALSE
;
1064 static LPWSTR
wildcard_to_file(LPCWSTR szWildCard
, LPCWSTR szFileName
)
1068 DWORD dwDirLen
, dwFullLen
;
1070 ptr
= StrRChrW(szWildCard
, NULL
, '\\');
1071 dwDirLen
= ptr
- szWildCard
+ 1;
1073 dwFullLen
= dwDirLen
+ lstrlenW(szFileName
) + 1;
1074 szFullPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwFullLen
* sizeof(WCHAR
));
1076 lstrcpynW(szFullPath
, szWildCard
, dwDirLen
+ 1);
1077 lstrcatW(szFullPath
, szFileName
);
1082 static void parse_wildcard_files(FILE_LIST
*flList
, LPCWSTR szFile
, LPDWORD pdwListIndex
)
1084 WIN32_FIND_DATAW wfd
;
1085 HANDLE hFile
= FindFirstFileW(szFile
, &wfd
);
1090 if (hFile
== INVALID_HANDLE_VALUE
) return;
1092 for (res
= TRUE
; res
; res
= FindNextFileW(hFile
, &wfd
))
1094 if (IsDotDir(wfd
.cFileName
))
1097 if (*pdwListIndex
>= flList
->num_alloc
)
1098 grow_list( flList
);
1100 szFullPath
= wildcard_to_file(szFile
, wfd
.cFileName
);
1101 file
= &flList
->feFiles
[(*pdwListIndex
)++];
1102 add_file_to_entry(file
, szFullPath
);
1103 file
->bFromWildcard
= TRUE
;
1104 file
->attributes
= wfd
.dwFileAttributes
;
1106 if (IsAttribDir(file
->attributes
))
1107 flList
->bAnyDirectories
= TRUE
;
1109 HeapFree(GetProcessHeap(), 0, szFullPath
);
1115 /* takes the null-separated file list and fills out the FILE_LIST */
1116 static HRESULT
parse_file_list(FILE_LIST
*flList
, LPCWSTR szFiles
)
1118 LPCWSTR ptr
= szFiles
;
1119 WCHAR szCurFile
[MAX_PATH
];
1123 return ERROR_INVALID_PARAMETER
;
1125 flList
->bAnyFromWildcard
= FALSE
;
1126 flList
->bAnyDirectories
= FALSE
;
1127 flList
->bAnyDontExist
= FALSE
;
1128 flList
->num_alloc
= 32;
1129 flList
->dwNumFiles
= 0;
1133 return ERROR_ACCESS_DENIED
;
1135 flList
->feFiles
= (FILE_ENTRY
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1136 flList
->num_alloc
* sizeof(FILE_ENTRY
));
1140 if (i
>= flList
->num_alloc
) grow_list( flList
);
1142 /* change relative to absolute path */
1143 if (PathIsRelativeW(ptr
))
1145 GetCurrentDirectoryW(MAX_PATH
, szCurFile
);
1146 PathCombineW(szCurFile
, szCurFile
, ptr
);
1147 flList
->feFiles
[i
].bFromRelative
= TRUE
;
1151 lstrcpyW(szCurFile
, ptr
);
1152 flList
->feFiles
[i
].bFromRelative
= FALSE
;
1155 /* parse wildcard files if they are in the filename */
1156 if (StrPBrkW(szCurFile
, wWildcardChars
))
1158 parse_wildcard_files(flList
, szCurFile
, &i
);
1159 flList
->bAnyFromWildcard
= TRUE
;
1164 FILE_ENTRY
*file
= &flList
->feFiles
[i
];
1165 add_file_to_entry(file
, szCurFile
);
1166 file
->attributes
= GetFileAttributesW( file
->szFullPath
);
1167 file
->bExists
= (file
->attributes
!= INVALID_FILE_ATTRIBUTES
);
1170 flList
->bAnyDontExist
= TRUE
;
1172 if (IsAttribDir(file
->attributes
))
1173 flList
->bAnyDirectories
= TRUE
;
1176 /* advance to the next string */
1177 ptr
+= lstrlenW(ptr
) + 1;
1180 flList
->dwNumFiles
= i
;
1185 /* free the FILE_LIST */
1186 static void destroy_file_list(FILE_LIST
*flList
)
1190 if (!flList
|| !flList
->feFiles
)
1193 for (i
= 0; i
< flList
->dwNumFiles
; i
++)
1195 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szDirectory
);
1196 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFilename
);
1197 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFullPath
);
1200 HeapFree(GetProcessHeap(), 0, flList
->feFiles
);
1203 static void copy_dir_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, LPCWSTR szDestPath
)
1205 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
1206 SHFILEOPSTRUCTW fileOp
;
1208 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1210 if (IsDotDir(feFrom
->szFilename
))
1213 if (PathFileExistsW(szDestPath
))
1214 PathCombineW(szTo
, szDestPath
, feFrom
->szFilename
);
1216 lstrcpyW(szTo
, szDestPath
);
1218 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
))
1220 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FOLDER
, feFrom
->szFilename
, op
))
1222 /* Vista returns an ERROR_CANCELLED even if user pressed "No" */
1223 if (!op
->bManyItems
)
1224 op
->bCancelled
= TRUE
;
1229 szTo
[lstrlenW(szTo
) + 1] = '\0';
1230 SHNotifyCreateDirectoryW(szTo
, NULL
);
1232 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1233 szFrom
[lstrlenW(szFrom
) + 1] = '\0';
1236 fileOp
.pFrom
= szFrom
;
1238 fileOp
.fFlags
&= ~FOF_MULTIDESTFILES
; /* we know we're copying to one dir */
1240 /* Don't ask the user about overwriting files when he accepted to overwrite the
1241 folder. FIXME: this is not exactly what Windows does - e.g. there would be
1242 an additional confirmation for a nested folder */
1243 fileOp
.fFlags
|= FOF_NOCONFIRMATION
;
1245 SHFileOperationW(&fileOp
);
1248 static BOOL
copy_file_to_file(FILE_OPERATION
*op
, const WCHAR
*szFrom
, const WCHAR
*szTo
)
1250 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
))
1252 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FILE
, PathFindFileNameW(szTo
), op
))
1256 return SHNotifyCopyFileW(szFrom
, szTo
, FALSE
) == 0;
1259 /* copy a file or directory to another directory */
1260 static void copy_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1262 if (!PathFileExistsW(feTo
->szFullPath
))
1263 SHNotifyCreateDirectoryW(feTo
->szFullPath
, NULL
);
1265 if (IsAttribFile(feFrom
->attributes
))
1267 WCHAR szDestPath
[MAX_PATH
];
1269 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1270 copy_file_to_file(op
, feFrom
->szFullPath
, szDestPath
);
1272 else if (!(op
->req
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1273 copy_dir_to_dir(op
, feFrom
, feTo
->szFullPath
);
1276 static void create_dest_dirs(LPCWSTR szDestDir
)
1278 WCHAR dir
[MAX_PATH
];
1279 LPCWSTR ptr
= StrChrW(szDestDir
, '\\');
1281 /* make sure all directories up to last one are created */
1282 while (ptr
&& (ptr
= StrChrW(ptr
+ 1, '\\')))
1284 lstrcpynW(dir
, szDestDir
, ptr
- szDestDir
+ 1);
1286 if (!PathFileExistsW(dir
))
1287 SHNotifyCreateDirectoryW(dir
, NULL
);
1290 /* create last directory */
1291 if (!PathFileExistsW(szDestDir
))
1292 SHNotifyCreateDirectoryW(szDestDir
, NULL
);
1295 /* the FO_COPY operation */
1296 static HRESULT
copy_files(FILE_OPERATION
*op
, const FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
1299 const FILE_ENTRY
*entryToCopy
;
1300 const FILE_ENTRY
*fileDest
= &flTo
->feFiles
[0];
1302 if (flFrom
->bAnyDontExist
)
1303 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1305 if (flTo
->dwNumFiles
== 0)
1307 /* If the destination is empty, SHFileOperation should use the current directory */
1308 WCHAR curdir
[MAX_PATH
+1];
1310 GetCurrentDirectoryW(MAX_PATH
, curdir
);
1311 curdir
[lstrlenW(curdir
)+1] = 0;
1313 destroy_file_list(flTo
);
1314 ZeroMemory(flTo
, sizeof(FILE_LIST
));
1315 parse_file_list(flTo
, curdir
);
1316 fileDest
= &flTo
->feFiles
[0];
1319 if (op
->req
->fFlags
& FOF_MULTIDESTFILES
)
1321 if (flFrom
->bAnyFromWildcard
)
1322 return ERROR_CANCELLED
;
1324 if (flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1326 if (flFrom
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1327 return ERROR_CANCELLED
;
1329 /* Free all but the first entry. */
1330 for (i
= 1; i
< flTo
->dwNumFiles
; i
++)
1332 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szDirectory
);
1333 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szFilename
);
1334 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szFullPath
);
1337 flTo
->dwNumFiles
= 1;
1339 else if (IsAttribDir(fileDest
->attributes
))
1341 for (i
= 1; i
< flTo
->dwNumFiles
; i
++)
1342 if (!IsAttribDir(flTo
->feFiles
[i
].attributes
) ||
1343 !IsAttribDir(flFrom
->feFiles
[i
].attributes
))
1345 return ERROR_CANCELLED
;
1349 else if (flFrom
->dwNumFiles
!= 1)
1351 if (flTo
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1352 return ERROR_CANCELLED
;
1354 if (PathFileExistsW(fileDest
->szFullPath
) &&
1355 IsAttribFile(fileDest
->attributes
))
1357 return ERROR_CANCELLED
;
1360 if (flTo
->dwNumFiles
== 1 && fileDest
->bFromRelative
&&
1361 !PathFileExistsW(fileDest
->szFullPath
))
1363 return ERROR_CANCELLED
;
1367 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1369 entryToCopy
= &flFrom
->feFiles
[i
];
1371 if ((op
->req
->fFlags
& FOF_MULTIDESTFILES
) &&
1372 flTo
->dwNumFiles
> 1)
1374 fileDest
= &flTo
->feFiles
[i
];
1377 if (IsAttribDir(entryToCopy
->attributes
) &&
1378 !lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szDirectory
))
1380 return ERROR_SUCCESS
;
1383 create_dest_dirs(fileDest
->szDirectory
);
1385 if (!lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1387 if (IsAttribFile(entryToCopy
->attributes
))
1388 return ERROR_NO_MORE_SEARCH_HANDLES
;
1390 return ERROR_SUCCESS
;
1393 if ((flFrom
->dwNumFiles
> 1 && flTo
->dwNumFiles
== 1) ||
1394 IsAttribDir(fileDest
->attributes
))
1396 copy_to_dir(op
, entryToCopy
, fileDest
);
1398 else if (IsAttribDir(entryToCopy
->attributes
))
1400 copy_dir_to_dir(op
, entryToCopy
, fileDest
->szFullPath
);
1404 if (!copy_file_to_file(op
, entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1406 op
->req
->fAnyOperationsAborted
= TRUE
;
1407 return ERROR_CANCELLED
;
1411 /* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
1413 return ERROR_CANCELLED
;
1416 /* Vista return code. On XP if the used pressed "No" for the last item,
1417 * ERROR_ARENA_TRASHED would be returned */
1418 return ERROR_SUCCESS
;
1421 static BOOL
confirm_delete_list(HWND hWnd
, DWORD fFlags
, BOOL fTrash
, const FILE_LIST
*flFrom
)
1423 if (flFrom
->dwNumFiles
> 1)
1426 const WCHAR format
[] = {'%','d',0};
1428 wnsprintfW(tmp
, sizeof(tmp
)/sizeof(tmp
[0]), format
, flFrom
->dwNumFiles
);
1429 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_MULTIPLE_ITEM
:ASK_DELETE_MULTIPLE_ITEM
), tmp
, NULL
);
1433 const FILE_ENTRY
*fileEntry
= &flFrom
->feFiles
[0];
1435 if (IsAttribFile(fileEntry
->attributes
))
1436 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FILE
:ASK_DELETE_FILE
), fileEntry
->szFullPath
, NULL
);
1437 else if (!(fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1438 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FOLDER
:ASK_DELETE_FOLDER
), fileEntry
->szFullPath
, NULL
);
1443 /* the FO_DELETE operation */
1444 static HRESULT
delete_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
)
1446 const FILE_ENTRY
*fileEntry
;
1451 if (!flFrom
->dwNumFiles
)
1452 return ERROR_SUCCESS
;
1454 /* Windows also checks only the first item */
1455 bTrash
= (lpFileOp
->fFlags
& FOF_ALLOWUNDO
)
1456 && TRASH_CanTrashFile(flFrom
->feFiles
[0].szFullPath
);
1458 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (!bTrash
&& lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1459 if (!confirm_delete_list(lpFileOp
->hwnd
, lpFileOp
->fFlags
, bTrash
, flFrom
))
1461 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1465 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1468 fileEntry
= &flFrom
->feFiles
[i
];
1470 if (!IsAttribFile(fileEntry
->attributes
) &&
1471 (lpFileOp
->fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1477 if (TRASH_TrashFile(fileEntry
->szFullPath
))
1479 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, fileEntry
->szFullPath
, NULL
);
1483 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1484 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1485 bDelete
= SHELL_ConfirmDialogW(lpFileOp
->hwnd
, ASK_CANT_TRASH_ITEM
, fileEntry
->szFullPath
, NULL
);
1491 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1496 /* delete the file or directory */
1497 if (IsAttribFile(fileEntry
->attributes
))
1499 bPathExists
= DeleteFileW(fileEntry
->szFullPath
);
1500 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, fileEntry
->szFullPath
, NULL
);
1503 bPathExists
= SHELL_DeleteDirectoryW(lpFileOp
->hwnd
, fileEntry
->szFullPath
, FALSE
);
1507 DWORD err
= GetLastError();
1509 if (ERROR_FILE_NOT_FOUND
== err
)
1511 // This is a windows 2003 server specific value which ahs been removed.
1512 // Later versions of windows return ERROR_FILE_NOT_FOUND.
1522 return ERROR_SUCCESS
;
1525 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_ENTRY
*feFrom
, LPCWSTR szDestPath
)
1527 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
1528 SHFILEOPSTRUCTW fileOp
;
1530 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1532 if (IsDotDir(feFrom
->szFilename
))
1535 SHNotifyCreateDirectoryW(szDestPath
, NULL
);
1537 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1538 szFrom
[lstrlenW(szFrom
) + 1] = '\0';
1540 lstrcpyW(szTo
, szDestPath
);
1541 szTo
[lstrlenW(szDestPath
) + 1] = '\0';
1544 fileOp
.pFrom
= szFrom
;
1547 SHFileOperationW(&fileOp
);
1550 /* moves a file or directory to another directory */
1551 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1553 WCHAR szDestPath
[MAX_PATH
];
1555 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1557 if (IsAttribFile(feFrom
->attributes
))
1558 SHNotifyMoveFileW(feFrom
->szFullPath
, szDestPath
, FALSE
);
1559 else if (!(lpFileOp
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1560 move_dir_to_dir(lpFileOp
, feFrom
, szDestPath
);
1563 /* the FO_MOVE operation */
1564 static HRESULT
move_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
, const FILE_LIST
*flTo
)
1567 const FILE_ENTRY
*entryToMove
;
1568 const FILE_ENTRY
*fileDest
;
1570 if (!flFrom
->dwNumFiles
|| !flTo
->dwNumFiles
)
1571 return ERROR_CANCELLED
;
1573 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1574 flTo
->dwNumFiles
> 1 && flFrom
->dwNumFiles
> 1)
1576 return ERROR_CANCELLED
;
1579 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1580 !flFrom
->bAnyDirectories
&&
1581 flFrom
->dwNumFiles
> flTo
->dwNumFiles
)
1583 return ERROR_CANCELLED
;
1586 if (!PathFileExistsW(flTo
->feFiles
[0].szDirectory
))
1587 return ERROR_CANCELLED
;
1589 if ((lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1590 flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1592 return ERROR_CANCELLED
;
1595 fileDest
= &flTo
->feFiles
[0];
1596 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1598 entryToMove
= &flFrom
->feFiles
[i
];
1600 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
)
1601 fileDest
= &flTo
->feFiles
[i
];
1603 if (!PathFileExistsW(fileDest
->szDirectory
))
1604 return ERROR_CANCELLED
;
1606 if (fileDest
->bExists
&& IsAttribDir(fileDest
->attributes
))
1607 move_to_dir(lpFileOp
, entryToMove
, fileDest
);
1609 SHNotifyMoveFileW(entryToMove
->szFullPath
, fileDest
->szFullPath
, IsAttribDir(entryToMove
->attributes
));
1612 return ERROR_SUCCESS
;
1615 /* the FO_RENAME files */
1616 static HRESULT
rename_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
, const FILE_LIST
*flTo
)
1618 const FILE_ENTRY
*feFrom
;
1619 const FILE_ENTRY
*feTo
;
1621 if (flFrom
->dwNumFiles
!= 1)
1622 return ERROR_GEN_FAILURE
;
1624 if (flTo
->dwNumFiles
!= 1)
1625 return ERROR_CANCELLED
;
1627 feFrom
= &flFrom
->feFiles
[0];
1628 feTo
= &flTo
->feFiles
[0];
1630 /* fail if destination doesn't exist */
1631 if (!feFrom
->bExists
)
1632 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1634 /* fail if destination already exists */
1636 return ERROR_ALREADY_EXISTS
;
1638 return SHNotifyMoveFileW(feFrom
->szFullPath
, feTo
->szFullPath
, IsAttribDir(feFrom
->attributes
));
1641 /* alert the user if an unsupported flag is used */
1642 static void check_flags(FILEOP_FLAGS fFlags
)
1644 WORD wUnsupportedFlags
= FOF_NO_CONNECTED_ELEMENTS
|
1645 FOF_NOCOPYSECURITYATTRIBS
| FOF_NORECURSEREPARSE
|
1646 FOF_RENAMEONCOLLISION
| FOF_WANTMAPPINGHANDLE
;
1648 if (fFlags
& wUnsupportedFlags
)
1649 FIXME("Unsupported flags: %04x\n", fFlags
);
1652 /*************************************************************************
1653 * SHFileOperationW [SHELL32.@]
1655 * See SHFileOperationA
1657 int WINAPI
SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp
)
1660 FILE_LIST flFrom
, flTo
;
1664 return ERROR_INVALID_PARAMETER
;
1666 check_flags(lpFileOp
->fFlags
);
1668 ZeroMemory(&flFrom
, sizeof(FILE_LIST
));
1669 ZeroMemory(&flTo
, sizeof(FILE_LIST
));
1671 if ((ret
= parse_file_list(&flFrom
, lpFileOp
->pFrom
)))
1674 if (lpFileOp
->wFunc
!= FO_DELETE
)
1675 parse_file_list(&flTo
, lpFileOp
->pTo
);
1677 ZeroMemory(&op
, sizeof(op
));
1679 op
.bManyItems
= (flFrom
.dwNumFiles
> 1);
1681 switch (lpFileOp
->wFunc
)
1684 ret
= copy_files(&op
, &flFrom
, &flTo
);
1687 ret
= delete_files(lpFileOp
, &flFrom
);
1690 ret
= move_files(lpFileOp
, &flFrom
, &flTo
);
1693 ret
= rename_files(lpFileOp
, &flFrom
, &flTo
);
1696 ret
= ERROR_INVALID_PARAMETER
;
1700 destroy_file_list(&flFrom
);
1702 if (lpFileOp
->wFunc
!= FO_DELETE
)
1703 destroy_file_list(&flTo
);
1705 if (ret
== ERROR_CANCELLED
)
1706 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1711 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1713 /*************************************************************************
1714 * SHFreeNameMappings [shell32.246]
1716 * Free the mapping handle returned by SHFileOperation if FOF_WANTSMAPPINGHANDLE
1720 * hNameMapping [I] handle to the name mappings used during renaming of files
1725 void WINAPI
SHFreeNameMappings(HANDLE hNameMapping
)
1729 int i
= SHDSA_GetItemCount((HDSA
)hNameMapping
) - 1;
1733 LPSHNAMEMAPPINGW lp
= (SHNAMEMAPPINGW
*)DSA_GetItemPtr((HDSA
)hNameMapping
, i
);
1735 SHFree(lp
->pszOldPath
);
1736 SHFree(lp
->pszNewPath
);
1738 DSA_Destroy((HDSA
)hNameMapping
);
1742 /*************************************************************************
1743 * SheGetDirA [SHELL32.@]
1745 * drive = 0: returns the current directory path
1746 * drive > 0: returns the current directory path of the specified drive
1747 * drive=1 -> A: drive=2 -> B: ...
1748 * returns 0 if successful
1750 EXTERN_C DWORD WINAPI
SheGetDirA(DWORD drive
, LPSTR buffer
)
1752 WCHAR org_path
[MAX_PATH
];
1756 /* change current directory to the specified drive */
1758 strcpy(drv_path
, "A:");
1759 drv_path
[0] += (char)drive
-1;
1761 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1763 SetCurrentDirectoryA(drv_path
);
1766 /* query current directory path of the specified drive */
1767 ret
= GetCurrentDirectoryA(MAX_PATH
, buffer
);
1769 /* back to the original drive */
1771 SetCurrentDirectoryW(org_path
);
1774 return GetLastError();
1779 /*************************************************************************
1780 * SheGetDirW [SHELL32.@]
1782 * drive = 0: returns the current directory path
1783 * drive > 0: returns the current directory path of the specified drive
1784 * drive=1 -> A: drive=2 -> B: ...
1785 * returns 0 if successful
1787 EXTERN_C DWORD WINAPI
SheGetDirW(DWORD drive
, LPWSTR buffer
)
1789 WCHAR org_path
[MAX_PATH
];
1793 /* change current directory to the specified drive */
1796 strcpy(drv_path
, "A:");
1797 drv_path
[0] += (char)drive
-1;
1799 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1801 SetCurrentDirectoryA(drv_path
);
1804 /* query current directory path of the specified drive */
1805 ret
= GetCurrentDirectoryW(MAX_PATH
, buffer
);
1807 /* back to the original drive */
1809 SetCurrentDirectoryW(org_path
);
1812 return GetLastError();
1817 /*************************************************************************
1818 * SheChangeDirA [SHELL32.@]
1820 * changes the current directory to the specified path
1821 * and returns 0 if successful
1823 EXTERN_C DWORD WINAPI
SheChangeDirA(LPSTR path
)
1825 if (SetCurrentDirectoryA(path
))
1828 return GetLastError();
1831 /*************************************************************************
1832 * SheChangeDirW [SHELL32.@]
1834 * changes the current directory to the specified path
1835 * and returns 0 if successful
1837 EXTERN_C DWORD WINAPI
SheChangeDirW(LPWSTR path
)
1839 if (SetCurrentDirectoryW(path
))
1842 return GetLastError();
1845 /*************************************************************************
1846 * IsNetDrive [SHELL32.66]
1848 EXTERN_C
int WINAPI
IsNetDrive(int drive
)
1851 strcpy(root
, "A:\\");
1852 root
[0] += (char)drive
;
1853 return (GetDriveTypeA(root
) == DRIVE_REMOTE
);
1857 /*************************************************************************
1858 * RealDriveType [SHELL32.524]
1860 EXTERN_C INT WINAPI
RealDriveType(INT drive
, BOOL bQueryNet
)
1862 char root
[] = "A:\\";
1863 root
[0] += (char)drive
;
1864 return GetDriveTypeA(root
);
1867 /***********************************************************************
1868 * SHPathPrepareForWriteW (SHELL32.@)
1870 EXTERN_C HRESULT WINAPI
SHPathPrepareForWriteW(HWND hwnd
, IUnknown
*modless
, LPCWSTR path
, DWORD flags
)
1877 WCHAR
* temppath
=NULL
;
1879 TRACE("%p %p %s 0x%80x\n", hwnd
, modless
, debugstr_w(path
), flags
);
1881 if (flags
& ~(SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
|SHPPFW_IGNOREFILENAME
))
1882 FIXME("unimplemented flags 0x%08x\n", flags
);
1884 /* cut off filename if necessary */
1885 if (flags
& SHPPFW_IGNOREFILENAME
)
1887 last_slash
= StrRChrW(path
, NULL
, '\\');
1888 if (last_slash
== NULL
)
1891 len
= last_slash
- path
+ 1;
1892 temppath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1894 return E_OUTOFMEMORY
;
1895 StrCpyNW(temppath
, path
, len
);
1896 realpath
= temppath
;
1903 /* try to create the directory if asked to */
1904 if (flags
& (SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
))
1906 if (flags
& SHPPFW_ASKDIRCREATE
)
1907 FIXME("treating SHPPFW_ASKDIRCREATE as SHPPFW_DIRCREATE\n");
1909 SHCreateDirectoryExW(0, realpath
, NULL
);
1912 /* check if we can access the directory */
1913 res
= GetFileAttributesW(realpath
);
1915 HeapFree(GetProcessHeap(), 0, temppath
);
1917 if (res
== INVALID_FILE_ATTRIBUTES
)
1919 err
= GetLastError();
1920 if (err
== ERROR_FILE_NOT_FOUND
)
1921 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
1922 return HRESULT_FROM_WIN32(err
);
1924 else if (res
& FILE_ATTRIBUTE_DIRECTORY
)
1927 return HRESULT_FROM_WIN32(ERROR_DIRECTORY
);
1930 /***********************************************************************
1931 * SHPathPrepareForWriteA (SHELL32.@)
1933 EXTERN_C HRESULT WINAPI
SHPathPrepareForWriteA(HWND hwnd
, IUnknown
*modless
, LPCSTR path
, DWORD flags
)
1935 WCHAR wpath
[MAX_PATH
];
1936 MultiByteToWideChar( CP_ACP
, 0, path
, -1, wpath
, MAX_PATH
);
1937 return SHPathPrepareForWriteW(hwnd
, modless
, wpath
, flags
);