MCPcopy Create free account
hub / github.com/diasurgical/devilution / PartStream_CreateMirror

Function PartStream_CreateMirror

3rdParty/StormLib/src/FileStream.cpp:1697–1775  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1695}
1696
1697static bool PartStream_CreateMirror(TBlockStream * pStream)
1698{
1699 ULONGLONG RemainingSize;
1700 ULONGLONG MasterSize = 0;
1701 ULONGLONG MirrorSize = 0;
1702 LPBYTE FileBitmap = NULL;
1703 DWORD dwBitmapSize;
1704 DWORD dwBlockCount;
1705 bool bNeedCreateMirrorStream = true;
1706 bool bNeedResizeMirrorStream = true;
1707
1708 // Do we have master function and base creation function?
1709 if(pStream->pMaster == NULL || pStream->BaseCreate == NULL)
1710 return false;
1711
1712 // Retrieve the master file size, block count and bitmap size
1713 FileStream_GetSize(pStream->pMaster, &MasterSize);
1714 dwBlockCount = (DWORD)((MasterSize + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE);
1715 dwBitmapSize = (DWORD)(dwBlockCount * sizeof(PART_FILE_MAP_ENTRY));
1716
1717 // Setup stream size and position
1718 pStream->BuildNumber = DEFAULT_BUILD_NUMBER; // BUGBUG: Really???
1719 pStream->StreamSize = MasterSize;
1720 pStream->StreamPos = 0;
1721
1722 // Open the base stream for write access
1723 if(pStream->BaseOpen(pStream, pStream->szFileName, 0))
1724 {
1725 // If the file open succeeded, check if the file size matches required size
1726 pStream->BaseGetSize(pStream, &MirrorSize);
1727 if(MirrorSize >= sizeof(PART_FILE_HEADER) + dwBitmapSize)
1728 {
1729 // Check if the remaining size is aligned to block
1730 RemainingSize = MirrorSize - sizeof(PART_FILE_HEADER) - dwBitmapSize;
1731 if((RemainingSize & (DEFAULT_BLOCK_SIZE - 1)) == 0 || RemainingSize == MasterSize)
1732 {
1733 // Attempt to load an existing file bitmap
1734 if(PartStream_LoadBitmap(pStream))
1735 return true;
1736 }
1737 }
1738
1739 // We need to create mirror stream
1740 bNeedCreateMirrorStream = false;
1741 }
1742
1743 // Create a new stream, if needed
1744 if(bNeedCreateMirrorStream)
1745 {
1746 if(!pStream->BaseCreate(pStream))
1747 return false;
1748 }
1749
1750 // If we need to, then resize the mirror stream
1751 if(bNeedResizeMirrorStream)
1752 {
1753 if(!pStream->BaseResize(pStream, sizeof(PART_FILE_HEADER) + dwBitmapSize))
1754 return false;

Callers 1

PartStream_OpenFunction · 0.85

Calls 2

FileStream_GetSizeFunction · 0.85
PartStream_LoadBitmapFunction · 0.85

Tested by

no test coverage detected