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 pathfilesystem.cpp
310 lines (252 loc) · 8.35 KB
/
filesystem.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
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
{
bool Virtual::PathIsRelative(const char *Path)
{
return cwk_path_is_relative(Path);
}
void Virtual::AddRoot(Inode *Root)
{
SmartLock(VirtualLock);
FileSystemRoots->Children.push_back(Root);
}
FileNode *Virtual::GetRoot(size_t Index)
{
assert(Index < FileSystemRoots->Children.size());
auto it = FileRoots.find(Index);
if (it != FileRoots.end())
return it->second;
Inode *rootNode = FileSystemRoots->Children[Index];
char rootName[128]{};
snprintf(rootName, sizeof(rootName), "\x06root-%ld\x06", Index);
FileNode *ret = this->CreateCacheNode(nullptr, rootNode, rootName, 0);
FileRoots.insert({Index, ret});
return ret;
}
FileNode *Virtual::Create(FileNode *Parent, const char *Name, mode_t Mode)
{
FileNode *existingNode = this->GetByPath(Name, Parent);
if (existingNode != nullptr)
ReturnLogError(existingNode, "File %s already exists", Name);
if (Parent == nullptr)
{
assert(thisProcess != nullptr);
Parent = thisProcess->Info.RootNode;
}
auto it = DeviceMap.find(Parent->Node->Device);
if (it == DeviceMap.end())
ReturnLogError(nullptr, "Device %d not found", Parent->Node->Device);
Inode *Node = NULL;
if (it->second.fsi->Ops.Create == NULL)
ReturnLogError(nullptr, "Create not supported for %d", it->first);
int ret = it->second.fsi->Ops.Create(Parent->Node, Name, Mode, &Node);
if (ret < 0)
ReturnLogError(nullptr, "Create for %d failed with %d", it->first, ret);
return this->CreateCacheNode(Parent, Node, Name, Mode);
}
FileNode *Virtual::ForceCreate(FileNode *Parent, const char *Name, mode_t Mode)
{
fixme("ForceCreate: %s", Name);
return this->Create(Parent, Name, Mode);
}
FileNode *Virtual::Mount(FileNode *Parent, Inode *Node, const char *Path)
{
char *path = strdup(Path);
char *lastSlash = strrchr(path, '/');
if (lastSlash == path)
lastSlash++;
*lastSlash = '\0';
FileNode *parentNode = this->GetByPath(path, Parent);
free(path);
lastSlash = strrchr(Path, '/');
lastSlash++;
return this->CreateCacheNode(parentNode, Node, lastSlash, Node->Mode);
}
int Virtual::Unmount(const char *Path)
{
FileNode *node = this->GetByPath(Path, nullptr);
if (node == nullptr)
ReturnLogError(-ENOENT, "Path %s not found", Path);
return this->RemoveCacheNode(node);
}
FileNode *Virtual::GetByPath(const char *Path, FileNode *Parent)
{
debug("GetByPath: %s", Path);
if (Parent == nullptr || this->PathIsAbsolute(Path))
Parent = thisProcess ? thisProcess->Info.RootNode : this->GetRoot(0);
if (strcmp(Path, ".") == 0)
return Parent;
if (strcmp(Path, "..") == 0)
return Parent->Parent ? Parent->Parent : Parent;
FileNode *fn = this->CacheRecursiveSearch(Parent, Path, this->PathIsRelative(Path));
if (fn)
return fn;
if (strncmp(Path, "\x06root-", 6) == 0) /* FIXME: deduce the index */
{
Path += 7;
while (*Path != '\0' && *Path != '\x06')
Path++;
if (*Path == '\x06')
Path++;
}
FileNode *__Parent = CacheSearchReturnLast(Parent, &Path);
struct cwk_segment segment;
if (!cwk_path_get_first_segment(Path, &segment))
{
auto it = DeviceMap.find(Parent->Node->Device);
if (unlikely(it == DeviceMap.end()))
ReturnLogError(nullptr, "Device %d not found", Parent->Node->Device);
if (it->second.fsi->Ops.Lookup == NULL)
ReturnLogError(nullptr, "Lookup not supported for %d", it->first);
Inode *Node = NULL;
int ret = it->second.fsi->Ops.Lookup(Parent->Node, Path, &Node);
if (ret < 0)
ReturnLogError(nullptr, "Lookup for \"%s\"(%d) failed with %d", Path, it->first, ret);
if (Parent->Node == Node) /* root / */
{
debug("Returning root (%#lx)", Node);
return Parent;
}
ReturnLogError(nullptr, "Path has no segments");
}
Inode *Node = NULL;
do
{
auto it = DeviceMap.find(__Parent->Node->Device);
if (unlikely(it == DeviceMap.end()))
ReturnLogError(nullptr, "Device %d not found", __Parent->Node->Device);
if (it->second.fsi->Ops.Lookup == NULL)
ReturnLogError(nullptr, "Lookup not supported for %d", it->first);
std::string segmentName(segment.begin, segment.size);
int ret = it->second.fsi->Ops.Lookup(__Parent->Node, segmentName.c_str(), &Node);
if (ret < 0)
ReturnLogError(nullptr, "Lookup for \"%s\"(%d) failed with %d", segmentName.c_str(), it->first, ret);
__Parent = this->CreateCacheNode(__Parent, Node, segmentName.c_str(), 0);
} while (cwk_path_get_next_segment(&segment));
FileNode *ret = __Parent;
if (!ret->IsDirectory())
return ret;
auto it = DeviceMap.find(__Parent->Node->Device);
if (unlikely(it == DeviceMap.end()))
ReturnLogError(nullptr, "Device %d not found", __Parent->Node->Device);
size_t dirAllocLen = sizeof(struct kdirent) + strlen(Path);
struct kdirent *dirent = (struct kdirent *)malloc(dirAllocLen);
size_t offset = 2; /* Skip . and .. */
while (it->second.fsi->Ops.ReadDir(Node, dirent, dirAllocLen, offset++, 1) > 0)
{
Inode *ChildNode = NULL;
int luRet = it->second.fsi->Ops.Lookup(Node, dirent->d_name, &ChildNode);
if (luRet < 0)
{
debug("Lookup for %d failed with %d", it->first, luRet);
break;
}
this->CreateCacheNode(ret, ChildNode, dirent->d_name, 0);
}
free(dirent);
return ret;
}
std::string Virtual::GetByNode(FileNode *Node)
{
assert(Node != nullptr);
if (Node->Parent == nullptr)
{
if (Node->Node->Flags & I_FLAG_ROOT)
return Node->fsi->RootName;
assert(Node->Parent != nullptr);
}
std::string path;
auto appendPath = [&path](const char *name)
{
if (path.size() > 0)
path += "/";
path += name;
};
FileNode *current = Node;
while (current->Parent != nullptr)
{
appendPath(current->Name.c_str());
current = current->Parent;
}
return path;
}
FileNode *Virtual::CreateLink(const char *Path, FileNode *Parent, const char *Target)
{
auto it = DeviceMap.find(Parent->Node->Device);
if (it == DeviceMap.end())
ReturnLogError(nullptr, "Device %d not found", Parent->Node->Device);
Inode *Node = NULL;
if (it->second.fsi->Ops.SymLink == NULL)
ReturnLogError(nullptr, "SymLink not supported for %d", it->first);
int ret = it->second.fsi->Ops.SymLink(Parent->Node, Path, Target, &Node);
if (ret < 0)
ReturnLogError(nullptr, "SymLink for %d failed with %d", it->first, ret);
return this->CreateCacheNode(Parent, Node, Path, 0);
}
FileNode *Virtual::CreateLink(const char *Path, FileNode *Parent, FileNode *Target)
{
return this->CreateLink(Path, Parent, Target->Path.c_str());
}
bool Virtual::PathExists(const char *Path, FileNode *Parent)
{
FileNode *fn = this->CacheLookup(Path);
if (fn)
return true;
FileNode *Node = this->GetByPath(Path, Parent);
if (Node)
return true;
return false;
}
int Virtual::Remove(FileNode *Node)
{
auto it = DeviceMap.find(Node->Node->Device);
if (it == DeviceMap.end())
ReturnLogError(-ENODEV, "Device %d not found", Node->Node->Device);
if (it->second.fsi->Ops.Remove == NULL)
ReturnLogError(-ENOTSUP, "Remove not supported for %d", it->first);
int ret = it->second.fsi->Ops.Remove(Node->Parent->Node, Node->Name.c_str());
if (ret < 0)
ReturnLogError(ret, "Remove for %d failed with %d", it->first, ret);
this->RemoveCacheNode(Node);
return 0;
}
}
std::string FileNode::GetName()
{
return this->Name;
}
std::string FileNode::GetPath()
{
const char *path = this->Path.c_str();
if (strncmp(path, "\x06root-", 6) == 0) /* FIXME: deduce the index */
{
path += 6;
while (*path != '\0' && *path != '\x06')
path++;
if (*path == '\x06')
path++;
}
else
return this->Path;
if (path[0] == '\0')
return std::string(this->fsi->RootName);
return std::string(path);
}