-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
190 lines (171 loc) · 6.1 KB
/
Copy pathfilesystem.cpp
File metadata and controls
190 lines (171 loc) · 6.1 KB
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
#include "filesystem.hpp"
FileSystem::FileSystem()
{
root = new Node("/root", 'd');
currentDirectory = root; // current directory alternates depending on where it is currently
address = root->getName();
}
FileSystem::~FileSystem()
{
deleteNode(root);
}
void FileSystem::deleteNode(Node* node)
{
if(node != nullptr)
{
std::vector<Node*> children = node->getChildren();
for(Node* child : children)
{
deleteNode(child);
}
delete node;
}
}
void FileSystem::addNode(Node *newNode)
{
currentDirectory->addChild(newNode); // add a file or directory to root
}
Node *FileSystem::findNode(std::string name)
{
std::vector<Node *> cd = currentDirectory->getChildren(); // get files and directories from current directory, traverse and return node that matches
for (Node *item : cd)
{
if (item->getName() == name) // if item's name is equal to parameter name, return that Node* object.
return item;
}
return nullptr; // Node not found
}
std::string FileSystem::pwd()
{
return root->getName();
}
std::string FileSystem::ls()
{
std::vector<Node *> cd = currentDirectory->getChildren(); // gets the children of the directory that currentDirectory is pointing at
std::string formString = "\0";
if (!cd.empty()) // if current directory is not empty, form the string
{
for (Node *item : cd)
{
formString += item->getType();
formString += " ";
formString += item->getName();
formString += "\n";
}
} // if it's empty, just make it empty
else
formString = "";
return formString;
}
std::string FileSystem::touch(std::string itemName)
{
// std::cout << "hello\n";
std::vector<Node *> cd = currentDirectory->getChildren();
if (!cd.empty()) // if the directory is not empty
{
for (Node *item : cd) // check the directory for the itemName and see if there's already one.
{
if (item->getName() == itemName) // if the itemName exists whether it's a file or a directory print to console that that item already exists
{
return "Error: " + itemName + " exists"; // cd has that file already
}
}
}
// else, cd is empty or the itemName can't be found, do this:
Node *newItem = new Node(itemName, 'f'); // f for file
addNode(newItem); // add the item to the current directory
return "file " + itemName + " created successfully";
}
std::string FileSystem::mkdir(std::string itemName)
{
std::vector<Node *> cd = currentDirectory->getChildren();
if (!cd.empty()) // if the directory is not empty
{
for (Node *item : cd) // check the directory for the itemName and see if there's already one.
{
if (item->getName() == itemName) // if the itemName exists whether it's a file or a directory print to console that that item already exists
{
return "Error: " + itemName + " exists"; // cd has that file already
}
}
}
// else, cd is empty or the itemName can't be found, do this:
Node *newItem = new Node(itemName, 'd'); // d for directory
addNode(newItem); // add the item to the current directory
return "directory " + itemName + " created successfully";
}
std::string FileSystem::cd(std::string dirName) // cd for change directory
{
std::vector<Node *> cd = currentDirectory->getChildren(); // Look at current directory
bool movedInside = false;
bool movedOutside = false;
// move out of current directory:
if (dirName == "..")
{
// move out of the directory
if (currentDirectory->getParent() == nullptr) // if there's nothing to move out of
{
return "can't change to directory ..";
}
else
{
currentDirectory = currentDirectory->getParent();
movedOutside = true;
}
}
// change directory:
for (Node *item : cd)
{
if (item->getType() == 'd' && item->getName() == dirName) // if this item is a directory and it matches the parameter, set this directory
{
currentDirectory = item; // point currentDirectory to current item inside cd
movedInside = true;
}
else if (item->getType() == 'f' && item->getName() == dirName)
{
return dirName + ": is not a directory";
}
}
if (movedInside)
{
address += "/";
address += dirName;
root->setName(address);
return address;
}
if (movedOutside)
{
char delimiter = '/'; // when moving out, delete the path
std::size_t pos = address.find_last_of(delimiter);
if (pos != std::string::npos)
{ // Erase everything from the last delimiter to the end of the string
address.erase(pos);
}
root->setName(address);
return address;
}
return dirName + ": no such directory"; // traversed the entire vector and did not find the directory to change to
}
std::string FileSystem::mv(std::string currName, std::string newName)
{
std::vector<Node *> cd = currentDirectory->getChildren(); // get all items in current directory
// check current directory for a match of currName
for (Node *item : cd)
{
if (currName == item->getName()) // item found
{
item->setName(newName);
return "file/dir renamed successfully";
}
}
// item is not found if it finishes the loop and the if statement did not execute.
return "file not found";
}
std::string FileSystem::rm(std::string dataToRemove)
{
bool success = currentDirectory->removeChild(dataToRemove);
if(success)
return dataToRemove + " removed successfully";
//item is not found if it finishes the loop and the if statement did not execute.
return "No such file or directory";
}