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
25 #include "wine/port.h"
39 #define NO_SHLWAPI_STREAM
41 #include "shell32_main.h"
42 #include "undocshell.h"
43 #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 DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec);
59 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec);
60 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path);
61 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path);
62 static DWORD SHNotifyDeleteFileA(LPCSTR path);
63 static DWORD SHNotifyDeleteFileW(LPCWSTR path);
64 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest);
65 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists);
66 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly);
70 HINSTANCE hIconInstance;
71 UINT icon_resource_id;
72 UINT caption_resource_id, text_resource_id;
73 } SHELL_ConfirmIDstruc;
75 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
77 ids->hIconInstance = shell32_hInstance;
78 switch (nKindOfDialog) {
80 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
81 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
82 ids->text_resource_id = IDS_DELETEITEM_TEXT;
84 case ASK_DELETE_FOLDER:
85 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
86 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
87 ids->text_resource_id = IDS_DELETEITEM_TEXT;
89 case ASK_DELETE_MULTIPLE_ITEM:
90 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
91 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
92 ids->text_resource_id = IDS_DELETEMULTIPLE_TEXT;
95 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
96 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
97 ids->text_resource_id = IDS_TRASHITEM_TEXT;
99 case ASK_TRASH_FOLDER:
100 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
101 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
102 ids->text_resource_id = IDS_TRASHFOLDER_TEXT;
104 case ASK_TRASH_MULTIPLE_ITEM:
105 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
106 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
107 ids->text_resource_id = IDS_TRASHMULTIPLE_TEXT;
109 case ASK_CANT_TRASH_ITEM:
110 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
111 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
112 ids->text_resource_id = IDS_CANTTRASH_TEXT;
114 case ASK_DELETE_SELECTED:
115 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
116 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
117 ids->text_resource_id = IDS_DELETESELECTED_TEXT;
119 case ASK_OVERWRITE_FILE:
120 ids->hIconInstance = NULL;
121 ids->icon_resource_id = IDI_WARNING;
122 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
123 ids->text_resource_id = IDS_OVERWRITEFILE_TEXT;
126 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
131 BOOL SHELL_ConfirmDialogW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir)
133 WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
134 SHELL_ConfirmIDstruc ids;
135 MSGBOXPARAMSW params;
137 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
140 LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption)/sizeof(WCHAR));
141 LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText)/sizeof(WCHAR));
143 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
144 szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
146 ZeroMemory(¶ms, sizeof(params));
147 params.cbSize = sizeof(MSGBOXPARAMSW);
148 params.hwndOwner = hWnd;
149 params.hInstance = ids.hIconInstance;
150 params.lpszText = szBuffer;
151 params.lpszCaption = szCaption;
152 params.lpszIcon = (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id);
153 params.dwStyle = MB_YESNO | MB_USERICON;
155 return (IDYES == MessageBoxIndirectW(¶ms));
158 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
160 DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
165 *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
168 MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
171 return E_OUTOFMEMORY;
174 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
176 HeapFree(GetProcessHeap(), 0, wPath);
179 HRESULT WINAPI SHIsFileAvailableOffline(LPCWSTR path, LPDWORD status)
181 FIXME("(%s, %p) stub\n", debugstr_w(path), status);
185 /**************************************************************************
186 * SHELL_DeleteDirectory() [internal]
188 * Asks for confirmation when bShowUI is true and deletes the directory and
189 * all its subdirectories and files if necessary.
191 BOOL SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
195 WIN32_FIND_DATAW wfd;
196 WCHAR szTemp[MAX_PATH];
198 /* Make sure the directory exists before eventually prompting the user */
199 PathCombineW(szTemp, pszDir, wWildcardFile);
200 hFind = FindFirstFileW(szTemp, &wfd);
201 if (hFind == INVALID_HANDLE_VALUE)
204 if (!bShowUI || (ret = SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir)))
208 LPWSTR lp = wfd.cAlternateFileName;
213 PathCombineW(szTemp, pszDir, lp);
214 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
215 ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
217 ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
218 } while (ret && FindNextFileW(hFind, &wfd));
222 ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
226 /**************************************************************************
227 * Win32CreateDirectory [SHELL32.93]
229 * Creates a directory. Also triggers a change notify if one exists.
232 * path [I] path to directory to create
235 * TRUE if successful, FALSE otherwise
238 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
239 * This is Unicode on NT/2000
241 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
246 TRACE("(%s, %p)\n", debugstr_a(path), sec);
248 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
251 retCode = SHNotifyCreateDirectoryW(wPath, sec);
252 SHELL32_FreeUnicodeBuf(wPath);
257 /**********************************************************************/
259 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
261 TRACE("(%s, %p)\n", debugstr_w(path), sec);
263 if (CreateDirectoryW(path, sec))
265 SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
266 return ERROR_SUCCESS;
268 return GetLastError();
271 /**********************************************************************/
273 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
275 if (SHELL_OsIsUnicode())
276 return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
277 return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
280 /************************************************************************
281 * Win32RemoveDirectory [SHELL32.94]
283 * Deletes a directory. Also triggers a change notify if one exists.
286 * path [I] path to directory to delete
289 * TRUE if successful, FALSE otherwise
292 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
293 * This is Unicode on NT/2000
295 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
300 TRACE("(%s)\n", debugstr_a(path));
302 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
305 retCode = SHNotifyRemoveDirectoryW(wPath);
306 SHELL32_FreeUnicodeBuf(wPath);
311 /***********************************************************************/
313 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
316 TRACE("(%s)\n", debugstr_w(path));
318 ret = RemoveDirectoryW(path);
321 /* Directory may be write protected */
322 DWORD dwAttr = GetFileAttributesW(path);
323 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
324 if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
325 ret = RemoveDirectoryW(path);
329 SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
330 return ERROR_SUCCESS;
332 return GetLastError();
335 /***********************************************************************/
337 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
339 if (SHELL_OsIsUnicode())
340 return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
341 return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
344 /************************************************************************
345 * Win32DeleteFile [SHELL32.164]
347 * Deletes a file. Also triggers a change notify if one exists.
350 * path [I] path to file to delete
353 * TRUE if successful, FALSE otherwise
356 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
357 * This is Unicode on NT/2000
359 static DWORD SHNotifyDeleteFileA(LPCSTR path)
364 TRACE("(%s)\n", debugstr_a(path));
366 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
369 retCode = SHNotifyDeleteFileW(wPath);
370 SHELL32_FreeUnicodeBuf(wPath);
375 /***********************************************************************/
377 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
381 TRACE("(%s)\n", debugstr_w(path));
383 ret = DeleteFileW(path);
386 /* File may be write protected or a system file */
387 DWORD dwAttr = GetFileAttributesW(path);
388 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
389 if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
390 ret = DeleteFileW(path);
394 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
395 return ERROR_SUCCESS;
397 return GetLastError();
400 /***********************************************************************/
402 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
404 if (SHELL_OsIsUnicode())
405 return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
406 return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
409 /************************************************************************
410 * SHNotifyMoveFile [internal]
412 * Moves a file. Also triggers a change notify if one exists.
415 * src [I] path to source file to move
416 * dest [I] path to target file to move to
419 * ERORR_SUCCESS if successful
421 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
425 TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
427 ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
429 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
431 ret = MoveFileW(src, dest);
437 dwAttr = SHFindAttrW(dest, FALSE);
438 if (INVALID_FILE_ATTRIBUTES == dwAttr)
440 /* Source file may be write protected or a system file */
441 dwAttr = GetFileAttributesW(src);
442 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
443 if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
444 ret = MoveFileW(src, dest);
449 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
450 return ERROR_SUCCESS;
452 return GetLastError();
455 /************************************************************************
456 * SHNotifyCopyFile [internal]
458 * Copies a file. Also triggers a change notify if one exists.
461 * src [I] path to source file to move
462 * dest [I] path to target file to move to
463 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
464 * a file with this name already exists
467 * ERROR_SUCCESS if successful
469 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
473 TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
475 ret = CopyFileW(src, dest, bFailIfExists);
478 SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
479 return ERROR_SUCCESS;
482 return GetLastError();
485 /*************************************************************************
486 * SHCreateDirectory [SHELL32.165]
488 * This function creates a file system folder whose fully qualified path is
489 * given by path. If one or more of the intermediate folders do not exist,
490 * they will be created as well.
494 * path [I] path of directory to create
497 * ERRROR_SUCCESS or one of the following values:
498 * ERROR_BAD_PATHNAME if the path is relative
499 * ERROR_FILE_EXISTS when a file with that name exists
500 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
501 * ERROR_INVLID_NAME if the path contains invalid chars
502 * ERROR_ALREADY_EXISTS when the directory already exists
503 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
506 * exported by ordinal
508 * WinNT/2000 exports Unicode
510 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
512 if (SHELL_OsIsUnicode())
513 return SHCreateDirectoryExW(hWnd, path, NULL);
514 return SHCreateDirectoryExA(hWnd, path, NULL);
517 /*************************************************************************
518 * SHCreateDirectoryExA [SHELL32.@]
520 * This function creates a file system folder whose fully qualified path is
521 * given by path. If one or more of the intermediate folders do not exist,
522 * they will be created as well.
526 * path [I] path of directory to create
527 * sec [I] security attributes to use or NULL
530 * ERRROR_SUCCESS or one of the following values:
531 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
532 * ERROR_INVLID_NAME if the path contains invalid chars
533 * ERROR_FILE_EXISTS when a file with that name exists
534 * ERROR_ALREADY_EXISTS when the directory already exists
535 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
537 * FIXME: Not implemented yet;
538 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
539 * if the path is a network path to deal with network drivers which might have a limited
540 * but unknown maximum path length. If not:
542 * If hWnd is set to a valid window handle, a message box is displayed warning
543 * the user that the files may not be accessible. If the user chooses not to
544 * proceed, the function returns ERROR_CANCELLED.
546 * If hWnd is set to NULL, no user interface is displayed and the function
547 * returns ERROR_CANCELLED.
549 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
554 TRACE("(%s, %p)\n", debugstr_a(path), sec);
556 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
559 retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
560 SHELL32_FreeUnicodeBuf(wPath);
565 /*************************************************************************
566 * SHCreateDirectoryExW [SHELL32.@]
568 * See SHCreateDirectoryExA.
570 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
572 int ret = ERROR_BAD_PATHNAME;
573 TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
575 if (PathIsRelativeW(path))
581 ret = SHNotifyCreateDirectoryW(path, sec);
582 /* Refuse to work on certain error codes before trying to create directories recursively */
583 if (ret != ERROR_SUCCESS &&
584 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 = lstrlenW(szFile) + 1;
786 feFile->szFullPath = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
787 lstrcpyW(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 = lstrlenW(feFile->szFullPath) - dwLen + 1;
797 feFile->szFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
798 lstrcpyW(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 + lstrlenW(szFileName) + 1;
812 szFullPath = HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
814 lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
815 lstrcatW(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 lstrcpyW(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 += lstrlenW(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 lstrcpyW(szTo, szDestPath);
943 szTo[lstrlenW(szTo) + 1] = '\0';
944 SHNotifyCreateDirectoryW(szTo, NULL);
946 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
947 szFrom[lstrlenW(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 if (!PathFileExistsW(feTo->szFullPath))
961 SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
963 if (IsAttribFile(feFrom->attributes))
965 WCHAR szDestPath[MAX_PATH];
967 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
968 SHNotifyCopyFileW(feFrom->szFullPath, szDestPath, FALSE);
970 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
971 copy_dir_to_dir(lpFileOp, feFrom, feTo->szFullPath);
974 static void create_dest_dirs(LPWSTR szDestDir)
977 LPWSTR ptr = StrChrW(szDestDir, '\\');
979 /* make sure all directories up to last one are created */
980 while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
982 lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
984 if (!PathFileExistsW(dir))
985 SHNotifyCreateDirectoryW(dir, NULL);
988 /* create last directory */
989 if (!PathFileExistsW(szDestDir))
990 SHNotifyCreateDirectoryW(szDestDir, NULL);
993 /* the FO_COPY operation */
994 static HRESULT copy_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
997 FILE_ENTRY *entryToCopy;
998 FILE_ENTRY *fileDest = &flTo->feFiles[0];
999 BOOL bCancelIfAnyDirectories = FALSE;
1001 if (flFrom->bAnyDontExist)
1002 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1004 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->bAnyFromWildcard)
1005 return ERROR_CANCELLED;
1007 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flTo->dwNumFiles != 1)
1008 return ERROR_CANCELLED;
1010 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->dwNumFiles != 1 &&
1011 flFrom->dwNumFiles != flTo->dwNumFiles)
1013 return ERROR_CANCELLED;
1016 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 &&
1017 !PathFileExistsW(flTo->feFiles[0].szFullPath) &&
1018 IsAttribFile(fileDest->attributes))
1020 bCancelIfAnyDirectories = TRUE;
1023 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
1024 !PathFileExistsW(fileDest->szFullPath))
1026 lpFileOp->fAnyOperationsAborted = TRUE;
1027 return ERROR_CANCELLED;
1030 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flFrom->dwNumFiles != 1 &&
1031 PathFileExistsW(fileDest->szFullPath) &&
1032 IsAttribFile(fileDest->attributes))
1034 return ERROR_CANCELLED;
1037 for (i = 0; i < flFrom->dwNumFiles; i++)
1039 entryToCopy = &flFrom->feFiles[i];
1041 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1042 fileDest = &flTo->feFiles[i];
1044 if (IsAttribDir(entryToCopy->attributes) &&
1045 !lstrcmpiW(entryToCopy->szFullPath, fileDest->szDirectory))
1047 return ERROR_SUCCESS;
1050 if (IsAttribDir(entryToCopy->attributes) && bCancelIfAnyDirectories)
1051 return ERROR_CANCELLED;
1053 create_dest_dirs(fileDest->szDirectory);
1055 if (!lstrcmpiW(entryToCopy->szFullPath, fileDest->szFullPath))
1057 if (IsAttribFile(entryToCopy->attributes))
1058 return ERROR_NO_MORE_SEARCH_HANDLES;
1060 return ERROR_SUCCESS;
1063 if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1064 (flFrom->dwNumFiles == 1 && IsAttribDir(fileDest->attributes)))
1066 copy_to_dir(lpFileOp, entryToCopy, fileDest);
1068 else if (IsAttribDir(entryToCopy->attributes))
1070 copy_dir_to_dir(lpFileOp, entryToCopy, fileDest->szFullPath);
1074 if (SHNotifyCopyFileW(entryToCopy->szFullPath, fileDest->szFullPath, TRUE))
1076 lpFileOp->fAnyOperationsAborted = TRUE;
1077 return ERROR_CANCELLED;
1082 return ERROR_SUCCESS;
1085 static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, BOOL fTrash, FILE_LIST *flFrom)
1087 if (flFrom->dwNumFiles > 1)
1090 const WCHAR format[] = {'%','d',0};
1092 wnsprintfW(tmp, sizeof(tmp)/sizeof(tmp[0]), format, flFrom->dwNumFiles);
1093 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_MULTIPLE_ITEM:ASK_DELETE_MULTIPLE_ITEM), tmp);
1097 FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
1099 if (IsAttribFile(fileEntry->attributes))
1100 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FILE:ASK_DELETE_FILE), fileEntry->szFullPath);
1101 else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1102 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FOLDER:ASK_DELETE_FOLDER), fileEntry->szFullPath);
1107 /* the FO_DELETE operation */
1108 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom)
1110 FILE_ENTRY *fileEntry;
1115 if (!flFrom->dwNumFiles)
1116 return ERROR_SUCCESS;
1118 /* Windows also checks only the first item */
1119 bTrash = (lpFileOp->fFlags & FOF_ALLOWUNDO)
1120 && TRASH_CanTrashFile(flFrom->feFiles[0].szFullPath);
1122 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (!bTrash && lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1123 if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, bTrash, flFrom))
1125 lpFileOp->fAnyOperationsAborted = TRUE;
1129 for (i = 0; i < flFrom->dwNumFiles; i++)
1132 fileEntry = &flFrom->feFiles[i];
1134 if (!IsAttribFile(fileEntry->attributes) &&
1135 (lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1141 if (TRASH_TrashFile(fileEntry->szFullPath))
1144 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1145 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1146 bDelete = SHELL_ConfirmDialogW(lpFileOp->hwnd, ASK_CANT_TRASH_ITEM, fileEntry->szFullPath);
1152 lpFileOp->fAnyOperationsAborted = TRUE;
1157 /* delete the file or directory */
1158 if (IsAttribFile(fileEntry->attributes))
1159 bPathExists = DeleteFileW(fileEntry->szFullPath);
1161 bPathExists = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1164 return ERROR_PATH_NOT_FOUND;
1167 return ERROR_SUCCESS;
1170 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
1172 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1173 SHFILEOPSTRUCTW fileOp;
1175 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1177 if (IsDotDir(feFrom->szFilename))
1180 SHNotifyCreateDirectoryW(szDestPath, NULL);
1182 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1183 szFrom[lstrlenW(szFrom) + 1] = '\0';
1185 lstrcpyW(szTo, szDestPath);
1186 szTo[lstrlenW(szDestPath) + 1] = '\0';
1188 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
1189 fileOp.pFrom = szFrom;
1192 SHFileOperationW(&fileOp);
1195 /* moves a file or directory to another directory */
1196 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
1198 WCHAR szDestPath[MAX_PATH];
1200 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1202 if (IsAttribFile(feFrom->attributes))
1203 SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
1204 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1205 move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1208 /* the FO_MOVE operation */
1209 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1212 FILE_ENTRY *entryToMove;
1213 FILE_ENTRY *fileDest;
1215 if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1216 return ERROR_CANCELLED;
1218 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1219 flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1221 return ERROR_CANCELLED;
1224 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1225 !flFrom->bAnyDirectories &&
1226 flFrom->dwNumFiles > flTo->dwNumFiles)
1228 return ERROR_CANCELLED;
1231 if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1232 return ERROR_CANCELLED;
1234 if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1235 flFrom->dwNumFiles != flTo->dwNumFiles)
1237 return ERROR_CANCELLED;
1240 fileDest = &flTo->feFiles[0];
1241 for (i = 0; i < flFrom->dwNumFiles; i++)
1243 entryToMove = &flFrom->feFiles[i];
1245 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1246 fileDest = &flTo->feFiles[i];
1248 if (!PathFileExistsW(fileDest->szDirectory))
1249 return ERROR_CANCELLED;
1251 if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1252 move_to_dir(lpFileOp, entryToMove, fileDest);
1254 SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
1257 return ERROR_SUCCESS;
1260 /* the FO_RENAME files */
1261 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1266 if (flFrom->dwNumFiles != 1)
1267 return ERROR_GEN_FAILURE;
1269 if (flTo->dwNumFiles != 1)
1270 return ERROR_CANCELLED;
1272 feFrom = &flFrom->feFiles[0];
1273 feTo= &flTo->feFiles[0];
1275 /* fail if destination doesn't exist */
1276 if (!feFrom->bExists)
1277 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1279 /* fail if destination already exists */
1281 return ERROR_ALREADY_EXISTS;
1283 return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
1286 /* alert the user if an unsupported flag is used */
1287 static void check_flags(FILEOP_FLAGS fFlags)
1289 WORD wUnsupportedFlags = FOF_NO_CONNECTED_ELEMENTS |
1290 FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1291 FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1293 if (fFlags & wUnsupportedFlags)
1294 FIXME("Unsupported flags: %04x\n", fFlags);
1297 /*************************************************************************
1298 * SHFileOperationW [SHELL32.@]
1300 * See SHFileOperationA
1302 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1304 FILE_LIST flFrom, flTo;
1308 return ERROR_INVALID_PARAMETER;
1310 check_flags(lpFileOp->fFlags);
1312 ZeroMemory(&flFrom, sizeof(FILE_LIST));
1313 ZeroMemory(&flTo, sizeof(FILE_LIST));
1315 if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1318 if (lpFileOp->wFunc != FO_DELETE)
1319 parse_file_list(&flTo, lpFileOp->pTo);
1321 switch (lpFileOp->wFunc)
1324 ret = copy_files(lpFileOp, &flFrom, &flTo);
1327 ret = delete_files(lpFileOp, &flFrom);
1330 ret = move_files(lpFileOp, &flFrom, &flTo);
1333 ret = rename_files(lpFileOp, &flFrom, &flTo);
1336 ret = ERROR_INVALID_PARAMETER;
1340 destroy_file_list(&flFrom);
1342 if (lpFileOp->wFunc != FO_DELETE)
1343 destroy_file_list(&flTo);
1345 if (ret == ERROR_CANCELLED)
1346 lpFileOp->fAnyOperationsAborted = TRUE;
1351 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1353 /*************************************************************************
1354 * SHFreeNameMappings [shell32.246]
1356 * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1360 * hNameMapping [I] handle to the name mappings used during renaming of files
1365 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1369 int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1373 LPSHNAMEMAPPINGW lp = DSA_GetItemPtr((HDSA)hNameMapping, i);
1375 SHFree(lp->pszOldPath);
1376 SHFree(lp->pszNewPath);
1378 DSA_Destroy((HDSA)hNameMapping);
1382 /*************************************************************************
1383 * SheGetDirA [SHELL32.@]
1386 HRESULT WINAPI SheGetDirA(LPSTR u, LPSTR v)
1387 { FIXME("%p %p stub\n",u,v);
1391 /*************************************************************************
1392 * SheGetDirW [SHELL32.@]
1395 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1396 { FIXME("%p %p stub\n",u,v);
1400 /*************************************************************************
1401 * SheChangeDirA [SHELL32.@]
1404 HRESULT WINAPI SheChangeDirA(LPSTR u)
1405 { FIXME("(%s),stub\n",debugstr_a(u));
1409 /*************************************************************************
1410 * SheChangeDirW [SHELL32.@]
1413 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1414 { FIXME("(%s),stub\n",debugstr_w(u));
1418 /*************************************************************************
1419 * IsNetDrive [SHELL32.66]
1421 BOOL WINAPI IsNetDrive(DWORD drive)
1424 strcpy(root, "A:\\");
1425 root[0] += (char)drive;
1426 return (GetDriveTypeA(root) == DRIVE_REMOTE);
1430 /*************************************************************************
1431 * RealDriveType [SHELL32.524]
1433 INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1435 char root[] = "A:\\";
1436 root[0] += (char)drive;
1437 return GetDriveTypeA(root);