4 * Copyright 2000 Juergen Schmied
5 * Copyright 2002 Andriy Palamarchuk
6 * Copyright 2004 Dietrich Teickner (from Odin)
7 * Copyright 2004 Rolf Kalbermatter
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 //#define NO_SHLWAPI_STREAM
27 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
29 #define IsAttrib(x, y) ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
30 #define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
31 #define IsAttribDir(x) IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
32 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
35 #define WM_FILE (WM_USER + 1)
36 #define TIMER_ID (100)
38 static const WCHAR wWildcardFile
[] = {'*',0};
39 static const WCHAR wWildcardChars
[] = {'*','?',0};
41 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
);
42 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
);
43 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
);
44 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
);
45 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bFailIfExists
);
46 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
);
56 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
74 BOOL bAnyFromWildcard
;
87 }FILE_OPERATION_CONTEXT
;
90 /* Confirm dialogs with an optional "Yes To All" as used in file operations confirmations
92 static const WCHAR CONFIRM_MSG_PROP
[] = {'W','I','N','E','_','C','O','N','F','I','R','M',0};
94 struct confirm_msg_info
102 /* as some buttons may be hidden and the dialog height may change we may need
103 * to move the controls */
104 static void confirm_msg_move_button(HWND hDlg
, INT iId
, INT
*xPos
, INT yOffset
, BOOL bShow
)
106 HWND hButton
= GetDlgItem(hDlg
, iId
);
114 GetWindowRect(hButton
, &r
);
115 width
= r
.right
- r
.left
;
118 ScreenToClient(hDlg
, &pt
);
119 MoveWindow(hButton
, *xPos
- width
, pt
.y
- yOffset
, width
, r
.bottom
- r
.top
, FALSE
);
123 ShowWindow(hButton
, SW_HIDE
);
126 /* Note: we paint the text manually and don't use the static control to make
127 * sure the text has the same height as the one computed in WM_INITDIALOG
129 static INT_PTR
ConfirmMsgBox_Paint(HWND hDlg
)
136 BeginPaint(hDlg
, &ps
);
139 GetClientRect(GetDlgItem(hDlg
, IDC_YESTOALL_MESSAGE
), &r
);
140 /* this will remap the rect to dialog coords */
141 MapWindowPoints(GetDlgItem(hDlg
, IDC_YESTOALL_MESSAGE
), hDlg
, (LPPOINT
)&r
, 2);
142 hOldFont
= (HFONT
)SelectObject(hdc
, (HFONT
)SendDlgItemMessageW(hDlg
, IDC_YESTOALL_MESSAGE
, WM_GETFONT
, 0, 0));
143 DrawTextW(hdc
, (LPWSTR
)GetPropW(hDlg
, CONFIRM_MSG_PROP
), -1, &r
, DT_NOPREFIX
| DT_PATH_ELLIPSIS
| DT_WORDBREAK
);
144 SelectObject(hdc
, hOldFont
);
150 static INT_PTR
ConfirmMsgBox_Init(HWND hDlg
, LPARAM lParam
)
152 struct confirm_msg_info
*info
= (struct confirm_msg_info
*)lParam
;
159 SetWindowTextW(hDlg
, info
->lpszCaption
);
160 ShowWindow(GetDlgItem(hDlg
, IDC_YESTOALL_MESSAGE
), SW_HIDE
);
161 SetPropW(hDlg
, CONFIRM_MSG_PROP
, info
->lpszText
);
162 SendDlgItemMessageW(hDlg
, IDC_YESTOALL_ICON
, STM_SETICON
, (WPARAM
)info
->hIcon
, 0);
164 /* compute the text height and resize the dialog */
165 GetClientRect(GetDlgItem(hDlg
, IDC_YESTOALL_MESSAGE
), &r
);
168 hOldFont
= (HFONT
)SelectObject(hdc
, (HFONT
)SendDlgItemMessageW(hDlg
, IDC_YESTOALL_MESSAGE
, WM_GETFONT
, 0, 0));
169 DrawTextW(hdc
, info
->lpszText
, -1, &r
, DT_NOPREFIX
| DT_PATH_ELLIPSIS
| DT_WORDBREAK
| DT_CALCRECT
);
170 SelectObject(hdc
, hOldFont
);
172 yOffset
= min(yOffset
, 35); /* don't make the dialog too small */
173 ReleaseDC(hDlg
, hdc
);
175 GetClientRect(hDlg
, &r
);
177 GetWindowRect(hDlg
, &r
);
178 width
= r
.right
- r
.left
;
179 height
= r
.bottom
- r
.top
- yOffset
;
180 MoveWindow(hDlg
, (GetSystemMetrics(SM_CXSCREEN
) - width
)/2,
181 (GetSystemMetrics(SM_CYSCREEN
) - height
)/2, width
, height
, FALSE
);
183 confirm_msg_move_button(hDlg
, IDCANCEL
, &xPos
, yOffset
, info
->bYesToAll
);
184 confirm_msg_move_button(hDlg
, IDNO
, &xPos
, yOffset
, TRUE
);
185 confirm_msg_move_button(hDlg
, IDC_YESTOALL
, &xPos
, yOffset
, info
->bYesToAll
);
186 confirm_msg_move_button(hDlg
, IDYES
, &xPos
, yOffset
, TRUE
);
191 static INT_PTR CALLBACK
ConfirmMsgBoxProc(HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
196 return ConfirmMsgBox_Init(hDlg
, lParam
);
198 return ConfirmMsgBox_Paint(hDlg
);
200 EndDialog(hDlg
, wParam
);
203 EndDialog(hDlg
, IDCANCEL
);
209 int SHELL_ConfirmMsgBox(HWND hWnd
, LPWSTR lpszText
, LPWSTR lpszCaption
, HICON hIcon
, BOOL bYesToAll
)
211 struct confirm_msg_info info
;
213 info
.lpszText
= lpszText
;
214 info
.lpszCaption
= lpszCaption
;
216 info
.bYesToAll
= bYesToAll
;
217 return DialogBoxParamW(shell32_hInstance
, MAKEINTRESOURCEW(IDD_YESTOALL_MSGBOX
), hWnd
, ConfirmMsgBoxProc
, (LPARAM
)&info
);
220 /* confirmation dialogs content */
223 HINSTANCE hIconInstance
;
224 UINT icon_resource_id
;
225 UINT caption_resource_id
, text_resource_id
;
226 } SHELL_ConfirmIDstruc
;
228 static BOOL
SHELL_ConfirmIDs(int nKindOfDialog
, SHELL_ConfirmIDstruc
*ids
)
230 ids
->hIconInstance
= shell32_hInstance
;
231 switch (nKindOfDialog
)
233 case ASK_DELETE_FILE
:
234 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
235 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
236 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
239 case ASK_DELETE_FOLDER
:
240 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
241 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
242 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
245 case ASK_DELETE_MULTIPLE_ITEM
:
246 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
247 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
248 ids
->text_resource_id
= IDS_DELETEMULTIPLE_TEXT
;
252 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
253 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
254 ids
->text_resource_id
= IDS_TRASHITEM_TEXT
;
257 case ASK_TRASH_FOLDER
:
258 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
259 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
260 ids
->text_resource_id
= IDS_TRASHFOLDER_TEXT
;
263 case ASK_TRASH_MULTIPLE_ITEM
:
264 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
265 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
266 ids
->text_resource_id
= IDS_TRASHMULTIPLE_TEXT
;
269 case ASK_CANT_TRASH_ITEM
:
270 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
271 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
272 ids
->text_resource_id
= IDS_CANTTRASH_TEXT
;
275 case ASK_DELETE_SELECTED
:
276 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
277 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
278 ids
->text_resource_id
= IDS_DELETESELECTED_TEXT
;
281 case ASK_OVERWRITE_FILE
:
282 ids
->hIconInstance
= NULL
;
283 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
284 ids
->caption_resource_id
= IDS_OVERWRITEFILE_CAPTION
;
285 ids
->text_resource_id
= IDS_OVERWRITEFILE_TEXT
;
288 case ASK_OVERWRITE_FOLDER
:
289 ids
->hIconInstance
= NULL
;
290 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
291 ids
->caption_resource_id
= IDS_OVERWRITEFILE_CAPTION
;
292 ids
->text_resource_id
= IDS_OVERWRITEFOLDER_TEXT
;
296 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog
);
301 static BOOL
SHELL_ConfirmDialogW(HWND hWnd
, int nKindOfDialog
, LPCWSTR szDir
, FILE_OPERATION
*op
)
303 WCHAR szCaption
[255], szText
[255], szBuffer
[MAX_PATH
+ 256];
304 SHELL_ConfirmIDstruc ids
;
309 assert(nKindOfDialog
>= 0 && nKindOfDialog
< 32);
310 if (op
&& (op
->dwYesToAllMask
& (1 << nKindOfDialog
)))
313 if (!SHELL_ConfirmIDs(nKindOfDialog
, &ids
)) return FALSE
;
315 LoadStringW(shell32_hInstance
, ids
.caption_resource_id
, szCaption
, sizeof(szCaption
)/sizeof(WCHAR
));
316 LoadStringW(shell32_hInstance
, ids
.text_resource_id
, szText
, sizeof(szText
)/sizeof(WCHAR
));
318 args
[0] = (DWORD_PTR
)szDir
;
319 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
320 szText
, 0, 0, szBuffer
, sizeof(szBuffer
), (va_list*)args
);
321 hIcon
= LoadIconW(ids
.hIconInstance
, (LPWSTR
)MAKEINTRESOURCE(ids
.icon_resource_id
));
323 ret
= SHELL_ConfirmMsgBox(hWnd
, szBuffer
, szCaption
, hIcon
, op
&& op
->bManyItems
);
326 if (ret
== IDC_YESTOALL
)
328 op
->dwYesToAllMask
|= (1 << nKindOfDialog
);
332 op
->bCancelled
= TRUE
;
334 op
->req
->fAnyOperationsAborted
= TRUE
;
339 BOOL
SHELL_ConfirmYesNoW(HWND hWnd
, int nKindOfDialog
, LPCWSTR szDir
)
341 return SHELL_ConfirmDialogW(hWnd
, nKindOfDialog
, szDir
, NULL
);
344 static DWORD
SHELL32_AnsiToUnicodeBuf(LPCSTR aPath
, LPWSTR
*wPath
, DWORD minChars
)
346 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, NULL
, 0);
351 *wPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
354 MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, *wPath
, len
);
357 return E_OUTOFMEMORY
;
360 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath
)
362 HeapFree(GetProcessHeap(), 0, wPath
);
365 EXTERN_C HRESULT WINAPI
SHIsFileAvailableOffline(LPCWSTR path
, LPDWORD status
)
367 FIXME("(%s, %p) stub\n", debugstr_w(path
), status
);
371 /**************************************************************************
372 * SHELL_DeleteDirectory() [internal]
374 * Asks for confirmation when bShowUI is true and deletes the directory and
375 * all its subdirectories and files if necessary.
377 BOOL
SHELL_DeleteDirectoryW(HWND hwnd
, LPCWSTR pszDir
, BOOL bShowUI
)
381 WIN32_FIND_DATAW wfd
;
382 WCHAR szTemp
[MAX_PATH
];
384 /* Make sure the directory exists before eventually prompting the user */
385 PathCombineW(szTemp
, pszDir
, wWildcardFile
);
386 hFind
= FindFirstFileW(szTemp
, &wfd
);
387 if (hFind
== INVALID_HANDLE_VALUE
)
390 if (!bShowUI
|| (ret
= SHELL_ConfirmDialogW(hwnd
, ASK_DELETE_FOLDER
, pszDir
, NULL
)))
394 if (IsDotDir(wfd
.cFileName
))
396 PathCombineW(szTemp
, pszDir
, wfd
.cFileName
);
397 if (FILE_ATTRIBUTE_DIRECTORY
& wfd
.dwFileAttributes
)
398 ret
= SHELL_DeleteDirectoryW(hwnd
, szTemp
, FALSE
);
400 ret
= (SHNotifyDeleteFileW(szTemp
) == ERROR_SUCCESS
);
401 } while (ret
&& FindNextFileW(hFind
, &wfd
));
405 ret
= (SHNotifyRemoveDirectoryW(pszDir
) == ERROR_SUCCESS
);
409 /**************************************************************************
410 * Win32CreateDirectory [SHELL32.93]
412 * Creates a directory. Also triggers a change notify if one exists.
415 * path [I] path to directory to create
418 * TRUE if successful, FALSE otherwise
421 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
423 TRACE("(%s, %p)\n", debugstr_w(path
), sec
);
425 if (CreateDirectoryW(path
, sec
))
427 SHChangeNotify(SHCNE_MKDIR
, SHCNF_PATHW
, path
, NULL
);
428 return ERROR_SUCCESS
;
430 return GetLastError();
433 /**********************************************************************/
435 EXTERN_C BOOL WINAPI
Win32CreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
437 return (SHNotifyCreateDirectoryW(path
, sec
) == ERROR_SUCCESS
);
440 /************************************************************************
441 * Win32RemoveDirectory [SHELL32.94]
443 * Deletes a directory. Also triggers a change notify if one exists.
446 * path [I] path to directory to delete
449 * TRUE if successful, FALSE otherwise
451 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
)
454 TRACE("(%s)\n", debugstr_w(path
));
456 ret
= RemoveDirectoryW(path
);
459 /* Directory may be write protected */
460 DWORD dwAttr
= GetFileAttributesW(path
);
461 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
))
462 if (SetFileAttributesW(path
, dwAttr
& ~FILE_ATTRIBUTE_READONLY
))
463 ret
= RemoveDirectoryW(path
);
467 SHChangeNotify(SHCNE_RMDIR
, SHCNF_PATHW
, path
, NULL
);
468 return ERROR_SUCCESS
;
470 return GetLastError();
473 /***********************************************************************/
475 EXTERN_C BOOL WINAPI
Win32RemoveDirectoryW(LPCWSTR path
)
477 return (SHNotifyRemoveDirectoryW(path
) == ERROR_SUCCESS
);
480 /************************************************************************
481 * Win32DeleteFile [SHELL32.164]
483 * Deletes a file. Also triggers a change notify if one exists.
486 * path [I] path to file to delete
489 * TRUE if successful, FALSE otherwise
491 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
)
495 TRACE("(%s)\n", debugstr_w(path
));
497 ret
= DeleteFileW(path
);
500 /* File may be write protected or a system file */
501 DWORD dwAttr
= GetFileAttributesW(path
);
502 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
503 if (SetFileAttributesW(path
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
504 ret
= DeleteFileW(path
);
508 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, path
, NULL
);
509 return ERROR_SUCCESS
;
511 return GetLastError();
514 /***********************************************************************/
516 EXTERN_C DWORD WINAPI
Win32DeleteFileW(LPCWSTR path
)
518 return (SHNotifyDeleteFileW(path
) == ERROR_SUCCESS
);
521 /************************************************************************
522 * SHNotifyMoveFile [internal]
524 * Moves a file. Also triggers a change notify if one exists.
527 * src [I] path to source file to move
528 * dest [I] path to target file to move to
531 * ERORR_SUCCESS if successful
533 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
)
537 TRACE("(%s %s)\n", debugstr_w(src
), debugstr_w(dest
));
539 ret
= MoveFileExW(src
, dest
, MOVEFILE_REPLACE_EXISTING
);
541 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
543 ret
= MoveFileW(src
, dest
);
549 dwAttr
= SHFindAttrW(dest
, FALSE
);
550 if (INVALID_FILE_ATTRIBUTES
== dwAttr
)
552 /* Source file may be write protected or a system file */
553 dwAttr
= GetFileAttributesW(src
);
554 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
555 if (SetFileAttributesW(src
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
556 ret
= MoveFileW(src
, dest
);
561 SHChangeNotify(SHCNE_RENAMEITEM
, SHCNF_PATHW
, src
, dest
);
562 return ERROR_SUCCESS
;
564 return GetLastError();
567 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
)
569 FILE_OPERATION_CONTEXT
* Context
;
570 LARGE_INTEGER Progress
;
573 Context
= (FILE_OPERATION_CONTEXT
*)lpData
;
575 if (TotalBytesTransferred
.QuadPart
)
577 Progress
.QuadPart
= (TotalBytesTransferred
.QuadPart
* 100) / TotalFileSize
.QuadPart
;
581 Progress
.QuadPart
= 1;
584 /* update progress bar */
585 SendMessageW(Context
->hDlgCtrl
, PBM_SETPOS
, (WPARAM
)Progress
.u
.LowPart
, 0);
587 if (TotalBytesTransferred
.QuadPart
== TotalFileSize
.QuadPart
)
589 /* file was copied */
591 PostMessageW(Context
->hwndDlg
, WM_FILE
, 0, 0);
594 return PROGRESS_CONTINUE
;
599 FILE_OPERATION_CONTEXT
* Context
)
601 FILE_ENTRY
* from
, *to
= NULL
;
604 if (Context
->Index
>= Context
->from
->dwNumFiles
)
607 /* get current file */
608 from
= &Context
->from
->feFiles
[Context
->Index
];
610 if (Context
->op
->req
->wFunc
!= FO_DELETE
)
611 to
= &Context
->to
->feFiles
[Context
->Index
];
614 SetDlgItemTextW(Context
->hwndDlg
, 14001, from
->szFullPath
);
616 if (Context
->op
->req
->wFunc
== FO_COPY
)
618 bRet
= CopyFileExW(from
->szFullPath
, to
->szFullPath
, SHOperationProgressRoutine
, (LPVOID
)Context
, &Context
->op
->bCancelled
, 0);
620 else if (Context
->op
->req
->wFunc
== FO_MOVE
)
622 //bRet = MoveFileWithProgressW(from->szFullPath, to->szFullPath, SHOperationProgressRoutine, (LPVOID)Context, MOVEFILE_COPY_ALLOWED);
624 else if (Context
->op
->req
->wFunc
== FO_DELETE
)
626 bRet
= DeleteFile(from
->szFullPath
);
632 static INT_PTR CALLBACK
SHOperationDialog(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
634 FILE_OPERATION_CONTEXT
* Context
;
636 Context
= (FILE_OPERATION_CONTEXT
*) GetWindowLongPtr(hwndDlg
, DWLP_USER
);
641 SetWindowLongPtr(hwndDlg
, DWLP_USER
, (LONG
)lParam
);
644 Context
= (FILE_OPERATION_CONTEXT
*)lParam
;
646 /* store progress bar handle */
647 Context
->hDlgCtrl
= GetDlgItem(hwndDlg
, 14000);
649 /* store window handle */
650 Context
->hwndDlg
= hwndDlg
;
652 /* set progress bar range */
653 (void)SendMessageW(Context
->hDlgCtrl
, (UINT
) PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
655 /* start file queueing */
656 SetTimer(hwndDlg
, TIMER_ID
, 1000, NULL
);
657 //QueueFile(Context);
662 Context
->op
->bCancelled
= TRUE
;
663 EndDialog(hwndDlg
, Context
->op
->bCancelled
);
667 if (LOWORD(wParam
) == 14002)
669 Context
->op
->bCancelled
= TRUE
;
670 EndDialog(hwndDlg
, Context
->op
->bCancelled
);
675 if (wParam
== TIMER_ID
)
678 KillTimer(hwndDlg
, TIMER_ID
);
682 if (!QueueFile(Context
))
683 EndDialog(hwndDlg
, Context
->op
->bCancelled
);
691 SHShowFileOperationDialog(FILE_OPERATION
*op
, FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
696 FILE_OPERATION_CONTEXT Context
;
698 Context
.from
= flFrom
;
702 Context
.op
->bCancelled
= FALSE
;
704 hwnd
= CreateDialogParam(shell32_hInstance
, MAKEINTRESOURCE(IDD_FILE_COPY
), NULL
, SHOperationDialog
, (LPARAM
)&Context
);
707 ERR("Failed to create dialog\n");
710 ShowWindow(hwnd
, SW_SHOWNORMAL
);
712 while ((bRet
= GetMessage(&msg
, NULL
, 0, 0)) != 0)
714 if (!IsWindow(hwnd
) || !IsDialogMessage(hwnd
, &msg
))
716 TranslateMessage(&msg
);
717 DispatchMessage(&msg
);
725 /************************************************************************
726 * SHNotifyCopyFile [internal]
728 * Copies a file. Also triggers a change notify if one exists.
731 * src [I] path to source file to move
732 * dest [I] path to target file to move to
733 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
734 * a file with this name already exists
737 * ERROR_SUCCESS if successful
739 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bFailIfExists
)
744 TRACE("(%s %s %s)\n", debugstr_w(src
), debugstr_w(dest
), bFailIfExists
? "failIfExists" : "");
746 /* Destination file may already exist with read only attribute */
747 attribs
= GetFileAttributesW(dest
);
748 if (IsAttrib(attribs
, FILE_ATTRIBUTE_READONLY
))
749 SetFileAttributesW(dest
, attribs
& ~FILE_ATTRIBUTE_READONLY
);
751 if (GetFileAttributesW(dest
) & FILE_ATTRIBUTE_READONLY
)
753 SetFileAttributesW(dest
, attribs
& ~FILE_ATTRIBUTE_READONLY
);
754 if (GetFileAttributesW(dest
) & FILE_ATTRIBUTE_READONLY
)
756 TRACE("[shell32, SHNotifyCopyFileW] STILL SHIT\n");
760 ret
= CopyFileW(src
, dest
, bFailIfExists
);
763 SHChangeNotify(SHCNE_CREATE
, SHCNF_PATHW
, dest
, NULL
);
764 return ERROR_SUCCESS
;
767 return GetLastError();
770 /*************************************************************************
771 * SHCreateDirectory [SHELL32.165]
773 * This function creates a file system folder whose fully qualified path is
774 * given by path. If one or more of the intermediate folders do not exist,
775 * they will be created as well.
779 * path [I] path of directory to create
782 * ERROR_SUCCESS or one of the following values:
783 * ERROR_BAD_PATHNAME if the path is relative
784 * ERROR_FILE_EXISTS when a file with that name exists
785 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
786 * ERROR_INVALID_NAME if the path contains invalid chars
787 * ERROR_ALREADY_EXISTS when the directory already exists
788 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
791 * exported by ordinal
793 * WinNT/2000 exports Unicode
795 int WINAPI
SHCreateDirectory(HWND hWnd
, LPCWSTR path
)
797 return SHCreateDirectoryExW(hWnd
, path
, NULL
);
800 /*************************************************************************
801 * SHCreateDirectoryExA [SHELL32.@]
803 * This function creates a file system folder whose fully qualified path is
804 * given by path. If one or more of the intermediate folders do not exist,
805 * they will be created as well.
809 * path [I] path of directory to create
810 * sec [I] security attributes to use or NULL
813 * ERROR_SUCCESS or one of the following values:
814 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
815 * ERROR_INVALID_NAME if the path contains invalid chars
816 * ERROR_FILE_EXISTS when a file with that name exists
817 * ERROR_ALREADY_EXISTS when the directory already exists
818 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
820 * FIXME: Not implemented yet;
821 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
822 * if the path is a network path to deal with network drivers which might have a limited
823 * but unknown maximum path length. If not:
825 * If hWnd is set to a valid window handle, a message box is displayed warning
826 * the user that the files may not be accessible. If the user chooses not to
827 * proceed, the function returns ERROR_CANCELLED.
829 * If hWnd is set to NULL, no user interface is displayed and the function
830 * returns ERROR_CANCELLED.
832 int WINAPI
SHCreateDirectoryExA(HWND hWnd
, LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
837 TRACE("(%s, %p)\n", debugstr_a(path
), sec
);
839 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
842 retCode
= SHCreateDirectoryExW(hWnd
, wPath
, sec
);
843 SHELL32_FreeUnicodeBuf(wPath
);
848 /*************************************************************************
849 * SHCreateDirectoryExW [SHELL32.@]
851 * See SHCreateDirectoryExA.
853 int WINAPI
SHCreateDirectoryExW(HWND hWnd
, LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
855 int ret
= ERROR_BAD_PATHNAME
;
856 TRACE("(%p, %s, %p)\n", hWnd
, debugstr_w(path
), sec
);
858 if (PathIsRelativeW(path
))
864 ret
= SHNotifyCreateDirectoryW(path
, sec
);
865 /* Refuse to work on certain error codes before trying to create directories recursively */
866 if (ret
!= ERROR_SUCCESS
&&
867 ret
!= ERROR_FILE_EXISTS
&&
868 ret
!= ERROR_ALREADY_EXISTS
&&
869 ret
!= ERROR_FILENAME_EXCED_RANGE
)
871 WCHAR
*pEnd
, *pSlash
, szTemp
[MAX_PATH
+ 1]; /* extra for PathAddBackslash() */
873 lstrcpynW(szTemp
, path
, MAX_PATH
);
874 pEnd
= PathAddBackslashW(szTemp
);
879 while (*pSlash
&& *pSlash
!= '\\') pSlash
++;
882 *pSlash
= 0; /* terminate path at separator */
884 ret
= SHNotifyCreateDirectoryW(szTemp
, pSlash
+ 1 == pEnd
? sec
: NULL
);
886 *pSlash
++ = '\\'; /* put the separator back */
890 if (ret
&& hWnd
&& (ERROR_CANCELLED
!= ret
))
892 /* We failed and should show a dialog box */
893 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path
), ret
);
894 ret
= ERROR_CANCELLED
; /* Error has been already presented to user (not really yet!) */
901 /*************************************************************************
902 * SHFindAttrW [internal]
904 * Get the Attributes for a file or directory. The difference to GetAttributes()
905 * is that this function will also work for paths containing wildcard characters
909 * path [I] path of directory or file to check
910 * fileOnly [I] TRUE if only files should be found
913 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
914 * the first file or directory found otherwise
916 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
)
918 WIN32_FIND_DATAW wfd
;
919 BOOL b_FileMask
= fileOnly
&& (NULL
!= StrPBrkW(pName
, wWildcardChars
));
920 DWORD dwAttr
= INVALID_FILE_ATTRIBUTES
;
921 HANDLE hFind
= FindFirstFileW(pName
, &wfd
);
923 TRACE("%s %d\n", debugstr_w(pName
), fileOnly
);
924 if (INVALID_HANDLE_VALUE
!= hFind
)
928 if (b_FileMask
&& IsAttribDir(wfd
.dwFileAttributes
))
930 dwAttr
= wfd
.dwFileAttributes
;
932 } while (FindNextFileW(hFind
, &wfd
));
939 /*************************************************************************
941 * SHNameTranslate HelperFunction for SHFileOperationA
943 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
944 * is NULL, only the necessary size of the string is determined and returned,
945 * otherwise the ASCII strings are copied into it and the buffer is increased
946 * to point to the location after the final 0 termination char.
948 static DWORD
SHNameTranslate(LPWSTR
* wString
, LPCWSTR
* pWToFrom
, BOOL more
)
950 DWORD size
= 0, aSize
= 0;
951 LPCSTR aString
= (LPCSTR
)*pWToFrom
;
957 size
= lstrlenA(aString
) + 1;
960 } while ((size
!= 1) && more
);
962 /* The two sizes might be different in the case of multibyte chars */
963 size
= MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, 0);
964 if (*wString
) /* only in the second loop */
966 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, size
);
967 *pWToFrom
= *wString
;
973 /*************************************************************************
974 * SHFileOperationA [SHELL32.@]
976 * Function to copy, move, delete and create one or more files with optional
980 * lpFileOp [I/O] pointer to a structure containing all the necessary information
983 * Success: ERROR_SUCCESS.
984 * Failure: ERROR_CANCELLED.
989 int WINAPI
SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp
)
991 SHFILEOPSTRUCTW nFileOp
= *((LPSHFILEOPSTRUCTW
)lpFileOp
);
994 LPWSTR ForFree
= NULL
, /* we change wString in SHNameTranslate and can't use it for freeing */
995 wString
= NULL
; /* we change this in SHNameTranslate */
998 if (FO_DELETE
== (nFileOp
.wFunc
& FO_MASK
))
999 nFileOp
.pTo
= NULL
; /* we need a NULL or a valid pointer for translation */
1000 if (!(nFileOp
.fFlags
& FOF_SIMPLEPROGRESS
))
1001 nFileOp
.lpszProgressTitle
= NULL
; /* we need a NULL or a valid pointer for translation */
1002 while (1) /* every loop calculate size, second translate also, if we have storage for this */
1004 size
= SHNameTranslate(&wString
, &nFileOp
.lpszProgressTitle
, FALSE
); /* no loop */
1005 size
+= SHNameTranslate(&wString
, &nFileOp
.pFrom
, TRUE
); /* internal loop */
1006 size
+= SHNameTranslate(&wString
, &nFileOp
.pTo
, TRUE
); /* internal loop */
1010 retCode
= SHFileOperationW(&nFileOp
);
1011 HeapFree(GetProcessHeap(), 0, ForFree
); /* we cannot use wString, it was changed */
1016 wString
= ForFree
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
1017 if (ForFree
) continue;
1018 retCode
= ERROR_OUTOFMEMORY
;
1019 nFileOp
.fAnyOperationsAborted
= TRUE
;
1020 SetLastError(retCode
);
1025 lpFileOp
->hNameMappings
= nFileOp
.hNameMappings
;
1026 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
1030 static void __inline
grow_list(FILE_LIST
*list
)
1032 FILE_ENTRY
*newx
= (FILE_ENTRY
*)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, list
->feFiles
,
1033 list
->num_alloc
* 2 * sizeof(*newx
) );
1034 list
->feFiles
= newx
;
1035 list
->num_alloc
*= 2;
1038 /* adds a file to the FILE_ENTRY struct
1040 static void add_file_to_entry(FILE_ENTRY
*feFile
, LPCWSTR szFile
)
1042 DWORD dwLen
= lstrlenW(szFile
) + 1;
1045 feFile
->szFullPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
1046 lstrcpyW(feFile
->szFullPath
, szFile
);
1048 ptr
= StrRChrW(szFile
, NULL
, '\\');
1051 dwLen
= ptr
- szFile
+ 1;
1052 feFile
->szDirectory
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
1053 lstrcpynW(feFile
->szDirectory
, szFile
, dwLen
);
1055 dwLen
= lstrlenW(feFile
->szFullPath
) - dwLen
+ 1;
1056 feFile
->szFilename
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
1057 lstrcpyW(feFile
->szFilename
, ptr
+ 1); /* skip over backslash */
1059 feFile
->bFromWildcard
= FALSE
;
1062 static LPWSTR
wildcard_to_file(LPCWSTR szWildCard
, LPCWSTR szFileName
)
1066 DWORD dwDirLen
, dwFullLen
;
1068 ptr
= StrRChrW(szWildCard
, NULL
, '\\');
1069 dwDirLen
= ptr
- szWildCard
+ 1;
1071 dwFullLen
= dwDirLen
+ lstrlenW(szFileName
) + 1;
1072 szFullPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwFullLen
* sizeof(WCHAR
));
1074 lstrcpynW(szFullPath
, szWildCard
, dwDirLen
+ 1);
1075 lstrcatW(szFullPath
, szFileName
);
1080 static void parse_wildcard_files(FILE_LIST
*flList
, LPCWSTR szFile
, LPDWORD pdwListIndex
)
1082 WIN32_FIND_DATAW wfd
;
1083 HANDLE hFile
= FindFirstFileW(szFile
, &wfd
);
1088 if (hFile
== INVALID_HANDLE_VALUE
) return;
1090 for (res
= TRUE
; res
; res
= FindNextFileW(hFile
, &wfd
))
1092 if (IsDotDir(wfd
.cFileName
))
1095 if (*pdwListIndex
>= flList
->num_alloc
)
1096 grow_list( flList
);
1098 szFullPath
= wildcard_to_file(szFile
, wfd
.cFileName
);
1099 file
= &flList
->feFiles
[(*pdwListIndex
)++];
1100 add_file_to_entry(file
, szFullPath
);
1101 file
->bFromWildcard
= TRUE
;
1102 file
->attributes
= wfd
.dwFileAttributes
;
1104 if (IsAttribDir(file
->attributes
))
1105 flList
->bAnyDirectories
= TRUE
;
1107 HeapFree(GetProcessHeap(), 0, szFullPath
);
1113 /* takes the null-separated file list and fills out the FILE_LIST */
1114 static HRESULT
parse_file_list(FILE_LIST
*flList
, LPCWSTR szFiles
)
1116 LPCWSTR ptr
= szFiles
;
1117 WCHAR szCurFile
[MAX_PATH
];
1121 return ERROR_INVALID_PARAMETER
;
1123 flList
->bAnyFromWildcard
= FALSE
;
1124 flList
->bAnyDirectories
= FALSE
;
1125 flList
->bAnyDontExist
= FALSE
;
1126 flList
->num_alloc
= 32;
1127 flList
->dwNumFiles
= 0;
1131 return ERROR_ACCESS_DENIED
;
1133 flList
->feFiles
= (FILE_ENTRY
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1134 flList
->num_alloc
* sizeof(FILE_ENTRY
));
1138 if (i
>= flList
->num_alloc
) grow_list( flList
);
1140 /* change relative to absolute path */
1141 if (PathIsRelativeW(ptr
))
1143 GetCurrentDirectoryW(MAX_PATH
, szCurFile
);
1144 PathCombineW(szCurFile
, szCurFile
, ptr
);
1145 flList
->feFiles
[i
].bFromRelative
= TRUE
;
1149 lstrcpyW(szCurFile
, ptr
);
1150 flList
->feFiles
[i
].bFromRelative
= FALSE
;
1153 /* parse wildcard files if they are in the filename */
1154 if (StrPBrkW(szCurFile
, wWildcardChars
))
1156 parse_wildcard_files(flList
, szCurFile
, &i
);
1157 flList
->bAnyFromWildcard
= TRUE
;
1162 FILE_ENTRY
*file
= &flList
->feFiles
[i
];
1163 add_file_to_entry(file
, szCurFile
);
1164 file
->attributes
= GetFileAttributesW( file
->szFullPath
);
1165 file
->bExists
= (file
->attributes
!= INVALID_FILE_ATTRIBUTES
);
1168 flList
->bAnyDontExist
= TRUE
;
1170 if (IsAttribDir(file
->attributes
))
1171 flList
->bAnyDirectories
= TRUE
;
1174 /* advance to the next string */
1175 ptr
+= lstrlenW(ptr
) + 1;
1178 flList
->dwNumFiles
= i
;
1183 /* free the FILE_LIST */
1184 static void destroy_file_list(FILE_LIST
*flList
)
1188 if (!flList
|| !flList
->feFiles
)
1191 for (i
= 0; i
< flList
->dwNumFiles
; i
++)
1193 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szDirectory
);
1194 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFilename
);
1195 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFullPath
);
1198 HeapFree(GetProcessHeap(), 0, flList
->feFiles
);
1201 static void copy_dir_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, LPCWSTR szDestPath
)
1203 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
1204 SHFILEOPSTRUCTW fileOp
;
1206 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1208 if (IsDotDir(feFrom
->szFilename
))
1211 if (PathFileExistsW(szDestPath
))
1212 PathCombineW(szTo
, szDestPath
, feFrom
->szFilename
);
1214 lstrcpyW(szTo
, szDestPath
);
1216 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
))
1218 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FOLDER
, feFrom
->szFilename
, op
))
1220 /* Vista returns an ERROR_CANCELLED even if user pressed "No" */
1221 if (!op
->bManyItems
)
1222 op
->bCancelled
= TRUE
;
1227 szTo
[lstrlenW(szTo
) + 1] = '\0';
1228 SHNotifyCreateDirectoryW(szTo
, NULL
);
1230 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1231 szFrom
[lstrlenW(szFrom
) + 1] = '\0';
1234 fileOp
.pFrom
= szFrom
;
1236 fileOp
.fFlags
&= ~FOF_MULTIDESTFILES
; /* we know we're copying to one dir */
1238 /* Don't ask the user about overwriting files when he accepted to overwrite the
1239 folder. FIXME: this is not exactly what Windows does - e.g. there would be
1240 an additional confirmation for a nested folder */
1241 fileOp
.fFlags
|= FOF_NOCONFIRMATION
;
1243 SHFileOperationW(&fileOp
);
1246 static BOOL
copy_file_to_file(FILE_OPERATION
*op
, const WCHAR
*szFrom
, const WCHAR
*szTo
)
1248 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
))
1250 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FILE
, PathFindFileNameW(szTo
), op
))
1254 return SHNotifyCopyFileW(szFrom
, szTo
, FALSE
) == 0;
1257 /* copy a file or directory to another directory */
1258 static void copy_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1260 if (!PathFileExistsW(feTo
->szFullPath
))
1261 SHNotifyCreateDirectoryW(feTo
->szFullPath
, NULL
);
1263 if (IsAttribFile(feFrom
->attributes
))
1265 WCHAR szDestPath
[MAX_PATH
];
1267 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1268 copy_file_to_file(op
, feFrom
->szFullPath
, szDestPath
);
1270 else if (!(op
->req
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1271 copy_dir_to_dir(op
, feFrom
, feTo
->szFullPath
);
1274 static void create_dest_dirs(LPCWSTR szDestDir
)
1276 WCHAR dir
[MAX_PATH
];
1277 LPCWSTR ptr
= StrChrW(szDestDir
, '\\');
1279 /* make sure all directories up to last one are created */
1280 while (ptr
&& (ptr
= StrChrW(ptr
+ 1, '\\')))
1282 lstrcpynW(dir
, szDestDir
, ptr
- szDestDir
+ 1);
1284 if (!PathFileExistsW(dir
))
1285 SHNotifyCreateDirectoryW(dir
, NULL
);
1288 /* create last directory */
1289 if (!PathFileExistsW(szDestDir
))
1290 SHNotifyCreateDirectoryW(szDestDir
, NULL
);
1293 /* the FO_COPY operation */
1294 static HRESULT
copy_files(FILE_OPERATION
*op
, const FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
1297 const FILE_ENTRY
*entryToCopy
;
1298 const FILE_ENTRY
*fileDest
= &flTo
->feFiles
[0];
1300 if (flFrom
->bAnyDontExist
)
1301 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1303 if (flTo
->dwNumFiles
== 0)
1305 /* If the destination is empty, SHFileOperation should use the current directory */
1306 WCHAR curdir
[MAX_PATH
+1];
1308 GetCurrentDirectoryW(MAX_PATH
, curdir
);
1309 curdir
[lstrlenW(curdir
)+1] = 0;
1311 destroy_file_list(flTo
);
1312 ZeroMemory(flTo
, sizeof(FILE_LIST
));
1313 parse_file_list(flTo
, curdir
);
1314 fileDest
= &flTo
->feFiles
[0];
1317 if (op
->req
->fFlags
& FOF_MULTIDESTFILES
)
1319 if (flFrom
->bAnyFromWildcard
)
1320 return ERROR_CANCELLED
;
1322 if (flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1324 if (flFrom
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1325 return ERROR_CANCELLED
;
1327 /* Free all but the first entry. */
1328 for (i
= 1; i
< flTo
->dwNumFiles
; i
++)
1330 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szDirectory
);
1331 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szFilename
);
1332 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szFullPath
);
1335 flTo
->dwNumFiles
= 1;
1337 else if (IsAttribDir(fileDest
->attributes
))
1339 for (i
= 1; i
< flTo
->dwNumFiles
; i
++)
1340 if (!IsAttribDir(flTo
->feFiles
[i
].attributes
) ||
1341 !IsAttribDir(flFrom
->feFiles
[i
].attributes
))
1343 return ERROR_CANCELLED
;
1347 else if (flFrom
->dwNumFiles
!= 1)
1349 if (flTo
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1350 return ERROR_CANCELLED
;
1352 if (PathFileExistsW(fileDest
->szFullPath
) &&
1353 IsAttribFile(fileDest
->attributes
))
1355 return ERROR_CANCELLED
;
1358 if (flTo
->dwNumFiles
== 1 && fileDest
->bFromRelative
&&
1359 !PathFileExistsW(fileDest
->szFullPath
))
1361 return ERROR_CANCELLED
;
1365 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1367 entryToCopy
= &flFrom
->feFiles
[i
];
1369 if ((op
->req
->fFlags
& FOF_MULTIDESTFILES
) &&
1370 flTo
->dwNumFiles
> 1)
1372 fileDest
= &flTo
->feFiles
[i
];
1375 if (IsAttribDir(entryToCopy
->attributes
) &&
1376 !lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szDirectory
))
1378 return ERROR_SUCCESS
;
1381 create_dest_dirs(fileDest
->szDirectory
);
1383 if (!lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1385 if (IsAttribFile(entryToCopy
->attributes
))
1386 return ERROR_NO_MORE_SEARCH_HANDLES
;
1388 return ERROR_SUCCESS
;
1391 if ((flFrom
->dwNumFiles
> 1 && flTo
->dwNumFiles
== 1) ||
1392 IsAttribDir(fileDest
->attributes
))
1394 copy_to_dir(op
, entryToCopy
, fileDest
);
1396 else if (IsAttribDir(entryToCopy
->attributes
))
1398 copy_dir_to_dir(op
, entryToCopy
, fileDest
->szFullPath
);
1402 if (!copy_file_to_file(op
, entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1404 op
->req
->fAnyOperationsAborted
= TRUE
;
1405 return ERROR_CANCELLED
;
1409 /* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
1411 return ERROR_CANCELLED
;
1414 /* Vista return code. On XP if the used pressed "No" for the last item,
1415 * ERROR_ARENA_TRASHED would be returned */
1416 return ERROR_SUCCESS
;
1419 static BOOL
confirm_delete_list(HWND hWnd
, DWORD fFlags
, BOOL fTrash
, const FILE_LIST
*flFrom
)
1421 if (flFrom
->dwNumFiles
> 1)
1424 const WCHAR format
[] = {'%','d',0};
1426 wnsprintfW(tmp
, sizeof(tmp
)/sizeof(tmp
[0]), format
, flFrom
->dwNumFiles
);
1427 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_MULTIPLE_ITEM
:ASK_DELETE_MULTIPLE_ITEM
), tmp
, NULL
);
1431 const FILE_ENTRY
*fileEntry
= &flFrom
->feFiles
[0];
1433 if (IsAttribFile(fileEntry
->attributes
))
1434 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FILE
:ASK_DELETE_FILE
), fileEntry
->szFullPath
, NULL
);
1435 else if (!(fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1436 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FOLDER
:ASK_DELETE_FOLDER
), fileEntry
->szFullPath
, NULL
);
1441 /* the FO_DELETE operation */
1442 static HRESULT
delete_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
)
1444 const FILE_ENTRY
*fileEntry
;
1449 if (!flFrom
->dwNumFiles
)
1450 return ERROR_SUCCESS
;
1452 /* Windows also checks only the first item */
1453 bTrash
= (lpFileOp
->fFlags
& FOF_ALLOWUNDO
)
1454 && TRASH_CanTrashFile(flFrom
->feFiles
[0].szFullPath
);
1456 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (!bTrash
&& lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1457 if (!confirm_delete_list(lpFileOp
->hwnd
, lpFileOp
->fFlags
, bTrash
, flFrom
))
1459 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1463 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1466 fileEntry
= &flFrom
->feFiles
[i
];
1468 if (!IsAttribFile(fileEntry
->attributes
) &&
1469 (lpFileOp
->fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1475 if (TRASH_TrashFile(fileEntry
->szFullPath
))
1478 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1479 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1480 bDelete
= SHELL_ConfirmDialogW(lpFileOp
->hwnd
, ASK_CANT_TRASH_ITEM
, fileEntry
->szFullPath
, NULL
);
1486 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1491 /* delete the file or directory */
1492 if (IsAttribFile(fileEntry
->attributes
))
1493 bPathExists
= DeleteFileW(fileEntry
->szFullPath
);
1495 bPathExists
= SHELL_DeleteDirectoryW(lpFileOp
->hwnd
, fileEntry
->szFullPath
, FALSE
);
1499 DWORD err
= GetLastError();
1501 if (ERROR_FILE_NOT_FOUND
== err
)
1503 // This is a windows 2003 server specific value which ahs been removed.
1504 // Later versions of windows return ERROR_FILE_NOT_FOUND.
1514 return ERROR_SUCCESS
;
1517 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_ENTRY
*feFrom
, LPCWSTR szDestPath
)
1519 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
1520 SHFILEOPSTRUCTW fileOp
;
1522 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1524 if (IsDotDir(feFrom
->szFilename
))
1527 SHNotifyCreateDirectoryW(szDestPath
, NULL
);
1529 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1530 szFrom
[lstrlenW(szFrom
) + 1] = '\0';
1532 lstrcpyW(szTo
, szDestPath
);
1533 szTo
[lstrlenW(szDestPath
) + 1] = '\0';
1536 fileOp
.pFrom
= szFrom
;
1539 SHFileOperationW(&fileOp
);
1542 /* moves a file or directory to another directory */
1543 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1545 WCHAR szDestPath
[MAX_PATH
];
1547 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1549 if (IsAttribFile(feFrom
->attributes
))
1550 SHNotifyMoveFileW(feFrom
->szFullPath
, szDestPath
);
1551 else if (!(lpFileOp
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1552 move_dir_to_dir(lpFileOp
, feFrom
, szDestPath
);
1555 /* the FO_MOVE operation */
1556 static HRESULT
move_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
, const FILE_LIST
*flTo
)
1559 const FILE_ENTRY
*entryToMove
;
1560 const FILE_ENTRY
*fileDest
;
1562 if (!flFrom
->dwNumFiles
|| !flTo
->dwNumFiles
)
1563 return ERROR_CANCELLED
;
1565 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1566 flTo
->dwNumFiles
> 1 && flFrom
->dwNumFiles
> 1)
1568 return ERROR_CANCELLED
;
1571 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1572 !flFrom
->bAnyDirectories
&&
1573 flFrom
->dwNumFiles
> flTo
->dwNumFiles
)
1575 return ERROR_CANCELLED
;
1578 if (!PathFileExistsW(flTo
->feFiles
[0].szDirectory
))
1579 return ERROR_CANCELLED
;
1581 if ((lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1582 flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1584 return ERROR_CANCELLED
;
1587 fileDest
= &flTo
->feFiles
[0];
1588 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1590 entryToMove
= &flFrom
->feFiles
[i
];
1592 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
)
1593 fileDest
= &flTo
->feFiles
[i
];
1595 if (!PathFileExistsW(fileDest
->szDirectory
))
1596 return ERROR_CANCELLED
;
1598 if (fileDest
->bExists
&& IsAttribDir(fileDest
->attributes
))
1599 move_to_dir(lpFileOp
, entryToMove
, fileDest
);
1601 SHNotifyMoveFileW(entryToMove
->szFullPath
, fileDest
->szFullPath
);
1604 return ERROR_SUCCESS
;
1607 /* the FO_RENAME files */
1608 static HRESULT
rename_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
, const FILE_LIST
*flTo
)
1610 const FILE_ENTRY
*feFrom
;
1611 const FILE_ENTRY
*feTo
;
1613 if (flFrom
->dwNumFiles
!= 1)
1614 return ERROR_GEN_FAILURE
;
1616 if (flTo
->dwNumFiles
!= 1)
1617 return ERROR_CANCELLED
;
1619 feFrom
= &flFrom
->feFiles
[0];
1620 feTo
= &flTo
->feFiles
[0];
1622 /* fail if destination doesn't exist */
1623 if (!feFrom
->bExists
)
1624 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1626 /* fail if destination already exists */
1628 return ERROR_ALREADY_EXISTS
;
1630 return SHNotifyMoveFileW(feFrom
->szFullPath
, feTo
->szFullPath
);
1633 /* alert the user if an unsupported flag is used */
1634 static void check_flags(FILEOP_FLAGS fFlags
)
1636 WORD wUnsupportedFlags
= FOF_NO_CONNECTED_ELEMENTS
|
1637 FOF_NOCOPYSECURITYATTRIBS
| FOF_NORECURSEREPARSE
|
1638 FOF_RENAMEONCOLLISION
| FOF_WANTMAPPINGHANDLE
;
1640 if (fFlags
& wUnsupportedFlags
)
1641 FIXME("Unsupported flags: %04x\n", fFlags
);
1644 /*************************************************************************
1645 * SHFileOperationW [SHELL32.@]
1647 * See SHFileOperationA
1649 int WINAPI
SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp
)
1652 FILE_LIST flFrom
, flTo
;
1656 return ERROR_INVALID_PARAMETER
;
1658 check_flags(lpFileOp
->fFlags
);
1660 ZeroMemory(&flFrom
, sizeof(FILE_LIST
));
1661 ZeroMemory(&flTo
, sizeof(FILE_LIST
));
1663 if ((ret
= parse_file_list(&flFrom
, lpFileOp
->pFrom
)))
1666 if (lpFileOp
->wFunc
!= FO_DELETE
)
1667 parse_file_list(&flTo
, lpFileOp
->pTo
);
1669 ZeroMemory(&op
, sizeof(op
));
1671 op
.bManyItems
= (flFrom
.dwNumFiles
> 1);
1673 switch (lpFileOp
->wFunc
)
1676 ret
= copy_files(&op
, &flFrom
, &flTo
);
1679 ret
= delete_files(lpFileOp
, &flFrom
);
1682 ret
= move_files(lpFileOp
, &flFrom
, &flTo
);
1685 ret
= rename_files(lpFileOp
, &flFrom
, &flTo
);
1688 ret
= ERROR_INVALID_PARAMETER
;
1692 destroy_file_list(&flFrom
);
1694 if (lpFileOp
->wFunc
!= FO_DELETE
)
1695 destroy_file_list(&flTo
);
1697 if (ret
== ERROR_CANCELLED
)
1698 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1703 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1705 /*************************************************************************
1706 * SHFreeNameMappings [shell32.246]
1708 * Free the mapping handle returned by SHFileOperation if FOF_WANTSMAPPINGHANDLE
1712 * hNameMapping [I] handle to the name mappings used during renaming of files
1717 void WINAPI
SHFreeNameMappings(HANDLE hNameMapping
)
1721 int i
= SHDSA_GetItemCount((HDSA
)hNameMapping
) - 1;
1725 LPSHNAMEMAPPINGW lp
= (SHNAMEMAPPINGW
*)DSA_GetItemPtr((HDSA
)hNameMapping
, i
);
1727 SHFree(lp
->pszOldPath
);
1728 SHFree(lp
->pszNewPath
);
1730 DSA_Destroy((HDSA
)hNameMapping
);
1734 /*************************************************************************
1735 * SheGetDirA [SHELL32.@]
1737 * drive = 0: returns the current directory path
1738 * drive > 0: returns the current directory path of the specified drive
1739 * drive=1 -> A: drive=2 -> B: ...
1740 * returns 0 if successful
1742 EXTERN_C DWORD WINAPI
SheGetDirA(DWORD drive
, LPSTR buffer
)
1744 WCHAR org_path
[MAX_PATH
];
1748 /* change current directory to the specified drive */
1750 strcpy(drv_path
, "A:");
1751 drv_path
[0] += (char)drive
-1;
1753 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1755 SetCurrentDirectoryA(drv_path
);
1758 /* query current directory path of the specified drive */
1759 ret
= GetCurrentDirectoryA(MAX_PATH
, buffer
);
1761 /* back to the original drive */
1763 SetCurrentDirectoryW(org_path
);
1766 return GetLastError();
1771 /*************************************************************************
1772 * SheGetDirW [SHELL32.@]
1774 * drive = 0: returns the current directory path
1775 * drive > 0: returns the current directory path of the specified drive
1776 * drive=1 -> A: drive=2 -> B: ...
1777 * returns 0 if successful
1779 EXTERN_C DWORD WINAPI
SheGetDirW(DWORD drive
, LPWSTR buffer
)
1781 WCHAR org_path
[MAX_PATH
];
1785 /* change current directory to the specified drive */
1788 strcpy(drv_path
, "A:");
1789 drv_path
[0] += (char)drive
-1;
1791 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1793 SetCurrentDirectoryA(drv_path
);
1796 /* query current directory path of the specified drive */
1797 ret
= GetCurrentDirectoryW(MAX_PATH
, buffer
);
1799 /* back to the original drive */
1801 SetCurrentDirectoryW(org_path
);
1804 return GetLastError();
1809 /*************************************************************************
1810 * SheChangeDirA [SHELL32.@]
1812 * changes the current directory to the specified path
1813 * and returns 0 if successful
1815 EXTERN_C DWORD WINAPI
SheChangeDirA(LPSTR path
)
1817 if (SetCurrentDirectoryA(path
))
1820 return GetLastError();
1823 /*************************************************************************
1824 * SheChangeDirW [SHELL32.@]
1826 * changes the current directory to the specified path
1827 * and returns 0 if successful
1829 EXTERN_C DWORD WINAPI
SheChangeDirW(LPWSTR path
)
1831 if (SetCurrentDirectoryW(path
))
1834 return GetLastError();
1837 /*************************************************************************
1838 * IsNetDrive [SHELL32.66]
1840 EXTERN_C
int WINAPI
IsNetDrive(int drive
)
1843 strcpy(root
, "A:\\");
1844 root
[0] += (char)drive
;
1845 return (GetDriveTypeA(root
) == DRIVE_REMOTE
);
1849 /*************************************************************************
1850 * RealDriveType [SHELL32.524]
1852 EXTERN_C INT WINAPI
RealDriveType(INT drive
, BOOL bQueryNet
)
1854 char root
[] = "A:\\";
1855 root
[0] += (char)drive
;
1856 return GetDriveTypeA(root
);
1859 /***********************************************************************
1860 * SHPathPrepareForWriteW (SHELL32.@)
1862 EXTERN_C HRESULT WINAPI
SHPathPrepareForWriteW(HWND hwnd
, IUnknown
*modless
, LPCWSTR path
, DWORD flags
)
1869 WCHAR
* temppath
=NULL
;
1871 TRACE("%p %p %s 0x%80x\n", hwnd
, modless
, debugstr_w(path
), flags
);
1873 if (flags
& ~(SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
|SHPPFW_IGNOREFILENAME
))
1874 FIXME("unimplemented flags 0x%08x\n", flags
);
1876 /* cut off filename if necessary */
1877 if (flags
& SHPPFW_IGNOREFILENAME
)
1879 last_slash
= StrRChrW(path
, NULL
, '\\');
1880 if (last_slash
== NULL
)
1883 len
= last_slash
- path
+ 1;
1884 temppath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1886 return E_OUTOFMEMORY
;
1887 StrCpyNW(temppath
, path
, len
);
1888 realpath
= temppath
;
1895 /* try to create the directory if asked to */
1896 if (flags
& (SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
))
1898 if (flags
& SHPPFW_ASKDIRCREATE
)
1899 FIXME("treating SHPPFW_ASKDIRCREATE as SHPPFW_DIRCREATE\n");
1901 SHCreateDirectoryExW(0, realpath
, NULL
);
1904 /* check if we can access the directory */
1905 res
= GetFileAttributesW(realpath
);
1907 HeapFree(GetProcessHeap(), 0, temppath
);
1909 if (res
== INVALID_FILE_ATTRIBUTES
)
1911 err
= GetLastError();
1912 if (err
== ERROR_FILE_NOT_FOUND
)
1913 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
1914 return HRESULT_FROM_WIN32(err
);
1916 else if (res
& FILE_ATTRIBUTE_DIRECTORY
)
1919 return HRESULT_FROM_WIN32(ERROR_DIRECTORY
);
1922 /***********************************************************************
1923 * SHPathPrepareForWriteA (SHELL32.@)
1925 EXTERN_C HRESULT WINAPI
SHPathPrepareForWriteA(HWND hwnd
, IUnknown
*modless
, LPCSTR path
, DWORD flags
)
1927 WCHAR wpath
[MAX_PATH
];
1928 MultiByteToWideChar( CP_ACP
, 0, path
, -1, wpath
, MAX_PATH
);
1929 return SHPathPrepareForWriteW(hwnd
, modless
, wpath
, flags
);