//
// Copyright (c) 2008-2011, Kenneth Bell
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
namespace DiscUtils
{
using System;
using System.Globalization;
using DiscUtils.Partitions;
///
/// Enumeration of possible types of physical volume.
///
public enum PhysicalVolumeType
{
///
/// Unknown type.
///
None,
///
/// Physical volume encompasses the entire disk.
///
EntireDisk,
///
/// Physical volume is defined by a BIOS-style partition table.
///
BiosPartition,
///
/// Physical volume is defined by a GUID partition table.
///
GptPartition,
///
/// Physical volume is defined by an Apple partition map.
///
ApplePartition
}
///
/// Information about a physical disk volume, which may be a partition or an entire disk.
///
public sealed class PhysicalVolumeInfo : VolumeInfo
{
private string _diskId;
private VirtualDisk _disk;
private SparseStreamOpenDelegate _streamOpener;
private PhysicalVolumeType _type;
private PartitionInfo _partitionInfo;
///
/// Initializes a new instance of the PhysicalVolumeInfo class.
///
/// The containing disk's identity.
/// The disk containing the partition.
/// Information about the partition.
/// Use this constructor to represent a (BIOS or GPT) partition.
internal PhysicalVolumeInfo(
string diskId,
VirtualDisk disk,
PartitionInfo partitionInfo)
{
_diskId = diskId;
_disk = disk;
_streamOpener = partitionInfo.Open;
_type = partitionInfo.VolumeType;
_partitionInfo = partitionInfo;
}
///
/// Initializes a new instance of the PhysicalVolumeInfo class.
///
/// The identity of the disk.
/// The disk itself.
/// Use this constructor to represent an entire disk as a single volume.
internal PhysicalVolumeInfo(
string diskId,
VirtualDisk disk)
{
_diskId = diskId;
_disk = disk;
_streamOpener = delegate { return new SubStream(disk.Content, Ownership.None, 0, disk.Capacity); };
_type = PhysicalVolumeType.EntireDisk;
}
///
/// Gets the type of the volume.
///
public PhysicalVolumeType VolumeType
{
get { return _type; }
}
///
/// Gets the signature of the disk containing the volume (only valid for partition-type volumes).
///
public int DiskSignature
{
get { return (_type != PhysicalVolumeType.EntireDisk) ? _disk.Signature : 0; }
}
///
/// Gets the unique identity of the disk containing the volume, if known.
///
public Guid DiskIdentity
{
get { return (_type != PhysicalVolumeType.EntireDisk) ? _disk.Partitions.DiskGuid : Guid.Empty; }
}
///
/// Gets the one-byte BIOS type for this volume, which indicates the content.
///
public override byte BiosType
{
get { return (_partitionInfo == null) ? (byte)0 : _partitionInfo.BiosType; }
}
///
/// Gets the size of the volume, in bytes.
///
public override long Length
{
get { return (_partitionInfo == null) ? _disk.Capacity : _partitionInfo.SectorCount * _disk.SectorSize; }
}
///
/// Gets the stable identity for this physical volume.
///
/// The stability of the identity depends the disk structure.
/// In some cases the identity may include a simple index, when no other information
/// is available. Best practice is to add disks to the Volume Manager in a stable
/// order, if the stability of this identity is paramount.
public override string Identity
{
get
{
if (_type == PhysicalVolumeType.GptPartition)
{
return "VPG" + PartitionIdentity.ToString("B");
}
else
{
string partId;
switch (_type)
{
case PhysicalVolumeType.EntireDisk:
partId = "PD";
break;
case PhysicalVolumeType.BiosPartition:
case PhysicalVolumeType.ApplePartition:
partId = "PO" + (_partitionInfo.FirstSector * _disk.SectorSize).ToString("X", CultureInfo.InvariantCulture);
break;
default:
partId = "P*";
break;
}
return "VPD:" + _diskId + ":" + partId;
}
}
}
///
/// Gets the disk geometry of the underlying storage medium, if any (may be null).
///
public override Geometry PhysicalGeometry
{
get { return _disk.Geometry; }
}
///
/// Gets the disk geometry of the underlying storage medium (as used in BIOS calls), may be null.
///
public override Geometry BiosGeometry
{
get { return _disk.BiosGeometry; }
}
///
/// Gets the offset of this volume in the underlying storage medium, if any (may be Zero).
///
public override long PhysicalStartSector
{
get { return _type == PhysicalVolumeType.EntireDisk ? 0 : _partitionInfo.FirstSector; }
}
///
/// Gets the unique identity of the physical partition, if known.
///
public Guid PartitionIdentity
{
get
{
GuidPartitionInfo gpi = _partitionInfo as GuidPartitionInfo;
if (gpi != null)
{
return gpi.Identity;
}
return Guid.Empty;
}
}
///
/// Gets the underlying partition (if any).
///
internal PartitionInfo Partition
{
get { return _partitionInfo; }
}
///
/// Opens the volume, providing access to its contents.
///
/// A stream that can be used to access the volume.
public override SparseStream Open()
{
return _streamOpener();
}
}
}