-
Notifications
You must be signed in to change notification settings - Fork 1
/
yfs_client.cc
326 lines (288 loc) · 8.95 KB
/
yfs_client.cc
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "yfs_client.h"
#include "extent_client_cache.h"
#include "lock_client_cache.h"
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h> /* srand, rand */
#include <time.h>
#include <jsl_log.h>
#include <sstream>
#include <algorithm>
void yfs_client::acquireLock(inum i) {
jsl_log(JSL_DBG_ME, "yfs_client::acquiring lock %llu\n", i);
lock_protocol::status st = lc->acquire(i);
VERIFY(st == lock_protocol::OK);
jsl_log(JSL_DBG_ME, "yfs_client::acquired\n");
}
void yfs_client::releaseLock(inum i) {
jsl_log(JSL_DBG_ME, "yfs_client::releasing lock %llu\n", i);
lock_protocol::status st = lc->release(i);
VERIFY(st == lock_protocol::OK);
jsl_log(JSL_DBG_ME, "yfs_client::released\n");
}
yfs_client::yfs_client(std::string extent_dst, std::string lock_dst) {
ec = new extent_client_cache(extent_dst);
lock_release_user *lru = new my_lock_release_user(ec);
lc = new lock_client_cache(lock_dst, lru);
srand (time(NULL)); // TODO déjà fait dans fuse ??
}
yfs_client::status yfs_client::create(inum parent, const char *name,
inum &file_inum) {
jsl_log(JSL_DBG_ME, "yfs_client_create %s\n", name);
extent_protocol::status st1, st2;
std::vector<dirent> content;
st1 = read_dir(parent, content);
if (st1 != extent_protocol::OK) {
return IOERR;
}
for (dirent const &de : content) {
if (de.name == name) {
jsl_log(JSL_DBG_ME, "yfs_client_create file exists\n");
return EXIST;
}
}
file_inum = fresh_inum(false);
content.push_back(dirent(name, file_inum));
std::string new_dir = serialize_dir(content);
acquireLock(file_inum);
st1 = ec->put(file_inum, "");
releaseLock(file_inum);
st2 = ec->put(parent, new_dir);
if (st1 != extent_protocol::OK || st2 != extent_protocol::OK) {
return IOERR;
}
jsl_log(JSL_DBG_ME, "yfs_client_create file %016llx and added to parent dir\n", file_inum);
return OK;
}
yfs_client::status yfs_client::mkdir(inum parent, const char *name,
inum &dir_inum) {
jsl_log(JSL_DBG_ME, "yfs_client_mkdir %s\n", name);
std::vector<dirent> content;
extent_protocol::status st1, st2;
st1 = read_dir(parent, content);
if (st1 != extent_protocol::OK) {
return IOERR;
}
for (dirent const &de : content) {
if (de.name == name) {
jsl_log(JSL_DBG_ME, "yfs_client_mkdir dir exists\n");
return EXIST;
}
}
dir_inum = fresh_inum(true);
content.push_back(dirent(name, dir_inum));
std::string new_dir = serialize_dir(content);
acquireLock(dir_inum);
st1 = ec->put(dir_inum, "");
releaseLock(dir_inum);
st2 = ec->put(parent, new_dir);
if (st1 != extent_protocol::OK || st2 != extent_protocol::OK) {
return IOERR;
}
jsl_log(JSL_DBG_ME, "yfs_client_mkdir dir %016llx created and added to parent dir\n", dir_inum);
return OK;
}
// TODO utiliser lookup plutot que de refaire la recherche dans le dir à chaque fois
yfs_client::status yfs_client::unlink(inum parent, const char *name) {
jsl_log(JSL_DBG_ME, "yfs_client_unlink %s\n", name);
std::vector<dirent> content;
extent_protocol::status st1;
st1 = read_dir(parent, content);
if (st1 != extent_protocol::OK) {
return IOERR;
}
auto it = std::find_if(content.begin(), content.end(), [name] (dirent &s) { return s.name == name; } );
if (it == content.end()) {
jsl_log(JSL_DBG_ME, "yfs_client_unlink file doesn't exists\n");
return NOENT;
}
jsl_log(JSL_DBG_ME, "yfs_client_unlink removing file\n");
st1 = ec->remove(it->inum);
if (st1 != extent_protocol::OK) {
return IOERR;
}
content.erase(it);
std::string new_dir = serialize_dir(content);
st1 = ec->put(parent, new_dir);
if (st1 != extent_protocol::OK) {
return IOERR;
}
return OK;
}
yfs_client::status yfs_client::lookup(inum parent, const char *name,
inum &file_inum) {
jsl_log(JSL_DBG_ME, "yfs_client_lookup %s\n", name);
std::vector<dirent> content;
extent_protocol::status st1;
st1 = read_dir(parent, content);
if (st1 != extent_protocol::OK) {
return IOERR;
}
for (dirent const &de : content) {
if (de.name == name) {
file_inum = de.inum;
jsl_log(JSL_DBG_ME, "yfs_client_lookup found %s %llx\n", name, file_inum);
return OK;
}
}
jsl_log(JSL_DBG_ME, "yfs_client_lookup didn't found %s\n", name);
return NOENT;
}
yfs_client::status yfs_client::read_dir(inum parent, std::vector<dirent> &v) {
jsl_log(JSL_DBG_ME, "yfs_client_read_dir %016llx\n", parent);
VERIFY(isdir(parent));
std::string buf;
extent_protocol::status st = ec->get(parent, buf);
if (st != extent_protocol::OK) {
return IOERR;
}
jsl_log(JSL_DBG_ME, "yfs_client_read_dir %016llx buf = %s \n", parent, buf.c_str());
deserialize_dir(buf, v);
return OK;
}
yfs_client::status yfs_client::read(inum num, size_t size, off_t off,
std::string &buf) {
jsl_log(JSL_DBG_ME, "yfs_client_read %016llx size %lu off %lu\n", num, size, off);
std::string extent;
extent_protocol::status st = ec->get(num, extent);
if (st != extent_protocol::OK) {
return IOERR;
}
size_t extent_size = extent.size();
if ((size_t)off >= extent_size) { // TODO est-ce utile ? voir comportement par defaut de substr
buf = "";
return OK;
}
buf = extent.substr(off, size);
return OK;
}
yfs_client::status yfs_client::write(inum num, size_t size, off_t off,
const char *buf) {
jsl_log(JSL_DBG_ME, "yfs_client_write %016llx\n", num);
std::string extent;
if (size == 0) {
return OK;
}
extent_protocol::status st = ec->get(num, extent);
if (st != extent_protocol::OK) {
return IOERR;
}
size_t old_size = extent.size();
size_t new_size = std::max(old_size, off + size);
jsl_log(JSL_DBG_ME, "yfs_client_write %016llx - size %lu - old size %lu - new size - %lu - offset %lu\n", num, size, old_size, new_size, off);
extent.resize(new_size, 0);
VERIFY(extent.size() == new_size);
if ((size_t)off > old_size) {
jsl_log(JSL_DBG_ME, "yfs_client_write %016llx - there's a hole %lu\n", num, off - old_size);
}
// TODO replace this with a string method
for (size_t i = 0; i < size; i++){
extent[(size_t)(off + i)] = buf[i];
}
// update extent
st = ec->put(num, extent);
if (st != extent_protocol::OK) {
return IOERR;
}
return OK;
}
yfs_client::status yfs_client::resize(inum num, size_t size) {
jsl_log(JSL_DBG_ME, "yfs_client_resize %016llx\n", num);
std::string extent;
extent_protocol::status st1, st2;
st1 = ec->get(num, extent);
extent.resize(size, 0);
st2 = ec->put(num, extent);
if (st1 != extent_protocol::OK && st2 != extent_protocol::OK) {
return IOERR;
}
return OK;
}
yfs_client::status
yfs_client::getfile(inum inum, fileinfo &fin)
{
int r = OK;
// You modify this function for Lab 3
// - hold and release the file lock
jsl_log(JSL_DBG_ME, "yfs_client getfile %016llx\n", inum);
extent_protocol::attr a;
if (ec->getattr(inum, a) != extent_protocol::OK) {
r = IOERR;
goto release;
}
fin.atime = a.atime;
fin.mtime = a.mtime;
fin.ctime = a.ctime;
fin.size = a.size;
printf("getfile %016llx -> sz %llx\n", inum, fin.size);
release:
return r;
}
yfs_client::status
yfs_client::getdir(inum inum, dirinfo &din)
{
int r = OK;
// You modify this function for Lab 3
// - hold and release the directory lock
jsl_log(JSL_DBG_ME, "yfs_client getdir %016llx\n", inum);
extent_protocol::attr a;
if (ec->getattr(inum, a) != extent_protocol::OK) {
r = IOERR;
goto release;
}
din.atime = a.atime;
din.mtime = a.mtime;
din.ctime = a.ctime;
release:
return r;
}
///// Local methods, don't access the server, stateless
bool yfs_client::isfile(inum inum) {
return (inum & 0x80000000);
}
bool yfs_client::isdir(inum inum) {
return ! isfile(inum);
}
// TODO at some point use const & wherever possible
std::string yfs_client::serialize_dir(std::vector<dirent> dir) {
std::string res;
// TODO antipattern? make that more efficient
// TODO check that name can't contain "/"
for (auto const &de : dir) {
res += de.name;
res += "/";
res += filename(de.inum);
res += "/";
}
jsl_log(JSL_DBG_ME, "yfs_client_serialize %s\n", res.c_str());
return res;
}
void yfs_client::deserialize_dir(std::string s, std::vector<dirent> &r) {
std::stringstream ss(s);
std::string item1, item2;
while (std::getline(ss, item1, '/')) {
VERIFY(std::getline(ss, item2, '/'));
r.push_back(dirent(item1, n2i(item2)));
}
}
yfs_client::inum yfs_client::fresh_inum(bool is_dir) {
unsigned long int r = rand();
if (!is_dir) { r |= 0x80000000; }
return (inum) r;
}
yfs_client::inum yfs_client::n2i(std::string n) {
std::istringstream ist(n);
unsigned long long finum;
ist >> finum;
return finum;
}
std::string yfs_client::filename(inum inum) {
std::ostringstream ost;
ost << inum;
return ost.str();
}