public VfsExtFileSystem(Stream stream, FileSystemParameters parameters) : base(new ExtFileSystemOptions(parameters)) { stream.Position = 1024; byte[] superblockData = Utilities.ReadFully(stream, 1024); SuperBlock superblock = new SuperBlock(); superblock.ReadFrom(superblockData, 0); if (superblock.Magic != SuperBlock.Ext2Magic) { throw new IOException("Invalid superblock magic - probably not an Ext file system"); } if (superblock.RevisionLevel == SuperBlock.OldRevision) { throw new IOException("Old ext revision - not supported"); } if ((superblock.IncompatibleFeatures & ~SupportedIncompatibleFeatures) != 0) { throw new IOException("Incompatible ext features present: " + (superblock.IncompatibleFeatures & ~SupportedIncompatibleFeatures)); } Context = new Context { RawStream = stream, SuperBlock = superblock, Options = (ExtFileSystemOptions)Options }; uint numGroups = Utilities.Ceil(superblock.BlocksCount, superblock.BlocksPerGroup); long blockDescStart = (superblock.FirstDataBlock + 1) * (long)superblock.BlockSize; stream.Position = blockDescStart; byte[] blockDescData = Utilities.ReadFully(stream, (int)numGroups * BlockGroup.DescriptorSize); _blockGroups = new BlockGroup[numGroups]; for (int i = 0; i < numGroups; ++i) { BlockGroup bg = new BlockGroup(); bg.ReadFrom(blockDescData, i * BlockGroup.DescriptorSize); _blockGroups[i] = bg; } RootDirectory = new Directory(Context, 2, GetInode(2)); }
internal static bool Detect(Stream stream) { if (stream.Length < 2048) { return false; } stream.Position = 1024; byte[] superblockData = Utilities.ReadFully(stream, 1024); SuperBlock superblock = new SuperBlock(); superblock.ReadFrom(superblockData, 0); return superblock.Magic == SuperBlock.Ext2Magic; }
internal static bool Detect(Stream stream) { if (stream.Length < 2048) { return(false); } stream.Position = 1024; byte[] superblockData = Utilities.ReadFully(stream, 1024); SuperBlock superblock = new SuperBlock(); superblock.ReadFrom(superblockData, 0); return(superblock.Magic == SuperBlock.Ext2Magic); }
public VfsExtFileSystem(Stream stream, FileSystemParameters parameters) : base(new ExtFileSystemOptions(parameters)) { stream.Position = 1024; byte[] superblockData = StreamUtilities.ReadExact(stream, 1024); SuperBlock superblock = new SuperBlock(); superblock.ReadFrom(superblockData, 0); if (superblock.Magic != SuperBlock.Ext2Magic) { throw new IOException("Invalid superblock magic - probably not an Ext file system"); } if (superblock.RevisionLevel == SuperBlock.OldRevision) { throw new IOException("Old ext revision - not supported"); } if ((superblock.IncompatibleFeatures & ~SupportedIncompatibleFeatures) != 0) { throw new IOException("Incompatible ext features present: " + (superblock.IncompatibleFeatures & ~SupportedIncompatibleFeatures)); } Context = new Context { RawStream = stream, SuperBlock = superblock, Options = (ExtFileSystemOptions)Options }; uint numGroups = MathUtilities.Ceil(superblock.BlocksCount, superblock.BlocksPerGroup); long blockDescStart = (superblock.FirstDataBlock + 1) * (long)superblock.BlockSize; stream.Position = blockDescStart; var bgDescSize = superblock.Has64Bit ? BlockGroup64.DescriptorSize64 : BlockGroup.DescriptorSize; byte[] blockDescData = StreamUtilities.ReadExact(stream, (int)numGroups * bgDescSize); _blockGroups = new BlockGroup[numGroups]; for (int i = 0; i < numGroups; ++i) { BlockGroup bg = superblock.Has64Bit ? new BlockGroup64() : new BlockGroup(); bg.ReadFrom(blockDescData, i * bgDescSize); _blockGroups[i] = bg; } var journalSuperBlock = new JournalSuperBlock(); if (superblock.JournalInode != 0) { var journalInode = GetInode(superblock.JournalInode); var journalDataStream = journalInode.GetContentBuffer(Context); var journalData = StreamUtilities.ReadExact(journalDataStream, 0, 1024 + 12); journalSuperBlock.ReadFrom(journalData, 0); Context.JournalSuperblock = journalSuperBlock; } RootDirectory = new Directory(Context, 2, GetInode(2)); }