コード例 #1
0
ファイル: DiskImageFile.cs プロジェクト: Arsslensoft/ALFX
        /// <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 + ((long)header.BlockSize * (long)header.BlockCount);

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

            return(new DiskImageFile(stream, ownsStream));
        }
コード例 #2
0
ファイル: DiskImageFile.cs プロジェクト: Arsslensoft/ALFX
 private void ReadHeader()
 {
     _stream.Position = 0;
     _preHeader       = new PreHeaderRecord();
     _preHeader.Read(_stream);
     _header = new HeaderRecord();
     _header.Read(_preHeader.Version, _stream);
 }
コード例 #3
0
ファイル: PreHeaderRecord.cs プロジェクト: Arsslensoft/ALFX
        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);
        }
コード例 #4
0
ファイル: DiskImageFile.cs プロジェクト: Arsslensoft/ALFX
        /// <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));
        }