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
|
#include <mercury/kernel/storage.hpp>
#define RETURN_IF_NOT_SUCCESS(expr) \
{ \
io_result _result = expr; \
if (_result != io_result::success) \
return _result; \
}
namespace mercury::kernel::storage {
io_result block_device::load_cache_block(uint64_t i) {
if (block_cache_i == i)
return io_result::success;
if (block_cache_dirty) {
RETURN_IF_NOT_SUCCESS(
write_blocks_no_cache(block_cache_i, 1, block_cache))
block_cache_dirty = false;
}
io_result result = read_blocks_no_cache(i, 1, block_cache);
if (result != io_result::success) {
block_cache_i = block_count;
return result;
}
block_cache_i = i;
return io_result::success;
}
io_result block_device::read_bytes(
uint64_t start, uint64_t count, void *into
) {
if (start + count > block_size * block_count)
return io_result::out_of_bounds;
uint8_t *into_u8 = (uint8_t *)into;
if (start % block_size != 0) {
uint64_t prefix_len = block_size - start % block_size;
RETURN_IF_NOT_SUCCESS(load_cache_block(start / block_size))
for (uint64_t i = 0; i < prefix_len; ++i)
into_u8[i] = block_cache[start % block_size + i];
into_u8 += prefix_len;
start += prefix_len;
count -= prefix_len;
}
uint64_t postfix_start = ((start + count) / block_size) * block_size;
if (postfix_start != start) {
RETURN_IF_NOT_SUCCESS(read_blocks_no_cache(
start / block_size, (postfix_start - start) / block_size, into_u8))
count -= postfix_start - start;
into_u8 += postfix_start - start;
start = postfix_start;
}
if (count != 0) {
RETURN_IF_NOT_SUCCESS(load_cache_block(start / block_size))
for (uint64_t i = 0; i < count; ++i)
into_u8[i] = block_cache[i];
}
return io_result::success;
}
utility::list<block_device *> *block_devices;
static utility::trie<utility::string, block_device *> *mounted_devices;
void init_storage() {
block_devices = new utility::list<block_device *>();
mounted_devices = new utility::trie<utility::string, block_device *>();
}
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 char *str, size_t len, canon_path &out) {
out.absolute = false;
out.parent_count = 0;
out.segments.count = 0;
if (len == 0)
return;
if (len == 1 && str[0] == '/') {
out.absolute = true;
return;
}
if (str[0] == '/') {
out.absolute = true;
++str;
--len;
}
while (len != 0) {
size_t segment_len = utility::find(str, len, '/');
size_t 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(std::move(segment));
}
str += to_skip;
len -= to_skip;
}
}
io_result mount_device(
block_device *bd, const canon_path &path, file_system_mounter mounter
) {
if (!path.absolute)
return io_result::bad_path;
if (mounted_devices->has_key(path.segments))
return io_result::mount_point_already_used;
file_system_instance *fs;
RETURN_IF_NOT_SUCCESS(mounter(bd, fs));
bd->mounted_as = fs;
mounted_devices->try_insert(path.segments, bd);
return io_result::success;
}
static io_result symlink_contents(
block_device *bd, node_id_t node, canon_path &out
) {
//TODO
while (1)
;
}
static io_result resolve_symlinks(
block_device *&bd, node_id_t &node, canon_path &path
) {
file_type ft;
RETURN_IF_NOT_SUCCESS(bd->mounted_as->get_file_type(node, ft))
if (ft != file_type::symlink)
return io_result::success;
canon_path contents;
RETURN_IF_NOT_SUCCESS(symlink_contents(bd, node, contents))
path.parent();
path.rel(contents);
return look_up_absolute_path(path, bd, node, true, path);
}
io_result look_up_absolute_path(const canon_path &path,
block_device *&bd_out, node_id_t &node_out, bool resolve_final_node,
canon_path &path_without_symlinks_out
) {
if (!path.absolute)
return io_result::bad_path;
unsigned prefix_length;
bd_out =
*mounted_devices->longest_prefix_with_value(path.segments, prefix_length)
.value_here;
RETURN_IF_NOT_SUCCESS(bd_out->mounted_as->get_root_node(node_out))
path_without_symlinks_out.absolute = true;
path_without_symlinks_out.parent_count = 0;
path_without_symlinks_out.segments.count = 0;
for (unsigned i = 0; i < prefix_length; ++i)
path_without_symlinks_out.segments.add_end(path.segments.buffer[i]);
if (path.segments.count == 0)
goto on_final_node;
for (unsigned i = prefix_length; i < path.segments.count - 1; ++i) {
path_without_symlinks_out.segments.add_end(path.segments.buffer[i]);
RETURN_IF_NOT_SUCCESS(bd_out->mounted_as->get_child(node_out, node_out,
path.segments.buffer[i].buffer, path.segments.buffer[i].count))
RETURN_IF_NOT_SUCCESS(resolve_symlinks(
bd_out, node_out, path_without_symlinks_out))
file_type ft;
RETURN_IF_NOT_SUCCESS(bd_out->mounted_as->get_file_type(node_out, ft))
if (ft != file_type::directory)
return io_result::not_a_directory;
}
{
//in a block so that compiler sees last_segment
//isn't needed after this and allows the goto above.
const utility::string &last_segment =
path.segments.buffer[path.segments.count - 1];
path_without_symlinks_out.segments.add_end(last_segment);
RETURN_IF_NOT_SUCCESS(bd_out->mounted_as->get_child(
node_out, node_out, last_segment.buffer, last_segment.count))
}
on_final_node:
if (resolve_final_node)
RETURN_IF_NOT_SUCCESS(resolve_symlinks(
bd_out, node_out, path_without_symlinks_out))
return io_result::success;
}
}
|