/// <summary> /// Creates a <see cref="DiskMedium"/> from a <see cref="stream"/>. /// This will read the existing header from the <see cref="stream"/>. /// </summary> /// <param name="stream">An open <see cref="FileStream"/> to use. The <see cref="DiskMedium"/> /// will assume ownership of this <see cref="FileStream"/>.</param> /// <param name="pool">The <see cref="MemoryPool"/> to allocate data from.</param> /// <param name="fileStructureBlockSize">the block size of the file structure. Usually 4kb.</param> /// <returns></returns> public static DiskMedium OpenFile(CustomFileStream stream, MemoryPool pool, int fileStructureBlockSize) { byte[] buffer = new byte[fileStructureBlockSize]; stream.ReadRaw(0, buffer, fileStructureBlockSize); FileHeaderBlock header = FileHeaderBlock.Open(buffer); BufferedFile disk = new BufferedFile(stream, pool, header, isNewFile: false); return(new DiskMedium(disk, header)); }
public void Test() { MemoryPoolTest.TestMemoryLeak(); Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, 0L); FileHeaderBlock header = FileHeaderBlock.CreateNew(4096); header = header.CloneEditable(); header.CreateNewFile(SubFileName.CreateRandom()); header.CreateNewFile(SubFileName.CreateRandom()); header.CreateNewFile(SubFileName.CreateRandom()); header.IsReadOnly = true; FileHeaderBlock header2 = FileHeaderBlock.Open(header.GetBytes()); CheckEqual(header2, header); Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, 0L); //verify they are the same; MemoryPoolTest.TestMemoryLeak(); }
private unsafe string TestFile(string fileName) { ShortTime lastReport = ShortTime.Now; int errorBlocks = 0; uint firstBlockWithError = uint.MaxValue; try { using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { int blockSize = FileHeaderBlock.SearchForBlockSize(stream); stream.Position = 0; byte[] data = new byte[blockSize]; stream.ReadAll(data, 0, blockSize); var header = FileHeaderBlock.Open(data); fixed(byte *lp = data) { stream.Position = header.HeaderBlockCount * blockSize; uint startingBlock = header.HeaderBlockCount; if (m_shouldQuickScan) { uint blocksToScan = (uint)Math.Ceiling(m_mbToScan * 1024 * 1024 / blockSize); if (blocksToScan < header.LastAllocatedBlock) { startingBlock = Math.Max(header.HeaderBlockCount, header.LastAllocatedBlock - blocksToScan); } } for (uint x = startingBlock; x <= header.LastAllocatedBlock; x++) { long checksum1; int checksum2; stream.ReadAll(data, 0, blockSize); Footer.ComputeChecksum((IntPtr)lp, out checksum1, out checksum2, blockSize - 16); long checksumInData1 = *(long *)(lp + blockSize - 16); int checksumInData2 = *(int *)(lp + blockSize - 8); if (!(checksum1 == checksumInData1 && checksum2 == checksumInData2)) { firstBlockWithError = Math.Min(firstBlockWithError, x); errorBlocks++; } if (m_quit) { if (errorBlocks == 0) { return("Quit Early - No Errors Found"); } return($"Quit Early - Blocks With Errors {errorBlocks} Starting At {firstBlockWithError}"); } if (lastReport.ElapsedSeconds() > .25) { lastReport = ShortTime.Now; lblProgress.Text = $"Completed {m_filesScanned} of {m_filesToScan} files. Current File: {(stream.Position / (double)stream.Length).ToString("0.0%")}"; Application.DoEvents(); } } } } if (errorBlocks == 0) { return("No Errors Found"); } return($"Blocks With Errors {errorBlocks} Starting At {firstBlockWithError}"); } catch (Exception e) { MessageBox.Show(e.ToString()); return("Error"); } }