4 * Copyright 2000 Juergen Schmied
5 * Copyright 2002 Andriy Palamarchuk
6 * Copyright 2004 Dietrich Teickner (from Odin)
7 * Copyright 2004 Rolf Kalbermatter
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
39 #define NO_SHLWAPI_STREAM
41 #include "shell32_main.h"
42 #include "undocshell.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
48 #define IsAttrib(x, y) ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
49 #define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
50 #define IsAttribDir(x) IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
51 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
55 static const WCHAR wWildcardFile
[] = {'*',0};
56 static const WCHAR wWildcardChars
[] = {'*','?',0};
58 static DWORD
SHNotifyCreateDirectoryA(LPCSTR path
, LPSECURITY_ATTRIBUTES sec
);
59 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
);
60 static DWORD
SHNotifyRemoveDirectoryA(LPCSTR path
);
61 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
);
62 static DWORD
SHNotifyDeleteFileA(LPCSTR path
);
63 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
);
64 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
);
65 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bFailIfExists
);
66 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
);
70 UINT caption_resource_id
, text_resource_id
;
71 } SHELL_ConfirmIDstruc
;
73 static BOOL
SHELL_ConfirmIDs(int nKindOfDialog
, SHELL_ConfirmIDstruc
*ids
)
75 switch (nKindOfDialog
) {
77 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
78 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
80 case ASK_DELETE_FOLDER
:
81 ids
->caption_resource_id
= IDS_DELETEFOLDER_CAPTION
;
82 ids
->text_resource_id
= IDS_DELETEITEM_TEXT
;
84 case ASK_DELETE_MULTIPLE_ITEM
:
85 ids
->caption_resource_id
= IDS_DELETEITEM_CAPTION
;
86 ids
->text_resource_id
= IDS_DELETEMULTIPLE_TEXT
;
88 case ASK_OVERWRITE_FILE
:
89 ids
->caption_resource_id
= IDS_OVERWRITEFILE_CAPTION
;
90 ids
->text_resource_id
= IDS_OVERWRITEFILE_TEXT
;
93 FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog
);
98 BOOL
SHELL_ConfirmDialog(int nKindOfDialog
, LPCSTR szDir
)
100 CHAR szCaption
[255], szText
[255], szBuffer
[MAX_PATH
+ 256];
101 SHELL_ConfirmIDstruc ids
;
103 if (!SHELL_ConfirmIDs(nKindOfDialog
, &ids
))
106 LoadStringA(shell32_hInstance
, ids
.caption_resource_id
, szCaption
, sizeof(szCaption
));
107 LoadStringA(shell32_hInstance
, ids
.text_resource_id
, szText
, sizeof(szText
));
109 FormatMessageA(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
110 szText
, 0, 0, szBuffer
, sizeof(szBuffer
), (va_list*)&szDir
);
112 return (IDOK
== MessageBoxA(GetActiveWindow(), szBuffer
, szCaption
, MB_OKCANCEL
| MB_ICONEXCLAMATION
));
115 BOOL
SHELL_ConfirmDialogW(int nKindOfDialog
, LPCWSTR szDir
)
117 WCHAR szCaption
[255], szText
[255], szBuffer
[MAX_PATH
+ 256];
118 SHELL_ConfirmIDstruc ids
;
120 if (!SHELL_ConfirmIDs(nKindOfDialog
, &ids
))
123 LoadStringW(shell32_hInstance
, ids
.caption_resource_id
, szCaption
, sizeof(szCaption
));
124 LoadStringW(shell32_hInstance
, ids
.text_resource_id
, szText
, sizeof(szText
));
126 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
127 szText
, 0, 0, szBuffer
, sizeof(szBuffer
), (va_list*)&szDir
);
129 return (IDOK
== MessageBoxW(GetActiveWindow(), szBuffer
, szCaption
, MB_OKCANCEL
| MB_ICONEXCLAMATION
));
132 static DWORD
SHELL32_AnsiToUnicodeBuf(LPCSTR aPath
, LPWSTR
*wPath
, DWORD minChars
)
134 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, NULL
, 0);
139 *wPath
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
142 MultiByteToWideChar(CP_ACP
, 0, aPath
, -1, *wPath
, len
);
145 return E_OUTOFMEMORY
;
148 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath
)
150 HeapFree(GetProcessHeap(), 0, wPath
);
153 /**************************************************************************
154 * SHELL_DeleteDirectory() [internal]
156 * Asks for confirmation when bShowUI is true and deletes the directory and
157 * all its subdirectories and files if necessary.
159 BOOL
SHELL_DeleteDirectoryW(LPCWSTR pszDir
, BOOL bShowUI
)
163 WIN32_FIND_DATAW wfd
;
164 WCHAR szTemp
[MAX_PATH
];
166 /* Make sure the directory exists before eventually prompting the user */
167 PathCombineW(szTemp
, pszDir
, wWildcardFile
);
168 hFind
= FindFirstFileW(szTemp
, &wfd
);
169 if (hFind
== INVALID_HANDLE_VALUE
)
172 if (!bShowUI
|| (ret
= SHELL_ConfirmDialogW(ASK_DELETE_FOLDER
, pszDir
)))
176 LPWSTR lp
= wfd
.cAlternateFileName
;
181 PathCombineW(szTemp
, pszDir
, lp
);
182 if (FILE_ATTRIBUTE_DIRECTORY
& wfd
.dwFileAttributes
)
183 ret
= SHELL_DeleteDirectoryW(szTemp
, FALSE
);
185 ret
= (SHNotifyDeleteFileW(szTemp
) == ERROR_SUCCESS
);
186 } while (ret
&& FindNextFileW(hFind
, &wfd
));
190 ret
= (SHNotifyRemoveDirectoryW(pszDir
) == ERROR_SUCCESS
);
194 /**************************************************************************
195 * SHELL_DeleteFileW() [internal]
197 BOOL
SHELL_DeleteFileW(LPCWSTR pszFile
, BOOL bShowUI
)
199 if (bShowUI
&& !SHELL_ConfirmDialogW(ASK_DELETE_FILE
, pszFile
))
202 return (SHNotifyDeleteFileW(pszFile
) == ERROR_SUCCESS
);
205 /**************************************************************************
206 * Win32CreateDirectory [SHELL32.93]
208 * Creates a directory. Also triggers a change notify if one exists.
211 * path [I] path to directory to create
214 * TRUE if successful, FALSE otherwise
217 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
218 * This is Unicode on NT/2000
220 static DWORD
SHNotifyCreateDirectoryA(LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
225 TRACE("(%s, %p)\n", debugstr_a(path
), sec
);
227 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
230 retCode
= SHNotifyCreateDirectoryW(wPath
, sec
);
231 SHELL32_FreeUnicodeBuf(wPath
);
236 /**********************************************************************/
238 static DWORD
SHNotifyCreateDirectoryW(LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
240 TRACE("(%s, %p)\n", debugstr_w(path
), sec
);
242 if (CreateDirectoryW(path
, sec
))
244 SHChangeNotify(SHCNE_MKDIR
, SHCNF_PATHW
, path
, NULL
);
245 return ERROR_SUCCESS
;
247 return GetLastError();
250 /**********************************************************************/
252 BOOL WINAPI
Win32CreateDirectoryAW(LPCVOID path
, LPSECURITY_ATTRIBUTES sec
)
254 if (SHELL_OsIsUnicode())
255 return (SHNotifyCreateDirectoryW(path
, sec
) == ERROR_SUCCESS
);
256 return (SHNotifyCreateDirectoryA(path
, sec
) == ERROR_SUCCESS
);
259 /************************************************************************
260 * Win32RemoveDirectory [SHELL32.94]
262 * Deletes a directory. Also triggers a change notify if one exists.
265 * path [I] path to directory to delete
268 * TRUE if successful, FALSE otherwise
271 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
272 * This is Unicode on NT/2000
274 static DWORD
SHNotifyRemoveDirectoryA(LPCSTR path
)
279 TRACE("(%s)\n", debugstr_a(path
));
281 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
284 retCode
= SHNotifyRemoveDirectoryW(wPath
);
285 SHELL32_FreeUnicodeBuf(wPath
);
290 /***********************************************************************/
292 static DWORD
SHNotifyRemoveDirectoryW(LPCWSTR path
)
295 TRACE("(%s)\n", debugstr_w(path
));
297 ret
= RemoveDirectoryW(path
);
300 /* Directory may be write protected */
301 DWORD dwAttr
= GetFileAttributesW(path
);
302 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
))
303 if (SetFileAttributesW(path
, dwAttr
& ~FILE_ATTRIBUTE_READONLY
))
304 ret
= RemoveDirectoryW(path
);
308 SHChangeNotify(SHCNE_RMDIR
, SHCNF_PATHW
, path
, NULL
);
309 return ERROR_SUCCESS
;
311 return GetLastError();
314 /***********************************************************************/
316 BOOL WINAPI
Win32RemoveDirectoryAW(LPCVOID path
)
318 if (SHELL_OsIsUnicode())
319 return (SHNotifyRemoveDirectoryW(path
) == ERROR_SUCCESS
);
320 return (SHNotifyRemoveDirectoryA(path
) == ERROR_SUCCESS
);
323 /************************************************************************
324 * Win32DeleteFile [SHELL32.164]
326 * Deletes a file. Also triggers a change notify if one exists.
329 * path [I] path to file to delete
332 * TRUE if successful, FALSE otherwise
335 * Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
336 * This is Unicode on NT/2000
338 static DWORD
SHNotifyDeleteFileA(LPCSTR path
)
343 TRACE("(%s)\n", debugstr_a(path
));
345 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
348 retCode
= SHNotifyDeleteFileW(wPath
);
349 SHELL32_FreeUnicodeBuf(wPath
);
354 /***********************************************************************/
356 static DWORD
SHNotifyDeleteFileW(LPCWSTR path
)
360 TRACE("(%s)\n", debugstr_w(path
));
362 ret
= DeleteFileW(path
);
365 /* File may be write protected or a system file */
366 DWORD dwAttr
= GetFileAttributesW(path
);
367 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
368 if (SetFileAttributesW(path
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
369 ret
= DeleteFileW(path
);
373 SHChangeNotify(SHCNE_DELETE
, SHCNF_PATHW
, path
, NULL
);
374 return ERROR_SUCCESS
;
376 return GetLastError();
379 /***********************************************************************/
381 DWORD WINAPI
Win32DeleteFileAW(LPCVOID path
)
383 if (SHELL_OsIsUnicode())
384 return (SHNotifyDeleteFileW(path
) == ERROR_SUCCESS
);
385 return (SHNotifyDeleteFileA(path
) == ERROR_SUCCESS
);
388 /************************************************************************
389 * SHNotifyMoveFile [internal]
391 * Moves a file. Also triggers a change notify if one exists.
394 * src [I] path to source file to move
395 * dest [I] path to target file to move to
398 * ERORR_SUCCESS if successful
400 static DWORD
SHNotifyMoveFileW(LPCWSTR src
, LPCWSTR dest
)
404 TRACE("(%s %s)\n", debugstr_w(src
), debugstr_w(dest
));
406 ret
= MoveFileExW(src
, dest
, MOVEFILE_REPLACE_EXISTING
);
408 /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
410 ret
= MoveFileW(src
, dest
);
416 dwAttr
= SHFindAttrW(dest
, FALSE
);
417 if (INVALID_FILE_ATTRIBUTES
== dwAttr
)
419 /* Source file may be write protected or a system file */
420 dwAttr
= GetFileAttributesW(src
);
421 if (IsAttrib(dwAttr
, FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
))
422 if (SetFileAttributesW(src
, dwAttr
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_SYSTEM
)))
423 ret
= MoveFileW(src
, dest
);
428 SHChangeNotify(SHCNE_RENAMEITEM
, SHCNF_PATHW
, src
, dest
);
429 return ERROR_SUCCESS
;
431 return GetLastError();
434 /************************************************************************
435 * SHNotifyCopyFile [internal]
437 * Copies a file. Also triggers a change notify if one exists.
440 * src [I] path to source file to move
441 * dest [I] path to target file to move to
442 * bFailIfExists [I] if TRUE, the target file will not be overwritten if
443 * a file with this name already exists
446 * ERROR_SUCCESS if successful
448 static DWORD
SHNotifyCopyFileW(LPCWSTR src
, LPCWSTR dest
, BOOL bFailIfExists
)
452 TRACE("(%s %s %s)\n", debugstr_w(src
), debugstr_w(dest
), bFailIfExists
? "failIfExists" : "");
454 ret
= CopyFileW(src
, dest
, bFailIfExists
);
457 SHChangeNotify(SHCNE_CREATE
, SHCNF_PATHW
, dest
, NULL
);
458 return ERROR_SUCCESS
;
461 return GetLastError();
464 /*************************************************************************
465 * SHCreateDirectory [SHELL32.165]
467 * This function creates a file system folder whose fully qualified path is
468 * given by path. If one or more of the intermediate folders do not exist,
469 * they will be created as well.
473 * path [I] path of directory to create
476 * ERRROR_SUCCESS or one of the following values:
477 * ERROR_BAD_PATHNAME if the path is relative
478 * ERROR_FILE_EXISTS when a file with that name exists
479 * ERROR_PATH_NOT_FOUND can't find the path, probably invalid
480 * ERROR_INVLID_NAME if the path contains invalid chars
481 * ERROR_ALREADY_EXISTS when the directory already exists
482 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
485 * exported by ordinal
487 * WinNT/2000 exports Unicode
489 DWORD WINAPI
SHCreateDirectory(HWND hWnd
, LPCVOID path
)
491 if (SHELL_OsIsUnicode())
492 return SHCreateDirectoryExW(hWnd
, path
, NULL
);
493 return SHCreateDirectoryExA(hWnd
, path
, NULL
);
496 /*************************************************************************
497 * SHCreateDirectoryExA [SHELL32.@]
499 * This function creates a file system folder whose fully qualified path is
500 * given by path. If one or more of the intermediate folders do not exist,
501 * they will be created as well.
505 * path [I] path of directory to create
506 * sec [I] security attributes to use or NULL
509 * ERRROR_SUCCESS or one of the following values:
510 * ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
511 * ERROR_INVLID_NAME if the path contains invalid chars
512 * ERROR_FILE_EXISTS when a file with that name exists
513 * ERROR_ALREADY_EXISTS when the directory already exists
514 * ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
516 * FIXME: Not implemented yet;
517 * SHCreateDirectoryEx also verifies that the files in the directory will be visible
518 * if the path is a network path to deal with network drivers which might have a limited
519 * but unknown maximum path length. If not:
521 * If hWnd is set to a valid window handle, a message box is displayed warning
522 * the user that the files may not be accessible. If the user chooses not to
523 * proceed, the function returns ERROR_CANCELLED.
525 * If hWnd is set to NULL, no user interface is displayed and the function
526 * returns ERROR_CANCELLED.
528 int WINAPI
SHCreateDirectoryExA(HWND hWnd
, LPCSTR path
, LPSECURITY_ATTRIBUTES sec
)
533 TRACE("(%s, %p)\n", debugstr_a(path
), sec
);
535 retCode
= SHELL32_AnsiToUnicodeBuf(path
, &wPath
, 0);
538 retCode
= SHCreateDirectoryExW(hWnd
, wPath
, sec
);
539 SHELL32_FreeUnicodeBuf(wPath
);
544 /*************************************************************************
545 * SHCreateDirectoryExW [SHELL32.@]
547 * See SHCreateDirectoryExA.
549 int WINAPI
SHCreateDirectoryExW(HWND hWnd
, LPCWSTR path
, LPSECURITY_ATTRIBUTES sec
)
551 int ret
= ERROR_BAD_PATHNAME
;
552 TRACE("(%p, %s, %p)\n", hWnd
, debugstr_w(path
), sec
);
554 if (PathIsRelativeW(path
))
560 ret
= SHNotifyCreateDirectoryW(path
, sec
);
561 /* Refuse to work on certain error codes before trying to create directories recursively */
562 if (ret
!= ERROR_FILE_EXISTS
&&
563 ret
!= ERROR_ALREADY_EXISTS
&&
564 ret
!= ERROR_FILENAME_EXCED_RANGE
)
566 WCHAR
*pEnd
, *pSlash
, szTemp
[MAX_PATH
+ 1]; /* extra for PathAddBackslash() */
568 lstrcpynW(szTemp
, path
, MAX_PATH
);
569 pEnd
= PathAddBackslashW(szTemp
);
574 while (*pSlash
&& *pSlash
!= '\\')
575 pSlash
= CharNextW(pSlash
);
579 *pSlash
= 0; /* terminate path at separator */
581 ret
= SHNotifyCreateDirectoryW(szTemp
, pSlash
+ 1 == pEnd
? sec
: NULL
);
583 *pSlash
++ = '\\'; /* put the separator back */
587 if (ret
&& hWnd
&& (ERROR_CANCELLED
!= ret
))
589 /* We failed and should show a dialog box */
590 FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path
), ret
);
591 ret
= ERROR_CANCELLED
; /* Error has been already presented to user (not really yet!) */
597 /*************************************************************************
598 * SHFindAttrW [internal]
600 * Get the Attributes for a file or directory. The difference to GetAttributes()
601 * is that this function will also work for paths containing wildcard characters
605 * path [I] path of directory or file to check
606 * fileOnly [I] TRUE if only files should be found
609 * INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
610 * the first file or directory found otherwise
612 static DWORD
SHFindAttrW(LPCWSTR pName
, BOOL fileOnly
)
614 WIN32_FIND_DATAW wfd
;
615 BOOL b_FileMask
= fileOnly
&& (NULL
!= StrPBrkW(pName
, wWildcardChars
));
616 DWORD dwAttr
= INVALID_FILE_ATTRIBUTES
;
617 HANDLE hFind
= FindFirstFileW(pName
, &wfd
);
619 TRACE("%s %d\n", debugstr_w(pName
), fileOnly
);
620 if (INVALID_HANDLE_VALUE
!= hFind
)
624 if (b_FileMask
&& IsAttribDir(wfd
.dwFileAttributes
))
626 dwAttr
= wfd
.dwFileAttributes
;
629 while (FindNextFileW(hFind
, &wfd
));
635 /*************************************************************************
637 * SHNameTranslate HelperFunction for SHFileOperationA
639 * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
640 * is NULL, only the necessary size of the string is determined and returned,
641 * otherwise the ASCII strings are copied into it and the buffer is increased
642 * to point to the location after the final 0 termination char.
644 DWORD
SHNameTranslate(LPWSTR
* wString
, LPCWSTR
* pWToFrom
, BOOL more
)
646 DWORD size
= 0, aSize
= 0;
647 LPCSTR aString
= (LPCSTR
)*pWToFrom
;
653 size
= lstrlenA(aString
) + 1;
656 } while ((size
!= 1) && more
);
657 /* The two sizes might be different in the case of multibyte chars */
658 size
= MultiByteToWideChar(CP_ACP
, 0, aString
, aSize
, *wString
, 0);
659 if (*wString
) /* only in the second loop */
661 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)*pWToFrom
, aSize
, *wString
, size
);
662 *pWToFrom
= *wString
;
668 /*************************************************************************
669 * SHFileOperationA [SHELL32.@]
671 * Function to copy, move, delete and create one or more files with optional
675 * lpFileOp [I/O] pointer to a structure containing all the necessary information
678 * Success: ERROR_SUCCESS.
679 * Failure: ERROR_CANCELLED.
684 int WINAPI
SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp
)
686 SHFILEOPSTRUCTW nFileOp
= *((LPSHFILEOPSTRUCTW
)lpFileOp
);
689 LPWSTR ForFree
= NULL
, /* we change wString in SHNameTranslate and can't use it for freeing */
690 wString
= NULL
; /* we change this in SHNameTranslate */
693 if (FO_DELETE
== (nFileOp
.wFunc
& FO_MASK
))
694 nFileOp
.pTo
= NULL
; /* we need a NULL or a valid pointer for translation */
695 if (!(nFileOp
.fFlags
& FOF_SIMPLEPROGRESS
))
696 nFileOp
.lpszProgressTitle
= NULL
; /* we need a NULL or a valid pointer for translation */
697 while (1) /* every loop calculate size, second translate also, if we have storage for this */
699 size
= SHNameTranslate(&wString
, &nFileOp
.lpszProgressTitle
, FALSE
); /* no loop */
700 size
+= SHNameTranslate(&wString
, &nFileOp
.pFrom
, TRUE
); /* internal loop */
701 size
+= SHNameTranslate(&wString
, &nFileOp
.pTo
, TRUE
); /* internal loop */
705 retCode
= SHFileOperationW(&nFileOp
);
706 HeapFree(GetProcessHeap(), 0, ForFree
); /* we cannot use wString, it was changed */
711 wString
= ForFree
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
712 if (ForFree
) continue;
713 retCode
= ERROR_OUTOFMEMORY
;
714 nFileOp
.fAnyOperationsAborted
= TRUE
;
715 SetLastError(retCode
);
720 lpFileOp
->hNameMappings
= nFileOp
.hNameMappings
;
721 lpFileOp
->fAnyOperationsAborted
= nFileOp
.fAnyOperationsAborted
;
725 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
743 BOOL bAnyFromWildcard
;
744 BOOL bAnyDirectories
;
749 static inline void grow_list(FILE_LIST
*list
)
751 FILE_ENTRY
*new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, list
->feFiles
,
752 list
->num_alloc
* 2 * sizeof(*new) );
754 list
->num_alloc
*= 2;
757 /* adds a file to the FILE_ENTRY struct
759 static void add_file_to_entry(FILE_ENTRY
*feFile
, LPWSTR szFile
)
761 DWORD dwLen
= strlenW(szFile
) + 1;
764 feFile
->szFullPath
= HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
765 strcpyW(feFile
->szFullPath
, szFile
);
767 ptr
= StrRChrW(szFile
, NULL
, '\\');
770 dwLen
= ptr
- szFile
+ 1;
771 feFile
->szDirectory
= HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
772 lstrcpynW(feFile
->szDirectory
, szFile
, dwLen
);
774 dwLen
= strlenW(feFile
->szFullPath
) - dwLen
+ 1;
775 feFile
->szFilename
= HeapAlloc(GetProcessHeap(), 0, dwLen
* sizeof(WCHAR
));
776 strcpyW(feFile
->szFilename
, ptr
+ 1); /* skip over backslash */
778 feFile
->bFromWildcard
= FALSE
;
781 static LPWSTR
wildcard_to_file(LPWSTR szWildCard
, LPWSTR szFileName
)
783 LPWSTR szFullPath
, ptr
;
784 DWORD dwDirLen
, dwFullLen
;
786 ptr
= StrRChrW(szWildCard
, NULL
, '\\');
787 dwDirLen
= ptr
- szWildCard
+ 1;
789 dwFullLen
= dwDirLen
+ strlenW(szFileName
) + 1;
790 szFullPath
= HeapAlloc(GetProcessHeap(), 0, dwFullLen
* sizeof(WCHAR
));
792 lstrcpynW(szFullPath
, szWildCard
, dwDirLen
+ 1);
793 strcatW(szFullPath
, szFileName
);
798 static void parse_wildcard_files(FILE_LIST
*flList
, LPWSTR szFile
, LPDWORD pdwListIndex
)
800 WIN32_FIND_DATAW wfd
;
801 HANDLE hFile
= FindFirstFileW(szFile
, &wfd
);
806 for (res
= TRUE
; res
; res
= FindNextFileW(hFile
, &wfd
))
808 if (IsDotDir(wfd
.cFileName
)) continue;
809 if (*pdwListIndex
>= flList
->num_alloc
) grow_list( flList
);
810 szFullPath
= wildcard_to_file(szFile
, wfd
.cFileName
);
811 file
= &flList
->feFiles
[(*pdwListIndex
)++];
812 add_file_to_entry(file
, szFullPath
);
813 file
->bFromWildcard
= TRUE
;
814 file
->attributes
= wfd
.dwFileAttributes
;
815 if (IsAttribDir(file
->attributes
)) flList
->bAnyDirectories
= TRUE
;
816 HeapFree(GetProcessHeap(), 0, szFullPath
);
822 /* takes the null-separated file list and fills out the FILE_LIST */
823 static HRESULT
parse_file_list(FILE_LIST
*flList
, LPCWSTR szFiles
)
825 LPCWSTR ptr
= szFiles
;
826 WCHAR szCurFile
[MAX_PATH
];
827 DWORD i
= 0, dwDirLen
;
830 return ERROR_INVALID_PARAMETER
;
832 flList
->bAnyFromWildcard
= FALSE
;
833 flList
->bAnyDirectories
= FALSE
;
834 flList
->bAnyDontExist
= FALSE
;
835 flList
->num_alloc
= 32;
836 flList
->dwNumFiles
= 0;
840 return ERROR_ACCESS_DENIED
;
842 flList
->feFiles
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
843 flList
->num_alloc
* sizeof(FILE_ENTRY
));
847 if (i
>= flList
->num_alloc
) grow_list( flList
);
849 /* change relative to absolute path */
850 if (PathIsRelativeW(ptr
))
852 dwDirLen
= GetCurrentDirectoryW(MAX_PATH
, szCurFile
) + 1;
853 PathCombineW(szCurFile
, szCurFile
, ptr
);
854 flList
->feFiles
[i
].bFromRelative
= TRUE
;
858 strcpyW(szCurFile
, ptr
);
859 flList
->feFiles
[i
].bFromRelative
= FALSE
;
862 /* parse wildcard files if they are in the filename */
863 if (StrPBrkW(szCurFile
, wWildcardChars
))
865 parse_wildcard_files(flList
, szCurFile
, &i
);
866 flList
->bAnyFromWildcard
= TRUE
;
871 FILE_ENTRY
*file
= &flList
->feFiles
[i
];
872 add_file_to_entry(file
, szCurFile
);
873 file
->attributes
= GetFileAttributesW( file
->szFullPath
);
874 file
->bExists
= (file
->attributes
!= INVALID_FILE_ATTRIBUTES
);
875 if (!file
->bExists
) flList
->bAnyDontExist
= TRUE
;
876 if (IsAttribDir(file
->attributes
)) flList
->bAnyDirectories
= TRUE
;
879 /* advance to the next string */
880 ptr
+= strlenW(ptr
) + 1;
883 flList
->dwNumFiles
= i
;
888 /* free the FILE_LIST */
889 static void destroy_file_list(FILE_LIST
*flList
)
893 if (!flList
|| !flList
->feFiles
)
896 for (i
= 0; i
< flList
->dwNumFiles
; i
++)
898 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szDirectory
);
899 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFilename
);
900 HeapFree(GetProcessHeap(), 0, flList
->feFiles
[i
].szFullPath
);
903 HeapFree(GetProcessHeap(), 0, flList
->feFiles
);
906 static void copy_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, FILE_ENTRY
*feFrom
, LPWSTR szDestPath
)
908 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
909 SHFILEOPSTRUCTW fileOp
;
911 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
913 if (IsDotDir(feFrom
->szFilename
))
916 if (PathFileExistsW(szDestPath
))
917 PathCombineW(szTo
, szDestPath
, feFrom
->szFilename
);
919 strcpyW(szTo
, szDestPath
);
921 szTo
[strlenW(szTo
) + 1] = '\0';
922 SHNotifyCreateDirectoryW(szTo
, NULL
);
924 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
925 szFrom
[strlenW(szFrom
) + 1] = '\0';
927 memcpy(&fileOp
, lpFileOp
, sizeof(fileOp
));
928 fileOp
.pFrom
= szFrom
;
930 fileOp
.fFlags
&= ~FOF_MULTIDESTFILES
; /* we know we're copying to one dir */
932 SHFileOperationW(&fileOp
);
935 /* copy a file or directory to another directory */
936 static void copy_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, FILE_ENTRY
*feFrom
, FILE_ENTRY
*feTo
)
938 WCHAR szDestPath
[MAX_PATH
];
940 if (!PathFileExistsW(feTo
->szFullPath
))
941 SHNotifyCreateDirectoryW(feTo
->szFullPath
, NULL
);
943 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
945 if (IsAttribFile(feFrom
->attributes
))
946 SHNotifyCopyFileW(feFrom
->szFullPath
, szDestPath
, FALSE
);
947 else if (!(lpFileOp
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
948 copy_dir_to_dir(lpFileOp
, feFrom
, szDestPath
);
951 static void create_dest_dirs(LPWSTR szDestDir
)
954 LPWSTR ptr
= StrChrW(szDestDir
, '\\');
956 /* make sure all directories up to last one are created */
957 while (ptr
&& (ptr
= StrChrW(ptr
+ 1, '\\')))
959 lstrcpynW(dir
, szDestDir
, ptr
- szDestDir
+ 1);
961 if (!PathFileExistsW(dir
))
962 SHNotifyCreateDirectoryW(dir
, NULL
);
965 /* create last directory */
966 if (!PathFileExistsW(szDestDir
))
967 SHNotifyCreateDirectoryW(szDestDir
, NULL
);
970 /* the FO_COPY operation */
971 static HRESULT
copy_files(LPSHFILEOPSTRUCTW lpFileOp
, FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
974 FILE_ENTRY
*entryToCopy
;
975 FILE_ENTRY
*fileDest
= &flTo
->feFiles
[0];
976 BOOL bCancelIfAnyDirectories
= FALSE
;
978 if (flFrom
->bAnyDontExist
)
979 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
981 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
&& flFrom
->bAnyFromWildcard
)
982 return ERROR_CANCELLED
;
984 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) && flTo
->dwNumFiles
!= 1)
985 return ERROR_CANCELLED
;
987 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
&& flFrom
->dwNumFiles
!= 1 &&
988 flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
990 return ERROR_CANCELLED
;
993 if (flFrom
->dwNumFiles
> 1 && flTo
->dwNumFiles
== 1 &&
994 !PathFileExistsW(flTo
->feFiles
[0].szFullPath
) &&
995 IsAttribFile(fileDest
->attributes
))
997 bCancelIfAnyDirectories
= TRUE
;
1000 if (flFrom
->dwNumFiles
> 1 && flTo
->dwNumFiles
== 1 && fileDest
->bFromRelative
&&
1001 !PathFileExistsW(fileDest
->szFullPath
))
1003 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1004 return ERROR_CANCELLED
;
1007 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) && flFrom
->dwNumFiles
!= 1 &&
1008 PathFileExistsW(fileDest
->szFullPath
) &&
1009 IsAttribFile(fileDest
->attributes
))
1011 return ERROR_CANCELLED
;
1014 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1016 entryToCopy
= &flFrom
->feFiles
[i
];
1018 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
)
1019 fileDest
= &flTo
->feFiles
[i
];
1021 if (IsAttribDir(entryToCopy
->attributes
) &&
1022 !lstrcmpW(entryToCopy
->szFullPath
, fileDest
->szDirectory
))
1024 return ERROR_SUCCESS
;
1027 if (IsAttribDir(entryToCopy
->attributes
) && bCancelIfAnyDirectories
)
1028 return ERROR_CANCELLED
;
1030 create_dest_dirs(fileDest
->szDirectory
);
1032 if (!strcmpW(entryToCopy
->szFullPath
, fileDest
->szFullPath
))
1034 if (IsAttribFile(entryToCopy
->attributes
))
1035 return ERROR_NO_MORE_SEARCH_HANDLES
;
1037 return ERROR_SUCCESS
;
1040 if ((flFrom
->dwNumFiles
> 1 && flTo
->dwNumFiles
== 1) ||
1041 (flFrom
->dwNumFiles
== 1 && IsAttribDir(fileDest
->attributes
)))
1043 copy_to_dir(lpFileOp
, entryToCopy
, fileDest
);
1045 else if (IsAttribDir(entryToCopy
->attributes
))
1047 copy_dir_to_dir(lpFileOp
, entryToCopy
, fileDest
->szFullPath
);
1051 if (SHNotifyCopyFileW(entryToCopy
->szFullPath
, fileDest
->szFullPath
, TRUE
))
1053 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1054 return ERROR_CANCELLED
;
1059 return ERROR_SUCCESS
;
1062 /* the FO_DELETE operation */
1063 static HRESULT
delete_files(LPSHFILEOPSTRUCTW lpFileOp
, FILE_LIST
*flFrom
)
1065 FILE_ENTRY
*fileEntry
;
1068 BOOL bConfirm
= !(lpFileOp
->fFlags
& FOF_NOCONFIRMATION
);
1070 if (!flFrom
->dwNumFiles
)
1071 return ERROR_SUCCESS
;
1073 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1076 fileEntry
= &flFrom
->feFiles
[i
];
1078 /* delete the file or directory */
1079 if (IsAttribFile(fileEntry
->attributes
))
1080 bPathExists
= DeleteFileW(fileEntry
->szFullPath
);
1081 else if (!(lpFileOp
->fFlags
& FOF_FILESONLY
&& fileEntry
->bFromWildcard
))
1082 bPathExists
= SHELL_DeleteDirectoryW(fileEntry
->szFullPath
, bConfirm
);
1085 return ERROR_PATH_NOT_FOUND
;
1088 return ERROR_SUCCESS
;
1091 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, FILE_ENTRY
*feFrom
, LPWSTR szDestPath
)
1093 WCHAR szFrom
[MAX_PATH
], szTo
[MAX_PATH
];
1094 SHFILEOPSTRUCTW fileOp
;
1096 static const WCHAR wildCardFiles
[] = {'*','.','*',0};
1098 if (IsDotDir(feFrom
->szFilename
))
1101 SHNotifyCreateDirectoryW(szDestPath
, NULL
);
1103 PathCombineW(szFrom
, feFrom
->szFullPath
, wildCardFiles
);
1104 szFrom
[strlenW(szFrom
) + 1] = '\0';
1106 strcpyW(szTo
, szDestPath
);
1107 szTo
[strlenW(szDestPath
) + 1] = '\0';
1109 memcpy(&fileOp
, lpFileOp
, sizeof(fileOp
));
1110 fileOp
.pFrom
= szFrom
;
1113 SHFileOperationW(&fileOp
);
1116 /* moves a file or directory to another directory */
1117 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp
, FILE_ENTRY
*feFrom
, FILE_ENTRY
*feTo
)
1119 WCHAR szDestPath
[MAX_PATH
];
1121 PathCombineW(szDestPath
, feTo
->szFullPath
, feFrom
->szFilename
);
1123 if (IsAttribFile(feFrom
->attributes
))
1124 SHNotifyMoveFileW(feFrom
->szFullPath
, szDestPath
);
1125 else if (!(lpFileOp
->fFlags
& FOF_FILESONLY
&& feFrom
->bFromWildcard
))
1126 move_dir_to_dir(lpFileOp
, feFrom
, szDestPath
);
1129 /* the FO_MOVE operation */
1130 static HRESULT
move_files(LPSHFILEOPSTRUCTW lpFileOp
, FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
1133 FILE_ENTRY
*entryToMove
;
1134 FILE_ENTRY
*fileDest
;
1136 if (!flFrom
->dwNumFiles
|| !flTo
->dwNumFiles
)
1137 return ERROR_CANCELLED
;
1139 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1140 flTo
->dwNumFiles
> 1 && flFrom
->dwNumFiles
> 1)
1142 return ERROR_CANCELLED
;
1145 if (!(lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1146 !flFrom
->bAnyDirectories
&&
1147 flFrom
->dwNumFiles
> flTo
->dwNumFiles
)
1149 return ERROR_CANCELLED
;
1152 if (!PathFileExistsW(flTo
->feFiles
[0].szDirectory
))
1153 return ERROR_CANCELLED
;
1155 if ((lpFileOp
->fFlags
& FOF_MULTIDESTFILES
) &&
1156 flFrom
->dwNumFiles
!= flTo
->dwNumFiles
)
1158 return ERROR_CANCELLED
;
1161 fileDest
= &flTo
->feFiles
[0];
1162 for (i
= 0; i
< flFrom
->dwNumFiles
; i
++)
1164 entryToMove
= &flFrom
->feFiles
[i
];
1166 if (lpFileOp
->fFlags
& FOF_MULTIDESTFILES
)
1167 fileDest
= &flTo
->feFiles
[i
];
1169 if (!PathFileExistsW(fileDest
->szDirectory
))
1170 return ERROR_CANCELLED
;
1172 if (fileDest
->bExists
&& IsAttribDir(fileDest
->attributes
))
1173 move_to_dir(lpFileOp
, entryToMove
, fileDest
);
1175 SHNotifyMoveFileW(entryToMove
->szFullPath
, fileDest
->szFullPath
);
1178 return ERROR_SUCCESS
;
1181 /* the FO_RENAME files */
1182 static HRESULT
rename_files(LPSHFILEOPSTRUCTW lpFileOp
, FILE_LIST
*flFrom
, FILE_LIST
*flTo
)
1187 if (flFrom
->dwNumFiles
!= 1)
1188 return ERROR_GEN_FAILURE
;
1190 if (flTo
->dwNumFiles
!= 1)
1191 return ERROR_CANCELLED
;
1193 feFrom
= &flFrom
->feFiles
[0];
1194 feTo
= &flTo
->feFiles
[0];
1196 /* fail if destination doesn't exist */
1197 if (!feFrom
->bExists
)
1198 return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND
;
1200 /* fail if destination already exists */
1202 return ERROR_ALREADY_EXISTS
;
1204 return SHNotifyMoveFileW(feFrom
->szFullPath
, feTo
->szFullPath
);
1207 /* alert the user if an unsupported flag is used */
1208 static void check_flags(FILEOP_FLAGS fFlags
)
1210 WORD wUnsupportedFlags
= FOF_ALLOWUNDO
| FOF_NO_CONNECTED_ELEMENTS
|
1211 FOF_NOCOPYSECURITYATTRIBS
| FOF_NORECURSEREPARSE
| FOF_WANTNUKEWARNING
|
1212 FOF_RENAMEONCOLLISION
| FOF_WANTMAPPINGHANDLE
;
1214 if (fFlags
& wUnsupportedFlags
)
1215 FIXME("Unsupported flags: %04x\n", fFlags
);
1218 /*************************************************************************
1219 * SHFileOperationW [SHELL32.@]
1221 * See SHFileOperationA
1223 int WINAPI
SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp
)
1225 FILE_LIST flFrom
, flTo
;
1229 return ERROR_INVALID_PARAMETER
;
1231 check_flags(lpFileOp
->fFlags
);
1233 ZeroMemory(&flFrom
, sizeof(FILE_LIST
));
1234 ZeroMemory(&flTo
, sizeof(FILE_LIST
));
1236 if ((ret
= parse_file_list(&flFrom
, lpFileOp
->pFrom
)))
1239 if (lpFileOp
->wFunc
!= FO_DELETE
)
1240 parse_file_list(&flTo
, lpFileOp
->pTo
);
1242 switch (lpFileOp
->wFunc
)
1245 ret
= copy_files(lpFileOp
, &flFrom
, &flTo
);
1248 ret
= delete_files(lpFileOp
, &flFrom
);
1251 ret
= move_files(lpFileOp
, &flFrom
, &flTo
);
1254 ret
= rename_files(lpFileOp
, &flFrom
, &flTo
);
1257 ret
= ERROR_INVALID_PARAMETER
;
1261 destroy_file_list(&flFrom
);
1263 if (lpFileOp
->wFunc
!= FO_DELETE
)
1264 destroy_file_list(&flTo
);
1266 if (ret
== ERROR_CANCELLED
)
1267 lpFileOp
->fAnyOperationsAborted
= TRUE
;
1272 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1274 /*************************************************************************
1275 * SHFreeNameMappings [shell32.246]
1277 * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1281 * hNameMapping [I] handle to the name mappings used during renaming of files
1286 void WINAPI
SHFreeNameMappings(HANDLE hNameMapping
)
1290 int i
= SHDSA_GetItemCount((HDSA
)hNameMapping
) - 1;
1294 LPSHNAMEMAPPINGW lp
= DSA_GetItemPtr((HDSA
)hNameMapping
, i
);
1296 SHFree(lp
->pszOldPath
);
1297 SHFree(lp
->pszNewPath
);
1299 DSA_Destroy((HDSA
)hNameMapping
);
1303 /*************************************************************************
1304 * SheGetDirA [SHELL32.@]
1307 HRESULT WINAPI
SheGetDirA(LPSTR u
, LPSTR v
)
1308 { FIXME("%p %p stub\n",u
,v
);
1312 /*************************************************************************
1313 * SheGetDirW [SHELL32.@]
1316 HRESULT WINAPI
SheGetDirW(LPWSTR u
, LPWSTR v
)
1317 { FIXME("%p %p stub\n",u
,v
);
1321 /*************************************************************************
1322 * SheChangeDirA [SHELL32.@]
1325 HRESULT WINAPI
SheChangeDirA(LPSTR u
)
1326 { FIXME("(%s),stub\n",debugstr_a(u
));
1330 /*************************************************************************
1331 * SheChangeDirW [SHELL32.@]
1334 HRESULT WINAPI
SheChangeDirW(LPWSTR u
)
1335 { FIXME("(%s),stub\n",debugstr_w(u
));
1339 /*************************************************************************
1340 * IsNetDrive [SHELL32.66]
1342 BOOL WINAPI
IsNetDrive(DWORD drive
)
1345 strcpy(root
, "A:\\");
1346 root
[0] += (char)drive
;
1347 return (GetDriveTypeA(root
) == DRIVE_REMOTE
);
1351 /*************************************************************************
1352 * RealDriveType [SHELL32.524]
1354 INT WINAPI
RealDriveType(INT drive
, BOOL bQueryNet
)
1356 char root
[] = "A:\\";
1357 root
[0] += (char)drive
;
1358 return GetDriveTypeA(root
);