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
26 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
28 #define IsAttrib(x, y) ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
29 #define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
30 #define IsAttribDir(x) IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
31 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
34 #define WM_FILE (WM_USER + 1)
35 #define TIMER_ID (100)
37 static const WCHAR wWildcardFile
[] = {'*',0};
38 static const WCHAR wWildcardChars
[] = {'*','?',0};
40 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
);
41 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
);
42 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
);
43 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
, BOOL isdir
);
44 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bFailIfExists
);
45 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
);
55 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
73 BOOL bAnyFromWildcard
;
86 }FILE_OPERATION_CONTEXT
;
89 /* Confirm dialogs with an optional "Yes To All" as used in file operations confirmations
91 static const WCHAR CONFIRM_MSG_PROP
[] = {'W','I','N','E','_','C','O','N','F','I','R','M',0};
93 struct confirm_msg_info
101 /* as some buttons may be hidden and the dialog height may change we may need
102 * to move the controls */
103 static void confirm_msg_move_button(HWND hDlg
, INT iId
, INT
*xPos
, INT yOffset
, BOOL bShow
)
105 HWND hButton
= GetDlgItem(hDlg
, iId
);
113 GetWindowRect(hButton
, &r
);
114 width
= r
.right
- r
.left
;
117 ScreenToClient(hDlg
, &pt
);
118 MoveWindow(hButton
, *xPos
- width
, pt
.y
- yOffset
, width
, r
.bottom
- r
.top
, FALSE
);
122 ShowWindow(hButton
, SW_HIDE
);
125 /* Note: we paint the text manually and don't use the static control to make
126 * sure the text has the same height as the one computed in WM_INITDIALOG
128 static INT_PTR
ConfirmMsgBox_Paint(HWND hDlg
)
135 BeginPaint(hDlg
, &ps
);
138 GetClientRect(GetDlgItem(hDlg
, IDC_YESTOALL_MESSAGE
), &r
);
139 /* this will remap the rect to dialog coords */
140 MapWindowPoints(GetDlgItem(hDlg
, IDC_YESTOALL_MESSAGE
), hDlg
, (LPPOINT
)&r
, 2);
141 hOldFont
= (HFONT
)SelectObject(hdc
, (HFONT
)SendDlgItemMessageW(hDlg
, IDC_YESTOALL_MESSAGE
, WM_GETFONT
, 0, 0));
142 DrawTextW(hdc
, (LPWSTR
)GetPropW(hDlg
, CONFIRM_MSG_PROP
), -1, &r
, DT_NOPREFIX
| DT_PATH_ELLIPSIS
| DT_WORDBREAK
);
143 SelectObject(hdc
, hOldFont
);
149 static INT_PTR
ConfirmMsgBox_Init(HWND hDlg
, LPARAM lParam
)
151 struct confirm_msg_info
*info
= (struct confirm_msg_info
*)lParam
;
158 SetWindowTextW(hDlg
, info
->lpszCaption
);
159 ShowWindow(GetDlgItem(hDlg
, IDC_YESTOALL_MESSAGE
), SW_HIDE
);
160 SetPropW(hDlg
, CONFIRM_MSG_PROP
, info
->lpszText
);
161 SendDlgItemMessageW(hDlg
, IDC_YESTOALL_ICON
, STM_SETICON
, (WPARAM
)info
->hIcon
, 0);
163 /* compute the text height and resize the dialog */
164 GetClientRect(GetDlgItem(hDlg
, IDC_YESTOALL_MESSAGE
), &r
);
167 hOldFont
= (HFONT
)SelectObject(hdc
, (HFONT
)SendDlgItemMessageW(hDlg
, IDC_YESTOALL_MESSAGE
, WM_GETFONT
, 0, 0));
168 DrawTextW(hdc
, info
->lpszText
, -1, &r
, DT_NOPREFIX
| DT_PATH_ELLIPSIS
| DT_WORDBREAK
| DT_CALCRECT
);
169 SelectObject(hdc
, hOldFont
);
171 yOffset
= min(yOffset
, 35); /* don't make the dialog too small */
172 ReleaseDC(hDlg
, hdc
);
174 GetClientRect(hDlg
, &r
);
176 GetWindowRect(hDlg
, &r
);
177 width
= r
.right
- r
.left
;
178 height
= r
.bottom
- r
.top
- yOffset
;
179 MoveWindow(hDlg
, (GetSystemMetrics(SM_CXSCREEN
) - width
)/2,
180 (GetSystemMetrics(SM_CYSCREEN
) - height
)/2, width
, height
, FALSE
);
182 confirm_msg_move_button(hDlg
, IDCANCEL
, &xPos
, yOffset
, info
->bYesToAll
);
183 confirm_msg_move_button(hDlg
, IDNO
, &xPos
, yOffset
, TRUE
);
184 confirm_msg_move_button(hDlg
, IDC_YESTOALL
, &xPos
, yOffset
, info
->bYesToAll
);
185 confirm_msg_move_button(hDlg
, IDYES
, &xPos
, yOffset
, TRUE
);
190 static INT_PTR CALLBACK
ConfirmMsgBoxProc(HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
195 return ConfirmMsgBox_Init(hDlg
, lParam
);
197 return ConfirmMsgBox_Paint(hDlg
);
199 EndDialog(hDlg
, wParam
);
202 EndDialog(hDlg
, IDCANCEL
);
208 int SHELL_ConfirmMsgBox(HWND hWnd
, LPWSTR lpszText
, LPWSTR lpszCaption
, HICON hIcon
, BOOL bYesToAll
)
210 struct confirm_msg_info info
;
212 info
.lpszText
= lpszText
;
213 info
.lpszCaption
= lpszCaption
;
215 info
.bYesToAll
= bYesToAll
;
216 return DialogBoxParamW(shell32_hInstance
, MAKEINTRESOURCEW(IDD_YESTOALL_MSGBOX
), hWnd
, ConfirmMsgBoxProc
, (LPARAM
)&info
);
219 /* confirmation dialogs content */
222 HINSTANCE hIconInstance
;
223 UINT icon_resource_id
;
224 UINT caption_resource_id
, text_resource_id
;
225 } SHELL_ConfirmIDstruc
;
227 static BOOL
SHELL_ConfirmIDs(int nKindOfDialog
, SHELL_ConfirmIDstruc
*ids
)
229 ids
->hIconInstance
= shell32_hInstance
;
230 switch (nKindOfDialog
)
232 case ASK_DELETE_FILE
:
233 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
234 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
235 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
238 case ASK_DELETE_FOLDER
:
239 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
240 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
241 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
244 case ASK_DELETE_MULTIPLE_ITEM
:
245 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
246 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
247 ids
->text_resource_id
= IDS_DELETEMULTIPLE_TEXT
;
251 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
252 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
253 ids
->text_resource_id
= IDS_TRASHITEM_TEXT
;
256 case ASK_TRASH_FOLDER
:
257 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
258 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
259 ids
->text_resource_id
= IDS_TRASHFOLDER_TEXT
;
262 case ASK_TRASH_MULTIPLE_ITEM
:
263 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
264 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
265 ids
->text_resource_id
= IDS_TRASHMULTIPLE_TEXT
;
268 case ASK_CANT_TRASH_ITEM
:
269 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
270 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
271 ids
->text_resource_id
= IDS_CANTTRASH_TEXT
;
274 case ASK_DELETE_SELECTED
:
275 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
276 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
277 ids
->text_resource_id
= IDS_DELETESELECTED_TEXT
;
280 case ASK_OVERWRITE_FILE
:
281 ids
->hIconInstance
= NULL
;
282 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
283 ids
->caption_resource_id
= IDS_OVERWRITEFILE_CAPTION
;
284 ids
->text_resource_id
= IDS_OVERWRITEFILE_TEXT
;
287 case ASK_OVERWRITE_FOLDER
:
288 ids
->hIconInstance
= NULL
;
289 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
290 ids
->caption_resource_id
= IDS_OVERWRITEFILE_CAPTION
;
291 ids
->text_resource_id
= IDS_OVERWRITEFOLDER_TEXT
;
295 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog
);
300 static BOOL
SHELL_ConfirmDialogW(HWND hWnd
, int nKindOfDialog
, LPCWSTR szDir
, FILE_OPERATION
*op
)
302 WCHAR szCaption
[255], szText
[255], szBuffer
[MAX_PATH
+ 256];
303 SHELL_ConfirmIDstruc ids
;
308 assert(nKindOfDialog
>= 0 && nKindOfDialog
< 32);
309 if (op
&& (op
->dwYesToAllMask
& (1 << nKindOfDialog
)))
312 if (!SHELL_ConfirmIDs(nKindOfDialog
, &ids
)) return FALSE
;
314 LoadStringW(shell32_hInstance
, ids
.caption_resource_id
, szCaption
, sizeof(szCaption
)/sizeof(WCHAR
));
315 LoadStringW(shell32_hInstance
, ids
.text_resource_id
, szText
, sizeof(szText
)/sizeof(WCHAR
));
317 args
[0] = (DWORD_PTR
)szDir
;
318 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
319 szText
, 0, 0, szBuffer
, sizeof(szBuffer
), (va_list*)args
);
320 hIcon
= LoadIconW(ids
.hIconInstance
, (LPWSTR
)MAKEINTRESOURCE(ids
.icon_resource_id
));
322 ret
= SHELL_ConfirmMsgBox(hWnd
, szBuffer
, szCaption
, hIcon
, op
&& op
->bManyItems
);
325 if (ret
== IDC_YESTOALL
)
327 op
->dwYesToAllMask
|= (1 << nKindOfDialog
);
331 op
->bCancelled
= TRUE
;
333 op
->req
->fAnyOperationsAborted
= TRUE
;
338 BOOL
SHELL_ConfirmYesNoW(HWND hWnd
, int nKindOfDialog
, LPCWSTR szDir
)
340 return SHELL_ConfirmDialogW(hWnd
, nKindOfDialog
, szDir
, NULL
);
343 static DWORD
SHELL32_AnsiToUnicodeBuf(LPCSTR aPath
, LPWSTR
*wPath
, DWORD minChars
)
345 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, NULL
, 0);
350 *wPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
353 MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, *wPath
, len
);
356 return E_OUTOFMEMORY
;
359 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath
)
361 HeapFree(GetProcessHeap(), 0, wPath
);
364 EXTERN_C HRESULT WINAPI
SHIsFileAvailableOffline(LPCWSTR path
, LPDWORD status
)
366 FIXME("(%s, %p) stub\n", debugstr_w(path
), status
);
370 /**************************************************************************
371 * SHELL_DeleteDirectory() [internal]
373 * Asks for confirmation when bShowUI is true and deletes the directory and
374 * all its subdirectories and files if necessary.
376 BOOL
SHELL_DeleteDirectoryW(HWND hwnd
, LPCWSTR pszDir
, BOOL bShowUI
)
380 WIN32_FIND_DATAW wfd
;
381 WCHAR szTemp
[MAX_PATH
];
383 /* Make sure the directory exists before eventually prompting the user */
384 PathCombineW(szTemp
, pszDir
, wWildcardFile
);
385 hFind
= FindFirstFileW(szTemp
, &wfd
);
386 if (hFind
== INVALID_HANDLE_VALUE
)
389 if (!bShowUI
|| (ret
= SHELL_ConfirmDialogW(hwnd
, ASK_DELETE_FOLDER
, pszDir
, NULL
)))
393 if (IsDotDir(wfd
.cFileName
))
395 PathCombineW(szTemp
, pszDir
, wfd
.cFileName
);
396 if (FILE_ATTRIBUTE_DIRECTORY
& wfd
.dwFileAttributes
)
397 ret
= SHELL_DeleteDirectoryW(hwnd
, szTemp
, FALSE
);
399 ret
= (SHNotifyDeleteFileW(szTemp
) == ERROR_SUCCESS
);
400 } while (ret
&& FindNextFileW(hFind
, &wfd
));
404 ret
= (SHNotifyRemoveDirectoryW(pszDir
) == ERROR_SUCCESS
);
408 /**************************************************************************
409 * Win32CreateDirectory [SHELL32.93]
411 * Creates a directory. Also triggers a change notify if one exists.
414 * path [I] path to directory to create
417 * TRUE if successful, FALSE otherwise
420 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
422 TRACE("(%s, %p)\n", debugstr_w(path
), sec
);
424 if (CreateDirectoryW(path
, sec
))
426 SHChangeNotify(SHCNE_MKDIR
, SHCNF_PATHW
, path
, NULL
);
427 return ERROR_SUCCESS
;
429 return GetLastError();
432 /**********************************************************************/
434 EXTERN_C BOOL WINAPI
Win32CreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
436 return (SHNotifyCreateDirectoryW(path
, sec
) == ERROR_SUCCESS
);
439 /************************************************************************
440 * Win32RemoveDirectory [SHELL32.94]
442 * Deletes a directory. Also triggers a change notify if one exists.
445 * path [I] path to directory to delete
448 * TRUE if successful, FALSE otherwise
450 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
)
453 TRACE("(%s)\n", debugstr_w(path
));
455 ret
= RemoveDirectoryW(path
);
458 /* Directory may be write protected */
459 DWORD dwAttr
= GetFileAttributesW(path
);
460 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
))
461 if (SetFileAttributesW(path
, dwAttr
& ~FILE_ATTRIBUTE_READONLY
))
462 ret
= RemoveDirectoryW(path
);
466 SHChangeNotify(SHCNE_RMDIR
, SHCNF_PATHW
, path
, NULL
);
467 return ERROR_SUCCESS
;
469 return GetLastError();
472 /***********************************************************************/
474 EXTERN_C BOOL WINAPI
Win32RemoveDirectoryW(LPCWSTR path
)
476 return (SHNotifyRemoveDirectoryW(path
) == ERROR_SUCCESS
);
479 /************************************************************************
480 * Win32DeleteFile [SHELL32.164]
482 * Deletes a file. Also triggers a change notify if one exists.
485 * path [I] path to file to delete
488 * TRUE if successful, FALSE otherwise
490 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
)
494 TRACE("(%s)\n", debugstr_w(path
));
496 ret
= DeleteFileW(path
);
499 /* File may be write protected or a system file */
500 DWORD dwAttr
= GetFileAttributesW(path
);
501 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
502 if (SetFileAttributesW(path
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
503 ret
= DeleteFileW(path
);
507 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, path
, NULL
);
508 return ERROR_SUCCESS
;
510 return GetLastError();
513 /***********************************************************************/
515 EXTERN_C DWORD WINAPI
Win32DeleteFileW(LPCWSTR path
)
517 return (SHNotifyDeleteFileW(path
) == ERROR_SUCCESS
);
520 /************************************************************************
521 * SHNotifyMoveFile [internal]
523 * Moves a file. Also triggers a change notify if one exists.
526 * src [I] path to source file to move
527 * dest [I] path to target file to move to
530 * ERORR_SUCCESS if successful
532 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
, BOOL isdir
)
536 TRACE("(%s %s)\n", debugstr_w(src
), debugstr_w(dest
));
538 ret
= MoveFileExW(src
, dest
, MOVEFILE_REPLACE_EXISTING
);
540 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
542 ret
= MoveFileW(src
, dest
);
548 dwAttr
= SHFindAttrW(dest
, FALSE
);
549 if (INVALID_FILE_ATTRIBUTES
== dwAttr
)
551 /* Source file may be write protected or a system file */
552 dwAttr
= GetFileAttributesW(src
);
553 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
554 if (SetFileAttributesW(src
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
555 ret
= MoveFileW(src
, dest
);
560 SHChangeNotify(isdir
? SHCNE_MKDIR
: SHCNE_CREATE
, SHCNF_PATHW
, dest
, NULL
);
561 SHChangeNotify(isdir
? SHCNE_RMDIR
: SHCNE_DELETE
, SHCNF_PATHW
, src
, NULL
);
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 * _ConvertAtoW helper function for SHFileOperationA
943 * Converts a string or string-list to unicode.
945 static DWORD
_ConvertAtoW(PCSTR strSrc
, PCWSTR
* pStrDest
, BOOL isList
)
949 // If the input is null, nothing to convert.
953 // Measure the total size, depending on if it's a zero-terminated list.
957 PCSTR tmpSrc
= strSrc
;
961 size
= lstrlenA(tmpSrc
) + 1;
968 sizeA
= lstrlenA(strSrc
) + 1;
971 // Measure the needed allocation size.
972 int sizeW
= MultiByteToWideChar(CP_ACP
, 0, strSrc
, sizeA
, NULL
, 0);
974 return GetLastError();
976 PWSTR strDest
= (PWSTR
) HeapAlloc(GetProcessHeap(), 0, sizeW
* sizeof(WCHAR
));
978 return ERROR_OUTOFMEMORY
;
980 int err
= MultiByteToWideChar(CP_ACP
, 0, strSrc
, sizeA
, strDest
, sizeW
);
983 HeapFree(GetProcessHeap(), 0, strDest
);
984 return GetLastError();
991 /*************************************************************************
992 * SHFileOperationA [SHELL32.@]
994 * Function to copy, move, delete and create one or more files with optional
998 * lpFileOp [I/O] pointer to a structure containing all the necessary information
1001 * Success: ERROR_SUCCESS.
1002 * Failure: ERROR_CANCELLED.
1007 int WINAPI
SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp
)
1009 int errCode
, retCode
;
1010 SHFILEOPSTRUCTW nFileOp
= { 0 };
1012 // Convert A information to W
1013 nFileOp
.hwnd
= lpFileOp
->hwnd
;
1014 nFileOp
.wFunc
= lpFileOp
->wFunc
;
1015 nFileOp
.fFlags
= lpFileOp
->fFlags
;
1017 errCode
= _ConvertAtoW(lpFileOp
->pFrom
, &nFileOp
.pFrom
, TRUE
);
1021 if (FO_DELETE
!= (nFileOp
.wFunc
& FO_MASK
))
1023 errCode
= _ConvertAtoW(lpFileOp
->pTo
, &nFileOp
.pTo
, TRUE
);
1028 if (nFileOp
.fFlags
& FOF_SIMPLEPROGRESS
)
1030 errCode
= _ConvertAtoW(lpFileOp
->lpszProgressTitle
, &nFileOp
.lpszProgressTitle
, FALSE
);
1035 // Call the actual function
1036 retCode
= SHFileOperationW(&nFileOp
);
1041 HeapFree(GetProcessHeap(), 0, (PVOID
) nFileOp
.pFrom
);
1043 HeapFree(GetProcessHeap(), 0, (PVOID
) nFileOp
.pTo
);
1044 if (nFileOp
.lpszProgressTitle
)
1045 HeapFree(GetProcessHeap(), 0, (PVOID
) nFileOp
.lpszProgressTitle
);
1049 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1050 SetLastError(errCode
);
1055 // Thankfully, starting with NT4 the name mappings are always unicode, so no need to convert.
1056 lpFileOp
->hNameMappings
= nFileOp
.hNameMappings
;
1057 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
1061 static void __inline
grow_list(FILE_LIST
*list
)
1063 FILE_ENTRY
*newx
= (FILE_ENTRY
*)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, list
->feFiles
,
1064 list
->num_alloc
* 2 * sizeof(*newx
) );
1065 list
->feFiles
= newx
;
1066 list
->num_alloc
*= 2;
1069 /* adds a file to the FILE_ENTRY struct
1071 static void add_file_to_entry(FILE_ENTRY
*feFile
, LPCWSTR szFile
)
1073 DWORD dwLen
= lstrlenW(szFile
) + 1;
1076 feFile
->szFullPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
1077 lstrcpyW(feFile
->szFullPath
, szFile
);
1079 ptr
= StrRChrW(szFile
, NULL
, '\\');
1082 dwLen
= ptr
- szFile
+ 1;
1083 feFile
->szDirectory
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
1084 lstrcpynW(feFile
->szDirectory
, szFile
, dwLen
);
1086 dwLen
= lstrlenW(feFile
->szFullPath
) - dwLen
+ 1;
1087 feFile
->szFilename
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
1088 lstrcpyW(feFile
->szFilename
, ptr
+ 1); /* skip over backslash */
1090 feFile
->bFromWildcard
= FALSE
;
1093 static LPWSTR
wildcard_to_file(LPCWSTR szWildCard
, LPCWSTR szFileName
)
1097 DWORD dwDirLen
, dwFullLen
;
1099 ptr
= StrRChrW(szWildCard
, NULL
, '\\');
1100 dwDirLen
= ptr
- szWildCard
+ 1;
1102 dwFullLen
= dwDirLen
+ lstrlenW(szFileName
) + 1;
1103 szFullPath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwFullLen
* sizeof(WCHAR
));
1105 lstrcpynW(szFullPath
, szWildCard
, dwDirLen
+ 1);
1106 lstrcatW(szFullPath
, szFileName
);
1111 static void parse_wildcard_files(FILE_LIST
*flList
, LPCWSTR szFile
, LPDWORD pdwListIndex
)
1113 WIN32_FIND_DATAW wfd
;
1114 HANDLE hFile
= FindFirstFileW(szFile
, &wfd
);
1119 if (hFile
== INVALID_HANDLE_VALUE
) return;
1121 for (res
= TRUE
; res
; res
= FindNextFileW(hFile
, &wfd
))
1123 if (IsDotDir(wfd
.cFileName
))
1126 if (*pdwListIndex
>= flList
->num_alloc
)
1127 grow_list( flList
);
1129 szFullPath
= wildcard_to_file(szFile
, wfd
.cFileName
);
1130 file
= &flList
->feFiles
[(*pdwListIndex
)++];
1131 add_file_to_entry(file
, szFullPath
);
1132 file
->bFromWildcard
= TRUE
;
1133 file
->attributes
= wfd
.dwFileAttributes
;
1135 if (IsAttribDir(file
->attributes
))
1136 flList
->bAnyDirectories
= TRUE
;
1138 HeapFree(GetProcessHeap(), 0, szFullPath
);
1144 /* takes the null-separated file list and fills out the FILE_LIST */
1145 static HRESULT
parse_file_list(FILE_LIST
*flList
, LPCWSTR szFiles
)
1147 LPCWSTR ptr
= szFiles
;
1148 WCHAR szCurFile
[MAX_PATH
];
1152 return ERROR_INVALID_PARAMETER
;
1154 flList
->bAnyFromWildcard
= FALSE
;
1155 flList
->bAnyDirectories
= FALSE
;
1156 flList
->bAnyDontExist
= FALSE
;
1157 flList
->num_alloc
= 32;
1158 flList
->dwNumFiles
= 0;
1162 return ERROR_ACCESS_DENIED
;
1164 flList
->feFiles
= (FILE_ENTRY
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1165 flList
->num_alloc
* sizeof(FILE_ENTRY
));
1169 if (i
>= flList
->num_alloc
) grow_list( flList
);
1171 /* change relative to absolute path */
1172 if (PathIsRelativeW(ptr
))
1174 GetCurrentDirectoryW(MAX_PATH
, szCurFile
);
1175 PathCombineW(szCurFile
, szCurFile
, ptr
);
1176 flList
->feFiles
[i
].bFromRelative
= TRUE
;
1180 lstrcpyW(szCurFile
, ptr
);
1181 flList
->feFiles
[i
].bFromRelative
= FALSE
;
1184 /* parse wildcard files if they are in the filename */
1185 if (StrPBrkW(szCurFile
, wWildcardChars
))
1187 parse_wildcard_files(flList
, szCurFile
, &i
);
1188 flList
->bAnyFromWildcard
= TRUE
;
1193 FILE_ENTRY
*file
= &flList
->feFiles
[i
];
1194 add_file_to_entry(file
, szCurFile
);
1195 file
->attributes
= GetFileAttributesW( file
->szFullPath
);
1196 file
->bExists
= (file
->attributes
!= INVALID_FILE_ATTRIBUTES
);
1199 flList
->bAnyDontExist
= TRUE
;
1201 if (IsAttribDir(file
->attributes
))
1202 flList
->bAnyDirectories
= TRUE
;
1205 /* advance to the next string */
1206 ptr
+= lstrlenW(ptr
) + 1;
1209 flList
->dwNumFiles
= i
;
1214 /* free the FILE_LIST */
1215 static void destroy_file_list(FILE_LIST
*flList
)
1219 if (!flList
|| !flList
->feFiles
)
1222 for (i
= 0; i
< flList
->dwNumFiles
; i
++)
1224 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szDirectory
);
1225 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFilename
);
1226 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFullPath
);
1229 HeapFree(GetProcessHeap(), 0, flList
->feFiles
);
1232 static void copy_dir_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, LPCWSTR szDestPath
)
1234 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
1235 SHFILEOPSTRUCTW fileOp
;
1237 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1239 if (IsDotDir(feFrom
->szFilename
))
1242 if (PathFileExistsW(szDestPath
))
1243 PathCombineW(szTo
, szDestPath
, feFrom
->szFilename
);
1245 lstrcpyW(szTo
, szDestPath
);
1247 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
))
1249 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FOLDER
, feFrom
->szFilename
, op
))
1251 /* Vista returns an ERROR_CANCELLED even if user pressed "No" */
1252 if (!op
->bManyItems
)
1253 op
->bCancelled
= TRUE
;
1258 szTo
[lstrlenW(szTo
) + 1] = '\0';
1259 SHNotifyCreateDirectoryW(szTo
, NULL
);
1261 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1262 szFrom
[lstrlenW(szFrom
) + 1] = '\0';
1265 fileOp
.pFrom
= szFrom
;
1267 fileOp
.fFlags
&= ~FOF_MULTIDESTFILES
; /* we know we're copying to one dir */
1269 /* Don't ask the user about overwriting files when he accepted to overwrite the
1270 folder. FIXME: this is not exactly what Windows does - e.g. there would be
1271 an additional confirmation for a nested folder */
1272 fileOp
.fFlags
|= FOF_NOCONFIRMATION
;
1274 SHFileOperationW(&fileOp
);
1277 static BOOL
copy_file_to_file(FILE_OPERATION
*op
, const WCHAR
*szFrom
, const WCHAR
*szTo
)
1279 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
))
1281 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FILE
, PathFindFileNameW(szTo
), op
))
1285 return SHNotifyCopyFileW(szFrom
, szTo
, FALSE
) == 0;
1288 /* copy a file or directory to another directory */
1289 static void copy_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1291 if (!PathFileExistsW(feTo
->szFullPath
))
1292 SHNotifyCreateDirectoryW(feTo
->szFullPath
, NULL
);
1294 if (IsAttribFile(feFrom
->attributes
))
1296 WCHAR szDestPath
[MAX_PATH
];
1298 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1299 copy_file_to_file(op
, feFrom
->szFullPath
, szDestPath
);
1301 else if (!(op
->req
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1302 copy_dir_to_dir(op
, feFrom
, feTo
->szFullPath
);
1305 static void create_dest_dirs(LPCWSTR szDestDir
)
1307 WCHAR dir
[MAX_PATH
];
1308 LPCWSTR ptr
= StrChrW(szDestDir
, '\\');
1310 /* make sure all directories up to last one are created */
1311 while (ptr
&& (ptr
= StrChrW(ptr
+ 1, '\\')))
1313 lstrcpynW(dir
, szDestDir
, ptr
- szDestDir
+ 1);
1315 if (!PathFileExistsW(dir
))
1316 SHNotifyCreateDirectoryW(dir
, NULL
);
1319 /* create last directory */
1320 if (!PathFileExistsW(szDestDir
))
1321 SHNotifyCreateDirectoryW(szDestDir
, NULL
);
1324 /* the FO_COPY operation */
1325 static HRESULT
copy_files(FILE_OPERATION
*op
, const FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
1328 const FILE_ENTRY
*entryToCopy
;
1329 const FILE_ENTRY
*fileDest
= &flTo
->feFiles
[0];
1331 if (flFrom
->bAnyDontExist
)
1332 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1334 if (flTo
->dwNumFiles
== 0)
1336 /* If the destination is empty, SHFileOperation should use the current directory */
1337 WCHAR curdir
[MAX_PATH
+1];
1339 GetCurrentDirectoryW(MAX_PATH
, curdir
);
1340 curdir
[lstrlenW(curdir
)+1] = 0;
1342 destroy_file_list(flTo
);
1343 ZeroMemory(flTo
, sizeof(FILE_LIST
));
1344 parse_file_list(flTo
, curdir
);
1345 fileDest
= &flTo
->feFiles
[0];
1348 if (op
->req
->fFlags
& FOF_MULTIDESTFILES
)
1350 if (flFrom
->bAnyFromWildcard
)
1351 return ERROR_CANCELLED
;
1353 if (flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1355 if (flFrom
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1356 return ERROR_CANCELLED
;
1358 /* Free all but the first entry. */
1359 for (i
= 1; i
< flTo
->dwNumFiles
; i
++)
1361 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szDirectory
);
1362 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szFilename
);
1363 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szFullPath
);
1366 flTo
->dwNumFiles
= 1;
1368 else if (IsAttribDir(fileDest
->attributes
))
1370 for (i
= 1; i
< flTo
->dwNumFiles
; i
++)
1371 if (!IsAttribDir(flTo
->feFiles
[i
].attributes
) ||
1372 !IsAttribDir(flFrom
->feFiles
[i
].attributes
))
1374 return ERROR_CANCELLED
;
1378 else if (flFrom
->dwNumFiles
!= 1)
1380 if (flTo
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1381 return ERROR_CANCELLED
;
1383 if (PathFileExistsW(fileDest
->szFullPath
) &&
1384 IsAttribFile(fileDest
->attributes
))
1386 return ERROR_CANCELLED
;
1389 if (flTo
->dwNumFiles
== 1 && fileDest
->bFromRelative
&&
1390 !PathFileExistsW(fileDest
->szFullPath
))
1392 return ERROR_CANCELLED
;
1396 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1398 entryToCopy
= &flFrom
->feFiles
[i
];
1400 if ((op
->req
->fFlags
& FOF_MULTIDESTFILES
) &&
1401 flTo
->dwNumFiles
> 1)
1403 fileDest
= &flTo
->feFiles
[i
];
1406 if (IsAttribDir(entryToCopy
->attributes
) &&
1407 !lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szDirectory
))
1409 return ERROR_SUCCESS
;
1412 create_dest_dirs(fileDest
->szDirectory
);
1414 if (!lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1416 if (IsAttribFile(entryToCopy
->attributes
))
1417 return ERROR_NO_MORE_SEARCH_HANDLES
;
1419 return ERROR_SUCCESS
;
1422 if ((flFrom
->dwNumFiles
> 1 && flTo
->dwNumFiles
== 1) ||
1423 IsAttribDir(fileDest
->attributes
))
1425 copy_to_dir(op
, entryToCopy
, fileDest
);
1427 else if (IsAttribDir(entryToCopy
->attributes
))
1429 copy_dir_to_dir(op
, entryToCopy
, fileDest
->szFullPath
);
1433 if (!copy_file_to_file(op
, entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1435 op
->req
->fAnyOperationsAborted
= TRUE
;
1436 return ERROR_CANCELLED
;
1440 /* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
1442 return ERROR_CANCELLED
;
1445 /* Vista return code. On XP if the used pressed "No" for the last item,
1446 * ERROR_ARENA_TRASHED would be returned */
1447 return ERROR_SUCCESS
;
1450 static BOOL
confirm_delete_list(HWND hWnd
, DWORD fFlags
, BOOL fTrash
, const FILE_LIST
*flFrom
)
1452 if (flFrom
->dwNumFiles
> 1)
1455 const WCHAR format
[] = {'%','d',0};
1457 wnsprintfW(tmp
, sizeof(tmp
)/sizeof(tmp
[0]), format
, flFrom
->dwNumFiles
);
1458 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_MULTIPLE_ITEM
:ASK_DELETE_MULTIPLE_ITEM
), tmp
, NULL
);
1462 const FILE_ENTRY
*fileEntry
= &flFrom
->feFiles
[0];
1464 if (IsAttribFile(fileEntry
->attributes
))
1465 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FILE
:ASK_DELETE_FILE
), fileEntry
->szFullPath
, NULL
);
1466 else if (!(fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1467 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FOLDER
:ASK_DELETE_FOLDER
), fileEntry
->szFullPath
, NULL
);
1472 /* the FO_DELETE operation */
1473 static HRESULT
delete_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
)
1475 const FILE_ENTRY
*fileEntry
;
1480 if (!flFrom
->dwNumFiles
)
1481 return ERROR_SUCCESS
;
1483 /* Windows also checks only the first item */
1484 bTrash
= (lpFileOp
->fFlags
& FOF_ALLOWUNDO
)
1485 && TRASH_CanTrashFile(flFrom
->feFiles
[0].szFullPath
);
1487 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (!bTrash
&& lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1488 if (!confirm_delete_list(lpFileOp
->hwnd
, lpFileOp
->fFlags
, bTrash
, flFrom
))
1490 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1494 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1497 fileEntry
= &flFrom
->feFiles
[i
];
1499 if (!IsAttribFile(fileEntry
->attributes
) &&
1500 (lpFileOp
->fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1506 if (TRASH_TrashFile(fileEntry
->szFullPath
))
1508 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, fileEntry
->szFullPath
, NULL
);
1512 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1513 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1514 bDelete
= SHELL_ConfirmDialogW(lpFileOp
->hwnd
, ASK_CANT_TRASH_ITEM
, fileEntry
->szFullPath
, NULL
);
1520 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1525 /* delete the file or directory */
1526 if (IsAttribFile(fileEntry
->attributes
))
1528 bPathExists
= DeleteFileW(fileEntry
->szFullPath
);
1529 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, fileEntry
->szFullPath
, NULL
);
1532 bPathExists
= SHELL_DeleteDirectoryW(lpFileOp
->hwnd
, fileEntry
->szFullPath
, FALSE
);
1536 DWORD err
= GetLastError();
1538 if (ERROR_FILE_NOT_FOUND
== err
)
1540 // This is a windows 2003 server specific value which ahs been removed.
1541 // Later versions of windows return ERROR_FILE_NOT_FOUND.
1551 return ERROR_SUCCESS
;
1554 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_ENTRY
*feFrom
, LPCWSTR szDestPath
)
1556 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
1557 SHFILEOPSTRUCTW fileOp
;
1559 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1561 if (IsDotDir(feFrom
->szFilename
))
1564 SHNotifyCreateDirectoryW(szDestPath
, NULL
);
1566 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1567 szFrom
[lstrlenW(szFrom
) + 1] = '\0';
1569 lstrcpyW(szTo
, szDestPath
);
1570 szTo
[lstrlenW(szDestPath
) + 1] = '\0';
1573 fileOp
.pFrom
= szFrom
;
1576 SHFileOperationW(&fileOp
);
1579 /* moves a file or directory to another directory */
1580 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1582 WCHAR szDestPath
[MAX_PATH
];
1584 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1586 if (IsAttribFile(feFrom
->attributes
))
1587 SHNotifyMoveFileW(feFrom
->szFullPath
, szDestPath
, FALSE
);
1588 else if (!(lpFileOp
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1589 move_dir_to_dir(lpFileOp
, feFrom
, szDestPath
);
1592 /* the FO_MOVE operation */
1593 static HRESULT
move_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
, const FILE_LIST
*flTo
)
1596 const FILE_ENTRY
*entryToMove
;
1597 const FILE_ENTRY
*fileDest
;
1599 if (!flFrom
->dwNumFiles
|| !flTo
->dwNumFiles
)
1600 return ERROR_CANCELLED
;
1602 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1603 flTo
->dwNumFiles
> 1 && flFrom
->dwNumFiles
> 1)
1605 return ERROR_CANCELLED
;
1608 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1609 !flFrom
->bAnyDirectories
&&
1610 flFrom
->dwNumFiles
> flTo
->dwNumFiles
)
1612 return ERROR_CANCELLED
;
1615 if (!PathFileExistsW(flTo
->feFiles
[0].szDirectory
))
1616 return ERROR_CANCELLED
;
1618 if ((lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1619 flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1621 return ERROR_CANCELLED
;
1624 fileDest
= &flTo
->feFiles
[0];
1625 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1627 entryToMove
= &flFrom
->feFiles
[i
];
1629 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
)
1630 fileDest
= &flTo
->feFiles
[i
];
1632 if (!PathFileExistsW(fileDest
->szDirectory
))
1633 return ERROR_CANCELLED
;
1635 if (fileDest
->bExists
&& IsAttribDir(fileDest
->attributes
))
1636 move_to_dir(lpFileOp
, entryToMove
, fileDest
);
1638 SHNotifyMoveFileW(entryToMove
->szFullPath
, fileDest
->szFullPath
, IsAttribDir(entryToMove
->attributes
));
1641 return ERROR_SUCCESS
;
1644 /* the FO_RENAME files */
1645 static HRESULT
rename_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
, const FILE_LIST
*flTo
)
1647 const FILE_ENTRY
*feFrom
;
1648 const FILE_ENTRY
*feTo
;
1650 if (flFrom
->dwNumFiles
!= 1)
1651 return ERROR_GEN_FAILURE
;
1653 if (flTo
->dwNumFiles
!= 1)
1654 return ERROR_CANCELLED
;
1656 feFrom
= &flFrom
->feFiles
[0];
1657 feTo
= &flTo
->feFiles
[0];
1659 /* fail if destination doesn't exist */
1660 if (!feFrom
->bExists
)
1661 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1663 /* fail if destination already exists */
1665 return ERROR_ALREADY_EXISTS
;
1667 return SHNotifyMoveFileW(feFrom
->szFullPath
, feTo
->szFullPath
, IsAttribDir(feFrom
->attributes
));
1670 /* alert the user if an unsupported flag is used */
1671 static void check_flags(FILEOP_FLAGS fFlags
)
1673 WORD wUnsupportedFlags
= FOF_NO_CONNECTED_ELEMENTS
|
1674 FOF_NOCOPYSECURITYATTRIBS
| FOF_NORECURSEREPARSE
|
1675 FOF_RENAMEONCOLLISION
| FOF_WANTMAPPINGHANDLE
;
1677 if (fFlags
& wUnsupportedFlags
)
1678 FIXME("Unsupported flags: %04x\n", fFlags
);
1681 /*************************************************************************
1682 * SHFileOperationW [SHELL32.@]
1684 * See SHFileOperationA
1686 int WINAPI
SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp
)
1689 FILE_LIST flFrom
, flTo
;
1693 return ERROR_INVALID_PARAMETER
;
1695 check_flags(lpFileOp
->fFlags
);
1697 ZeroMemory(&flFrom
, sizeof(FILE_LIST
));
1698 ZeroMemory(&flTo
, sizeof(FILE_LIST
));
1700 if ((ret
= parse_file_list(&flFrom
, lpFileOp
->pFrom
)))
1703 if (lpFileOp
->wFunc
!= FO_DELETE
)
1704 parse_file_list(&flTo
, lpFileOp
->pTo
);
1706 ZeroMemory(&op
, sizeof(op
));
1708 op
.bManyItems
= (flFrom
.dwNumFiles
> 1);
1710 switch (lpFileOp
->wFunc
)
1713 ret
= copy_files(&op
, &flFrom
, &flTo
);
1716 ret
= delete_files(lpFileOp
, &flFrom
);
1719 ret
= move_files(lpFileOp
, &flFrom
, &flTo
);
1722 ret
= rename_files(lpFileOp
, &flFrom
, &flTo
);
1725 ret
= ERROR_INVALID_PARAMETER
;
1729 destroy_file_list(&flFrom
);
1731 if (lpFileOp
->wFunc
!= FO_DELETE
)
1732 destroy_file_list(&flTo
);
1734 if (ret
== ERROR_CANCELLED
)
1735 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1740 // Used by SHFreeNameMappings
1741 static int CALLBACK
_DestroyCallback(void *p
, void *pData
)
1743 LPSHNAMEMAPPINGW lp
= (SHNAMEMAPPINGW
*)p
;
1745 SHFree(lp
->pszOldPath
);
1746 SHFree(lp
->pszNewPath
);
1751 /*************************************************************************
1752 * SHFreeNameMappings [shell32.246]
1754 * Free the mapping handle returned by SHFileOperation if FOF_WANTSMAPPINGHANDLE
1758 * hNameMapping [I] handle to the name mappings used during renaming of files
1763 void WINAPI
SHFreeNameMappings(HANDLE hNameMapping
)
1767 DSA_DestroyCallback((HDSA
) hNameMapping
, _DestroyCallback
, NULL
);
1771 /*************************************************************************
1772 * SheGetDirA [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
SheGetDirA(DWORD drive
, LPSTR buffer
)
1781 WCHAR org_path
[MAX_PATH
];
1785 /* change current directory to the specified drive */
1787 strcpy(drv_path
, "A:");
1788 drv_path
[0] += (char)drive
-1;
1790 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1792 SetCurrentDirectoryA(drv_path
);
1795 /* query current directory path of the specified drive */
1796 ret
= GetCurrentDirectoryA(MAX_PATH
, buffer
);
1798 /* back to the original drive */
1800 SetCurrentDirectoryW(org_path
);
1803 return GetLastError();
1808 /*************************************************************************
1809 * SheGetDirW [SHELL32.@]
1811 * drive = 0: returns the current directory path
1812 * drive > 0: returns the current directory path of the specified drive
1813 * drive=1 -> A: drive=2 -> B: ...
1814 * returns 0 if successful
1816 EXTERN_C DWORD WINAPI
SheGetDirW(DWORD drive
, LPWSTR buffer
)
1818 WCHAR org_path
[MAX_PATH
];
1822 /* change current directory to the specified drive */
1825 strcpy(drv_path
, "A:");
1826 drv_path
[0] += (char)drive
-1;
1828 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1830 SetCurrentDirectoryA(drv_path
);
1833 /* query current directory path of the specified drive */
1834 ret
= GetCurrentDirectoryW(MAX_PATH
, buffer
);
1836 /* back to the original drive */
1838 SetCurrentDirectoryW(org_path
);
1841 return GetLastError();
1846 /*************************************************************************
1847 * SheChangeDirA [SHELL32.@]
1849 * changes the current directory to the specified path
1850 * and returns 0 if successful
1852 EXTERN_C DWORD WINAPI
SheChangeDirA(LPSTR path
)
1854 if (SetCurrentDirectoryA(path
))
1857 return GetLastError();
1860 /*************************************************************************
1861 * SheChangeDirW [SHELL32.@]
1863 * changes the current directory to the specified path
1864 * and returns 0 if successful
1866 EXTERN_C DWORD WINAPI
SheChangeDirW(LPWSTR path
)
1868 if (SetCurrentDirectoryW(path
))
1871 return GetLastError();
1874 /*************************************************************************
1875 * IsNetDrive [SHELL32.66]
1877 EXTERN_C
int WINAPI
IsNetDrive(int drive
)
1880 strcpy(root
, "A:\\");
1881 root
[0] += (char)drive
;
1882 return (GetDriveTypeA(root
) == DRIVE_REMOTE
);
1886 /*************************************************************************
1887 * RealDriveType [SHELL32.524]
1889 EXTERN_C INT WINAPI
RealDriveType(INT drive
, BOOL bQueryNet
)
1891 char root
[] = "A:\\";
1892 root
[0] += (char)drive
;
1893 return GetDriveTypeA(root
);
1896 /***********************************************************************
1897 * SHPathPrepareForWriteW (SHELL32.@)
1899 EXTERN_C HRESULT WINAPI
SHPathPrepareForWriteW(HWND hwnd
, IUnknown
*modless
, LPCWSTR path
, DWORD flags
)
1906 WCHAR
* temppath
=NULL
;
1908 TRACE("%p %p %s 0x%80x\n", hwnd
, modless
, debugstr_w(path
), flags
);
1910 if (flags
& ~(SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
|SHPPFW_IGNOREFILENAME
))
1911 FIXME("unimplemented flags 0x%08x\n", flags
);
1913 /* cut off filename if necessary */
1914 if (flags
& SHPPFW_IGNOREFILENAME
)
1916 last_slash
= StrRChrW(path
, NULL
, '\\');
1917 if (last_slash
== NULL
)
1920 len
= last_slash
- path
+ 1;
1921 temppath
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1923 return E_OUTOFMEMORY
;
1924 StrCpyNW(temppath
, path
, len
);
1925 realpath
= temppath
;
1932 /* try to create the directory if asked to */
1933 if (flags
& (SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
))
1935 if (flags
& SHPPFW_ASKDIRCREATE
)
1936 FIXME("treating SHPPFW_ASKDIRCREATE as SHPPFW_DIRCREATE\n");
1938 SHCreateDirectoryExW(0, realpath
, NULL
);
1941 /* check if we can access the directory */
1942 res
= GetFileAttributesW(realpath
);
1944 HeapFree(GetProcessHeap(), 0, temppath
);
1946 if (res
== INVALID_FILE_ATTRIBUTES
)
1948 err
= GetLastError();
1949 if (err
== ERROR_FILE_NOT_FOUND
)
1950 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
1951 return HRESULT_FROM_WIN32(err
);
1953 else if (res
& FILE_ATTRIBUTE_DIRECTORY
)
1956 return HRESULT_FROM_WIN32(ERROR_DIRECTORY
);
1959 /***********************************************************************
1960 * SHPathPrepareForWriteA (SHELL32.@)
1962 EXTERN_C HRESULT WINAPI
SHPathPrepareForWriteA(HWND hwnd
, IUnknown
*modless
, LPCSTR path
, DWORD flags
)
1964 WCHAR wpath
[MAX_PATH
];
1965 MultiByteToWideChar( CP_ACP
, 0, path
, -1, wpath
, MAX_PATH
);
1966 return SHPathPrepareForWriteW(hwnd
, modless
, wpath
, flags
);