/// <summary> /// The ProcessRecord method instantiates a Superblock object based /// on the disk given as an argument. /// </summary> protected override void ProcessRecord() { #region MBR MasterBootRecord mbr = MasterBootRecord.Get(devicePath); uint superblockOffset = 0; foreach (PartitionEntry partition in mbr.PartitionTable) { if (partition.Bootable && partition.SystemID == "LINUX") { superblockOffset = partition.StartSector; } } #endregion MBR // Obtain a handle to the device named "devicePath" IntPtr hDevice = NativeMethods.getHandle(devicePath); using (FileStream streamToRead = NativeMethods.getFileStream(hDevice)) { // Get Superblock to understand File System Layout Superblock superBlock = new Superblock(Superblock.GetBytes(streamToRead, superblockOffset)); if (this.MyInvocation.BoundParameters.ContainsKey("GroupNumber")) { if (asbytes) { WriteObject(BlockGroupDescriptor.GetBytes(streamToRead, superblockOffset, superBlock, number)); } else { WriteObject(new BlockGroupDescriptor(BlockGroupDescriptor.GetBytes(streamToRead, superblockOffset, superBlock, number))); } } else { if (asbytes) { BlockGroupDescriptorTable.GetBytes(streamToRead, superblockOffset, superBlock); } else { byte[] bgdtBytes = BlockGroupDescriptorTable.GetBytes(streamToRead, superblockOffset, superBlock); for (uint o = 0; o < bgdtBytes.Length; o += BlockGroupDescriptor.BLOCK_GROUP_DESCRIPTOR_LENGTH) { WriteObject(new BlockGroupDescriptor(NativeMethods.GetSubArray(bgdtBytes, o, BlockGroupDescriptor.BLOCK_GROUP_DESCRIPTOR_LENGTH))); } } } } }
/// <summary> /// /// </summary> protected override void ProcessRecord() { #region MBR MasterBootRecord mbr = MasterBootRecord.Get(devicePath); uint superblockOffset = 0; foreach (PartitionEntry partition in mbr.PartitionTable) { if (partition.Bootable && partition.SystemID == "LINUX") { superblockOffset = partition.StartSector; } } #endregion MBR // Obtain a handle to the device named "devicePath" IntPtr hDevice = NativeMethods.getHandle(devicePath); using (FileStream streamToRead = NativeMethods.getFileStream(hDevice)) { // Get Superblock to understand File System Layout Superblock superBlock = new Superblock(Superblock.GetBytes(streamToRead, superblockOffset)); if (this.MyInvocation.BoundParameters.ContainsKey("Inode")) { if (inode == 0) { throw new Exception("0 is not a valid Inode value."); } else { uint block_group = (inode - 1) / superBlock.InodesPerGroup; BlockGroupDescriptor bgd = new BlockGroupDescriptor(BlockGroupDescriptorTable.GetBytes(streamToRead, superblockOffset, superBlock)); uint inodeTableOffset = (superblockOffset * NativeMethods.BYTES_PER_SECTOR) + (bgd.InodeTableOffset * superBlock.BlockSize); uint inodeSectorOffset = (inode - 1) / (NativeMethods.BYTES_PER_SECTOR / (uint)superBlock.InodeSize); byte[] SectorBytes = NativeMethods.readDrive(streamToRead, inodeTableOffset + (inodeSectorOffset * NativeMethods.BYTES_PER_SECTOR), NativeMethods.BYTES_PER_SECTOR); uint sectorOffset = ((inode - 1) % (NativeMethods.BYTES_PER_SECTOR / (uint)superBlock.InodeSize)) * (uint)superBlock.InodeSize; byte[] inodeBytes = NativeMethods.GetSubArray(SectorBytes, sectorOffset, (uint)superBlock.InodeSize); if (asbytes) { WriteObject(inodeBytes); } else { WriteObject(new Inode(inodeBytes, inode)); } } } else { // Create a byte array representing the BGDT byte[] bgdtBytes = BlockGroupDescriptorTable.GetBytes(streamToRead, superblockOffset, superBlock); // Iterate through BGDTs and output associate Inodes for (uint o = 0; o < bgdtBytes.Length; o += BlockGroupDescriptor.BLOCK_GROUP_DESCRIPTOR_LENGTH) { BlockGroupDescriptor bgd = new BlockGroupDescriptor(NativeMethods.GetSubArray(bgdtBytes, o, BlockGroupDescriptor.BLOCK_GROUP_DESCRIPTOR_LENGTH)); uint inodeTableOffset = (superblockOffset * NativeMethods.BYTES_PER_SECTOR) + (bgd.InodeTableOffset * superBlock.BlockSize); byte[] inodeTableBytes = InodeTable.GetBytes(streamToRead, superBlock, inodeTableOffset); for (uint i = 0; i < inodeTableBytes.Length; i += (uint)superBlock.InodeSize) { uint inode = ((o / BlockGroupDescriptor.BLOCK_GROUP_DESCRIPTOR_LENGTH) + 1) * ((i / (uint)superBlock.InodeSize) + 1); WriteObject(new Inode(NativeMethods.GetSubArray(inodeTableBytes, i, superBlock.InodeSize), inode)); } } } } }