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

Function Unlink

src/filesystem/vfs/unlink.cpp:15–88  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13namespace vfs {
14
15auto Unlink(const char* path) -> Expected<void> {
16 if (path == nullptr) {
17 return std::unexpected(Error(ErrorCode::kInvalidArgument));
18 }
19
20 LockGuard<SpinLock> guard(GetVfsState().vfs_lock_);
21 // 解析父目录路径和文件名
22 char parent_path[512];
23 char file_name[256];
24 const char* last_slash = strrchr(path, '/');
25 if (last_slash == nullptr || last_slash == path) {
26 strncpy(parent_path, "/", sizeof(parent_path));
27 strncpy(file_name, path[0] == '/' ? path + 1 : path, sizeof(file_name));
28 } else {
29 size_t parent_len = last_slash - path;
30 if (parent_len >= sizeof(parent_path)) {
31 parent_len = sizeof(parent_path) - 1;
32 }
33 strncpy(parent_path, path, parent_len);
34 parent_path[parent_len] = '\0';
35 strncpy(file_name, last_slash + 1, sizeof(file_name));
36 }
37 file_name[sizeof(file_name) - 1] = '\0';
38
39 // 查找父目录
40 auto parent_result = Lookup(parent_path);
41 if (!parent_result.has_value()) {
42 return std::unexpected(parent_result.error());
43 }
44
45 Dentry* parent_dentry = parent_result.value();
46 if (parent_dentry->inode == nullptr) {
47 return std::unexpected(Error(ErrorCode::kFsCorrupted));
48 }
49
50 // 查找目标文件
51 Dentry* target_dentry = FindChild(parent_dentry, file_name);
52 if (target_dentry == nullptr) {
53 return std::unexpected(Error(ErrorCode::kFsFileNotFound));
54 }
55
56 if (target_dentry->inode == nullptr) {
57 return std::unexpected(Error(ErrorCode::kFsCorrupted));
58 }
59
60 // 不能删除目录
61 if (target_dentry->inode->type == FileType::kDirectory) {
62 return std::unexpected(Error(ErrorCode::kFsIsADirectory));
63 }
64
65 // 删除文件
66 if (parent_dentry->inode->ops == nullptr) {
67 return std::unexpected(Error(ErrorCode::kDeviceNotSupported));
68 }
69
70 auto result =
71 parent_dentry->inode->ops->Unlink(parent_dentry->inode, file_name);
72 if (!result.has_value()) {

Callers 2

RmdirMethod · 0.85
ramfs_testFunction · 0.85

Calls 9

ErrorClass · 0.85
GetVfsStateFunction · 0.85
strrchrFunction · 0.85
strncpyFunction · 0.85
LookupFunction · 0.85
FindChildFunction · 0.85
RemoveChildFunction · 0.85
DebugFunction · 0.85
UnlinkMethod · 0.45

Tested by 1

ramfs_testFunction · 0.68