-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualFileSystem.cpp
More file actions
444 lines (400 loc) · 15 KB
/
VirtualFileSystem.cpp
File metadata and controls
444 lines (400 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
// VirtualFileSystem.cpp
#include "VirtualFileSystem.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <cstdio> // for remove()
#include <algorithm>
#include <utility> // for std::move
using namespace std;
// Special FAT markers (constexpr was suggested by CLANG-Tidy)
static constexpr int32_t FAT_FREE = 0; // Block is free
static constexpr int32_t FAT_EOF = -1; // End-of-file chain
static constexpr int32_t FAT_RESERVED = -2; // Reserved for metadata (not usable by files)
// Constructor: initialize internal structures
VirtualFileSystem::VirtualFileSystem(std::string diskPath)
: diskPath(std::move(diskPath)), directory(MAX_FILES) {
// Reserve directory entries.
}
// Destructor: close disk file if open
VirtualFileSystem::~VirtualFileSystem() {
if (disk.is_open()) {
disk.close(); // Just close the fstream, nothing fancy
}
}
// Create a new VD file and initialize filesystem structures
bool VirtualFileSystem::createDisk(uint32_t diskSize) {
// Adjust disk size to a multiple of BLOCK_SIZE
// So, if the user specified 1000 bytes, it will be rounded up to 1024
if (diskSize == 0) {
diskSize = DEFAULT_DISK_SIZE;
}
if (diskSize % BLOCK_SIZE != 0) {
diskSize = ((diskSize / BLOCK_SIZE) + 1) * BLOCK_SIZE;
}
// Create and size the file
ofstream out(diskPath, ios::binary | ios::trunc);
if (!out) {
cerr << "Error: Cannot create disk file '" << diskPath << "'\n";
return false;
}
// Seek to last byte and write a zero to allocate space
out.seekp(diskSize - 1);
constexpr char zero = '\0';
out.write(&zero, 1);
out.close();
// Open the file for read/write access
disk.open(diskPath, ios::binary | ios::in | ios::out);
if (!disk) {
cerr << "Error: Cannot open disk file after creation\n";
return false;
}
// Initialize superblock
memset(&sb, 0, sizeof(sb)); // Clear the superblock structure
memcpy(sb.fsName, FS_NAME, sizeof(sb.fsName));
sb.blockSize = BLOCK_SIZE;
sb.totalBlocks = diskSize / BLOCK_SIZE;
sb.totalDirEntries = MAX_FILES;
sb.dirStartBlock = 1;
// Calculate blocks for directory
constexpr uint32_t dirBytes = MAX_FILES * sizeof(DirEntry);
sb.dirBlockCount = (dirBytes + BLOCK_SIZE - 1) / BLOCK_SIZE;
sb.fatStartBlock = sb.dirStartBlock + sb.dirBlockCount;
// Calculate blocks for FAT (one int32 per block)
const uint32_t fatBytes = sb.totalBlocks * sizeof(int32_t);
sb.fatBlockCount = (fatBytes + BLOCK_SIZE - 1) / BLOCK_SIZE;
sb.dataStartBlock = sb.fatStartBlock + sb.fatBlockCount;
// Write superblock to disk (block 0)
writeSuperblock();
// Initialize and write empty directory entries
for (auto &entry: directory) {
memset(&entry, 0, sizeof(DirEntry));
}
writeDirectory();
// Initialize FAT: mark metadata blocks reserved and others free
FAT.assign(sb.totalBlocks, FAT_FREE);
for (uint32_t i = 0; i < sb.dataStartBlock; i++) {
FAT[i] = FAT_RESERVED;
}
writeFAT();
disk.close();
cout << "Virtual disk '" << diskPath << "' created ("
<< diskSize << " bytes, " << sb.totalBlocks << " blocks).\n";
return true;
}
// Load an existing virtual disk (read superblock, directory, FAT into memory)
bool VirtualFileSystem::loadDisk() {
disk.open(diskPath, ios::binary | ios::in | ios::out);
if (!disk) {
cerr << "Error: Cannot open virtual disk '" << diskPath << "'\n";
return false;
}
if (!readSuperblock()) {
cerr << "Error: Invalid or corrupt superblock\n";
disk.close();
return false;
}
readDirectory();
readFAT();
return true;
}
// Read superblock from disk
bool VirtualFileSystem::readSuperblock() {
disk.seekg(0);
disk.read(reinterpret_cast<char *>(&sb), sizeof(sb));
// Verify filesystem identifier
if (strncmp(sb.fsName, FS_NAME, strlen(FS_NAME)) != 0) {
return false;
}
return true;
}
// Write superblock to disk (block 0)
bool VirtualFileSystem::writeSuperblock() {
disk.seekp(0);
disk.write(reinterpret_cast<char *>(&sb), sizeof(sb));
// Pad remaining bytes of block with zeros, if any
if constexpr (sizeof(sb) < BLOCK_SIZE) {
const vector<char> pad(BLOCK_SIZE - sizeof(sb), 0);
disk.write(pad.data(), pad.size());
}
return disk.good();
}
// Read directory entries from disk
bool VirtualFileSystem::readDirectory() {
directory.assign(MAX_FILES, DirEntry());
disk.seekg(sb.dirStartBlock * BLOCK_SIZE);
disk.read(reinterpret_cast<char *>(directory.data()), MAX_FILES * sizeof(DirEntry));
return disk.good();
}
// Write directory entries to disk
bool VirtualFileSystem::writeDirectory() {
disk.seekp(sb.dirStartBlock * BLOCK_SIZE);
disk.write(reinterpret_cast<char *>(directory.data()), MAX_FILES * sizeof(DirEntry));
// Pad the rest of directory blocks
constexpr uint32_t usedBytes = MAX_FILES * sizeof(DirEntry);
if (const uint32_t totalBytes = sb.dirBlockCount * BLOCK_SIZE; usedBytes < totalBytes) {
const vector<char> pad(totalBytes - usedBytes, 0);
disk.write(pad.data(), pad.size());
}
return disk.good();
}
// Read FAT from disk
bool VirtualFileSystem::readFAT() {
FAT.assign(sb.totalBlocks, FAT_FREE);
disk.seekg(sb.fatStartBlock * BLOCK_SIZE);
disk.read(reinterpret_cast<char *>(FAT.data()), sb.totalBlocks * sizeof(int32_t));
return disk.good();
}
// Write FAT to disk
bool VirtualFileSystem::writeFAT() {
disk.seekp(sb.fatStartBlock * BLOCK_SIZE);
disk.write(reinterpret_cast<char *>(FAT.data()), sb.totalBlocks * sizeof(int32_t));
// Pad the rest of FAT blocks
const uint32_t usedBytes = sb.totalBlocks * sizeof(int32_t);
if (const uint32_t totalBytes = sb.fatBlockCount * BLOCK_SIZE; usedBytes < totalBytes) {
const vector<char> pad(totalBytes - usedBytes, 0);
disk.write(pad.data(), pad.size());
}
return disk.good();
}
// Find 'count' free blocks in FAT; return true if found and fill 'blocks' vector
bool VirtualFileSystem::findFreeBlocks(const uint32_t count, vector<int32_t> &blocks) const {
blocks.clear();
for (uint32_t i = sb.dataStartBlock; i < sb.totalBlocks && blocks.size() < count; ++i) {
if (FAT[i] == FAT_FREE) {
blocks.push_back(i);
}
}
return (blocks.size() == count);
}
// Find directory entry index by file name; return -1 if not found
int VirtualFileSystem::findDirectoryEntry(const string &name) const {
for (int i = 0; i < static_cast<int>(directory.size()); ++i) {
if (directory[i].name[0] != '\0' && name == directory[i].name) {
return i;
}
}
return -1;
}
// Copy a host file into the virtual disk
bool VirtualFileSystem::copyFromHost(const std::string &hostFile) {
// Determine file name (strip path)
size_t pos = hostFile.find_last_of("/\\");
string fname = (pos == string::npos ? hostFile : hostFile.substr(pos + 1));
// Check if file already exists in VFS
if (findDirectoryEntry(fname) >= 0) {
cerr << "Error: File '" << fname << "' already exists in virtual disk\n";
return false;
}
// Check directory capacity
int freeSlot = -1;
for (int i = 0; i < MAX_FILES; ++i) {
if (directory[i].name[0] == '\0') {
freeSlot = i;
break;
}
}
if (freeSlot < 0) {
cerr << "Error: Directory is full (max " << MAX_FILES << " files)\n";
return false;
}
// Open host file
ifstream in(hostFile, ios::binary | ios::ate);
if (!in) {
cerr << "Error: Cannot open host file '" << hostFile << "'\n";
return false;
}
streamsize fileSize = in.tellg();
in.seekg(0, ios::beg);
if (fileSize <= 0) {
cerr << "Error: Host file is empty or unreadable\n";
in.close();
return false;
}
// Calculate blocks needed
auto blocksNeeded = static_cast<uint32_t>((fileSize + BLOCK_SIZE - 1) / BLOCK_SIZE);
vector<int32_t> blocks;
if (!findFreeBlocks(blocksNeeded, blocks)) {
cerr << "Error: Not enough free space on virtual disk\n";
in.close();
return false;
}
// Fill directory entry
DirEntry &entry = directory[freeSlot];
memset(&entry, 0, sizeof(DirEntry));
strncpy(entry.name, fname.c_str(), sizeof(entry.name) - 1);
entry.size = static_cast<uint64_t>(fileSize);
entry.created = time(nullptr);
entry.type = 'F'; // Just means file. This is a placeholder for future types, such as (sub)directories
entry.firstBlock = blocks[0];
// Update FAT for the allocated blocks
for (uint32_t i = 0; i < blocksNeeded; ++i) {
int blk = blocks[i];
if (i == blocksNeeded - 1) {
FAT[blk] = FAT_EOF;
} else {
FAT[blk] = blocks[i + 1];
}
}
// Write file data into data blocks
char buffer[BLOCK_SIZE];
for (uint32_t i = 0; i < blocksNeeded; ++i) {
int blk = blocks[i];
disk.seekp((uint64_t) blk * BLOCK_SIZE);
// Compute bytes to read for this block
uint32_t bytesToRead = static_cast<uint32_t>(min(static_cast<streamsize>(BLOCK_SIZE),
fileSize - static_cast<streamsize>(i) * BLOCK_SIZE));
in.read(buffer, bytesToRead);
disk.write(buffer, bytesToRead);
// Pad remainder of block with zeros if last block not full
if (bytesToRead < BLOCK_SIZE) {
vector<char> pad(BLOCK_SIZE - bytesToRead, 0);
disk.write(pad.data(), pad.size());
}
}
in.close();
// Save updated metadata (directory and FAT)
writeDirectory();
writeFAT();
cout << "Copied '" << fname << "' (" << fileSize << " bytes) to virtual disk.\n";
return true;
}
// Copy a file from the virtual disk to host filesystem
bool VirtualFileSystem::copyToHost(const std::string &fileName, const std::string &destPath) {
const int idx = findDirectoryEntry(fileName);
if (idx < 0) {
cerr << "Error: File '" << fileName << "' not found in virtual disk\n";
return false;
}
const DirEntry &entry = directory[idx];
// Determine output path
const string outPath = destPath.empty() ? fileName : destPath;
ofstream out(outPath, ios::binary);
if (!out) {
cerr << "Error: Cannot create host file '" << outPath << "'\n";
return false;
}
// Follow FAT chain and write blocks
int32_t blk = entry.firstBlock;
uint64_t remaining = entry.size;
char buffer[BLOCK_SIZE];
while (blk != FAT_EOF && remaining > 0) {
disk.seekg((uint64_t) blk * BLOCK_SIZE);
const uint32_t toRead = static_cast<uint32_t>(min(static_cast<uint64_t>(BLOCK_SIZE), remaining));
disk.read(buffer, toRead);
out.write(buffer, toRead);
remaining -= toRead;
blk = (blk < 0 ? FAT_EOF : FAT[blk]);
}
out.close();
cout << "Copied '" << fileName << "' from virtual disk to '" << outPath << "'.\n";
return true;
}
// Delete a file from the virtual disk
bool VirtualFileSystem::deleteFile(const std::string &fileName) {
const int idx = findDirectoryEntry(fileName);
if (idx < 0) {
cerr << "Error: File '" << fileName << "' not found in virtual disk\n";
return false;
}
DirEntry &entry = directory[idx];
// Free all blocks in the file's chain
int32_t blk = entry.firstBlock;
while (blk != FAT_EOF && blk != FAT_RESERVED) {
const int32_t next = FAT[blk];
FAT[blk] = FAT_FREE;
blk = next;
}
// Mark directory entry as unused
entry.name[0] = '\0';
entry.size = 0;
entry.firstBlock = 0;
writeDirectory();
writeFAT();
cout << "Deleted file '" << fileName << "' from virtual disk.\n";
return true;
}
// List all files in the virtual disk directory
void VirtualFileSystem::listFiles() const {
cout << left << setw(20) << "Name"
<< right << setw(10) << "Size" << " "
<< left << "Created Type\n";
cout << string(20 + 10 + 2 + 19 + 6, '-') << "\n";
bool any = false;
for (const auto &entry: directory) {
if (entry.name[0] == '\0') continue;
any = true;
// Format creation time
const std::tm *tm_info = std::localtime(&entry.created);
char timestr[20];
std::strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", tm_info);
cout << left << setw(20) << entry.name
<< right << setw(10) << entry.size << " "
<< left << timestr << " "
<< entry.type << "\n";
}
if (!any) {
cout << "(no files)\n";
}
}
// Show the occupancy map of blocks on the virtual disk
void VirtualFileSystem::showMap() const {
cout << "Range | Type | Status\n";
cout << "-----------------------------------------------\n";
auto describe_block = [&](uint32_t i) -> pair<string, string> {
if (i == 0) return {"Superblock", "occupied"};
else if (i >= sb.dirStartBlock && i < sb.dirStartBlock + sb.dirBlockCount)
return {"Directory", "occupied"};
else if (i >= sb.fatStartBlock && i < sb.fatStartBlock + sb.fatBlockCount)
return {"FAT", "occupied"};
else {
if (FAT[i] == FAT_FREE) return {"Free", "free"};
else {
string fname;
for (const auto &entry: directory) {
if (entry.name[0] == '\0') continue;
int32_t blk = entry.firstBlock;
while (blk != FAT_EOF && blk >= 0) {
if (static_cast<uint32_t>(blk) == i) {
fname = entry.name;
break;
}
blk = FAT[blk];
}
if (!fname.empty()) break;
}
return !fname.empty()
? make_pair("File(" + fname + ")", "occupied")
: make_pair("Unknown", "occupied");
}
}
};
uint32_t start = 0;
auto [currType, currStatus] = describe_block(0);
for (uint32_t i = 1; i < sb.totalBlocks; ++i) {
if (auto [type, status] = describe_block(i); type != currType || status != currStatus) {
cout << setw(4) << start << "-" << setw(4) << i - 1 << " | "
<< setw(13) << currType << " | " << currStatus << "\n";
start = i;
currType = type;
currStatus = status;
}
}
// Final group
cout << setw(4) << start << "-" << setw(4) << sb.totalBlocks - 1 << " | "
<< setw(13) << currType << " | " << currStatus << "\n";
}
// Delete the virtual disk file
bool VirtualFileSystem::removeDisk() {
if (disk.is_open()) {
disk.close();
}
if (std::remove(diskPath.c_str()) != 0) {
cerr << "Error: Could not delete disk '" << diskPath << "'\n";
return false;
}
cout << "Deleted virtual disk '" << diskPath << "'.\n";
return true;
}