-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
59 lines (54 loc) · 1.27 KB
/
filesystem.cpp
File metadata and controls
59 lines (54 loc) · 1.27 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
#include "filesystem.hpp"
#include <memory>
#include <string>
#include <vector>
FilesystemPath compute_path(const FilesystemPath &cwd, const std::string &path) {
size_t i = 0;
FilesystemPath result = cwd;
if (path[0] == '/') {
// absolute
i = 1;
result = {};
}
// relative
std::string component;
for (; i < path.size(); ++i) {
if (path[i] == '/') {
if (component == ".") {
} else if (component == "..") {
if (result.size())
result.pop_back();
} else {
result.push_back(component);
}
component = "";
} else {
component += path[i];
}
}
if (component == "") {
} else if (component == ".") {
} else if (component == "..") {
if (result.size())
result.pop_back();
} else {
result.push_back(component);
component = "";
}
return result;
}
Filesystem::Filesystem() {
root = std::make_unique<FSINode>(INodeType::INODE_DIR, nullptr, std::string{""}, 0777);
root->parent = root.get();
}
FSINode *Filesystem::get_inode(const FilesystemPath &path) {
FSINode *ptr = root.get();
for (auto segment : path) {
if (auto it = ptr->children.find(segment); it != ptr->children.end()) {
ptr = it->second.get();
} else {
return nullptr;
}
}
return ptr;
}