4 * Copyright 2000 Juergen Schmied
5 * Copyright 2002 Andriy Palamarchuk
6 * Copyright 2002 Dietrich Teickner (from Odin)
7 * Copyright 2002 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 IsAttribFile(x) (!(x == -1) && !(x & FILE_ATTRIBUTE_DIRECTORY))
49 #define IsAttribDir(x) (!(x == -1) && (x & FILE_ATTRIBUTE_DIRECTORY))
51 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
55 static const CHAR aWildcardFile
[] = {'*','.','*',0};
56 static const WCHAR wWildcardFile
[] = {'*','.','*',0};
57 static const WCHAR wWildcardChars
[] = {'*','?',0};
58 static const WCHAR wBackslash
[] = {'\\',0};
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
, BOOL bRenameIfExists
);
67 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bRenameIfExists
);
71 UINT caption_resource_id
, text_resource_id
;
72 } SHELL_ConfirmIDstruc
;
74 static BOOL
SHELL_ConfirmIDs(int nKindOfDialog
, SHELL_ConfirmIDstruc
*ids
)
76 switch (nKindOfDialog
) {
78 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
79 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
81 case ASK_DELETE_FOLDER
:
82 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
83 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
85 case ASK_DELETE_MULTIPLE_ITEM
:
86 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
87 ids
->text_resource_id
= IDS_DELETEMULTIPLE_TEXT
;
89 case ASK_OVERWRITE_FILE
:
90 ids
->caption_resource_id
= IDS_OVERWRITEFILE_CAPTION
;
91 ids
->text_resource_id
= IDS_OVERWRITEFILE_TEXT
;
94 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog
);
99 BOOL
SHELL_ConfirmDialog(int nKindOfDialog
, LPCSTR szDir
)
101 CHAR szCaption
[255], szText
[255], szBuffer
[MAX_PATH
+ 256];
102 SHELL_ConfirmIDstruc ids
;
104 if (!SHELL_ConfirmIDs(nKindOfDialog
, &ids
))
107 LoadStringA(shell32_hInstance
, ids
.caption_resource_id
, szCaption
, sizeof(szCaption
));
108 LoadStringA(shell32_hInstance
, ids
.text_resource_id
, szText
, sizeof(szText
));
110 FormatMessageA(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
111 szText
, 0, 0, szBuffer
, sizeof(szBuffer
), (va_list*)&szDir
);
113 return (IDOK
== MessageBoxA(GetActiveWindow(), szBuffer
, szCaption
, MB_OKCANCEL
| MB_ICONEXCLAMATION
));
116 BOOL
SHELL_ConfirmDialogW(int nKindOfDialog
, LPCWSTR szDir
)
118 WCHAR szCaption
[255], szText
[255], szBuffer
[MAX_PATH
+ 256];
119 SHELL_ConfirmIDstruc ids
;
121 if (!SHELL_ConfirmIDs(nKindOfDialog
, &ids
))
124 LoadStringW(shell32_hInstance
, ids
.caption_resource_id
, szCaption
, sizeof(szCaption
));
125 LoadStringW(shell32_hInstance
, ids
.text_resource_id
, szText
, sizeof(szText
));
127 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
128 szText
, 0, 0, szBuffer
, sizeof(szBuffer
), (va_list*)&szDir
);
130 return (IDOK
== MessageBoxW(GetActiveWindow(), szBuffer
, szCaption
, MB_OKCANCEL
| MB_ICONEXCLAMATION
));
133 /**************************************************************************
134 * SHELL_DeleteDirectoryA() [internal]
138 BOOL
SHELL_DeleteDirectoryA(LPCSTR pszDir
, BOOL bShowUI
)
142 WIN32_FIND_DATAA wfd
;
143 char szTemp
[MAX_PATH
];
145 /* Make sure the directory exists before eventually prompting the user */
146 PathCombineA(szTemp
, pszDir
, aWildcardFile
);
147 hFind
= FindFirstFileA(szTemp
, &wfd
);
148 if (hFind
== INVALID_HANDLE_VALUE
)
151 if (!bShowUI
|| (ret
= SHELL_ConfirmDialog(ASK_DELETE_FOLDER
, pszDir
)))
155 LPSTR lp
= wfd
.cAlternateFileName
;
160 PathCombineA(szTemp
, pszDir
, lp
);
161 if (FILE_ATTRIBUTE_DIRECTORY
& wfd
.dwFileAttributes
)
162 ret
= SHELL_DeleteDirectoryA(szTemp
, FALSE
);
164 ret
= (SHNotifyDeleteFileA(szTemp
) == ERROR_SUCCESS
);
165 } while (ret
&& FindNextFileA(hFind
, &wfd
));
169 ret
= (SHNotifyRemoveDirectoryA(pszDir
) == ERROR_SUCCESS
);
173 BOOL
SHELL_DeleteDirectoryW(LPCWSTR pszDir
, BOOL bShowUI
)
177 WIN32_FIND_DATAW wfd
;
178 WCHAR szTemp
[MAX_PATH
];
180 /* Make sure the directory exists before eventually prompting the user */
181 PathCombineW(szTemp
, pszDir
, wWildcardFile
);
182 hFind
= FindFirstFileW(szTemp
, &wfd
);
183 if (hFind
== INVALID_HANDLE_VALUE
)
186 if (!bShowUI
|| (ret
= SHELL_ConfirmDialogW(ASK_DELETE_FOLDER
, pszDir
)))
190 LPWSTR lp
= wfd
.cAlternateFileName
;
195 PathCombineW(szTemp
, pszDir
, lp
);
196 if (FILE_ATTRIBUTE_DIRECTORY
& wfd
.dwFileAttributes
)
197 ret
= SHELL_DeleteDirectoryW(szTemp
, FALSE
);
199 ret
= (SHNotifyDeleteFileW(szTemp
) == ERROR_SUCCESS
);
200 } while (ret
&& FindNextFileW(hFind
, &wfd
));
204 ret
= (SHNotifyRemoveDirectoryW(pszDir
) == ERROR_SUCCESS
);
208 /**************************************************************************
209 * SHELL_DeleteFileA() [internal]
211 BOOL
SHELL_DeleteFileA(LPCSTR pszFile
, BOOL bShowUI
)
213 if (bShowUI
&& !SHELL_ConfirmDialog(ASK_DELETE_FILE
, pszFile
))
216 return (SHNotifyDeleteFileA(pszFile
) == ERROR_SUCCESS
);
219 BOOL
SHELL_DeleteFileW(LPCWSTR pszFile
, BOOL bShowUI
)
221 if (bShowUI
&& !SHELL_ConfirmDialogW(ASK_DELETE_FILE
, pszFile
))
224 return (SHNotifyDeleteFileW(pszFile
) == ERROR_SUCCESS
);
227 /**************************************************************************
228 * Win32CreateDirectory [SHELL32.93]
230 * Creates a directory. Also triggers a change notify if one exists.
233 * path [I] path to directory to create
236 * TRUE if successful, FALSE otherwise
239 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
240 * This is Unicode on NT/2000
242 static DWORD
SHNotifyCreateDirectoryA(LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
244 WCHAR wPath
[MAX_PATH
];
245 TRACE("(%s, %p)\n", debugstr_a(path
), sec
);
247 MultiByteToWideChar(CP_ACP
, 0, path
, -1, wPath
, MAX_PATH
);
248 return SHNotifyCreateDirectoryW(wPath
, sec
);
251 /**********************************************************************/
253 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
255 TRACE("(%s, %p)\n", debugstr_w(path
), sec
);
257 if (StrPBrkW(path
, wWildcardChars
))
259 /* FIXME: This test is currently necessary since our CreateDirectory
260 implementation does create directories with wildcard characters
261 without objection!! Once this is fixed, this here can go away. */
262 SetLastError(ERROR_INVALID_NAME
);
263 #ifdef W98_FO_FUNCTION /* W98 */
264 return ERROR_FILE_NOT_FOUND
;
266 return ERROR_INVALID_NAME
;
270 if (CreateDirectoryW(path
, sec
))
272 SHChangeNotify(SHCNE_MKDIR
, SHCNF_PATHW
, path
, NULL
);
273 return ERROR_SUCCESS
;
275 return GetLastError();
278 /**********************************************************************/
280 BOOL WINAPI
Win32CreateDirectoryAW(LPCVOID path
, LPSECURITY_ATTRIBUTES sec
)
282 if (SHELL_OsIsUnicode())
283 return (SHNotifyCreateDirectoryW(path
, sec
) == ERROR_SUCCESS
);
284 return (SHNotifyCreateDirectoryA(path
, sec
) == ERROR_SUCCESS
);
287 /************************************************************************
288 * Win32RemoveDirectory [SHELL32.94]
290 * Deletes a directory. Also triggers a change notify if one exists.
293 * path [I] path to directory to delete
296 * TRUE if successful, FALSE otherwise
299 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
300 * This is Unicode on NT/2000
303 static DWORD
SHNotifyRemoveDirectoryA(LPCSTR path
)
305 WCHAR wPath
[MAX_PATH
];
306 TRACE("(%s)\n", debugstr_a(path
));
308 MultiByteToWideChar(CP_ACP
, 0, path
, -1, wPath
, MAX_PATH
);
309 return SHNotifyRemoveDirectoryW(wPath
);
312 /***********************************************************************/
314 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
)
317 TRACE("(%s)\n", debugstr_w(path
));
319 ret
= RemoveDirectoryW(path
);
322 /* Directory may be write protected */
323 DWORD dwAttr
= GetFileAttributesW(path
);
324 if (dwAttr
!= -1 && dwAttr
& FILE_ATTRIBUTE_READONLY
)
325 if (SetFileAttributesW(path
, dwAttr
& ~FILE_ATTRIBUTE_READONLY
))
326 ret
= RemoveDirectoryW(path
);
330 SHChangeNotify(SHCNE_RMDIR
, SHCNF_PATHW
, path
, NULL
);
331 return ERROR_SUCCESS
;
333 return GetLastError();
336 /***********************************************************************/
338 BOOL WINAPI
Win32RemoveDirectoryAW(LPCVOID path
)
340 if (SHELL_OsIsUnicode())
341 return (SHNotifyRemoveDirectoryW(path
) == ERROR_SUCCESS
);
342 return (SHNotifyRemoveDirectoryA(path
) == ERROR_SUCCESS
);
345 /************************************************************************
346 * Win32DeleteFile [SHELL32.164]
348 * Deletes a file. Also triggers a change notify if one exists.
351 * path [I] path to file to delete
354 * TRUE if successful, FALSE otherwise
357 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
358 * This is Unicode on NT/2000
361 static DWORD
SHNotifyDeleteFileA(LPCSTR path
)
363 WCHAR wPath
[MAX_PATH
];
364 TRACE("(%s)\n", debugstr_a(path
));
366 MultiByteToWideChar(CP_ACP
, 0, path
, -1, wPath
, MAX_PATH
);
367 return SHNotifyDeleteFileW(wPath
);
370 /***********************************************************************/
372 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
)
376 TRACE("(%s)\n", debugstr_w(path
));
378 ret
= DeleteFileW(path
);
381 /* File may be write protected or a system file */
382 DWORD dwAttr
= GetFileAttributesW(path
);
383 if ((dwAttr
!= -1) && (dwAttr
& (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
384 if (SetFileAttributesW(path
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
385 ret
= DeleteFileW(path
);
389 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, path
, NULL
);
390 return ERROR_SUCCESS
;
392 return GetLastError();
395 /***********************************************************************/
397 DWORD WINAPI
Win32DeleteFileAW(LPCVOID path
)
399 if (SHELL_OsIsUnicode())
400 return (SHNotifyDeleteFileW(path
) == ERROR_SUCCESS
);
401 return (SHNotifyDeleteFileA(path
) == ERROR_SUCCESS
);
404 /************************************************************************
405 * SHNotifyMoveFile [internal]
407 * Moves a file. Also triggers a change notify if one exists.
410 * src [I] path to source file to move
411 * dest [I] path to target file to move to
412 * bRename [I] if TRUE, the target file will be renamed if a
413 * file with this name already exists
416 * ERORR_SUCCESS if successful
418 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bRename
)
422 TRACE("(%s %s %s)\n", debugstr_w(src
), debugstr_w(dest
), bRename
? "renameIfExists" : "");
424 if (StrPBrkW(dest
, wWildcardChars
))
426 /* FIXME: This test is currently necessary since our MoveFile
427 implementation does create files with wildcard characters
428 without objection!! Once this is fixed, this here can go away. */
429 SetLastError(ERROR_INVALID_NAME
);
430 #ifdef W98_FO_FUNCTION /* W98 */
431 return ERROR_FILE_NOT_FOUND
;
433 return ERROR_INVALID_NAME
;
437 ret
= MoveFileW(src
, dest
);
440 /* Source file may be write protected or a system file */
441 DWORD dwAttr
= GetFileAttributesW(src
);
442 if ((dwAttr
!= -1) && (dwAttr
& (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
443 if (SetFileAttributesW(src
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
444 ret
= MoveFileW(src
, dest
);
448 /* Destination file probably exists */
449 dwAttr
= GetFileAttributesW(dest
);
452 FIXME("Rename on move to existing file not implemented!\n");
458 SHChangeNotify(SHCNE_RENAMEITEM
, SHCNF_PATHW
, src
, dest
);
459 return ERROR_SUCCESS
;
461 return GetLastError();
464 /************************************************************************
465 * SHNotifyCopyFile [internal]
467 * Copies a file. Also triggers a change notify if one exists.
470 * src [I] path to source file to move
471 * dest [I] path to target file to move to
472 * bRename [I] if TRUE, the target file will be renamed if a
473 * file with this name already exists
476 * ERROR_SUCCESS if successful
478 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bRename
)
482 TRACE("(%s %s %s)\n", debugstr_w(src
), debugstr_w(dest
), bRename
? "renameIfExists" : "");
484 ret
= CopyFileW(src
, dest
, TRUE
);
487 /* Destination file probably exists */
488 DWORD dwAttr
= GetFileAttributesW(dest
);
491 FIXME("Rename on copy to existing file not implemented!\n");
496 SHChangeNotify(SHCNE_CREATE
, SHCNF_PATHW
, dest
, NULL
);
497 return ERROR_SUCCESS
;
499 return GetLastError();
502 /*************************************************************************
503 * SHCreateDirectory [SHELL32.165]
505 * Create a directory at the specified location
509 * path [I] path of directory to create
512 * ERRROR_SUCCESS or one of the following values:
513 * ERROR_BAD_PATHNAME if the path is relative
514 * ERROR_FILE_EXISTS when a file with that name exists
515 * ERROR_ALREADY_EXISTS when the directory already exists
516 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
519 * exported by ordinal
521 * WinNT/2000 exports Unicode
523 DWORD WINAPI
SHCreateDirectory(HWND hWnd
, LPCVOID path
)
525 if (SHELL_OsIsUnicode())
526 return SHCreateDirectoryExW(hWnd
, path
, NULL
);
527 return SHCreateDirectoryExA(hWnd
, path
, NULL
);
530 /*************************************************************************
531 * SHCreateDirectoryExA [SHELL32.@]
533 * Create a directory at the specified location
537 * path [I] path of directory to create
538 * sec [I] security attributes to use or NULL
541 * ERRROR_SUCCESS or one of the following values:
542 * ERROR_BAD_PATHNAME if the path is relative
543 * ERORO_INVALID_NAME if the path contains invalid chars
544 * ERROR_FILE_EXISTS when a file with that name exists
545 * ERROR_ALREADY_EXISTS when the directory already exists
546 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
548 int WINAPI
SHCreateDirectoryExA(HWND hWnd
, LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
550 WCHAR wPath
[MAX_PATH
];
551 TRACE("(%p, %s, %p)\n",hWnd
, debugstr_a(path
), sec
);
553 MultiByteToWideChar(CP_ACP
, 0, path
, -1, wPath
, MAX_PATH
);
554 return SHCreateDirectoryExW(hWnd
, wPath
, sec
);
557 /*************************************************************************
558 * SHCreateDirectoryExW [SHELL32.@]
560 int WINAPI
SHCreateDirectoryExW(HWND hWnd
, LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
562 int ret
= ERROR_BAD_PATHNAME
;
563 TRACE("(%p, %s, %p)\n",hWnd
, debugstr_w(path
), sec
);
565 if (PathIsRelativeW(path
))
571 ret
= SHNotifyCreateDirectoryW(path
, sec
);
572 if (ret
!= ERROR_FILE_EXISTS
&&
573 ret
!= ERROR_ALREADY_EXISTS
&&
574 ret
!= ERROR_FILENAME_EXCED_RANGE
)
576 /* handling network file names?
577 lstrcpynW(pathName, path, MAX_PATH);
578 lpStr = PathAddBackslashW(pathName);*/
579 FIXME("Semi-stub, non zero hWnd should be used somehow?\n");
585 /*************************************************************************
587 * SHFileStrICmp HelperFunction for SHFileOperationW
590 BOOL
SHFileStrICmpW(LPWSTR p1
, LPWSTR p2
, LPWSTR p1End
, LPWSTR p2End
)
595 int i_len1
= lstrlenW(p1
);
596 int i_len2
= lstrlenW(p2
);
598 if (p1End
&& (&p1
[i_len1
] >= p1End
) && ('\\' == p1End
[0]))
602 i_len1
= lstrlenW(p1
);
606 if ((&p2
[i_len2
] >= p2End
) && ('\\' == p2End
[0]))
615 if ((i_len1
<= i_len2
) && ('\\' == p2
[i_len1
]))
622 i_len2
= lstrlenW(p2
);
623 if (i_len1
== i_len2
)
624 i_Temp
= lstrcmpiW(p1
,p2
);
632 /*************************************************************************
634 * SHFileStrCpyCat HelperFunction for SHFileOperationW
637 LPWSTR
SHFileStrCpyCatW(LPWSTR pTo
, LPCWSTR pFrom
, LPCWSTR pCatStr
)
639 LPWSTR pToFile
= NULL
;
644 lstrcpyW(pTo
, pFrom
);
647 i_len
= lstrlenW(pTo
);
648 if ((i_len
) && (pTo
[--i_len
] != '\\'))
651 if (pCatStr
[0] == '\\')
653 lstrcpyW(&pTo
[i_len
+1], pCatStr
);
655 pToFile
= StrRChrW(pTo
,NULL
,'\\');
656 /* termination of the new string-group */
657 pTo
[(lstrlenW(pTo
)) + 1] = '\0';
662 /**************************************************************************
663 * SHELL_FileNamesMatch()
665 * Accepts two \0 delimited lists of the file names. Checks whether number of
666 * files in both lists is the same, and checks also if source-name exists.
668 BOOL
SHELL_FileNamesMatch(LPCWSTR pszFiles1
, LPCWSTR pszFiles2
, BOOL bOnlySrc
)
670 while ((pszFiles1
[0] != '\0') &&
671 (bOnlySrc
|| (pszFiles2
[0] != '\0')))
673 if (NULL
== StrPBrkW(pszFiles1
, wWildcardChars
))
675 if (-1 == GetFileAttributesW(pszFiles1
))
678 pszFiles1
+= lstrlenW(pszFiles1
) + 1;
680 pszFiles2
+= lstrlenW(pszFiles2
) + 1;
682 return ((pszFiles1
[0] == '\0') && (bOnlySrc
|| (pszFiles2
[0] == '\0')));
685 /*************************************************************************
687 * SHNameTranslate HelperFunction for SHFileOperationA
689 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
690 * is NULL, only the necessary size of the string is determined and returned,
691 * otherwise the ASCII strings are copied into it and the buffer is increased
692 * to point to the location after the final 0 termination char.
694 DWORD
SHNameTranslate(LPWSTR
* wString
, LPCWSTR
* pWToFrom
, BOOL more
)
696 DWORD size
= 0, aSize
= 0;
697 LPCSTR aString
= (LPCSTR
)*pWToFrom
;
703 size
= lstrlenA(aString
) + 1;
706 } while ((size
!= 1) && more
);
707 /* The two sizes might be different in the case of multibyte chars */
708 size
= MultiByteToWideChar(CP_ACP
, 0, aString
, aSize
, *wString
, 0);
709 if (*wString
) /* only in the second loop */
711 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, size
);
712 *pWToFrom
= *wString
;
718 /*************************************************************************
719 * SHFileOperationA [SHELL32.@]
721 * Function to copy, move, delete and create one or more files with optional
725 * lpFileOp [I/O] pointer to a structure containing all the necessary information
730 int WINAPI
SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp
)
732 SHFILEOPSTRUCTW nFileOp
= *((LPSHFILEOPSTRUCTW
)lpFileOp
);
735 LPWSTR ForFree
= NULL
, /* we change wString in SHNameTranslate and can't use it for freeing */
736 wString
= NULL
; /* we change this in SHNameTranslate */
739 if (FO_DELETE
== (nFileOp
.wFunc
& FO_MASK
))
740 nFileOp
.pTo
= NULL
; /* we need a NULL or a valid pointer for translation */
741 if (!(nFileOp
.fFlags
& FOF_SIMPLEPROGRESS
))
742 nFileOp
.lpszProgressTitle
= NULL
; /* we need a NULL or a valid pointer for translation */
743 while (1) /* every loop calculate size, second translate also, if we have storage for this */
745 size
= SHNameTranslate(&wString
, &nFileOp
.lpszProgressTitle
, FALSE
); /* no loop */
746 size
+= SHNameTranslate(&wString
, &nFileOp
.pFrom
, TRUE
); /* internal loop */
747 size
+= SHNameTranslate(&wString
, &nFileOp
.pTo
, TRUE
); /* internal loop */
751 retCode
= SHFileOperationW(&nFileOp
);
752 HeapFree(GetProcessHeap(), 0, ForFree
); /* we can not use wString, it was changed */
757 wString
= ForFree
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
758 if (ForFree
) continue;
759 retCode
= ERROR_OUTOFMEMORY
;
760 nFileOp
.fAnyOperationsAborted
= TRUE
;
761 SetLastError(retCode
);
766 lpFileOp
->hNameMappings
= nFileOp
.hNameMappings
;
767 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
771 static const char * debug_shfileops_flags( DWORD fFlags
)
773 return wine_dbg_sprintf( "%s%s%s%s%s%s%s%s%s%s%s%s%s",
774 fFlags
& FOF_MULTIDESTFILES
? "FOF_MULTIDESTFILES " : "",
775 fFlags
& FOF_CONFIRMMOUSE
? "FOF_CONFIRMMOUSE " : "",
776 fFlags
& FOF_SILENT
? "FOF_SILENT " : "",
777 fFlags
& FOF_RENAMEONCOLLISION
? "FOF_RENAMEONCOLLISION " : "",
778 fFlags
& FOF_NOCONFIRMATION
? "FOF_NOCONFIRMATION " : "",
779 fFlags
& FOF_WANTMAPPINGHANDLE
? "FOF_WANTMAPPINGHANDLE " : "",
780 fFlags
& FOF_ALLOWUNDO
? "FOF_ALLOWUNDO " : "",
781 fFlags
& FOF_FILESONLY
? "FOF_FILESONLY " : "",
782 fFlags
& FOF_SIMPLEPROGRESS
? "FOF_SIMPLEPROGRESS " : "",
783 fFlags
& FOF_NOCONFIRMMKDIR
? "FOF_NOCONFIRMMKDIR " : "",
784 fFlags
& FOF_NOERRORUI
? "FOF_NOERRORUI " : "",
785 fFlags
& FOF_NOCOPYSECURITYATTRIBS
? "FOF_NOCOPYSECURITYATTRIBS" : "",
786 fFlags
& 0xf000 ? "MORE-UNKNOWN-Flags" : "");
789 static const char * debug_shfileops_action( DWORD op
)
791 LPCSTR cFO_Name
[] = {"FO_????","FO_MOVE","FO_COPY","FO_DELETE","FO_RENAME"};
792 return wine_dbg_sprintf("%s", cFO_Name
[ op
]);
795 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
796 #define HIGH_ADR (LPWSTR)0xffffffff
798 /*************************************************************************
799 * SHFileOperationW [SHELL32.@]
801 * See SHFileOperationA
803 int WINAPI
SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp
)
805 SHFILEOPSTRUCTW nFileOp
= *(lpFileOp
);
807 LPCWSTR pNextFrom
= nFileOp
.pFrom
;
808 LPCWSTR pNextTo
= nFileOp
.pTo
;
809 LPCWSTR pFrom
= pNextFrom
;
811 HANDLE hFind
= INVALID_HANDLE_VALUE
;
812 WIN32_FIND_DATAW wfd
;
813 LPWSTR pTempFrom
= NULL
;
814 LPWSTR pTempTo
= NULL
;
816 LPWSTR pToFile
= NULL
;
822 FILEOP_FLAGS OFl
= ((FILEOP_FLAGS
)lpFileOp
->fFlags
& 0xfff);
824 BOOL b_Multi
= (nFileOp
.fFlags
& FOF_MULTIDESTFILES
);
826 BOOL b_MultiTo
= (FO_DELETE
!= (lpFileOp
->wFunc
& FO_MASK
));
827 BOOL b_MultiPaired
= (!b_MultiTo
);
828 BOOL b_MultiFrom
= FALSE
;
833 BOOL b_ToInvalidTail
= FALSE
;
834 BOOL b_ToValid
; /* for W98-Bug for FO_MOVE with source and target in same rootdrive */
836 BOOL b_ToTailSlash
= FALSE
;
838 long FuncSwitch
= (nFileOp
.wFunc
& FO_MASK
);
839 long level
= nFileOp
.wFunc
>>4;
841 /* default no error */
842 nFileOp
.fAnyOperationsAborted
= FALSE
;
844 if ((FuncSwitch
< FO_MOVE
) || (FuncSwitch
> FO_RENAME
))
845 goto shfileop_end
; /* no valid FunctionCode */
848 TRACE("%s: flags (0x%04x) : %s\n",
849 debug_shfileops_action(FuncSwitch
), nFileOp
.fFlags
,
850 debug_shfileops_flags(nFileOp
.fFlags
) );
852 /* establish when pTo is interpreted as the name of the destination file
853 * or the directory where the Fromfile should be copied to.
855 * (1) pTo points to the name of an existing directory;
856 * (2) the flag FOF_MULTIDESTFILES is present;
857 * (3) whether pFrom point to multiple filenames.
861 * destisdir 1 1 1 1 0 0 0 0
862 * FOF_MULTIDESTFILES 1 1 0 0 1 1 0 0
863 * multiple from filenames 1 0 1 0 1 0 1 0
865 * copy files to dir 1 0 1 1 0 0 1 0
866 * create dir 0 0 0 0 0 0 1 0
873 * FOF_MULTIDESTFILES, FOF_NOCONFIRMATION, FOF_FILESONLY
875 * unimplememented and ignored flags:
876 * FOF_CONFIRMMOUSE, FOF_SILENT, FOF_NOCONFIRMMKDIR,
877 * FOF_SIMPLEPROGRESS, FOF_NOCOPYSECURITYATTRIBS
879 * partially implemented, breaks if file exists:
880 * FOF_RENAMEONCOLLISION
882 * unimplemented and break if any other flag set:
883 * FOF_ALLOWUNDO, FOF_WANTMAPPINGHANDLE
886 TRACE("%s level=%ld nFileOp.fFlags=0x%x\n",
887 debug_shfileops_action(FuncSwitch
), level
, lpFileOp
->fFlags
);
889 /* OFl &= (-1 - (FOF_MULTIDESTFILES | FOF_FILESONLY)); */
890 /* OFl ^= (FOF_SILENT | FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS | FOF_NOCONFIRMMKDIR); */
891 OFl
&= (~(FOF_MULTIDESTFILES
| FOF_NOCONFIRMATION
| FOF_FILESONLY
)); /* implemented */
892 OFl
^= (FOF_SILENT
| FOF_NOCONFIRMMKDIR
| FOF_NOERRORUI
| FOF_NOCOPYSECURITYATTRIBS
); /* ignored, if one */
893 OFl
&= (~FOF_SIMPLEPROGRESS
); /* ignored, only with FOF_SILENT */
896 if (OFl
& (~(FOF_CONFIRMMOUSE
| FOF_SILENT
| FOF_RENAMEONCOLLISION
|
897 FOF_NOCONFIRMMKDIR
| FOF_NOERRORUI
| FOF_NOCOPYSECURITYATTRIBS
)))
899 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not implemented, Aborted=TRUE, stub\n",
900 debug_shfileops_action(FuncSwitch
), level
, OFl
);
901 retCode
= 0x403; /* 1027, we need an extension to shlfileop */
906 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not fully implemented, stub\n",
907 debug_shfileops_action(FuncSwitch
), level
, OFl
);
911 if ((pNextFrom
) && (!(b_MultiTo
) || (pNextTo
)))
913 nFileOp
.pFrom
= pTempFrom
= HeapAlloc(GetProcessHeap(), 0, ((1 + 2 * (b_MultiTo
)) * MAX_PATH
+ 6) * sizeof(WCHAR
));
916 retCode
= ERROR_OUTOFMEMORY
;
917 SetLastError(retCode
);
921 pTempTo
= &pTempFrom
[MAX_PATH
+ 4];
922 nFileOp
.pTo
= pTempTo
;
923 ask_overwrite
= (!(nFileOp
.fFlags
& FOF_NOCONFIRMATION
) && !(nFileOp
.fFlags
& FOF_RENAMEONCOLLISION
));
924 not_overwrite
= (!(nFileOp
.fFlags
& FOF_NOCONFIRMATION
) || (nFileOp
.fFlags
& FOF_RENAMEONCOLLISION
));
928 retCode
= ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
931 /* need break at error before change sourcepointer */
932 while(!nFileOp
.fAnyOperationsAborted
&& (pNextFrom
[0]))
934 nFileOp
.wFunc
= ((level
+ 1) << 4) + FuncSwitch
;
935 nFileOp
.fFlags
= lpFileOp
->fFlags
;
940 pNextTo
= &pNextTo
[lstrlenW(pTo
)+1];
941 b_MultiTo
= (b_Multi
&& pNextTo
[0]);
945 pNextFrom
= &pNextFrom
[lstrlenW(pNextFrom
)+1];
946 if (!b_MultiFrom
&& !b_MultiTo
)
947 b_MultiFrom
= (pNextFrom
[0]);
949 pFromFile
= SHFileStrCpyCatW(pTempFrom
, pFrom
, NULL
);
953 pToFile
= SHFileStrCpyCatW(pTempTo
, pTo
, NULL
);
958 SHELL_FileNamesMatch(lpFileOp
->pFrom
, lpFileOp
->pTo
, (!b_Multi
|| b_MultiFrom
));
960 if (!(b_MultiPaired
) || !(pFromFile
) || !(pFromFile
[1]) || ((pTo
) && !(pToFile
)))
962 retCode
= ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
967 b_ToTailSlash
= (!pToFile
[1]);
971 if (StrChrW(pTempTo
,'\\'))
973 pToFile
= SHFileStrCpyCatW(pTempTo
, NULL
, NULL
);
976 b_ToInvalidTail
= (NULL
!= StrPBrkW(&pToFile
[1], wWildcardChars
));
980 b_Mask
= (NULL
!= StrPBrkW(&pFromFile
[1], wWildcardChars
));
981 if (FO_RENAME
== FuncSwitch
)
983 /* temporary only for FO_RENAME */
984 if (b_MultiTo
|| b_MultiFrom
|| (b_Mask
&& !b_ToInvalidTail
))
986 #ifndef W98_FO_FUNCTION
987 retCode
= ERROR_GEN_FAILURE
; /* W2K ERROR_GEN_FAILURE, W98 returns no error */
993 hFind
= FindFirstFileW(pFrom
, &wfd
);
994 if (INVALID_HANDLE_VALUE
== hFind
)
996 if ((FO_DELETE
== FuncSwitch
) && (b_Mask
))
999 FromPathAttr
= GetFileAttributesW(pTempFrom
);
1000 pFromFile
[0] = '\\';
1001 if (IsAttribDir(FromPathAttr
))
1003 /* FO_DELETE with mask and without found is valid */
1007 /* root (without mask) is also not allowed as source, tested in W98 */
1008 retCode
= ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1014 /* ??? b_Mask = (!SHFileStrICmpA(&pFromFile[1], &wfd.cFileName[0], HIGH_ADR, HIGH_ADR)); */
1015 if (!pTo
) /* FO_DELETE */
1019 lpFileName
= wfd
.cAlternateFileName
;
1021 lpFileName
= wfd
.cFileName
;
1022 if (IsDotDir(lpFileName
) ||
1023 ((b_Mask
) && IsAttribDir(wfd
.dwFileAttributes
) && (nFileOp
.fFlags
& FOF_FILESONLY
)))
1025 SHFileStrCpyCatW(&pFromFile
[1], lpFileName
, NULL
);
1026 /* TODO: Check the SHELL_DeleteFileOrDirectoryW() function in shell32.dll */
1027 if (IsAttribFile(wfd
.dwFileAttributes
))
1029 if(SHNotifyDeleteFileW(pTempFrom
) != ERROR_SUCCESS
)
1031 nFileOp
.fAnyOperationsAborted
= TRUE
;
1032 retCode
= 0x78; /* value unknown */
1037 if(!SHELL_DeleteDirectoryW(pTempFrom
, (!(nFileOp
.fFlags
& FOF_NOCONFIRMATION
))))
1039 nFileOp
.fAnyOperationsAborted
= TRUE
;
1040 retCode
= 0x79; /* value unknown */
1043 } while (!nFileOp
.fAnyOperationsAborted
&& FindNextFileW(hFind
, &wfd
));
1045 hFind
= INVALID_HANDLE_VALUE
;
1046 if (nFileOp
.fAnyOperationsAborted
)
1049 } /* FO_DELETE ends, pTo must be always valid from here */
1051 b_SameRoot
= (toupperW(pTempFrom
[0]) == toupperW(pTempTo
[0]));
1052 b_SameTailName
= SHFileStrICmpW(pToFile
, pFromFile
, NULL
, NULL
);
1054 ToPathAttr
= ToAttr
= GetFileAttributesW(pTempTo
);
1055 if (!b_Mask
&& (ToAttr
== -1) && (pToFile
))
1058 ToPathAttr
= GetFileAttributesW(pTempTo
);
1062 if (FO_RENAME
== FuncSwitch
)
1064 if (!b_SameRoot
|| b_Mask
/* FO_RENAME works not with Mask */
1065 || !SHFileStrICmpW(pTempFrom
, pTempTo
, pFromFile
, NULL
)
1066 || (SHFileStrICmpW(pTempFrom
, pTempTo
, pFromFile
, HIGH_ADR
) && !b_ToTailSlash
))
1071 if (b_ToInvalidTail
)
1076 if (-1 == ToPathAttr
)
1081 if (IsAttribDir(wfd
.dwFileAttributes
) && IsAttribDir(ToAttr
))
1083 retCode
= (b_ToTailSlash
) ? 0xb7 : 0x7b;
1086 /* we use SHNotifyMoveFile() instead MoveFileW */
1087 if (SHNotifyMoveFileW(pTempFrom
, pTempTo
, nFileOp
.fFlags
& FOF_RENAMEONCOLLISION
) != ERROR_SUCCESS
)
1089 /* we need still the value for the returncode, we use the mostly assumed */
1096 /* W98 Bug with FO_MOVE different from FO_COPY, better the same as FO_COPY */
1097 b_ToValid
= ((b_SameTailName
&& b_SameRoot
&& (FO_COPY
== FuncSwitch
)) ||
1098 (b_SameTailName
&& !b_SameRoot
) || (b_ToInvalidTail
));
1100 /* handle mask in source */
1103 if (!IsAttribDir(ToAttr
))
1105 retCode
= (b_ToInvalidTail
&&/* b_SameTailName &&*/ (FO_MOVE
== FuncSwitch
)) \
1109 pToFile
= SHFileStrCpyCatW(pTempTo
, NULL
, wBackslash
);
1110 nFileOp
.fFlags
= (nFileOp
.fFlags
| FOF_MULTIDESTFILES
);
1113 lpFileName
= wfd
.cAlternateFileName
;
1115 lpFileName
= wfd
.cFileName
;
1116 if (IsDotDir(lpFileName
) ||
1117 (IsAttribDir(wfd
.dwFileAttributes
) && (nFileOp
.fFlags
& FOF_FILESONLY
)))
1118 continue; /* next name in pTempFrom(dir) */
1119 SHFileStrCpyCatW(&pToFile
[1], lpFileName
, NULL
);
1120 SHFileStrCpyCatW(&pFromFile
[1], lpFileName
, NULL
);
1121 retCode
= SHFileOperationW (&nFileOp
);
1122 } while(!nFileOp
.fAnyOperationsAborted
&& FindNextFileW(hFind
, &wfd
));
1125 hFind
= INVALID_HANDLE_VALUE
;
1126 /* FO_COPY/FO_MOVE with mask, FO_DELETE and FO_RENAME are solved */
1130 /* only FO_COPY/FO_MOVE without mask, all others are (must be) solved */
1131 if (IsAttribDir(wfd
.dwFileAttributes
) && (ToAttr
== -1))
1136 ToPathAttr
= GetFileAttributesW(pTempTo
);
1137 if ((ToPathAttr
== -1) && b_ToValid
)
1139 /* create dir must be here, sample target D:\y\ *.* create with RC=10003 */
1140 if (SHCreateDirectoryExW(NULL
, pTempTo
, NULL
))
1142 retCode
= 0x73;/* value unknown */
1145 ToPathAttr
= GetFileAttributesW(pTempTo
);
1148 if (b_ToInvalidTail
)
1156 /* trailing BackSlash is ever removed and pToFile points to BackSlash before */
1157 if (!b_MultiTo
&& (b_MultiFrom
|| (!(b_Multi
) && IsAttribDir(ToAttr
))))
1159 if ((FO_MOVE
== FuncSwitch
) && IsAttribDir(ToAttr
) && IsAttribDir(wfd
.dwFileAttributes
))
1163 retCode
= 0x73; /* !b_Multi = 0x8 ?? */
1167 pToFile
= SHFileStrCpyCatW(pTempTo
, NULL
, wfd
.cFileName
);
1168 ToAttr
= GetFileAttributesW(pTempTo
);
1171 if (IsAttribDir(ToAttr
))
1173 if (IsAttribFile(wfd
.dwFileAttributes
))
1175 retCode
= (FO_COPY
== FuncSwitch
) ? 0x75 : 0xb7;
1182 ToPathAttr
= GetFileAttributesW(pTempTo
);
1184 if (IsAttribFile(ToPathAttr
))
1186 /* error, is this tested ? */
1192 /* singlesource + no mask */
1193 if (-1 == (ToAttr
& ToPathAttr
))
1195 /* Target-dir does not exist, and cannot be created */
1204 if ((ToAttr
== -1) && SHFileStrICmpW(pTempFrom
, pTempTo
, pFromFile
, NULL
))
1206 nFileOp
.wFunc
= ((level
+1)<<4) + FO_RENAME
;
1210 if (b_SameRoot
&& IsAttribDir(ToAttr
) && IsAttribDir(wfd
.dwFileAttributes
))
1212 /* we need pToFile for FO_DELETE after FO_MOVE contence */
1213 pToFile
= SHFileStrCpyCatW(pTempFrom
, NULL
, wWildcardFile
);
1217 nFileOp
.wFunc
= ((level
+1)<<4) + FO_COPY
;
1220 retCode
= SHFileOperationW(&nFileOp
);
1222 ((DWORD
*)pToFile
)[0] = '\0';
1223 if (!nFileOp
.fAnyOperationsAborted
&& (FO_RENAME
!= (nFileOp
.wFunc
& 0xf)))
1225 nFileOp
.wFunc
= ((level
+1)<<4) + FO_DELETE
;
1226 retCode
= SHFileOperationW(&nFileOp
);
1230 if (SHFileStrICmpW(pTempFrom
, pTempTo
, NULL
, NULL
))
1231 { /* target is the same as source ? */
1232 /* we still need the value for the returncode, we assume 0x71 */
1236 if (IsAttribDir((ToAttr
& wfd
.dwFileAttributes
)))
1238 if (IsAttribDir(ToAttr
) || !SHCreateDirectoryExW(NULL
,pTempTo
, NULL
))
1240 /* ??? nFileOp.fFlags = (nFileOp.fFlags | FOF_MULTIDESTFILES); */
1241 SHFileStrCpyCatW(pTempFrom
, NULL
, wWildcardFile
);
1242 retCode
= SHFileOperationW(&nFileOp
);
1246 retCode
= 0x750;/* value unknown */
1252 if (!(ask_overwrite
&& SHELL_ConfirmDialogW(ASK_OVERWRITE_FILE
, pTempTo
))
1255 /* we still need the value for the returncode, we use the mostly assumed */
1259 if (SHNotifyCopyFileW(pTempFrom
, pTempTo
, nFileOp
.fFlags
& FOF_RENAMEONCOLLISION
) != ERROR_SUCCESS
)
1261 retCode
= 0x77; /* value unknown */
1269 if (hFind
!= INVALID_HANDLE_VALUE
)
1271 hFind
= INVALID_HANDLE_VALUE
;
1273 HeapFree(GetProcessHeap(), 0, pTempFrom
);
1275 nFileOp
.fAnyOperationsAborted
= TRUE
;
1276 TRACE("%s level=%ld AnyOpsAborted=%s ret=0x%x, with %s %s%s\n",
1277 debug_shfileops_action(FuncSwitch
), level
,
1278 nFileOp
.fAnyOperationsAborted
? "TRUE":"FALSE",
1279 retCode
, debugstr_w(pFrom
), pTo
? "-> ":"", debugstr_w(pTo
));
1281 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
1285 /*************************************************************************
1286 * SHFileOperation [SHELL32.@]
1289 DWORD WINAPI
SHFileOperationAW(LPVOID lpFileOp
)
1291 if (SHELL_OsIsUnicode())
1292 return SHFileOperationW(lpFileOp
);
1293 return SHFileOperationA(lpFileOp
);
1296 /*************************************************************************
1297 * SheGetDirW [SHELL32.281]
1300 HRESULT WINAPI
SheGetDirW(LPWSTR u
, LPWSTR v
)
1301 { FIXME("%p %p stub\n",u
,v
);
1305 /*************************************************************************
1306 * SheChangeDirW [SHELL32.274]
1309 HRESULT WINAPI
SheChangeDirW(LPWSTR u
)
1310 { FIXME("(%s),stub\n",debugstr_w(u
));
1314 /*************************************************************************
1315 * IsNetDrive [SHELL32.66]
1317 BOOL WINAPI
IsNetDrive(DWORD drive
)
1320 strcpy(root
, "A:\\");
1321 root
[0] += (char)drive
;
1322 return (GetDriveTypeA(root
) == DRIVE_REMOTE
);
1326 /*************************************************************************
1327 * RealDriveType [SHELL32.524]
1329 INT WINAPI
RealDriveType(INT drive
, BOOL bQueryNet
)
1331 char root
[] = "A:\\";
1332 root
[0] += (char)drive
;
1333 return GetDriveTypeA(root
);