-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualFileSystem.hpp
More file actions
459 lines (391 loc) · 15 KB
/
VirtualFileSystem.hpp
File metadata and controls
459 lines (391 loc) · 15 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
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
#ifndef SEMESTRALNIPRACE_VIRTUALFILESYSTEM_HPP
#define SEMESTRALNIPRACE_VIRTUALFILESYSTEM_HPP
#include <string>
#include <cstdint>
#include <cstdio>
#include <vector>
#include <fstream>
#include <unordered_map>
#include "Constants.hpp"
#include "Inode.hpp"
#include "Directory.hpp"
#include "Superblock.hpp"
using std::streamsize;
using std::unordered_map;
using std::fstream;
using std::string;
using std::vector;
using std::stringstream;
class VirtualFileSystem {
public:
/**
* Default constructor for virtual file system
*/
VirtualFileSystem();
/**
* Constructor for virtual file system
* @param superblock reference to superblock
* @param inodes reference to inodes
* @param dataBitmap reference to data bitmap
* @param isFormatted flag indicating whether the file system is formatted
* @param currentDir reference to current directory
* @param name name of the file system file
* @param vfsFile reference to file stream
*/
VirtualFileSystem(Superblock* superblock,
Inode* inodes,
int8_t* dataBitmap,
bool isFormatted,
Directory* currentDir,
const string& name,
fstream* vfsFile);
/**
* Constructor for virtual file system
* @param vfsName name of the file system file
*/
VirtualFileSystem(const string& vfsName);
/**
* Destructor for virtual file system
*/
~VirtualFileSystem();
/**
* Gets current path in the virtual file system (e.g. /home/user)
* @return current path in the virtual file system
*/
string getCurrentPath();
/**
* Finds directory with the given path in the virtual file system or nullptr if the directory is not found
* @param path path to the directory
* @return directory with the given path in the virtual file system or nullptr if the directory is not found
*/
Directory* findDirectory(const string& path);
/**
* Gets superblock of the virtual file system
* @return superblock of the virtual file system
*/
Superblock* getSuperblock() const;
/**
* Sets superblock of the virtual file system
* @param newSuperblock new superblock
*/
void setSuperblock(Superblock* newSuperblock);
/**
* Gets i-nodes of the virtual file system
* @return i-nodes of the virtual file system
*/
Inode* getInodes() const;
/**
* Sets i-nodes of the virtual file system
* @param newInodes new i-nodes
*/
void setInodes(Inode* newInodes);
/**
* Gets the number of blocks with indirect blocks for the given number of blocks if its needed
* @param blockCount number of blocks
* @return number of blocks with indirect blocks for the given number of blocks if its needed
*/
int getBlockCountWithIndirect(int blockCount);
/**
* Gets data bitmap of the virtual file system
* @return data bitmap of the virtual file system
*/
int8_t* getDataBitmap() const;
/**
* Sets data bitmap of the virtual file system
* @param newDataBitmap new data bitmap
*/
void setDataBitmap(int8_t* newDataBitmap);
/**
* Gets flag indicating whether the virtual file system is formatted
* @return flag indicating whether the virtual file system is formatted
*/
bool getIsFormatted() const;
/**
* Sets flag indicating whether the virtual file system is formatted
* @param newIsFormatted new flag indicating whether the virtual file system is formatted
*/
void setIsFormatted(bool newIsFormatted);
/**
* Gets current directory in the virtual file system
* @return current directory in the virtual file system
*/
Directory* getCurrentDir() const;
/**
* Sets current directory in the virtual file system
* @param newCurrentDir new current directory
*/
void setCurrentDir(Directory* newCurrentDir);
/**
* Gets name of the virtual file system file
* @return name of the virtual file system file
*/
string getName() const;
/**
* Sets name of the virtual file system file
* @param newName new name of the virtual file system file
*/
void setName(const string& newName);
/**
* Gets file stream of the virtual file system
* @return file stream of the virtual file system
*/
fstream* getVfsFile() const;
/**
* Sets file stream of the virtual file system
* @param newVfsFile new file stream of the virtual file system
*/
void setVfsFile(fstream* newVfsFile);
/**
* Prints information about the given directory item to the console in special format
* @param item directory item to print information about
*/
void printDirItemInfo(const DirectoryItem* item);
/**
* Prints information about the given indirect block to the console in special format
* @param indirectBlockAddress address of the indirect block to print information about
*/
void printIndirectBlocks(int32_t indirectBlockAddress, stringstream& ss);
/**
* Cleanup the virtual file system ( delete all directories, i-nodes, data bitmap, etc. ) and set isFormatted flag to false
*/
void cleanup();
/**
* Findes free data blocks in the virtual file system and returns them
* @param count number of blocks to find
* @return vector of free data blocks
*/
vector<int32_t> findFreeDataBlocks(int count);
/**
* Finds free i-node in the virtual file system and returns its id or -1 if there is no free i-node
* @return free i-node id or -1 if there is no free i-node
*/
int32_t findFreeInode();
/**
* Gets vector of data blocks of the given i-node in the virtual file system and returns it
* @param nodeid id of the i-node
* @param block_count number of blocks
* @param rest rest of the blocks
* @return vector of data blocks of the given i-node in the virtual file system
*/
vector<int32_t> getDataBlocks(int32_t nodeid, int* block_count, int* rest);
/**
* Adds direct blocks of the given i-node in the virtual file system to the given vector
* @param node i-node
* @param blocks vector of blocks
*/
void addDirectBlocks(const Inode& node, vector<int32_t>& blocks);
/**
* Adds indirect blocks of the given i-node in the virtual file system to the given vector
* @param indirect_block_address address of the indirect block
* @param blocks vector of blocks
* @param max_blocks max number of blocks
*/
void addIndirectBlocks(int32_t indirect_block_address, vector<int32_t>& blocks, int max_blocks);
/**
* Fills direct blocks of the given i-node in the virtual file system to the given vector
* @param node i-node
* @param blocks vector of blocks
*/
void fillDirectBlocks(const Inode& node, vector<int32_t>& blocks);
/**
* Fills indirect blocks of the given i-node in the virtual file system to the given vector
* @param node i-node
* @param blocks vector of blocks
* @param block_count number of blocks
*/
void fillIndirectBlocks(const Inode& node, vector<int32_t>& blocks, int block_count);
/**
* Writes the given i-node to the virtual file system file
* @param id id of the i-node
*/
void writeInodeToVfs(int id);
/**
* Updates bitmap in the virtual file system file for the given directory item with the given value and data blocks
* @param item directory item to update bitmap for
* @param value value to update bitmap with
* @param dataBlocks data blocks to update bitmap with
*/
void updateBitmapInFile(DirectoryItem* item, int8_t value, vector<int32_t> const& dataBlocks);
/**
* Updates indirect blocks in bitmap in the virtual file system file for the given indirect block with the given value
* @param indirectBlock indirect block to update in bitmap
* @param value value to update indirect block in bitmap with
*/
void updateIndirectBlocksInBitmap(int32_t indirectBlock, int8_t value);
/**
* Formats the virtual file system with the given size and returns true if the virtual file system was formatted successfully, false otherwise
* @param filesystemSize size of the virtual file system
* @return true if the virtual file system was formatted successfully, false otherwise
*/
bool format(int32_t filesystemSize);
/**
* Writes superblock to the virtual file system file or throws an exception if the file is not open
*/
void writeSuperblock();
/**
* Flushes the virtual file system file ( saves changes )
*/
void flushVfs();
/**
* Frees the i-node with the given id in the virtual file system or throws an exception if the id is invalid ( initialized with ID_ITEM_FREE )
* @param id id of the i-node to free
*/
void freeInode(int id);
/**
* Seeks the data cluster for a given block number.
* @param blockNumber The block number to seek.
* @return 0 if successful, -1 on error.
*/
int seekDataCluster(int blockNumber) const;
/**
* Seeks to a specific offset in the virtual file system file.
* @param offset The offset to seek to.
* @return 0 if successful, -1 on error.
*/
int seekSet(long int offset) const;
/**
* Seeks to a specific offset relative to the current position in the file.
* @param offset The offset to seek to.
* @return 0 if successful, -1 on error.
*/
int seekCur(long int offset);
/**
* Rewinds the virtual file system file to the beginning.
*/
void rewindVfs() const;
/**
* Reads a value from a file and sets it in the superblock.
* @tparam T The type of the value to read.
* @param file The file to read from.
* @param superblock The superblock to set the value in.
* @param setter A pointer to a setter function in the superblock.
*/
template<typename T> void readAndSet(fstream& file, Superblock& superblock, void(Superblock::*setter)(T));
/**
* Writes data to the virtual file system file.
* @tparam T The type of data to write.
* @param ptr A pointer to the data to write.
* @param count The number of elements to write.
* @return The number of elements written.
*/
template<typename T> streamsize writeToFile(const T* ptr, size_t count = 1);
/**
* Reads data from the virtual file system file.
* @tparam T The type of data to read.
* @param ptr A pointer to the buffer to read data into.
* @param count The number of elements to read.
* @return The number of elements read.
*/
template<typename T> streamsize readFromFile(T* ptr, size_t count = 1);
/**
* Creates directory in the virtual file system file with the given name in the given parent directory
* @param dir directory to create
* @param item directory item to create
* @return true if the directory was created successfully, false otherwise
*/
int createDirectoryInFile(Directory* dir, DirectoryItem* item);
/**
* Deletes a directory item from the virtual file system file, including updating the associated data blocks
* and the directory's inode
* @param dir The directory containing the item to remove.
* @param item The directory item to be removed.
* @return 0 if the item was successfully removed, -1 otherwise.
*/
int removeDirectoryFromFile(Directory* dir, DirectoryItem* item);
/**
* Create or remove directory in file
* @param dir pointer to directory
* @param item pointer to directory item
* @param create true if create, false if remove
* @return 0 if success, -1 if error
*/
int updateDirectoryInFile(Directory* dir, DirectoryItem* item, bool create);
/**
* Clear indirect blocks of inode by id
* @param inodeId id of inode to clear indirect blocks
*/
void clearIndirectBlocks(int32_t inodeId);
/**
* Remove file from directory and from file system ( including linking links )
* @param parentDir directory where file is located
* @param item file to remove
* @return true if file was removed successfully, false otherwise
*/
bool removeFile(Directory* parentDir, DirectoryItem* item);
/**
* Get parent directory of item (file or directory)
* @param item item to find parent directory
* @return parent directory of item
*/
Directory* getParentDirectory(DirectoryItem* item);
/**
* Initializes i-node in the virtual file system with the given parameters
* @param inodeId id of the i-node
* @param size size of file
* @param blockCount number of blocks
* @param blocks vector of blocks
* @return last data block
*/
int32_t initializeInode(int32_t inode_id, int32_t size, int block_count, vector<int32_t>& blocks);
/**
* Updates the directory in the virtual file system file
* @param dir directory to update in the virtual file system file
* @param size size of the directory to update in the virtual file system file
*/
void updateSizesInFile(Directory* dir, int32_t size);
/**
* Write inode to file
* @param ptr pointer to inode with data to write
*/
void writeInodeToFile(Inode* ptr);
/**
* Read inode from file
* @param ptr pointer to inode the read data to store
* @param count number of inodes to read
*/
void readInodeFromFile(Inode* ptr, size_t count = 1);
/**
* Loads the directory from the virtual file system
* @param dir directory to load
* @param id id of the directory
*/
void loadDirectoryFromVfs(Directory* dir, int id);
/**
* Loads the virtual file system from the file
*/
void loadVfs();
/**
* Gets all directories in the virtual file system ( map<int, Directory*> )
* @return all directories in the virtual file system ( map<int, Directory*> )
*/
unordered_map<int, Directory*>& getAllDirectories();
/**
* Gets the directory with the given id in the virtual file system or nullptr if the id is invalid
* @param id id of the directory
* @return directory with the given id or nullptr if the id is invalid
*/
Directory* getDirectory(int32_t id);
/**
* Adds the directory to the virtual file system
* @param dir directory to add
* @param index index of the directory
*/
void addDirectory(Directory* dir, int32_t index);
/**
* Removees directory item from the virtual file system file by name
* @param parentDir parent directory of the directory item to remove
* @param name name of the directory item to remove
* @return true if the directory item was removed successfully, false otherwise
*/
bool removeDirectory(Directory* parentDir, const string& name);
private:
Superblock* superblock;
Inode* inodes;
int8_t* dataBitmap;
bool isFormatted;
Directory* currentDir;
unordered_map<int, Directory*> allDirs;
string name;
fstream* vfsFile;
};
#endif //SEMESTRALNIPRACE_VIRTUALFILESYSTEM_HPP