-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtfs.cpp
405 lines (307 loc) · 7.3 KB
/
virtfs.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
#include "virtfs.hpp"
using namespace std;
// Virtual FS class constructor
// Takes in
// Permissions string
// Pointer to parent folder
// Date string,
// Folder name,
// Owner
// Group
cvirtfs::cvirtfs(string perms, cvirtfs *vfs_parent, string date, string name, string owner, string group)
{
if(name.empty())
name = string("/");
this->vfs_name = name;
this->vfs_owner = owner;
this->vfs_group = group;
this->vfs_parent = vfs_parent;
this->vfs_date = date;
this->vfs_perms = perms;
this->vfs_folders.reserve(10);
this->vfs_files.reserve(10);
}
// Wrapper to make a call to cvirtfs and apply parent folder.
// Only if folder doesn't exist.
bool cvirtfs::make_folder(string perms, string name, string date, cvirtfs **out, string owner, string group)
{
if(!this->folder_exists(name))
{
cvirtfs *newfolder = new cvirtfs(perms, this, date, name, owner, group);
this->vfs_folders.push_back(newfolder);
if(out)
*out = this->vfs_folders.back();
} else
return false;
return true;
}
// Recursively remove folders and files.
// Goes top down
bool cvirtfs::remove_folder()
{
for(auto i = this->vfs_files.begin(); i != this->vfs_files.end(); i++)
delete (*i);
for(auto i = this->vfs_folders.begin(); i != this->vfs_folders.end(); i++)
(*i)->remove_folder();
return true;
}
// Rename a folder if it doesn't already exist.
bool cvirtfs::rename_folder(string name, cvirtfs *folder)
{
if(!name.empty() && folder)
return folder->rename_folder(name);
if(!name.empty() && !this->folder_exists(name))
this->vfs_name = name;
else
return false;
return true;
}
// Return folder string
string cvirtfs::get_folder_name()
{
return this->vfs_name;
}
// Find a folder, not recursive
cvirtfs *cvirtfs::find_folder(string name)
{
cvirtfs *ret = nullptr;
if(name.empty())
return ret;
for(auto &i : this->vfs_folders)
{
if(!i->vfs_name.compare(name))
{
ret = i;
break;
}
}
return ret;
}
// Does a folder exist
// I guess it's pointless as we can use the function find_folder...
bool cvirtfs::folder_exists(string name)
{
bool ret = false;
if(name.empty())
return ret;
for(auto &i : this->vfs_folders)
{
if(!i->vfs_name.compare(name))
{
ret = true;
break;
}
}
return ret;
}
// Make a new file and add it to the vector of the parent folder.
bool cvirtfs::make_file(string perms, string filename, int filebytes, string date, string owner, string group, svirtfd **out)
{
if(!filename.empty() && !this->file_exists(filename))
{
svirtfd *newfile = new svirtfd;
newfile->filename = filename;
newfile->fileowner = owner;
newfile->filegroup = group;
newfile->filebytes = filebytes;
newfile->filedate = date;
newfile->parent = (void *)this;
newfile->fileperm = perms;
this->vfs_files.push_back(newfile);
if(out)
*out = this->vfs_files.back();
} else
return false;
return true;
}
// Delete a file.
bool cvirtfs::remove_file(string filename)
{
for(auto i = this->vfs_files.begin(); i != this->vfs_files.end();)
{
if(!(*i)->filename.compare(filename))
{
svirtfd *erase = *i;
i = this->vfs_files.erase(i);
delete erase;
return true;
} else
i++;
}
return false;
}
// Rename a file
bool cvirtfs::rename_file(string filename, string new_filename)
{
bool ret = false;
if(new_filename.empty() || this->file_exists(new_filename))
return ret;
svirtfd *find = nullptr;
if((find = this->find_file(filename)))
{
find->filename = filename;
ret = true;
}
return ret;
}
// Find a file, not recursive.
svirtfd *cvirtfs::find_file(string filename)
{
svirtfd *ret = nullptr;
for(auto &i : this->vfs_files)
{
if(!i->filename.compare(filename))
{
ret = i;
break;
}
}
return ret;
}
// Again kinda pointless because of find_file...
bool cvirtfs::file_exists(string filename)
{
bool ret = false;
for(auto &i : this->vfs_files)
{
if(!i->filename.compare(filename))
{
ret = true;
break;
}
}
return ret;
}
// Return the vector of folders.
vector<cvirtfs *> cvirtfs::get_all_folders()
{
return this->vfs_folders;
}
// Return the vector of files.
vector<svirtfd *> cvirtfs::get_all_files()
{
return this->vfs_files;
}
// Return parent
cvirtfs *cvirtfs::get_parent()
{
return this->vfs_parent;
}
// Is this the root? Is parent null?
bool cvirtfs::is_root_dir()
{
return (this->vfs_parent ? false : true);
}
// Recursive until we hit root dir.
void cvirtfs::get_root(cvirtfs **out)
{
if(!this->is_root_dir())
this->get_parent()->get_root(out);
else
*out = this;
}
// Recursive addition to path until root.
void cvirtfs::get_full_path(void *item, bool bfolder, string *full_path_out)
{
if(!full_path_out || !item)
return;
cvirtfs *folder = nullptr;
if(bfolder)
folder = (cvirtfs *)item;
else {
folder = (cvirtfs *)(((svirtfd *)item)->parent);
bfolder = !bfolder;
}
bool broot = folder->is_root_dir();
string fol = folder->get_folder_name();
if(broot)
*full_path_out = (fol + *full_path_out);
else
*full_path_out = (fol + "/" + *full_path_out);
if(broot)
return;
this->get_full_path((void *)folder->get_parent(), bfolder, full_path_out);
}
// Return folder date
string cvirtfs::get_folder_date()
{
return this->vfs_date;
}
// Return folder permissions
string cvirtfs::get_folder_perm()
{
return this->vfs_perms;
}
// Return folder owner
string cvirtfs::get_folder_owner()
{
return this->vfs_owner;
}
// Return folder group
string cvirtfs::get_folder_group()
{
return this->vfs_group;
}
// Debug function to print the tree, with depth.
void cvirtfs::printtree(cvirtfs *folder, int depth, bool print_files)
{
if(folder->is_root_dir())
{
cout << "[d] - " << folder->get_folder_name() << endl;
depth++;
}
for(auto &k : folder->get_all_files())
{
for(int j = 0; j < depth; j++)
cout << "\t";
cout << "[f] - " << k->filename << endl;
}
for(auto &i : folder->get_all_folders())
{
for(int j = 0; j < depth; j++)
cout << "\t";
cout << "[d] - " << i->get_folder_name() << endl;
this->printtree(i, (depth + 1));
}
}
/*void cvirtfs::find(cvirtfs *folder, string match, findparams params, find_callback cbfunc)
{
if(!folder || !cbfunc || match.empty())
return;
static vector<cvirtfs *> fnd_folders;
static vector<svirtfd *> fnd_files;
if(params == FIND_BOTH || params == FIND_FILE)
for(auto &i : folder->get_all_files())
{
if(i->filename.find(match) != string::npos)
fnd_files.push_back(i);
}
if(params == FIND_BOTH || params == FIND_FOLDER)
for(auto &i : folder->get_all_folders())
{
if(i->get_folder_name().find(match) != string::npos)
fnd_folders.push_back(i);
}
cbfunc(fnd_folders, fnd_files);
fnd_folders.clear();
fnd_files.clear();
for(auto &i : folder->get_all_folders())
this->find(i, match, params, cbfunc);
}
void find_callbackfunc(vector<cvirtfs *> fnd_folders, vector<cvirtfs::svirtfd *> fnd_files)
{
string full_path;
for(auto &i : fnd_folders)
{
full_path.clear();
i->get_full_path(i, true, &full_path);
cout << "[x] Found folder: " << full_path << endl;
}
for(auto &i : fnd_files)
{
full_path.clear();
((cvirtfs *)((cvirtfs::svirtfd *)i)->parent)->get_full_path(i, false, &full_path);
cout << "[x] Found file: " << (full_path + i->filename + i-> file_ext) << endl;
}
}
*/