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;
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 args[0] = (DWORD_PTR)szDir;
145 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
146 szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)args);
148 ZeroMemory(¶ms, sizeof(params));
149 params.cbSize = sizeof(MSGBOXPARAMSW);
150 params.hwndOwner = hWnd;
151 params.hInstance = ids.hIconInstance;
152 params.lpszText = szBuffer;
153 params.lpszCaption = szCaption;
154 params.lpszIcon = (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id);
155 params.dwStyle = MB_YESNO | MB_USERICON;
157 return (IDYES == MessageBoxIndirectW(¶ms));
160 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
162 DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
167 *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
170 MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
173 return E_OUTOFMEMORY;
176 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
178 HeapFree(GetProcessHeap(), 0, wPath);
181 HRESULT WINAPI SHIsFileAvailableOffline(LPCWSTR path, LPDWORD status)
183 FIXME("(%s, %p) stub\n", debugstr_w(path), status);
187 /**************************************************************************
188 * SHELL_DeleteDirectory() [internal]
190 * Asks for confirmation when bShowUI is true and deletes the directory and
191 * all its subdirectories and files if necessary.
193 BOOL SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
197 WIN32_FIND_DATAW wfd;
198 WCHAR szTemp[MAX_PATH];
200 /* Make sure the directory exists before eventually prompting the user */
201 PathCombineW(szTemp, pszDir, wWildcardFile);
202 hFind = FindFirstFileW(szTemp, &wfd);
203 if (hFind == INVALID_HANDLE_VALUE)
206 if (!bShowUI || (ret = SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir)))
210 LPWSTR lp = wfd.cAlternateFileName;
215 PathCombineW(szTemp, pszDir, lp);
216 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
217 ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
219 ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
220 } while (ret && FindNextFileW(hFind, &wfd));
224 ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
228 /**************************************************************************
229 * Win32CreateDirectory [SHELL32.93]
231 * Creates a directory. Also triggers a change notify if one exists.
234 * path [I] path to directory to create
237 * TRUE if successful, FALSE otherwise
240 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
241 * This is Unicode on NT/2000
243 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
248 TRACE("(%s, %p)\n", debugstr_a(path), sec);
250 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
253 retCode = SHNotifyCreateDirectoryW(wPath, sec);
254 SHELL32_FreeUnicodeBuf(wPath);
259 /**********************************************************************/
261 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
263 TRACE("(%s, %p)\n", debugstr_w(path), sec);
265 if (CreateDirectoryW(path, sec))
267 SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
268 return ERROR_SUCCESS;
270 return GetLastError();
273 /**********************************************************************/
275 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
277 if (SHELL_OsIsUnicode())
278 return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
279 return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
282 /************************************************************************
283 * Win32RemoveDirectory [SHELL32.94]
285 * Deletes a directory. Also triggers a change notify if one exists.
288 * path [I] path to directory to delete
291 * TRUE if successful, FALSE otherwise
294 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
295 * This is Unicode on NT/2000
297 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
302 TRACE("(%s)\n", debugstr_a(path));
304 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
307 retCode = SHNotifyRemoveDirectoryW(wPath);
308 SHELL32_FreeUnicodeBuf(wPath);
313 /***********************************************************************/
315 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
318 TRACE("(%s)\n", debugstr_w(path));
320 ret = RemoveDirectoryW(path);
323 /* Directory may be write protected */
324 DWORD dwAttr = GetFileAttributesW(path);
325 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
326 if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
327 ret = RemoveDirectoryW(path);
331 SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
332 return ERROR_SUCCESS;
334 return GetLastError();
337 /***********************************************************************/
339 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
341 if (SHELL_OsIsUnicode())
342 return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
343 return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
346 /************************************************************************
347 * Win32DeleteFile [SHELL32.164]
349 * Deletes a file. Also triggers a change notify if one exists.
352 * path [I] path to file to delete
355 * TRUE if successful, FALSE otherwise
358 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
359 * This is Unicode on NT/2000
361 static DWORD SHNotifyDeleteFileA(LPCSTR path)
366 TRACE("(%s)\n", debugstr_a(path));
368 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
371 retCode = SHNotifyDeleteFileW(wPath);
372 SHELL32_FreeUnicodeBuf(wPath);
377 /***********************************************************************/
379 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
383 TRACE("(%s)\n", debugstr_w(path));
385 ret = DeleteFileW(path);
388 /* File may be write protected or a system file */
389 DWORD dwAttr = GetFileAttributesW(path);
390 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
391 if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
392 ret = DeleteFileW(path);
396 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
397 return ERROR_SUCCESS;
399 return GetLastError();
402 /***********************************************************************/
404 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
406 if (SHELL_OsIsUnicode())
407 return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
408 return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
411 /************************************************************************
412 * SHNotifyMoveFile [internal]
414 * Moves a file. Also triggers a change notify if one exists.
417 * src [I] path to source file to move
418 * dest [I] path to target file to move to
421 * ERORR_SUCCESS if successful
423 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
427 TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
429 ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
431 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
433 ret = MoveFileW(src, dest);
439 dwAttr = SHFindAttrW(dest, FALSE);
440 if (INVALID_FILE_ATTRIBUTES == dwAttr)
442 /* Source file may be write protected or a system file */
443 dwAttr = GetFileAttributesW(src);
444 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
445 if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
446 ret = MoveFileW(src, dest);
451 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
452 return ERROR_SUCCESS;
454 return GetLastError();
457 /************************************************************************
458 * SHNotifyCopyFile [internal]
460 * Copies a file. Also triggers a change notify if one exists.
463 * src [I] path to source file to move
464 * dest [I] path to target file to move to
465 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
466 * a file with this name already exists
469 * ERROR_SUCCESS if successful
471 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
475 TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
477 ret = CopyFileW(src, dest, bFailIfExists);
480 SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
481 return ERROR_SUCCESS;
484 return GetLastError();
487 /*************************************************************************
488 * SHCreateDirectory [SHELL32.165]
490 * This function creates a file system folder whose fully qualified path is
491 * given by path. If one or more of the intermediate folders do not exist,
492 * they will be created as well.
496 * path [I] path of directory to create
499 * ERRROR_SUCCESS or one of the following values:
500 * ERROR_BAD_PATHNAME if the path is relative
501 * ERROR_FILE_EXISTS when a file with that name exists
502 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
503 * ERROR_INVLID_NAME if the path contains invalid chars
504 * ERROR_ALREADY_EXISTS when the directory already exists
505 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
508 * exported by ordinal
510 * WinNT/2000 exports Unicode
512 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
514 if (SHELL_OsIsUnicode())
515 return SHCreateDirectoryExW(hWnd, path, NULL);
516 return SHCreateDirectoryExA(hWnd, path, NULL);
519 /*************************************************************************
520 * SHCreateDirectoryExA [SHELL32.@]
522 * This function creates a file system folder whose fully qualified path is
523 * given by path. If one or more of the intermediate folders do not exist,
524 * they will be created as well.
528 * path [I] path of directory to create
529 * sec [I] security attributes to use or NULL
532 * ERRROR_SUCCESS or one of the following values:
533 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
534 * ERROR_INVLID_NAME if the path contains invalid chars
535 * ERROR_FILE_EXISTS when a file with that name exists
536 * ERROR_ALREADY_EXISTS when the directory already exists
537 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
539 * FIXME: Not implemented yet;
540 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
541 * if the path is a network path to deal with network drivers which might have a limited
542 * but unknown maximum path length. If not:
544 * If hWnd is set to a valid window handle, a message box is displayed warning
545 * the user that the files may not be accessible. If the user chooses not to
546 * proceed, the function returns ERROR_CANCELLED.
548 * If hWnd is set to NULL, no user interface is displayed and the function
549 * returns ERROR_CANCELLED.
551 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
556 TRACE("(%s, %p)\n", debugstr_a(path), sec);
558 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
561 retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
562 SHELL32_FreeUnicodeBuf(wPath);
567 /*************************************************************************
568 * SHCreateDirectoryExW [SHELL32.@]
570 * See SHCreateDirectoryExA.
572 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
574 int ret = ERROR_BAD_PATHNAME;
575 TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
577 if (PathIsRelativeW(path))
583 ret = SHNotifyCreateDirectoryW(path, sec);
584 /* Refuse to work on certain error codes before trying to create directories recursively */
585 if (ret != ERROR_SUCCESS &&
586 ret != ERROR_FILE_EXISTS &&
587 ret != ERROR_ALREADY_EXISTS &&
588 ret != ERROR_FILENAME_EXCED_RANGE)
590 WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1]; /* extra for PathAddBackslash() */
592 lstrcpynW(szTemp, path, MAX_PATH);
593 pEnd = PathAddBackslashW(szTemp);
598 while (*pSlash && *pSlash != '\\')
599 pSlash = CharNextW(pSlash);
603 *pSlash = 0; /* terminate path at separator */
605 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
607 *pSlash++ = '\\'; /* put the separator back */
611 if (ret && hWnd && (ERROR_CANCELLED != ret))
613 /* We failed and should show a dialog box */
614 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
615 ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
621 /*************************************************************************
622 * SHFindAttrW [internal]
624 * Get the Attributes for a file or directory. The difference to GetAttributes()
625 * is that this function will also work for paths containing wildcard characters
629 * path [I] path of directory or file to check
630 * fileOnly [I] TRUE if only files should be found
633 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
634 * the first file or directory found otherwise
636 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
638 WIN32_FIND_DATAW wfd;
639 BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
640 DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
641 HANDLE hFind = FindFirstFileW(pName, &wfd);
643 TRACE("%s %d\n", debugstr_w(pName), fileOnly);
644 if (INVALID_HANDLE_VALUE != hFind)
648 if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
650 dwAttr = wfd.dwFileAttributes;
653 while (FindNextFileW(hFind, &wfd));
659 /*************************************************************************
661 * SHNameTranslate HelperFunction for SHFileOperationA
663 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
664 * is NULL, only the necessary size of the string is determined and returned,
665 * otherwise the ASCII strings are copied into it and the buffer is increased
666 * to point to the location after the final 0 termination char.
668 DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
670 DWORD size = 0, aSize = 0;
671 LPCSTR aString = (LPCSTR)*pWToFrom;
677 size = lstrlenA(aString) + 1;
680 } while ((size != 1) && more);
681 /* The two sizes might be different in the case of multibyte chars */
682 size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
683 if (*wString) /* only in the second loop */
685 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
686 *pWToFrom = *wString;
692 /*************************************************************************
693 * SHFileOperationA [SHELL32.@]
695 * Function to copy, move, delete and create one or more files with optional
699 * lpFileOp [I/O] pointer to a structure containing all the necessary information
702 * Success: ERROR_SUCCESS.
703 * Failure: ERROR_CANCELLED.
708 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
710 SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
713 LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
714 wString = NULL; /* we change this in SHNameTranslate */
717 if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
718 nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
719 if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
720 nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
721 while (1) /* every loop calculate size, second translate also, if we have storage for this */
723 size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
724 size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
725 size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
729 retCode = SHFileOperationW(&nFileOp);
730 HeapFree(GetProcessHeap(), 0, ForFree); /* we cannot use wString, it was changed */
735 wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
736 if (ForFree) continue;
737 retCode = ERROR_OUTOFMEMORY;
738 nFileOp.fAnyOperationsAborted = TRUE;
739 SetLastError(retCode);
744 lpFileOp->hNameMappings = nFileOp.hNameMappings;
745 lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
749 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
767 BOOL bAnyFromWildcard;
768 BOOL bAnyDirectories;
773 static inline void grow_list(FILE_LIST *list)
775 FILE_ENTRY *new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
776 list->num_alloc * 2 * sizeof(*new) );
778 list->num_alloc *= 2;
781 /* adds a file to the FILE_ENTRY struct
783 static void add_file_to_entry(FILE_ENTRY *feFile, LPWSTR szFile)
785 DWORD dwLen = lstrlenW(szFile) + 1;
788 feFile->szFullPath = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
789 lstrcpyW(feFile->szFullPath, szFile);
791 ptr = StrRChrW(szFile, NULL, '\\');
794 dwLen = ptr - szFile + 1;
795 feFile->szDirectory = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
796 lstrcpynW(feFile->szDirectory, szFile, dwLen);
798 dwLen = lstrlenW(feFile->szFullPath) - dwLen + 1;
799 feFile->szFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
800 lstrcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
802 feFile->bFromWildcard = FALSE;
805 static LPWSTR wildcard_to_file(LPWSTR szWildCard, LPWSTR szFileName)
807 LPWSTR szFullPath, ptr;
808 DWORD dwDirLen, dwFullLen;
810 ptr = StrRChrW(szWildCard, NULL, '\\');
811 dwDirLen = ptr - szWildCard + 1;
813 dwFullLen = dwDirLen + lstrlenW(szFileName) + 1;
814 szFullPath = HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
816 lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
817 lstrcatW(szFullPath, szFileName);
822 static void parse_wildcard_files(FILE_LIST *flList, LPWSTR szFile, LPDWORD pdwListIndex)
824 WIN32_FIND_DATAW wfd;
825 HANDLE hFile = FindFirstFileW(szFile, &wfd);
830 for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
832 if (IsDotDir(wfd.cFileName)) continue;
833 if (*pdwListIndex >= flList->num_alloc) grow_list( flList );
834 szFullPath = wildcard_to_file(szFile, wfd.cFileName);
835 file = &flList->feFiles[(*pdwListIndex)++];
836 add_file_to_entry(file, szFullPath);
837 file->bFromWildcard = TRUE;
838 file->attributes = wfd.dwFileAttributes;
839 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
840 HeapFree(GetProcessHeap(), 0, szFullPath);
846 /* takes the null-separated file list and fills out the FILE_LIST */
847 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
849 LPCWSTR ptr = szFiles;
850 WCHAR szCurFile[MAX_PATH];
851 DWORD i = 0, dwDirLen;
854 return ERROR_INVALID_PARAMETER;
856 flList->bAnyFromWildcard = FALSE;
857 flList->bAnyDirectories = FALSE;
858 flList->bAnyDontExist = FALSE;
859 flList->num_alloc = 32;
860 flList->dwNumFiles = 0;
864 return ERROR_ACCESS_DENIED;
866 flList->feFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
867 flList->num_alloc * sizeof(FILE_ENTRY));
871 if (i >= flList->num_alloc) grow_list( flList );
873 /* change relative to absolute path */
874 if (PathIsRelativeW(ptr))
876 dwDirLen = GetCurrentDirectoryW(MAX_PATH, szCurFile) + 1;
877 PathCombineW(szCurFile, szCurFile, ptr);
878 flList->feFiles[i].bFromRelative = TRUE;
882 lstrcpyW(szCurFile, ptr);
883 flList->feFiles[i].bFromRelative = FALSE;
886 /* parse wildcard files if they are in the filename */
887 if (StrPBrkW(szCurFile, wWildcardChars))
889 parse_wildcard_files(flList, szCurFile, &i);
890 flList->bAnyFromWildcard = TRUE;
895 FILE_ENTRY *file = &flList->feFiles[i];
896 add_file_to_entry(file, szCurFile);
897 file->attributes = GetFileAttributesW( file->szFullPath );
898 file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
899 if (!file->bExists) flList->bAnyDontExist = TRUE;
900 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
903 /* advance to the next string */
904 ptr += lstrlenW(ptr) + 1;
907 flList->dwNumFiles = i;
912 /* free the FILE_LIST */
913 static void destroy_file_list(FILE_LIST *flList)
917 if (!flList || !flList->feFiles)
920 for (i = 0; i < flList->dwNumFiles; i++)
922 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
923 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
924 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
927 HeapFree(GetProcessHeap(), 0, flList->feFiles);
930 static void copy_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
932 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
933 SHFILEOPSTRUCTW fileOp;
935 static const WCHAR wildCardFiles[] = {'*','.','*',0};
937 if (IsDotDir(feFrom->szFilename))
940 if (PathFileExistsW(szDestPath))
941 PathCombineW(szTo, szDestPath, feFrom->szFilename);
943 lstrcpyW(szTo, szDestPath);
945 szTo[lstrlenW(szTo) + 1] = '\0';
946 SHNotifyCreateDirectoryW(szTo, NULL);
948 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
949 szFrom[lstrlenW(szFrom) + 1] = '\0';
951 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
952 fileOp.pFrom = szFrom;
954 fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
956 SHFileOperationW(&fileOp);
959 /* copy a file or directory to another directory */
960 static void copy_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
962 if (!PathFileExistsW(feTo->szFullPath))
963 SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
965 if (IsAttribFile(feFrom->attributes))
967 WCHAR szDestPath[MAX_PATH];
969 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
970 SHNotifyCopyFileW(feFrom->szFullPath, szDestPath, FALSE);
972 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
973 copy_dir_to_dir(lpFileOp, feFrom, feTo->szFullPath);
976 static void create_dest_dirs(LPWSTR szDestDir)
979 LPWSTR ptr = StrChrW(szDestDir, '\\');
981 /* make sure all directories up to last one are created */
982 while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
984 lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
986 if (!PathFileExistsW(dir))
987 SHNotifyCreateDirectoryW(dir, NULL);
990 /* create last directory */
991 if (!PathFileExistsW(szDestDir))
992 SHNotifyCreateDirectoryW(szDestDir, NULL);
995 /* the FO_COPY operation */
996 static HRESULT copy_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
999 FILE_ENTRY *entryToCopy;
1000 FILE_ENTRY *fileDest = &flTo->feFiles[0];
1001 BOOL bCancelIfAnyDirectories = FALSE;
1003 if (flFrom->bAnyDontExist)
1004 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1006 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->bAnyFromWildcard)
1007 return ERROR_CANCELLED;
1009 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flTo->dwNumFiles != 1)
1010 return ERROR_CANCELLED;
1012 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->dwNumFiles != 1 &&
1013 flFrom->dwNumFiles != flTo->dwNumFiles)
1015 return ERROR_CANCELLED;
1018 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 &&
1019 !PathFileExistsW(flTo->feFiles[0].szFullPath) &&
1020 IsAttribFile(fileDest->attributes))
1022 bCancelIfAnyDirectories = TRUE;
1025 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
1026 !PathFileExistsW(fileDest->szFullPath))
1028 lpFileOp->fAnyOperationsAborted = TRUE;
1029 return ERROR_CANCELLED;
1032 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flFrom->dwNumFiles != 1 &&
1033 PathFileExistsW(fileDest->szFullPath) &&
1034 IsAttribFile(fileDest->attributes))
1036 return ERROR_CANCELLED;
1039 for (i = 0; i < flFrom->dwNumFiles; i++)
1041 entryToCopy = &flFrom->feFiles[i];
1043 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1044 fileDest = &flTo->feFiles[i];
1046 if (IsAttribDir(entryToCopy->attributes) &&
1047 !lstrcmpiW(entryToCopy->szFullPath, fileDest->szDirectory))
1049 return ERROR_SUCCESS;
1052 if (IsAttribDir(entryToCopy->attributes) && bCancelIfAnyDirectories)
1053 return ERROR_CANCELLED;
1055 create_dest_dirs(fileDest->szDirectory);
1057 if (!lstrcmpiW(entryToCopy->szFullPath, fileDest->szFullPath))
1059 if (IsAttribFile(entryToCopy->attributes))
1060 return ERROR_NO_MORE_SEARCH_HANDLES;
1062 return ERROR_SUCCESS;
1065 if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1066 (flFrom->dwNumFiles == 1 && IsAttribDir(fileDest->attributes)))
1068 copy_to_dir(lpFileOp, entryToCopy, fileDest);
1070 else if (IsAttribDir(entryToCopy->attributes))
1072 copy_dir_to_dir(lpFileOp, entryToCopy, fileDest->szFullPath);
1076 if (SHNotifyCopyFileW(entryToCopy->szFullPath, fileDest->szFullPath, TRUE))
1078 lpFileOp->fAnyOperationsAborted = TRUE;
1079 return ERROR_CANCELLED;
1084 return ERROR_SUCCESS;
1087 static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, BOOL fTrash, FILE_LIST *flFrom)
1089 if (flFrom->dwNumFiles > 1)
1092 const WCHAR format[] = {'%','d',0};
1094 wnsprintfW(tmp, sizeof(tmp)/sizeof(tmp[0]), format, flFrom->dwNumFiles);
1095 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_MULTIPLE_ITEM:ASK_DELETE_MULTIPLE_ITEM), tmp);
1099 FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
1101 if (IsAttribFile(fileEntry->attributes))
1102 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FILE:ASK_DELETE_FILE), fileEntry->szFullPath);
1103 else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1104 return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FOLDER:ASK_DELETE_FOLDER), fileEntry->szFullPath);
1109 /* the FO_DELETE operation */
1110 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom)
1112 FILE_ENTRY *fileEntry;
1117 if (!flFrom->dwNumFiles)
1118 return ERROR_SUCCESS;
1120 /* Windows also checks only the first item */
1121 bTrash = (lpFileOp->fFlags & FOF_ALLOWUNDO)
1122 && TRASH_CanTrashFile(flFrom->feFiles[0].szFullPath);
1124 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (!bTrash && lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1125 if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, bTrash, flFrom))
1127 lpFileOp->fAnyOperationsAborted = TRUE;
1131 for (i = 0; i < flFrom->dwNumFiles; i++)
1134 fileEntry = &flFrom->feFiles[i];
1136 if (!IsAttribFile(fileEntry->attributes) &&
1137 (lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1143 if (TRASH_TrashFile(fileEntry->szFullPath))
1146 /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1147 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1148 bDelete = SHELL_ConfirmDialogW(lpFileOp->hwnd, ASK_CANT_TRASH_ITEM, fileEntry->szFullPath);
1154 lpFileOp->fAnyOperationsAborted = TRUE;
1159 /* delete the file or directory */
1160 if (IsAttribFile(fileEntry->attributes))
1161 bPathExists = DeleteFileW(fileEntry->szFullPath);
1163 bPathExists = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1166 return ERROR_PATH_NOT_FOUND;
1169 return ERROR_SUCCESS;
1172 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
1174 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1175 SHFILEOPSTRUCTW fileOp;
1177 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1179 if (IsDotDir(feFrom->szFilename))
1182 SHNotifyCreateDirectoryW(szDestPath, NULL);
1184 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1185 szFrom[lstrlenW(szFrom) + 1] = '\0';
1187 lstrcpyW(szTo, szDestPath);
1188 szTo[lstrlenW(szDestPath) + 1] = '\0';
1190 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
1191 fileOp.pFrom = szFrom;
1194 SHFileOperationW(&fileOp);
1197 /* moves a file or directory to another directory */
1198 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
1200 WCHAR szDestPath[MAX_PATH];
1202 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1204 if (IsAttribFile(feFrom->attributes))
1205 SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
1206 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1207 move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1210 /* the FO_MOVE operation */
1211 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1214 FILE_ENTRY *entryToMove;
1215 FILE_ENTRY *fileDest;
1217 if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1218 return ERROR_CANCELLED;
1220 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1221 flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1223 return ERROR_CANCELLED;
1226 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1227 !flFrom->bAnyDirectories &&
1228 flFrom->dwNumFiles > flTo->dwNumFiles)
1230 return ERROR_CANCELLED;
1233 if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1234 return ERROR_CANCELLED;
1236 if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1237 flFrom->dwNumFiles != flTo->dwNumFiles)
1239 return ERROR_CANCELLED;
1242 fileDest = &flTo->feFiles[0];
1243 for (i = 0; i < flFrom->dwNumFiles; i++)
1245 entryToMove = &flFrom->feFiles[i];
1247 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1248 fileDest = &flTo->feFiles[i];
1250 if (!PathFileExistsW(fileDest->szDirectory))
1251 return ERROR_CANCELLED;
1253 if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1254 move_to_dir(lpFileOp, entryToMove, fileDest);
1256 SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
1259 return ERROR_SUCCESS;
1262 /* the FO_RENAME files */
1263 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1268 if (flFrom->dwNumFiles != 1)
1269 return ERROR_GEN_FAILURE;
1271 if (flTo->dwNumFiles != 1)
1272 return ERROR_CANCELLED;
1274 feFrom = &flFrom->feFiles[0];
1275 feTo= &flTo->feFiles[0];
1277 /* fail if destination doesn't exist */
1278 if (!feFrom->bExists)
1279 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1281 /* fail if destination already exists */
1283 return ERROR_ALREADY_EXISTS;
1285 return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
1288 /* alert the user if an unsupported flag is used */
1289 static void check_flags(FILEOP_FLAGS fFlags)
1291 WORD wUnsupportedFlags = FOF_NO_CONNECTED_ELEMENTS |
1292 FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1293 FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1295 if (fFlags & wUnsupportedFlags)
1296 FIXME("Unsupported flags: %04x\n", fFlags);
1299 /*************************************************************************
1300 * SHFileOperationW [SHELL32.@]
1302 * See SHFileOperationA
1304 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1306 FILE_LIST flFrom, flTo;
1310 return ERROR_INVALID_PARAMETER;
1312 check_flags(lpFileOp->fFlags);
1314 ZeroMemory(&flFrom, sizeof(FILE_LIST));
1315 ZeroMemory(&flTo, sizeof(FILE_LIST));
1317 if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1320 if (lpFileOp->wFunc != FO_DELETE)
1321 parse_file_list(&flTo, lpFileOp->pTo);
1323 switch (lpFileOp->wFunc)
1326 ret = copy_files(lpFileOp, &flFrom, &flTo);
1329 ret = delete_files(lpFileOp, &flFrom);
1332 ret = move_files(lpFileOp, &flFrom, &flTo);
1335 ret = rename_files(lpFileOp, &flFrom, &flTo);
1338 ret = ERROR_INVALID_PARAMETER;
1342 destroy_file_list(&flFrom);
1344 if (lpFileOp->wFunc != FO_DELETE)
1345 destroy_file_list(&flTo);
1347 if (ret == ERROR_CANCELLED)
1348 lpFileOp->fAnyOperationsAborted = TRUE;
1353 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1355 /*************************************************************************
1356 * SHFreeNameMappings [shell32.246]
1358 * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1362 * hNameMapping [I] handle to the name mappings used during renaming of files
1367 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1371 int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1375 LPSHNAMEMAPPINGW lp = DSA_GetItemPtr((HDSA)hNameMapping, i);
1377 SHFree(lp->pszOldPath);
1378 SHFree(lp->pszNewPath);
1380 DSA_Destroy((HDSA)hNameMapping);
1384 /*************************************************************************
1385 * SheGetDirA [SHELL32.@]
1388 HRESULT WINAPI SheGetDirA(LPSTR u, LPSTR v)
1389 { FIXME("%p %p stub\n",u,v);
1393 /*************************************************************************
1394 * SheGetDirW [SHELL32.@]
1397 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1398 { FIXME("%p %p stub\n",u,v);
1402 /*************************************************************************
1403 * SheChangeDirA [SHELL32.@]
1406 HRESULT WINAPI SheChangeDirA(LPSTR u)
1407 { FIXME("(%s),stub\n",debugstr_a(u));
1411 /*************************************************************************
1412 * SheChangeDirW [SHELL32.@]
1415 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1416 { FIXME("(%s),stub\n",debugstr_w(u));
1420 /*************************************************************************
1421 * IsNetDrive [SHELL32.66]
1423 BOOL WINAPI IsNetDrive(DWORD drive)
1426 strcpy(root, "A:\\");
1427 root[0] += (char)drive;
1428 return (GetDriveTypeA(root) == DRIVE_REMOTE);
1432 /*************************************************************************
1433 * RealDriveType [SHELL32.524]
1435 INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1437 char root[] = "A:\\";
1438 root[0] += (char)drive;
1439 return GetDriveTypeA(root);