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/unicode.h"
44 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(shell);
49 #define IsAttrib(x, y) ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
50 #define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
51 #define IsAttribDir(x) IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
52 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
56 static const WCHAR wWildcardFile[] = {'*',0};
57 static const WCHAR wWildcardChars[] = {'*','?',0};
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 HINSTANCE hIconInstance;
72 UINT icon_resource_id;
73 UINT caption_resource_id, text_resource_id;
74 } SHELL_ConfirmIDstruc;
76 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
78 ids->hIconInstance = shell32_hInstance;
79 switch (nKindOfDialog) {
81 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
82 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
83 ids->text_resource_id = IDS_DELETEITEM_TEXT;
85 case ASK_DELETE_FOLDER:
86 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
87 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
88 ids->text_resource_id = IDS_DELETEITEM_TEXT;
90 case ASK_DELETE_MULTIPLE_ITEM:
91 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
92 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
93 ids->text_resource_id = IDS_DELETEMULTIPLE_TEXT;
96 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
97 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
98 ids->text_resource_id = IDS_TRASHITEM_TEXT;
100 case ASK_TRASH_FOLDER:
101 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
102 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
103 ids->text_resource_id = IDS_TRASHFOLDER_TEXT;
105 case ASK_TRASH_MULTIPLE_ITEM:
106 ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
107 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
108 ids->text_resource_id = IDS_TRASHMULTIPLE_TEXT;
110 case ASK_CANT_TRASH_ITEM:
111 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
112 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
113 ids->text_resource_id = IDS_CANTTRASH_TEXT;
115 case ASK_DELETE_SELECTED:
116 ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
117 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
118 ids->text_resource_id = IDS_DELETESELECTED_TEXT;
120 case ASK_OVERWRITE_FILE:
121 ids->hIconInstance = NULL;
122 ids->icon_resource_id = IDI_WARNING;
123 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
124 ids->text_resource_id = IDS_OVERWRITEFILE_TEXT;
127 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
132 BOOL SHELL_ConfirmDialogW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir)
134 WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
135 SHELL_ConfirmIDstruc ids;
136 MSGBOXPARAMSW params;
138 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
141 LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption)/sizeof(WCHAR));
142 LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText)/sizeof(WCHAR));
144 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
145 szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
147 ZeroMemory(¶ms, sizeof(params));
148 params.cbSize = sizeof(MSGBOXPARAMSW);
149 params.hwndOwner = hWnd;
150 params.hInstance = ids.hIconInstance;
151 params.lpszText = szBuffer;
152 params.lpszCaption = szCaption;
153 params.lpszIcon = (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id);
154 params.dwStyle = MB_YESNO | MB_USERICON;
156 return (IDOK == MessageBoxIndirectW(¶ms));
159 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
161 DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
166 *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
169 MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
172 return E_OUTOFMEMORY;
175 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
177 HeapFree(GetProcessHeap(), 0, wPath);
180 HRESULT WINAPI SHIsFileAvailableOffline(LPCWSTR path, LPDWORD status)
182 FIXME("(%s, %p) stub\n", debugstr_w(path), status);
186 /**************************************************************************
187 * SHELL_DeleteDirectory() [internal]
189 * Asks for confirmation when bShowUI is true and deletes the directory and
190 * all its subdirectories and files if necessary.
192 BOOL SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
196 WIN32_FIND_DATAW wfd;
197 WCHAR szTemp[MAX_PATH];
199 /* Make sure the directory exists before eventually prompting the user */
200 PathCombineW(szTemp, pszDir, wWildcardFile);
201 hFind = FindFirstFileW(szTemp, &wfd);
202 if (hFind == INVALID_HANDLE_VALUE)
205 if (!bShowUI || (ret = SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir)))
209 LPWSTR lp = wfd.cAlternateFileName;
214 PathCombineW(szTemp, pszDir, lp);
215 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
216 ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
218 ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
219 } while (ret && FindNextFileW(hFind, &wfd));
223 ret = (SHNotifyRemoveDirectoryW(pszDir) == 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_SUCCESS &&
585 ret != ERROR_FILE_EXISTS &&
586 ret != ERROR_ALREADY_EXISTS &&
587 ret != ERROR_FILENAME_EXCED_RANGE)
589 WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1]; /* extra for PathAddBackslash() */
591 lstrcpynW(szTemp, path, MAX_PATH);
592 pEnd = PathAddBackslashW(szTemp);
597 while (*pSlash && *pSlash != '\\')
598 pSlash = CharNextW(pSlash);
602 *pSlash = 0; /* terminate path at separator */
604 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
606 *pSlash++ = '\\'; /* put the separator back */
610 if (ret && hWnd && (ERROR_CANCELLED != ret))
612 /* We failed and should show a dialog box */
613 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
614 ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
620 /*************************************************************************
621 * SHFindAttrW [internal]
623 * Get the Attributes for a file or directory. The difference to GetAttributes()
624 * is that this function will also work for paths containing wildcard characters
628 * path [I] path of directory or file to check
629 * fileOnly [I] TRUE if only files should be found
632 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
633 * the first file or directory found otherwise
635 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
637 WIN32_FIND_DATAW wfd;
638 BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
639 DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
640 HANDLE hFind = FindFirstFileW(pName, &wfd);
642 TRACE("%s %d\n", debugstr_w(pName), fileOnly);
643 if (INVALID_HANDLE_VALUE != hFind)
647 if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
649 dwAttr = wfd.dwFileAttributes;
652 while (FindNextFileW(hFind, &wfd));
658 /*************************************************************************
660 * SHNameTranslate HelperFunction for SHFileOperationA
662 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
663 * is NULL, only the necessary size of the string is determined and returned,
664 * otherwise the ASCII strings are copied into it and the buffer is increased
665 * to point to the location after the final 0 termination char.
667 DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
669 DWORD size = 0, aSize = 0;
670 LPCSTR aString = (LPCSTR)*pWToFrom;
676 size = lstrlenA(aString) + 1;
679 } while ((size != 1) && more);
680 /* The two sizes might be different in the case of multibyte chars */
681 size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
682 if (*wString) /* only in the second loop */
684 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
685 *pWToFrom = *wString;
691 /*************************************************************************
692 * SHFileOperationA [SHELL32.@]
694 * Function to copy, move, delete and create one or more files with optional
698 * lpFileOp [I/O] pointer to a structure containing all the necessary information
701 * Success: ERROR_SUCCESS.
702 * Failure: ERROR_CANCELLED.
707 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
709 SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
712 LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
713 wString = NULL; /* we change this in SHNameTranslate */
716 if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
717 nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
718 if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
719 nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
720 while (1) /* every loop calculate size, second translate also, if we have storage for this */
722 size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
723 size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
724 size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
728 retCode = SHFileOperationW(&nFileOp);
729 HeapFree(GetProcessHeap(), 0, ForFree); /* we cannot use wString, it was changed */
734 wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
735 if (ForFree) continue;
736 retCode = ERROR_OUTOFMEMORY;
737 nFileOp.fAnyOperationsAborted = TRUE;
738 SetLastError(retCode);
743 lpFileOp->hNameMappings = nFileOp.hNameMappings;
744 lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
748 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
766 BOOL bAnyFromWildcard;
767 BOOL bAnyDirectories;
772 static inline void grow_list(FILE_LIST *list)
774 FILE_ENTRY *new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
775 list->num_alloc * 2 * sizeof(*new) );
777 list->num_alloc *= 2;
780 /* adds a file to the FILE_ENTRY struct
782 static void add_file_to_entry(FILE_ENTRY *feFile, LPWSTR szFile)
784 DWORD dwLen = strlenW(szFile) + 1;
787 feFile->szFullPath = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
788 strcpyW(feFile->szFullPath, szFile);
790 ptr = StrRChrW(szFile, NULL, '\\');
793 dwLen = ptr - szFile + 1;
794 feFile->szDirectory = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
795 lstrcpynW(feFile->szDirectory, szFile, dwLen);
797 dwLen = strlenW(feFile->szFullPath) - dwLen + 1;
798 feFile->szFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
799 strcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
801 feFile->bFromWildcard = FALSE;
804 static LPWSTR wildcard_to_file(LPWSTR szWildCard, LPWSTR szFileName)
806 LPWSTR szFullPath, ptr;
807 DWORD dwDirLen, dwFullLen;
809 ptr = StrRChrW(szWildCard, NULL, '\\');
810 dwDirLen = ptr - szWildCard + 1;
812 dwFullLen = dwDirLen + strlenW(szFileName) + 1;
813 szFullPath = HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
815 lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
816 strcatW(szFullPath, szFileName);
821 static void parse_wildcard_files(FILE_LIST *flList, LPWSTR szFile, LPDWORD pdwListIndex)
823 WIN32_FIND_DATAW wfd;
824 HANDLE hFile = FindFirstFileW(szFile, &wfd);
829 for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
831 if (IsDotDir(wfd.cFileName)) continue;
832 if (*pdwListIndex >= flList->num_alloc) grow_list( flList );
833 szFullPath = wildcard_to_file(szFile, wfd.cFileName);
834 file = &flList->feFiles[(*pdwListIndex)++];
835 add_file_to_entry(file, szFullPath);
836 file->bFromWildcard = TRUE;
837 file->attributes = wfd.dwFileAttributes;
838 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
839 HeapFree(GetProcessHeap(), 0, szFullPath);
845 /* takes the null-separated file list and fills out the FILE_LIST */
846 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
848 LPCWSTR ptr = szFiles;
849 WCHAR szCurFile[MAX_PATH];
850 DWORD i = 0, dwDirLen;
853 return ERROR_INVALID_PARAMETER;
855 flList->bAnyFromWildcard = FALSE;
856 flList->bAnyDirectories = FALSE;
857 flList->bAnyDontExist = FALSE;
858 flList->num_alloc = 32;
859 flList->dwNumFiles = 0;
863 return ERROR_ACCESS_DENIED;
865 flList->feFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
866 flList->num_alloc * sizeof(FILE_ENTRY));
870 if (i >= flList->num_alloc) grow_list( flList );
872 /* change relative to absolute path */
873 if (PathIsRelativeW(ptr))
875 dwDirLen = GetCurrentDirectoryW(MAX_PATH, szCurFile) + 1;
876 PathCombineW(szCurFile, szCurFile, ptr);
877 flList->feFiles[i].bFromRelative = TRUE;
881 strcpyW(szCurFile, ptr);
882 flList->feFiles[i].bFromRelative = FALSE;
885 /* parse wildcard files if they are in the filename */
886 if (StrPBrkW(szCurFile, wWildcardChars))
888 parse_wildcard_files(flList, szCurFile, &i);
889 flList->bAnyFromWildcard = TRUE;
894 FILE_ENTRY *file = &flList->feFiles[i];
895 add_file_to_entry(file, szCurFile);
896 file->attributes = GetFileAttributesW( file->szFullPath );
897 file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
898 if (!file->bExists) flList->bAnyDontExist = TRUE;
899 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
902 /* advance to the next string */
903 ptr += strlenW(ptr) + 1;
906 flList->dwNumFiles = i;
911 /* free the FILE_LIST */
912 static void destroy_file_list(FILE_LIST *flList)
916 if (!flList || !flList->feFiles)
919 for (i = 0; i < flList->dwNumFiles; i++)
921 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
922 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
923 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
926 HeapFree(GetProcessHeap(), 0, flList->feFiles);
929 static void copy_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
931 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
932 SHFILEOPSTRUCTW fileOp;
934 static const WCHAR wildCardFiles[] = {'*','.','*',0};
936 if (IsDotDir(feFrom->szFilename))
939 if (PathFileExistsW(szDestPath))
940 PathCombineW(szTo, szDestPath, feFrom->szFilename);
942 strcpyW(szTo, szDestPath);
944 szTo[strlenW(szTo) + 1] = '\0';
945 SHNotifyCreateDirectoryW(szTo, NULL);
947 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
948 szFrom[strlenW(szFrom) + 1] = '\0';
950 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
951 fileOp.pFrom = szFrom;
953 fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
955 SHFileOperationW(&fileOp);
958 /* copy a file or directory to another directory */
959 static void copy_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
961 if (!PathFileExistsW(feTo->szFullPath))
962 SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
964 if (IsAttribFile(feFrom->attributes))
966 WCHAR szDestPath[MAX_PATH];
968 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
969 SHNotifyCopyFileW(feFrom->szFullPath, szDestPath, FALSE);
971 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
972 copy_dir_to_dir(lpFileOp, feFrom, feTo->szFullPath);
975 static void create_dest_dirs(LPWSTR szDestDir)
978 LPWSTR ptr = StrChrW(szDestDir, '\\');
980 /* make sure all directories up to last one are created */
981 while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
983 lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
985 if (!PathFileExistsW(dir))
986 SHNotifyCreateDirectoryW(dir, NULL);
989 /* create last directory */
990 if (!PathFileExistsW(szDestDir))
991 SHNotifyCreateDirectoryW(szDestDir, NULL);
994 /* the FO_COPY operation */
995 static HRESULT copy_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
998 FILE_ENTRY *entryToCopy;
999 FILE_ENTRY *fileDest = &flTo->feFiles[0];
1000 BOOL bCancelIfAnyDirectories = FALSE;
1002 if (flFrom->bAnyDontExist)
1003 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1005 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->bAnyFromWildcard)
1006 return ERROR_CANCELLED;
1008 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flTo->dwNumFiles != 1)
1009 return ERROR_CANCELLED;
1011 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->dwNumFiles != 1 &&
1012 flFrom->dwNumFiles != flTo->dwNumFiles)
1014 return ERROR_CANCELLED;
1017 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 &&
1018 !PathFileExistsW(flTo->feFiles[0].szFullPath) &&
1019 IsAttribFile(fileDest->attributes))
1021 bCancelIfAnyDirectories = TRUE;
1024 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
1025 !PathFileExistsW(fileDest->szFullPath))
1027 lpFileOp->fAnyOperationsAborted = TRUE;
1028 return ERROR_CANCELLED;
1031 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flFrom->dwNumFiles != 1 &&
1032 PathFileExistsW(fileDest->szFullPath) &&
1033 IsAttribFile(fileDest->attributes))
1035 return ERROR_CANCELLED;
1038 for (i = 0; i < flFrom->dwNumFiles; i++)
1040 entryToCopy = &flFrom->feFiles[i];
1042 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1043 fileDest = &flTo->feFiles[i];
1045 if (IsAttribDir(entryToCopy->attributes) &&
1046 !lstrcmpiW(entryToCopy->szFullPath, fileDest->szDirectory))
1048 return ERROR_SUCCESS;
1051 if (IsAttribDir(entryToCopy->attributes) && bCancelIfAnyDirectories)
1052 return ERROR_CANCELLED;
1054 create_dest_dirs(fileDest->szDirectory);
1056 if (!lstrcmpiW(entryToCopy->szFullPath, fileDest->szFullPath))
1058 if (IsAttribFile(entryToCopy->attributes))
1059 return ERROR_NO_MORE_SEARCH_HANDLES;
1061 return ERROR_SUCCESS;
1064 if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1065 (flFrom->dwNumFiles == 1 && IsAttribDir(fileDest->attributes)))
1067 copy_to_dir(lpFileOp, entryToCopy, fileDest);
1069 else if (IsAttribDir(entryToCopy->attributes))
1071 copy_dir_to_dir(lpFileOp, entryToCopy, fileDest->szFullPath);
1075 if (SHNotifyCopyFileW(entryToCopy->szFullPath, fileDest->szFullPath, TRUE))
1077 lpFileOp->fAnyOperationsAborted = TRUE;
1078 return ERROR_CANCELLED;
1083 return ERROR_SUCCESS;
1086 static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, BOOL fTrash, FILE_LIST *flFrom)
1088 if (flFrom->dwNumFiles > 1)
1091 const WCHAR format[] = {'%','d',0};
1093 wnsprintfW(tmp, sizeof(tmp)/sizeof(tmp[0]), format, flFrom->dwNumFiles);
1094 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_MULTIPLE_ITEM:ASK_DELETE_MULTIPLE_ITEM), tmp);
1098 FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
1100 if (IsAttribFile(fileEntry->attributes))
1101 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FILE:ASK_DELETE_FILE), fileEntry->szFullPath);
1102 else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1103 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FOLDER:ASK_DELETE_FOLDER), fileEntry->szFullPath);
1108 /* the FO_DELETE operation */
1109 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom)
1111 FILE_ENTRY *fileEntry;
1116 if (!flFrom->dwNumFiles)
1117 return ERROR_SUCCESS;
1119 /* Windows also checks only the first item */
1120 bTrash = (lpFileOp->fFlags & FOF_ALLOWUNDO)
1121 && TRASH_CanTrashFile(flFrom->feFiles[0].szFullPath);
1123 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (!bTrash && lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1124 if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, bTrash, flFrom))
1126 lpFileOp->fAnyOperationsAborted = TRUE;
1130 for (i = 0; i < flFrom->dwNumFiles; i++)
1133 fileEntry = &flFrom->feFiles[i];
1135 if (!IsAttribFile(fileEntry->attributes) &&
1136 (lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1142 if (TRASH_TrashFile(fileEntry->szFullPath))
1145 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1146 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1147 bDelete = SHELL_ConfirmDialogW(lpFileOp->hwnd, ASK_CANT_TRASH_ITEM, fileEntry->szFullPath);
1153 lpFileOp->fAnyOperationsAborted = TRUE;
1158 /* delete the file or directory */
1159 if (IsAttribFile(fileEntry->attributes))
1160 bPathExists = DeleteFileW(fileEntry->szFullPath);
1162 bPathExists = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1165 return ERROR_PATH_NOT_FOUND;
1168 return ERROR_SUCCESS;
1171 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
1173 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1174 SHFILEOPSTRUCTW fileOp;
1176 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1178 if (IsDotDir(feFrom->szFilename))
1181 SHNotifyCreateDirectoryW(szDestPath, NULL);
1183 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1184 szFrom[strlenW(szFrom) + 1] = '\0';
1186 strcpyW(szTo, szDestPath);
1187 szTo[strlenW(szDestPath) + 1] = '\0';
1189 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
1190 fileOp.pFrom = szFrom;
1193 SHFileOperationW(&fileOp);
1196 /* moves a file or directory to another directory */
1197 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
1199 WCHAR szDestPath[MAX_PATH];
1201 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1203 if (IsAttribFile(feFrom->attributes))
1204 SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
1205 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1206 move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1209 /* the FO_MOVE operation */
1210 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1213 FILE_ENTRY *entryToMove;
1214 FILE_ENTRY *fileDest;
1216 if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1217 return ERROR_CANCELLED;
1219 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1220 flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1222 return ERROR_CANCELLED;
1225 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1226 !flFrom->bAnyDirectories &&
1227 flFrom->dwNumFiles > flTo->dwNumFiles)
1229 return ERROR_CANCELLED;
1232 if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1233 return ERROR_CANCELLED;
1235 if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1236 flFrom->dwNumFiles != flTo->dwNumFiles)
1238 return ERROR_CANCELLED;
1241 fileDest = &flTo->feFiles[0];
1242 for (i = 0; i < flFrom->dwNumFiles; i++)
1244 entryToMove = &flFrom->feFiles[i];
1246 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1247 fileDest = &flTo->feFiles[i];
1249 if (!PathFileExistsW(fileDest->szDirectory))
1250 return ERROR_CANCELLED;
1252 if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1253 move_to_dir(lpFileOp, entryToMove, fileDest);
1255 SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
1258 return ERROR_SUCCESS;
1261 /* the FO_RENAME files */
1262 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1267 if (flFrom->dwNumFiles != 1)
1268 return ERROR_GEN_FAILURE;
1270 if (flTo->dwNumFiles != 1)
1271 return ERROR_CANCELLED;
1273 feFrom = &flFrom->feFiles[0];
1274 feTo= &flTo->feFiles[0];
1276 /* fail if destination doesn't exist */
1277 if (!feFrom->bExists)
1278 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1280 /* fail if destination already exists */
1282 return ERROR_ALREADY_EXISTS;
1284 return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
1287 /* alert the user if an unsupported flag is used */
1288 static void check_flags(FILEOP_FLAGS fFlags)
1290 WORD wUnsupportedFlags = FOF_NO_CONNECTED_ELEMENTS |
1291 FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1292 FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1294 if (fFlags & wUnsupportedFlags)
1295 FIXME("Unsupported flags: %04x\n", fFlags);
1298 /*************************************************************************
1299 * SHFileOperationW [SHELL32.@]
1301 * See SHFileOperationA
1303 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1305 FILE_LIST flFrom, flTo;
1309 return ERROR_INVALID_PARAMETER;
1311 check_flags(lpFileOp->fFlags);
1313 ZeroMemory(&flFrom, sizeof(FILE_LIST));
1314 ZeroMemory(&flTo, sizeof(FILE_LIST));
1316 if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1319 if (lpFileOp->wFunc != FO_DELETE)
1320 parse_file_list(&flTo, lpFileOp->pTo);
1322 switch (lpFileOp->wFunc)
1325 ret = copy_files(lpFileOp, &flFrom, &flTo);
1328 ret = delete_files(lpFileOp, &flFrom);
1331 ret = move_files(lpFileOp, &flFrom, &flTo);
1334 ret = rename_files(lpFileOp, &flFrom, &flTo);
1337 ret = ERROR_INVALID_PARAMETER;
1341 destroy_file_list(&flFrom);
1343 if (lpFileOp->wFunc != FO_DELETE)
1344 destroy_file_list(&flTo);
1346 if (ret == ERROR_CANCELLED)
1347 lpFileOp->fAnyOperationsAborted = TRUE;
1352 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1354 /*************************************************************************
1355 * SHFreeNameMappings [shell32.246]
1357 * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1361 * hNameMapping [I] handle to the name mappings used during renaming of files
1366 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1370 int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1374 LPSHNAMEMAPPINGW lp = DSA_GetItemPtr((HDSA)hNameMapping, i);
1376 SHFree(lp->pszOldPath);
1377 SHFree(lp->pszNewPath);
1379 DSA_Destroy((HDSA)hNameMapping);
1383 /*************************************************************************
1384 * SheGetDirA [SHELL32.@]
1387 HRESULT WINAPI SheGetDirA(LPSTR u, LPSTR v)
1388 { FIXME("%p %p stub\n",u,v);
1392 /*************************************************************************
1393 * SheGetDirW [SHELL32.@]
1396 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1397 { FIXME("%p %p stub\n",u,v);
1401 /*************************************************************************
1402 * SheChangeDirA [SHELL32.@]
1405 HRESULT WINAPI SheChangeDirA(LPSTR u)
1406 { FIXME("(%s),stub\n",debugstr_a(u));
1410 /*************************************************************************
1411 * SheChangeDirW [SHELL32.@]
1414 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1415 { FIXME("(%s),stub\n",debugstr_w(u));
1419 /*************************************************************************
1420 * IsNetDrive [SHELL32.66]
1422 BOOL WINAPI IsNetDrive(DWORD drive)
1425 strcpy(root, "A:\\");
1426 root[0] += (char)drive;
1427 return (GetDriveTypeA(root) == DRIVE_REMOTE);
1431 /*************************************************************************
1432 * RealDriveType [SHELL32.524]
1434 INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1436 char root[] = "A:\\";
1437 root[0] += (char)drive;
1438 return GetDriveTypeA(root);