private IEnumerable <CompletionPort> CreateAsync(AsyncMachine <VhdFile> machine, StreamSource streamSource)
        {
            var disposer = new Action(() => { if (streamSource.DisposeOnException)
                                              {
                                                  streamSource.Stream.Dispose();
                                              }
                                      });

            var reader        = TryCatch(() => new BinaryReader(streamSource.Stream, Encoding.Unicode), disposer);
            var dataReader    = TryCatch(() => new VhdDataReader(reader), disposer);
            var footerFactory = TryCatch(() => new VhdFooterFactory(dataReader), disposer);

            footerFactory.BeginCreateFooter(machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            var footer = TryCatch <VhdFooter>(footerFactory.EndCreateFooter, disposer, machine.CompletionResult);

            VhdHeader            header = null;
            BlockAllocationTable blockAllocationTable = null;
            VhdFile parent = null;

            if (footer.DiskType != DiskType.Fixed)
            {
                var headerFactory = new VhdHeaderFactory(dataReader, footer);

                headerFactory.BeginCreateHeader(machine.CompletionCallback, null);
                yield return(CompletionPort.SingleOperation);

                header = TryCatch <VhdHeader>(headerFactory.EndCreateHeader, disposer, machine.CompletionResult);

                var tableFactory = new BlockAllocationTableFactory(dataReader, header);
                tableFactory.BeginCreate(machine.CompletionCallback, null);
                yield return(CompletionPort.SingleOperation);

                blockAllocationTable = TryCatch <BlockAllocationTable>(tableFactory.EndCreate, disposer, machine.CompletionResult);

                if (footer.DiskType == DiskType.Differencing)
                {
                    var parentPath = streamSource.VhdDirectory == null ? header.ParentPath : Path.Combine(streamSource.VhdDirectory, header.GetRelativeParentPath());

                    BeginCreate(parentPath, machine.CompletionCallback, null);
                    yield return(CompletionPort.SingleOperation);

                    parent = TryCatch <VhdFile>(EndCreate, disposer, machine.CompletionResult);
                }
            }
            machine.ParameterValue = new VhdFile(footer, header, blockAllocationTable, parent, streamSource.Stream);
        }
Example #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)
                    {
                        string parentPath = GetParentPath(streamSource.VhdDirectory, header);

                        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();
                }
            }
        }