15 if (path ==
nullptr) {
21 char parent_path[512];
23 const char* last_slash =
strrchr(path,
'/');
24 if (last_slash ==
nullptr || last_slash == path) {
25 strncpy(parent_path,
"/",
sizeof(parent_path));
26 const char* name_start = path[0] ==
'/' ? path + 1 : path;
27 if (
strlen(name_start) >=
sizeof(dir_name)) {
30 strncpy(dir_name, name_start,
sizeof(dir_name));
32 size_t parent_len = last_slash - path;
33 if (parent_len >=
sizeof(parent_path)) {
36 strncpy(parent_path, path, parent_len);
37 parent_path[parent_len] =
'\0';
38 if (
strlen(last_slash + 1) >=
sizeof(dir_name)) {
41 strncpy(dir_name, last_slash + 1,
sizeof(dir_name));
43 dir_name[
sizeof(dir_name) - 1] =
'\0';
46 auto parent_result =
Lookup(parent_path);
47 if (!parent_result.has_value()) {
48 return std::unexpected(parent_result.error());
51 Dentry* parent_dentry = parent_result.value();
52 if (parent_dentry->
inode ==
nullptr ||
58 if (
FindChild(parent_dentry, dir_name) !=
nullptr) {
63 if (parent_dentry->
inode->
ops ==
nullptr) {
69 if (!result.has_value()) {
70 return std::unexpected(result.error());
74 auto new_dentry = kstd::make_unique<Dentry>();
79 strncpy(new_dentry->name, dir_name,
sizeof(new_dentry->name) - 1);
80 new_dentry->name[
sizeof(new_dentry->name) - 1] =
'\0';
81 new_dentry->inode = result.value();
82 AddChild(parent_dentry, new_dentry.release());
Dentry — 目录项缓存(路径名 ↔ Inode 的映射)