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
28 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
30 #define IsAttrib(x, y) ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
31 #define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
32 #define IsAttribDir(x) IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
33 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
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
);
44 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bFailIfExists
);
45 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
);
55 /* Confirm dialogs with an optional "Yes To All" as used in file operations confirmations
57 static const WCHAR CONFIRM_MSG_PROP
[] = {'W','I','N','E','_','C','O','N','F','I','R','M',0};
59 struct confirm_msg_info
67 /* as some buttons may be hidden and the dialog height may change we may need
68 * to move the controls */
69 static void confirm_msg_move_button(HWND hDlg
, INT iId
, INT
*xPos
, INT yOffset
, BOOL bShow
)
71 HWND hButton
= GetDlgItem(hDlg
, iId
);
78 GetWindowRect(hButton
, &r
);
79 width
= r
.right
- r
.left
;
82 ScreenToClient(hDlg
, &pt
);
83 MoveWindow(hButton
, *xPos
- width
, pt
.y
- yOffset
, width
, r
.bottom
- r
.top
, FALSE
);
87 ShowWindow(hButton
, SW_HIDE
);
90 /* Note: we paint the text manually and don't use the static control to make
91 * sure the text has the same height as the one computed in WM_INITDIALOG
93 static INT_PTR
ConfirmMsgBox_Paint(HWND hDlg
)
100 BeginPaint(hDlg
, &ps
);
103 GetClientRect(GetDlgItem(hDlg
, IDD_MESSAGE
), &r
);
104 /* this will remap the rect to dialog coords */
105 MapWindowPoints(GetDlgItem(hDlg
, IDD_MESSAGE
), hDlg
, (LPPOINT
)&r
, 2);
106 hOldFont
= SelectObject(hdc
, (HFONT
)SendDlgItemMessageW(hDlg
, IDD_MESSAGE
, WM_GETFONT
, 0, 0));
107 DrawTextW(hdc
, GetPropW(hDlg
, CONFIRM_MSG_PROP
), -1, &r
, DT_NOPREFIX
| DT_PATH_ELLIPSIS
| DT_WORDBREAK
);
108 SelectObject(hdc
, hOldFont
);
113 static INT_PTR
ConfirmMsgBox_Init(HWND hDlg
, LPARAM lParam
)
115 struct confirm_msg_info
*info
= (struct confirm_msg_info
*)lParam
;
122 SetWindowTextW(hDlg
, info
->lpszCaption
);
123 ShowWindow(GetDlgItem(hDlg
, IDD_MESSAGE
), SW_HIDE
);
124 SetPropW(hDlg
, CONFIRM_MSG_PROP
, info
->lpszText
);
125 SendDlgItemMessageW(hDlg
, IDD_ICON
, STM_SETICON
, (WPARAM
)info
->hIcon
, 0);
127 /* compute the text height and resize the dialog */
128 GetClientRect(GetDlgItem(hDlg
, IDD_MESSAGE
), &r
);
131 hOldFont
= SelectObject(hdc
, (HFONT
)SendDlgItemMessageW(hDlg
, IDD_MESSAGE
, WM_GETFONT
, 0, 0));
132 DrawTextW(hdc
, info
->lpszText
, -1, &r
, DT_NOPREFIX
| DT_PATH_ELLIPSIS
| DT_WORDBREAK
| DT_CALCRECT
);
133 SelectObject(hdc
, hOldFont
);
135 yOffset
= min(yOffset
, 35); /* don't make the dialog too small */
136 ReleaseDC(hDlg
, hdc
);
138 GetClientRect(hDlg
, &r
);
140 GetWindowRect(hDlg
, &r
);
141 width
= r
.right
- r
.left
;
142 height
= r
.bottom
- r
.top
- yOffset
;
143 MoveWindow(hDlg
, (GetSystemMetrics(SM_CXSCREEN
) - width
)/2,
144 (GetSystemMetrics(SM_CYSCREEN
) - height
)/2, width
, height
, FALSE
);
146 confirm_msg_move_button(hDlg
, IDCANCEL
, &xPos
, yOffset
, info
->bYesToAll
);
147 confirm_msg_move_button(hDlg
, IDNO
, &xPos
, yOffset
, TRUE
);
148 confirm_msg_move_button(hDlg
, IDD_YESTOALL
, &xPos
, yOffset
, info
->bYesToAll
);
149 confirm_msg_move_button(hDlg
, IDYES
, &xPos
, yOffset
, TRUE
);
153 static INT_PTR CALLBACK
ConfirmMsgBoxProc(HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
158 return ConfirmMsgBox_Init(hDlg
, lParam
);
160 return ConfirmMsgBox_Paint(hDlg
);
162 EndDialog(hDlg
, wParam
);
165 EndDialog(hDlg
, IDCANCEL
);
171 int SHELL_ConfirmMsgBox(HWND hWnd
, LPWSTR lpszText
, LPWSTR lpszCaption
, HICON hIcon
, BOOL bYesToAll
)
173 static const WCHAR wszTemplate
[] = {'S','H','E','L','L','_','Y','E','S','T','O','A','L','L','_','M','S','G','B','O','X',0};
174 struct confirm_msg_info info
;
176 info
.lpszText
= lpszText
;
177 info
.lpszCaption
= lpszCaption
;
179 info
.bYesToAll
= bYesToAll
;
180 return DialogBoxParamW(shell32_hInstance
, wszTemplate
, hWnd
, ConfirmMsgBoxProc
, (LPARAM
)&info
);
183 /* confirmation dialogs content */
186 HINSTANCE hIconInstance
;
187 UINT icon_resource_id
;
188 UINT caption_resource_id
, text_resource_id
;
189 } SHELL_ConfirmIDstruc
;
191 static BOOL
SHELL_ConfirmIDs(int nKindOfDialog
, SHELL_ConfirmIDstruc
*ids
)
193 ids
->hIconInstance
= shell32_hInstance
;
194 switch (nKindOfDialog
) {
195 case ASK_DELETE_FILE
:
196 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
197 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
198 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
200 case ASK_DELETE_FOLDER
:
201 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
202 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
203 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
205 case ASK_DELETE_MULTIPLE_ITEM
:
206 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
207 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
208 ids
->text_resource_id
= IDS_DELETEMULTIPLE_TEXT
;
211 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
212 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
213 ids
->text_resource_id
= IDS_TRASHITEM_TEXT
;
215 case ASK_TRASH_FOLDER
:
216 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
217 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
218 ids
->text_resource_id
= IDS_TRASHFOLDER_TEXT
;
220 case ASK_TRASH_MULTIPLE_ITEM
:
221 ids
->icon_resource_id
= IDI_SHELL_TRASH_FILE
;
222 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
223 ids
->text_resource_id
= IDS_TRASHMULTIPLE_TEXT
;
225 case ASK_CANT_TRASH_ITEM
:
226 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
227 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
228 ids
->text_resource_id
= IDS_CANTTRASH_TEXT
;
230 case ASK_DELETE_SELECTED
:
231 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
232 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
233 ids
->text_resource_id
= IDS_DELETESELECTED_TEXT
;
235 case ASK_OVERWRITE_FILE
:
236 ids
->hIconInstance
= NULL
;
237 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
238 ids
->caption_resource_id
= IDS_OVERWRITEFILE_CAPTION
;
239 ids
->text_resource_id
= IDS_OVERWRITEFILE_TEXT
;
241 case ASK_OVERWRITE_FOLDER
:
242 ids
->hIconInstance
= NULL
;
243 ids
->icon_resource_id
= IDI_SHELL_CONFIRM_DELETE
;
244 ids
->caption_resource_id
= IDS_OVERWRITEFILE_CAPTION
;
245 ids
->text_resource_id
= IDS_OVERWRITEFOLDER_TEXT
;
248 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog
);
253 static BOOL
SHELL_ConfirmDialogW(HWND hWnd
, int nKindOfDialog
, LPCWSTR szDir
, FILE_OPERATION
*op
)
255 WCHAR szCaption
[255], szText
[255], szBuffer
[MAX_PATH
+ 256];
256 SHELL_ConfirmIDstruc ids
;
261 assert(nKindOfDialog
>= 0 && nKindOfDialog
< 32);
262 if (op
&& (op
->dwYesToAllMask
& (1 << nKindOfDialog
)))
265 if (!SHELL_ConfirmIDs(nKindOfDialog
, &ids
)) return FALSE
;
267 LoadStringW(shell32_hInstance
, ids
.caption_resource_id
, szCaption
, sizeof(szCaption
)/sizeof(WCHAR
));
268 LoadStringW(shell32_hInstance
, ids
.text_resource_id
, szText
, sizeof(szText
)/sizeof(WCHAR
));
270 args
[0] = (DWORD_PTR
)szDir
;
271 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
272 szText
, 0, 0, szBuffer
, sizeof(szBuffer
), (va_list*)args
);
273 hIcon
= LoadIconW(ids
.hIconInstance
, (LPWSTR
)MAKEINTRESOURCE(ids
.icon_resource_id
));
275 ret
= SHELL_ConfirmMsgBox(hWnd
, szBuffer
, szCaption
, hIcon
, op
&& op
->bManyItems
);
277 if (ret
== IDD_YESTOALL
) {
278 op
->dwYesToAllMask
|= (1 << nKindOfDialog
);
282 op
->bCancelled
= TRUE
;
284 op
->req
->fAnyOperationsAborted
= TRUE
;
289 BOOL
SHELL_ConfirmYesNoW(HWND hWnd
, int nKindOfDialog
, LPCWSTR szDir
)
291 return SHELL_ConfirmDialogW(hWnd
, nKindOfDialog
, szDir
, NULL
);
294 static DWORD
SHELL32_AnsiToUnicodeBuf(LPCSTR aPath
, LPWSTR
*wPath
, DWORD minChars
)
296 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, NULL
, 0);
301 *wPath
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
304 MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, *wPath
, len
);
307 return E_OUTOFMEMORY
;
310 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath
)
312 HeapFree(GetProcessHeap(), 0, wPath
);
315 HRESULT WINAPI
SHIsFileAvailableOffline(LPCWSTR path
, LPDWORD status
)
317 FIXME("(%s, %p) stub\n", debugstr_w(path
), status
);
321 /**************************************************************************
322 * SHELL_DeleteDirectory() [internal]
324 * Asks for confirmation when bShowUI is true and deletes the directory and
325 * all its subdirectories and files if necessary.
327 BOOL
SHELL_DeleteDirectoryW(HWND hwnd
, LPCWSTR pszDir
, BOOL bShowUI
)
331 WIN32_FIND_DATAW wfd
;
332 WCHAR szTemp
[MAX_PATH
];
334 /* Make sure the directory exists before eventually prompting the user */
335 PathCombineW(szTemp
, pszDir
, wWildcardFile
);
336 hFind
= FindFirstFileW(szTemp
, &wfd
);
337 if (hFind
== INVALID_HANDLE_VALUE
)
340 if (!bShowUI
|| (ret
= SHELL_ConfirmDialogW(hwnd
, ASK_DELETE_FOLDER
, pszDir
, NULL
)))
344 if (IsDotDir(wfd
.cFileName
))
346 PathCombineW(szTemp
, pszDir
, wfd
.cFileName
);
347 if (FILE_ATTRIBUTE_DIRECTORY
& wfd
.dwFileAttributes
)
348 ret
= SHELL_DeleteDirectoryW(hwnd
, szTemp
, FALSE
);
350 ret
= (SHNotifyDeleteFileW(szTemp
) == ERROR_SUCCESS
);
351 } while (ret
&& FindNextFileW(hFind
, &wfd
));
355 ret
= (SHNotifyRemoveDirectoryW(pszDir
) == ERROR_SUCCESS
);
359 /**************************************************************************
360 * Win32CreateDirectory [SHELL32.93]
362 * Creates a directory. Also triggers a change notify if one exists.
365 * path [I] path to directory to create
368 * TRUE if successful, FALSE otherwise
371 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
373 TRACE("(%s, %p)\n", debugstr_w(path
), sec
);
375 if (CreateDirectoryW(path
, sec
))
377 SHChangeNotify(SHCNE_MKDIR
, SHCNF_PATHW
, path
, NULL
);
378 return ERROR_SUCCESS
;
380 return GetLastError();
383 /**********************************************************************/
385 BOOL WINAPI
Win32CreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
387 return (SHNotifyCreateDirectoryW(path
, sec
) == ERROR_SUCCESS
);
390 /************************************************************************
391 * Win32RemoveDirectory [SHELL32.94]
393 * Deletes a directory. Also triggers a change notify if one exists.
396 * path [I] path to directory to delete
399 * TRUE if successful, FALSE otherwise
401 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
)
404 TRACE("(%s)\n", debugstr_w(path
));
406 ret
= RemoveDirectoryW(path
);
409 /* Directory may be write protected */
410 DWORD dwAttr
= GetFileAttributesW(path
);
411 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
))
412 if (SetFileAttributesW(path
, dwAttr
& ~FILE_ATTRIBUTE_READONLY
))
413 ret
= RemoveDirectoryW(path
);
417 SHChangeNotify(SHCNE_RMDIR
, SHCNF_PATHW
, path
, NULL
);
418 return ERROR_SUCCESS
;
420 return GetLastError();
423 /***********************************************************************/
425 BOOL WINAPI
Win32RemoveDirectoryW(LPCWSTR path
)
427 return (SHNotifyRemoveDirectoryW(path
) == ERROR_SUCCESS
);
430 /************************************************************************
431 * Win32DeleteFile [SHELL32.164]
433 * Deletes a file. Also triggers a change notify if one exists.
436 * path [I] path to file to delete
439 * TRUE if successful, FALSE otherwise
441 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
)
445 TRACE("(%s)\n", debugstr_w(path
));
447 ret
= DeleteFileW(path
);
450 /* File may be write protected or a system file */
451 DWORD dwAttr
= GetFileAttributesW(path
);
452 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
453 if (SetFileAttributesW(path
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
454 ret
= DeleteFileW(path
);
458 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, path
, NULL
);
459 return ERROR_SUCCESS
;
461 return GetLastError();
464 /***********************************************************************/
466 DWORD WINAPI
Win32DeleteFileW(LPCWSTR path
)
468 return (SHNotifyDeleteFileW(path
) == ERROR_SUCCESS
);
471 /************************************************************************
472 * SHNotifyMoveFile [internal]
474 * Moves a file. Also triggers a change notify if one exists.
477 * src [I] path to source file to move
478 * dest [I] path to target file to move to
481 * ERORR_SUCCESS if successful
483 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
)
487 TRACE("(%s %s)\n", debugstr_w(src
), debugstr_w(dest
));
489 ret
= MoveFileExW(src
, dest
, MOVEFILE_REPLACE_EXISTING
);
491 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
493 ret
= MoveFileW(src
, dest
);
499 dwAttr
= SHFindAttrW(dest
, FALSE
);
500 if (INVALID_FILE_ATTRIBUTES
== dwAttr
)
502 /* Source file may be write protected or a system file */
503 dwAttr
= GetFileAttributesW(src
);
504 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
505 if (SetFileAttributesW(src
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
506 ret
= MoveFileW(src
, dest
);
511 SHChangeNotify(SHCNE_RENAMEITEM
, SHCNF_PATHW
, src
, dest
);
512 return ERROR_SUCCESS
;
514 return GetLastError();
517 /************************************************************************
518 * SHNotifyCopyFile [internal]
520 * Copies a file. Also triggers a change notify if one exists.
523 * src [I] path to source file to move
524 * dest [I] path to target file to move to
525 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
526 * a file with this name already exists
529 * ERROR_SUCCESS if successful
531 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bFailIfExists
)
535 TRACE("(%s %s %s)\n", debugstr_w(src
), debugstr_w(dest
), bFailIfExists
? "failIfExists" : "");
537 ret
= CopyFileW(src
, dest
, bFailIfExists
);
540 SHChangeNotify(SHCNE_CREATE
, SHCNF_PATHW
, dest
, NULL
);
541 return ERROR_SUCCESS
;
544 return GetLastError();
547 /*************************************************************************
548 * SHCreateDirectory [SHELL32.165]
550 * This function creates a file system folder whose fully qualified path is
551 * given by path. If one or more of the intermediate folders do not exist,
552 * they will be created as well.
556 * path [I] path of directory to create
559 * ERROR_SUCCESS or one of the following values:
560 * ERROR_BAD_PATHNAME if the path is relative
561 * ERROR_FILE_EXISTS when a file with that name exists
562 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
563 * ERROR_INVALID_NAME if the path contains invalid chars
564 * ERROR_ALREADY_EXISTS when the directory already exists
565 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
568 * exported by ordinal
570 * WinNT/2000 exports Unicode
572 DWORD WINAPI
SHCreateDirectory(HWND hWnd
, LPCWSTR path
)
574 return SHCreateDirectoryExW(hWnd
, path
, NULL
);
577 /*************************************************************************
578 * SHCreateDirectoryExA [SHELL32.@]
580 * This function creates a file system folder whose fully qualified path is
581 * given by path. If one or more of the intermediate folders do not exist,
582 * they will be created as well.
586 * path [I] path of directory to create
587 * sec [I] security attributes to use or NULL
590 * ERROR_SUCCESS or one of the following values:
591 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
592 * ERROR_INVALID_NAME if the path contains invalid chars
593 * ERROR_FILE_EXISTS when a file with that name exists
594 * ERROR_ALREADY_EXISTS when the directory already exists
595 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
597 * FIXME: Not implemented yet;
598 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
599 * if the path is a network path to deal with network drivers which might have a limited
600 * but unknown maximum path length. If not:
602 * If hWnd is set to a valid window handle, a message box is displayed warning
603 * the user that the files may not be accessible. If the user chooses not to
604 * proceed, the function returns ERROR_CANCELLED.
606 * If hWnd is set to NULL, no user interface is displayed and the function
607 * returns ERROR_CANCELLED.
609 int WINAPI
SHCreateDirectoryExA(HWND hWnd
, LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
614 TRACE("(%s, %p)\n", debugstr_a(path
), sec
);
616 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
619 retCode
= SHCreateDirectoryExW(hWnd
, wPath
, sec
);
620 SHELL32_FreeUnicodeBuf(wPath
);
625 /*************************************************************************
626 * SHCreateDirectoryExW [SHELL32.@]
628 * See SHCreateDirectoryExA.
630 int WINAPI
SHCreateDirectoryExW(HWND hWnd
, LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
632 int ret
= ERROR_BAD_PATHNAME
;
633 TRACE("(%p, %s, %p)\n", hWnd
, debugstr_w(path
), sec
);
635 if (PathIsRelativeW(path
))
641 ret
= SHNotifyCreateDirectoryW(path
, sec
);
642 /* Refuse to work on certain error codes before trying to create directories recursively */
643 if (ret
!= ERROR_SUCCESS
&&
644 ret
!= ERROR_FILE_EXISTS
&&
645 ret
!= ERROR_ALREADY_EXISTS
&&
646 ret
!= ERROR_FILENAME_EXCED_RANGE
)
648 WCHAR
*pEnd
, *pSlash
, szTemp
[MAX_PATH
+ 1]; /* extra for PathAddBackslash() */
650 lstrcpynW(szTemp
, path
, MAX_PATH
);
651 pEnd
= PathAddBackslashW(szTemp
);
656 while (*pSlash
&& *pSlash
!= '\\') pSlash
++;
659 *pSlash
= 0; /* terminate path at separator */
661 ret
= SHNotifyCreateDirectoryW(szTemp
, pSlash
+ 1 == pEnd
? sec
: NULL
);
663 *pSlash
++ = '\\'; /* put the separator back */
667 if (ret
&& hWnd
&& (ERROR_CANCELLED
!= ret
))
669 /* We failed and should show a dialog box */
670 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path
), ret
);
671 ret
= ERROR_CANCELLED
; /* Error has been already presented to user (not really yet!) */
677 /*************************************************************************
678 * SHFindAttrW [internal]
680 * Get the Attributes for a file or directory. The difference to GetAttributes()
681 * is that this function will also work for paths containing wildcard characters
685 * path [I] path of directory or file to check
686 * fileOnly [I] TRUE if only files should be found
689 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
690 * the first file or directory found otherwise
692 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
)
694 WIN32_FIND_DATAW wfd
;
695 BOOL b_FileMask
= fileOnly
&& (NULL
!= StrPBrkW(pName
, wWildcardChars
));
696 DWORD dwAttr
= INVALID_FILE_ATTRIBUTES
;
697 HANDLE hFind
= FindFirstFileW(pName
, &wfd
);
699 TRACE("%s %d\n", debugstr_w(pName
), fileOnly
);
700 if (INVALID_HANDLE_VALUE
!= hFind
)
704 if (b_FileMask
&& IsAttribDir(wfd
.dwFileAttributes
))
706 dwAttr
= wfd
.dwFileAttributes
;
709 while (FindNextFileW(hFind
, &wfd
));
715 /*************************************************************************
717 * SHNameTranslate HelperFunction for SHFileOperationA
719 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
720 * is NULL, only the necessary size of the string is determined and returned,
721 * otherwise the ASCII strings are copied into it and the buffer is increased
722 * to point to the location after the final 0 termination char.
724 static DWORD
SHNameTranslate(LPWSTR
* wString
, LPCWSTR
* pWToFrom
, BOOL more
)
726 DWORD size
= 0, aSize
= 0;
727 LPCSTR aString
= (LPCSTR
)*pWToFrom
;
733 size
= lstrlenA(aString
) + 1;
736 } while ((size
!= 1) && more
);
737 /* The two sizes might be different in the case of multibyte chars */
738 size
= MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, 0);
739 if (*wString
) /* only in the second loop */
741 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, size
);
742 *pWToFrom
= *wString
;
748 /*************************************************************************
749 * SHFileOperationA [SHELL32.@]
751 * Function to copy, move, delete and create one or more files with optional
755 * lpFileOp [I/O] pointer to a structure containing all the necessary information
758 * Success: ERROR_SUCCESS.
759 * Failure: ERROR_CANCELLED.
764 int WINAPI
SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp
)
766 SHFILEOPSTRUCTW nFileOp
= *((LPSHFILEOPSTRUCTW
)lpFileOp
);
769 LPWSTR ForFree
= NULL
, /* we change wString in SHNameTranslate and can't use it for freeing */
770 wString
= NULL
; /* we change this in SHNameTranslate */
773 if (FO_DELETE
== (nFileOp
.wFunc
& FO_MASK
))
774 nFileOp
.pTo
= NULL
; /* we need a NULL or a valid pointer for translation */
775 if (!(nFileOp
.fFlags
& FOF_SIMPLEPROGRESS
))
776 nFileOp
.lpszProgressTitle
= NULL
; /* we need a NULL or a valid pointer for translation */
777 while (1) /* every loop calculate size, second translate also, if we have storage for this */
779 size
= SHNameTranslate(&wString
, &nFileOp
.lpszProgressTitle
, FALSE
); /* no loop */
780 size
+= SHNameTranslate(&wString
, &nFileOp
.pFrom
, TRUE
); /* internal loop */
781 size
+= SHNameTranslate(&wString
, &nFileOp
.pTo
, TRUE
); /* internal loop */
785 retCode
= SHFileOperationW(&nFileOp
);
786 HeapFree(GetProcessHeap(), 0, ForFree
); /* we cannot use wString, it was changed */
791 wString
= ForFree
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
792 if (ForFree
) continue;
793 retCode
= ERROR_OUTOFMEMORY
;
794 nFileOp
.fAnyOperationsAborted
= TRUE
;
795 SetLastError(retCode
);
800 lpFileOp
->hNameMappings
= nFileOp
.hNameMappings
;
801 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
805 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
823 BOOL bAnyFromWildcard
;
824 BOOL bAnyDirectories
;
829 static void __inline
grow_list(FILE_LIST
*list
)
831 FILE_ENTRY
*new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, list
->feFiles
,
832 list
->num_alloc
* 2 * sizeof(*new) );
834 list
->num_alloc
*= 2;
837 /* adds a file to the FILE_ENTRY struct
839 static void add_file_to_entry(FILE_ENTRY
*feFile
, LPCWSTR szFile
)
841 DWORD dwLen
= lstrlenW(szFile
) + 1;
844 feFile
->szFullPath
= HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
845 lstrcpyW(feFile
->szFullPath
, szFile
);
847 ptr
= StrRChrW(szFile
, NULL
, '\\');
850 dwLen
= ptr
- szFile
+ 1;
851 feFile
->szDirectory
= HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
852 lstrcpynW(feFile
->szDirectory
, szFile
, dwLen
);
854 dwLen
= lstrlenW(feFile
->szFullPath
) - dwLen
+ 1;
855 feFile
->szFilename
= HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
856 lstrcpyW(feFile
->szFilename
, ptr
+ 1); /* skip over backslash */
858 feFile
->bFromWildcard
= FALSE
;
861 static LPWSTR
wildcard_to_file(LPCWSTR szWildCard
, LPCWSTR szFileName
)
865 DWORD dwDirLen
, dwFullLen
;
867 ptr
= StrRChrW(szWildCard
, NULL
, '\\');
868 dwDirLen
= ptr
- szWildCard
+ 1;
870 dwFullLen
= dwDirLen
+ lstrlenW(szFileName
) + 1;
871 szFullPath
= HeapAlloc(GetProcessHeap(), 0, dwFullLen
* sizeof(WCHAR
));
873 lstrcpynW(szFullPath
, szWildCard
, dwDirLen
+ 1);
874 lstrcatW(szFullPath
, szFileName
);
879 static void parse_wildcard_files(FILE_LIST
*flList
, LPCWSTR szFile
, LPDWORD pdwListIndex
)
881 WIN32_FIND_DATAW wfd
;
882 HANDLE hFile
= FindFirstFileW(szFile
, &wfd
);
887 if (hFile
== INVALID_HANDLE_VALUE
) return;
889 for (res
= TRUE
; res
; res
= FindNextFileW(hFile
, &wfd
))
891 if (IsDotDir(wfd
.cFileName
)) continue;
892 if (*pdwListIndex
>= flList
->num_alloc
) grow_list( flList
);
893 szFullPath
= wildcard_to_file(szFile
, wfd
.cFileName
);
894 file
= &flList
->feFiles
[(*pdwListIndex
)++];
895 add_file_to_entry(file
, szFullPath
);
896 file
->bFromWildcard
= TRUE
;
897 file
->attributes
= wfd
.dwFileAttributes
;
898 if (IsAttribDir(file
->attributes
)) flList
->bAnyDirectories
= TRUE
;
899 HeapFree(GetProcessHeap(), 0, szFullPath
);
905 /* takes the null-separated file list and fills out the FILE_LIST */
906 static HRESULT
parse_file_list(FILE_LIST
*flList
, LPCWSTR szFiles
)
908 LPCWSTR ptr
= szFiles
;
909 WCHAR szCurFile
[MAX_PATH
];
913 return ERROR_INVALID_PARAMETER
;
915 flList
->bAnyFromWildcard
= FALSE
;
916 flList
->bAnyDirectories
= FALSE
;
917 flList
->bAnyDontExist
= FALSE
;
918 flList
->num_alloc
= 32;
919 flList
->dwNumFiles
= 0;
923 return ERROR_ACCESS_DENIED
;
925 flList
->feFiles
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
926 flList
->num_alloc
* sizeof(FILE_ENTRY
));
930 if (i
>= flList
->num_alloc
) grow_list( flList
);
932 /* change relative to absolute path */
933 if (PathIsRelativeW(ptr
))
935 GetCurrentDirectoryW(MAX_PATH
, szCurFile
);
936 PathCombineW(szCurFile
, szCurFile
, ptr
);
937 flList
->feFiles
[i
].bFromRelative
= TRUE
;
941 lstrcpyW(szCurFile
, ptr
);
942 flList
->feFiles
[i
].bFromRelative
= FALSE
;
945 /* parse wildcard files if they are in the filename */
946 if (StrPBrkW(szCurFile
, wWildcardChars
))
948 parse_wildcard_files(flList
, szCurFile
, &i
);
949 flList
->bAnyFromWildcard
= TRUE
;
954 FILE_ENTRY
*file
= &flList
->feFiles
[i
];
955 add_file_to_entry(file
, szCurFile
);
956 file
->attributes
= GetFileAttributesW( file
->szFullPath
);
957 file
->bExists
= (file
->attributes
!= INVALID_FILE_ATTRIBUTES
);
958 if (!file
->bExists
) flList
->bAnyDontExist
= TRUE
;
959 if (IsAttribDir(file
->attributes
)) flList
->bAnyDirectories
= TRUE
;
962 /* advance to the next string */
963 ptr
+= lstrlenW(ptr
) + 1;
966 flList
->dwNumFiles
= i
;
971 /* free the FILE_LIST */
972 static void destroy_file_list(FILE_LIST
*flList
)
976 if (!flList
|| !flList
->feFiles
)
979 for (i
= 0; i
< flList
->dwNumFiles
; i
++)
981 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szDirectory
);
982 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFilename
);
983 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFullPath
);
986 HeapFree(GetProcessHeap(), 0, flList
->feFiles
);
989 static void copy_dir_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, LPCWSTR szDestPath
)
991 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
992 SHFILEOPSTRUCTW fileOp
;
994 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
996 if (IsDotDir(feFrom
->szFilename
))
999 if (PathFileExistsW(szDestPath
))
1000 PathCombineW(szTo
, szDestPath
, feFrom
->szFilename
);
1002 lstrcpyW(szTo
, szDestPath
);
1004 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
)) {
1005 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FOLDER
, feFrom
->szFilename
, op
))
1007 /* Vista returns an ERROR_CANCELLED even if user pressed "No" */
1008 if (!op
->bManyItems
)
1009 op
->bCancelled
= TRUE
;
1014 szTo
[lstrlenW(szTo
) + 1] = '\0';
1015 SHNotifyCreateDirectoryW(szTo
, NULL
);
1017 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1018 szFrom
[lstrlenW(szFrom
) + 1] = '\0';
1021 fileOp
.pFrom
= szFrom
;
1023 fileOp
.fFlags
&= ~FOF_MULTIDESTFILES
; /* we know we're copying to one dir */
1025 /* Don't ask the user about overwriting files when he accepted to overwrite the
1026 folder. FIXME: this is not exactly what Windows does - e.g. there would be
1027 an additional confirmation for a nested folder */
1028 fileOp
.fFlags
|= FOF_NOCONFIRMATION
;
1030 SHFileOperationW(&fileOp
);
1033 static BOOL
copy_file_to_file(FILE_OPERATION
*op
, const WCHAR
*szFrom
, const WCHAR
*szTo
)
1035 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
))
1037 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FILE
, PathFindFileNameW(szTo
), op
))
1041 return SHNotifyCopyFileW(szFrom
, szTo
, FALSE
) == 0;
1044 /* copy a file or directory to another directory */
1045 static void copy_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1047 if (!PathFileExistsW(feTo
->szFullPath
))
1048 SHNotifyCreateDirectoryW(feTo
->szFullPath
, NULL
);
1050 if (IsAttribFile(feFrom
->attributes
))
1052 WCHAR szDestPath
[MAX_PATH
];
1054 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1055 copy_file_to_file(op
, feFrom
->szFullPath
, szDestPath
);
1057 else if (!(op
->req
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1058 copy_dir_to_dir(op
, feFrom
, feTo
->szFullPath
);
1061 static void create_dest_dirs(LPCWSTR szDestDir
)
1063 WCHAR dir
[MAX_PATH
];
1064 LPCWSTR ptr
= StrChrW(szDestDir
, '\\');
1066 /* make sure all directories up to last one are created */
1067 while (ptr
&& (ptr
= StrChrW(ptr
+ 1, '\\')))
1069 lstrcpynW(dir
, szDestDir
, ptr
- szDestDir
+ 1);
1071 if (!PathFileExistsW(dir
))
1072 SHNotifyCreateDirectoryW(dir
, NULL
);
1075 /* create last directory */
1076 if (!PathFileExistsW(szDestDir
))
1077 SHNotifyCreateDirectoryW(szDestDir
, NULL
);
1080 /* the FO_COPY operation */
1081 static HRESULT
copy_files(FILE_OPERATION
*op
, const FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
1084 const FILE_ENTRY
*entryToCopy
;
1085 const FILE_ENTRY
*fileDest
= &flTo
->feFiles
[0];
1087 if (flFrom
->bAnyDontExist
)
1088 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1090 if (op
->req
->fFlags
& FOF_MULTIDESTFILES
)
1092 if (flFrom
->bAnyFromWildcard
)
1093 return ERROR_CANCELLED
;
1095 if (flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1097 if (flFrom
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1098 return ERROR_CANCELLED
;
1100 flTo
->dwNumFiles
= 1;
1102 else if (IsAttribDir(fileDest
->attributes
))
1104 for (i
= 1; i
< flTo
->dwNumFiles
; i
++)
1105 if (!IsAttribDir(flTo
->feFiles
[i
].attributes
) ||
1106 !IsAttribDir(flFrom
->feFiles
[i
].attributes
))
1108 return ERROR_CANCELLED
;
1112 else if (flFrom
->dwNumFiles
!= 1)
1114 if (flTo
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1115 return ERROR_CANCELLED
;
1117 if (PathFileExistsW(fileDest
->szFullPath
) &&
1118 IsAttribFile(fileDest
->attributes
))
1120 return ERROR_CANCELLED
;
1123 if (flTo
->dwNumFiles
== 1 && fileDest
->bFromRelative
&&
1124 !PathFileExistsW(fileDest
->szFullPath
))
1126 return ERROR_CANCELLED
;
1130 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1132 entryToCopy
= &flFrom
->feFiles
[i
];
1134 if ((op
->req
->fFlags
& FOF_MULTIDESTFILES
) &&
1135 flTo
->dwNumFiles
> 1)
1137 fileDest
= &flTo
->feFiles
[i
];
1140 if (IsAttribDir(entryToCopy
->attributes
) &&
1141 !lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szDirectory
))
1143 return ERROR_SUCCESS
;
1146 create_dest_dirs(fileDest
->szDirectory
);
1148 if (!lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1150 if (IsAttribFile(entryToCopy
->attributes
))
1151 return ERROR_NO_MORE_SEARCH_HANDLES
;
1153 return ERROR_SUCCESS
;
1156 if ((flFrom
->dwNumFiles
> 1 && flTo
->dwNumFiles
== 1) ||
1157 IsAttribDir(fileDest
->attributes
))
1159 copy_to_dir(op
, entryToCopy
, fileDest
);
1161 else if (IsAttribDir(entryToCopy
->attributes
))
1163 copy_dir_to_dir(op
, entryToCopy
, fileDest
->szFullPath
);
1167 if (!copy_file_to_file(op
, entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1169 op
->req
->fAnyOperationsAborted
= TRUE
;
1170 return ERROR_CANCELLED
;
1174 /* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
1176 return ERROR_CANCELLED
;
1179 /* Vista return code. On XP if the used pressed "No" for the last item,
1180 * ERROR_ARENA_TRASHED would be returned */
1181 return ERROR_SUCCESS
;
1184 static BOOL
confirm_delete_list(HWND hWnd
, DWORD fFlags
, BOOL fTrash
, const FILE_LIST
*flFrom
)
1186 if (flFrom
->dwNumFiles
> 1)
1189 const WCHAR format
[] = {'%','d',0};
1191 wnsprintfW(tmp
, sizeof(tmp
)/sizeof(tmp
[0]), format
, flFrom
->dwNumFiles
);
1192 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_MULTIPLE_ITEM
:ASK_DELETE_MULTIPLE_ITEM
), tmp
, NULL
);
1196 const FILE_ENTRY
*fileEntry
= &flFrom
->feFiles
[0];
1198 if (IsAttribFile(fileEntry
->attributes
))
1199 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FILE
:ASK_DELETE_FILE
), fileEntry
->szFullPath
, NULL
);
1200 else if (!(fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1201 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FOLDER
:ASK_DELETE_FOLDER
), fileEntry
->szFullPath
, NULL
);
1206 /* the FO_DELETE operation */
1207 static HRESULT
delete_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
)
1209 const FILE_ENTRY
*fileEntry
;
1214 if (!flFrom
->dwNumFiles
)
1215 return ERROR_SUCCESS
;
1217 /* Windows also checks only the first item */
1218 bTrash
= (lpFileOp
->fFlags
& FOF_ALLOWUNDO
)
1219 && TRASH_CanTrashFile(flFrom
->feFiles
[0].szFullPath
);
1221 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (!bTrash
&& lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1222 if (!confirm_delete_list(lpFileOp
->hwnd
, lpFileOp
->fFlags
, bTrash
, flFrom
))
1224 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1228 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1231 fileEntry
= &flFrom
->feFiles
[i
];
1233 if (!IsAttribFile(fileEntry
->attributes
) &&
1234 (lpFileOp
->fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1240 if (TRASH_TrashFile(fileEntry
->szFullPath
))
1243 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1244 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1245 bDelete
= SHELL_ConfirmDialogW(lpFileOp
->hwnd
, ASK_CANT_TRASH_ITEM
, fileEntry
->szFullPath
, NULL
);
1251 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1256 /* delete the file or directory */
1257 if (IsAttribFile(fileEntry
->attributes
))
1258 bPathExists
= DeleteFileW(fileEntry
->szFullPath
);
1260 bPathExists
= SHELL_DeleteDirectoryW(lpFileOp
->hwnd
, fileEntry
->szFullPath
, FALSE
);
1263 return ERROR_PATH_NOT_FOUND
;
1266 return ERROR_SUCCESS
;
1269 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_ENTRY
*feFrom
, LPCWSTR szDestPath
)
1271 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
1272 SHFILEOPSTRUCTW fileOp
;
1274 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1276 if (IsDotDir(feFrom
->szFilename
))
1279 SHNotifyCreateDirectoryW(szDestPath
, NULL
);
1281 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1282 szFrom
[lstrlenW(szFrom
) + 1] = '\0';
1284 lstrcpyW(szTo
, szDestPath
);
1285 szTo
[lstrlenW(szDestPath
) + 1] = '\0';
1288 fileOp
.pFrom
= szFrom
;
1291 SHFileOperationW(&fileOp
);
1294 /* moves a file or directory to another directory */
1295 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1297 WCHAR szDestPath
[MAX_PATH
];
1299 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1301 if (IsAttribFile(feFrom
->attributes
))
1302 SHNotifyMoveFileW(feFrom
->szFullPath
, szDestPath
);
1303 else if (!(lpFileOp
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1304 move_dir_to_dir(lpFileOp
, feFrom
, szDestPath
);
1307 /* the FO_MOVE operation */
1308 static HRESULT
move_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
, const FILE_LIST
*flTo
)
1311 const FILE_ENTRY
*entryToMove
;
1312 const FILE_ENTRY
*fileDest
;
1314 if (!flFrom
->dwNumFiles
|| !flTo
->dwNumFiles
)
1315 return ERROR_CANCELLED
;
1317 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1318 flTo
->dwNumFiles
> 1 && flFrom
->dwNumFiles
> 1)
1320 return ERROR_CANCELLED
;
1323 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1324 !flFrom
->bAnyDirectories
&&
1325 flFrom
->dwNumFiles
> flTo
->dwNumFiles
)
1327 return ERROR_CANCELLED
;
1330 if (!PathFileExistsW(flTo
->feFiles
[0].szDirectory
))
1331 return ERROR_CANCELLED
;
1333 if ((lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1334 flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1336 return ERROR_CANCELLED
;
1339 fileDest
= &flTo
->feFiles
[0];
1340 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1342 entryToMove
= &flFrom
->feFiles
[i
];
1344 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
)
1345 fileDest
= &flTo
->feFiles
[i
];
1347 if (!PathFileExistsW(fileDest
->szDirectory
))
1348 return ERROR_CANCELLED
;
1350 if (fileDest
->bExists
&& IsAttribDir(fileDest
->attributes
))
1351 move_to_dir(lpFileOp
, entryToMove
, fileDest
);
1353 SHNotifyMoveFileW(entryToMove
->szFullPath
, fileDest
->szFullPath
);
1356 return ERROR_SUCCESS
;
1359 /* the FO_RENAME files */
1360 static HRESULT
rename_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
, const FILE_LIST
*flTo
)
1362 const FILE_ENTRY
*feFrom
;
1363 const FILE_ENTRY
*feTo
;
1365 if (flFrom
->dwNumFiles
!= 1)
1366 return ERROR_GEN_FAILURE
;
1368 if (flTo
->dwNumFiles
!= 1)
1369 return ERROR_CANCELLED
;
1371 feFrom
= &flFrom
->feFiles
[0];
1372 feTo
= &flTo
->feFiles
[0];
1374 /* fail if destination doesn't exist */
1375 if (!feFrom
->bExists
)
1376 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1378 /* fail if destination already exists */
1380 return ERROR_ALREADY_EXISTS
;
1382 return SHNotifyMoveFileW(feFrom
->szFullPath
, feTo
->szFullPath
);
1385 /* alert the user if an unsupported flag is used */
1386 static void check_flags(FILEOP_FLAGS fFlags
)
1388 WORD wUnsupportedFlags
= FOF_NO_CONNECTED_ELEMENTS
|
1389 FOF_NOCOPYSECURITYATTRIBS
| FOF_NORECURSEREPARSE
|
1390 FOF_RENAMEONCOLLISION
| FOF_WANTMAPPINGHANDLE
;
1392 if (fFlags
& wUnsupportedFlags
)
1393 FIXME("Unsupported flags: %04x\n", fFlags
);
1396 /*************************************************************************
1397 * SHFileOperationW [SHELL32.@]
1399 * See SHFileOperationA
1401 int WINAPI
SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp
)
1404 FILE_LIST flFrom
, flTo
;
1408 return ERROR_INVALID_PARAMETER
;
1410 check_flags(lpFileOp
->fFlags
);
1412 ZeroMemory(&flFrom
, sizeof(FILE_LIST
));
1413 ZeroMemory(&flTo
, sizeof(FILE_LIST
));
1415 if ((ret
= parse_file_list(&flFrom
, lpFileOp
->pFrom
)))
1418 if (lpFileOp
->wFunc
!= FO_DELETE
)
1419 parse_file_list(&flTo
, lpFileOp
->pTo
);
1421 ZeroMemory(&op
, sizeof(op
));
1423 op
.bManyItems
= (flFrom
.dwNumFiles
> 1);
1425 switch (lpFileOp
->wFunc
)
1428 ret
= copy_files(&op
, &flFrom
, &flTo
);
1431 ret
= delete_files(lpFileOp
, &flFrom
);
1434 ret
= move_files(lpFileOp
, &flFrom
, &flTo
);
1437 ret
= rename_files(lpFileOp
, &flFrom
, &flTo
);
1440 ret
= ERROR_INVALID_PARAMETER
;
1444 destroy_file_list(&flFrom
);
1446 if (lpFileOp
->wFunc
!= FO_DELETE
)
1447 destroy_file_list(&flTo
);
1449 if (ret
== ERROR_CANCELLED
)
1450 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1455 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1457 /*************************************************************************
1458 * SHFreeNameMappings [shell32.246]
1460 * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1464 * hNameMapping [I] handle to the name mappings used during renaming of files
1469 void WINAPI
SHFreeNameMappings(HANDLE hNameMapping
)
1473 int i
= SHDSA_GetItemCount((HDSA
)hNameMapping
) - 1;
1477 LPSHNAMEMAPPINGW lp
= DSA_GetItemPtr((HDSA
)hNameMapping
, i
);
1479 SHFree(lp
->pszOldPath
);
1480 SHFree(lp
->pszNewPath
);
1482 DSA_Destroy((HDSA
)hNameMapping
);
1486 /*************************************************************************
1487 * SheGetDirA [SHELL32.@]
1489 * drive = 0: returns the current directory path
1490 * drive > 0: returns the current directory path of the specified drive
1491 * drive=1 -> A: drive=2 -> B: ...
1492 * returns 0 if successful
1494 DWORD WINAPI
SheGetDirA(DWORD drive
, LPSTR buffer
)
1496 WCHAR org_path
[MAX_PATH
];
1500 /* change current directory to the specified drive */
1502 strcpy(drv_path
, "A:");
1503 drv_path
[0] += (char)drive
-1;
1505 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1507 SetCurrentDirectoryA(drv_path
);
1510 /* query current directory path of the specified drive */
1511 ret
= GetCurrentDirectoryA(MAX_PATH
, buffer
);
1513 /* back to the original drive */
1515 SetCurrentDirectoryW(org_path
);
1518 return GetLastError();
1523 /*************************************************************************
1524 * SheGetDirW [SHELL32.@]
1526 * drive = 0: returns the current directory path
1527 * drive > 0: returns the current directory path of the specified drive
1528 * drive=1 -> A: drive=2 -> B: ...
1529 * returns 0 if successful
1531 DWORD WINAPI
SheGetDirW(DWORD drive
, LPWSTR buffer
)
1533 WCHAR org_path
[MAX_PATH
];
1537 /* change current directory to the specified drive */
1539 strcpy(drv_path
, "A:");
1540 drv_path
[0] += (char)drive
-1;
1542 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1544 SetCurrentDirectoryA(drv_path
);
1547 /* query current directory path of the specified drive */
1548 ret
= GetCurrentDirectoryW(MAX_PATH
, buffer
);
1550 /* back to the original drive */
1552 SetCurrentDirectoryW(org_path
);
1555 return GetLastError();
1560 /*************************************************************************
1561 * SheChangeDirA [SHELL32.@]
1563 * changes the current directory to the specified path
1564 * and returns 0 if successful
1566 DWORD WINAPI
SheChangeDirA(LPSTR path
)
1568 if (SetCurrentDirectoryA(path
))
1571 return GetLastError();
1574 /*************************************************************************
1575 * SheChangeDirW [SHELL32.@]
1577 * changes the current directory to the specified path
1578 * and returns 0 if successful
1580 DWORD WINAPI
SheChangeDirW(LPWSTR path
)
1582 if (SetCurrentDirectoryW(path
))
1585 return GetLastError();
1588 /*************************************************************************
1589 * IsNetDrive [SHELL32.66]
1591 BOOL WINAPI
IsNetDrive(DWORD drive
)
1594 strcpy(root
, "A:\\");
1595 root
[0] += (char)drive
;
1596 return (GetDriveTypeA(root
) == DRIVE_REMOTE
);
1600 /*************************************************************************
1601 * RealDriveType [SHELL32.524]
1603 INT WINAPI
RealDriveType(INT drive
, BOOL bQueryNet
)
1605 char root
[] = "A:\\";
1606 root
[0] += (char)drive
;
1607 return GetDriveTypeA(root
);
1610 /***********************************************************************
1611 * SHPathPrepareForWriteW (SHELL32.@)
1613 HRESULT WINAPI
SHPathPrepareForWriteW(HWND hwnd
, IUnknown
*modless
, LPCWSTR path
, DWORD flags
)
1620 WCHAR
* temppath
=NULL
;
1622 TRACE("%p %p %s 0x%80x\n", hwnd
, modless
, debugstr_w(path
), flags
);
1624 if (flags
& ~(SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
|SHPPFW_IGNOREFILENAME
))
1625 FIXME("unimplemented flags 0x%08x\n", flags
);
1627 /* cut off filename if necessary */
1628 if (flags
& SHPPFW_IGNOREFILENAME
)
1630 last_slash
= StrRChrW(path
, NULL
, '\\');
1631 if (last_slash
== NULL
)
1634 len
= last_slash
- path
+ 1;
1635 temppath
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1637 return E_OUTOFMEMORY
;
1638 StrCpyNW(temppath
, path
, len
);
1639 realpath
= temppath
;
1646 /* try to create the directory if asked to */
1647 if (flags
& (SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
))
1649 if (flags
& SHPPFW_ASKDIRCREATE
)
1650 FIXME("treating SHPPFW_ASKDIRCREATE as SHPPFW_DIRCREATE\n");
1652 SHCreateDirectoryExW(0, realpath
, NULL
);
1655 /* check if we can access the directory */
1656 res
= GetFileAttributesW(realpath
);
1658 HeapFree(GetProcessHeap(), 0, temppath
);
1660 if (res
== INVALID_FILE_ATTRIBUTES
)
1662 err
= GetLastError();
1663 if (err
== ERROR_FILE_NOT_FOUND
)
1664 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
1665 return HRESULT_FROM_WIN32(err
);
1667 else if (res
& FILE_ATTRIBUTE_DIRECTORY
)
1670 return HRESULT_FROM_WIN32(ERROR_DIRECTORY
);
1673 /***********************************************************************
1674 * SHPathPrepareForWriteA (SHELL32.@)
1676 HRESULT WINAPI
SHPathPrepareForWriteA(HWND hwnd
, IUnknown
*modless
, LPCSTR path
, DWORD flags
)
1678 WCHAR wpath
[MAX_PATH
];
1679 MultiByteToWideChar( CP_ACP
, 0, path
, -1, wpath
, MAX_PATH
);
1680 return SHPathPrepareForWriteW(hwnd
, modless
, wpath
, flags
);