public void FailWrite() { MemoryStream ms = CreateSequencedMemStream(100, false); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[25]; cacheStream.Position = 0; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 0); int freeBefore = cacheStream.Statistics.FreeReadBlocks; cacheStream.Position = 11; try { cacheStream.Write(new byte[10], 0, 10); } catch (NotSupportedException) { Assert.Equal(freeBefore + 2, cacheStream.Statistics.FreeReadBlocks); } }
/// <summary> /// Initializes a new instance of the BlockCacheSettings class. /// </summary> /// <param name="settings">The cache settings.</param> internal BlockCacheSettings(BlockCacheSettings settings) { BlockSize = settings.BlockSize; ReadCacheSize = settings.ReadCacheSize; LargeReadSize = settings.LargeReadSize; OptimumReadSize = settings.OptimumReadSize; }
public void Write() { MemoryStream ms = CreateSequencedMemStream(100, true); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[20]; cacheStream.Position = 10; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 10); cacheStream.Position = 20; cacheStream.Write(new byte[10], 0, 10); Assert.Equal(30, cacheStream.Position); cacheStream.Position = 10; buffer = new byte[30]; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 0, 10, 10); AssertSequenced(buffer, 20, 10, 30); Assert.Equal(0, buffer[10]); Assert.Equal(0, buffer[19]); }
public void CacheBlockRecycle() { MemoryStream ms = CreateSequencedMemStream(100, false); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 50, LargeReadSize = 100 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[50]; cacheStream.Position = 10; int numRead = cacheStream.Read(buffer, 0, buffer.Length); Assert.Equal(50, numRead); AssertSequenced(buffer, 10); Assert.Equal(0, cacheStream.Statistics.LargeReadsIn); Assert.Equal(0, cacheStream.Statistics.ReadCacheHits); Assert.Equal(1, cacheStream.Statistics.TotalReadsIn); buffer = new byte[40]; cacheStream.Position = 50; numRead = cacheStream.Read(buffer, 0, buffer.Length); Assert.Equal(40, numRead); AssertSequenced(buffer, 50); Assert.Equal(0, cacheStream.Statistics.LargeReadsIn); Assert.Equal(1, cacheStream.Statistics.ReadCacheHits); Assert.Equal(2, cacheStream.Statistics.TotalReadsIn); }
public void UnalignedCachedRead() { MemoryStream ms = CreateSequencedMemStream(100, false); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[20]; cacheStream.Position = 3; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 3); Assert.Equal(0, cacheStream.Statistics.LargeReadsIn); Assert.Equal(0, cacheStream.Statistics.ReadCacheHits); Assert.Equal(1, cacheStream.Statistics.TotalReadsIn); buffer = new byte[buffer.Length]; cacheStream.Position = 3; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 3); Assert.Equal(0, cacheStream.Statistics.LargeReadsIn); Assert.Equal(1, cacheStream.Statistics.ReadCacheHits); Assert.Equal(2, cacheStream.Statistics.TotalReadsIn); }
/// <summary> /// Initializes a new instance of the BlockCacheSettings class. /// </summary> /// <param name="settings">The cache settings</param> internal BlockCacheSettings(BlockCacheSettings settings) { this.BlockSize = settings.BlockSize; this.ReadCacheSize = settings.ReadCacheSize; this.LargeReadSize = settings.LargeReadSize; this.OptimumReadSize = settings.OptimumReadSize; }
internal DiscImageFile(Uri uri, string userName, string password) { _content = new BufferStream(new DiscContentBuffer(uri, userName, password), FileAccess.Read); BlockCacheSettings cacheSettings = new BlockCacheSettings() { BlockSize = (int)(32 * Sizes.OneKiB), OptimumReadSize = (int)(128 * Sizes.OneKiB), }; _content = new BlockCacheStream(_content, Ownership.Dispose); }
public void Bug5203_IncreaseSize() { MemoryStream ms = new MemoryStream(); BlockCacheSettings settings = new BlockCacheSettings { BlockSize = 64, LargeReadSize = 128, OptimumReadSize = 64, ReadCacheSize = 1024 }; BlockCacheStream bcs = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); // Pre-load read cache with a 'short' block bcs.Write(new byte[11], 0, 11); bcs.Position = 0; bcs.Read(new byte[11], 0, 11); // Extend stream for (int i = 0; i < 20; ++i) { bcs.Write(new byte[11], 0, 11); } // Try to read from first block beyond length of original cached short length // Bug was throwing exception here bcs.Position = 60; bcs.Read(new byte[20], 0, 20); }
/// <summary> /// Initializes a new instance of the NtfsFileSystem class. /// </summary> /// <param name="stream">The stream containing the NTFS file system.</param> public NtfsFileSystem(Stream stream) : base(new NtfsOptions()) { _context = new NtfsContext(); _context.RawStream = stream; _context.Options = NtfsOptions; _context.GetFileByIndex = GetFile; _context.GetFileByRef = GetFile; _context.GetDirectoryByRef = GetDirectory; _context.GetDirectoryByIndex = GetDirectory; _context.AllocateFile = AllocateFile; _context.ForgetFile = ForgetFile; _context.ReadOnly = !stream.CanWrite; _fileCache = new ObjectCache<long, File>(); stream.Position = 0; byte[] bytes = Utilities.ReadFully(stream, 512); _context.BiosParameterBlock = BiosParameterBlock.FromBytes(bytes, 0); if (!IsValidBPB(_context.BiosParameterBlock, stream.Length)) { throw new InvalidFileSystemException("BIOS Parameter Block is invalid for an NTFS file system"); } if (NtfsOptions.ReadCacheEnabled) { BlockCacheSettings cacheSettings = new BlockCacheSettings(); cacheSettings.BlockSize = _context.BiosParameterBlock.BytesPerCluster; _context.RawStream = new BlockCacheStream(SparseStream.FromStream(stream, Ownership.None), Ownership.None, cacheSettings); } // Bootstrap the Master File Table _context.Mft = new MasterFileTable(_context); File mftFile = new File(_context, _context.Mft.GetBootstrapRecord()); _fileCache[MasterFileTable.MftIndex] = mftFile; _context.Mft.Initialize(mftFile); // Get volume information (includes version number) File volumeInfoFile = GetFile(MasterFileTable.VolumeIndex); _volumeInfo = volumeInfoFile.GetStream(AttributeType.VolumeInformation, null).GetContent<VolumeInformation>(); // Initialize access to the other well-known metadata files _context.ClusterBitmap = new ClusterBitmap(GetFile(MasterFileTable.BitmapIndex)); _context.AttributeDefinitions = new AttributeDefinitions(GetFile(MasterFileTable.AttrDefIndex)); _context.UpperCase = new UpperCase(GetFile(MasterFileTable.UpCaseIndex)); if (_volumeInfo.Version >= VolumeInformation.VersionW2k) { _context.SecurityDescriptors = new SecurityDescriptors(GetFile(MasterFileTable.SecureIndex)); _context.ObjectIds = new ObjectIds(GetFile(GetDirectoryEntry(@"$Extend\$ObjId").Reference)); _context.ReparsePoints = new ReparsePoints(GetFile(GetDirectoryEntry(@"$Extend\$Reparse").Reference)); _context.Quotas = new Quotas(GetFile(GetDirectoryEntry(@"$Extend\$Quota").Reference)); } #if false byte[] buffer = new byte[1024]; for (int i = 0; i < buffer.Length; ++i) { buffer[i] = 0xFF; } using (Stream s = OpenFile("$LogFile", FileMode.Open, FileAccess.ReadWrite)) { while (s.Position != s.Length) { s.Write(buffer, 0, (int)Math.Min(buffer.Length, s.Length - s.Position)); } } #endif }