/// <summary>
 /// Represents a single EWF file.
 /// </summary>
 /// <param name="path">Path to the ewf file.</param>
 /// <param name="access">Desired access.</param>
 public DiskImageFile(string path, FileAccess access)
 {
     if (_content == null)
     {
         _content = new EWFStream(path);
     }
 }
        public HostedSparseExtentStream(Stream file, Ownership ownsFile, long diskOffset, SparseStream parentDiskStream, Ownership ownsParentDiskStream)
        {
            _fileStream = file;
            _ownsFileStream = ownsFile;
            _diskOffset = diskOffset;
            _parentDiskStream = parentDiskStream;
            _ownsParentDiskStream = ownsParentDiskStream;

            file.Position = 0;
            byte[] headerSector = Utilities.ReadFully(file, Sizes.Sector);
            _hostedHeader = HostedSparseExtentHeader.Read(headerSector, 0);
            if (_hostedHeader.GdOffset == -1)
            {
                // Fall back to secondary copy that (should) be at the end of the stream, just before the end-of-stream sector marker
                file.Position = file.Length - Sizes.OneKiB;
                headerSector = Utilities.ReadFully(file, Sizes.Sector);
                _hostedHeader = HostedSparseExtentHeader.Read(headerSector, 0);

                if (_hostedHeader.MagicNumber != HostedSparseExtentHeader.VmdkMagicNumber)
                {
                    throw new IOException("Unable to locate valid VMDK header or footer");
                }
            }

            _header = _hostedHeader;

            if (_hostedHeader.CompressAlgorithm != 0 && _hostedHeader.CompressAlgorithm != 1)
            {
                throw new NotSupportedException("Only uncompressed and DEFLATE compressed disks supported");
            }

            _gtCoverage = _header.NumGTEsPerGT * _header.GrainSize * Sizes.Sector;

            LoadGlobalDirectory();
        }
        public NtfsFileStream(NtfsFileSystem fileSystem, DirectoryEntry entry, AttributeType attrType, string attrName, FileAccess access)
        {
            _entry = entry;

            _file = fileSystem.GetFile(entry.Reference);
            _baseStream = _file.OpenStream(attrType, attrName, access);
        }
 public ContentStream(SparseStream fileStream, BlockAllocationTable bat, long length, SparseStream parentStream, Ownership ownsParent)
 {
     _fileStream = fileStream;
     _bat = bat;
     _length = length;
     _parentStream = parentStream;
     _ownsParent = ownsParent;
 }
        public override SparseStream OpenContent(SparseStream parent, Ownership ownsParent)
        {
            if (ownsParent == Ownership.Dispose && parent != null)
            {
                parent.Dispose();
            }

            return SparseStream.FromStream(Content, Ownership.None);
        }
Example #6
0
        public Chunk(Stream bat, SparseStream file, FreeSpaceTable freeSpace, FileParameters fileParameters, int chunk, int blocksPerChunk)
        {
            _bat = bat;
            _file = file;
            _freeSpace = freeSpace;
            _fileParameters = fileParameters;
            _chunk = chunk;
            _blocksPerChunk = blocksPerChunk;

            _bat.Position = _chunk * (_blocksPerChunk + 1) * 8;
            _batData = Utilities.ReadFully(bat, (_blocksPerChunk + 1) * 8);
        }
        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);
        }
Example #8
0
 /// <summary>
 /// Disposes of this instance, freeing underlying resources.
 /// </summary>
 /// <param name="disposing"><c>true</c> if running inside Dispose(), indicating
 /// graceful cleanup of all managed objects should be performed, or <c>false</c>
 /// if running inside destructor.</param>
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (_content != null)
         {
             _content.Dispose();
             _content = null;
         }
     }
     base.Dispose(disposing);
 }
Example #9
0
        /// <summary>
        /// Initializes a new instance of the DiskImageFile class.
        /// </summary>
        /// <param name="stream">The stream to interpret</param>
        /// <param name="ownsStream">Indicates if the new instance should control the lifetime of the stream.</param>
        /// <param name="geometry">The emulated geometry of the disk.</param>
        public DiskImageFile(Stream stream, Ownership ownsStream, Geometry geometry)
        {
            _content = stream as SparseStream;
            _ownsContent = ownsStream;

            if (_content == null)
            {
                _content = SparseStream.FromStream(stream, ownsStream);
                _ownsContent = Ownership.Dispose;
            }

            _geometry = geometry ?? DetectGeometry(_content);
        }
Example #10
0
        internal MappedStream DoOpenContent(SparseStream parent, Ownership ownsParent)
        {
            SparseStream theParent    = parent;
            Ownership    theOwnership = ownsParent;

            if (parent == null)
            {
                theParent    = new ZeroStream(Capacity);
                theOwnership = Ownership.Dispose;
            }

            return(new ContentStream(SparseStream.FromStream(_fileStream, Ownership.None), _bat, Capacity, theParent, theOwnership));
        }
Example #11
0
        /// <summary>
        /// Disposes of this instance, freeing underlying resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> if running inside Dispose(), indicating
        /// graceful cleanup of all managed objects should be performed, or <c>false</c>
        /// if running inside destructor.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_content != null)
                {
                    _content.Dispose();
                    _content = null;
                }
            }

            base.Dispose(disposing);
        }
Example #12
0
        public Chunk(Stream bat, SparseStream file, FreeSpaceTable freeSpace, FileParameters fileParameters, int chunk,
                     int blocksPerChunk)
        {
            _bat            = bat;
            _file           = file;
            _freeSpace      = freeSpace;
            _fileParameters = fileParameters;
            _chunk          = chunk;
            _blocksPerChunk = blocksPerChunk;

            _bat.Position = _chunk * (_blocksPerChunk + 1) * 8;
            _batData      = StreamUtilities.ReadExact(bat, (_blocksPerChunk + 1) * 8);
        }
Example #13
0
        public ContentStream(SparseStream fileStream, Stream batStream, FreeSpaceTable freeSpaceTable, Metadata metadata, long length, SparseStream parentStream, Ownership ownsParent)
        {
            _fileStream = fileStream;
            _batStream = batStream;
            _freeSpaceTable = freeSpaceTable;
            _metadata = metadata;
            _fileParameters = _metadata.FileParameters;
            _length = length;
            _parentStream = parentStream;
            _ownsParent = ownsParent;

            _chunks = new ObjectCache<int, Chunk>();
        }
        public ContentStream(SparseStream fileStream, Stream batStream, FreeSpaceTable freeSpaceTable, Metadata metadata, long length, SparseStream parentStream, Ownership ownsParent)
        {
            _fileStream     = fileStream;
            _batStream      = batStream;
            _freeSpaceTable = freeSpaceTable;
            _metadata       = metadata;
            _fileParameters = _metadata.FileParameters;
            _length         = length;
            _parentStream   = parentStream;
            _ownsParent     = ownsParent;

            _chunks = new ObjectCache <int, Chunk>();
        }
Example #15
0
        /// <summary>
        /// Opens the content of the disk image file as a stream.
        /// </summary>
        /// <param name="parent">The parent file's content (if any).</param>
        /// <param name="ownsParent">Whether the created stream assumes ownership of parent stream.</param>
        /// <returns>The new content stream.</returns>
        public override SparseStream OpenContent(SparseStream parent, Ownership ownsParent)
        {
            if (parent != null && ownsParent == Ownership.Dispose)
            {
                // Not needed until differencing disks supported.
                parent.Dispose();
            }

            DiskStream stream = new DiskStream(_stream, Ownership.None, _header);

            stream.WriteOccurred += OnWriteOccurred;
            return(stream);
        }
Example #16
0
 private void NewToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (fileLoaded)
     {
         string       fname = Interaction.InputBox("Enter file name.", "Dreamcaster", "file.lua");
         LuaFile      file  = new LuaFile(fname, "-- " + fname);
         SparseStream strm  = fs.OpenFile(fname, FileMode.Create);
         byte[]       data  = Encoding.ASCII.GetBytes(file.data);
         strm.Write(data, 0, data.Length);
         strm.Close();
         UpdateListbox();
     }
 }
Example #17
0
        /// <summary>
        /// Initializes a new instance of the DiskImageFile class.
        /// </summary>
        /// <param name="stream">The stream to interpret.</param>
        /// <param name="ownsStream">Indicates if the new instance should control the lifetime of the stream.</param>
        /// <param name="geometry">The emulated geometry of the disk.</param>
        public DiskImageFile(Stream stream, Ownership ownsStream, Geometry geometry)
        {
            _content     = stream as SparseStream;
            _ownsContent = ownsStream;

            if (_content == null)
            {
                _content     = SparseStream.FromStream(stream, ownsStream);
                _ownsContent = Ownership.Dispose;
            }

            _geometry = geometry ?? DetectGeometry(_content);
        }
Example #18
0
        private SparseStream OpenExtent(ExtentDescriptor extent, long extentStart, SparseStream parent,
                                        Ownership ownsParent)
        {
            FileAccess access = FileAccess.Read;
            FileShare  share  = FileShare.Read;

            if (extent.Access == ExtentAccess.ReadWrite && _access != FileAccess.Read)
            {
                access = FileAccess.ReadWrite;
                share  = FileShare.None;
            }

            if (extent.Type != ExtentType.Sparse && extent.Type != ExtentType.VmfsSparse)
            {
                if (ownsParent == Ownership.Dispose && parent != null)
                {
                    parent.Dispose();
                }
            }

            switch (extent.Type)
            {
            case ExtentType.Flat:
            case ExtentType.Vmfs:
                return(SparseStream.FromStream(
                           _fileLocator.Open(extent.FileName, FileMode.Open, access, share),
                           Ownership.Dispose));

            case ExtentType.Zero:
                return(new ZeroStream(extent.SizeInSectors * Sizes.Sector));

            case ExtentType.Sparse:
                return(new HostedSparseExtentStream(
                           _fileLocator.Open(extent.FileName, FileMode.Open, access, share),
                           Ownership.Dispose,
                           extentStart,
                           parent,
                           ownsParent));

            case ExtentType.VmfsSparse:
                return(new ServerSparseExtentStream(
                           _fileLocator.Open(extent.FileName, FileMode.Open, access, share),
                           Ownership.Dispose,
                           extentStart,
                           parent,
                           ownsParent));

            default:
                throw new NotSupportedException();
            }
        }
Example #19
0
            public override void Dispose()
            {
                if (_content != null && _contentOwnership == Ownership.Dispose)
                {
                    _content.Dispose();
                    _content = null;
                }

                if (_grainTableStream != null)
                {
                    _grainTableStream.Dispose();
                    _grainTableStream = null;
                }
            }
            public override void Dispose()
            {
                if (_content != null && _ownership == Ownership.Dispose)
                {
                    _content.Dispose();
                    _content = null;
                }

                if (_bitmapStream != null)
                {
                    _bitmapStream.Dispose();
                    _bitmapStream = null;
                }
            }
Example #21
0
        public override SparseStream OpenContent(SparseStream parentStream, Ownership ownsStream)
        {
            if (parentStream != null && ownsStream == Ownership.Dispose)
            {
                parentStream.Dispose();
            }

            if (Buffer != null)
            {
                SparseStream rawStream = new BufferStream(Buffer, FileAccess.Read);
                return(new BlockCacheStream(rawStream, Ownership.Dispose));
            }
            return(SparseStream.FromStream(_stream, Ownership.None));
        }
Example #22
0
 private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (fileLoaded)
     {
         LuaFile file = m_luaFiles.SelectedItem as LuaFile;
         Console.WriteLine("Saving " + file.name);
         SparseStream strm = fs.OpenFile(file.name, FileMode.Create, FileAccess.Write);
         byte[]       data = Encoding.ASCII.GetBytes(file.data);
         strm.Write(data, 0, data.Length);
         strm.Close();
         Console.WriteLine("Saved");
         UpdateListbox();
     }
 }
Example #23
0
        internal MappedStream DoOpenContent(SparseStream parent, Ownership ownsParent)
        {
            SparseStream theParent    = parent;
            Ownership    theOwnership = ownsParent;

            if (parent == null)
            {
                theParent    = new ZeroStream(Capacity);
                theOwnership = Ownership.Dispose;
            }

            ContentStream contentStream = new ContentStream(SparseStream.FromStream(_logicalStream, Ownership.None), _fileStream.CanWrite, _batStream, _freeSpace, _metadata, Capacity, theParent, theOwnership);

            return(new AligningStream(contentStream, Ownership.Dispose, (int)_metadata.LogicalSectorSize));
        }
Example #24
0
        public void Build()
        {
            OutputSDImageStream = new MemoryStream();
            long     capacity = 64 * 1024 * 1024;
            Geometry geometry = new Geometry(capacity, 8, 32, 512);
            BiosPartitionedDiskBuilder builder = new BiosPartitionedDiskBuilder(capacity, geometry);

            builder.PartitionTable.Create(WellKnownPartitionType.WindowsFat, false);
            SparseStream  partitionContent = SparseStream.FromStream(new MemoryStream((int)(builder.PartitionTable[0].SectorCount * 512)), Ownership.Dispose);
            FatFileSystem ffs = FatFileSystem.FormatPartition(partitionContent, "TEST", geometry, 0, (int)(capacity / 512) - 32, 0);

            CopyDirectoriesAndFiles(ffs, InputRootDirectory);
            builder.SetPartitionContent(0, partitionContent);
            OutputSDImageStream = builder.Build();
        }
Example #25
0
        public DiskBuilderTest()
        {
            MemoryStream fileStream = new MemoryStream();
            Disk         baseFile   = Disk.InitializeDynamic(fileStream, Ownership.Dispose, 16 * 1024L * 1024);

            for (int i = 0; i < 8; i += 1024 * 1024)
            {
                baseFile.Content.Position = i;
                baseFile.Content.WriteByte((byte)i);
            }

            baseFile.Content.Position = 15 * 1024 * 1024;
            baseFile.Content.WriteByte(0xFF);

            diskContent = baseFile.Content;
        }
Example #26
0
        public static DiscDirectoryInfoTreeItemViewModel OpenImgStream(Stream stream)
        {
            if (SparseStream.Detect(stream))
            {
                var sparseStream = new SparseStream(stream);
                if (!sparseStream.Open())
                {
                    throw new Exception("Wrong format");
                }
                stream = sparseStream;
            }

            var fileSystem = new ExtFileSystem(stream);

            return(new DiscDirectoryInfoTreeItemViewModel(fileSystem.Root));
        }
Example #27
0
        public DiskBuilderTest()
        {
            SparseMemoryStream sourceStream = new SparseMemoryStream();

            sourceStream.SetLength(160 * 1024L * 1024);
            for (int i = 0; i < 8; ++i)
            {
                sourceStream.Position = i * 1024L * 1024;
                sourceStream.WriteByte((byte)i);
            }

            sourceStream.Position = 150 * 1024 * 1024;
            sourceStream.WriteByte(0xFF);

            diskContent = sourceStream;
        }
Example #28
0
        /// <summary>
        /// Gets the contents of this disk as a stream.
        /// </summary>
        /// <param name="parent">The content of the parent disk (needed if this is a differencing disk).</param>
        /// <param name="ownsParent">A value indicating whether ownership of the parent stream is transfered.</param>
        /// <returns>The stream containing the disk contents.</returns>
        public override SparseStream OpenContent(SparseStream parent, Ownership ownsParent)
        {
            if (_descriptor.ParentContentId == uint.MaxValue)
            {
                if (parent != null && ownsParent == Ownership.Dispose)
                {
                    parent.Dispose();
                }

                parent = null;
            }

            if (parent == null)
            {
                parent     = new ZeroStream(Capacity);
                ownsParent = Ownership.Dispose;
            }

            if (_descriptor.Extents.Count == 1)
            {
                if (_monolithicStream != null)
                {
                    return(new HostedSparseExtentStream(
                               _monolithicStream,
                               Ownership.None,
                               0,
                               parent,
                               ownsParent));
                }
                else
                {
                    return(OpenExtent(_descriptor.Extents[0], 0, parent, ownsParent));
                }
            }
            else
            {
                long           extentStart = 0;
                SparseStream[] streams     = new SparseStream[_descriptor.Extents.Count];
                for (int i = 0; i < streams.Length; ++i)
                {
                    streams[i]   = OpenExtent(_descriptor.Extents[i], extentStart, parent, (i == streams.Length - 1) ? ownsParent : Ownership.None);
                    extentStart += _descriptor.Extents[i].SizeInSectors * Sizes.Sector;
                }

                return(new ConcatStream(Ownership.Dispose, streams));
            }
        }
Example #29
0
        public override void Close()
        {
            if (_baseStream == null)
            {
                return;
            }

            using (new NtfsTransaction())
            {
                base.Close();
                _baseStream.Close();

                UpdateMetadata();

                _baseStream = null;
            }
        }
        public ServerSparseExtentStream(Stream file, Ownership ownsFile, long diskOffset, SparseStream parentDiskStream, Ownership ownsParentDiskStream)
        {
            _fileStream           = file;
            _ownsFileStream       = ownsFile;
            _diskOffset           = diskOffset;
            _parentDiskStream     = parentDiskStream;
            _ownsParentDiskStream = ownsParentDiskStream;

            file.Position = 0;
            byte[] firstSectors = Utilities.ReadFully(file, Sizes.Sector * 4);
            _serverHeader = ServerSparseExtentHeader.Read(firstSectors, 0);
            _header       = _serverHeader;

            _gtCoverage = _header.NumGTEsPerGT * (long)_header.GrainSize * Sizes.Sector;

            LoadGlobalDirectory();
        }
        public ServerSparseExtentStream(Stream file, Ownership ownsFile, long diskOffset, SparseStream parentDiskStream, Ownership ownsParentDiskStream)
        {
            _fileStream = file;
            _ownsFileStream = ownsFile;
            _diskOffset = diskOffset;
            _parentDiskStream = parentDiskStream;
            _ownsParentDiskStream = ownsParentDiskStream;

            file.Position = 0;
            byte[] firstSectors = Utilities.ReadFully(file, Sizes.Sector * 4);
            _serverHeader = ServerSparseExtentHeader.Read(firstSectors, 0);
            _header = _serverHeader;

            _gtCoverage = _header.NumGTEsPerGT * (long)_header.GrainSize * Sizes.Sector;

            LoadGlobalDirectory();
        }
Example #32
0
        public void ReadFromOverflow_Works()
        {
            var cnid = new CatalogNodeId(1);

            var descriptor = new ExtentDescriptor()
            {
                BlockCount = 1, StartBlock = 0
            };

            byte[] descriptorBytes = new byte[2 * descriptor.Size];
            descriptor.WriteTo(descriptorBytes, 0);

            descriptor.StartBlock = 1;
            descriptor.WriteTo(descriptorBytes, descriptor.Size);

            var extentsOverflow = new Mock <BTree <ExtentKey> >(MockBehavior.Strict);

            extentsOverflow
            .Setup(e => e.Find(new ExtentKey(cnid, 0, false)))
            .Returns(descriptorBytes);

            var buffer = new FileBuffer(
                new Context()
            {
                VolumeStream =
                    SparseStream.FromStream(
                        new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!")), Ownership.Dispose),
                VolumeHeader = new VolumeHeader()
                {
                    BlockSize = 8,
                },
                ExtentsOverflow = extentsOverflow.Object,
            },
                new ForkData()
            {
                LogicalSize = 0x123,
                Extents     = Array.Empty <ExtentDescriptor>(),
                TotalBlocks = 2,
            },
                cnid);

            byte[] data = new byte[0x10];
            Assert.Equal(8, buffer.Read(0, data, 0, data.Length));
            Assert.Equal(5, buffer.Read(8, data, 8, data.Length));
        }
Example #33
0
 private void CompileToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (fileLoaded)
     {
         UpdateListbox();
         fs.DeleteDirectory("lua", true);
         fs.CreateDirectory("lua");
         foreach (LuaFile file in lfiles)
         {
             Console.WriteLine("Writing " + file.name);
             SparseStream stream = fs.OpenFile(file.name, FileMode.Create);
             stream.Write(Encoding.ASCII.GetBytes(file.data), 0, file.data.Length);
             stream.Close();
             Console.WriteLine("Wrote");
         }
         MessageBox.Show("Finished saving.", "Dreamcaster");
     }
 }
Example #34
0
 /// <summary>
 /// Disposes of underlying resources.
 /// </summary>
 /// <param name="disposing">Set to <c>true</c> if called within Dispose(),
 /// else <c>false</c>.</param>
 protected override void Dispose(bool disposing)
 {
     try
     {
         if (disposing)
         {
             if (_ownsContent == Ownership.Dispose && _content != null)
             {
                 _content.Dispose();
             }
             _content = null;
         }
     }
     finally
     {
         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();
        }
Example #36
0
        protected override void Dispose(bool disposing)
        {
            if (_baseStream == null)
            {
                base.Dispose(disposing);
                return;
            }

            using (new NtfsTransaction())
            {
                base.Dispose(disposing);
                _baseStream.Dispose();

                UpdateMetadata();

                _baseStream = null;
            }
        }
Example #37
0
 /// <summary>
 /// Disposes of underlying resources.
 /// </summary>
 /// <param name="disposing">Set to <c>true</c> if called within Dispose(),
 /// else <c>false</c>.</param>
 protected override void Dispose(bool disposing)
 {
     try
     {
         if (disposing)
         {
             if (_ownsContent == Ownership.Dispose && _content != null)
             {
                 _content.Dispose();
             }
             _content = null;
         }
     }
     finally
     {
         base.Dispose(disposing);
     }
 }
Example #38
0
        public void ReadThrough()
        {
            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 = 0;
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 0);
            Assert.Equal(0, cacheStream.Statistics.LargeReadsIn);
            Assert.Equal(0, cacheStream.Statistics.ReadCacheHits);
            Assert.Equal(1, cacheStream.Statistics.TotalReadsIn);
        }
Example #39
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_stream != null)
                {
                    _stream.Dispose();
                    _stream = null;
                }
            }

            if (!_handle.IsClosed)
            {
                _handle.Dispose();
            }

            base.Dispose(disposing);
        }
Example #40
0
 public static void ReadIsoFolder(DiscFileSystem cdReader, string sIsoPath, string sDestinationRootPath)
 {
     try
     {
         string[] saFiles = cdReader.GetFiles(sIsoPath);
         foreach (string sFile in saFiles)
         {
             DiscFileInfo dfiIso = cdReader.GetFileInfo(sFile);
             string sDestinationPath = Path.Combine(sDestinationRootPath, dfiIso.DirectoryName.Substring(0, dfiIso.DirectoryName.Length - 1));
             if (!Directory.Exists(sDestinationPath))
             {
                 Directory.CreateDirectory(sDestinationPath);
             }
             string sDestinationFile = Path.Combine(sDestinationPath, dfiIso.Name);
             SparseStream streamIsoFile = cdReader.OpenFile(sFile, FileMode.Open);
             FileStream fsDest = new FileStream(sDestinationFile, FileMode.Create);
             byte[] baData = new byte[0x4000];
             while (true)
             {
                 int nReadCount = streamIsoFile.Read(baData, 0, baData.Length);
                 if (nReadCount < 1)
                 {
                     break;
                 }
                 else
                 {
                     fsDest.Write(baData, 0, nReadCount);
                 }
             }
             streamIsoFile.Close();
             fsDest.Close();
         }
         string[] saDirectories = cdReader.GetDirectories(sIsoPath);
         foreach (string sDirectory in saDirectories)
         {
             ReadIsoFolder(cdReader, sDirectory, sDestinationRootPath);
         }
         return;
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }
 }
Example #41
0
        public DynamicStream(Stream fileStream, DynamicHeader dynamicHeader, long length, SparseStream parentStream,
                             Ownership ownsParentStream)
        {
            if (fileStream == null)
            {
                throw new ArgumentNullException(nameof(fileStream));
            }

            if (dynamicHeader == null)
            {
                throw new ArgumentNullException(nameof(dynamicHeader));
            }

            if (parentStream == null)
            {
                throw new ArgumentNullException(nameof(parentStream));
            }

            if (length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length), length, "Negative lengths not allowed");
            }

            _fileStream       = fileStream;
            _dynamicHeader    = dynamicHeader;
            _length           = length;
            _parentStream     = parentStream;
            _ownsParentStream = ownsParentStream;

            _blockBitmaps    = new byte[_dynamicHeader.MaxTableEntries][];
            _blockBitmapSize =
                (int)
                MathUtilities.RoundUp(MathUtilities.Ceil(_dynamicHeader.BlockSize, Sizes.Sector * 8), Sizes.Sector);

            ReadBlockAllocationTable();

            // Detect where next block should go (cope if the footer is missing)
            _fileStream.Position = MathUtilities.RoundDown(_fileStream.Length, Sizes.Sector) - Sizes.Sector;
            byte[] footerBytes = StreamUtilities.ReadExact(_fileStream, Sizes.Sector);
            Footer footer      = Footer.FromBytes(footerBytes, 0);

            _nextBlockStart = _fileStream.Position - (footer.IsValid() ? Sizes.Sector : 0);
        }
Example #42
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (_parentStream != null)
                {
                    if (_ownsParent == Ownership.Dispose)
                    {
                        _parentStream.Dispose();
                    }

                    _parentStream = null;
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
Example #43
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (_parentStream != null)
                {
                    if (_ownsParent == Ownership.Dispose)
                    {
                        _parentStream.Dispose();
                    }

                    _parentStream = null;
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
Example #44
0
        /// <summary>
        /// Initializes a new instance of the DiscImageFile class.
        /// </summary>
        /// <param name="stream">The stream to interpret</param>
        /// <param name="ownsStream">Indicates if the new instance should control the lifetime of the stream.</param>
        /// <param name="format">The disc image format</param>
        public DiscImageFile(Stream stream, Ownership ownsStream, OpticalFormat format)
        {
            if (ownsStream == Ownership.Dispose)
            {
                _toDispose = stream;
            }

            if (format == OpticalFormat.None)
            {
                if ((stream.Length % Mode1SectorSize) == 0 && (stream.Length % Mode2SectorSize) != 0)
                {
                    _format = OpticalFormat.Mode1;
                }
                else if ((stream.Length % Mode1SectorSize) != 0 && (stream.Length % Mode2SectorSize) == 0)
                {
                    _format = OpticalFormat.Mode2;
                }
                else
                {
                    throw new IOException("Unable to detect optical disk format");
                }
            }
            else
            {
                _format = format;
            }

            _content = stream as SparseStream;
            if (_content == null)
            {
                _content = SparseStream.FromStream(stream, Ownership.None);
            }

            if (_format == OpticalFormat.Mode2)
            {
                Mode2Buffer converter = new Mode2Buffer(new StreamBuffer(_content, Ownership.None));
                _content = new BufferStream(converter, FileAccess.Read);
            }
        }
Example #45
0
        public DynamicStream(Stream fileStream, DynamicHeader dynamicHeader, long length, SparseStream parentStream, Ownership ownsParentStream)
        {
            if (fileStream == null)
            {
                throw new ArgumentNullException("fileStream");
            }

            if (dynamicHeader == null)
            {
                throw new ArgumentNullException("dynamicHeader");
            }

            if (parentStream == null)
            {
                throw new ArgumentNullException("parentStream");
            }

            if (length < 0)
            {
                throw new ArgumentOutOfRangeException("length", length, "Negative lengths not allowed");
            }

            _fileStream = fileStream;
            _dynamicHeader = dynamicHeader;
            _length = length;
            _parentStream = parentStream;
            _ownsParentStream = ownsParentStream;

            _blockBitmaps = new byte[_dynamicHeader.MaxTableEntries][];
            _blockBitmapSize = (int)Utilities.RoundUp(Utilities.Ceil(_dynamicHeader.BlockSize, Utilities.SectorSize * 8), Utilities.SectorSize);

            ReadBlockAllocationTable();

            // Detect where next block should go (cope if the footer is missing)
            _fileStream.Position = Utilities.RoundDown(_fileStream.Length, Utilities.SectorSize) - Utilities.SectorSize;
            byte[] footerBytes = Utilities.ReadFully(_fileStream, Utilities.SectorSize);
            Footer footer = Footer.FromBytes(footerBytes, 0);
            _nextBlockStart = _fileStream.Position - (footer.IsValid() ? Utilities.SectorSize : 0);
        }
 /// <summary>
 /// Adds a sparse disk image to the XVA file.
 /// </summary>
 /// <param name="label">The admin-visible name of the disk.</param>
 /// <param name="content">The content of the disk.</param>
 /// <param name="ownsContent">Indicates if ownership of content is transfered.</param>
 public void AddDisk(string label, SparseStream content, Ownership ownsContent)
 {
     _disks.Add(new DiskRecord(label, content, ownsContent));
 }
 public GrainTableExtent(long outputStart, SparseStream content, ServerSparseExtentHeader header)
     : base(outputStart, CalcSize(content, header))
 {
     _content = content;
     _header = header;
 }
 public DynamicDiskBuilder(SparseStream content, Footer footer, uint blockSize)
 {
     _content = content;
     _footer = footer;
     _blockSize = blockSize;
 }
            public override void Dispose()
            {
                if (_content != null && _ownership == Ownership.Dispose)
                {
                    _content.Dispose();
                    _content = null;
                }

                if (_bitmapStream != null)
                {
                    _bitmapStream.Dispose();
                    _bitmapStream = null;
                }
            }
 public DataBlockExtent(long start, SparseStream content, Ownership ownership)
     : base(start, Utilities.RoundUp(Utilities.Ceil(content.Length, Sizes.Sector) / 8, Sizes.Sector) + Utilities.RoundUp(content.Length, Sizes.Sector))
 {
     _content = content;
     _ownership = ownership;
 }
 public DataBlockExtent(long start, SparseStream content)
     : this(start, content, Ownership.None)
 {
 }
Example #52
0
        /// <summary>
        /// Disposes of underlying resources.
        /// </summary>
        /// <param name="disposing">Set to <c>true</c> if called within Dispose(),
        /// else <c>false</c>.</param>
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    if (_content != null)
                    {
                        _content.Dispose();
                        _content = null;
                    }

                    if (_files != null)
                    {
                        foreach (var record in _files)
                        {
                            if (record.Second == Ownership.Dispose)
                            {
                                record.First.Dispose();
                            }
                        }

                        _files = null;
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
Example #53
0
        private SparseStream OpenExtent(ExtentDescriptor extent, long extentStart, SparseStream parent, Ownership ownsParent)
        {
            FileAccess access = FileAccess.Read;
            FileShare share = FileShare.Read;
            if(extent.Access == ExtentAccess.ReadWrite && _access != FileAccess.Read)
            {
                access = FileAccess.ReadWrite;
                share = FileShare.None;
            }

            if (extent.Type != ExtentType.Sparse && extent.Type != ExtentType.VmfsSparse)
            {
                if (ownsParent == Ownership.Dispose && parent != null)
                {
                    parent.Dispose();
                }
            }

            switch (extent.Type)
            {
                case ExtentType.Flat:
                case ExtentType.Vmfs:
                    return SparseStream.FromStream(
                        _fileLocator.Open(extent.FileName, FileMode.Open, access, share),
                        Ownership.Dispose);

                case ExtentType.Zero:
                    return new ZeroStream(extent.SizeInSectors * Utilities.SectorSize);

                case ExtentType.Sparse:
                    return new HostedSparseExtentStream(
                        _fileLocator.Open(extent.FileName, FileMode.Open, access, share),
                        Ownership.Dispose,
                        extentStart,
                        parent,
                        ownsParent);

                case ExtentType.VmfsSparse:
                    return new ServerSparseExtentStream(
                        _fileLocator.Open(extent.FileName, FileMode.Open, access, share),
                        Ownership.Dispose,
                        extentStart,
                        parent,
                        ownsParent);

                default:
                    throw new NotSupportedException();
            }
        }
Example #54
0
        /// <summary>
        /// Gets the contents of this disk as a stream.
        /// </summary>
        internal SparseStream OpenContent(SparseStream parent, Ownership ownsParent)
        {
            if (_descriptor.ParentContentId == uint.MaxValue)
            {
                if (parent != null && ownsParent == Ownership.Dispose)
                {
                    parent.Dispose();
                }
                parent = null;
            }

            if (parent == null)
            {
                parent = new ZeroStream(Capacity);
                ownsParent = Ownership.Dispose;
            }

            if (_descriptor.Extents.Count == 1)
            {
                if (_monolithicStream != null)
                {
                    return new HostedSparseExtentStream(
                        _monolithicStream,
                        Ownership.None,
                        0,
                        parent,
                        ownsParent);
                }
                else
                {
                    return OpenExtent(_descriptor.Extents[0], 0, parent, ownsParent);
                }
            }
            else
            {
                long extentStart = 0;
                SparseStream[] streams = new SparseStream[_descriptor.Extents.Count];
                for (int i = 0; i < streams.Length; ++i)
                {
                    streams[i] = OpenExtent(_descriptor.Extents[i], extentStart, parent, (i == streams.Length - 1) ? ownsParent : Ownership.None);
                    extentStart += _descriptor.Extents[i].SizeInSectors * Sizes.Sector;
                }
                return new ConcatStream(Ownership.Dispose, streams);
            }
        }
Example #55
0
        /// <summary>
        /// Disposes of this instance.
        /// </summary>
        /// <param name="disposing"><c>true</c> if disposing, <c>false</c> if in destructor</param>
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    if (_contentStream != null)
                    {
                        _contentStream.Dispose();
                        _contentStream = null;
                    }

                    if (_ownsMonolithicStream == Ownership.Dispose && _monolithicStream != null)
                    {
                        _monolithicStream.Dispose();
                        _monolithicStream = null;
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
Example #56
0
 public FileStream(File file, NtfsAttribute attr, FileAccess access)
 {
     _file = file;
     _attr = attr;
     _wrapped = attr.Open(access);
 }
            private static long CalcSize(SparseStream content, ServerSparseExtentHeader header)
            {
                long numDataGrains = StreamExtent.BlockCount(content.Extents, header.GrainSize * Sizes.Sector);
                long grainTableSectors = Utilities.Ceil(header.NumGTEsPerGT * 4, Sizes.Sector);

                return (grainTableSectors + (numDataGrains * header.GrainSize)) * Sizes.Sector;
            }
Example #58
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    UpdateFooter();

                    if (_ownsParentStream == Ownership.Dispose && _parentStream != null)
                    {
                        _parentStream.Dispose();
                        _parentStream = null;
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
Example #59
0
        /// <summary>
        /// Disposes of underlying resources.
        /// </summary>
        /// <param name="disposing">Set to <c>true</c> if called within Dispose(),
        /// else <c>false</c>.</param>
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    if (_content != null)
                    {
                        _content.Dispose();
                        _content = null;
                    }

                    if (_file != null)
                    {

                        if (_file.Second == Ownership.Dispose)
                        {
                            _file.First.Dispose();
                        }

                        _file = null;
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
 public VmfsSparseExtentBuilder(SparseStream content)
 {
     _content = content;
 }