ntdll: Fix race in NtRead/WriteFile.
[wine/testsucceed.git] / dlls / ole32 / stg_bigblockfile.c
blob77abbf6286bef392ad8320f367156b82dd58c634
1 /******************************************************************************
3 * BigBlockFile
5 * This is the implementation of a file that consists of blocks of
6 * a predetermined size.
7 * This class is used in the Compound File implementation of the
8 * IStorage and IStream interfaces. It provides the functionality
9 * to read and write any blocks in the file as well as setting and
10 * obtaining the size of the file.
11 * The blocks are indexed sequentially from the start of the file
12 * starting with -1.
14 * TODO:
15 * - Support for a transacted mode
17 * Copyright 1999 Thuy Nguyen
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2.1 of the License, or (at your option) any later version.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #include <assert.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <limits.h>
41 #define COBJMACROS
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winuser.h"
48 #include "winerror.h"
49 #include "objbase.h"
50 #include "ole2.h"
52 #include "storage32.h"
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(storage);
58 /***********************************************************
59 * Data structures used internally by the BigBlockFile
60 * class.
63 /* We map in PAGE_SIZE-sized chunks. Must be a multiple of 4096. */
64 #define PAGE_SIZE 131072
66 /* We keep a list of recently-discarded pages. This controls the
67 * size of that list. */
68 #define MAX_VICTIM_PAGES 16
70 /***
71 * This structure identifies the paged that are mapped
72 * from the file and their position in memory. It is
73 * also used to hold a reference count to those pages.
75 * page_index identifies which PAGE_SIZE chunk from the
76 * file this mapping represents. (The mappings are always
77 * PAGE_SIZE-aligned.)
80 typedef struct MappedPage MappedPage;
81 struct MappedPage
83 MappedPage *next;
84 MappedPage *prev;
86 DWORD page_index;
87 DWORD mapped_bytes;
88 LPVOID lpBytes;
89 LONG refcnt;
92 struct BigBlockFile
94 BOOL fileBased;
95 ULARGE_INTEGER filesize;
96 HANDLE hfile;
97 HANDLE hfilemap;
98 DWORD flProtect;
99 MappedPage *maplist;
100 MappedPage *victimhead, *victimtail;
101 ULONG num_victim_pages;
102 ILockBytes *pLkbyt;
105 /***********************************************************
106 * Prototypes for private methods
109 /* Note that this evaluates a and b multiple times, so don't
110 * pass expressions with side effects. */
111 #define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
113 /******************************************************************************
114 * BIGBLOCKFILE_FileInit
116 * Initialize a big block object supported by a file.
118 static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile)
120 This->pLkbyt = NULL;
121 This->hfile = hFile;
123 if (This->hfile == INVALID_HANDLE_VALUE)
124 return FALSE;
126 This->filesize.u.LowPart = GetFileSize(This->hfile,
127 &This->filesize.u.HighPart);
129 if( This->filesize.u.LowPart || This->filesize.u.HighPart )
131 /* create the file mapping object
133 This->hfilemap = CreateFileMappingA(This->hfile,
134 NULL,
135 This->flProtect,
136 0, 0,
137 NULL);
139 if (!This->hfilemap)
141 CloseHandle(This->hfile);
142 return FALSE;
145 else
146 This->hfilemap = NULL;
148 This->maplist = NULL;
150 TRACE("file len %u\n", This->filesize.u.LowPart);
152 return TRUE;
155 /******************************************************************************
156 * BIGBLOCKFILE_LockBytesInit
158 * Initialize a big block object supported by an ILockBytes.
160 static BOOL BIGBLOCKFILE_LockBytesInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt)
162 This->hfile = 0;
163 This->hfilemap = 0;
164 This->pLkbyt = plkbyt;
165 ILockBytes_AddRef(This->pLkbyt);
167 /* We'll get the size directly with ILockBytes_Stat */
168 This->filesize.QuadPart = 0;
170 TRACE("ILockBytes %p\n", This->pLkbyt);
171 return TRUE;
174 /******************************************************************************
175 * BIGBLOCKFILE_FindPageInList [PRIVATE]
178 static MappedPage *BIGBLOCKFILE_FindPageInList(MappedPage *head,
179 ULONG page_index)
181 for (; head != NULL; head = head->next)
183 if (head->page_index == page_index)
185 InterlockedIncrement(&head->refcnt);
186 break;
190 return head;
194 static void BIGBLOCKFILE_UnlinkPage(MappedPage *page)
196 if (page->next) page->next->prev = page->prev;
197 if (page->prev) page->prev->next = page->next;
200 static void BIGBLOCKFILE_LinkHeadPage(MappedPage **head, MappedPage *page)
202 if (*head) (*head)->prev = page;
203 page->next = *head;
204 page->prev = NULL;
205 *head = page;
208 static BOOL BIGBLOCKFILE_MapPage(BigBlockFile *This, MappedPage *page)
210 DWORD lowoffset = PAGE_SIZE * page->page_index;
211 DWORD numBytesToMap;
212 DWORD desired_access;
214 assert(This->fileBased);
216 if( !This->hfilemap )
217 return FALSE;
219 if (lowoffset + PAGE_SIZE > This->filesize.u.LowPart)
220 numBytesToMap = This->filesize.u.LowPart - lowoffset;
221 else
222 numBytesToMap = PAGE_SIZE;
224 if (This->flProtect == PAGE_READONLY)
225 desired_access = FILE_MAP_READ;
226 else
227 desired_access = FILE_MAP_WRITE;
229 page->lpBytes = MapViewOfFile(This->hfilemap, desired_access, 0,
230 lowoffset, numBytesToMap);
231 page->mapped_bytes = numBytesToMap;
233 TRACE("mapped page %u to %p\n", page->page_index, page->lpBytes);
235 return page->lpBytes != NULL;
239 static MappedPage *BIGBLOCKFILE_CreatePage(BigBlockFile *This, ULONG page_index)
241 MappedPage *page;
243 page = HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage));
244 if (page == NULL)
245 return NULL;
247 page->page_index = page_index;
248 page->refcnt = 1;
250 page->next = NULL;
251 page->prev = NULL;
253 if (!BIGBLOCKFILE_MapPage(This, page))
255 HeapFree(GetProcessHeap(),0,page);
256 return NULL;
259 return page;
263 /******************************************************************************
264 * BIGBLOCKFILE_GetMappedView [PRIVATE]
266 * Gets the page requested if it is already mapped.
267 * If it's not already mapped, this method will map it
269 static void * BIGBLOCKFILE_GetMappedView(
270 LPBIGBLOCKFILE This,
271 DWORD page_index)
273 MappedPage *page;
275 page = BIGBLOCKFILE_FindPageInList(This->maplist, page_index);
276 if (!page)
278 page = BIGBLOCKFILE_FindPageInList(This->victimhead, page_index);
279 if (page)
281 This->num_victim_pages--;
285 if (page)
287 /* If the page is not already at the head of the list, move
288 * it there. (Also moves pages from victim to main list.) */
289 if (This->maplist != page)
291 if (This->victimhead == page) This->victimhead = page->next;
292 if (This->victimtail == page) This->victimtail = page->prev;
294 BIGBLOCKFILE_UnlinkPage(page);
296 BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
299 return page;
302 page = BIGBLOCKFILE_CreatePage(This, page_index);
303 if (!page) return NULL;
305 BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
307 return page;
310 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This, MappedPage *page)
312 TRACE("%d at %p\n", page->page_index, page->lpBytes);
314 assert(This->fileBased);
316 if (page->refcnt > 0)
317 ERR("unmapping inuse page %p\n", page->lpBytes);
319 if (page->lpBytes)
320 UnmapViewOfFile(page->lpBytes);
322 page->lpBytes = NULL;
325 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This, MappedPage *page)
327 BIGBLOCKFILE_UnmapPage(This, page);
329 HeapFree(GetProcessHeap(), 0, page);
332 /******************************************************************************
333 * BIGBLOCKFILE_ReleaseMappedPage [PRIVATE]
335 * Decrements the reference count of the mapped page.
337 static void BIGBLOCKFILE_ReleaseMappedPage(
338 LPBIGBLOCKFILE This,
339 MappedPage *page)
341 assert(This != NULL);
342 assert(page != NULL);
344 /* If the page is no longer refenced, move it to the victim list.
345 * If the victim list is too long, kick somebody off. */
346 if (!InterlockedDecrement(&page->refcnt))
348 if (This->maplist == page) This->maplist = page->next;
350 BIGBLOCKFILE_UnlinkPage(page);
352 if (MAX_VICTIM_PAGES > 0)
354 if (This->num_victim_pages >= MAX_VICTIM_PAGES)
356 MappedPage *victim = This->victimtail;
357 if (victim)
359 This->victimtail = victim->prev;
360 if (This->victimhead == victim)
361 This->victimhead = victim->next;
363 BIGBLOCKFILE_UnlinkPage(victim);
364 BIGBLOCKFILE_DeletePage(This, victim);
367 else This->num_victim_pages++;
369 BIGBLOCKFILE_LinkHeadPage(&This->victimhead, page);
370 if (This->victimtail == NULL) This->victimtail = page;
372 else
373 BIGBLOCKFILE_DeletePage(This, page);
377 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This, MappedPage *list)
379 while (list != NULL)
381 MappedPage *next = list->next;
383 BIGBLOCKFILE_DeletePage(This, list);
385 list = next;
389 /******************************************************************************
390 * BIGBLOCKFILE_FreeAllMappedPages [PRIVATE]
392 * Unmap all currently mapped pages.
393 * Empty mapped pages list.
395 static void BIGBLOCKFILE_FreeAllMappedPages(
396 LPBIGBLOCKFILE This)
398 BIGBLOCKFILE_DeleteList(This, This->maplist);
399 BIGBLOCKFILE_DeleteList(This, This->victimhead);
401 This->maplist = NULL;
402 This->victimhead = NULL;
403 This->victimtail = NULL;
404 This->num_victim_pages = 0;
407 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This, MappedPage *list)
409 for (; list != NULL; list = list->next)
411 BIGBLOCKFILE_UnmapPage(This, list);
415 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This)
417 BIGBLOCKFILE_UnmapList(This, This->maplist);
418 BIGBLOCKFILE_UnmapList(This, This->victimhead);
421 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This, MappedPage *list)
423 while (list != NULL)
425 MappedPage *next = list->next;
427 if (list->page_index * PAGE_SIZE > This->filesize.u.LowPart)
429 TRACE("discarding %u\n", list->page_index);
431 /* page is entirely outside of the file, delete it */
432 BIGBLOCKFILE_UnlinkPage(list);
433 BIGBLOCKFILE_DeletePage(This, list);
435 else
437 /* otherwise, remap it */
438 BIGBLOCKFILE_MapPage(This, list);
441 list = next;
445 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This)
447 BIGBLOCKFILE_RemapList(This, This->maplist);
448 BIGBLOCKFILE_RemapList(This, This->victimhead);
451 /****************************************************************************
452 * BIGBLOCKFILE_GetProtectMode
454 * This function will return a protection mode flag for a file-mapping object
455 * from the open flags of a file.
457 static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags)
459 switch(STGM_ACCESS_MODE(openFlags))
461 case STGM_WRITE:
462 case STGM_READWRITE:
463 return PAGE_READWRITE;
465 return PAGE_READONLY;
469 /* ILockByte Interfaces */
471 /******************************************************************************
472 * This method is part of the ILockBytes interface.
474 * It reads a block of information from the byte array at the specified
475 * offset.
477 * See the documentation of ILockBytes for more info.
479 static HRESULT ImplBIGBLOCKFILE_ReadAt(
480 BigBlockFile* const This,
481 ULARGE_INTEGER ulOffset, /* [in] */
482 void* pv, /* [length_is][size_is][out] */
483 ULONG cb, /* [in] */
484 ULONG* pcbRead) /* [out] */
486 ULONG first_page = ulOffset.u.LowPart / PAGE_SIZE;
487 ULONG offset_in_page = ulOffset.u.LowPart % PAGE_SIZE;
488 ULONG bytes_left = cb;
489 ULONG page_index = first_page;
490 ULONG bytes_from_page;
491 LPVOID writePtr = pv;
493 HRESULT rc = S_OK;
495 TRACE("(%p)-> %i %p %i %p\n",This, ulOffset.u.LowPart, pv, cb, pcbRead);
497 /* verify a sane environment */
498 if (!This) return E_FAIL;
500 if (offset_in_page + bytes_left > PAGE_SIZE)
501 bytes_from_page = PAGE_SIZE - offset_in_page;
502 else
503 bytes_from_page = bytes_left;
505 if (pcbRead)
506 *pcbRead = 0;
508 while (bytes_left)
510 LPBYTE readPtr;
511 BOOL eof = FALSE;
512 MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
514 if (!page || !page->lpBytes)
516 rc = STG_E_READFAULT;
517 break;
520 TRACE("page %i, offset %u, bytes_from_page %u, bytes_left %u\n",
521 page->page_index, offset_in_page, bytes_from_page, bytes_left);
523 if (page->mapped_bytes < bytes_from_page)
525 eof = TRUE;
526 bytes_from_page = page->mapped_bytes;
529 readPtr = (BYTE*)page->lpBytes + offset_in_page;
530 memcpy(writePtr,readPtr,bytes_from_page);
531 BIGBLOCKFILE_ReleaseMappedPage(This, page);
533 if (pcbRead)
534 *pcbRead += bytes_from_page;
535 bytes_left -= bytes_from_page;
537 if (bytes_left && !eof)
539 writePtr = (LPBYTE)writePtr + bytes_from_page;
540 page_index ++;
541 offset_in_page = 0;
542 if (bytes_left > PAGE_SIZE)
543 bytes_from_page = PAGE_SIZE;
544 else
545 bytes_from_page = bytes_left;
547 if (eof)
549 rc = STG_E_READFAULT;
550 break;
554 TRACE("finished\n");
555 return rc;
558 /******************************************************************************
559 * This method is part of the ILockBytes interface.
561 * It writes the specified bytes at the specified offset.
562 * position. If the file is too small, it will be resized.
564 * See the documentation of ILockBytes for more info.
566 static HRESULT ImplBIGBLOCKFILE_WriteAt(
567 BigBlockFile* const This,
568 ULARGE_INTEGER ulOffset, /* [in] */
569 const void* pv, /* [size_is][in] */
570 ULONG cb, /* [in] */
571 ULONG* pcbWritten) /* [out] */
573 ULONG size_needed = ulOffset.u.LowPart + cb;
574 ULONG first_page = ulOffset.u.LowPart / PAGE_SIZE;
575 ULONG offset_in_page = ulOffset.u.LowPart % PAGE_SIZE;
576 ULONG bytes_left = cb;
577 ULONG page_index = first_page;
578 ULONG bytes_to_page;
579 LPCVOID readPtr = pv;
581 HRESULT rc = S_OK;
583 TRACE("(%p)-> %i %p %i %p\n",This, ulOffset.u.LowPart, pv, cb, pcbWritten);
585 /* verify a sane environment */
586 if (!This) return E_FAIL;
588 if (This->flProtect != PAGE_READWRITE)
589 return STG_E_ACCESSDENIED;
591 if (size_needed > This->filesize.u.LowPart)
593 ULARGE_INTEGER newSize;
594 newSize.u.HighPart = 0;
595 newSize.u.LowPart = size_needed;
596 BIGBLOCKFILE_SetSize(This, newSize);
599 if (offset_in_page + bytes_left > PAGE_SIZE)
600 bytes_to_page = PAGE_SIZE - offset_in_page;
601 else
602 bytes_to_page = bytes_left;
604 if (pcbWritten)
605 *pcbWritten = 0;
607 while (bytes_left)
609 LPBYTE writePtr;
610 MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
612 TRACE("page %i, offset %u, bytes_to_page %u, bytes_left %u\n",
613 page ? page->page_index : 0, offset_in_page, bytes_to_page, bytes_left);
615 if (!page)
617 ERR("Unable to get a page to write. This should never happen\n");
618 rc = E_FAIL;
619 break;
622 if (page->mapped_bytes < bytes_to_page)
624 ERR("Not enough bytes mapped to the page. This should never happen\n");
625 rc = E_FAIL;
626 break;
629 writePtr = (BYTE*)page->lpBytes + offset_in_page;
630 memcpy(writePtr,readPtr,bytes_to_page);
631 BIGBLOCKFILE_ReleaseMappedPage(This, page);
633 if (pcbWritten)
634 *pcbWritten += bytes_to_page;
635 bytes_left -= bytes_to_page;
637 if (bytes_left)
639 readPtr = (const BYTE *)readPtr + bytes_to_page;
640 page_index ++;
641 offset_in_page = 0;
642 if (bytes_left > PAGE_SIZE)
643 bytes_to_page = PAGE_SIZE;
644 else
645 bytes_to_page = bytes_left;
649 return rc;
652 /******************************************************************************
653 * BIGBLOCKFILE_Construct
655 * Construct a big block file. Create the file mapping object.
656 * Create the read only mapped pages list, the writable mapped page list
657 * and the blocks in use list.
659 BigBlockFile *BIGBLOCKFILE_Construct(HANDLE hFile, ILockBytes* pLkByt, DWORD openFlags,
660 BOOL fileBased)
662 BigBlockFile *This;
664 This = HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile));
666 if (This == NULL)
667 return NULL;
669 This->fileBased = fileBased;
670 This->flProtect = BIGBLOCKFILE_GetProtectMode(openFlags);
672 This->maplist = NULL;
673 This->victimhead = NULL;
674 This->victimtail = NULL;
675 This->num_victim_pages = 0;
677 if (This->fileBased)
679 if (!BIGBLOCKFILE_FileInit(This, hFile))
681 HeapFree(GetProcessHeap(), 0, This);
682 return NULL;
685 else
687 if (!BIGBLOCKFILE_LockBytesInit(This, pLkByt))
689 HeapFree(GetProcessHeap(), 0, This);
690 return NULL;
694 return This;
697 /******************************************************************************
698 * BIGBLOCKFILE_Destructor
700 * Destructor. Clean up, free memory.
702 void BIGBLOCKFILE_Destructor(BigBlockFile *This)
704 BIGBLOCKFILE_FreeAllMappedPages(This);
706 if (This->fileBased)
708 CloseHandle(This->hfilemap);
709 CloseHandle(This->hfile);
711 else
713 ILockBytes_Release(This->pLkbyt);
716 HeapFree(GetProcessHeap(), 0, This);
719 /******************************************************************************
720 * BIGBLOCKFILE_ReadAt
722 HRESULT BIGBLOCKFILE_ReadAt(BigBlockFile *This, ULARGE_INTEGER offset,
723 void* buffer, ULONG size, ULONG* bytesRead)
725 if (This->fileBased)
726 return ImplBIGBLOCKFILE_ReadAt(This,offset,buffer,size,bytesRead);
727 else
728 return ILockBytes_ReadAt(This->pLkbyt,offset,buffer,size,bytesRead);
731 /******************************************************************************
732 * BIGBLOCKFILE_WriteAt
734 HRESULT BIGBLOCKFILE_WriteAt(BigBlockFile *This, ULARGE_INTEGER offset,
735 const void* buffer, ULONG size, ULONG* bytesRead)
737 if (This->fileBased)
738 return ImplBIGBLOCKFILE_WriteAt(This,offset,buffer,size,bytesRead);
739 else
740 return ILockBytes_WriteAt(This->pLkbyt,offset,buffer,size,bytesRead);
743 /******************************************************************************
744 * BIGBLOCKFILE_SetSize
746 * Sets the size of the file.
749 HRESULT BIGBLOCKFILE_SetSize(BigBlockFile *This, ULARGE_INTEGER newSize)
751 HRESULT hr = S_OK;
752 LARGE_INTEGER newpos;
754 if (!This->fileBased)
755 return ILockBytes_SetSize(This->pLkbyt, newSize);
757 if (This->filesize.u.LowPart == newSize.u.LowPart)
758 return hr;
760 TRACE("from %u to %u\n", This->filesize.u.LowPart, newSize.u.LowPart);
763 * Unmap all views, must be done before call to SetEndFile.
765 * Just ditch the victim list because there is no guarantee we will need them
766 * and it is not worth the performance hit to unmap and remap them all.
768 BIGBLOCKFILE_DeleteList(This, This->victimhead);
769 This->victimhead = NULL;
770 This->victimtail = NULL;
771 This->num_victim_pages = 0;
773 BIGBLOCKFILE_UnmapAllMappedPages(This);
775 newpos.QuadPart = newSize.QuadPart;
776 if (SetFilePointerEx(This->hfile, newpos, NULL, FILE_BEGIN))
778 if( This->hfilemap ) CloseHandle(This->hfilemap);
780 SetEndOfFile(This->hfile);
782 /* re-create the file mapping object */
783 This->hfilemap = CreateFileMappingA(This->hfile, NULL, This->flProtect,
784 0, 0, NULL);
787 This->filesize = newSize;
788 BIGBLOCKFILE_RemapAllMappedPages(This);
789 return hr;
792 /******************************************************************************
793 * BIGBLOCKFILE_GetSize
795 * Gets the size of the file.
798 static HRESULT BIGBLOCKFILE_GetSize(BigBlockFile *This, ULARGE_INTEGER *size)
800 HRESULT hr = S_OK;
801 if(This->fileBased)
802 *size = This->filesize;
803 else
805 STATSTG stat;
806 hr = ILockBytes_Stat(This->pLkbyt, &stat, STATFLAG_NONAME);
807 if(SUCCEEDED(hr)) *size = stat.cbSize;
809 return hr;
812 /******************************************************************************
813 * BIGBLOCKFILE_Expand
815 * Grows the file to the specified size if necessary.
817 HRESULT BIGBLOCKFILE_Expand(BigBlockFile *This, ULARGE_INTEGER newSize)
819 ULARGE_INTEGER size;
820 HRESULT hr;
822 hr = BIGBLOCKFILE_GetSize(This, &size);
823 if(FAILED(hr)) return hr;
825 if (newSize.QuadPart > size.QuadPart)
826 hr = BIGBLOCKFILE_SetSize(This, newSize);
827 return hr;