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 BOOL
SHNotifyCreateDirectoryA(LPCSTR path
, LPSECURITY_ATTRIBUTES sec
);
56 static BOOL
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
);
57 static BOOL
SHNotifyRemoveDirectoryA(LPCSTR path
);
58 static BOOL
SHNotifyRemoveDirectoryW(LPCWSTR path
);
59 static BOOL
SHNotifyDeleteFileA(LPCSTR path
);
60 static BOOL
SHNotifyDeleteFileW(LPCWSTR path
);
61 static BOOL
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
);
62 static BOOL
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
);
160 } while (ret
&& FindNextFileA(hFind
, &wfd
));
164 ret
= SHNotifyRemoveDirectoryA(pszDir
);
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
);
195 } while (ret
&& FindNextFileW(hFind
, &wfd
));
199 ret
= SHNotifyRemoveDirectoryW(pszDir
);
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
);
214 BOOL
SHELL_DeleteFileW(LPCWSTR pszFile
, BOOL bShowUI
)
216 if (bShowUI
&& !SHELL_ConfirmDialogW(ASK_DELETE_FILE
, pszFile
))
219 return SHNotifyDeleteFileW(pszFile
);
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 BOOL
SHNotifyCreateDirectoryA(LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
240 TRACE("(%s, %p)\n", debugstr_a(path
), sec
);
242 ret
= CreateDirectoryA(path
, sec
);
245 SHChangeNotify(SHCNE_MKDIR
, SHCNF_PATHA
, path
, NULL
);
250 static BOOL
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
253 TRACE("(%s, %p)\n", debugstr_w(path
), sec
);
255 ret
= CreateDirectoryW(path
, sec
);
258 SHChangeNotify(SHCNE_MKDIR
, SHCNF_PATHW
, path
, NULL
);
263 BOOL WINAPI
Win32CreateDirectoryAW(LPCVOID path
, LPSECURITY_ATTRIBUTES sec
)
265 if (SHELL_OsIsUnicode())
266 return SHNotifyCreateDirectoryW(path
, sec
);
267 return SHNotifyCreateDirectoryA(path
, sec
);
270 /************************************************************************
271 * Win32RemoveDirectory [SHELL32.94]
273 * Deletes a directory. Also triggers a change notify if one exists.
276 * path [I] path to directory to delete
279 * TRUE if successful, FALSE otherwise
282 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
283 * This is Unicode on NT/2000
285 static BOOL
SHNotifyRemoveDirectoryA(LPCSTR path
)
288 TRACE("(%s)\n", debugstr_a(path
));
290 ret
= RemoveDirectoryA(path
);
293 /* Directory may be write protected */
294 DWORD dwAttr
= GetFileAttributesA(path
);
295 if (dwAttr
!= -1 && dwAttr
& FILE_ATTRIBUTE_READONLY
)
296 if (SetFileAttributesA(path
, dwAttr
& ~FILE_ATTRIBUTE_READONLY
))
297 ret
= RemoveDirectoryA(path
);
300 SHChangeNotify(SHCNE_RMDIR
, SHCNF_PATHA
, path
, NULL
);
304 static BOOL
SHNotifyRemoveDirectoryW(LPCWSTR path
)
307 TRACE("(%s)\n", debugstr_w(path
));
309 ret
= RemoveDirectoryW(path
);
312 /* Directory may be write protected */
313 DWORD dwAttr
= GetFileAttributesW(path
);
314 if (dwAttr
!= -1 && dwAttr
& FILE_ATTRIBUTE_READONLY
)
315 if (SetFileAttributesW(path
, dwAttr
& ~FILE_ATTRIBUTE_READONLY
))
316 ret
= RemoveDirectoryW(path
);
319 SHChangeNotify(SHCNE_RMDIR
, SHCNF_PATHW
, path
, NULL
);
323 BOOL WINAPI
Win32RemoveDirectoryAW(LPCVOID path
)
325 if (SHELL_OsIsUnicode())
326 return SHNotifyRemoveDirectoryW(path
);
327 return SHNotifyRemoveDirectoryA(path
);
330 /************************************************************************
331 * Win32DeleteFile [SHELL32.164]
333 * Deletes a file. Also triggers a change notify if one exists.
336 * path [I] path to file to delete
339 * TRUE if successful, FALSE otherwise
342 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
343 * This is Unicode on NT/2000
345 static BOOL
SHNotifyDeleteFileA(LPCSTR path
)
349 TRACE("(%s)\n", debugstr_a(path
));
351 ret
= DeleteFileA(path
);
354 /* File may be write protected or a system file */
355 DWORD dwAttr
= GetFileAttributesA(path
);
356 if ((dwAttr
!= -1) && (dwAttr
& (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
357 if (SetFileAttributesA(path
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
358 ret
= DeleteFileA(path
);
361 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHA
, path
, NULL
);
365 static BOOL
SHNotifyDeleteFileW(LPCWSTR path
)
369 TRACE("(%s)\n", debugstr_w(path
));
371 ret
= DeleteFileW(path
);
374 /* File may be write protected or a system file */
375 DWORD dwAttr
= GetFileAttributesW(path
);
376 if ((dwAttr
!= -1) && (dwAttr
& (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
377 if (SetFileAttributesW(path
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
378 ret
= DeleteFileW(path
);
381 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, path
, NULL
);
385 DWORD WINAPI
Win32DeleteFileAW(LPCVOID path
)
387 if (SHELL_OsIsUnicode())
388 return SHNotifyDeleteFileW(path
);
389 return SHNotifyDeleteFileA(path
);
392 /************************************************************************
393 * SHNotifyMoveFile [internal]
395 * Moves a file. Also triggers a change notify if one exists.
398 * src [I] path to source file to move
399 * dest [I] path to target file to move to
402 * TRUE if successful, FALSE otherwise
404 static BOOL
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
)
408 TRACE("(%s %s)\n", debugstr_w(src
), debugstr_w(dest
));
410 ret
= MoveFileW(src
, dest
);
413 /* Source file may be write protected or a system file */
414 DWORD dwAttr
= GetFileAttributesW(src
);
415 if ((dwAttr
!= -1) && (dwAttr
& (FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
416 if (SetFileAttributesW(src
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
417 ret
= MoveFileW(src
, dest
);
420 SHChangeNotify(SHCNE_RENAMEITEM
, SHCNF_PATHW
, src
, dest
);
424 /************************************************************************
425 * SHNotifyCopyFile [internal]
427 * Copies a file. Also triggers a change notify if one exists.
430 * src [I] path to source file to move
431 * dest [I] path to target file to move to
432 * bRename [I] if TRUE, the target file will be renamed if a
433 * file with this name already exists
436 * TRUE if successful, FALSE otherwise
438 static BOOL
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bRename
)
442 TRACE("(%s %s %s)\n", debugstr_w(src
), debugstr_w(dest
), bRename
? "renameIfExists" : "");
444 ret
= CopyFileW(src
, dest
, TRUE
);
447 /* Destination file probably exists */
448 DWORD dwAttr
= GetFileAttributesW(dest
);
451 FIXME("Rename on copy to existing file not implemented!");
455 SHChangeNotify(SHCNE_CREATE
, SHCNF_PATHW
, dest
, NULL
);
459 /*************************************************************************
460 * SHCreateDirectory [SHELL32.165]
462 * Create a directory at the specified location
466 * path [I] path of directory to create
469 * ERRROR_SUCCESS or one of the following values:
470 * ERROR_BAD_PATHNAME if the path is relative
471 * ERROR_FILE_EXISTS when a file with that name exists
472 * ERROR_ALREADY_EXISTS when the directory already exists
473 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
476 * exported by ordinal
478 * WinNT/2000 exports Unicode
480 DWORD WINAPI
SHCreateDirectory(HWND hWnd
, LPCVOID path
)
482 if (SHELL_OsIsUnicode())
483 return SHCreateDirectoryExW(hWnd
, path
, NULL
);
484 return SHCreateDirectoryExA(hWnd
, path
, NULL
);
487 /*************************************************************************
488 * SHCreateDirectoryExA [SHELL32.@]
490 * Create a directory at the specified location
494 * path [I] path of directory to create
495 * sec [I] security attributes to use or NULL
498 * ERRROR_SUCCESS or one of the following values:
499 * ERROR_BAD_PATHNAME if the path is relative
500 * ERROR_FILE_EXISTS when a file with that name exists
501 * ERROR_ALREADY_EXISTS when the directory already exists
502 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
504 DWORD WINAPI
SHCreateDirectoryExA(HWND hWnd
, LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
506 WCHAR wPath
[MAX_PATH
];
507 TRACE("(%p, %s, %p)\n",hWnd
, debugstr_a(path
), sec
);
509 MultiByteToWideChar(CP_ACP
, 0, path
, -1, wPath
, MAX_PATH
);
510 return SHCreateDirectoryExW(hWnd
, wPath
, sec
);
513 /*************************************************************************
514 * SHCreateDirectoryExW [SHELL32.@]
516 DWORD WINAPI
SHCreateDirectoryExW(HWND hWnd
, LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
518 DWORD ret
= ERROR_SUCCESS
;
519 TRACE("(%p, %s, %p)\n",hWnd
, debugstr_w(path
), sec
);
521 if (PathIsRelativeW(path
))
523 ret
= ERROR_BAD_PATHNAME
;
524 SetLastError(ERROR_BAD_PATHNAME
);
528 if (!SHNotifyCreateDirectoryW(path
, sec
))
530 ret
= GetLastError();
531 if (ret
!= ERROR_FILE_EXISTS
&&
532 ret
!= ERROR_ALREADY_EXISTS
&&
533 ret
!= ERROR_FILENAME_EXCED_RANGE
)
535 /* handling network file names?
536 lstrcpynW(pathName, path, MAX_PATH);
537 lpStr = PathAddBackslashW(pathName);*/
538 FIXME("Semi-stub, non zero hWnd should be used somehow?");
545 /*************************************************************************
547 * SHFileStrICmp HelperFunction for SHFileOperationW
550 BOOL
SHFileStrICmpW(LPWSTR p1
, LPWSTR p2
, LPWSTR p1End
, LPWSTR p2End
)
555 int i_len1
= lstrlenW(p1
);
556 int i_len2
= lstrlenW(p2
);
558 if (p1End
&& (&p1
[i_len1
] >= p1End
) && ('\\' == p1End
[0]))
562 i_len1
= lstrlenW(p1
);
566 if ((&p2
[i_len2
] >= p2End
) && ('\\' == p2End
[0]))
575 if ((i_len1
<= i_len2
) && ('\\' == p2
[i_len1
]))
582 i_len2
= lstrlenW(p2
);
583 if (i_len1
== i_len2
)
584 i_Temp
= lstrcmpiW(p1
,p2
);
592 /*************************************************************************
594 * SHFileStrCpyCat HelperFunction for SHFileOperationW
597 LPWSTR
SHFileStrCpyCatW(LPWSTR pTo
, LPCWSTR pFrom
, LPCWSTR pCatStr
)
599 LPWSTR pToFile
= NULL
;
604 lstrcpyW(pTo
, pFrom
);
607 i_len
= lstrlenW(pTo
);
608 if ((i_len
) && (pTo
[--i_len
] != '\\'))
611 if (pCatStr
[0] == '\\')
613 lstrcpyW(&pTo
[i_len
+1], pCatStr
);
615 pToFile
= StrRChrW(pTo
,NULL
,'\\');
616 /* termination of the new string-group */
617 pTo
[(lstrlenW(pTo
)) + 1] = '\0';
622 /**************************************************************************
623 * SHELL_FileNamesMatch()
625 * Accepts two \0 delimited lists of the file names. Checks whether number of
626 * files in both lists is the same, and checks also if source-name exists.
628 BOOL
SHELL_FileNamesMatch(LPCWSTR pszFiles1
, LPCWSTR pszFiles2
, BOOL bOnlySrc
)
630 while ((pszFiles1
[0] != '\0') &&
631 (bOnlySrc
|| (pszFiles2
[0] != '\0')))
633 if (NULL
== StrPBrkW(pszFiles1
, wWildcardChars
))
635 if (-1 == GetFileAttributesW(pszFiles1
))
638 pszFiles1
+= lstrlenW(pszFiles1
) + 1;
640 pszFiles2
+= lstrlenW(pszFiles2
) + 1;
642 return ((pszFiles1
[0] == '\0') && (bOnlySrc
|| (pszFiles2
[0] == '\0')));
645 /*************************************************************************
647 * SHNameTranslate HelperFunction for SHFileOperationA
649 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
650 * is NULL, only the necessary size of the string is determined and returned,
651 * otherwise the ASCII strings are copied into it and the buffer is increased
652 * to point to the location after the final 0 termination char.
654 DWORD
SHNameTranslate(LPWSTR
* wString
, LPCWSTR
* pWToFrom
, BOOL more
)
656 DWORD size
= 0, aSize
= 0;
657 LPCSTR aString
= (LPCSTR
)*pWToFrom
;
663 size
= lstrlenA(aString
) + 1;
666 } while ((size
!= 1) && more
);
667 /* The two sizes might be different in the case of multibyte chars */
668 size
= MultiByteToWideChar(CP_ACP
, 0, aString
, aSize
, *wString
, 0);
669 if (*wString
) /* only in the second loop */
671 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, size
);
672 *pWToFrom
= *wString
;
678 /*************************************************************************
679 * SHFileOperationA [SHELL32.@]
681 * Function to copy, move, delete and create one or more files with optional
685 * lpFileOp [I/O] pointer to a structure containing all the necessary information
690 DWORD WINAPI
SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp
)
692 SHFILEOPSTRUCTW nFileOp
= *((LPSHFILEOPSTRUCTW
)lpFileOp
);
693 DWORD retCode
= 0, size
;
694 LPWSTR ForFree
= NULL
, /* we change wString in SHNameTranslate and can't use it for freeing */
695 wString
= NULL
; /* we change this in SHNameTranslate */
698 if (FO_DELETE
== (nFileOp
.wFunc
& FO_MASK
))
699 nFileOp
.pTo
= NULL
; /* we need a NULL or a valid pointer for translation */
700 if (!(nFileOp
.fFlags
& FOF_SIMPLEPROGRESS
))
701 nFileOp
.lpszProgressTitle
= NULL
; /* we need a NULL or a valid pointer for translation */
702 while (1) /* every loop calculate size, second translate also, if we have storage for this */
704 size
= SHNameTranslate(&wString
, &nFileOp
.lpszProgressTitle
, FALSE
); /* no loop */
705 size
+= SHNameTranslate(&wString
, &nFileOp
.pFrom
, TRUE
); /* internal loop */
706 size
+= SHNameTranslate(&wString
, &nFileOp
.pTo
, TRUE
); /* internal loop */
710 retCode
= SHFileOperationW(&nFileOp
);
711 HeapFree(GetProcessHeap(), 0, ForFree
); /* we can not use wString, it was changed */
716 wString
= ForFree
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
717 if (ForFree
) continue;
718 retCode
= ERROR_OUTOFMEMORY
;
719 nFileOp
.fAnyOperationsAborted
= TRUE
;
720 SetLastError(retCode
);
725 lpFileOp
->hNameMappings
= nFileOp
.hNameMappings
;
726 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
730 static const char * debug_shfileops_flags( DWORD fFlags
)
732 return wine_dbg_sprintf( "%s%s%s%s%s%s%s%s%s%s%s%s%s",
733 fFlags
& FOF_MULTIDESTFILES
? "FOF_MULTIDESTFILES " : "",
734 fFlags
& FOF_CONFIRMMOUSE
? "FOF_CONFIRMMOUSE " : "",
735 fFlags
& FOF_SILENT
? "FOF_SILENT " : "",
736 fFlags
& FOF_RENAMEONCOLLISION
? "FOF_RENAMEONCOLLISION " : "",
737 fFlags
& FOF_NOCONFIRMATION
? "FOF_NOCONFIRMATION " : "",
738 fFlags
& FOF_WANTMAPPINGHANDLE
? "FOF_WANTMAPPINGHANDLE " : "",
739 fFlags
& FOF_ALLOWUNDO
? "FOF_ALLOWUNDO " : "",
740 fFlags
& FOF_FILESONLY
? "FOF_FILESONLY " : "",
741 fFlags
& FOF_SIMPLEPROGRESS
? "FOF_SIMPLEPROGRESS " : "",
742 fFlags
& FOF_NOCONFIRMMKDIR
? "FOF_NOCONFIRMMKDIR " : "",
743 fFlags
& FOF_NOERRORUI
? "FOF_NOERRORUI " : "",
744 fFlags
& FOF_NOCOPYSECURITYATTRIBS
? "FOF_NOCOPYSECURITYATTRIBS" : "",
745 fFlags
& 0xf000 ? "MORE-UNKNOWN-Flags" : "");
748 static const char * debug_shfileops_action( DWORD op
)
750 LPCSTR cFO_Name
[] = {"FO_????","FO_MOVE","FO_COPY","FO_DELETE","FO_RENAME"};
751 return wine_dbg_sprintf("%s", cFO_Name
[ op
]);
754 /*************************************************************************
755 * SHFileOperationW [SHELL32.@]
757 * See SHFileOperationA
759 DWORD WINAPI
SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp
)
761 SHFILEOPSTRUCTW nFileOp
= *(lpFileOp
);
763 LPCWSTR pNextFrom
= nFileOp
.pFrom
;
764 LPCWSTR pNextTo
= nFileOp
.pTo
;
765 LPCWSTR pFrom
= pNextFrom
;
767 HANDLE hFind
= INVALID_HANDLE_VALUE
;
768 WIN32_FIND_DATAW wfd
;
769 LPWSTR pTempFrom
= NULL
;
770 LPWSTR pTempTo
= NULL
;
772 LPWSTR pToFile
= NULL
;
778 FILEOP_FLAGS OFl
= ((FILEOP_FLAGS
)lpFileOp
->fFlags
& 0xfff);
780 BOOL b_Multi
= (nFileOp
.fFlags
& FOF_MULTIDESTFILES
);
782 BOOL b_MultiTo
= (FO_DELETE
!= (lpFileOp
->wFunc
& FO_MASK
));
783 BOOL b_MultiPaired
= (!b_MultiTo
);
784 BOOL b_MultiFrom
= FALSE
;
789 BOOL b_ToInvalidTail
= FALSE
;
790 BOOL b_ToValid
; /* for W98-Bug for FO_MOVE with source and target in same rootdrive */
792 BOOL b_ToTailSlash
= FALSE
;
794 long FuncSwitch
= (nFileOp
.wFunc
& FO_MASK
);
795 long level
= nFileOp
.wFunc
>>4;
797 /* default no error */
798 nFileOp
.fAnyOperationsAborted
= FALSE
;
800 if ((FuncSwitch
< FO_MOVE
) || (FuncSwitch
> FO_RENAME
))
801 goto shfileop_normal
; /* no valid FunctionCode */
804 TRACE("%s: flags (0x%04x) : %s\n",
805 debug_shfileops_action(FuncSwitch
), nFileOp
.fFlags
,
806 debug_shfileops_flags(nFileOp
.fFlags
) );
808 /* establish when pTo is interpreted as the name of the destination file
809 * or the directory where the Fromfile should be copied to.
811 * (1) pTo points to the name of an existing directory;
812 * (2) the flag FOF_MULTIDESTFILES is present;
813 * (3) whether pFrom point to multiple filenames.
817 * destisdir 1 1 1 1 0 0 0 0
818 * FOF_MULTIDESTFILES 1 1 0 0 1 1 0 0
819 * multiple from filenames 1 0 1 0 1 0 1 0
821 * copy files to dir 1 0 1 1 0 0 1 0
822 * create dir 0 0 0 0 0 0 1 0
825 * FOF_MULTIDESTFILES, FOF_NOCONFIRMATION, FOF_FILESONLY are implemented
826 * FOF_CONFIRMMOUSE, FOF_SILENT, FOF_NOCONFIRMMKDIR,
827 * FOF_SIMPLEPROGRESS, FOF_NOCOPYSECURITYATTRIBS are not implemented and ignored
828 * FOF_RENAMEONCOLLISION are implemented partially and breaks if file exist
829 * FOF_ALLOWUNDO, FOF_WANTMAPPINGHANDLE are not implemented and breaks
830 * if any other flag set, an error occurs
832 TRACE("%s level=%ld nFileOp.fFlags=0x%x\n",
833 debug_shfileops_action(FuncSwitch
), level
, lpFileOp
->fFlags
);
835 /* OFl &= (-1 - (FOF_MULTIDESTFILES | FOF_FILESONLY)); */
836 /* OFl ^= (FOF_SILENT | FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS | FOF_NOCONFIRMMKDIR); */
837 OFl
&= (~(FOF_MULTIDESTFILES
| FOF_NOCONFIRMATION
| FOF_FILESONLY
)); /* implemented */
838 OFl
^= (FOF_SILENT
| FOF_NOCONFIRMMKDIR
| FOF_NOERRORUI
| FOF_NOCOPYSECURITYATTRIBS
); /* ignored, if one */
839 OFl
&= (~FOF_SIMPLEPROGRESS
); /* ignored, only with FOF_SILENT */
842 if (OFl
& (~(FOF_CONFIRMMOUSE
| FOF_SILENT
| FOF_RENAMEONCOLLISION
|
843 FOF_NOCONFIRMMKDIR
| FOF_NOERRORUI
| FOF_NOCOPYSECURITYATTRIBS
)))
845 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not implemented, Aborted=TRUE, stub\n",
846 debug_shfileops_action(FuncSwitch
), level
, OFl
);
847 retCode
= 0x403; /* 1027, we need an extension to shlfileop */
852 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not fully implemented, stub\n",
853 debug_shfileops_action(FuncSwitch
), level
, OFl
);
857 if ((pNextFrom
) && (!(b_MultiTo
) || (pNextTo
)))
859 nFileOp
.pFrom
= pTempFrom
= HeapAlloc(GetProcessHeap(), 0, ((1 + 2 * (b_MultiTo
)) * MAX_PATH
+ 6) * sizeof(WCHAR
));
862 retCode
= ERROR_OUTOFMEMORY
;
863 SetLastError(retCode
);
867 pTempTo
= &pTempFrom
[MAX_PATH
+ 4];
868 nFileOp
.pTo
= pTempTo
;
869 ask_overwrite
= (!(nFileOp
.fFlags
& FOF_NOCONFIRMATION
) && !(nFileOp
.fFlags
& FOF_RENAMEONCOLLISION
));
870 not_overwrite
= (!(nFileOp
.fFlags
& FOF_NOCONFIRMATION
) || (nFileOp
.fFlags
& FOF_RENAMEONCOLLISION
));
874 retCode
= 0x402; /* 1026 */
877 /* need break at error before change sourcepointer */
878 while(!nFileOp
.fAnyOperationsAborted
&& (pNextFrom
[0]))
880 nFileOp
.wFunc
= ((level
+ 1) << 4) + FuncSwitch
;
881 nFileOp
.fFlags
= lpFileOp
->fFlags
;
886 pNextTo
= &pNextTo
[lstrlenW(pTo
)+1];
887 b_MultiTo
= (b_Multi
&& pNextTo
[0]);
891 pNextFrom
= &pNextFrom
[lstrlenW(pNextFrom
)+1];
892 if (!b_MultiFrom
&& !b_MultiTo
)
893 b_MultiFrom
= (pNextFrom
[0]);
895 pFromFile
= SHFileStrCpyCatW(pTempFrom
, pFrom
, NULL
);
899 pToFile
= SHFileStrCpyCatW(pTempTo
, pTo
, NULL
);
904 SHELL_FileNamesMatch(lpFileOp
->pFrom
, lpFileOp
->pTo
, (!b_Multi
|| b_MultiFrom
));
906 if (!(b_MultiPaired
) || !(pFromFile
) || !(pFromFile
[1]) || ((pTo
) && !(pToFile
)))
908 retCode
= 0x402; /* 1026 */
913 b_ToTailSlash
= (!pToFile
[1]);
917 if (StrChrW(pTempTo
,'\\'))
919 pToFile
= SHFileStrCpyCatW(pTempTo
, NULL
, NULL
);
922 b_ToInvalidTail
= (NULL
!= StrPBrkW(&pToFile
[1], wWildcardChars
));
926 b_Mask
= (NULL
!= StrPBrkW(&pFromFile
[1], wWildcardChars
));
927 if (FO_RENAME
== FuncSwitch
)
929 /* temporary only for FO_RENAME */
930 /* ??? b_Mask = (NULL != strrbrk(pFrom,"*?")); */
931 if (b_MultiTo
|| b_MultiFrom
|| (b_Mask
&& !b_ToInvalidTail
))
933 /* no work, only RC=0 */
934 /* ??? nFileOp.fAnyOperationsAborted = TRUE; */
935 /*#define W98_FO_RENEME */
937 goto shfileop_normal
;
939 retCode
= 0x1; /* 1 value unknown, W98 returns no error */
944 hFind
= FindFirstFileW(pFrom
, &wfd
);
945 if (INVALID_HANDLE_VALUE
== hFind
)
947 if ((FO_DELETE
== FuncSwitch
) && (b_Mask
))
950 FromPathAttr
= GetFileAttributesW(pTempFrom
);
952 if (IsAttribDir(FromPathAttr
))
954 /* FO_DELETE with mask and without found is valid */
955 goto shfileop_normal
;
958 /* root (without mask) is also not allowed as source, tested in W98 */
959 retCode
= 0x402; /* 1026 */
964 #define HIGH_ADR (LPWSTR)0xffffffff
966 /* ??? b_Mask = (!SHFileStrICmpA(&pFromFile[1], &wfd.cFileName[0], HIGH_ADR, HIGH_ADR)); */
967 if (!pTo
) /* FO_DELETE */
971 lpFileName
= wfd
.cAlternateFileName
;
973 lpFileName
= wfd
.cFileName
;
974 if (IsDotDir(lpFileName
) ||
975 ((b_Mask
) && IsAttribDir(wfd
.dwFileAttributes
) && (nFileOp
.fFlags
& FOF_FILESONLY
)))
977 SHFileStrCpyCatW(&pFromFile
[1], lpFileName
, NULL
);
978 /* TODO: Check the SHELL_DeleteFileOrDirectoryW() function in shell32.dll */
979 if (IsAttribFile(wfd
.dwFileAttributes
))
981 nFileOp
.fAnyOperationsAborted
= (!SHNotifyDeleteFileW(pTempFrom
));
982 retCode
= 0x78; /* value unknown */
986 nFileOp
.fAnyOperationsAborted
= (!SHELL_DeleteDirectoryW(pTempFrom
, (!(nFileOp
.fFlags
& FOF_NOCONFIRMATION
))));
987 retCode
= 0x79; /* value unknown */
989 } while (!nFileOp
.fAnyOperationsAborted
&& FindNextFileW(hFind
, &wfd
));
991 hFind
= INVALID_HANDLE_VALUE
;
992 if (nFileOp
.fAnyOperationsAborted
)
997 } /* FO_DELETE ends, pTo must be always valid from here */
999 b_SameRoot
= (toupperW(pTempFrom
[0]) == toupperW(pTempTo
[0]));
1000 b_SameTailName
= SHFileStrICmpW(pToFile
, pFromFile
, NULL
, NULL
);
1002 ToPathAttr
= ToAttr
= GetFileAttributesW(pTempTo
);
1003 if (!b_Mask
&& (ToAttr
== -1) && (pToFile
))
1006 ToPathAttr
= GetFileAttributesW(pTempTo
);
1010 if (FO_RENAME
== FuncSwitch
)
1012 if (!b_SameRoot
|| b_Mask
/* FO_RENAME works not with Mask */
1013 || !SHFileStrICmpW(pTempFrom
, pTempTo
, pFromFile
, NULL
)
1014 || (SHFileStrICmpW(pTempFrom
, pTempTo
, pFromFile
, HIGH_ADR
) && !b_ToTailSlash
))
1017 goto shfileop_error
;
1019 if (b_ToInvalidTail
)
1022 goto shfileop_error
;
1024 if (-1 == ToPathAttr
)
1027 goto shfileop_error
;
1029 if (IsAttribDir(wfd
.dwFileAttributes
) && IsAttribDir(ToAttr
))
1031 retCode
= (b_ToTailSlash
) ? 0xb7 : 0x7b;
1032 goto shfileop_error
;
1034 /* we use SHNotifyMoveFile() instead MoveFileW */
1035 if (!SHNotifyMoveFileW(pTempFrom
, pTempTo
))
1037 /* we need still the value for the returncode, we use the mostly assumed */
1039 goto shfileop_error
;
1041 goto shfileop_normal
;
1044 /* W98 Bug with FO_MOVE different to FO_COPY, better the same as FO_COPY */
1045 b_ToValid
= ((b_SameTailName
&& b_SameRoot
&& (FO_COPY
== FuncSwitch
)) ||
1046 (b_SameTailName
&& !b_SameRoot
) || (b_ToInvalidTail
));
1048 /* handle mask in source */
1051 if (!IsAttribDir(ToAttr
))
1053 retCode
= (b_ToInvalidTail
&&/* b_SameTailName &&*/ (FO_MOVE
== FuncSwitch
)) \
1055 goto shfileop_error
;
1057 pToFile
= SHFileStrCpyCatW(pTempTo
, NULL
, wBackslash
);
1058 nFileOp
.fFlags
= (nFileOp
.fFlags
| FOF_MULTIDESTFILES
);
1061 lpFileName
= wfd
.cAlternateFileName
;
1063 lpFileName
= wfd
.cFileName
;
1064 if (IsDotDir(lpFileName
) ||
1065 (IsAttribDir(wfd
.dwFileAttributes
) && (nFileOp
.fFlags
& FOF_FILESONLY
)))
1066 continue; /* next name in pTempFrom(dir) */
1067 SHFileStrCpyCatW(&pToFile
[1], lpFileName
, NULL
);
1068 SHFileStrCpyCatW(&pFromFile
[1], lpFileName
, NULL
);
1069 retCode
= SHFileOperationW (&nFileOp
);
1070 } while(!nFileOp
.fAnyOperationsAborted
&& FindNextFileW(hFind
, &wfd
));
1073 hFind
= INVALID_HANDLE_VALUE
;
1074 /* FO_COPY/FO_MOVE with mask, FO_DELETE and FO_RENAME are solved */
1078 /* only FO_COPY/FO_MOVE without mask, all others are (must be) solved */
1079 if (IsAttribDir(wfd
.dwFileAttributes
) && (ToAttr
== -1))
1084 ToPathAttr
= GetFileAttributesW(pTempTo
);
1085 if ((ToPathAttr
== -1) && b_ToValid
)
1087 /* create dir must be here, sample target D:\y\ *.* create with RC=10003 */
1088 if (SHCreateDirectoryExW(NULL
, pTempTo
, NULL
))
1090 retCode
= 0x73;/* value unknown */
1091 goto shfileop_error
;
1093 ToPathAttr
= GetFileAttributesW(pTempTo
);
1096 if (b_ToInvalidTail
)
1099 goto shfileop_error
;
1104 /* trailing BackSlash is ever removed and pToFile points to BackSlash before */
1105 if (!b_MultiTo
&& (b_MultiFrom
|| (!(b_Multi
) && IsAttribDir(ToAttr
))))
1107 if ((FO_MOVE
== FuncSwitch
) && IsAttribDir(ToAttr
) && IsAttribDir(wfd
.dwFileAttributes
))
1111 retCode
= 0x73; /* !b_Multi = 0x8 ?? */
1112 goto shfileop_error
;
1115 pToFile
= SHFileStrCpyCatW(pTempTo
, NULL
, wfd
.cFileName
);
1116 ToAttr
= GetFileAttributesW(pTempTo
);
1119 if (IsAttribDir(ToAttr
))
1121 if (IsAttribFile(wfd
.dwFileAttributes
))
1123 retCode
= (FO_COPY
== FuncSwitch
) ? 0x75 : 0xb7;
1124 goto shfileop_error
;
1130 ToPathAttr
= GetFileAttributesW(pTempTo
);
1132 if (IsAttribFile(ToPathAttr
))
1134 /* error, is this tested ? */
1136 goto shfileop_error
;
1140 /* singlesource + no mask */
1141 if (-1 == (ToAttr
& ToPathAttr
))
1143 /* Target-dir does not exist, and cannot be created */
1145 goto shfileop_error
;
1152 if ((ToAttr
== -1) && SHFileStrICmpW(pTempFrom
, pTempTo
, pFromFile
, NULL
))
1154 nFileOp
.wFunc
= ((level
+1)<<4) + FO_RENAME
;
1158 if (b_SameRoot
&& IsAttribDir(ToAttr
) && IsAttribDir(wfd
.dwFileAttributes
))
1160 /* we need pToFile for FO_DELETE after FO_MOVE contence */
1161 pToFile
= SHFileStrCpyCatW(pTempFrom
, NULL
, wWildcardFile
);
1165 nFileOp
.wFunc
= ((level
+1)<<4) + FO_COPY
;
1168 retCode
= SHFileOperationW(&nFileOp
);
1170 ((DWORD
*)pToFile
)[0] = '\0';
1171 if (!nFileOp
.fAnyOperationsAborted
&& (FO_RENAME
!= (nFileOp
.wFunc
& 0xf)))
1173 nFileOp
.wFunc
= ((level
+1)<<4) + FO_DELETE
;
1174 retCode
= SHFileOperationW(&nFileOp
);
1178 if (SHFileStrICmpW(pTempFrom
, pTempTo
, NULL
, NULL
))
1179 { /* target is the same as source ? */
1180 /* we still need the value for the returncode, we assume 0x71 */
1182 goto shfileop_error
;
1184 if (IsAttribDir((ToAttr
& wfd
.dwFileAttributes
)))
1186 if (IsAttribDir(ToAttr
) || !SHCreateDirectoryExW(NULL
,pTempTo
, NULL
))
1188 /* ??? nFileOp.fFlags = (nFileOp.fFlags | FOF_MULTIDESTFILES); */
1189 SHFileStrCpyCatW(pTempFrom
, NULL
, wWildcardFile
);
1190 retCode
= SHFileOperationW(&nFileOp
);
1194 retCode
= 0x750;/* value unknown */
1195 goto shfileop_error
;
1200 if (!(ask_overwrite
&& SHELL_ConfirmDialogW(ASK_OVERWRITE_FILE
, pTempTo
))
1203 /* we still need the value for the returncode, we use the mostly assumed */
1205 goto shfileop_error
;
1207 if (!(SHNotifyCopyFileW(pTempFrom
, pTempTo
, nFileOp
.fFlags
& FOF_RENAMEONCOLLISION
)))
1209 retCode
= 0x77; /* value unknown */
1210 goto shfileop_error
;
1217 if (!(nFileOp
.fAnyOperationsAborted
))
1220 if (hFind
!= INVALID_HANDLE_VALUE
)
1222 hFind
= INVALID_HANDLE_VALUE
;
1224 HeapFree(GetProcessHeap(), 0, pTempFrom
);
1227 nFileOp
.fAnyOperationsAborted
= TRUE
;
1229 TRACE("%s level=%ld AnyOpsAborted=%s ret=0x%lx, with %s %s%s\n",
1230 debug_shfileops_action(FuncSwitch
), level
,
1231 nFileOp
.fAnyOperationsAborted
? "TRUE":"FALSE",
1232 retCode
, debugstr_w(pFrom
), pTo
? "-> ":"", debugstr_w(pTo
));
1234 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
1238 /*************************************************************************
1239 * SHFileOperation [SHELL32.@]
1242 DWORD WINAPI
SHFileOperationAW(LPVOID lpFileOp
)
1244 if (SHELL_OsIsUnicode())
1245 return SHFileOperationW(lpFileOp
);
1246 return SHFileOperationA(lpFileOp
);
1249 /*************************************************************************
1250 * SheGetDirW [SHELL32.281]
1253 HRESULT WINAPI
SheGetDirW(LPWSTR u
, LPWSTR v
)
1254 { FIXME("%p %p stub\n",u
,v
);
1258 /*************************************************************************
1259 * SheChangeDirW [SHELL32.274]
1262 HRESULT WINAPI
SheChangeDirW(LPWSTR u
)
1263 { FIXME("(%s),stub\n",debugstr_w(u
));
1267 /*************************************************************************
1268 * IsNetDrive [SHELL32.66]
1270 BOOL WINAPI
IsNetDrive(DWORD drive
)
1273 strcpy(root
, "A:\\");
1274 root
[0] += (char)drive
;
1275 return (GetDriveTypeA(root
) == DRIVE_REMOTE
);