ReactOS 0.4.16-dev-336-gb667d82
recyclebin_generic.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: Recycle bin management
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Deals with a system-wide recycle bin
5 * COPYRIGHT: Copyright 2007 Hervé Poussineau (hpoussin@reactos.org)
6 * Copyright 2024 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
7 */
8
10
11class RecycleBinGeneric : public IRecycleBin
12{
13public:
15 virtual ~RecycleBinGeneric();
16
17 /* IUnknown methods */
21
22 /* IRecycleBin methods */
23 STDMETHODIMP DeleteFile(LPCWSTR szFileName) override;
25 STDMETHODIMP EnumObjects(IRecycleBinEnumList **ppEnumList) override;
26
27protected:
29};
30
32{
33 TRACE("(%p, %s, %p)\n", this, debugstr_guid(&riid), ppvObject);
34
35 if (!ppvObject)
36 return E_POINTER;
37
39 *ppvObject = static_cast<IRecycleBin *>(this);
40 else
41 {
42 *ppvObject = NULL;
43 return E_NOINTERFACE;
44 }
45
46 AddRef();
47 return S_OK;
48}
49
50STDMETHODIMP_(ULONG) RecycleBinGeneric::AddRef()
51{
52 ULONG refCount = InterlockedIncrement(&m_ref);
53 TRACE("(%p)\n", this);
54 return refCount;
55}
56
58{
59 TRACE("(%p)\n", this);
60}
61
62STDMETHODIMP_(ULONG) RecycleBinGeneric::Release()
63{
64 TRACE("(%p)\n", this);
65
66 ULONG refCount = InterlockedDecrement(&m_ref);
67 if (refCount == 0)
68 delete this;
69 return refCount;
70}
71
73{
74 TRACE("(%p, %s)\n", this, debugstr_w(szFileName));
75
76 /* Get full file name */
77 LPWSTR szFullName = NULL;
78 DWORD dwBufferLength = 0;
79 while (TRUE)
80 {
81 DWORD len = GetFullPathNameW(szFileName, dwBufferLength, szFullName, NULL);
82 if (len == 0)
83 {
84 if (szFullName)
85 CoTaskMemFree(szFullName);
87 }
88 else if (len < dwBufferLength)
89 break;
90 if (szFullName)
91 CoTaskMemFree(szFullName);
92 dwBufferLength = len;
93 szFullName = (LPWSTR)CoTaskMemAlloc(dwBufferLength * sizeof(WCHAR));
94 if (!szFullName)
96 }
97
98 /* Get associated volume path */
99 WCHAR szVolume[MAX_PATH];
100#ifndef __REACTOS__
101 if (!GetVolumePathNameW(szFullName, szVolume, _countof(szVolume)))
102 {
103 CoTaskMemFree(szFullName);
105 }
106#else
107 swprintf(szVolume, L"%c:\\", szFullName[0]);
108#endif
109
110 /* Skip namespace (if any): "\\.\" or "\\?\" */
111 if (szVolume[0] == '\\' &&
112 szVolume[1] == '\\' &&
113 (szVolume[2] == '.' || szVolume[2] == '?') &&
114 szVolume[3] == '\\')
115 {
116 MoveMemory(szVolume, &szVolume[4], (_countof(szVolume) - 4) * sizeof(WCHAR));
117 }
118
119 IRecycleBin *prb;
120 HRESULT hr = GetDefaultRecycleBin(szVolume, &prb);
121 if (!SUCCEEDED(hr))
122 {
123 CoTaskMemFree(szFullName);
124 return hr;
125 }
126
127 hr = prb->DeleteFile(szFullName);
128 CoTaskMemFree(szFullName);
129 prb->Release();
130 return hr;
131}
132
134{
135 TRACE("(%p)\n", this);
136
137 DWORD dwLogicalDrives = GetLogicalDrives();
138 if (dwLogicalDrives == 0)
140
141 for (DWORD i = 0; i < 'Z' - 'A' + 1; i++)
142 {
143 if (!(dwLogicalDrives & (1 << i)))
144 continue;
145
146 WCHAR szVolumeName[MAX_PATH];
147 swprintf(szVolumeName, L"%c:\\", L'A' + i);
148 if (GetDriveTypeW(szVolumeName) != DRIVE_FIXED)
149 continue;
150
151 IRecycleBin *prb;
152 HRESULT hr = GetDefaultRecycleBin(szVolumeName, &prb);
153 if (!SUCCEEDED(hr))
154 return hr;
155
156 hr = prb->EmptyRecycleBin();
157 prb->Release();
158 }
159
160 return S_OK;
161}
162
163STDMETHODIMP RecycleBinGeneric::EnumObjects(IRecycleBinEnumList **ppEnumList)
164{
165 TRACE("(%p, %p)\n", this, ppEnumList);
166 return RecycleBinGenericEnum_Constructor(ppEnumList);
167}
168
170 : m_ref(1)
171{
172}
173
176{
177 /* This RecycleBin implementation was introduced to be able to manage all
178 * drives at once, and instanciate the 'real' implementations when needed */
180 if (!pThis)
181 return E_OUTOFMEMORY;
182
183 *ppUnknown = static_cast<IRecycleBin *>(pThis);
184 return S_OK;
185}
#define InterlockedIncrement
Definition: armddk.h:53
#define InterlockedDecrement
Definition: armddk.h:52
#define EXTERN_C
Definition: basetyps.h:12
#define STDMETHODIMP
Definition: basetyps.h:43
#define STDMETHODIMP_(t)
Definition: basetyps.h:44
const GUID IID_IUnknown
_In_ BOOLEAN Release
Definition: cdrom.h:920
STDMETHODIMP EmptyRecycleBin() override
STDMETHODIMP_(ULONG) Release() override
STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject) override
STDMETHODIMP EnumObjects(IRecycleBinEnumList **ppEnumList) override
STDMETHODIMP_(ULONG) AddRef() override
STDMETHODIMP DeleteFile(LPCWSTR szFileName) override
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define MAX_PATH
Definition: compat.h:34
UINT WINAPI GetDriveTypeW(IN LPCWSTR lpRootPathName)
Definition: disk.c:497
BOOL WINAPI GetVolumePathNameW(IN LPCWSTR lpszFileName, IN LPWSTR lpszVolumePathName, IN DWORD cchBufferLength)
Definition: volume.c:815
DWORD WINAPI GetFullPathNameW(IN LPCWSTR lpFileName, IN DWORD nBufferLength, OUT LPWSTR lpBuffer, OUT LPWSTR *lpFilePart)
Definition: path.c:1106
HRESULT WINAPI GetDefaultRecycleBin(IN LPCWSTR pszVolume OPTIONAL, OUT IRecycleBin **pprb)
Definition: recyclebin.c:399
#define swprintf
Definition: precomp.h:40
unsigned long DWORD
Definition: ntddk_ex.h:95
GLenum GLsizei len
Definition: glext.h:6722
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
VOID WINAPI CoTaskMemFree(LPVOID ptr)
Definition: ifs.c:442
LPVOID WINAPI CoTaskMemAlloc(SIZE_T size)
Definition: ifs.c:426
REFIID riid
Definition: atlbase.h:39
ULONG Release()
#define S_OK
Definition: intsafe.h:52
#define SUCCEEDED(hr)
Definition: intsafe.h:50
#define debugstr_guid
Definition: kernel32.h:35
#define debugstr_w
Definition: kernel32.h:32
static ULONG WINAPI AddRef(IStream *iface)
Definition: clist.c:90
#define L(x)
Definition: ntvdm.h:50
long LONG
Definition: pedump.c:60
#define IsEqualIID(riid1, riid2)
Definition: guiddef.h:95
#define REFIID
Definition: guiddef.h:118
EXTERN_C const IID IID_IRecycleBin
Definition: recyclebin.h:246
EXTERN_C HRESULT RecycleBinGeneric_Constructor(OUT IUnknown **ppUnknown)
EXTERN_C HRESULT RecycleBinGenericEnum_Constructor(OUT IRecycleBinEnumList **pprbel)
HRESULT hr
Definition: shlfolder.c:183
#define _countof(array)
Definition: sndvol32.h:70
#define TRACE(s)
Definition: solgame.cpp:4
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
DWORD WINAPI GetLogicalDrives(void)
Definition: disk.c:110
#define MoveMemory
Definition: winbase.h:1734
#define DeleteFile
Definition: winbase.h:3789
#define DRIVE_FIXED
Definition: winbase.h:278
_In_ void _In_ PCCERT_CONTEXT _In_opt_ LPFILETIME _In_ DWORD _In_ DWORD _Outptr_opt_ void ** ppvObject
Definition: wincrypt.h:6082
#define E_NOINTERFACE
Definition: winerror.h:2364
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
#define E_POINTER
Definition: winerror.h:2365
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185