Ejemplo n.º 1
0
        private VhdFile Create(Stream stream, string vhdDirectory)
        {
            var reader = new BinaryReader(stream, Encoding.Unicode);

            try
            {
                var dataReader = new VhdDataReader(reader);
                var footer     = new VhdFooterFactory(dataReader).CreateFooter();

                VhdHeader            header = null;
                BlockAllocationTable blockAllocationTable = null;
                VhdFile parent = null;
                if (footer.DiskType != DiskType.Fixed)
                {
                    header = new VhdHeaderFactory(dataReader, footer).CreateHeader();
                    blockAllocationTable = new BlockAllocationTableFactory(dataReader, header).Create();
                    if (footer.DiskType == DiskType.Differencing)
                    {
                        var parentPath = vhdDirectory == null ? header.ParentPath : Path.Combine(vhdDirectory, header.GetRelativeParentPath());
                        parent = Create(parentPath);
                    }
                }
                return(new VhdFile(footer, header, blockAllocationTable, parent, stream));
            }
            catch (EndOfStreamException)
            {
                throw new VhdParsingException("unsupported format");
            }
        }
Ejemplo n.º 2
0
        private VhdFile Create(StreamSource streamSource)
        {
            var disposer = new Action(() => { if (streamSource.DisposeOnException)
                                              {
                                                  streamSource.Stream.Dispose();
                                              }
                                      });
            bool throwing = false;

            try
            {
                var reader     = new BinaryReader(streamSource.Stream, Encoding.Unicode);
                var dataReader = new VhdDataReader(reader);
                var footer     = new VhdFooterFactory(dataReader).CreateFooter();

                VhdHeader            header = null;
                BlockAllocationTable blockAllocationTable = null;
                VhdFile parent = null;
                if (footer.DiskType != DiskType.Fixed)
                {
                    header = new VhdHeaderFactory(dataReader, footer).CreateHeader();
                    blockAllocationTable = new BlockAllocationTableFactory(dataReader, header).Create();
                    if (footer.DiskType == DiskType.Differencing)
                    {
                        var parentPath = streamSource.VhdDirectory == null ? header.ParentPath : Path.Combine(streamSource.VhdDirectory, header.GetRelativeParentPath());
                        parent = Create(parentPath);
                    }
                }
                return(new VhdFile(footer, header, blockAllocationTable, parent, streamSource.Stream));
            }
            catch (Exception e)
            {
                throwing = true;
                throw new VhdParsingException("unsupported format", e);
            }
            finally
            {
                if (throwing)
                {
                    disposer();
                }
            }
        }