public void TestUnion1() { StreamExtent[] s1 = new StreamExtent[] { new StreamExtent(0, 4) }; StreamExtent[] s2 = new StreamExtent[] { new StreamExtent(4, 8) }; StreamExtent[] r = new StreamExtent[] { new StreamExtent(0, 12) }; Compare(r, StreamExtent.Union(s1, s2)); }
/// <summary> /// Indicates if a stream contains a valid partition table. /// </summary> /// <param name="disk">The stream to inspect</param> /// <returns><c>true</c> if the partition table is valid, else <c>false</c>.</returns> public static bool IsValid(Stream disk) { if (disk.Length < Utilities.SectorSize) { return(false); } disk.Position = 0; byte[] bootSector = Utilities.ReadFully(disk, Utilities.SectorSize); // Check for the 'bootable sector' marker if (bootSector[510] != 0x55 || bootSector[511] != 0xAA) { return(false); } bool foundPartition = false; List <StreamExtent> knownPartitions = new List <StreamExtent>(); foreach (var record in ReadPrimaryRecords(bootSector)) { // If the partition extends beyond the end of the disk, this is probably an invalid partition table if (record.LBALength != 0xFFFFFFFF && (record.LBAStart + (long)record.LBALength) * Sizes.Sector > disk.Length) { return(false); } if (record.LBALength > 0) { foundPartition = true; } StreamExtent[] thisPartitionExtents = new StreamExtent[] { new StreamExtent(record.LBAStart, record.LBALength) }; // If the partition intersects another partition, this is probably an invalid partition table foreach (var overlap in StreamExtent.Intersect(knownPartitions, thisPartitionExtents)) { return(false); } knownPartitions = new List <StreamExtent>(StreamExtent.Union(knownPartitions, thisPartitionExtents)); } return(foundPartition); }
public override IEnumerable <StreamExtent> GetExtentsInRange(long start, long count) { CheckDisposed(); long maxCount = Math.Min(Length, start + count) - start; if (maxCount < 0) { return(new StreamExtent[0]); } IEnumerable <StreamExtent> parentExtents = _parentStream.GetExtentsInRange(start, maxCount); IEnumerable <StreamExtent> result = StreamExtent.Union(LayerExtents(start, maxCount), parentExtents); result = StreamExtent.Intersect(result, new[] { new StreamExtent(start, maxCount) }); return(result); }
public void ExtendTo(long fileSize, bool isFree) { if (fileSize % Sizes.OneMiB != 0) { throw new ArgumentException("VHDX space must be allocated on 1MB boundaries", "fileSize"); } if (fileSize < _fileSize) { throw new ArgumentOutOfRangeException("fileSize", "Attempt to extend file to smaller size", fileSize.ToString(CultureInfo.InvariantCulture)); } _fileSize = fileSize; if (isFree) { _freeExtents = new List <StreamExtent>(StreamExtent.Union(_freeExtents, new StreamExtent(_fileSize, fileSize - _fileSize))); } }
public void Release(long start, long length) { ValidateRange(start, length, "release"); _freeExtents = new List <StreamExtent>(StreamExtent.Union(_freeExtents, new StreamExtent(start, length))); }
public override IEnumerable <StreamExtent> GetExtentsInRange(long start, long count) { CheckDisposed(); return(StreamExtent.Intersect(StreamExtent.Union(GetExtentsRaw(start, count), _parentStream.GetExtentsInRange(start, count)), new StreamExtent(start, count))); }
public void TestUnion5() { StreamExtent[] r = new StreamExtent[] { }; Compare(r, StreamExtent.Union()); }