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"
34 #define NO_SHLWAPI_STREAM
36 #include "shell32_main.h"
37 #include "undocshell.h"
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
43 #define IsAttribFile(x) (!(x == -1) && !(x & FILE_ATTRIBUTE_DIRECTORY))
44 #define IsAttribDir(x) (!(x == -1) && (x & FILE_ATTRIBUTE_DIRECTORY))
46 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
50 CHAR aWildcardFile
[] = {'*','.','*',0};
51 WCHAR wWildcardFile
[] = {'*','.','*',0};
52 WCHAR wWildcardChars
[] = {'*','?',0};
53 WCHAR wBackslash
[] = {'\\',0};
55 static DWORD
SHNotifyCreateDirectoryA(LPCSTR path
, LPSECURITY_ATTRIBUTES sec
);
56 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
);
57 static DWORD
SHNotifyRemoveDirectoryA(LPCSTR path
);
58 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
);
59 static DWORD
SHNotifyDeleteFileA(LPCSTR path
);
60 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
);
61 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bRenameIfExists
);
62 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bRenameIfExists
);
66 UINT caption_resource_id
, text_resource_id
;
67 } SHELL_ConfirmIDstruc
;
69 static BOOL
SHELL_ConfirmIDs(int nKindOfDialog
, SHELL_ConfirmIDstruc
*ids
)
71 switch (nKindOfDialog
) {
73 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
74 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
76 case ASK_DELETE_FOLDER
:
77 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
78 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
80 case ASK_DELETE_MULTIPLE_ITEM
:
81 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
82 ids
->text_resource_id
= IDS_DELETEMULTIPLE_TEXT
;
84 case ASK_OVERWRITE_FILE
:
85 ids
->caption_resource_id
= IDS_OVERWRITEFILE_CAPTION
;
86 ids
->text_resource_id
= IDS_OVERWRITEFILE_TEXT
;
89 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog
);
94 BOOL
SHELL_ConfirmDialog(int nKindOfDialog
, LPCSTR szDir
)
96 CHAR szCaption
[255], szText
[255], szBuffer
[MAX_PATH
+ 256];
97 SHELL_ConfirmIDstruc ids
;
99 if (!SHELL_ConfirmIDs(nKindOfDialog
, &ids
))
102 LoadStringA(shell32_hInstance
, ids
.caption_resource_id
, szCaption
, sizeof(szCaption
));
103 LoadStringA(shell32_hInstance
, ids
.text_resource_id
, szText
, sizeof(szText
));
105 FormatMessageA(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
106 szText
, 0, 0, szBuffer
, sizeof(szBuffer
), (va_list*)&szDir
);
108 return (IDOK
== MessageBoxA(GetActiveWindow(), szBuffer
, szCaption
, MB_OKCANCEL
| MB_ICONEXCLAMATION
));
111 BOOL
SHELL_ConfirmDialogW(int nKindOfDialog
, LPCWSTR szDir
)
113 WCHAR szCaption
[255], szText
[255], szBuffer
[MAX_PATH
+ 256];
114 SHELL_ConfirmIDstruc ids
;
116 if (!SHELL_ConfirmIDs(nKindOfDialog
, &ids
))
119 LoadStringW(shell32_hInstance
, ids
.caption_resource_id
, szCaption
, sizeof(szCaption
));
120 LoadStringW(shell32_hInstance
, ids
.text_resource_id
, szText
, sizeof(szText
));
122 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
123 szText
, 0, 0, szBuffer
, sizeof(szBuffer
), (va_list*)&szDir
);
125 return (IDOK
== MessageBoxW(GetActiveWindow(), szBuffer
, szCaption
, MB_OKCANCEL
| MB_ICONEXCLAMATION
));
128 /**************************************************************************
129 * SHELL_DeleteDirectoryA() [internal]
133 BOOL
SHELL_DeleteDirectoryA(LPCSTR pszDir
, BOOL bShowUI
)
137 WIN32_FIND_DATAA wfd
;
138 char szTemp
[MAX_PATH
];
140 /* Make sure the directory exists before eventually prompting the user */
141 PathCombineA(szTemp
, pszDir
, aWildcardFile
);
142 hFind
= FindFirstFileA(szTemp
, &wfd
);
143 if (hFind
== INVALID_HANDLE_VALUE
)
146 if (!bShowUI
|| SHELL_ConfirmDialog(ASK_DELETE_FOLDER
, pszDir
))
150 LPSTR lp
= wfd
.cAlternateFileName
;
155 PathCombineA(szTemp
, pszDir
, lp
);
156 if (FILE_ATTRIBUTE_DIRECTORY
& wfd
.dwFileAttributes
)
157 ret
= SHELL_DeleteDirectoryA(szTemp
, FALSE
);
159 ret
= (SHNotifyDeleteFileA(szTemp
) == ERROR_SUCCESS
);
160 } while (ret
&& FindNextFileA(hFind
, &wfd
));
164 ret
= (SHNotifyRemoveDirectoryA(pszDir
) == ERROR_SUCCESS
);
168 BOOL
SHELL_DeleteDirectoryW(LPCWSTR pszDir
, BOOL bShowUI
)
172 WIN32_FIND_DATAW wfd
;
173 WCHAR szTemp
[MAX_PATH
];
175 /* Make sure the directory exists before eventually prompting the user */
176 PathCombineW(szTemp
, pszDir
, wWildcardFile
);
177 hFind
= FindFirstFileW(szTemp
, &wfd
);
178 if (hFind
== INVALID_HANDLE_VALUE
)
181 if (!bShowUI
|| SHELL_ConfirmDialogW(ASK_DELETE_FOLDER
, pszDir
))
185 LPWSTR lp
= wfd
.cAlternateFileName
;
190 PathCombineW(szTemp
, pszDir
, lp
);
191 if (FILE_ATTRIBUTE_DIRECTORY
& wfd
.dwFileAttributes
)
192 ret
= SHELL_DeleteDirectoryW(szTemp
, FALSE
);
194 ret
= (SHNotifyDeleteFileW(szTemp
) == ERROR_SUCCESS
);
195 } while (ret
&& FindNextFileW(hFind
, &wfd
));
199 ret
= (SHNotifyRemoveDirectoryW(pszDir
) == ERROR_SUCCESS
);
203 /**************************************************************************
204 * SHELL_DeleteFileA() [internal]
206 BOOL
SHELL_DeleteFileA(LPCSTR pszFile
, BOOL bShowUI
)
208 if (bShowUI
&& !SHELL_ConfirmDialog(ASK_DELETE_FILE
, pszFile
))
211 return (SHNotifyDeleteFileA(pszFile
) == ERROR_SUCCESS
);
214 BOOL
SHELL_DeleteFileW(LPCWSTR pszFile
, BOOL bShowUI
)
216 if (bShowUI
&& !SHELL_ConfirmDialogW(ASK_DELETE_FILE
, pszFile
))
219 return (SHNotifyDeleteFileW(pszFile
) == ERROR_SUCCESS
);
222 /**************************************************************************
223 * Win32CreateDirectory [SHELL32.93]
225 * Creates a directory. Also triggers a change notify if one exists.
228 * path [I] path to directory to create
231 * TRUE if successful, FALSE otherwise
234 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
235 * This is Unicode on NT/2000
237 static DWORD
SHNotifyCreateDirectoryA(LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
239 WCHAR wPath
[MAX_PATH
];
240 TRACE("(%s, %p)\n", debugstr_a(path
), sec
);
242 MultiByteToWideChar(CP_ACP
, 0, path
, -1, wPath
, MAX_PATH
);
243 return SHNotifyCreateDirectoryW(wPath
, sec
);
246 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
248 TRACE("(%s, %p)\n", debugstr_w(path
), sec
);
250 if (StrPBrkW(path
, wWildcardChars
))
252 /* FIXME: This test is currently necessary since our CreateDirectory
253 implementation does create directories with wildcard characters
254 without objection!! Once this is fixed, this here can go away. */
255 SetLastError(ERROR_INVALID_NAME
);
256 return ERROR_INVALID_NAME
;
259 if (CreateDirectoryW(path
, sec
))
261 SHChangeNotify(SHCNE_MKDIR
, SHCNF_PATHW
, path
, NULL
);
262 return ERROR_SUCCESS
;
264 return GetLastError();
267 BOOL WINAPI
Win32CreateDirectoryAW(LPCVOID path
, LPSECURITY_ATTRIBUTES sec
)
269 if (SHELL_OsIsUnicode())
270 return (SHNotifyCreateDirectoryW(path
, sec
) == ERROR_SUCCESS
);
271 return (SHNotifyCreateDirectoryA(path
, sec
) == ERROR_SUCCESS
);
274 /************************************************************************
275 * Win32RemoveDirectory [SHELL32.94]
277 * Deletes a directory. Also triggers a change notify if one exists.
280 * path [I] path to directory to delete
283 * TRUE if successful, FALSE otherwise
286 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
287 * This is Unicode on NT/2000
290 static DWORD
SHNotifyRemoveDirectoryA(LPCSTR path
)
292 WCHAR wPath
[MAX_PATH
];
293 TRACE("(%s)\n", debugstr_a(path
));
295 MultiByteToWideChar(CP_ACP
, 0, path
, -1, wPath
, MAX_PATH
);
296 return SHNotifyRemoveDirectoryW(wPath
);
299 /***********************************************************************/
301 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
)
304 TRACE("(%s)\n", debugstr_w(path
));
306 ret
= RemoveDirectoryW(path
);
309 /* Directory may be write protected */
310 DWORD dwAttr
= GetFileAttributesW(path
);
311 if (dwAttr
!= -1 && dwAttr
& FILE_ATTRIBUTE_READONLY
)
312 if (SetFileAttributesW(path
, dwAttr
& ~FILE_ATTRIBUTE_READONLY
))
313 ret
= RemoveDirectoryW(path
);
317 SHChangeNotify(SHCNE_RMDIR
, SHCNF_PATHW
, path
, NULL
);
318 return ERROR_SUCCESS
;
320 return GetLastError();
323 /***********************************************************************/
325 BOOL WINAPI
Win32RemoveDirectoryAW(LPCVOID path
)
327 if (SHELL_OsIsUnicode())
328 return (SHNotifyRemoveDirectoryW(path
) == ERROR_SUCCESS
);
329 return (SHNotifyRemoveDirectoryA(path
) == ERROR_SUCCESS
);
332 /************************************************************************
333 * Win32DeleteFile [SHELL32.164]
335 * Deletes a file. Also triggers a change notify if one exists.
338 * path [I] path to file to delete
341 * TRUE if successful, FALSE otherwise
344 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
345 * This is Unicode on NT/2000
348 static DWORD
SHNotifyDeleteFileA(LPCSTR path
)
350 WCHAR wPath
[MAX_PATH
];
351 TRACE("(%s)\n", debugstr_a(path
));
353 MultiByteToWideChar(CP_ACP
, 0, path
, -1, wPath
, MAX_PATH
);
354 return SHNotifyDeleteFileW(wPath
);
357 /***********************************************************************/
359 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
)
363 TRACE("(%s)\n", debugstr_w(path
));
365 ret
= DeleteFileW(path
);
368 /* File may be write protected or a system file */
369 DWORD dwAttr
= GetFileAttributesW(path
);
370 if ((dwAttr
!= -1) && (dwAttr
& (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
371 if (SetFileAttributesW(path
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
372 ret
= DeleteFileW(path
);
376 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, path
, NULL
);
377 return ERROR_SUCCESS
;
379 return GetLastError();
382 /***********************************************************************/
384 DWORD WINAPI
Win32DeleteFileAW(LPCVOID path
)
386 if (SHELL_OsIsUnicode())
387 return (SHNotifyDeleteFileW(path
) == ERROR_SUCCESS
);
388 return (SHNotifyDeleteFileA(path
) == ERROR_SUCCESS
);
391 /************************************************************************
392 * SHNotifyMoveFile [internal]
394 * Moves a file. Also triggers a change notify if one exists.
397 * src [I] path to source file to move
398 * dest [I] path to target file to move to
399 * bRename [I] if TRUE, the target file will be renamed if a
400 * file with this name already exists
403 * ERORR_SUCCESS if successful
405 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bRename
)
409 TRACE("(%s %s %s)\n", debugstr_w(src
), debugstr_w(dest
), bRename
? "renameIfExists" : "");
411 ret
= MoveFileW(src
, dest
);
414 /* Source file may be write protected or a system file */
415 DWORD dwAttr
= GetFileAttributesW(src
);
416 if ((dwAttr
!= -1) && (dwAttr
& (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
417 if (SetFileAttributesW(src
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
418 ret
= MoveFileW(src
, dest
);
422 /* Destination file probably exists */
423 dwAttr
= GetFileAttributesW(dest
);
426 FIXME("Rename on move to existing file not implemented!");
432 SHChangeNotify(SHCNE_RENAMEITEM
, SHCNF_PATHW
, src
, dest
);
433 return ERROR_SUCCESS
;
435 return GetLastError();
438 /************************************************************************
439 * SHNotifyCopyFile [internal]
441 * Copies a file. Also triggers a change notify if one exists.
444 * src [I] path to source file to move
445 * dest [I] path to target file to move to
446 * bRename [I] if TRUE, the target file will be renamed if a
447 * file with this name already exists
450 * ERROR_SUCCESS if successful
452 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bRename
)
456 TRACE("(%s %s %s)\n", debugstr_w(src
), debugstr_w(dest
), bRename
? "renameIfExists" : "");
458 ret
= CopyFileW(src
, dest
, TRUE
);
461 /* Destination file probably exists */
462 DWORD dwAttr
= GetFileAttributesW(dest
);
465 FIXME("Rename on copy to existing file not implemented!");
470 SHChangeNotify(SHCNE_CREATE
, SHCNF_PATHW
, dest
, NULL
);
471 return ERROR_SUCCESS
;
473 return GetLastError();
476 /*************************************************************************
477 * SHCreateDirectory [SHELL32.165]
479 * Create a directory at the specified location
483 * path [I] path of directory to create
486 * ERRROR_SUCCESS or one of the following values:
487 * ERROR_BAD_PATHNAME if the path is relative
488 * ERROR_FILE_EXISTS when a file with that name exists
489 * ERROR_ALREADY_EXISTS when the directory already exists
490 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
493 * exported by ordinal
495 * WinNT/2000 exports Unicode
497 DWORD WINAPI
SHCreateDirectory(HWND hWnd
, LPCVOID path
)
499 if (SHELL_OsIsUnicode())
500 return SHCreateDirectoryExW(hWnd
, path
, NULL
);
501 return SHCreateDirectoryExA(hWnd
, path
, NULL
);
504 /*************************************************************************
505 * SHCreateDirectoryExA [SHELL32.@]
507 * Create a directory at the specified location
511 * path [I] path of directory to create
512 * sec [I] security attributes to use or NULL
515 * ERRROR_SUCCESS or one of the following values:
516 * ERROR_BAD_PATHNAME if the path is relative
517 * ERORO_INVALID_NAME if the path contains invalid chars
518 * ERROR_FILE_EXISTS when a file with that name exists
519 * ERROR_ALREADY_EXISTS when the directory already exists
520 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
522 DWORD WINAPI
SHCreateDirectoryExA(HWND hWnd
, LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
524 WCHAR wPath
[MAX_PATH
];
525 TRACE("(%p, %s, %p)\n",hWnd
, debugstr_a(path
), sec
);
527 MultiByteToWideChar(CP_ACP
, 0, path
, -1, wPath
, MAX_PATH
);
528 return SHCreateDirectoryExW(hWnd
, wPath
, sec
);
531 /*************************************************************************
532 * SHCreateDirectoryExW [SHELL32.@]
534 DWORD WINAPI
SHCreateDirectoryExW(HWND hWnd
, LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
536 DWORD ret
= ERROR_BAD_PATHNAME
;
537 TRACE("(%p, %s, %p)\n",hWnd
, debugstr_w(path
), sec
);
539 if (PathIsRelativeW(path
))
545 ret
= SHNotifyCreateDirectoryW(path
, sec
);
546 if (ret
!= ERROR_FILE_EXISTS
&&
547 ret
!= ERROR_ALREADY_EXISTS
&&
548 ret
!= ERROR_FILENAME_EXCED_RANGE
)
550 /* handling network file names?
551 lstrcpynW(pathName, path, MAX_PATH);
552 lpStr = PathAddBackslashW(pathName);*/
553 FIXME("Semi-stub, non zero hWnd should be used somehow?");
559 /*************************************************************************
561 * SHFileStrICmp HelperFunction for SHFileOperationW
564 BOOL
SHFileStrICmpW(LPWSTR p1
, LPWSTR p2
, LPWSTR p1End
, LPWSTR p2End
)
569 int i_len1
= lstrlenW(p1
);
570 int i_len2
= lstrlenW(p2
);
572 if (p1End
&& (&p1
[i_len1
] >= p1End
) && ('\\' == p1End
[0]))
576 i_len1
= lstrlenW(p1
);
580 if ((&p2
[i_len2
] >= p2End
) && ('\\' == p2End
[0]))
589 if ((i_len1
<= i_len2
) && ('\\' == p2
[i_len1
]))
596 i_len2
= lstrlenW(p2
);
597 if (i_len1
== i_len2
)
598 i_Temp
= lstrcmpiW(p1
,p2
);
606 /*************************************************************************
608 * SHFileStrCpyCat HelperFunction for SHFileOperationW
611 LPWSTR
SHFileStrCpyCatW(LPWSTR pTo
, LPCWSTR pFrom
, LPCWSTR pCatStr
)
613 LPWSTR pToFile
= NULL
;
618 lstrcpyW(pTo
, pFrom
);
621 i_len
= lstrlenW(pTo
);
622 if ((i_len
) && (pTo
[--i_len
] != '\\'))
625 if (pCatStr
[0] == '\\')
627 lstrcpyW(&pTo
[i_len
+1], pCatStr
);
629 pToFile
= StrRChrW(pTo
,NULL
,'\\');
630 /* termination of the new string-group */
631 pTo
[(lstrlenW(pTo
)) + 1] = '\0';
636 /**************************************************************************
637 * SHELL_FileNamesMatch()
639 * Accepts two \0 delimited lists of the file names. Checks whether number of
640 * files in both lists is the same, and checks also if source-name exists.
642 BOOL
SHELL_FileNamesMatch(LPCWSTR pszFiles1
, LPCWSTR pszFiles2
, BOOL bOnlySrc
)
644 while ((pszFiles1
[0] != '\0') &&
645 (bOnlySrc
|| (pszFiles2
[0] != '\0')))
647 if (NULL
== StrPBrkW(pszFiles1
, wWildcardChars
))
649 if (-1 == GetFileAttributesW(pszFiles1
))
652 pszFiles1
+= lstrlenW(pszFiles1
) + 1;
654 pszFiles2
+= lstrlenW(pszFiles2
) + 1;
656 return ((pszFiles1
[0] == '\0') && (bOnlySrc
|| (pszFiles2
[0] == '\0')));
659 /*************************************************************************
661 * SHNameTranslate HelperFunction for SHFileOperationA
663 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
664 * is NULL, only the necessary size of the string is determined and returned,
665 * otherwise the ASCII strings are copied into it and the buffer is increased
666 * to point to the location after the final 0 termination char.
668 DWORD
SHNameTranslate(LPWSTR
* wString
, LPCWSTR
* pWToFrom
, BOOL more
)
670 DWORD size
= 0, aSize
= 0;
671 LPCSTR aString
= (LPCSTR
)*pWToFrom
;
677 size
= lstrlenA(aString
) + 1;
680 } while ((size
!= 1) && more
);
681 /* The two sizes might be different in the case of multibyte chars */
682 size
= MultiByteToWideChar(CP_ACP
, 0, aString
, aSize
, *wString
, 0);
683 if (*wString
) /* only in the second loop */
685 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, size
);
686 *pWToFrom
= *wString
;
692 /*************************************************************************
693 * SHFileOperationA [SHELL32.@]
695 * Function to copy, move, delete and create one or more files with optional
699 * lpFileOp [I/O] pointer to a structure containing all the necessary information
704 DWORD WINAPI
SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp
)
706 SHFILEOPSTRUCTW nFileOp
= *((LPSHFILEOPSTRUCTW
)lpFileOp
);
707 DWORD retCode
= 0, size
;
708 LPWSTR ForFree
= NULL
, /* we change wString in SHNameTranslate and can't use it for freeing */
709 wString
= NULL
; /* we change this in SHNameTranslate */
712 if (FO_DELETE
== (nFileOp
.wFunc
& FO_MASK
))
713 nFileOp
.pTo
= NULL
; /* we need a NULL or a valid pointer for translation */
714 if (!(nFileOp
.fFlags
& FOF_SIMPLEPROGRESS
))
715 nFileOp
.lpszProgressTitle
= NULL
; /* we need a NULL or a valid pointer for translation */
716 while (1) /* every loop calculate size, second translate also, if we have storage for this */
718 size
= SHNameTranslate(&wString
, &nFileOp
.lpszProgressTitle
, FALSE
); /* no loop */
719 size
+= SHNameTranslate(&wString
, &nFileOp
.pFrom
, TRUE
); /* internal loop */
720 size
+= SHNameTranslate(&wString
, &nFileOp
.pTo
, TRUE
); /* internal loop */
724 retCode
= SHFileOperationW(&nFileOp
);
725 HeapFree(GetProcessHeap(), 0, ForFree
); /* we can not use wString, it was changed */
730 wString
= ForFree
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
731 if (ForFree
) continue;
732 retCode
= ERROR_OUTOFMEMORY
;
733 nFileOp
.fAnyOperationsAborted
= TRUE
;
734 SetLastError(retCode
);
739 lpFileOp
->hNameMappings
= nFileOp
.hNameMappings
;
740 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
744 static const char * debug_shfileops_flags( DWORD fFlags
)
746 return wine_dbg_sprintf( "%s%s%s%s%s%s%s%s%s%s%s%s%s",
747 fFlags
& FOF_MULTIDESTFILES
? "FOF_MULTIDESTFILES " : "",
748 fFlags
& FOF_CONFIRMMOUSE
? "FOF_CONFIRMMOUSE " : "",
749 fFlags
& FOF_SILENT
? "FOF_SILENT " : "",
750 fFlags
& FOF_RENAMEONCOLLISION
? "FOF_RENAMEONCOLLISION " : "",
751 fFlags
& FOF_NOCONFIRMATION
? "FOF_NOCONFIRMATION " : "",
752 fFlags
& FOF_WANTMAPPINGHANDLE
? "FOF_WANTMAPPINGHANDLE " : "",
753 fFlags
& FOF_ALLOWUNDO
? "FOF_ALLOWUNDO " : "",
754 fFlags
& FOF_FILESONLY
? "FOF_FILESONLY " : "",
755 fFlags
& FOF_SIMPLEPROGRESS
? "FOF_SIMPLEPROGRESS " : "",
756 fFlags
& FOF_NOCONFIRMMKDIR
? "FOF_NOCONFIRMMKDIR " : "",
757 fFlags
& FOF_NOERRORUI
? "FOF_NOERRORUI " : "",
758 fFlags
& FOF_NOCOPYSECURITYATTRIBS
? "FOF_NOCOPYSECURITYATTRIBS" : "",
759 fFlags
& 0xf000 ? "MORE-UNKNOWN-Flags" : "");
762 static const char * debug_shfileops_action( DWORD op
)
764 LPCSTR cFO_Name
[] = {"FO_????","FO_MOVE","FO_COPY","FO_DELETE","FO_RENAME"};
765 return wine_dbg_sprintf("%s", cFO_Name
[ op
]);
768 /*************************************************************************
769 * SHFileOperationW [SHELL32.@]
771 * See SHFileOperationA
773 DWORD WINAPI
SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp
)
775 SHFILEOPSTRUCTW nFileOp
= *(lpFileOp
);
777 LPCWSTR pNextFrom
= nFileOp
.pFrom
;
778 LPCWSTR pNextTo
= nFileOp
.pTo
;
779 LPCWSTR pFrom
= pNextFrom
;
781 HANDLE hFind
= INVALID_HANDLE_VALUE
;
782 WIN32_FIND_DATAW wfd
;
783 LPWSTR pTempFrom
= NULL
;
784 LPWSTR pTempTo
= NULL
;
786 LPWSTR pToFile
= NULL
;
792 FILEOP_FLAGS OFl
= ((FILEOP_FLAGS
)lpFileOp
->fFlags
& 0xfff);
794 BOOL b_Multi
= (nFileOp
.fFlags
& FOF_MULTIDESTFILES
);
796 BOOL b_MultiTo
= (FO_DELETE
!= (lpFileOp
->wFunc
& FO_MASK
));
797 BOOL b_MultiPaired
= (!b_MultiTo
);
798 BOOL b_MultiFrom
= FALSE
;
803 BOOL b_ToInvalidTail
= FALSE
;
804 BOOL b_ToValid
; /* for W98-Bug for FO_MOVE with source and target in same rootdrive */
806 BOOL b_ToTailSlash
= FALSE
;
808 long FuncSwitch
= (nFileOp
.wFunc
& FO_MASK
);
809 long level
= nFileOp
.wFunc
>>4;
811 /* default no error */
812 nFileOp
.fAnyOperationsAborted
= FALSE
;
814 if ((FuncSwitch
< FO_MOVE
) || (FuncSwitch
> FO_RENAME
))
815 goto shfileop_normal
; /* no valid FunctionCode */
818 TRACE("%s: flags (0x%04x) : %s\n",
819 debug_shfileops_action(FuncSwitch
), nFileOp
.fFlags
,
820 debug_shfileops_flags(nFileOp
.fFlags
) );
822 /* establish when pTo is interpreted as the name of the destination file
823 * or the directory where the Fromfile should be copied to.
825 * (1) pTo points to the name of an existing directory;
826 * (2) the flag FOF_MULTIDESTFILES is present;
827 * (3) whether pFrom point to multiple filenames.
831 * destisdir 1 1 1 1 0 0 0 0
832 * FOF_MULTIDESTFILES 1 1 0 0 1 1 0 0
833 * multiple from filenames 1 0 1 0 1 0 1 0
835 * copy files to dir 1 0 1 1 0 0 1 0
836 * create dir 0 0 0 0 0 0 1 0
839 * FOF_MULTIDESTFILES, FOF_NOCONFIRMATION, FOF_FILESONLY are implemented
840 * FOF_CONFIRMMOUSE, FOF_SILENT, FOF_NOCONFIRMMKDIR,
841 * FOF_SIMPLEPROGRESS, FOF_NOCOPYSECURITYATTRIBS are not implemented and ignored
842 * FOF_RENAMEONCOLLISION are implemented partially and breaks if file exist
843 * FOF_ALLOWUNDO, FOF_WANTMAPPINGHANDLE are not implemented and breaks
844 * if any other flag set, an error occurs
846 TRACE("%s level=%ld nFileOp.fFlags=0x%x\n",
847 debug_shfileops_action(FuncSwitch
), level
, lpFileOp
->fFlags
);
849 /* OFl &= (-1 - (FOF_MULTIDESTFILES | FOF_FILESONLY)); */
850 /* OFl ^= (FOF_SILENT | FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS | FOF_NOCONFIRMMKDIR); */
851 OFl
&= (~(FOF_MULTIDESTFILES
| FOF_NOCONFIRMATION
| FOF_FILESONLY
)); /* implemented */
852 OFl
^= (FOF_SILENT
| FOF_NOCONFIRMMKDIR
| FOF_NOERRORUI
| FOF_NOCOPYSECURITYATTRIBS
); /* ignored, if one */
853 OFl
&= (~FOF_SIMPLEPROGRESS
); /* ignored, only with FOF_SILENT */
856 if (OFl
& (~(FOF_CONFIRMMOUSE
| FOF_SILENT
| FOF_RENAMEONCOLLISION
|
857 FOF_NOCONFIRMMKDIR
| FOF_NOERRORUI
| FOF_NOCOPYSECURITYATTRIBS
)))
859 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not implemented, Aborted=TRUE, stub\n",
860 debug_shfileops_action(FuncSwitch
), level
, OFl
);
861 retCode
= 0x403; /* 1027, we need an extension to shlfileop */
866 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not fully implemented, stub\n",
867 debug_shfileops_action(FuncSwitch
), level
, OFl
);
871 if ((pNextFrom
) && (!(b_MultiTo
) || (pNextTo
)))
873 nFileOp
.pFrom
= pTempFrom
= HeapAlloc(GetProcessHeap(), 0, ((1 + 2 * (b_MultiTo
)) * MAX_PATH
+ 6) * sizeof(WCHAR
));
876 retCode
= ERROR_OUTOFMEMORY
;
877 SetLastError(retCode
);
881 pTempTo
= &pTempFrom
[MAX_PATH
+ 4];
882 nFileOp
.pTo
= pTempTo
;
883 ask_overwrite
= (!(nFileOp
.fFlags
& FOF_NOCONFIRMATION
) && !(nFileOp
.fFlags
& FOF_RENAMEONCOLLISION
));
884 not_overwrite
= (!(nFileOp
.fFlags
& FOF_NOCONFIRMATION
) || (nFileOp
.fFlags
& FOF_RENAMEONCOLLISION
));
888 retCode
= 0x402; /* 1026 */
891 /* need break at error before change sourcepointer */
892 while(!nFileOp
.fAnyOperationsAborted
&& (pNextFrom
[0]))
894 nFileOp
.wFunc
= ((level
+ 1) << 4) + FuncSwitch
;
895 nFileOp
.fFlags
= lpFileOp
->fFlags
;
900 pNextTo
= &pNextTo
[lstrlenW(pTo
)+1];
901 b_MultiTo
= (b_Multi
&& pNextTo
[0]);
905 pNextFrom
= &pNextFrom
[lstrlenW(pNextFrom
)+1];
906 if (!b_MultiFrom
&& !b_MultiTo
)
907 b_MultiFrom
= (pNextFrom
[0]);
909 pFromFile
= SHFileStrCpyCatW(pTempFrom
, pFrom
, NULL
);
913 pToFile
= SHFileStrCpyCatW(pTempTo
, pTo
, NULL
);
918 SHELL_FileNamesMatch(lpFileOp
->pFrom
, lpFileOp
->pTo
, (!b_Multi
|| b_MultiFrom
));
920 if (!(b_MultiPaired
) || !(pFromFile
) || !(pFromFile
[1]) || ((pTo
) && !(pToFile
)))
922 retCode
= 0x402; /* 1026 */
927 b_ToTailSlash
= (!pToFile
[1]);
931 if (StrChrW(pTempTo
,'\\'))
933 pToFile
= SHFileStrCpyCatW(pTempTo
, NULL
, NULL
);
936 b_ToInvalidTail
= (NULL
!= StrPBrkW(&pToFile
[1], wWildcardChars
));
940 b_Mask
= (NULL
!= StrPBrkW(&pFromFile
[1], wWildcardChars
));
941 if (FO_RENAME
== FuncSwitch
)
943 /* temporary only for FO_RENAME */
944 /* ??? b_Mask = (NULL != strrbrk(pFrom,"*?")); */
945 if (b_MultiTo
|| b_MultiFrom
|| (b_Mask
&& !b_ToInvalidTail
))
947 /* no work, only RC=0 */
948 /* ??? nFileOp.fAnyOperationsAborted = TRUE; */
949 /*#define W98_FO_RENEME */
951 goto shfileop_normal
;
953 retCode
= 0x1; /* 1 value unknown, W98 returns no error */
958 hFind
= FindFirstFileW(pFrom
, &wfd
);
959 if (INVALID_HANDLE_VALUE
== hFind
)
961 if ((FO_DELETE
== FuncSwitch
) && (b_Mask
))
964 FromPathAttr
= GetFileAttributesW(pTempFrom
);
966 if (IsAttribDir(FromPathAttr
))
968 /* FO_DELETE with mask and without found is valid */
969 goto shfileop_normal
;
972 /* root (without mask) is also not allowed as source, tested in W98 */
973 retCode
= 0x402; /* 1026 */
978 #define HIGH_ADR (LPWSTR)0xffffffff
980 /* ??? b_Mask = (!SHFileStrICmpA(&pFromFile[1], &wfd.cFileName[0], HIGH_ADR, HIGH_ADR)); */
981 if (!pTo
) /* FO_DELETE */
985 lpFileName
= wfd
.cAlternateFileName
;
987 lpFileName
= wfd
.cFileName
;
988 if (IsDotDir(lpFileName
) ||
989 ((b_Mask
) && IsAttribDir(wfd
.dwFileAttributes
) && (nFileOp
.fFlags
& FOF_FILESONLY
)))
991 SHFileStrCpyCatW(&pFromFile
[1], lpFileName
, NULL
);
992 /* TODO: Check the SHELL_DeleteFileOrDirectoryW() function in shell32.dll */
993 if (IsAttribFile(wfd
.dwFileAttributes
))
995 nFileOp
.fAnyOperationsAborted
= (SHNotifyDeleteFileW(pTempFrom
) != ERROR_SUCCESS
);
996 retCode
= 0x78; /* value unknown */
1000 nFileOp
.fAnyOperationsAborted
= (!SHELL_DeleteDirectoryW(pTempFrom
, (!(nFileOp
.fFlags
& FOF_NOCONFIRMATION
))));
1001 retCode
= 0x79; /* value unknown */
1003 } while (!nFileOp
.fAnyOperationsAborted
&& FindNextFileW(hFind
, &wfd
));
1005 hFind
= INVALID_HANDLE_VALUE
;
1006 if (nFileOp
.fAnyOperationsAborted
)
1008 goto shfileop_error
;
1011 } /* FO_DELETE ends, pTo must be always valid from here */
1013 b_SameRoot
= (toupperW(pTempFrom
[0]) == toupperW(pTempTo
[0]));
1014 b_SameTailName
= SHFileStrICmpW(pToFile
, pFromFile
, NULL
, NULL
);
1016 ToPathAttr
= ToAttr
= GetFileAttributesW(pTempTo
);
1017 if (!b_Mask
&& (ToAttr
== -1) && (pToFile
))
1020 ToPathAttr
= GetFileAttributesW(pTempTo
);
1024 if (FO_RENAME
== FuncSwitch
)
1026 if (!b_SameRoot
|| b_Mask
/* FO_RENAME works not with Mask */
1027 || !SHFileStrICmpW(pTempFrom
, pTempTo
, pFromFile
, NULL
)
1028 || (SHFileStrICmpW(pTempFrom
, pTempTo
, pFromFile
, HIGH_ADR
) && !b_ToTailSlash
))
1031 goto shfileop_error
;
1033 if (b_ToInvalidTail
)
1036 goto shfileop_error
;
1038 if (-1 == ToPathAttr
)
1041 goto shfileop_error
;
1043 if (IsAttribDir(wfd
.dwFileAttributes
) && IsAttribDir(ToAttr
))
1045 retCode
= (b_ToTailSlash
) ? 0xb7 : 0x7b;
1046 goto shfileop_error
;
1048 /* we use SHNotifyMoveFile() instead MoveFileW */
1049 if (SHNotifyMoveFileW(pTempFrom
, pTempTo
, nFileOp
.fFlags
& FOF_RENAMEONCOLLISION
) != ERROR_SUCCESS
)
1051 /* we need still the value for the returncode, we use the mostly assumed */
1053 goto shfileop_error
;
1055 goto shfileop_normal
;
1058 /* W98 Bug with FO_MOVE different to FO_COPY, better the same as FO_COPY */
1059 b_ToValid
= ((b_SameTailName
&& b_SameRoot
&& (FO_COPY
== FuncSwitch
)) ||
1060 (b_SameTailName
&& !b_SameRoot
) || (b_ToInvalidTail
));
1062 /* handle mask in source */
1065 if (!IsAttribDir(ToAttr
))
1067 retCode
= (b_ToInvalidTail
&&/* b_SameTailName &&*/ (FO_MOVE
== FuncSwitch
)) \
1069 goto shfileop_error
;
1071 pToFile
= SHFileStrCpyCatW(pTempTo
, NULL
, wBackslash
);
1072 nFileOp
.fFlags
= (nFileOp
.fFlags
| FOF_MULTIDESTFILES
);
1075 lpFileName
= wfd
.cAlternateFileName
;
1077 lpFileName
= wfd
.cFileName
;
1078 if (IsDotDir(lpFileName
) ||
1079 (IsAttribDir(wfd
.dwFileAttributes
) && (nFileOp
.fFlags
& FOF_FILESONLY
)))
1080 continue; /* next name in pTempFrom(dir) */
1081 SHFileStrCpyCatW(&pToFile
[1], lpFileName
, NULL
);
1082 SHFileStrCpyCatW(&pFromFile
[1], lpFileName
, NULL
);
1083 retCode
= SHFileOperationW (&nFileOp
);
1084 } while(!nFileOp
.fAnyOperationsAborted
&& FindNextFileW(hFind
, &wfd
));
1087 hFind
= INVALID_HANDLE_VALUE
;
1088 /* FO_COPY/FO_MOVE with mask, FO_DELETE and FO_RENAME are solved */
1092 /* only FO_COPY/FO_MOVE without mask, all others are (must be) solved */
1093 if (IsAttribDir(wfd
.dwFileAttributes
) && (ToAttr
== -1))
1098 ToPathAttr
= GetFileAttributesW(pTempTo
);
1099 if ((ToPathAttr
== -1) && b_ToValid
)
1101 /* create dir must be here, sample target D:\y\ *.* create with RC=10003 */
1102 if (SHCreateDirectoryExW(NULL
, pTempTo
, NULL
))
1104 retCode
= 0x73;/* value unknown */
1105 goto shfileop_error
;
1107 ToPathAttr
= GetFileAttributesW(pTempTo
);
1110 if (b_ToInvalidTail
)
1113 goto shfileop_error
;
1118 /* trailing BackSlash is ever removed and pToFile points to BackSlash before */
1119 if (!b_MultiTo
&& (b_MultiFrom
|| (!(b_Multi
) && IsAttribDir(ToAttr
))))
1121 if ((FO_MOVE
== FuncSwitch
) && IsAttribDir(ToAttr
) && IsAttribDir(wfd
.dwFileAttributes
))
1125 retCode
= 0x73; /* !b_Multi = 0x8 ?? */
1126 goto shfileop_error
;
1129 pToFile
= SHFileStrCpyCatW(pTempTo
, NULL
, wfd
.cFileName
);
1130 ToAttr
= GetFileAttributesW(pTempTo
);
1133 if (IsAttribDir(ToAttr
))
1135 if (IsAttribFile(wfd
.dwFileAttributes
))
1137 retCode
= (FO_COPY
== FuncSwitch
) ? 0x75 : 0xb7;
1138 goto shfileop_error
;
1144 ToPathAttr
= GetFileAttributesW(pTempTo
);
1146 if (IsAttribFile(ToPathAttr
))
1148 /* error, is this tested ? */
1150 goto shfileop_error
;
1154 /* singlesource + no mask */
1155 if (-1 == (ToAttr
& ToPathAttr
))
1157 /* Target-dir does not exist, and cannot be created */
1159 goto shfileop_error
;
1166 if ((ToAttr
== -1) && SHFileStrICmpW(pTempFrom
, pTempTo
, pFromFile
, NULL
))
1168 nFileOp
.wFunc
= ((level
+1)<<4) + FO_RENAME
;
1172 if (b_SameRoot
&& IsAttribDir(ToAttr
) && IsAttribDir(wfd
.dwFileAttributes
))
1174 /* we need pToFile for FO_DELETE after FO_MOVE contence */
1175 pToFile
= SHFileStrCpyCatW(pTempFrom
, NULL
, wWildcardFile
);
1179 nFileOp
.wFunc
= ((level
+1)<<4) + FO_COPY
;
1182 retCode
= SHFileOperationW(&nFileOp
);
1184 ((DWORD
*)pToFile
)[0] = '\0';
1185 if (!nFileOp
.fAnyOperationsAborted
&& (FO_RENAME
!= (nFileOp
.wFunc
& 0xf)))
1187 nFileOp
.wFunc
= ((level
+1)<<4) + FO_DELETE
;
1188 retCode
= SHFileOperationW(&nFileOp
);
1192 if (SHFileStrICmpW(pTempFrom
, pTempTo
, NULL
, NULL
))
1193 { /* target is the same as source ? */
1194 /* we still need the value for the returncode, we assume 0x71 */
1196 goto shfileop_error
;
1198 if (IsAttribDir((ToAttr
& wfd
.dwFileAttributes
)))
1200 if (IsAttribDir(ToAttr
) || !SHCreateDirectoryExW(NULL
,pTempTo
, NULL
))
1202 /* ??? nFileOp.fFlags = (nFileOp.fFlags | FOF_MULTIDESTFILES); */
1203 SHFileStrCpyCatW(pTempFrom
, NULL
, wWildcardFile
);
1204 retCode
= SHFileOperationW(&nFileOp
);
1208 retCode
= 0x750;/* value unknown */
1209 goto shfileop_error
;
1214 if (!(ask_overwrite
&& SHELL_ConfirmDialogW(ASK_OVERWRITE_FILE
, pTempTo
))
1217 /* we still need the value for the returncode, we use the mostly assumed */
1219 goto shfileop_error
;
1221 if (SHNotifyCopyFileW(pTempFrom
, pTempTo
, nFileOp
.fFlags
& FOF_RENAMEONCOLLISION
) != ERROR_SUCCESS
)
1223 retCode
= 0x77; /* value unknown */
1224 goto shfileop_error
;
1231 if (!(nFileOp
.fAnyOperationsAborted
))
1234 if (hFind
!= INVALID_HANDLE_VALUE
)
1236 hFind
= INVALID_HANDLE_VALUE
;
1238 HeapFree(GetProcessHeap(), 0, pTempFrom
);
1241 nFileOp
.fAnyOperationsAborted
= TRUE
;
1243 TRACE("%s level=%ld AnyOpsAborted=%s ret=0x%lx, with %s %s%s\n",
1244 debug_shfileops_action(FuncSwitch
), level
,
1245 nFileOp
.fAnyOperationsAborted
? "TRUE":"FALSE",
1246 retCode
, debugstr_w(pFrom
), pTo
? "-> ":"", debugstr_w(pTo
));
1248 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
1252 /*************************************************************************
1253 * SHFileOperation [SHELL32.@]
1256 DWORD WINAPI
SHFileOperationAW(LPVOID lpFileOp
)
1258 if (SHELL_OsIsUnicode())
1259 return SHFileOperationW(lpFileOp
);
1260 return SHFileOperationA(lpFileOp
);
1263 /*************************************************************************
1264 * SheGetDirW [SHELL32.281]
1267 HRESULT WINAPI
SheGetDirW(LPWSTR u
, LPWSTR v
)
1268 { FIXME("%p %p stub\n",u
,v
);
1272 /*************************************************************************
1273 * SheChangeDirW [SHELL32.274]
1276 HRESULT WINAPI
SheChangeDirW(LPWSTR u
)
1277 { FIXME("(%s),stub\n",debugstr_w(u
));
1281 /*************************************************************************
1282 * IsNetDrive [SHELL32.66]
1284 BOOL WINAPI
IsNetDrive(DWORD drive
)
1287 strcpy(root
, "A:\\");
1288 root
[0] += (char)drive
;
1289 return (GetDriveTypeA(root
) == DRIVE_REMOTE
);