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"
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;
94 case ASK_OVERWRITE_FILE:
95 ids->hIconInstance = NULL;
96 ids->icon_resource_id = IDI_WARNING;
97 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
98 ids->text_resource_id = IDS_OVERWRITEFILE_TEXT;
101 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
106 BOOL SHELL_ConfirmDialogW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir)
108 WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
109 SHELL_ConfirmIDstruc ids;
110 MSGBOXPARAMSW params;
112 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
115 LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption)/sizeof(WCHAR));
116 LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText)/sizeof(WCHAR));
118 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
119 szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
121 ZeroMemory(¶ms, sizeof(params));
122 params.cbSize = sizeof(MSGBOXPARAMSW);
123 params.hwndOwner = hWnd;
124 params.hInstance = ids.hIconInstance;
125 params.lpszText = szBuffer;
126 params.lpszCaption = szCaption;
127 params.lpszIcon = (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id);
128 params.dwStyle = MB_OKCANCEL | MB_USERICON;
130 return (IDOK == MessageBoxIndirectW(¶ms));
133 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
135 DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
140 *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
143 MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
146 return E_OUTOFMEMORY;
149 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
151 HeapFree(GetProcessHeap(), 0, wPath);
154 /**************************************************************************
155 * SHELL_DeleteDirectory() [internal]
157 * Asks for confirmation when bShowUI is true and deletes the directory and
158 * all its subdirectories and files if necessary.
160 BOOL SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
164 WIN32_FIND_DATAW wfd;
165 WCHAR szTemp[MAX_PATH];
167 /* Make sure the directory exists before eventually prompting the user */
168 PathCombineW(szTemp, pszDir, wWildcardFile);
169 hFind = FindFirstFileW(szTemp, &wfd);
170 if (hFind == INVALID_HANDLE_VALUE)
173 if (!bShowUI || (ret = SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir)))
177 LPWSTR lp = wfd.cAlternateFileName;
182 PathCombineW(szTemp, pszDir, lp);
183 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
184 ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
186 ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
187 } while (ret && FindNextFileW(hFind, &wfd));
191 ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
195 /**************************************************************************
196 * Win32CreateDirectory [SHELL32.93]
198 * Creates a directory. Also triggers a change notify if one exists.
201 * path [I] path to directory to create
204 * TRUE if successful, FALSE otherwise
207 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
208 * This is Unicode on NT/2000
210 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
215 TRACE("(%s, %p)\n", debugstr_a(path), sec);
217 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
220 retCode = SHNotifyCreateDirectoryW(wPath, sec);
221 SHELL32_FreeUnicodeBuf(wPath);
226 /**********************************************************************/
228 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
230 TRACE("(%s, %p)\n", debugstr_w(path), sec);
232 if (CreateDirectoryW(path, sec))
234 SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
235 return ERROR_SUCCESS;
237 return GetLastError();
240 /**********************************************************************/
242 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
244 if (SHELL_OsIsUnicode())
245 return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
246 return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
249 /************************************************************************
250 * Win32RemoveDirectory [SHELL32.94]
252 * Deletes a directory. Also triggers a change notify if one exists.
255 * path [I] path to directory to delete
258 * TRUE if successful, FALSE otherwise
261 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
262 * This is Unicode on NT/2000
264 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
269 TRACE("(%s)\n", debugstr_a(path));
271 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
274 retCode = SHNotifyRemoveDirectoryW(wPath);
275 SHELL32_FreeUnicodeBuf(wPath);
280 /***********************************************************************/
282 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
285 TRACE("(%s)\n", debugstr_w(path));
287 ret = RemoveDirectoryW(path);
290 /* Directory may be write protected */
291 DWORD dwAttr = GetFileAttributesW(path);
292 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
293 if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
294 ret = RemoveDirectoryW(path);
298 SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
299 return ERROR_SUCCESS;
301 return GetLastError();
304 /***********************************************************************/
306 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
308 if (SHELL_OsIsUnicode())
309 return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
310 return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
313 /************************************************************************
314 * Win32DeleteFile [SHELL32.164]
316 * Deletes a file. Also triggers a change notify if one exists.
319 * path [I] path to file to delete
322 * TRUE if successful, FALSE otherwise
325 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
326 * This is Unicode on NT/2000
328 static DWORD SHNotifyDeleteFileA(LPCSTR path)
333 TRACE("(%s)\n", debugstr_a(path));
335 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
338 retCode = SHNotifyDeleteFileW(wPath);
339 SHELL32_FreeUnicodeBuf(wPath);
344 /***********************************************************************/
346 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
350 TRACE("(%s)\n", debugstr_w(path));
352 ret = DeleteFileW(path);
355 /* File may be write protected or a system file */
356 DWORD dwAttr = GetFileAttributesW(path);
357 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
358 if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
359 ret = DeleteFileW(path);
363 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
364 return ERROR_SUCCESS;
366 return GetLastError();
369 /***********************************************************************/
371 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
373 if (SHELL_OsIsUnicode())
374 return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
375 return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
378 /************************************************************************
379 * SHNotifyMoveFile [internal]
381 * Moves a file. Also triggers a change notify if one exists.
384 * src [I] path to source file to move
385 * dest [I] path to target file to move to
388 * ERORR_SUCCESS if successful
390 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
394 TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
396 ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
398 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
400 ret = MoveFileW(src, dest);
406 dwAttr = SHFindAttrW(dest, FALSE);
407 if (INVALID_FILE_ATTRIBUTES == dwAttr)
409 /* Source file may be write protected or a system file */
410 dwAttr = GetFileAttributesW(src);
411 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
412 if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
413 ret = MoveFileW(src, dest);
418 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
419 return ERROR_SUCCESS;
421 return GetLastError();
424 /************************************************************************
425 * SHNotifyCopyFile [internal]
427 * Copies a file. Also triggers a change notify if one exists.
430 * src [I] path to source file to move
431 * dest [I] path to target file to move to
432 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
433 * a file with this name already exists
436 * ERROR_SUCCESS if successful
438 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
442 TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
444 ret = CopyFileW(src, dest, bFailIfExists);
447 SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
448 return ERROR_SUCCESS;
451 return GetLastError();
454 /*************************************************************************
455 * SHCreateDirectory [SHELL32.165]
457 * This function creates a file system folder whose fully qualified path is
458 * given by path. If one or more of the intermediate folders do not exist,
459 * they will be created as well.
463 * path [I] path of directory to create
466 * ERRROR_SUCCESS or one of the following values:
467 * ERROR_BAD_PATHNAME if the path is relative
468 * ERROR_FILE_EXISTS when a file with that name exists
469 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
470 * ERROR_INVLID_NAME if the path contains invalid chars
471 * ERROR_ALREADY_EXISTS when the directory already exists
472 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
475 * exported by ordinal
477 * WinNT/2000 exports Unicode
479 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
481 if (SHELL_OsIsUnicode())
482 return SHCreateDirectoryExW(hWnd, path, NULL);
483 return SHCreateDirectoryExA(hWnd, path, NULL);
486 /*************************************************************************
487 * SHCreateDirectoryExA [SHELL32.@]
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
496 * sec [I] security attributes to use or NULL
499 * ERRROR_SUCCESS or one of the following values:
500 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
501 * ERROR_INVLID_NAME if the path contains invalid chars
502 * ERROR_FILE_EXISTS when a file with that name exists
503 * ERROR_ALREADY_EXISTS when the directory already exists
504 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
506 * FIXME: Not implemented yet;
507 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
508 * if the path is a network path to deal with network drivers which might have a limited
509 * but unknown maximum path length. If not:
511 * If hWnd is set to a valid window handle, a message box is displayed warning
512 * the user that the files may not be accessible. If the user chooses not to
513 * proceed, the function returns ERROR_CANCELLED.
515 * If hWnd is set to NULL, no user interface is displayed and the function
516 * returns ERROR_CANCELLED.
518 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
523 TRACE("(%s, %p)\n", debugstr_a(path), sec);
525 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
528 retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
529 SHELL32_FreeUnicodeBuf(wPath);
534 /*************************************************************************
535 * SHCreateDirectoryExW [SHELL32.@]
537 * See SHCreateDirectoryExA.
539 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
541 int ret = ERROR_BAD_PATHNAME;
542 TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
544 if (PathIsRelativeW(path))
550 ret = SHNotifyCreateDirectoryW(path, sec);
551 /* Refuse to work on certain error codes before trying to create directories recursively */
552 if (ret != ERROR_SUCCESS &&
553 ret != ERROR_FILE_EXISTS &&
554 ret != ERROR_ALREADY_EXISTS &&
555 ret != ERROR_FILENAME_EXCED_RANGE)
557 WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1]; /* extra for PathAddBackslash() */
559 lstrcpynW(szTemp, path, MAX_PATH);
560 pEnd = PathAddBackslashW(szTemp);
565 while (*pSlash && *pSlash != '\\')
566 pSlash = CharNextW(pSlash);
570 *pSlash = 0; /* terminate path at separator */
572 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
574 *pSlash++ = '\\'; /* put the separator back */
578 if (ret && hWnd && (ERROR_CANCELLED != ret))
580 /* We failed and should show a dialog box */
581 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
582 ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
588 /*************************************************************************
589 * SHFindAttrW [internal]
591 * Get the Attributes for a file or directory. The difference to GetAttributes()
592 * is that this function will also work for paths containing wildcard characters
596 * path [I] path of directory or file to check
597 * fileOnly [I] TRUE if only files should be found
600 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
601 * the first file or directory found otherwise
603 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
605 WIN32_FIND_DATAW wfd;
606 BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
607 DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
608 HANDLE hFind = FindFirstFileW(pName, &wfd);
610 TRACE("%s %d\n", debugstr_w(pName), fileOnly);
611 if (INVALID_HANDLE_VALUE != hFind)
615 if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
617 dwAttr = wfd.dwFileAttributes;
620 while (FindNextFileW(hFind, &wfd));
626 /*************************************************************************
628 * SHNameTranslate HelperFunction for SHFileOperationA
630 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
631 * is NULL, only the necessary size of the string is determined and returned,
632 * otherwise the ASCII strings are copied into it and the buffer is increased
633 * to point to the location after the final 0 termination char.
635 DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
637 DWORD size = 0, aSize = 0;
638 LPCSTR aString = (LPCSTR)*pWToFrom;
644 size = lstrlenA(aString) + 1;
647 } while ((size != 1) && more);
648 /* The two sizes might be different in the case of multibyte chars */
649 size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
650 if (*wString) /* only in the second loop */
652 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
653 *pWToFrom = *wString;
659 /*************************************************************************
660 * SHFileOperationA [SHELL32.@]
662 * Function to copy, move, delete and create one or more files with optional
666 * lpFileOp [I/O] pointer to a structure containing all the necessary information
669 * Success: ERROR_SUCCESS.
670 * Failure: ERROR_CANCELLED.
675 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
677 SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
680 LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
681 wString = NULL; /* we change this in SHNameTranslate */
684 if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
685 nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
686 if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
687 nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
688 while (1) /* every loop calculate size, second translate also, if we have storage for this */
690 size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
691 size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
692 size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
696 retCode = SHFileOperationW(&nFileOp);
697 HeapFree(GetProcessHeap(), 0, ForFree); /* we cannot use wString, it was changed */
702 wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
703 if (ForFree) continue;
704 retCode = ERROR_OUTOFMEMORY;
705 nFileOp.fAnyOperationsAborted = TRUE;
706 SetLastError(retCode);
711 lpFileOp->hNameMappings = nFileOp.hNameMappings;
712 lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
716 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
734 BOOL bAnyFromWildcard;
735 BOOL bAnyDirectories;
740 static inline void grow_list(FILE_LIST *list)
742 FILE_ENTRY *new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
743 list->num_alloc * 2 * sizeof(*new) );
745 list->num_alloc *= 2;
748 /* adds a file to the FILE_ENTRY struct
750 static void add_file_to_entry(FILE_ENTRY *feFile, LPWSTR szFile)
752 DWORD dwLen = strlenW(szFile) + 1;
755 feFile->szFullPath = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
756 strcpyW(feFile->szFullPath, szFile);
758 ptr = StrRChrW(szFile, NULL, '\\');
761 dwLen = ptr - szFile + 1;
762 feFile->szDirectory = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
763 lstrcpynW(feFile->szDirectory, szFile, dwLen);
765 dwLen = strlenW(feFile->szFullPath) - dwLen + 1;
766 feFile->szFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
767 strcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
769 feFile->bFromWildcard = FALSE;
772 static LPWSTR wildcard_to_file(LPWSTR szWildCard, LPWSTR szFileName)
774 LPWSTR szFullPath, ptr;
775 DWORD dwDirLen, dwFullLen;
777 ptr = StrRChrW(szWildCard, NULL, '\\');
778 dwDirLen = ptr - szWildCard + 1;
780 dwFullLen = dwDirLen + strlenW(szFileName) + 1;
781 szFullPath = HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
783 lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
784 strcatW(szFullPath, szFileName);
789 static void parse_wildcard_files(FILE_LIST *flList, LPWSTR szFile, LPDWORD pdwListIndex)
791 WIN32_FIND_DATAW wfd;
792 HANDLE hFile = FindFirstFileW(szFile, &wfd);
797 for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
799 if (IsDotDir(wfd.cFileName)) continue;
800 if (*pdwListIndex >= flList->num_alloc) grow_list( flList );
801 szFullPath = wildcard_to_file(szFile, wfd.cFileName);
802 file = &flList->feFiles[(*pdwListIndex)++];
803 add_file_to_entry(file, szFullPath);
804 file->bFromWildcard = TRUE;
805 file->attributes = wfd.dwFileAttributes;
806 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
807 HeapFree(GetProcessHeap(), 0, szFullPath);
813 /* takes the null-separated file list and fills out the FILE_LIST */
814 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
816 LPCWSTR ptr = szFiles;
817 WCHAR szCurFile[MAX_PATH];
818 DWORD i = 0, dwDirLen;
821 return ERROR_INVALID_PARAMETER;
823 flList->bAnyFromWildcard = FALSE;
824 flList->bAnyDirectories = FALSE;
825 flList->bAnyDontExist = FALSE;
826 flList->num_alloc = 32;
827 flList->dwNumFiles = 0;
831 return ERROR_ACCESS_DENIED;
833 flList->feFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
834 flList->num_alloc * sizeof(FILE_ENTRY));
838 if (i >= flList->num_alloc) grow_list( flList );
840 /* change relative to absolute path */
841 if (PathIsRelativeW(ptr))
843 dwDirLen = GetCurrentDirectoryW(MAX_PATH, szCurFile) + 1;
844 PathCombineW(szCurFile, szCurFile, ptr);
845 flList->feFiles[i].bFromRelative = TRUE;
849 strcpyW(szCurFile, ptr);
850 flList->feFiles[i].bFromRelative = FALSE;
853 /* parse wildcard files if they are in the filename */
854 if (StrPBrkW(szCurFile, wWildcardChars))
856 parse_wildcard_files(flList, szCurFile, &i);
857 flList->bAnyFromWildcard = TRUE;
862 FILE_ENTRY *file = &flList->feFiles[i];
863 add_file_to_entry(file, szCurFile);
864 file->attributes = GetFileAttributesW( file->szFullPath );
865 file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
866 if (!file->bExists) flList->bAnyDontExist = TRUE;
867 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
870 /* advance to the next string */
871 ptr += strlenW(ptr) + 1;
874 flList->dwNumFiles = i;
879 /* free the FILE_LIST */
880 static void destroy_file_list(FILE_LIST *flList)
884 if (!flList || !flList->feFiles)
887 for (i = 0; i < flList->dwNumFiles; i++)
889 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
890 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
891 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
894 HeapFree(GetProcessHeap(), 0, flList->feFiles);
897 static void copy_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
899 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
900 SHFILEOPSTRUCTW fileOp;
902 static const WCHAR wildCardFiles[] = {'*','.','*',0};
904 if (IsDotDir(feFrom->szFilename))
907 if (PathFileExistsW(szDestPath))
908 PathCombineW(szTo, szDestPath, feFrom->szFilename);
910 strcpyW(szTo, szDestPath);
912 szTo[strlenW(szTo) + 1] = '\0';
913 SHNotifyCreateDirectoryW(szTo, NULL);
915 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
916 szFrom[strlenW(szFrom) + 1] = '\0';
918 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
919 fileOp.pFrom = szFrom;
921 fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
923 SHFileOperationW(&fileOp);
926 /* copy a file or directory to another directory */
927 static void copy_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
929 WCHAR szDestPath[MAX_PATH];
931 if (!PathFileExistsW(feTo->szFullPath))
932 SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
934 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
936 if (IsAttribFile(feFrom->attributes))
937 SHNotifyCopyFileW(feFrom->szFullPath, szDestPath, FALSE);
938 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
939 copy_dir_to_dir(lpFileOp, feFrom, szDestPath);
942 static void create_dest_dirs(LPWSTR szDestDir)
945 LPWSTR ptr = StrChrW(szDestDir, '\\');
947 /* make sure all directories up to last one are created */
948 while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
950 lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
952 if (!PathFileExistsW(dir))
953 SHNotifyCreateDirectoryW(dir, NULL);
956 /* create last directory */
957 if (!PathFileExistsW(szDestDir))
958 SHNotifyCreateDirectoryW(szDestDir, NULL);
961 /* the FO_COPY operation */
962 static HRESULT copy_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
965 FILE_ENTRY *entryToCopy;
966 FILE_ENTRY *fileDest = &flTo->feFiles[0];
967 BOOL bCancelIfAnyDirectories = FALSE;
969 if (flFrom->bAnyDontExist)
970 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
972 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->bAnyFromWildcard)
973 return ERROR_CANCELLED;
975 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flTo->dwNumFiles != 1)
976 return ERROR_CANCELLED;
978 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->dwNumFiles != 1 &&
979 flFrom->dwNumFiles != flTo->dwNumFiles)
981 return ERROR_CANCELLED;
984 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 &&
985 !PathFileExistsW(flTo->feFiles[0].szFullPath) &&
986 IsAttribFile(fileDest->attributes))
988 bCancelIfAnyDirectories = TRUE;
991 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
992 !PathFileExistsW(fileDest->szFullPath))
994 lpFileOp->fAnyOperationsAborted = TRUE;
995 return ERROR_CANCELLED;
998 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flFrom->dwNumFiles != 1 &&
999 PathFileExistsW(fileDest->szFullPath) &&
1000 IsAttribFile(fileDest->attributes))
1002 return ERROR_CANCELLED;
1005 for (i = 0; i < flFrom->dwNumFiles; i++)
1007 entryToCopy = &flFrom->feFiles[i];
1009 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1010 fileDest = &flTo->feFiles[i];
1012 if (IsAttribDir(entryToCopy->attributes) &&
1013 !lstrcmpW(entryToCopy->szFullPath, fileDest->szDirectory))
1015 return ERROR_SUCCESS;
1018 if (IsAttribDir(entryToCopy->attributes) && bCancelIfAnyDirectories)
1019 return ERROR_CANCELLED;
1021 create_dest_dirs(fileDest->szDirectory);
1023 if (!strcmpW(entryToCopy->szFullPath, fileDest->szFullPath))
1025 if (IsAttribFile(entryToCopy->attributes))
1026 return ERROR_NO_MORE_SEARCH_HANDLES;
1028 return ERROR_SUCCESS;
1031 if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1032 (flFrom->dwNumFiles == 1 && IsAttribDir(fileDest->attributes)))
1034 copy_to_dir(lpFileOp, entryToCopy, fileDest);
1036 else if (IsAttribDir(entryToCopy->attributes))
1038 copy_dir_to_dir(lpFileOp, entryToCopy, fileDest->szFullPath);
1042 if (SHNotifyCopyFileW(entryToCopy->szFullPath, fileDest->szFullPath, TRUE))
1044 lpFileOp->fAnyOperationsAborted = TRUE;
1045 return ERROR_CANCELLED;
1050 return ERROR_SUCCESS;
1053 static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, FILE_LIST *flFrom)
1055 if (flFrom->dwNumFiles > 1)
1058 const WCHAR format[] = {'%','d',0};
1060 wnsprintfW(tmp, sizeof(tmp)/sizeof(tmp[0]), format, flFrom->dwNumFiles);
1061 return SHELL_ConfirmDialogW(hWnd, ASK_DELETE_MULTIPLE_ITEM, tmp);
1065 FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
1067 if (IsAttribFile(fileEntry->attributes))
1068 return SHELL_ConfirmDialogW(hWnd, ASK_DELETE_FILE, fileEntry->szFullPath);
1069 else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1070 return SHELL_ConfirmDialogW(hWnd, ASK_DELETE_FOLDER, fileEntry->szFullPath);
1075 /* the FO_DELETE operation */
1076 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom)
1078 FILE_ENTRY *fileEntry;
1082 if (!flFrom->dwNumFiles)
1083 return ERROR_SUCCESS;
1085 if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1086 if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, flFrom))
1088 lpFileOp->fAnyOperationsAborted = TRUE;
1092 for (i = 0; i < flFrom->dwNumFiles; i++)
1095 fileEntry = &flFrom->feFiles[i];
1097 /* delete the file or directory */
1098 if (IsAttribFile(fileEntry->attributes))
1099 bPathExists = DeleteFileW(fileEntry->szFullPath);
1100 else if (!(lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1101 bPathExists = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1104 return ERROR_PATH_NOT_FOUND;
1107 return ERROR_SUCCESS;
1110 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
1112 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1113 SHFILEOPSTRUCTW fileOp;
1115 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1117 if (IsDotDir(feFrom->szFilename))
1120 SHNotifyCreateDirectoryW(szDestPath, NULL);
1122 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1123 szFrom[strlenW(szFrom) + 1] = '\0';
1125 strcpyW(szTo, szDestPath);
1126 szTo[strlenW(szDestPath) + 1] = '\0';
1128 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
1129 fileOp.pFrom = szFrom;
1132 SHFileOperationW(&fileOp);
1135 /* moves a file or directory to another directory */
1136 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
1138 WCHAR szDestPath[MAX_PATH];
1140 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1142 if (IsAttribFile(feFrom->attributes))
1143 SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
1144 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1145 move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1148 /* the FO_MOVE operation */
1149 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1152 FILE_ENTRY *entryToMove;
1153 FILE_ENTRY *fileDest;
1155 if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1156 return ERROR_CANCELLED;
1158 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1159 flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1161 return ERROR_CANCELLED;
1164 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1165 !flFrom->bAnyDirectories &&
1166 flFrom->dwNumFiles > flTo->dwNumFiles)
1168 return ERROR_CANCELLED;
1171 if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1172 return ERROR_CANCELLED;
1174 if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1175 flFrom->dwNumFiles != flTo->dwNumFiles)
1177 return ERROR_CANCELLED;
1180 fileDest = &flTo->feFiles[0];
1181 for (i = 0; i < flFrom->dwNumFiles; i++)
1183 entryToMove = &flFrom->feFiles[i];
1185 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1186 fileDest = &flTo->feFiles[i];
1188 if (!PathFileExistsW(fileDest->szDirectory))
1189 return ERROR_CANCELLED;
1191 if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1192 move_to_dir(lpFileOp, entryToMove, fileDest);
1194 SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
1197 return ERROR_SUCCESS;
1200 /* the FO_RENAME files */
1201 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1206 if (flFrom->dwNumFiles != 1)
1207 return ERROR_GEN_FAILURE;
1209 if (flTo->dwNumFiles != 1)
1210 return ERROR_CANCELLED;
1212 feFrom = &flFrom->feFiles[0];
1213 feTo= &flTo->feFiles[0];
1215 /* fail if destination doesn't exist */
1216 if (!feFrom->bExists)
1217 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1219 /* fail if destination already exists */
1221 return ERROR_ALREADY_EXISTS;
1223 return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
1226 /* alert the user if an unsupported flag is used */
1227 static void check_flags(FILEOP_FLAGS fFlags)
1229 WORD wUnsupportedFlags = FOF_ALLOWUNDO | FOF_NO_CONNECTED_ELEMENTS |
1230 FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1231 FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1233 if (fFlags & wUnsupportedFlags)
1234 FIXME("Unsupported flags: %04x\n", fFlags);
1237 /*************************************************************************
1238 * SHFileOperationW [SHELL32.@]
1240 * See SHFileOperationA
1242 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1244 FILE_LIST flFrom, flTo;
1248 return ERROR_INVALID_PARAMETER;
1250 check_flags(lpFileOp->fFlags);
1252 ZeroMemory(&flFrom, sizeof(FILE_LIST));
1253 ZeroMemory(&flTo, sizeof(FILE_LIST));
1255 if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1258 if (lpFileOp->wFunc != FO_DELETE)
1259 parse_file_list(&flTo, lpFileOp->pTo);
1261 switch (lpFileOp->wFunc)
1264 ret = copy_files(lpFileOp, &flFrom, &flTo);
1267 ret = delete_files(lpFileOp, &flFrom);
1270 ret = move_files(lpFileOp, &flFrom, &flTo);
1273 ret = rename_files(lpFileOp, &flFrom, &flTo);
1276 ret = ERROR_INVALID_PARAMETER;
1280 destroy_file_list(&flFrom);
1282 if (lpFileOp->wFunc != FO_DELETE)
1283 destroy_file_list(&flTo);
1285 if (ret == ERROR_CANCELLED)
1286 lpFileOp->fAnyOperationsAborted = TRUE;
1291 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1293 /*************************************************************************
1294 * SHFreeNameMappings [shell32.246]
1296 * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1300 * hNameMapping [I] handle to the name mappings used during renaming of files
1305 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1309 int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1313 LPSHNAMEMAPPINGW lp = DSA_GetItemPtr((HDSA)hNameMapping, i);
1315 SHFree(lp->pszOldPath);
1316 SHFree(lp->pszNewPath);
1318 DSA_Destroy((HDSA)hNameMapping);
1322 /*************************************************************************
1323 * SheGetDirA [SHELL32.@]
1326 HRESULT WINAPI SheGetDirA(LPSTR u, LPSTR v)
1327 { FIXME("%p %p stub\n",u,v);
1331 /*************************************************************************
1332 * SheGetDirW [SHELL32.@]
1335 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1336 { FIXME("%p %p stub\n",u,v);
1340 /*************************************************************************
1341 * SheChangeDirA [SHELL32.@]
1344 HRESULT WINAPI SheChangeDirA(LPSTR u)
1345 { FIXME("(%s),stub\n",debugstr_a(u));
1349 /*************************************************************************
1350 * SheChangeDirW [SHELL32.@]
1353 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1354 { FIXME("(%s),stub\n",debugstr_w(u));
1358 /*************************************************************************
1359 * IsNetDrive [SHELL32.66]
1361 BOOL WINAPI IsNetDrive(DWORD drive)
1364 strcpy(root, "A:\\");
1365 root[0] += (char)drive;
1366 return (GetDriveTypeA(root) == DRIVE_REMOTE);
1370 /*************************************************************************
1371 * RealDriveType [SHELL32.524]
1373 INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1375 char root[] = "A:\\";
1376 root[0] += (char)drive;
1377 return GetDriveTypeA(root);