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
)
536 TRACE("(%s %s %s)\n", debugstr_w(src
), debugstr_w(dest
), bFailIfExists
? "failIfExists" : "");
538 /* Destination file may already exist with read only attribute */
539 attribs
= GetFileAttributesW(dest
);
540 if (IsAttrib(attribs
, FILE_ATTRIBUTE_READONLY
))
541 SetFileAttributesW(dest
, attribs
& ~FILE_ATTRIBUTE_READONLY
);
543 ret
= CopyFileW(src
, dest
, bFailIfExists
);
546 SHChangeNotify(SHCNE_CREATE
, SHCNF_PATHW
, dest
, NULL
);
547 return ERROR_SUCCESS
;
550 return GetLastError();
553 /*************************************************************************
554 * SHCreateDirectory [SHELL32.165]
556 * This function creates a file system folder whose fully qualified path is
557 * given by path. If one or more of the intermediate folders do not exist,
558 * they will be created as well.
562 * path [I] path of directory to create
565 * ERROR_SUCCESS or one of the following values:
566 * ERROR_BAD_PATHNAME if the path is relative
567 * ERROR_FILE_EXISTS when a file with that name exists
568 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
569 * ERROR_INVALID_NAME if the path contains invalid chars
570 * ERROR_ALREADY_EXISTS when the directory already exists
571 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
574 * exported by ordinal
576 * WinNT/2000 exports Unicode
578 DWORD WINAPI
SHCreateDirectory(HWND hWnd
, LPCWSTR path
)
580 return SHCreateDirectoryExW(hWnd
, path
, NULL
);
583 /*************************************************************************
584 * SHCreateDirectoryExA [SHELL32.@]
586 * This function creates a file system folder whose fully qualified path is
587 * given by path. If one or more of the intermediate folders do not exist,
588 * they will be created as well.
592 * path [I] path of directory to create
593 * sec [I] security attributes to use or NULL
596 * ERROR_SUCCESS or one of the following values:
597 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
598 * ERROR_INVALID_NAME if the path contains invalid chars
599 * ERROR_FILE_EXISTS when a file with that name exists
600 * ERROR_ALREADY_EXISTS when the directory already exists
601 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
603 * FIXME: Not implemented yet;
604 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
605 * if the path is a network path to deal with network drivers which might have a limited
606 * but unknown maximum path length. If not:
608 * If hWnd is set to a valid window handle, a message box is displayed warning
609 * the user that the files may not be accessible. If the user chooses not to
610 * proceed, the function returns ERROR_CANCELLED.
612 * If hWnd is set to NULL, no user interface is displayed and the function
613 * returns ERROR_CANCELLED.
615 int WINAPI
SHCreateDirectoryExA(HWND hWnd
, LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
620 TRACE("(%s, %p)\n", debugstr_a(path
), sec
);
622 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
625 retCode
= SHCreateDirectoryExW(hWnd
, wPath
, sec
);
626 SHELL32_FreeUnicodeBuf(wPath
);
631 /*************************************************************************
632 * SHCreateDirectoryExW [SHELL32.@]
634 * See SHCreateDirectoryExA.
636 int WINAPI
SHCreateDirectoryExW(HWND hWnd
, LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
638 int ret
= ERROR_BAD_PATHNAME
;
639 TRACE("(%p, %s, %p)\n", hWnd
, debugstr_w(path
), sec
);
641 if (PathIsRelativeW(path
))
647 ret
= SHNotifyCreateDirectoryW(path
, sec
);
648 /* Refuse to work on certain error codes before trying to create directories recursively */
649 if (ret
!= ERROR_SUCCESS
&&
650 ret
!= ERROR_FILE_EXISTS
&&
651 ret
!= ERROR_ALREADY_EXISTS
&&
652 ret
!= ERROR_FILENAME_EXCED_RANGE
)
654 WCHAR
*pEnd
, *pSlash
, szTemp
[MAX_PATH
+ 1]; /* extra for PathAddBackslash() */
656 lstrcpynW(szTemp
, path
, MAX_PATH
);
657 pEnd
= PathAddBackslashW(szTemp
);
662 while (*pSlash
&& *pSlash
!= '\\') pSlash
++;
665 *pSlash
= 0; /* terminate path at separator */
667 ret
= SHNotifyCreateDirectoryW(szTemp
, pSlash
+ 1 == pEnd
? sec
: NULL
);
669 *pSlash
++ = '\\'; /* put the separator back */
673 if (ret
&& hWnd
&& (ERROR_CANCELLED
!= ret
))
675 /* We failed and should show a dialog box */
676 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path
), ret
);
677 ret
= ERROR_CANCELLED
; /* Error has been already presented to user (not really yet!) */
683 /*************************************************************************
684 * SHFindAttrW [internal]
686 * Get the Attributes for a file or directory. The difference to GetAttributes()
687 * is that this function will also work for paths containing wildcard characters
691 * path [I] path of directory or file to check
692 * fileOnly [I] TRUE if only files should be found
695 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
696 * the first file or directory found otherwise
698 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
)
700 WIN32_FIND_DATAW wfd
;
701 BOOL b_FileMask
= fileOnly
&& (NULL
!= StrPBrkW(pName
, wWildcardChars
));
702 DWORD dwAttr
= INVALID_FILE_ATTRIBUTES
;
703 HANDLE hFind
= FindFirstFileW(pName
, &wfd
);
705 TRACE("%s %d\n", debugstr_w(pName
), fileOnly
);
706 if (INVALID_HANDLE_VALUE
!= hFind
)
710 if (b_FileMask
&& IsAttribDir(wfd
.dwFileAttributes
))
712 dwAttr
= wfd
.dwFileAttributes
;
715 while (FindNextFileW(hFind
, &wfd
));
721 /*************************************************************************
723 * SHNameTranslate HelperFunction for SHFileOperationA
725 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
726 * is NULL, only the necessary size of the string is determined and returned,
727 * otherwise the ASCII strings are copied into it and the buffer is increased
728 * to point to the location after the final 0 termination char.
730 static DWORD
SHNameTranslate(LPWSTR
* wString
, LPCWSTR
* pWToFrom
, BOOL more
)
732 DWORD size
= 0, aSize
= 0;
733 LPCSTR aString
= (LPCSTR
)*pWToFrom
;
739 size
= lstrlenA(aString
) + 1;
742 } while ((size
!= 1) && more
);
743 /* The two sizes might be different in the case of multibyte chars */
744 size
= MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, 0);
745 if (*wString
) /* only in the second loop */
747 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, size
);
748 *pWToFrom
= *wString
;
754 /*************************************************************************
755 * SHFileOperationA [SHELL32.@]
757 * Function to copy, move, delete and create one or more files with optional
761 * lpFileOp [I/O] pointer to a structure containing all the necessary information
764 * Success: ERROR_SUCCESS.
765 * Failure: ERROR_CANCELLED.
770 int WINAPI
SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp
)
772 SHFILEOPSTRUCTW nFileOp
= *((LPSHFILEOPSTRUCTW
)lpFileOp
);
775 LPWSTR ForFree
= NULL
, /* we change wString in SHNameTranslate and can't use it for freeing */
776 wString
= NULL
; /* we change this in SHNameTranslate */
779 if (FO_DELETE
== (nFileOp
.wFunc
& FO_MASK
))
780 nFileOp
.pTo
= NULL
; /* we need a NULL or a valid pointer for translation */
781 if (!(nFileOp
.fFlags
& FOF_SIMPLEPROGRESS
))
782 nFileOp
.lpszProgressTitle
= NULL
; /* we need a NULL or a valid pointer for translation */
783 while (1) /* every loop calculate size, second translate also, if we have storage for this */
785 size
= SHNameTranslate(&wString
, &nFileOp
.lpszProgressTitle
, FALSE
); /* no loop */
786 size
+= SHNameTranslate(&wString
, &nFileOp
.pFrom
, TRUE
); /* internal loop */
787 size
+= SHNameTranslate(&wString
, &nFileOp
.pTo
, TRUE
); /* internal loop */
791 retCode
= SHFileOperationW(&nFileOp
);
792 HeapFree(GetProcessHeap(), 0, ForFree
); /* we cannot use wString, it was changed */
797 wString
= ForFree
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
798 if (ForFree
) continue;
799 retCode
= ERROR_OUTOFMEMORY
;
800 nFileOp
.fAnyOperationsAborted
= TRUE
;
801 SetLastError(retCode
);
806 lpFileOp
->hNameMappings
= nFileOp
.hNameMappings
;
807 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
811 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
829 BOOL bAnyFromWildcard
;
830 BOOL bAnyDirectories
;
835 static void __inline
grow_list(FILE_LIST
*list
)
837 FILE_ENTRY
*new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, list
->feFiles
,
838 list
->num_alloc
* 2 * sizeof(*new) );
840 list
->num_alloc
*= 2;
843 /* adds a file to the FILE_ENTRY struct
845 static void add_file_to_entry(FILE_ENTRY
*feFile
, LPCWSTR szFile
)
847 DWORD dwLen
= lstrlenW(szFile
) + 1;
850 feFile
->szFullPath
= HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
851 lstrcpyW(feFile
->szFullPath
, szFile
);
853 ptr
= StrRChrW(szFile
, NULL
, '\\');
856 dwLen
= ptr
- szFile
+ 1;
857 feFile
->szDirectory
= HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
858 lstrcpynW(feFile
->szDirectory
, szFile
, dwLen
);
860 dwLen
= lstrlenW(feFile
->szFullPath
) - dwLen
+ 1;
861 feFile
->szFilename
= HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
862 lstrcpyW(feFile
->szFilename
, ptr
+ 1); /* skip over backslash */
864 feFile
->bFromWildcard
= FALSE
;
867 static LPWSTR
wildcard_to_file(LPCWSTR szWildCard
, LPCWSTR szFileName
)
871 DWORD dwDirLen
, dwFullLen
;
873 ptr
= StrRChrW(szWildCard
, NULL
, '\\');
874 dwDirLen
= ptr
- szWildCard
+ 1;
876 dwFullLen
= dwDirLen
+ lstrlenW(szFileName
) + 1;
877 szFullPath
= HeapAlloc(GetProcessHeap(), 0, dwFullLen
* sizeof(WCHAR
));
879 lstrcpynW(szFullPath
, szWildCard
, dwDirLen
+ 1);
880 lstrcatW(szFullPath
, szFileName
);
885 static void parse_wildcard_files(FILE_LIST
*flList
, LPCWSTR szFile
, LPDWORD pdwListIndex
)
887 WIN32_FIND_DATAW wfd
;
888 HANDLE hFile
= FindFirstFileW(szFile
, &wfd
);
893 if (hFile
== INVALID_HANDLE_VALUE
) return;
895 for (res
= TRUE
; res
; res
= FindNextFileW(hFile
, &wfd
))
897 if (IsDotDir(wfd
.cFileName
)) continue;
898 if (*pdwListIndex
>= flList
->num_alloc
) grow_list( flList
);
899 szFullPath
= wildcard_to_file(szFile
, wfd
.cFileName
);
900 file
= &flList
->feFiles
[(*pdwListIndex
)++];
901 add_file_to_entry(file
, szFullPath
);
902 file
->bFromWildcard
= TRUE
;
903 file
->attributes
= wfd
.dwFileAttributes
;
904 if (IsAttribDir(file
->attributes
)) flList
->bAnyDirectories
= TRUE
;
905 HeapFree(GetProcessHeap(), 0, szFullPath
);
911 /* takes the null-separated file list and fills out the FILE_LIST */
912 static HRESULT
parse_file_list(FILE_LIST
*flList
, LPCWSTR szFiles
)
914 LPCWSTR ptr
= szFiles
;
915 WCHAR szCurFile
[MAX_PATH
];
919 return ERROR_INVALID_PARAMETER
;
921 flList
->bAnyFromWildcard
= FALSE
;
922 flList
->bAnyDirectories
= FALSE
;
923 flList
->bAnyDontExist
= FALSE
;
924 flList
->num_alloc
= 32;
925 flList
->dwNumFiles
= 0;
929 return ERROR_ACCESS_DENIED
;
931 flList
->feFiles
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
932 flList
->num_alloc
* sizeof(FILE_ENTRY
));
936 if (i
>= flList
->num_alloc
) grow_list( flList
);
938 /* change relative to absolute path */
939 if (PathIsRelativeW(ptr
))
941 GetCurrentDirectoryW(MAX_PATH
, szCurFile
);
942 PathCombineW(szCurFile
, szCurFile
, ptr
);
943 flList
->feFiles
[i
].bFromRelative
= TRUE
;
947 lstrcpyW(szCurFile
, ptr
);
948 flList
->feFiles
[i
].bFromRelative
= FALSE
;
951 /* parse wildcard files if they are in the filename */
952 if (StrPBrkW(szCurFile
, wWildcardChars
))
954 parse_wildcard_files(flList
, szCurFile
, &i
);
955 flList
->bAnyFromWildcard
= TRUE
;
960 FILE_ENTRY
*file
= &flList
->feFiles
[i
];
961 add_file_to_entry(file
, szCurFile
);
962 file
->attributes
= GetFileAttributesW( file
->szFullPath
);
963 file
->bExists
= (file
->attributes
!= INVALID_FILE_ATTRIBUTES
);
964 if (!file
->bExists
) flList
->bAnyDontExist
= TRUE
;
965 if (IsAttribDir(file
->attributes
)) flList
->bAnyDirectories
= TRUE
;
968 /* advance to the next string */
969 ptr
+= lstrlenW(ptr
) + 1;
972 flList
->dwNumFiles
= i
;
977 /* free the FILE_LIST */
978 static void destroy_file_list(FILE_LIST
*flList
)
982 if (!flList
|| !flList
->feFiles
)
985 for (i
= 0; i
< flList
->dwNumFiles
; i
++)
987 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szDirectory
);
988 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFilename
);
989 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFullPath
);
992 HeapFree(GetProcessHeap(), 0, flList
->feFiles
);
995 static void copy_dir_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, LPCWSTR szDestPath
)
997 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
998 SHFILEOPSTRUCTW fileOp
;
1000 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1002 if (IsDotDir(feFrom
->szFilename
))
1005 if (PathFileExistsW(szDestPath
))
1006 PathCombineW(szTo
, szDestPath
, feFrom
->szFilename
);
1008 lstrcpyW(szTo
, szDestPath
);
1010 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
)) {
1011 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FOLDER
, feFrom
->szFilename
, op
))
1013 /* Vista returns an ERROR_CANCELLED even if user pressed "No" */
1014 if (!op
->bManyItems
)
1015 op
->bCancelled
= TRUE
;
1020 szTo
[lstrlenW(szTo
) + 1] = '\0';
1021 SHNotifyCreateDirectoryW(szTo
, NULL
);
1023 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1024 szFrom
[lstrlenW(szFrom
) + 1] = '\0';
1027 fileOp
.pFrom
= szFrom
;
1029 fileOp
.fFlags
&= ~FOF_MULTIDESTFILES
; /* we know we're copying to one dir */
1031 /* Don't ask the user about overwriting files when he accepted to overwrite the
1032 folder. FIXME: this is not exactly what Windows does - e.g. there would be
1033 an additional confirmation for a nested folder */
1034 fileOp
.fFlags
|= FOF_NOCONFIRMATION
;
1036 SHFileOperationW(&fileOp
);
1039 static BOOL
copy_file_to_file(FILE_OPERATION
*op
, const WCHAR
*szFrom
, const WCHAR
*szTo
)
1041 if (!(op
->req
->fFlags
& FOF_NOCONFIRMATION
) && PathFileExistsW(szTo
))
1043 if (!SHELL_ConfirmDialogW(op
->req
->hwnd
, ASK_OVERWRITE_FILE
, PathFindFileNameW(szTo
), op
))
1047 return SHNotifyCopyFileW(szFrom
, szTo
, FALSE
) == 0;
1050 /* copy a file or directory to another directory */
1051 static void copy_to_dir(FILE_OPERATION
*op
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1053 if (!PathFileExistsW(feTo
->szFullPath
))
1054 SHNotifyCreateDirectoryW(feTo
->szFullPath
, NULL
);
1056 if (IsAttribFile(feFrom
->attributes
))
1058 WCHAR szDestPath
[MAX_PATH
];
1060 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1061 copy_file_to_file(op
, feFrom
->szFullPath
, szDestPath
);
1063 else if (!(op
->req
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1064 copy_dir_to_dir(op
, feFrom
, feTo
->szFullPath
);
1067 static void create_dest_dirs(LPCWSTR szDestDir
)
1069 WCHAR dir
[MAX_PATH
];
1070 LPCWSTR ptr
= StrChrW(szDestDir
, '\\');
1072 /* make sure all directories up to last one are created */
1073 while (ptr
&& (ptr
= StrChrW(ptr
+ 1, '\\')))
1075 lstrcpynW(dir
, szDestDir
, ptr
- szDestDir
+ 1);
1077 if (!PathFileExistsW(dir
))
1078 SHNotifyCreateDirectoryW(dir
, NULL
);
1081 /* create last directory */
1082 if (!PathFileExistsW(szDestDir
))
1083 SHNotifyCreateDirectoryW(szDestDir
, NULL
);
1086 /* the FO_COPY operation */
1087 static HRESULT
copy_files(FILE_OPERATION
*op
, const FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
1090 const FILE_ENTRY
*entryToCopy
;
1091 const FILE_ENTRY
*fileDest
= &flTo
->feFiles
[0];
1093 if (flFrom
->bAnyDontExist
)
1094 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1096 if (flTo
->dwNumFiles
== 0)
1098 /* If the destination is empty, SHFileOperation should use the current directory */
1099 WCHAR curdir
[MAX_PATH
+1];
1101 GetCurrentDirectoryW(MAX_PATH
, curdir
);
1102 curdir
[lstrlenW(curdir
)+1] = 0;
1104 destroy_file_list(flTo
);
1105 ZeroMemory(flTo
, sizeof(FILE_LIST
));
1106 parse_file_list(flTo
, curdir
);
1107 fileDest
= &flTo
->feFiles
[0];
1110 if (op
->req
->fFlags
& FOF_MULTIDESTFILES
)
1112 if (flFrom
->bAnyFromWildcard
)
1113 return ERROR_CANCELLED
;
1115 if (flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1117 if (flFrom
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1118 return ERROR_CANCELLED
;
1120 /* Free all but the first entry. */
1121 for (i
= 1; i
< flTo
->dwNumFiles
; i
++)
1123 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szDirectory
);
1124 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szFilename
);
1125 HeapFree(GetProcessHeap(), 0, flTo
->feFiles
[i
].szFullPath
);
1128 flTo
->dwNumFiles
= 1;
1130 else if (IsAttribDir(fileDest
->attributes
))
1132 for (i
= 1; i
< flTo
->dwNumFiles
; i
++)
1133 if (!IsAttribDir(flTo
->feFiles
[i
].attributes
) ||
1134 !IsAttribDir(flFrom
->feFiles
[i
].attributes
))
1136 return ERROR_CANCELLED
;
1140 else if (flFrom
->dwNumFiles
!= 1)
1142 if (flTo
->dwNumFiles
!= 1 && !IsAttribDir(fileDest
->attributes
))
1143 return ERROR_CANCELLED
;
1145 if (PathFileExistsW(fileDest
->szFullPath
) &&
1146 IsAttribFile(fileDest
->attributes
))
1148 return ERROR_CANCELLED
;
1151 if (flTo
->dwNumFiles
== 1 && fileDest
->bFromRelative
&&
1152 !PathFileExistsW(fileDest
->szFullPath
))
1154 return ERROR_CANCELLED
;
1158 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1160 entryToCopy
= &flFrom
->feFiles
[i
];
1162 if ((op
->req
->fFlags
& FOF_MULTIDESTFILES
) &&
1163 flTo
->dwNumFiles
> 1)
1165 fileDest
= &flTo
->feFiles
[i
];
1168 if (IsAttribDir(entryToCopy
->attributes
) &&
1169 !lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szDirectory
))
1171 return ERROR_SUCCESS
;
1174 create_dest_dirs(fileDest
->szDirectory
);
1176 if (!lstrcmpiW(entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1178 if (IsAttribFile(entryToCopy
->attributes
))
1179 return ERROR_NO_MORE_SEARCH_HANDLES
;
1181 return ERROR_SUCCESS
;
1184 if ((flFrom
->dwNumFiles
> 1 && flTo
->dwNumFiles
== 1) ||
1185 IsAttribDir(fileDest
->attributes
))
1187 copy_to_dir(op
, entryToCopy
, fileDest
);
1189 else if (IsAttribDir(entryToCopy
->attributes
))
1191 copy_dir_to_dir(op
, entryToCopy
, fileDest
->szFullPath
);
1195 if (!copy_file_to_file(op
, entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1197 op
->req
->fAnyOperationsAborted
= TRUE
;
1198 return ERROR_CANCELLED
;
1202 /* Vista return code. XP would return e.g. ERROR_FILE_NOT_FOUND, ERROR_ALREADY_EXISTS */
1204 return ERROR_CANCELLED
;
1207 /* Vista return code. On XP if the used pressed "No" for the last item,
1208 * ERROR_ARENA_TRASHED would be returned */
1209 return ERROR_SUCCESS
;
1212 static BOOL
confirm_delete_list(HWND hWnd
, DWORD fFlags
, BOOL fTrash
, const FILE_LIST
*flFrom
)
1214 if (flFrom
->dwNumFiles
> 1)
1217 const WCHAR format
[] = {'%','d',0};
1219 wnsprintfW(tmp
, sizeof(tmp
)/sizeof(tmp
[0]), format
, flFrom
->dwNumFiles
);
1220 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_MULTIPLE_ITEM
:ASK_DELETE_MULTIPLE_ITEM
), tmp
, NULL
);
1224 const FILE_ENTRY
*fileEntry
= &flFrom
->feFiles
[0];
1226 if (IsAttribFile(fileEntry
->attributes
))
1227 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FILE
:ASK_DELETE_FILE
), fileEntry
->szFullPath
, NULL
);
1228 else if (!(fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1229 return SHELL_ConfirmDialogW(hWnd
, (fTrash
?ASK_TRASH_FOLDER
:ASK_DELETE_FOLDER
), fileEntry
->szFullPath
, NULL
);
1234 /* the FO_DELETE operation */
1235 static HRESULT
delete_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
)
1237 const FILE_ENTRY
*fileEntry
;
1242 if (!flFrom
->dwNumFiles
)
1243 return ERROR_SUCCESS
;
1245 /* Windows also checks only the first item */
1246 bTrash
= (lpFileOp
->fFlags
& FOF_ALLOWUNDO
)
1247 && TRASH_CanTrashFile(flFrom
->feFiles
[0].szFullPath
);
1249 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (!bTrash
&& lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1250 if (!confirm_delete_list(lpFileOp
->hwnd
, lpFileOp
->fFlags
, bTrash
, flFrom
))
1252 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1256 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1259 fileEntry
= &flFrom
->feFiles
[i
];
1261 if (!IsAttribFile(fileEntry
->attributes
) &&
1262 (lpFileOp
->fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1268 if (TRASH_TrashFile(fileEntry
->szFullPath
))
1271 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1272 if (!(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
) || (lpFileOp
->fFlags
& FOF_WANTNUKEWARNING
))
1273 bDelete
= SHELL_ConfirmDialogW(lpFileOp
->hwnd
, ASK_CANT_TRASH_ITEM
, fileEntry
->szFullPath
, NULL
);
1279 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1284 /* delete the file or directory */
1285 if (IsAttribFile(fileEntry
->attributes
))
1286 bPathExists
= DeleteFileW(fileEntry
->szFullPath
);
1288 bPathExists
= SHELL_DeleteDirectoryW(lpFileOp
->hwnd
, fileEntry
->szFullPath
, FALSE
);
1291 return ERROR_PATH_NOT_FOUND
;
1294 return ERROR_SUCCESS
;
1297 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_ENTRY
*feFrom
, LPCWSTR szDestPath
)
1299 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
1300 SHFILEOPSTRUCTW fileOp
;
1302 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1304 if (IsDotDir(feFrom
->szFilename
))
1307 SHNotifyCreateDirectoryW(szDestPath
, NULL
);
1309 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1310 szFrom
[lstrlenW(szFrom
) + 1] = '\0';
1312 lstrcpyW(szTo
, szDestPath
);
1313 szTo
[lstrlenW(szDestPath
) + 1] = '\0';
1316 fileOp
.pFrom
= szFrom
;
1319 SHFileOperationW(&fileOp
);
1322 /* moves a file or directory to another directory */
1323 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_ENTRY
*feFrom
, const FILE_ENTRY
*feTo
)
1325 WCHAR szDestPath
[MAX_PATH
];
1327 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1329 if (IsAttribFile(feFrom
->attributes
))
1330 SHNotifyMoveFileW(feFrom
->szFullPath
, szDestPath
);
1331 else if (!(lpFileOp
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1332 move_dir_to_dir(lpFileOp
, feFrom
, szDestPath
);
1335 /* the FO_MOVE operation */
1336 static HRESULT
move_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
, const FILE_LIST
*flTo
)
1339 const FILE_ENTRY
*entryToMove
;
1340 const FILE_ENTRY
*fileDest
;
1342 if (!flFrom
->dwNumFiles
|| !flTo
->dwNumFiles
)
1343 return ERROR_CANCELLED
;
1345 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1346 flTo
->dwNumFiles
> 1 && flFrom
->dwNumFiles
> 1)
1348 return ERROR_CANCELLED
;
1351 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1352 !flFrom
->bAnyDirectories
&&
1353 flFrom
->dwNumFiles
> flTo
->dwNumFiles
)
1355 return ERROR_CANCELLED
;
1358 if (!PathFileExistsW(flTo
->feFiles
[0].szDirectory
))
1359 return ERROR_CANCELLED
;
1361 if ((lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1362 flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1364 return ERROR_CANCELLED
;
1367 fileDest
= &flTo
->feFiles
[0];
1368 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1370 entryToMove
= &flFrom
->feFiles
[i
];
1372 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
)
1373 fileDest
= &flTo
->feFiles
[i
];
1375 if (!PathFileExistsW(fileDest
->szDirectory
))
1376 return ERROR_CANCELLED
;
1378 if (fileDest
->bExists
&& IsAttribDir(fileDest
->attributes
))
1379 move_to_dir(lpFileOp
, entryToMove
, fileDest
);
1381 SHNotifyMoveFileW(entryToMove
->szFullPath
, fileDest
->szFullPath
);
1384 return ERROR_SUCCESS
;
1387 /* the FO_RENAME files */
1388 static HRESULT
rename_files(LPSHFILEOPSTRUCTW lpFileOp
, const FILE_LIST
*flFrom
, const FILE_LIST
*flTo
)
1390 const FILE_ENTRY
*feFrom
;
1391 const FILE_ENTRY
*feTo
;
1393 if (flFrom
->dwNumFiles
!= 1)
1394 return ERROR_GEN_FAILURE
;
1396 if (flTo
->dwNumFiles
!= 1)
1397 return ERROR_CANCELLED
;
1399 feFrom
= &flFrom
->feFiles
[0];
1400 feTo
= &flTo
->feFiles
[0];
1402 /* fail if destination doesn't exist */
1403 if (!feFrom
->bExists
)
1404 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1406 /* fail if destination already exists */
1408 return ERROR_ALREADY_EXISTS
;
1410 return SHNotifyMoveFileW(feFrom
->szFullPath
, feTo
->szFullPath
);
1413 /* alert the user if an unsupported flag is used */
1414 static void check_flags(FILEOP_FLAGS fFlags
)
1416 WORD wUnsupportedFlags
= FOF_NO_CONNECTED_ELEMENTS
|
1417 FOF_NOCOPYSECURITYATTRIBS
| FOF_NORECURSEREPARSE
|
1418 FOF_RENAMEONCOLLISION
| FOF_WANTMAPPINGHANDLE
;
1420 if (fFlags
& wUnsupportedFlags
)
1421 FIXME("Unsupported flags: %04x\n", fFlags
);
1424 /*************************************************************************
1425 * SHFileOperationW [SHELL32.@]
1427 * See SHFileOperationA
1429 int WINAPI
SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp
)
1432 FILE_LIST flFrom
, flTo
;
1436 return ERROR_INVALID_PARAMETER
;
1438 check_flags(lpFileOp
->fFlags
);
1440 ZeroMemory(&flFrom
, sizeof(FILE_LIST
));
1441 ZeroMemory(&flTo
, sizeof(FILE_LIST
));
1443 if ((ret
= parse_file_list(&flFrom
, lpFileOp
->pFrom
)))
1446 if (lpFileOp
->wFunc
!= FO_DELETE
)
1447 parse_file_list(&flTo
, lpFileOp
->pTo
);
1449 ZeroMemory(&op
, sizeof(op
));
1451 op
.bManyItems
= (flFrom
.dwNumFiles
> 1);
1453 switch (lpFileOp
->wFunc
)
1456 ret
= copy_files(&op
, &flFrom
, &flTo
);
1459 ret
= delete_files(lpFileOp
, &flFrom
);
1462 ret
= move_files(lpFileOp
, &flFrom
, &flTo
);
1465 ret
= rename_files(lpFileOp
, &flFrom
, &flTo
);
1468 ret
= ERROR_INVALID_PARAMETER
;
1472 destroy_file_list(&flFrom
);
1474 if (lpFileOp
->wFunc
!= FO_DELETE
)
1475 destroy_file_list(&flTo
);
1477 if (ret
== ERROR_CANCELLED
)
1478 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1483 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1485 /*************************************************************************
1486 * SHFreeNameMappings [shell32.246]
1488 * Free the mapping handle returned by SHFileOperation if FOF_WANTSMAPPINGHANDLE
1492 * hNameMapping [I] handle to the name mappings used during renaming of files
1497 void WINAPI
SHFreeNameMappings(HANDLE hNameMapping
)
1501 int i
= SHDSA_GetItemCount((HDSA
)hNameMapping
) - 1;
1505 LPSHNAMEMAPPINGW lp
= DSA_GetItemPtr(hNameMapping
, i
);
1507 SHFree(lp
->pszOldPath
);
1508 SHFree(lp
->pszNewPath
);
1510 DSA_Destroy(hNameMapping
);
1514 /*************************************************************************
1515 * SheGetDirA [SHELL32.@]
1517 * drive = 0: returns the current directory path
1518 * drive > 0: returns the current directory path of the specified drive
1519 * drive=1 -> A: drive=2 -> B: ...
1520 * returns 0 if successful
1522 DWORD WINAPI
SheGetDirA(DWORD drive
, LPSTR buffer
)
1524 WCHAR org_path
[MAX_PATH
];
1528 /* change current directory to the specified drive */
1530 strcpy(drv_path
, "A:");
1531 drv_path
[0] += (char)drive
-1;
1533 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1535 SetCurrentDirectoryA(drv_path
);
1538 /* query current directory path of the specified drive */
1539 ret
= GetCurrentDirectoryA(MAX_PATH
, buffer
);
1541 /* back to the original drive */
1543 SetCurrentDirectoryW(org_path
);
1546 return GetLastError();
1551 /*************************************************************************
1552 * SheGetDirW [SHELL32.@]
1554 * drive = 0: returns the current directory path
1555 * drive > 0: returns the current directory path of the specified drive
1556 * drive=1 -> A: drive=2 -> B: ...
1557 * returns 0 if successful
1559 DWORD WINAPI
SheGetDirW(DWORD drive
, LPWSTR buffer
)
1561 WCHAR org_path
[MAX_PATH
];
1565 /* change current directory to the specified drive */
1567 strcpy(drv_path
, "A:");
1568 drv_path
[0] += (char)drive
-1;
1570 GetCurrentDirectoryW(MAX_PATH
, org_path
);
1572 SetCurrentDirectoryA(drv_path
);
1575 /* query current directory path of the specified drive */
1576 ret
= GetCurrentDirectoryW(MAX_PATH
, buffer
);
1578 /* back to the original drive */
1580 SetCurrentDirectoryW(org_path
);
1583 return GetLastError();
1588 /*************************************************************************
1589 * SheChangeDirA [SHELL32.@]
1591 * changes the current directory to the specified path
1592 * and returns 0 if successful
1594 DWORD WINAPI
SheChangeDirA(LPSTR path
)
1596 if (SetCurrentDirectoryA(path
))
1599 return GetLastError();
1602 /*************************************************************************
1603 * SheChangeDirW [SHELL32.@]
1605 * changes the current directory to the specified path
1606 * and returns 0 if successful
1608 DWORD WINAPI
SheChangeDirW(LPWSTR path
)
1610 if (SetCurrentDirectoryW(path
))
1613 return GetLastError();
1616 /*************************************************************************
1617 * IsNetDrive [SHELL32.66]
1619 BOOL WINAPI
IsNetDrive(DWORD drive
)
1622 strcpy(root
, "A:\\");
1623 root
[0] += (char)drive
;
1624 return (GetDriveTypeA(root
) == DRIVE_REMOTE
);
1628 /*************************************************************************
1629 * RealDriveType [SHELL32.524]
1631 INT WINAPI
RealDriveType(INT drive
, BOOL bQueryNet
)
1633 char root
[] = "A:\\";
1634 root
[0] += (char)drive
;
1635 return GetDriveTypeA(root
);
1638 /***********************************************************************
1639 * SHPathPrepareForWriteW (SHELL32.@)
1641 HRESULT WINAPI
SHPathPrepareForWriteW(HWND hwnd
, IUnknown
*modless
, LPCWSTR path
, DWORD flags
)
1648 WCHAR
* temppath
=NULL
;
1650 TRACE("%p %p %s 0x%80x\n", hwnd
, modless
, debugstr_w(path
), flags
);
1652 if (flags
& ~(SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
|SHPPFW_IGNOREFILENAME
))
1653 FIXME("unimplemented flags 0x%08x\n", flags
);
1655 /* cut off filename if necessary */
1656 if (flags
& SHPPFW_IGNOREFILENAME
)
1658 last_slash
= StrRChrW(path
, NULL
, '\\');
1659 if (last_slash
== NULL
)
1662 len
= last_slash
- path
+ 1;
1663 temppath
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1665 return E_OUTOFMEMORY
;
1666 StrCpyNW(temppath
, path
, len
);
1667 realpath
= temppath
;
1674 /* try to create the directory if asked to */
1675 if (flags
& (SHPPFW_DIRCREATE
|SHPPFW_ASKDIRCREATE
))
1677 if (flags
& SHPPFW_ASKDIRCREATE
)
1678 FIXME("treating SHPPFW_ASKDIRCREATE as SHPPFW_DIRCREATE\n");
1680 SHCreateDirectoryExW(0, realpath
, NULL
);
1683 /* check if we can access the directory */
1684 res
= GetFileAttributesW(realpath
);
1686 HeapFree(GetProcessHeap(), 0, temppath
);
1688 if (res
== INVALID_FILE_ATTRIBUTES
)
1690 err
= GetLastError();
1691 if (err
== ERROR_FILE_NOT_FOUND
)
1692 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
1693 return HRESULT_FROM_WIN32(err
);
1695 else if (res
& FILE_ATTRIBUTE_DIRECTORY
)
1698 return HRESULT_FROM_WIN32(ERROR_DIRECTORY
);
1701 /***********************************************************************
1702 * SHPathPrepareForWriteA (SHELL32.@)
1704 HRESULT WINAPI
SHPathPrepareForWriteA(HWND hwnd
, IUnknown
*modless
, LPCSTR path
, DWORD flags
)
1706 WCHAR wpath
[MAX_PATH
];
1707 MultiByteToWideChar( CP_ACP
, 0, path
, -1, wpath
, MAX_PATH
);
1708 return SHPathPrepareForWriteW(hwnd
, modless
, wpath
, flags
);