-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_trace.cpp
More file actions
83 lines (80 loc) · 1.55 KB
/
path_trace.cpp
File metadata and controls
83 lines (80 loc) · 1.55 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
#include"path_trace.h"
#include"inode_helper.h"
#include "dir.h"
#include <sstream>
using namespace std;
void PathTrace::push(const Dir & dir, const string & name)
{
if (dir.GetID() == INodeHelper::GetRootDir().GetID()) {
traces.clear();
format();
return;
}
ID_T parent_id = dir.findEntry("..");
if (parent_id == INodeHelper::GetRootDir().GetID()) {
traces.clear();
traces.push_back(PathTraceEntry{
name,
dir
});
format();
return;
}
for (auto it = traces.begin(); it != traces.end();++it) {
if (it->dir.GetID() == parent_id) {
traces.erase(++it, traces.end());
traces.push_back(PathTraceEntry{
name,
dir
});
format();
return;
}
}
//not found
traces.clear();
Dir curdir = dir;
string curname;
while (curdir.GetID() != kROOT_ID) {
Dir parent = Dir(curdir.findEntry(".."));
traces.push_front(PathTraceEntry{
parent.findEntryName(curdir.GetID()),
curdir
});
curdir = parent;
}
format();
return;
//throw new exception("path trace faild.");
}
void PathTrace::format()
{
bool needFormat = false;
for (auto & entry : traces) {
if (entry.name == "." || entry.name == "..") {
needFormat = true;
break;
}
}
if (needFormat) {
Dir curDir = INodeHelper::GetRootDir();
for (auto & entry : traces) {
entry.name = curDir.findEntryName(entry.dir.GetID());
curDir = entry.dir;
}
}
}
string PathTrace::getPath() const
{
stringstream in;
for (auto entry : traces) {
in << "/"<<entry.name;
}
string path = in.str();
if (path.length() == 0) {
return "/";
}
else {
return path;
}
}