-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_delete.cpp
More file actions
48 lines (47 loc) · 1.11 KB
/
cmd_delete.cpp
File metadata and controls
48 lines (47 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include"inode_helper.h"
#include"path_helper.h"
#include "arg_parser.h"
#include "user_context.h"
class CmdDelete :public IRunnable {
public:
int run(int argc, string* argv) override {
if (argc >= 1) {
string filepath = argv[0];
Dir curDir = Dir(UserContext::cur_dir_id);
PathRoute route;
PathHelper::GetPathRoute(filepath, route);
if (route.size() == 0) {
return -1;
}
string filename = route.back();
route.pop_back();
INode * inode = PathHelper::GetINodeFromPath(curDir, route);
if (inode == 0) {
cerr << "path is not exist!" << endl;
return -1;
}
if (inode->GetType() != INodeType::kDIR) {
cerr << "path is not a dir!";
return -1;
}
Dir parentDir = Dir(*inode);
if (!parentDir.HasPermissionWrite()) {
cerr << "permission deny" << endl;
return -1;
}
ID_T cur_id = parentDir.findEntry(filename);
if (cur_id != 0) {
INode inode = INode(cur_id);
parentDir.deleteEntry(inode);
cout << "delete file success! inode id:" << cur_id << endl;
}
else {
cerr << "file not exist" << endl;
}
return 0;
}
else {
return -1;
}
}
};