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
|
#include <cstdint>
#include <generic/vfs.hpp>
#include <drivers/disk.hpp>
#include <klibc/string.hpp>
#include <utils/errno.hpp>
#include <klibc/stdio.hpp>
#include <generic/lock/mutex.hpp>
#include <utils/assert.hpp>
#include <generic/tmpfs.hpp>
// /bin/path -> /usr/bin/path
void __vfs_symlink_resolve_no_at_symlink_follow(char* path, char* out) {
char *next = nullptr;
char buffer[4096];
int e = vfs::readlink(path,buffer,4096);
if(e == -ENOSYS)
klibc::memcpy(out,path,klibc::strlen(path) + 1);
if(e == -EINVAL)
klibc::memcpy(out,path,klibc::strlen(path) + 1);
else if(e == 0) {
klibc::memcpy(out,path,klibc::strlen(path) + 1);
} else if(e == -ENOENT) {
klibc::memcpy(out,path,klibc::strlen(path) + 1);
// maybe it wants directory symlink ?
char path0[4096];
klibc::memcpy(path0,path,klibc::strlen(path) + 1);
char result[4096];
int c = 0;
char* token = klibc::strtok(&next, path0, "/");
/* Start building path from null and trying to find symlink */
while(token) {
result[c] = '/';
c++;
std::uint64_t mm = 0;
mm = klibc::strlen(token);
klibc::memcpy((char*)((std::uint64_t)result + c),token,mm);
c += mm;
result[c] = '\0';
e = vfs::readlink(result,buffer,4096);
if(e == 0) {
char buffer2[4096];
vfs::resolve_path(buffer,result,buffer2,0);
c = klibc::strlen(buffer2);
buffer2[c++] = '/';
klibc::memcpy(buffer2 + c, path + klibc::strlen(result) + 1, klibc::strlen(path) - klibc::strlen(result));
__vfs_symlink_resolve_no_at_symlink_follow(buffer2,out);
return;
} else if(e == -ENOENT) {
klibc::memcpy(out,path,klibc::strlen(path) + 1);
}
token = klibc::strtok(&next,0,"/");
}
klibc::memcpy(out,path,klibc::strlen(path) + 1);
}
}
void __vfs_symlink_resolve(char* path, char* out, int level) {
char *next = nullptr;
char buffer[4096];
if(level == 25) {
// hell no :skull:
klibc::memcpy(out,path,klibc::strlen(path) + 1);
return;
}
int e = vfs::readlink(path,buffer,4096);
if(e == -ENOSYS)
klibc::memcpy(out,path,klibc::strlen(path) + 1);
if(e == -EINVAL)
klibc::memcpy(out,path,klibc::strlen(path) + 1);
else if(e == 0) {
char result[4096];
vfs::resolve_path(buffer,path,result,0);
__vfs_symlink_resolve(result,out, level + 1);
} else if(e == -ENOENT) {
klibc::memcpy(out,path,klibc::strlen(path) + 1);
// maybe it wants directory symlink ?
char path0[4096];
klibc::memcpy(path0,path,klibc::strlen(path) + 1);
char result[4096];
int c = 0;
char* token = klibc::strtok(&next, path0, "/");
/* Start building path from null and trying to find symlink */
while(token) {
result[c] = '/';
c++;
std::uint64_t mm = 0;
mm = klibc::strlen(token);
klibc:: memcpy((char*)((std::uint64_t)result + c),token,mm);
c += mm;
result[c] = '\0';
e = vfs::readlink(result,buffer,4096);
if(e == 0) {
char buffer2[4096];
vfs::resolve_path(buffer,result,buffer2,0);
c = klibc::strlen(buffer2);
buffer2[c++] = '/';
klibc::memcpy(buffer2 + c, path + klibc::strlen(result) + 1, klibc::strlen(path) - klibc::strlen(result));
__vfs_symlink_resolve(buffer2,out, level + 1);
return;
} else if(e == -ENOENT) {
klibc::memcpy(out,path,klibc::strlen(path) + 1);
return;
} else {
klibc::memcpy(out,path,klibc::strlen(path) + 1);
return;
}
token = klibc::strtok(&next,0,"/");
}
klibc::memcpy(out,path,klibc::strlen(path) + 1);
}
}
vfs::node vfs_nodes[512];
vfs::node* find_node(char* path) {
int r = 0;
vfs::node* match;
for(int i = 0;i < 512;i++) {
if(!klibc::strncmp(path,vfs_nodes[i].path,klibc::strlen(vfs_nodes[i].path))) {
if(klibc::strlen(vfs_nodes[i].path) > r) {
match = &vfs_nodes[i];
}
}
if(!klibc::strncmp(path, vfs_nodes[i].path,klibc::strlen(vfs_nodes[i].path) - 1) && path[klibc::strlen(vfs_nodes[i].path) + 1] == '\0') {
return &vfs_nodes[i];
}
}
return match;
}
std::int32_t vfs::open(file_descriptor* fd, char* path, bool follow_symlinks, bool is_directory) {
char out[4096];
klibc::memset(out,0,4096);
node* node = find_node(out);
if(follow_symlinks) {
__vfs_symlink_resolve(path, out, 0);
} else {
__vfs_symlink_resolve_no_at_symlink_follow(path, out);
}
if(!node) {
return -ENOENT; }
char* fs_love_name = out + klibc::strlen(node->path) - 1;
if(!node->fs->open) { assert(0, "meow :3");
return -ENOSYS; }
if(fs_love_name[0] == '\0') {
fs_love_name[0] = '/';
fs_love_name[1] = '\0';
}
std::int32_t status = node->fs->open(node->fs, (void*)fd, fs_love_name, is_directory);
return status;
}
std::int32_t vfs::create(char* path, vfs_file_type type, std::uint32_t mode) {
char out[4096];
klibc::memset(out,0,4096);
node* node = find_node(out);
__vfs_symlink_resolve_no_at_symlink_follow(path, out);
if(!node) {
return -ENOENT; }
char* fs_love_name = out + klibc::strlen(node->path) - 1;
if(!node->fs->create) { assert(0, "meow :3");
return -ENOSYS; }
if(fs_love_name[0] == '\0') {
fs_love_name[0] = '/';
fs_love_name[1] = '\0';
}
std::int32_t status = node->fs->create(node->fs, fs_love_name, type, mode);
return status;
}
std::int32_t vfs::readlink(char* path, char* out, std::uint32_t out_len) {
assert(out_len == 4096, "stfu");
char outp[4096];
klibc::memset(outp,0,4096);
node* node = find_node(outp);
klibc::memcpy(outp, path, klibc::strlen(path) + 1);
if(!node) {
return -ENOENT; }
char* fs_love_name = outp + klibc::strlen(node->path) - 1;
if(!node->fs->readlink) { assert(0, "meow :3");
return -ENOSYS; }
if(fs_love_name[0] == '\0') {
fs_love_name[0] = '/';
fs_love_name[1] = '\0';
}
std::int32_t status = node->fs->readlink(node->fs, fs_love_name, out);
return status;
}
std::int32_t vfs::remove(char* path) {
char out[4096];
klibc::memset(out,0,4096);
node* node = find_node(out);
__vfs_symlink_resolve_no_at_symlink_follow(path, out);
if(!node) {
return -ENOENT; }
char* fs_love_name = out + klibc::strlen(node->path) - 1;
if(!node->fs->remove) { assert(0, "meow remove :3");
return -ENOSYS; }
if(fs_love_name[0] == '\0') {
fs_love_name[0] = '/';
fs_love_name[1] = '\0';
}
std::int32_t status = node->fs->remove(node->fs, fs_love_name);
return status;
}
// will init default environment
void vfs::init() {
klibc::memset(vfs_nodes, 0, sizeof(vfs_nodes));
tmpfs::init_default(&vfs_nodes[0]);
log("vfs", "vfs_nodes 0x%p",vfs_nodes);
}
|