📄 shlfileop.c
字号:
/*
* SHFileOperation
*
* Copyright 2000 Juergen Schmied
* Copyright 2002 Andriy Palamarchuk
* Copyright 2004 Dietrich Teickner (from Odin)
* Copyright 2004 Rolf Kalbermatter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "wine/port.h"
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "shellapi.h"
#include "wingdi.h"
#include "winuser.h"
#include "shlobj.h"
#include "shresdef.h"
#define NO_SHLWAPI_STREAM
#include "shlwapi.h"
#include "shell32_main.h"
#include "undocshell.h"
#include "wine/unicode.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(shell);
#define IsAttrib(x, y) ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
#define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
#define IsAttribDir(x) IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
#define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
#define FO_MASK 0xF
static const WCHAR wWildcardFile[] = {'*',0};
static const WCHAR wWildcardChars[] = {'*','?',0};
static const WCHAR wBackslash[] = {'\\',0};
static BOOL SHELL_DeleteDirectoryW(LPCWSTR path, BOOL bShowUI);
static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec);
static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec);
static DWORD SHNotifyRemoveDirectoryA(LPCSTR path);
static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path);
static DWORD SHNotifyDeleteFileA(LPCSTR path);
static DWORD SHNotifyDeleteFileW(LPCWSTR path);
static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest);
static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists);
static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly);
typedef struct
{
UINT caption_resource_id, text_resource_id;
} SHELL_ConfirmIDstruc;
static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
{
switch (nKindOfDialog) {
case ASK_DELETE_FILE:
ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
ids->text_resource_id = IDS_DELETEITEM_TEXT;
return TRUE;
case ASK_DELETE_FOLDER:
ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
ids->text_resource_id = IDS_DELETEITEM_TEXT;
return TRUE;
case ASK_DELETE_MULTIPLE_ITEM:
ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
ids->text_resource_id = IDS_DELETEMULTIPLE_TEXT;
return TRUE;
case ASK_OVERWRITE_FILE:
ids->caption_resource_id = IDS_OVERWRITEFILE_CAPTION;
ids->text_resource_id = IDS_OVERWRITEFILE_TEXT;
return TRUE;
default:
FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
}
return FALSE;
}
BOOL SHELL_ConfirmDialog(int nKindOfDialog, LPCSTR szDir)
{
CHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
SHELL_ConfirmIDstruc ids;
if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
return FALSE;
LoadStringA(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption));
LoadStringA(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText));
FormatMessageA(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
return (IDOK == MessageBoxA(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
}
BOOL SHELL_ConfirmDialogW(int nKindOfDialog, LPCWSTR szDir)
{
WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
SHELL_ConfirmIDstruc ids;
if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
return FALSE;
LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption));
LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText));
FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
return (IDOK == MessageBoxW(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
}
static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
{
DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
if (len < minChars)
len = minChars;
*wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (*wPath)
{
MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
return NO_ERROR;
}
return E_OUTOFMEMORY;
}
static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
{
HeapFree(GetProcessHeap(), 0, wPath);
}
/**************************************************************************
* SHELL_DeleteDirectory() [internal]
*
* Asks for confirmation when bShowUI is true and deletes the directory and
* all its subdirectories and files if necessary.
*/
BOOL SHELL_DeleteDirectoryA(LPCSTR pszDir, BOOL bShowUI)
{
LPWSTR wPath;
BOOL ret = FALSE;
if (!SHELL32_AnsiToUnicodeBuf(pszDir, &wPath, 0))
{
ret = SHELL_DeleteDirectoryW(wPath, bShowUI);
SHELL32_FreeUnicodeBuf(wPath);
}
return ret;
}
static BOOL SHELL_DeleteDirectoryW(LPCWSTR pszDir, BOOL bShowUI)
{
BOOL ret = TRUE;
HANDLE hFind;
WIN32_FIND_DATAW wfd;
WCHAR szTemp[MAX_PATH];
/* Make sure the directory exists before eventually prompting the user */
PathCombineW(szTemp, pszDir, wWildcardFile);
hFind = FindFirstFileW(szTemp, &wfd);
if (hFind == INVALID_HANDLE_VALUE)
return FALSE;
if (!bShowUI || (ret = SHELL_ConfirmDialogW(ASK_DELETE_FOLDER, pszDir)))
{
do
{
LPWSTR lp = wfd.cAlternateFileName;
if (!lp[0])
lp = wfd.cFileName;
if (IsDotDir(lp))
continue;
PathCombineW(szTemp, pszDir, lp);
if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
ret = SHELL_DeleteDirectoryW(szTemp, FALSE);
else
ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
} while (ret && FindNextFileW(hFind, &wfd));
}
FindClose(hFind);
if (ret)
ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
return ret;
}
/**************************************************************************
* SHELL_DeleteFileA() [internal]
*/
BOOL SHELL_DeleteFileA(LPCSTR pszFile, BOOL bShowUI)
{
if (bShowUI && !SHELL_ConfirmDialog(ASK_DELETE_FILE, pszFile))
return FALSE;
return (SHNotifyDeleteFileA(pszFile) == ERROR_SUCCESS);
}
BOOL SHELL_DeleteFileW(LPCWSTR pszFile, BOOL bShowUI)
{
if (bShowUI && !SHELL_ConfirmDialogW(ASK_DELETE_FILE, pszFile))
return FALSE;
return (SHNotifyDeleteFileW(pszFile) == ERROR_SUCCESS);
}
/**************************************************************************
* Win32CreateDirectory [SHELL32.93]
*
* Creates a directory. Also triggers a change notify if one exists.
*
* PARAMS
* path [I] path to directory to create
*
* RETURNS
* TRUE if successful, FALSE otherwise
*
* NOTES
* Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
* This is Unicode on NT/2000
*/
static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
{
LPWSTR wPath;
DWORD retCode;
TRACE("(%s, %p)\n", debugstr_a(path), sec);
retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
if (!retCode)
{
retCode = SHNotifyCreateDirectoryW(wPath, sec);
SHELL32_FreeUnicodeBuf(wPath);
}
return retCode;
}
/**********************************************************************/
static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
{
TRACE("(%s, %p)\n", debugstr_w(path), sec);
if (CreateDirectoryW(path, sec))
{
SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
return ERROR_SUCCESS;
}
return GetLastError();
}
/**********************************************************************/
BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
{
if (SHELL_OsIsUnicode())
return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
}
/************************************************************************
* Win32RemoveDirectory [SHELL32.94]
*
* Deletes a directory. Also triggers a change notify if one exists.
*
* PARAMS
* path [I] path to directory to delete
*
* RETURNS
* TRUE if successful, FALSE otherwise
*
* NOTES
* Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
* This is Unicode on NT/2000
*/
static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
{
LPWSTR wPath;
DWORD retCode;
TRACE("(%s)\n", debugstr_a(path));
retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
if (!retCode)
{
retCode = SHNotifyRemoveDirectoryW(wPath);
SHELL32_FreeUnicodeBuf(wPath);
}
return retCode;
}
/***********************************************************************/
static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
{
BOOL ret;
TRACE("(%s)\n", debugstr_w(path));
ret = RemoveDirectoryW(path);
if (!ret)
{
/* Directory may be write protected */
DWORD dwAttr = GetFileAttributesW(path);
if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
ret = RemoveDirectoryW(path);
}
if (ret)
{
SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
return ERROR_SUCCESS;
}
return GetLastError();
}
/***********************************************************************/
BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
{
if (SHELL_OsIsUnicode())
return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
}
/************************************************************************
* Win32DeleteFile [SHELL32.164]
*
* Deletes a file. Also triggers a change notify if one exists.
*
* PARAMS
* path [I] path to file to delete
*
* RETURNS
* TRUE if successful, FALSE otherwise
*
* NOTES
* Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
* This is Unicode on NT/2000
*/
static DWORD SHNotifyDeleteFileA(LPCSTR path)
{
LPWSTR wPath;
DWORD retCode;
TRACE("(%s)\n", debugstr_a(path));
retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
if (!retCode)
{
retCode = SHNotifyDeleteFileW(wPath);
SHELL32_FreeUnicodeBuf(wPath);
}
return retCode;
}
/***********************************************************************/
static DWORD SHNotifyDeleteFileW(LPCWSTR path)
{
BOOL ret;
TRACE("(%s)\n", debugstr_w(path));
ret = DeleteFileW(path);
if (!ret)
{
/* File may be write protected or a system file */
DWORD dwAttr = GetFileAttributesW(path);
if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -