#include "simplefs.h" #include "disk_io.h" #include "utils.h" #include #include #include #include #include #include int main(int argc, char* argv[]) { if (argc < 2) { std::cerr << "用法: " << argv[0] << " <设备文件>" << std::endl; return 1; } const char* path = argv[1]; int fd = open(path, O_RDONLY); if (fd < 0) { perror("打开设备文件失败"); return 1; } std::vector buf(SIMPLEFS_BLOCK_SIZE); if (read_block(fd, 1, buf.data()) != 0) { std::cerr << "读取超级块失败" << std::endl; close(fd); return 1; } SimpleFS_SuperBlock sb; std::memcpy(&sb, buf.data(), sizeof(sb)); if (sb.s_magic != SIMPLEFS_MAGIC) { std::cerr << "魔数不匹配,不是SimpleFS镜像" << std::endl; close(fd); return 1; } uint32_t num_groups = static_cast(std::ceil((double)sb.s_blocks_count / sb.s_blocks_per_group)); uint32_t gdt_size = num_groups * sizeof(SimpleFS_GroupDesc); uint32_t gdt_blocks = static_cast(std::ceil((double)gdt_size / SIMPLEFS_BLOCK_SIZE)); std::vector gdt_raw(gdt_blocks * SIMPLEFS_BLOCK_SIZE); for (uint32_t i=0;i gdt(num_groups); std::memcpy(gdt.data(), gdt_raw.data(), gdt_size); uint64_t calc_free_blocks=0, calc_free_inodes=0; for(uint32_t grp=0; grp bb(SIMPLEFS_BLOCK_SIZE), ib(SIMPLEFS_BLOCK_SIZE); read_block(fd, gd.bg_block_bitmap, bb.data()); read_block(fd, gd.bg_inode_bitmap, ib.data()); uint32_t freeb=0, freei=0; for(uint32_t b=0;b