1 /******************************************************************************
3 * File-based ILockBytes implementation
5 * Copyright 1999 Thuy Nguyen
6 * Copyright 2010 Vincent Povirk for CodeWeavers
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.
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.
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
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
41 #include "storage32.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
47 typedef struct FileLockBytesImpl
49 ILockBytes ILockBytes_iface
;
56 static const ILockBytesVtbl FileLockBytesImpl_Vtbl
;
58 static inline FileLockBytesImpl
*impl_from_ILockBytes(ILockBytes
*iface
)
60 return CONTAINING_RECORD(iface
, FileLockBytesImpl
, ILockBytes_iface
);
63 /***********************************************************
64 * Prototypes for private methods
67 /****************************************************************************
70 * This function will return a protection mode flag for a file-mapping object
71 * from the open flags of a file.
73 static DWORD
GetProtectMode(DWORD openFlags
)
75 switch(STGM_ACCESS_MODE(openFlags
))
79 return PAGE_READWRITE
;
84 /******************************************************************************
85 * FileLockBytesImpl_Construct
87 * Initialize a big block object supported by a file.
89 HRESULT
FileLockBytesImpl_Construct(HANDLE hFile
, DWORD openFlags
, LPCWSTR pwcsName
, ILockBytes
**pLockBytes
)
91 FileLockBytesImpl
*This
;
92 WCHAR fullpath
[MAX_PATH
];
94 if (hFile
== INVALID_HANDLE_VALUE
)
97 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileLockBytesImpl
));
100 return E_OUTOFMEMORY
;
102 This
->ILockBytes_iface
.lpVtbl
= &FileLockBytesImpl_Vtbl
;
105 This
->flProtect
= GetProtectMode(openFlags
);
108 if (!GetFullPathNameW(pwcsName
, MAX_PATH
, fullpath
, NULL
))
110 lstrcpynW(fullpath
, pwcsName
, MAX_PATH
);
112 This
->pwcsName
= HeapAlloc(GetProcessHeap(), 0,
113 (lstrlenW(fullpath
)+1)*sizeof(WCHAR
));
116 HeapFree(GetProcessHeap(), 0, This
);
117 return E_OUTOFMEMORY
;
119 lstrcpyW(This
->pwcsName
, fullpath
);
122 This
->pwcsName
= NULL
;
124 *pLockBytes
= &This
->ILockBytes_iface
;
129 /* ILockByte Interfaces */
131 static HRESULT WINAPI
FileLockBytesImpl_QueryInterface(ILockBytes
*iface
, REFIID riid
,
134 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_ILockBytes
))
139 return E_NOINTERFACE
;
142 IUnknown_AddRef((IUnknown
*)*ppvObject
);
147 static ULONG WINAPI
FileLockBytesImpl_AddRef(ILockBytes
*iface
)
149 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
150 return InterlockedIncrement(&This
->ref
);
153 static ULONG WINAPI
FileLockBytesImpl_Release(ILockBytes
*iface
)
155 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
158 ref
= InterlockedDecrement(&This
->ref
);
162 CloseHandle(This
->hfile
);
163 HeapFree(GetProcessHeap(), 0, This
->pwcsName
);
164 HeapFree(GetProcessHeap(), 0, This
);
170 /******************************************************************************
171 * This method is part of the ILockBytes interface.
173 * It reads a block of information from the byte array at the specified
176 * See the documentation of ILockBytes for more info.
178 static HRESULT WINAPI
FileLockBytesImpl_ReadAt(
180 ULARGE_INTEGER ulOffset
, /* [in] */
181 void* pv
, /* [length_is][size_is][out] */
183 ULONG
* pcbRead
) /* [out] */
185 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
186 ULONG bytes_left
= cb
;
189 LARGE_INTEGER offset
;
192 TRACE("(%p)-> %i %p %i %p\n",This
, ulOffset
.u
.LowPart
, pv
, cb
, pcbRead
);
194 /* verify a sane environment */
195 if (!This
) return E_FAIL
;
200 offset
.QuadPart
= ulOffset
.QuadPart
;
202 ret
= SetFilePointerEx(This
->hfile
, offset
, NULL
, FILE_BEGIN
);
205 return STG_E_READFAULT
;
209 ret
= ReadFile(This
->hfile
, readPtr
, bytes_left
, &cbRead
, NULL
);
211 if (!ret
|| cbRead
== 0)
212 return STG_E_READFAULT
;
217 bytes_left
-= cbRead
;
225 /******************************************************************************
226 * This method is part of the ILockBytes interface.
228 * It writes the specified bytes at the specified offset.
229 * position. If the file is too small, it will be resized.
231 * See the documentation of ILockBytes for more info.
233 static HRESULT WINAPI
FileLockBytesImpl_WriteAt(
235 ULARGE_INTEGER ulOffset
, /* [in] */
236 const void* pv
, /* [size_is][in] */
238 ULONG
* pcbWritten
) /* [out] */
240 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
241 ULONG bytes_left
= cb
;
242 const BYTE
*writePtr
= pv
;
244 LARGE_INTEGER offset
;
247 TRACE("(%p)-> %i %p %i %p\n",This
, ulOffset
.u
.LowPart
, pv
, cb
, pcbWritten
);
249 /* verify a sane environment */
250 if (!This
) return E_FAIL
;
252 if (This
->flProtect
!= PAGE_READWRITE
)
253 return STG_E_ACCESSDENIED
;
258 offset
.QuadPart
= ulOffset
.QuadPart
;
260 ret
= SetFilePointerEx(This
->hfile
, offset
, NULL
, FILE_BEGIN
);
263 return STG_E_WRITEFAULT
;
267 ret
= WriteFile(This
->hfile
, writePtr
, bytes_left
, &cbWritten
, NULL
);
270 return STG_E_WRITEFAULT
;
273 *pcbWritten
+= cbWritten
;
275 bytes_left
-= cbWritten
;
276 writePtr
+= cbWritten
;
283 static HRESULT WINAPI
FileLockBytesImpl_Flush(ILockBytes
* iface
)
288 /******************************************************************************
291 * Sets the size of the file.
294 static HRESULT WINAPI
FileLockBytesImpl_SetSize(ILockBytes
* iface
, ULARGE_INTEGER newSize
)
296 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
298 LARGE_INTEGER newpos
;
300 TRACE("new size %u\n", newSize
.u
.LowPart
);
302 newpos
.QuadPart
= newSize
.QuadPart
;
303 if (SetFilePointerEx(This
->hfile
, newpos
, NULL
, FILE_BEGIN
))
305 SetEndOfFile(This
->hfile
);
311 static HRESULT
get_lock_error(void)
313 switch (GetLastError())
315 case ERROR_LOCK_VIOLATION
: return STG_E_LOCKVIOLATION
; break;
316 case ERROR_ACCESS_DENIED
: return STG_E_ACCESSDENIED
; break;
317 case ERROR_NOT_SUPPORTED
: return STG_E_INVALIDFUNCTION
; break;
319 FIXME("no mapping for error %d\n", GetLastError());
320 return STG_E_INVALIDFUNCTION
;
324 static HRESULT WINAPI
FileLockBytesImpl_LockRegion(ILockBytes
* iface
,
325 ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
327 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
329 DWORD lock_flags
= LOCKFILE_FAIL_IMMEDIATELY
;
331 TRACE("ofs %u count %u flags %x\n", libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
333 if (dwLockType
& LOCK_WRITE
)
334 return STG_E_INVALIDFUNCTION
;
336 if (dwLockType
& (LOCK_EXCLUSIVE
|LOCK_ONLYONCE
))
337 lock_flags
|= LOCKFILE_EXCLUSIVE_LOCK
;
340 ol
.u
.s
.Offset
= libOffset
.u
.LowPart
;
341 ol
.u
.s
.OffsetHigh
= libOffset
.u
.HighPart
;
343 if (LockFileEx(This
->hfile
, lock_flags
, 0, cb
.u
.LowPart
, cb
.u
.HighPart
, &ol
))
345 return get_lock_error();
348 static HRESULT WINAPI
FileLockBytesImpl_UnlockRegion(ILockBytes
* iface
,
349 ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
351 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
354 TRACE("ofs %u count %u flags %x\n", libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
356 if (dwLockType
& LOCK_WRITE
)
357 return STG_E_INVALIDFUNCTION
;
360 ol
.u
.s
.Offset
= libOffset
.u
.LowPart
;
361 ol
.u
.s
.OffsetHigh
= libOffset
.u
.HighPart
;
363 if (UnlockFileEx(This
->hfile
, 0, cb
.u
.LowPart
, cb
.u
.HighPart
, &ol
))
365 return get_lock_error();
368 static HRESULT WINAPI
FileLockBytesImpl_Stat(ILockBytes
* iface
,
369 STATSTG
*pstatstg
, DWORD grfStatFlag
)
371 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
373 if (!(STATFLAG_NONAME
& grfStatFlag
) && This
->pwcsName
)
376 CoTaskMemAlloc((lstrlenW(This
->pwcsName
)+1)*sizeof(WCHAR
));
378 lstrcpyW(pstatstg
->pwcsName
, This
->pwcsName
);
381 pstatstg
->pwcsName
= NULL
;
383 pstatstg
->type
= STGTY_LOCKBYTES
;
385 pstatstg
->cbSize
.u
.LowPart
= GetFileSize(This
->hfile
, &pstatstg
->cbSize
.u
.HighPart
);
386 /* FIXME: If the implementation is exported, we'll need to set other fields. */
388 pstatstg
->grfLocksSupported
= LOCK_EXCLUSIVE
|LOCK_ONLYONCE
|WINE_LOCK_READ
;
393 static const ILockBytesVtbl FileLockBytesImpl_Vtbl
= {
394 FileLockBytesImpl_QueryInterface
,
395 FileLockBytesImpl_AddRef
,
396 FileLockBytesImpl_Release
,
397 FileLockBytesImpl_ReadAt
,
398 FileLockBytesImpl_WriteAt
,
399 FileLockBytesImpl_Flush
,
400 FileLockBytesImpl_SetSize
,
401 FileLockBytesImpl_LockRegion
,
402 FileLockBytesImpl_UnlockRegion
,
403 FileLockBytesImpl_Stat