Beispiel #1
0
		public FileSystem (SuperBlock superBlock)
		{
			this.superBlock = superBlock;

			///////////////////////////////////////////////////////////////////////
			this.blockSize = (uint) (1024 << (int) this.superBlock.BlockSize);

			if (this.superBlock.FragmentSize < 0)
				this.fragmentSize = (uint) (1024 >> -superBlock.FragmentSize);
			else
				this.fragmentSize = (uint) (1024 << superBlock.FragmentSize);

			if (superBlock.INodesPerGroup == 0)
				throw new Exception ("Not a valid SuperBlock.");

			this.groupsCount = (uint) (superBlock.INodesCount / superBlock.INodesPerGroup);
			this.fragmentsPerBlock = (uint) (this.blockSize / this.fragmentSize);
			this.groupDescriptorsPerBlock = (uint) (this.blockSize / GroupDescriptor.GroupDescriptorSize); //Marshal.SizeOf (typeof (GroupDescriptor)));
			this.indirectCount = this.blockSize >> 2;

			if (this.superBlock.RevisionLevel == 1)
				this.inodeSize = this.superBlock.INodeSize;
			else
				this.inodeSize = 128;

			this.inodesPerBlock = (uint) (this.blockSize / this.inodeSize);
			this.inodeTableBlocksCount = (uint) Math.Ceiling ((double) (this.superBlock.INodesPerGroup * this.inodeSize) / (double) this.blockSize);

			///////////////////////////////////////////////////////////////////////
			uint bitmapSize = (uint) Math.Ceiling (this.superBlock.BlocksPerGroup / 8.0f);
			this.blockBitmapBlocksCount = (uint) Math.Ceiling ((double) bitmapSize / (double) this.blockSize);

			this.groupDescriptors = new GroupDescriptor [this.groupsCount];
		}
Beispiel #2
0
        public FileSystem(SuperBlock superBlock)
        {
            this.superBlock = superBlock;

            ///////////////////////////////////////////////////////////////////////
            this.blockSize = (uint)(1024 << (int)this.superBlock.BlockSize);

            if (this.superBlock.FragmentSize < 0)
            {
                this.fragmentSize = (uint)(1024 >> -superBlock.FragmentSize);
            }
            else
            {
                this.fragmentSize = (uint)(1024 << superBlock.FragmentSize);
            }

            if (superBlock.INodesPerGroup == 0)
            {
                throw new Exception("Not a valid SuperBlock.");
            }

            this.groupsCount              = (uint)(superBlock.INodesCount / superBlock.INodesPerGroup);
            this.fragmentsPerBlock        = (uint)(this.blockSize / this.fragmentSize);
            this.groupDescriptorsPerBlock = (uint)(this.blockSize / GroupDescriptor.GroupDescriptorSize);              //Marshal.SizeOf (typeof (GroupDescriptor)));
            this.indirectCount            = this.blockSize >> 2;

            if (this.superBlock.RevisionLevel == 1)
            {
                this.inodeSize = this.superBlock.INodeSize;
            }
            else
            {
                this.inodeSize = 128;
            }

            this.inodesPerBlock        = (uint)(this.blockSize / this.inodeSize);
            this.inodeTableBlocksCount = (uint)Math.Ceiling((double)(this.superBlock.INodesPerGroup * this.inodeSize) / (double)this.blockSize);

            ///////////////////////////////////////////////////////////////////////
            uint bitmapSize = (uint)Math.Ceiling(this.superBlock.BlocksPerGroup / 8.0f);

            this.blockBitmapBlocksCount = (uint)Math.Ceiling((double)bitmapSize / (double)this.blockSize);

            this.groupDescriptors = new GroupDescriptor [this.groupsCount];
        }
Beispiel #3
0
        private void ReadSuperBlock(bool hasMBR)
        {
            if (hasMBR)
            {
                byte [] mbr = binaryReader.ReadBytes(512);

                if (mbr [510] != 55 && mbr [511] != 0xaa)
                {
                    throw new Exception("Invalid MBR Signature.");
                }

                bool found = false;

                for (int i = 446; i < 510; i += 16)
                {
                    if ((mbr [i] & 0x80) != 0)
                    {
                        found = true;

                        if (mbr [i + 4] != 0x83)
                        {
                            throw new Exception("The bootable partition is not of type Ext2.");
                        }

                        startOffset  = mbr [i + 8];
                        startOffset += (uint)(mbr [i + 9] << 8);
                        startOffset += (uint)(mbr [i + 10] << 16);
                        startOffset += (uint)(mbr [i + 11] << 24);

                        startOffset *= 512;

                        break;
                    }
                }

                if (!found)
                {
                    throw new Exception("No bootable partition found.");
                }
            }

            binaryReader.BaseStream.Seek(startOffset + 1024, SeekOrigin.Begin);

            byte [] buffer = binaryReader.ReadBytes(1024);
            Block   block  = new Block(1024, buffer);

            SuperBlock superBlock = new SuperBlock(block);

            this.fileSystem = new FileSystem(superBlock);

            if (this.fileSystem.SuperBlock.Magic != SuperBlock.EXT2_MAGIC)
            {
                throw new Exception("Not an Ext2 partition.");
            }

            if (this.fileSystem.SuperBlock.Errors != SuperBlock.EXT2_ERRORS_CONTINUE)
            {
                throw new Exception("Invalid Ext2 SuperBlock state.");
            }

            if (this.fileSystem.SuperBlock.AlgorithmsBitmap != 0)
            {
                throw new Exception("Unsupported Bitmap Compression Algorithm.");
            }
        }