// // 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. // using System; using System.Collections.Generic; using System.IO; using DiscUtils.Internal; using DiscUtils.Streams; namespace DiscUtils.Raw { /// /// Represents a raw disk image. /// /// This disk format is simply an uncompressed capture of all blocks on a disk. public sealed class Disk : VirtualDisk { private DiskImageFile _file; /// /// Initializes a new instance of the Disk class. /// /// The stream to read. /// Indicates if the new instance should control the lifetime of the stream. public Disk(Stream stream, Ownership ownsStream) : this(stream, ownsStream, null) {} /// /// Initializes a new instance of the Disk class. /// /// The stream to read. /// Indicates if the new instance should control the lifetime of the stream. /// The emulated geometry of the disk. public Disk(Stream stream, Ownership ownsStream, Geometry geometry) { _file = new DiskImageFile(stream, ownsStream, geometry); } /// /// Initializes a new instance of the Disk class. /// /// The path to the disk image. public Disk(string path) :this(path, FileAccess.ReadWrite) {} /// /// Initializes a new instance of the Disk class. /// /// The path to the disk image. /// The access requested to the disk. public Disk(string path, FileAccess access) { FileShare share = access == FileAccess.Read ? FileShare.Read : FileShare.None; var locator = new LocalFileLocator(string.Empty); _file = new DiskImageFile(locator.Open(path, FileMode.Open, access, share), Ownership.Dispose, null); } /// /// Initializes a new instance of the Disk class. /// /// The contents of the disk. private Disk(DiskImageFile file) { _file = file; } /// /// Gets the capacity of the disk (in bytes). /// public override long Capacity { get { return _file.Capacity; } } /// /// Gets the content of the disk as a stream. /// /// Note the returned stream is not guaranteed to be at any particular position. The actual position /// will depend on the last partition table/file system activity, since all access to the disk contents pass /// through a single stream instance. Set the stream position before accessing the stream. public override SparseStream Content { get { return _file.Content; } } /// /// Gets the type of disk represented by this object. /// public override VirtualDiskClass DiskClass { get { return _file.DiskType; } } /// /// Gets information about the type of disk. /// /// This property provides access to meta-data about the disk format, for example whether the /// BIOS geometry is preserved in the disk file. public override VirtualDiskTypeInfo DiskTypeInfo { get { return DiskFactory.MakeDiskTypeInfo(); } } /// /// Gets the geometry of the disk. /// public override Geometry Geometry { get { return _file.Geometry; } } /// /// Gets the layers that make up the disk. /// public override IEnumerable Layers { get { yield return _file; } } /// /// Initializes a stream as an unformatted disk. /// /// The stream to initialize. /// Indicates if the new instance controls the lifetime of the stream. /// The desired capacity of the new disk. /// An object that accesses the stream as a disk. public static Disk Initialize(Stream stream, Ownership ownsStream, long capacity) { return Initialize(stream, ownsStream, capacity, null); } /// /// Initializes a stream as an unformatted disk. /// /// The stream to initialize. /// Indicates if the new instance controls the lifetime of the stream. /// The desired capacity of the new disk. /// The desired geometry of the new disk, or null for default. /// An object that accesses the stream as a disk. public static Disk Initialize(Stream stream, Ownership ownsStream, long capacity, Geometry geometry) { return new Disk(DiskImageFile.Initialize(stream, ownsStream, capacity, geometry)); } /// /// Initializes a stream as an unformatted floppy disk. /// /// The stream to initialize. /// Indicates if the new instance controls the lifetime of the stream. /// The type of floppy disk image to create. /// An object that accesses the stream as a disk. public static Disk Initialize(Stream stream, Ownership ownsStream, FloppyDiskType type) { return new Disk(DiskImageFile.Initialize(stream, ownsStream, type)); } /// /// Create a new differencing disk, possibly within an existing disk. /// /// The file system to create the disk on. /// The path (or URI) for the disk to create. /// The newly created disk. public override VirtualDisk CreateDifferencingDisk(DiscFileSystem fileSystem, string path) { throw new NotSupportedException("Differencing disks not supported for raw disks"); } /// /// Create a new differencing disk. /// /// The path (or URI) for the disk to create. /// The newly created disk. public override VirtualDisk CreateDifferencingDisk(string path) { throw new NotSupportedException("Differencing disks not supported for raw disks"); } /// /// Disposes of underlying resources. /// /// Set to true if called within Dispose(), /// else false. protected override void Dispose(bool disposing) { try { if (disposing) { if (_file != null) { _file.Dispose(); } _file = null; } } finally { base.Dispose(disposing); } } } }