-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyfs_client.cc
318 lines (281 loc) · 7.34 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
// yfs client. implements FS operations using extent and lock server
#include "yfs_client.h"
#include "extent_client.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>
yfs_client::yfs_client(std::string extent_dst, std::string lock_dst) :
ec(new extent_client(extent_dst)), lc(new lock_client_cache(lock_dst, new lock_release_user(ec))) {
srandom(getpid());
inum myinum = 0x00000001;
lc->acquire(myinum);
std::string s;
if (ec->get(myinum,s) == extent_protocol::NOENT) {
if (ec->put(myinum, "") != extent_protocol::OK) {
fprintf(stderr, "failed to create root\n");
lc->release(myinum);
exit(1);
}
}
lc->release(myinum);
}
yfs_client::~yfs_client() {
delete ec;
delete lc;
}
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();
}
bool yfs_client::isfile(inum inum) {
if (inum & 0x80000000) return true;
return false;
}
bool yfs_client::isdir(inum inum) {
return !isfile(inum);
}
int yfs_client::getfileatt(inum inum, fileinfo &fin) {
int r = OK;
printf("getfile %016llx\n", inum);
extent_protocol::attr a;
lc->acquire(inum);
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 %llu\n", inum, fin.size);
release: lc->release(inum);
return r;
}
int yfs_client::getdiratt(inum inum, dirinfo &din) {
int r = OK;
printf("getdir %016llx\n", inum);
extent_protocol::attr a;
lc->acquire(inum);
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: lc->release(inum);
return r;
}
int yfs_client::yfs_client::find_in_dir(inum myinum, std::string name,
inum& found) {
lc->acquire(myinum);
int r = find_in_dir_helper(myinum, name, found);
lc->release(myinum);
return r;
}
int yfs_client::yfs_client::find_in_dir_helper(inum myinum, std::string name,
inum& found) {
std::string buf;
if (ec->get(myinum, buf) != extent_protocol::OK) {
return IOERR;
}
std::stringstream ss(buf);
std::string record;
while (getline(ss, record, '|')) {
size_t dollar = record.find('$');
std::string id(record.begin(), record.begin() + dollar);
std::string file(record.begin() + dollar + 1, record.end());
std::stringstream getid(id);
inum item;
getid >> item;
if (name.compare(file) == 0) {
found = item;
return OK;
}
}
return NOENT;
}
int yfs_client::yfs_client::remove_from_dir(inum myinum, std::string name) {
lc->acquire(myinum);
std::string buf;
if (ec->get(myinum, buf) != extent_protocol::OK) {
lc->release(myinum);
return IOERR;
}
int r = NOENT;
std::stringstream ss(buf);
std::string record;
std::string newbuf;
inum found;
while (getline(ss, record, '|')) {
size_t dollar = record.find('$');
std::string id(record.begin(), record.begin() + dollar);
std::string file(record.begin() + dollar + 1, record.end());
std::stringstream getid(id);
inum item;
getid >> item;
if (name.compare(file) != 0) {
newbuf.append(record);
newbuf.append("|");
} else {
found = item;
r = OK;
}
}
if (r == OK) {
lc->acquire(found);
if (ec->put(myinum, newbuf) != extent_protocol::OK) {
lc->release(myinum);
lc->release(found);
return IOERR;
}
if (ec->remove(found) != extent_protocol::OK) {
lc->release(myinum);
lc->release(found);
return IOERR;
}
lc->release(found);
}
lc->release(myinum);
return r;
}
int yfs_client::put_file_in_dir(inum parentinum, std::string file,
inum& newinum) {
lc->acquire(parentinum);
std::string buf;
if (ec->get(parentinum, buf) != extent_protocol::OK) {
lc->release(parentinum);
return IOERR;
}
inum found;
int r = find_in_dir_helper(parentinum, file, found);
if (r != NOENT) {
lc->release(parentinum);
return EXIST;
}
newinum = random();
newinum = newinum | 0x80000000;
lc->acquire(newinum);
r = put_in_dir(parentinum, newinum, file, buf);
lc->release(newinum);
lc->release(parentinum);
return r;
}
int yfs_client::put_dir_in_dir(inum parentinum, std::string file, inum& newinum) {
lc->acquire(parentinum);
std::string buf;
if (ec->get(parentinum, buf) != extent_protocol::OK) {
lc->release(parentinum);
return IOERR;
}
inum found;
int r = find_in_dir_helper(parentinum, file, found);
if (r != NOENT) {
lc->release(parentinum);
return EXIST;
}
newinum = random();
newinum = newinum & ~(1 << 31);
lc->acquire(newinum);
r = put_in_dir(parentinum, newinum, file, buf);
lc->release(newinum);
lc->release(parentinum);
return r;
}
int yfs_client::put_in_dir(inum parentinum, inum fileinum, std::string file,
std::string buf) {
std::stringstream ss;
ss << fileinum;
ss << "$";
ss << file;
ss << "|";
buf.append(ss.str());
if (ec->put(parentinum, buf) != extent_protocol::OK) {
return IOERR;
}
if (ec->put(fileinum, "") != extent_protocol::OK) {
return IOERR;
}
return OK;
}
int yfs_client::readdir(inum parentinum,
std::vector<yfs_client::dirent>& filelist) {
std::string buf;
lc->acquire(parentinum);
if (ec->get(parentinum, buf) != extent_protocol::OK) {
lc->release(parentinum);
return IOERR;
}
std::stringstream ss(buf);
std::string record;
while (getline(ss, record, '|')) {
size_t found = record.find('$');
std::string id(record.begin(), record.begin() + found);
std::string file(record.begin() + found + 1, record.end());
std::stringstream getid(id);
inum item;
getid >> item;
dirent d;
d.name = file;
d.inum = item;
filelist.push_back(d);
}
lc->release(parentinum);
return OK;
}
int yfs_client::write_file_content(inum myinum, const char * mybuf,
size_t size, off_t off) {
lc->acquire(myinum);
std::string oldbuf;
extent_protocol::status r = ec->get(myinum, oldbuf);
if (r != extent_protocol::OK) {
lc->release(myinum);
return NOENT;
}
if (off + size > oldbuf.size()) {
oldbuf.resize(off + size);
}
oldbuf.replace(off, size, mybuf, size);
r = ec->put(myinum, oldbuf);
if (r != extent_protocol::OK) {
lc->release(myinum);
return IOERR;
}
lc->release(myinum);
return OK;
}
int yfs_client::set_file_size(inum myinum, off_t size) {
lc->acquire(myinum);
std::string oldbuf;
extent_protocol::status r = ec->get(myinum, oldbuf);
if (r != extent_protocol::OK) {
lc->release(myinum);
return NOENT;
}
oldbuf.resize(size);
r = ec->put(myinum, oldbuf);
if (r != extent_protocol::OK) {
lc->release(myinum);
return IOERR;
}
lc->release(myinum);
return OK;
}
int yfs_client::read_file_content(inum myinum, std::string& buf) {
lc->acquire(myinum);
extent_protocol::status r = ec->get(myinum, buf);
lc->release(myinum);
return r;
}