ReactOS 0.4.16-dev-2380-gf63df20
filelockbytes.c
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * File-based ILockBytes implementation
4 *
5 * Copyright 1999 Thuy Nguyen
6 * Copyright 2010 Vincent Povirk for CodeWeavers
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23#include <assert.h>
24#include <stdlib.h>
25#include <stdarg.h>
26#include <stdio.h>
27#include <string.h>
28#include <limits.h>
29
30#define COBJMACROS
31#include "windef.h"
32#include "winbase.h"
33#include "winuser.h"
34#include "winerror.h"
35#include "objbase.h"
36#include "ole2.h"
37
38#include "storage32.h"
39
40#include "wine/debug.h"
41
43
44typedef struct FileLockBytesImpl
45{
52
53static const ILockBytesVtbl FileLockBytesImpl_Vtbl;
54
56{
57 return CONTAINING_RECORD(iface, FileLockBytesImpl, ILockBytes_iface);
58}
59
60/***********************************************************
61 * Prototypes for private methods
62 */
63
64/****************************************************************************
65 * GetProtectMode
66 *
67 * This function will return a protection mode flag for a file-mapping object
68 * from the open flags of a file.
69 */
70static DWORD GetProtectMode(DWORD openFlags)
71{
72 switch(STGM_ACCESS_MODE(openFlags))
73 {
74 case STGM_WRITE:
75 case STGM_READWRITE:
76 return PAGE_READWRITE;
77 }
78 return PAGE_READONLY;
79}
80
81/******************************************************************************
82 * FileLockBytesImpl_Construct
83 *
84 * Initialize a big block object supported by a file.
85 */
87{
89 WCHAR fullpath[MAX_PATH];
90
92 return E_FAIL;
93
95
96 if (!This)
97 return E_OUTOFMEMORY;
98
99 This->ILockBytes_iface.lpVtbl = &FileLockBytesImpl_Vtbl;
100 This->ref = 1;
101 This->hfile = hFile;
102 This->flProtect = GetProtectMode(openFlags);
103
104 if(pwcsName) {
105 if (!GetFullPathNameW(pwcsName, MAX_PATH, fullpath, NULL))
106 {
107 lstrcpynW(fullpath, pwcsName, MAX_PATH);
108 }
109 This->pwcsName = HeapAlloc(GetProcessHeap(), 0,
110 (lstrlenW(fullpath)+1)*sizeof(WCHAR));
111 if (!This->pwcsName)
112 {
114 return E_OUTOFMEMORY;
115 }
116 lstrcpyW(This->pwcsName, fullpath);
117 }
118 else
119 This->pwcsName = NULL;
120
121 *pLockBytes = &This->ILockBytes_iface;
122
123 return S_OK;
124}
125
126/* ILockByte Interfaces */
127
129 void **ppvObject)
130{
131 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_ILockBytes))
132 *ppvObject = iface;
133 else
134 {
135 *ppvObject = NULL;
136 return E_NOINTERFACE;
137 }
138
139 IUnknown_AddRef((IUnknown*)*ppvObject);
140
141 return S_OK;
142}
143
145{
147 return InterlockedIncrement(&This->ref);
148}
149
151{
153 ULONG ref;
154
156
157 if (ref == 0)
158 {
159 CloseHandle(This->hfile);
160 HeapFree(GetProcessHeap(), 0, This->pwcsName);
162 }
163
164 return ref;
165}
166
167/******************************************************************************
168 * This method is part of the ILockBytes interface.
169 *
170 * It reads a block of information from the byte array at the specified
171 * offset.
172 *
173 * See the documentation of ILockBytes for more info.
174 */
176 ILockBytes* iface,
177 ULARGE_INTEGER ulOffset, /* [in] */
178 void* pv, /* [length_is][size_is][out] */
179 ULONG cb, /* [in] */
180 ULONG* pcbRead) /* [out] */
181{
183 ULONG bytes_left = cb;
184 LPBYTE readPtr = pv;
185 BOOL ret;
187 ULONG cbRead;
188
189 TRACE("%p, %ld, %p, %lu, %p.\n", iface, ulOffset.LowPart, pv, cb, pcbRead);
190
191 /* verify a sane environment */
192 if (!This) return E_FAIL;
193
194 if (pcbRead)
195 *pcbRead = 0;
196
197 offset.QuadPart = ulOffset.QuadPart;
198
200
201 if (!ret)
202 return STG_E_READFAULT;
203
204 while (bytes_left)
205 {
206 ret = ReadFile(This->hfile, readPtr, bytes_left, &cbRead, NULL);
207
208 if (!ret || cbRead == 0)
209 return STG_E_READFAULT;
210
211 if (pcbRead)
212 *pcbRead += cbRead;
213
214 bytes_left -= cbRead;
215 readPtr += cbRead;
216 }
217
218 TRACE("finished\n");
219 return S_OK;
220}
221
222/******************************************************************************
223 * This method is part of the ILockBytes interface.
224 *
225 * It writes the specified bytes at the specified offset.
226 * position. If the file is too small, it will be resized.
227 *
228 * See the documentation of ILockBytes for more info.
229 */
231 ILockBytes* iface,
232 ULARGE_INTEGER ulOffset, /* [in] */
233 const void* pv, /* [size_is][in] */
234 ULONG cb, /* [in] */
235 ULONG* pcbWritten) /* [out] */
236{
238 ULONG bytes_left = cb;
239 const BYTE *writePtr = pv;
240 BOOL ret;
242 ULONG cbWritten;
243
244 TRACE("%p, %ld, %p, %lu, %p.\n", iface, ulOffset.LowPart, pv, cb, pcbWritten);
245
246 /* verify a sane environment */
247 if (!This) return E_FAIL;
248
249 if (This->flProtect != PAGE_READWRITE)
250 return STG_E_ACCESSDENIED;
251
252 if (pcbWritten)
253 *pcbWritten = 0;
254
255 offset.QuadPart = ulOffset.QuadPart;
256
258
259 if (!ret)
260 return STG_E_WRITEFAULT;
261
262 while (bytes_left)
263 {
264 ret = WriteFile(This->hfile, writePtr, bytes_left, &cbWritten, NULL);
265
266 if (!ret)
267 return STG_E_WRITEFAULT;
268
269 if (pcbWritten)
270 *pcbWritten += cbWritten;
271
272 bytes_left -= cbWritten;
273 writePtr += cbWritten;
274 }
275
276 TRACE("finished\n");
277 return S_OK;
278}
279
281{
282 return S_OK;
283}
284
285/******************************************************************************
286 * ILockBytes_SetSize
287 *
288 * Sets the size of the file.
289 *
290 */
292{
294 HRESULT hr = S_OK;
295 LARGE_INTEGER newpos;
296
297 TRACE("new size %lu\n", newSize.LowPart);
298
299 newpos.QuadPart = newSize.QuadPart;
300 if (SetFilePointerEx(This->hfile, newpos, NULL, FILE_BEGIN))
301 {
302 SetEndOfFile(This->hfile);
303 }
304
305 return hr;
306}
307
309{
310 switch (GetLastError())
311 {
312 case ERROR_LOCK_VIOLATION: return STG_E_LOCKVIOLATION; break;
313 case ERROR_ACCESS_DENIED: return STG_E_ACCESSDENIED; break;
314 case ERROR_NOT_SUPPORTED: return STG_E_INVALIDFUNCTION; break;
315 default:
316 FIXME("no mapping for error %ld\n", GetLastError());
318 }
319}
320
322 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
323{
325 OVERLAPPED ol;
326 DWORD lock_flags = LOCKFILE_FAIL_IMMEDIATELY;
327
328 TRACE("ofs %lu count %lu flags %lx\n", libOffset.LowPart, cb.LowPart, dwLockType);
329
330 if (dwLockType & LOCK_WRITE)
332
333 if (dwLockType & (LOCK_EXCLUSIVE|LOCK_ONLYONCE))
334 lock_flags |= LOCKFILE_EXCLUSIVE_LOCK;
335
336 ol.hEvent = 0;
337 ol.Offset = libOffset.LowPart;
338 ol.OffsetHigh = libOffset.HighPart;
339
340 if (LockFileEx(This->hfile, lock_flags, 0, cb.LowPart, cb.HighPart, &ol))
341 return S_OK;
342 return get_lock_error();
343}
344
346 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
347{
349 OVERLAPPED ol;
350
351 TRACE("ofs %lu count %lu flags %lx\n", libOffset.LowPart, cb.LowPart, dwLockType);
352
353 if (dwLockType & LOCK_WRITE)
355
356 ol.hEvent = 0;
357 ol.Offset = libOffset.LowPart;
358 ol.OffsetHigh = libOffset.HighPart;
359
360 if (UnlockFileEx(This->hfile, 0, cb.LowPart, cb.HighPart, &ol))
361 return S_OK;
362 return get_lock_error();
363}
364
366 STATSTG *pstatstg, DWORD grfStatFlag)
367{
369
370 if (!(STATFLAG_NONAME & grfStatFlag) && This->pwcsName)
371 {
372 pstatstg->pwcsName =
373 CoTaskMemAlloc((lstrlenW(This->pwcsName)+1)*sizeof(WCHAR));
374
375 lstrcpyW(pstatstg->pwcsName, This->pwcsName);
376 }
377 else
378 pstatstg->pwcsName = NULL;
379
380 pstatstg->type = STGTY_LOCKBYTES;
381
382 pstatstg->cbSize.LowPart = GetFileSize(This->hfile, &pstatstg->cbSize.HighPart);
383 /* FIXME: If the implementation is exported, we'll need to set other fields. */
384
385 pstatstg->grfLocksSupported = LOCK_EXCLUSIVE|LOCK_ONLYONCE|WINE_LOCK_READ;
386
387 return S_OK;
388}
389
390static const ILockBytesVtbl FileLockBytesImpl_Vtbl = {
401};
#define InterlockedIncrement
Definition: armddk.h:53
#define InterlockedDecrement
Definition: armddk.h:52
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define FIXME(fmt,...)
Definition: precomp.h:53
const GUID IID_IUnknown
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
#define E_FAIL
Definition: ddrawi.h:102
#define NULL
Definition: types.h:112
#define CloseHandle
Definition: compat.h:739
#define GetProcessHeap()
Definition: compat.h:736
#define PAGE_READONLY
Definition: compat.h:138
#define FILE_BEGIN
Definition: compat.h:761
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define HeapAlloc
Definition: compat.h:733
#define ERROR_NOT_SUPPORTED
Definition: compat.h:100
#define MAX_PATH
Definition: compat.h:34
#define HeapFree(x, y, z)
Definition: compat.h:735
#define lstrcpyW
Definition: compat.h:749
#define ERROR_ACCESS_DENIED
Definition: compat.h:97
#define lstrcpynW
Definition: compat.h:738
#define lstrlenW
Definition: compat.h:750
BOOL WINAPI SetEndOfFile(HANDLE hFile)
Definition: fileinfo.c:988
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:331
BOOL WINAPI SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod)
Definition: fileinfo.c:177
BOOL WINAPI UnlockFileEx(IN HANDLE hFile, IN DWORD dwReserved, IN DWORD nNumberOfBytesToUnLockLow, IN DWORD nNumberOfBytesToUnLockHigh, IN LPOVERLAPPED lpOverlapped)
Definition: lock.c:183
BOOL WINAPI LockFileEx(IN HANDLE hFile, IN DWORD dwFlags, IN DWORD dwReserved, IN DWORD nNumberOfBytesToLockLow, IN DWORD nNumberOfBytesToLockHigh, IN LPOVERLAPPED lpOverlapped)
Definition: lock.c:82
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
DWORD WINAPI GetFullPathNameW(IN LPCWSTR lpFileName, IN DWORD nBufferLength, OUT LPWSTR lpBuffer, OUT LPWSTR *lpFilePart)
Definition: path.c:1106
static MonoProfilerRuntimeShutdownBeginCallback cb
Definition: metahost.c:118
return ret
Definition: mutex.c:146
static ULONG WINAPI FileLockBytesImpl_Release(ILockBytes *iface)
static HRESULT WINAPI FileLockBytesImpl_UnlockRegion(ILockBytes *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
static HRESULT get_lock_error(void)
static ULONG WINAPI FileLockBytesImpl_AddRef(ILockBytes *iface)
static HRESULT WINAPI FileLockBytesImpl_WriteAt(ILockBytes *iface, ULARGE_INTEGER ulOffset, const void *pv, ULONG cb, ULONG *pcbWritten)
static HRESULT WINAPI FileLockBytesImpl_Flush(ILockBytes *iface)
static const ILockBytesVtbl FileLockBytesImpl_Vtbl
Definition: filelockbytes.c:53
static HRESULT WINAPI FileLockBytesImpl_Stat(ILockBytes *iface, STATSTG *pstatstg, DWORD grfStatFlag)
static HRESULT WINAPI FileLockBytesImpl_ReadAt(ILockBytes *iface, ULARGE_INTEGER ulOffset, void *pv, ULONG cb, ULONG *pcbRead)
static HRESULT WINAPI FileLockBytesImpl_SetSize(ILockBytes *iface, ULARGE_INTEGER newSize)
static DWORD GetProtectMode(DWORD openFlags)
Definition: filelockbytes.c:70
HRESULT FileLockBytesImpl_Construct(HANDLE hFile, DWORD openFlags, LPCWSTR pwcsName, ILockBytes **pLockBytes)
Definition: filelockbytes.c:86
static FileLockBytesImpl * impl_from_ILockBytes(ILockBytes *iface)
Definition: filelockbytes.c:55
static HRESULT WINAPI FileLockBytesImpl_LockRegion(ILockBytes *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
static HRESULT WINAPI FileLockBytesImpl_QueryInterface(ILockBytes *iface, REFIID riid, void **ppvObject)
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLintptr offset
Definition: glext.h:5920
REFIID riid
Definition: atlbase.h:39
#define S_OK
Definition: intsafe.h:52
void *WINAPI CoTaskMemAlloc(SIZE_T size)
Definition: malloc.c:381
#define LOCKFILE_FAIL_IMMEDIATELY
Definition: minwinbase.h:100
#define LOCKFILE_EXCLUSIVE_LOCK
Definition: minwinbase.h:101
_In_ HANDLE hFile
Definition: mswsock.h:90
#define PAGE_READWRITE
Definition: nt_native.h:1307
#define STGM_READWRITE
Definition: objbase.h:936
#define STGM_WRITE
Definition: objbase.h:935
long LONG
Definition: pedump.c:60
#define IsEqualIID(riid1, riid2)
Definition: guiddef.h:95
#define REFIID
Definition: guiddef.h:118
HRESULT hr
Definition: shlfolder.c:183
#define TRACE(s)
Definition: solgame.cpp:4
#define WINE_LOCK_READ
Definition: storage32.h:523
#define STGM_ACCESS_MODE(stgm)
Definition: storage32.h:115
ILockBytes ILockBytes_iface
Definition: filelockbytes.c:46
DWORD OffsetHigh
Definition: minwinbase.h:226
DWORD Offset
Definition: minwinbase.h:225
HANDLE hEvent
Definition: minwinbase.h:230
$ULONG LowPart
Definition: ntbasedef.h:581
ULONGLONG QuadPart
Definition: ms-dtyp.idl:185
$ULONG HighPart
Definition: ntbasedef.h:582
Definition: send.c:48
unsigned char * LPBYTE
Definition: typedefs.h:53
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
LONGLONG QuadPart
Definition: typedefs.h:114
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define WINAPI
Definition: msvc.h:6
#define STG_E_LOCKVIOLATION
Definition: winerror.h:3673
#define E_NOINTERFACE
Definition: winerror.h:3479
#define STG_E_READFAULT
Definition: winerror.h:3671
#define ERROR_LOCK_VIOLATION
Definition: winerror.h:258
#define STG_E_ACCESSDENIED
Definition: winerror.h:3663
#define STG_E_WRITEFAULT
Definition: winerror.h:3670
#define STG_E_INVALIDFUNCTION
Definition: winerror.h:3659
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
unsigned char BYTE
Definition: xxhash.c:193