-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht_functions.h
437 lines (391 loc) · 15.4 KB
/
dht_functions.h
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* File contains many print statements to show program progress */
// NEWPROGRAM3 WILL CORRESPOND TO DHT_FUNCTIONS
/* compile with: g++ newProgram2.cc -std=c++14 -lopendht -lgnutls
run with: ./a.out
*/
#include <unistd.h>
#include <stdio.h>
#include <random>
#include <opendht.h>
#include <sstream>
#include <fstream>
#include "functions.h"
#include "file.h"
#include "auxiliary_dht_functions.h"
/* Used for outputting to log */
std::ofstream output_log;
void dht_init_root() {
std::cout << "initializing root...\n\n";
output_log << "initializing root...\n\n";
file root_directory(1, ".", true, true);
dht_exclusive_put("/", "1", 1, 0);
dht_exclusive_put("1", root_directory.content, 1, 0);
}
void dht_create(std::string path_name) {
std::cout << "=== dht_create(" << path_name << ") ===\n";
output_log << "=== dht_create(" << path_name << ") ===\n";
if (!file_exists(path_name)) {
unsigned long long file_id = idGenerator();
std::string parent_uuid;
/* put new file into parent path */
parent_uuid = parent_path(path_name);
parent_uuid = existing_uuid_key(parent_uuid);
if (parent_uuid == "NONE") {
std::cout << "ERROR3: create: cannot create file '" << path_name << "': No such file or directory\n";
std::cout << "=== dht_create complete ===\n\n";
output_log << "ERROR3: create: cannot create file '" << path_name << "': No such file or directory\n";
output_log << "=== dht_create complete ===\n\n";
return;
}
/* when putting to parent, content is file name, e.g. hello.txt */
file new_file(file_id, file_name(path_name), true, false);
if (dht_exclusive_put(parent_uuid, parseOutgoingDHT(new_file), 0, 1)) {
std::cout << "Linking '" << path_name << "' to parent...\n";
output_log << "Linking '" << path_name << "' to parent...\n";
/* Create the file on dht */
std::cout << "Creating '" << path_name << "' on dht...\n";
output_log << "Creating '" << path_name << "' on dht with id: " << file_id << std::endl;
/* put <path, id> */
dht_exclusive_put(path_name, std::to_string(file_id), 0, 0);
/* put <id, <id, "", 1, 0>> */
new_file.content = "";
dht_exclusive_put(std::to_string(file_id), parseOutgoingDHT(new_file), 0, 1);
}
else {
std::cout << "ERROR2: create: cannot create file '" << path_name << "': File exists\n";
output_log << "ERROR2: create: cannot create file '" << path_name << "': File exists\n";
}
}
else {
std::cout << "ERROR1: create: cannot create file '" << path_name << "': File exists\n";
output_log << "ERROR1: create: cannot create file '" << path_name << "'File exists\n";
}
std::cout << "=== dht_create complete ===\n\n";
output_log << "=== dht_create complete ===\n\n";
}
void dht_mkdir(std::string path_name) {
std::cout << "=== dht_mkdir(" << path_name << ") ===\n";
output_log << "=== dht_mkdir(" << path_name << ") ===\n";
if (!file_exists(path_name)) {
unsigned long long file_id = idGenerator();
std::string parent_uuid;
/* put new directory into parent path */
parent_uuid = parent_path(path_name);
parent_uuid = existing_uuid_key(parent_uuid);
if (parent_uuid == "NONE") {
std::cout << "ERROR2: mkdir: cannot create directory '" << path_name << "': No such file or directory\n";
std::cout << "=== dht_mkdir complete ===\n\n";
output_log << "ERROR2: mkdir: cannot create directory'" << path_name << "': No such file or directory\n";
output_log << "=== dht_mkdir complete ===\n\n";
return;
}
file new_directory(file_id, file_name(path_name), true, true);
if (dht_exclusive_put(parent_uuid, parseOutgoingDHT(new_directory), 1, 1)) {
std::cout << "Linking '" << path_name << "' to parent...\n";
output_log << "Linking '" << path_name << "' to parent...\n";
/* Create the directory on dht */
std::cout << "Creating '" << path_name << "' on dht...\n";
output_log << "Creating '" << path_name << "' on dht with id: " << file_id << std::endl;
/* On directory itself, content is '.' */
//new_directory.content = ".";
/* put <path, id> */
dht_exclusive_put(path_name, std::to_string(file_id), 1, 0);
/* put <id, <id,., 1, 1>> */
dht_exclusive_put(std::to_string(file_id), ".", 1, 0);
}
}
else {
std::cout << "ERROR1: mkdir: cannot create directory '" << path_name << "': Directory exists\n";
output_log << "ERROR1: mkdir: cannot create directory '" << path_name << "': Directory exists\n";
}
std::cout << "=== dht_mkdir complete ===\n\n";
output_log << "=== dht_mkdir complete ===\n\n";
}
/* remove file at path_name - put the same thing with isExist = 0 */
void dht_remove(std::string path_name) {
std::cout << "=== dht_remove(" << path_name << ") ===\n";
output_log << "=== dht_remove(" << path_name << ") ===\n";
if (file_exists(path_name)) {
/* set isExists = 0 in parent directory */
std::string path_uuid;
/* string to hold parent uuid */
std::string parent_uuid;
/* vector that stores all file strings */
std::vector<std::string> fileStringsVec;
/* vector that stores all files */
std::vector<file> fileVec;
if (is_root(path_name)) {
std::cout << "ERROR2: rm cannot remove '/': directory is the root directory\n";
output_log << "ERROR2: rm cannot remove '/': directory is the root directory\n";
return;
}
path_uuid = existing_uuid_key(path_name);
parent_uuid = existing_uuid_key(parent_path(path_name));
/* Get all files in parent */
node.get(parent_uuid, [&fileStringsVec](const std::vector<std::shared_ptr<dht::Value>>& values) {
for (const auto& value : values) {
std::string uuid2file = asciify((*value).data);
fileStringsVec.push_back(uuid2file);
}
return true;
});
sleep(1);
for (int i = 0; i < fileStringsVec.size(); i++) {
fileVec.push_back(parseIncomingDHT(fileStringsVec[i]));
}
for (int i = 0; i < fileVec.size(); i++) {
if (fileVec[i].id == stoull(path_uuid)) {
file remove_file(fileVec[i].id, fileVec[i].content, 0, fileVec[i].isDirectory);
/* put (labeled) deleted directory to parent */
node.put(parent_uuid, parseOutgoingDHT(remove_file));
}
}
/* set (add) uuid of path_name to deleted, i.e. uuid + 'D' */
output_log << "REMOVING ID: " << path_uuid << std::endl;
node.put(path_name, path_uuid + 'D');
sleep(1);
}
else {
std::cout << "ERROR1: rm: cannot remove '" << path_name << "': is not a file or directory\n";
output_log << "ERROR1: rm: cannot remove '" << path_name << "': is not a file or directory\n";
}
std::cout << "=== dht_remove complete ===\n\n";
output_log << "=== dht_remove complete ===\n\n";
}
/* renames path_from to path_to */
void dht_rename(std::string path_from, std::string path_to) {
std::cout << "=== dht_rename(" << path_from << ") ===\n";
output_log << "=== dht_rename(" << path_from << ") ===\n";
if (file_exists(path_from)) {
if (!file_exists(path_to)) {
if(is_directory(path_from)) {
/* TODO renaming a directory */
std::cout << "ERROR3: rename: renaming directory feature not yet supported\n";
output_log << "ERROR3: rename: renaming directory feature not yet supported\n";
}
else {
/* renaming a file */
std::cout << "renaming FILE '" << path_from << "' to '" << path_to << "'...\n\n";
output_log << "renaming FILE '" << path_from << "' to '" << path_to << "'...\n\n";
std::string path_from_uuid = existing_uuid_key(path_from);
std::string parent_uuid = existing_uuid_key(parent_path(path_from));
/* file with format to push it to parent */
file child_file(std::stoull(path_from_uuid), file_name(path_to), 1, 0);
/* remove <path_from, uuid> entry and <parent_from, <...>> */
dht_remove(path_from);
/* put <path_to, uuid of path_from> */
node.put(path_to, path_from_uuid);
/* put name of new file in parent */
node.put(parent_uuid, parseOutgoingDHT(child_file));
//TODO xcp maybe?
//dht_exclusive_put(path_to, path_uuid, 0, 0);
//dht_exclusive_put(parent_uuid, file_name(path_to), 0, 0);
}
}
else {
/* dst file already exists */
std::cout << "ERROR2: rename: cannot rename '" << path_to << "': already exists\n";
output_log << "ERROR2: rename: cannot rename '" << path_to << "': already exists\n";
}
}
else {
/* src file does not exist */
std::cout << "ERROR1: rename: cannot rename '" << path_from << "': is not a file or directory\n";
output_log << "ERROR1: rename: cannot rename '" << path_from << "': is not a file or directory\n";
}
std::cout << "=== dht_rename complete ===\n\n";
output_log << "=== dht_rename complete ===\n\n";
}
/* return the contents of a file (path_name) */
std::string dht_read(std::string path_name) {
std::cout << "=== dht_read(" << path_name << ") ===\n";
output_log << "=== dht_read(" << path_name << ") ===\n";
if (file_exists(path_name)) {
if(!is_directory(path_name)) {
/* string to hold uuid */
std::string path_uuid;
/* vector that stores all file strings */
std::vector<std::string> fileStringsVec;
/* vector that stores all files */
std::vector<file> fileVec;
/* vector that stores all (labeled) deleted files */
std::vector<file> deletedFilesVec;
/* vector that will filter out to only existingFilesVec */
std::vector<file> filterFilesVec;
file current_file;
path_uuid = existing_uuid_key(path_name);
node.get(path_uuid, [&fileStringsVec](const std::vector<std::shared_ptr<dht::Value>>& values) {
for (const auto& value : values) {
std::string uuid2file = asciify((*value).data);
fileStringsVec.push_back(uuid2file);
}
return true;
});
sleep(1);
for (int i = 0; i < fileStringsVec.size(); i++) {
fileVec.push_back(parseIncomingDHT(fileStringsVec[i]));
}
for (int i = 0; i < fileVec.size(); i++) {
/* populate vector of deleted files */
if (fileVec[i].isExists == false) {
deletedFilesVec.push_back(fileVec[i]);
}
else {
filterFilesVec.push_back(fileVec[i]);
}
}
/* find the file that has not been deleted */
for (int i = 0; i < filterFilesVec.size(); i++) {
bool fileExists = true;
for (int j = 0; j < deletedFilesVec.size(); j++) {
if (filterFilesVec[i] == deletedFilesVec[j]) {
fileExists = false;
}
}
if (fileExists) {
current_file = filterFilesVec[i];
break;
}
}
std::cout << "=== dht_read complete ===\n\n";
output_log << "=== dht_read complete ===\n\n";
return current_file.content;
}
else {
std::cout << "ERROR2: read: cannot read file '" << path_name << "'file is a directory\n";
output_log << "ERROR2: read: cannot read file '" << path_name << "'file is a directory\n";
std::string emptyString;
std::cout << "=== dht_read complete ===\n\n";
output_log << "=== dht_read complete ===\n\n";
return emptyString;
}}
else {
std::cout << "ERROR1: read: cannot read file '" << path_name << "'file does not exist\n";
output_log << "ERROR1: read: cannot read file '" << path_name << "'file does not exist\n";
std::string emptyString;
std::cout << "=== dht_read complete ===\n\n";
output_log << "=== dht_read complete ===\n\n";
return emptyString;
}
}
/* append content to a file */
void dht_write(std::string path_name, std::string content) {
std::cout << "=== dht_write(" << path_name << ") ===\n";
output_log << "=== dht_write(" << path_name << ") ===\n";
if (file_exists(path_name)) {
if (!is_directory(path_name)) {
/* string that holds the uuid of the file */
std::string path_uuid;
/* file in string format */
std::string fileString;
file current_file;
file delete_file;
std::cout << "Writing '"<< content << "' to " << file_name(path_name) << "...\n";
output_log << "Writing '" << content << "' to " << file_name(path_name) << "...\n";
//TODO deleted values
path_uuid = existing_uuid_key(path_name);
node.get(path_uuid, [&fileString](const std::vector<std::shared_ptr<dht::Value>>& values) {
for (const auto& value : values) {
//should only be one value
fileString = asciify((*value).data);
}
return true;
});
sleep(1);
delete_file = parseIncomingDHT(fileString);
current_file = parseIncomingDHT(fileString);
/* delete the old file */
delete_file.isExists = false;
node.put(path_uuid, parseOutgoingDHT(delete_file));
/* append the new content */
current_file.content = current_file.content + content;
/* put the new file */
node.put(path_uuid, parseOutgoingDHT(current_file));
}
else {
std::cout << "ERROR2: write: cannot write to '" << path_name << "file is a directory\n";
output_log << "ERROR2: write: cannot write to '" << path_name << "file is a directory\n";
}
}
else {
std::cout << "ERROR1: write: cannot write to '" << path_name << "file does not exist\n";
output_log << "ERROR1: write: cannot write to '" << path_name << "file does not exist\n";
}
std::cout << "=== dht_write complete ===\n\n";
output_log << "=== dht_write complete ===\n\n";
}
std::vector<std::string> dht_readdir(std::string path_name) {
std::cout << "=== dht_readdir(" << path_name << ")===\n";
output_log << "=== dht_readdir(" << path_name << ")===\n";
if (is_directory(path_name)) {
/* string that holds the uuid of the file */
std::string fileUuid;
/* vector that stores all file strings */
std::vector<std::string> fileStringsVec;
/* vector that stores all files */
std::vector<file> fileVec;
/* vector that stores all (labeled) deleted files */
std::vector<file> deletedFilesVec;
/* vector that will filter out to only existingFilesVec */
std::vector<file> filterFilesVec;
/* vector that contains the existing files */
std::vector<file> existingFilesVec;
/* vector that contains names of all files */
std::vector<std::string> fileNamesVec;
fileUuid = existing_uuid_key(path_name);
/* With uuid, get all files in directory */
node.get(fileUuid, [&fileStringsVec](const std::vector<std::shared_ptr<dht::Value>>& values) {
for (const auto& value : values) {
std::string uuid2file = asciify((*value).data);
fileStringsVec.push_back(uuid2file);
}
return true;
});
sleep(1);
for (int i = 0; i < fileStringsVec.size(); i++) {
fileVec.push_back(parseIncomingDHT(fileStringsVec[i]));
}
for (int i = 0; i < fileVec.size(); i++) {
/* populate vector of deleted files */
if (fileVec[i].isExists == false) {
deletedFilesVec.push_back(fileVec[i]);
}
else {
filterFilesVec.push_back(fileVec[i]);
}
}
/* remove files that have been (labeled as) deleted */
for (int i = 0; i < filterFilesVec.size(); i++) {
bool fileExists = true;
for (int j = 0; j < deletedFilesVec.size(); j++) {
if (filterFilesVec[i] == deletedFilesVec[j]) {
fileExists = false;
}
}
if (fileExists) {
existingFilesVec.push_back(filterFilesVec[i]);
}
}
/* extract file names */
for (int i = 0; i < existingFilesVec.size(); i++) {
fileNamesVec.push_back(existingFilesVec[i].content);
}
/* print file names */
output_log << "CONTENTS OF " << path_name << ":\n";
for (int i = 0; i < fileNamesVec.size(); i++) {
output_log << fileNamesVec[i] << " ";
}
output_log << std::endl;
std::cout << "=== dht_readdir complete ===\n\n";
output_log << "=== dht_readdir complete ===\n\n";
return fileNamesVec;
}
else {
std::cout << "ERROR1: readdir: cannot readdir '" << path_name << "': is not a directory\n";
output_log << "ERROR1: readdir: cannot readdir '" << path_name << "': is not a directory\n";
std::vector<std::string> emptyVec;
std::cout << "=== dht_readdir complete ===\n\n";
output_log << "=== dht_readdir complete ===\n\n";
return emptyVec;
}
}