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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
39 #define NO_SHLWAPI_STREAM
41 #include "shell32_main.h"
42 #include "undocshell.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
48 #define IsAttrib(x, y) ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
49 #define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
50 #define IsAttribDir(x) IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
51 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
55 static const WCHAR wWildcardFile
[] = {'*',0};
56 static const WCHAR wWildcardChars
[] = {'*','?',0};
58 static BOOL
SHELL_DeleteDirectoryW(LPCWSTR path
, BOOL bShowUI
);
59 static DWORD
SHNotifyCreateDirectoryA(LPCSTR path
, LPSECURITY_ATTRIBUTES sec
);
60 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
);
61 static DWORD
SHNotifyRemoveDirectoryA(LPCSTR path
);
62 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
);
63 static DWORD
SHNotifyDeleteFileA(LPCSTR path
);
64 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
);
65 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
);
66 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bFailIfExists
);
67 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
);
71 UINT caption_resource_id
, text_resource_id
;
72 } SHELL_ConfirmIDstruc
;
74 static BOOL
SHELL_ConfirmIDs(int nKindOfDialog
, SHELL_ConfirmIDstruc
*ids
)
76 switch (nKindOfDialog
) {
78 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
79 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
81 case ASK_DELETE_FOLDER
:
82 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
83 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
85 case ASK_DELETE_MULTIPLE_ITEM
:
86 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
87 ids
->text_resource_id
= IDS_DELETEMULTIPLE_TEXT
;
89 case ASK_OVERWRITE_FILE
:
90 ids
->caption_resource_id
= IDS_OVERWRITEFILE_CAPTION
;
91 ids
->text_resource_id
= IDS_OVERWRITEFILE_TEXT
;
94 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog
);
99 BOOL
SHELL_ConfirmDialog(int nKindOfDialog
, LPCSTR szDir
)
101 CHAR szCaption
[255], szText
[255], szBuffer
[MAX_PATH
+ 256];
102 SHELL_ConfirmIDstruc ids
;
104 if (!SHELL_ConfirmIDs(nKindOfDialog
, &ids
))
107 LoadStringA(shell32_hInstance
, ids
.caption_resource_id
, szCaption
, sizeof(szCaption
));
108 LoadStringA(shell32_hInstance
, ids
.text_resource_id
, szText
, sizeof(szText
));
110 FormatMessageA(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
111 szText
, 0, 0, szBuffer
, sizeof(szBuffer
), (va_list*)&szDir
);
113 return (IDOK
== MessageBoxA(GetActiveWindow(), szBuffer
, szCaption
, MB_OKCANCEL
| MB_ICONEXCLAMATION
));
116 BOOL
SHELL_ConfirmDialogW(int nKindOfDialog
, LPCWSTR szDir
)
118 WCHAR szCaption
[255], szText
[255], szBuffer
[MAX_PATH
+ 256];
119 SHELL_ConfirmIDstruc ids
;
121 if (!SHELL_ConfirmIDs(nKindOfDialog
, &ids
))
124 LoadStringW(shell32_hInstance
, ids
.caption_resource_id
, szCaption
, sizeof(szCaption
));
125 LoadStringW(shell32_hInstance
, ids
.text_resource_id
, szText
, sizeof(szText
));
127 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
128 szText
, 0, 0, szBuffer
, sizeof(szBuffer
), (va_list*)&szDir
);
130 return (IDOK
== MessageBoxW(GetActiveWindow(), szBuffer
, szCaption
, MB_OKCANCEL
| MB_ICONEXCLAMATION
));
133 static DWORD
SHELL32_AnsiToUnicodeBuf(LPCSTR aPath
, LPWSTR
*wPath
, DWORD minChars
)
135 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, NULL
, 0);
140 *wPath
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
143 MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, *wPath
, len
);
146 return E_OUTOFMEMORY
;
149 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath
)
151 HeapFree(GetProcessHeap(), 0, wPath
);
154 /**************************************************************************
155 * SHELL_DeleteDirectory() [internal]
157 * Asks for confirmation when bShowUI is true and deletes the directory and
158 * all its subdirectories and files if necessary.
160 BOOL
SHELL_DeleteDirectoryA(LPCSTR pszDir
, BOOL bShowUI
)
165 if (!SHELL32_AnsiToUnicodeBuf(pszDir
, &wPath
, 0))
167 ret
= SHELL_DeleteDirectoryW(wPath
, bShowUI
);
168 SHELL32_FreeUnicodeBuf(wPath
);
173 static BOOL
SHELL_DeleteDirectoryW(LPCWSTR pszDir
, BOOL bShowUI
)
177 WIN32_FIND_DATAW wfd
;
178 WCHAR szTemp
[MAX_PATH
];
180 /* Make sure the directory exists before eventually prompting the user */
181 PathCombineW(szTemp
, pszDir
, wWildcardFile
);
182 hFind
= FindFirstFileW(szTemp
, &wfd
);
183 if (hFind
== INVALID_HANDLE_VALUE
)
186 if (!bShowUI
|| (ret
= SHELL_ConfirmDialogW(ASK_DELETE_FOLDER
, pszDir
)))
190 LPWSTR lp
= wfd
.cAlternateFileName
;
195 PathCombineW(szTemp
, pszDir
, lp
);
196 if (FILE_ATTRIBUTE_DIRECTORY
& wfd
.dwFileAttributes
)
197 ret
= SHELL_DeleteDirectoryW(szTemp
, FALSE
);
199 ret
= (SHNotifyDeleteFileW(szTemp
) == ERROR_SUCCESS
);
200 } while (ret
&& FindNextFileW(hFind
, &wfd
));
204 ret
= (SHNotifyRemoveDirectoryW(pszDir
) == ERROR_SUCCESS
);
208 /**************************************************************************
209 * SHELL_DeleteFileA() [internal]
211 BOOL
SHELL_DeleteFileA(LPCSTR pszFile
, BOOL bShowUI
)
213 if (bShowUI
&& !SHELL_ConfirmDialog(ASK_DELETE_FILE
, pszFile
))
216 return (SHNotifyDeleteFileA(pszFile
) == ERROR_SUCCESS
);
219 BOOL
SHELL_DeleteFileW(LPCWSTR pszFile
, BOOL bShowUI
)
221 if (bShowUI
&& !SHELL_ConfirmDialogW(ASK_DELETE_FILE
, pszFile
))
224 return (SHNotifyDeleteFileW(pszFile
) == ERROR_SUCCESS
);
227 /**************************************************************************
228 * Win32CreateDirectory [SHELL32.93]
230 * Creates a directory. Also triggers a change notify if one exists.
233 * path [I] path to directory to create
236 * TRUE if successful, FALSE otherwise
239 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
240 * This is Unicode on NT/2000
242 static DWORD
SHNotifyCreateDirectoryA(LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
247 TRACE("(%s, %p)\n", debugstr_a(path
), sec
);
249 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
252 retCode
= SHNotifyCreateDirectoryW(wPath
, sec
);
253 SHELL32_FreeUnicodeBuf(wPath
);
258 /**********************************************************************/
260 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
262 TRACE("(%s, %p)\n", debugstr_w(path
), sec
);
264 if (CreateDirectoryW(path
, sec
))
266 SHChangeNotify(SHCNE_MKDIR
, SHCNF_PATHW
, path
, NULL
);
267 return ERROR_SUCCESS
;
269 return GetLastError();
272 /**********************************************************************/
274 BOOL WINAPI
Win32CreateDirectoryAW(LPCVOID path
, LPSECURITY_ATTRIBUTES sec
)
276 if (SHELL_OsIsUnicode())
277 return (SHNotifyCreateDirectoryW(path
, sec
) == ERROR_SUCCESS
);
278 return (SHNotifyCreateDirectoryA(path
, sec
) == ERROR_SUCCESS
);
281 /************************************************************************
282 * Win32RemoveDirectory [SHELL32.94]
284 * Deletes a directory. Also triggers a change notify if one exists.
287 * path [I] path to directory to delete
290 * TRUE if successful, FALSE otherwise
293 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
294 * This is Unicode on NT/2000
296 static DWORD
SHNotifyRemoveDirectoryA(LPCSTR path
)
301 TRACE("(%s)\n", debugstr_a(path
));
303 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
306 retCode
= SHNotifyRemoveDirectoryW(wPath
);
307 SHELL32_FreeUnicodeBuf(wPath
);
312 /***********************************************************************/
314 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
)
317 TRACE("(%s)\n", debugstr_w(path
));
319 ret
= RemoveDirectoryW(path
);
322 /* Directory may be write protected */
323 DWORD dwAttr
= GetFileAttributesW(path
);
324 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
))
325 if (SetFileAttributesW(path
, dwAttr
& ~FILE_ATTRIBUTE_READONLY
))
326 ret
= RemoveDirectoryW(path
);
330 SHChangeNotify(SHCNE_RMDIR
, SHCNF_PATHW
, path
, NULL
);
331 return ERROR_SUCCESS
;
333 return GetLastError();
336 /***********************************************************************/
338 BOOL WINAPI
Win32RemoveDirectoryAW(LPCVOID path
)
340 if (SHELL_OsIsUnicode())
341 return (SHNotifyRemoveDirectoryW(path
) == ERROR_SUCCESS
);
342 return (SHNotifyRemoveDirectoryA(path
) == ERROR_SUCCESS
);
345 /************************************************************************
346 * Win32DeleteFile [SHELL32.164]
348 * Deletes a file. Also triggers a change notify if one exists.
351 * path [I] path to file to delete
354 * TRUE if successful, FALSE otherwise
357 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
358 * This is Unicode on NT/2000
360 static DWORD
SHNotifyDeleteFileA(LPCSTR path
)
365 TRACE("(%s)\n", debugstr_a(path
));
367 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
370 retCode
= SHNotifyDeleteFileW(wPath
);
371 SHELL32_FreeUnicodeBuf(wPath
);
376 /***********************************************************************/
378 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
)
382 TRACE("(%s)\n", debugstr_w(path
));
384 ret
= DeleteFileW(path
);
387 /* File may be write protected or a system file */
388 DWORD dwAttr
= GetFileAttributesW(path
);
389 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
390 if (SetFileAttributesW(path
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
391 ret
= DeleteFileW(path
);
395 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, path
, NULL
);
396 return ERROR_SUCCESS
;
398 return GetLastError();
401 /***********************************************************************/
403 DWORD WINAPI
Win32DeleteFileAW(LPCVOID path
)
405 if (SHELL_OsIsUnicode())
406 return (SHNotifyDeleteFileW(path
) == ERROR_SUCCESS
);
407 return (SHNotifyDeleteFileA(path
) == ERROR_SUCCESS
);
410 /************************************************************************
411 * SHNotifyMoveFile [internal]
413 * Moves a file. Also triggers a change notify if one exists.
416 * src [I] path to source file to move
417 * dest [I] path to target file to move to
420 * ERORR_SUCCESS if successful
422 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
)
426 TRACE("(%s %s)\n", debugstr_w(src
), debugstr_w(dest
));
428 ret
= MoveFileExW(src
, dest
, MOVEFILE_REPLACE_EXISTING
);
430 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
432 ret
= MoveFileW(src
, dest
);
438 dwAttr
= SHFindAttrW(dest
, FALSE
);
439 if (INVALID_FILE_ATTRIBUTES
== dwAttr
)
441 /* Source file may be write protected or a system file */
442 dwAttr
= GetFileAttributesW(src
);
443 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
444 if (SetFileAttributesW(src
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
445 ret
= MoveFileW(src
, dest
);
450 SHChangeNotify(SHCNE_RENAMEITEM
, SHCNF_PATHW
, src
, dest
);
451 return ERROR_SUCCESS
;
453 return GetLastError();
456 /************************************************************************
457 * SHNotifyCopyFile [internal]
459 * Copies a file. Also triggers a change notify if one exists.
462 * src [I] path to source file to move
463 * dest [I] path to target file to move to
464 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
465 * a file with this name already exists
468 * ERROR_SUCCESS if successful
470 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bFailIfExists
)
474 TRACE("(%s %s %s)\n", debugstr_w(src
), debugstr_w(dest
), bFailIfExists
? "failIfExists" : "");
476 ret
= CopyFileW(src
, dest
, bFailIfExists
);
479 SHChangeNotify(SHCNE_CREATE
, SHCNF_PATHW
, dest
, NULL
);
480 return ERROR_SUCCESS
;
483 return GetLastError();
486 /*************************************************************************
487 * SHCreateDirectory [SHELL32.165]
489 * This function creates a file system folder whose fully qualified path is
490 * given by path. If one or more of the intermediate folders do not exist,
491 * they will be created as well.
495 * path [I] path of directory to create
498 * ERRROR_SUCCESS or one of the following values:
499 * ERROR_BAD_PATHNAME if the path is relative
500 * ERROR_FILE_EXISTS when a file with that name exists
501 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
502 * ERROR_INVLID_NAME if the path contains invalid chars
503 * ERROR_ALREADY_EXISTS when the directory already exists
504 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
507 * exported by ordinal
509 * WinNT/2000 exports Unicode
511 DWORD WINAPI
SHCreateDirectory(HWND hWnd
, LPCVOID path
)
513 if (SHELL_OsIsUnicode())
514 return SHCreateDirectoryExW(hWnd
, path
, NULL
);
515 return SHCreateDirectoryExA(hWnd
, path
, NULL
);
518 /*************************************************************************
519 * SHCreateDirectoryExA [SHELL32.@]
521 * This function creates a file system folder whose fully qualified path is
522 * given by path. If one or more of the intermediate folders do not exist,
523 * they will be created as well.
527 * path [I] path of directory to create
528 * sec [I] security attributes to use or NULL
531 * ERRROR_SUCCESS or one of the following values:
532 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
533 * ERROR_INVLID_NAME if the path contains invalid chars
534 * ERROR_FILE_EXISTS when a file with that name exists
535 * ERROR_ALREADY_EXISTS when the directory already exists
536 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
538 * FIXME: Not implemented yet;
539 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
540 * if the path is a network path to deal with network drivers which might have a limited
541 * but unknown maximum path length. If not:
543 * If hWnd is set to a valid window handle, a message box is displayed warning
544 * the user that the files may not be accessible. If the user chooses not to
545 * proceed, the function returns ERROR_CANCELLED.
547 * If hWnd is set to NULL, no user interface is displayed and the function
548 * returns ERROR_CANCELLED.
550 int WINAPI
SHCreateDirectoryExA(HWND hWnd
, LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
555 TRACE("(%s, %p)\n", debugstr_a(path
), sec
);
557 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
560 retCode
= SHCreateDirectoryExW(hWnd
, wPath
, sec
);
561 SHELL32_FreeUnicodeBuf(wPath
);
566 /*************************************************************************
567 * SHCreateDirectoryExW [SHELL32.@]
569 * See SHCreateDirectoryExA.
571 int WINAPI
SHCreateDirectoryExW(HWND hWnd
, LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
573 int ret
= ERROR_BAD_PATHNAME
;
574 TRACE("(%p, %s, %p)\n", hWnd
, debugstr_w(path
), sec
);
576 if (PathIsRelativeW(path
))
582 ret
= SHNotifyCreateDirectoryW(path
, sec
);
583 /* Refuse to work on certain error codes before trying to create directories recursively */
584 if (ret
!= ERROR_FILE_EXISTS
&&
585 ret
!= ERROR_ALREADY_EXISTS
&&
586 ret
!= ERROR_FILENAME_EXCED_RANGE
)
588 WCHAR
*pEnd
, *pSlash
, szTemp
[MAX_PATH
+ 1]; /* extra for PathAddBackslash() */
590 lstrcpynW(szTemp
, path
, MAX_PATH
);
591 pEnd
= PathAddBackslashW(szTemp
);
596 while (*pSlash
&& *pSlash
!= '\\')
597 pSlash
= CharNextW(pSlash
);
601 *pSlash
= 0; /* terminate path at separator */
603 ret
= SHNotifyCreateDirectoryW(szTemp
, pSlash
+ 1 == pEnd
? sec
: NULL
);
605 *pSlash
++ = '\\'; /* put the separator back */
609 if (ret
&& hWnd
&& (ERROR_CANCELLED
!= ret
))
611 /* We failed and should show a dialog box */
612 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path
), ret
);
613 ret
= ERROR_CANCELLED
; /* Error has been already presented to user (not really yet!) */
619 /*************************************************************************
620 * SHFindAttrW [internal]
622 * Get the Attributes for a file or directory. The difference to GetAttributes()
623 * is that this function will also work for paths containing wildcard characters
627 * path [I] path of directory or file to check
628 * fileOnly [I] TRUE if only files should be found
631 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
632 * the first file or directory found otherwise
634 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
)
636 WIN32_FIND_DATAW wfd
;
637 BOOL b_FileMask
= fileOnly
&& (NULL
!= StrPBrkW(pName
, wWildcardChars
));
638 DWORD dwAttr
= INVALID_FILE_ATTRIBUTES
;
639 HANDLE hFind
= FindFirstFileW(pName
, &wfd
);
641 TRACE("%s %d\n", debugstr_w(pName
), fileOnly
);
642 if (INVALID_HANDLE_VALUE
!= hFind
)
646 if (b_FileMask
&& IsAttribDir(wfd
.dwFileAttributes
))
648 dwAttr
= wfd
.dwFileAttributes
;
651 while (FindNextFileW(hFind
, &wfd
));
657 /*************************************************************************
659 * SHNameTranslate HelperFunction for SHFileOperationA
661 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
662 * is NULL, only the necessary size of the string is determined and returned,
663 * otherwise the ASCII strings are copied into it and the buffer is increased
664 * to point to the location after the final 0 termination char.
666 DWORD
SHNameTranslate(LPWSTR
* wString
, LPCWSTR
* pWToFrom
, BOOL more
)
668 DWORD size
= 0, aSize
= 0;
669 LPCSTR aString
= (LPCSTR
)*pWToFrom
;
675 size
= lstrlenA(aString
) + 1;
678 } while ((size
!= 1) && more
);
679 /* The two sizes might be different in the case of multibyte chars */
680 size
= MultiByteToWideChar(CP_ACP
, 0, aString
, aSize
, *wString
, 0);
681 if (*wString
) /* only in the second loop */
683 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, size
);
684 *pWToFrom
= *wString
;
690 /*************************************************************************
691 * SHFileOperationA [SHELL32.@]
693 * Function to copy, move, delete and create one or more files with optional
697 * lpFileOp [I/O] pointer to a structure containing all the necessary information
700 * Success: ERROR_SUCCESS.
701 * Failure: ERROR_CANCELLED.
706 int WINAPI
SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp
)
708 SHFILEOPSTRUCTW nFileOp
= *((LPSHFILEOPSTRUCTW
)lpFileOp
);
711 LPWSTR ForFree
= NULL
, /* we change wString in SHNameTranslate and can't use it for freeing */
712 wString
= NULL
; /* we change this in SHNameTranslate */
715 if (FO_DELETE
== (nFileOp
.wFunc
& FO_MASK
))
716 nFileOp
.pTo
= NULL
; /* we need a NULL or a valid pointer for translation */
717 if (!(nFileOp
.fFlags
& FOF_SIMPLEPROGRESS
))
718 nFileOp
.lpszProgressTitle
= NULL
; /* we need a NULL or a valid pointer for translation */
719 while (1) /* every loop calculate size, second translate also, if we have storage for this */
721 size
= SHNameTranslate(&wString
, &nFileOp
.lpszProgressTitle
, FALSE
); /* no loop */
722 size
+= SHNameTranslate(&wString
, &nFileOp
.pFrom
, TRUE
); /* internal loop */
723 size
+= SHNameTranslate(&wString
, &nFileOp
.pTo
, TRUE
); /* internal loop */
727 retCode
= SHFileOperationW(&nFileOp
);
728 HeapFree(GetProcessHeap(), 0, ForFree
); /* we cannot use wString, it was changed */
733 wString
= ForFree
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
734 if (ForFree
) continue;
735 retCode
= ERROR_OUTOFMEMORY
;
736 nFileOp
.fAnyOperationsAborted
= TRUE
;
737 SetLastError(retCode
);
742 lpFileOp
->hNameMappings
= nFileOp
.hNameMappings
;
743 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
747 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
765 BOOL bAnyFromWildcard
;
766 BOOL bAnyDirectories
;
771 static inline void grow_list(FILE_LIST
*list
)
773 FILE_ENTRY
*new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, list
->feFiles
,
774 list
->num_alloc
* 2 * sizeof(*new) );
776 list
->num_alloc
*= 2;
779 /* adds a file to the FILE_ENTRY struct
781 static void add_file_to_entry(FILE_ENTRY
*feFile
, LPWSTR szFile
)
783 DWORD dwLen
= strlenW(szFile
) + 1;
786 feFile
->szFullPath
= HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
787 strcpyW(feFile
->szFullPath
, szFile
);
789 ptr
= StrRChrW(szFile
, NULL
, '\\');
792 dwLen
= ptr
- szFile
+ 1;
793 feFile
->szDirectory
= HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
794 lstrcpynW(feFile
->szDirectory
, szFile
, dwLen
);
796 dwLen
= strlenW(feFile
->szFullPath
) - dwLen
+ 1;
797 feFile
->szFilename
= HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
798 strcpyW(feFile
->szFilename
, ptr
+ 1); /* skip over backslash */
800 feFile
->bFromWildcard
= FALSE
;
803 static LPWSTR
wildcard_to_file(LPWSTR szWildCard
, LPWSTR szFileName
)
805 LPWSTR szFullPath
, ptr
;
806 DWORD dwDirLen
, dwFullLen
;
808 ptr
= StrRChrW(szWildCard
, NULL
, '\\');
809 dwDirLen
= ptr
- szWildCard
+ 1;
811 dwFullLen
= dwDirLen
+ strlenW(szFileName
) + 1;
812 szFullPath
= HeapAlloc(GetProcessHeap(), 0, dwFullLen
* sizeof(WCHAR
));
814 lstrcpynW(szFullPath
, szWildCard
, dwDirLen
+ 1);
815 strcatW(szFullPath
, szFileName
);
820 static void parse_wildcard_files(FILE_LIST
*flList
, LPWSTR szFile
, LPDWORD pdwListIndex
)
822 WIN32_FIND_DATAW wfd
;
823 HANDLE hFile
= FindFirstFileW(szFile
, &wfd
);
828 for (res
= TRUE
; res
; res
= FindNextFileW(hFile
, &wfd
))
830 if (IsDotDir(wfd
.cFileName
)) continue;
831 if (*pdwListIndex
>= flList
->num_alloc
) grow_list( flList
);
832 szFullPath
= wildcard_to_file(szFile
, wfd
.cFileName
);
833 file
= &flList
->feFiles
[(*pdwListIndex
)++];
834 add_file_to_entry(file
, szFullPath
);
835 file
->bFromWildcard
= TRUE
;
836 file
->attributes
= wfd
.dwFileAttributes
;
837 if (IsAttribDir(file
->attributes
)) flList
->bAnyDirectories
= TRUE
;
838 HeapFree(GetProcessHeap(), 0, szFullPath
);
844 /* takes the null-separated file list and fills out the FILE_LIST */
845 static HRESULT
parse_file_list(FILE_LIST
*flList
, LPCWSTR szFiles
)
847 LPCWSTR ptr
= szFiles
;
848 WCHAR szCurFile
[MAX_PATH
];
849 DWORD i
= 0, dwDirLen
;
852 return ERROR_INVALID_PARAMETER
;
854 flList
->bAnyFromWildcard
= FALSE
;
855 flList
->bAnyDirectories
= FALSE
;
856 flList
->bAnyDontExist
= FALSE
;
857 flList
->num_alloc
= 32;
858 flList
->dwNumFiles
= 0;
862 return ERROR_ACCESS_DENIED
;
864 flList
->feFiles
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
865 flList
->num_alloc
* sizeof(FILE_ENTRY
));
869 if (i
>= flList
->num_alloc
) grow_list( flList
);
871 /* change relative to absolute path */
872 if (PathIsRelativeW(ptr
))
874 dwDirLen
= GetCurrentDirectoryW(MAX_PATH
, szCurFile
) + 1;
875 PathCombineW(szCurFile
, szCurFile
, ptr
);
876 flList
->feFiles
[i
].bFromRelative
= TRUE
;
880 strcpyW(szCurFile
, ptr
);
881 flList
->feFiles
[i
].bFromRelative
= FALSE
;
884 /* parse wildcard files if they are in the filename */
885 if (StrPBrkW(szCurFile
, wWildcardChars
))
887 parse_wildcard_files(flList
, szCurFile
, &i
);
888 flList
->bAnyFromWildcard
= TRUE
;
893 FILE_ENTRY
*file
= &flList
->feFiles
[i
];
894 add_file_to_entry(file
, szCurFile
);
895 file
->attributes
= GetFileAttributesW( file
->szFullPath
);
896 file
->bExists
= (file
->attributes
!= INVALID_FILE_ATTRIBUTES
);
897 if (!file
->bExists
) flList
->bAnyDontExist
= TRUE
;
898 if (IsAttribDir(file
->attributes
)) flList
->bAnyDirectories
= TRUE
;
901 /* advance to the next string */
902 ptr
+= strlenW(ptr
) + 1;
905 flList
->dwNumFiles
= i
;
910 /* free the FILE_LIST */
911 static void destroy_file_list(FILE_LIST
*flList
)
915 if (!flList
|| !flList
->feFiles
)
918 for (i
= 0; i
< flList
->dwNumFiles
; i
++)
920 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szDirectory
);
921 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFilename
);
922 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFullPath
);
925 HeapFree(GetProcessHeap(), 0, flList
->feFiles
);
928 static void copy_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, FILE_ENTRY
*feFrom
, LPWSTR szDestPath
)
930 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
931 SHFILEOPSTRUCTW fileOp
;
933 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
935 if (IsDotDir(feFrom
->szFilename
))
938 if (PathFileExistsW(szDestPath
))
939 PathCombineW(szTo
, szDestPath
, feFrom
->szFilename
);
941 strcpyW(szTo
, szDestPath
);
943 szTo
[strlenW(szTo
) + 1] = '\0';
944 SHNotifyCreateDirectoryW(szTo
, NULL
);
946 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
947 szFrom
[strlenW(szFrom
) + 1] = '\0';
949 memcpy(&fileOp
, lpFileOp
, sizeof(fileOp
));
950 fileOp
.pFrom
= szFrom
;
952 fileOp
.fFlags
&= ~FOF_MULTIDESTFILES
; /* we know we're copying to one dir */
954 SHFileOperationW(&fileOp
);
957 /* copy a file or directory to another directory */
958 static void copy_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, FILE_ENTRY
*feFrom
, FILE_ENTRY
*feTo
)
960 WCHAR szDestPath
[MAX_PATH
];
962 if (!PathFileExistsW(feTo
->szFullPath
))
963 SHNotifyCreateDirectoryW(feTo
->szFullPath
, NULL
);
965 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
967 if (IsAttribFile(feFrom
->attributes
))
968 SHNotifyCopyFileW(feFrom
->szFullPath
, szDestPath
, FALSE
);
969 else if (!(lpFileOp
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
970 copy_dir_to_dir(lpFileOp
, feFrom
, szDestPath
);
973 static void create_dest_dirs(LPWSTR szDestDir
)
976 LPWSTR ptr
= StrChrW(szDestDir
, '\\');
978 /* make sure all directories up to last one are created */
979 while (ptr
&& (ptr
= StrChrW(ptr
+ 1, '\\')))
981 lstrcpynW(dir
, szDestDir
, ptr
- szDestDir
+ 1);
983 if (!PathFileExistsW(dir
))
984 SHNotifyCreateDirectoryW(dir
, NULL
);
987 /* create last directory */
988 if (!PathFileExistsW(szDestDir
))
989 SHNotifyCreateDirectoryW(szDestDir
, NULL
);
992 /* the FO_COPY operation */
993 static HRESULT
copy_files(LPSHFILEOPSTRUCTW lpFileOp
, FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
996 FILE_ENTRY
*entryToCopy
;
997 FILE_ENTRY
*fileDest
= &flTo
->feFiles
[0];
998 BOOL bCancelIfAnyDirectories
= FALSE
;
1000 if (flFrom
->bAnyDontExist
)
1001 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1003 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
&& flFrom
->bAnyFromWildcard
)
1004 return ERROR_CANCELLED
;
1006 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) && flTo
->dwNumFiles
!= 1)
1007 return ERROR_CANCELLED
;
1009 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
&& flFrom
->dwNumFiles
!= 1 &&
1010 flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1012 return ERROR_CANCELLED
;
1015 if (flFrom
->dwNumFiles
> 1 && flTo
->dwNumFiles
== 1 &&
1016 !PathFileExistsW(flTo
->feFiles
[0].szFullPath
) &&
1017 IsAttribFile(fileDest
->attributes
))
1019 bCancelIfAnyDirectories
= TRUE
;
1022 if (flFrom
->dwNumFiles
> 1 && flTo
->dwNumFiles
== 1 && fileDest
->bFromRelative
&&
1023 !PathFileExistsW(fileDest
->szFullPath
))
1025 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1026 return ERROR_CANCELLED
;
1029 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) && flFrom
->dwNumFiles
!= 1 &&
1030 PathFileExistsW(fileDest
->szFullPath
) &&
1031 IsAttribFile(fileDest
->attributes
))
1033 return ERROR_CANCELLED
;
1036 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1038 entryToCopy
= &flFrom
->feFiles
[i
];
1040 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
)
1041 fileDest
= &flTo
->feFiles
[i
];
1043 if (IsAttribDir(entryToCopy
->attributes
) &&
1044 !lstrcmpW(entryToCopy
->szFullPath
, fileDest
->szDirectory
))
1046 return ERROR_SUCCESS
;
1049 if (IsAttribDir(entryToCopy
->attributes
) && bCancelIfAnyDirectories
)
1050 return ERROR_CANCELLED
;
1052 create_dest_dirs(fileDest
->szDirectory
);
1054 if (!strcmpW(entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1056 if (IsAttribFile(entryToCopy
->attributes
))
1057 return ERROR_NO_MORE_SEARCH_HANDLES
;
1059 return ERROR_SUCCESS
;
1062 if ((flFrom
->dwNumFiles
> 1 && flTo
->dwNumFiles
== 1) ||
1063 (flFrom
->dwNumFiles
== 1 && IsAttribDir(fileDest
->attributes
)))
1065 copy_to_dir(lpFileOp
, entryToCopy
, fileDest
);
1067 else if (IsAttribDir(entryToCopy
->attributes
))
1069 copy_dir_to_dir(lpFileOp
, entryToCopy
, fileDest
->szFullPath
);
1073 if (SHNotifyCopyFileW(entryToCopy
->szFullPath
, fileDest
->szFullPath
, TRUE
))
1075 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1076 return ERROR_CANCELLED
;
1081 return ERROR_SUCCESS
;
1084 /* the FO_DELETE operation */
1085 static HRESULT
delete_files(LPSHFILEOPSTRUCTW lpFileOp
, FILE_LIST
*flFrom
)
1087 FILE_ENTRY
*fileEntry
;
1090 BOOL bConfirm
= !(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
);
1092 if (!flFrom
->dwNumFiles
)
1093 return ERROR_SUCCESS
;
1095 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1098 fileEntry
= &flFrom
->feFiles
[i
];
1100 /* delete the file or directory */
1101 if (IsAttribFile(fileEntry
->attributes
))
1102 bPathExists
= DeleteFileW(fileEntry
->szFullPath
);
1103 else if (!(lpFileOp
->fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1104 bPathExists
= SHELL_DeleteDirectoryW(fileEntry
->szFullPath
, bConfirm
);
1107 return ERROR_PATH_NOT_FOUND
;
1110 return ERROR_SUCCESS
;
1113 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, FILE_ENTRY
*feFrom
, LPWSTR szDestPath
)
1115 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
1116 SHFILEOPSTRUCTW fileOp
;
1118 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1120 if (IsDotDir(feFrom
->szFilename
))
1123 SHNotifyCreateDirectoryW(szDestPath
, NULL
);
1125 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1126 szFrom
[strlenW(szFrom
) + 1] = '\0';
1128 strcpyW(szTo
, szDestPath
);
1129 szTo
[strlenW(szDestPath
) + 1] = '\0';
1131 memcpy(&fileOp
, lpFileOp
, sizeof(fileOp
));
1132 fileOp
.pFrom
= szFrom
;
1135 SHFileOperationW(&fileOp
);
1138 /* moves a file or directory to another directory */
1139 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, FILE_ENTRY
*feFrom
, FILE_ENTRY
*feTo
)
1141 WCHAR szDestPath
[MAX_PATH
];
1143 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1145 if (IsAttribFile(feFrom
->attributes
))
1146 SHNotifyMoveFileW(feFrom
->szFullPath
, szDestPath
);
1147 else if (!(lpFileOp
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1148 move_dir_to_dir(lpFileOp
, feFrom
, szDestPath
);
1151 /* the FO_MOVE operation */
1152 static HRESULT
move_files(LPSHFILEOPSTRUCTW lpFileOp
, FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
1155 FILE_ENTRY
*entryToMove
;
1156 FILE_ENTRY
*fileDest
;
1158 if (!flFrom
->dwNumFiles
|| !flTo
->dwNumFiles
)
1159 return ERROR_CANCELLED
;
1161 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1162 flTo
->dwNumFiles
> 1 && flFrom
->dwNumFiles
> 1)
1164 return ERROR_CANCELLED
;
1167 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1168 !flFrom
->bAnyDirectories
&&
1169 flFrom
->dwNumFiles
> flTo
->dwNumFiles
)
1171 return ERROR_CANCELLED
;
1174 if (!PathFileExistsW(flTo
->feFiles
[0].szDirectory
))
1175 return ERROR_CANCELLED
;
1177 if ((lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1178 flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1180 return ERROR_CANCELLED
;
1183 fileDest
= &flTo
->feFiles
[0];
1184 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1186 entryToMove
= &flFrom
->feFiles
[i
];
1188 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
)
1189 fileDest
= &flTo
->feFiles
[i
];
1191 if (!PathFileExistsW(fileDest
->szDirectory
))
1192 return ERROR_CANCELLED
;
1194 if (fileDest
->bExists
&& IsAttribDir(fileDest
->attributes
))
1195 move_to_dir(lpFileOp
, entryToMove
, fileDest
);
1197 SHNotifyMoveFileW(entryToMove
->szFullPath
, fileDest
->szFullPath
);
1200 return ERROR_SUCCESS
;
1203 /* the FO_RENAME files */
1204 static HRESULT
rename_files(LPSHFILEOPSTRUCTW lpFileOp
, FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
1209 if (flFrom
->dwNumFiles
!= 1)
1210 return ERROR_GEN_FAILURE
;
1212 if (flTo
->dwNumFiles
!= 1)
1213 return ERROR_CANCELLED
;
1215 feFrom
= &flFrom
->feFiles
[0];
1216 feTo
= &flTo
->feFiles
[0];
1218 /* fail if destination doesn't exist */
1219 if (!feFrom
->bExists
)
1220 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1222 /* fail if destination already exists */
1224 return ERROR_ALREADY_EXISTS
;
1226 return SHNotifyMoveFileW(feFrom
->szFullPath
, feTo
->szFullPath
);
1229 /* alert the user if an unsupported flag is used */
1230 static void check_flags(FILEOP_FLAGS fFlags
)
1232 WORD wUnsupportedFlags
= FOF_ALLOWUNDO
| FOF_NO_CONNECTED_ELEMENTS
|
1233 FOF_NOCOPYSECURITYATTRIBS
| FOF_NORECURSEREPARSE
| FOF_WANTNUKEWARNING
|
1234 FOF_RENAMEONCOLLISION
| FOF_WANTMAPPINGHANDLE
;
1236 if (fFlags
& wUnsupportedFlags
)
1237 FIXME("Unsupported flags: %04x\n", fFlags
);
1240 /*************************************************************************
1241 * SHFileOperationW [SHELL32.@]
1243 * See SHFileOperationA
1245 int WINAPI
SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp
)
1247 FILE_LIST flFrom
, flTo
;
1251 return ERROR_INVALID_PARAMETER
;
1253 check_flags(lpFileOp
->fFlags
);
1255 ZeroMemory(&flFrom
, sizeof(FILE_LIST
));
1256 ZeroMemory(&flTo
, sizeof(FILE_LIST
));
1258 if ((ret
= parse_file_list(&flFrom
, lpFileOp
->pFrom
)))
1261 if (lpFileOp
->wFunc
!= FO_DELETE
)
1262 parse_file_list(&flTo
, lpFileOp
->pTo
);
1264 switch (lpFileOp
->wFunc
)
1267 ret
= copy_files(lpFileOp
, &flFrom
, &flTo
);
1270 ret
= delete_files(lpFileOp
, &flFrom
);
1273 ret
= move_files(lpFileOp
, &flFrom
, &flTo
);
1276 ret
= rename_files(lpFileOp
, &flFrom
, &flTo
);
1279 ret
= ERROR_INVALID_PARAMETER
;
1283 destroy_file_list(&flFrom
);
1285 if (lpFileOp
->wFunc
!= FO_DELETE
)
1286 destroy_file_list(&flTo
);
1288 if (ret
== ERROR_CANCELLED
)
1289 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1294 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1296 /*************************************************************************
1297 * SHFreeNameMappings [shell32.246]
1299 * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1303 * hNameMapping [I] handle to the name mappings used during renaming of files
1308 void WINAPI
SHFreeNameMappings(HANDLE hNameMapping
)
1312 int i
= SHDSA_GetItemCount((HDSA
)hNameMapping
) - 1;
1316 LPSHNAMEMAPPINGW lp
= DSA_GetItemPtr((HDSA
)hNameMapping
, i
);
1318 SHFree(lp
->pszOldPath
);
1319 SHFree(lp
->pszNewPath
);
1321 DSA_Destroy((HDSA
)hNameMapping
);
1325 /*************************************************************************
1326 * SheGetDirA [SHELL32.@]
1329 HRESULT WINAPI
SheGetDirA(LPSTR u
, LPSTR v
)
1330 { FIXME("%p %p stub\n",u
,v
);
1334 /*************************************************************************
1335 * SheGetDirW [SHELL32.@]
1338 HRESULT WINAPI
SheGetDirW(LPWSTR u
, LPWSTR v
)
1339 { FIXME("%p %p stub\n",u
,v
);
1343 /*************************************************************************
1344 * SheChangeDirA [SHELL32.@]
1347 HRESULT WINAPI
SheChangeDirA(LPSTR u
)
1348 { FIXME("(%s),stub\n",debugstr_a(u
));
1352 /*************************************************************************
1353 * SheChangeDirW [SHELL32.@]
1356 HRESULT WINAPI
SheChangeDirW(LPWSTR u
)
1357 { FIXME("(%s),stub\n",debugstr_w(u
));
1361 /*************************************************************************
1362 * IsNetDrive [SHELL32.66]
1364 BOOL WINAPI
IsNetDrive(DWORD drive
)
1367 strcpy(root
, "A:\\");
1368 root
[0] += (char)drive
;
1369 return (GetDriveTypeA(root
) == DRIVE_REMOTE
);
1373 /*************************************************************************
1374 * RealDriveType [SHELL32.524]
1376 INT WINAPI
RealDriveType(INT drive
, BOOL bQueryNet
)
1378 char root
[] = "A:\\";
1379 root
[0] += (char)drive
;
1380 return GetDriveTypeA(root
);