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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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};
57 static const WCHAR wBackslash[] = {'\\',0};
59 static BOOL SHELL_DeleteDirectoryW(LPCWSTR path, BOOL bShowUI);
60 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec);
61 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec);
62 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path);
63 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path);
64 static DWORD SHNotifyDeleteFileA(LPCSTR path);
65 static DWORD SHNotifyDeleteFileW(LPCWSTR path);
66 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest);
67 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists);
68 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly);
72 UINT caption_resource_id, text_resource_id;
73 } SHELL_ConfirmIDstruc;
75 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
77 switch (nKindOfDialog) {
79 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
80 ids->text_resource_id = IDS_DELETEITEM_TEXT;
82 case ASK_DELETE_FOLDER:
83 ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
84 ids->text_resource_id = IDS_DELETEITEM_TEXT;
86 case ASK_DELETE_MULTIPLE_ITEM:
87 ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
88 ids->text_resource_id = IDS_DELETEMULTIPLE_TEXT;
90 case ASK_OVERWRITE_FILE:
91 ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
92 ids->text_resource_id = IDS_OVERWRITEFILE_TEXT;
95 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
100 BOOL SHELL_ConfirmDialog(int nKindOfDialog, LPCSTR szDir)
102 CHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
103 SHELL_ConfirmIDstruc ids;
105 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
108 LoadStringA(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption));
109 LoadStringA(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText));
111 FormatMessageA(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
112 szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
114 return (IDOK == MessageBoxA(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
117 BOOL SHELL_ConfirmDialogW(int nKindOfDialog, LPCWSTR szDir)
119 WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
120 SHELL_ConfirmIDstruc ids;
122 if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
125 LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption));
126 LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText));
128 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
129 szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
131 return (IDOK == MessageBoxW(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
134 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
136 DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
141 *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
144 MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
147 return E_OUTOFMEMORY;
150 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
152 HeapFree(GetProcessHeap(), 0, wPath);
155 /**************************************************************************
156 * SHELL_DeleteDirectory() [internal]
158 * Asks for confirmation when bShowUI is true and deletes the directory and
159 * all its subdirectories and files if necessary.
161 BOOL SHELL_DeleteDirectoryA(LPCSTR pszDir, BOOL bShowUI)
166 if (!SHELL32_AnsiToUnicodeBuf(pszDir, &wPath, 0))
168 ret = SHELL_DeleteDirectoryW(wPath, bShowUI);
169 SHELL32_FreeUnicodeBuf(wPath);
174 static BOOL SHELL_DeleteDirectoryW(LPCWSTR pszDir, BOOL bShowUI)
178 WIN32_FIND_DATAW wfd;
179 WCHAR szTemp[MAX_PATH];
181 /* Make sure the directory exists before eventually prompting the user */
182 PathCombineW(szTemp, pszDir, wWildcardFile);
183 hFind = FindFirstFileW(szTemp, &wfd);
184 if (hFind == INVALID_HANDLE_VALUE)
187 if (!bShowUI || (ret = SHELL_ConfirmDialogW(ASK_DELETE_FOLDER, pszDir)))
191 LPWSTR lp = wfd.cAlternateFileName;
196 PathCombineW(szTemp, pszDir, lp);
197 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
198 ret = SHELL_DeleteDirectoryW(szTemp, FALSE);
200 ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
201 } while (ret && FindNextFileW(hFind, &wfd));
205 ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
209 /**************************************************************************
210 * SHELL_DeleteFileA() [internal]
212 BOOL SHELL_DeleteFileA(LPCSTR pszFile, BOOL bShowUI)
214 if (bShowUI && !SHELL_ConfirmDialog(ASK_DELETE_FILE, pszFile))
217 return (SHNotifyDeleteFileA(pszFile) == ERROR_SUCCESS);
220 BOOL SHELL_DeleteFileW(LPCWSTR pszFile, BOOL bShowUI)
222 if (bShowUI && !SHELL_ConfirmDialogW(ASK_DELETE_FILE, pszFile))
225 return (SHNotifyDeleteFileW(pszFile) == ERROR_SUCCESS);
228 /**************************************************************************
229 * Win32CreateDirectory [SHELL32.93]
231 * Creates a directory. Also triggers a change notify if one exists.
234 * path [I] path to directory to create
237 * TRUE if successful, FALSE otherwise
240 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
241 * This is Unicode on NT/2000
243 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
248 TRACE("(%s, %p)\n", debugstr_a(path), sec);
250 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
253 retCode = SHNotifyCreateDirectoryW(wPath, sec);
254 SHELL32_FreeUnicodeBuf(wPath);
259 /**********************************************************************/
261 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
263 TRACE("(%s, %p)\n", debugstr_w(path), sec);
265 if (CreateDirectoryW(path, sec))
267 SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
268 return ERROR_SUCCESS;
270 return GetLastError();
273 /**********************************************************************/
275 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
277 if (SHELL_OsIsUnicode())
278 return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
279 return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
282 /************************************************************************
283 * Win32RemoveDirectory [SHELL32.94]
285 * Deletes a directory. Also triggers a change notify if one exists.
288 * path [I] path to directory to delete
291 * TRUE if successful, FALSE otherwise
294 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
295 * This is Unicode on NT/2000
297 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
302 TRACE("(%s)\n", debugstr_a(path));
304 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
307 retCode = SHNotifyRemoveDirectoryW(wPath);
308 SHELL32_FreeUnicodeBuf(wPath);
313 /***********************************************************************/
315 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
318 TRACE("(%s)\n", debugstr_w(path));
320 ret = RemoveDirectoryW(path);
323 /* Directory may be write protected */
324 DWORD dwAttr = GetFileAttributesW(path);
325 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
326 if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
327 ret = RemoveDirectoryW(path);
331 SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
332 return ERROR_SUCCESS;
334 return GetLastError();
337 /***********************************************************************/
339 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
341 if (SHELL_OsIsUnicode())
342 return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
343 return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
346 /************************************************************************
347 * Win32DeleteFile [SHELL32.164]
349 * Deletes a file. Also triggers a change notify if one exists.
352 * path [I] path to file to delete
355 * TRUE if successful, FALSE otherwise
358 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
359 * This is Unicode on NT/2000
361 static DWORD SHNotifyDeleteFileA(LPCSTR path)
366 TRACE("(%s)\n", debugstr_a(path));
368 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
371 retCode = SHNotifyDeleteFileW(wPath);
372 SHELL32_FreeUnicodeBuf(wPath);
377 /***********************************************************************/
379 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
383 TRACE("(%s)\n", debugstr_w(path));
385 ret = DeleteFileW(path);
388 /* File may be write protected or a system file */
389 DWORD dwAttr = GetFileAttributesW(path);
390 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
391 if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
392 ret = DeleteFileW(path);
396 SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
397 return ERROR_SUCCESS;
399 return GetLastError();
402 /***********************************************************************/
404 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
406 if (SHELL_OsIsUnicode())
407 return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
408 return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
411 /************************************************************************
412 * SHNotifyMoveFile [internal]
414 * Moves a file. Also triggers a change notify if one exists.
417 * src [I] path to source file to move
418 * dest [I] path to target file to move to
421 * ERORR_SUCCESS if successful
423 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
427 TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
429 ret = MoveFileW(src, dest);
434 dwAttr = SHFindAttrW(dest, FALSE);
435 if (INVALID_FILE_ATTRIBUTES == dwAttr)
437 /* Source file may be write protected or a system file */
438 dwAttr = GetFileAttributesW(src);
439 if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
440 if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
441 ret = MoveFileW(src, dest);
446 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
447 return ERROR_SUCCESS;
449 return GetLastError();
452 /************************************************************************
453 * SHNotifyCopyFile [internal]
455 * Copies a file. Also triggers a change notify if one exists.
458 * src [I] path to source file to move
459 * dest [I] path to target file to move to
460 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
461 * a file with this name already exists
464 * ERROR_SUCCESS if successful
466 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
470 TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
472 ret = CopyFileW(src, dest, bFailIfExists);
475 SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
476 return ERROR_SUCCESS;
478 return GetLastError();
481 /*************************************************************************
482 * SHCreateDirectory [SHELL32.165]
484 * This function creates a file system folder whose fully qualified path is
485 * given by path. If one or more of the intermediate folders do not exist,
486 * they will be created as well.
490 * path [I] path of directory to create
493 * ERRROR_SUCCESS or one of the following values:
494 * ERROR_BAD_PATHNAME if the path is relative
495 * ERROR_FILE_EXISTS when a file with that name exists
496 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
497 * ERROR_INVLID_NAME if the path contains invalid chars
498 * ERROR_ALREADY_EXISTS when the directory already exists
499 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
502 * exported by ordinal
504 * WinNT/2000 exports Unicode
506 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
508 if (SHELL_OsIsUnicode())
509 return SHCreateDirectoryExW(hWnd, path, NULL);
510 return SHCreateDirectoryExA(hWnd, path, NULL);
513 /*************************************************************************
514 * SHCreateDirectoryExA [SHELL32.@]
516 * This function creates a file system folder whose fully qualified path is
517 * given by path. If one or more of the intermediate folders do not exist,
518 * they will be created as well.
522 * path [I] path of directory to create
523 * sec [I] security attributes to use or NULL
526 * ERRROR_SUCCESS or one of the following values:
527 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
528 * ERROR_INVLID_NAME if the path contains invalid chars
529 * ERROR_FILE_EXISTS when a file with that name exists
530 * ERROR_ALREADY_EXISTS when the directory already exists
531 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
533 * FIXME: Not implemented yet;
534 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
535 * if the path is a network path to deal with network drivers which might have a limited
536 * but unknown maximum path length. If not:
538 * If hWnd is set to a valid window handle, a message box is displayed warning
539 * the user that the files may not be accessible. If the user chooses not to
540 * proceed, the function returns ERROR_CANCELLED.
542 * If hWnd is set to NULL, no user interface is displayed and the function
543 * returns ERROR_CANCELLED.
545 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
550 TRACE("(%s, %p)\n", debugstr_a(path), sec);
552 retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
555 retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
556 SHELL32_FreeUnicodeBuf(wPath);
561 /*************************************************************************
562 * SHCreateDirectoryExW [SHELL32.@]
564 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
566 int ret = ERROR_BAD_PATHNAME;
567 TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
569 if (PathIsRelativeW(path))
575 ret = SHNotifyCreateDirectoryW(path, sec);
576 /* Refuse to work on certain error codes before trying to create directories recursively */
577 if (ret != ERROR_FILE_EXISTS &&
578 ret != ERROR_ALREADY_EXISTS &&
579 ret != ERROR_FILENAME_EXCED_RANGE)
581 WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1]; /* extra for PathAddBackslash() */
583 lstrcpynW(szTemp, path, MAX_PATH);
584 pEnd = PathAddBackslashW(szTemp);
589 while (*pSlash && *pSlash != '\\')
590 pSlash = CharNextW(pSlash);
594 *pSlash = 0; /* terminate path at separator */
596 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
598 *pSlash++ = '\\'; /* put the separator back */
602 if (ret && hWnd && (ERROR_CANCELLED != ret))
604 /* We failed and should show a dialog box */
605 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
606 ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
613 /*************************************************************************
614 * SHFindAttrW [internal]
616 * Get the Attributes for a file or directory. The difference to GetAttributes()
617 * is that this function will also work for paths containing wildcard characters
621 * path [I] path of directory or file to check
622 * fileOnly [I] TRUE if only files should be found
625 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
626 * the first file or directory found otherwise
628 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
630 WIN32_FIND_DATAW wfd;
631 BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
632 DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
633 HANDLE hFind = FindFirstFileW(pName, &wfd);
635 TRACE("%s %d\n", debugstr_w(pName), fileOnly);
636 if (INVALID_HANDLE_VALUE != hFind)
640 if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
642 dwAttr = wfd.dwFileAttributes;
645 while (FindNextFileW(hFind, &wfd));
651 /*************************************************************************
653 * SHFileStrICmp HelperFunction for SHFileOperationW
656 BOOL SHFileStrICmpW(LPWSTR p1, LPWSTR p2, LPWSTR p1End, LPWSTR p2End)
661 int i_len1 = lstrlenW(p1);
662 int i_len2 = lstrlenW(p2);
664 if (p1End && (&p1[i_len1] >= p1End) && ('\\' == p1End[0]))
668 i_len1 = lstrlenW(p1);
672 if ((&p2[i_len2] >= p2End) && ('\\' == p2End[0]))
681 if ((i_len1 <= i_len2) && ('\\' == p2[i_len1]))
688 i_len2 = lstrlenW(p2);
689 if (i_len1 == i_len2)
690 i_Temp = lstrcmpiW(p1,p2);
698 /*************************************************************************
700 * SHFileStrCpyCat HelperFunction for SHFileOperationW
703 LPWSTR SHFileStrCpyCatW(LPWSTR pTo, LPCWSTR pFrom, LPCWSTR pCatStr)
705 LPWSTR pToFile = NULL;
710 lstrcpyW(pTo, pFrom);
713 i_len = lstrlenW(pTo);
714 if ((i_len) && (pTo[--i_len] != '\\'))
717 if (pCatStr[0] == '\\')
719 lstrcpyW(&pTo[i_len+1], pCatStr);
721 pToFile = StrRChrW(pTo,NULL,'\\');
722 /* termination of the new string-group */
723 pTo[(lstrlenW(pTo)) + 1] = '\0';
728 /**************************************************************************
729 * SHELL_FileNamesMatch()
731 * Accepts two \0 delimited lists of the file names. Checks whether number of
732 * files in both lists is the same, and checks also if source-name exists.
734 BOOL SHELL_FileNamesMatch(LPCWSTR pszFiles1, LPCWSTR pszFiles2, BOOL bOnlySrc)
736 while ((pszFiles1[0] != '\0') &&
737 (bOnlySrc || (pszFiles2[0] != '\0')))
739 if (NULL == StrPBrkW(pszFiles1, wWildcardChars))
741 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(pszFiles1))
744 pszFiles1 += lstrlenW(pszFiles1) + 1;
746 pszFiles2 += lstrlenW(pszFiles2) + 1;
748 return ((pszFiles1[0] == '\0') && (bOnlySrc || (pszFiles2[0] == '\0')));
751 /*************************************************************************
753 * SHNameTranslate HelperFunction for SHFileOperationA
755 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
756 * is NULL, only the necessary size of the string is determined and returned,
757 * otherwise the ASCII strings are copied into it and the buffer is increased
758 * to point to the location after the final 0 termination char.
760 DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
762 DWORD size = 0, aSize = 0;
763 LPCSTR aString = (LPCSTR)*pWToFrom;
769 size = lstrlenA(aString) + 1;
772 } while ((size != 1) && more);
773 /* The two sizes might be different in the case of multibyte chars */
774 size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
775 if (*wString) /* only in the second loop */
777 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
778 *pWToFrom = *wString;
784 /*************************************************************************
785 * SHFileOperationA [SHELL32.@]
787 * Function to copy, move, delete and create one or more files with optional
791 * lpFileOp [I/O] pointer to a structure containing all the necessary information
796 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
798 SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
801 LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
802 wString = NULL; /* we change this in SHNameTranslate */
805 if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
806 nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
807 if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
808 nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
809 while (1) /* every loop calculate size, second translate also, if we have storage for this */
811 size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
812 size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
813 size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
817 retCode = SHFileOperationW(&nFileOp);
818 HeapFree(GetProcessHeap(), 0, ForFree); /* we can not use wString, it was changed */
823 wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
824 if (ForFree) continue;
825 retCode = ERROR_OUTOFMEMORY;
826 nFileOp.fAnyOperationsAborted = TRUE;
827 SetLastError(retCode);
832 lpFileOp->hNameMappings = nFileOp.hNameMappings;
833 lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
837 static const char * debug_shfileops_flags( DWORD fFlags )
839 return wine_dbg_sprintf( "%s%s%s%s%s%s%s%s%s%s%s%s%s",
840 fFlags & FOF_MULTIDESTFILES ? "FOF_MULTIDESTFILES " : "",
841 fFlags & FOF_CONFIRMMOUSE ? "FOF_CONFIRMMOUSE " : "",
842 fFlags & FOF_SILENT ? "FOF_SILENT " : "",
843 fFlags & FOF_RENAMEONCOLLISION ? "FOF_RENAMEONCOLLISION " : "",
844 fFlags & FOF_NOCONFIRMATION ? "FOF_NOCONFIRMATION " : "",
845 fFlags & FOF_WANTMAPPINGHANDLE ? "FOF_WANTMAPPINGHANDLE " : "",
846 fFlags & FOF_ALLOWUNDO ? "FOF_ALLOWUNDO " : "",
847 fFlags & FOF_FILESONLY ? "FOF_FILESONLY " : "",
848 fFlags & FOF_SIMPLEPROGRESS ? "FOF_SIMPLEPROGRESS " : "",
849 fFlags & FOF_NOCONFIRMMKDIR ? "FOF_NOCONFIRMMKDIR " : "",
850 fFlags & FOF_NOERRORUI ? "FOF_NOERRORUI " : "",
851 fFlags & FOF_NOCOPYSECURITYATTRIBS ? "FOF_NOCOPYSECURITYATTRIBS" : "",
852 fFlags & 0xf000 ? "MORE-UNKNOWN-Flags" : "");
855 static const char * debug_shfileops_action( DWORD op )
857 LPCSTR cFO_Name [] = {"FO_????","FO_MOVE","FO_COPY","FO_DELETE","FO_RENAME"};
858 return wine_dbg_sprintf("%s", cFO_Name[ op ]);
861 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
862 #define HIGH_ADR (LPWSTR)0xffffffff
864 static int file_operation_delete( WIN32_FIND_DATAW *wfd,SHFILEOPSTRUCTW nFileOp, LPWSTR pFromFile,LPWSTR pTempFrom,HANDLE *hFind)
868 BOOL b_Mask = (NULL != StrPBrkW(&pFromFile[1], wWildcardChars));
872 lpFileName = wfd->cAlternateFileName;
874 lpFileName = wfd->cFileName;
875 if (IsDotDir(lpFileName) ||
876 ((b_Mask) && IsAttribDir(wfd->dwFileAttributes) && (nFileOp.fFlags & FOF_FILESONLY)))
878 SHFileStrCpyCatW(&pFromFile[1], lpFileName, NULL);
879 /* TODO: Check the SHELL_DeleteFileOrDirectoryW() function in shell32.dll */
880 if (IsAttribFile(wfd->dwFileAttributes))
882 if(SHNotifyDeleteFileW(pTempFrom) != ERROR_SUCCESS)
884 nFileOp.fAnyOperationsAborted = TRUE;
885 retCode = 0x78; /* value unknown */
888 else if(!SHELL_DeleteDirectoryW(pTempFrom, (!(nFileOp.fFlags & FOF_NOCONFIRMATION))))
890 nFileOp.fAnyOperationsAborted = TRUE;
891 retCode = 0x79; /* value unknown */
894 while (!nFileOp.fAnyOperationsAborted && FindNextFileW(*hFind,wfd));
896 *hFind = INVALID_HANDLE_VALUE;
904 * FOF_MULTIDESTFILES, FOF_NOCONFIRMATION, FOF_FILESONLY
906 * unimplememented and ignored flags:
907 * FOF_CONFIRMMOUSE, FOF_SILENT, FOF_NOCONFIRMMKDIR,
908 * FOF_SIMPLEPROGRESS, FOF_NOCOPYSECURITYATTRIBS
910 * partially implemented, breaks if file exists:
911 * FOF_RENAMEONCOLLISION
913 * unimplemented and break if any other flag set:
914 * FOF_ALLOWUNDO, FOF_WANTMAPPINGHANDLE
917 static int file_operation_checkFlags(SHFILEOPSTRUCTW nFileOp)
919 FILEOP_FLAGS OFl = ((FILEOP_FLAGS)nFileOp.fFlags & 0xfff);
920 long FuncSwitch = (nFileOp.wFunc & FO_MASK);
921 long level= nFileOp.wFunc >> 4;
923 TRACE("%s level=%ld nFileOp.fFlags=0x%x\n",
924 debug_shfileops_action(FuncSwitch), level, nFileOp.fFlags);
925 /* OFl &= (-1 - (FOF_MULTIDESTFILES | FOF_FILESONLY)); */
926 /* OFl ^= (FOF_SILENT | FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS | FOF_NOCONFIRMMKDIR); */
927 OFl &= (~(FOF_MULTIDESTFILES | FOF_NOCONFIRMATION | FOF_FILESONLY)); /* implemented */
928 OFl ^= (FOF_SILENT | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_NOCOPYSECURITYATTRIBS); /* ignored, if one */
929 OFl &= (~FOF_SIMPLEPROGRESS); /* ignored, only with FOF_SILENT */
932 if (OFl & (~(FOF_CONFIRMMOUSE | FOF_SILENT | FOF_RENAMEONCOLLISION |
933 FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_NOCOPYSECURITYATTRIBS)))
935 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not implemented, Aborted=TRUE, stub\n",
936 debug_shfileops_action(FuncSwitch), level, OFl);
937 return 0x403; /* 1027, we need an extension to shlfileop */
941 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not fully implemented, stub\n",
942 debug_shfileops_action(FuncSwitch), level, OFl);
948 /*************************************************************************
949 * SHFileOperationW [SHELL32.@]
951 * See SHFileOperationA
953 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
955 SHFILEOPSTRUCTW nFileOp = *(lpFileOp);
957 LPCWSTR pNextFrom = nFileOp.pFrom;
958 LPCWSTR pNextTo = nFileOp.pTo;
959 LPCWSTR pFrom = pNextFrom;
961 HANDLE hFind = INVALID_HANDLE_VALUE;
962 WIN32_FIND_DATAW wfd;
963 LPWSTR pTempFrom = NULL;
964 LPWSTR pTempTo = NULL;
966 LPWSTR pToFile = NULL;
972 BOOL b_Multi = (nFileOp.fFlags & FOF_MULTIDESTFILES);
974 BOOL b_MultiTo = (FO_DELETE != (lpFileOp->wFunc & FO_MASK));
975 BOOL b_MultiPaired = (!b_MultiTo);
976 BOOL b_MultiFrom = FALSE;
981 BOOL b_ToInvalidTail = FALSE;
982 BOOL b_ToValid; /* for W98-Bug for FO_MOVE with source and target in same rootdrive */
984 BOOL b_ToTailSlash = FALSE;
986 long FuncSwitch = (nFileOp.wFunc & FO_MASK);
987 long level= nFileOp.wFunc>>4;
991 /* default no error */
992 nFileOp.fAnyOperationsAborted = FALSE;
994 if ((FuncSwitch < FO_MOVE) || (FuncSwitch > FO_RENAME))
995 goto shfileop_end; /* no valid FunctionCode */
998 TRACE("%s: flags (0x%04x) : %s\n",
999 debug_shfileops_action(FuncSwitch), nFileOp.fFlags,
1000 debug_shfileops_flags(nFileOp.fFlags) );
1002 /* establish when pTo is interpreted as the name of the destination file
1003 * or the directory where the Fromfile should be copied to.
1005 * (1) pTo points to the name of an existing directory;
1006 * (2) the flag FOF_MULTIDESTFILES is present;
1007 * (3) whether pFrom point to multiple filenames.
1011 * destisdir 1 1 1 1 0 0 0 0
1012 * FOF_MULTIDESTFILES 1 1 0 0 1 1 0 0
1013 * multiple from filenames 1 0 1 0 1 0 1 0
1015 * copy files to dir 1 0 1 1 0 0 1 0
1016 * create dir 0 0 0 0 0 0 1 0
1019 ret = file_operation_checkFlags(nFileOp);
1026 if ((pNextFrom) && (!(b_MultiTo) || (pNextTo)))
1028 nFileOp.pFrom = pTempFrom = HeapAlloc(GetProcessHeap(), 0, ((1 + 2 * (b_MultiTo)) * MAX_PATH + 6) * sizeof(WCHAR));
1031 retCode = ERROR_OUTOFMEMORY;
1032 SetLastError(retCode);
1036 pTempTo = &pTempFrom[MAX_PATH + 4];
1037 nFileOp.pTo = pTempTo;
1038 ask_overwrite = (!(nFileOp.fFlags & FOF_NOCONFIRMATION) && !(nFileOp.fFlags & FOF_RENAMEONCOLLISION));
1039 not_overwrite = (!(nFileOp.fFlags & FOF_NOCONFIRMATION) || (nFileOp.fFlags & FOF_RENAMEONCOLLISION));
1043 retCode = ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1046 /* need break at error before change sourcepointer */
1047 while(!nFileOp.fAnyOperationsAborted && (pNextFrom[0]))
1049 nFileOp.wFunc = ((level + 1) << 4) + FuncSwitch;
1050 nFileOp.fFlags = lpFileOp->fFlags;
1055 pNextTo = &pNextTo[lstrlenW(pTo)+1];
1056 b_MultiTo = (b_Multi && pNextTo[0]);
1060 pNextFrom = &pNextFrom[lstrlenW(pNextFrom)+1];
1061 if (!b_MultiFrom && !b_MultiTo)
1062 b_MultiFrom = (pNextFrom[0]);
1064 pFromFile = SHFileStrCpyCatW(pTempFrom, pFrom, NULL);
1068 pToFile = SHFileStrCpyCatW(pTempTo, pTo, NULL);
1073 SHELL_FileNamesMatch(lpFileOp->pFrom, lpFileOp->pTo, (!b_Multi || b_MultiFrom));
1075 if (!(b_MultiPaired) || !(pFromFile) || !(pFromFile[1]) || ((pTo) && !(pToFile)))
1077 retCode = ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1082 b_ToTailSlash = (!pToFile[1]);
1086 if (StrChrW(pTempTo,'\\'))
1088 pToFile = SHFileStrCpyCatW(pTempTo, NULL, NULL);
1091 b_ToInvalidTail = (NULL != StrPBrkW(&pToFile[1], wWildcardChars));
1095 b_Mask = (NULL != StrPBrkW(&pFromFile[1], wWildcardChars));
1096 if (FO_RENAME == FuncSwitch)
1098 /* temporary only for FO_RENAME */
1099 if (b_MultiTo || b_MultiFrom || (b_Mask && !b_ToInvalidTail))
1101 #ifndef W98_FO_FUNCTION
1102 retCode = ERROR_GEN_FAILURE; /* W2K ERROR_GEN_FAILURE, W98 returns no error */
1108 hFind = FindFirstFileW(pFrom, &wfd);
1109 if (INVALID_HANDLE_VALUE == hFind)
1111 if ((FO_DELETE == FuncSwitch) && (b_Mask))
1114 pFromFile[0] = '\0';
1115 FromPathAttr = GetFileAttributesW(pTempFrom);
1116 pFromFile[0] = '\\';
1117 if (IsAttribDir(FromPathAttr))
1119 /* FO_DELETE with mask and without found is valid */
1123 /* root (without mask) is also not allowed as source, tested in W98 */
1124 retCode = ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1130 /* ??? b_Mask = (!SHFileStrICmpA(&pFromFile[1], &wfd.cFileName[0], HIGH_ADR, HIGH_ADR)); */
1131 if (!pTo) /* FO_DELETE */
1133 ret = file_operation_delete(&wfd,nFileOp,pFromFile,pTempFrom,&hFind);
1134 /* if ret is not 0, nFileOp.fAnyOperationsAborted is TRUE */
1141 } /* FO_DELETE ends, pTo must be always valid from here */
1143 b_SameRoot = (toupperW(pTempFrom[0]) == toupperW(pTempTo[0]));
1144 b_SameTailName = SHFileStrICmpW(pToFile, pFromFile, NULL, NULL);
1146 ToPathAttr = ToAttr = GetFileAttributesW(pTempTo);
1147 if (!b_Mask && (ToAttr == INVALID_FILE_ATTRIBUTES) && (pToFile))
1150 ToPathAttr = GetFileAttributesW(pTempTo);
1154 if (FO_RENAME == FuncSwitch)
1156 if (!b_SameRoot || b_Mask /* FO_RENAME works not with Mask */
1157 || !SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, NULL)
1158 || (SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, HIGH_ADR) && !b_ToTailSlash))
1163 if (b_ToInvalidTail)
1168 if (INVALID_FILE_ATTRIBUTES == ToPathAttr)
1173 if (IsAttribDir(wfd.dwFileAttributes) && IsAttribDir(ToAttr))
1175 retCode = (b_ToTailSlash) ? 0xb7 : 0x7b;
1178 /* we use SHNotifyMoveFile() instead MoveFileW */
1179 if (SHNotifyMoveFileW(pTempFrom, pTempTo) != ERROR_SUCCESS)
1181 /* we need still the value for the returncode, we use the mostly assumed */
1188 /* W98 Bug with FO_MOVE different from FO_COPY, better the same as FO_COPY */
1189 b_ToValid = ((b_SameTailName && b_SameRoot && (FO_COPY == FuncSwitch)) ||
1190 (b_SameTailName && !b_SameRoot) || (b_ToInvalidTail));
1192 /* handle mask in source */
1195 if (!IsAttribDir(ToAttr))
1197 retCode = (b_ToInvalidTail &&/* b_SameTailName &&*/ (FO_MOVE == FuncSwitch)) \
1201 pToFile = SHFileStrCpyCatW(pTempTo, NULL, wBackslash);
1202 nFileOp.fFlags = (nFileOp.fFlags | FOF_MULTIDESTFILES);
1205 lpFileName = wfd.cAlternateFileName;
1207 lpFileName = wfd.cFileName;
1208 if (IsDotDir(lpFileName) ||
1209 (IsAttribDir(wfd.dwFileAttributes) && (nFileOp.fFlags & FOF_FILESONLY)))
1210 continue; /* next name in pTempFrom(dir) */
1211 SHFileStrCpyCatW(&pToFile[1], lpFileName, NULL);
1212 SHFileStrCpyCatW(&pFromFile[1], lpFileName, NULL);
1213 retCode = SHFileOperationW (&nFileOp);
1214 } while(!nFileOp.fAnyOperationsAborted && FindNextFileW(hFind, &wfd));
1217 hFind = INVALID_HANDLE_VALUE;
1218 /* FO_COPY/FO_MOVE with mask, FO_DELETE and FO_RENAME are solved */
1222 /* only FO_COPY/FO_MOVE without mask, all others are (must be) solved */
1223 if (IsAttribDir(wfd.dwFileAttributes) && (ToAttr == INVALID_FILE_ATTRIBUTES))
1228 ToPathAttr = GetFileAttributesW(pTempTo);
1229 if ((ToPathAttr == INVALID_FILE_ATTRIBUTES) && b_ToValid)
1231 /* create dir must be here, sample target D:\y\ *.* create with RC=10003 */
1232 if (SHNotifyCreateDirectoryW(pTempTo, NULL))
1234 retCode = 0x73;/* value unknown */
1237 ToPathAttr = GetFileAttributesW(pTempTo);
1240 if (b_ToInvalidTail)
1248 /* trailing BackSlash is ever removed and pToFile points to BackSlash before */
1249 if (!b_MultiTo && (b_MultiFrom || (!(b_Multi) && IsAttribDir(ToAttr))))
1251 if ((FO_MOVE == FuncSwitch) && IsAttribDir(ToAttr) && IsAttribDir(wfd.dwFileAttributes))
1255 retCode = 0x73; /* !b_Multi = 0x8 ?? */
1259 pToFile = SHFileStrCpyCatW(pTempTo, NULL, wfd.cFileName);
1260 ToAttr = GetFileAttributesW(pTempTo);
1263 if (IsAttribDir(ToAttr))
1265 if (IsAttribFile(wfd.dwFileAttributes))
1267 retCode = (FO_COPY == FuncSwitch) ? 0x75 : 0xb7;
1274 ToPathAttr = GetFileAttributesW(pTempTo);
1276 if (IsAttribFile(ToPathAttr))
1278 /* error, is this tested ? */
1284 /* singlesource + no mask */
1285 if (INVALID_FILE_ATTRIBUTES == (ToAttr & ToPathAttr))
1287 /* Target-dir does not exist, and cannot be created */
1296 if ((ToAttr == INVALID_FILE_ATTRIBUTES) && SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, NULL))
1298 nFileOp.wFunc = ((level+1)<<4) + FO_RENAME;
1302 if (b_SameRoot && IsAttribDir(ToAttr) && IsAttribDir(wfd.dwFileAttributes))
1304 /* we need pToFile for FO_DELETE after FO_MOVE contence */
1305 pToFile = SHFileStrCpyCatW(pTempFrom, NULL, wWildcardFile);
1309 nFileOp.wFunc = ((level+1)<<4) + FO_COPY;
1312 retCode = SHFileOperationW(&nFileOp);
1314 ((DWORD*)pToFile)[0] = '\0';
1315 if (!nFileOp.fAnyOperationsAborted && (FO_RENAME != (nFileOp.wFunc & 0xf)))
1317 nFileOp.wFunc = ((level+1)<<4) + FO_DELETE;
1318 retCode = SHFileOperationW(&nFileOp);
1322 if (SHFileStrICmpW(pTempFrom, pTempTo, NULL, NULL))
1323 { /* target is the same as source ? */
1324 /* we still need the value for the returncode, we assume 0x71 */
1328 if (IsAttribDir((ToAttr & wfd.dwFileAttributes)))
1330 if (IsAttribDir(ToAttr) || !SHNotifyCreateDirectoryW(pTempTo, NULL))
1332 /* ??? nFileOp.fFlags = (nFileOp.fFlags | FOF_MULTIDESTFILES); */
1333 SHFileStrCpyCatW(pTempFrom, NULL, wWildcardFile);
1334 retCode = SHFileOperationW(&nFileOp);
1338 retCode = 0x750;/* value unknown */
1344 if (!(ask_overwrite && SHELL_ConfirmDialogW(ASK_OVERWRITE_FILE, pTempTo))
1347 /* we still need the value for the returncode, we use the mostly assumed */
1351 if (SHNotifyCopyFileW(pTempFrom, pTempTo, TRUE) != ERROR_SUCCESS)
1353 retCode = 0x77; /* value unknown */
1361 if (hFind != INVALID_HANDLE_VALUE)
1363 hFind = INVALID_HANDLE_VALUE;
1364 HeapFree(GetProcessHeap(), 0, pTempFrom);
1366 nFileOp.fAnyOperationsAborted = TRUE;
1367 TRACE("%s level=%ld AnyOpsAborted=%s ret=0x%x, with %s %s%s\n",
1368 debug_shfileops_action(FuncSwitch), level,
1369 nFileOp.fAnyOperationsAborted ? "TRUE":"FALSE",
1370 retCode, debugstr_w(pFrom), pTo ? "-> ":"", debugstr_w(pTo));
1372 lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
1376 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1378 /*************************************************************************
1379 * SHFreeNameMappings [shell32.246]
1381 * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1385 * hNameMapping [I] handle to the name mappings used during renaming of files
1388 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1392 int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1396 LPSHNAMEMAPPINGW lp = DSA_GetItemPtr((HDSA)hNameMapping, i);
1398 SHFree(lp->pszOldPath);
1399 SHFree(lp->pszNewPath);
1401 DSA_Destroy((HDSA)hNameMapping);
1405 /*************************************************************************
1406 * SheGetDirW [SHELL32.281]
1409 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1410 { FIXME("%p %p stub\n",u,v);
1414 /*************************************************************************
1415 * SheChangeDirW [SHELL32.274]
1418 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1419 { FIXME("(%s),stub\n",debugstr_w(u));
1423 /*************************************************************************
1424 * IsNetDrive [SHELL32.66]
1426 BOOL WINAPI IsNetDrive(DWORD drive)
1429 strcpy(root, "A:\\");
1430 root[0] += (char)drive;
1431 return (GetDriveTypeA(root) == DRIVE_REMOTE);
1435 /*************************************************************************
1436 * RealDriveType [SHELL32.524]
1438 INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1440 char root[] = "A:\\";
1441 root[0] += (char)drive;
1442 return GetDriveTypeA(root);