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_ConfirmDialog(int nKindOfDialog, LPCSTR szDir)
100 CHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
101 SHELL_ConfirmIDstruc ids;
103 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
106 LoadStringA(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption));
107 LoadStringA(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText));
109 FormatMessageA(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
110 szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
112 return (IDOK == MessageBoxA(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
115 BOOL SHELL_ConfirmDialogW(int nKindOfDialog, LPCWSTR szDir)
117 WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
118 SHELL_ConfirmIDstruc ids;
120 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
123 LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption));
124 LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText));
126 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
127 szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
129 return (IDOK == MessageBoxW(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
132 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
134 DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
139 *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
142 MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
145 return E_OUTOFMEMORY;
148 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
150 HeapFree(GetProcessHeap(), 0, wPath);
153 /**************************************************************************
154 * SHELL_DeleteDirectory() [internal]
156 * Asks for confirmation when bShowUI is true and deletes the directory and
157 * all its subdirectories and files if necessary.
159 BOOL SHELL_DeleteDirectoryW(LPCWSTR pszDir, BOOL bShowUI)
163 WIN32_FIND_DATAW wfd;
164 WCHAR szTemp[MAX_PATH];
166 /* Make sure the directory exists before eventually prompting the user */
167 PathCombineW(szTemp, pszDir, wWildcardFile);
168 hFind = FindFirstFileW(szTemp, &wfd);
169 if (hFind == INVALID_HANDLE_VALUE)
172 if (!bShowUI || (ret = SHELL_ConfirmDialogW(ASK_DELETE_FOLDER, pszDir)))
176 LPWSTR lp = wfd.cAlternateFileName;
181 PathCombineW(szTemp, pszDir, lp);
182 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
183 ret = SHELL_DeleteDirectoryW(szTemp, FALSE);
185 ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
186 } while (ret && FindNextFileW(hFind, &wfd));
190 ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
194 /**************************************************************************
195 * SHELL_DeleteFileW() [internal]
197 BOOL SHELL_DeleteFileW(LPCWSTR pszFile, BOOL bShowUI)
199 if (bShowUI && !SHELL_ConfirmDialogW(ASK_DELETE_FILE, pszFile))
202 return (SHNotifyDeleteFileW(pszFile) == ERROR_SUCCESS);
205 /**************************************************************************
206 * Win32CreateDirectory [SHELL32.93]
208 * Creates a directory. Also triggers a change notify if one exists.
211 * path [I] path to directory to create
214 * TRUE if successful, FALSE otherwise
217 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
218 * This is Unicode on NT/2000
220 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
225 TRACE("(%s, %p)\n", debugstr_a(path), sec);
227 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
230 retCode = SHNotifyCreateDirectoryW(wPath, sec);
231 SHELL32_FreeUnicodeBuf(wPath);
236 /**********************************************************************/
238 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
240 TRACE("(%s, %p)\n", debugstr_w(path), sec);
242 if (CreateDirectoryW(path, sec))
244 SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
245 return ERROR_SUCCESS;
247 return GetLastError();
250 /**********************************************************************/
252 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
254 if (SHELL_OsIsUnicode())
255 return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
256 return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
259 /************************************************************************
260 * Win32RemoveDirectory [SHELL32.94]
262 * Deletes a directory. Also triggers a change notify if one exists.
265 * path [I] path to directory to delete
268 * TRUE if successful, FALSE otherwise
271 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
272 * This is Unicode on NT/2000
274 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
279 TRACE("(%s)\n", debugstr_a(path));
281 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
284 retCode = SHNotifyRemoveDirectoryW(wPath);
285 SHELL32_FreeUnicodeBuf(wPath);
290 /***********************************************************************/
292 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
295 TRACE("(%s)\n", debugstr_w(path));
297 ret = RemoveDirectoryW(path);
300 /* Directory may be write protected */
301 DWORD dwAttr = GetFileAttributesW(path);
302 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
303 if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
304 ret = RemoveDirectoryW(path);
308 SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
309 return ERROR_SUCCESS;
311 return GetLastError();
314 /***********************************************************************/
316 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
318 if (SHELL_OsIsUnicode())
319 return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
320 return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
323 /************************************************************************
324 * Win32DeleteFile [SHELL32.164]
326 * Deletes a file. Also triggers a change notify if one exists.
329 * path [I] path to file to delete
332 * TRUE if successful, FALSE otherwise
335 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
336 * This is Unicode on NT/2000
338 static DWORD SHNotifyDeleteFileA(LPCSTR path)
343 TRACE("(%s)\n", debugstr_a(path));
345 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
348 retCode = SHNotifyDeleteFileW(wPath);
349 SHELL32_FreeUnicodeBuf(wPath);
354 /***********************************************************************/
356 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
360 TRACE("(%s)\n", debugstr_w(path));
362 ret = DeleteFileW(path);
365 /* File may be write protected or a system file */
366 DWORD dwAttr = GetFileAttributesW(path);
367 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
368 if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
369 ret = DeleteFileW(path);
373 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
374 return ERROR_SUCCESS;
376 return GetLastError();
379 /***********************************************************************/
381 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
383 if (SHELL_OsIsUnicode())
384 return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
385 return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
388 /************************************************************************
389 * SHNotifyMoveFile [internal]
391 * Moves a file. Also triggers a change notify if one exists.
394 * src [I] path to source file to move
395 * dest [I] path to target file to move to
398 * ERORR_SUCCESS if successful
400 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
404 TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
406 ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
408 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
410 ret = MoveFileW(src, dest);
416 dwAttr = SHFindAttrW(dest, FALSE);
417 if (INVALID_FILE_ATTRIBUTES == dwAttr)
419 /* Source file may be write protected or a system file */
420 dwAttr = GetFileAttributesW(src);
421 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
422 if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
423 ret = MoveFileW(src, dest);
428 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
429 return ERROR_SUCCESS;
431 return GetLastError();
434 /************************************************************************
435 * SHNotifyCopyFile [internal]
437 * Copies a file. Also triggers a change notify if one exists.
440 * src [I] path to source file to move
441 * dest [I] path to target file to move to
442 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
443 * a file with this name already exists
446 * ERROR_SUCCESS if successful
448 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
452 TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
454 ret = CopyFileW(src, dest, bFailIfExists);
457 SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
458 return ERROR_SUCCESS;
461 return GetLastError();
464 /*************************************************************************
465 * SHCreateDirectory [SHELL32.165]
467 * This function creates a file system folder whose fully qualified path is
468 * given by path. If one or more of the intermediate folders do not exist,
469 * they will be created as well.
473 * path [I] path of directory to create
476 * ERRROR_SUCCESS or one of the following values:
477 * ERROR_BAD_PATHNAME if the path is relative
478 * ERROR_FILE_EXISTS when a file with that name exists
479 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
480 * ERROR_INVLID_NAME if the path contains invalid chars
481 * ERROR_ALREADY_EXISTS when the directory already exists
482 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
485 * exported by ordinal
487 * WinNT/2000 exports Unicode
489 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
491 if (SHELL_OsIsUnicode())
492 return SHCreateDirectoryExW(hWnd, path, NULL);
493 return SHCreateDirectoryExA(hWnd, path, NULL);
496 /*************************************************************************
497 * SHCreateDirectoryExA [SHELL32.@]
499 * This function creates a file system folder whose fully qualified path is
500 * given by path. If one or more of the intermediate folders do not exist,
501 * they will be created as well.
505 * path [I] path of directory to create
506 * sec [I] security attributes to use or NULL
509 * ERRROR_SUCCESS or one of the following values:
510 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
511 * ERROR_INVLID_NAME if the path contains invalid chars
512 * ERROR_FILE_EXISTS when a file with that name exists
513 * ERROR_ALREADY_EXISTS when the directory already exists
514 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
516 * FIXME: Not implemented yet;
517 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
518 * if the path is a network path to deal with network drivers which might have a limited
519 * but unknown maximum path length. If not:
521 * If hWnd is set to a valid window handle, a message box is displayed warning
522 * the user that the files may not be accessible. If the user chooses not to
523 * proceed, the function returns ERROR_CANCELLED.
525 * If hWnd is set to NULL, no user interface is displayed and the function
526 * returns ERROR_CANCELLED.
528 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
533 TRACE("(%s, %p)\n", debugstr_a(path), sec);
535 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
538 retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
539 SHELL32_FreeUnicodeBuf(wPath);
544 /*************************************************************************
545 * SHCreateDirectoryExW [SHELL32.@]
547 * See SHCreateDirectoryExA.
549 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
551 int ret = ERROR_BAD_PATHNAME;
552 TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
554 if (PathIsRelativeW(path))
560 ret = SHNotifyCreateDirectoryW(path, sec);
561 /* Refuse to work on certain error codes before trying to create directories recursively */
562 if (ret != ERROR_SUCCESS &&
563 ret != ERROR_FILE_EXISTS &&
564 ret != ERROR_ALREADY_EXISTS &&
565 ret != ERROR_FILENAME_EXCED_RANGE)
567 WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1]; /* extra for PathAddBackslash() */
569 lstrcpynW(szTemp, path, MAX_PATH);
570 pEnd = PathAddBackslashW(szTemp);
575 while (*pSlash && *pSlash != '\\')
576 pSlash = CharNextW(pSlash);
580 *pSlash = 0; /* terminate path at separator */
582 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
584 *pSlash++ = '\\'; /* put the separator back */
588 if (ret && hWnd && (ERROR_CANCELLED != ret))
590 /* We failed and should show a dialog box */
591 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
592 ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
598 /*************************************************************************
599 * SHFindAttrW [internal]
601 * Get the Attributes for a file or directory. The difference to GetAttributes()
602 * is that this function will also work for paths containing wildcard characters
606 * path [I] path of directory or file to check
607 * fileOnly [I] TRUE if only files should be found
610 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
611 * the first file or directory found otherwise
613 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
615 WIN32_FIND_DATAW wfd;
616 BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
617 DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
618 HANDLE hFind = FindFirstFileW(pName, &wfd);
620 TRACE("%s %d\n", debugstr_w(pName), fileOnly);
621 if (INVALID_HANDLE_VALUE != hFind)
625 if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
627 dwAttr = wfd.dwFileAttributes;
630 while (FindNextFileW(hFind, &wfd));
636 /*************************************************************************
638 * SHNameTranslate HelperFunction for SHFileOperationA
640 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
641 * is NULL, only the necessary size of the string is determined and returned,
642 * otherwise the ASCII strings are copied into it and the buffer is increased
643 * to point to the location after the final 0 termination char.
645 DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
647 DWORD size = 0, aSize = 0;
648 LPCSTR aString = (LPCSTR)*pWToFrom;
654 size = lstrlenA(aString) + 1;
657 } while ((size != 1) && more);
658 /* The two sizes might be different in the case of multibyte chars */
659 size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
660 if (*wString) /* only in the second loop */
662 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
663 *pWToFrom = *wString;
669 /*************************************************************************
670 * SHFileOperationA [SHELL32.@]
672 * Function to copy, move, delete and create one or more files with optional
676 * lpFileOp [I/O] pointer to a structure containing all the necessary information
679 * Success: ERROR_SUCCESS.
680 * Failure: ERROR_CANCELLED.
685 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
687 SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
690 LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
691 wString = NULL; /* we change this in SHNameTranslate */
694 if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
695 nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
696 if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
697 nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
698 while (1) /* every loop calculate size, second translate also, if we have storage for this */
700 size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
701 size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
702 size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
706 retCode = SHFileOperationW(&nFileOp);
707 HeapFree(GetProcessHeap(), 0, ForFree); /* we cannot use wString, it was changed */
712 wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
713 if (ForFree) continue;
714 retCode = ERROR_OUTOFMEMORY;
715 nFileOp.fAnyOperationsAborted = TRUE;
716 SetLastError(retCode);
721 lpFileOp->hNameMappings = nFileOp.hNameMappings;
722 lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
726 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
744 BOOL bAnyFromWildcard;
745 BOOL bAnyDirectories;
750 static inline void grow_list(FILE_LIST *list)
752 FILE_ENTRY *new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
753 list->num_alloc * 2 * sizeof(*new) );
755 list->num_alloc *= 2;
758 /* adds a file to the FILE_ENTRY struct
760 static void add_file_to_entry(FILE_ENTRY *feFile, LPWSTR szFile)
762 DWORD dwLen = strlenW(szFile) + 1;
765 feFile->szFullPath = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
766 strcpyW(feFile->szFullPath, szFile);
768 ptr = StrRChrW(szFile, NULL, '\\');
771 dwLen = ptr - szFile + 1;
772 feFile->szDirectory = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
773 lstrcpynW(feFile->szDirectory, szFile, dwLen);
775 dwLen = strlenW(feFile->szFullPath) - dwLen + 1;
776 feFile->szFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
777 strcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
779 feFile->bFromWildcard = FALSE;
782 static LPWSTR wildcard_to_file(LPWSTR szWildCard, LPWSTR szFileName)
784 LPWSTR szFullPath, ptr;
785 DWORD dwDirLen, dwFullLen;
787 ptr = StrRChrW(szWildCard, NULL, '\\');
788 dwDirLen = ptr - szWildCard + 1;
790 dwFullLen = dwDirLen + strlenW(szFileName) + 1;
791 szFullPath = HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
793 lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
794 strcatW(szFullPath, szFileName);
799 static void parse_wildcard_files(FILE_LIST *flList, LPWSTR szFile, LPDWORD pdwListIndex)
801 WIN32_FIND_DATAW wfd;
802 HANDLE hFile = FindFirstFileW(szFile, &wfd);
807 for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
809 if (IsDotDir(wfd.cFileName)) continue;
810 if (*pdwListIndex >= flList->num_alloc) grow_list( flList );
811 szFullPath = wildcard_to_file(szFile, wfd.cFileName);
812 file = &flList->feFiles[(*pdwListIndex)++];
813 add_file_to_entry(file, szFullPath);
814 file->bFromWildcard = TRUE;
815 file->attributes = wfd.dwFileAttributes;
816 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
817 HeapFree(GetProcessHeap(), 0, szFullPath);
823 /* takes the null-separated file list and fills out the FILE_LIST */
824 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
826 LPCWSTR ptr = szFiles;
827 WCHAR szCurFile[MAX_PATH];
828 DWORD i = 0, dwDirLen;
831 return ERROR_INVALID_PARAMETER;
833 flList->bAnyFromWildcard = FALSE;
834 flList->bAnyDirectories = FALSE;
835 flList->bAnyDontExist = FALSE;
836 flList->num_alloc = 32;
837 flList->dwNumFiles = 0;
841 return ERROR_ACCESS_DENIED;
843 flList->feFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
844 flList->num_alloc * sizeof(FILE_ENTRY));
848 if (i >= flList->num_alloc) grow_list( flList );
850 /* change relative to absolute path */
851 if (PathIsRelativeW(ptr))
853 dwDirLen = GetCurrentDirectoryW(MAX_PATH, szCurFile) + 1;
854 PathCombineW(szCurFile, szCurFile, ptr);
855 flList->feFiles[i].bFromRelative = TRUE;
859 strcpyW(szCurFile, ptr);
860 flList->feFiles[i].bFromRelative = FALSE;
863 /* parse wildcard files if they are in the filename */
864 if (StrPBrkW(szCurFile, wWildcardChars))
866 parse_wildcard_files(flList, szCurFile, &i);
867 flList->bAnyFromWildcard = TRUE;
872 FILE_ENTRY *file = &flList->feFiles[i];
873 add_file_to_entry(file, szCurFile);
874 file->attributes = GetFileAttributesW( file->szFullPath );
875 file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
876 if (!file->bExists) flList->bAnyDontExist = TRUE;
877 if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
880 /* advance to the next string */
881 ptr += strlenW(ptr) + 1;
884 flList->dwNumFiles = i;
889 /* free the FILE_LIST */
890 static void destroy_file_list(FILE_LIST *flList)
894 if (!flList || !flList->feFiles)
897 for (i = 0; i < flList->dwNumFiles; i++)
899 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
900 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
901 HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
904 HeapFree(GetProcessHeap(), 0, flList->feFiles);
907 static void copy_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
909 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
910 SHFILEOPSTRUCTW fileOp;
912 static const WCHAR wildCardFiles[] = {'*','.','*',0};
914 if (IsDotDir(feFrom->szFilename))
917 if (PathFileExistsW(szDestPath))
918 PathCombineW(szTo, szDestPath, feFrom->szFilename);
920 strcpyW(szTo, szDestPath);
922 szTo[strlenW(szTo) + 1] = '\0';
923 SHNotifyCreateDirectoryW(szTo, NULL);
925 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
926 szFrom[strlenW(szFrom) + 1] = '\0';
928 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
929 fileOp.pFrom = szFrom;
931 fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
933 SHFileOperationW(&fileOp);
936 /* copy a file or directory to another directory */
937 static void copy_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
939 WCHAR szDestPath[MAX_PATH];
941 if (!PathFileExistsW(feTo->szFullPath))
942 SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
944 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
946 if (IsAttribFile(feFrom->attributes))
947 SHNotifyCopyFileW(feFrom->szFullPath, szDestPath, FALSE);
948 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
949 copy_dir_to_dir(lpFileOp, feFrom, szDestPath);
952 static void create_dest_dirs(LPWSTR szDestDir)
955 LPWSTR ptr = StrChrW(szDestDir, '\\');
957 /* make sure all directories up to last one are created */
958 while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
960 lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
962 if (!PathFileExistsW(dir))
963 SHNotifyCreateDirectoryW(dir, NULL);
966 /* create last directory */
967 if (!PathFileExistsW(szDestDir))
968 SHNotifyCreateDirectoryW(szDestDir, NULL);
971 /* the FO_COPY operation */
972 static HRESULT copy_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
975 FILE_ENTRY *entryToCopy;
976 FILE_ENTRY *fileDest = &flTo->feFiles[0];
977 BOOL bCancelIfAnyDirectories = FALSE;
979 if (flFrom->bAnyDontExist)
980 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
982 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->bAnyFromWildcard)
983 return ERROR_CANCELLED;
985 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flTo->dwNumFiles != 1)
986 return ERROR_CANCELLED;
988 if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->dwNumFiles != 1 &&
989 flFrom->dwNumFiles != flTo->dwNumFiles)
991 return ERROR_CANCELLED;
994 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 &&
995 !PathFileExistsW(flTo->feFiles[0].szFullPath) &&
996 IsAttribFile(fileDest->attributes))
998 bCancelIfAnyDirectories = TRUE;
1001 if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
1002 !PathFileExistsW(fileDest->szFullPath))
1004 lpFileOp->fAnyOperationsAborted = TRUE;
1005 return ERROR_CANCELLED;
1008 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flFrom->dwNumFiles != 1 &&
1009 PathFileExistsW(fileDest->szFullPath) &&
1010 IsAttribFile(fileDest->attributes))
1012 return ERROR_CANCELLED;
1015 for (i = 0; i < flFrom->dwNumFiles; i++)
1017 entryToCopy = &flFrom->feFiles[i];
1019 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1020 fileDest = &flTo->feFiles[i];
1022 if (IsAttribDir(entryToCopy->attributes) &&
1023 !lstrcmpW(entryToCopy->szFullPath, fileDest->szDirectory))
1025 return ERROR_SUCCESS;
1028 if (IsAttribDir(entryToCopy->attributes) && bCancelIfAnyDirectories)
1029 return ERROR_CANCELLED;
1031 create_dest_dirs(fileDest->szDirectory);
1033 if (!strcmpW(entryToCopy->szFullPath, fileDest->szFullPath))
1035 if (IsAttribFile(entryToCopy->attributes))
1036 return ERROR_NO_MORE_SEARCH_HANDLES;
1038 return ERROR_SUCCESS;
1041 if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1042 (flFrom->dwNumFiles == 1 && IsAttribDir(fileDest->attributes)))
1044 copy_to_dir(lpFileOp, entryToCopy, fileDest);
1046 else if (IsAttribDir(entryToCopy->attributes))
1048 copy_dir_to_dir(lpFileOp, entryToCopy, fileDest->szFullPath);
1052 if (SHNotifyCopyFileW(entryToCopy->szFullPath, fileDest->szFullPath, TRUE))
1054 lpFileOp->fAnyOperationsAborted = TRUE;
1055 return ERROR_CANCELLED;
1060 return ERROR_SUCCESS;
1063 /* the FO_DELETE operation */
1064 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom)
1066 FILE_ENTRY *fileEntry;
1069 BOOL bConfirm = !(lpFileOp->fFlags & FOF_NOCONFIRMATION);
1071 if (!flFrom->dwNumFiles)
1072 return ERROR_SUCCESS;
1074 for (i = 0; i < flFrom->dwNumFiles; i++)
1077 fileEntry = &flFrom->feFiles[i];
1079 /* delete the file or directory */
1080 if (IsAttribFile(fileEntry->attributes))
1081 bPathExists = DeleteFileW(fileEntry->szFullPath);
1082 else if (!(lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1083 bPathExists = SHELL_DeleteDirectoryW(fileEntry->szFullPath, bConfirm);
1086 return ERROR_PATH_NOT_FOUND;
1089 return ERROR_SUCCESS;
1092 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
1094 WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1095 SHFILEOPSTRUCTW fileOp;
1097 static const WCHAR wildCardFiles[] = {'*','.','*',0};
1099 if (IsDotDir(feFrom->szFilename))
1102 SHNotifyCreateDirectoryW(szDestPath, NULL);
1104 PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1105 szFrom[strlenW(szFrom) + 1] = '\0';
1107 strcpyW(szTo, szDestPath);
1108 szTo[strlenW(szDestPath) + 1] = '\0';
1110 memcpy(&fileOp, lpFileOp, sizeof(fileOp));
1111 fileOp.pFrom = szFrom;
1114 SHFileOperationW(&fileOp);
1117 /* moves a file or directory to another directory */
1118 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
1120 WCHAR szDestPath[MAX_PATH];
1122 PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1124 if (IsAttribFile(feFrom->attributes))
1125 SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
1126 else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1127 move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1130 /* the FO_MOVE operation */
1131 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1134 FILE_ENTRY *entryToMove;
1135 FILE_ENTRY *fileDest;
1137 if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1138 return ERROR_CANCELLED;
1140 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1141 flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1143 return ERROR_CANCELLED;
1146 if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1147 !flFrom->bAnyDirectories &&
1148 flFrom->dwNumFiles > flTo->dwNumFiles)
1150 return ERROR_CANCELLED;
1153 if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1154 return ERROR_CANCELLED;
1156 if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1157 flFrom->dwNumFiles != flTo->dwNumFiles)
1159 return ERROR_CANCELLED;
1162 fileDest = &flTo->feFiles[0];
1163 for (i = 0; i < flFrom->dwNumFiles; i++)
1165 entryToMove = &flFrom->feFiles[i];
1167 if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1168 fileDest = &flTo->feFiles[i];
1170 if (!PathFileExistsW(fileDest->szDirectory))
1171 return ERROR_CANCELLED;
1173 if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1174 move_to_dir(lpFileOp, entryToMove, fileDest);
1176 SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
1179 return ERROR_SUCCESS;
1182 /* the FO_RENAME files */
1183 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1188 if (flFrom->dwNumFiles != 1)
1189 return ERROR_GEN_FAILURE;
1191 if (flTo->dwNumFiles != 1)
1192 return ERROR_CANCELLED;
1194 feFrom = &flFrom->feFiles[0];
1195 feTo= &flTo->feFiles[0];
1197 /* fail if destination doesn't exist */
1198 if (!feFrom->bExists)
1199 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1201 /* fail if destination already exists */
1203 return ERROR_ALREADY_EXISTS;
1205 return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
1208 /* alert the user if an unsupported flag is used */
1209 static void check_flags(FILEOP_FLAGS fFlags)
1211 WORD wUnsupportedFlags = FOF_ALLOWUNDO | FOF_NO_CONNECTED_ELEMENTS |
1212 FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE | FOF_WANTNUKEWARNING |
1213 FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1215 if (fFlags & wUnsupportedFlags)
1216 FIXME("Unsupported flags: %04x\n", fFlags);
1219 /*************************************************************************
1220 * SHFileOperationW [SHELL32.@]
1222 * See SHFileOperationA
1224 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1226 FILE_LIST flFrom, flTo;
1230 return ERROR_INVALID_PARAMETER;
1232 check_flags(lpFileOp->fFlags);
1234 ZeroMemory(&flFrom, sizeof(FILE_LIST));
1235 ZeroMemory(&flTo, sizeof(FILE_LIST));
1237 if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1240 if (lpFileOp->wFunc != FO_DELETE)
1241 parse_file_list(&flTo, lpFileOp->pTo);
1243 switch (lpFileOp->wFunc)
1246 ret = copy_files(lpFileOp, &flFrom, &flTo);
1249 ret = delete_files(lpFileOp, &flFrom);
1252 ret = move_files(lpFileOp, &flFrom, &flTo);
1255 ret = rename_files(lpFileOp, &flFrom, &flTo);
1258 ret = ERROR_INVALID_PARAMETER;
1262 destroy_file_list(&flFrom);
1264 if (lpFileOp->wFunc != FO_DELETE)
1265 destroy_file_list(&flTo);
1267 if (ret == ERROR_CANCELLED)
1268 lpFileOp->fAnyOperationsAborted = TRUE;
1273 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1275 /*************************************************************************
1276 * SHFreeNameMappings [shell32.246]
1278 * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1282 * hNameMapping [I] handle to the name mappings used during renaming of files
1287 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1291 int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1295 LPSHNAMEMAPPINGW lp = DSA_GetItemPtr((HDSA)hNameMapping, i);
1297 SHFree(lp->pszOldPath);
1298 SHFree(lp->pszNewPath);
1300 DSA_Destroy((HDSA)hNameMapping);
1304 /*************************************************************************
1305 * SheGetDirA [SHELL32.@]
1308 HRESULT WINAPI SheGetDirA(LPSTR u, LPSTR v)
1309 { FIXME("%p %p stub\n",u,v);
1313 /*************************************************************************
1314 * SheGetDirW [SHELL32.@]
1317 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1318 { FIXME("%p %p stub\n",u,v);
1322 /*************************************************************************
1323 * SheChangeDirA [SHELL32.@]
1326 HRESULT WINAPI SheChangeDirA(LPSTR u)
1327 { FIXME("(%s),stub\n",debugstr_a(u));
1331 /*************************************************************************
1332 * SheChangeDirW [SHELL32.@]
1335 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1336 { FIXME("(%s),stub\n",debugstr_w(u));
1340 /*************************************************************************
1341 * IsNetDrive [SHELL32.66]
1343 BOOL WINAPI IsNetDrive(DWORD drive)
1346 strcpy(root, "A:\\");
1347 root[0] += (char)drive;
1348 return (GetDriveTypeA(root) == DRIVE_REMOTE);
1352 /*************************************************************************
1353 * RealDriveType [SHELL32.524]
1355 INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1357 char root[] = "A:\\";
1358 root[0] += (char)drive;
1359 return GetDriveTypeA(root);