C++实现含权限保护的二级文件系统:从设计到代码全解析
为了实现上述需求,我们可以设计一个简单的文件系统。以下是C++代码实现: ```cpp #include <iostream> #include <vector> #include <string> #include <unordered_map> // 文件保护码 enum Protection { READ = 1 << 2, // 0100 WRITE = 1 << 1, // 0010 EXECUTE = 1 << 0 // 0001 }; // 文件结构体 struct File { std::string name; int protectionCode; int length; int readPointer; int writePointer; File(std::string name, int protectionCode, int length) : name(name), protectionCode(protectionCode), length(length), readPointer(0), writePointer(0) {} }; // 用户文件目录 (UFD) struct UserFileDirectory { std::string username; std::vector<File> files; UserFileDirectory(std::string username) : username(username) {} }; // 主文件目录 (MFD) class MasterFileDirectory { private: std::unordered_map<std::string, UserFileDirectory*> mfd; public: void createUser(const std::string& username) { if (mfd.find(username) == mfd.end()) { mfd[username] = new UserFileDirectory(username); } else { std::cout << "User already exists." << std::endl; } } UserFileDirectory* getUser(const std::string& username) { if (mfd.find(username) != mfd.end()) { return mfd[username]; } else { std::cout << "User not found." << std::endl; return nullptr; } } void createFile(const std::string& username, const std::string& filename, int protectionCode, int length) { UserFileDirectory* ufd = getUser(username); if (ufd) { ufd->files.push_back(File(filename, protectionCode, length)); } } void deleteFile(const std::string& username, const std::string& filename) { UserFileDirectory* ufd = getUser(username); if (ufd) { for (auto it = ufd->files.begin(); it != ufd->files.end(); ++it) { if (it->name == filename) { ufd->files.erase(it); return; } } std::cout << "File not found." << std::endl; } } ~MasterFileDirectory() { for (auto& pair : mfd) { delete pair.second; } } }; // 打开文件目录 (AFD) class ActiveFileDirectory { private: File* currentFile; public: ActiveFileDirectory() : currentFile(nullptr) {} bool openFile(UserFileDirectory* ufd, const std::string& filename) { if (currentFile) { std::cout << "A file is already open. Close it first." << std::endl; return false; } for (auto& file : ufd->files) { if (file.name == filename) { currentFile = &file; return true; } } std::cout << "File not found." << std::endl; return false; } void closeFile() { currentFile = nullptr; } bool hasPermission(int permission) { if (currentFile) { return (currentFile->protectionCode & permission) == permission; } return false; } void readFile(int bytes) { if (!hasPermission(READ)) { std::cout << "Read permission denied." << std::endl; return; } if (currentFile->readPointer + bytes > currentFile->length) { bytes = currentFile->length - currentFile->readPointer; } std::cout << "Reading " << bytes << " bytes from " << currentFile->name << std::endl; currentFile->readPointer += bytes; } void writeFile(int bytes) { if (!hasPermission(WRITE)) { std::cout << "Write permission denied." << std::endl; return; } if (currentFile->writePointer + bytes > currentFile->length) { bytes = currentFile->length - currentFile->writePointer; } std::cout << "Writing " << bytes << " bytes to " << currentFile->name << std::endl; currentFile->writePointer += bytes; } }; int main() { MasterFileDirectory mfd; ActiveFileDirectory afd; mfd.createUser("user1"); mfd.createFile("user1", "file1", READ | WRITE, 100); mfd.createFile("user1", "file2", READ, 200); UserFileDirectory* ufd = mfd.getUser("user1"); if (afd.openFile(ufd, "file1")) { afd.readFile(50); afd.writeFile(30); afd.closeFile(); } if (afd.openFile(ufd, "file2")) { afd.readFile(50); afd.writeFile(30); // This should be denied afd.closeFile(); } mfd.deleteFile("user1", "file1"); return 0; } ``` 代码说明 1. 文件保护码:使用枚举 `Protection` 定义了读、写、执行的权限。 2. 文件结构体:定义了文件的基本属性,包括名称、保护码、长度以及读写指针。 3. 用户文件目录 (UFD):存储了用户的文件列表。 4. 主文件目录 (MFD):管理所有用户的文件目录,提供了创建用户、创建文件、删除文件等操作。 5. 打开文件目录 (AFD):管理当前打开的文件,提供了打开、关闭、读取、写入等操作,并检查权限。 功能说明 - 创建用户:通过 `createUser` 方法创建新用户。 - 创建文件:通过 `createFile` 方法为指定用户创建文件。 - 删除文件:通过 `deleteFile` 方法删除指定用户的文件。 - 打开文件:通过 `openFile` 方法打开文件,检查是否已经有文件打开。 - 关闭文件:通过 `closeFile` 方法关闭当前打开的文件。 - 读取文件:通过 `readFile` 方法读取文件内容,检查读权限。 - 写入文件:通过 `writeFile` 方法写入文件内容,检查写权限。 运行示例 1. 创建用户 `user1`。 2. 为 `user1` 创建两个文件 `file1` 和 `file2`,其中 `file1` 有读写权限,`file2` 只有读权限。 3. 打开 `file1`,读取 50 字节,写入 30 字节,然后关闭。 4. 打开 `file2`,读取 50 字节,尝试写入 30 字节(应被拒绝),然后关闭。 5. 删除 `file1`。 通过这个示例,你可以看到文件系统的创建、文件的创建与删除、文件的打开与关闭、文件的读写操作以及权限检查的完整流程。 ######[AI写代码神器 | 1602点数解答 | 2024-10-28 14:42:23]
- 批处理文件:依据参数表.xlsx精准修改文件.txt中p、t、s值(GPT | 811点数解答 | 2024-12-05 12:58:37)377
- 用批处理和Python实现“参数表.xlsx”与“文件.txt”数据匹配及修改(讯飞星火 | 531点数解答 | 2024-12-05 13:20:42)365
- 批处理文件:依据参数表.xlsx精准修改文件.txt特定参数值(阿里通义 | 495点数解答 | 2024-12-05 13:22:26)346
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)343
- 易语言编程:权限相关概念、获取方法、数据操作及管理注意事项揭秘(字节豆包 | 800点数解答 | 2026-02-10 01:27:41)41
- Python 利用 win32com 库:将 Excel 转换为 A4 横向 PDF 并单独打印首列(字节豆包 | 507点数解答 | 2025-03-10 15:48:12)301
- Golang开发:解析JSON数据时数值默认类型及示例解析(百度文心 | 393点数解答 | 2023-11-09 18:13:06)274
- SQL Working Areas Sizing: Manual vs. Automatic Policy - A Comprehensive Analysis(阿里通义 | 530点数解答 | 2024-05-13 10:55:58)305
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)310
- 用 JS 中 for 循环实现 1 到 100 相加并输出结果到页面的完整代码 ( | 240点数解答 | 2024-05-20 22:11:29)457
- 用 JS 的 while 循环实现 1 到 100 相加并输出到页面的代码揭秘( | 47点数解答 | 2024-05-20 22:16:48)354
- 深度剖析:黑盒、白盒、单元、集成、系统与验收测试的区别与联系 (百度文心 | 424点数解答 | 2023-11-09 18:24:11)298