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
24 #include "storage32.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
28 typedef struct FileLockBytesImpl
30 ILockBytes ILockBytes_iface
;
37 static const ILockBytesVtbl FileLockBytesImpl_Vtbl
;
39 static inline FileLockBytesImpl
*impl_from_ILockBytes(ILockBytes
*iface
)
41 return CONTAINING_RECORD(iface
, FileLockBytesImpl
, ILockBytes_iface
);
44 /***********************************************************
45 * Prototypes for private methods
48 /* Note that this evaluates a and b multiple times, so don't
49 * pass expressions with side effects. */
50 #define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
52 /****************************************************************************
55 * This function will return a protection mode flag for a file-mapping object
56 * from the open flags of a file.
58 static DWORD
GetProtectMode(DWORD openFlags
)
60 switch(STGM_ACCESS_MODE(openFlags
))
64 return PAGE_READWRITE
;
69 /******************************************************************************
70 * FileLockBytesImpl_Construct
72 * Initialize a big block object supported by a file.
74 HRESULT
FileLockBytesImpl_Construct(HANDLE hFile
, DWORD openFlags
, LPCWSTR pwcsName
, ILockBytes
**pLockBytes
)
76 FileLockBytesImpl
*This
;
77 WCHAR fullpath
[MAX_PATH
];
79 if (hFile
== INVALID_HANDLE_VALUE
)
82 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileLockBytesImpl
));
87 This
->ILockBytes_iface
.lpVtbl
= &FileLockBytesImpl_Vtbl
;
90 This
->flProtect
= GetProtectMode(openFlags
);
93 if (!GetFullPathNameW(pwcsName
, MAX_PATH
, fullpath
, NULL
))
95 lstrcpynW(fullpath
, pwcsName
, MAX_PATH
);
97 This
->pwcsName
= HeapAlloc(GetProcessHeap(), 0,
98 (lstrlenW(fullpath
)+1)*sizeof(WCHAR
));
101 HeapFree(GetProcessHeap(), 0, This
);
102 return E_OUTOFMEMORY
;
104 strcpyW(This
->pwcsName
, fullpath
);
107 This
->pwcsName
= NULL
;
109 *pLockBytes
= &This
->ILockBytes_iface
;
114 /* ILockByte Interfaces */
116 static HRESULT WINAPI
FileLockBytesImpl_QueryInterface(ILockBytes
*iface
, REFIID riid
,
119 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_ILockBytes
))
124 return E_NOINTERFACE
;
127 IUnknown_AddRef((IUnknown
*)*ppvObject
);
132 static ULONG WINAPI
FileLockBytesImpl_AddRef(ILockBytes
*iface
)
134 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
135 return InterlockedIncrement(&This
->ref
);
138 static ULONG WINAPI
FileLockBytesImpl_Release(ILockBytes
*iface
)
140 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
143 ref
= InterlockedDecrement(&This
->ref
);
147 CloseHandle(This
->hfile
);
148 HeapFree(GetProcessHeap(), 0, This
->pwcsName
);
149 HeapFree(GetProcessHeap(), 0, This
);
155 /******************************************************************************
156 * This method is part of the ILockBytes interface.
158 * It reads a block of information from the byte array at the specified
161 * See the documentation of ILockBytes for more info.
163 static HRESULT WINAPI
FileLockBytesImpl_ReadAt(
165 ULARGE_INTEGER ulOffset
, /* [in] */
166 void* pv
, /* [length_is][size_is][out] */
168 ULONG
* pcbRead
) /* [out] */
170 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
171 ULONG bytes_left
= cb
;
174 LARGE_INTEGER offset
;
177 TRACE("(%p)-> %i %p %i %p\n",This
, ulOffset
.u
.LowPart
, pv
, cb
, pcbRead
);
179 /* verify a sane environment */
180 if (!This
) return E_FAIL
;
185 offset
.QuadPart
= ulOffset
.QuadPart
;
187 ret
= SetFilePointerEx(This
->hfile
, offset
, NULL
, FILE_BEGIN
);
190 return STG_E_READFAULT
;
194 ret
= ReadFile(This
->hfile
, readPtr
, bytes_left
, &cbRead
, NULL
);
196 if (!ret
|| cbRead
== 0)
197 return STG_E_READFAULT
;
202 bytes_left
-= cbRead
;
210 /******************************************************************************
211 * This method is part of the ILockBytes interface.
213 * It writes the specified bytes at the specified offset.
214 * position. If the file is too small, it will be resized.
216 * See the documentation of ILockBytes for more info.
218 static HRESULT WINAPI
FileLockBytesImpl_WriteAt(
220 ULARGE_INTEGER ulOffset
, /* [in] */
221 const void* pv
, /* [size_is][in] */
223 ULONG
* pcbWritten
) /* [out] */
225 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
226 ULONG bytes_left
= cb
;
227 const BYTE
*writePtr
= pv
;
229 LARGE_INTEGER offset
;
232 TRACE("(%p)-> %i %p %i %p\n",This
, ulOffset
.u
.LowPart
, pv
, cb
, pcbWritten
);
234 /* verify a sane environment */
235 if (!This
) return E_FAIL
;
237 if (This
->flProtect
!= PAGE_READWRITE
)
238 return STG_E_ACCESSDENIED
;
243 offset
.QuadPart
= ulOffset
.QuadPart
;
245 ret
= SetFilePointerEx(This
->hfile
, offset
, NULL
, FILE_BEGIN
);
248 return STG_E_WRITEFAULT
;
252 ret
= WriteFile(This
->hfile
, writePtr
, bytes_left
, &cbWritten
, NULL
);
255 return STG_E_WRITEFAULT
;
258 *pcbWritten
+= cbWritten
;
260 bytes_left
-= cbWritten
;
261 writePtr
+= cbWritten
;
268 static HRESULT WINAPI
FileLockBytesImpl_Flush(ILockBytes
* iface
)
273 /******************************************************************************
276 * Sets the size of the file.
279 static HRESULT WINAPI
FileLockBytesImpl_SetSize(ILockBytes
* iface
, ULARGE_INTEGER newSize
)
281 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
283 LARGE_INTEGER newpos
;
285 TRACE("new size %u\n", newSize
.u
.LowPart
);
287 newpos
.QuadPart
= newSize
.QuadPart
;
288 if (SetFilePointerEx(This
->hfile
, newpos
, NULL
, FILE_BEGIN
))
290 SetEndOfFile(This
->hfile
);
296 static HRESULT
get_lock_error(void)
298 switch (GetLastError())
300 case ERROR_LOCK_VIOLATION
: return STG_E_LOCKVIOLATION
; break;
301 case ERROR_ACCESS_DENIED
: return STG_E_ACCESSDENIED
; break;
302 case ERROR_NOT_SUPPORTED
: return STG_E_INVALIDFUNCTION
; break;
304 FIXME("no mapping for error %d\n", GetLastError());
305 return STG_E_INVALIDFUNCTION
;
309 static HRESULT WINAPI
FileLockBytesImpl_LockRegion(ILockBytes
* iface
,
310 ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
312 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
314 DWORD lock_flags
= LOCKFILE_FAIL_IMMEDIATELY
;
316 TRACE("ofs %u count %u flags %x\n", libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
318 if (dwLockType
& LOCK_WRITE
)
319 return STG_E_INVALIDFUNCTION
;
321 if (dwLockType
& (LOCK_EXCLUSIVE
|LOCK_ONLYONCE
))
322 lock_flags
|= LOCKFILE_EXCLUSIVE_LOCK
;
325 ol
.u
.s
.Offset
= libOffset
.u
.LowPart
;
326 ol
.u
.s
.OffsetHigh
= libOffset
.u
.HighPart
;
328 if (LockFileEx(This
->hfile
, lock_flags
, 0, cb
.u
.LowPart
, cb
.u
.HighPart
, &ol
))
330 return get_lock_error();
333 HRESULT
FileLockBytesImpl_LockRegionSync(ILockBytes
* iface
,
334 ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
)
336 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
339 if (iface
->lpVtbl
!= &FileLockBytesImpl_Vtbl
)
343 ol
.u
.s
.Offset
= libOffset
.u
.LowPart
;
344 ol
.u
.s
.OffsetHigh
= libOffset
.u
.HighPart
;
346 if (LockFileEx(This
->hfile
, LOCKFILE_EXCLUSIVE_LOCK
, 0, cb
.u
.LowPart
, cb
.u
.HighPart
, &ol
))
348 return get_lock_error();
351 static HRESULT WINAPI
FileLockBytesImpl_UnlockRegion(ILockBytes
* iface
,
352 ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
354 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
357 TRACE("ofs %u count %u flags %x\n", libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
359 if (dwLockType
& LOCK_WRITE
)
360 return STG_E_INVALIDFUNCTION
;
363 ol
.u
.s
.Offset
= libOffset
.u
.LowPart
;
364 ol
.u
.s
.OffsetHigh
= libOffset
.u
.HighPart
;
366 if (UnlockFileEx(This
->hfile
, 0, cb
.u
.LowPart
, cb
.u
.HighPart
, &ol
))
368 return get_lock_error();
371 static HRESULT WINAPI
FileLockBytesImpl_Stat(ILockBytes
* iface
,
372 STATSTG
*pstatstg
, DWORD grfStatFlag
)
374 FileLockBytesImpl
* This
= impl_from_ILockBytes(iface
);
376 if (!(STATFLAG_NONAME
& grfStatFlag
) && This
->pwcsName
)
379 CoTaskMemAlloc((lstrlenW(This
->pwcsName
)+1)*sizeof(WCHAR
));
381 strcpyW(pstatstg
->pwcsName
, This
->pwcsName
);
384 pstatstg
->pwcsName
= NULL
;
386 pstatstg
->type
= STGTY_LOCKBYTES
;
388 pstatstg
->cbSize
.u
.LowPart
= GetFileSize(This
->hfile
, &pstatstg
->cbSize
.u
.HighPart
);
389 /* FIXME: If the implementation is exported, we'll need to set other fields. */
394 static const ILockBytesVtbl FileLockBytesImpl_Vtbl
= {
395 FileLockBytesImpl_QueryInterface
,
396 FileLockBytesImpl_AddRef
,
397 FileLockBytesImpl_Release
,
398 FileLockBytesImpl_ReadAt
,
399 FileLockBytesImpl_WriteAt
,
400 FileLockBytesImpl_Flush
,
401 FileLockBytesImpl_SetSize
,
402 FileLockBytesImpl_LockRegion
,
403 FileLockBytesImpl_UnlockRegion
,
404 FileLockBytesImpl_Stat