-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_helper.cpp
More file actions
126 lines (124 loc) · 3.11 KB
/
path_helper.cpp
File metadata and controls
126 lines (124 loc) · 3.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "path_helper.h"
#include "inode_helper.h"
#include <string>
using namespace std;
INode * PathHelper::GetINodeFromPath(const Dir & curDir, const PathRoute & route) {
Dir cur_dir = curDir;
if (route.startFromRoot) {
cur_dir = INodeHelper::GetRootDir();
}
if (!(cur_dir.HasPermissionRead() && cur_dir.HasPermissionExec())) {
return NULL;
}
for (size_t i = 0; i + 1 < route.size();i++) {
const string & name = route[i];
ID_T sub_dir_id = cur_dir.findEntry(name);
if (sub_dir_id == 0) {
return NULL;
}
cur_dir = Dir(sub_dir_id);
if (!(cur_dir.HasPermissionRead() && cur_dir.HasPermissionExec())) {
return NULL;
}
}
if (route.size() > 0) {
string last_file_name = route.back();
ID_T ret_id = cur_dir.findEntry(last_file_name);
if (ret_id == 0) {
return NULL;
}
INode inode = INode(ret_id);
//if (!(inode.HasPermissionRead() && inode.HasPermissionExec())) {
// return NULL;
//}
if (inode.GetType() == INodeType::kFILE) {
return new File(inode);
}
else if (inode.GetType() == INodeType::kDIR) {
return new Dir(inode);
}
return NULL;
}
else {
return new Dir(cur_dir);
}
}
INode * PathHelper::MakeDir(const Dir & curDir, const PathRoute & route) {
Dir cur_dir = curDir;
if (route.startFromRoot) {
cur_dir = INodeHelper::GetRootDir();
}
if (!(cur_dir.HasPermissionRead() && cur_dir.HasPermissionExec())) {
return NULL;
}
for (size_t i = 0; i + 1 < route.size(); i++) {
const string & name = route[i];
ID_T sub_dir_id = cur_dir.findEntry(name);
if (sub_dir_id == 0) {
if (!(cur_dir.HasPermissionWrite())) {
return NULL;
}
Dir newDir = INodeHelper::CreateDir(cur_dir.GetID(), name);
sub_dir_id = newDir.GetID();
}
cur_dir = Dir(sub_dir_id);
if (!(cur_dir.HasPermissionRead() && cur_dir.HasPermissionExec())) {
return NULL;
}
}
if (route.size() > 0) {
string last_file_name = route.back();
ID_T ret_id = cur_dir.findEntry(last_file_name);
if (ret_id == 0) {
if (!(cur_dir.HasPermissionWrite())) {
return NULL;
}
Dir newDir = INodeHelper::CreateDir(cur_dir.GetID(), last_file_name);
return new Dir(newDir);
}
INode inode = INode(ret_id);
if (!(inode.HasPermissionWrite())) {
return NULL;
}
if (inode.GetType() == INodeType::kFILE) {
return NULL;
}
else if (inode.GetType() == INodeType::kDIR) {
return new Dir(inode);
}
return NULL;
}
else {
return new Dir(cur_dir);
}
}
void PathHelper::GetPathRoute(const string & path, PathRoute & route)
{
size_t last_pos = 0;// last found path delimiter plus 1
if (path.length() == 0) {
return;
}
size_t pos = string::npos;
if (path.substr(0, kPATH_DELIMITER.length()) == kPATH_DELIMITER) {
last_pos = kPATH_DELIMITER.length();
route.startFromRoot = true;
}
else {
route.startFromRoot = false;
}
while ((pos = path.find(kPATH_DELIMITER, last_pos)) != string::npos) {
if (pos == last_pos) {
last_pos++;
continue;
}
string sub_str = path.substr(last_pos, pos - last_pos);
route.push_back(sub_str);
last_pos = pos + 1;
}
if (last_pos >= path.length()) {
return;
}
string filename = path.substr(last_pos);
route.push_back(filename);
return;
}