| 1279 | } |
| 1280 | |
| 1281 | static bool FlatStream_CreateMirror(TBlockStream * pStream) |
| 1282 | { |
| 1283 | ULONGLONG MasterSize = 0; |
| 1284 | ULONGLONG MirrorSize = 0; |
| 1285 | LPBYTE FileBitmap = NULL; |
| 1286 | DWORD dwBitmapSize; |
| 1287 | DWORD dwBlockCount; |
| 1288 | bool bNeedCreateMirrorStream = true; |
| 1289 | bool bNeedResizeMirrorStream = true; |
| 1290 | |
| 1291 | // Do we have master function and base creation function? |
| 1292 | if(pStream->pMaster == NULL || pStream->BaseCreate == NULL) |
| 1293 | return false; |
| 1294 | |
| 1295 | // Retrieve the master file size, block count and bitmap size |
| 1296 | FileStream_GetSize(pStream->pMaster, &MasterSize); |
| 1297 | dwBlockCount = (DWORD)((MasterSize + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE); |
| 1298 | dwBitmapSize = (DWORD)((dwBlockCount + 7) / 8); |
| 1299 | |
| 1300 | // Setup stream size and position |
| 1301 | pStream->BuildNumber = DEFAULT_BUILD_NUMBER; // BUGBUG: Really??? |
| 1302 | pStream->StreamSize = MasterSize; |
| 1303 | pStream->StreamPos = 0; |
| 1304 | |
| 1305 | // Open the base stream for write access |
| 1306 | if(pStream->BaseOpen(pStream, pStream->szFileName, 0)) |
| 1307 | { |
| 1308 | // If the file open succeeded, check if the file size matches required size |
| 1309 | pStream->BaseGetSize(pStream, &MirrorSize); |
| 1310 | if(MirrorSize == MasterSize + dwBitmapSize + sizeof(FILE_BITMAP_FOOTER)) |
| 1311 | { |
| 1312 | // Attempt to load an existing file bitmap |
| 1313 | if(FlatStream_LoadBitmap(pStream)) |
| 1314 | return true; |
| 1315 | |
| 1316 | // We need to create new file bitmap |
| 1317 | bNeedResizeMirrorStream = false; |
| 1318 | } |
| 1319 | |
| 1320 | // We need to create mirror stream |
| 1321 | bNeedCreateMirrorStream = false; |
| 1322 | } |
| 1323 | |
| 1324 | // Create a new stream, if needed |
| 1325 | if(bNeedCreateMirrorStream) |
| 1326 | { |
| 1327 | if(!pStream->BaseCreate(pStream)) |
| 1328 | return false; |
| 1329 | } |
| 1330 | |
| 1331 | // If we need to, then resize the mirror stream |
| 1332 | if(bNeedResizeMirrorStream) |
| 1333 | { |
| 1334 | if(!pStream->BaseResize(pStream, MasterSize + dwBitmapSize + sizeof(FILE_BITMAP_FOOTER))) |
| 1335 | return false; |
| 1336 | } |
| 1337 | |
| 1338 | // Allocate the bitmap array |
no test coverage detected