private void LoadDescriptor(Stream s)
{
s.Position = 0;
byte[] header = Utilities.ReadFully(s, (int)Math.Min(Sizes.Sector, s.Length));
if (header.Length < Sizes.Sector || Utilities.ToUInt32LittleEndian(header, 0) != HostedSparseExtentHeader.VmdkMagicNumber)
{
s.Position = 0;
_descriptor = new DescriptorFile(s);
if (_access != FileAccess.Read)
{
_descriptor.ContentId = (uint)_rng.Next();
s.Position = 0;
_descriptor.Write(s);
s.SetLength(s.Position);
}
}
else
{
// This is a sparse disk extent, hopefully with embedded descriptor...
HostedSparseExtentHeader hdr = HostedSparseExtentHeader.Read(header, 0);
if (hdr.DescriptorOffset != 0)
{
Stream descriptorStream = new SubStream(s, hdr.DescriptorOffset * Sizes.Sector, hdr.DescriptorSize * Sizes.Sector);
_descriptor = new DescriptorFile(descriptorStream);
if (_access != FileAccess.Read)
{
_descriptor.ContentId = (uint)_rng.Next();
descriptorStream.Position = 0;
_descriptor.Write(descriptorStream);
byte[] blank = new byte[descriptorStream.Length - descriptorStream.Position];
descriptorStream.Write(blank, 0, blank.Length);
}
}
}
}