Example #1
0
        /// <summary>
        /// Initializes a stream as a fixed-sized VDI file.
        /// </summary>
        /// <param name="stream">The stream to initialize.</param>
        /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
        /// <param name="capacity">The desired capacity of the new disk.</param>
        /// <returns>An object that accesses the stream as a VDI file.</returns>
        public static DiskImageFile InitializeFixed(Stream stream, Ownership ownsStream, long capacity)
        {
            PreHeaderRecord preHeader = PreHeaderRecord.Initialized();
            HeaderRecord    header    = HeaderRecord.Initialized(ImageType.Fixed, ImageFlags.None, capacity, 1024 * 1024, 0);

            byte[] blockTable = new byte[header.BlockCount * 4];
            for (int i = 0; i < header.BlockCount; ++i)
            {
                Utilities.WriteBytesLittleEndian((uint)i, blockTable, i * 4);
            }

            header.BlocksAllocated = header.BlockCount;

            stream.Position = 0;
            preHeader.Write(stream);
            header.Write(stream);

            stream.Position = header.BlocksOffset;
            stream.Write(blockTable, 0, blockTable.Length);

            long totalSize = header.DataOffset + header.BlockSize * (long)header.BlockCount;

            if (stream.Length < totalSize)
            {
                stream.SetLength(totalSize);
            }

            return(new DiskImageFile(stream, ownsStream));
        }
Example #2
0
 private void ReadHeader()
 {
     _stream.Position = 0;
     _preHeader       = new PreHeaderRecord();
     _preHeader.Read(_stream);
     _header = new HeaderRecord();
     _header.Read(_preHeader.Version, _stream);
 }
Example #3
0
 public static PreHeaderRecord Initialized()
 {
     PreHeaderRecord result = new PreHeaderRecord();
     result.FileInfo = "<<< Sun xVM VirtualBox Disk Image >>>\n";
     result.Signature = VdiSignature;
     result.Version = new FileVersion(0x00010001);
     return result;
 }
Example #4
0
        public static PreHeaderRecord Initialized()
        {
            PreHeaderRecord result = new PreHeaderRecord();

            result.FileInfo  = "<<< Sun xVM VirtualBox Disk Image >>>\n";
            result.Signature = VdiSignature;
            result.Version   = new FileVersion(0x00010001);
            return(result);
        }
Example #5
0
        /// <summary>
        /// Initializes a stream as a dynamically-sized VDI file.
        /// </summary>
        /// <param name="stream">The stream to initialize.</param>
        /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
        /// <param name="capacity">The desired capacity of the new disk</param>
        /// <returns>An object that accesses the stream as a VDI file</returns>
        public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStream, long capacity)
        {
            PreHeaderRecord preHeader = PreHeaderRecord.Initialized();
            HeaderRecord    header    = HeaderRecord.Initialized(ImageType.Dynamic, ImageFlags.None, capacity, 1024 * 1024, 0);

            byte[] blockTable = new byte[header.BlockCount * 4];
            for (int i = 0; i < blockTable.Length; ++i)
            {
                blockTable[i] = 0xFF;
            }
            header.BlocksAllocated = 0;

            stream.Position = 0;
            preHeader.Write(stream);
            header.Write(stream);

            stream.Position = header.BlocksOffset;
            stream.Write(blockTable, 0, blockTable.Length);

            return(new DiskImageFile(stream, ownsStream));
        }
 private void ReadHeader()
 {
     _stream.Position = 0;
     _preHeader = new PreHeaderRecord();
     _preHeader.Read(_stream);
     _header = new HeaderRecord();
     _header.Read(_preHeader.Version, _stream);
 }