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
|
#include <hilbert/kernel/vfile.hpp>
//TODO: handle symlink loops nicely in vfile::get_child,
// vfile::get_children, and lookup_path.
namespace hilbert::kernel::vfile {
void canon_path::parent() {
if (segments.count != 0)
--segments.count;
else if (!absolute)
++parent_count;
}
void canon_path::rel(const canon_path &r) {
if (r.absolute) {
segments.count = 0;
absolute = true;
parent_count = 0;
}
for (unsigned i = 0; i < r.parent_count; ++i)
parent();
for (unsigned i = 0; i < r.segments.count; ++i)
segments.add_end(r.segments.buffer[i]);
}
void canonize_path(const utility::string &name, canon_path &out) {
out.absolute = false;
out.parent_count = 0;
out.segments.count = 0;
const char *str = name.buffer;
unsigned len = name.count;
if (len == 0)
return;
if (len == 1 && str[0] == '/') {
out.absolute = true;
return;
}
if (str[0] == '/') {
out.absolute = true;
++str;
--len;
}
while (len != 0) {
unsigned segment_len = utility::find(str, len, '/');
unsigned to_skip = segment_len == len ? segment_len : segment_len + 1;
if (segment_len == 0)
;
else if (segment_len == 1 && str[0] == '.')
;
else if (segment_len == 2 && str[0] == '.' && str[1] == '.')
out.parent();
else {
utility::string segment(str, segment_len);
out.segments.add_end(utility::move(segment));
}
str += to_skip;
len -= to_skip;
}
}
#define RET_NOT_SUC(expr) \
{ \
storage::fs_result _result = expr; \
if (_result != storage::fs_result::success) \
return _result; \
}
storage::fs_result vfile::follow_symlinks(vfile &out) const {
if (dir_entry.type != storage::file_type::symlink) {
out = *this;
return storage::fs_result::success;
}
canon_path target_path;
canonize_path(dir_entry.target, target_path);
canon_path full_path = path;
full_path.parent();
full_path.rel(target_path);
vfile next;
RET_NOT_SUC(lookup_path(full_path, next, false))
next.path = path;
return next.follow_symlinks(out);
}
storage::fs_result vfile::get_child(
vfile &out, const utility::string &name
) const {
storage::dir_entry entry;
storage::directory_iter_t iter;
RET_NOT_SUC(bd->mounted_as->get_first_child(dir_entry.node, entry, iter))
while (true) {
if (entry.name == name) {
vfile vf;
vf.bd = bd;
vf.dir_entry = utility::move(entry);
vf.path = path;
vf.path.segments.add_end(name);
out = utility::move(vf);
return storage::fs_result::success;
}
RET_NOT_SUC(bd->mounted_as->get_next_child(dir_entry.node, entry, iter))
}
}
storage::fs_result vfile::get_children(utility::vector<vfile> &out) const {
storage::dir_entry entry;
storage::directory_iter_t iter;
storage::fs_result result =
bd->mounted_as->get_first_child(dir_entry.node, entry, iter);
if (result == storage::fs_result::does_not_exist)
return storage::fs_result::success;
else if (result != storage::fs_result::success)
return result;
while (true) {
vfile vf;
vf.bd = bd;
vf.path = path;
vf.path.segments.add_end(entry.name);
vf.dir_entry = utility::move(entry);
out.add_end(utility::move(vf));
result = bd->mounted_as->get_next_child(dir_entry.node, entry, iter);
if (result == storage::fs_result::does_not_exist)
return storage::fs_result::success;
else if (result != storage::fs_result::success)
return result;
}
}
storage::fs_result vfile::read_file(
uint64_t start, uint64_t length, void *into
) const {
return bd->mounted_as->read_bytes_from_file(
dir_entry.node, start, length, into);
}
//TODO: see comment at top of vfile.hpp.
static const vfile *root;
void set_root(const vfile &root) {
kernel::vfile::root = new vfile(root);
}
storage::fs_result lookup_path(
const canon_path &path, vfile &out, bool follow_final_symlink
) {
//assume path is absolute.
out = *root;
for (unsigned i = 0; i < path.segments.count; ++i) {
vfile result;
RET_NOT_SUC(out.follow_symlinks(result))
out = utility::move(result);
RET_NOT_SUC(out.get_child(result, path.segments.buffer[i]))
out = utility::move(result);
}
if (follow_final_symlink) {
vfile result;
RET_NOT_SUC(out.follow_symlinks(result))
out = utility::move(result);
}
return storage::fs_result::success;
}
}
|