-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNFileTree.cpp
708 lines (572 loc) · 19.6 KB
/
NFileTree.cpp
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <fstream>
#include <cstring>
#include <cctype>
#include <unordered_map>
using namespace std;
const int chunkSize = 4096;
class TreeNode;
long unsigned int divide_chunks(string, TreeNode);
size_t gchunkcount=0;
std::string storePrefix = "chunk_";
std::unordered_map<int, int> chunkHashMap;
// Define a class to represent a node in the tree
class TreeNode {
public:
std::string fileName;
std::time_t timestampCreated;
size_t fileSize;
std::string fileType; // "file" or "folder"
std::vector<TreeNode *> children; // Pointers to children nodes
TreeNode *parent; // Pointer to parent node
long unsigned int chunk_inds;
int numChunks;
// File Constructor
TreeNode(std::string name, size_t size, std::string type,
TreeNode *parentNode)
: fileName(name), fileSize(size), fileType(type), parent(parentNode) {
timestampCreated = std::time(nullptr); // Set the current time as timestamp
}
//Constructor for reading tree from .data (use for fileType other than folders)
TreeNode(std::string name, time_t time, size_t size, std::string type,
TreeNode* parentNode, long unsigned int inds, int numberChunks){
this->fileName = name;
this->timestampCreated = time;
this->fileSize = size;
this->fileType = type;
this->parent = parentNode;
this->chunk_inds = inds;
this->numChunks = numberChunks;
}
//Constructor for reading ree from .data (used for fileType:"folder")
TreeNode(std::string name, time_t time, size_t size, std::string type,
TreeNode* parentNode, int numberChunks){
this->fileName = name;
this->timestampCreated = time;
this->fileSize = size;
this->fileType = type;
this->parent = parentNode;
this->numChunks = numberChunks;
}
//Folder Constructor
TreeNode(std::string name, std::string type,
TreeNode* parentNode)
: fileName(name), fileType(type), parent(parentNode){
timestampCreated = std::time(nullptr);
}
void printTreeNode(TreeNode* node){
std::cout << "Node Details: " << std::endl;
std::cout << "filename : " << node->fileName << std::endl;
std::cout << "filetype : " << node->fileType << std::endl;
if(node->fileType != "folder"){ std::cout << "filesize : " << node->fileSize << std::endl;}
std::cout << "timeStamp : " << node->timestampCreated << std::endl;
std::cout << "timeStamp : " << node->numChunks << std::endl;
}
// Function to add a child to this node
void addChild(TreeNode *child) { children.push_back(child); }
//Function to remove a child from this node, returns true if done successfully
bool removeChild(TreeNode* child){
for(int i=0;i<this->children.size();i++){
if(this->children[i]->fileName == child->fileName){
this->children.erase(this->children.begin()+i);
return true;
}
}
return false;
}
void serializeNode(std::ofstream& file){
file << this->fileName << '\n';
file << this->timestampCreated << '\n';
file << this->fileSize << '\n';
file << this->fileType << '\n';
if(this->parent == nullptr){
file << '\n';
}
else{
file << this->parent->fileName << '\n';
}
file << this->chunk_inds << '\n';
file << this->numChunks << '\n';
}
};
// A class for the tree structure itself
class FileTree {
private:
TreeNode *root;
TreeNode *searchHelper(TreeNode *currentNode, const std::string &name) {
if (currentNode == nullptr) {
return nullptr;
}
//std::cout<<"Inside1" << std::endl;
if (currentNode->fileName == name) {
return currentNode;
}
//std::cout<<"Inside2" << std::endl;
for (TreeNode *child : currentNode->children) {
TreeNode *result = searchHelper(child, name);
if (result != nullptr) {
return result;
}
}
return nullptr;
}
void deleteNode(TreeNode *node){
if (node == nullptr) {
return;
}
for (TreeNode* child : node->children) {
deleteNode(child); // Recursively delete children
}
node->parent->removeChild(node);
delete node;
}
void printTreeHelper(TreeNode *node, int level) {
if (node == nullptr) {
return;
}
// Print the current node with indentation based on its level
std::string indentation(level * 2, ' '); // 2 spaces per level of depth
std::cout << indentation << "- " << node->fileName << " (" << node->fileType
<< ")" << std::endl;
// Recursively print each child
for (TreeNode *child : node->children) {
printTreeHelper(child, level + 1);
}
}
public:
FileTree() : root(nullptr) {}
// Function to set the root of the tree
void setRoot(TreeNode *rootNode) { root = rootNode; }
TreeNode *searchByName(const std::string &name) {
return searchHelper(root, name);
}
void printTree() { printTreeHelper(root, 0); }
void moveFile(std::string fileName,std::string destinationName){
TreeNode* file = this->searchByName(fileName);
TreeNode* destination = this->searchByName(destinationName);
//Check if file and destination folder exist
if(file == nullptr){
std::cout<<"File does not exist!" << std::endl;
return;
}
else if(destination == nullptr || destination->fileType != "folder"){
std::cout<<"Folder does not exist!" << std::endl;
return;
}
//Update parent/children relationships
file->parent->removeChild(file);
destination->addChild(file);
file->parent = destination;
}
void copyFile(std::string fileName, std::string newFile, std::string destinationName){
TreeNode* file = this->searchByName(fileName);
TreeNode* newfile = this->searchByName(newFile);
TreeNode* destination = this->searchByName(destinationName);
if(newfile != nullptr){
std::cout<<"File name exists already! Use a different name" << std::endl;
return;
}
//Check if file and destination folder exist
if(file == nullptr){
std::cout<<"File does not exist!" << std::endl;
return;
}
else if(destination == nullptr || destination->fileType != "folder"){
std::cout<<"Folder does not exist!" << std::endl;
return;
}
TreeNode* newNode = new TreeNode(newFile, file->fileType, destination);
newNode->chunk_inds=file->chunk_inds;
newNode->numChunks=file->numChunks;
destination->addChild(newNode);
for(int i=file->chunk_inds; i<file->numChunks; i++){
chunkHashMap[i]++;
}
}
bool createFolder(std::string filename, std::string location){
TreeNode* folder = this->searchByName(filename);
TreeNode* parent_folder = this->searchByName(location);
if(folder != nullptr){
std::cout<<"Folder or file with name already exist!" << std::endl;
return false;
}
if(parent_folder == nullptr || parent_folder->fileType != "folder"){
std::cout<<"Enter valid parent folder path" << std::endl;
return false;
}
TreeNode* newNode = new TreeNode(filename, "folder", parent_folder);
parent_folder->addChild(newNode);
return true;
}
//Store file in tree by creating new node and calling divide_chunks
bool storeFile(std::string filename,std::string location){
std::istringstream iss(filename);
std::string token;
std::string fileExtension;
std::vector<std::string> substrings;
TreeNode* efile = this->searchByName(filename);
if(efile != nullptr){
std::cout<<"Folder or file with name already exist!" << std::endl;
return false;
}
TreeNode* folder = this->searchByName(location);
if(folder == nullptr || folder->fileType != "folder"){
std::cout<<"Invalid folder!" << std::endl;
return false;
}
// Split the string using dot as the delimiter
while (std::getline(iss, token, '.')) {
substrings.push_back(token);
}
// If there is at least one dot in the file name, the last element in the vector
// will contain the file extension (e.g., "txt")
if (substrings.size() > 1) {
fileExtension = substrings.back();
//std::cout << "File extension: " << fileExtension << std::endl;
} else {
fileExtension = "txt";
std::cout << "No file extension found." << std::endl;
}
TreeNode* file = new TreeNode(filename, fileExtension, folder);
folder->addChild(file);
splitFile(filename, file);
const char* cstr_filename = filename.c_str();
//std::remove(cstr_filename);
// std::cout << "file->TYPE : " << file->fileType << std::endl;
// std::cout << "file->fileExtension : " << fileExtension << std::endl;
// std::cout << "file num chunks : " << file->numChunks << std::endl;
// std::cout << "file chunk start : " << file->chunk_inds << std::endl;
return true;
}
bool deleteFile(std::string filename){
TreeNode* file = this->searchByName(filename);
if(file == nullptr){
std::cout<<"File does not exist!" <<endl;
return false;
}
size_t chunk_start=file->chunk_inds;
// std::cout << "chunk_start: " << chunk_start <<endl;
size_t chunk_end=file->numChunks;
// std::cout << "chunk_end: " << chunk_end <<endl;
for(int i=chunk_start; i<chunk_end; i++){
std::string deleteFileName = "./store/" + storePrefix + std::to_string(i);
const char* cstr_filename = deleteFileName.c_str();
if(chunkHashMap[i]==1){
if (std::remove(cstr_filename) != 0) {
std::perror("Error deleting chunks\n");
} else {
chunkHashMap[i]--;
std::cout << "chunks deleted successfully" <<endl;
}
} else {
chunkHashMap[i]--;
}
}
deleteNode(file);
return true;
}
bool updateFile(std::string filename){
TreeNode* file = this->searchByName(filename);
if(file == nullptr){
std::cout<<"File does not exist!" <<endl;
return false;
}
TreeNode* parent= file->parent;
removeFile(filename);
std::string parent_name= parent->fileName;
std::cout<< "parent_name: " << parent_name <<endl;
storeFile(filename, parent_name);
}
bool getFile(std::string filename){
//std::cout<<"Inside" <<endl;
TreeNode* file = this->searchByName(filename);
//std::cout<<"Inside" <<endl;
if(file == nullptr){
std::cout<<"File does not exist!" <<endl;
return false;
}
std::string outputFileName = filename + "_fetched";
mergeChunks(outputFileName, file);
}
bool removeFile(std::string filename){
TreeNode* file = this->searchByName(filename);
if(file == nullptr){
std::cout<<"File does not exist!" <<endl;
return false;
}
removeFileHelper(file);
}
//Run DFS to delete all subfolders/subfiles
bool removeFileHelper(TreeNode* currentFile){
if(currentFile->children.size()!=0){
for(int i=0;i<currentFile->children.size();i++){
removeFileHelper(currentFile->children[i]);
}
}
//free
delete currentFile;
return true;
}
//Writes each file node's data to a file (whose name is provided in argument .data)
void writeTreeFile(std::string treeName){
std::string fileName = treeName + ".data";
std::ofstream outFile(fileName);
if(!outFile.is_open()){
std::cout<<"Unable to open file" << std::endl;
return;
}
//Store gchunkcount
outFile << gchunkcount << '\n';
//Store chunkHashMap
for(const auto& pair : chunkHashMap){
outFile << pair.first << '\n';
outFile << pair.second << '\n';
}
//Don't need to store the root node
for(int i=0;i<this->root->children.size();i++){
this->writeTreeHelper(outFile,this->root->children[i]);
}
outFile.close();
}
void writeTreeHelper(std::ofstream& file,TreeNode* currentNode){
if(currentNode == nullptr){
return;
}
currentNode->serializeNode(file);
for(int i=0;i<currentNode->children.size();i++){
this->writeTreeHelper(file,currentNode->children[i]);
}
}
void splitFile(const std::string& inputFileName, TreeNode* currNode) {
std::ifstream inputFile(inputFileName, std::ios::binary);
if (!inputFile) {
std::cerr << "Error opening input file: " << inputFileName << std::endl;
return;
}
size_t chunkCounter = gchunkcount;
currNode->chunk_inds=gchunkcount;
char buffer[chunkSize];
while (!inputFile.eof()) {
inputFile.read(buffer, chunkSize);
std::streamsize bytesRead = inputFile.gcount();
if (bytesRead > 0) {
chunkHashMap[chunkCounter]=1;
std::string outputFileName = "./store/" + storePrefix + std::to_string(chunkCounter++);
std::ofstream outputFile(outputFileName, std::ios::binary);
outputFile.write(buffer, bytesRead);
}
}
currNode->numChunks=chunkCounter;
gchunkcount=chunkCounter;
//std::cout<<"chunkCounter: " << chunkCounter << std::endl;
//std::cout<<"gchunkcount: " << gchunkcount << std::endl;
inputFile.close();
}
void mergeChunks(const std::string& outputFileName, TreeNode* currNode) {
std::ofstream outputFile(outputFileName, std::ios::binary);
if (!outputFile) {
std::cerr << "Error opening output file: " << outputFileName << std::endl;
return;
}
size_t chunkCounter = currNode->chunk_inds;
size_t chunk_end = currNode->numChunks;
std::string inputFileName;
for(int i=chunkCounter; i<=chunk_end; i++){
inputFileName = "./store/" + storePrefix + std::to_string(i);
std::ifstream inputFile(inputFileName, std::ios::binary);
if (!inputFile) {
break; // No more chunks to read
}
outputFile << inputFile.rdbuf();
inputFile.close();
}
// do {
// inputFileName = "./store/" + storePrefix + std::to_string(chunkCounter++);
// std::ifstream inputFile(inputFileName, std::ios::binary);
// if (!inputFile) {
// break; // No more chunks to read
// }
// outputFile << inputFile.rdbuf();
// inputFile.close();
// } while (true);
outputFile.close();
}
};
FileTree readTreeFile(std::string fileName){
FileTree ft;
TreeNode* root = new TreeNode("RootFolder",0,"folder",nullptr);
ft.setRoot(root);
std::ifstream file(fileName);
if(!file.is_open()){
std::cout<<"Unable to open file" << std::endl;
return ft;
}
int mapLineCounter = 0;
int nodeLineCounter = 0;
int key;
int value;
std::string name;
std::time_t time;
size_t size;
std::string type;
std::string parentName;
long unsigned int inds;
int numberChunks;
std::string line;
if(std::getline(file,line)){
gchunkcount = stoi(line);
}
else{
std::cout<<"File is empty \n";
return ft;
}
//Read lines 1 through 2*gchunkcount+1 to populate chunkHashMap
while(mapLineCounter<gchunkcount*2 && std::getline(file,line)){
switch(mapLineCounter%2)
{
case 0: //map key
key = stoi(line);
break;
case 1: //map value
value = stoi(line);
//When we read the value of the key/value pair, insert pair to map
chunkHashMap[key] = value;
break;
}
mapLineCounter++;
}
//Read rest of lines to populate tree with nodes
while (std::getline(file,line)){
switch(nodeLineCounter%7)
{
case 0: //name
name = line;
break;
case 1: //time
time = (time_t)stoll(line);
break;
case 2: //size
size = std::stoul(line);
break;
case 3: //type
type = line;
break;
case 4: //parentName
parentName = line;
break;
case 5: //inds, folders have no chunk indicies
if(type != "folder"){
inds = stoul(line);
}
break;
case 6: //numberChunks, once we read 7 lines, we create a node
numberChunks = stoi(line);
//Create a node
TreeNode* parentNode = ft.searchByName(parentName);
if(type != "folder"){
TreeNode* currentNode = new TreeNode(name,time,size,type,parentNode,inds,numberChunks);
parentNode->addChild(currentNode);
}
else{
TreeNode* currentNode = new TreeNode(name,time,size,type,parentNode,numberChunks);
parentNode->addChild(currentNode);
}
break;
}
//std::cout << "|" <<line << "|" << '\n';
nodeLineCounter++;
}
file.close();
return ft;
}
int main() {
bool exitProgram = false;
FileTree ft;
// Creating the root node (typically a folder)
TreeNode *root = new TreeNode("RootFolder", 0, "folder", nullptr);
ft.setRoot(root);
while (!exitProgram) {
std::cout << "Enter your command: \n";
std::string userInput;
std::getline(std::cin, userInput);
// Use std::istringstream to split the input based on spaces
std::istringstream iss(userInput);
std::vector<std::string> substrings;
std::string token;
while (iss >> token) {
substrings.push_back(token);
}
int args=substrings.size();
// Output the number of substrings
// std::cout << "Number of substrings: " << substrings.size() << std::endl;
if (!substrings.empty()) {
std::string comm= substrings.at(0);
// Compare argv[1] to a string
if (substrings.at(0) == "moveFile") {
if(args<3){
std::cout << "Not enough arguments" << std::endl;
} else {
std::cout << substrings.at(0) << std::endl;
ft.moveFile(substrings.at(1), substrings.at(2));
//ft.printTree();
}
}
else if (substrings.at(0) == "copyFile") {
if(args<4){
std::cout << "Not enough arguments" << std::endl;
} else{
std::cout << substrings.at(0) << std::endl;
ft.copyFile(substrings.at(1), substrings.at(2), substrings.at(3));
}
} else if (substrings.at(0) == "createFolder") {
if(args<3){
std::cout << "Not enough arguments" << std::endl;
} else{
std::cout << substrings.at(0) << std::endl;
ft.createFolder(substrings.at(1), substrings.at(2));
}
} else if (substrings.at(0) == "deleteFile") {
if(args<2){
std::cout << "Not enough arguments" << std::endl;
} else{
std::cout << substrings.at(0) << std::endl;
ft.deleteFile(substrings.at(1));
}
} else if (substrings.at(0) == "storeFile") {
if(args<3){
std::cout << "Not enough arguments" << std::endl;
} else{
std::cout << substrings.at(0) << std::endl;
ft.storeFile(substrings.at(1), substrings.at(2));
}
} else if (substrings.at(0) == "getFile") {
if(args<2){
std::cout << "Not enough arguments" << std::endl;
} else{
std::cout << substrings.at(0) << std::endl;
ft.getFile(substrings.at(1));
}
} else if (substrings.at(0) == "printTree") {
if(args<0){
std::cout << "Not enough arguments" << std::endl;
} else{
std::cout << substrings.at(0) << std::endl;
ft.printTree();
};
} else if (substrings.at(0) == "quit") {
exitProgram = false;
break;
} else {
std::cout << "Enter a valid command." << std::endl;
}
} else {
std::cout << "Enter a valid command" << std::endl;
}
}
return 0;
}