MCPcopy Create free account
hub / github.com/Simple-XX/SimpleKernel / Open

Function Open

src/filesystem/vfs/open.cpp:14–150  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12namespace vfs {
13
14auto Open(const char* path, OpenFlags flags) -> Expected<File*> {
15 if (!GetVfsState().initialized) {
16 return std::unexpected(Error(ErrorCode::kFsNotMounted));
17 }
18
19 if (path == nullptr) {
20 return std::unexpected(Error(ErrorCode::kInvalidArgument));
21 }
22
23 if (strlen(path) >= kMaxPathLength) {
24 return std::unexpected(Error(ErrorCode::kFsInvalidPath));
25 }
26
27 LockGuard<SpinLock> guard(GetVfsState().vfs_lock_);
28 // 查找或创建 dentry
29 auto lookup_result = Lookup(path);
30 Dentry* dentry = nullptr;
31
32 if (!lookup_result.has_value()) {
33 // 文件不存在,检查是否需要创建
34 if ((flags & OpenFlags::kOCreate) == 0U) {
35 return std::unexpected(lookup_result.error());
36 }
37
38 // 创建新文件
39 // 找到父目录路径
40 char parent_path[512];
41 char file_name[256];
42 const char* last_slash = strrchr(path, '/');
43 if (last_slash == nullptr || last_slash == path) {
44 strncpy(parent_path, "/", sizeof(parent_path));
45 const char* name_start = path[0] == '/' ? path + 1 : path;
46 if (strlen(name_start) >= sizeof(file_name)) {
47 return std::unexpected(Error(ErrorCode::kFsInvalidPath));
48 }
49 strncpy(file_name, name_start, sizeof(file_name));
50 } else {
51 size_t parent_len = last_slash - path;
52 if (parent_len >= sizeof(parent_path)) {
53 return std::unexpected(Error(ErrorCode::kFsInvalidPath));
54 }
55 strncpy(parent_path, path, parent_len);
56 parent_path[parent_len] = '\0';
57 if (strlen(last_slash + 1) >= sizeof(file_name)) {
58 return std::unexpected(Error(ErrorCode::kFsInvalidPath));
59 }
60 strncpy(file_name, last_slash + 1, sizeof(file_name));
61 }
62 file_name[sizeof(file_name) - 1] = '\0';
63
64 // 查找父目录
65 auto parent_result = Lookup(parent_path);
66 if (!parent_result.has_value()) {
67 return std::unexpected(parent_result.error());
68 }
69
70 Dentry* parent_dentry = parent_result.value();
71 if (parent_dentry->inode == nullptr ||

Callers 2

ramfs_testFunction · 0.85
fatfs_testFunction · 0.85

Calls 11

GetVfsStateFunction · 0.85
ErrorClass · 0.85
strlenFunction · 0.85
LookupFunction · 0.85
strrchrFunction · 0.85
strncpyFunction · 0.85
AddChildFunction · 0.85
DebugFunction · 0.85
CreateMethod · 0.45
GetFileOpsMethod · 0.45
OpenMethod · 0.45

Tested by 2

ramfs_testFunction · 0.68
fatfs_testFunction · 0.68