public void ViewIO()
        {
            SparseMemoryStream memStream = new SparseMemoryStream();

            memStream.SetLength(1024);

            ThreadSafeStream tss = new ThreadSafeStream(memStream);

            SparseStream altView = tss.OpenView();

            // Check positions are independant
            tss.Position = 100;
            Assert.AreEqual(0, altView.Position);
            Assert.AreEqual(100, tss.Position);

            // Check I/O is synchronous
            byte[] buffer = new byte[200];
            tss.WriteByte(99);
            altView.Read(buffer, 0, 200);
            Assert.AreEqual(99, buffer[100]);

            // Check positions are updated correctly
            Assert.AreEqual(200, altView.Position);
            Assert.AreEqual(101, tss.Position);
        }
Example #2
0
        /// <summary>
        /// Discards the snapshot any changes made after the snapshot was taken are kept.
        /// </summary>
        public void ForgetSnapshot()
        {
            if (_diffStream == null)
            {
                throw new InvalidOperationException("No snapshot");
            }

            byte[] buffer = new byte[8192];

            foreach (StreamExtent extent in _diffExtents)
            {
                _diffStream.Position = extent.Start;
                _baseStream.Position = extent.Start;

                int totalRead = 0;
                while (totalRead < extent.Length)
                {
                    int toRead = (int)Math.Min(extent.Length - totalRead, buffer.Length);

                    int read = _diffStream.Read(buffer, 0, toRead);
                    _baseStream.Write(buffer, 0, read);

                    totalRead += read;
                }
            }

            _diffStream  = null;
            _diffExtents = null;
        }
        public Ntfs.NtfsFileSystem NtfsFileSystem()
        {
            SparseMemoryBuffer buffer       = new SparseMemoryBuffer(4096);
            SparseMemoryStream ms           = new SparseMemoryStream();
            Geometry           diskGeometry = Geometry.FromCapacity(30 * 1024 * 1024);

            return(Ntfs.NtfsFileSystem.Format(ms, "", diskGeometry, 0, diskGeometry.TotalSectorsLong));
        }
        private static DiscFileSystem FatFileSystem()
        {
            SparseMemoryBuffer buffer       = new SparseMemoryBuffer(4096);
            SparseMemoryStream ms           = new SparseMemoryStream();
            Geometry           diskGeometry = Geometry.FromCapacity(30 * 1024 * 1024);

            return(Fat.FatFileSystem.FormatFloppy(ms, FloppyDiskType.Extended, null));
        }
        public void CanWrite()
        {
            SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite);
            ThreadSafeStream tss = new ThreadSafeStream(memStream);
            Assert.AreEqual(true, tss.CanWrite);

            memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.Read);
            tss = new ThreadSafeStream(memStream);
            Assert.AreEqual(false, tss.CanWrite);
        }
Example #6
0
 public static DiscFileSystem DiagnosticNtfsFileSystem()
 {
     SparseMemoryBuffer buffer = new SparseMemoryBuffer(4096);
     SparseMemoryStream ms = new SparseMemoryStream();
     Geometry diskGeometry = Geometry.FromCapacity(30 * 1024 * 1024);
     Ntfs.NtfsFileSystem.Format(ms, "", diskGeometry, 0, diskGeometry.TotalSectors);
     var discFs = new DiscUtils.Diagnostics.ValidatingFileSystem<Ntfs.NtfsFileSystem, Ntfs.NtfsFileSystemChecker>(ms);
     discFs.CheckpointInterval = 1;
     discFs.GlobalIOTraceCapturesStackTraces = false;
     return discFs;
 }
        public void CanRead()
        {
            SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite);
            ThreadSafeStream   tss       = new ThreadSafeStream(memStream);

            Assert.AreEqual(true, tss.CanRead);

            memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.Write);
            tss       = new ThreadSafeStream(memStream);
            Assert.AreEqual(false, tss.CanRead);
        }
Example #8
0
        /// <summary>
        /// Reverts to a previous snapshot, discarding any changes made to the stream.
        /// </summary>
        public void RevertToSnapshot()
        {
            if (_diffStream == null)
            {
                throw new InvalidOperationException("No snapshot");
            }

            _diffStream  = null;
            _diffExtents = null;

            _position = _savedPosition;
        }
Example #9
0
        /// <summary>
        /// Takes a snapshot of the current stream contents.
        /// </summary>
        public void Snapshot()
        {
            if (_diffStream != null)
            {
                throw new InvalidOperationException("Already have a snapshot");
            }

            _savedPosition = _position;

            _diffExtents = new List <StreamExtent>();
            _diffStream  = new SparseMemoryStream();
            _diffStream.SetLength(_baseStream.Length);
        }
        public static DiscFileSystem DiagnosticNtfsFileSystem()
        {
            SparseMemoryBuffer buffer       = new SparseMemoryBuffer(4096);
            SparseMemoryStream ms           = new SparseMemoryStream();
            Geometry           diskGeometry = Geometry.FromCapacity(30 * 1024 * 1024);

            Ntfs.NtfsFileSystem.Format(ms, "", diskGeometry, 0, diskGeometry.TotalSectorsLong);
            var discFs = new DiscUtils.Diagnostics.ValidatingFileSystem <Ntfs.NtfsFileSystem, Ntfs.NtfsFileSystemChecker>(ms);

            discFs.CheckpointInterval = 1;
            discFs.GlobalIOTraceCapturesStackTraces = false;
            return(discFs);
        }
        public void Seek()
        {
            SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite);

            memStream.SetLength(1024);

            ThreadSafeStream tss = new ThreadSafeStream(memStream);

            tss.Seek(10, SeekOrigin.Begin);
            Assert.AreEqual(10, tss.Position);

            tss.Seek(10, SeekOrigin.Current);
            Assert.AreEqual(20, tss.Position);

            tss.Seek(-10, SeekOrigin.End);
            Assert.AreEqual(1014, tss.Position);
        }
Example #12
0
        public void ChangeLengthFails()
        {
            SparseMemoryStream memStream = new SparseMemoryStream();
            memStream.SetLength(2);

            ThreadSafeStream tss = new ThreadSafeStream(memStream);
            Assert.AreEqual(2, tss.Length);

            try
            {
                tss.SetLength(10);
                Assert.Fail("SetLength should fail");
            }
            catch (NotSupportedException)
            {
            }
        }
Example #13
0
        /// <summary>
        /// Disposes of this instance.
        /// </summary>
        /// <param name="disposing"><c>true</c> if called from Dispose(), else <c>false</c></param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_baseStreamOwnership == Ownership.Dispose && _baseStream != null)
                {
                    _baseStream.Dispose();
                }
                _baseStream = null;

                if (_diffStream != null)
                {
                    _diffStream.Dispose();
                }
                _diffStream = null;
            }
            base.Dispose(disposing);
        }
        public void DisposeView()
        {
            SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite);

            memStream.SetLength(1024);

            ThreadSafeStream tss = new ThreadSafeStream(memStream);

            SparseStream altView = tss.OpenView();

            altView.Dispose();

            tss.ReadByte();

            SparseStream altView2 = tss.OpenView();

            altView2.ReadByte();
        }
        public void ChangeLengthFails()
        {
            SparseMemoryStream memStream = new SparseMemoryStream();

            memStream.SetLength(2);

            ThreadSafeStream tss = new ThreadSafeStream(memStream);

            Assert.AreEqual(2, tss.Length);

            try
            {
                tss.SetLength(10);
                Assert.Fail("SetLength should fail");
            }
            catch (NotSupportedException)
            {
            }
        }
        public void Dispose_StopsView()
        {
            SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite);

            memStream.SetLength(1024);

            ThreadSafeStream tss = new ThreadSafeStream(memStream);

            SparseStream altView = tss.OpenView();

            tss.Dispose();

            try
            {
                altView.ReadByte();
                Assert.Fail("Disposed stream didn't stop view");
            }
            catch (ObjectDisposedException)
            {
            }
        }
        public void Extents()
        {
            SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite);

            memStream.SetLength(1024);

            ThreadSafeStream tss = new ThreadSafeStream(memStream);

            SparseStream altView = tss.OpenView();

            tss.Position = 100;
            tss.WriteByte(99);

            List <StreamExtent> extents = new List <StreamExtent>(altView.Extents);

            Assert.AreEqual(1, extents.Count);
            Assert.AreEqual(100, extents[0].Start);
            Assert.AreEqual(1, extents[0].Length);

            extents = new List <StreamExtent>(altView.GetExtentsInRange(10, 300));
            Assert.AreEqual(1, extents.Count);
            Assert.AreEqual(100, extents[0].Start);
            Assert.AreEqual(1, extents[0].Length);
        }
Example #18
0
        /// <summary>
        /// Takes a snapshot of the current stream contents.
        /// </summary>
        public void Snapshot()
        {
            if (_diffStream != null)
            {
                throw new InvalidOperationException("Already have a snapshot");
            }

            _savedPosition = _position;

            _diffExtents = new List<StreamExtent>();
            _diffStream = new SparseMemoryStream();
            _diffStream.SetLength(_baseStream.Length);
        }
Example #19
0
        public void ViewIO()
        {
            SparseMemoryStream memStream = new SparseMemoryStream();
            memStream.SetLength(1024);

            ThreadSafeStream tss = new ThreadSafeStream(memStream);

            SparseStream altView = tss.OpenView();

            // Check positions are independant
            tss.Position = 100;
            Assert.AreEqual(0, altView.Position);
            Assert.AreEqual(100, tss.Position);

            // Check I/O is synchronous
            byte[] buffer = new byte[200];
            tss.WriteByte(99);
            altView.Read(buffer, 0, 200);
            Assert.AreEqual(99, buffer[100]);

            // Check positions are updated correctly
            Assert.AreEqual(200, altView.Position);
            Assert.AreEqual(101, tss.Position);
        }
Example #20
0
        public void Seek()
        {
            SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite);
            memStream.SetLength(1024);

            ThreadSafeStream tss = new ThreadSafeStream(memStream);

            tss.Seek(10, SeekOrigin.Begin);
            Assert.AreEqual(10, tss.Position);

            tss.Seek(10, SeekOrigin.Current);
            Assert.AreEqual(20, tss.Position);

            tss.Seek(-10, SeekOrigin.End);
            Assert.AreEqual(1014, tss.Position);
        }
Example #21
0
        public void Extents()
        {
            SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite);
            memStream.SetLength(1024);

            ThreadSafeStream tss = new ThreadSafeStream(memStream);

            SparseStream altView = tss.OpenView();

            tss.Position = 100;
            tss.WriteByte(99);

            List<StreamExtent> extents = new List<StreamExtent>(altView.Extents);
            Assert.AreEqual(1, extents.Count);
            Assert.AreEqual(100, extents[0].Start);
            Assert.AreEqual(1, extents[0].Length);

            extents = new List<StreamExtent>(altView.GetExtentsInRange(10, 300));
            Assert.AreEqual(1, extents.Count);
            Assert.AreEqual(100, extents[0].Start);
            Assert.AreEqual(1, extents[0].Length);
        }
Example #22
0
        public void Dispose_StopsView()
        {
            SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite);
            memStream.SetLength(1024);

            ThreadSafeStream tss = new ThreadSafeStream(memStream);

            SparseStream altView = tss.OpenView();
            tss.Dispose();

            try
            {
                altView.ReadByte();
                Assert.Fail("Disposed stream didn't stop view");
            }
            catch (ObjectDisposedException)
            {
            }
        }
Example #23
0
 public Ntfs.NtfsFileSystem NtfsFileSystem()
 {
     SparseMemoryBuffer buffer = new SparseMemoryBuffer(4096);
     SparseMemoryStream ms = new SparseMemoryStream();
     Geometry diskGeometry = Geometry.FromCapacity(30 * 1024 * 1024);
     return Ntfs.NtfsFileSystem.Format(ms, "", diskGeometry, 0, diskGeometry.TotalSectors);
 }
Example #24
0
 private static DiscFileSystem FatFileSystem()
 {
     SparseMemoryBuffer buffer = new SparseMemoryBuffer(4096);
     SparseMemoryStream ms = new SparseMemoryStream();
     Geometry diskGeometry = Geometry.FromCapacity(30 * 1024 * 1024);
     return Fat.FatFileSystem.FormatFloppy(ms, FloppyDiskType.Extended, null);
 }
Example #25
0
        /// <summary>
        /// Discards the snapshot any changes made after the snapshot was taken are kept.
        /// </summary>
        public void ForgetSnapshot()
        {
            if (_diffStream == null)
            {
                throw new InvalidOperationException("No snapshot");
            }

            byte[] buffer = new byte[8192];

            foreach (var extent in _diffExtents)
            {
                _diffStream.Position = extent.Start;
                _baseStream.Position = extent.Start;

                int totalRead = 0;
                while (totalRead < extent.Length)
                {
                    int toRead = (int)Math.Min(extent.Length - totalRead, buffer.Length);

                    int read = _diffStream.Read(buffer, 0, toRead);
                    _baseStream.Write(buffer, 0, read);

                    totalRead += read;
                }
            }

            _diffStream = null;
            _diffExtents = null;
        }
Example #26
0
        /// <summary>
        /// Reverts to a previous snapshot, discarding any changes made to the stream.
        /// </summary>
        public void RevertToSnapshot()
        {
            if (_diffStream == null)
            {
                throw new InvalidOperationException("No snapshot");
            }

            _diffStream = null;
            _diffExtents = null;

            _position = _savedPosition;
        }
Example #27
0
        public void DisposeView()
        {
            SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite);
            memStream.SetLength(1024);

            ThreadSafeStream tss = new ThreadSafeStream(memStream);

            SparseStream altView = tss.OpenView();
            altView.Dispose();

            tss.ReadByte();

            SparseStream altView2 = tss.OpenView();
            altView2.ReadByte();
        }
Example #28
0
        /// <summary>
        /// Disposes of this instance.
        /// </summary>
        /// <param name="disposing"><c>true</c> if called from Dispose(), else <c>false</c></param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_baseStreamOwnership == Ownership.Dispose && _baseStream != null)
                {
                    _baseStream.Dispose();
                }

                _baseStream = null;

                if (_diffStream != null)
                {
                    _diffStream.Dispose();
                }

                _diffStream = null;
            }

            base.Dispose(disposing);
        }