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 UINT caption_resource_id, text_resource_id;
71 } SHELL_ConfirmIDstruc;
73 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
75 switch (nKindOfDialog) {
77 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
78 ids->text_resource_id = IDS_DELETEITEM_TEXT;
80 case ASK_DELETE_FOLDER:
81 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
82 ids->text_resource_id = IDS_DELETEITEM_TEXT;
84 case ASK_DELETE_MULTIPLE_ITEM:
85 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
86 ids->text_resource_id = IDS_DELETEMULTIPLE_TEXT;
88 case ASK_OVERWRITE_FILE:
89 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
90 ids->text_resource_id = IDS_OVERWRITEFILE_TEXT;
93 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
98 BOOL SHELL_ConfirmDialogW(int nKindOfDialog, LPCWSTR szDir)
100 WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
101 SHELL_ConfirmIDstruc ids;
103 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
106 LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption));
107 LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText));
109 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
110 szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
112 return (IDOK == MessageBoxW(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
115 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
117 DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
122 *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
125 MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
128 return E_OUTOFMEMORY;
131 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
133 HeapFree(GetProcessHeap(), 0, wPath);
136 /**************************************************************************
137 * SHELL_DeleteDirectory() [internal]
139 * Asks for confirmation when bShowUI is true and deletes the directory and
140 * all its subdirectories and files if necessary.
142 BOOL SHELL_DeleteDirectoryW(LPCWSTR pszDir, BOOL bShowUI)
146 WIN32_FIND_DATAW wfd;
147 WCHAR szTemp[MAX_PATH];
149 /* Make sure the directory exists before eventually prompting the user */
150 PathCombineW(szTemp, pszDir, wWildcardFile);
151 hFind = FindFirstFileW(szTemp, &wfd);
152 if (hFind == INVALID_HANDLE_VALUE)
155 if (!bShowUI || (ret = SHELL_ConfirmDialogW(ASK_DELETE_FOLDER, pszDir)))
159 LPWSTR lp = wfd.cAlternateFileName;
164 PathCombineW(szTemp, pszDir, lp);
165 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
166 ret = SHELL_DeleteDirectoryW(szTemp, FALSE);
168 ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
169 } while (ret && FindNextFileW(hFind, &wfd));
173 ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
177 /**************************************************************************
178 * SHELL_DeleteFileW() [internal]
180 BOOL SHELL_DeleteFileW(LPCWSTR pszFile, BOOL bShowUI)
182 if (bShowUI && !SHELL_ConfirmDialogW(ASK_DELETE_FILE, pszFile))
185 return (SHNotifyDeleteFileW(pszFile) == ERROR_SUCCESS);
188 /**************************************************************************
189 * Win32CreateDirectory [SHELL32.93]
191 * Creates a directory. Also triggers a change notify if one exists.
194 * path [I] path to directory to create
197 * TRUE if successful, FALSE otherwise
200 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
201 * This is Unicode on NT/2000
203 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
208 TRACE("(%s, %p)\n", debugstr_a(path), sec);
210 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
213 retCode = SHNotifyCreateDirectoryW(wPath, sec);
214 SHELL32_FreeUnicodeBuf(wPath);
219 /**********************************************************************/
221 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
223 TRACE("(%s, %p)\n", debugstr_w(path), sec);
225 if (CreateDirectoryW(path, sec))
227 SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
228 return ERROR_SUCCESS;
230 return GetLastError();
233 /**********************************************************************/
235 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
237 if (SHELL_OsIsUnicode())
238 return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
239 return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
242 /************************************************************************
243 * Win32RemoveDirectory [SHELL32.94]
245 * Deletes a directory. Also triggers a change notify if one exists.
248 * path [I] path to directory to delete
251 * TRUE if successful, FALSE otherwise
254 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
255 * This is Unicode on NT/2000
257 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
262 TRACE("(%s)\n", debugstr_a(path));
264 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
267 retCode = SHNotifyRemoveDirectoryW(wPath);
268 SHELL32_FreeUnicodeBuf(wPath);
273 /***********************************************************************/
275 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
278 TRACE("(%s)\n", debugstr_w(path));
280 ret = RemoveDirectoryW(path);
283 /* Directory may be write protected */
284 DWORD dwAttr = GetFileAttributesW(path);
285 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
286 if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
287 ret = RemoveDirectoryW(path);
291 SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
292 return ERROR_SUCCESS;
294 return GetLastError();
297 /***********************************************************************/
299 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
301 if (SHELL_OsIsUnicode())
302 return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
303 return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
306 /************************************************************************
307 * Win32DeleteFile [SHELL32.164]
309 * Deletes a file. Also triggers a change notify if one exists.
312 * path [I] path to file to delete
315 * TRUE if successful, FALSE otherwise
318 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
319 * This is Unicode on NT/2000
321 static DWORD SHNotifyDeleteFileA(LPCSTR path)
326 TRACE("(%s)\n", debugstr_a(path));
328 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
331 retCode = SHNotifyDeleteFileW(wPath);
332 SHELL32_FreeUnicodeBuf(wPath);
337 /***********************************************************************/
339 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
343 TRACE("(%s)\n", debugstr_w(path));
345 ret = DeleteFileW(path);
348 /* File may be write protected or a system file */
349 DWORD dwAttr = GetFileAttributesW(path);
350 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
351 if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
352 ret = DeleteFileW(path);
356 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
357 return ERROR_SUCCESS;
359 return GetLastError();
362 /***********************************************************************/
364 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
366 if (SHELL_OsIsUnicode())
367 return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
368 return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
371 /************************************************************************
372 * SHNotifyMoveFile [internal]
374 * Moves a file. Also triggers a change notify if one exists.
377 * src [I] path to source file to move
378 * dest [I] path to target file to move to
381 * ERORR_SUCCESS if successful
383 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
387 TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
389 ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
391 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
393 ret = MoveFileW(src, dest);
399 dwAttr = SHFindAttrW(dest, FALSE);
400 if (INVALID_FILE_ATTRIBUTES == dwAttr)
402 /* Source file may be write protected or a system file */
403 dwAttr = GetFileAttributesW(src);
404 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
405 if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
406 ret = MoveFileW(src, dest);
411 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
412 return ERROR_SUCCESS;
414 return GetLastError();
417 /************************************************************************
418 * SHNotifyCopyFile [internal]
420 * Copies a file. Also triggers a change notify if one exists.
423 * src [I] path to source file to move
424 * dest [I] path to target file to move to
425 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
426 * a file with this name already exists
429 * ERROR_SUCCESS if successful
431 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
435 TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
437 ret = CopyFileW(src, dest, bFailIfExists);
440 SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
441 return ERROR_SUCCESS;
444 return GetLastError();
447 /*************************************************************************
448 * SHCreateDirectory [SHELL32.165]
450 * This function creates a file system folder whose fully qualified path is
451 * given by path. If one or more of the intermediate folders do not exist,
452 * they will be created as well.
456 * path [I] path of directory to create
459 * ERRROR_SUCCESS or one of the following values:
460 * ERROR_BAD_PATHNAME if the path is relative
461 * ERROR_FILE_EXISTS when a file with that name exists
462 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
463 * ERROR_INVLID_NAME if the path contains invalid chars
464 * ERROR_ALREADY_EXISTS when the directory already exists
465 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
468 * exported by ordinal
470 * WinNT/2000 exports Unicode
472 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
474 if (SHELL_OsIsUnicode())
475 return SHCreateDirectoryExW(hWnd, path, NULL);
476 return SHCreateDirectoryExA(hWnd, path, NULL);
479 /*************************************************************************
480 * SHCreateDirectoryExA [SHELL32.@]
482 * This function creates a file system folder whose fully qualified path is
483 * given by path. If one or more of the intermediate folders do not exist,
484 * they will be created as well.
488 * path [I] path of directory to create
489 * sec [I] security attributes to use or NULL
492 * ERRROR_SUCCESS or one of the following values:
493 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
494 * ERROR_INVLID_NAME if the path contains invalid chars
495 * ERROR_FILE_EXISTS when a file with that name exists
496 * ERROR_ALREADY_EXISTS when the directory already exists
497 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
499 * FIXME: Not implemented yet;
500 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
501 * if the path is a network path to deal with network drivers which might have a limited
502 * but unknown maximum path length. If not:
504 * If hWnd is set to a valid window handle, a message box is displayed warning
505 * the user that the files may not be accessible. If the user chooses not to
506 * proceed, the function returns ERROR_CANCELLED.
508 * If hWnd is set to NULL, no user interface is displayed and the function
509 * returns ERROR_CANCELLED.
511 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
516 TRACE("(%s, %p)\n", debugstr_a(path), sec);
518 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
521 retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
522 SHELL32_FreeUnicodeBuf(wPath);
527 /*************************************************************************
528 * SHCreateDirectoryExW [SHELL32.@]
530 * See SHCreateDirectoryExA.
532 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
534 int ret = ERROR_BAD_PATHNAME;
535 TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
537 if (PathIsRelativeW(path))
543 ret = SHNotifyCreateDirectoryW(path, sec);
544 /* Refuse to work on certain error codes before trying to create directories recursively */
545 if (ret != ERROR_SUCCESS &&
546 ret != ERROR_FILE_EXISTS &&
547 ret != ERROR_ALREADY_EXISTS &&
548 ret != ERROR_FILENAME_EXCED_RANGE)
550 WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1]; /* extra for PathAddBackslash() */
552 lstrcpynW(szTemp, path, MAX_PATH);
553 pEnd = PathAddBackslashW(szTemp);
558 while (*pSlash && *pSlash != '\\')
559 pSlash = CharNextW(pSlash);
563 *pSlash = 0; /* terminate path at separator */
565 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
567 *pSlash++ = '\\'; /* put the separator back */
571 if (ret && hWnd && (ERROR_CANCELLED != ret))
573 /* We failed and should show a dialog box */
574 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
575 ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
581 /*************************************************************************
582 * SHFindAttrW [internal]
584 * Get the Attributes for a file or directory. The difference to GetAttributes()
585 * is that this function will also work for paths containing wildcard characters
589 * path [I] path of directory or file to check
590 * fileOnly [I] TRUE if only files should be found
593 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
594 * the first file or directory found otherwise
596 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
598 WIN32_FIND_DATAW wfd;
599 BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
600 DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
601 HANDLE hFind = FindFirstFileW(pName, &wfd);
603 TRACE("%s %d\n", debugstr_w(pName), fileOnly);
604 if (INVALID_HANDLE_VALUE != hFind)
608 if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
610 dwAttr = wfd.dwFileAttributes;
613 while (FindNextFileW(hFind, &wfd));
619 /*************************************************************************
621 * SHNameTranslate HelperFunction for SHFileOperationA
623 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
624 * is NULL, only the necessary size of the string is determined and returned,
625 * otherwise the ASCII strings are copied into it and the buffer is increased
626 * to point to the location after the final 0 termination char.
628 DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
630 DWORD size = 0, aSize = 0;
631 LPCSTR aString = (LPCSTR)*pWToFrom;
637 size = lstrlenA(aString) + 1;
640 } while ((size != 1) && more);
641 /* The two sizes might be different in the case of multibyte chars */
642 size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
643 if (*wString) /* only in the second loop */
645 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
646 *pWToFrom = *wString;
652 /*************************************************************************
653 * SHFileOperationA [SHELL32.@]
655 * Function to copy, move, delete and create one or more files with optional
659 * lpFileOp [I/O] pointer to a structure containing all the necessary information
662 * Success: ERROR_SUCCESS.
663 * Failure: ERROR_CANCELLED.
668 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
670 SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
673 LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
674 wString = NULL; /* we change this in SHNameTranslate */
677 if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
678 nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
679 if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
680 nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
681 while (1) /* every loop calculate size, second translate also, if we have storage for this */
683 size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
684 size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
685 size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
689 retCode = SHFileOperationW(&nFileOp);
690 HeapFree(GetProcessHeap(), 0, ForFree); /* we cannot use wString, it was changed */
695 wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
696 if (ForFree) continue;
697 retCode = ERROR_OUTOFMEMORY;
698 nFileOp.fAnyOperationsAborted = TRUE;
699 SetLastError(retCode);
704 lpFileOp->hNameMappings = nFileOp.hNameMappings;
705 lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
709 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
727 BOOL bAnyFromWildcard;
728 BOOL bAnyDirectories;
733 static inline void grow_list(FILE_LIST *list)
735 FILE_ENTRY *new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
736 list->num_alloc * 2 * sizeof(*new) );
738 list->num_alloc *= 2;
741 /* adds a file to the FILE_ENTRY struct
743 static void add_file_to_entry(FILE_ENTRY *feFile, LPWSTR szFile)
745 DWORD dwLen = strlenW(szFile) + 1;
748 feFile->szFullPath = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
749 strcpyW(feFile->szFullPath, szFile);
751 ptr = StrRChrW(szFile, NULL, '\\');
754 dwLen = ptr - szFile + 1;
755 feFile->szDirectory = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
756 lstrcpynW(feFile->szDirectory, szFile, dwLen);
758 dwLen = strlenW(feFile->szFullPath) - dwLen + 1;
759 feFile->szFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
760 strcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
762 feFile->bFromWildcard = FALSE;
765 static LPWSTR wildcard_to_file(LPWSTR szWildCard, LPWSTR szFileName)
767 LPWSTR szFullPath, ptr;
768 DWORD dwDirLen, dwFullLen;
770 ptr = StrRChrW(szWildCard, NULL, '\\');
771 dwDirLen = ptr - szWildCard + 1;
773 dwFullLen = dwDirLen + strlenW(szFileName) + 1;
774 szFullPath = HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
776 lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
777 strcatW(szFullPath, szFileName);
782 static void parse_wildcard_files(FILE_LIST *flList, LPWSTR szFile, LPDWORD pdwListIndex)
784 WIN32_FIND_DATAW wfd;
785 HANDLE hFile = FindFirstFileW(szFile, &wfd);
790 for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
792 if (IsDotDir(wfd.cFileName)) continue;
793 if (*pdwListIndex >= flList->num_alloc) grow_list( flList );
794 szFullPath = wildcard_to_file(szFile, wfd.cFileName);
795 file = &flList->feFiles[(*pdwListIndex)++];
796 add_file_to_entry(file, szFullPath);
797 file->bFromWildcard = TRUE;
798 file->attributes = wfd.dwFileAttributes;
799 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
800 HeapFree(GetProcessHeap(), 0, szFullPath);
806 /* takes the null-separated file list and fills out the FILE_LIST */
807 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
809 LPCWSTR ptr = szFiles;
810 WCHAR szCurFile[MAX_PATH];
811 DWORD i = 0, dwDirLen;
814 return ERROR_INVALID_PARAMETER;
816 flList->bAnyFromWildcard = FALSE;
817 flList->bAnyDirectories = FALSE;
818 flList->bAnyDontExist = FALSE;
819 flList->num_alloc = 32;
820 flList->dwNumFiles = 0;
824 return ERROR_ACCESS_DENIED;
826 flList->feFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
827 flList->num_alloc * sizeof(FILE_ENTRY));
831 if (i >= flList->num_alloc) grow_list( flList );
833 /* change relative to absolute path */
834 if (PathIsRelativeW(ptr))
836 dwDirLen = GetCurrentDirectoryW(MAX_PATH, szCurFile) + 1;
837 PathCombineW(szCurFile, szCurFile, ptr);
838 flList->feFiles[i].bFromRelative = TRUE;
842 strcpyW(szCurFile, ptr);
843 flList->feFiles[i].bFromRelative = FALSE;
846 /* parse wildcard files if they are in the filename */
847 if (StrPBrkW(szCurFile, wWildcardChars))
849 parse_wildcard_files(flList, szCurFile, &i);
850 flList->bAnyFromWildcard = TRUE;
855 FILE_ENTRY *file = &flList->feFiles[i];
856 add_file_to_entry(file, szCurFile);
857 file->attributes = GetFileAttributesW( file->szFullPath );
858 file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
859 if (!file->bExists) flList->bAnyDontExist = TRUE;
860 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
863 /* advance to the next string */
864 ptr += strlenW(ptr) + 1;
867 flList->dwNumFiles = i;
872 /* free the FILE_LIST */
873 static void destroy_file_list(FILE_LIST *flList)
877 if (!flList || !flList->feFiles)
880 for (i = 0; i < flList->dwNumFiles; i++)
882 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
883 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
884 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
887 HeapFree(GetProcessHeap(), 0, flList->feFiles);
890 static void copy_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
892 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
893 SHFILEOPSTRUCTW fileOp;
895 static const WCHAR wildCardFiles[] = {'*','.','*',0};
897 if (IsDotDir(feFrom->szFilename))
900 if (PathFileExistsW(szDestPath))
901 PathCombineW(szTo, szDestPath, feFrom->szFilename);
903 strcpyW(szTo, szDestPath);
905 szTo[strlenW(szTo) + 1] = '\0';
906 SHNotifyCreateDirectoryW(szTo, NULL);
908 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
909 szFrom[strlenW(szFrom) + 1] = '\0';
911 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
912 fileOp.pFrom = szFrom;
914 fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
916 SHFileOperationW(&fileOp);
919 /* copy a file or directory to another directory */
920 static void copy_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
922 WCHAR szDestPath[MAX_PATH];
924 if (!PathFileExistsW(feTo->szFullPath))
925 SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
927 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
929 if (IsAttribFile(feFrom->attributes))
930 SHNotifyCopyFileW(feFrom->szFullPath, szDestPath, FALSE);
931 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
932 copy_dir_to_dir(lpFileOp, feFrom, szDestPath);
935 static void create_dest_dirs(LPWSTR szDestDir)
938 LPWSTR ptr = StrChrW(szDestDir, '\\');
940 /* make sure all directories up to last one are created */
941 while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
943 lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
945 if (!PathFileExistsW(dir))
946 SHNotifyCreateDirectoryW(dir, NULL);
949 /* create last directory */
950 if (!PathFileExistsW(szDestDir))
951 SHNotifyCreateDirectoryW(szDestDir, NULL);
954 /* the FO_COPY operation */
955 static HRESULT copy_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
958 FILE_ENTRY *entryToCopy;
959 FILE_ENTRY *fileDest = &flTo->feFiles[0];
960 BOOL bCancelIfAnyDirectories = FALSE;
962 if (flFrom->bAnyDontExist)
963 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
965 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->bAnyFromWildcard)
966 return ERROR_CANCELLED;
968 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flTo->dwNumFiles != 1)
969 return ERROR_CANCELLED;
971 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->dwNumFiles != 1 &&
972 flFrom->dwNumFiles != flTo->dwNumFiles)
974 return ERROR_CANCELLED;
977 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 &&
978 !PathFileExistsW(flTo->feFiles[0].szFullPath) &&
979 IsAttribFile(fileDest->attributes))
981 bCancelIfAnyDirectories = TRUE;
984 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
985 !PathFileExistsW(fileDest->szFullPath))
987 lpFileOp->fAnyOperationsAborted = TRUE;
988 return ERROR_CANCELLED;
991 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flFrom->dwNumFiles != 1 &&
992 PathFileExistsW(fileDest->szFullPath) &&
993 IsAttribFile(fileDest->attributes))
995 return ERROR_CANCELLED;
998 for (i = 0; i < flFrom->dwNumFiles; i++)
1000 entryToCopy = &flFrom->feFiles[i];
1002 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1003 fileDest = &flTo->feFiles[i];
1005 if (IsAttribDir(entryToCopy->attributes) &&
1006 !lstrcmpW(entryToCopy->szFullPath, fileDest->szDirectory))
1008 return ERROR_SUCCESS;
1011 if (IsAttribDir(entryToCopy->attributes) && bCancelIfAnyDirectories)
1012 return ERROR_CANCELLED;
1014 create_dest_dirs(fileDest->szDirectory);
1016 if (!strcmpW(entryToCopy->szFullPath, fileDest->szFullPath))
1018 if (IsAttribFile(entryToCopy->attributes))
1019 return ERROR_NO_MORE_SEARCH_HANDLES;
1021 return ERROR_SUCCESS;
1024 if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1025 (flFrom->dwNumFiles == 1 && IsAttribDir(fileDest->attributes)))
1027 copy_to_dir(lpFileOp, entryToCopy, fileDest);
1029 else if (IsAttribDir(entryToCopy->attributes))
1031 copy_dir_to_dir(lpFileOp, entryToCopy, fileDest->szFullPath);
1035 if (SHNotifyCopyFileW(entryToCopy->szFullPath, fileDest->szFullPath, TRUE))
1037 lpFileOp->fAnyOperationsAborted = TRUE;
1038 return ERROR_CANCELLED;
1043 return ERROR_SUCCESS;
1046 /* the FO_DELETE operation */
1047 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom)
1049 FILE_ENTRY *fileEntry;
1052 BOOL bConfirm = !(lpFileOp->fFlags & FOF_NOCONFIRMATION);
1054 if (!flFrom->dwNumFiles)
1055 return ERROR_SUCCESS;
1057 for (i = 0; i < flFrom->dwNumFiles; i++)
1060 fileEntry = &flFrom->feFiles[i];
1062 /* delete the file or directory */
1063 if (IsAttribFile(fileEntry->attributes))
1064 bPathExists = DeleteFileW(fileEntry->szFullPath);
1065 else if (!(lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1066 bPathExists = SHELL_DeleteDirectoryW(fileEntry->szFullPath, bConfirm);
1069 return ERROR_PATH_NOT_FOUND;
1072 return ERROR_SUCCESS;
1075 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
1077 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1078 SHFILEOPSTRUCTW fileOp;
1080 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1082 if (IsDotDir(feFrom->szFilename))
1085 SHNotifyCreateDirectoryW(szDestPath, NULL);
1087 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1088 szFrom[strlenW(szFrom) + 1] = '\0';
1090 strcpyW(szTo, szDestPath);
1091 szTo[strlenW(szDestPath) + 1] = '\0';
1093 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
1094 fileOp.pFrom = szFrom;
1097 SHFileOperationW(&fileOp);
1100 /* moves a file or directory to another directory */
1101 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
1103 WCHAR szDestPath[MAX_PATH];
1105 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1107 if (IsAttribFile(feFrom->attributes))
1108 SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
1109 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1110 move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1113 /* the FO_MOVE operation */
1114 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1117 FILE_ENTRY *entryToMove;
1118 FILE_ENTRY *fileDest;
1120 if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1121 return ERROR_CANCELLED;
1123 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1124 flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1126 return ERROR_CANCELLED;
1129 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1130 !flFrom->bAnyDirectories &&
1131 flFrom->dwNumFiles > flTo->dwNumFiles)
1133 return ERROR_CANCELLED;
1136 if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1137 return ERROR_CANCELLED;
1139 if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1140 flFrom->dwNumFiles != flTo->dwNumFiles)
1142 return ERROR_CANCELLED;
1145 fileDest = &flTo->feFiles[0];
1146 for (i = 0; i < flFrom->dwNumFiles; i++)
1148 entryToMove = &flFrom->feFiles[i];
1150 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1151 fileDest = &flTo->feFiles[i];
1153 if (!PathFileExistsW(fileDest->szDirectory))
1154 return ERROR_CANCELLED;
1156 if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1157 move_to_dir(lpFileOp, entryToMove, fileDest);
1159 SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
1162 return ERROR_SUCCESS;
1165 /* the FO_RENAME files */
1166 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1171 if (flFrom->dwNumFiles != 1)
1172 return ERROR_GEN_FAILURE;
1174 if (flTo->dwNumFiles != 1)
1175 return ERROR_CANCELLED;
1177 feFrom = &flFrom->feFiles[0];
1178 feTo= &flTo->feFiles[0];
1180 /* fail if destination doesn't exist */
1181 if (!feFrom->bExists)
1182 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1184 /* fail if destination already exists */
1186 return ERROR_ALREADY_EXISTS;
1188 return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
1191 /* alert the user if an unsupported flag is used */
1192 static void check_flags(FILEOP_FLAGS fFlags)
1194 WORD wUnsupportedFlags = FOF_ALLOWUNDO | FOF_NO_CONNECTED_ELEMENTS |
1195 FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE | FOF_WANTNUKEWARNING |
1196 FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1198 if (fFlags & wUnsupportedFlags)
1199 FIXME("Unsupported flags: %04x\n", fFlags);
1202 /*************************************************************************
1203 * SHFileOperationW [SHELL32.@]
1205 * See SHFileOperationA
1207 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1209 FILE_LIST flFrom, flTo;
1213 return ERROR_INVALID_PARAMETER;
1215 check_flags(lpFileOp->fFlags);
1217 ZeroMemory(&flFrom, sizeof(FILE_LIST));
1218 ZeroMemory(&flTo, sizeof(FILE_LIST));
1220 if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1223 if (lpFileOp->wFunc != FO_DELETE)
1224 parse_file_list(&flTo, lpFileOp->pTo);
1226 switch (lpFileOp->wFunc)
1229 ret = copy_files(lpFileOp, &flFrom, &flTo);
1232 ret = delete_files(lpFileOp, &flFrom);
1235 ret = move_files(lpFileOp, &flFrom, &flTo);
1238 ret = rename_files(lpFileOp, &flFrom, &flTo);
1241 ret = ERROR_INVALID_PARAMETER;
1245 destroy_file_list(&flFrom);
1247 if (lpFileOp->wFunc != FO_DELETE)
1248 destroy_file_list(&flTo);
1250 if (ret == ERROR_CANCELLED)
1251 lpFileOp->fAnyOperationsAborted = TRUE;
1256 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1258 /*************************************************************************
1259 * SHFreeNameMappings [shell32.246]
1261 * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1265 * hNameMapping [I] handle to the name mappings used during renaming of files
1270 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1274 int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1278 LPSHNAMEMAPPINGW lp = DSA_GetItemPtr((HDSA)hNameMapping, i);
1280 SHFree(lp->pszOldPath);
1281 SHFree(lp->pszNewPath);
1283 DSA_Destroy((HDSA)hNameMapping);
1287 /*************************************************************************
1288 * SheGetDirA [SHELL32.@]
1291 HRESULT WINAPI SheGetDirA(LPSTR u, LPSTR v)
1292 { FIXME("%p %p stub\n",u,v);
1296 /*************************************************************************
1297 * SheGetDirW [SHELL32.@]
1300 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1301 { FIXME("%p %p stub\n",u,v);
1305 /*************************************************************************
1306 * SheChangeDirA [SHELL32.@]
1309 HRESULT WINAPI SheChangeDirA(LPSTR u)
1310 { FIXME("(%s),stub\n",debugstr_a(u));
1314 /*************************************************************************
1315 * SheChangeDirW [SHELL32.@]
1318 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1319 { FIXME("(%s),stub\n",debugstr_w(u));
1323 /*************************************************************************
1324 * IsNetDrive [SHELL32.66]
1326 BOOL WINAPI IsNetDrive(DWORD drive)
1329 strcpy(root, "A:\\");
1330 root[0] += (char)drive;
1331 return (GetDriveTypeA(root) == DRIVE_REMOTE);
1335 /*************************************************************************
1336 * RealDriveType [SHELL32.524]
1338 INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1340 char root[] = "A:\\";
1341 root[0] += (char)drive;
1342 return GetDriveTypeA(root);