public VFSCore(AbstractDevice device) { this.device = device; this.deviceSize = device.Size(); // 初始化超级块 this.superBlock = SuperBlock.Load(this); if (IsFormated()) { loadBitmaps(); Console.WriteLine("载入镜像成功。"); Console.WriteLine("SuperBlock: {0}", superBlock.ToString()); } }
/// <summary> /// 格式化存储介质 /// </summary> /// <param name="inodeCapacity">inode 数量</param> /// <param name="blockSizeKByte">block 大小(KB),必须为 1,2,4,8 中的一个</param> public void Format(UInt32 inodeCapacity, UInt16 blockSizeKByte = 4) { if (inodeCapacity < 32) { throw new Exception("inode 至少为 32 个"); } if (!new int[] { 1, 2, 4, 8 }.Contains(blockSizeKByte)) { throw new Exception("block 大小只能为 1KB, 2KB, 4KB 或 8KB"); } uint offset = 0; offset += (uint)Utils.GetStructSize<_SuperBlock>(); offset += (inodeCapacity / 32 + 1) * 4; offset += (uint)Utils.GetStructSize<_INode>() * inodeCapacity; if (offset > deviceSize) { throw new Exception("inode 数量过大,结构超出存储介质最大空间"); } // 可留给数据块位图和数据块区的大小 uint sizeRemain = deviceSize - offset; // 全部留给数据块,可以有多少个数据块 uint blockCapacity = sizeRemain / blockSizeKByte >> 10; if (blockCapacity < 128) { throw new Exception("磁盘空间太小,至少要可以容纳 128 个块"); } // 删除 (blockCapacity / 32 + 1) * 4 大小的数据块,留作数据块位图使用 blockCapacity -= ((blockCapacity / 32 + 1) * 4 / blockSizeKByte) + 1; // 初始化 superBlock superBlock = SuperBlock.Create(this, inodeCapacity, (UInt16)(blockSizeKByte << 10), blockCapacity); // 单个 inode BitVector 可容纳 32 位 inodeBitmaps = new UInt32[inodeCapacity / 32 + 1]; device.WriteArray(superBlock.pInodeBitVectors, inodeBitmaps, 0, inodeBitmaps.Length); // 单个 block BitVector 可容纳 32 位 blockBitmaps = new UInt32[blockCapacity / 32 + 1]; device.WriteArray(superBlock.pBlockBitVectors, blockBitmaps, 0, blockBitmaps.Length); // 写入根目录 INodeDirectory.Create(this); Console.WriteLine("格式化成功。"); Console.WriteLine("SuperBlock: {0}", superBlock.ToString()); }
/// <summary> /// 创建一个全新的 SuperBlock /// </summary> /// <param name="vfs"></param> /// <param name="inodeCapacity"></param> /// <param name="blockSize"></param> /// <param name="blockCapacity"></param> /// <returns></returns> public static SuperBlock Create(VFSCore vfs, UInt32 inodeCapacity, UInt16 blockSize, UInt32 blockCapacity) { var _superBlock = new _SuperBlock(inodeCapacity, blockSize, blockCapacity); var superBlock = new SuperBlock(vfs, _superBlock); superBlock.Save(); return superBlock; }