This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.cpp
226 lines (185 loc) · 5.11 KB
/
cache.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <filesystem.hpp>
#include <convert.h>
#include <printf.h>
#include <rand.hpp>
#include <cwalk.h>
#include "../kernel.h"
namespace vfs
{
FileNode *Virtual::CacheSearchReturnLast(FileNode *Parent, const char **Path)
{
assert(Parent != nullptr);
struct cwk_segment segment;
if (!cwk_path_get_first_segment(*Path, &segment))
{
if (strcmp(*Path, Parent->fsi->RootName) == 0)
return Parent;
ReturnLogError(nullptr, "Failed to get first segment of path");
}
size_t segments = 0;
while (cwk_path_get_next_segment(&segment))
segments++;
if (segments == 0)
return Parent;
const char *tmpPath = *Path;
if (strncmp(tmpPath, "\x06root-", 6) == 0) /* FIXME: deduce the index */
{
tmpPath += 6;
while (*tmpPath != '\0' && *tmpPath != '\x06')
tmpPath++;
if (*tmpPath == '\x06')
tmpPath++;
}
else
tmpPath = *Path;
FileNode *__Parent = Parent;
if (this->PathIsAbsolute(tmpPath))
{
while (__Parent->Parent)
__Parent = __Parent->Parent;
}
cwk_path_get_first_segment(tmpPath, &segment);
do
{
std::string segmentName(segment.begin, segment.size);
bool found = false;
for (FileNode *fn : __Parent->Children)
{
if (fn->Name != segmentName)
continue;
cwk_segment __seg = segment;
assert(cwk_path_get_next_segment(&__seg)); /* There's something wrong */
__Parent = fn;
found = true;
break;
}
if (!found)
{
*Path = segment.begin;
break;
}
} while (cwk_path_get_next_segment(&segment));
return __Parent;
}
FileNode *Virtual::CacheRecursiveSearch(FileNode *Root, const char *NameOrPath, bool IsName)
{
if (Root == nullptr)
return nullptr;
debug("%s cache search for \"%s\" in \"%s\"",
IsName ? "Relative" : "Absolute",
NameOrPath,
Root->Path.c_str());
struct cwk_segment segment;
if (!cwk_path_get_first_segment(NameOrPath, &segment))
{
if (strcmp(NameOrPath, Root->fsi->RootName) == 0)
return Root;
ReturnLogError(nullptr, "Failed to get first segment of path");
}
size_t segments = 0;
while (cwk_path_get_next_segment(&segment))
segments++;
if (IsName && segments == 0)
{
for (FileNode *fn : Root->Children)
{
if (fn->Name == NameOrPath)
return fn;
}
ReturnLogError(nullptr, "Failed to find \"%s\" in \"%s\"", NameOrPath, Root->Path.c_str());
}
const char *path = NameOrPath;
if (strncmp(path, "\x06root-", 6) == 0) /* FIXME: deduce the index */
{
path += 6;
while (*path != '\0' && *path != '\x06')
path++;
if (*path == '\x06')
path++;
}
else
path = NameOrPath;
FileNode *__Parent = Root;
if (this->PathIsAbsolute(path))
{
/* Get the root if Root is not the root 【・_・?】 */
while (__Parent->Parent)
__Parent = __Parent->Parent;
}
cwk_path_get_first_segment(path, &segment);
do
{
std::string segmentName(segment.begin, segment.size);
bool found = false;
for (FileNode *fn : __Parent->Children)
{
if (fn->Name != segmentName)
continue;
cwk_segment __seg = segment;
if (!cwk_path_get_next_segment(&__seg))
return fn;
__Parent = fn;
found = true;
break;
}
if (!found)
break;
} while (cwk_path_get_next_segment(&segment));
debug("Failed to find \"%s\" in \"%s\"", NameOrPath, Root->Path.c_str());
return nullptr;
}
FileNode *Virtual::CacheLookup(const char *Path)
{
debug("Cache lookup for \"%s\"", Path);
FileNode *rootNode = thisProcess ? thisProcess->Info.RootNode : this->GetRoot(0);
FileNode *ret = CacheRecursiveSearch(rootNode, Path, false);
if (ret)
return ret;
debug("Path \"%s\" not found", Path);
return nullptr;
}
FileNode *Virtual::CreateCacheNode(FileNode *Parent, Inode *Node, const char *Name, mode_t Mode)
{
FileNode *fn = new FileNode;
fn->Name = Name;
if (Parent)
{
fn->Path = Parent->Path + "/" + Name;
Parent->Children.push_back(fn);
}
else
fn->Path = Name;
fn->Parent = Parent;
fn->Node = Node;
fn->fsi = DeviceMap[Node->Device].fsi;
if (fn->fsi == nullptr)
warn("Failed to find filesystem for device %d", Node->Device);
debug("Created cache node %s", fn->Path.c_str());
return fn;
}
int Virtual::RemoveCacheNode(FileNode *Node)
{
if (Node == nullptr)
return -EINVAL;
if (Node->Parent)
{
Node->Parent->Children.erase(std::find(Node->Parent->Children.begin(), Node->Parent->Children.end(), Node));
// delete Node;
fixme("Node deletion is disabled for now (for debugging purposes)");
}
return 0;
}
}